~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/blenfont/intern/blf_font.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
#include "DNA_vec_types.h"
45
45
 
46
 
 
47
 
#include "BLI_blenlib.h"
48
 
#include "BLI_linklist.h"       /* linknode */
 
46
#include "BLI_listbase.h"
49
47
#include "BLI_math.h"
 
48
#include "BLI_rect.h"
 
49
#include "BLI_string.h"
 
50
#include "BLI_string_utf8.h"
 
51
#include "BLI_linklist.h"  /* linknode */
50
52
 
51
53
#include "BIF_gl.h"
52
54
#include "BLF_api.h"
53
55
 
 
56
#include "IMB_colormanagement.h"
 
57
 
54
58
#include "blf_internal_types.h"
55
59
#include "blf_internal.h"
56
60
 
103
107
        if (glyph_ascii_table['0'] == NULL) {
104
108
                GlyphBLF *g;
105
109
                unsigned int i;
106
 
                for (i = 0; i<256; i++) {
 
110
                for (i = 0; i < 256; i++) {
107
111
                        g = blf_glyph_search(font->glyph_cache, i);
108
112
                        if (!g) {
109
113
                                FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
132
136
                        _g = blf_glyph_add(_font,                                            \
133
137
                                          FT_Get_Char_Index((_font)->face, _c), _c);         \
134
138
                }                                                                        \
135
 
        }                                                                            \
 
139
        } (void)0
136
140
 
137
141
 
138
142
#define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode)                        \
152
156
                                   _kern_mode,                                           \
153
157
                                   &(_delta)) == 0)                                      \
154
158
                {                                                                        \
155
 
                        _pen_x += delta.x >> 6;                                              \
 
159
                        _pen_x += _delta.x >> 6;                                             \
156
160
                }                                                                        \
157
161
        }                                                                            \
158
 
}                                                                                \
 
162
} (void)0
159
163
 
160
 
void blf_font_draw(FontBLF *font, const char *str, unsigned int len)
 
