:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / subsys / win32k / freetype / src / truetype / ttdriver.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ttdriver.c                                                             */
4 /*                                                                         */
5 /*    TrueType font driver implementation (body).                          */
6 /*                                                                         */
7 /*  Copyright 1996-2000 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 <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftstream.h>
21 #include <freetype/internal/sfnt.h>
22 #include <freetype/ttnameid.h>
23
24
25 #ifdef FT_FLAT_COMPILE
26
27 #include "ttdriver.h"
28 #include "ttgload.h"
29
30 #else
31
32 #include <freetype/src/truetype/ttdriver.h>
33 #include <freetype/src/truetype/ttgload.h>
34
35 #endif
36
37
38   /*************************************************************************/
39   /*                                                                       */
40   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
41   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
42   /* messages during execution.                                            */
43   /*                                                                       */
44 #undef  FT_COMPONENT
45 #define FT_COMPONENT  trace_ttdriver
46
47
48   /*************************************************************************/
49   /*************************************************************************/
50   /*************************************************************************/
51   /****                                                                 ****/
52   /****                                                                 ****/
53   /****                          F A C E S                              ****/
54   /****                                                                 ****/
55   /****                                                                 ****/
56   /*************************************************************************/
57   /*************************************************************************/
58   /*************************************************************************/
59
60
61 #undef  PAIR_TAG
62 #define PAIR_TAG( left, right )  ( ( (FT_ULong)left << 16 ) | \
63                                      (FT_ULong)right        )
64
65
66   /*************************************************************************/
67   /*                                                                       */
68   /* <Function>                                                            */
69   /*    Get_Kerning                                                        */
70   /*                                                                       */
71   /* <Description>                                                         */
72   /*    A driver method used to return the kerning vector between two      */
73   /*    glyphs of the same face.                                           */
74   /*                                                                       */
75   /* <Input>                                                               */
76   /*    face        :: A handle to the source face object.                 */
77   /*                                                                       */
78   /*    left_glyph  :: The index of the left glyph in the kern pair.       */
79   /*                                                                       */
80   /*    right_glyph :: The index of the right glyph in the kern pair.      */
81   /*                                                                       */
82   /* <Output>                                                              */
83   /*    kerning     :: The kerning vector.  This is in font units for      */
84   /*                   scalable formats, and in pixels for fixed-sizes     */
85   /*                   formats.                                            */
86   /*                                                                       */
87   /* <Return>                                                              */
88   /*    FreeType error code.  0 means success.                             */
89   /*                                                                       */
90   /* <Note>                                                                */
91   /*    Only horizontal layouts (left-to-right & right-to-left) are        */
92   /*    supported by this function.  Other layouts, or more sophisticated  */
93   /*    kernings, are out of scope of this method (the basic driver        */
94   /*    interface is meant to be simple).                                  */
95   /*                                                                       */
96   /*    They can be implemented by format-specific interfaces.             */
97   /*                                                                       */
98   static
99   FT_Error  Get_Kerning( TT_Face     face,
100                          FT_UInt     left_glyph,
101                          FT_UInt     right_glyph,
102                          FT_Vector*  kerning )
103   {
104     TT_Kern_0_Pair*  pair;
105
106
107     if ( !face )
108       return TT_Err_Invalid_Face_Handle;
109
110     kerning->x = 0;
111     kerning->y = 0;
112
113     if ( face->kern_pairs )
114     {
115       /* there are some kerning pairs in this font file! */
116       FT_ULong  search_tag = PAIR_TAG( left_glyph, right_glyph );
117       FT_Long   left, right;
118
119
120       left  = 0;
121       right = face->num_kern_pairs - 1;
122
123       while ( left <= right )
124       {
125         FT_Int    middle = left + ( ( right - left ) >> 1 );
126         FT_ULong  cur_pair;
127
128
129         pair     = face->kern_pairs + middle;
130         cur_pair = PAIR_TAG( pair->left, pair->right );
131
132         if ( cur_pair == search_tag )
133           goto Found;
134
135         if ( cur_pair < search_tag )
136           left = middle + 1;
137         else
138           right = middle - 1;
139       }
140     }
141
142   Exit:
143     return TT_Err_Ok;
144
145   Found:
146     kerning->x = pair->value;
147     goto Exit;
148   }
149
150
151 #undef PAIR_TAG
152
153
154   /*************************************************************************/
155   /*************************************************************************/
156   /*************************************************************************/
157   /****                                                                 ****/
158   /****                                                                 ****/
159   /****                           S I Z E S                             ****/
160   /****                                                                 ****/
161   /****                                                                 ****/
162   /*************************************************************************/
163   /*************************************************************************/
164   /*************************************************************************/
165
166
167   /*************************************************************************/
168   /*                                                                       */
169   /* <Function>                                                            */
170   /*    Set_Char_Sizes                                                     */
171   /*                                                                       */
172   /* <Description>                                                         */
173   /*    A driver method used to reset a size's character sizes (horizontal */
174   /*    and vertical) expressed in fractional points.                      */
175   /*                                                                       */
176   /* <Input>                                                               */
177   /*    char_width      :: The character width expressed in 26.6           */
178   /*                       fractional points.                              */
179   /*                                                                       */
180   /*    char_height     :: The character height expressed in 26.6          */
181   /*                       fractional points.                              */
182   /*                                                                       */
183   /*    horz_resolution :: The horizontal resolution of the output device. */
184   /*                                                                       */
185   /*    vert_resolution :: The vertical resolution of the output device.   */
186   /*                                                                       */
187   /* <InOut>                                                               */
188   /*    size            :: A handle to the target size object.             */
189   /*                                                                       */
190   /* <Return>                                                              */
191   /*    FreeType error code.  0 means success.                             */
192   /*                                                                       */
193   static
194   FT_Error  Set_Char_Sizes( TT_Size     size,
195                             FT_F26Dot6  char_width,
196                             FT_F26Dot6  char_height,
197                             FT_UInt     horz_resolution,
198                             FT_UInt     vert_resolution )
199   {
200     FT_Size_Metrics*  metrics = &size->root.metrics;
201     TT_Face           face    = (TT_Face)size->root.face;
202     FT_Long           dim_x, dim_y;
203
204
205     /* This bit flag, when set, indicates that the pixel size must be */
206     /* truncated to an integer.  Nearly all TrueType fonts have this  */
207     /* bit set, as hinting won't work really well otherwise.          */
208     /*                                                                */
209     /* However, for those rare fonts who do not set it, we override   */
210     /* the default computations performed by the base layer.  I       */
211     /* really don't know whether this is useful, but hey, that's the  */
212     /* spec :-)                                                       */
213     /*                                                                */
214     if ( ( face->header.Flags & 8 ) == 0 )
215     {
216       /* Compute pixel sizes in 26.6 units */
217       dim_x = ( char_width  * horz_resolution ) / 72;
218       dim_y = ( char_height * vert_resolution ) / 72;
219
220       metrics->x_scale = FT_DivFix( dim_x, face->root.units_per_EM );
221       metrics->y_scale = FT_DivFix( dim_y, face->root.units_per_EM );
222
223       metrics->x_ppem  = (FT_UShort)( dim_x >> 6 );
224       metrics->y_ppem  = (FT_UShort)( dim_y >> 6 );
225     }
226
227     size->ttmetrics.valid = FALSE;
228
229     return TT_Reset_Size( size );
230   }
231
232
233   /*************************************************************************/
234   /*                                                                       */
235   /* <Function>                                                            */
236   /*    Set_Pixel_Sizes                                                    */
237   /*                                                                       */
238   /* <Description>                                                         */
239   /*    A driver method used to reset a size's character sizes (horizontal */
240   /*    and vertical) expressed in integer pixels.                         */
241   /*                                                                       */
242   /* <Input>                                                               */
243   /*    pixel_width  :: The character width expressed in integer pixels.   */
244   /*                                                                       */
245   /*    pixel_height :: The character height expressed in integer pixels.  */
246   /*                                                                       */
247   /* <InOut>                                                               */
248   /*    size         :: A handle to the target size object.                */
249   /*                                                                       */
250   /* <Return>                                                              */
251   /*    FreeType error code.  0 means success.                             */
252   /*                                                                       */
253   static
254   FT_Error  Set_Pixel_Sizes( TT_Size  size,
255                              FT_UInt  pixel_width,
256                              FT_UInt  pixel_height )
257   {
258     FT_UNUSED( pixel_width );
259     FT_UNUSED( pixel_height );
260
261     /* many things have been pre-computed by the base layer */
262
263     size->ttmetrics.valid = FALSE;
264
265     return TT_Reset_Size( size );
266   }
267
268
269   /*************************************************************************/
270   /*                                                                       */
271   /* <Function>                                                            */
272   /*    Load_Glyph                                                         */
273   /*                                                                       */
274   /* <Description>                                                         */
275   /*    A driver method used to load a glyph within a given glyph slot.    */
276   /*                                                                       */
277   /* <Input>                                                               */
278   /*    slot        :: A handle to the target slot object where the glyph  */
279   /*                   will be loaded.                                     */
280   /*                                                                       */
281   /*    size        :: A handle to the source face size at which the glyph */
282   /*                   must be scaled, loaded, etc.                        */
283   /*                                                                       */
284   /*    glyph_index :: The index of the glyph in the font file.            */
285   /*                                                                       */
286   /*    load_flags  :: A flag indicating what to load for this glyph.  The */
287   /*                   FTLOAD_??? constants can be used to control the     */
288   /*                   glyph loading process (e.g., whether the outline    */
289   /*                   should be scaled, whether to load bitmaps or not,   */
290   /*                   whether to hint the outline, etc).                  */
291   /*                                                                       */
292   /* <Return>                                                              */
293   /*    FreeType error code.  0 means success.                             */
294   /*                                                                       */
295   static
296   FT_Error  Load_Glyph( TT_GlyphSlot  slot,
297                         TT_Size       size,
298                         FT_UShort     glyph_index,
299                         FT_UInt       load_flags )
300   {
301     FT_Error  error;
302
303
304     if ( !slot )
305       return TT_Err_Invalid_Glyph_Handle;
306
307     /* check whether we want a scaled outline or bitmap */
308     if ( !size )
309       load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
310
311     if ( load_flags & FT_LOAD_NO_SCALE )
312       size = NULL;
313
314     /* reset the size object if necessary */
315     if ( size )
316     {
317       /* these two object must have the same parent */
318       if ( size->root.face != slot->face )
319         return TT_Err_Invalid_Face_Handle;
320
321       if ( !size->ttmetrics.valid )
322       {
323         if ( FT_SET_ERROR( TT_Reset_Size( size ) ) )
324           return error;
325       }
326     }
327
328     /* now load the glyph outline if necessary */
329     error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
330
331     /* force drop-out mode to 2 - irrelevant now */
332     /* slot->outline.dropout_mode = 2; */
333
334     return error;
335   }
336
337
338   /*************************************************************************/
339   /*************************************************************************/
340   /*************************************************************************/
341   /****                                                                 ****/
342   /****                                                                 ****/
343   /****             C H A R A C T E R   M A P P I N G S                 ****/
344   /****                                                                 ****/
345   /****                                                                 ****/
346   /*************************************************************************/
347   /*************************************************************************/
348   /*************************************************************************/
349
350   /*************************************************************************/
351   /*                                                                       */
352   /* <Function>                                                            */
353   /*    Get_Char_Index                                                     */
354   /*                                                                       */
355   /* <Description>                                                         */
356   /*    Uses a charmap to return a given character code's glyph index.     */
357   /*                                                                       */
358   /* <Input>                                                               */
359   /*    charmap  :: A handle to the source charmap object.                 */
360   /*    charcode :: The character code.                                    */
361   /*                                                                       */
362   /* <Return>                                                              */
363   /*    Glyph index.  0 means `undefined character code'.                  */
364   /*                                                                       */
365   static
366   FT_UInt  Get_Char_Index( TT_CharMap  charmap,
367                            FT_Long     charcode )
368   {
369     FT_Error       error;
370     TT_Face        face;
371     TT_CMapTable*  cmap;
372
373
374     cmap = &charmap->cmap;
375     face = (TT_Face)charmap->root.face;
376
377     /* Load table if needed */
378     if ( !cmap->loaded )
379     {
380       SFNT_Interface*  sfnt = (SFNT_Interface*)face->sfnt;
381
382
383       error = sfnt->load_charmap( face, cmap, face->root.stream );
384       if ( error )
385         return 0;
386
387       cmap->loaded = TRUE;
388     }
389
390     if ( cmap->get_index )
391       return cmap->get_index( cmap, charcode );
392     else
393       return 0;
394   }
395
396
397   /*************************************************************************/
398   /*************************************************************************/
399   /*************************************************************************/
400   /****                                                                 ****/
401   /****                                                                 ****/
402   /****                D R I V E R  I N T E R F A C E                   ****/
403   /****                                                                 ****/
404   /****                                                                 ****/
405   /*************************************************************************/
406   /*************************************************************************/
407   /*************************************************************************/
408
409
410   static
411   FT_Module_Interface  tt_get_interface( TT_Driver    driver,
412                                          const char*  interface )
413   {
414     FT_Module        sfntd = FT_Get_Module( driver->root.root.library,
415                                             "sfnt" );
416     SFNT_Interface*  sfnt;
417
418
419     /* only return the default interface from the SFNT module */
420     if ( sfntd )
421     {
422       sfnt = (SFNT_Interface*)( sfntd->clazz->module_interface );
423       if ( sfnt )
424         return sfnt->get_interface( FT_MODULE( driver ), interface );
425     }
426
427     return 0;
428   }
429
430
431   /* The FT_DriverInterface structure is defined in ftdriver.h. */
432
433   const FT_Driver_Class  tt_driver_class =
434   {
435     {
436       ft_module_font_driver     |
437       ft_module_driver_scalable |
438 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
439       ft_module_driver_has_hinter,
440 #else
441       0,
442 #endif            
443
444       sizeof ( TT_DriverRec ),
445
446       "truetype",      /* driver name                           */
447       0x10000L,        /* driver version == 1.0                 */
448       0x20000L,        /* driver requires FreeType 2.0 or above */
449
450       (void*)0,        /* driver specific interface */
451
452       (FT_Module_Constructor)TT_Init_Driver,
453       (FT_Module_Destructor) TT_Done_Driver,
454       (FT_Module_Requester)  tt_get_interface,
455     },
456
457     sizeof ( TT_FaceRec ),
458     sizeof ( TT_SizeRec ),
459     sizeof ( FT_GlyphSlotRec ),
460
461
462     (FTDriver_initFace)     TT_Init_Face,
463     (FTDriver_doneFace)     TT_Done_Face,
464     (FTDriver_initSize)     TT_Init_Size,
465     (FTDriver_doneSize)     TT_Done_Size,
466     (FTDriver_initGlyphSlot)0,
467     (FTDriver_doneGlyphSlot)0,
468
469     (FTDriver_setCharSizes) Set_Char_Sizes,
470     (FTDriver_setPixelSizes)Set_Pixel_Sizes,
471     (FTDriver_loadGlyph)    Load_Glyph,
472     (FTDriver_getCharIndex) Get_Char_Index,
473
474     (FTDriver_getKerning)   Get_Kerning,
475     (FTDriver_attachFile)   0,
476     (FTDriver_getAdvances)  0
477   };
478
479
480 #ifdef FT_CONFIG_OPTION_DYNAMIC_DRIVERS
481
482
483   /*************************************************************************/
484   /*                                                                       */
485   /* <Function>                                                            */
486   /*    getDriverInterface                                                 */
487   /*                                                                       */
488   /* <Description>                                                         */
489   /*    This function is used when compiling the TrueType driver as a      */
490   /*    shared library (`.DLL' or `.so').  It will be used by the          */
491   /*    high-level library of FreeType to retrieve the address of the      */
492   /*    driver's generic interface.                                        */
493   /*                                                                       */
494   /*    It shouldn't be implemented in a static build, as each driver must */
495   /*    have the same function as an exported entry point.                 */
496   /*                                                                       */
497   /* <Return>                                                              */
498   /*    The address of the TrueType's driver generic interface.  The       */
499   /*    format-specific interface can then be retrieved through the method */
500   /*    interface->get_format_interface.                                   */
501   /*                                                                       */
502   EXPORT_FUNC( const FT_Driver_Class* )  getDriverClass( void )
503   {
504     return &tt_driver_class;
505   }
506
507
508 #endif /* CONFIG_OPTION_DYNAMIC_DRIVERS */
509
510
511 /* END */