t42objs.c 18 KB

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