PageRenderTime 76ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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->byte_len );
  1217. if ( error )
  1218. goto Exit;
  1219. opened_frame = 1;
  1220. /* read glyph header first */
  1221. error = face->read_glyph_header( loader );
  1222. if ( error )
  1223. goto Exit;
  1224. /* the metrics must be computed after loading the glyph header */
  1225. /* since we need the glyph's `yMax' value in case the vertical */
  1226. /* metrics must be emulated */
  1227. error = tt_get_metrics( loader, glyph_index );
  1228. if ( error )
  1229. goto Exit;
  1230. if ( header_only )
  1231. goto Exit;
  1232. }
  1233. if ( loader->byte_len == 0 || loader->n_contours == 0 )
  1234. {
  1235. loader->bbox.xMin = 0;
  1236. loader->bbox.xMax = 0;
  1237. loader->bbox.yMin = 0;
  1238. loader->bbox.yMax = 0;
  1239. error = tt_get_metrics( loader, glyph_index );
  1240. if ( error )
  1241. goto Exit;
  1242. if ( header_only )
  1243. goto Exit;
  1244. /* must initialize points before (possibly) overriding */
  1245. /* glyph metrics from the incremental interface */
  1246. TT_LOADER_SET_PP( loader );
  1247. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1248. tt_get_metrics_incr_overrides( loader, glyph_index );
  1249. #endif
  1250. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  1251. if ( ((TT_Face)(loader->face))->doblend )
  1252. {
  1253. /* a small outline structure with four elements for */
  1254. /* communication with `TT_Vary_Apply_Glyph_Deltas' */
  1255. FT_Vector points[4];
  1256. char tags[4] = { 1, 1, 1, 1 };
  1257. short contours[4] = { 0, 1, 2, 3 };
  1258. FT_Outline outline;
  1259. points[0].x = loader->pp1.x;
  1260. points[0].y = loader->pp1.y;
  1261. points[1].x = loader->pp2.x;
  1262. points[1].y = loader->pp2.y;
  1263. points[2].x = loader->pp3.x;
  1264. points[2].y = loader->pp3.y;
  1265. points[3].x = loader->pp4.x;
  1266. points[3].y = loader->pp4.y;
  1267. outline.n_points = 4;
  1268. outline.n_contours = 4;
  1269. outline.points = points;
  1270. outline.tags = tags;
  1271. outline.contours = contours;
  1272. /* this must be done before scaling */
  1273. error = TT_Vary_Apply_Glyph_Deltas( (TT_Face)(loader->face),
  1274. glyph_index,
  1275. &outline,
  1276. outline.n_points );
  1277. if ( error )
  1278. goto Exit;
  1279. loader->pp1.x = points[0].x;
  1280. loader->pp1.y = points[0].y;
  1281. loader->pp2.x = points[1].x;
  1282. loader->pp2.y = points[1].y;
  1283. loader->pp3.x = points[2].x;
  1284. loader->pp3.y = points[2].y;
  1285. loader->pp4.x = points[3].x;
  1286. loader->pp4.y = points[3].y;
  1287. }
  1288. #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
  1289. /* scale phantom points, if necessary; */
  1290. /* they get rounded in `TT_Hint_Glyph' */
  1291. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1292. {
  1293. loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
  1294. loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
  1295. /* pp1.y and pp2.y are always zero */
  1296. loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
  1297. loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
  1298. loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
  1299. loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
  1300. }
  1301. error = FT_Err_Ok;
  1302. goto Exit;
  1303. }
  1304. /* must initialize phantom points before (possibly) overriding */
  1305. /* glyph metrics from the incremental interface */
  1306. TT_LOADER_SET_PP( loader );
  1307. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1308. tt_get_metrics_incr_overrides( loader, glyph_index );
  1309. #endif
  1310. /***********************************************************************/
  1311. /***********************************************************************/
  1312. /***********************************************************************/
  1313. /* if it is a simple glyph, load it */
  1314. if ( loader->n_contours > 0 )
  1315. {
  1316. error = face->read_simple_glyph( loader );
  1317. if ( error )
  1318. goto Exit;
  1319. /* all data have been read */
  1320. face->forget_glyph_frame( loader );
  1321. opened_frame = 0;
  1322. error = TT_Process_Simple_Glyph( loader );
  1323. if ( error )
  1324. goto Exit;
  1325. FT_GlyphLoader_Add( gloader );
  1326. }
  1327. /***********************************************************************/
  1328. /***********************************************************************/
  1329. /***********************************************************************/
  1330. /* otherwise, load a composite! */
  1331. else if ( loader->n_contours == -1 )
  1332. {
  1333. FT_UInt start_point;
  1334. FT_UInt start_contour;
  1335. FT_ULong ins_pos; /* position of composite instructions, if any */
  1336. start_point = (FT_UInt)gloader->base.outline.n_points;
  1337. start_contour = (FT_UInt)gloader->base.outline.n_contours;
  1338. /* for each subglyph, read composite header */
  1339. error = face->read_composite_glyph( loader );
  1340. if ( error )
  1341. goto Exit;
  1342. /* store the offset of instructions */
  1343. ins_pos = loader->ins_pos;
  1344. /* all data we need are read */
  1345. face->forget_glyph_frame( loader );
  1346. opened_frame = 0;
  1347. #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
  1348. if ( face->doblend )
  1349. {
  1350. FT_UInt i, limit;
  1351. FT_SubGlyph subglyph;
  1352. FT_Outline outline;
  1353. FT_Vector* points = NULL;
  1354. char* tags = NULL;
  1355. short* contours = NULL;
  1356. FT_Memory memory = face->root.memory;
  1357. limit = gloader->current.num_subglyphs;
  1358. /* construct an outline structure for */
  1359. /* communication with `TT_Vary_Apply_Glyph_Deltas' */
  1360. outline.n_points = gloader->current.num_subglyphs + 4;
  1361. outline.n_contours = outline.n_points;
  1362. if ( FT_NEW_ARRAY( points, outline.n_points ) ||
  1363. FT_NEW_ARRAY( tags, outline.n_points ) ||
  1364. FT_NEW_ARRAY( contours, outline.n_points ) )
  1365. goto Exit1;
  1366. subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
  1367. for ( i = 0; i < limit; i++, subglyph++ )
  1368. {
  1369. /* applying deltas for anchor points doesn't make sense, */
  1370. /* but we don't have to specially check this since */
  1371. /* unused delta values are zero anyways */
  1372. points[i].x = subglyph->arg1;
  1373. points[i].y = subglyph->arg2;
  1374. tags[i] = 1;
  1375. contours[i] = i;
  1376. }
  1377. points[i].x = loader->pp1.x;
  1378. points[i].y = loader->pp1.y;
  1379. tags[i] = 1;
  1380. contours[i] = i;
  1381. i++;
  1382. points[i].x = loader->pp2.x;
  1383. points[i].y = loader->pp2.y;
  1384. tags[i] = 1;
  1385. contours[i] = i;
  1386. i++;
  1387. points[i].x = loader->pp3.x;
  1388. points[i].y = loader->pp3.y;
  1389. tags[i] = 1;
  1390. contours[i] = i;
  1391. i++;
  1392. points[i].x = loader->pp4.x;
  1393. points[i].y = loader->pp4.y;
  1394. tags[i] = 1;
  1395. contours[i] = i;
  1396. outline.points = points;
  1397. outline.tags = tags;
  1398. outline.contours = contours;
  1399. /* this call provides additional offsets */
  1400. /* for each component's translation */
  1401. if ( ( error = TT_Vary_Apply_Glyph_Deltas(
  1402. face,
  1403. glyph_index,
  1404. &outline,
  1405. outline.n_points ) ) != 0 )
  1406. goto Exit1;
  1407. subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs;
  1408. for ( i = 0; i < limit; i++, subglyph++ )
  1409. {
  1410. /* XXX: overflow check for subglyph->{arg1,arg2}. */
  1411. /* Deltas must be within signed 16-bit, */
  1412. /* but the restriction of summed deltas is not clear */
  1413. subglyph->arg1 = (FT_Int16)points[i].x;
  1414. subglyph->arg2 = (FT_Int16)points[i].y;
  1415. }
  1416. loader->pp1.x = points[i + 0].x;
  1417. loader->pp1.y = points[i + 0].y;
  1418. loader->pp2.x = points[i + 1].x;
  1419. loader->pp2.y = points[i + 1].y;
  1420. loader->pp3.x = points[i + 2].x;
  1421. loader->pp3.y = points[i + 2].y;
  1422. loader->pp4.x = points[i + 3].x;
  1423. loader->pp4.y = points[i + 3].y;
  1424. Exit1:
  1425. FT_FREE( outline.points );
  1426. FT_FREE( outline.tags );
  1427. FT_FREE( outline.contours );
  1428. if ( error )
  1429. goto Exit;
  1430. }
  1431. #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
  1432. /* scale phantom points, if necessary; */
  1433. /* they get rounded in `TT_Hint_Glyph' */
  1434. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1435. {
  1436. loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale );
  1437. loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale );
  1438. /* pp1.y and pp2.y are always zero */
  1439. loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale );
  1440. loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale );
  1441. loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale );
  1442. loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale );
  1443. }
  1444. /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */
  1445. /* `as is' in the glyph slot (the client application will be */
  1446. /* responsible for interpreting these data)... */
  1447. if ( loader->load_flags & FT_LOAD_NO_RECURSE )
  1448. {
  1449. FT_GlyphLoader_Add( gloader );
  1450. loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
  1451. goto Exit;
  1452. }
  1453. /*********************************************************************/
  1454. /*********************************************************************/
  1455. /*********************************************************************/
  1456. {
  1457. FT_UInt n, num_base_points;
  1458. FT_SubGlyph subglyph = NULL;
  1459. FT_UInt num_points = start_point;
  1460. FT_UInt num_subglyphs = gloader->current.num_subglyphs;
  1461. FT_UInt num_base_subgs = gloader->base.num_subglyphs;
  1462. FT_Stream old_stream = loader->stream;
  1463. FT_Int old_byte_len = loader->byte_len;
  1464. FT_GlyphLoader_Add( gloader );
  1465. /* read each subglyph independently */
  1466. for ( n = 0; n < num_subglyphs; n++ )
  1467. {
  1468. FT_Vector pp[4];
  1469. /* Each time we call load_truetype_glyph in this loop, the */
  1470. /* value of `gloader.base.subglyphs' can change due to table */
  1471. /* reallocations. We thus need to recompute the subglyph */
  1472. /* pointer on each iteration. */
  1473. subglyph = gloader->base.subglyphs + num_base_subgs + n;
  1474. pp[0] = loader->pp1;
  1475. pp[1] = loader->pp2;
  1476. pp[2] = loader->pp3;
  1477. pp[3] = loader->pp4;
  1478. num_base_points = (FT_UInt)gloader->base.outline.n_points;
  1479. error = load_truetype_glyph( loader,
  1480. (FT_UInt)subglyph->index,
  1481. recurse_count + 1,
  1482. FALSE );
  1483. if ( error )
  1484. goto Exit;
  1485. /* restore subglyph pointer */
  1486. subglyph = gloader->base.subglyphs + num_base_subgs + n;
  1487. /* restore phantom points if necessary */
  1488. if ( !( subglyph->flags & USE_MY_METRICS ) )
  1489. {
  1490. loader->pp1 = pp[0];
  1491. loader->pp2 = pp[1];
  1492. loader->pp3 = pp[2];
  1493. loader->pp4 = pp[3];
  1494. }
  1495. num_points = (FT_UInt)gloader->base.outline.n_points;
  1496. if ( num_points == num_base_points )
  1497. continue;
  1498. /* gloader->base.outline consists of three parts: */
  1499. /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */
  1500. /* */
  1501. /* (1): exists from the beginning */
  1502. /* (2): components that have been loaded so far */
  1503. /* (3): the newly loaded component */
  1504. error = TT_Process_Composite_Component( loader,
  1505. subglyph,
  1506. start_point,
  1507. num_base_points );
  1508. if ( error )
  1509. goto Exit;
  1510. }
  1511. loader->stream = old_stream;
  1512. loader->byte_len = old_byte_len;
  1513. /* process the glyph */
  1514. loader->ins_pos = ins_pos;
  1515. if ( IS_HINTED( loader->load_flags ) &&
  1516. #ifdef TT_USE_BYTECODE_INTERPRETER
  1517. subglyph->flags & WE_HAVE_INSTR &&
  1518. #endif
  1519. num_points > start_point )
  1520. {
  1521. error = TT_Process_Composite_Glyph( loader,
  1522. start_point,
  1523. start_contour );
  1524. if ( error )
  1525. goto Exit;
  1526. }
  1527. }
  1528. }
  1529. else
  1530. {
  1531. /* invalid composite count (negative but not -1) */
  1532. error = FT_THROW( Invalid_Outline );
  1533. goto Exit;
  1534. }
  1535. /***********************************************************************/
  1536. /***********************************************************************/
  1537. /***********************************************************************/
  1538. Exit:
  1539. if ( opened_frame )
  1540. face->forget_glyph_frame( loader );
  1541. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1542. if ( glyph_data_loaded )
  1543. face->root.internal->incremental_interface->funcs->free_glyph_data(
  1544. face->root.internal->incremental_interface->object,
  1545. &glyph_data );
  1546. #endif
  1547. return error;
  1548. }
  1549. static FT_Error
  1550. compute_glyph_metrics( TT_Loader loader,
  1551. FT_UInt glyph_index )
  1552. {
  1553. TT_Face face = (TT_Face)loader->face;
  1554. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1555. TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
  1556. #endif
  1557. FT_BBox bbox;
  1558. FT_Fixed y_scale;
  1559. TT_GlyphSlot glyph = loader->glyph;
  1560. TT_Size size = (TT_Size)loader->size;
  1561. y_scale = 0x10000L;
  1562. if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1563. y_scale = size->root.metrics.y_scale;
  1564. if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE )
  1565. FT_Outline_Get_CBox( &glyph->outline, &bbox );
  1566. else
  1567. bbox = loader->bbox;
  1568. /* get the device-independent horizontal advance; it is scaled later */
  1569. /* by the base layer. */
  1570. glyph->linearHoriAdvance = loader->linear;
  1571. glyph->metrics.horiBearingX = bbox.xMin;
  1572. glyph->metrics.horiBearingY = bbox.yMax;
  1573. glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
  1574. /* adjust advance width to the value contained in the hdmx table */
  1575. if ( !face->postscript.isFixedPitch &&
  1576. IS_HINTED( loader->load_flags ) )
  1577. {
  1578. FT_Byte* widthp;
  1579. widthp = tt_face_get_device_metrics( face,
  1580. size->root.metrics.x_ppem,
  1581. glyph_index );
  1582. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1583. if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
  1584. {
  1585. FT_Bool ignore_x_mode;
  1586. ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) !=
  1587. FT_RENDER_MODE_MONO );
  1588. if ( widthp &&
  1589. ( ( ignore_x_mode && loader->exec->compatible_widths ) ||
  1590. !ignore_x_mode ||
  1591. SPH_OPTION_BITMAP_WIDTHS ) )
  1592. glyph->metrics.horiAdvance = *widthp << 6;
  1593. }
  1594. else
  1595. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  1596. {
  1597. if ( widthp )
  1598. glyph->metrics.horiAdvance = *widthp << 6;
  1599. }
  1600. }
  1601. /* set glyph dimensions */
  1602. glyph->metrics.width = bbox.xMax - bbox.xMin;
  1603. glyph->metrics.height = bbox.yMax - bbox.yMin;
  1604. /* Now take care of vertical metrics. In the case where there is */
  1605. /* no vertical information within the font (relatively common), */
  1606. /* create some metrics manually */
  1607. {
  1608. FT_Pos top; /* scaled vertical top side bearing */
  1609. FT_Pos advance; /* scaled vertical advance height */
  1610. /* Get the unscaled top bearing and advance height. */
  1611. if ( face->vertical_info &&
  1612. face->vertical.number_Of_VMetrics > 0 )
  1613. {
  1614. top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax,
  1615. y_scale );
  1616. if ( loader->pp3.y <= loader->pp4.y )
  1617. advance = 0;
  1618. else
  1619. advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y,
  1620. y_scale );
  1621. }
  1622. else
  1623. {
  1624. FT_Pos height;
  1625. /* XXX Compute top side bearing and advance height in */
  1626. /* Get_VMetrics instead of here. */
  1627. /* NOTE: The OS/2 values are the only `portable' ones, */
  1628. /* which is why we use them, if there is an OS/2 */
  1629. /* table in the font. Otherwise, we use the */
  1630. /* values defined in the horizontal header. */
  1631. height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin,
  1632. y_scale );
  1633. if ( face->os2.version != 0xFFFFU )
  1634. advance = (FT_Pos)( face->os2.sTypoAscender -
  1635. face->os2.sTypoDescender );
  1636. else
  1637. advance = (FT_Pos)( face->horizontal.Ascender -
  1638. face->horizontal.Descender );
  1639. top = ( advance - height ) / 2;
  1640. }
  1641. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1642. {
  1643. FT_Incremental_InterfaceRec* incr;
  1644. FT_Incremental_MetricsRec metrics;
  1645. FT_Error error;
  1646. incr = face->root.internal->incremental_interface;
  1647. /* If this is an incrementally loaded font see if there are */
  1648. /* overriding metrics for this glyph. */
  1649. if ( incr && incr->funcs->get_glyph_metrics )
  1650. {
  1651. metrics.bearing_x = 0;
  1652. metrics.bearing_y = top;
  1653. metrics.advance = advance;
  1654. error = incr->funcs->get_glyph_metrics( incr->object,
  1655. glyph_index,
  1656. TRUE,
  1657. &metrics );
  1658. if ( error )
  1659. return error;
  1660. top = metrics.bearing_y;
  1661. advance = metrics.advance;
  1662. }
  1663. }
  1664. /* GWW: Do vertical metrics get loaded incrementally too? */
  1665. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1666. glyph->linearVertAdvance = advance;
  1667. /* scale the metrics */
  1668. if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) )
  1669. {
  1670. top = FT_MulFix( top, y_scale );
  1671. advance = FT_MulFix( advance, y_scale );
  1672. }
  1673. /* XXX: for now, we have no better algorithm for the lsb, but it */
  1674. /* should work fine. */
  1675. /* */
  1676. glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
  1677. glyph->metrics.horiAdvance / 2;
  1678. glyph->metrics.vertBearingY = top;
  1679. glyph->metrics.vertAdvance = advance;
  1680. }
  1681. return 0;
  1682. }
  1683. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  1684. static FT_Error
  1685. load_sbit_image( TT_Size size,
  1686. TT_GlyphSlot glyph,
  1687. FT_UInt glyph_index,
  1688. FT_Int32 load_flags )
  1689. {
  1690. TT_Face face;
  1691. SFNT_Service sfnt;
  1692. FT_Stream stream;
  1693. FT_Error error;
  1694. TT_SBit_MetricsRec metrics;
  1695. face = (TT_Face)glyph->face;
  1696. sfnt = (SFNT_Service)face->sfnt;
  1697. stream = face->root.stream;
  1698. error = sfnt->load_sbit_image( face,
  1699. size->strike_index,
  1700. glyph_index,
  1701. (FT_UInt)load_flags,
  1702. stream,
  1703. &glyph->bitmap,
  1704. &metrics );
  1705. if ( !error )
  1706. {
  1707. glyph->outline.n_points = 0;
  1708. glyph->outline.n_contours = 0;
  1709. glyph->metrics.width = (FT_Pos)metrics.width << 6;
  1710. glyph->metrics.height = (FT_Pos)metrics.height << 6;
  1711. glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
  1712. glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
  1713. glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
  1714. glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
  1715. glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
  1716. glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
  1717. glyph->format = FT_GLYPH_FORMAT_BITMAP;
  1718. if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
  1719. {
  1720. glyph->bitmap_left = metrics.vertBearingX;
  1721. glyph->bitmap_top = metrics.vertBearingY;
  1722. }
  1723. else
  1724. {
  1725. glyph->bitmap_left = metrics.horiBearingX;
  1726. glyph->bitmap_top = metrics.horiBearingY;
  1727. }
  1728. }
  1729. return error;
  1730. }
  1731. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  1732. static FT_Error
  1733. tt_loader_init( TT_Loader loader,
  1734. TT_Size size,
  1735. TT_GlyphSlot glyph,
  1736. FT_Int32 load_flags,
  1737. FT_Bool glyf_table_only )
  1738. {
  1739. FT_Error error;
  1740. TT_Face face;
  1741. FT_Stream stream;
  1742. #ifdef TT_USE_BYTECODE_INTERPRETER
  1743. FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
  1744. #endif
  1745. face = (TT_Face)glyph->face;
  1746. stream = face->root.stream;
  1747. FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) );
  1748. #ifdef TT_USE_BYTECODE_INTERPRETER
  1749. /* load execution context */
  1750. if ( IS_HINTED( load_flags ) && !glyf_table_only )
  1751. {
  1752. TT_ExecContext exec;
  1753. FT_Bool grayscale;
  1754. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1755. TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
  1756. FT_Bool subpixel_hinting = FALSE;
  1757. #if 0
  1758. /* not used yet */
  1759. FT_Bool compatible_widths;
  1760. FT_Bool symmetrical_smoothing;
  1761. FT_Bool bgr;
  1762. FT_Bool vertical_lcd;
  1763. FT_Bool subpixel_positioned;
  1764. FT_Bool gray_cleartype;
  1765. #endif
  1766. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  1767. FT_Bool reexecute = FALSE;
  1768. if ( size->bytecode_ready < 0 || size->cvt_ready < 0 )
  1769. {
  1770. error = tt_size_ready_bytecode( size, pedantic );
  1771. if ( error )
  1772. return error;
  1773. }
  1774. else if ( size->bytecode_ready )
  1775. return size->bytecode_ready;
  1776. else if ( size->cvt_ready )
  1777. return size->cvt_ready;
  1778. /* query new execution context */
  1779. exec = size->context;
  1780. if ( !exec )
  1781. return FT_THROW( Could_Not_Find_Context );
  1782. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1783. if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
  1784. {
  1785. subpixel_hinting = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags ) !=
  1786. FT_RENDER_MODE_MONO ) &&
  1787. SPH_OPTION_SET_SUBPIXEL );
  1788. if ( subpixel_hinting )
  1789. grayscale = FALSE;
  1790. else if ( SPH_OPTION_SET_GRAYSCALE )
  1791. {
  1792. grayscale = TRUE;
  1793. subpixel_hinting = FALSE;
  1794. }
  1795. else
  1796. grayscale = FALSE;
  1797. if ( FT_IS_TRICKY( glyph->face ) )
  1798. subpixel_hinting = FALSE;
  1799. exec->ignore_x_mode = subpixel_hinting || grayscale;
  1800. exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION;
  1801. if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 )
  1802. exec->rasterizer_version = TT_INTERPRETER_VERSION_35;
  1803. #if 1
  1804. exec->compatible_widths = SPH_OPTION_SET_COMPATIBLE_WIDTHS;
  1805. exec->symmetrical_smoothing = TRUE;
  1806. exec->bgr = FALSE;
  1807. exec->vertical_lcd = FALSE;
  1808. exec->subpixel_positioned = TRUE;
  1809. exec->gray_cleartype = FALSE;
  1810. #else /* 0 */
  1811. exec->compatible_widths =
  1812. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1813. TT_LOAD_COMPATIBLE_WIDTHS );
  1814. exec->symmetrical_smoothing =
  1815. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1816. TT_LOAD_SYMMETRICAL_SMOOTHING );
  1817. exec->bgr =
  1818. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1819. TT_LOAD_BGR );
  1820. exec->vertical_lcd =
  1821. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1822. TT_LOAD_VERTICAL_LCD );
  1823. exec->subpixel_positioned =
  1824. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1825. TT_LOAD_SUBPIXEL_POSITIONED );
  1826. exec->gray_cleartype =
  1827. FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1828. TT_LOAD_GRAY_CLEARTYPE );
  1829. #endif /* 0 */
  1830. }
  1831. else
  1832. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  1833. {
  1834. grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) !=
  1835. FT_RENDER_MODE_MONO );
  1836. }
  1837. error = TT_Load_Context( exec, face, size );
  1838. if ( error )
  1839. return error;
  1840. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1841. if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 )
  1842. {
  1843. /* a change from mono to subpixel rendering (and vice versa) */
  1844. /* requires a re-execution of the CVT program */
  1845. if ( subpixel_hinting != exec->subpixel_hinting )
  1846. {
  1847. FT_TRACE4(( "tt_loader_init: subpixel hinting change,"
  1848. " re-executing `prep' table\n" ));
  1849. exec->subpixel_hinting = subpixel_hinting;
  1850. reexecute = TRUE;
  1851. }
  1852. /* a change from mono to grayscale rendering (and vice versa) */
  1853. /* requires a re-execution of the CVT program */
  1854. if ( grayscale != exec->grayscale )
  1855. {
  1856. FT_TRACE4(( "tt_loader_init: grayscale hinting change,"
  1857. " re-executing `prep' table\n" ));
  1858. exec->grayscale = grayscale;
  1859. reexecute = TRUE;
  1860. }
  1861. }
  1862. else
  1863. #endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */
  1864. {
  1865. /* a change from mono to grayscale rendering (and vice versa) */
  1866. /* requires a re-execution of the CVT program */
  1867. if ( grayscale != exec->grayscale )
  1868. {
  1869. FT_TRACE4(( "tt_loader_init: grayscale hinting change,"
  1870. " re-executing `prep' table\n" ));
  1871. exec->grayscale = grayscale;
  1872. reexecute = TRUE;
  1873. }
  1874. }
  1875. if ( reexecute )
  1876. {
  1877. FT_UInt i;
  1878. for ( i = 0; i < size->cvt_size; i++ )
  1879. size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale );
  1880. error = tt_size_run_prep( size, pedantic );
  1881. if ( error )
  1882. return error;
  1883. }
  1884. /* check whether the cvt program has disabled hinting */
  1885. if ( exec->GS.instruct_control & 1 )
  1886. load_flags |= FT_LOAD_NO_HINTING;
  1887. /* load default graphics state -- if needed */
  1888. if ( exec->GS.instruct_control & 2 )
  1889. exec->GS = tt_default_graphics_state;
  1890. #ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING
  1891. /* check whether we have a font hinted for ClearType -- */
  1892. /* note that this flag can also be modified in a glyph's bytecode */
  1893. if ( exec->GS.instruct_control & 4 )
  1894. exec->ignore_x_mode = 0;
  1895. #endif
  1896. exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC );
  1897. loader->exec = exec;
  1898. loader->instructions = exec->glyphIns;
  1899. }
  1900. #endif /* TT_USE_BYTECODE_INTERPRETER */
  1901. /* seek to the beginning of the glyph table -- for Type 42 fonts */
  1902. /* the table might be accessed from a Postscript stream or something */
  1903. /* else... */
  1904. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1905. if ( face->root.internal->incremental_interface )
  1906. loader->glyf_offset = 0;
  1907. else
  1908. #endif
  1909. {
  1910. error = face->goto_table( face, TTAG_glyf, stream, 0 );
  1911. if ( FT_ERR_EQ( error, Table_Missing ) )
  1912. loader->glyf_offset = 0;
  1913. else if ( error )
  1914. {
  1915. FT_ERROR(( "tt_loader_init: could not access glyph table\n" ));
  1916. return error;
  1917. }
  1918. else
  1919. loader->glyf_offset = FT_STREAM_POS();
  1920. }
  1921. /* get face's glyph loader */
  1922. if ( !glyf_table_only )
  1923. {
  1924. FT_GlyphLoader gloader = glyph->internal->loader;
  1925. FT_GlyphLoader_Rewind( gloader );
  1926. loader->gloader = gloader;
  1927. }
  1928. loader->load_flags = (FT_ULong)load_flags;
  1929. loader->face = (FT_Face)face;
  1930. loader->size = (FT_Size)size;
  1931. loader->glyph = (FT_GlyphSlot)glyph;
  1932. loader->stream = stream;
  1933. return FT_Err_Ok;
  1934. }
  1935. /*************************************************************************/
  1936. /* */
  1937. /* <Function> */
  1938. /* TT_Load_Glyph */
  1939. /* */
  1940. /* <Description> */
  1941. /* A function used to load a single glyph within a given glyph slot, */
  1942. /* for a given size. */
  1943. /* */
  1944. /* <Input> */
  1945. /* glyph :: A handle to a target slot object where the glyph */
  1946. /* will be loaded. */
  1947. /* */
  1948. /* size :: A handle to the source face size at which the glyph */
  1949. /* must be scaled/loaded. */
  1950. /* */
  1951. /* glyph_index :: The index of the glyph in the font file. */
  1952. /* */
  1953. /* load_flags :: A flag indicating what to load for this glyph. The */
  1954. /* FT_LOAD_XXX constants can be used to control the */
  1955. /* glyph loading process (e.g., whether the outline */
  1956. /* should be scaled, whether to load bitmaps or not, */
  1957. /* whether to hint the outline, etc). */
  1958. /* */
  1959. /* <Return> */
  1960. /* FreeType error code. 0 means success. */
  1961. /* */
  1962. FT_LOCAL_DEF( FT_Error )
  1963. TT_Load_Glyph( TT_Size size,
  1964. TT_GlyphSlot glyph,
  1965. FT_UInt glyph_index,
  1966. FT_Int32 load_flags )
  1967. {
  1968. FT_Error error;
  1969. TT_LoaderRec loader;
  1970. FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index ));
  1971. #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
  1972. /* try to load embedded bitmap if any */
  1973. /* */
  1974. /* XXX: The convention should be emphasized in */
  1975. /* the documents because it can be confusing. */
  1976. if ( size->strike_index != 0xFFFFFFFFUL &&
  1977. ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
  1978. {
  1979. error = load_sbit_image( size, glyph, glyph_index, load_flags );
  1980. if ( !error )
  1981. {
  1982. if ( FT_IS_SCALABLE( glyph->face ) )
  1983. {
  1984. /* for the bbox we need the header only */
  1985. (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE );
  1986. (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE );
  1987. glyph->linearHoriAdvance = loader.linear;
  1988. glyph->linearVertAdvance = loader.vadvance;
  1989. /* sanity checks: if `xxxAdvance' in the sbit metric */
  1990. /* structure isn't set, use `linearXXXAdvance' */
  1991. if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance )
  1992. glyph->metrics.horiAdvance =
  1993. FT_MulFix( glyph->linearHoriAdvance,
  1994. size->root.metrics.x_scale );
  1995. if ( !glyph->metrics.vertAdvance && glyph->linearVertAdvance )
  1996. glyph->metrics.vertAdvance =
  1997. FT_MulFix( glyph->linearVertAdvance,
  1998. size->root.metrics.y_scale );
  1999. }
  2000. return FT_Err_Ok;
  2001. }
  2002. }
  2003. #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
  2004. /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */
  2005. if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid )
  2006. return FT_THROW( Invalid_Size_Handle );
  2007. if ( load_flags & FT_LOAD_SBITS_ONLY )
  2008. return FT_THROW( Invalid_Argument );
  2009. error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
  2010. if ( error )
  2011. return error;
  2012. glyph->format = FT_GLYPH_FORMAT_OUTLINE;
  2013. glyph->num_subglyphs = 0;
  2014. glyph->outline.flags = 0;
  2015. /* main loading loop */
  2016. error = load_truetype_glyph( &loader, glyph_index, 0, FALSE );
  2017. if ( !error )
  2018. {
  2019. if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE )
  2020. {
  2021. glyph->num_subglyphs = loader.gloader->base.num_subglyphs;
  2022. glyph->subglyphs = loader.gloader->base.subglyphs;
  2023. }
  2024. else
  2025. {
  2026. glyph->outline = loader.gloader->base.outline;
  2027. glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS;
  2028. /* Translate array so that (0,0) is the glyph's origin. Note */
  2029. /* that this behaviour is independent on the value of bit 1 of */
  2030. /* the `flags' field in the `head' table -- at least major */
  2031. /* applications like Acroread indicate that. */
  2032. if ( loader.pp1.x )
  2033. FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 );
  2034. }
  2035. #ifdef TT_USE_BYTECODE_INTERPRETER
  2036. if ( IS_HINTED( load_flags ) )
  2037. {
  2038. if ( loader.exec->GS.scan_control )
  2039. {
  2040. /* convert scan conversion mode to FT_OUTLINE_XXX flags */
  2041. switch ( loader.exec->GS.scan_type )
  2042. {
  2043. case 0: /* simple drop-outs including stubs */
  2044. glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS;
  2045. break;
  2046. case 1: /* simple drop-outs excluding stubs */
  2047. /* nothing; it's the default rendering mode */
  2048. break;
  2049. case 4: /* smart drop-outs including stubs */
  2050. glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS |
  2051. FT_OUTLINE_INCLUDE_STUBS;
  2052. break;
  2053. case 5: /* smart drop-outs excluding stubs */
  2054. glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS;
  2055. break;
  2056. default: /* no drop-out control */
  2057. glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
  2058. break;
  2059. }
  2060. }
  2061. else
  2062. glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS;
  2063. }
  2064. #endif /* TT_USE_BYTECODE_INTERPRETER */
  2065. error = compute_glyph_metrics( &loader, glyph_index );
  2066. }
  2067. /* Set the `high precision' bit flag. */
  2068. /* This is _critical_ to get correct output for monochrome */
  2069. /* TrueType glyphs at all sizes using the bytecode interpreter. */
  2070. /* */
  2071. if ( !( load_flags & FT_LOAD_NO_SCALE ) &&
  2072. size->root.metrics.y_ppem < 24 )
  2073. glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
  2074. return error;
  2075. }
  2076. /* END */