/src/freetype/src/type42/t42objs.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 679 lines · 455 code · 150 blank · 74 comment · 60 complexity · c33b8c06c3b490bc11831a738a30afe5 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* t42objs.c */
  4. /* */
  5. /* Type 42 objects manager (body). */
  6. /* */
  7. /* Copyright 2002-2009, 2011 */
  8. /* by Roberto Alameda. */
  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 "t42objs.h"
  18. #include "t42parse.h"
  19. #include "t42error.h"
  20. #include FT_INTERNAL_DEBUG_H
  21. #include FT_LIST_H
  22. #include FT_TRUETYPE_IDS_H
  23. #undef FT_COMPONENT
  24. #define FT_COMPONENT trace_t42
  25. static FT_Error
  26. T42_Open_Face( T42_Face face )
  27. {
  28. T42_LoaderRec loader;
  29. T42_Parser parser;
  30. T1_Font type1 = &face->type1;
  31. FT_Memory memory = face->root.memory;
  32. FT_Error error;
  33. PSAux_Service psaux = (PSAux_Service)face->psaux;
  34. t42_loader_init( &loader, face );
  35. parser = &loader.parser;
  36. if ( FT_ALLOC( face->ttf_data, 12 ) )
  37. goto Exit;
  38. error = t42_parser_init( parser,
  39. face->root.stream,
  40. memory,
  41. psaux);
  42. if ( error )
  43. goto Exit;
  44. error = t42_parse_dict( face, &loader,
  45. parser->base_dict, parser->base_len );
  46. if ( error )
  47. goto Exit;
  48. if ( type1->font_type != 42 )
  49. {
  50. FT_ERROR(( "T42_Open_Face: cannot handle FontType %d\n",
  51. type1->font_type ));
  52. error = T42_Err_Unknown_File_Format;
  53. goto Exit;
  54. }
  55. /* now, propagate the charstrings and glyphnames tables */
  56. /* to the Type1 data */
  57. type1->num_glyphs = loader.num_glyphs;
  58. if ( !loader.charstrings.init )
  59. {
  60. FT_ERROR(( "T42_Open_Face: no charstrings array in face\n" ));
  61. error = T42_Err_Invalid_File_Format;
  62. }
  63. loader.charstrings.init = 0;
  64. type1->charstrings_block = loader.charstrings.block;
  65. type1->charstrings = loader.charstrings.elements;
  66. type1->charstrings_len = loader.charstrings.lengths;
  67. /* we copy the glyph names `block' and `elements' fields; */
  68. /* the `lengths' field must be released later */
  69. type1->glyph_names_block = loader.glyph_names.block;
  70. type1->glyph_names = (FT_String**)loader.glyph_names.elements;
  71. loader.glyph_names.block = 0;
  72. loader.glyph_names.elements = 0;
  73. /* we must now build type1.encoding when we have a custom array */
  74. if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
  75. {
  76. FT_Int charcode, idx, min_char, max_char;
  77. FT_Byte* char_name;
  78. FT_Byte* glyph_name;
  79. /* OK, we do the following: for each element in the encoding */
  80. /* table, look up the index of the glyph having the same name */
  81. /* as defined in the CharStrings array. */
  82. /* The index is then stored in type1.encoding.char_index, and */
  83. /* the name in type1.encoding.char_name */
  84. min_char = 0;
  85. max_char = 0;
  86. charcode = 0;
  87. for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
  88. {
  89. type1->encoding.char_index[charcode] = 0;
  90. type1->encoding.char_name [charcode] = (char *)".notdef";
  91. char_name = loader.encoding_table.elements[charcode];
  92. if ( char_name )
  93. for ( idx = 0; idx < type1->num_glyphs; idx++ )
  94. {
  95. glyph_name = (FT_Byte*)type1->glyph_names[idx];
  96. if ( ft_strcmp( (const char*)char_name,
  97. (const char*)glyph_name ) == 0 )
  98. {
  99. type1->encoding.char_index[charcode] = (FT_UShort)idx;
  100. type1->encoding.char_name [charcode] = (char*)glyph_name;
  101. /* Change min/max encoded char only if glyph name is */
  102. /* not /.notdef */
  103. if ( ft_strcmp( (const char*)".notdef",
  104. (const char*)glyph_name ) != 0 )
  105. {
  106. if ( charcode < min_char )
  107. min_char = charcode;
  108. if ( charcode >= max_char )
  109. max_char = charcode + 1;
  110. }
  111. break;
  112. }
  113. }
  114. }
  115. type1->encoding.code_first = min_char;
  116. type1->encoding.code_last = max_char;
  117. type1->encoding.num_chars = loader.num_chars;
  118. }
  119. Exit:
  120. t42_loader_done( &loader );
  121. return error;
  122. }
  123. /***************** Driver Functions *************/
  124. FT_LOCAL_DEF( FT_Error )
  125. T42_Face_Init( FT_Stream stream,
  126. FT_Face t42face, /* T42_Face */
  127. FT_Int face_index,
  128. FT_Int num_params,
  129. FT_Parameter* params )
  130. {
  131. T42_Face face = (T42_Face)t42face;
  132. FT_Error error;
  133. FT_Service_PsCMaps psnames;
  134. PSAux_Service psaux;
  135. FT_Face root = (FT_Face)&face->root;
  136. T1_Font type1 = &face->type1;
  137. PS_FontInfo info = &type1->font_info;
  138. FT_UNUSED( num_params );
  139. FT_UNUSED( params );
  140. FT_UNUSED( face_index );
  141. FT_UNUSED( stream );
  142. face->ttf_face = NULL;
  143. face->root.num_faces = 1;
  144. FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
  145. face->psnames = psnames;
  146. face->psaux = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
  147. "psaux" );
  148. psaux = (PSAux_Service)face->psaux;
  149. if ( !psaux )
  150. {
  151. FT_ERROR(( "T42_Face_Init: cannot access `psaux' module\n" ));
  152. error = T42_Err_Missing_Module;
  153. goto Exit;
  154. }
  155. FT_TRACE2(( "Type 42 driver\n" ));
  156. /* open the tokenizer, this will also check the font format */
  157. error = T42_Open_Face( face );
  158. if ( error )
  159. goto Exit;
  160. /* if we just wanted to check the format, leave successfully now */
  161. if ( face_index < 0 )
  162. goto Exit;
  163. /* check the face index */
  164. if ( face_index > 0 )
  165. {
  166. FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
  167. error = T42_Err_Invalid_Argument;
  168. goto Exit;
  169. }
  170. /* Now load the font program into the face object */
  171. /* Init the face object fields */
  172. /* Now set up root face fields */
  173. root->num_glyphs = type1->num_glyphs;
  174. root->num_charmaps = 0;
  175. root->face_index = 0;
  176. root->face_flags = FT_FACE_FLAG_SCALABLE |
  177. FT_FACE_FLAG_HORIZONTAL |
  178. FT_FACE_FLAG_GLYPH_NAMES;
  179. if ( info->is_fixed_pitch )
  180. root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
  181. /* We only set this flag if we have the patented bytecode interpreter. */
  182. /* There are no known `tricky' Type42 fonts that could be loaded with */
  183. /* the unpatented interpreter. */
  184. #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
  185. root->face_flags |= FT_FACE_FLAG_HINTER;
  186. #endif
  187. /* XXX: TODO -- add kerning with .afm support */
  188. /* get style name -- be careful, some broken fonts only */
  189. /* have a `/FontName' dictionary entry! */
  190. root->family_name = info->family_name;
  191. /* assume "Regular" style if we don't know better */
  192. root->style_name = (char *)"Regular";
  193. if ( root->family_name )
  194. {
  195. char* full = info->full_name;
  196. char* family = root->family_name;
  197. if ( full )
  198. {
  199. while ( *full )
  200. {
  201. if ( *full == *family )
  202. {
  203. family++;
  204. full++;
  205. }
  206. else
  207. {
  208. if ( *full == ' ' || *full == '-' )
  209. full++;
  210. else if ( *family == ' ' || *family == '-' )
  211. family++;
  212. else
  213. {
  214. if ( !*family )
  215. root->style_name = full;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. else
  223. {
  224. /* do we have a `/FontName'? */
  225. if ( type1->font_name )
  226. root->family_name = type1->font_name;
  227. }
  228. /* no embedded bitmap support */
  229. root->num_fixed_sizes = 0;
  230. root->available_sizes = 0;
  231. /* Load the TTF font embedded in the T42 font */
  232. {
  233. FT_Open_Args args;
  234. args.flags = FT_OPEN_MEMORY;
  235. args.memory_base = face->ttf_data;
  236. args.memory_size = face->ttf_size;
  237. if ( num_params )
  238. {
  239. args.flags |= FT_OPEN_PARAMS;
  240. args.num_params = num_params;
  241. args.params = params;
  242. }
  243. error = FT_Open_Face( FT_FACE_LIBRARY( face ),
  244. &args, 0, &face->ttf_face );
  245. }
  246. if ( error )
  247. goto Exit;
  248. FT_Done_Size( face->ttf_face->size );
  249. /* Ignore info in FontInfo dictionary and use the info from the */
  250. /* loaded TTF font. The PostScript interpreter also ignores it. */
  251. root->bbox = face->ttf_face->bbox;
  252. root->units_per_EM = face->ttf_face->units_per_EM;
  253. root->ascender = face->ttf_face->ascender;
  254. root->descender = face->ttf_face->descender;
  255. root->height = face->ttf_face->height;
  256. root->max_advance_width = face->ttf_face->max_advance_width;
  257. root->max_advance_height = face->ttf_face->max_advance_height;
  258. root->underline_position = (FT_Short)info->underline_position;
  259. root->underline_thickness = (FT_Short)info->underline_thickness;
  260. /* compute style flags */
  261. root->style_flags = 0;
  262. if ( info->italic_angle )
  263. root->style_flags |= FT_STYLE_FLAG_ITALIC;
  264. if ( face->ttf_face->style_flags & FT_STYLE_FLAG_BOLD )
  265. root->style_flags |= FT_STYLE_FLAG_BOLD;
  266. if ( face->ttf_face->face_flags & FT_FACE_FLAG_VERTICAL )
  267. root->face_flags |= FT_FACE_FLAG_VERTICAL;
  268. {
  269. if ( psnames )
  270. {
  271. FT_CharMapRec charmap;
  272. T1_CMap_Classes cmap_classes = psaux->t1_cmap_classes;
  273. FT_CMap_Class clazz;
  274. charmap.face = root;
  275. /* first of all, try to synthesize a Unicode charmap */
  276. charmap.platform_id = TT_PLATFORM_MICROSOFT;
  277. charmap.encoding_id = TT_MS_ID_UNICODE_CS;
  278. charmap.encoding = FT_ENCODING_UNICODE;
  279. error = FT_CMap_New( cmap_classes->unicode, NULL, &charmap, NULL );
  280. if ( error && FT_Err_No_Unicode_Glyph_Name != error )
  281. goto Exit;
  282. error = FT_Err_Ok;
  283. /* now, generate an Adobe Standard encoding when appropriate */
  284. charmap.platform_id = TT_PLATFORM_ADOBE;
  285. clazz = NULL;
  286. switch ( type1->encoding_type )
  287. {
  288. case T1_ENCODING_TYPE_STANDARD:
  289. charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
  290. charmap.encoding_id = TT_ADOBE_ID_STANDARD;
  291. clazz = cmap_classes->standard;
  292. break;
  293. case T1_ENCODING_TYPE_EXPERT:
  294. charmap.encoding = FT_ENCODING_ADOBE_EXPERT;
  295. charmap.encoding_id = TT_ADOBE_ID_EXPERT;
  296. clazz = cmap_classes->expert;
  297. break;
  298. case T1_ENCODING_TYPE_ARRAY:
  299. charmap.encoding = FT_ENCODING_ADOBE_CUSTOM;
  300. charmap.encoding_id = TT_ADOBE_ID_CUSTOM;
  301. clazz = cmap_classes->custom;
  302. break;
  303. case T1_ENCODING_TYPE_ISOLATIN1:
  304. charmap.encoding = FT_ENCODING_ADOBE_LATIN_1;
  305. charmap.encoding_id = TT_ADOBE_ID_LATIN_1;
  306. clazz = cmap_classes->unicode;
  307. break;
  308. default:
  309. ;
  310. }
  311. if ( clazz )
  312. error = FT_CMap_New( clazz, NULL, &charmap, NULL );
  313. #if 0
  314. /* Select default charmap */
  315. if ( root->num_charmaps )
  316. root->charmap = root->charmaps[0];
  317. #endif
  318. }
  319. }
  320. Exit:
  321. return error;
  322. }
  323. FT_LOCAL_DEF( void )
  324. T42_Face_Done( FT_Face t42face )
  325. {
  326. T42_Face face = (T42_Face)t42face;
  327. T1_Font type1;
  328. PS_FontInfo info;
  329. FT_Memory memory;
  330. if ( !face )
  331. return;
  332. type1 = &face->type1;
  333. info = &type1->font_info;
  334. memory = face->root.memory;
  335. /* delete internal ttf face prior to freeing face->ttf_data */
  336. if ( face->ttf_face )
  337. FT_Done_Face( face->ttf_face );
  338. /* release font info strings */
  339. FT_FREE( info->version );
  340. FT_FREE( info->notice );
  341. FT_FREE( info->full_name );
  342. FT_FREE( info->family_name );
  343. FT_FREE( info->weight );
  344. /* release top dictionary */
  345. FT_FREE( type1->charstrings_len );
  346. FT_FREE( type1->charstrings );
  347. FT_FREE( type1->glyph_names );
  348. FT_FREE( type1->charstrings_block );
  349. FT_FREE( type1->glyph_names_block );
  350. FT_FREE( type1->encoding.char_index );
  351. FT_FREE( type1->encoding.char_name );
  352. FT_FREE( type1->font_name );
  353. FT_FREE( face->ttf_data );
  354. #if 0
  355. /* release afm data if present */
  356. if ( face->afm_data )
  357. T1_Done_AFM( memory, (T1_AFM*)face->afm_data );
  358. #endif
  359. /* release unicode map, if any */
  360. FT_FREE( face->unicode_map.maps );
  361. face->unicode_map.num_maps = 0;
  362. face->root.family_name = 0;
  363. face->root.style_name = 0;
  364. }
  365. /*************************************************************************/
  366. /* */
  367. /* <Function> */
  368. /* T42_Driver_Init */
  369. /* */
  370. /* <Description> */
  371. /* Initializes a given Type 42 driver object. */
  372. /* */
  373. /* <Input> */
  374. /* driver :: A handle to the target driver object. */
  375. /* */
  376. /* <Return> */
  377. /* FreeType error code. 0 means success. */
  378. /* */
  379. FT_LOCAL_DEF( FT_Error )
  380. T42_Driver_Init( FT_Module module ) /* T42_Driver */
  381. {
  382. T42_Driver driver = (T42_Driver)module;
  383. FT_Module ttmodule;
  384. ttmodule = FT_Get_Module( module->library, "truetype" );
  385. if ( !ttmodule )
  386. {
  387. FT_ERROR(( "T42_Driver_Init: cannot access `truetype' module\n" ));
  388. return T42_Err_Missing_Module;
  389. }
  390. driver->ttclazz = (FT_Driver_Class)ttmodule->clazz;
  391. return T42_Err_Ok;
  392. }
  393. FT_LOCAL_DEF( void )
  394. T42_Driver_Done( FT_Module module )
  395. {
  396. FT_UNUSED( module );
  397. }
  398. FT_LOCAL_DEF( FT_Error )
  399. T42_Size_Init( FT_Size size ) /* T42_Size */
  400. {
  401. T42_Size t42size = (T42_Size)size;
  402. FT_Face face = size->face;
  403. T42_Face t42face = (T42_Face)face;
  404. FT_Size ttsize;
  405. FT_Error error = T42_Err_Ok;
  406. error = FT_New_Size( t42face->ttf_face, &ttsize );
  407. t42size->ttsize = ttsize;
  408. FT_Activate_Size( ttsize );
  409. return error;
  410. }
  411. FT_LOCAL_DEF( FT_Error )
  412. T42_Size_Request( FT_Size t42size, /* T42_Size */
  413. FT_Size_Request req )
  414. {
  415. T42_Size size = (T42_Size)t42size;
  416. T42_Face face = (T42_Face)t42size->face;
  417. FT_Error error;
  418. FT_Activate_Size( size->ttsize );
  419. error = FT_Request_Size( face->ttf_face, req );
  420. if ( !error )
  421. t42size->metrics = face->ttf_face->size->metrics;
  422. return error;
  423. }
  424. FT_LOCAL_DEF( FT_Error )
  425. T42_Size_Select( FT_Size t42size, /* T42_Size */
  426. FT_ULong strike_index )
  427. {
  428. T42_Size size = (T42_Size)t42size;
  429. T42_Face face = (T42_Face)t42size->face;
  430. FT_Error error;
  431. FT_Activate_Size( size->ttsize );
  432. error = FT_Select_Size( face->ttf_face, (FT_Int)strike_index );
  433. if ( !error )
  434. t42size->metrics = face->ttf_face->size->metrics;
  435. return error;
  436. }
  437. FT_LOCAL_DEF( void )
  438. T42_Size_Done( FT_Size t42size ) /* T42_Size */
  439. {
  440. T42_Size size = (T42_Size)t42size;
  441. FT_Face face = t42size->face;
  442. T42_Face t42face = (T42_Face)face;
  443. FT_ListNode node;
  444. node = FT_List_Find( &t42face->ttf_face->sizes_list, size->ttsize );
  445. if ( node )
  446. {
  447. FT_Done_Size( size->ttsize );
  448. size->ttsize = NULL;
  449. }
  450. }
  451. FT_LOCAL_DEF( FT_Error )
  452. T42_GlyphSlot_Init( FT_GlyphSlot t42slot ) /* T42_GlyphSlot */
  453. {
  454. T42_GlyphSlot slot = (T42_GlyphSlot)t42slot;
  455. FT_Face face = t42slot->face;
  456. T42_Face t42face = (T42_Face)face;
  457. FT_GlyphSlot ttslot;
  458. FT_Error error = T42_Err_Ok;
  459. if ( face->glyph == NULL )
  460. {
  461. /* First glyph slot for this face */
  462. slot->ttslot = t42face->ttf_face->glyph;
  463. }
  464. else
  465. {
  466. error = FT_New_GlyphSlot( t42face->ttf_face, &ttslot );
  467. slot->ttslot = ttslot;
  468. }
  469. return error;
  470. }
  471. FT_LOCAL_DEF( void )
  472. T42_GlyphSlot_Done( FT_GlyphSlot t42slot ) /* T42_GlyphSlot */
  473. {
  474. T42_GlyphSlot slot = (T42_GlyphSlot)t42slot;
  475. FT_Done_GlyphSlot( slot->ttslot );
  476. }
  477. static void
  478. t42_glyphslot_clear( FT_GlyphSlot slot )
  479. {
  480. /* free bitmap if needed */
  481. ft_glyphslot_free_bitmap( slot );
  482. /* clear all public fields in the glyph slot */
  483. FT_ZERO( &slot->metrics );
  484. FT_ZERO( &slot->outline );
  485. FT_ZERO( &slot->bitmap );
  486. slot->bitmap_left = 0;
  487. slot->bitmap_top = 0;
  488. slot->num_subglyphs = 0;
  489. slot->subglyphs = 0;
  490. slot->control_data = 0;
  491. slot->control_len = 0;
  492. slot->other = 0;
  493. slot->format = FT_GLYPH_FORMAT_NONE;
  494. slot->linearHoriAdvance = 0;
  495. slot->linearVertAdvance = 0;
  496. }
  497. FT_LOCAL_DEF( FT_Error )
  498. T42_GlyphSlot_Load( FT_GlyphSlot glyph,
  499. FT_Size size,
  500. FT_UInt glyph_index,
  501. FT_Int32 load_flags )
  502. {
  503. FT_Error error;
  504. T42_GlyphSlot t42slot = (T42_GlyphSlot)glyph;
  505. T42_Size t42size = (T42_Size)size;
  506. FT_Driver_Class ttclazz = ((T42_Driver)glyph->face->driver)->ttclazz;
  507. t42_glyphslot_clear( t42slot->ttslot );
  508. error = ttclazz->load_glyph( t42slot->ttslot,
  509. t42size->ttsize,
  510. glyph_index,
  511. load_flags | FT_LOAD_NO_BITMAP );
  512. if ( !error )
  513. {
  514. glyph->metrics = t42slot->ttslot->metrics;
  515. glyph->linearHoriAdvance = t42slot->ttslot->linearHoriAdvance;
  516. glyph->linearVertAdvance = t42slot->ttslot->linearVertAdvance;
  517. glyph->format = t42slot->ttslot->format;
  518. glyph->outline = t42slot->ttslot->outline;
  519. glyph->bitmap = t42slot->ttslot->bitmap;
  520. glyph->bitmap_left = t42slot->ttslot->bitmap_left;
  521. glyph->bitmap_top = t42slot->ttslot->bitmap_top;
  522. glyph->num_subglyphs = t42slot->ttslot->num_subglyphs;
  523. glyph->subglyphs = t42slot->ttslot->subglyphs;
  524. glyph->control_data = t42slot->ttslot->control_data;
  525. glyph->control_len = t42slot->ttslot->control_len;
  526. }
  527. return error;
  528. }
  529. /* END */