~ubuntu-branches/debian/squeeze/stellarium/squeeze

« back to all changes in this revision

Viewing changes to src/typeface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// __________________________________________________________________________________________________
2
 
//    GLGooey Graphical User Interface for OpenGL
3
 
//    Copyright (c) 2004 Niel Waldren
4
 
//
5
 
// This software is provided 'as-is', without any express or implied warranty. In no event will
6
 
// the authors be held liable for any damages arising from the use of this software.
7
 
//
8
 
// Permission is granted to anyone to use this software for any purpose, including commercial
9
 
// applications, and to alter it and redistribute it freely, subject to the following restrictions:
10
 
//
11
 
//     1. The origin of this software must not be misrepresented; you must not claim that you
12
 
//        wrote the original software. If you use this software in a product, an acknowledgment
13
 
//        in the product documentation would be appreciated but is not required.
14
 
//
15
 
//     2. Altered source versions must be plainly marked as such, and must not be misrepresented
16
 
//        as being the original software.
17
 
//
18
 
//     3. This notice may not be removed or altered from any source distribution.
19
 
//
20
 
// __________________________________________________________________________________________________
21
 
 
22
 
#ifdef GOOEY_MMGR
23
 
#include "mmgr/nommgr.h"
24
 
#endif
25
 
 
26
 
#include <cstddef>
27
 
#include <vector>
28
 
#include <map>
29
 
#include <iostream>
30
 
#include <fstream>
31
 
#include <cassert>
32
 
#include <algorithm>
33
 
 
34
 
#include <ft2build.h>
35
 
#include FT_FREETYPE_H
36
 
#include FT_GLYPH_H
37
 
#include "SDL_opengl.h"
38
 
 
39
 
#include "typeface.h"
40
 
 
41
 
 
42
 
 
43
 
// *************************************************************************************************
44
 
//  Data would be hidden inside an anonymous namespace, but msvc 6 can't handle it
45
 
//namespace 
46
 
//{
47
 
static FT_Library ftlibrary_;
48
 
static bool FTinitialized=false; 
49
 
 
50
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
51
 
//! Information on a cached glyph
52
 
struct CacheEntry
53
 
{
54
 
    size_t        textureIndex_;   //!< Index into the TypeFace's texture array
55
 
    Vec2f topLeftUV_;      //!< Texcoords for the top left corner of the glyph
56
 
    Vec2f bottomRightUV_;  //!< Texcoords for the bottom right corner of the glyph
57
 
    Vec2size_t    renderSize_;     //!< The size of the glyph in pixels on the render target
58
 
    Vec2f advance_;        //!< The character advance for the cached glyph
59
 
    Vec2f bitmapPosition_; //!< The offset from the pen to the top left corner of the bitmap
60
 
};
61
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
62
 
 
63
 
 
64
 
 
65
 
 
66
 
 
67
 
 
68
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
69
 
//! Information on a cache texture
70
 
struct TextureInfo
71
 
{
72
 
    typedef GLuint Handle; //!< typedef for a texture handle for an OpenGL texture
73
 
    Handle     handle_;    //!< The OpenGL texture handle
74
 
    Vec2size_t size_;      //!< The size of the texture in texels
75
 
};
76
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
77
 
 
78
 
 
79
 
 
80
 
 
81
 
 
82
 
 
83
 
 
84
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
85
 
typedef std::vector<TextureInfo>      Textures;       //!< An array of textures
86
 
typedef std::map<FT_UInt, CacheEntry> CharacterCache; //!< The cache maps glyphs to cache entries
87
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
88
 
 
89
 
 
90
 
 
91
 
 
92
 
 
93
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
94
 
//! Implementation data for the TypeFace class
95
 
struct Data
96
 
{
97
 
    //! Constructor
98
 
    Data(size_t aPointSize, size_t aResolution) :
99
 
        pointSize_(aPointSize), resolution_(aResolution) {}
100
 
 
101
 
    size_t         pointSize_;      //!< The size of the type face in points
102
 
    size_t         resolution_;     //!< The resolution of the type face in dpi
103
 
    FT_Face        face_;           //!< A handle to the FreeType face object
104
 
    CharacterCache characterCache_; //!< The glyph cache
105
 
    Vec2i offset_;         //!< The top left corner of the next glyph to be cached
106
 
    Textures       textures_;       //!< The texture sizes and handles obtained from OpenGL
107
 
    bool           hasKerning_;     //!< Set to true if the type face supports kerning
108
 
};
109
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
110
 
 
111
 
 
112
 
 
113
 
 
114
 
 
115
 
 
116
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
117
 
