~ubuntu-branches/debian/wheezy/gource/wheezy

« back to all changes in this revision

Viewing changes to src/core/fxfont.cpp

  • Committer: Package Import Robot
  • Author(s): Andrew Caudwell
  • Date: 2012-04-24 11:25:45 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20120424112545-18fbnycu9xrsl4s5
Tags: 0.38-1
* New upstream release (closes: #667189)
* New build dependencies on libglm-dev and libboost-filesystem-dev. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
 
65
65
    FT_Face ftface = set->getFTFace();
66
66
 
67
 
    if(FT_Load_Glyph( ftface, FT_Get_Char_Index( ftface, chr ), FT_LOAD_DEFAULT ))
 
67
    FT_UInt index = FT_Get_Char_Index( ftface, chr );
 
68
 
 
69
    //debugLog("FXGlyph %x %d %d", chr, chr, index);
 
70
 
 
71
    if(FT_Load_Glyph( ftface, index, FT_LOAD_DEFAULT ))
68
72
    throw FXFontException(ftface->family_name);
69
73
 
70
74
    FT_Glyph ftglyph;
76
80
 
77
81
    glyph_bitmap = (FT_BitmapGlyph)ftglyph;
78
82
 
79
 
    dims    = vec2f( glyph_bitmap->bitmap.width, glyph_bitmap->bitmap.rows) + vec2f(2.0f, 2.0f);
80
 
    corner  = vec2f( glyph_bitmap->left, -glyph_bitmap->top) + vec2f(0.5, -0.5);
81
 
    advance = vec2f( ftface->glyph->advance.x >> 6, ftface->glyph->advance.y >> 6);
 
83
    dims    = vec2( glyph_bitmap->bitmap.width, glyph_bitmap->bitmap.rows) + vec2(2.0f, 2.0f);
 
84
    corner  = vec2( glyph_bitmap->left, -glyph_bitmap->top) + vec2(0.5, -0.5);
 
85
    advance = vec2( ftface->glyph->advance.x >> 6, ftface->glyph->advance.y >> 6);
82
86
 
83
 
    vertex_positions[0] = vec2f(0.0f, 0.0f);
84
 
    vertex_positions[1] = vec2f(dims.x, 0.0f);
 
87
    vertex_positions[0] = vec2(0.0f, 0.0f);
 
88
    vertex_positions[1] = vec2(dims.x, 0.0f);
85
89
    vertex_positions[2] = dims;
86
 
    vertex_positions[3] = vec2f(0.0f, dims.y);
 
90
    vertex_positions[3] = vec2(0.0f, dims.y);
87
91
 
88
92
    //call_list = 0;
89
93
    page = 0;
90
94
}
91
95
 
92
 
void FXGlyph::setPage(FXGlyphPage* page, const vec4f& texcoords) {
 
96
void FXGlyph::setPage(FXGlyphPage* page, const vec4& texcoords) {
93
97
    this->page = page;
94
98
    this->texcoords = texcoords;
95
99
 
96
 
    vertex_texcoords[0] = vec2f(texcoords.x, texcoords.y);
97
 
    vertex_texcoords[1] = vec2f(texcoords.z, texcoords.y);
98
 
    vertex_texcoords[2] = vec2f(texcoords.z, texcoords.w);
99
 
    vertex_texcoords[3] = vec2f(texcoords.x, texcoords.w);
100
 
}
101
 
 
102
 
void FXGlyph::drawToVBO(quadbuf& buffer, const vec2f& pos, const vec4f& colour) const {
103
 
    buffer.add(page->textureid, pos + corner, dims, colour, texcoords);
104
 
}
105
 
 
106
 
void FXGlyph::draw(const vec2f& pos) const {
 
100
    vertex_texcoords[0] = vec2(texcoords.x, texcoords.y);
 
101
    vertex_texcoords[1] = vec2(texcoords.z, texcoords.y);
 
102
    vertex_texcoords[2] = vec2(texcoords.z, texcoords.w);
 
103
    vertex_texcoords[3] = vec2(texcoords.x, texcoords.w);
 
104
}
 
105
 
 
106
void FXGlyph::drawToVBO(quadbuf& buffer, const vec2& pos, const vec4& colour) const {
 
107
    buffer.add(page->texture->textureid, pos + corner, dims, colour, texcoords);
 
108
}
 
109
 
 
110
void FXGlyph::draw(const vec2& pos) const {
107
111
    for(int i=0;i<4;i++) {
108
 
        glTexCoord2fv(vertex_texcoords[i]);
109
 
        glVertex2fv(vertex_positions[i] + pos + corner);
 
112
        vec2 pos_offset = vertex_positions[i] + pos + corner;
 
113
 
 
114
        glTexCoord2fv(glm::value_ptr(vertex_texcoords[i]));
 
115
        glVertex2fv(glm::value_ptr(pos_offset));
 
116
 
110
117
    }
111
118
}
112
119
 
121
128
    memset(texture_data, 0, page_width * page_height);
122
129
 
123
130
    needs_update = false;
124
 
    textureid = 0;
125
 
    glGenTextures(1, &textureid);
 
131
    texture = 0;
126
132
 
127
133
    max_glyph_height = cursor_x = cursor_y = 1;
128
134
}
162
168
 
