The BIG graph update
[rrdtool.git] / libraries / freetype-2.0.5 / ftcimage.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftcimage.c                                                             */
4 /*                                                                         */
5 /*    FreeType Image cache (body).                                         */
6 /*                                                                         */
7 /*  Copyright 2000-2001 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
10 /*  This file is part of the FreeType project, and may only be used,       */
11 /*  modified, and distributed under the terms of the FreeType project      */
12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
13 /*  this file you indicate that you have read the license and              */
14 /*  understand and accept it fully.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include FT_CACHE_H
21 #include FT_CACHE_IMAGE_H
22 #include FT_INTERNAL_MEMORY_H
23
24 #include "ftcerror.h"
25
26 #include <string.h>     /* memcmp() */
27 #include <stdlib.h>     /* labs()   */
28
29
30   /* the FT_Glyph image `glyph node' type */
31   typedef struct  FTC_GlyphImageRec_
32   {
33     FTC_GlyphNodeRec  root;
34     FT_Glyph          ft_glyph;
35
36   } FTC_GlyphImageRec, *FTC_GlyphImage;
37
38
39   /* the glyph image queue type */
40   typedef struct  FTC_ImageSetRec_
41   {
42     FTC_GlyphSetRec  root;
43     FTC_Image_Desc   description;
44
45   } FTC_ImageSetRec, *FTC_ImageSet;
46
47
48   typedef struct  FTC_Image_CacheRec_
49   {
50     FTC_Glyph_CacheRec  root;
51
52   } FTC_Image_CacheRec;
53
54
55
56   /*************************************************************************/
57   /*************************************************************************/
58   /*****                                                               *****/
59   /*****                    GLYPH IMAGE NODES                          *****/
60   /*****                                                               *****/
61   /*************************************************************************/
62   /*************************************************************************/
63
64
65   FT_CALLBACK_DEF( void )
66   ftc_glyph_image_node_destroy( FTC_GlyphImage  node,
67                                 FTC_GlyphSet    gset )
68   {
69     FT_Memory  memory = gset->memory;
70
71
72     FT_Done_Glyph( node->ft_glyph );
73     FREE( node );
74   }
75
76
77   FT_CALLBACK_DEF( FT_Error )
78   ftc_glyph_image_node_new( FTC_GlyphSet     gset,
79                             FT_UInt          glyph_index,
80                             FTC_GlyphImage  *anode )
81   {
82     FT_Memory       memory   = gset->memory;
83     FTC_ImageSet    imageset = (FTC_ImageSet)gset;
84     FT_Error        error;
85     FTC_GlyphImage  node = 0;
86     FT_Face         face;
87     FT_Size         size;
88
89
90     /* allocate node */
91     if ( ALLOC( node, sizeof ( *node ) ) )
92       goto Exit;
93
94     /* initialize its inner fields */
95     FTC_GlyphNode_Init( FTC_GLYPHNODE( node ), gset, glyph_index );
96
97     /* we will now load the glyph image */
98     error = FTC_Manager_Lookup_Size( gset->manager,
99                                      &imageset->description.font,
100                                      &face, &size );
101     if ( !error )
102     {
103       FT_UInt  gindex = node->root.glyph_index;
104       FT_UInt  load_flags  = FT_LOAD_DEFAULT;
105       FT_UInt  image_type  = imageset->description.image_type;
106
107
108       if ( FTC_IMAGE_FORMAT( image_type ) == ftc_image_format_bitmap )
109       {
110         load_flags |= FT_LOAD_RENDER;
111         if ( image_type & ftc_image_flag_monochrome )
112           load_flags |= FT_LOAD_MONOCHROME;
113
114         /* disable embedded bitmaps loading if necessary */
115         if ( image_type & ftc_image_flag_no_sbits )
116           load_flags |= FT_LOAD_NO_BITMAP;
117       }
118       else if ( FTC_IMAGE_FORMAT( image_type ) == ftc_image_format_outline )
119       {
120         /* disable embedded bitmaps loading */
121         load_flags |= FT_LOAD_NO_BITMAP;
122
123         if ( image_type & ftc_image_flag_unscaled )
124           load_flags |= FT_LOAD_NO_SCALE;
125       }
126
127       if ( image_type & ftc_image_flag_unhinted )
128         load_flags |= FT_LOAD_NO_HINTING;
129
130       if ( image_type & ftc_image_flag_autohinted )
131         load_flags |= FT_LOAD_FORCE_AUTOHINT;
132
133       error = FT_Load_Glyph( face, gindex, load_flags );
134       if ( !error )
135       {
136         if ( face->glyph->format == ft_glyph_format_bitmap  ||
137              face->glyph->format == ft_glyph_format_outline )
138         {
139           /* ok, copy it */
140           FT_Glyph  glyph;
141
142
143           error = FT_Get_Glyph( face->glyph, &glyph );
144           if ( !error )
145             node->ft_glyph = glyph;
146         }
147         else
148           error = FTC_Err_Invalid_Argument;
149       }
150     }
151
152   Exit:
153     if ( error && node )
154       FREE( node );
155
156     *anode = node;
157     return error;
158   }
159
160
161   /* this function is important because it is both part of */
162   /* an FTC_GlyphSet_Class and an FTC_CacheNode_Class      */
163   /*                                                       */
164   FT_CALLBACK_DEF( FT_ULong )
165   ftc_glyph_image_node_size( FTC_GlyphImage  node )
166   {
167     FT_ULong  size  = 0;
168     FT_Glyph  glyph = node->ft_glyph;
169
170
171     switch ( glyph->format )
172     {
173     case ft_glyph_format_bitmap:
174       {
175         FT_BitmapGlyph  bitg;
176
177
178         bitg = (FT_BitmapGlyph)glyph;
179         size = bitg->bitmap.rows * labs( bitg->bitmap.pitch ) +
180                sizeof ( *bitg );
181       }
182       break;
183
184     case ft_glyph_format_outline:
185       {
186         FT_OutlineGlyph  outg;
187
188
189         outg = (FT_OutlineGlyph)glyph;
190         size = outg->outline.n_points *
191                  ( sizeof( FT_Vector ) + sizeof ( FT_Byte ) ) +
192                outg->outline.n_contours * sizeof ( FT_Short ) +
193                sizeof ( *outg );
194       }
195       break;
196
197     default:
198       ;
199     }
200
201     size += sizeof ( *node );
202     return size;
203   }
204
205
206   /*************************************************************************/
207   /*************************************************************************/
208   /*****                                                               *****/
209   /*****                    GLYPH IMAGE SETS                           *****/
210   /*****                                                               *****/
211   /*************************************************************************/
212   /*************************************************************************/
213
214
215   FT_CALLBACK_DEF( FT_Error )
216   ftc_image_set_init( FTC_ImageSet     iset,
217                       FTC_Image_Desc*  type )
218   {
219     iset->description = *type;
220     return 0;
221   }
222
223
224   FT_CALLBACK_DEF( FT_Bool )
225   ftc_image_set_compare( FTC_ImageSet     iset,
226                          FTC_Image_Desc*  type )
227   {
228     return FT_BOOL( !memcmp( &iset->description, type, sizeof ( *type ) ) );
229   }
230
231
232   FT_CALLBACK_TABLE_DEF
233   const FTC_GlyphSet_Class  ftc_glyph_image_set_class =
234   {
235     sizeof( FTC_ImageSetRec ),
236
237     (FTC_GlyphSet_InitFunc)       ftc_image_set_init,
238     (FTC_GlyphSet_DoneFunc)       0,
239     (FTC_GlyphSet_CompareFunc)    ftc_image_set_compare,
240
241     (FTC_GlyphSet_NewNodeFunc)    ftc_glyph_image_node_new,
242     (FTC_GlyphSet_SizeNodeFunc)   ftc_glyph_image_node_size,
243     (FTC_GlyphSet_DestroyNodeFunc)ftc_glyph_image_node_destroy
244   };
245
246
247   /*************************************************************************/
248   /*************************************************************************/
249   /*****                                                               *****/
250   /*****                    GLYPH IMAGE CACHE                          *****/
251   /*****                                                               *****/
252   /*************************************************************************/
253   /*************************************************************************/
254
255
256   FT_CALLBACK_TABLE_DEF
257   const FTC_Glyph_Cache_Class  ftc_glyph_image_cache_class =
258   {
259     {
260       sizeof( FTC_Image_CacheRec ),
261       (FTC_Cache_InitFunc) FTC_Glyph_Cache_Init,
262       (FTC_Cache_DoneFunc) FTC_Glyph_Cache_Done
263     },
264     (FTC_GlyphSet_Class*) &ftc_glyph_image_set_class
265   };
266
267
268   /* documentation is in ftcimage.h */
269
270   FT_EXPORT_DEF( FT_Error )
271   FTC_Image_Cache_New( FTC_Manager       manager,
272                        FTC_Image_Cache  *acache )
273   {
274     return FTC_Manager_Register_Cache(
275              manager,
276              (FTC_Cache_Class*)&ftc_glyph_image_cache_class,
277              (FTC_Cache*)acache );
278   }
279
280
281   /* documentation is in ftcimage.h */
282
283   FT_EXPORT_DEF( FT_Error )
284   FTC_Image_Cache_Lookup( FTC_Image_Cache  cache,
285                           FTC_Image_Desc*  desc,
286                           FT_UInt          gindex,
287                           FT_Glyph        *aglyph )
288   {
289     FT_Error       error;
290     FTC_GlyphNode  node;
291
292
293     /* some argument checks are delayed to FTC_Glyph_Cache_Lookup */
294
295     if ( !aglyph )
296       return FTC_Err_Invalid_Argument;
297
298     error = FTC_Glyph_Cache_Lookup( (FTC_Glyph_Cache)cache,
299                                     desc, gindex, &node );
300
301     if ( !error )
302       *aglyph = ((FTC_GlyphImage)node)->ft_glyph;
303
304     return error;
305   }
306
307
308 /* END */