~ubuntu-branches/debian/experimental/openscenegraph/experimental

« back to all changes in this revision

Viewing changes to OpenSceneGraph/src/osgText/Text3D.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Luaces
  • Date: 2011-01-29 11:36:29 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110129113629-qisrm2kdqlurc7t3
Tags: 2.9.11-1
* Removed bug-555869-ftbfs_with_binutils_gold.dpatch since upstream has
  already taken care of the issue.
* Removed bug-528229.dpatch since the pkgconfig files are now also split
  in upstream.
* Removed explicit dependency on GLU.
* Upstream no longer includes osgIntrospection (Closes: #592420).
* Disabled zip plugin as its implementation stores an embedded copy of
  zlib.
* Enabled Qt support. Thanks James Goppert.
* Enabled SVG and PDF plugins. Thanks James Goppert.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 
1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2
2
 *
3
 
 * This library is open source and may be redistributed and/or modified under  
4
 
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 
3
 * This library is open source and may be redistributed and/or modified under
 
4
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5
5
 * (at your option) any later version.  The full license is in LICENSE file
6
6
 * included with this distribution, and on the openscenegraph.org website.
7
 
 * 
 
7
 *
8
8
 * This library is distributed in the hope that it will be useful,
9
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
 * OpenSceneGraph Public License for more details.
12
12
*/
13
13
 
14
14
#include <osgText/Text3D>
 
15
#include <osg/Vec3d>
15
16
#include <osg/io_utils>
16
 
namespace osgText 
 
17
 
 
18
namespace osgText
17
19
{
18
20
 
19
21
Text3D::Text3D():
20
 
    _font(0),
21
 
    _characterDepth(1),
22
22
    _renderMode(PER_GLYPH)
23
23
{
24
24
}
25
25
 
26
26
Text3D::Text3D(const Text3D & text3D, const osg::CopyOp & copyop):
27
27
    osgText::TextBase(text3D, copyop),
28
 
    _font(text3D._font),
29
 
    _characterDepth(text3D._characterDepth),
30
28
    _renderMode(text3D._renderMode)
31
29
{
32
30
    computeGlyphRepresentation();
33
31
}
34
32
 
35
 
//void Text3D::accept(osg::Drawable::ConstAttributeFunctor& af) const
36
 
//{
37
 
//    TODO
38
 
//}
39
 
 
40
 
//void Text3D::accept(osg::PrimitiveFunctor& /*pf*/) const
41
 
//{
42
 
//    TODO 
43
 
//}
44
 
 
45
 
void Text3D::setFont(osg::ref_ptr<Font3D> font)
46
 
47
 
    _font = font;
48
 
    
 
33
float Text3D::getCharacterDepth() const
 
34
{
 
35
    if (!_style) return _characterHeight*0.1f;
 
36
    else return _characterHeight * _style->getThicknessRatio();
 
37
}
 
38
 
 
39
void Text3D::setCharacterDepth(float characterDepth)
 
40
{
 
41
    getOrCreateStyle()->setThicknessRatio(characterDepth / _characterHeight);
 
42
 
49
43
    computeGlyphRepresentation();
50
44
}
51
45
 
52
 
void Text3D::setFont(const std::string & fontfile)
53
 
54
 
    setFont(readRefFont3DFile(fontfile));
 
46
void Text3D::accept(osg::Drawable::ConstAttributeFunctor& af) const
 
47
{
 
48
    // ** for each line, do ...
 
49
    TextRenderInfo::const_iterator itLine, endLine = _textRenderInfo.end();
 
50
    for (itLine = _textRenderInfo.begin(); itLine!=endLine; ++itLine)
 
51
    {
 
52
        // ** for each glyph in the line, do ...
 
53
        LineRenderInfo::const_iterator it, end = itLine->end();
 
54
        for (it = itLine->begin(); it!=end; ++it)
 
55
        {
 
56
            // ** apply the vertex array
 
57
            af.apply(osg::Drawable::VERTICES, it->_glyphGeometry->getVertexArray()->size(), &(it->_glyphGeometry->getVertexArray()->front()));
 
58
        }
 
59
    }
 
60
}
 
61
void Text3D::accept(osg::PrimitiveFunctor& pf) const
 
62
{
 
63
    // ** for each line, do ...
 
64
    TextRenderInfo::const_iterator itLine, endLine = _textRenderInfo.end();
 
65
    for (itLine = _textRenderInfo.begin(); itLine!=endLine; ++itLine)
 
66
    {
 
67
        // ** for each glyph in the line, do ...
 
68
        LineRenderInfo::const_iterator it, end = itLine->end();
 
69
        for (it = itLine->begin(); it!=end; ++it)
 
70
        {
 
71
            osg::Vec3Array* vertices = it->_glyphGeometry->getVertexArray();
 
72
 
 
73
            if (!vertices || vertices->empty())
 
74
              continue; //skip over spaces
 
75
          
 
76
            //pf.setVertexArray(it->_glyph->getVertexArray()->size(),&(it->_glyph->getVertexArray()->front()));
 
77
            //////////////////////////////////////////////////////////////////////////
 
78
            // now apply matrix to the glyphs.
 
79
            osg::ref_ptr<osg::Vec3Array> transformedVertices = new osg::Vec3Array;
 
80
            osg::Matrix matrix = _autoTransformCache[0]._matrix;//osg::Matrix();
 
81
            matrix.preMultTranslate(it->_position);
 
82
            transformedVertices->reserve(vertices->size());
 
83
            for (osg::Vec3Array::iterator itr=vertices->begin(); itr!=vertices->end(); itr++)
 
84
            {
 
85
              transformedVertices->push_back((*itr)*matrix);
 
86
            }
 
87
            //////////////////////////////////////////////////////////////////////////
 
88
            pf.setVertexArray(transformedVertices->size(),&(transformedVertices->front()));
 
89
 
 
90
            // ** render the front face of the glyph
 
91
            osg::Geometry::PrimitiveSetList & pslFront = it->_glyphGeometry->getFrontPrimitiveSetList();
 
92
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=pslFront.begin(), end = pslFront.end(); itr!=end; ++itr)
 
93
            {
 
94
                (*itr)->accept(pf);
 
95
            }
 
96
 
 
97
            // ** render the wall face of the glyph
 
98
            osg::Geometry::PrimitiveSetList & pslWall = it->_glyphGeometry->getWallPrimitiveSetList();
 
99
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=pslWall.begin(), end=pslWall.end(); itr!=end; ++itr)
 
100
            {
 
101
                (*itr)->accept(pf);
 
102
            }
 
103
 
 
104
            // ** render the back face of the glyph
 
105
            osg::Geometry::PrimitiveSetList & pslBack = it->_glyphGeometry->getBackPrimitiveSetList();
 
106
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=pslBack.begin(), end=pslBack.end(); itr!=end; ++itr)
 
107
            {
 
108
                (*itr)->accept(pf);
 
109
            }
 
110
        }
 
111
    }