164
void blf_font_draw(FontBLF *font, const char *str, size_t len)
161
165
{
162
166
        unsigned int c;
163
167
        GlyphBLF *g, *g_prev = NULL;
170
174
 
171
175
        blf_font_ensure_ascii_table(font);
172
176
 
173
 
        while (str[i] && i < len) {
 
177
        while ((i < len) && str[i]) {
174
178
                BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
175
179
 
176
180
                if (c == BLI_UTF8_ERR)
189
193
}
190
194
 
191
195
/* faster version of blf_font_draw, ascii only for view dimensions */
192
 
void blf_font_draw_ascii(FontBLF *font, const char *str, unsigned int len)
 
196
void blf_font_draw_ascii(FontBLF *font, const char *str, size_t len)
193
197
{
194
198
        unsigned char c;
195
199
        GlyphBLF *g, *g_prev = NULL;
225
229
        size_t i = 0;
226
230
        GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
227
231
 
228
 
        /* buffer specific vars*/
229
 
        const unsigned char b_col_char[4] = {font->b_col[0] * 255,
230
 
                                             font->b_col[1] * 255,
231
 
                                             font->b_col[2] * 255,
232
 
                                             font->b_col[3] * 255};
 
232
        /* buffer specific vars */
 
233
        FontBufInfoBLF *buf_info = &font->buf_info;
 
234
        float b_col_float[4];
 
235
        const unsigned char b_col_char[4] = {buf_info->col[0] * 255,
 
236
                                             buf_info->col[1] * 255,
 
237
                                             buf_info->col[2] * 255,
 
238
                                             buf_info->col[3] * 255};
 
239
 
233
240
        unsigned char *cbuf;
234
241
        int chx, chy;
235
242
        int y, x;
239
246
 
240
247
        blf_font_ensure_ascii_table(font);
241
248
 
 
249
        /* another buffer specific call for color conversion */
 
250
        if (buf_info->display) {
 
251
                copy_v4_v4(b_col_float, buf_info->col);
 
252
                IMB_colormanagement_display_to_scene_linear_v3(b_col_float, buf_info->display);
 
253
        }
 
254
        else {
 
255
                srgb_to_linearrgb_v4(b_col_float, buf_info->col);
 
256
        }
 
257
 
242
258
        while (str[i]) {
243
259
                BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
244
260
 
259
275
                        pen_y = (int)font->pos[1] - (g->height - (int)g->pos_y);
260
276
                }
261
277
 
262
 
                if ((chx + g->width) >= 0 && chx < font->bw && (pen_y + g->height) >= 0 && pen_y < font->bh) {
 
278
                if ((chx + g->width) >= 0 && chx < buf_info->w && (pen_y + g->height) >= 0 && pen_y < buf_info->h) {
263
279
                        /* don't draw beyond the buffer bounds */
264
280
                        int width_clip = g->width;
265
281
                        int height_clip = g->height;
266
 
                        int yb_start = g->pitch < 0 ? 0 : g->height-1;
 
282
                        int yb_start = g->pitch < 0 ? 0 : g->height - 1;
267
283
 
268
 
                        if (width_clip + chx > font->bw)
269
 
                                width_clip -= chx + width_clip - font->bw;
270
 
                        if (height_clip + pen_y > font->bh)
271
 
                                height_clip -= pen_y + height_clip - font->bh;
 
284
                        if (width_clip + chx > buf_info->w)
 
285
                                width_clip -= chx + width_clip - buf_info->w;
 
286
                        if (height_clip + pen_y > buf_info->h)
 
287
                                height_clip -= pen_y + height_clip - buf_info->h;
272
288
                        
273
289
                        /* drawing below the image? */
274
290
                        if (pen_y < 0) {
277
293
                                pen_y = 0;
278
294
                        }
279
295
 
280
 
                        if (font->b_fbuf) {
 
296
                        if (buf_info->fbuf) {
281
297
                                int yb = yb_start;
282
298
                                for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) {
283
299
                                        for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) {
285
301
 
286
302
                                                if (a > 0.0f) {
287
303
                                                        float alphatest;
288
 
                                                        fbuf = font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
 
304
                                                        fbuf = buf_info->fbuf + buf_info->ch * ((chx + x) + ((pen_y + y) * buf_info->w));
289
305
                                                        if (a >= 1.0f) {
290
 
                                                                fbuf[0] = font->b_col[0];
291
 
                                                                fbuf[1] = font->b_col[1];
292
 
                                                                fbuf[2] = font->b_col[2];
293
 
                                                                fbuf[3] = (alphatest = (fbuf[3] + (font->b_col[3]))) < 1.0f ? alphatest : 1.0f;
 
306
                                                                fbuf[0] = b_col_float[0];
 
307
                                                                fbuf[1] = b_col_float[1];
 
308
                                                                fbuf[2] = b_col_float[2];
 
309
                                                                fbuf[3] = (alphatest = (fbuf[3] + (b_col_float[3]))) < 1.0f ? alphatest : 1.0f;
294
310
                                                        }
295
311
                                                        else {
296
 
                                                                fbuf[0] = (font->b_col[0]*a) + (fbuf[0] * (1-a));
297
 
                                                                fbuf[1] = (font->b_col[1]*a) + (fbuf[1] * (1-a));
298
 
                                                                fbuf[2] = (font->b_col[2]*a) + (fbuf[2] * (1-a));
299
 
                                                                fbuf[3] = (alphatest = (fbuf[3] + (font->b_col[3]*a))) < 1.0f ? alphatest : 1.0f;
 
312
                                                                fbuf[0] = (b_col_float[0] * a) + (fbuf[0] * (1.0f - a));
 
313
                                                                fbuf[1] = (b_col_float[1] * a) + (fbuf[1] * (1.0f - a));
 
314
                                                                fbuf[2] = (b_col_float[2] * a) + (fbuf[2] * (1.0f - a));
 
315
                                                                fbuf[3] = (alphatest = (fbuf[3] + (b_col_float[3] * a))) < 1.0f ? alphatest : 1.0f;
300
316
                                                        }
301
317
                                                }
302
318
                                        }
308
324
                                }
309
325
                        }
310
326
 
311
 
                        if (font->b_cbuf) {
 
327
                        if (buf_info->cbuf) {
312
328
                                int yb = yb_start;
313
329
                                for (y = 0; y < height_clip; y++) {
314
330
                                        for (x = 0; x < width_clip; x++) {
316
332
 
317
333
                                                if (a > 0.0f) {
318
334
                                                        int alphatest;
319
 
                                                        cbuf = font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
 
335
                                                        cbuf = buf_info->cbuf + buf_info->ch * ((chx + x) + ((pen_y + y) * buf_info->w));
320
336
                                                        if (a >= 1.0f) {
321
337
                                                                cbuf[0] = b_col_char[0];
322
338
                                                                cbuf[1] = b_col_char[1];
324
340
                                                                cbuf[3] = (alphatest = ((int)cbuf[3] + (int)b_col_char[3])) < 255 ? alphatest : 255;
325
341
                                                        }
326
342
                                                        else {
327
 
                                                                cbuf[0] = (b_col_char[0]*a) + (cbuf[0] * (1-a));
328
 
                                                                cbuf[1] = (b_col_char[1]*a) + (cbuf[1] * (1-a));
329
 
                                                                cbuf[2] = (b_col_char[2]*a) + (cbuf[2] * (1-a));
330
 
                                                                cbuf[3] = (alphatest = ((int)cbuf[3] + (int)((font->b_col[3]*a)*255.0f))) <
 
343
                                                                cbuf[0] = (b_col_char[0] * a) + (cbuf[0] * (1.0f - a));
 
344
                                                                cbuf[1] = (b_col_char[1] * a) + (cbuf[1] * (1.0f - a));
 
345
                                                                cbuf[2] = (b_col_char[2] * a) + (cbuf[2] * (1.0f - a));
 
346
                                                                cbuf[3] = (alphatest = ((int)cbuf[3] + (int)((b_col_float[3] * a) * 255.0f))) <
331
347
                                                                          255 ? alphatest : 255;
332
348
                                                        }
333
349
                                                }
414
430
        }
415
431
 
416
432
        blf_font_boundbox(font, str, &box);
417
 
        *width = ((box.xmax - box.xmin) * xa);
418
 
        *height = ((box.ymax - box.ymin) * ya);
 
433
        *width  = (BLI_rctf_size_x(&box) * xa);
 
434
        *height = (BLI_rctf_size_y(&box) * ya);
419
435
}
420
436
 
421
437
float blf_font_width(FontBLF *font, const char *str)
429
445
                xa = 1.0f;
430
446
 
431
447
        blf_font_boundbox(font, str, &box);
432
 
        return (box.xmax - box.xmin) * xa;
 
448
        return BLI_rctf_size_x(&box) * xa;
433
449
}
434
450
 
435
451
float blf_font_height(FontBLF *font, const char *str)
443
459
                ya = 1.0f;
444
460
 
445
461
        blf_font_boundbox(font, str, &box);
446
 
        return (box.ymax - box.ymin) * ya;
 
462
        return BLI_rctf_size_y(&box) * ya;
447
463
}
448
464
 
449
465
float blf_font_fixed_width(FontBLF *font)
507
523
        font->glyph_cache = NULL;
508
524
        font->blur = 0;
509
525
        font->max_tex_size = -1;
510
 
        font->b_fbuf = NULL;
511
 
        font->b_cbuf = NULL;
512
 
        font->bw = 0;
513
 
        font->bh = 0;
514
 
        font->bch = 0;
515
 
        font->b_col[0] = 0;
516
 
        font->b_col[1] = 0;
517
 
        font->b_col[2] = 0;
518
 
        font->b_col[3] = 0;
 
526
 
 
527
        font->buf_info.fbuf = NULL;
 
528
        font->buf_info.cbuf = NULL;
 
529
        font->buf_info.w = 0;
 
530
        font->buf_info.h = 0;
 
531
        font->buf_info.ch = 0;
 
532
        font->buf_info.col[0] = 0;
 
533
        font->buf_info.col[1] = 0;
 
534
        font->buf_info.col[2] = 0;
 
535
        font->buf_info.col[3] = 0;
 
536
 
519
537
        font->ft_lib = ft_lib;
520
538
}
521
539
 
560
578
        FT_Open_Args open;
561
579
 
562
580
        open.flags = FT_OPEN_MEMORY;
563
 
        open.memory_base = (FT_Byte *)mem;
 
581
        open.memory_base = (const FT_Byte *)mem;
564
582
        open.memory_size = mem_size;
565
583
        FT_Attach_Stream(font->face, &open);
566
584
}
567
585
 
568
 
FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_size)
 
586
FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, int mem_size)
569
587
{
570
588
        FontBLF *font;
571
589
        FT_Error err;