/src/freetype/src/cid/cidgload.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 442 lines · 266 code · 102 blank · 74 comment · 33 complexity · dbe78215396e65a7e4601038fef225f9 MD5 · raw file

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