const size_t glyphPadding = 1;     //!< Glyphs have padding around them to prevent filtering issues
118
 
const float OneOver64 = 0.015625f; //!< A frequently used constant
119
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
120
 
 
121
 
 
122
 
 
123
 
 
124
 
 
125
 
 
126
 
 
127
 
 
128
 
 
129
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
130
 
//! Returns the value of the next highest power of 2 above \c aValue
131
 
size_t
132
 
nextHighestPowerOf2(size_t aValue)
133
 
{
134
 
    size_t v = aValue - 1;
135
 
    for(int i = 16; i > 0; i >>= 1) v |= (v >> i);
136
 
    return (v + 1);
137
 
}
138
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
139
 
 
140
 
 
141
 
 
142
 
 
143
 
 
144
 
 
145
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
146
 
//! Returns the maximum texture size permitted by the OpenGL implementation
147
 
size_t
148
 
maximumTextureSize()
149
 
{
150
 
    GLint maxSize = 0;
151
 
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
152
 
    if(maxSize > 512) maxSize = 512;
153
 
    return static_cast<size_t>(maxSize);
154
 
}
155
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
156
 
 
157
 
 
158
 
 
159
 
 
160
 
 
161
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
162
 
//! returns the width to use for a cache texture
163
 
size_t
164
 
cacheTextureWidth(size_t aGlyphWidth, size_t numGlyphsToCache)
165
 
{
166
 
    size_t width = nextHighestPowerOf2( ( (aGlyphWidth + glyphPadding) * numGlyphsToCache) + glyphPadding );
167
 
    if(width > maximumTextureSize()) width = maximumTextureSize();
168
 
    return width;
169
 
}
170
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
171
 
 
172
 
 
173
 
 
174
 
 
175
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
176
 
//! Returns the size to use for a cache texture
177
 
Vec2size_t
178
 
cacheTextureSize(const Vec2size_t& aGlyphSize, size_t numGlyphsToCache)
179
 
{
180
 
    size_t textureWidth = cacheTextureWidth(aGlyphSize[0], numGlyphsToCache);
181
 
 
182
 
    const size_t numGlyphsInRow = (textureWidth - (glyphPadding * 2)) / aGlyphSize[0];
183
 
    const size_t requiredHeight = ((numGlyphsToCache / numGlyphsInRow) + 1) * aGlyphSize[1];
184
 
    size_t textureHeight = nextHighestPowerOf2(requiredHeight);
185
 
    if(textureHeight > maximumTextureSize()) textureHeight = maximumTextureSize();
186
 
 
187
 
    return Vec2size_t(textureWidth, textureHeight);
188
 
}
189
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190
 
 
191
 
 
192
 
 
193
 
 
194
 
 
195
 
 
196
 
 
197
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
198
 
//! Pushes the pixel storage client attributes and sets them to a standard packing mode
199
 
void
200
 
setPixelStorage()
201
 
{
202
 
    glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
203
 
 
204
 
    // Enforce a standard packing mode
205
 
    glPixelStorei( GL_UNPACK_SWAP_BYTES, GL_FALSE );
206
 
    glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
207
 
    glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
208
 
    glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
209
 
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
210
 
 
211
 
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
212
 
}
213
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
214
 
 
215
 
 
216
 
 
217
 
 
218
 
 
219
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
220
 
//! Pops the pixel storage client attributes off the stack
221
 
void
222
 
restorePixelStorage()
223
 
{
224
 
    glPopClientAttrib();
225
 
}
226
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
227
 
 
228
 
 
229
 
 
230
 
 
231
 
 
232
 
 
233
 
 
234
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
235
 
//! Creates a texture for caching pre-rendered glyphs
236
 
TextureInfo
237
 
createCacheTexture(const Vec2size_t& aGlyphSize, size_t numGlyphsToCache)
238
 
