cffdrivr.c revision a38fc482eeeb2c1929803c233835369dcf1b8781
1/***************************************************************************/
2/*                                                                         */
3/*  cffdrivr.c                                                             */
4/*                                                                         */
5/*    OpenType font driver implementation (body).                          */
6/*                                                                         */
7/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007 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_FREETYPE_H
21#include FT_INTERNAL_DEBUG_H
22#include FT_INTERNAL_STREAM_H
23#include FT_INTERNAL_SFNT_H
24#include FT_TRUETYPE_IDS_H
25#include FT_SERVICE_CID_H
26#include FT_SERVICE_POSTSCRIPT_CMAPS_H
27#include FT_SERVICE_POSTSCRIPT_INFO_H
28#include FT_SERVICE_POSTSCRIPT_NAME_H
29#include FT_SERVICE_TT_CMAP_H
30
31#include "cffdrivr.h"
32#include "cffgload.h"
33#include "cffload.h"
34#include "cffcmap.h"
35
36#include "cfferrs.h"
37
38#include FT_SERVICE_XFREE86_NAME_H
39#include FT_SERVICE_GLYPH_DICT_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_cffdriver
49
50
51  /*************************************************************************/
52  /*************************************************************************/
53  /*************************************************************************/
54  /****                                                                 ****/
55  /****                                                                 ****/
56  /****                          F A C E S                              ****/
57  /****                                                                 ****/
58  /****                                                                 ****/
59  /*************************************************************************/
60  /*************************************************************************/
61  /*************************************************************************/
62
63
64#undef  PAIR_TAG
65#define PAIR_TAG( left, right )  ( ( (FT_ULong)left << 16 ) | \
66                                     (FT_ULong)right        )
67
68
69  /*************************************************************************/
70  /*                                                                       */
71  /* <Function>                                                            */
72  /*    cff_get_kerning                                                    */
73  /*                                                                       */
74  /* <Description>                                                         */
75  /*    A driver method used to return the kerning vector between two      */
76  /*    glyphs of the same face.                                           */
77  /*                                                                       */
78  /* <Input>                                                               */
79  /*    face        :: A handle to the source face object.                 */
80  /*                                                                       */
81  /*    left_glyph  :: The index of the left glyph in the kern pair.       */
82  /*                                                                       */
83  /*    right_glyph :: The index of the right glyph in the kern pair.      */
84  /*                                                                       */
85  /* <Output>                                                              */
86  /*    kerning     :: The kerning vector.  This is in font units for      */
87  /*                   scalable formats, and in pixels for fixed-sizes     */
88  /*                   formats.                                            */
89  /*                                                                       */
90  /* <Return>                                                              */
91  /*    FreeType error code.  0 means success.                             */
92  /*                                                                       */
93  /* <Note>                                                                */
94  /*    Only horizontal layouts (left-to-right & right-to-left) are        */
95  /*    supported by this function.  Other layouts, or more sophisticated  */
96  /*    kernings, are out of scope of this method (the basic driver        */
97  /*    interface is meant to be simple).                                  */
98  /*                                                                       */
99  /*    They can be implemented by format-specific interfaces.             */
100  /*                                                                       */
101  FT_CALLBACK_DEF( FT_Error )
102  cff_get_kerning( FT_Face     ttface,          /* TT_Face */
103                   FT_UInt     left_glyph,
104                   FT_UInt     right_glyph,
105                   FT_Vector*  kerning )
106  {
107    TT_Face       face = (TT_Face)ttface;
108    SFNT_Service  sfnt = (SFNT_Service)face->sfnt;
109
110
111    kerning->x = 0;
112    kerning->y = 0;
113
114    if ( sfnt )
115      kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
116
117    return CFF_Err_Ok;
118  }
119
120
121#undef PAIR_TAG
122
123
124  /*************************************************************************/
125  /*                                                                       */
126  /* <Function>                                                            */
127  /*    Load_Glyph                                                         */
128  /*                                                                       */
129  /* <Description>                                                         */
130  /*    A driver method used to load a glyph within a given glyph slot.    */
131  /*                                                                       */
132  /* <Input>                                                               */
133  /*    slot        :: A handle to the target slot object where the glyph  */
134  /*                   will be loaded.                                     */
135  /*                                                                       */
136  /*    size        :: A handle to the source face size at which the glyph */
137  /*                   must be scaled, loaded, etc.                        */
138  /*                                                                       */
139  /*    glyph_index :: The index of the glyph in the font file.            */
140  /*                                                                       */
141  /*    load_flags  :: A flag indicating what to load for this glyph.  The */
142  /*                   FT_LOAD_??? constants can be used to control the    */
143  /*                   glyph loading process (e.g., whether the outline    */
144  /*                   should be scaled, whether to load bitmaps or not,   */
145  /*                   whether to hint the outline, etc).                  */
146  /*                                                                       */
147  /* <Return>                                                              */
148  /*    FreeType error code.  0 means success.                             */
149  /*                                                                       */
150  FT_CALLBACK_DEF( FT_Error )
151  Load_Glyph( FT_GlyphSlot  cffslot,        /* CFF_GlyphSlot */
152              FT_Size       cffsize,        /* CFF_Size      */
153              FT_UInt       glyph_index,
154              FT_Int32      load_flags )
155  {
156    FT_Error       error;
157    CFF_GlyphSlot  slot = (CFF_GlyphSlot)cffslot;
158    CFF_Size       size = (CFF_Size)cffsize;
159
160
161    if ( !slot )
162      return CFF_Err_Invalid_Slot_Handle;
163
164    /* check whether we want a scaled outline or bitmap */
165    if ( !size )
166      load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
167
168    /* reset the size object if necessary */
169    if ( load_flags & FT_LOAD_NO_SCALE )
170      size = NULL;
171
172    if ( size )
173    {
174      /* these two objects must have the same parent */
175      if ( cffsize->face != cffslot->face )
176        return CFF_Err_Invalid_Face_Handle;
177    }
178
179    /* now load the glyph outline if necessary */
180    error = cff_slot_load( slot, size, glyph_index, load_flags );
181
182    /* force drop-out mode to 2 - irrelevant now */
183    /* slot->outline.dropout_mode = 2; */
184
185    return error;
186  }
187
188
189  FT_CALLBACK_DEF(FT_Error)
190  cff_get_advances( FT_Face    ftface,
191                    FT_UInt    start,
192                    FT_UInt    count,
193                    FT_UInt    flags,
194                    FT_Fixed*  advances )
195  {
196    CFF_Face      face = (CFF_Face) ftface;
197    FT_UInt       nn;
198    FT_Error      error = 0;
199    FT_GlyphSlot  slot = face->root.glyph;
200
201    flags |= FT_LOAD_ADVANCE_ONLY;
202
203    for (nn = 0; nn < count; nn++)
204    {
205      error = Load_Glyph( slot, face->root.size, start+nn, flags );
206      if (error) break;
207
208      advances[nn] = (flags & FT_LOAD_VERTICAL_LAYOUT)
209                   ? slot->advance.y
210                   : slot->advance.x;
211    }
212    return error;
213  }
214
215 /*
216  *  GLYPH DICT SERVICE
217  *
218  */
219
220  static FT_Error
221  cff_get_glyph_name( CFF_Face    face,
222                      FT_UInt     glyph_index,
223                      FT_Pointer  buffer,
224                      FT_UInt     buffer_max )
225  {
226    CFF_Font            font   = (CFF_Font)face->extra.data;
227    FT_Memory           memory = FT_FACE_MEMORY( face );
228    FT_String*          gname;
229    FT_UShort           sid;
230    FT_Service_PsCMaps  psnames;
231    FT_Error            error;
232
233
234    FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
235    if ( !psnames )
236    {
237      FT_ERROR(( "cff_get_glyph_name:" ));
238      FT_ERROR(( " cannot get glyph name from CFF & CEF fonts\n" ));
239      FT_ERROR(( "                   " ));
240      FT_ERROR(( " without the `PSNames' module\n" ));
241      error = CFF_Err_Unknown_File_Format;
242      goto Exit;
243    }
244
245    /* first, locate the sid in the charset table */
246    sid = font->charset.sids[glyph_index];
247
248    /* now, lookup the name itself */
249    gname = cff_index_get_sid_string( &font->string_index, sid, psnames );
250
251    if ( gname )
252      FT_STRCPYN( buffer, gname, buffer_max );
253
254    FT_FREE( gname );
255    error = CFF_Err_Ok;
256
257  Exit:
258    return error;
259  }
260
261
262  static FT_UInt
263  cff_get_name_index( CFF_Face    face,
264                      FT_String*  glyph_name )
265  {
266    CFF_Font            cff;
267    CFF_Charset         charset;
268    FT_Service_PsCMaps  psnames;
269    FT_Memory           memory = FT_FACE_MEMORY( face );
270    FT_String*          name;
271    FT_UShort           sid;
272    FT_UInt             i;
273    FT_Int              result;
274
275
276    cff     = (CFF_FontRec *)face->extra.data;
277    charset = &cff->charset;
278
279    FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
280    if ( !psnames )
281      return 0;
282
283    for ( i = 0; i < cff->num_glyphs; i++ )
284    {
285      sid = charset->sids[i];
286
287      if ( sid > 390 )
288        name = cff_index_get_name( &cff->string_index, sid - 391 );
289      else
290        name = (FT_String *)psnames->adobe_std_strings( sid );
291
292      if ( !name )
293        continue;
294
295      result = ft_strcmp( glyph_name, name );
296
297      if ( sid > 390 )
298        FT_FREE( name );
299
300      if ( !result )
301        return i;
302    }
303
304    return 0;
305  }
306
307
308  static const FT_Service_GlyphDictRec  cff_service_glyph_dict =
309  {
310    (FT_GlyphDict_GetNameFunc)  cff_get_glyph_name,
311    (FT_GlyphDict_NameIndexFunc)cff_get_name_index,
312  };
313
314
315  /*
316   *  POSTSCRIPT INFO SERVICE
317   *
318   */
319
320  static FT_Int
321  cff_ps_has_glyph_names( FT_Face  face )
322  {
323    return ( face->face_flags & FT_FACE_FLAG_GLYPH_NAMES ) > 0;
324  }
325
326
327  static FT_Error
328  cff_ps_get_font_info( CFF_Face         face,
329                        PS_FontInfoRec*  afont_info )
330  {
331    CFF_Font  cff   = (CFF_Font)face->extra.data;
332    FT_Error  error = FT_Err_Ok;
333
334
335    if ( cff && cff->font_info == NULL )
336    {
337      CFF_FontRecDict     dict    = &cff->top_font.font_dict;
338      PS_FontInfoRec     *font_info;
339      FT_Memory           memory  = face->root.memory;
340      FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
341
342
343      if ( FT_ALLOC( font_info, sizeof ( *font_info ) ) )
344        goto Fail;
345
346      font_info->version     = cff_index_get_sid_string( &cff->string_index,
347                                                         dict->version,
348                                                         psnames );
349      font_info->notice      = cff_index_get_sid_string( &cff->string_index,
350                                                         dict->notice,
351                                                         psnames );
352      font_info->full_name   = cff_index_get_sid_string( &cff->string_index,
353                                                         dict->full_name,
354                                                         psnames );
355      font_info->family_name = cff_index_get_sid_string( &cff->string_index,
356                                                         dict->family_name,
357                                                         psnames );
358      font_info->weight      = cff_index_get_sid_string( &cff->string_index,
359                                                         dict->weight,
360                                                         psnames );
361      font_info->italic_angle        = dict->italic_angle;
362      font_info->is_fixed_pitch      = dict->is_fixed_pitch;
363      font_info->underline_position  = (FT_Short)dict->underline_position;
364      font_info->underline_thickness = (FT_Short)dict->underline_thickness;
365
366      cff->font_info = font_info;
367    }
368
369    *afont_info = *cff->font_info;
370
371  Fail:
372    return error;
373  }
374
375
376  static const FT_Service_PsInfoRec  cff_service_ps_info =
377  {
378    (PS_GetFontInfoFunc)   cff_ps_get_font_info,
379    (PS_HasGlyphNamesFunc) cff_ps_has_glyph_names,
380    (PS_GetFontPrivateFunc)NULL         /* unsupported with CFF fonts */
381  };
382
383
384  /*
385   *  POSTSCRIPT NAME SERVICE
386   *
387   */
388
389  static const char*
390  cff_get_ps_name( CFF_Face  face )
391  {
392    CFF_Font  cff = (CFF_Font)face->extra.data;
393
394
395    return (const char*)cff->font_name;
396  }
397
398
399  static const FT_Service_PsFontNameRec  cff_service_ps_name =
400  {
401    (FT_PsName_GetFunc)cff_get_ps_name
402  };
403
404
405  /*
406   * TT CMAP INFO
407   *
408   * If the charmap is a synthetic Unicode encoding cmap or
409   * a Type 1 standard (or expert) encoding cmap, hide TT CMAP INFO
410   * service defined in SFNT module.
411   *
412   * Otherwise call the service function in the sfnt module.
413   *
414   */
415  static FT_Error
416  cff_get_cmap_info( FT_CharMap    charmap,
417                     TT_CMapInfo  *cmap_info )
418  {
419    FT_CMap   cmap  = FT_CMAP( charmap );
420    FT_Error  error = CFF_Err_Ok;
421
422
423    cmap_info->language = 0;
424
425    if ( cmap->clazz != &cff_cmap_encoding_class_rec &&
426         cmap->clazz != &cff_cmap_unicode_class_rec  )
427    {
428      FT_Face             face    = FT_CMAP_FACE( cmap );
429      FT_Library          library = FT_FACE_LIBRARY( face );
430      FT_Module           sfnt    = FT_Get_Module( library, "sfnt" );
431      FT_Service_TTCMaps  service =
432        (FT_Service_TTCMaps)ft_module_get_service( sfnt,
433                                                   FT_SERVICE_ID_TT_CMAP );
434
435
436      if ( service && service->get_cmap_info )
437        error = service->get_cmap_info( charmap, cmap_info );
438    }
439
440    return error;
441  }
442
443
444  static const FT_Service_TTCMapsRec  cff_service_get_cmap_info =
445  {
446    (TT_CMap_Info_GetFunc)cff_get_cmap_info
447  };
448
449
450  /*
451   *  CID INFO SERVICE
452   *
453   */
454  static FT_Error
455  cff_get_ros( CFF_Face      face,
456               const char*  *registry,
457               const char*  *ordering,
458               FT_Int       *supplement )
459  {
460    FT_Error  error = CFF_Err_Ok;
461    CFF_Font  cff   = (CFF_Font)face->extra.data;
462
463
464    if ( cff )
465    {
466      CFF_FontRecDict     dict    = &cff->top_font.font_dict;
467      FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
468
469
470      if ( dict->cid_registry == 0xFFFFU )
471      {
472        error = CFF_Err_Invalid_Argument;
473        goto Fail;
474      }
475
476      if ( registry )
477      {
478        if ( cff->registry == NULL )
479          cff->registry = cff_index_get_sid_string( &cff->string_index,
480                                                    dict->cid_registry,
481                                                    psnames );
482        *registry = cff->registry;
483      }
484
485      if ( ordering )
486      {
487        if ( cff->ordering == NULL )
488          cff->ordering = cff_index_get_sid_string( &cff->string_index,
489                                                    dict->cid_ordering,
490                                                    psnames );
491        *ordering = cff->ordering;
492      }
493
494      if ( supplement )
495        *supplement = dict->cid_supplement;
496    }
497
498  Fail:
499    return error;
500  }
501
502
503  static const FT_Service_CIDRec  cff_service_cid_info =
504  {
505    (FT_CID_GetRegistryOrderingSupplementFunc)cff_get_ros
506  };
507
508
509  /*************************************************************************/
510  /*************************************************************************/
511  /*************************************************************************/
512  /****                                                                 ****/
513  /****                                                                 ****/
514  /****                D R I V E R  I N T E R F A C E                   ****/
515  /****                                                                 ****/
516  /****                                                                 ****/
517  /*************************************************************************/
518  /*************************************************************************/
519  /*************************************************************************/
520
521  static const FT_ServiceDescRec  cff_services[] =
522  {
523    { FT_SERVICE_ID_XF86_NAME,            FT_XF86_FORMAT_CFF },
524    { FT_SERVICE_ID_POSTSCRIPT_INFO,      &cff_service_ps_info },
525    { FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &cff_service_ps_name },
526#ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
527    { FT_SERVICE_ID_GLYPH_DICT,           &cff_service_glyph_dict },
528#endif
529    { FT_SERVICE_ID_TT_CMAP,              &cff_service_get_cmap_info },
530    { FT_SERVICE_ID_CID,                  &cff_service_cid_info },
531    { NULL, NULL }
532  };
533
534
535  FT_CALLBACK_DEF( FT_Module_Interface )
536  cff_get_interface( FT_Module    driver,       /* CFF_Driver */
537                     const char*  module_interface )
538  {
539    FT_Module            sfnt;
540    FT_Module_Interface  result;
541
542
543    result = ft_service_list_lookup( cff_services, module_interface );
544    if ( result != NULL )
545      return  result;
546
547    /* we pass our request to the `sfnt' module */
548    sfnt = FT_Get_Module( driver->library, "sfnt" );
549
550    return sfnt ? sfnt->clazz->get_interface( sfnt, module_interface ) : 0;
551  }
552
553
554  /* The FT_DriverInterface structure is defined in ftdriver.h. */
555
556  FT_CALLBACK_TABLE_DEF
557  const FT_Driver_ClassRec  cff_driver_class =
558  {
559    /* begin with the FT_Module_Class fields */
560    {
561      FT_MODULE_FONT_DRIVER       |
562      FT_MODULE_DRIVER_SCALABLE   |
563      FT_MODULE_DRIVER_HAS_HINTER,
564
565      sizeof( CFF_DriverRec ),
566      "cff",
567      0x10000L,
568      0x20000L,
569
570      0,   /* module-specific interface */
571
572      cff_driver_init,
573      cff_driver_done,
574      cff_get_interface,
575    },
576
577    /* now the specific driver fields */
578    sizeof( TT_FaceRec ),
579    sizeof( CFF_SizeRec ),
580    sizeof( CFF_GlyphSlotRec ),
581
582    cff_face_init,
583    cff_face_done,
584    cff_size_init,
585    cff_size_done,
586    cff_slot_init,
587    cff_slot_done,
588
589#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
590    ft_stub_set_char_sizes,
591    ft_stub_set_pixel_sizes,
592#endif
593
594    Load_Glyph,
595
596    cff_get_kerning,
597    0,                      /* FT_Face_AttachFunc      */
598    cff_get_advances,       /* FT_Face_GetAdvancesFunc */
599
600    cff_size_request,
601
602#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
603    cff_size_select
604#else
605    0                       /* FT_Size_SelectFunc      */
606#endif
607  };
608
609
610/* END */
611