55
112
}
56
113
 
57
114
String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last)
63
120
 
64
121
    String::iterator lastChar = first;
65
122
 
66
 
    std::set<unsigned int> deliminatorSet;
67
 
    deliminatorSet.insert(' ');
68
 
    deliminatorSet.insert('\n');
69
 
    deliminatorSet.insert(':');
70
 
    deliminatorSet.insert('/');
71
 
    deliminatorSet.insert(',');
72
 
    deliminatorSet.insert(';');
73
 
    deliminatorSet.insert(':');
74
 
    deliminatorSet.insert('.');
75
 
 
76
 
    float maximumHeight = _maximumHeight / _font->getScale();
77
 
    float maximumWidth = _maximumWidth / _font->getScale();
78
 
    
 
123
    float maximumHeight = _maximumHeight;
 
124
    float maximumWidth = _maximumWidth;
 
125
 
 
126
    float hr = 1.0f;
 
127
    float wr = 1.0f;
 
128
 
79
129
    for(bool outOfSpace=false;lastChar!=last;++lastChar)
80
130
    {
81
131
        unsigned int charcode = *lastChar;
82
 
        
 
132
 
83
133
        if (charcode=='\n')
84
134
        {
85
135
            return lastChar;
86
136
        }
87
137
 
88
 
        Font3D::Glyph3D* glyph = _font->getGlyph(charcode);
 
138
        Glyph3D* glyph = _font->getGlyph3D(charcode);
89
139
        if (glyph)
90
140
        {
91
141
            const osg::BoundingBox & bb = glyph->getBoundingBox();
92
 
            
 
142
 
 
143
            if (_layout==RIGHT_TO_LEFT)
 
144
            {
 
145
                cursor.x() -= glyph->getHorizontalAdvance() * wr;
 
146
            }
 
147
 
93
148
            // adjust cursor position w.r.t any kerning.
94
 
            if (previous_charcode)
 
149
            if (kerning && previous_charcode)
95
150
            {
96
 
                
97
 
                if (_layout == RIGHT_TO_LEFT)
98
 
                {
99
 
                    cursor.x() -= glyph->getHorizontalAdvance();
100
 
                }
101
 
                
102
 
                if (kerning)
103
 
                {
104
 
                    switch (_layout)
105
 
                    {
106
 
                        case LEFT_TO_RIGHT:
107
 
                        {
108
 
                            cursor += _font->getKerning(previous_charcode, charcode, _kerningType);
109
 
                            break;
110
 
                        }
111
 
                        case RIGHT_TO_LEFT:
112
 
                        {
113
 
                            cursor -= _font->getKerning(charcode, previous_charcode, _kerningType);
114
 
                            break;
115
 
                        }
116
 
                        case VERTICAL:
117
 
                            break; // no kerning when vertical.
118
 
                    }
119
 
                }
120
 
            }
121
 
            else
122
 
            { 
123
 
                switch (_layout)
124
 
                {
125
 
                    case LEFT_TO_RIGHT:
126
 
                    {
127
 
                        cursor.x() -= glyph->getHorizontalBearing().x();
128
 
                        break;
129
 
                    }
130
 
                    case RIGHT_TO_LEFT:
131
 
                    {
132
 
                        cursor.x() -= bb.xMax();
133
 
                        break;
134
 
                    }
135
 
                    case VERTICAL:
136
 
                        break;
137
 
                }
138
 
                
139
 
            }
140
 
            
141
 
            
 
151
                switch(_layout)
 
152
                {
 
153
                  case LEFT_TO_RIGHT:
 
154
                  {
 
155
                    osg::Vec2 delta(_font->getKerning(previous_charcode,charcode,_kerningType));
 
156
                    cursor.x() += delta.x() * wr;
 
157
                    cursor.y() += delta.y() * hr;
 
158
                    break;
 
159
                  }
 
160
                  case RIGHT_TO_LEFT:
 
161
                  {
 
162
                    osg::Vec2 delta(_font->getKerning(charcode,previous_charcode,_kerningType));
 
163
                    cursor.x() -= delta.x() * wr;
 
164
                    cursor.y() -= delta.y() * hr;
 
165
                    break;
 
166
                  }
 
167
                  case VERTICAL:
 
168
                    break; // no kerning when vertical.
 
169
                }
 
170
            }
 
171
 
 
172
            osg::Vec2 local = cursor;
142
173
 
143
174
            switch(_layout)
144
175
            {
145
176
              case LEFT_TO_RIGHT:
146
177
              {
147
 
                if (maximumWidth>0.0f && cursor.x()+bb.xMax()>maximumWidth) outOfSpace=true;
148
 
                if(maximumHeight>0.0f && cursor.y()<-maximumHeight) outOfSpace=true;
 
178
                if (maximumWidth>0.0f && local.x()+bb.xMax()>maximumWidth) outOfSpace=true;
 
179
                if(maximumHeight>0.0f && local.y()<-maximumHeight) outOfSpace=true;
149
180
                break;
150
181
              }
151
182
              case RIGHT_TO_LEFT:
152
183
              {
153
 
                if (maximumWidth>0.0f && cursor.x()+bb.xMin()<-maximumWidth) outOfSpace=true;
154
 
                if(maximumHeight>0.0f && cursor.y()<-maximumHeight) outOfSpace=true;
 
184
                if (maximumWidth>0.0f && local.x()+bb.xMin()<-maximumWidth) outOfSpace=true;
 
185
                if(maximumHeight>0.0f && local.y()<-maximumHeight) outOfSpace=true;
155
186
                break;
156
187
              }
157
188
              case VERTICAL:
158
 
                if (maximumHeight>0.0f && cursor.y()<-maximumHeight) outOfSpace=true;
 
189
                if (maximumHeight>0.0f && local.y()<-maximumHeight) outOfSpace=true;
159
190
                break;
160
191
            }
161
 
            
 
192
 
162
193
            // => word boundary detection & wrapping
163
194
            if (outOfSpace) break;
164
195
 
165
196
            // move the cursor onto the next character.
166
197
            switch(_layout)
167
198
            {
168
 
              case LEFT_TO_RIGHT: cursor.x() += glyph->getHorizontalAdvance(); break;
 
199
              case LEFT_TO_RIGHT: cursor.x() += glyph->getHorizontalAdvance() * wr; break;
169
200
              case RIGHT_TO_LEFT: break;
170
 
              case VERTICAL:      cursor.y() -= glyph->getVerticalAdvance(); break;
 
201
              case VERTICAL:      cursor.y() -= glyph->getVerticalAdvance() * hr; break;
171
202
            }
172
203
 
173
204
            previous_charcode = charcode;
175
206
        }
