source: svn/newcon3bcm2_21bu/nexus/app/bgfx_font.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 23.6 KB
Line 
1/***************************************************************
2**
3** Broadcom Corp. Confidential
4** Copyright 2002 Broadcom Corp. All Rights Reserved.
5**
6** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED
7** SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM.
8** YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT
9** SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
10**
11** Description: SGIL bit map font library
12**
13** Created: 8/22/2002 by Jeffrey P. Fisher
14**
15**
16**
17****************************************************************/
18
19/** include files **/
20#include "bgfx.h"
21
22
23static bgfx_glyph_t *bgfx_init_glyph(bgfx_font_t *font_p, FT_ULong  a_char, int glyph_idx);
24
25/* global SGL structure */
26typedef struct bgfx_t
27{
28#ifdef CONFIG_FREETYPE
29        FT_Library      ft_library;
30#endif
31        int dummy;
32}bgfx_t;
33
34#ifdef CONFIG_FREETYPE
35        static bgfx_t g_bgfx = { NULL };
36#endif
37
38/****************************************************************}
39* INPUTS:       none
40* OUTPUTS:      none
41* RETURNS:      non-zero on failure
42* FUNCTION: Initialize the SGIL font library
43*
44****************************************************************/
45int bgfx_font_init()
46{
47        int result = 0;
48
49    BGFX_TRACE(("font_init\n"));
50#ifdef CONFIG_FREETYPE
51        bgfx_lock();
52        {
53                FT_Error err;
54                err = FT_Init_FreeType( &g_bgfx.ft_library );
55                if (err)
56                {
57                        g_bgfx.ft_library = NULL;
58                        result = -1;
59                        BGFX_DEBUG(("font_init - FT_Init_FreeType(%d)\n",err));
60                }
61        }
62        bgfx_unlock();
63#endif
64    BGFX_TRACE(("font_init - done\n"));
65        return result;
66}
67
68/****************************************************************}
69* INPUTS:       none
70* OUTPUTS:      none
71* RETURNS:      non-zero on failure
72* FUNCTION: release sgl global resources, primarily for handling freetype
73*
74****************************************************************/
75void bgfx_font_done()
76{
77    BGFX_TRACE(("font_done\n"));
78#ifdef CONFIG_FREETYPE
79    bgfx_lock();
80       
81    if (g_bgfx.ft_library)
82                FT_Done_FreeType( g_bgfx.ft_library );
83        g_bgfx.ft_library = NULL;
84        bgfx_unlock();
85#endif
86}
87
88#ifdef CONFIG_FREETYPE
89/****************************************************************
90* INPUTS:               fr_p - font ripper parameters
91* OUTPUTS:              none
92* RETURNS:              number of glyph entries
93* DESCRIPTION:  Get the maximum number of glyph index entries.
94*
95****************************************************************/
96
97int get_max_glyph_index(bgfx_font_t *font_p)
98{
99        FT_ULong  charcode;                                             
100        FT_UInt   gindex;                                               
101        int cnt = 0;                                                                                                     
102
103        charcode = FT_Get_First_Char( font_p->face, &gindex );                   
104        while ( gindex != 0 )                                           
105        {   
106                ++cnt;
107                charcode = FT_Get_Next_Char( font_p->face, charcode, &gindex );       
108        }
109        return cnt;
110}
111
112/****************************************************************
113* INPUTS:   font_p - font definition
114* OUTPUTS:      none
115* RETURNS:      none-zero on failure
116* FUNCTION:     create the charmap.
117*
118****************************************************************/
119int bgfx_create_char_map(bgfx_font_t *font_p)
120{   
121    int i = 0;
122        FT_ULong  charcode;                                             
123        FT_UInt   gindex;
124
125        font_p->cache_size = get_max_glyph_index(font_p);
126        if (font_p->char_map_size <= 0)
127        {
128                BGFX_DEBUG(("bgfx_create_char_map failed, no unicode encoding for this font.\n"));
129                return -1;
130        }
131        font_p->char_map = (bgfx_glyph_t**)BGFX_MALLOC(sizeof(bgfx_glyph_t*) * font_p->cache_size );
132   
133        if (!font_p->glyph_cache)
134        {
135                BGFX_DEBUG(("bgfx_create_char_map failed, could not allocate memory for char map.\n"));
136                return -1;
137        }
138       
139    bgfx_lock();
140        charcode = FT_Get_First_Char( font_p->face, &gindex );                   
141        while ( gindex != 0 )                                           
142        {   
143                font_p->char_map[i++] = charcode;
144
145                charcode = FT_Get_Next_Char( font_p->face, charcode, &gindex );
146        }
147        bgfx_unlock();
148    BGFX_TRACE(("bgfx_create_char_map - %d chars.\n",i));
149
150    return 0;
151}
152#endif /* CONFIG_FREETYPE */
153
154/****************************************************************
155* INPUTS:   font_p - font definition
156*           typeface_name - name of the typeface
157*           height - font height
158* OUTPUTS:      none
159* RETURNS:      none-zero on failure
160* FUNCTION:     Load the font typeface.
161*
162* The general usage convention in SGIL is to avoid memory allocation
163* in SGIL functions.  For example before calling bgfx_new_font a
164* valid font_t structure must be created and passed to bgfx_new_font.
165* The font_t structure is modified but not allocated by bgfx_new_font. 
166* A primary exception is that the glyph cache and glyphs are
167* allocated and manage internally.  Therefore, it is important to
168* call bgfx_free_font to allow SGIL to perform cleanup on the glyph cache.
169****************************************************************/
170int bgfx_new_font(bgfx_font_t *font_p,
171                                 const char *typeface_name, unsigned char height,unsigned long flags)
172{
173    BGFX_TRACE(("bgfx_new_font - %s:%d,%ld\n",typeface_name,height,flags));
174#ifdef CONFIG_FREETYPE
175        {
176                FT_Error     err;
177                unsigned long i;
178                bgfx_glyph_t *glyph;
179       
180                bgfx_lock();
181       
182                err = FT_New_Face( g_bgfx.ft_library, typeface_name,0,  &font_p->face );
183                if (err)
184                {
185                        BGFX_DEBUG(("bgfx_new_font could not open typeface %s\n",typeface_name));
186                        bgfx_unlock();
187                        return -1;
188                }
189       
190                err = FT_Select_Charmap( font_p->face, ft_encoding_unicode );
191        #if FREETYPE_MINOR > 0
192                if (err)
193                {
194                        BGFX_DEBUG(("bgfx_new_font could not open unicode charmap, default to latin\n"));
195                        err = FT_Select_Charmap( font_p->face, ft_encoding_latin_1 );
196                }
197        #endif
198                if (err)
199                {
200                        BGFX_DEBUG(("bgfx_new_font could not select charmap for font %s\n",typeface_name));
201                        bgfx_unlock();
202                        return -1;
203                }
204                err = FT_Set_Pixel_Sizes( font_p->face, 0, height);
205                if (err)
206                {
207                        BGFX_DEBUG(("bgfx_new_font could set font height to %d\n",height));
208                        FT_Done_Face( font_p->face );
209                        bgfx_unlock();
210                        return -1;
211                }
212                if (flags & BGFX_MONO)
213                        font_p->face->generic.data = (void *) (FT_LOAD_DEFAULT | FT_LOAD_MONOCHROME);
214                else
215                        font_p->face->generic.data = (void *) FT_LOAD_DEFAULT;
216                font_p->ascender    = font_p->face->size->metrics.ascender  >> 6;
217                font_p->descender   = font_p->face->size->metrics.descender >> 6;
218                font_p->height      = (font_p->face->size->metrics.ascender -
219                                                           font_p->face->size->metrics.descender) >> 6;
220                font_p->maxadvance  = font_p->face->size->metrics.max_advance >> 6;
221               
222                /* create glyph cache */
223                if (flags & BGFX_CACHE_GLYPHS)
224                {
225                        font_p->cache_size = font_p->face->num_glyphs;
226                        font_p->glyph_cache = (bgfx_glyph_t**)BGFX_MALLOC(sizeof(bgfx_glyph_t*) * font_p->cache_size);
227                        if (font_p->glyph_cache)
228                                BGFX_MEMSET(font_p->glyph_cache,0,sizeof(bgfx_glyph_t*) * font_p->cache_size);
229                        else
230                                font_p->cache_size = 0;
231                }
232                else
233                {
234                        font_p->cache_size = 0;
235                        font_p->glyph_cache = NULL;
236                }
237                font_p->format = flags;
238                bgfx_unlock();
239               
240                if (bgfx_create_char_map(font_p) != 0)
241                {
242                        BGFX_DEBUG(("bgfx_new_font could not create charmap\n"));
243                        FT_Done_Face( font_p->face );
244                        return -1;
245                }
246       
247                if (flags & BGFX_PRELOAD_GLYPHS)
248                {
249                        for (i = 0; i < font_p->cache_size; ++i)
250                                glyph = bgfx_init_glyph(font_p,font_p->char_map[i],i);
251                }
252        }
253        return 0;
254#else
255        return -1; /* must load fonts when there is no freetype support */
256#endif
257}
258/****************************************************************
259* INPUTS:   font_p - font definition
260* OUTPUTS:      none
261* RETURNS:      none
262* FUNCTION:     Free font typeface.
263*
264****************************************************************/
265void bgfx_free_font(bgfx_font_t *font_p)
266{
267        if (font_p->glyph_cache[0]->buf)
268                BGFX_FREE(font_p->glyph_cache[0]->buf);
269        if (font_p->glyph_cache[0])
270                BGFX_FREE(font_p->glyph_cache[0]);
271        if (font_p->char_map)
272                BGFX_FREE(font_p->char_map);
273        if (font_p->glyph_cache)
274                BGFX_FREE(font_p->glyph_cache);
275#if 0
276    int i;
277    for (i = 0; i < font_p->cache_size; ++i)
278    {
279        bgfx_glyph_t *glyph = font_p->glyph_cache[i];
280        if (glyph)
281        {
282            BGFX_FREE(glyph->buf);
283            BGFX_FREE(glyph);
284            font_p->glyph_cache[i] = NULL;
285        }
286    }
287        BGFX_FREE(font_p->glyph_cache);
288#ifdef CONFIG_FREETYPE
289    FT_Done_Face (font_p->face);
290#endif
291#endif
292}
293
294/****************************************************************
295* INPUTS:       font_p - font
296*                       a_char - the character
297*                       glyph_idx - glyph index
298* OUTPUTS:      info - return char info
299* RETURNS:      glyph reference
300* FUNCTION:     Initialize a glyph data structure (FREETYPE ONLY).
301*
302****************************************************************/
303static bgfx_glyph_t *bgfx_init_glyph(bgfx_font_t *font_p, FT_ULong  a_char, int glyph_idx)
304{
305#ifdef CONFIG_FREETYPE
306        FT_Error err;
307        FT_Int   ld_flags;
308        FT_UInt  index;
309    bgfx_glyph_t *new_glyph;
310    unsigned long nbytes;
311
312        BGFX_TRACE(("bgfx_init_glyph 0x%08lx\n",a_char));
313
314        bgfx_lock();
315
316        index = FT_Get_Char_Index(font_p->face,a_char);
317        if (index == 0)
318        {
319                BGFX_DEBUG(("bgfx_init_glyph could not find char 0x%08lx in FreeType char map\n",a_char));
320                bgfx_unlock();
321                return NULL;
322        }
323
324        ld_flags = (FT_Int) font_p->face->generic.data;
325        ld_flags |= FT_LOAD_RENDER;
326        err = FT_Load_Glyph( font_p->face, index, ld_flags );
327        if (err)
328        {
329                BGFX_DEBUG(("bgfx_init_glyph could not load glyph %d\n",index));
330                bgfx_unlock();
331                return NULL;
332        }
333
334    /* allocate memory for glyph */
335
336    new_glyph = (bgfx_glyph_t*)BGFX_MALLOC(sizeof(bgfx_glyph_t));
337        if (!new_glyph)
338        {
339                BGFX_DEBUG(("bgfx_init_glyph could allocate memory for glyph\n"));
340                bgfx_unlock();
341                return NULL;
342        }
343        new_glyph->left = font_p->face->glyph->bitmap_left;
344        new_glyph->top  = font_p->face->glyph->bitmap_top;
345        new_glyph->advance = font_p->face->glyph->advance.x >> 6;
346        new_glyph->width = font_p->face->glyph->bitmap.width;
347        new_glyph->height = font_p->face->glyph->bitmap.rows;
348    new_glyph->pitch = font_p->face->glyph->bitmap.pitch;
349   
350    nbytes = font_p->face->glyph->bitmap.pitch * font_p->face->glyph->bitmap.rows;
351    new_glyph->buf = (unsigned char*)BGFX_MALLOC(nbytes);
352
353        if (!new_glyph->buf)
354        {
355                BGFX_FREE(new_glyph);
356        BGFX_DEBUG(("bgfx_init_glyph could allocate memory for glyph\n"));
357                bgfx_unlock();
358                return NULL;
359        }
360
361    /* copy glyph data */
362    BGFX_MEMCPY(new_glyph->buf, font_p->face->glyph->bitmap.buffer,nbytes);
363    font_p->glyph_cache[glyph_idx] = new_glyph;
364
365        bgfx_unlock();
366        return new_glyph;
367#else
368        return NULL;
369#endif
370}
371/****************************************************************
372* INPUTS:       font_p - font
373*                       a_char - char value
374* OUTPUTS:      none
375* RETURNS:      glyph cache index or -1 on failure
376* FUNCTION:     Get a glyph cache index from the char_map.
377*
378****************************************************************/
379static inline int bgfx_map_char(bgfx_font_t *font_p, unsigned long a_char)
380{
381        register int i;
382        for (i = 0; i < font_p->cache_size; ++i)
383        {
384                if (font_p->char_map[i] == a_char)
385                        return i;
386        }
387        return -1;
388}
389/****************************************************************
390* INPUTS:       font_p - font
391*                       a_char - char value
392* OUTPUTS:      info - return char info
393* RETURNS:      glyph reference
394* FUNCTION:     Get a glyph from the glyph cache or a fresh rendering.
395*
396****************************************************************/
397bgfx_glyph_t *bgfx_get_glyph(bgfx_font_t *font_p, unsigned long a_char)
398{
399        int cache_index;
400        BGFX_TRACE(("bgfx_get_glyph 0x%08lx\n",a_char));
401
402    /* Check for glyph in glyph cache */
403        cache_index = bgfx_map_char(font_p,a_char);
404    if (cache_index < 0)
405    {
406        BGFX_DEBUG(("bgfx_get_glyph failed 0x%08lx not in font.\n",a_char));
407        return NULL;
408    }
409
410    BGFX_TRACE(("bgfx_get_glyph find char in cache 0x%08lx (%ld)\n",cache_index,cache_index));
411    if (font_p->glyph_cache[cache_index] != NULL)
412    {
413        BGFX_TRACE(("bgfx_get_glyph found char in cache\n"));
414        return font_p->glyph_cache[cache_index];
415    }
416        return bgfx_init_glyph(font_p,a_char,cache_index);
417}
418/****************************************************************
419* INPUTS:       font_p - font definition
420*                       str_p - array of single 32-bit unicode characters
421*                       str_len - number of 32-bit characters in string
422* OUTPUTS:      w,h - width and height in pixels of the string
423* RETURNS:      none
424* FUNCTION:     Caclulate the bound rectangle for the string.
425*
426****************************************************************/
427
428void bgfx_string_info(bgfx_font_t *font_p,                                      /* initialized font structure */
429                                                        const unsigned long *str_p,     /* array of single 32-bit unicode characters */
430                                                        int     str_len,                                        /* number of 32-bit characters in string */
431                                                        int *w,                                                 /* width in pixels */
432                                                        int *h                                                  /* height in pixels */
433                                                        )
434{
435        unsigned long current;
436        int offset;
437        bgfx_glyph_t * bgfx_glyph;
438#ifdef CONFIG_FREETYPE
439    bgfx_glyph_t info;
440        FT_Error err;
441        FT_Int   ld_flags;
442        FT_UInt  index;
443#endif
444        *w = 0; *h = 0;
445        bgfx_glyph = NULL;
446        BGFX_TRACE(("bgfx_string_info %s\n",str_p));
447
448        for (offset = 0; offset < str_len; offset++)
449        {
450                current = str_p[offset];
451                BGFX_TRACE(("bgfx_string_info (0x%08lx)\n",current));
452
453                /* if using glyph cache use the get glyph rather than render directly to surface buffer */
454                if (font_p->cache_size > 0)
455                {
456                        bgfx_glyph = bgfx_get_glyph(font_p,current);
457                }
458#ifdef CONFIG_FREETYPE
459                if (!bgfx_glyph)
460                {   
461                        bgfx_lock();
462                        bgfx_glyph = &info;
463       
464                        index = FT_Get_Char_Index( font_p->face, current );
465                        BGFX_TRACE(("bgfx_string_info %d\n",index));
466               
467                        ld_flags = (FT_Int) font_p->face->generic.data;
468                        ld_flags |= FT_LOAD_RENDER;
469                        err = FT_Load_Glyph( font_p->face, index, ld_flags );
470                        if (err)
471                        {
472                                BGFX_DEBUG(("bgfx_string_info could not load glyph %p\n",bgfx_glyph));
473                                bgfx_unlock();
474                                return;
475                        }
476                        bgfx_unlock();
477       
478                        bgfx_glyph->left = font_p->face->glyph->bitmap_left;
479                        bgfx_glyph->top  = font_p->face->glyph->bitmap_top;
480               
481                        bgfx_glyph->advance = font_p->face->glyph->advance.x >> 6;
482                        bgfx_glyph->width = font_p->face->glyph->bitmap.width;
483               
484                        bgfx_glyph->height = font_p->face->glyph->bitmap.rows;
485                 
486                        bgfx_glyph->buf = font_p->face->glyph->bitmap.buffer;
487                        bgfx_glyph->pitch = font_p->face->glyph->bitmap.pitch;
488                }
489#endif
490                if (!bgfx_glyph)
491                {
492                        BGFX_DEBUG(("bgfx_string_info could not load glyph for char 0x%08lx\n",current));
493                        return;
494                }
495                if (bgfx_glyph->height > *h)
496                {
497                        *h = bgfx_glyph->height;
498                }
499                *w += bgfx_glyph->advance;
500        }
501}
502#if 1
503/****************************************************************
504* INPUTS:   font_p - font definition
505*           file_p - file descriptor
506* OUTPUTS:      none
507* RETURNS:      none-zero on failure
508* FUNCTION:     Load the font typeface from a binary file.
509*
510****************************************************************/
511static int bgfx_calc_mem_font(bgfx_font_t *font_p,void *file_p)   
512{
513    int err,size;
514    unsigned long i;
515    int result = -1;
516
517    size = 0;
518        size += sizeof(FT_ULong) * font_p->cache_size;
519
520        /* Load bitmaps */
521    for (i = 0; i < font_p->cache_size; ++i)
522    {
523                bgfx_glyph_t glyph;
524
525        BGFX_IO_CHECK (bgfx_read((void*)&glyph.left,1,sizeof(glyph.left),file_p));
526        BGFX_IO_CHECK (bgfx_read((void*)&glyph.top,1,sizeof(glyph.top),file_p));
527        BGFX_IO_CHECK (bgfx_read((void*)&glyph.advance,1,sizeof(glyph.advance),file_p));
528        BGFX_IO_CHECK (bgfx_read((void*)&glyph.width,1,sizeof(glyph.width),file_p));
529        BGFX_IO_CHECK (bgfx_read((void*)&glyph.height,1,sizeof(glyph.height),file_p));
530        BGFX_IO_CHECK (bgfx_read((void*)&glyph.pitch,1,sizeof(glyph.pitch),file_p));
531        BGFX_TRACE(("bgfx_load_font glyph %ld\n",i));
532        BGFX_TRACE(("bgfx_load_font glyph->pitch %d\n",glyph.pitch));
533        BGFX_TRACE(("bgfx_load_font glyph->height %d\n",glyph.height));
534        BGFX_TRACE(("bgfx_load_font glyph->left %d\n",glyph.left));
535        if ((glyph.pitch * glyph.height) > 0)
536        {
537                        size += glyph.pitch * glyph.height;
538            bgfx_set(file_p,bgfx_tell(file_p) + (glyph.pitch * glyph.height), 0);
539        }
540    }
541        result = 0;
542done:
543        if (result != 0)
544                return -1;
545    return size;
546}
547
548/****************************************************************
549* INPUTS:   font_p - font definition
550*           file_p - file descriptor
551* OUTPUTS:      none
552* RETURNS:      none-zero on failure
553* FUNCTION:     Load the font typeface from a binary file.
554*
555****************************************************************/
556int bgfx_load_font(bgfx_font_t *font_p,void *file_p)   
557{
558    int result = -1;
559    int err;
560    unsigned long i,fsize,pos;
561        bgfx_glyph_t *glyph_bufs = NULL;
562        bgfx_glyph_t *glyph = NULL;
563        unsigned char* fbuf = NULL;
564   
565        /* write font header */
566    BGFX_MEMSET(font_p,0,sizeof(bgfx_font_t));
567    BGFX_IO_CHECK (bgfx_read((void*)&font_p->ascender,1, sizeof(font_p->ascender),file_p));
568        font_p->ascender = BGFX_SWAP_INT(font_p->ascender);
569    BGFX_IO_CHECK (bgfx_read((void*)&font_p->descender,1, sizeof(font_p->descender),file_p));
570        font_p->descender = BGFX_SWAP_INT(font_p->descender);
571    BGFX_IO_CHECK (bgfx_read((void*)&font_p->height,1, sizeof(font_p->height),file_p));
572        font_p->height = BGFX_SWAP_INT(font_p->height);
573    BGFX_IO_CHECK (bgfx_read((void*)&font_p->maxadvance,1, sizeof(font_p->maxadvance),file_p));
574        font_p->maxadvance = BGFX_SWAP_INT(font_p->maxadvance);
575    BGFX_IO_CHECK (bgfx_read((void*)&font_p->format,1, sizeof(font_p->format),file_p));
576        font_p->format = BGFX_SWAP_ULONG(font_p->format);
577    BGFX_IO_CHECK (bgfx_read((void*)&font_p->cache_size,1, sizeof(font_p->cache_size),file_p));
578        font_p->cache_size = BGFX_SWAP_INT(font_p->cache_size);
579
580        font_p->glyph_cache = (bgfx_glyph_t**)BGFX_MALLOC(sizeof(bgfx_glyph_t*) * font_p->cache_size );
581       
582        BGFX_MEMSET(font_p->glyph_cache,0,sizeof(bgfx_glyph_t*) * font_p->cache_size);
583       
584        glyph_bufs = (bgfx_glyph_t*)BGFX_MALLOC(sizeof(bgfx_glyph_t) * font_p->cache_size );
585        font_p->char_map = (FT_ULong*)BGFX_MALLOC(sizeof(FT_ULong) * font_p->cache_size );
586
587        if (!font_p->char_map || !glyph_bufs || !font_p->glyph_cache)
588        goto done;
589
590        /* load character map */
591    for (i = 0; i < font_p->cache_size; ++i)
592        {
593                BGFX_IO_CHECK (bgfx_read((void*)&(font_p->char_map[i]),1, sizeof(FT_ULong), file_p ));
594                font_p->char_map[i] = BGFX_SWAP_ULONG(font_p->char_map[i]);
595        }
596
597        pos = bgfx_tell(file_p);
598        fsize = bgfx_calc_mem_font(font_p,file_p);
599        bgfx_set(file_p,pos,0);
600
601        fbuf = BGFX_MALLOC(fsize);
602        if ((fsize <= 0) || !fbuf)
603                goto done;
604
605        /* Load bitmaps */
606    for (i = 0; i < font_p->cache_size; ++i)
607    {
608        BGFX_MEMSET(glyph_bufs,0,sizeof(bgfx_glyph_t));
609                glyph = glyph_bufs++;
610        font_p->glyph_cache[i] = glyph;
611        BGFX_IO_CHECK (bgfx_read((void*)&glyph->left,1,sizeof(glyph->left),file_p));
612        BGFX_IO_CHECK (bgfx_read((void*)&glyph->top,1,sizeof(glyph->top),file_p));
613        BGFX_IO_CHECK (bgfx_read((void*)&glyph->advance,1,sizeof(glyph->advance),file_p));
614        BGFX_IO_CHECK (bgfx_read((void*)&glyph->width,1,sizeof(glyph->width),file_p));
615        BGFX_IO_CHECK (bgfx_read((void*)&glyph->height,1,sizeof(glyph->height),file_p));
616        BGFX_IO_CHECK (bgfx_read((void*)&glyph->pitch,1,sizeof(glyph->pitch),file_p));
617        BGFX_TRACE(("bgfx_load_font glyph %ld\n",i));
618        BGFX_TRACE(("bgfx_load_font glyph->pitch %d\n",glyph->pitch));
619        BGFX_TRACE(("bgfx_load_font glyph->height %d\n",glyph->height));
620        BGFX_TRACE(("bgfx_load_font glyph->left %d\n",glyph->left));
621        if ((glyph->pitch * glyph->height) > 0)
622        {
623            glyph->buf = (char*)fbuf;
624            BGFX_IO_CHECK (bgfx_read(glyph->buf,1, (glyph->pitch * glyph->height),file_p));
625                        fbuf += (glyph->pitch * glyph->height);
626        }
627        BGFX_TRACE(("bgfx_load_font glyph->buf %p\n",glyph->buf));
628    }
629    result = 0;
630
631 done:
632         if (result != 0)
633         {
634                 if (font_p->glyph_cache)
635                         BGFX_FREE(font_p->glyph_cache);
636                 if (font_p->char_map)
637                         BGFX_FREE(font_p->char_map);
638                 if (glyph_bufs)
639                         BGFX_FREE(glyph_bufs);
640                 if (fbuf)
641                         BGFX_FREE(fbuf);
642         }
643    return result;
644}
645#else
646
647/****************************************************************
648* INPUTS:   font_p - font definition
649*           file_p - file descriptor
650* OUTPUTS:      none
651* RETURNS:      none-zero on failure
652* FUNCTION:     Load the font typeface from a binary file.
653*
654****************************************************************/
655int bgfx_load_font(bgfx_font_t *font_p,void *file_p)   
656{
657    int result = -1;
658    int err;
659    unsigned long i;
660    bgfx_glyph_t *glyph;
661        unsigned int size;
662   
663        /* write font header */
664    BGFX_MEMSET(font_p,0,sizeof(bgfx_font_t));
665    BGFX_IO_CHECK (bgfx_read((void*)&font_p->ascender,1, sizeof(font_p->ascender),file_p));
666        font_p->ascender = BGFX_SWAP_INT(font_p->ascender);
667    BGFX_IO_CHECK (bgfx_read((void*)&font_p->descender,1, sizeof(font_p->descender),file_p));
668        font_p->descender = BGFX_SWAP_INT(font_p->descender);
669    BGFX_IO_CHECK (bgfx_read((void*)&font_p->height,1, sizeof(font_p->height),file_p));
670        font_p->height = BGFX_SWAP_INT(font_p->height);
671    BGFX_IO_CHECK (bgfx_read((void*)&font_p->maxadvance,1, sizeof(font_p->maxadvance),file_p));
672        font_p->maxadvance = BGFX_SWAP_INT(font_p->maxadvance);
673    BGFX_IO_CHECK (bgfx_read((void*)&font_p->format,1, sizeof(font_p->format),file_p));
674        font_p->format = BGFX_SWAP_ULONG(font_p->format);
675    BGFX_IO_CHECK (bgfx_read((void*)&font_p->cache_size,1, sizeof(font_p->cache_size),file_p));
676        font_p->cache_size = BGFX_SWAP_INT(font_p->cache_size);
677
678        font_p->glyph_cache = (bgfx_glyph_t**)BGFX_MALLOC(sizeof(bgfx_glyph_t*) * font_p->cache_size );
679    size = sizeof(bgfx_glyph_t*) * font_p->cache_size;
680        printf("%s:%d size = %d\n",__FUNCTION__,__LINE__,size);
681
682        if (!font_p->glyph_cache)
683        goto done;
684   
685        BGFX_MEMSET(font_p->glyph_cache,0,sizeof(bgfx_glyph_t*) * font_p->cache_size);
686   
687        font_p->char_map = (FT_ULong*)BGFX_MALLOC(sizeof(FT_ULong) * font_p->cache_size );
688        size += sizeof(FT_ULong) * font_p->cache_size;
689        printf("%s:%d size = %d\n",__FUNCTION__,__LINE__,size);
690   
691        if (!font_p->char_map)
692        goto done;
693
694        /* load character map */
695    for (i = 0; i < font_p->cache_size; ++i)
696        {
697                BGFX_IO_CHECK (bgfx_read((void*)&(font_p->char_map[i]),1, sizeof(FT_ULong), file_p ));
698                font_p->char_map[i] = BGFX_SWAP_ULONG(font_p->char_map[i]);
699        }
700
701        /* Load bitmaps */
702    for (i = 0; i < font_p->cache_size; ++i)
703    {
704        glyph = (bgfx_glyph_t*)BGFX_MALLOC(sizeof(bgfx_glyph_t));
705                size += sizeof(bgfx_glyph_t);
706                printf("%s:%d size = %d\n",__FUNCTION__,__LINE__,size);
707       
708                if (!glyph)
709            goto done;
710
711        BGFX_MEMSET(glyph,0,sizeof(bgfx_glyph_t));
712        font_p->glyph_cache[i] = glyph;
713
714        BGFX_IO_CHECK (bgfx_read((void*)&glyph->left,1,sizeof(glyph->left),file_p));
715        BGFX_IO_CHECK (bgfx_read((void*)&glyph->top,1,sizeof(glyph->top),file_p));
716        BGFX_IO_CHECK (bgfx_read((void*)&glyph->advance,1,sizeof(glyph->advance),file_p));
717        BGFX_IO_CHECK (bgfx_read((void*)&glyph->width,1,sizeof(glyph->width),file_p));
718        BGFX_IO_CHECK (bgfx_read((void*)&glyph->height,1,sizeof(glyph->height),file_p));
719        BGFX_IO_CHECK (bgfx_read((void*)&glyph->pitch,1,sizeof(glyph->pitch),file_p));
720        BGFX_TRACE(("bgfx_load_font glyph %ld\n",i));
721        BGFX_TRACE(("bgfx_load_font glyph->pitch %d\n",glyph->pitch));
722        BGFX_TRACE(("bgfx_load_font glyph->height %d\n",glyph->height));
723        BGFX_TRACE(("bgfx_load_font glyph->left %d\n",glyph->left));
724        if ((glyph->pitch * glyph->height) > 0)
725        {
726            glyph->buf = (char*)BGFX_MALLOC((glyph->pitch * glyph->height));
727                        size += glyph->pitch * glyph->height;
728                        printf("%s:%d size = %d(%d,%d)\n",__FUNCTION__,__LINE__,size,glyph->pitch,glyph->height);
729            if (!glyph->buf)
730                goto done;
731            BGFX_IO_CHECK (bgfx_read(glyph->buf,1, (glyph->pitch * glyph->height),file_p));
732        }
733        BGFX_TRACE(("bgfx_load_font glyph->buf %p\n",glyph->buf));
734    }
735    result = 0;
736
737 done:
738    if (result != 0)
739    {
740        if (font_p->char_map)
741            free(font_p->char_map);
742        for (i = 0; i < font_p->cache_size; ++i)
743        {
744            if (font_p->glyph_cache[i])
745            {
746                if (font_p->glyph_cache[i]->buf)
747                    BGFX_FREE(font_p->glyph_cache[i]->buf);
748                BGFX_FREE(font_p->glyph_cache[i]);
749            }
750        }
751    }
752    return result;
753}
754
755#endif
756
Note: See TracBrowser for help on using the repository browser.