163
169
    //fprintf(stderr, "corner_x = %d, corner_y = %d\n", corner_x, corner_y);
164
170
 
165
 
    vec4f texcoords = vec4f( (((float)corner_x)-0.5f) / (float) page_width,
 
171
    vec4 texcoords = vec4( (((float)corner_x)-0.5f) / (float) page_width,
166
172
                             (((float)corner_y)-0.5f) / (float) page_height,
167
173
                             (((float)corner_x+bitmap->bitmap.width)+1.5f) / (float) page_width,
168
174
                             (((float)corner_y+bitmap->bitmap.rows)+1.5f) / (float) page_height );
181
187
void FXGlyphPage::updateTexture() {
182
188
    if(!needs_update) return;
183
189
 
184
 
    glBindTexture( GL_TEXTURE_2D, textureid);
185
 
 
186
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
187
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
188
 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
189
 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
190
 
 
191
 
    glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, page_width, page_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture_data );
 
190
    if(!texture) {
 
191
        texture = texturemanager.create(page_width, page_height, false, GL_CLAMP_TO_EDGE, GL_ALPHA, texture_data);
 
192
    } else {
 
193
        texture->reload();
 
194
    }
192
195
 
193
196
    needs_update = false;
194
197
}
236
239
    //add to bitmap without updating textures until the end
237
240
    pre_caching = true;
238
241
 
239
 
    while (chr = *precache_glyphs++) {
 
242
    while (*precache_glyphs) {
 
243
        chr  = *precache_glyphs++;
240
244
        getGlyph(chr);
241
245
    }
242
246
 
313
317
    return width;
314
318
}
315
319
 
316
 