176
207
 
177
208
    }
178
 
    
 
209
 
179
210
    // word boundary detection & wrapping
180
211
    if (lastChar!=last)
181
212
    {
182
 
        if (deliminatorSet.count(*lastChar)==0) 
183
 
        {
184
 
            String::iterator lastValidChar = lastChar;
185
 
            while (lastValidChar!=first && deliminatorSet.count(*lastValidChar)==0)
 
213
        String::iterator lastValidChar = lastChar;
 
214
          String::iterator prevChar;
 
215
        while (lastValidChar != first){
 
216
            prevChar = lastValidChar - 1;
 
217
 
 
218
            // last char is after a hyphen
 
219
                if(*lastValidChar == '-')
 
220
                return lastValidChar + 1;
 
221
 
 
222
            // last char is start of whitespace
 
223
            if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n'))
 
224
                return lastValidChar;
 
225
 
 
226
            // Subtract off glyphs from the cursor position (to correctly center text)
 
227
                if(*prevChar != '-')
186
228
            {
187
 
                --lastValidChar;
188
 
                
189
 
                //Substract off glyphs from the cursor position (to correctly center text)
190
 
                Font3D::Glyph3D* glyph = _font->getGlyph(*lastValidChar);
 
229
                Glyph3D* glyph = _font->getGlyph3D(*prevChar);
191
230
                if (glyph)
192
231
                {
193
232
                    switch(_layout)
194
233
                    {
195
 
                    case LEFT_TO_RIGHT: cursor.x() -= glyph->getHorizontalAdvance(); break;
196
 
                    case RIGHT_TO_LEFT: cursor.x() += glyph->getHorizontalAdvance(); break;
197
 
                    case VERTICAL:      cursor.y() += glyph->getVerticalAdvance(); break;
 
234
                    case LEFT_TO_RIGHT: cursor.x() -= glyph->getHorizontalAdvance() * wr; break;
 
235
                    case RIGHT_TO_LEFT: cursor.x() += glyph->getHorizontalAdvance() * wr; break;
 
236
                    case VERTICAL:      cursor.y() += glyph->getVerticalAdvance() * wr; break;
198
237
                    }
199
238
                }
200
239
            }
201
 
            
202
 
            if (first!=lastValidChar)
203
 
            {
204
 
                ++lastValidChar;
205
 
                lastChar = lastValidChar;
206
 
            }
207
 
        }
 
240
 
 
241
            lastValidChar = prevChar;
 
242
          }
208
243
    }
209
244
 
210
245
    return lastChar;
