PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/compiler/android-ndk/jni/freetype/src/truetype/ttgload.c

http://ftk.googlecode.com/
C | 1996 lines | 1269 code | 439 blank | 288 comment | 202 complexity | 7ee15b458c8604b94b0d46fe4f5cdc39 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /***************************************************************************/
  2. /* */
  3. /* ttgload.c */
  4. /* */
  5. /* TrueType Glyph Loader (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
  8. /* 2010 by */
  9. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  10. /* */
  11. /* This file is part of the FreeType project, and may only be used, */
  12. /* modified, and distributed under the terms of the FreeType project */
  13. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  14. /* this file you indicate that you have read the license and */
  15. /* understand and accept it fully. */
  16. /* */
  17. /***************************************************************************/
  18. #include <ft2build.h>
  19. #include FT_INTERNAL_DEBUG_H
  20. #include FT_INTERNAL_CALC_H
  21. #include FT_INTERNAL_STREAM_H
  22. #include FT_INTERNAL_SFNT_H
  23. #include FT_TRUETYPE_TAGS_H
  24. #include FT_OUTLINE_H
  25. #include "ttgload.h"
  26. #include "ttpload.h"
  27. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  28. #include "ttgxvar.h"
  29. #endif
  30. #include "tterrors.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_ttgload
  39. /*************************************************************************/
  40. /* */
  41. /* Composite font flags. */
  42. /* */
  43. #define ARGS_ARE_WORDS 0x0001
  44. #define ARGS_ARE_XY_VALUES 0x0002
  45. #define ROUND_XY_TO_GRID 0x0004
  46. #define WE_HAVE_A_SCALE 0x0008
  47. /* reserved 0x0010 */
  48. #define MORE_COMPONENTS 0x0020
  49. #define WE_HAVE_AN_XY_SCALE 0x0040
  50. #define WE_HAVE_A_2X2 0x0080
  51. #define WE_HAVE_INSTR 0x0100
  52. #define USE_MY_METRICS 0x0200
  53. #define OVERLAP_COMPOUND 0x0400
  54. #define SCALED_COMPONENT_OFFSET 0x0800
  55. #define UNSCALED_COMPONENT_OFFSET 0x1000
  56. /*************************************************************************/
  57. /* */
  58. /* Returns the horizontal metrics in font units for a given glyph. If */
  59. /* `check' is true, take care of monospaced fonts by returning the */
  60. /* advance width maximum. */
  61. /* */
  62. FT_LOCAL_DEF( void )
  63. TT_Get_HMetrics( TT_Face face,
  64. FT_UInt idx,
  65. FT_Bool check,
  66. FT_Short* lsb,
  67. FT_UShort* aw )
  68. {
  69. ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
  70. if ( check && face->postscript.isFixedPitch )
  71. *aw = face->horizontal.advance_Width_Max;
  72. FT_TRACE5(( " advance width (font units): %d\n", *aw ));
  73. FT_TRACE5(( " left side bearing (font units): %d\n", *lsb ));
  74. }
  75. /*************************************************************************/
  76. /* */
  77. /* Returns the vertical metrics in font units for a given glyph. */
  78. /* Greg Hitchcock from Microsoft told us that if there were no `vmtx' */
  79. /* table, typoAscender/Descender from the `OS/2' table would be used */
  80. /* instead, and if there were no `OS/2' table, use ascender/descender */
  81. /* from the `hhea' table. But that is not what Microsoft's rasterizer */
  82. /* apparently does: It uses the ppem value as the advance height, and */
  83. /* sets the top side bearing to be zero. */
  84. /* */
  85. /* The monospace `check' is probably not meaningful here, but we leave */
  86. /* it in for a consistent interface. */
  87. /* */
  88. FT_LOCAL_DEF( void )
  89. TT_Get_VMetrics( TT_Face face,
  90. FT_UInt idx,
  91. FT_Bool check,
  92. FT_Short* tsb,
  93. FT_UShort* ah )
  94. {
  95. FT_UNUSED( check );
  96. if ( face->vertical_info )
  97. ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
  98. #if 1 /* Empirically determined, at variance with what MS said */
  99. else
  100. {
  101. *tsb = 0;
  102. *ah = face->root.units_per_EM;
  103. }
  104. #else /* This is what MS said to do. It isn't what they do, however. */
  105. else if ( face->os2.version != 0xFFFFU )
  106. {
  107. *tsb = face->os2.sTypoAscender;
  108. *ah = face->os2.sTypoAscender - face->os2.sTypoDescender;
  109. }
  110. else
  111. {
  112. *tsb = face->horizontal.Ascender;
  113. *ah = face->horizontal.Ascender - face->horizontal.Descender;
  114. }
  115. #endif
  116. FT_TRACE5(( " advance height (font units): %d\n", *ah ));
  117. FT_TRACE5(( " top side bearing (font units): %d\n", *tsb ));
  118. }
  119. static void
  120. tt_get_metrics( TT_Loader loader,
  121. FT_UInt glyph_index )
  122. {
  123. TT_Face face = (TT_Face)loader->face;
  124. FT_Short left_bearing = 0, top_bearing = 0;
  125. FT_UShort advance_width = 0, advance_height = 0;
  126. TT_Get_HMetrics( face, glyph_index,
  127. (FT_Bool)!( loader->load_flags &
  128. FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
  129. &left_bearing,
  130. &advance_width );
  131. TT_Get_VMetrics( face, glyph_index,
  132. (FT_Bool)!( loader->load_flags &
  133. FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ),
  134. &top_bearing,
  135. &advance_height );
  136. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  137. /* If this is an incrementally loaded font check whether there are */
  138. /* overriding metrics for this glyph. */
  139. if ( face->root.internal->incremental_interface &&
  140. face->root.internal->incremental_interface->funcs->get_glyph_metrics )
  141. {
  142. FT_Incremental_MetricsRec metrics;
  143. FT_Error error;
  144. metrics.bearing_x = left_bearing;
  145. metrics.bearing_y = 0;
  146. metrics.advance = advance_width;
  147. metrics.advance_v = 0;
  148. error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
  149. face->root.internal->incremental_interface->object,
  150. glyph_index, FALSE, &metrics );
  151. if ( error )
  152. goto Exit;
  153. left_bearing = (FT_Short)metrics.bearing_x;
  154. advance_width = (FT_UShort)metrics.advance;
  155. #if 0
  156. /* GWW: Do I do the same for vertical metrics? */
  157. metrics.bearing_x = 0;
  158. metrics.bearing_y = top_bearing;
  159. metrics.advance = advance_height;
  160. error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
  161. face->root.internal->incremental_interface->object,
  162. glyph_index, TRUE, &metrics );
  163. if ( error )
  164. goto Exit;
  165. top_bearing = (FT_Short)metrics.bearing_y;
  166. advance_height = (FT_UShort)metrics.advance;
  167. #endif /* 0 */
  168. }
  169. Exit:
  170. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  171. loader->left_bearing = left_bearing;
  172. loader->advance = advance_width;
  173. loader->top_bearing = top_bearing;
  174. loader->vadvance = advance_height;
  175. if ( !loader->linear_def )
  176. {
  177. loader->linear_def = 1;
  178. loader->linear = advance_width;
  179. }
  180. }
  181. /*************************************************************************/
  182. /* */
  183. /* Translates an array of coordinates. */
  184. /* */
  185. static void
  186. translate_array( FT_UInt n,
  187. FT_Vector* coords,
  188. FT_Pos delta_x,
  189. FT_Pos delta_y )
  190. {
  191. FT_UInt k;
  192. if ( delta_x )
  193. for ( k = 0; k < n; k++ )
  194. coords[k].x += delta_x;
  195. if ( delta_y )
  196. for ( k = 0; k < n; k++ )
  197. coords[k].y += delta_y;
  198. }
  199. #undef IS_HINTED
  200. #define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
  201. /*************************************************************************/
  202. /* */
  203. /* The following functions are used by default with TrueType fonts. */
  204. /* However, they can be replaced by alternatives if we need to support */
  205. /* TrueType-compressed formats (like MicroType) in the future. */
  206. /* */
  207. /*************************************************************************/
  208. FT_CALLBACK_DEF( FT_Error )
  209. TT_Access_Glyph_Frame( TT_Loader loader,
  210. FT_UInt glyph_index,
  211. FT_ULong offset,
  212. FT_UInt byte_count )
  213. {
  214. FT_Error error;
  215. FT_Stream stream = loader->stream;
  216. /* for non-debug mode */
  217. FT_UNUSED( glyph_index );
  218. FT_TRACE5(( "Glyph %ld\n", glyph_index ));
  219. /* the following line sets the `error' variable through macros! */
  220. if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
  221. return error;
  222. loader->cursor = stream->cursor;
  223. loader->limit = stream->limit;
  224. return TT_Err_Ok;
  225. }
  226. FT_CALLBACK_DEF( void )
  227. TT_Forget_Glyph_Frame( TT_Loader loader )
  228. {
  229. FT_Stream stream = loader->stream;
  230. FT_FRAME_EXIT();
  231. }
  232. FT_CALLBACK_DEF( FT_Error )
  233. TT_Load_Glyph_Header( TT_Loader loader )
  234. {
  235. FT_Byte* p = loader->cursor;
  236. FT_Byte* limit = loader->limit;
  237. if ( p + 10 > limit )
  238. return TT_Err_Invalid_Outline;
  239. loader->n_contours = FT_NEXT_SHORT( p );
  240. loader->bbox.xMin = FT_NEXT_SHORT( p );
  241. loader->bbox.yMin = FT_NEXT_SHORT( p );
  242. loader->bbox.xMax = FT_NEXT_SHORT( p );
  243. loader->bbox.yMax = FT_NEXT_SHORT( p );
  244. FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
  245. FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin,
  246. loader->bbox.xMax ));
  247. FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin,
  248. loader->bbox.yMax ));
  249. loader->cursor = p;
  250. return TT_Err_Ok;
  251. }
  252. FT_CALLBACK_DEF( FT_Error )
  253. TT_Load_Simple_Glyph( TT_Loader load )
  254. {
  255. FT_Error error;
  256. FT_Byte* p = load->cursor;
  257. FT_Byte* limit = load->limit;
  258. FT_GlyphLoader gloader = load->gloader;
  259. FT_Int n_contours = load->n_contours;
  260. FT_Outline* outline;
  261. TT_Face face = (TT_Face)load->face;
  262. FT_UShort n_ins;
  263. FT_Int n_points;
  264. FT_Byte *flag, *flag_limit;
  265. FT_Byte c, count;
  266. FT_Vector *vec, *vec_limit;
  267. FT_Pos x;
  268. FT_Short *cont, *cont_limit, prev_cont;
  269. FT_Int xy_size = 0;
  270. /* check that we can add the contours to the glyph */
  271. error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
  272. if ( error )
  273. goto Fail;
  274. /* reading the contours' endpoints & number of points */
  275. cont = gloader->current.outline.contours;
  276. cont_limit = cont + n_contours;
  277. /* check space for contours array + instructions count */
  278. if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
  279. goto Invalid_Outline;
  280. prev_cont = FT_NEXT_USHORT( p );
  281. if ( n_contours > 0 )
  282. cont[0] = prev_cont;
  283. for ( cont++; cont < cont_limit; cont++ )
  284. {
  285. cont[0] = FT_NEXT_USHORT( p );
  286. if ( cont[0] <= prev_cont )
  287. {
  288. /* unordered contours: this is invalid */
  289. error = FT_Err_Invalid_Table;
  290. goto Fail;
  291. }
  292. prev_cont = cont[0];
  293. }
  294. n_points = 0;
  295. if ( n_contours > 0 )
  296. {
  297. n_points = cont[-1] + 1;
  298. if ( n_points < 0 )
  299. goto Invalid_Outline;
  300. }
  301. /* note that we will add four phantom points later */
  302. error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
  303. if ( error )
  304. goto Fail;
  305. /* we'd better check the contours table right now */
  306. outline = &gloader->current.outline;
  307. for ( cont = outline->contours + 1; cont < cont_limit; cont++ )
  308. if ( cont[-1] >= cont[0] )
  309. goto Invalid_Outline;
  310. /* reading the bytecode instructions */
  311. load->glyph->control_len = 0;
  312. load->glyph->control_data = 0;
  313. if ( p + 2 > limit )
  314. goto Invalid_Outline;
  315. n_ins = FT_NEXT_USHORT( p );
  316. FT_TRACE5(( " Instructions size: %u\n", n_ins ));
  317. if ( n_ins > face->max_profile.maxSizeOfInstructions )
  318. {
  319. FT_TRACE0(( "TT_Load_Simple_Glyph: too many instructions (%d)\n",
  320. n_ins ));
  321. error = TT_Err_Too_Many_Hints;
  322. goto Fail;
  323. }
  324. if ( ( limit - p ) < n_ins )
  325. {
  326. FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
  327. error = TT_Err_Too_Many_Hints;
  328. goto Fail;
  329. }
  330. #ifdef TT_USE_BYTECODE_INTERPRETER
  331. if ( IS_HINTED( load->load_flags ) )
  332. {
  333. load->glyph->control_len = n_ins;
  334. load->glyph->control_data = load->exec->glyphIns;
  335. FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
  336. }
  337. #endif /* TT_USE_BYTECODE_INTERPRETER */
  338. p += n_ins;
  339. /* reading the point tags */
  340. flag = (FT_Byte*)outline->tags;
  341. flag_limit = flag + n_points;
  342. FT_ASSERT( flag != NULL );
  343. while ( flag < flag_limit )
  344. {
  345. if ( p + 1 > limit )
  346. goto Invalid_Outline;
  347. *flag++ = c = FT_NEXT_BYTE( p );
  348. if ( c & 8 )
  349. {
  350. if ( p + 1 > limit )
  351. goto Invalid_Outline;
  352. count = FT_NEXT_BYTE( p );
  353. if ( flag + (FT_Int)count > flag_limit )
  354. goto Invalid_Outline;
  355. for ( ; count > 0; count-- )
  356. *flag++ = c;
  357. }
  358. }
  359. /* reading the X coordinates */
  360. vec = outline->points;
  361. vec_limit = vec + n_points;
  362. flag = (FT_Byte*)outline->tags;
  363. x = 0;
  364. if ( p + xy_size > limit )
  365. goto Invalid_Outline;
  366. for ( ; vec < vec_limit; vec++, flag++ )
  367. {
  368. FT_Pos y = 0;
  369. FT_Byte f = *flag;
  370. if ( f & 2 )
  371. {
  372. if ( p + 1 > limit )
  373. goto Invalid_Outline;
  374. y = (FT_Pos)FT_NEXT_BYTE( p );
  375. if ( ( f & 16 ) == 0 )
  376. y = -y;
  377. }
  378. else if ( ( f & 16 ) == 0 )
  379. {
  380. if ( p + 2 > limit )
  381. goto Invalid_Outline;
  382. y = (FT_Pos)FT_NEXT_SHORT( p );
  383. }
  384. x += y;
  385. vec->x = x;
  386. /* the cast is for stupid compilers */
  387. *flag = (FT_Byte)( f & ~( 2 | 16 ) );
  388. }
  389. /* reading the Y coordinates */
  390. vec = gloader->current.outline.points;
  391. vec_limit = vec + n_points;
  392. flag = (FT_Byte*)outline->tags;
  393. x = 0;
  394. for ( ; vec < vec_limit; vec++, flag++ )
  395. {
  396. FT_Pos y = 0;
  397. FT_Byte f = *flag;
  398. if ( f & 4 )
  399. {
  400. if ( p + 1 > limit )
  401. goto Invalid_Outline;
  402. y = (FT_Pos)FT_NEXT_BYTE( p );
  403. if ( ( f & 32 ) == 0 )
  404. y = -y;
  405. }
  406. else if ( ( f & 32 ) == 0 )
  407. {
  408. if ( p + 2 > limit )
  409. goto Invalid_Outline;
  410. y = (FT_Pos)FT_NEXT_SHORT( p );
  411. }
  412. x += y;
  413. vec->y = x;
  414. /* the cast is for stupid compilers */
  415. *flag = (FT_Byte)( f & FT_CURVE_TAG_ON );
  416. }
  417. outline->n_points = (FT_UShort)n_points;
  418. outline->n_contours = (FT_Short) n_contours;
  419. load->cursor = p;
  420. Fail:
  421. return error;
  422. Invalid_Outline:
  423. error = TT_Err_Invalid_Outline;
  424. goto Fail;
  425. }
  426. FT_CALLBACK_DEF( FT_Error )
  427. TT_Load_Composite_Glyph( TT_Loader loader )
  428. {
  429. FT_Error error;
  430. FT_Byte* p = loader->cursor;
  431. FT_Byte* limit = loader->limit;
  432. FT_GlyphLoader gloader = loader->gloader;
  433. FT_SubGlyph subglyph;
  434. FT_UInt num_subglyphs;
  435. num_subglyphs = 0;
  436. do
  437. {
  438. FT_Fixed xx, xy, yy, yx;
  439. FT_UInt count;
  440. /* check that we can load a new subglyph */
  441. error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
  442. if ( error )
  443. goto Fail;
  444. /* check space */
  445. if ( p + 4 > limit )
  446. goto Invalid_Composite;
  447. subglyph = gloader->current.subglyphs + num_subglyphs;
  448. subglyph->arg1 = subglyph->arg2 = 0;
  449. subglyph->flags = FT_NEXT_USHORT( p );
  450. subglyph->index = FT_NEXT_USHORT( p );
  451. /* check space */
  452. count = 2;
  453. if ( subglyph->flags & ARGS_ARE_WORDS )
  454. count += 2;
  455. if ( subglyph->flags & WE_HAVE_A_SCALE )
  456. count += 2;
  457. else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
  458. count += 4;
  459. else if ( subglyph->flags & WE_HAVE_A_2X2 )
  460. count += 8;
  461. if ( p + count > limit )
  462. goto Invalid_Composite;
  463. /* read arguments */
  464. if ( subglyph->flags & ARGS_ARE_WORDS )
  465. {
  466. subglyph->arg1 = FT_NEXT_SHORT( p );
  467. subglyph->arg2 = FT_NEXT_SHORT( p );
  468. }
  469. else
  470. {
  471. subglyph->arg1 = FT_NEXT_CHAR( p );
  472. subglyph->arg2 = FT_NEXT_CHAR( p );
  473. }
  474. /* read transform */
  475. xx = yy = 0x10000L;
  476. xy = yx = 0;
  477. if ( subglyph->flags & WE_HAVE_A_SCALE )
  478. {
  479. xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  480. yy = xx;
  481. }
  482. else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
  483. {
  484. xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  485. yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  486. }
  487. else if ( subglyph->flags & WE_HAVE_A_2X2 )
  488. {
  489. xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  490. yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  491. xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  492. yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  493. }
  494. subglyph->transform.xx = xx;
  495. subglyph->transform.xy = xy;
  496. subglyph->transform.yx = yx;
  497. subglyph->transform.yy = yy;
  498. num_subglyphs++;
  499. } while ( subglyph->flags & MORE_COMPONENTS );
  500. gloader->current.num_subglyphs = num_subglyphs;
  501. #ifdef TT_USE_BYTECODE_INTERPRETER
  502. {
  503. FT_Stream stream = loader->stream;
  504. /* we must undo the FT_FRAME_ENTER in order to point */
  505. /* to the composite instructions, if we find some. */
  506. /* We will process them later. */
  507. /* */
  508. loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
  509. p - limit );
  510. }
  511. #endif
  512. loader->cursor = p;
  513. Fail:
  514. return error;
  515. Invalid_Composite:
  516. error = TT_Err_Invalid_Composite;
  517. goto Fail;
  518. }
  519. FT_LOCAL_DEF( void )
  520. TT_Init_Glyph_Loading( TT_Face face )
  521. {
  522. face->access_glyph_frame = TT_Access_Glyph_Frame;
  523. face->read_glyph_header = TT_Load_Glyph_Header;
  524. face->read_simple_glyph = TT_Load_Simple_Glyph;
  525. face->read_composite_glyph = TT_Load_Composite_Glyph;
  526. face->forget_glyph_frame = TT_Forget_Glyph_Frame;
  527. }
  528. static void
  529. tt_prepare_zone( TT_GlyphZone zone,
  530. FT_GlyphLoad load,
  531. FT_UInt start_point,
  532. FT_UInt start_contour )
  533. {
  534. zone->n_points = (FT_UShort)( load->outline.n_points - start_point );
  535. zone->n_contours = (FT_Short) ( load->outline.n_contours -
  536. start_contour );
  537. zone->org = load->extra_points + start_point;
  538. zone->cur = load->outline.points + start_point;
  539. zone->orus = load->extra_points2 + start_point;
  540. zone->tags = (FT_Byte*)load->outline.tags + start_point;
  541. zone->contours = (FT_UShort*)load->outline.contours + start_contour;
  542. zone->first_point = (FT_UShort)start_point;
  543. }
  544. /*************************************************************************/
  545. /* */
  546. /* <Function> */
  547. /* TT_Hint_Glyph */
  548. /* */
  549. /* <Description> */
  550. /* Hint the glyph using the zone prepared by the caller. Note that */
  551. /* the zone is supposed to include four phantom points. */
  552. /* */
  553. static FT_Error
  554. TT_Hint_Glyph( TT_Loader loader,
  555. FT_Bool is_composite )
  556. {
  557. TT_GlyphZone zone = &loader->zone;
  558. FT_Pos origin;
  559. #ifdef TT_USE_BYTECODE_INTERPRETER
  560. FT_UInt n_ins;
  561. #else
  562. FT_UNUSED( is_composite );
  563. #endif
  564. #ifdef TT_USE_BYTECODE_INTERPRETER
  565. if ( loader->glyph->control_len > 0xFFFFL )
  566. {
  567. FT_TRACE1(( "TT_Hint_Glyph: too long instructions " ));
  568. FT_TRACE1(( "(0x%lx byte) is truncated\n",
  569. loader->glyph->control_len ));
  570. }
  571. n_ins = (FT_UInt)( loader->glyph->control_len );
  572. #endif
  573. origin = zone->cur[zone->n_points - 4].x;
  574. origin = FT_PIX_ROUND( origin ) - origin;
  575. if ( origin )
  576. translate_array( zone->n_points, zone->cur, origin, 0 );
  577. #ifdef TT_USE_BYTECODE_INTERPRETER
  578. /* save original point position in org */
  579. if ( n_ins > 0 )
  580. FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
  581. /* Reset graphics state. */
  582. loader->exec->GS = ((TT_Size)loader->size)->GS;
  583. /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
  584. /* completely refer to the (already) hinted subglyphs. */
  585. if ( is_composite )
  586. {
  587. loader->exec->metrics.x_scale = 1 << 16;
  588. loader->exec->metrics.y_scale = 1 << 16;
  589. FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
  590. }
  591. else
  592. {
  593. loader->exec->metrics.x_scale =
  594. ((TT_Size)loader->size)->metrics.x_scale;
  595. loader->exec->metrics.y_scale =
  596. ((TT_Size)loader->size)->metrics.y_scale;
  597. }
  598. #endif
  599. /* round pp2 and pp4 */
  600. zone->cur[zone->n_points - 3].x =
  601. FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
  602. zone->cur[zone->n_points - 1].y =
  603. FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
  604. #ifdef TT_USE_BYTECODE_INTERPRETER
  605. if ( n_ins > 0 )
  606. {
  607. FT_Bool debug;
  608. FT_Error error;
  609. FT_GlyphLoader gloader = loader->gloader;
  610. FT_Outline current_outline = gloader->current.outline;
  611. error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
  612. loader->exec->glyphIns, n_ins );
  613. if ( error )
  614. return error;
  615. loader->exec->is_composite = is_composite;
  616. loader->exec->pts = *zone;
  617. debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) &&
  618. ((TT_Size)loader->size)->debug );
  619. error = TT_Run_Context( loader->exec, debug );
  620. if ( error && loader->exec->pedantic_hinting )
  621. return error;
  622. /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
  623. current_outline.tags[0] |=
  624. ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
  625. }
  626. #endif
  627. /* save glyph phantom points */
  628. if ( !loader->preserve_pps )
  629. {
  630. loader->pp1 = zone->cur[zone->n_points - 4];
  631. loader->pp2 = zone->cur[zone->n_points - 3];
  632. loader->pp3 = zone->cur[zone->n_points - 2];
  633. loader->pp4 = zone->cur[zone->n_points - 1];
  634. }
  635. return TT_Err_Ok;
  636. }
  637. /*************************************************************************/
  638. /* */
  639. /* <Function> */
  640. /* TT_Process_Simple_Glyph */
  641. /* */
  642. /* <Description> */
  643. /* Once a simple glyph has been loaded, it needs to be processed. */
  644. /* Usually, this means scaling and hinting through bytecode */
  645. /* interpretation. */
  646. /* */
  647. static FT_Error
  648. TT_Process_Simple_Glyph( TT_Loader loader )
  649. {
  650. FT_GlyphLoader gloader = loader->gloader;
  651. FT_Error error = TT_Err_Ok;
  652. FT_Outline* outline;
  653. FT_Int n_points;
  654. outline = &gloader->current.outline;
  655. n_points = outline->n_points;
  656. /* set phantom points */
  657. outline->points[n_points ] = loader->pp1;
  658. outline->points[n_points + 1] = loader->pp2;
  659. outline->points[n_points + 2] = loader->pp3;
  660. outline->points[n_points + 3] = loader->pp4;
  661. outline->tags[n_points ] = 0;
  662. outline->tags[n_points + 1] = 0;
  663. outline->tags[n_points + 2] = 0;
  664. outline->tags[n_points + 3] = 0;
  665. n_points += 4;
  666. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  667. if ( ((TT_Face)loader->face)->doblend )
  668. {
  669. /* Deltas apply to the unscaled data. */
  670. FT_Vector* deltas;
  671. FT_Memory memory = loader->face->memory;
  672. FT_Int i;
  673. error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
  674. loader->glyph_index,
  675. &deltas,
  676. n_points );
  677. if ( error )
  678. return error;
  679. for ( i = 0; i < n_points; ++i )
  680. {
  681. outline->points[i].x += deltas[i].x;
  682. outline->points[i].y += deltas[i].y;
  683. }
  684. FT_FREE( deltas );
  685. }
  686. #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
  687. if ( IS_HINTED( loader->load_flags ) )
  688. {
  689. tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
  690. FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
  691. loader->zone.n_points + 4 );
  692. }
  693. /* scale the glyph */
  694. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  695. {
  696. FT_Vector* vec = outline->points;
  697. FT_Vector* limit = outline->points + n_points;
  698. FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
  699. FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  700. for ( ; vec < limit; vec++ )
  701. {
  702. vec->x = FT_MulFix( vec->x, x_scale );
  703. vec->y = FT_MulFix( vec->y, y_scale );
  704. }
  705. loader->pp1 = outline->points[n_points - 4];
  706. loader->pp2 = outline->points[n_points - 3];
  707. loader->pp3 = outline->points[n_points - 2];
  708. loader->pp4 = outline->points[n_points - 1];
  709. }
  710. if ( IS_HINTED( loader->load_flags ) )
  711. {
  712. loader->zone.n_points += 4;
  713. error = TT_Hint_Glyph( loader, 0 );
  714. }
  715. return error;
  716. }
  717. /*************************************************************************/
  718. /* */
  719. /* <Function> */
  720. /* TT_Process_Composite_Component */
  721. /* */
  722. /* <Description> */
  723. /* Once a composite component has been loaded, it needs to be */
  724. /* processed. Usually, this means transforming and translating. */
  725. /* */
  726. static FT_Error
  727. TT_Process_Composite_Component( TT_Loader loader,
  728. FT_SubGlyph subglyph,
  729. FT_UInt start_point,
  730. FT_UInt num_base_points )
  731. {
  732. FT_GlyphLoader gloader = loader->gloader;
  733. FT_Vector* base_vec = gloader->base.outline.points;
  734. FT_UInt num_points = gloader->base.outline.n_points;
  735. FT_Bool have_scale;
  736. FT_Pos x, y;
  737. have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
  738. WE_HAVE_AN_XY_SCALE |
  739. WE_HAVE_A_2X2 ) );
  740. /* perform the transform required for this subglyph */
  741. if ( have_scale )
  742. {
  743. FT_UInt i;
  744. for ( i = num_base_points; i < num_points; i++ )
  745. FT_Vector_Transform( base_vec + i, &subglyph->transform );
  746. }
  747. /* get offset */
  748. if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
  749. {
  750. FT_UInt k = subglyph->arg1;
  751. FT_UInt l = subglyph->arg2;
  752. FT_Vector* p1;
  753. FT_Vector* p2;
  754. /* match l-th point of the newly loaded component to the k-th point */
  755. /* of the previously loaded components. */
  756. /* change to the point numbers used by our outline */
  757. k += start_point;
  758. l += num_base_points;
  759. if ( k >= num_base_points ||
  760. l >= num_points )
  761. return TT_Err_Invalid_Composite;
  762. p1 = gloader->base.outline.points + k;
  763. p2 = gloader->base.outline.points + l;
  764. x = p1->x - p2->x;
  765. y = p1->y - p2->y;
  766. }
  767. else
  768. {
  769. x = subglyph->arg1;
  770. y = subglyph->arg2;
  771. if ( !x && !y )
  772. return TT_Err_Ok;
  773. /* Use a default value dependent on */
  774. /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */
  775. /* fonts which don't set the xxx_COMPONENT_OFFSET bit. */
  776. if ( have_scale &&
  777. #ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
  778. !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
  779. #else
  780. ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
  781. #endif
  782. {
  783. #if 0
  784. /*************************************************************************/
  785. /* */
  786. /* This algorithm is what Apple documents. But it doesn't work. */
  787. /* */
  788. int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
  789. : -subglyph->transform.xx;
  790. int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
  791. : -subglyph->transform.yx;
  792. int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
  793. : -subglyph->transform.xy;
  794. int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
  795. : -subglyph->transform.yy;
  796. int m = a > b ? a : b;
  797. int n = c > d ? c : d;
  798. if ( a - b <= 33 && a - b >= -33 )
  799. m *= 2;
  800. if ( c - d <= 33 && c - d >= -33 )
  801. n *= 2;
  802. x = FT_MulFix( x, m );
  803. y = FT_MulFix( y, n );
  804. #else /* 0 */
  805. /*************************************************************************/
  806. /* */
  807. /* This algorithm is a guess and works much better than the above. */
  808. /* */
  809. FT_Fixed mac_xscale = FT_SqrtFixed(
  810. (FT_Int32)FT_MulFix( subglyph->transform.xx,
  811. subglyph->transform.xx ) +
  812. (FT_Int32)FT_MulFix( subglyph->transform.xy,
  813. subglyph->transform.xy ) );
  814. FT_Fixed mac_yscale = FT_SqrtFixed(
  815. (FT_Int32)FT_MulFix( subglyph->transform.yy,
  816. subglyph->transform.yy ) +
  817. (FT_Int32)FT_MulFix( subglyph->transform.yx,
  818. subglyph->transform.yx ) );
  819. x = FT_MulFix( x, mac_xscale );
  820. y = FT_MulFix( y, mac_yscale );
  821. #endif /* 0 */
  822. }
  823. if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
  824. {
  825. FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
  826. FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  827. x = FT_MulFix( x, x_scale );
  828. y = FT_MulFix( y, y_scale );
  829. if ( subglyph->flags & ROUND_XY_TO_GRID )
  830. {
  831. x = FT_PIX_ROUND( x );
  832. y = FT_PIX_ROUND( y );
  833. }
  834. }
  835. }
  836. if ( x || y )
  837. translate_array( num_points - num_base_points,
  838. base_vec + num_base_points,
  839. x, y );
  840. return TT_Err_Ok;
  841. }
  842. /*************************************************************************/
  843. /* */
  844. /* <Function> */
  845. /* TT_Process_Composite_Glyph */
  846. /* */
  847. /* <Description> */
  848. /* This is slightly different from TT_Process_Simple_Glyph, in that */
  849. /* its sole purpose is to hint the glyph. Thus this function is */
  850. /* only available when bytecode interpreter is enabled. */
  851. /* */
  852. static FT_Error
  853. TT_Process_Composite_Glyph( TT_Loader loader,
  854. FT_UInt start_point,
  855. FT_UInt start_contour )
  856. {
  857. FT_Error error;
  858. FT_Outline* outline;
  859. FT_UInt i;
  860. outline = &loader->gloader->base.outline;
  861. /* make room for phantom points */
  862. error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
  863. outline->n_points + 4,
  864. 0 );
  865. if ( error )
  866. return error;
  867. outline->points[outline->n_points ] = loader->pp1;
  868. outline->points[outline->n_points + 1] = loader->pp2;
  869. outline->points[outline->n_points + 2] = loader->pp3;
  870. outline->points[outline->n_points + 3] = loader->pp4;
  871. outline->tags[outline->n_points ] = 0;
  872. outline->tags[outline->n_points + 1] = 0;
  873. outline->tags[outline->n_points + 2] = 0;
  874. outline->tags[outline->n_points + 3] = 0;
  875. #ifdef TT_USE_BYTECODE_INTERPRETER
  876. {
  877. FT_Stream stream = loader->stream;
  878. FT_UShort n_ins;
  879. /* TT_Load_Composite_Glyph only gives us the offset of instructions */
  880. /* so we read them here */
  881. if ( FT_STREAM_SEEK( loader->ins_pos ) ||
  882. FT_READ_USHORT( n_ins ) )
  883. return error;
  884. FT_TRACE5(( " Instructions size = %d\n", n_ins ));
  885. /* check it */
  886. if ( n_ins > ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions )
  887. {
  888. FT_TRACE0(( "TT_Process_Composite_Glyph: too many instructions (%d)\n",
  889. n_ins ));
  890. return TT_Err_Too_Many_Hints;
  891. }
  892. else if ( n_ins == 0 )
  893. return TT_Err_Ok;
  894. if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
  895. return error;
  896. loader->glyph->control_data = loader->exec->glyphIns;
  897. loader->glyph->control_len = n_ins;
  898. }
  899. #endif
  900. tt_prepare_zone( &loader->zone, &loader->gloader->base,
  901. start_point, start_contour );
  902. /* Some points are likely touched during execution of */
  903. /* instructions on components. So let's untouch them. */
  904. for ( i = start_point; i < loader->zone.n_points; i++ )
  905. loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
  906. loader->zone.n_points += 4;
  907. return TT_Hint_Glyph( loader, 1 );
  908. }
  909. /* Calculate the four phantom points. */
  910. /* The first two stand for horizontal origin and advance. */
  911. /* The last two stand for vertical origin and advance. */
  912. #define TT_LOADER_SET_PP( loader ) \
  913. do { \
  914. (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
  915. (loader)->pp1.y = 0; \
  916. (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
  917. (loader)->pp2.y = 0; \
  918. (loader)->pp3.x = 0; \
  919. (loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax; \
  920. (loader)->pp4.x = 0; \
  921. (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
  922. } while ( 0 )
  923. /*************************************************************************/
  924. /* */
  925. /* <Function> */
  926. /* load_truetype_glyph */
  927. /* */
  928. /* <Description> */
  929. /* Loads a given truetype glyph. Handles composites and uses a */
  930. /* TT_Loader object. */
  931. /* */
  932. static FT_Error
  933. load_truetype_glyph( TT_Loader loader,
  934. FT_UInt glyph_index,
  935. FT_UInt recurse_count,
  936. FT_Bool header_only )
  937. {
  938. FT_Error error = TT_Err_Ok;
  939. FT_Fixed x_scale, y_scale;
  940. FT_ULong offset;
  941. TT_Face face = (TT_Face)loader->face;
  942. FT_GlyphLoader gloader = loader->gloader;
  943. FT_Bool opened_frame = 0;
  944. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  945. FT_Vector* deltas = NULL;
  946. #endif
  947. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  948. FT_StreamRec inc_stream;
  949. FT_Data glyph_data;
  950. FT_Bool glyph_data_loaded = 0;
  951. #endif
  952. /* some fonts have an incorrect value of `maxComponentDepth', */
  953. /* thus we allow depth 1 to catch the majority of them */
  954. if ( recurse_count > 1 &&
  955. recurse_count > face->max_profile.maxComponentDepth )
  956. {
  957. error = TT_Err_Invalid_Composite;
  958. goto Exit;
  959. }
  960. /* check glyph index */
  961. if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
  962. {
  963. error = TT_Err_Invalid_Glyph_Index;
  964. goto Exit;
  965. }
  966. loader->glyph_index = glyph_index;
  967. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  968. {
  969. x_scale = ((TT_Size)loader->size)->metrics.x_scale;
  970. y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  971. }
  972. else
  973. {
  974. x_scale = 0x10000L;
  975. y_scale = 0x10000L;
  976. }
  977. tt_get_metrics( loader, glyph_index );
  978. /* Set `offset' to the start of the glyph relative to the start of */
  979. /* the `glyf' table, and `byte_len' to the length of the glyph in */
  980. /* bytes. */
  981. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  982. /* If we are loading glyph data via the incremental interface, set */
  983. /* the loader stream to a memory stream reading the data returned */
  984. /* by the interface. */
  985. if ( face->root.internal->incremental_interface )
  986. {
  987. error = face->root.internal->incremental_interface->funcs->get_glyph_data(
  988. face->root.internal->incremental_interface->object,
  989. glyph_index, &glyph_data );
  990. if ( error )
  991. goto Exit;
  992. glyph_data_loaded = 1;
  993. offset = 0;
  994. loader->byte_len = glyph_data.length;
  995. FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
  996. FT_Stream_OpenMemory( &inc_stream,
  997. glyph_data.pointer, glyph_data.length );
  998. loader->stream = &inc_stream;
  999. }
  1000. else
  1001. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1002. offset = tt_face_get_location( face, glyph_index,
  1003. (FT_UInt*)&loader->byte_len );
  1004. if ( loader->byte_len > 0 )
  1005. {
  1006. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1007. /* for the incremental interface, `glyf_offset' is always zero */
  1008. if ( !loader->glyf_offset &&
  1009. !face->root.internal->incremental_interface )
  1010. #else
  1011. if ( !loader->glyf_offset )
  1012. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1013. {
  1014. FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
  1015. error = TT_Err_Invalid_Table;
  1016. goto Exit;
  1017. }
  1018. error = face->access_glyph_frame( loader, glyph_index,
  1019. loader->glyf_offset + offset,
  1020. loader->byte_len );
  1021. if ( error )
  1022. goto Exit;
  1023. opened_frame = 1;
  1024. /* read glyph header first */
  1025. error = face->read_glyph_header( loader );
  1026. if ( error || header_only )
  1027. goto Exit;
  1028. }
  1029. if ( loader->byte_len == 0 || loader->n_contours == 0 )
  1030. {
  1031. loader->bbox.xMin = 0;
  1032. loader->bbox.xMax = 0;
  1033. loader->bbox.yMin = 0;
  1034. loader->bbox.yMax = 0;
  1035. if ( header_only )
  1036. goto Exit;
  1037. TT_LOADER_SET_PP( loader );
  1038. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  1039. if ( ((TT_Face)(loader->face))->doblend )
  1040. {
  1041. /* this must be done before scaling */
  1042. FT_Memory memory = loader->face->memory;
  1043. error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face),
  1044. glyph_index, &deltas, 4 );
  1045. if ( error )
  1046. goto Exit;
  1047. loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y;
  1048. loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y;
  1049. loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y;
  1050. loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y;
  1051. FT_FREE( deltas );
  1052. }
  1053. #endif
  1054. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1055. {
  1056. loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
  1057. loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
  1058. loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
  1059. loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
  1060. }
  1061. error = TT_Err_Ok;
  1062. goto Exit;
  1063. }
  1064. TT_LOADER_SET_PP( loader );
  1065. /***********************************************************************/
  1066. /***********************************************************************/
  1067. /***********************************************************************/
  1068. /* if it is a simple glyph, load it */
  1069. if ( loader->n_contours > 0 )
  1070. {
  1071. error = face->read_simple_glyph( loader );
  1072. if ( error )
  1073. goto Exit;
  1074. /* all data have been read */
  1075. face->forget_glyph_frame( loader );
  1076. opened_frame = 0;
  1077. error = TT_Process_Simple_Glyph( loader );
  1078. if ( error )
  1079. goto Exit;
  1080. FT_GlyphLoader_Add( gloader );
  1081. }
  1082. /***********************************************************************/
  1083. /***********************************************************************/
  1084. /***********************************************************************/
  1085. /* otherwise, load a composite! */
  1086. else if ( loader->n_contours == -1 )
  1087. {
  1088. FT_UInt start_point;
  1089. FT_UInt start_contour;
  1090. FT_ULong ins_pos; /* position of composite instructions, if any */
  1091. start_point = gloader->base.outline.n_points;
  1092. start_contour = gloader->base.outline.n_contours;
  1093. /* for each subglyph, read composite header */
  1094. error = face->read_composite_glyph( loader );
  1095. if ( error )
  1096. goto Exit;
  1097. /* store the offset of instructions */
  1098. ins_pos = loader->ins_pos;
  1099. /* all data we need are read */
  1100. face->forget_glyph_frame( loader );
  1101. opened_frame = 0;
  1102. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  1103. if ( face->doblend )
  1104. {
  1105. FT_Int i, limit;
  1106. FT_SubGlyph subglyph;
  1107. FT_Memory memory = face->root.memory;
  1108. /* this provides additional offsets */
  1109. /* for each component's translation */
  1110. if ( ( error = TT_Vary_Get_Glyph_Deltas(
  1111. face,
  1112. glyph_index,
  1113. &deltas,
  1114. gloader->current.num_subglyphs + 4 )) != 0 )
  1115. goto Exit;
  1116. subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
  1117. limit = gloader->current.num_subglyphs;
  1118. for ( i = 0; i < limit; ++i, ++subglyph )
  1119. {
  1120. if ( subglyph->flags & ARGS_ARE_XY_VALUES )
  1121. {
  1122. /* XXX: overflow check for subglyph->{arg1,arg2}. */
  1123. /* deltas[i].{x,y} must be within signed 16-bit, */
  1124. /* but the restriction of summed delta is not clear */
  1125. subglyph->arg1 += (FT_Int16)deltas[i].x;
  1126. subglyph->arg2 += (FT_Int16)deltas[i].y;
  1127. }
  1128. }
  1129. loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y;
  1130. loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y;
  1131. loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y;
  1132. loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y;
  1133. FT_FREE( deltas );
  1134. }
  1135. #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
  1136. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1137. {
  1138. loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
  1139. loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
  1140. loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
  1141. loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
  1142. }
  1143. /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
  1144. /* `as is' in the glyph slot (the client application will be */
  1145. /* responsible for interpreting these data)... */
  1146. if ( loader->load_flags & FT_LOAD_NO_RECURSE )
  1147. {
  1148. FT_GlyphLoader_Add( gloader );
  1149. loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
  1150. goto Exit;
  1151. }
  1152. /*********************************************************************/
  1153. /*********************************************************************/
  1154. /*********************************************************************/
  1155. {
  1156. FT_UInt n, num_base_points;
  1157. FT_SubGlyph subglyph = 0;
  1158. FT_UInt num_points = start_point;
  1159. FT_UInt num_subglyphs = gloader->current.num_subglyphs;
  1160. FT_UInt num_base_subgs = gloader->base.num_subglyphs;
  1161. FT_Stream old_stream = loader->stream;
  1162. FT_GlyphLoader_Add( gloader );
  1163. /* read each subglyph independently */
  1164. for ( n = 0; n < num_subglyphs; n++ )
  1165. {
  1166. FT_Vector pp[4];
  1167. /* Each time we call load_truetype_glyph in this loop, the */
  1168. /* value of `gloader.base.subglyphs' can change due to table */
  1169. /* reallocations. We thus need to recompute the subglyph */
  1170. /* pointer on each iteration. */
  1171. subglyph = gloader->base.subglyphs + num_base_subgs + n;
  1172. pp[0] = loader->pp1;
  1173. pp[1] = loader->pp2;
  1174. pp[2] = loader->pp3;
  1175. pp[3] = loader->pp4;
  1176. num_base_points = gloader->base.outline.n_points;
  1177. error = load_truetype_glyph( loader, subglyph->index,
  1178. recurse_count + 1, FALSE );
  1179. if ( error )
  1180. goto Exit;
  1181. /* restore subglyph pointer */
  1182. subglyph = gloader->base.subglyphs + num_base_subgs + n;
  1183. if ( !( subglyph->flags & USE_MY_METRICS ) )
  1184. {
  1185. loader->pp1 = pp[0];
  1186. loader->pp2 = pp[1];
  1187. loader->pp3 = pp[2];
  1188. loader->pp4 = pp[3];
  1189. }
  1190. num_points = gloader->base.outline.n_points;
  1191. if ( num_points == num_base_points )
  1192. continue;
  1193. /* gloader->base.outline consists of three parts: */
  1194. /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
  1195. /* */
  1196. /* (1): exists from the beginning */
  1197. /* (2): components that have been loaded so far */
  1198. /* (3): the newly loaded component */
  1199. TT_Process_Composite_Component( loader, subglyph, start_point,
  1200. num_base_points );
  1201. }
  1202. loader->stream = old_stream;
  1203. /* process the glyph */
  1204. loader->ins_pos = ins_pos;
  1205. if ( IS_HINTED( loader->load_flags ) &&
  1206. #ifdef TT_USE_BYTECODE_INTERPRETER
  1207. subglyph->flags & WE_HAVE_INSTR &&
  1208. #endif
  1209. num_points > start_point )
  1210. TT_Process_Composite_Glyph( loader, start_point, start_contour );
  1211. }
  1212. }
  1213. else
  1214. {
  1215. /* invalid composite count (negative but not -1) */
  1216. error = TT_Err_Invalid_Outline;
  1217. goto Exit;
  1218. }
  1219. /***********************************************************************/
  1220. /***********************************************************************/
  1221. /***********************************************************************/
  1222. Exit:
  1223. if ( opened_frame )
  1224. face->forget_glyph_frame( loader );
  1225. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1226. if ( glyph_data_loaded )
  1227. face->root.internal->incremental_interface->funcs->free_glyph_data(
  1228. face->root.internal->incremental_interface->object,
  1229. &glyph_data );
  1230. #endif
  1231. return error;
  1232. }
  1233. static FT_Error
  1234. compute_glyph_metrics( TT_Loader loader,
  1235. FT_UInt glyph_index )
  1236. {
  1237. FT_BBox bbox;
  1238. TT_Face face = (TT_Face)loader->face;
  1239. FT_Fixed y_scale;
  1240. TT_GlyphSlot glyph = loader->glyph;
  1241. TT_Size size = (TT_Size)loader->size;
  1242. y_scale = 0x10000L;
  1243. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1244. y_scale = size->root.metrics.y_scale;
  1245. if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
  1246. FT_Outline_Get_CBox( &glyph->outline, &bbox );
  1247. else
  1248. bbox = loader->bbox;
  1249. /* get the device-independent horizontal advance; it is scaled later */
  1250. /* by the base layer. */
  1251. {
  1252. FT_Pos advance = loader->linear;
  1253. /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */
  1254. /* correctly support DynaLab fonts, which have an incorrect */
  1255. /* `advance_Width_Max' field! It is used, to my knowledge, */
  1256. /* exclusively in the X-TrueType font server. */
  1257. /* */
  1258. if ( face->postscript.isFixedPitch &&
  1259. ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 )
  1260. advance = face->horizontal.advance_Width_Max;
  1261. /* we need to return the advance in font units in linearHoriAdvance, */
  1262. /* it will be scaled later by the base layer. */
  1263. glyph->linearHoriAdvance = advance;
  1264. }
  1265. glyph->metrics.horiBearingX = bbox.xMin;
  1266. glyph->metrics.horiBearingY = bbox.yMax;
  1267. glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
  1268. /* adjust advance width to the value contained in the hdmx table */
  1269. if ( !face->postscript.isFixedPitch &&
  1270. IS_HINTED( loader->load_flags ) )
  1271. {
  1272. FT_Byte* widthp;
  1273. widthp = tt_face_get_device_metrics( face,
  1274. size->root.metrics.x_ppem,
  1275. glyph_index );
  1276. if ( widthp )
  1277. glyph->metrics.horiAdvance = *widthp << 6;
  1278. }
  1279. /* set glyph dimensions */
  1280. glyph->metrics.width = bbox.xMax - bbox.xMin;
  1281. glyph->metrics.height = bbox.yMax - bbox.yMin;
  1282. /* Now take care of vertical metrics. In the case where there is */
  1283. /* no vertical information within the font (relatively common), */
  1284. /* create some metrics manually */
  1285. {
  1286. FT_Pos top; /* scaled vertical top side bearing */
  1287. FT_Pos advance; /* scaled vertical advance height */
  1288. /* Get the unscaled top bearing and advance height. */
  1289. if ( face->vertical_info &&
  1290. face->vertical.number_Of_VMetrics > 0 )
  1291. {
  1292. top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
  1293. y_scale );
  1294. if ( loader->pp3.y <= loader->pp4.y )
  1295. advance = 0;
  1296. else
  1297. advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
  1298. y_scale );
  1299. }
  1300. else
  1301. {
  1302. FT_Pos height;
  1303. /* XXX Compute top side bearing and advance height in */
  1304. /* Get_VMetrics instead of here. */
  1305. /* NOTE: The OS/2 values are the only `portable' ones, */
  1306. /* which is why we use them, if there is an OS/2 */
  1307. /* table in the font. Otherwise, we use the */
  1308. /* values defined in the horizontal header. */
  1309. height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
  1310. y_scale );
  1311. if ( face->os2.version != 0xFFFFU )
  1312. advance = (FT_Pos)( face->os2.sTypoAscender -
  1313. face->os2.sTypoDescender );
  1314. else
  1315. advance = (FT_Pos)( face->horizontal.Ascender -
  1316. face->horizontal.Descender );
  1317. top = ( advance - height ) / 2;
  1318. }
  1319. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1320. {
  1321. FT_Incremental_InterfaceRec* incr;
  1322. FT_Incremental_MetricsRec metrics;
  1323. FT_Error error;
  1324. incr = face->root.internal->incremental_interface;
  1325. /* If this is an incrementally loaded font see if there are */
  1326. /* overriding metrics for this glyph. */
  1327. if ( incr && incr->funcs->get_glyph_metrics )
  1328. {
  1329. metrics.bearing_x = 0;
  1330. metrics.bearing_y = top;
  1331. metrics.advance = advance;
  1332. error = incr->funcs->get_glyph_metrics( incr->object,
  1333. glyph_index,
  1334. TRUE,
  1335. &metrics );
  1336. if ( error )
  1337. return error;
  1338. top = metrics.bearing_y;
  1339. advance = metrics.advance;
  1340. }
  1341. }
  1342. /* GWW: Do vertical metrics get loaded incrementally too? */
  1343. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1344. glyph->linearVertAdvance = advance;
  1345. /* scale the metrics */
  1346. if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
  1347. {
  1348. top = FT_MulFix( top, y_scale );
  1349. advance = FT_MulFix( advance, y_scale );
  1350. }
  1351. /* XXX: for now, we have no better algorithm for the lsb, but it */
  1352. /* should work fine. */
  1353. /* */
  1354. glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
  1355. glyph->metrics.horiAdvance / 2;
  1356. glyph->metrics.vertBearingY = top;
  1357. glyph->metrics.vertAdvance = advance;
  1358. }
  1359. return 0;
  1360. }
  1361. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  1362. static FT_Error
  1363. load_sbit_image( TT_Size size,
  1364. TT_GlyphSlot glyph,
  1365. FT_UInt glyph_index,
  1366. FT_Int32 load_flags )
  1367. {
  1368. TT_Face face;
  1369. SFNT_Service sfnt;
  1370. FT_Stream stream;
  1371. FT_Error error;
  1372. TT_SBit_MetricsRec metrics;
  1373. face = (TT_Face)glyph->face;
  1374. sfnt = (SFNT_Service)face->sfnt;
  1375. stream = face->root.stream;
  1376. error = sfnt->load_sbit_image( face,
  1377. size->strike_index,
  1378. glyph_index,
  1379. (FT_Int)load_flags,
  1380. stream,
  1381. &glyph->bitmap,
  1382. &metrics );
  1383. if ( !error )
  1384. {
  1385. glyph->outline.n_points = 0;
  1386. glyph->outline.n_contours = 0;
  1387. glyph->metrics.width = (FT_Pos)metrics.width << 6;
  1388. glyph->metrics.height = (FT_Pos)metrics.height << 6;
  1389. glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
  1390. glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
  1391. glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
  1392. glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
  1393. glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
  1394. glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
  1395. glyph->format = FT_GLYPH_FORMAT_BITMAP;
  1396. if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
  1397. {
  1398. glyph->bitmap_left = metrics.vertBearingX;
  1399. glyph->bitmap_top = metrics.vertBearingY;
  1400. }
  1401. else
  1402. {
  1403. glyph->bitmap_left = metrics.horiBearingX;
  1404. glyph->bitmap_top = metrics.horiBearingY;
  1405. }
  1406. }
  1407. return error;
  1408. }
  1409. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  1410. static FT_Error
  1411. tt_loader_init( TT_Loader loader,
  1412. TT_Size size,
  1413. TT_GlyphSlot glyph,
  1414. FT_Int32 load_flags,
  1415. FT_Bool glyf_table_only )
  1416. {
  1417. TT_Face face;
  1418. FT_Stream stream;
  1419. face = (TT_Face)glyph->face;
  1420. stream = face->root.stream;
  1421. FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
  1422. #ifdef TT_USE_BYTECODE_INTERPRETER
  1423. /* load execution context */
  1424. if ( IS_HINTED( load_flags ) && !glyf_table_only )
  1425. {
  1426. TT_ExecContext exec;
  1427. FT_Bool grayscale;
  1428. if ( !size->cvt_ready )
  1429. {
  1430. FT_Error error = tt_size_ready_bytecode( size );
  1431. if ( error )
  1432. return error;
  1433. }
  1434. /* query new execution context */
  1435. exec = size->debug ? size->context
  1436. : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context;
  1437. if ( !exec )
  1438. return TT_Err_Could_Not_Find_Context;
  1439. grayscale =
  1440. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != FT_RENDER_MODE_MONO );
  1441. TT_Load_Context( exec, face, size );
  1442. /* a change from mono to grayscale rendering (and vice versa) */
  1443. /* requires a re-execution of the CVT program */
  1444. if ( grayscale != exec->grayscale )
  1445. {
  1446. FT_UInt i;
  1447. exec->grayscale = grayscale;
  1448. for ( i = 0; i < size->cvt_size; i++ )
  1449. size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
  1450. tt_size_run_prep( size );
  1451. }
  1452. /* see whether the cvt program has disabled hinting */
  1453. if ( exec->GS.instruct_control & 1 )
  1454. load_flags |= FT_LOAD_NO_HINTING;
  1455. /* load default graphics state -- if needed */
  1456. if ( exec->GS.instruct_control & 2 )
  1457. exec->GS = tt_default_graphics_state;
  1458. exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
  1459. loader->exec = exec;
  1460. loader->instructions = exec->glyphIns;
  1461. }
  1462. #endif /* TT_USE_BYTECODE_INTERPRETER */
  1463. /* seek to the beginning of the glyph table -- for Type 42 fonts */
  1464. /* the table might be accessed from a Postscript stream or something */
  1465. /* else... */
  1466. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1467. if ( face->root.internal->incremental_interface )
  1468. loader->glyf_offset = 0;
  1469. else
  1470. #endif
  1471. {
  1472. FT_Error error = face->goto_table( face, TTAG_glyf, stream, 0 );
  1473. if ( error == TT_Err_Table_Missing )
  1474. loader->glyf_offset = 0;
  1475. else if ( error )
  1476. {
  1477. FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
  1478. return error;
  1479. }
  1480. else
  1481. loader->glyf_offset = FT_STREAM_POS();
  1482. }
  1483. /* get face's glyph loader */
  1484. if ( !glyf_table_only )
  1485. {
  1486. FT_GlyphLoader gloader = glyph->internal->loader;
  1487. FT_GlyphLoader_Rewind( gloader );
  1488. loader->gloader = gloader;
  1489. }
  1490. loader->load_flags = load_flags;
  1491. loader->face = (FT_Face)face;
  1492. loader->size = (FT_Size)size;
  1493. loader->glyph = (FT_GlyphSlot)glyph;
  1494. loader->stream = stream;
  1495. return TT_Err_Ok;
  1496. }
  1497. /*************************************************************************/
  1498. /* */
  1499. /* <Function> */
  1500. /* TT_Load_Glyph */
  1501. /* */
  1502. /* <Description> */
  1503. /* A function used to load a single glyph within a given glyph slot, */
  1504. /* for a given size. */
  1505. /* */
  1506. /* <Input> */
  1507. /* glyph :: A handle to a target slot object where the glyph */
  1508. /* will be loaded. */
  1509. /* */
  1510. /* size :: A handle to the source face size at which the glyph */
  1511. /* must be scaled/loaded. */
  1512. /* */
  1513. /* glyph_index :: The index of the glyph in the font file. */
  1514. /* */
  1515. /* load_flags :: A flag indicating what to load for this glyph. The */
  1516. /* FT_LOAD_XXX constants can be used to control the */
  1517. /* glyph loading process (e.g., whether the outline */
  1518. /* should be scaled, whether to load bitmaps or not, */
  1519. /* whether to hint the outline, etc). */
  1520. /* */
  1521. /* <Return> */
  1522. /* FreeType error code. 0 means success. */
  1523. /* */
  1524. FT_LOCAL_DEF( FT_Error )
  1525. TT_Load_Glyph( TT_Size size,
  1526. TT_GlyphSlot glyph,
  1527. FT_UInt glyph_index,
  1528. FT_Int32 load_flags )
  1529. {
  1530. TT_Face face;
  1531. FT_Error error;
  1532. TT_LoaderRec loader;
  1533. face = (TT_Face)glyph->face;
  1534. error = TT_Err_Ok;
  1535. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  1536. /* try to load embedded bitmap if any */
  1537. /* */
  1538. /* XXX: The convention should be emphasized in */
  1539. /* the documents because it can be confusing. */
  1540. if ( size->strike_index != 0xFFFFFFFFUL &&
  1541. ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
  1542. {
  1543. error = load_sbit_image( size, glyph, glyph_index, load_flags );
  1544. if ( !error )
  1545. {
  1546. FT_Face root = &face->root;
  1547. if ( FT_IS_SCALABLE( root ) )
  1548. {
  1549. /* for the bbox we need the header only */
  1550. (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
  1551. (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
  1552. glyph->linearHoriAdvance = loader.linear;
  1553. glyph->linearVertAdvance = loader.top_bearing + loader.bbox.yMax -
  1554. loader.vadvance;
  1555. if ( face->postscript.isFixedPitch &&
  1556. ( load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 )
  1557. glyph->