{
239
 
    setPixelStorage();
240
 
    
241
 
    TextureInfo info;
242
 
    glGenTextures(1, &info.handle_);
243
 
 
244
 
    glBindTexture(GL_TEXTURE_2D, info.handle_);
245
 
 
246
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
247
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
248
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
249
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
250
 
 
251
 
    info.size_ = cacheTextureSize(aGlyphSize, numGlyphsToCache);
252
 
 
253
 
    unsigned char* textureData = new unsigned char[info.size_[0]*info.size_[1]];
254
 
    memset(textureData, 0, info.size_[0]*info.size_[1]);
255
 
 
256
 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, info.size_[0], info.size_[1], 0, GL_ALPHA,
257
 
                 GL_UNSIGNED_BYTE, textureData);
258
 
 
259
 
    restorePixelStorage();
260
 
 
261
 
    delete[] textureData;
262
 
 
263
 
    return info;
264
 
}
265
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
266
 
 
267
 
 
268
 
 
269
 
 
270
 
 
271
 
 
272
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
273
 
//! \brief Blits the glyph represented by the passed in bitmap to the texture described by
274
 
//! the passed in texture info.
275
 
void
276
 
blitGlyphToTexture(FT_Bitmap* aBitmap, const TextureInfo& aTextureInfo,
277
 
                   const Vec2i& anOffset, const Vec2size_t& aGlyphSize)
278
 
{
279
 
    setPixelStorage();
280
 
 
281
 
    const int pitch = aBitmap->pitch;
282
 
    unsigned char* data = new unsigned char[aGlyphSize[0]*aGlyphSize[1]];
283
 
 
284
 
    for(size_t y = 0; y < aGlyphSize[1]; ++y)
285
 
    {
286
 
        memcpy(data + (y * aGlyphSize[0]), aBitmap->buffer + (y * pitch), aGlyphSize[0]);
287
 
    }
288
 
 
289
 
    glBindTexture(GL_TEXTURE_2D, aTextureInfo.handle_);
290
 
    glTexSubImage2D(GL_TEXTURE_2D, 0, anOffset[0], anOffset[1],
291
 
        aGlyphSize[0], aGlyphSize[1], GL_ALPHA, GL_UNSIGNED_BYTE, data);
292
 
 
293
 
    delete[] data;
294
 
 
295
 
    restorePixelStorage();
296
 
}
297
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
298
 
 
299
 
 
300
 
 
301
 
 
302
 
 
303
 
 
304
 
 
305
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
306
 
//! Functor for destroying OpenGL textures
307
 
struct DestroyTexture
308
 
{
309
 
    //! The function that does the destroying
310
 
    void
311
 
    operator() (const TextureInfo& info)
312
 
    {
313
 
        glDeleteTextures(1, &info.handle_);
314
 
    }
315
 
};
316
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
317
 
 
318
 
 
319
 
 
320
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
321
 
TypeFace::TypeFace(const std::string& aFaceName, size_t aPointSize, size_t aResolution) :
322
 
    data_(new Data(aPointSize, aResolution))
323
 
{
324
 
        std::ifstream ifs(aFaceName.c_str(), std::ios_base::in);
325
 
        if (!ifs.is_open())
326
 
        {
327
 
                std::cerr << "Error: cannot find TTF font file " << aFaceName << "." << std::endl;
328
 
        }
329
 
        ifs.close();
330
 
        
331
 
        if (FTinitialized==false)
332
 
        {
333
 
                assert(FT_Init_FreeType(&ftlibrary_) == 0);
334
 
                FTinitialized = true;
335
 
        }
336
 
 
337
 
    assert(FT_New_Face(ftlibrary_, aFaceName.c_str(), 0, &data_->face_) == 0);
338
 
 
339
 
    assert(FT_IS_SCALABLE(data_->face_));
340
 
    assert(FT_IS_SFNT(data_->face_));
341
 
 
342
 
    if( !data_->face_->charmap) FT_Set_Charmap(data_->face_, data_->face_->charmaps[0]);
343
 
 
344
 
    assert(FT_Set_Char_Size(data_->face_, 0L, static_cast<FT_F26Dot6>(aPointSize << 6), aResolution, aResolution) == 0);
345
 
 
346
 
    data_->hasKerning_ = (FT_HAS_KERNING(data_->face_) != 0);
347
 
}
348
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
349
 
 
350
 
 
351
 
 
352
 
 
353
 
 
354
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
355
 
TypeFace::~TypeFace()
356
 
