| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "Exception.h" |
|---|
| 16 | #include "Text3D.h" |
|---|
| 17 | #include "Text.h" |
|---|
| 18 | #include "Drawable.h" |
|---|
| 19 | #include "Object.h" |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/FileUtils> |
|---|
| 22 | #include <osgDB/FileNameUtils> |
|---|
| 23 | #include <osg/Notify> |
|---|
| 24 | |
|---|
| 25 | using namespace ive; |
|---|
| 26 | |
|---|
| 27 | void Text3D::write(DataOutputStream* out){ |
|---|
| 28 | |
|---|
| 29 | out->writeInt(IVETEXT3D); |
|---|
| 30 | |
|---|
| 31 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 32 | if(obj){ |
|---|
| 33 | ((ive::Drawable*)(obj))->write(out); |
|---|
| 34 | } |
|---|
| 35 | else |
|---|
| 36 | out_THROW_EXCEPTION("Text::write(): Could not cast this osgText::Text to an osg::Drawable."); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | if( getFont() ) |
|---|
| 40 | { |
|---|
| 41 | std::string fname = getFont()->getFileName(); |
|---|
| 42 | |
|---|
| 43 | if(!fname.empty()) |
|---|
| 44 | { |
|---|
| 45 | if(out->getUseOriginalExternalReferences()) |
|---|
| 46 | { |
|---|
| 47 | out->writeString(fname); |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | out->writeString(osgDB::getSimpleFileName(fname)); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | else |
|---|
| 55 | out->writeString(""); |
|---|
| 56 | } |
|---|
| 57 | else |
|---|
| 58 | out->writeString(""); |
|---|
| 59 | |
|---|
| 60 | out->writeUInt(getFontWidth()); |
|---|
| 61 | out->writeUInt(getFontHeight()); |
|---|
| 62 | out->writeFloat(getCharacterHeight()); |
|---|
| 63 | out->writeFloat(getCharacterAspectRatio()); |
|---|
| 64 | out->writeUInt(getCharacterSizeMode()); |
|---|
| 65 | out->writeFloat(getMaximumWidth()); |
|---|
| 66 | out->writeFloat(getMaximumHeight()); |
|---|
| 67 | |
|---|
| 68 | out->writeFloat(getLineSpacing()); |
|---|
| 69 | |
|---|
| 70 | out->writeUInt(getAlignment()); |
|---|
| 71 | |
|---|
| 72 | out->writeQuat(getRotation()); |
|---|
| 73 | |
|---|
| 74 | out->writeBool(getAutoRotateToScreen()); |
|---|
| 75 | out->writeUInt(getLayout()); |
|---|
| 76 | out->writeVec3(getPosition()); |
|---|
| 77 | |
|---|
| 78 | out->writeUInt(getDrawMode()); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | out->writeFloat(getCharacterDepth()); |
|---|
| 82 | out->writeUInt(getRenderMode()); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | const osgText::String& textstring = getText(); |
|---|
| 86 | bool isACString = true; |
|---|
| 87 | osgText::String::const_iterator itr; |
|---|
| 88 | for(itr=textstring.begin(); |
|---|
| 89 | itr!=textstring.end() && isACString; |
|---|
| 90 | ++itr) |
|---|
| 91 | { |
|---|
| 92 | if (*itr==0 || *itr>256) isACString=false; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | if (isACString) |
|---|
| 96 | { |
|---|
| 97 | std::string str; |
|---|
| 98 | |
|---|
| 99 | for(itr=textstring.begin(); |
|---|
| 100 | itr!=textstring.end(); |
|---|
| 101 | ++itr) |
|---|
| 102 | { |
|---|
| 103 | str += (char)(*itr); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | out->writeBool(true); |
|---|
| 109 | out->writeString(str); |
|---|
| 110 | } |
|---|
| 111 | else |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| 114 | osg::ref_ptr<osg::UIntArray> strarr = new osg::UIntArray(textstring.size()); |
|---|
| 115 | |
|---|
| 116 | for(itr=textstring.begin(); |
|---|
| 117 | itr!=textstring.end(); |
|---|
| 118 | ++itr) |
|---|
| 119 | { |
|---|
| 120 | strarr->push_back((*itr)); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | out->writeBool(false); |
|---|
| 124 | out->writeUIntArray(strarr.get()); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void Text3D::read(DataInputStream* in){ |
|---|
| 129 | |
|---|
| 130 | int id = in->peekInt(); |
|---|
| 131 | if(id == IVETEXT3D){ |
|---|
| 132 | |
|---|
| 133 | id = in->readInt(); |
|---|
| 134 | |
|---|
| 135 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 136 | if(obj){ |
|---|
| 137 | ((ive::Drawable*)(obj))->read(in); |
|---|
| 138 | } |
|---|
| 139 | else |
|---|
| 140 | in_THROW_EXCEPTION("Text::read(): Could not cast this osgText::Text to an osg::Drawable."); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | unsigned int width, height; |
|---|
| 144 | float c_height, aspectRatio; |
|---|
| 145 | |
|---|
| 146 | setFont(in->readString()); |
|---|
| 147 | |
|---|
| 148 | width = in->readUInt(); |
|---|
| 149 | height = in->readUInt(); |
|---|
| 150 | |
|---|
| 151 | setFontResolution(width,height); |
|---|
| 152 | |
|---|
| 153 | c_height = in->readFloat(); |
|---|
| 154 | aspectRatio = in->readFloat(); |
|---|
| 155 | |
|---|
| 156 | setCharacterSize(c_height,aspectRatio); |
|---|
| 157 | |
|---|
| 158 | setCharacterSizeMode((osgText::TextBase::CharacterSizeMode) in->readUInt()); |
|---|
| 159 | |
|---|
| 160 | setMaximumWidth(in->readFloat()); |
|---|
| 161 | setMaximumHeight(in->readFloat()); |
|---|
| 162 | |
|---|
| 163 | if ( in->getVersion() >= VERSION_0020 ) |
|---|
| 164 | { |
|---|
| 165 | setLineSpacing(in->readFloat()); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | setAlignment((osgText::TextBase::AlignmentType) in->readUInt()); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | setRotation(in->readQuat()); |
|---|
| 174 | setAutoRotateToScreen(in->readBool()); |
|---|
| 175 | setLayout((osgText::Text::Layout) in->readUInt()); |
|---|
| 176 | |
|---|
| 177 | setPosition(in->readVec3()); |
|---|
| 178 | |
|---|
| 179 | setDrawMode(in->readUInt()); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | setCharacterDepth(in->readFloat()); |
|---|
| 183 | setRenderMode((osgText::Text3D::RenderMode) in->readUInt()); |
|---|
| 184 | |
|---|
| 185 | if(in->readBool()) |
|---|
| 186 | setText(in->readString()); |
|---|
| 187 | else |
|---|
| 188 | { |
|---|
| 189 | if ( in->getVersion() >= VERSION_0018 ) |
|---|
| 190 | { |
|---|
| 191 | osgText::String textstr; |
|---|
| 192 | osg::ref_ptr<osg::UIntArray> arr = in->readUIntArray(); |
|---|
| 193 | for(unsigned int i = 0; i < arr->getNumElements(); i++) |
|---|
| 194 | { |
|---|
| 195 | textstr.push_back( arr->at(i) ); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | setText(textstr); |
|---|
| 199 | } |
|---|
| 200 | else |
|---|
| 201 | { |
|---|
| 202 | |
|---|
| 203 | std::string textstr; |
|---|
| 204 | osg::ref_ptr<osg::UByteArray> arr = in->readUByteArray(); |
|---|
| 205 | for(unsigned int i = 0; i < arr->getNumElements(); i++) |
|---|
| 206 | { |
|---|
| 207 | textstr += (char) arr->at(i); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | setText(textstr); |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | } |
|---|
| 215 | else{ |
|---|
| 216 | in_THROW_EXCEPTION("Text3D::read(): Expected ShadeModel identification."); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|