source: svn/trunk/newcon3bcm2_21bu/nexus/app/nanotv/bgfx_font.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

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