{
357
 
    flushCache();
358
 
    FT_Done_Face(data_->face_);
359
 
    delete data_;
360
 
}
361
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
362
 
 
363
 
 
364
 
 
365
 
 
366
 
 
367
 
 
368
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
369
 
size_t
370
 
TypeFace::pointSize() const
371
 
{
372
 
    return data_->pointSize_;
373
 
}
374
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
375
 
 
376
 
 
377
 
 
378
 
 
379
 
 
380
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
381
 
void
382
 
TypeFace::setPointSize(size_t aPointSize)
383
 
{
384
 
    if(data_->pointSize_ != aPointSize)
385
 
    {
386
 
        data_->pointSize_ = aPointSize;
387
 
        const FT_F26Dot6 sz = static_cast<FT_F26Dot6>(aPointSize << 6);
388
 
        assert(FT_Set_Char_Size(data_->face_, sz, sz, data_->resolution_, data_->resolution_) == 0);
389
 
        flushCache();
390
 
    }
391
 
}
392
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
393
 
 
394
 
 
395
 
 
396
 
 
397
 
 
398
 
 
399
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
400
 
size_t
401
 
TypeFace::resolution() const
402
 
{
403
 
    return data_->resolution_;
404
 
}
405
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
406
 
 
407
 
 
408
 
 
409
 
 
410
 
 
411
 
 
412
 
 
413
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
414
 
void
415
 
TypeFace::setResolution(size_t aResolution)
416
 
{
417
 
    if(data_->resolution_ != aResolution)
418
 
    {
419
 
        data_->resolution_ = aResolution;
420
 
        flushCache();
421
 
    }
422
 
}
423
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
424
 
 
425
 
 
426
 
 
427
 
 
428
 
 
429
 
 
430
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
431
 
float TypeFace::ascent() const
432
 
{
433
 
    return float(data_->face_->size->metrics.y_ppem) - descent();
434
 
}
435
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
436
 
 
437
 
 
438
 
 
439
 
 
440
 
 
441
 
 
442
 
 
443
 
 
444
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
445
 
float TypeFace::descent() const
446
 
{
447
 
    return static_cast<float>(-data_->face_->size->metrics.descender) * OneOver64;
448
 
}
449
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
450
 
 
451
 
 
452
 
 
453
 
 
454
 
 
455
 
 
456
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
457
 
float TypeFace::lineHeight() const
458
 
{
459
 
    return (ascent() + descent());
460
 
}
461
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
462
 
 
463
 
 
464
 
 
465
 
 
466
 
 
467
 
 
468
 
 
469
 
 
470
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
471
 
void
472
 
TypeFace::flushCache()
473
 
{
474
 
    data_->characterCache_.clear();
475
 
    std::for_each(data_->textures_.begin(), data_->textures_.end(), DestroyTexture());
476
 
    data_->textures_.clear();
477
 
}
478
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
479
 
 
480
 
 
481
 
 
482
 
 
483
 
 
484
 
 
485
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
486
 
void
487
 
TypeFace::addNewTexture(const Vec2size_t& aGlyphSize)
488
 
{
489
 
    const size_t numUncachedGlyphs = data_->face_->num_glyphs - data_->characterCache_.size();
490
 
    data_->textures_.push_back(createCacheTexture(aGlyphSize, numUncachedGlyphs));
491
 
    data_->offset_.set(glyphPadding, glyphPadding);
492
 
}
493
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
494
 
 
495
 
 
496
 
 
497
 
 
498
 
 
499
 
 
500
 
 
501
 
 
502
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
503
 
void
504
 
TypeFace::render(const std::wstring& aString, const Vec2f& aPosition, bool upsideDown)
505
 
{
506
 
    glPushAttrib(GL_ALL_ATTRIB_BITS);
507
 
    glEnable(GL_TEXTURE_2D);
508
 
 
509
 
    glEnable(GL_BLEND);
510
 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
511
 
 
512
 
    glPushMatrix();
513
 
    glTranslatef(aPosition[0], aPosition[1], 0.0f);
514
 
        if (upsideDown)
515
 
        {
516
 
                glScalef(1.f, -1.f, 1.f);
517
 
                glTranslatef(0.f, lineHeight(), 0.f);
518
 
        }
519
 
 
520
 
    renderGlyphs(aString);
521
 
 
522
 
    glPopMatrix();
523
 
 
524
 
    glPopAttrib();
525
 
}
526
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
527
 
 
528
 
 
529
 
 
530
 
 
531
 
 
532
 
 
533
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
534
 
