root/OpenSceneGraph/trunk/src/osgPlugins/ive/Text.cpp @ 13041

Revision 13041, 7.5 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 *    FILE:            Text.cpp
4 *
5 *    DESCRIPTION:    Read/Write osgText::Text in binary format to disk.
6 *
7 *    CREATED BY:        Auto generated by iveGenerator
8 *                    and later modified by Rune Schmidt Jensen.
9 *
10 *    HISTORY:        Created 27.3.2003
11 *
12 *    Copyright 2003 VR-C
13 **********************************************************************/
14
15#include "Exception.h"
16#include "Text.h"
17#include "Drawable.h"
18#include "Object.h"
19
20#include <osgDB/FileUtils>
21#include <osgDB/FileNameUtils>
22#include <osg/Notify>
23
24using namespace ive;
25
26void Text::write(DataOutputStream* out){
27    // Write Text's identification.
28    out->writeInt(IVETEXT);
29    // If the osg class is inherited by any other class we should also write this to file.
30    osg::Object*  obj = dynamic_cast<osg::Object*>(this);
31    if(obj){
32        ((ive::Drawable*)(obj))->write(out);
33    }
34    else
35        out_THROW_EXCEPTION("Text::write(): Could not cast this osgText::Text to an osg::Drawable.");
36
37    // Write Text's properties.
38    if( getFont() )
39    {
40        std::string fname = getFont()->getFileName();
41
42        if(!fname.empty())
43        {
44            if(out->getUseOriginalExternalReferences())
45            {
46                out->writeString(fname); //Saving file name with local directory
47            }
48            else
49            {
50                out->writeString(osgDB::getSimpleFileName(fname)); //Saving original file name
51            }
52        }
53        else
54            out->writeString(""); //Blank string
55    }
56    else
57        out->writeString(""); //Blank string
58
59    out->writeUInt(getFontWidth());
60    out->writeUInt(getFontHeight());
61    out->writeFloat(getCharacterHeight());
62    out->writeFloat(getCharacterAspectRatio());
63    out->writeUInt(getCharacterSizeMode());
64    out->writeFloat(getMaximumWidth());
65    out->writeFloat(getMaximumHeight());
66
67    out->writeFloat(getLineSpacing());
68
69    out->writeUInt(getAlignment());
70
71    out->writeQuat(getRotation()); //FIXME: controllare che ci sia
72
73    out->writeBool(getAutoRotateToScreen());
74    out->writeUInt(getLayout());
75    out->writeVec3(getPosition());
76    out->writeVec4(getColor());
77    out->writeUInt(getDrawMode());
78
79    if ( out->getVersion() >= VERSION_0041 )
80    {
81        out->writeFloat(getBoundingBoxMargin());
82        out->writeVec4(getBoundingBoxColor());
83    }
84
85    if ( out->getVersion() >= VERSION_0028 )
86    {
87        out->writeUInt(getBackdropType());
88
89        out->writeFloat(getBackdropHorizontalOffset());
90        out->writeFloat(getBackdropVerticalOffset());
91
92        out->writeVec4(getBackdropColor());
93        out->writeUInt(getBackdropImplementation());
94
95        out->writeUInt(getColorGradientMode());
96        out->writeVec4(getColorGradientTopLeft());
97        out->writeVec4(getColorGradientBottomLeft());
98        out->writeVec4(getColorGradientBottomRight());
99        out->writeVec4(getColorGradientTopRight());
100    }
101
102    // text :: Modified from osgPlugins::osg
103    const osgText::String& textstring = getText();
104    bool isACString = true;
105    osgText::String::const_iterator itr;
106    for(itr=textstring.begin();
107        itr!=textstring.end() && isACString;
108        ++itr)
109    {
110        if (*itr==0 || *itr>256) isACString=false;
111    }
112
113    if (isACString)
114    {
115        std::string str;
116
117        for(itr=textstring.begin();
118            itr!=textstring.end();
119            ++itr)
120        {
121            str += (char)(*itr);
122        }
123
124        //std::copy(textstring.begin(),textstring.end(),std::back_inserter(str));
125
126        out->writeBool(true);
127        out->writeString(str);
128    }
129    else
130    {
131        // do it the hardway...output each character as an int
132        osg::ref_ptr<osg::UIntArray> strarr = new osg::UIntArray(textstring.size());
133
134        for(itr=textstring.begin();
135            itr!=textstring.end();
136            ++itr)
137        {
138            strarr->push_back((*itr));
139        }
140
141        out->writeBool(false);
142        out->writeUIntArray(strarr.get());
143    }
144}
145
146void Text::read(DataInputStream* in){
147    // Peek on Text's identification.
148    int id = in->peekInt();
149    if(id == IVETEXT){
150        // Read Text's identification.
151        id = in->readInt();
152        // If the osg class is inherited by any other class we should also read this from file.
153        osg::Object*  obj = dynamic_cast<osg::Object*>(this);
154        if(obj){
155            ((ive::Drawable*)(obj))->read(in);
156        }
157        else
158            in_THROW_EXCEPTION("Text::read(): Could not cast this osgText::Text to an osg::Drawable.");
159        // Read Text's properties
160
161    unsigned int width, height;
162    float c_height, aspectRatio;
163
164    setFont(in->readString());
165
166    width = in->readUInt();
167    height = in->readUInt();
168
169    setFontResolution(width,height);
170
171    c_height = in->readFloat();
172    aspectRatio = in->readFloat();
173
174    setCharacterSize(c_height,aspectRatio);
175
176    setCharacterSizeMode((osgText::TextBase::CharacterSizeMode) in->readUInt());
177
178    setMaximumWidth(in->readFloat());
179    setMaximumHeight(in->readFloat());
180
181    if ( in->getVersion() >= VERSION_0020 )
182    {
183        setLineSpacing(in->readFloat());
184    }
185
186    setAlignment((osgText::TextBase::AlignmentType) in->readUInt());
187
188    //Nothing to do...
189    //setAxisAlignment((osgText::Text::AxisAlignment) in->readUint());
190
191    setRotation(in->readQuat());
192    setAutoRotateToScreen(in->readBool());
193    setLayout((osgText::Text::Layout) in->readUInt());
194
195    setPosition(in->readVec3());
196    setColor(in->readVec4());
197    setDrawMode(in->readUInt());
198
199    if ( in->getVersion() >= VERSION_0041 )
200    {
201        setBoundingBoxMargin(in->readFloat());
202        setBoundingBoxColor(in->readVec4());
203    }
204
205    if ( in->getVersion() >= VERSION_0028 )
206    {
207        setBackdropType((osgText::Text::BackdropType) in->readUInt());
208
209        float horizontalOffset,verticalOffset;
210        horizontalOffset = in->readFloat();
211        verticalOffset = in->readFloat();
212        setBackdropOffset(horizontalOffset,verticalOffset);
213
214        setBackdropColor(in->readVec4());
215        setBackdropImplementation((osgText::Text::BackdropImplementation) in->readUInt());
216        setColorGradientMode((osgText::Text::ColorGradientMode) in->readUInt());
217
218        osg::Vec4 colorGradientTopLeft,colorGradientBottomLeft,colorGradientBottomRight,colorGradientTopRight;
219        colorGradientTopLeft = in->readVec4();
220        colorGradientBottomLeft = in->readVec4();
221        colorGradientBottomRight = in->readVec4();
222        colorGradientTopRight = in->readVec4();
223        setColorGradientCorners(colorGradientTopLeft,colorGradientBottomLeft,colorGradientBottomRight,colorGradientTopRight);
224    }
225
226    if(in->readBool())
227        setText(in->readString());
228    else
229    {
230        if ( in->getVersion() >= VERSION_0018 )
231        {
232            osgText::String textstr;
233            osg::ref_ptr<osg::UIntArray> arr = in->readUIntArray();
234            for(unsigned int i = 0; i < arr->getNumElements(); i++)
235            {
236                textstr.push_back( arr->at(i) );
237            }
238
239            setText(textstr);
240        }
241        else
242        {
243            // buggy original path, should have used a UIntArray originally, now fixed above.
244            std::string textstr;
245            osg::ref_ptr<osg::UByteArray> arr = in->readUByteArray();
246            for(unsigned int i = 0; i < arr->getNumElements(); i++)
247            {
248                textstr += (char) arr->at(i);
249            }
250
251            setText(textstr);
252        }
253    }
254
255    }
256    else{
257        in_THROW_EXCEPTION("Text::read(): Expected Text identification.");
258    }
259}
Note: See TracBrowser for help on using the browser.