1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftglyph.c                                                              */
4 /*                                                                         */
5 /*    FreeType convenience functions to handle glyphs (body).              */
6 /*                                                                         */
7 /*  Copyright 1996-2005, 2007, 2008, 2010, 2012, 2013 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   /*                                                                       */
20   /*  This file contains the definition of several convenience functions   */
21   /*  that can be used by client applications to easily retrieve glyph     */
22   /*  bitmaps and outlines from a given face.                              */
23   /*                                                                       */
24   /*  These functions should be optional if you are writing a font server  */
25   /*  or text layout engine on top of FreeType.  However, they are pretty  */
26   /*  handy for many other simple uses of the library.                     */
27   /*                                                                       */
28   /*************************************************************************/
29 
30 
31 #include <ft2build.h>
32 #include FT_INTERNAL_DEBUG_H
33 
34 #include FT_GLYPH_H
35 #include FT_OUTLINE_H
36 #include FT_BITMAP_H
37 #include FT_INTERNAL_OBJECTS_H
38 
39 #include "basepic.h"
40 
41   /*************************************************************************/
42   /*                                                                       */
43   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
44   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
45   /* messages during execution.                                            */
46   /*                                                                       */
47 #undef  FT_COMPONENT
48 #define FT_COMPONENT  trace_glyph
49 
50 
51   /*************************************************************************/
52   /*************************************************************************/
53   /****                                                                 ****/
54   /****   FT_BitmapGlyph support                                        ****/
55   /****                                                                 ****/
56   /*************************************************************************/
57   /*************************************************************************/
58 
59   FT_CALLBACK_DEF( FT_Error )
ft_bitmap_glyph_init(FT_Glyph bitmap_glyph,FT_GlyphSlot slot)60   ft_bitmap_glyph_init( FT_Glyph      bitmap_glyph,
61                         FT_GlyphSlot  slot )
62   {
63     FT_BitmapGlyph  glyph   = (FT_BitmapGlyph)bitmap_glyph;
64     FT_Error        error   = FT_Err_Ok;
65     FT_Library      library = FT_GLYPH( glyph )->library;
66 
67 
68     if ( slot->format != FT_GLYPH_FORMAT_BITMAP )
69     {
70       error = FT_THROW( Invalid_Glyph_Format );
71       goto Exit;
72     }
73 
74     glyph->left = slot->bitmap_left;
75     glyph->top  = slot->bitmap_top;
76 
77     /* do lazy copying whenever possible */
78     if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
79     {
80       glyph->bitmap = slot->bitmap;
81       slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
82     }
83     else
84     {
85       FT_Bitmap_New( &glyph->bitmap );
86       error = FT_Bitmap_Copy( library, &slot->bitmap, &glyph->bitmap );
87     }
88 
89   Exit:
90     return error;
91   }
92 
93 
94   FT_CALLBACK_DEF( FT_Error )
ft_bitmap_glyph_copy(FT_Glyph bitmap_source,FT_Glyph bitmap_target)95   ft_bitmap_glyph_copy( FT_Glyph  bitmap_source,
96                         FT_Glyph  bitmap_target )
97   {
98     FT_Library      library = bitmap_source->library;
99     FT_BitmapGlyph  source  = (FT_BitmapGlyph)bitmap_source;
100     FT_BitmapGlyph  target  = (FT_BitmapGlyph)bitmap_target;
101 
102 
103     target->left = source->left;
104     target->top  = source->top;
105 
106     return FT_Bitmap_Copy( library, &source->bitmap, &target->bitmap );
107   }
108 
109 
110   FT_CALLBACK_DEF( void )
ft_bitmap_glyph_done(FT_Glyph bitmap_glyph)111   ft_bitmap_glyph_done( FT_Glyph  bitmap_glyph )
112   {
113     FT_BitmapGlyph  glyph   = (FT_BitmapGlyph)bitmap_glyph;
114     FT_Library      library = FT_GLYPH( glyph )->library;
115 
116 
117     FT_Bitmap_Done( library, &glyph->bitmap );
118   }
119 
120 
121   FT_CALLBACK_DEF( void )
ft_bitmap_glyph_bbox(FT_Glyph bitmap_glyph,FT_BBox * cbox)122   ft_bitmap_glyph_bbox( FT_Glyph  bitmap_glyph,
123                         FT_BBox*  cbox )
124   {
125     FT_BitmapGlyph  glyph = (FT_BitmapGlyph)bitmap_glyph;
126 
127 
128     cbox->xMin = glyph->left << 6;
129     cbox->xMax = cbox->xMin + ( glyph->bitmap.width << 6 );
130     cbox->yMax = glyph->top << 6;
131     cbox->yMin = cbox->yMax - ( glyph->bitmap.rows << 6 );
132   }
133 
134 
135   FT_DEFINE_GLYPH(ft_bitmap_glyph_class,
136     sizeof ( FT_BitmapGlyphRec ),
137     FT_GLYPH_FORMAT_BITMAP,
138 
139     ft_bitmap_glyph_init,
140     ft_bitmap_glyph_done,
141     ft_bitmap_glyph_copy,
142     0,                          /* FT_Glyph_TransformFunc */
143     ft_bitmap_glyph_bbox,
144     0                           /* FT_Glyph_PrepareFunc   */
145   )
146 
147 
148   /*************************************************************************/
149   /*************************************************************************/
150   /****                                                                 ****/
151   /****   FT_OutlineGlyph support                                       ****/
152   /****                                                                 ****/
153   /*************************************************************************/
154   /*************************************************************************/
155 
156 
FT_CALLBACK_DEF(FT_Error)157   FT_CALLBACK_DEF( FT_Error )
158   ft_outline_glyph_init( FT_Glyph      outline_glyph,
159                          FT_GlyphSlot  slot )
160   {
161     FT_OutlineGlyph  glyph   = (FT_OutlineGlyph)outline_glyph;
162     FT_Error         error   = FT_Err_Ok;
163     FT_Library       library = FT_GLYPH( glyph )->library;
164     FT_Outline*      source  = &slot->outline;
165     FT_Outline*      target  = &glyph->outline;
166 
167 
168     /* check format in glyph slot */
169     if ( slot->format != FT_GLYPH_FORMAT_OUTLINE )
170     {
171       error = FT_THROW( Invalid_Glyph_Format );
172       goto Exit;
173     }
174 
175     /* allocate new outline */
176     error = FT_Outline_New( library, source->n_points, source->n_contours,
177                             &glyph->outline );
178     if ( error )
179       goto Exit;
180 
181     FT_Outline_Copy( source, target );
182 
183   Exit:
184     return error;
185   }
186 
187 
188   FT_CALLBACK_DEF( void )
ft_outline_glyph_done(FT_Glyph outline_glyph)189   ft_outline_glyph_done( FT_Glyph  outline_glyph )
190   {
191     FT_OutlineGlyph  glyph = (FT_OutlineGlyph)outline_glyph;
192 
193 
194     FT_Outline_Done( FT_GLYPH( glyph )->library, &glyph->outline );
195   }
196 
197 
198   FT_CALLBACK_DEF( FT_Error )
ft_outline_glyph_copy(FT_Glyph outline_source,FT_Glyph outline_target)199   ft_outline_glyph_copy( FT_Glyph  outline_source,
200                          FT_Glyph  outline_target )
201   {
202     FT_OutlineGlyph  source  = (FT_OutlineGlyph)outline_source;
203     FT_OutlineGlyph  target  = (FT_OutlineGlyph)outline_target;
204     FT_Error         error;
205     FT_Library       library = FT_GLYPH( source )->library;
206 
207 
208     error = FT_Outline_New( library, source->outline.n_points,
209                             source->outline.n_contours, &target->outline );
210     if ( !error )
211       FT_Outline_Copy( &source->outline, &target->outline );
212 
213     return error;
214   }
215 
216 
217   FT_CALLBACK_DEF( void )
ft_outline_glyph_transform(FT_Glyph outline_glyph,const FT_Matrix * matrix,const FT_Vector * delta)218   ft_outline_glyph_transform( FT_Glyph          outline_glyph,
219                               const FT_Matrix*  matrix,
220                               const FT_Vector*  delta )
221   {
222     FT_OutlineGlyph  glyph = (FT_OutlineGlyph)outline_glyph;
223 
224 
225     if ( matrix )
226       FT_Outline_Transform( &glyph->outline, matrix );
227 
228     if ( delta )
229       FT_Outline_Translate( &glyph->outline, delta->x, delta->y );
230   }
231 
232 
233   FT_CALLBACK_DEF( void )
ft_outline_glyph_bbox(FT_Glyph outline_glyph,FT_BBox * bbox)234   ft_outline_glyph_bbox( FT_Glyph  outline_glyph,
235                          FT_BBox*  bbox )
236   {
237     FT_OutlineGlyph  glyph = (FT_OutlineGlyph)outline_glyph;
238 
239 
240     FT_Outline_Get_CBox( &glyph->outline, bbox );
241   }
242 
243 
244   FT_CALLBACK_DEF( FT_Error )
ft_outline_glyph_prepare(FT_Glyph outline_glyph,FT_GlyphSlot slot)245   ft_outline_glyph_prepare( FT_Glyph      outline_glyph,
246                             FT_GlyphSlot  slot )
247   {
248     FT_OutlineGlyph  glyph = (FT_OutlineGlyph)outline_glyph;
249 
250 
251     slot->format         = FT_GLYPH_FORMAT_OUTLINE;
252     slot->outline        = glyph->outline;
253     slot->outline.flags &= ~FT_OUTLINE_OWNER;
254 
255     return FT_Err_Ok;
256   }
257 
258 
FT_DEFINE_GLYPH(ft_outline_glyph_class,sizeof (FT_OutlineGlyphRec),FT_GLYPH_FORMAT_OUTLINE,ft_outline_glyph_init,ft_outline_glyph_done,ft_outline_glyph_copy,ft_outline_glyph_transform,ft_outline_glyph_bbox,ft_outline_glyph_prepare)259   FT_DEFINE_GLYPH( ft_outline_glyph_class,
260     sizeof ( FT_OutlineGlyphRec ),
261     FT_GLYPH_FORMAT_OUTLINE,
262 
263     ft_outline_glyph_init,
264     ft_outline_glyph_done,
265     ft_outline_glyph_copy,
266     ft_outline_glyph_transform,
267     ft_outline_glyph_bbox,
268     ft_outline_glyph_prepare
269   )
270 
271 
272   /*************************************************************************/
273   /*************************************************************************/
274   /****                                                                 ****/
275   /****   FT_Glyph class and API                                        ****/
276   /****                                                                 ****/
277   /*************************************************************************/
278   /*************************************************************************/
279 
280    static FT_Error
281    ft_new_glyph( FT_Library             library,
282                  const FT_Glyph_Class*  clazz,
283                  FT_Glyph*              aglyph )
284    {
285      FT_Memory  memory = library->memory;
286      FT_Error   error;
287      FT_Glyph   glyph  = NULL;
288 
289 
290      *aglyph = 0;
291 
292      if ( !FT_ALLOC( glyph, clazz->glyph_size ) )
293      {
294        glyph->library = library;
295        glyph->clazz   = clazz;
296        glyph->format  = clazz->glyph_format;
297 
298        *aglyph = glyph;
299      }
300 
301      return error;
302    }
303 
304 
305   /* documentation is in ftglyph.h */
306 
307   FT_EXPORT_DEF( FT_Error )
FT_Glyph_Copy(FT_Glyph source,FT_Glyph * target)308   FT_Glyph_Copy( FT_Glyph   source,
309                  FT_Glyph  *target )
310   {
311     FT_Glyph               copy;
312     FT_Error               error;
313     const FT_Glyph_Class*  clazz;
314 
315 
316     /* check arguments */
317     if ( !target )
318     {
319       error = FT_THROW( Invalid_Argument );
320       goto Exit;
321     }
322 
323     *target = 0;
324 
325     if ( !source || !source->clazz )
326     {
327       error = FT_THROW( Invalid_Argument );
328       goto Exit;
329     }
330 
331     clazz = source->clazz;
332     error = ft_new_glyph( source->library, clazz, &copy );
333     if ( error )
334       goto Exit;
335 
336     copy->advance = source->advance;
337     copy->format  = source->format;
338 
339     if ( clazz->glyph_copy )
340       error = clazz->glyph_copy( source, copy );
341 
342     if ( error )
343       FT_Done_Glyph( copy );
344     else
345       *target = copy;
346 
347   Exit:
348     return error;
349   }
350 
351 
352   /* documentation is in ftglyph.h */
353 
354   FT_EXPORT_DEF( FT_Error )
FT_Get_Glyph(FT_GlyphSlot slot,FT_Glyph * aglyph)355   FT_Get_Glyph( FT_GlyphSlot  slot,
356                 FT_Glyph     *aglyph )
357   {
358     FT_Library  library;
359     FT_Error    error;
360     FT_Glyph    glyph;
361 
362     const FT_Glyph_Class*  clazz = 0;
363 
364 
365     if ( !slot )
366       return FT_THROW( Invalid_Slot_Handle );
367 
368     library = slot->library;
369 
370     if ( !aglyph )
371       return FT_THROW( Invalid_Argument );
372 
373     /* if it is a bitmap, that's easy :-) */
374     if ( slot->format == FT_GLYPH_FORMAT_BITMAP )
375       clazz = FT_BITMAP_GLYPH_CLASS_GET;
376 
377     /* if it is an outline */
378     else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
379       clazz = FT_OUTLINE_GLYPH_CLASS_GET;
380 
381     else
382     {
383       /* try to find a renderer that supports the glyph image format */
384       FT_Renderer  render = FT_Lookup_Renderer( library, slot->format, 0 );
385 
386 
387       if ( render )
388         clazz = &render->glyph_class;
389     }
390 
391     if ( !clazz )
392     {
393       error = FT_THROW( Invalid_Glyph_Format );
394       goto Exit;
395     }
396 
397     /* create FT_Glyph object */
398     error = ft_new_glyph( library, clazz, &glyph );
399     if ( error )
400       goto Exit;
401 
402     /* copy advance while converting it to 16.16 format */
403     glyph->advance.x = slot->advance.x << 10;
404     glyph->advance.y = slot->advance.y << 10;
405 
406     /* now import the image from the glyph slot */
407     error = clazz->glyph_init( glyph, slot );
408 
409     /* if an error occurred, destroy the glyph */
410     if ( error )
411       FT_Done_Glyph( glyph );
412     else
413       *aglyph = glyph;
414 
415   Exit:
416     return error;
417   }
418 
419 
420   /* documentation is in ftglyph.h */
421 
422   FT_EXPORT_DEF( FT_Error )
FT_Glyph_Transform(FT_Glyph glyph,FT_Matrix * matrix,FT_Vector * delta)423   FT_Glyph_Transform( FT_Glyph    glyph,
424                       FT_Matrix*  matrix,
425                       FT_Vector*  delta )
426   {
427     FT_Error  error = FT_Err_Ok;
428 
429 
430     if ( !glyph || !glyph->clazz )
431       error = FT_THROW( Invalid_Argument );
432     else
433     {
434       const FT_Glyph_Class*  clazz = glyph->clazz;
435 
436 
437       if ( clazz->glyph_transform )
438       {
439         /* transform glyph image */
440         clazz->glyph_transform( glyph, matrix, delta );
441 
442         /* transform advance vector */
443         if ( matrix )
444           FT_Vector_Transform( &glyph->advance, matrix );
445       }
446       else
447         error = FT_THROW( Invalid_Glyph_Format );
448     }
449     return error;
450   }
451 
452 
453   /* documentation is in ftglyph.h */
454 
455   FT_EXPORT_DEF( void )
FT_Glyph_Get_CBox(FT_Glyph glyph,FT_UInt bbox_mode,FT_BBox * acbox)456   FT_Glyph_Get_CBox( FT_Glyph  glyph,
457                      FT_UInt   bbox_mode,
458                      FT_BBox  *acbox )
459   {
460     const FT_Glyph_Class*  clazz;
461 
462 
463     if ( !acbox )
464       return;
465 
466     acbox->xMin = acbox->yMin = acbox->xMax = acbox->yMax = 0;
467 
468     if ( !glyph || !glyph->clazz )
469       return;
470 
471     clazz = glyph->clazz;
472     if ( !clazz->glyph_bbox )
473       return;
474 
475     /* retrieve bbox in 26.6 coordinates */
476     clazz->glyph_bbox( glyph, acbox );
477 
478     /* perform grid fitting if needed */
479     if ( bbox_mode == FT_GLYPH_BBOX_GRIDFIT ||
480          bbox_mode == FT_GLYPH_BBOX_PIXELS  )
481     {
482       acbox->xMin = FT_PIX_FLOOR( acbox->xMin );
483       acbox->yMin = FT_PIX_FLOOR( acbox->yMin );
484       acbox->xMax = FT_PIX_CEIL( acbox->xMax );
485       acbox->yMax = FT_PIX_CEIL( acbox->yMax );
486     }
487 
488     /* convert to integer pixels if needed */
489     if ( bbox_mode == FT_GLYPH_BBOX_TRUNCATE ||
490          bbox_mode == FT_GLYPH_BBOX_PIXELS   )
491     {
492       acbox->xMin >>= 6;
493       acbox->yMin >>= 6;
494       acbox->xMax >>= 6;
495       acbox->yMax >>= 6;
496     }
497   }
498 
499 
500   /* documentation is in ftglyph.h */
501 
502   FT_EXPORT_DEF( FT_Error )
FT_Glyph_To_Bitmap(FT_Glyph * the_glyph,FT_Render_Mode render_mode,FT_Vector * origin,FT_Bool destroy)503   FT_Glyph_To_Bitmap( FT_Glyph*       the_glyph,
504                       FT_Render_Mode  render_mode,
505                       FT_Vector*      origin,
506                       FT_Bool         destroy )
507   {
508     FT_GlyphSlotRec           dummy;
509     FT_GlyphSlot_InternalRec  dummy_internal;
510     FT_Error                  error = FT_Err_Ok;
511     FT_Glyph                  b, glyph;
512     FT_BitmapGlyph            bitmap = NULL;
513     const FT_Glyph_Class*     clazz;
514 
515     /* FT_BITMAP_GLYPH_CLASS_GET derefers `library' in PIC mode */
516     FT_Library                library;
517 
518 
519     /* check argument */
520     if ( !the_glyph )
521       goto Bad;
522     glyph = *the_glyph;
523     if ( !glyph )
524       goto Bad;
525 
526     clazz   = glyph->clazz;
527     library = glyph->library;
528     if ( !library || !clazz )
529       goto Bad;
530 
531     /* when called with a bitmap glyph, do nothing and return successfully */
532     if ( clazz == FT_BITMAP_GLYPH_CLASS_GET )
533       goto Exit;
534 
535     if ( !clazz->glyph_prepare )
536       goto Bad;
537 
538     /* we render the glyph into a glyph bitmap using a `dummy' glyph slot */
539     /* then calling FT_Render_Glyph_Internal()                            */
540 
541     FT_MEM_ZERO( &dummy, sizeof ( dummy ) );
542     FT_MEM_ZERO( &dummy_internal, sizeof ( dummy_internal ) );
543     dummy.internal = &dummy_internal;
544     dummy.library  = library;
545     dummy.format   = clazz->glyph_format;
546 
547     /* create result bitmap glyph */
548     error = ft_new_glyph( library, FT_BITMAP_GLYPH_CLASS_GET, &b );
549     if ( error )
550       goto Exit;
551     bitmap = (FT_BitmapGlyph)b;
552 
553 #if 1
554     /* if `origin' is set, translate the glyph image */
555     if ( origin )
556       FT_Glyph_Transform( glyph, 0, origin );
557 #else
558     FT_UNUSED( origin );
559 #endif
560 
561     /* prepare dummy slot for rendering */
562     error = clazz->glyph_prepare( glyph, &dummy );
563     if ( !error )
564       error = FT_Render_Glyph_Internal( glyph->library, &dummy, render_mode );
565 
566 #if 1
567     if ( !destroy && origin )
568     {
569       FT_Vector  v;
570 
571 
572       v.x = -origin->x;
573       v.y = -origin->y;
574       FT_Glyph_Transform( glyph, 0, &v );
575     }
576 #endif
577 
578     if ( error )
579       goto Exit;
580 
581     /* in case of success, copy the bitmap to the glyph bitmap */
582     error = ft_bitmap_glyph_init( (FT_Glyph)bitmap, &dummy );
583     if ( error )
584       goto Exit;
585 
586     /* copy advance */
587     bitmap->root.advance = glyph->advance;
588 
589     if ( destroy )
590       FT_Done_Glyph( glyph );
591 
592     *the_glyph = FT_GLYPH( bitmap );
593 
594   Exit:
595     if ( error && bitmap )
596       FT_Done_Glyph( FT_GLYPH( bitmap ) );
597 
598     return error;
599 
600   Bad:
601     error = FT_THROW( Invalid_Argument );
602     goto Exit;
603   }
604 
605 
606   /* documentation is in ftglyph.h */
607 
608   FT_EXPORT_DEF( void )
FT_Done_Glyph(FT_Glyph glyph)609   FT_Done_Glyph( FT_Glyph  glyph )
610   {
611     if ( glyph )
612     {
613       FT_Memory              memory = glyph->library->memory;
614       const FT_Glyph_Class*  clazz  = glyph->clazz;
615 
616 
617       if ( clazz->glyph_done )
618         clazz->glyph_done( glyph );
619 
620       FT_FREE( glyph );
621     }
622   }
623 
624 
625 /* END */
626