float
535
 
TypeFace::width(const std::wstring& aString)
536
 
{
537
 
    std::wstring::const_iterator it = aString.begin();
538
 
    float ret = 0;
539
 
    size_t leftChar = 0;
540
 
    while(it != aString.end())
541
 
    {
542
 
        size_t rightChar = FT_Get_Char_Index(data_->face_, *it);
543
 
        ret += kerning(leftChar, rightChar)[0];
544
 
        ret += cachedGlyph(rightChar).advance_[0];
545
 
        leftChar = rightChar;
546
 
        ++it;
547
 
    }
548
 
    return ret;
549
 
}
550
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
551
 
 
552
 
 
553
 
 
554
 
 
555
 
 
556
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
557
 
size_t TypeFace::hitCharacterIndex(const std::wstring& aString, float anOffset)
558
 
{
559
 
    bool found = false;
560
 
 
561
 
    size_t characterIndex = 0;
562
 
    if(anOffset >= 0.0f)
563
 
    {
564
 
        std::wstring::const_iterator it = aString.begin();
565
 
        float oldOffset = 0;
566
 
        size_t leftChar = 0;
567
 
        while(it != aString.end())
568
 
        {
569
 
            float newOffset = oldOffset;
570
 
            size_t rightChar = FT_Get_Char_Index(data_->face_, *it);
571
 
            newOffset += kerning(leftChar, rightChar)[0];
572
 
            newOffset += cachedGlyph(rightChar).advance_[0];
573
 
 
574
 
            if( (newOffset >= anOffset) && (oldOffset <= anOffset) )
575
 
            {
576
 
                found = true;
577
 
                break;
578
 
            }
579
 
 
580
 
            leftChar = rightChar;
581
 
            oldOffset = newOffset;
582
 
            ++characterIndex;
583
 
            ++it;
584
 
        }
585
 
    }
586
 
 
587
 
    if(!found) characterIndex = std::wstring::npos;
588
 
 
589
 
    return characterIndex;
590
 
}
591
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
592
 
 
593
 
 
594
 
 
595
 
 
596
 
 
597
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
598
 
void
599
 
TypeFace::addCacheEntry(FT_GlyphRec_* aGlyph, FT_BitmapGlyphRec_* aBitmapGlyph, size_t aGlyphIndex, const Vec2size_t& aGlyphSize)
600
 
{
601
 
    const TextureInfo& textureInfo = *(data_->textures_.rbegin());
602
 
 
603
 
    const Vec2i bottomRight = data_->offset_ + Vec2i(aGlyphSize[0], aGlyphSize[1]);
604
 
 
605
 
    const float widthF  = static_cast<float>(textureInfo.size_[0]);
606
 
    const float heightF = static_cast<float>(textureInfo.size_[1]);
607
 
 
608
 
    CacheEntry entry;
609
 
    entry.topLeftUV_[0]=static_cast<float>(data_->offset_[0]) / widthF;
610
 
    entry.topLeftUV_[1]=static_cast<float>(data_->offset_[1]) / heightF;
611
 
    entry.bottomRightUV_[0]=static_cast<float>(bottomRight[0]) / widthF;
612
 
    entry.bottomRightUV_[1]=static_cast<float>(bottomRight[1]) / heightF;
613
 
    entry.textureIndex_ = data_->textures_.size() - 1;
614
 
    entry.renderSize_ = aGlyphSize;
615
 
    entry.advance_ = Vec2f(float(aGlyph->advance.x >> 16), float(aGlyph->advance.y >> 16));
616
 
    entry.bitmapPosition_ = Vec2f(float(aBitmapGlyph->left), float(-aBitmapGlyph->top));
617
 
 
618
 
    data_->characterCache_[aGlyphIndex] = entry;
619
 
}
620
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
621
 
 
622
 
 
623
 
 
624
 
 
625
 
 
626
 
 
627
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
628
 
Vec2size_t
629
 
TypeFace::maximumGlyphSize() const
630
 
