PageRenderTime 71ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/lib_freetype/freetype/src/truetype/ttgload.c

https://bitbucket.org/xixs/lua
C | 2589 lines | 1668 code | 513 blank | 408 comment | 262 complexity | c3773bb36b6e64cd54e85ec57d8c189c MD5 | raw file
Possible License(s): Zlib, BSD-3-Clause, CC0-1.0, GPL-3.0, GPL-2.0, CPL-1.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, LGPL-2.1, LGPL-3.0, 0BSD, Cube

Large files files are truncated, but you can click here to view the full file

  1. /***************************************************************************/
  2. /* */
  3. /* ttgload.c */
  4. /* */
  5. /* TrueType Glyph Loader (body). */
  6. /* */
  7. /* Copyright 1996-2015 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 FT_INTERNAL_DEBUG_H
  19. #include FT_INTERNAL_CALC_H
  20. #include FT_INTERNAL_STREAM_H
  21. #include FT_INTERNAL_SFNT_H
  22. #include FT_TRUETYPE_TAGS_H
  23. #include FT_OUTLINE_H
  24. #include FT_TRUETYPE_DRIVER_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. #include "ttsubpix.h"
  32. /*************************************************************************/
  33. /* */
  34. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  35. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  36. /* messages during execution. */
  37. /* */
  38. #undef FT_COMPONENT
  39. #define FT_COMPONENT trace_ttgload
  40. /*************************************************************************/
  41. /* */
  42. /* Composite glyph flags. */
  43. /* */
  44. #define ARGS_ARE_WORDS 0x0001
  45. #define ARGS_ARE_XY_VALUES 0x0002
  46. #define ROUND_XY_TO_GRID 0x0004
  47. #define WE_HAVE_A_SCALE 0x0008
  48. /* reserved 0x0010 */
  49. #define MORE_COMPONENTS 0x0020
  50. #define WE_HAVE_AN_XY_SCALE 0x0040
  51. #define WE_HAVE_A_2X2 0x0080
  52. #define WE_HAVE_INSTR 0x0100
  53. #define USE_MY_METRICS 0x0200
  54. #define OVERLAP_COMPOUND 0x0400
  55. #define SCALED_COMPONENT_OFFSET 0x0800
  56. #define UNSCALED_COMPONENT_OFFSET 0x1000
  57. /*************************************************************************/
  58. /* */
  59. /* Return the horizontal metrics in font units for a given glyph. */
  60. /* */
  61. FT_LOCAL_DEF( void )
  62. TT_Get_HMetrics( TT_Face face,
  63. FT_UInt idx,
  64. FT_Short* lsb,
  65. FT_UShort* aw )
  66. {
  67. ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw );
  68. FT_TRACE5(( " advance width (font units): %d\n", *aw ));
  69. FT_TRACE5(( " left side bearing (font units): %d\n", *lsb ));
  70. }
  71. /*************************************************************************/
  72. /* */
  73. /* Return the vertical metrics in font units for a given glyph. */
  74. /* See macro `TT_LOADER_SET_PP' below for explanations. */
  75. /* */
  76. FT_LOCAL_DEF( void )
  77. TT_Get_VMetrics( TT_Face face,
  78. FT_UInt idx,
  79. FT_Pos yMax,
  80. FT_Short* tsb,
  81. FT_UShort* ah )
  82. {
  83. if ( face->vertical_info )
  84. ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah );
  85. else if ( face->os2.version != 0xFFFFU )
  86. {
  87. *tsb = (FT_Short)( face->os2.sTypoAscender - yMax );
  88. *ah = (FT_UShort)FT_ABS( face->os2.sTypoAscender -
  89. face->os2.sTypoDescender );
  90. }
  91. else
  92. {
  93. *tsb = (FT_Short)( face->horizontal.Ascender - yMax );
  94. *ah = (FT_UShort)FT_ABS( face->horizontal.Ascender -
  95. face->horizontal.Descender );
  96. }
  97. FT_TRACE5(( " advance height (font units): %d\n", *ah ));
  98. FT_TRACE5(( " top side bearing (font units): %d\n", *tsb ));
  99. }
  100. static FT_Error
  101. tt_get_metrics( TT_Loader loader,
  102. FT_UInt glyph_index )
  103. {
  104. TT_Face face = (TT_Face)loader->face;
  105. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  106. TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
  107. #endif
  108. FT_Error error;
  109. FT_Stream stream = loader->stream;
  110. FT_Short left_bearing = 0, top_bearing = 0;
  111. FT_UShort advance_width = 0, advance_height = 0;
  112. /* we must preserve the stream position */
  113. /* (which gets altered by the metrics functions) */
  114. FT_ULong pos = FT_STREAM_POS();
  115. TT_Get_HMetrics( face, glyph_index,
  116. &left_bearing,
  117. &advance_width );
  118. TT_Get_VMetrics( face, glyph_index,
  119. loader->bbox.yMax,
  120. &top_bearing,
  121. &advance_height );
  122. if ( FT_STREAM_SEEK( pos ) )
  123. return error;
  124. loader->left_bearing = left_bearing;
  125. loader->advance = advance_width;
  126. loader->top_bearing = top_bearing;
  127. loader->vadvance = advance_height;
  128. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  129. if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 &&
  130. loader->exec )
  131. {
  132. loader->exec->sph_tweak_flags = 0;
  133. /* This may not be the right place for this, but it works... */
  134. /* Note that we have to unconditionally load the tweaks since */
  135. /* it is possible that glyphs individually switch ClearType's */
  136. /* backwards compatibility mode on and off. */
  137. sph_set_tweaks( loader, glyph_index );
  138. }
  139. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  140. if ( !loader->linear_def )
  141. {
  142. loader->linear_def = 1;
  143. loader->linear = advance_width;
  144. }
  145. return FT_Err_Ok;
  146. }
  147. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  148. static void
  149. tt_get_metrics_incr_overrides( TT_Loader loader,
  150. FT_UInt glyph_index )
  151. {
  152. TT_Face face = (TT_Face)loader->face;
  153. FT_Short left_bearing = 0, top_bearing = 0;
  154. FT_UShort advance_width = 0, advance_height = 0;
  155. /* If this is an incrementally loaded font check whether there are */
  156. /* overriding metrics for this glyph. */
  157. if ( face->root.internal->incremental_interface &&
  158. face->root.internal->incremental_interface->funcs->get_glyph_metrics )
  159. {
  160. FT_Incremental_MetricsRec metrics;
  161. FT_Error error;
  162. metrics.bearing_x = loader->left_bearing;
  163. metrics.bearing_y = 0;
  164. metrics.advance = loader->advance;
  165. metrics.advance_v = 0;
  166. error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
  167. face->root.internal->incremental_interface->object,
  168. glyph_index, FALSE, &metrics );
  169. if ( error )
  170. goto Exit;
  171. left_bearing = (FT_Short)metrics.bearing_x;
  172. advance_width = (FT_UShort)metrics.advance;
  173. #if 0
  174. /* GWW: Do I do the same for vertical metrics? */
  175. metrics.bearing_x = 0;
  176. metrics.bearing_y = loader->top_bearing;
  177. metrics.advance = loader->vadvance;
  178. error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
  179. face->root.internal->incremental_interface->object,
  180. glyph_index, TRUE, &metrics );
  181. if ( error )
  182. goto Exit;
  183. top_bearing = (FT_Short)metrics.bearing_y;
  184. advance_height = (FT_UShort)metrics.advance;
  185. #endif /* 0 */
  186. loader->left_bearing = left_bearing;
  187. loader->advance = advance_width;
  188. loader->top_bearing = top_bearing;
  189. loader->vadvance = advance_height;
  190. if ( !loader->linear_def )
  191. {
  192. loader->linear_def = 1;
  193. loader->linear = advance_width;
  194. }
  195. }
  196. Exit:
  197. return;
  198. }
  199. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  200. /*************************************************************************/
  201. /* */
  202. /* Translates an array of coordinates. */
  203. /* */
  204. static void
  205. translate_array( FT_UInt n,
  206. FT_Vector* coords,
  207. FT_Pos delta_x,
  208. FT_Pos delta_y )
  209. {
  210. FT_UInt k;
  211. if ( delta_x )
  212. for ( k = 0; k < n; k++ )
  213. coords[k].x += delta_x;
  214. if ( delta_y )
  215. for ( k = 0; k < n; k++ )
  216. coords[k].y += delta_y;
  217. }
  218. /*************************************************************************/
  219. /* */
  220. /* The following functions are used by default with TrueType fonts. */
  221. /* However, they can be replaced by alternatives if we need to support */
  222. /* TrueType-compressed formats (like MicroType) in the future. */
  223. /* */
  224. /*************************************************************************/
  225. FT_CALLBACK_DEF( FT_Error )
  226. TT_Access_Glyph_Frame( TT_Loader loader,
  227. FT_UInt glyph_index,
  228. FT_ULong offset,
  229. FT_UInt byte_count )
  230. {
  231. FT_Error error;
  232. FT_Stream stream = loader->stream;
  233. /* for non-debug mode */
  234. FT_UNUSED( glyph_index );
  235. FT_TRACE4(( "Glyph %ld\n", glyph_index ));
  236. /* the following line sets the `error' variable through macros! */
  237. if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) )
  238. return error;
  239. loader->cursor = stream->cursor;
  240. loader->limit = stream->limit;
  241. return FT_Err_Ok;
  242. }
  243. FT_CALLBACK_DEF( void )
  244. TT_Forget_Glyph_Frame( TT_Loader loader )
  245. {
  246. FT_Stream stream = loader->stream;
  247. FT_FRAME_EXIT();
  248. }
  249. FT_CALLBACK_DEF( FT_Error )
  250. TT_Load_Glyph_Header( TT_Loader loader )
  251. {
  252. FT_Byte* p = loader->cursor;
  253. FT_Byte* limit = loader->limit;
  254. if ( p + 10 > limit )
  255. return FT_THROW( Invalid_Outline );
  256. loader->n_contours = FT_NEXT_SHORT( p );
  257. loader->bbox.xMin = FT_NEXT_SHORT( p );
  258. loader->bbox.yMin = FT_NEXT_SHORT( p );
  259. loader->bbox.xMax = FT_NEXT_SHORT( p );
  260. loader->bbox.yMax = FT_NEXT_SHORT( p );
  261. FT_TRACE5(( " # of contours: %d\n", loader->n_contours ));
  262. FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin,
  263. loader->bbox.xMax ));
  264. FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin,
  265. loader->bbox.yMax ));
  266. loader->cursor = p;
  267. return FT_Err_Ok;
  268. }
  269. FT_CALLBACK_DEF( FT_Error )
  270. TT_Load_Simple_Glyph( TT_Loader load )
  271. {
  272. FT_Error error;
  273. FT_Byte* p = load->cursor;
  274. FT_Byte* limit = load->limit;
  275. FT_GlyphLoader gloader = load->gloader;
  276. FT_Int n_contours = load->n_contours;
  277. FT_Outline* outline;
  278. FT_UShort n_ins;
  279. FT_Int n_points;
  280. FT_ULong tmp;
  281. FT_Byte *flag, *flag_limit;
  282. FT_Byte c, count;
  283. FT_Vector *vec, *vec_limit;
  284. FT_Pos x;
  285. FT_Short *cont, *cont_limit, prev_cont;
  286. FT_Int xy_size = 0;
  287. /* check that we can add the contours to the glyph */
  288. error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours );
  289. if ( error )
  290. goto Fail;
  291. /* reading the contours' endpoints & number of points */
  292. cont = gloader->current.outline.contours;
  293. cont_limit = cont + n_contours;
  294. /* check space for contours array + instructions count */
  295. if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit )
  296. goto Invalid_Outline;
  297. prev_cont = FT_NEXT_SHORT( p );
  298. if ( n_contours > 0 )
  299. cont[0] = prev_cont;
  300. if ( prev_cont < 0 )
  301. goto Invalid_Outline;
  302. for ( cont++; cont < cont_limit; cont++ )
  303. {
  304. cont[0] = FT_NEXT_SHORT( p );
  305. if ( cont[0] <= prev_cont )
  306. {
  307. /* unordered contours: this is invalid */
  308. goto Invalid_Outline;
  309. }
  310. prev_cont = cont[0];
  311. }
  312. n_points = 0;
  313. if ( n_contours > 0 )
  314. {
  315. n_points = cont[-1] + 1;
  316. if ( n_points < 0 )
  317. goto Invalid_Outline;
  318. }
  319. /* note that we will add four phantom points later */
  320. error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
  321. if ( error )
  322. goto Fail;
  323. /* reading the bytecode instructions */
  324. load->glyph->control_len = 0;
  325. load->glyph->control_data = NULL;
  326. if ( p + 2 > limit )
  327. goto Invalid_Outline;
  328. n_ins = FT_NEXT_USHORT( p );
  329. FT_TRACE5(( " Instructions size: %u\n", n_ins ));
  330. /* check it */
  331. if ( ( limit - p ) < n_ins )
  332. {
  333. FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" ));
  334. error = FT_THROW( Too_Many_Hints );
  335. goto Fail;
  336. }
  337. #ifdef TT_USE_BYTECODE_INTERPRETER
  338. if ( IS_HINTED( load->load_flags ) )
  339. {
  340. /* we don't trust `maxSizeOfInstructions' in the `maxp' table */
  341. /* and thus update the bytecode array size by ourselves */
  342. tmp = load->exec->glyphSize;
  343. error = Update_Max( load->exec->memory,
  344. &tmp,
  345. sizeof ( FT_Byte ),
  346. (void*)&load->exec->glyphIns,
  347. n_ins );
  348. load->exec->glyphSize = (FT_UShort)tmp;
  349. if ( error )
  350. return error;
  351. load->glyph->control_len = n_ins;
  352. load->glyph->control_data = load->exec->glyphIns;
  353. FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins );
  354. }
  355. #endif /* TT_USE_BYTECODE_INTERPRETER */
  356. p += n_ins;
  357. outline = &gloader->current.outline;
  358. /* reading the point tags */
  359. flag = (FT_Byte*)outline->tags;
  360. flag_limit = flag + n_points;
  361. FT_ASSERT( flag != NULL );
  362. while ( flag < flag_limit )
  363. {
  364. if ( p + 1 > limit )
  365. goto Invalid_Outline;
  366. *flag++ = c = FT_NEXT_BYTE( p );
  367. if ( c & 8 )
  368. {
  369. if ( p + 1 > limit )
  370. goto Invalid_Outline;
  371. count = FT_NEXT_BYTE( p );
  372. if ( flag + (FT_Int)count > flag_limit )
  373. goto Invalid_Outline;
  374. for ( ; count > 0; count-- )
  375. *flag++ = c;
  376. }
  377. }
  378. /* reading the X coordinates */
  379. vec = outline->points;
  380. vec_limit = vec + n_points;
  381. flag = (FT_Byte*)outline->tags;
  382. x = 0;
  383. if ( p + xy_size > limit )
  384. goto Invalid_Outline;
  385. for ( ; vec < vec_limit; vec++, flag++ )
  386. {
  387. FT_Pos y = 0;
  388. FT_Byte f = *flag;
  389. if ( f & 2 )
  390. {
  391. if ( p + 1 > limit )
  392. goto Invalid_Outline;
  393. y = (FT_Pos)FT_NEXT_BYTE( p );
  394. if ( ( f & 16 ) == 0 )
  395. y = -y;
  396. }
  397. else if ( ( f & 16 ) == 0 )
  398. {
  399. if ( p + 2 > limit )
  400. goto Invalid_Outline;
  401. y = (FT_Pos)FT_NEXT_SHORT( p );
  402. }
  403. x += y;
  404. vec->x = x;
  405. /* the cast is for stupid compilers */
  406. *flag = (FT_Byte)( f & ~( 2 | 16 ) );
  407. }
  408. /* reading the Y coordinates */
  409. vec = gloader->current.outline.points;
  410. vec_limit = vec + n_points;
  411. flag = (FT_Byte*)outline->tags;
  412. x = 0;
  413. for ( ; vec < vec_limit; vec++, flag++ )
  414. {
  415. FT_Pos y = 0;
  416. FT_Byte f = *flag;
  417. if ( f & 4 )
  418. {
  419. if ( p + 1 > limit )
  420. goto Invalid_Outline;
  421. y = (FT_Pos)FT_NEXT_BYTE( p );
  422. if ( ( f & 32 ) == 0 )
  423. y = -y;
  424. }
  425. else if ( ( f & 32 ) == 0 )
  426. {
  427. if ( p + 2 > limit )
  428. goto Invalid_Outline;
  429. y = (FT_Pos)FT_NEXT_SHORT( p );
  430. }
  431. x += y;
  432. vec->y = x;
  433. /* the cast is for stupid compilers */
  434. *flag = (FT_Byte)( f & FT_CURVE_TAG_ON );
  435. }
  436. outline->n_points = (FT_Short)n_points;
  437. outline->n_contours = (FT_Short)n_contours;
  438. load->cursor = p;
  439. Fail:
  440. return error;
  441. Invalid_Outline:
  442. error = FT_THROW( Invalid_Outline );
  443. goto Fail;
  444. }
  445. FT_CALLBACK_DEF( FT_Error )
  446. TT_Load_Composite_Glyph( TT_Loader loader )
  447. {
  448. FT_Error error;
  449. FT_Byte* p = loader->cursor;
  450. FT_Byte* limit = loader->limit;
  451. FT_GlyphLoader gloader = loader->gloader;
  452. FT_SubGlyph subglyph;
  453. FT_UInt num_subglyphs;
  454. num_subglyphs = 0;
  455. do
  456. {
  457. FT_Fixed xx, xy, yy, yx;
  458. FT_UInt count;
  459. /* check that we can load a new subglyph */
  460. error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 );
  461. if ( error )
  462. goto Fail;
  463. /* check space */
  464. if ( p + 4 > limit )
  465. goto Invalid_Composite;
  466. subglyph = gloader->current.subglyphs + num_subglyphs;
  467. subglyph->arg1 = subglyph->arg2 = 0;
  468. subglyph->flags = FT_NEXT_USHORT( p );
  469. subglyph->index = FT_NEXT_USHORT( p );
  470. /* check space */
  471. count = 2;
  472. if ( subglyph->flags & ARGS_ARE_WORDS )
  473. count += 2;
  474. if ( subglyph->flags & WE_HAVE_A_SCALE )
  475. count += 2;
  476. else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
  477. count += 4;
  478. else if ( subglyph->flags & WE_HAVE_A_2X2 )
  479. count += 8;
  480. if ( p + count > limit )
  481. goto Invalid_Composite;
  482. /* read arguments */
  483. if ( subglyph->flags & ARGS_ARE_XY_VALUES )
  484. {
  485. if ( subglyph->flags & ARGS_ARE_WORDS )
  486. {
  487. subglyph->arg1 = FT_NEXT_SHORT( p );
  488. subglyph->arg2 = FT_NEXT_SHORT( p );
  489. }
  490. else
  491. {
  492. subglyph->arg1 = FT_NEXT_CHAR( p );
  493. subglyph->arg2 = FT_NEXT_CHAR( p );
  494. }
  495. }
  496. else
  497. {
  498. if ( subglyph->flags & ARGS_ARE_WORDS )
  499. {
  500. subglyph->arg1 = (FT_Int)FT_NEXT_USHORT( p );
  501. subglyph->arg2 = (FT_Int)FT_NEXT_USHORT( p );
  502. }
  503. else
  504. {
  505. subglyph->arg1 = (FT_Int)FT_NEXT_BYTE( p );
  506. subglyph->arg2 = (FT_Int)FT_NEXT_BYTE( p );
  507. }
  508. }
  509. /* read transform */
  510. xx = yy = 0x10000L;
  511. xy = yx = 0;
  512. if ( subglyph->flags & WE_HAVE_A_SCALE )
  513. {
  514. xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  515. yy = xx;
  516. }
  517. else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE )
  518. {
  519. xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  520. yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  521. }
  522. else if ( subglyph->flags & WE_HAVE_A_2X2 )
  523. {
  524. xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  525. yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  526. xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  527. yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2;
  528. }
  529. subglyph->transform.xx = xx;
  530. subglyph->transform.xy = xy;
  531. subglyph->transform.yx = yx;
  532. subglyph->transform.yy = yy;
  533. num_subglyphs++;
  534. } while ( subglyph->flags & MORE_COMPONENTS );
  535. gloader->current.num_subglyphs = num_subglyphs;
  536. #ifdef TT_USE_BYTECODE_INTERPRETER
  537. {
  538. FT_Stream stream = loader->stream;
  539. /* we must undo the FT_FRAME_ENTER in order to point */
  540. /* to the composite instructions, if we find some. */
  541. /* We will process them later. */
  542. /* */
  543. loader->ins_pos = (FT_ULong)( FT_STREAM_POS() +
  544. p - limit );
  545. }
  546. #endif
  547. loader->cursor = p;
  548. Fail:
  549. return error;
  550. Invalid_Composite:
  551. error = FT_THROW( Invalid_Composite );
  552. goto Fail;
  553. }
  554. FT_LOCAL_DEF( void )
  555. TT_Init_Glyph_Loading( TT_Face face )
  556. {
  557. face->access_glyph_frame = TT_Access_Glyph_Frame;
  558. face->read_glyph_header = TT_Load_Glyph_Header;
  559. face->read_simple_glyph = TT_Load_Simple_Glyph;
  560. face->read_composite_glyph = TT_Load_Composite_Glyph;
  561. face->forget_glyph_frame = TT_Forget_Glyph_Frame;
  562. }
  563. static void
  564. tt_prepare_zone( TT_GlyphZone zone,
  565. FT_GlyphLoad load,
  566. FT_UInt start_point,
  567. FT_UInt start_contour )
  568. {
  569. zone->n_points = (FT_UShort)load->outline.n_points -
  570. (FT_UShort)start_point;
  571. zone->n_contours = load->outline.n_contours -
  572. (FT_Short)start_contour;
  573. zone->org = load->extra_points + start_point;
  574. zone->cur = load->outline.points + start_point;
  575. zone->orus = load->extra_points2 + start_point;
  576. zone->tags = (FT_Byte*)load->outline.tags + start_point;
  577. zone->contours = (FT_UShort*)load->outline.contours + start_contour;
  578. zone->first_point = (FT_UShort)start_point;
  579. }
  580. /*************************************************************************/
  581. /* */
  582. /* <Function> */
  583. /* TT_Hint_Glyph */
  584. /* */
  585. /* <Description> */
  586. /* Hint the glyph using the zone prepared by the caller. Note that */
  587. /* the zone is supposed to include four phantom points. */
  588. /* */
  589. static FT_Error
  590. TT_Hint_Glyph( TT_Loader loader,
  591. FT_Bool is_composite )
  592. {
  593. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  594. TT_Face face = (TT_Face)loader->face;
  595. TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
  596. #endif
  597. TT_GlyphZone zone = &loader->zone;
  598. #ifdef TT_USE_BYTECODE_INTERPRETER
  599. FT_Long n_ins;
  600. #else
  601. FT_UNUSED( is_composite );
  602. #endif
  603. #ifdef TT_USE_BYTECODE_INTERPRETER
  604. if ( loader->glyph->control_len > 0xFFFFL )
  605. {
  606. FT_TRACE1(( "TT_Hint_Glyph: too long instructions" ));
  607. FT_TRACE1(( " (0x%lx byte) is truncated\n",
  608. loader->glyph->control_len ));
  609. }
  610. n_ins = loader->glyph->control_len;
  611. /* save original point position in org */
  612. if ( n_ins > 0 )
  613. FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points );
  614. /* Reset graphics state. */
  615. loader->exec->GS = ((TT_Size)loader->size)->GS;
  616. /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */
  617. /* completely refer to the (already) hinted subglyphs. */
  618. if ( is_composite )
  619. {
  620. loader->exec->metrics.x_scale = 1 << 16;
  621. loader->exec->metrics.y_scale = 1 << 16;
  622. FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points );
  623. }
  624. else
  625. {
  626. loader->exec->metrics.x_scale =
  627. ((TT_Size)loader->size)->metrics.x_scale;
  628. loader->exec->metrics.y_scale =
  629. ((TT_Size)loader->size)->metrics.y_scale;
  630. }
  631. #endif
  632. /* round phantom points */
  633. zone->cur[zone->n_points - 4].x =
  634. FT_PIX_ROUND( zone->cur[zone->n_points - 4].x );
  635. zone->cur[zone->n_points - 3].x =
  636. FT_PIX_ROUND( zone->cur[zone->n_points - 3].x );
  637. zone->cur[zone->n_points - 2].y =
  638. FT_PIX_ROUND( zone->cur[zone->n_points - 2].y );
  639. zone->cur[zone->n_points - 1].y =
  640. FT_PIX_ROUND( zone->cur[zone->n_points - 1].y );
  641. #ifdef TT_USE_BYTECODE_INTERPRETER
  642. if ( n_ins > 0 )
  643. {
  644. FT_Error error;
  645. FT_GlyphLoader gloader = loader->gloader;
  646. FT_Outline current_outline = gloader->current.outline;
  647. TT_Set_CodeRange( loader->exec, tt_coderange_glyph,
  648. loader->exec->glyphIns, n_ins );
  649. loader->exec->is_composite = is_composite;
  650. loader->exec->pts = *zone;
  651. error = TT_Run_Context( loader->exec );
  652. if ( error && loader->exec->pedantic_hinting )
  653. return error;
  654. /* store drop-out mode in bits 5-7; set bit 2 also as a marker */
  655. current_outline.tags[0] |=
  656. ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE;
  657. }
  658. #endif
  659. /* save glyph phantom points */
  660. loader->pp1 = zone->cur[zone->n_points - 4];
  661. loader->pp2 = zone->cur[zone->n_points - 3];
  662. loader->pp3 = zone->cur[zone->n_points - 2];
  663. loader->pp4 = zone->cur[zone->n_points - 1];
  664. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  665. if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
  666. {
  667. if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN )
  668. FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 );
  669. else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN )
  670. FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 );
  671. }
  672. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  673. return FT_Err_Ok;
  674. }
  675. /*************************************************************************/
  676. /* */
  677. /* <Function> */
  678. /* TT_Process_Simple_Glyph */
  679. /* */
  680. /* <Description> */
  681. /* Once a simple glyph has been loaded, it needs to be processed. */
  682. /* Usually, this means scaling and hinting through bytecode */
  683. /* interpretation. */
  684. /* */
  685. static FT_Error
  686. TT_Process_Simple_Glyph( TT_Loader loader )
  687. {
  688. FT_GlyphLoader gloader = loader->gloader;
  689. FT_Error error = FT_Err_Ok;
  690. FT_Outline* outline;
  691. FT_Int n_points;
  692. outline = &gloader->current.outline;
  693. n_points = outline->n_points;
  694. /* set phantom points */
  695. outline->points[n_points ] = loader->pp1;
  696. outline->points[n_points + 1] = loader->pp2;
  697. outline->points[n_points + 2] = loader->pp3;
  698. outline->points[n_points + 3] = loader->pp4;
  699. outline->tags[n_points ] = 0;
  700. outline->tags[n_points + 1] = 0;
  701. outline->tags[n_points + 2] = 0;
  702. outline->tags[n_points + 3] = 0;
  703. n_points += 4;
  704. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  705. if ( ((TT_Face)loader->face)->doblend )
  706. {
  707. /* Deltas apply to the unscaled data. */
  708. error = TT_Vary_Apply_Glyph_Deltas( (TT_Face)(loader->face),
  709. loader->glyph_index,
  710. outline,
  711. (FT_UInt)n_points );
  712. if ( error )
  713. return error;
  714. }
  715. #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
  716. if ( IS_HINTED( loader->load_flags ) )
  717. {
  718. tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 );
  719. FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur,
  720. loader->zone.n_points + 4 );
  721. }
  722. {
  723. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  724. TT_Face face = (TT_Face)loader->face;
  725. TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
  726. FT_String* family = face->root.family_name;
  727. FT_UInt ppem = loader->size->metrics.x_ppem;
  728. FT_String* style = face->root.style_name;
  729. FT_UInt x_scale_factor = 1000;
  730. #endif
  731. FT_Vector* vec = outline->points;
  732. FT_Vector* limit = outline->points + n_points;
  733. FT_Fixed x_scale = 0; /* pacify compiler */
  734. FT_Fixed y_scale = 0;
  735. FT_Bool do_scale = FALSE;
  736. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  737. if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
  738. {
  739. /* scale, but only if enabled and only if TT hinting is being used */
  740. if ( IS_HINTED( loader->load_flags ) )
  741. x_scale_factor = sph_test_tweak_x_scaling( face,
  742. family,
  743. ppem,
  744. style,
  745. loader->glyph_index );
  746. /* scale the glyph */
  747. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ||
  748. x_scale_factor != 1000 )
  749. {
  750. x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale,
  751. (FT_Long)x_scale_factor, 1000 );
  752. y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  753. /* compensate for any scaling by de/emboldening; */
  754. /* the amount was determined via experimentation */
  755. if ( x_scale_factor != 1000 && ppem > 11 )
  756. FT_Outline_EmboldenXY( outline,
  757. FT_MulFix( 1280 * ppem,
  758. 1000 - x_scale_factor ),
  759. 0 );
  760. do_scale = TRUE;
  761. }
  762. }
  763. else
  764. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  765. {
  766. /* scale the glyph */
  767. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  768. {
  769. x_scale = ((TT_Size)loader->size)->metrics.x_scale;
  770. y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  771. do_scale = TRUE;
  772. }
  773. }
  774. if ( do_scale )
  775. {
  776. for ( ; vec < limit; vec++ )
  777. {
  778. vec->x = FT_MulFix( vec->x, x_scale );
  779. vec->y = FT_MulFix( vec->y, y_scale );
  780. }
  781. loader->pp1 = outline->points[n_points - 4];
  782. loader->pp2 = outline->points[n_points - 3];
  783. loader->pp3 = outline->points[n_points - 2];
  784. loader->pp4 = outline->points[n_points - 1];
  785. }
  786. }
  787. if ( IS_HINTED( loader->load_flags ) )
  788. {
  789. loader->zone.n_points += 4;
  790. error = TT_Hint_Glyph( loader, 0 );
  791. }
  792. return error;
  793. }
  794. /*************************************************************************/
  795. /* */
  796. /* <Function> */
  797. /* TT_Process_Composite_Component */
  798. /* */
  799. /* <Description> */
  800. /* Once a composite component has been loaded, it needs to be */
  801. /* processed. Usually, this means transforming and translating. */
  802. /* */
  803. static FT_Error
  804. TT_Process_Composite_Component( TT_Loader loader,
  805. FT_SubGlyph subglyph,
  806. FT_UInt start_point,
  807. FT_UInt num_base_points )
  808. {
  809. FT_GlyphLoader gloader = loader->gloader;
  810. FT_Vector* base_vec = gloader->base.outline.points;
  811. FT_UInt num_points = (FT_UInt)gloader->base.outline.n_points;
  812. FT_Bool have_scale;
  813. FT_Pos x, y;
  814. have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE |
  815. WE_HAVE_AN_XY_SCALE |
  816. WE_HAVE_A_2X2 ) );
  817. /* perform the transform required for this subglyph */
  818. if ( have_scale )
  819. {
  820. FT_UInt i;
  821. for ( i = num_base_points; i < num_points; i++ )
  822. FT_Vector_Transform( base_vec + i, &subglyph->transform );
  823. }
  824. /* get offset */
  825. if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) )
  826. {
  827. FT_UInt k = (FT_UInt)subglyph->arg1;
  828. FT_UInt l = (FT_UInt)subglyph->arg2;
  829. FT_Vector* p1;
  830. FT_Vector* p2;
  831. /* match l-th point of the newly loaded component to the k-th point */
  832. /* of the previously loaded components. */
  833. /* change to the point numbers used by our outline */
  834. k += start_point;
  835. l += num_base_points;
  836. if ( k >= num_base_points ||
  837. l >= num_points )
  838. return FT_THROW( Invalid_Composite );
  839. p1 = gloader->base.outline.points + k;
  840. p2 = gloader->base.outline.points + l;
  841. x = p1->x - p2->x;
  842. y = p1->y - p2->y;
  843. }
  844. else
  845. {
  846. x = subglyph->arg1;
  847. y = subglyph->arg2;
  848. if ( !x && !y )
  849. return FT_Err_Ok;
  850. /* Use a default value dependent on */
  851. /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old */
  852. /* TT fonts which don't set the xxx_COMPONENT_OFFSET bit. */
  853. if ( have_scale &&
  854. #ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED
  855. !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) )
  856. #else
  857. ( subglyph->flags & SCALED_COMPONENT_OFFSET ) )
  858. #endif
  859. {
  860. #if 0
  861. /*******************************************************************/
  862. /* */
  863. /* This algorithm is what Apple documents. But it doesn't work. */
  864. /* */
  865. int a = subglyph->transform.xx > 0 ? subglyph->transform.xx
  866. : -subglyph->transform.xx;
  867. int b = subglyph->transform.yx > 0 ? subglyph->transform.yx
  868. : -subglyph->transform.yx;
  869. int c = subglyph->transform.xy > 0 ? subglyph->transform.xy
  870. : -subglyph->transform.xy;
  871. int d = subglyph->transform.yy > 0 ? subglyph->transform.yy
  872. : -subglyph->transform.yy;
  873. int m = a > b ? a : b;
  874. int n = c > d ? c : d;
  875. if ( a - b <= 33 && a - b >= -33 )
  876. m *= 2;
  877. if ( c - d <= 33 && c - d >= -33 )
  878. n *= 2;
  879. x = FT_MulFix( x, m );
  880. y = FT_MulFix( y, n );
  881. #else /* 1 */
  882. /*******************************************************************/
  883. /* */
  884. /* This algorithm is a guess and works much better than the above. */
  885. /* */
  886. FT_Fixed mac_xscale = FT_Hypot( subglyph->transform.xx,
  887. subglyph->transform.xy );
  888. FT_Fixed mac_yscale = FT_Hypot( subglyph->transform.yy,
  889. subglyph->transform.yx );
  890. x = FT_MulFix( x, mac_xscale );
  891. y = FT_MulFix( y, mac_yscale );
  892. #endif /* 1 */
  893. }
  894. if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
  895. {
  896. FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale;
  897. FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  898. x = FT_MulFix( x, x_scale );
  899. y = FT_MulFix( y, y_scale );
  900. if ( subglyph->flags & ROUND_XY_TO_GRID )
  901. {
  902. x = FT_PIX_ROUND( x );
  903. y = FT_PIX_ROUND( y );
  904. }
  905. }
  906. }
  907. if ( x || y )
  908. translate_array( num_points - num_base_points,
  909. base_vec + num_base_points,
  910. x, y );
  911. return FT_Err_Ok;
  912. }
  913. /*************************************************************************/
  914. /* */
  915. /* <Function> */
  916. /* TT_Process_Composite_Glyph */
  917. /* */
  918. /* <Description> */
  919. /* This is slightly different from TT_Process_Simple_Glyph, in that */
  920. /* its sole purpose is to hint the glyph. Thus this function is */
  921. /* only available when bytecode interpreter is enabled. */
  922. /* */
  923. static FT_Error
  924. TT_Process_Composite_Glyph( TT_Loader loader,
  925. FT_UInt start_point,
  926. FT_UInt start_contour )
  927. {
  928. FT_Error error;
  929. FT_Outline* outline;
  930. FT_UInt i;
  931. outline = &loader->gloader->base.outline;
  932. /* make room for phantom points */
  933. error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader,
  934. outline->n_points + 4,
  935. 0 );
  936. if ( error )
  937. return error;
  938. outline->points[outline->n_points ] = loader->pp1;
  939. outline->points[outline->n_points + 1] = loader->pp2;
  940. outline->points[outline->n_points + 2] = loader->pp3;
  941. outline->points[outline->n_points + 3] = loader->pp4;
  942. outline->tags[outline->n_points ] = 0;
  943. outline->tags[outline->n_points + 1] = 0;
  944. outline->tags[outline->n_points + 2] = 0;
  945. outline->tags[outline->n_points + 3] = 0;
  946. #ifdef TT_USE_BYTECODE_INTERPRETER
  947. {
  948. FT_Stream stream = loader->stream;
  949. FT_UShort n_ins, max_ins;
  950. FT_ULong tmp;
  951. /* TT_Load_Composite_Glyph only gives us the offset of instructions */
  952. /* so we read them here */
  953. if ( FT_STREAM_SEEK( loader->ins_pos ) ||
  954. FT_READ_USHORT( n_ins ) )
  955. return error;
  956. FT_TRACE5(( " Instructions size = %d\n", n_ins ));
  957. /* check it */
  958. max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions;
  959. if ( n_ins > max_ins )
  960. {
  961. /* don't trust `maxSizeOfInstructions'; */
  962. /* only do a rough safety check */
  963. if ( (FT_Int)n_ins > loader->byte_len )
  964. {
  965. FT_TRACE1(( "TT_Process_Composite_Glyph:"
  966. " too many instructions (%d) for glyph with length %d\n",
  967. n_ins, loader->byte_len ));
  968. return FT_THROW( Too_Many_Hints );
  969. }
  970. tmp = loader->exec->glyphSize;
  971. error = Update_Max( loader->exec->memory,
  972. &tmp,
  973. sizeof ( FT_Byte ),
  974. (void*)&loader->exec->glyphIns,
  975. n_ins );
  976. loader->exec->glyphSize = (FT_UShort)tmp;
  977. if ( error )
  978. return error;
  979. }
  980. else if ( n_ins == 0 )
  981. return FT_Err_Ok;
  982. if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) )
  983. return error;
  984. loader->glyph->control_data = loader->exec->glyphIns;
  985. loader->glyph->control_len = n_ins;
  986. }
  987. #endif
  988. tt_prepare_zone( &loader->zone, &loader->gloader->base,
  989. start_point, start_contour );
  990. /* Some points are likely touched during execution of */
  991. /* instructions on components. So let's untouch them. */
  992. for ( i = 0; i < loader->zone.n_points; i++ )
  993. loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH;
  994. loader->zone.n_points += 4;
  995. return TT_Hint_Glyph( loader, 1 );
  996. }
  997. /*
  998. * Calculate the phantom points
  999. *
  1000. * Defining the right side bearing (rsb) as
  1001. *
  1002. * rsb = aw - (lsb + xmax - xmin)
  1003. *
  1004. * (with `aw' the advance width, `lsb' the left side bearing, and `xmin'
  1005. * and `xmax' the glyph's minimum and maximum x value), the OpenType
  1006. * specification defines the initial position of horizontal phantom points
  1007. * as
  1008. *
  1009. * pp1 = (round(xmin - lsb), 0) ,
  1010. * pp2 = (round(pp1 + aw), 0) .
  1011. *
  1012. * Note that the rounding to the grid (in the device space) is not
  1013. * documented currently in the specification.
  1014. *
  1015. * However, the specification lacks the precise definition of vertical
  1016. * phantom points. Greg Hitchcock provided the following explanation.
  1017. *
  1018. * - a `vmtx' table is present
  1019. *
  1020. * For any glyph, the minimum and maximum y values (`ymin' and `ymax')
  1021. * are given in the `glyf' table, the top side bearing (tsb) and advance
  1022. * height (ah) are given in the `vmtx' table. The bottom side bearing
  1023. * (bsb) is then calculated as
  1024. *
  1025. * bsb = ah - (tsb + ymax - ymin) ,
  1026. *
  1027. * and the initial position of vertical phantom points is
  1028. *
  1029. * pp3 = (x, round(ymax + tsb)) ,
  1030. * pp4 = (x, round(pp3 - ah)) .
  1031. *
  1032. * See below for value `x'.
  1033. *
  1034. * - no `vmtx' table in the font
  1035. *
  1036. * If there is an `OS/2' table, we set
  1037. *
  1038. * DefaultAscender = sTypoAscender ,
  1039. * DefaultDescender = sTypoDescender ,
  1040. *
  1041. * otherwise we use data from the `hhea' table:
  1042. *
  1043. * DefaultAscender = Ascender ,
  1044. * DefaultDescender = Descender .
  1045. *
  1046. * With these two variables we can now set
  1047. *
  1048. * ah = DefaultAscender - sDefaultDescender ,
  1049. * tsb = DefaultAscender - yMax ,
  1050. *
  1051. * and proceed as if a `vmtx' table was present.
  1052. *
  1053. * Usually we have
  1054. *
  1055. * x = aw / 2 , (1)
  1056. *
  1057. * but there is one compatibility case where it can be set to
  1058. *
  1059. * x = -DefaultDescender -
  1060. * ((DefaultAscender - DefaultDescender - aw) / 2) . (2)
  1061. *
  1062. * and another one with
  1063. *
  1064. * x = 0 . (3)
  1065. *
  1066. * In Windows, the history of those values is quite complicated,
  1067. * depending on the hinting engine (that is, the graphics framework).
  1068. *
  1069. * framework from to formula
  1070. * ----------------------------------------------------------
  1071. * GDI Windows 98 current (1)
  1072. * (Windows 2000 for NT)
  1073. * GDI+ Windows XP Windows 7 (2)
  1074. * GDI+ Windows 8 current (3)
  1075. * DWrite Windows 7 current (3)
  1076. *
  1077. * For simplicity, FreeType uses (1) for grayscale subpixel hinting and
  1078. * (3) for everything else.
  1079. *
  1080. */
  1081. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1082. #define TT_LOADER_SET_PP( loader ) \
  1083. do \
  1084. { \
  1085. FT_Bool subpixel_hinting_ = loader->exec \
  1086. ? loader->exec->subpixel_hinting \
  1087. : 0; \
  1088. FT_Bool grayscale_ = loader->exec \
  1089. ? loader->exec->grayscale \
  1090. : 0; \
  1091. FT_Bool use_aw_2_ = (FT_Bool)( subpixel_hinting_ && \
  1092. grayscale_ ); \
  1093. \
  1094. \
  1095. (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
  1096. (loader)->pp1.y = 0; \
  1097. (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
  1098. (loader)->pp2.y = 0; \
  1099. \
  1100. (loader)->pp3.x = use_aw_2_ ? (loader)->advance / 2 : 0; \
  1101. (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \
  1102. (loader)->pp4.x = use_aw_2_ ? (loader)->advance / 2 : 0; \
  1103. (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
  1104. } while ( 0 )
  1105. #else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  1106. #define TT_LOADER_SET_PP( loader ) \
  1107. do \
  1108. { \
  1109. (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \
  1110. (loader)->pp1.y = 0; \
  1111. (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \
  1112. (loader)->pp2.y = 0; \
  1113. \
  1114. (loader)->pp3.x = 0; \
  1115. (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \
  1116. (loader)->pp4.x = 0; \
  1117. (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \
  1118. } while ( 0 )
  1119. #endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  1120. /*************************************************************************/
  1121. /* */
  1122. /* <Function> */
  1123. /* load_truetype_glyph */
  1124. /* */
  1125. /* <Description> */
  1126. /* Loads a given truetype glyph. Handles composites and uses a */
  1127. /* TT_Loader object. */
  1128. /* */
  1129. static FT_Error
  1130. load_truetype_glyph( TT_Loader loader,
  1131. FT_UInt glyph_index,
  1132. FT_UInt recurse_count,
  1133. FT_Bool header_only )
  1134. {
  1135. FT_Error error = FT_Err_Ok;
  1136. FT_Fixed x_scale, y_scale;
  1137. FT_ULong offset;
  1138. TT_Face face = (TT_Face)loader->face;
  1139. FT_GlyphLoader gloader = loader->gloader;
  1140. FT_Bool opened_frame = 0;
  1141. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1142. FT_StreamRec inc_stream;
  1143. FT_Data glyph_data;
  1144. FT_Bool glyph_data_loaded = 0;
  1145. #endif
  1146. /* some fonts have an incorrect value of `maxComponentDepth', */
  1147. /* thus we allow depth 1 to catch the majority of them */
  1148. if ( recurse_count > 1 &&
  1149. recurse_count > face->max_profile.maxComponentDepth )
  1150. {
  1151. error = FT_THROW( Invalid_Composite );
  1152. goto Exit;
  1153. }
  1154. #ifndef FT_CONFIG_OPTION_INCREMENTAL
  1155. /* check glyph index */
  1156. if ( glyph_index >= (FT_UInt)face->root.num_glyphs )
  1157. {
  1158. error = FT_THROW( Invalid_Glyph_Index );
  1159. goto Exit;
  1160. }
  1161. #endif
  1162. loader->glyph_index = glyph_index;
  1163. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1164. {
  1165. x_scale = ((TT_Size)loader->size)->metrics.x_scale;
  1166. y_scale = ((TT_Size)loader->size)->metrics.y_scale;
  1167. }
  1168. else
  1169. {
  1170. x_scale = 0x10000L;
  1171. y_scale = 0x10000L;
  1172. }
  1173. /* Set `offset' to the start of the glyph relative to the start of */
  1174. /* the `glyf' table, and `byte_len' to the length of the glyph in */
  1175. /* bytes. */
  1176. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1177. /* If we are loading glyph data via the incremental interface, set */
  1178. /* the loader stream to a memory stream reading the data returned */
  1179. /* by the interface. */
  1180. if ( face->root.internal->incremental_interface )
  1181. {
  1182. error = face->root.internal->incremental_interface->funcs->get_glyph_data(
  1183. face->root.internal->incremental_interface->object,
  1184. glyph_index, &glyph_data );
  1185. if ( error )
  1186. goto Exit;
  1187. glyph_data_loaded = 1;
  1188. offset = 0;
  1189. loader->byte_len = glyph_data.length;
  1190. FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) );
  1191. FT_Stream_OpenMemory( &inc_stream,
  1192. glyph_data.pointer,
  1193. (FT_ULong)glyph_data.length );
  1194. loader->stream = &inc_stream;
  1195. }
  1196. else
  1197. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1198. offset = tt_face_get_location( face, glyph_index,
  1199. (FT_UInt*)&loader->byte_len );
  1200. if ( loader->byte_len > 0 )
  1201. {
  1202. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1203. /* for the incremental interface, `glyf_offset' is always zero */
  1204. if ( !loader->glyf_offset &&
  1205. !face->root.internal->incremental_interface )
  1206. #else
  1207. if ( !loader->glyf_offset )
  1208. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1209. {
  1210. FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" ));
  1211. error = FT_THROW( Invalid_Table );
  1212. goto Exit;
  1213. }
  1214. error = face->access_glyph_frame( loader, glyph_index,
  1215. loader->glyf_offset + offset,
  1216. (FT_UInt)loader->

Large files files are truncated, but you can click here to view the full file