cffobjs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /***************************************************************************/
  2. /* */
  3. /* cffobjs.c */
  4. /* */
  5. /* OpenType objects manager (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002 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. #include <ft2build.h>
  18. #include FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_CALC_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include FT_ERRORS_H
  22. #include FT_TRUETYPE_IDS_H
  23. #include FT_TRUETYPE_TAGS_H
  24. #include FT_INTERNAL_SFNT_H
  25. #include FT_INTERNAL_POSTSCRIPT_NAMES_H
  26. #include FT_INTERNAL_POSTSCRIPT_HINTS_H
  27. #include "cffobjs.h"
  28. #include "cffload.h"
  29. #include "cffcmap.h"
  30. #include "cfferrs.h"
  31. /*************************************************************************/
  32. /* */
  33. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  34. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  35. /* messages during execution. */
  36. /* */
  37. #undef FT_COMPONENT
  38. #define FT_COMPONENT trace_cffobjs
  39. /*************************************************************************/
  40. /* */
  41. /* SIZE FUNCTIONS */
  42. /* */
  43. /* Note that we store the global hints in the size's "internal" root */
  44. /* field. */
  45. /* */
  46. /*************************************************************************/
  47. static PSH_Globals_Funcs
  48. cff_size_get_globals_funcs( CFF_Size size )
  49. {
  50. CFF_Face face = (CFF_Face)size->face;
  51. CFF_Font font = (CFF_FontRec *)face->extra.data;
  52. PSHinter_Service pshinter = (PSHinter_Service)font->pshinter;
  53. FT_Module module;
  54. module = FT_Get_Module( size->face->driver->root.library,
  55. "pshinter" );
  56. return ( module && pshinter && pshinter->get_globals_funcs )
  57. ? pshinter->get_globals_funcs( module )
  58. : 0;
  59. }
  60. FT_LOCAL_DEF( void )
  61. cff_size_done( CFF_Size size )
  62. {
  63. if ( size->internal )
  64. {
  65. PSH_Globals_Funcs funcs;
  66. funcs = cff_size_get_globals_funcs( size );
  67. if ( funcs )
  68. funcs->destroy( (PSH_Globals)size->internal );
  69. size->internal = 0;
  70. }
  71. }
  72. FT_LOCAL_DEF( FT_Error )
  73. cff_size_init( CFF_Size size )
  74. {
  75. FT_Error error = 0;
  76. PSH_Globals_Funcs funcs = cff_size_get_globals_funcs( size );
  77. if ( funcs )
  78. {
  79. PSH_Globals globals;
  80. CFF_Face face = (CFF_Face)size->face;
  81. CFF_Font font = (CFF_FontRec *)face->extra.data;
  82. CFF_SubFont subfont = &font->top_font;
  83. CFF_Private cpriv = &subfont->private_dict;
  84. PS_PrivateRec priv;
  85. /* IMPORTANT: The CFF and Type1 private dictionaries have */
  86. /* slightly different structures; we need to */
  87. /* synthetize a type1 dictionary on the fly here. */
  88. {
  89. FT_UInt n, count;
  90. FT_MEM_ZERO( &priv, sizeof ( priv ) );
  91. count = priv.num_blue_values = cpriv->num_blue_values;
  92. for ( n = 0; n < count; n++ )
  93. priv.blue_values[n] = (FT_Short)cpriv->blue_values[n];
  94. count = priv.num_other_blues = cpriv->num_other_blues;
  95. for ( n = 0; n < count; n++ )
  96. priv.other_blues[n] = (FT_Short)cpriv->other_blues[n];
  97. count = priv.num_family_blues = cpriv->num_family_blues;
  98. for ( n = 0; n < count; n++ )
  99. priv.family_blues[n] = (FT_Short)cpriv->family_blues[n];
  100. count = priv.num_family_other_blues = cpriv->num_family_other_blues;
  101. for ( n = 0; n < count; n++ )
  102. priv.family_other_blues[n] = (FT_Short)cpriv->family_other_blues[n];
  103. priv.blue_scale = cpriv->blue_scale;
  104. priv.blue_shift = (FT_Int)cpriv->blue_shift;
  105. priv.blue_fuzz = (FT_Int)cpriv->blue_fuzz;
  106. priv.standard_width[0] = (FT_UShort)cpriv->standard_width;
  107. priv.standard_height[0] = (FT_UShort)cpriv->standard_height;
  108. count = priv.num_snap_widths = cpriv->num_snap_widths;
  109. for ( n = 0; n < count; n++ )
  110. priv.snap_widths[n] = (FT_Short)cpriv->snap_widths[n];
  111. count = priv.num_snap_heights = cpriv->num_snap_heights;
  112. for ( n = 0; n < count; n++ )
  113. priv.snap_heights[n] = (FT_Short)cpriv->snap_heights[n];
  114. priv.force_bold = cpriv->force_bold;
  115. priv.language_group = cpriv->language_group;
  116. priv.lenIV = cpriv->lenIV;
  117. }
  118. error = funcs->create( size->face->memory, &priv, &globals );
  119. if ( !error )
  120. size->internal = (FT_Size_Internal)(void*)globals;
  121. }
  122. return error;
  123. }
  124. FT_LOCAL_DEF( FT_Error )
  125. cff_size_reset( CFF_Size size )
  126. {
  127. PSH_Globals_Funcs funcs = cff_size_get_globals_funcs( size );
  128. FT_Error error = 0;
  129. if ( funcs )
  130. error = funcs->set_scale( (PSH_Globals)size->internal,
  131. size->metrics.x_scale,
  132. size->metrics.y_scale,
  133. 0, 0 );
  134. return error;
  135. }
  136. /*************************************************************************/
  137. /* */
  138. /* SLOT FUNCTIONS */
  139. /* */
  140. /*************************************************************************/
  141. FT_LOCAL_DEF( void )
  142. cff_slot_done( CFF_GlyphSlot slot )
  143. {
  144. slot->root.internal->glyph_hints = 0;
  145. }
  146. FT_LOCAL_DEF( FT_Error )
  147. cff_slot_init( CFF_GlyphSlot slot )
  148. {
  149. CFF_Face face = (CFF_Face)slot->root.face;
  150. CFF_Font font = (CFF_FontRec *)face->extra.data;
  151. PSHinter_Service pshinter = (PSHinter_Service)font->pshinter;
  152. if ( pshinter )
  153. {
  154. FT_Module module;
  155. module = FT_Get_Module( slot->root.face->driver->root.library,
  156. "pshinter" );
  157. if ( module )
  158. {
  159. T2_Hints_Funcs funcs;
  160. funcs = pshinter->get_t2_funcs( module );
  161. slot->root.internal->glyph_hints = (void*)funcs;
  162. }
  163. }
  164. return 0;
  165. }
  166. /*************************************************************************/
  167. /* */
  168. /* FACE FUNCTIONS */
  169. /* */
  170. /*************************************************************************/
  171. static FT_String*
  172. cff_strcpy( FT_Memory memory,
  173. const FT_String* source )
  174. {
  175. FT_Error error;
  176. FT_String* result = 0;
  177. FT_Int len = (FT_Int)ft_strlen( source );
  178. if ( !FT_ALLOC( result, len + 1 ) )
  179. {
  180. FT_MEM_COPY( result, source, len );
  181. result[len] = 0;
  182. }
  183. FT_UNUSED( error );
  184. return result;
  185. }
  186. FT_LOCAL_DEF( FT_Error )
  187. cff_face_init( FT_Stream stream,
  188. CFF_Face face,
  189. FT_Int face_index,
  190. FT_Int num_params,
  191. FT_Parameter* params )
  192. {
  193. FT_Error error;
  194. SFNT_Service sfnt;
  195. PSNames_Service psnames;
  196. PSHinter_Service pshinter;
  197. FT_Bool pure_cff = 1;
  198. FT_Bool sfnt_format = 0;
  199. sfnt = (SFNT_Service)FT_Get_Module_Interface(
  200. face->root.driver->root.library, "sfnt" );
  201. if ( !sfnt )
  202. goto Bad_Format;
  203. psnames = (PSNames_Service)FT_Get_Module_Interface(
  204. face->root.driver->root.library, "psnames" );
  205. pshinter = (PSHinter_Service)FT_Get_Module_Interface(
  206. face->root.driver->root.library, "pshinter" );
  207. /* create input stream from resource */
  208. if ( FT_STREAM_SEEK( 0 ) )
  209. goto Exit;
  210. /* check that we have a valid OpenType file */
  211. error = sfnt->init_face( stream, face, face_index, num_params, params );
  212. if ( !error )
  213. {
  214. if ( face->format_tag != 0x4F54544FL ) /* `OTTO'; OpenType/CFF font */
  215. {
  216. FT_TRACE2(( "[not a valid OpenType/CFF font]\n" ));
  217. goto Bad_Format;
  218. }
  219. /* if we are performing a simple font format check, exit immediately */
  220. if ( face_index < 0 )
  221. return CFF_Err_Ok;
  222. sfnt_format = 1;
  223. /* now, the font can be either an OpenType/CFF font, or an SVG CEF */
  224. /* font in the later case; it doesn't have a `head' table */
  225. error = face->goto_table( face, TTAG_head, stream, 0 );
  226. if ( !error )
  227. {
  228. pure_cff = 0;
  229. /* load font directory */
  230. error = sfnt->load_face( stream, face,
  231. face_index, num_params, params );
  232. if ( error )
  233. goto Exit;
  234. }
  235. else
  236. {
  237. /* load the `cmap' table by hand */
  238. error = sfnt->load_charmaps( face, stream );
  239. if ( error )
  240. goto Exit;
  241. /* XXX: we don't load the GPOS table, as OpenType Layout */
  242. /* support will be added later to a layout library on top of */
  243. /* FreeType 2 */
  244. }
  245. /* now, load the CFF part of the file */
  246. error = face->goto_table( face, TTAG_CFF, stream, 0 );
  247. if ( error )
  248. goto Exit;
  249. }
  250. else
  251. {
  252. /* rewind to start of file; we are going to load a pure-CFF font */
  253. if ( FT_STREAM_SEEK( 0 ) )
  254. goto Exit;
  255. error = CFF_Err_Ok;
  256. }
  257. /* now load and parse the CFF table in the file */
  258. {
  259. CFF_Font cff;
  260. FT_Memory memory = face->root.memory;
  261. FT_Face root;
  262. FT_Int32 flags;
  263. if ( FT_NEW( cff ) )
  264. goto Exit;
  265. face->extra.data = cff;
  266. error = cff_font_load( stream, face_index, cff );
  267. if ( error )
  268. goto Exit;
  269. cff->pshinter = pshinter;
  270. cff->psnames = psnames;
  271. /* Complement the root flags with some interesting information. */
  272. /* Note that this is only necessary for pure CFF and CEF fonts. */
  273. root = &face->root;
  274. root->num_glyphs = cff->num_glyphs;
  275. if ( pure_cff )
  276. {
  277. CFF_FontRecDict dict = &cff->top_font.font_dict;
  278. /* we need the `PSNames' module for pure-CFF and CEF formats */
  279. if ( !psnames )
  280. {
  281. FT_ERROR(( "cff_face_init:" ));
  282. FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
  283. FT_ERROR(( " " ));
  284. FT_ERROR(( " without the `PSNames' module\n" ));
  285. goto Bad_Format;
  286. }
  287. /* Set up num_faces. */
  288. root->num_faces = cff->num_faces;
  289. /* compute number of glyphs */
  290. if ( dict->cid_registry )
  291. root->num_glyphs = dict->cid_count;
  292. else
  293. root->num_glyphs = cff->charstrings_index.count;
  294. /* set global bbox, as well as EM size */
  295. root->bbox.xMin = dict->font_bbox.xMin >> 16;
  296. root->bbox.yMin = dict->font_bbox.yMin >> 16;
  297. root->bbox.xMax = ( dict->font_bbox.xMax + 0xFFFFU ) >> 16;
  298. root->bbox.yMax = ( dict->font_bbox.yMax + 0xFFFFU ) >> 16;
  299. root->ascender = (FT_Short)( root->bbox.yMax );
  300. root->descender = (FT_Short)( root->bbox.yMin );
  301. root->height = (FT_Short)(
  302. ( ( root->ascender - root->descender ) * 12 ) / 10 );
  303. if ( dict->units_per_em )
  304. root->units_per_EM = dict->units_per_em;
  305. else
  306. root->units_per_EM = 1000;
  307. /* retrieve font family & style name */
  308. root->family_name = cff_index_get_name( &cff->name_index, face_index );
  309. if ( dict->cid_registry )
  310. root->style_name = cff_strcpy( memory, "Regular" ); /* XXXX */
  311. else
  312. root->style_name = cff_index_get_sid_string( &cff->string_index,
  313. dict->weight,
  314. psnames );
  315. /*******************************************************************/
  316. /* */
  317. /* Compute face flags. */
  318. /* */
  319. flags = FT_FACE_FLAG_SCALABLE | /* scalable outlines */
  320. FT_FACE_FLAG_HORIZONTAL; /* horizontal data */
  321. if ( sfnt_format )
  322. flags |= FT_FACE_FLAG_SFNT;
  323. /* fixed width font? */
  324. if ( dict->is_fixed_pitch )
  325. flags |= FT_FACE_FLAG_FIXED_WIDTH;
  326. /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
  327. #if 0
  328. /* kerning available? */
  329. if ( face->kern_pairs )
  330. flags |= FT_FACE_FLAG_KERNING;
  331. #endif
  332. #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
  333. flags |= FT_FACE_FLAG_GLYPH_NAMES;
  334. #endif
  335. root->face_flags = flags;
  336. /*******************************************************************/
  337. /* */
  338. /* Compute style flags. */
  339. /* */
  340. flags = 0;
  341. if ( dict->italic_angle )
  342. flags |= FT_STYLE_FLAG_ITALIC;
  343. /* XXX: may not be correct */
  344. if ( cff->top_font.private_dict.force_bold )
  345. flags |= FT_STYLE_FLAG_BOLD;
  346. root->style_flags = flags;
  347. }
  348. /*******************************************************************/
  349. /* */
  350. /* Compute char maps. */
  351. /* */
  352. /* Try to synthetize a Unicode charmap if there is none available */
  353. /* already. If an OpenType font contains a Unicode "cmap", we */
  354. /* will use it, whatever be in the CFF part of the file. */
  355. {
  356. FT_CharMapRec cmaprec;
  357. FT_CharMap cmap;
  358. FT_UInt nn;
  359. CFF_Encoding encoding = &cff->encoding;
  360. for ( nn = 0; nn < (FT_UInt) root->num_charmaps; nn++ )
  361. {
  362. cmap = root->charmaps[nn];
  363. /* Windows Unicode (3,1)? */
  364. if ( cmap->platform_id == 3 && cmap->encoding_id == 1 )
  365. goto Skip_Unicode;
  366. /* Deprecated Unicode platform id? */
  367. if ( cmap->platform_id == 0 )
  368. goto Skip_Unicode; /* Standard Unicode (deprecated) */
  369. }
  370. /* we didn't find a Unicode charmap, synthetize one */
  371. cmaprec.face = root;
  372. cmaprec.platform_id = 3;
  373. cmaprec.encoding_id = 1;
  374. cmaprec.encoding = FT_ENCODING_UNICODE;
  375. nn = (FT_UInt) root->num_charmaps;
  376. FT_CMap_New( &cff_cmap_unicode_class_rec, NULL, &cmaprec, NULL );
  377. /* if no Unicode charmap was previously selected, select this one */
  378. if ( root->charmap == NULL && nn != (FT_UInt) root->num_charmaps )
  379. root->charmap = root->charmaps[nn];
  380. Skip_Unicode:
  381. if ( encoding->count > 0 )
  382. {
  383. FT_CMap_Class clazz;
  384. cmaprec.face = root;
  385. cmaprec.platform_id = 7; /* Adobe platform id */
  386. if ( encoding->offset == 0 )
  387. {
  388. cmaprec.encoding_id = 0;
  389. cmaprec.encoding = FT_ENCODING_ADOBE_STANDARD;
  390. clazz = &cff_cmap_encoding_class_rec;
  391. }
  392. else if ( encoding->offset == 1 )
  393. {
  394. cmaprec.encoding_id = 1;
  395. cmaprec.encoding = FT_ENCODING_ADOBE_EXPERT;
  396. clazz = &cff_cmap_encoding_class_rec;
  397. }
  398. else
  399. {
  400. cmaprec.encoding_id = 3;
  401. cmaprec.encoding = FT_ENCODING_ADOBE_CUSTOM;
  402. clazz = &cff_cmap_encoding_class_rec;
  403. }
  404. FT_CMap_New( clazz, NULL, &cmaprec, NULL );
  405. }
  406. }
  407. }
  408. Exit:
  409. return error;
  410. Bad_Format:
  411. error = CFF_Err_Unknown_File_Format;
  412. goto Exit;
  413. }
  414. FT_LOCAL_DEF( void )
  415. cff_face_done( CFF_Face face )
  416. {
  417. FT_Memory memory = face->root.memory;
  418. SFNT_Service sfnt = (SFNT_Service)face->sfnt;
  419. if ( sfnt )
  420. sfnt->done_face( face );
  421. {
  422. CFF_Font cff = (CFF_Font)face->extra.data;
  423. if ( cff )
  424. {
  425. cff_font_done( cff );
  426. FT_FREE( face->extra.data );
  427. }
  428. }
  429. }
  430. FT_LOCAL_DEF( FT_Error )
  431. cff_driver_init( CFF_Driver driver )
  432. {
  433. FT_UNUSED( driver );
  434. return CFF_Err_Ok;
  435. }
  436. FT_LOCAL_DEF( void )
  437. cff_driver_done( CFF_Driver driver )
  438. {
  439. FT_UNUSED( driver );
  440. }
  441. /* END */