{
631
 
    FT_BBox& bounds = data_->face_->bbox;
632
 
    float maxGlyphWidthF = static_cast<float>(bounds.xMax - bounds.xMin);
633
 
    maxGlyphWidthF *= ( (float)data_->face_->size->metrics.x_ppem / (float)data_->face_->units_per_EM);
634
 
 
635
 
    float maxGlyphHeightF = static_cast<float>(bounds.yMax - bounds.yMin);
636
 
    maxGlyphHeightF *= ( (float)data_->face_->size->metrics.y_ppem / (float)data_->face_->units_per_EM);
637
 
 
638
 
    return Vec2size_t(static_cast<size_t>(maxGlyphWidthF), static_cast<size_t>(maxGlyphHeightF));
639
 
}
640
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
641
 
 
642
 
 
643
 
 
644
 
 
645
 
 
646
 
 
647
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
648
 
void
649
 
TypeFace::ensureTextureIsAvailable()
650
 
{
651
 
    const Vec2size_t maxGlyphSize = maximumGlyphSize();
652
 
 
653
 
    if(data_->textures_.size() == 0)
654
 
    {
655
 
        addNewTexture(maxGlyphSize);
656
 
    }
657
 
    else
658
 
    {
659
 
        const TextureInfo& textureInfo = *(data_->textures_.rbegin());
660
 
        if(data_->offset_[0] > int(textureInfo.size_[0]) - int(maxGlyphSize[0]) - int(glyphPadding))
661
 
        {
662
 
            data_->offset_.set(glyphPadding, data_->offset_[1] + maxGlyphSize[1]);
663
 
 
664
 
            if(data_->offset_[1] > int(textureInfo.size_[1]) - int(maxGlyphSize[1]) - int(glyphPadding))
665
 
            {
666
 
                addNewTexture(maxGlyphSize);
667
 
            }
668
 
        }
669
 
    }
670
 
}
671
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
672
 
 
673
 
 
674
 
 
675
 
 
676
 
 
677
 
 
678
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
679
 
void
680
 
TypeFace::cacheGlyph(size_t aGlyphIndex)
681
 
{
682
 
    ensureTextureIsAvailable();
683
 
 
684
 
    const FT_Error loadError = FT_Load_Glyph(data_->face_, aGlyphIndex, FT_LOAD_NO_HINTING);
685
 
    assert(loadError == 0);
686
 
    if(loadError == 0)
687
 
    {
688
 
        FT_Glyph glyph;
689
 
        const FT_Error getError = FT_Get_Glyph( data_->face_->glyph, &glyph);
690
 
        if(getError == 0)
691
 
        {
692
 
            const FT_Error bitmapError = FT_Glyph_To_Bitmap( &glyph, FT_RENDER_MODE_NORMAL, 0, 1);
693
 
            if( (bitmapError == 0) && (glyph->format == FT_GLYPH_FORMAT_BITMAP) )
694
 
            {
695
 
                FT_BitmapGlyph bitmapGlyph = reinterpret_cast<FT_BitmapGlyph>(glyph);
696
 
                FT_Bitmap* bitmap = &bitmapGlyph->bitmap;
697
 
 
698
 
                const Vec2size_t glyphSize(bitmap->width, bitmap->rows);
699
 
 
700
 
                if(glyphSize[0]*glyphSize[1] > 0)
701
 
                    blitGlyphToTexture(bitmap, *(data_->textures_.rbegin()), data_->offset_, glyphSize);
702
 
 
703
 
                addCacheEntry(glyph, bitmapGlyph, aGlyphIndex, glyphSize);
704
 
                data_->offset_[0]=data_->offset_[0] + glyphSize[0] + glyphPadding;
705
 
            }
706
 
        }
707
 
        FT_Done_Glyph(glyph);
708
 
    }
709
 
}
710
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
711
 
 
712
 
 
713
 
 
714
 
 
715
 
 
716
 
 
717
 
 
718
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
719
 
CacheEntry&
720
 
TypeFace::cachedGlyph(size_t aGlyphIndex)
721
 
{
722
 
    CharacterCache::iterator cacheIterator = data_->characterCache_.find(aGlyphIndex);
723
 
    if(cacheIterator == data_->characterCache_.end())
724
 
    {
725
 
        cacheGlyph(aGlyphIndex);
726
 
        cacheIterator = data_->characterCache_.find(aGlyphIndex);
727
 
        assert(cacheIterator != data_->characterCache_.end());
728
 
    }
729
 
    return cacheIterator->second;
730
 
}
731
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
732
 
 
733
 
 
734
 
 
735
 
 
736
 
 
737
 
 
738
 
 
739
 
 
740
 
 
741
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
742
 