213
248
void Text3D::computeGlyphRepresentation()
214
249
{
215
250
    if (_font.valid() == false) return;
216
 
    
 
251
 
217
252
    _textRenderInfo.clear();
218
253
    _lineCount = 0;
219
 
    
220
 
    if (_text.empty()) 
 
254
 
 
255
    if (_text.empty())
221
256
    {
222
257
        _textBB.set(0,0,0, 0,0,0);//no size text
223
258
        TextBase::computePositions(); //to reset the origin
224
259
        return;
225
260
    }
226
 
    
 
261
 
227
262
    // initialize bounding box, it will be expanded during glyph position calculation
228
263
    _textBB.init();
229
264
 
 
265
 
 
266
    float hr = 1.0f;
 
267
    float wr = 1.0f;
 
268
 
230
269
    osg::Vec2 startOfLine_coords(0.0f,0.0f);
231
270
    osg::Vec2 cursor(startOfLine_coords);
232
271
    osg::Vec2 local(0.0f,0.0f);
233
272
    osg::Vec2 startOffset(0.0f,0.0f);
234
 
    
 
273
 
235
274
    unsigned int previous_charcode = 0;
236
275
    unsigned int linelength = 0;
237
276
    bool kerning = true;
238
 
    
 
277
 
239
278
    unsigned int lineNumber = 0;
240
279
 
241
 
  
242
 
 
243
280
    for(String::iterator itr=_text.begin(); itr!=_text.end(); )
244
281
    {
245
282
        _textRenderInfo.resize(lineNumber + 1);
246
283
        LineRenderInfo & currentLineRenderInfo = _textRenderInfo.back();
247
 
        
 
284
 
248
285
        // record the start of the current line
249
286
        String::iterator startOfLine_itr = itr;
250
287
 
251
288
        // find the end of the current line.
252
289
        osg::Vec2 endOfLine_coords(cursor);
253
290
        String::iterator endOfLine_itr = computeLastCharacterOnLine(endOfLine_coords, itr,_text.end());
254
 
        
255
 
        // ** position the cursor function to the Layout and the alignement 
 
291
 
 
292
        // ** position the cursor function to the Layout and the alignement
256
293
        TextBase::positionCursor(endOfLine_coords, cursor, (unsigned int) (endOfLine_itr - startOfLine_itr));
257
294
 
258
 
        
 
295
 
259
296
        if (itr!=endOfLine_itr)
260
297
        {
261
298
            for(;itr!=endOfLine_itr;++itr)
262
299
            {
263
300
                unsigned int charcode = *itr;
264
301
 
265
 
                Font3D::Glyph3D* glyph = _font->getGlyph(charcode);
 
302
                Glyph3D* glyph = _font->getGlyph3D(charcode);
266
303
                if (glyph)
267
304
                {
268
305
                    const osg::BoundingBox & bb = glyph->getBoundingBox();
269
 
                    
 
306
 
 
307
                    if (_layout==RIGHT_TO_LEFT)
 
308
                    {
 
309
                        cursor.x() -= glyph->getHorizontalAdvance() * wr;
 
310
                    }
 
311
 
270
312
                    // adjust cursor position w.r.t any kerning.
271
 
                    if (previous_charcode)
 
313
                    if (kerning && previous_charcode)
272
314
                    {
273
 
                        if (_layout == RIGHT_TO_LEFT)
274
 
                        {
275
 
                            cursor.x() -= glyph->getHorizontalAdvance();
276
 
                        }
277
 
                        
278
 
                        if (kerning)
279
 
                        {
280
 
                            switch (_layout)
281
 
                            {
282
 
                                case LEFT_TO_RIGHT:
283
 
                                {
284
 
                                    cursor += _font->getKerning(previous_charcode, charcode, _kerningType);
285
 
                                    break;
286
 
                                }
287
 
                                case RIGHT_TO_LEFT:
288
 
                                {
289
 
                                    cursor -= _font->getKerning(charcode, previous_charcode, _kerningType);
290
 
                                    break;
291
 
                                }
292
 
                                case VERTICAL:
293
 
                                    break; // no kerning when vertical.
294
 
                            }
295
 
                        }
296
 
                    }
297
 
                    else
298
 
                    { 
299
 
                        switch (_layout)
300
 
                        {
301
 
                            case LEFT_TO_RIGHT:
302
 
                            {
303
 
                                cursor.x() -= glyph->getHorizontalBearing().x();
304
 
                                break;
305
 
                            }
306
 
                            case RIGHT_TO_LEFT:
307
 
                            {
308
 
                                cursor.x() -= bb.xMax();
309
 
                                break;
310
 
                            }
311
 
                            case VERTICAL:
312
 
                            {
313
 
//                                cursor.y() += glyph->getVerticalBearing().y();
314
 
                                break;
315
 
                            }
316
 
                        }
317
 
                        
 
315
                        switch(_layout)
 
316
                        {
 
317
                          case LEFT_TO_RIGHT:
 
318
                          {
 
319
                            osg::Vec2 delta(_font->getKerning(previous_charcode,charcode,_kerningType));
 
320
                            cursor.x() += delta.x() * wr;
 
321
                            cursor.y() += delta.y() * hr;
 
322
                            break;
 
323
                          }
 
324
                          case RIGHT_TO_LEFT:
 
325
                          {
 
326
                            osg::Vec2 delta(_font->getKerning(charcode,previous_charcode,_kerningType));
 
327
                            cursor.x() -= delta.x() * wr;
 
328
                            cursor.y() -= delta.y() * hr;
 
329
                            break;
 
330
                          }
 
331
                          case VERTICAL:
 
332
                            break; // no kerning when vertical.
 
333
                        }
318
334
                    }
319
335
 
320
336
                    local = cursor;
321
 
                    
322
 
                    if (_layout==VERTICAL)
323
 
                    {
324
 
                        local.x() += -glyph->getVerticalBearing().x();
325
 
                        local.y() += -glyph->getVerticalBearing().y();
326
 
                    }
327
 
                     
328
 
                    
329
 
                    
 
337
 
330
338
                    // move the cursor onto the next character.
331
339
                    // also expand bounding box
332
340
                    switch (_layout)
333
341
                    {
334
342
                        case LEFT_TO_RIGHT:
335
 
                            _textBB.expandBy(osg::Vec3(cursor.x() + bb.xMin(), cursor.y() + bb.yMin(), 0.0f)); //lower left corner
336
 
                            _textBB.expandBy(osg::Vec3(cursor.x() + bb.xMax(), cursor.y() + bb.yMax(), 0.0f)); //upper right corner
337
 
                            cursor.x() += glyph->getHorizontalAdvance();
 
343
                            _textBB.expandBy(osg::Vec3(cursor.x() + bb.xMin()*wr, cursor.y() + bb.yMin()*hr, 0.0f)); //lower left corner
 
344
                            _textBB.expandBy(osg::Vec3(cursor.x() + bb.xMax()*wr, cursor.y() + bb.yMax()*hr, 0.0f)); //upper right corner
 
345
                            cursor.x() += glyph->getHorizontalAdvance() * wr;
338
346
                            break;
339
347
                        case VERTICAL:
340
348
                            _textBB.expandBy(osg::Vec3(cursor.x(), cursor.y(), 0.0f)); //upper left corner
341
 
                            _textBB.expandBy(osg::Vec3(cursor.x() + glyph->getWidth(), cursor.y() - glyph->getHeight(), 0.0f)); //lower right corner
342
 
                            cursor.y() -= glyph->getVerticalAdvance();
 
349
                            _textBB.expandBy(osg::Vec3(cursor.x() + glyph->getWidth()*wr, cursor.y() - glyph->getHeight()*hr, 0.0f)); //lower right corner
 
350
                            cursor.y() -= glyph->getVerticalAdvance() * hr;
343
351
                            break;
344
352
                        case RIGHT_TO_LEFT:
345
 
                            _textBB.expandBy(osg::Vec3(cursor.x()+bb.xMax(), cursor.y() + bb.yMax(), 0.0f)); //upper right corner
346
 
                            _textBB.expandBy(osg::Vec3(cursor.x()+bb.xMin(), cursor.y()+bb.yMin(), 0.0f)); //lower left corner
347
 
                            
 
353
                            _textBB.expandBy(osg::Vec3(cursor.x()+bb.xMax()*wr, cursor.y() + bb.yMax()*hr, 0.0f)); //upper right corner
 
354
                            _textBB.expandBy(osg::Vec3(cursor.x()+bb.xMin()*wr, cursor.y()+bb.yMin()*hr, 0.0f)); //lower left corner
 
355
 
348
356
                            break;
349
357
                    }
350
 
                    
 
358
 
351
359
                    osg::Vec3 pos = osg::Vec3(local.x(), local.y(), 0.0f);
352
 
                    currentLineRenderInfo.push_back(Text3D::GlyphRenderInfo(glyph, pos));
 
360
                    GlyphGeometry* glyphGeometry = glyph->getGlyphGeometry(_style.get());
 
361
                    currentLineRenderInfo.push_back(Text3D::GlyphRenderInfo(glyphGeometry, pos));
353
362
 
354
 
                    
355
363
                    previous_charcode = charcode;
356
364
                }
357
365
            }
360
368
        {
361
369
            ++itr;
362
370
        }
363
 
                                
 
371
 
364
372
        if (itr!=_text.end())
365
373
        {
366
 
            // skip over return.
 
374
            // skip over spaces and return.
 
375
            while (*itr==' ') ++itr;
367
376
            if (*itr=='\n') ++itr;
368
377
        }
369
 
                
 
378
 
370
379
        // move to new line.
371
380
        switch(_layout)
372
381
        {
373
382
            case LEFT_TO_RIGHT:
374
383
            case RIGHT_TO_LEFT:
375
384
            {
376
 
                startOfLine_coords.y() -= (1.0 + _lineSpacing) / _font->getScale();
 
385
                startOfLine_coords.y() -= (1.0 + _lineSpacing) * hr;
377
386
                ++_lineCount;
378
387
                break;
379
388
            }
380
389
            case VERTICAL:
381
390
            {
382
 
                startOfLine_coords.x() += _characterHeight / _font->getScale() / _characterAspectRatio * (1.0 + _lineSpacing);
 
391
                startOfLine_coords.x() += _characterHeight * (1.0 + _lineSpacing) * wr;
383
392
                // because _lineCount is the max vertical no. of characters....
384
393
                _lineCount = (_lineCount >linelength)?_lineCount:linelength;
385
394
                break;
393
402
 
394
403
        ++lineNumber;
395
404
    }
396
 
    _textBB.expandBy(0.0f,0.0f,-1);
397
 
    
 
405
 
 
406
    float thickness = _style.valid() ? _style->getThicknessRatio() : 0.1f;
 
407
    _textBB.zMin() = -thickness;
 
408
 
398
409
    TextBase::computePositions();
399
410
}
400
411
 
401
 
 
402
 
 
403
412
osg::BoundingBox Text3D::computeBound() const
404
413
{
405
414
    osg::BoundingBox  bbox;
410
419
        {
411
420
            osg::Matrix& matrix = _autoTransformCache[i]._matrix;
412
421
            bbox.expandBy(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin())*matrix);
413
 
//          bbox.expandBy(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMin())*matrix);
414
422
            bbox.expandBy(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMax())*matrix);
415
 
//          bbox.expandBy(osg::Vec3(_textBB.xMin(),_textBB.yMax(),_textBB.zMin())*matrix);
416
423
        }
417
424
    }
418
 
    
 
425
 
419
426
    return bbox;
420
427
}
421
428
 
