cidgload.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /***************************************************************************/
  2. /* */
  3. /* cidgload.c */
  4. /* */
  5. /* CID-keyed Type1 Glyph Loader (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 "cidload.h"
  19. #include "cidgload.h"
  20. #include FT_INTERNAL_DEBUG_H
  21. #include FT_INTERNAL_STREAM_H
  22. #include FT_OUTLINE_H
  23. #include "ciderrs.h"
  24. /*************************************************************************/
  25. /* */
  26. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  27. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  28. /* messages during execution. */
  29. /* */
  30. #undef FT_COMPONENT
  31. #define FT_COMPONENT trace_cidgload
  32. FT_CALLBACK_DEF( FT_Error )
  33. cid_load_glyph( T1_Decoder decoder,
  34. FT_UInt glyph_index )
  35. {
  36. CID_Face face = (CID_Face)decoder->builder.face;
  37. CID_FaceInfo cid = &face->cid;
  38. FT_Byte* p;
  39. FT_UInt fd_select;
  40. FT_Stream stream = face->root.stream;
  41. FT_Error error = 0;
  42. FT_Byte* charstring = 0;
  43. FT_Memory memory = face->root.memory;
  44. FT_UInt glyph_length = 0;
  45. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  46. /* For incremental fonts get the character data using */
  47. /* the callback function. */
  48. if ( face->root.internal->incremental_interface )
  49. {
  50. FT_Data glyph_data;
  51. error = face->root.internal->incremental_interface->funcs->get_glyph_data(
  52. face->root.internal->incremental_interface->object,
  53. glyph_index,
  54. &glyph_data );
  55. if ( error )
  56. goto Exit;
  57. p = (FT_Byte*)glyph_data.pointer;
  58. fd_select = (FT_UInt)cid_get_offset( &p, (FT_Byte)cid->fd_bytes );
  59. if ( glyph_data.length != 0 )
  60. {
  61. glyph_length = glyph_data.length - cid->fd_bytes;
  62. FT_ALLOC( charstring, glyph_length );
  63. if ( !error )
  64. ft_memcpy( charstring, glyph_data.pointer + cid->fd_bytes,
  65. glyph_length );
  66. }
  67. face->root.internal->incremental_interface->funcs->free_glyph_data(
  68. face->root.internal->incremental_interface->object,
  69. &glyph_data );
  70. if ( error )
  71. goto Exit;
  72. }
  73. else
  74. #endif
  75. /* For ordinary fonts read the CID font dictionary index */
  76. /* and charstring offset from the CIDMap. */
  77. {
  78. FT_UInt entry_len = cid->fd_bytes + cid->gd_bytes;
  79. FT_ULong off1;
  80. if ( FT_STREAM_SEEK( cid->data_offset + cid->cidmap_offset +
  81. glyph_index * entry_len ) ||
  82. FT_FRAME_ENTER( 2 * entry_len ) )
  83. goto Exit;
  84. p = (FT_Byte*)stream->cursor;
  85. fd_select = (FT_UInt) cid_get_offset( &p, (FT_Byte)cid->fd_bytes );
  86. off1 = (FT_ULong)cid_get_offset( &p, (FT_Byte)cid->gd_bytes );
  87. p += cid->fd_bytes;
  88. glyph_length = (FT_UInt) cid_get_offset(
  89. &p, (FT_Byte)cid->gd_bytes ) - off1;
  90. FT_FRAME_EXIT();
  91. if ( glyph_length == 0 )
  92. goto Exit;
  93. if ( FT_ALLOC( charstring, glyph_length ) )
  94. goto Exit;
  95. if ( FT_STREAM_READ_AT( cid->data_offset + off1,
  96. charstring, glyph_length ) )
  97. goto Exit;
  98. }
  99. /* Now set up the subrs array and parse the charstrings. */
  100. {
  101. CID_FaceDict dict;
  102. CID_Subrs cid_subrs = face->subrs + fd_select;
  103. FT_Int cs_offset;
  104. /* Set up subrs */
  105. decoder->num_subrs = cid_subrs->num_subrs;
  106. decoder->subrs = cid_subrs->code;
  107. decoder->subrs_len = 0;
  108. /* Set up font matrix */
  109. dict = cid->font_dicts + fd_select;
  110. decoder->font_matrix = dict->font_matrix;
  111. decoder->font_offset = dict->font_offset;
  112. decoder->lenIV = dict->private_dict.lenIV;
  113. /* Decode the charstring. */
  114. /* Adjustment for seed bytes. */
  115. cs_offset = ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
  116. /* Decrypt only if lenIV >= 0. */
  117. if ( decoder->lenIV >= 0 )
  118. cid_decrypt( charstring, glyph_length, 4330 );
  119. error = decoder->funcs.parse_charstrings( decoder,
  120. charstring + cs_offset,
  121. glyph_length - cs_offset );
  122. }
  123. FT_FREE( charstring );
  124. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  125. /* Incremental fonts can optionally override the metrics. */
  126. if ( !error &&
  127. face->root.internal->incremental_interface &&
  128. face->root.internal->incremental_interface->funcs->get_glyph_metrics )
  129. {
  130. FT_Bool found = FALSE;
  131. FT_Incremental_MetricsRec metrics;
  132. error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
  133. face->root.internal->incremental_interface->object,
  134. glyph_index, FALSE, &metrics, &found );
  135. if ( found )
  136. {
  137. decoder->builder.left_bearing.x = metrics.bearing_x;
  138. decoder->builder.left_bearing.y = metrics.bearing_y;
  139. decoder->builder.advance.x = metrics.advance;
  140. decoder->builder.advance.y = 0;
  141. }
  142. }
  143. #endif
  144. Exit:
  145. return error;
  146. }
  147. #if 0
  148. /*************************************************************************/
  149. /*************************************************************************/
  150. /*************************************************************************/
  151. /********** *********/
  152. /********** *********/
  153. /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
  154. /********** *********/
  155. /********** The following code is in charge of computing *********/
  156. /********** the maximum advance width of the font. It *********/
  157. /********** quickly processes each glyph charstring to *********/
  158. /********** extract the value from either a `sbw' or `seac' *********/
  159. /********** operator. *********/
  160. /********** *********/
  161. /*************************************************************************/
  162. /*************************************************************************/
  163. /*************************************************************************/
  164. FT_LOCAL_DEF( FT_Error )
  165. cid_face_compute_max_advance( CID_Face face,
  166. FT_Int* max_advance )
  167. {
  168. FT_Error error;
  169. T1_DecoderRec decoder;
  170. FT_Int glyph_index;
  171. PSAux_Service psaux = (PSAux_Service)face->psaux;
  172. *max_advance = 0;
  173. /* Initialize load decoder */
  174. error = psaux->t1_decoder_funcs->init( &decoder,
  175. (FT_Face)face,
  176. 0, /* size */
  177. 0, /* glyph slot */
  178. 0, /* glyph names! XXX */
  179. 0, /* blend == 0 */
  180. 0, /* hinting == 0 */
  181. cid_load_glyph );
  182. if ( error )
  183. return error;
  184. decoder.builder.metrics_only = 1;
  185. decoder.builder.load_points = 0;
  186. /* for each glyph, parse the glyph charstring and extract */
  187. /* the advance width */
  188. for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
  189. glyph_index++ )
  190. {
  191. /* now get load the unscaled outline */
  192. error = cid_load_glyph( &decoder, glyph_index );
  193. /* ignore the error if one occurred - skip to next glyph */
  194. }
  195. *max_advance = decoder.builder.advance.x;
  196. return CID_Err_Ok;
  197. }
  198. #endif /* 0 */
  199. /*************************************************************************/
  200. /*************************************************************************/
  201. /*************************************************************************/
  202. /********** *********/
  203. /********** *********/
  204. /********** UNHINTED GLYPH LOADER *********/
  205. /********** *********/
  206. /********** The following code is in charge of loading a *********/
  207. /********** single outline. It completely ignores hinting *********/
  208. /********** and is used when FT_LOAD_NO_HINTING is set. *********/
  209. /********** *********/
  210. /*************************************************************************/
  211. /*************************************************************************/
  212. /*************************************************************************/
  213. FT_LOCAL_DEF( FT_Error )
  214. cid_slot_load_glyph( CID_GlyphSlot glyph,
  215. CID_Size size,
  216. FT_Int glyph_index,
  217. FT_Int32 load_flags )
  218. {
  219. FT_Error error;
  220. T1_DecoderRec decoder;
  221. CID_Face face = (CID_Face)glyph->root.face;
  222. FT_Bool hinting;
  223. PSAux_Service psaux = (PSAux_Service)face->psaux;
  224. FT_Matrix font_matrix;
  225. FT_Vector font_offset;
  226. if ( load_flags & FT_LOAD_NO_RECURSE )
  227. load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
  228. glyph->x_scale = size->root.metrics.x_scale;
  229. glyph->y_scale = size->root.metrics.y_scale;
  230. glyph->root.outline.n_points = 0;
  231. glyph->root.outline.n_contours = 0;
  232. hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
  233. ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
  234. glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
  235. {
  236. error = psaux->t1_decoder_funcs->init( &decoder,
  237. (FT_Face)face,
  238. (FT_Size)size,
  239. (FT_GlyphSlot)glyph,
  240. 0, /* glyph names -- XXX */
  241. 0, /* blend == 0 */
  242. hinting,
  243. FT_LOAD_TARGET_MODE(load_flags),
  244. cid_load_glyph );
  245. /* set up the decoder */
  246. decoder.builder.no_recurse = FT_BOOL(
  247. ( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ) );
  248. error = cid_load_glyph( &decoder, glyph_index );
  249. font_matrix = decoder.font_matrix;
  250. font_offset = decoder.font_offset;
  251. /* save new glyph tables */
  252. psaux->t1_decoder_funcs->done( &decoder );
  253. }
  254. /* now, set the metrics -- this is rather simple, as */
  255. /* the left side bearing is the xMin, and the top side */
  256. /* bearing the yMax */
  257. if ( !error )
  258. {
  259. glyph->root.outline.flags &= FT_OUTLINE_OWNER;
  260. glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
  261. /* for composite glyphs, return only left side bearing and */
  262. /* advance width */
  263. if ( load_flags & FT_LOAD_NO_RECURSE )
  264. {
  265. FT_Slot_Internal internal = glyph->root.internal;
  266. glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
  267. glyph->root.metrics.horiAdvance = decoder.builder.advance.x;
  268. internal->glyph_matrix = font_matrix;
  269. internal->glyph_delta = font_offset;
  270. internal->glyph_transformed = 1;
  271. }
  272. else
  273. {
  274. FT_BBox cbox;
  275. FT_Glyph_Metrics* metrics = &glyph->root.metrics;
  276. /* copy the _unscaled_ advance width */
  277. metrics->horiAdvance = decoder.builder.advance.x;
  278. glyph->root.linearHoriAdvance = decoder.builder.advance.x;
  279. glyph->root.internal->glyph_transformed = 0;
  280. /* make up vertical metrics */
  281. metrics->vertBearingX = 0;
  282. metrics->vertBearingY = 0;
  283. metrics->vertAdvance = 0;
  284. glyph->root.linearVertAdvance = 0;
  285. glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
  286. if ( size && size->root.metrics.y_ppem < 24 )
  287. glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
  288. /* apply the font matrix */
  289. FT_Outline_Transform( &glyph->root.outline, &font_matrix );
  290. FT_Outline_Translate( &glyph->root.outline,
  291. font_offset.x,
  292. font_offset.y );
  293. if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
  294. {
  295. /* scale the outline and the metrics */
  296. FT_Int n;
  297. FT_Outline* cur = decoder.builder.base;
  298. FT_Vector* vec = cur->points;
  299. FT_Fixed x_scale = glyph->x_scale;
  300. FT_Fixed y_scale = glyph->y_scale;
  301. /* First of all, scale the points */
  302. if ( !hinting )
  303. for ( n = cur->n_points; n > 0; n--, vec++ )
  304. {
  305. vec->x = FT_MulFix( vec->x, x_scale );
  306. vec->y = FT_MulFix( vec->y, y_scale );
  307. }
  308. FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
  309. /* Then scale the metrics */
  310. metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
  311. metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
  312. metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
  313. metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
  314. if ( hinting )
  315. {
  316. metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64;
  317. metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64;
  318. metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64;
  319. metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64;
  320. }
  321. }
  322. /* compute the other metrics */
  323. FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
  324. /* grid fit the bounding box if necessary */
  325. if ( hinting )
  326. {
  327. cbox.xMin &= -64;
  328. cbox.yMin &= -64;
  329. cbox.xMax = ( cbox.xMax + 63 ) & -64;
  330. cbox.yMax = ( cbox.yMax + 63 ) & -64;
  331. }
  332. metrics->width = cbox.xMax - cbox.xMin;
  333. metrics->height = cbox.yMax - cbox.yMin;
  334. metrics->horiBearingX = cbox.xMin;
  335. metrics->horiBearingY = cbox.yMax;
  336. }
  337. }
  338. return error;
  339. }
  340. /* END */