void FXGlyphSet::drawToVBO(vec2f& cursor, const std::string& text, const vec4f& colour) {
 
320
void FXGlyphSet::drawToVBO(vec2& cursor, const std::string& text, const vec4& colour) {
317
321
    FTUnicodeStringItr<unsigned char> unicode_text((const unsigned char*)text.c_str());
318
322
 
319
323
    unsigned int chr;
335
339
 
336
340
    unsigned int chr;
337
341
 
338
 
    vec2f pos;
 
342
    vec2 pos;
339
343
 
340
344
    while (*unicode_text) {
341
345
        chr = *unicode_text++;
342
346
 
343
347
        FXGlyph* glyph = getGlyph(chr);
344
348
 
345
 
        if(glyph->page->textureid != textureid) {
 
349
        if(glyph->page->texture->textureid != textureid) {
346
350
            if(textureid != -1) glEnd();
347
 
            textureid = glyph->page->textureid;
 
351
            textureid = glyph->page->texture->textureid;
348
352
            glBindTexture(GL_TEXTURE_2D, textureid);
349
353
            glBegin(GL_QUADS);
350
354
        }
356
360
    if(textureid != -1) glEnd();
357
361
}
358
362
 
 
363
void FXGlyphSet::drawPages() {
 
364
    vec2 corner = vec2(0.0f);
 
365
 
 
366
    glPushMatrix();
 
367
 
 
368
    for(std::vector<FXGlyphPage*>::iterator it = pages.begin(); it != pages.end(); it++) {
 
369
        FXGlyphPage* page = *it;
 
370
        page->texture->bind();
 
371
 
 
372
        glBegin(GL_QUADS);
 
373
            glTexCoord2f(0.0f, 0.0f);
 
374
            glVertex2f(0.0f, 0.0f);
 
375
 
 
376
            glTexCoord2f(1.0f, 0.0f);
 
377
            glVertex2f(page->texture->w, 0.0f);
 
378
 
 
379
            glTexCoord2f(1.0f, 1.0f);
 
380
            glVertex2f(page->texture->w, page->texture->h);
 
381
 
 
382
            glTexCoord2f(0.0f, 1.0f);
 
383
            glVertex2f(0.0f, page->texture->h);
 
384
        glEnd();
 
385
 
 
386
        glTranslatef(page->texture->w, 0.0f, 0.0f);
 
387
    }
 
388
 
 
389
    glPopMatrix();
 
390
}
 
391
 
359
392
//FXFont
360
393
 
361
394
FXFont::FXFont() {
370
403
void FXFont::init() {
371
404
    shadow          = false;
372
405
    shadow_strength = 0.7;
373
 
    shadow_offset   = vec2f(1.0, 1.0);
 
406
    shadow_offset   = vec2(1.0, 1.0);
374
407
    round           = false;
375
408
    align_right     = false;
376
409
    align_top       = true;
377
410
 
378
 
    colour          = vec4f(1.0f, 1.0f, 1.0f, 1.0f);
379
 
    shadow_colour   = vec4f(0.0f, 0.0f, 0.0f, shadow_strength);
 
411
    colour          = vec4(1.0f, 1.0f, 1.0f, 1.0f);
 
412
    shadow_colour   = vec4(0.0f, 0.0f, 0.0f, shadow_strength);
380
413
}
381
414
 
382
415
void FXFont::roundCoordinates(bool round) {
396
429
    shadow_colour.w = colour.w * shadow_strength;
397
430
}
398
431
 
399
 
void FXFont::setColour(const vec4f& colour) {
 
432
void FXFont::setColour(const vec4& colour) {
400
433
    this->colour = colour;
401
434
    shadow_colour.w = colour.w * shadow_strength;
402
435
}
407
440
}
408
441
 
409
442
void FXFont::shadowOffset(float x, float y) {
410
 
    shadow_offset = vec2f(x,y);
 
443
    shadow_offset = vec2(x,y);
411
444
}
412
445
 
413
446
void FXFont::dropShadow(bool shadow) {
434
467
    return glyphset->getWidth(text);
435
468
}
436
469
 
437
 
void FXFont::render(float x, float y, const std::string& text, const vec4f& colour) const{
 
470
void FXFont::render(float x, float y, const std::string& text, const vec4& colour) const{
438
471
 
439
472
    if(fontmanager.use_vbo) {
440
 
        vec2f cursor_start(x,y);
 
473
        vec2 cursor_start(x,y);
441
474
        glyphset->drawToVBO(cursor_start, text, colour);
442
475
        return;
443
476
    }
444
477
 
445
 
    glColor4fv(colour);
 
478
    glColor4fv(glm::value_ptr(colour));
446
479
 
447
480
    glPushMatrix();
448
481
 
491
524
    render(x, y, text, colour);
492
525
}
493
526
 
 
527
void FXFont::drawGlyphes() {
 
528
    if(glyphset) glyphset->drawPages();
 
529
}
 
530
 
494
531
// FXFontManager
495
532
FXFontManager::FXFontManager() {
496
533
    library = 0;
502
539
    use_vbo = false;
503
540
}
504
541
 
 
542
void FXFontManager::unload() {
 
543
    font_vbo.unload();
 
544
}
 
545
 
 
546
void FXFontManager::reload() {
 
547
}
 
548
 
505
549
void FXFontManager::startBuffer() {
506
550
    font_vbo.reset();
507
551
    use_vbo = true;
540
584
 
541
585
FXFont FXFontManager::grab(std::string font_file, int size, int dpi) {
542
586
 
543
 
    char buf[256];
544
 
 
545
587
    if(font_dir.size()>0 && font_file[0] != '/') {
546
588
        font_file = font_dir + font_file;
547
589
    }