422
 
 
423
 
 
424
 
 
425
 
 
426
 
 
427
 
 
428
429
void Text3D::computePositions(unsigned int contextID) const
429
430
{
430
431
    if (_font.valid() == false) return;
431
 
    
 
432
 
432
433
    switch(_alignment)
433
434
    {
434
435
    case LEFT_TOP:      _offset.set(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); break;
446
447
    case LEFT_BASE_LINE:  _offset.set(0.0f,0.0f,0.0f); break;
447
448
    case CENTER_BASE_LINE:  _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,0.0f,0.0f); break;
448
449
    case RIGHT_BASE_LINE:  _offset.set(_textBB.xMax(),0.0f,0.0f); break;
449
 
    
 
450
 
450
451
    case LEFT_BOTTOM_BASE_LINE:  _offset.set(0.0f,-_characterHeight*(_lineCount-1),0.0f); break;
451
452
    case CENTER_BOTTOM_BASE_LINE:  _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,-_characterHeight*(_lineCount-1),0.0f); break;
452
453
    case RIGHT_BOTTOM_BASE_LINE:  _offset.set(_textBB.xMax(),-_characterHeight*(_lineCount-1),0.0f); break;
453
454
    }
454
 
    
 
455
 
455
456
    AutoTransformCache& atc = _autoTransformCache[contextID];
456
457
    osg::Matrix& matrix = atc._matrix;