Vec2f
743
 
TypeFace::renderGlyph(size_t aGlyphIndex, const Vec2f& aPosition)
744
 
{
745
 
    CacheEntry& entry = cachedGlyph(aGlyphIndex);
746
 
 
747
 
    bindTexture(entry);
748
 
 
749
 
    const Vec2f position = aPosition + entry.bitmapPosition_;
750
 
 
751
 
    const Vec2f topLeftUV     = entry.topLeftUV_;
752
 
    const Vec2f bottomRightUV = entry.bottomRightUV_;
753
 
    const float glyphWidth  = static_cast<float>(entry.renderSize_[0]);
754
 
    const float glyphHeight = static_cast<float>(entry.renderSize_[1]);
755
 
 
756
 
    glBegin(GL_QUADS);
757
 
    {
758
 
        glTexCoord2f( topLeftUV[0], topLeftUV[1] );
759
 
        glVertex2f( position[0], position[1] );
760
 
 
761
 
        glTexCoord2f( bottomRightUV[0], topLeftUV[1] );
762
 
        glVertex2f( position[0] + glyphWidth, position[1] );
763
 
 
764
 
        glTexCoord2f( bottomRightUV[0], bottomRightUV[1] );
765
 
        glVertex2f( position[0] + glyphWidth, position[1] + glyphHeight );
766
 
 
767
 
        glTexCoord2f( topLeftUV[0], bottomRightUV[1] );
768
 
        glVertex2f( position[0], position[1] + glyphHeight );
769
 
    }
770
 
    glEnd();
771
 
 
772
 
    return entry.advance_;
773
 
}
774
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
775
 
 
776
 
 
777
 
 
778
 
 
779
 
 
780
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
781
 
Vec2f
782
 
TypeFace::kerning(size_t leftGlyphIndex, size_t rightGlyphIndex) const
783
 
{
784
 
    Vec2f ret;
785
 
    if(data_->hasKerning_ && (leftGlyphIndex > 0) && (rightGlyphIndex > 0))
786
 
    {
787
 
        FT_Vector kerningVector;
788
 
        FT_Error ftError = FT_Get_Kerning(data_->face_, leftGlyphIndex, rightGlyphIndex, FT_KERNING_DEFAULT, &kerningVector);
789
 
        if(!ftError)
790
 
        {
791
 
            ret = Vec2f(static_cast<float>(kerningVector.x),
792
 
                                static_cast<float>(kerningVector.y)) * OneOver64;
793
 
        }
794
 
    }
795
 
    return ret;
796
 
}
797
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
798
 
 
799
 
 
800
 
 
801
 
 
802
 
 
803
 
 
804
 
 
805
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
806
 
void
807
 
TypeFace::bindTexture(const CacheEntry& aCacheEntry) const
808
 
{
809
 
    assert(aCacheEntry.textureIndex_ < data_->textures_.size());
810
 
    const TextureInfo& info = data_->textures_[aCacheEntry.textureIndex_];
811
 
 
812
 
    GLint currentTextureID = -1;
813
 
    glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &currentTextureID);
814
 
    if(static_cast<GLuint>(currentTextureID) != info.handle_)
815
 
    {
816
 
        glBindTexture(GL_TEXTURE_2D, info.handle_);
817
 
    }
818
 
}
819
 
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
820
 
 
821
 
 
822
 
 
823
 
 
824
 
 
825
 
 
826
 
 
827
 
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
828
 
void
829
 
TypeFace::renderGlyphs(const std::wstring& aString)
830
 
{
831
 
    Vec2f pos(0.0f, 0.0f);
832
 
    size_t leftChar = 0;
833
 
    std::wstring::const_iterator it = aString.begin();
834
 
    while(it != aString.end())
835
 
    {
836
 
        size_t rightChar = FT_Get_Char_Index(data_->face_, *it);
837
 
        pos += kerning(leftChar, rightChar);
838
 
        pos += renderGlyph(rightChar, pos);
839
 
        leftChar = rightChar;
840
 
        ++it;
841
 
    }
842
 
}
843