457
458
 
458
 
    
459
 
    float scale = _font->getScale();
460
 
    osg::Vec3 scaleVec(scale * _characterHeight, scale * _characterHeight / _characterAspectRatio, _characterDepth);
 
459
 
 
460
    osg::Vec3 scaleVec(_characterHeight / getCharacterAspectRatio(), _characterHeight , _characterHeight);
 
461
 
461
462
    matrix.makeTranslate(-_offset);
462
463
    matrix.postMultScale(scaleVec);
463
464
    matrix.postMultRotate(_rotation);
464
465
    matrix.postMultTranslate(_position);
465
466
 
466
 
    
 
467
 
467
468
    _normal = osg::Matrix::transform3x3(osg::Vec3(0.0f,0.0f,1.0f),matrix);
468
469
    _normal.normalize();
469
470
 
470
 
    const_cast<Text3D*>(this)->dirtyBound();    
 
471
    const_cast<Text3D*>(this)->dirtyBound();
471
472
}
472
473
 
473
474
void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const
474
475
{
475
476
    osg::State & state = *renderInfo.getState();
476
477
    unsigned int contextID = state.getContextID();
477
 
    
478
 
    
 
478
 
479
479
    // ** save the previous modelview matrix
480
 
    osg::ref_ptr<osg::RefMatrix> previous(new osg::RefMatrix(state.getModelViewMatrix()));
 
480
    osg::Matrix previous(state.getModelViewMatrix());
481
481
 
482
482
    // ** get the modelview for this context
483
 
    osg::ref_ptr<osg::RefMatrix> modelview(new osg::RefMatrix(_autoTransformCache[contextID]._matrix));
 
483
    osg::Matrix modelview(_autoTransformCache[contextID]._matrix);
484
484
 
485
485
    // ** mult previous by the modelview for this context
486
 
    modelview->postMult(*previous.get());
487
 
   
 
486
    modelview.postMult(previous);
 
487
 
488
488
    // ** apply this new modelview matrix
489
 
    state.applyModelViewMatrix(modelview.get());
490
 
    
491
 
    
492
 
    if (_drawMode & TEXT)
493
 
    {
494
 
        renderInfo.getState()->disableAllVertexArrays();
495
 
        
496
 
        glPushAttrib(GL_TRANSFORM_BIT);
497
 
        glEnable(GL_RESCALE_NORMAL);
498
 
        
499
 
        switch(_renderMode)
500
 
        {
501
 
            case PER_FACE:  renderPerFace(*renderInfo.getState());   break;
502
 
            case PER_GLYPH:
503
 
            default:        renderPerGlyph(*renderInfo.getState());  break;
504
 
        }
505
 
        
506
 
        glPopAttrib();
507
 
    }
 
489
    state.applyModelViewMatrix(modelview);
 
490
 
 
491
    osg::GLBeginEndAdapter& gl = (state.getGLBeginEndAdapter());
508
492
 
509
493
    if (_drawMode & BOUNDINGBOX)
510
494
    {
511
495
        if (_textBB.valid())
512
496
        {
 
497
            gl.Color4fv(_color.ptr());
 
498
 
513
499
            osg::Vec3 c000(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMax()));
514
500
            osg::Vec3 c100(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMax()));
515
501
            osg::Vec3 c110(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMax()));
516
502
            osg::Vec3 c010(osg::Vec3(_textBB.xMin(),_textBB.yMax(),_textBB.zMax()));
517
 
        
 
503
 
518
504
            osg::Vec3 c001(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin()));
519
505
            osg::Vec3 c101(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()));
520
506
            osg::Vec3 c111(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()));
521
507
            osg::Vec3 c011(osg::Vec3(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()));
522
 
        
523
 
            glBegin(GL_LINE_LOOP);
524
 
                glVertex3fv(c000.ptr());
525
 
                glVertex3fv(c100.ptr());
526
 
                glVertex3fv(c110.ptr());
527
 
                glVertex3fv(c010.ptr());
528
 
            glEnd();
529
 
            
530
 
            glBegin(GL_LINE_LOOP);
531
 
                glVertex3fv(c001.ptr());
532
 
                glVertex3fv(c011.ptr());
533
 
                glVertex3fv(c111.ptr());
534
 
                glVertex3fv(c101.ptr());
535
 
            glEnd();
536
 
            
537
 
            glBegin(GL_LINES);
538
 
                glVertex3fv(c000.ptr());
539
 
                glVertex3fv(c001.ptr());
540
 
                
541
 
                glVertex3fv(c100.ptr());
542
 
                glVertex3fv(c101.ptr());
543
 
                
544
 
                glVertex3fv(c110.ptr());
545
 
                glVertex3fv(c111.ptr());
546
 
                
547
 
                glVertex3fv(c010.ptr());
548
 
                glVertex3fv(c011.ptr());
549
 
            glEnd();
 
508
 
 
509
            gl.Begin(GL_LINE_LOOP);
 
510
                gl.Vertex3fv(c000.ptr());
 
511
                gl.Vertex3fv(c100.ptr());
 
512
                gl.Vertex3fv(c110.ptr());
 
513
                gl.Vertex3fv(c010.ptr());
 
514
            gl.End();
 
515
 
 
516
            gl.Begin(GL_LINE_LOOP);
 
517
                gl.Vertex3fv(c001.ptr());
 
518
                gl.Vertex3fv(c011.ptr());
 
519
                gl.Vertex3fv(c111.ptr());
 
520
                gl.Vertex3fv(c101.ptr());
 
521
            gl.End();
 
522
 
 
523
            gl.Begin(GL_LINES);
 
524
                gl.Vertex3fv(c000.ptr());
 
525
                gl.Vertex3fv(c001.ptr());
 
526
 
 
527
                gl.Vertex3fv(c100.ptr());
 
528
                gl.Vertex3fv(c101.ptr());
 
529
 
 
530
                gl.Vertex3fv(c110.ptr());
 
531
                gl.Vertex3fv(c111.ptr());
 
532
 
 
533
                gl.Vertex3fv(c010.ptr());
 
534
                gl.Vertex3fv(c011.ptr());
 
535
            gl.End();
550
536
        }
551
 
    }    
 
537
    }
552
538
 
553
539
    if (_drawMode & ALIGNMENT)
554
540
    {
559
545
        osg::Vec3 vt(osg::Vec3(_offset.x(),_offset.y()-cursorsize,_offset.z()));
560
546
        osg::Vec3 vb(osg::Vec3(_offset.x(),_offset.y()+cursorsize,_offset.z()));
561
547
 
562
 
        glBegin(GL_LINES);
563
 
            glVertex3fv(hl.ptr());
564
 
            glVertex3fv(hr.ptr());
565
 
            glVertex3fv(vt.ptr());
566
 
            glVertex3fv(vb.ptr());
567
 
        glEnd();
568
 
        
569
 
    }
 
548
        gl.Color4fv(_color.ptr());
 
549
 
 
550
        gl.Begin(GL_LINES);
 
551
            gl.Vertex3fv(hl.ptr());
 
552
            gl.Vertex3fv(hr.ptr());
 
553
            gl.Vertex3fv(vt.ptr());
 
554
            gl.Vertex3fv(vb.ptr());
 
555
        gl.End();
 
556
 
 
557
    }
 
558
 
 
559
    if (_drawMode & TEXT)
 
560
    {
 
561
        state.disableColorPointer();
 
562
        state.Color(_color.r(),_color.g(),_color.b(),_color.a());
 
563
 
 
564
        renderInfo.getState()->disableAllVertexArrays();
 
565
 
 
566
        #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE)
 
567
            renderInfo.getState()->applyMode(GL_NORMALIZE, true);
 
568
        #endif
 
569
 
 
570
        switch(_renderMode)
 
571
        {
 
572
            case PER_FACE:  renderPerFace(*renderInfo.getState());   break;
 
573
            case PER_GLYPH:
 
574
            default:        renderPerGlyph(*renderInfo.getState());  break;
 
575
        }
 
576
    }
 
577
 
570
578
 
571
579
    // restore the previous modelview matrix
572
 
    state.applyModelViewMatrix(previous.get());
 
580
    state.applyModelViewMatrix(previous);
573
581
}
574
582
 
575
 
 
576
 
 
577
583
void Text3D::renderPerGlyph(osg::State & state) const
578
 
{   
 
584
{
 
585
    osg::Matrix original_modelview = state.getModelViewMatrix();
 
586
 
579
587
    // ** for each line, do ...
580
588
    TextRenderInfo::const_iterator itLine, endLine = _textRenderInfo.end();
581
589
    for (itLine = _textRenderInfo.begin(); itLine!=endLine; ++itLine)
584
592
        LineRenderInfo::const_iterator it, end = itLine->end();
585
593
        for (it = itLine->begin(); it!=end; ++it)
586
594
        {
587
 
            
588
 
            glPushMatrix();
589
 
 
590
 
            glTranslatef(it->_position.x(), it->_position.y(), it->_position.z());
591
 
           
 
595
 
 
596
            osg::Matrix matrix(original_modelview);
 
597
            matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
 
598
            state.applyModelViewMatrix(matrix);
 
599
 
 
600
            state.lazyDisablingOfVertexAttributes();
 
601
 
592
602
            // ** apply the vertex array
593
 
            state.setVertexPointer(it->_glyph->getVertexArray());
594
 
            
595
 
            // ** render the front face of the glyph
596
 
            glNormal3f(0.0f,0.0f,1.0f);
597
 
            
598
 
            osg::Geometry::PrimitiveSetList & pslFront = it->_glyph->getFrontPrimitiveSetList();
 
603
            state.setVertexPointer(it->_glyphGeometry->getVertexArray());
 
604
            state.setNormalPointer(it->_glyphGeometry->getNormalArray());
 
605
 
 
606
            state.applyDisablingOfVertexAttributes();
 
607
 
 
608
 
 
609
            osg::Geometry::PrimitiveSetList & pslFront = it->_glyphGeometry->getFrontPrimitiveSetList();
599
610
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=pslFront.begin(), end = pslFront.end(); itr!=end; ++itr)
600
611
            {
601
 
                
602
612
                (*itr)->draw(state, false);
603
613
            }
604
 
            
 
614
 
605
615
            // ** render the wall face of the glyph
606
 
            state.setNormalPointer(it->_glyph->getNormalArray());
607
 
            osg::Geometry::PrimitiveSetList & pslWall = it->_glyph->getWallPrimitiveSetList();
 
616
            osg::Geometry::PrimitiveSetList & pslWall = it->_glyphGeometry->getWallPrimitiveSetList();
608
617
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=pslWall.begin(), end=pslWall.end(); itr!=end; ++itr)
609
618
            {
610
619
                (*itr)->draw(state, false);
611
620
            }
612
 
            
613
 
            // ** render the back face of the glyph
614
 
            glNormal3f(0.0f,0.0f,-1.0f);
615
 
                        
616
 
            osg::Geometry::PrimitiveSetList & pslBack = it->_glyph->getBackPrimitiveSetList();
 
621
 
 
622
            osg::Geometry::PrimitiveSetList & pslBack = it->_glyphGeometry->getBackPrimitiveSetList();
617
623
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=pslBack.begin(), end=pslBack.end(); itr!=end; ++itr)
618
624
            {
619
625
                (*itr)->draw(state, false);
620
626
            }
621
 
            
622
 
            glPopMatrix();
623
627
        }
624
628
    }
625
629
}
626
630
 
627
631
void Text3D::renderPerFace(osg::State & state) const
628
632
{
 
633
    osg::Matrix original_modelview = state.getModelViewMatrix();
 
634
#if 0
629
635
    // ** render all front faces
630
 
    glNormal3f(0.0f,0.0f,1.0f);
631
 
    
 
636
    state.Normal(0.0f,0.0f,1.0f);
 
637
#endif
 
638
 
632
639
    TextRenderInfo::const_iterator itLine, endLine = _textRenderInfo.end();
633
640
    for (itLine = _textRenderInfo.begin(); itLine!=endLine; ++itLine)
634
641
    {
635
642
        // ** for each glyph in the line, do ...
636
643
        LineRenderInfo::const_iterator it, end = itLine->end();
637
 
        for (it = itLine->begin(); it!=end; ++it)   
 
644
        for (it = itLine->begin(); it!=end; ++it)
638
645
        {
639
 
            glPushMatrix();
640
 
            glTranslatef(it->_position.x(), it->_position.y(), it->_position.z());
641
 
            state.setVertexPointer(it->_glyph->getVertexArray());
642
 
            
 
646
            osg::Matrix matrix(original_modelview);
 
647
            matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
 
648
            state.applyModelViewMatrix(matrix);
 
649
 
 
650
            state.setVertexPointer(it->_glyphGeometry->getVertexArray());
 
651
            state.setNormalPointer(it->_glyphGeometry->getNormalArray());
 
652
 
643
653
            // ** render the front face of the glyph
644
 
            osg::Geometry::PrimitiveSetList & psl = it->_glyph->getFrontPrimitiveSetList();
 
654
            osg::Geometry::PrimitiveSetList & psl = it->_glyphGeometry->getFrontPrimitiveSetList();
645
655
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=psl.begin(), end=psl.end(); itr!=end; ++itr)
646
656
            {
647
657
                (*itr)->draw(state, false);
648
658
            }
649
 
            glPopMatrix();
650
659
        }
651
660
    }
652
 
    
 
661
 
653
662
 
654
663
    // ** render all wall face of the text
655
664
    for (itLine = _textRenderInfo.begin(); itLine!=endLine; ++itLine)
658
667
        LineRenderInfo::const_iterator it, end = itLine->end();
659
668
        for (it = itLine->begin(); it!=end; ++it)
660
669
        {
661
 
            glPushMatrix();
662
 
            glTranslatef(it->_position.x(), it->_position.y(), it->_position.z());
663
 
            state.setVertexPointer(it->_glyph->getVertexArray());
664
 
            state.setNormalPointer(it->_glyph->getNormalArray());
665
 
            
666
 
            osg::Geometry::PrimitiveSetList & psl = it->_glyph->getWallPrimitiveSetList();
 
670
            osg::Matrix matrix(original_modelview);
 
671
            matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
 
672
            state.applyModelViewMatrix(matrix);
 
673
 
 
674
            state.setVertexPointer(it->_glyphGeometry->getVertexArray());
 
675
            state.setNormalPointer(it->_glyphGeometry->getNormalArray());
 
676
 
 
677
            const osg::Geometry::PrimitiveSetList & psl = it->_glyphGeometry->getWallPrimitiveSetList();
667
678
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=psl.begin(), end=psl.end(); itr!=end; ++itr)
668
679
            {
669
680
                (*itr)->draw(state, false);
670
681
            }
671
 
            glPopMatrix();
672
682
        }
673
683
    }
 
684
#if 0
 
685
    state.disableNormalPointer();
674
686
 
675
 
    
676
687
    // ** render all back face of the text
677
 
    glNormal3f(0.0f,0.0f,-1.0f);
678
 
                
 
688
    state.Normal(0.0f,0.0f,-1.0f);
 
689
#endif
679
690
    for (itLine = _textRenderInfo.begin(); itLine!=endLine; ++itLine)
680
691
    {
681
692
        // ** for each glyph in the line, do ...
682
693
        LineRenderInfo::const_iterator it, end = itLine->end();
683
694
        for (it = itLine->begin(); it!=end; ++it)
684
695
        {
685
 
            glPushMatrix();
686
 
            glTranslatef(it->_position.x(), it->_position.y(), it->_position.z());
687
 
            state.setVertexPointer(it->_glyph->getVertexArray());
688
 
            
 
696
            osg::Matrix matrix(original_modelview);
 
697
            matrix.preMultTranslate(osg::Vec3d(it->_position.x(), it->_position.y(), it->_position.z()));
 
698
            state.applyModelViewMatrix(matrix);
 
699
 
 
700
            state.setVertexPointer(it->_glyphGeometry->getVertexArray());
 
701
            state.setNormalPointer(it->_glyphGeometry->getNormalArray());
 
702
 
689
703
            // ** render the back face of the glyph
690
 
            osg::Geometry::PrimitiveSetList & psl = it->_glyph->getBackPrimitiveSetList();
 
704
            const osg::Geometry::PrimitiveSetList & psl = it->_glyphGeometry->getBackPrimitiveSetList();
691
705
            for(osg::Geometry::PrimitiveSetList::const_iterator itr=psl.begin(), end=psl.end(); itr!=end; ++itr)
692
706
            {
693
707
                (*itr)->draw(state, false);
694
708
            }
695
 
            glPopMatrix();
696
709
        }
697
710
    }
698
711
}
709
722
void Text3D::resizeGLObjectBuffers(unsigned int maxSize)
710
723
{
711
724
    TextBase::resizeGLObjectBuffers(maxSize);
712
 
    
 
725
 
713
726
    if (_font.valid()) _font->resizeGLObjectBuffers(maxSize);
714
727
}
715
728
 
716
729
void Text3D::releaseGLObjects(osg::State* state) const
717
730
{
718
731
    TextBase::releaseGLObjects(state);
719
 
    
 
732
 
720
733
    if (_font.valid()) _font->releaseGLObjects(state);
721
734
}
722
735