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

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

http://ftk.googlecode.com/
C | 1713 lines | 923 code | 314 blank | 476 comment | 45 complexity | a38ffc3ad46931423aa26ed21fd0579a MD5 | raw file
Possible License(s): LGPL-3.0
  1. /***************************************************************************/
  2. /* */
  3. /* ttinterp.c */
  4. /* */
  5. /* TrueType bytecode interpreter (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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_TRIGONOMETRY_H
  21. #include FT_SYSTEM_H
  22. #include "ttinterp.h"
  23. #include "tterrors.h"
  24. #ifdef TT_USE_BYTECODE_INTERPRETER
  25. #define TT_MULFIX FT_MulFix
  26. #define TT_MULDIV FT_MulDiv
  27. #define TT_MULDIV_NO_ROUND FT_MulDiv_No_Round
  28. /*************************************************************************/
  29. /* */
  30. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  31. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  32. /* messages during execution. */
  33. /* */
  34. #undef FT_COMPONENT
  35. #define FT_COMPONENT trace_ttinterp
  36. /*************************************************************************/
  37. /* */
  38. /* In order to detect infinite loops in the code, we set up a counter */
  39. /* within the run loop. A single stroke of interpretation is now */
  40. /* limited to a maximal number of opcodes defined below. */
  41. /* */
  42. #define MAX_RUNNABLE_OPCODES 1000000L
  43. /*************************************************************************/
  44. /* */
  45. /* There are two kinds of implementations: */
  46. /* */
  47. /* a. static implementation */
  48. /* */
  49. /* The current execution context is a static variable, which fields */
  50. /* are accessed directly by the interpreter during execution. The */
  51. /* context is named `cur'. */
  52. /* */
  53. /* This version is non-reentrant, of course. */
  54. /* */
  55. /* b. indirect implementation */
  56. /* */
  57. /* The current execution context is passed to _each_ function as its */
  58. /* first argument, and each field is thus accessed indirectly. */
  59. /* */
  60. /* This version is fully re-entrant. */
  61. /* */
  62. /* The idea is that an indirect implementation may be slower to execute */
  63. /* on low-end processors that are used in some systems (like 386s or */
  64. /* even 486s). */
  65. /* */
  66. /* As a consequence, the indirect implementation is now the default, as */
  67. /* its performance costs can be considered negligible in our context. */
  68. /* Note, however, that we kept the same source with macros because: */
  69. /* */
  70. /* - The code is kept very close in design to the Pascal code used for */
  71. /* development. */
  72. /* */
  73. /* - It's much more readable that way! */
  74. /* */
  75. /* - It's still open to experimentation and tuning. */
  76. /* */
  77. /*************************************************************************/
  78. #ifndef TT_CONFIG_OPTION_STATIC_INTERPRETER /* indirect implementation */
  79. #define CUR (*exc) /* see ttobjs.h */
  80. /*************************************************************************/
  81. /* */
  82. /* This macro is used whenever `exec' is unused in a function, to avoid */
  83. /* stupid warnings from pedantic compilers. */
  84. /* */
  85. #define FT_UNUSED_EXEC FT_UNUSED( exc )
  86. #else /* static implementation */
  87. #define CUR cur
  88. #define FT_UNUSED_EXEC int __dummy = __dummy
  89. static
  90. TT_ExecContextRec cur; /* static exec. context variable */
  91. /* apparently, we have a _lot_ of direct indexing when accessing */
  92. /* the static `cur', which makes the code bigger (due to all the */
  93. /* four bytes addresses). */
  94. #endif /* TT_CONFIG_OPTION_STATIC_INTERPRETER */
  95. /*************************************************************************/
  96. /* */
  97. /* The instruction argument stack. */
  98. /* */
  99. #define INS_ARG EXEC_OP_ FT_Long* args /* see ttobjs.h for EXEC_OP_ */
  100. /*************************************************************************/
  101. /* */
  102. /* This macro is used whenever `args' is unused in a function, to avoid */
  103. /* stupid warnings from pedantic compilers. */
  104. /* */
  105. #define FT_UNUSED_ARG FT_UNUSED_EXEC; FT_UNUSED( args )
  106. /*************************************************************************/
  107. /* */
  108. /* The following macros hide the use of EXEC_ARG and EXEC_ARG_ to */
  109. /* increase readability of the code. */
  110. /* */
  111. /*************************************************************************/
  112. #define SKIP_Code() \
  113. SkipCode( EXEC_ARG )
  114. #define GET_ShortIns() \
  115. GetShortIns( EXEC_ARG )
  116. #define NORMalize( x, y, v ) \
  117. Normalize( EXEC_ARG_ x, y, v )
  118. #define SET_SuperRound( scale, flags ) \
  119. SetSuperRound( EXEC_ARG_ scale, flags )
  120. #define ROUND_None( d, c ) \
  121. Round_None( EXEC_ARG_ d, c )
  122. #define INS_Goto_CodeRange( range, ip ) \
  123. Ins_Goto_CodeRange( EXEC_ARG_ range, ip )
  124. #define CUR_Func_move( z, p, d ) \
  125. CUR.func_move( EXEC_ARG_ z, p, d )
  126. #define CUR_Func_move_orig( z, p, d ) \
  127. CUR.func_move_orig( EXEC_ARG_ z, p, d )
  128. #define CUR_Func_round( d, c ) \
  129. CUR.func_round( EXEC_ARG_ d, c )
  130. #define CUR_Func_read_cvt( index ) \
  131. CUR.func_read_cvt( EXEC_ARG_ index )
  132. #define CUR_Func_write_cvt( index, val ) \
  133. CUR.func_write_cvt( EXEC_ARG_ index, val )
  134. #define CUR_Func_move_cvt( index, val ) \
  135. CUR.func_move_cvt( EXEC_ARG_ index, val )
  136. #define CURRENT_Ratio() \
  137. Current_Ratio( EXEC_ARG )
  138. #define CURRENT_Ppem() \
  139. Current_Ppem( EXEC_ARG )
  140. #define CUR_Ppem() \
  141. Cur_PPEM( EXEC_ARG )
  142. #define INS_SxVTL( a, b, c, d ) \
  143. Ins_SxVTL( EXEC_ARG_ a, b, c, d )
  144. #define COMPUTE_Funcs() \
  145. Compute_Funcs( EXEC_ARG )
  146. #define COMPUTE_Round( a ) \
  147. Compute_Round( EXEC_ARG_ a )
  148. #define COMPUTE_Point_Displacement( a, b, c, d ) \
  149. Compute_Point_Displacement( EXEC_ARG_ a, b, c, d )
  150. #define MOVE_Zp2_Point( a, b, c, t ) \
  151. Move_Zp2_Point( EXEC_ARG_ a, b, c, t )
  152. #define CUR_Func_project( v1, v2 ) \
  153. CUR.func_project( EXEC_ARG_ (v1)->x - (v2)->x, (v1)->y - (v2)->y )
  154. #define CUR_Func_dualproj( v1, v2 ) \
  155. CUR.func_dualproj( EXEC_ARG_ (v1)->x - (v2)->x, (v1)->y - (v2)->y )
  156. #define CUR_fast_project( v ) \
  157. CUR.func_project( EXEC_ARG_ (v)->x, (v)->y )
  158. #define CUR_fast_dualproj( v ) \
  159. CUR.func_dualproj( EXEC_ARG_ (v)->x, (v)->y )
  160. /*************************************************************************/
  161. /* */
  162. /* Instruction dispatch function, as used by the interpreter. */
  163. /* */
  164. typedef void (*TInstruction_Function)( INS_ARG );
  165. /*************************************************************************/
  166. /* */
  167. /* A simple bounds-checking macro. */
  168. /* */
  169. #define BOUNDS( x, n ) ( (FT_UInt)(x) >= (FT_UInt)(n) )
  170. #undef SUCCESS
  171. #define SUCCESS 0
  172. #undef FAILURE
  173. #define FAILURE 1
  174. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  175. #define GUESS_VECTOR( V ) \
  176. if ( CUR.face->unpatented_hinting ) \
  177. { \
  178. CUR.GS.V.x = (FT_F2Dot14)( CUR.GS.both_x_axis ? 0x4000 : 0 ); \
  179. CUR.GS.V.y = (FT_F2Dot14)( CUR.GS.both_x_axis ? 0 : 0x4000 ); \
  180. }
  181. #else
  182. #define GUESS_VECTOR( V )
  183. #endif
  184. /*************************************************************************/
  185. /* */
  186. /* CODERANGE FUNCTIONS */
  187. /* */
  188. /*************************************************************************/
  189. /*************************************************************************/
  190. /* */
  191. /* <Function> */
  192. /* TT_Goto_CodeRange */
  193. /* */
  194. /* <Description> */
  195. /* Switches to a new code range (updates the code related elements in */
  196. /* `exec', and `IP'). */
  197. /* */
  198. /* <Input> */
  199. /* range :: The new execution code range. */
  200. /* */
  201. /* IP :: The new IP in the new code range. */
  202. /* */
  203. /* <InOut> */
  204. /* exec :: The target execution context. */
  205. /* */
  206. /* <Return> */
  207. /* FreeType error code. 0 means success. */
  208. /* */
  209. FT_LOCAL_DEF( FT_Error )
  210. TT_Goto_CodeRange( TT_ExecContext exec,
  211. FT_Int range,
  212. FT_Long IP )
  213. {
  214. TT_CodeRange* coderange;
  215. FT_ASSERT( range >= 1 && range <= 3 );
  216. coderange = &exec->codeRangeTable[range - 1];
  217. FT_ASSERT( coderange->base != NULL );
  218. /* NOTE: Because the last instruction of a program may be a CALL */
  219. /* which will return to the first byte *after* the code */
  220. /* range, we test for IP <= Size instead of IP < Size. */
  221. /* */
  222. FT_ASSERT( (FT_ULong)IP <= coderange->size );
  223. exec->code = coderange->base;
  224. exec->codeSize = coderange->size;
  225. exec->IP = IP;
  226. exec->curRange = range;
  227. return TT_Err_Ok;
  228. }
  229. /*************************************************************************/
  230. /* */
  231. /* <Function> */
  232. /* TT_Set_CodeRange */
  233. /* */
  234. /* <Description> */
  235. /* Sets a code range. */
  236. /* */
  237. /* <Input> */
  238. /* range :: The code range index. */
  239. /* */
  240. /* base :: The new code base. */
  241. /* */
  242. /* length :: The range size in bytes. */
  243. /* */
  244. /* <InOut> */
  245. /* exec :: The target execution context. */
  246. /* */
  247. /* <Return> */
  248. /* FreeType error code. 0 means success. */
  249. /* */
  250. FT_LOCAL_DEF( FT_Error )
  251. TT_Set_CodeRange( TT_ExecContext exec,
  252. FT_Int range,
  253. void* base,
  254. FT_Long length )
  255. {
  256. FT_ASSERT( range >= 1 && range <= 3 );
  257. exec->codeRangeTable[range - 1].base = (FT_Byte*)base;
  258. exec->codeRangeTable[range - 1].size = length;
  259. return TT_Err_Ok;
  260. }
  261. /*************************************************************************/
  262. /* */
  263. /* <Function> */
  264. /* TT_Clear_CodeRange */
  265. /* */
  266. /* <Description> */
  267. /* Clears a code range. */
  268. /* */
  269. /* <Input> */
  270. /* range :: The code range index. */
  271. /* */
  272. /* <InOut> */
  273. /* exec :: The target execution context. */
  274. /* */
  275. /* <Return> */
  276. /* FreeType error code. 0 means success. */
  277. /* */
  278. /* <Note> */
  279. /* Does not set the Error variable. */
  280. /* */
  281. FT_LOCAL_DEF( FT_Error )
  282. TT_Clear_CodeRange( TT_ExecContext exec,
  283. FT_Int range )
  284. {
  285. FT_ASSERT( range >= 1 && range <= 3 );
  286. exec->codeRangeTable[range - 1].base = NULL;
  287. exec->codeRangeTable[range - 1].size = 0;
  288. return TT_Err_Ok;
  289. }
  290. /*************************************************************************/
  291. /* */
  292. /* EXECUTION CONTEXT ROUTINES */
  293. /* */
  294. /*************************************************************************/
  295. /*************************************************************************/
  296. /* */
  297. /* <Function> */
  298. /* TT_Done_Context */
  299. /* */
  300. /* <Description> */
  301. /* Destroys a given context. */
  302. /* */
  303. /* <Input> */
  304. /* exec :: A handle to the target execution context. */
  305. /* */
  306. /* memory :: A handle to the parent memory object. */
  307. /* */
  308. /* <Return> */
  309. /* FreeType error code. 0 means success. */
  310. /* */
  311. /* <Note> */
  312. /* Only the glyph loader and debugger should call this function. */
  313. /* */
  314. FT_LOCAL_DEF( FT_Error )
  315. TT_Done_Context( TT_ExecContext exec )
  316. {
  317. FT_Memory memory = exec->memory;
  318. /* points zone */
  319. exec->maxPoints = 0;
  320. exec->maxContours = 0;
  321. /* free stack */
  322. FT_FREE( exec->stack );
  323. exec->stackSize = 0;
  324. /* free call stack */
  325. FT_FREE( exec->callStack );
  326. exec->callSize = 0;
  327. exec->callTop = 0;
  328. /* free glyph code range */
  329. FT_FREE( exec->glyphIns );
  330. exec->glyphSize = 0;
  331. exec->size = NULL;
  332. exec->face = NULL;
  333. FT_FREE( exec );
  334. return TT_Err_Ok;
  335. }
  336. /*************************************************************************/
  337. /* */
  338. /* <Function> */
  339. /* Init_Context */
  340. /* */
  341. /* <Description> */
  342. /* Initializes a context object. */
  343. /* */
  344. /* <Input> */
  345. /* memory :: A handle to the parent memory object. */
  346. /* */
  347. /* <InOut> */
  348. /* exec :: A handle to the target execution context. */
  349. /* */
  350. /* <Return> */
  351. /* FreeType error code. 0 means success. */
  352. /* */
  353. static FT_Error
  354. Init_Context( TT_ExecContext exec,
  355. FT_Memory memory )
  356. {
  357. FT_Error error;
  358. FT_TRACE1(( "Init_Context: new object at 0x%08p\n", exec ));
  359. exec->memory = memory;
  360. exec->callSize = 32;
  361. if ( FT_NEW_ARRAY( exec->callStack, exec->callSize ) )
  362. goto Fail_Memory;
  363. /* all values in the context are set to 0 already, but this is */
  364. /* here as a remainder */
  365. exec->maxPoints = 0;
  366. exec->maxContours = 0;
  367. exec->stackSize = 0;
  368. exec->glyphSize = 0;
  369. exec->stack = NULL;
  370. exec->glyphIns = NULL;
  371. exec->face = NULL;
  372. exec->size = NULL;
  373. return TT_Err_Ok;
  374. Fail_Memory:
  375. FT_ERROR(( "Init_Context: not enough memory for 0x%08lx\n",
  376. (FT_Long)exec ));
  377. TT_Done_Context( exec );
  378. return error;
  379. }
  380. /*************************************************************************/
  381. /* */
  382. /* <Function> */
  383. /* Update_Max */
  384. /* */
  385. /* <Description> */
  386. /* Checks the size of a buffer and reallocates it if necessary. */
  387. /* */
  388. /* <Input> */
  389. /* memory :: A handle to the parent memory object. */
  390. /* */
  391. /* multiplier :: The size in bytes of each element in the buffer. */
  392. /* */
  393. /* new_max :: The new capacity (size) of the buffer. */
  394. /* */
  395. /* <InOut> */
  396. /* size :: The address of the buffer's current size expressed */
  397. /* in elements. */
  398. /* */
  399. /* buff :: The address of the buffer base pointer. */
  400. /* */
  401. /* <Return> */
  402. /* FreeType error code. 0 means success. */
  403. /* */
  404. static FT_Error
  405. Update_Max( FT_Memory memory,
  406. FT_ULong* size,
  407. FT_Long multiplier,
  408. void* _pbuff,
  409. FT_ULong new_max )
  410. {
  411. FT_Error error;
  412. void** pbuff = (void**)_pbuff;
  413. if ( *size < new_max )
  414. {
  415. if ( FT_REALLOC( *pbuff, *size * multiplier, new_max * multiplier ) )
  416. return error;
  417. *size = new_max;
  418. }
  419. return TT_Err_Ok;
  420. }
  421. /*************************************************************************/
  422. /* */
  423. /* <Function> */
  424. /* TT_Load_Context */
  425. /* */
  426. /* <Description> */
  427. /* Prepare an execution context for glyph hinting. */
  428. /* */
  429. /* <Input> */
  430. /* face :: A handle to the source face object. */
  431. /* */
  432. /* size :: A handle to the source size object. */
  433. /* */
  434. /* <InOut> */
  435. /* exec :: A handle to the target execution context. */
  436. /* */
  437. /* <Return> */
  438. /* FreeType error code. 0 means success. */
  439. /* */
  440. /* <Note> */
  441. /* Only the glyph loader and debugger should call this function. */
  442. /* */
  443. FT_LOCAL_DEF( FT_Error )
  444. TT_Load_Context( TT_ExecContext exec,
  445. TT_Face face,
  446. TT_Size size )
  447. {
  448. FT_Int i;
  449. FT_ULong tmp;
  450. TT_MaxProfile* maxp;
  451. FT_Error error;
  452. exec->face = face;
  453. maxp = &face->max_profile;
  454. exec->size = size;
  455. if ( size )
  456. {
  457. exec->numFDefs = size->num_function_defs;
  458. exec->maxFDefs = size->max_function_defs;
  459. exec->numIDefs = size->num_instruction_defs;
  460. exec->maxIDefs = size->max_instruction_defs;
  461. exec->FDefs = size->function_defs;
  462. exec->IDefs = size->instruction_defs;
  463. exec->tt_metrics = size->ttmetrics;
  464. exec->metrics = size->metrics;
  465. exec->maxFunc = size->max_func;
  466. exec->maxIns = size->max_ins;
  467. for ( i = 0; i < TT_MAX_CODE_RANGES; i++ )
  468. exec->codeRangeTable[i] = size->codeRangeTable[i];
  469. /* set graphics state */
  470. exec->GS = size->GS;
  471. exec->cvtSize = size->cvt_size;
  472. exec->cvt = size->cvt;
  473. exec->storeSize = size->storage_size;
  474. exec->storage = size->storage;
  475. exec->twilight = size->twilight;
  476. }
  477. /* XXX: We reserve a little more elements on the stack to deal safely */
  478. /* with broken fonts like arialbs, courbs, timesbs, etc. */
  479. tmp = exec->stackSize;
  480. error = Update_Max( exec->memory,
  481. &tmp,
  482. sizeof ( FT_F26Dot6 ),
  483. (void*)&exec->stack,
  484. maxp->maxStackElements + 32 );
  485. exec->stackSize = (FT_UInt)tmp;
  486. if ( error )
  487. return error;
  488. tmp = exec->glyphSize;
  489. error = Update_Max( exec->memory,
  490. &tmp,
  491. sizeof ( FT_Byte ),
  492. (void*)&exec->glyphIns,
  493. maxp->maxSizeOfInstructions );
  494. exec->glyphSize = (FT_UShort)tmp;
  495. if ( error )
  496. return error;
  497. exec->pts.n_points = 0;
  498. exec->pts.n_contours = 0;
  499. exec->zp1 = exec->pts;
  500. exec->zp2 = exec->pts;
  501. exec->zp0 = exec->pts;
  502. exec->instruction_trap = FALSE;
  503. return TT_Err_Ok;
  504. }
  505. /*************************************************************************/
  506. /* */
  507. /* <Function> */
  508. /* TT_Save_Context */
  509. /* */
  510. /* <Description> */
  511. /* Saves the code ranges in a `size' object. */
  512. /* */
  513. /* <Input> */
  514. /* exec :: A handle to the source execution context. */
  515. /* */
  516. /* <InOut> */
  517. /* size :: A handle to the target size object. */
  518. /* */
  519. /* <Return> */
  520. /* FreeType error code. 0 means success. */
  521. /* */
  522. /* <Note> */
  523. /* Only the glyph loader and debugger should call this function. */
  524. /* */
  525. FT_LOCAL_DEF( FT_Error )
  526. TT_Save_Context( TT_ExecContext exec,
  527. TT_Size size )
  528. {
  529. FT_Int i;
  530. /* XXXX: Will probably disappear soon with all the code range */
  531. /* management, which is now rather obsolete. */
  532. /* */
  533. size->num_function_defs = exec->numFDefs;
  534. size->num_instruction_defs = exec->numIDefs;
  535. size->max_func = exec->maxFunc;
  536. size->max_ins = exec->maxIns;
  537. for ( i = 0; i < TT_MAX_CODE_RANGES; i++ )
  538. size->codeRangeTable[i] = exec->codeRangeTable[i];
  539. return TT_Err_Ok;
  540. }
  541. /*************************************************************************/
  542. /* */
  543. /* <Function> */
  544. /* TT_Run_Context */
  545. /* */
  546. /* <Description> */
  547. /* Executes one or more instructions in the execution context. */
  548. /* */
  549. /* <Input> */
  550. /* debug :: A Boolean flag. If set, the function sets some internal */
  551. /* variables and returns immediately, otherwise TT_RunIns() */
  552. /* is called. */
  553. /* */
  554. /* This is commented out currently. */
  555. /* */
  556. /* <Input> */
  557. /* exec :: A handle to the target execution context. */
  558. /* */
  559. /* <Return> */
  560. /* TrueType error code. 0 means success. */
  561. /* */
  562. /* <Note> */
  563. /* Only the glyph loader and debugger should call this function. */
  564. /* */
  565. FT_LOCAL_DEF( FT_Error )
  566. TT_Run_Context( TT_ExecContext exec,
  567. FT_Bool debug )
  568. {
  569. FT_Error error;
  570. if ( ( error = TT_Goto_CodeRange( exec, tt_coderange_glyph, 0 ) )
  571. != TT_Err_Ok )
  572. return error;
  573. exec->zp0 = exec->pts;
  574. exec->zp1 = exec->pts;
  575. exec->zp2 = exec->pts;
  576. exec->GS.gep0 = 1;
  577. exec->GS.gep1 = 1;
  578. exec->GS.gep2 = 1;
  579. exec->GS.projVector.x = 0x4000;
  580. exec->GS.projVector.y = 0x0000;
  581. exec->GS.freeVector = exec->GS.projVector;
  582. exec->GS.dualVector = exec->GS.projVector;
  583. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  584. exec->GS.both_x_axis = TRUE;
  585. #endif
  586. exec->GS.round_state = 1;
  587. exec->GS.loop = 1;
  588. /* some glyphs leave something on the stack. so we clean it */
  589. /* before a new execution. */
  590. exec->top = 0;
  591. exec->callTop = 0;
  592. #if 1
  593. FT_UNUSED( debug );
  594. return exec->face->interpreter( exec );
  595. #else
  596. if ( !debug )
  597. return TT_RunIns( exec );
  598. else
  599. return TT_Err_Ok;
  600. #endif
  601. }
  602. /* The default value for `scan_control' is documented as FALSE in the */
  603. /* TrueType specification. This is confusing since it implies a */
  604. /* Boolean value. However, this is not the case, thus both the */
  605. /* default values of our `scan_type' and `scan_control' fields (which */
  606. /* the documentation's `scan_control' variable is split into) are */
  607. /* zero. */
  608. const TT_GraphicsState tt_default_graphics_state =
  609. {
  610. 0, 0, 0,
  611. { 0x4000, 0 },
  612. { 0x4000, 0 },
  613. { 0x4000, 0 },
  614. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  615. TRUE,
  616. #endif
  617. 1, 64, 1,
  618. TRUE, 68, 0, 0, 9, 3,
  619. 0, FALSE, 0, 1, 1, 1
  620. };
  621. /* documentation is in ttinterp.h */
  622. FT_EXPORT_DEF( TT_ExecContext )
  623. TT_New_Context( TT_Driver driver )
  624. {
  625. TT_ExecContext exec;
  626. FT_Memory memory;
  627. memory = driver->root.root.memory;
  628. exec = driver->context;
  629. if ( !driver->context )
  630. {
  631. FT_Error error;
  632. /* allocate object */
  633. if ( FT_NEW( exec ) )
  634. goto Fail;
  635. /* initialize it; in case of error this deallocates `exec' too */
  636. error = Init_Context( exec, memory );
  637. if ( error )
  638. goto Fail;
  639. /* store it into the driver */
  640. driver->context = exec;
  641. }
  642. return driver->context;
  643. Fail:
  644. return NULL;
  645. }
  646. /*************************************************************************/
  647. /* */
  648. /* Before an opcode is executed, the interpreter verifies that there are */
  649. /* enough arguments on the stack, with the help of the `Pop_Push_Count' */
  650. /* table. */
  651. /* */
  652. /* For each opcode, the first column gives the number of arguments that */
  653. /* are popped from the stack; the second one gives the number of those */
  654. /* that are pushed in result. */
  655. /* */
  656. /* Opcodes which have a varying number of parameters in the data stream */
  657. /* (NPUSHB, NPUSHW) are handled specially; they have a negative value in */
  658. /* the `opcode_length' table, and the value in `Pop_Push_Count' is set */
  659. /* to zero. */
  660. /* */
  661. /*************************************************************************/
  662. #undef PACK
  663. #define PACK( x, y ) ( ( x << 4 ) | y )
  664. static
  665. const FT_Byte Pop_Push_Count[256] =
  666. {
  667. /* opcodes are gathered in groups of 16 */
  668. /* please keep the spaces as they are */
  669. /* SVTCA y */ PACK( 0, 0 ),
  670. /* SVTCA x */ PACK( 0, 0 ),
  671. /* SPvTCA y */ PACK( 0, 0 ),
  672. /* SPvTCA x */ PACK( 0, 0 ),
  673. /* SFvTCA y */ PACK( 0, 0 ),
  674. /* SFvTCA x */ PACK( 0, 0 ),
  675. /* SPvTL // */ PACK( 2, 0 ),
  676. /* SPvTL + */ PACK( 2, 0 ),
  677. /* SFvTL // */ PACK( 2, 0 ),
  678. /* SFvTL + */ PACK( 2, 0 ),
  679. /* SPvFS */ PACK( 2, 0 ),
  680. /* SFvFS */ PACK( 2, 0 ),
  681. /* GPV */ PACK( 0, 2 ),
  682. /* GFV */ PACK( 0, 2 ),
  683. /* SFvTPv */ PACK( 0, 0 ),
  684. /* ISECT */ PACK( 5, 0 ),
  685. /* SRP0 */ PACK( 1, 0 ),
  686. /* SRP1 */ PACK( 1, 0 ),
  687. /* SRP2 */ PACK( 1, 0 ),
  688. /* SZP0 */ PACK( 1, 0 ),
  689. /* SZP1 */ PACK( 1, 0 ),
  690. /* SZP2 */ PACK( 1, 0 ),
  691. /* SZPS */ PACK( 1, 0 ),
  692. /* SLOOP */ PACK( 1, 0 ),
  693. /* RTG */ PACK( 0, 0 ),
  694. /* RTHG */ PACK( 0, 0 ),
  695. /* SMD */ PACK( 1, 0 ),
  696. /* ELSE */ PACK( 0, 0 ),
  697. /* JMPR */ PACK( 1, 0 ),
  698. /* SCvTCi */ PACK( 1, 0 ),
  699. /* SSwCi */ PACK( 1, 0 ),
  700. /* SSW */ PACK( 1, 0 ),
  701. /* DUP */ PACK( 1, 2 ),
  702. /* POP */ PACK( 1, 0 ),
  703. /* CLEAR */ PACK( 0, 0 ),
  704. /* SWAP */ PACK( 2, 2 ),
  705. /* DEPTH */ PACK( 0, 1 ),
  706. /* CINDEX */ PACK( 1, 1 ),
  707. /* MINDEX */ PACK( 1, 0 ),
  708. /* AlignPTS */ PACK( 2, 0 ),
  709. /* INS_$28 */ PACK( 0, 0 ),
  710. /* UTP */ PACK( 1, 0 ),
  711. /* LOOPCALL */ PACK( 2, 0 ),
  712. /* CALL */ PACK( 1, 0 ),
  713. /* FDEF */ PACK( 1, 0 ),
  714. /* ENDF */ PACK( 0, 0 ),
  715. /* MDAP[0] */ PACK( 1, 0 ),
  716. /* MDAP[1] */ PACK( 1, 0 ),
  717. /* IUP[0] */ PACK( 0, 0 ),
  718. /* IUP[1] */ PACK( 0, 0 ),
  719. /* SHP[0] */ PACK( 0, 0 ),
  720. /* SHP[1] */ PACK( 0, 0 ),
  721. /* SHC[0] */ PACK( 1, 0 ),
  722. /* SHC[1] */ PACK( 1, 0 ),
  723. /* SHZ[0] */ PACK( 1, 0 ),
  724. /* SHZ[1] */ PACK( 1, 0 ),
  725. /* SHPIX */ PACK( 1, 0 ),
  726. /* IP */ PACK( 0, 0 ),
  727. /* MSIRP[0] */ PACK( 2, 0 ),
  728. /* MSIRP[1] */ PACK( 2, 0 ),
  729. /* AlignRP */ PACK( 0, 0 ),
  730. /* RTDG */ PACK( 0, 0 ),
  731. /* MIAP[0] */ PACK( 2, 0 ),
  732. /* MIAP[1] */ PACK( 2, 0 ),
  733. /* NPushB */ PACK( 0, 0 ),
  734. /* NPushW */ PACK( 0, 0 ),
  735. /* WS */ PACK( 2, 0 ),
  736. /* RS */ PACK( 1, 1 ),
  737. /* WCvtP */ PACK( 2, 0 ),
  738. /* RCvt */ PACK( 1, 1 ),
  739. /* GC[0] */ PACK( 1, 1 ),
  740. /* GC[1] */ PACK( 1, 1 ),
  741. /* SCFS */ PACK( 2, 0 ),
  742. /* MD[0] */ PACK( 2, 1 ),
  743. /* MD[1] */ PACK( 2, 1 ),
  744. /* MPPEM */ PACK( 0, 1 ),
  745. /* MPS */ PACK( 0, 1 ),
  746. /* FlipON */ PACK( 0, 0 ),
  747. /* FlipOFF */ PACK( 0, 0 ),
  748. /* DEBUG */ PACK( 1, 0 ),
  749. /* LT */ PACK( 2, 1 ),
  750. /* LTEQ */ PACK( 2, 1 ),
  751. /* GT */ PACK( 2, 1 ),
  752. /* GTEQ */ PACK( 2, 1 ),
  753. /* EQ */ PACK( 2, 1 ),
  754. /* NEQ */ PACK( 2, 1 ),
  755. /* ODD */ PACK( 1, 1 ),
  756. /* EVEN */ PACK( 1, 1 ),
  757. /* IF */ PACK( 1, 0 ),
  758. /* EIF */ PACK( 0, 0 ),
  759. /* AND */ PACK( 2, 1 ),
  760. /* OR */ PACK( 2, 1 ),
  761. /* NOT */ PACK( 1, 1 ),
  762. /* DeltaP1 */ PACK( 1, 0 ),
  763. /* SDB */ PACK( 1, 0 ),
  764. /* SDS */ PACK( 1, 0 ),
  765. /* ADD */ PACK( 2, 1 ),
  766. /* SUB */ PACK( 2, 1 ),
  767. /* DIV */ PACK( 2, 1 ),
  768. /* MUL */ PACK( 2, 1 ),
  769. /* ABS */ PACK( 1, 1 ),
  770. /* NEG */ PACK( 1, 1 ),
  771. /* FLOOR */ PACK( 1, 1 ),
  772. /* CEILING */ PACK( 1, 1 ),
  773. /* ROUND[0] */ PACK( 1, 1 ),
  774. /* ROUND[1] */ PACK( 1, 1 ),
  775. /* ROUND[2] */ PACK( 1, 1 ),
  776. /* ROUND[3] */ PACK( 1, 1 ),
  777. /* NROUND[0] */ PACK( 1, 1 ),
  778. /* NROUND[1] */ PACK( 1, 1 ),
  779. /* NROUND[2] */ PACK( 1, 1 ),
  780. /* NROUND[3] */ PACK( 1, 1 ),
  781. /* WCvtF */ PACK( 2, 0 ),
  782. /* DeltaP2 */ PACK( 1, 0 ),
  783. /* DeltaP3 */ PACK( 1, 0 ),
  784. /* DeltaCn[0] */ PACK( 1, 0 ),
  785. /* DeltaCn[1] */ PACK( 1, 0 ),
  786. /* DeltaCn[2] */ PACK( 1, 0 ),
  787. /* SROUND */ PACK( 1, 0 ),
  788. /* S45Round */ PACK( 1, 0 ),
  789. /* JROT */ PACK( 2, 0 ),
  790. /* JROF */ PACK( 2, 0 ),
  791. /* ROFF */ PACK( 0, 0 ),
  792. /* INS_$7B */ PACK( 0, 0 ),
  793. /* RUTG */ PACK( 0, 0 ),
  794. /* RDTG */ PACK( 0, 0 ),
  795. /* SANGW */ PACK( 1, 0 ),
  796. /* AA */ PACK( 1, 0 ),
  797. /* FlipPT */ PACK( 0, 0 ),
  798. /* FlipRgON */ PACK( 2, 0 ),
  799. /* FlipRgOFF */ PACK( 2, 0 ),
  800. /* INS_$83 */ PACK( 0, 0 ),
  801. /* INS_$84 */ PACK( 0, 0 ),
  802. /* ScanCTRL */ PACK( 1, 0 ),
  803. /* SDVPTL[0] */ PACK( 2, 0 ),
  804. /* SDVPTL[1] */ PACK( 2, 0 ),
  805. /* GetINFO */ PACK( 1, 1 ),
  806. /* IDEF */ PACK( 1, 0 ),
  807. /* ROLL */ PACK( 3, 3 ),
  808. /* MAX */ PACK( 2, 1 ),
  809. /* MIN */ PACK( 2, 1 ),
  810. /* ScanTYPE */ PACK( 1, 0 ),
  811. /* InstCTRL */ PACK( 2, 0 ),
  812. /* INS_$8F */ PACK( 0, 0 ),
  813. /* INS_$90 */ PACK( 0, 0 ),
  814. /* INS_$91 */ PACK( 0, 0 ),
  815. /* INS_$92 */ PACK( 0, 0 ),
  816. /* INS_$93 */ PACK( 0, 0 ),
  817. /* INS_$94 */ PACK( 0, 0 ),
  818. /* INS_$95 */ PACK( 0, 0 ),
  819. /* INS_$96 */ PACK( 0, 0 ),
  820. /* INS_$97 */ PACK( 0, 0 ),
  821. /* INS_$98 */ PACK( 0, 0 ),
  822. /* INS_$99 */ PACK( 0, 0 ),
  823. /* INS_$9A */ PACK( 0, 0 ),
  824. /* INS_$9B */ PACK( 0, 0 ),
  825. /* INS_$9C */ PACK( 0, 0 ),
  826. /* INS_$9D */ PACK( 0, 0 ),
  827. /* INS_$9E */ PACK( 0, 0 ),
  828. /* INS_$9F */ PACK( 0, 0 ),
  829. /* INS_$A0 */ PACK( 0, 0 ),
  830. /* INS_$A1 */ PACK( 0, 0 ),
  831. /* INS_$A2 */ PACK( 0, 0 ),
  832. /* INS_$A3 */ PACK( 0, 0 ),
  833. /* INS_$A4 */ PACK( 0, 0 ),
  834. /* INS_$A5 */ PACK( 0, 0 ),
  835. /* INS_$A6 */ PACK( 0, 0 ),
  836. /* INS_$A7 */ PACK( 0, 0 ),
  837. /* INS_$A8 */ PACK( 0, 0 ),
  838. /* INS_$A9 */ PACK( 0, 0 ),
  839. /* INS_$AA */ PACK( 0, 0 ),
  840. /* INS_$AB */ PACK( 0, 0 ),
  841. /* INS_$AC */ PACK( 0, 0 ),
  842. /* INS_$AD */ PACK( 0, 0 ),
  843. /* INS_$AE */ PACK( 0, 0 ),
  844. /* INS_$AF */ PACK( 0, 0 ),
  845. /* PushB[0] */ PACK( 0, 1 ),
  846. /* PushB[1] */ PACK( 0, 2 ),
  847. /* PushB[2] */ PACK( 0, 3 ),
  848. /* PushB[3] */ PACK( 0, 4 ),
  849. /* PushB[4] */ PACK( 0, 5 ),
  850. /* PushB[5] */ PACK( 0, 6 ),
  851. /* PushB[6] */ PACK( 0, 7 ),
  852. /* PushB[7] */ PACK( 0, 8 ),
  853. /* PushW[0] */ PACK( 0, 1 ),
  854. /* PushW[1] */ PACK( 0, 2 ),
  855. /* PushW[2] */ PACK( 0, 3 ),
  856. /* PushW[3] */ PACK( 0, 4 ),
  857. /* PushW[4] */ PACK( 0, 5 ),
  858. /* PushW[5] */ PACK( 0, 6 ),
  859. /* PushW[6] */ PACK( 0, 7 ),
  860. /* PushW[7] */ PACK( 0, 8 ),
  861. /* MDRP[00] */ PACK( 1, 0 ),
  862. /* MDRP[01] */ PACK( 1, 0 ),
  863. /* MDRP[02] */ PACK( 1, 0 ),
  864. /* MDRP[03] */ PACK( 1, 0 ),
  865. /* MDRP[04] */ PACK( 1, 0 ),
  866. /* MDRP[05] */ PACK( 1, 0 ),
  867. /* MDRP[06] */ PACK( 1, 0 ),
  868. /* MDRP[07] */ PACK( 1, 0 ),
  869. /* MDRP[08] */ PACK( 1, 0 ),
  870. /* MDRP[09] */ PACK( 1, 0 ),
  871. /* MDRP[10] */ PACK( 1, 0 ),
  872. /* MDRP[11] */ PACK( 1, 0 ),
  873. /* MDRP[12] */ PACK( 1, 0 ),
  874. /* MDRP[13] */ PACK( 1, 0 ),
  875. /* MDRP[14] */ PACK( 1, 0 ),
  876. /* MDRP[15] */ PACK( 1, 0 ),
  877. /* MDRP[16] */ PACK( 1, 0 ),
  878. /* MDRP[17] */ PACK( 1, 0 ),
  879. /* MDRP[18] */ PACK( 1, 0 ),
  880. /* MDRP[19] */ PACK( 1, 0 ),
  881. /* MDRP[20] */ PACK( 1, 0 ),
  882. /* MDRP[21] */ PACK( 1, 0 ),
  883. /* MDRP[22] */ PACK( 1, 0 ),
  884. /* MDRP[23] */ PACK( 1, 0 ),
  885. /* MDRP[24] */ PACK( 1, 0 ),
  886. /* MDRP[25] */ PACK( 1, 0 ),
  887. /* MDRP[26] */ PACK( 1, 0 ),
  888. /* MDRP[27] */ PACK( 1, 0 ),
  889. /* MDRP[28] */ PACK( 1, 0 ),
  890. /* MDRP[29] */ PACK( 1, 0 ),
  891. /* MDRP[30] */ PACK( 1, 0 ),
  892. /* MDRP[31] */ PACK( 1, 0 ),
  893. /* MIRP[00] */ PACK( 2, 0 ),
  894. /* MIRP[01] */ PACK( 2, 0 ),
  895. /* MIRP[02] */ PACK( 2, 0 ),
  896. /* MIRP[03] */ PACK( 2, 0 ),
  897. /* MIRP[04] */ PACK( 2, 0 ),
  898. /* MIRP[05] */ PACK( 2, 0 ),
  899. /* MIRP[06] */ PACK( 2, 0 ),
  900. /* MIRP[07] */ PACK( 2, 0 ),
  901. /* MIRP[08] */ PACK( 2, 0 ),
  902. /* MIRP[09] */ PACK( 2, 0 ),
  903. /* MIRP[10] */ PACK( 2, 0 ),
  904. /* MIRP[11] */ PACK( 2, 0 ),
  905. /* MIRP[12] */ PACK( 2, 0 ),
  906. /* MIRP[13] */ PACK( 2, 0 ),
  907. /* MIRP[14] */ PACK( 2, 0 ),
  908. /* MIRP[15] */ PACK( 2, 0 ),
  909. /* MIRP[16] */ PACK( 2, 0 ),
  910. /* MIRP[17] */ PACK( 2, 0 ),
  911. /* MIRP[18] */ PACK( 2, 0 ),
  912. /* MIRP[19] */ PACK( 2, 0 ),
  913. /* MIRP[20] */ PACK( 2, 0 ),
  914. /* MIRP[21] */ PACK( 2, 0 ),
  915. /* MIRP[22] */ PACK( 2, 0 ),
  916. /* MIRP[23] */ PACK( 2, 0 ),
  917. /* MIRP[24] */ PACK( 2, 0 ),
  918. /* MIRP[25] */ PACK( 2, 0 ),
  919. /* MIRP[26] */ PACK( 2, 0 ),
  920. /* MIRP[27] */ PACK( 2, 0 ),
  921. /* MIRP[28] */ PACK( 2, 0 ),
  922. /* MIRP[29] */ PACK( 2, 0 ),
  923. /* MIRP[30] */ PACK( 2, 0 ),
  924. /* MIRP[31] */ PACK( 2, 0 )
  925. };
  926. static
  927. const FT_Char opcode_length[256] =
  928. {
  929. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  930. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  931. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  932. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  933. -1,-2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  934. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  935. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  936. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  937. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  938. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  939. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  940. 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 7, 9, 11,13,15,17,
  941. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  942. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  943. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  944. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  945. };
  946. #undef PACK
  947. #if 1
  948. static FT_Int32
  949. TT_MulFix14( FT_Int32 a,
  950. FT_Int b )
  951. {
  952. FT_Int32 sign;
  953. FT_UInt32 ah, al, mid, lo, hi;
  954. sign = a ^ b;
  955. if ( a < 0 )
  956. a = -a;
  957. if ( b < 0 )
  958. b = -b;
  959. ah = (FT_UInt32)( ( a >> 16 ) & 0xFFFFU );
  960. al = (FT_UInt32)( a & 0xFFFFU );
  961. lo = al * b;
  962. mid = ah * b;
  963. hi = mid >> 16;
  964. mid = ( mid << 16 ) + ( 1 << 13 ); /* rounding */
  965. lo += mid;
  966. if ( lo < mid )
  967. hi += 1;
  968. mid = ( lo >> 14 ) | ( hi << 18 );
  969. return sign >= 0 ? (FT_Int32)mid : -(FT_Int32)mid;
  970. }
  971. #else
  972. /* compute (a*b)/2^14 with maximal accuracy and rounding */
  973. static FT_Int32
  974. TT_MulFix14( FT_Int32 a,
  975. FT_Int b )
  976. {
  977. FT_Int32 m, s, hi;
  978. FT_UInt32 l, lo;
  979. /* compute ax*bx as 64-bit value */
  980. l = (FT_UInt32)( ( a & 0xFFFFU ) * b );
  981. m = ( a >> 16 ) * b;
  982. lo = l + (FT_UInt32)( m << 16 );
  983. hi = ( m >> 16 ) + ( (FT_Int32)l >> 31 ) + ( lo < l );
  984. /* divide the result by 2^14 with rounding */
  985. s = hi >> 31;
  986. l = lo + (FT_UInt32)s;
  987. hi += s + ( l < lo );
  988. lo = l;
  989. l = lo + 0x2000U;
  990. hi += l < lo;
  991. return ( hi << 18 ) | ( l >> 14 );
  992. }
  993. #endif
  994. /* compute (ax*bx+ay*by)/2^14 with maximal accuracy and rounding */
  995. static FT_Int32
  996. TT_DotFix14( FT_Int32 ax,
  997. FT_Int32 ay,
  998. FT_Int bx,
  999. FT_Int by )
  1000. {
  1001. FT_Int32 m, s, hi1, hi2, hi;
  1002. FT_UInt32 l, lo1, lo2, lo;
  1003. /* compute ax*bx as 64-bit value */
  1004. l = (FT_UInt32)( ( ax & 0xFFFFU ) * bx );
  1005. m = ( ax >> 16 ) * bx;
  1006. lo1 = l + (FT_UInt32)( m << 16 );
  1007. hi1 = ( m >> 16 ) + ( (FT_Int32)l >> 31 ) + ( lo1 < l );
  1008. /* compute ay*by as 64-bit value */
  1009. l = (FT_UInt32)( ( ay & 0xFFFFU ) * by );
  1010. m = ( ay >> 16 ) * by;
  1011. lo2 = l + (FT_UInt32)( m << 16 );
  1012. hi2 = ( m >> 16 ) + ( (FT_Int32)l >> 31 ) + ( lo2 < l );
  1013. /* add them */
  1014. lo = lo1 + lo2;
  1015. hi = hi1 + hi2 + ( lo < lo1 );
  1016. /* divide the result by 2^14 with rounding */
  1017. s = hi >> 31;
  1018. l = lo + (FT_UInt32)s;
  1019. hi += s + ( l < lo );
  1020. lo = l;
  1021. l = lo + 0x2000U;
  1022. hi += ( l < lo );
  1023. return ( hi << 18 ) | ( l >> 14 );
  1024. }
  1025. /* return length of given vector */
  1026. #if 0
  1027. static FT_Int32
  1028. TT_VecLen( FT_Int32 x,
  1029. FT_Int32 y )
  1030. {
  1031. FT_Int32 m, hi1, hi2, hi;
  1032. FT_UInt32 l, lo1, lo2, lo;
  1033. /* compute x*x as 64-bit value */
  1034. lo = (FT_UInt32)( x & 0xFFFFU );
  1035. hi = x >> 16;
  1036. l = lo * lo;
  1037. m = hi * lo;
  1038. hi = hi * hi;
  1039. lo1 = l + (FT_UInt32)( m << 17 );
  1040. hi1 = hi + ( m >> 15 ) + ( lo1 < l );
  1041. /* compute y*y as 64-bit value */
  1042. lo = (FT_UInt32)( y & 0xFFFFU );
  1043. hi = y >> 16;
  1044. l = lo * lo;
  1045. m = hi * lo;
  1046. hi = hi * hi;
  1047. lo2 = l + (FT_UInt32)( m << 17 );
  1048. hi2 = hi + ( m >> 15 ) + ( lo2 < l );
  1049. /* add them to get 'x*x+y*y' as 64-bit value */
  1050. lo = lo1 + lo2;
  1051. hi = hi1 + hi2 + ( lo < lo1 );
  1052. /* compute the square root of this value */
  1053. {
  1054. FT_UInt32 root, rem, test_div;
  1055. FT_Int count;
  1056. root = 0;
  1057. {
  1058. rem = 0;
  1059. count = 32;
  1060. do
  1061. {
  1062. rem = ( rem << 2 ) | ( (FT_UInt32)hi >> 30 );
  1063. hi = ( hi << 2 ) | ( lo >> 30 );
  1064. lo <<= 2;
  1065. root <<= 1;
  1066. test_div = ( root << 1 ) + 1;
  1067. if ( rem >= test_div )
  1068. {
  1069. rem -= test_div;
  1070. root += 1;
  1071. }
  1072. } while ( --count );
  1073. }
  1074. return (FT_Int32)root;
  1075. }
  1076. }
  1077. #else
  1078. /* this version uses FT_Vector_Length which computes the same value */
  1079. /* much, much faster.. */
  1080. /* */
  1081. static FT_F26Dot6
  1082. TT_VecLen( FT_F26Dot6 X,
  1083. FT_F26Dot6 Y )
  1084. {
  1085. FT_Vector v;
  1086. v.x = X;
  1087. v.y = Y;
  1088. return FT_Vector_Length( &v );
  1089. }
  1090. #endif
  1091. /*************************************************************************/
  1092. /* */
  1093. /* <Function> */
  1094. /* Current_Ratio */
  1095. /* */
  1096. /* <Description> */
  1097. /* Returns the current aspect ratio scaling factor depending on the */
  1098. /* projection vector's state and device resolutions. */
  1099. /* */
  1100. /* <Return> */
  1101. /* The aspect ratio in 16.16 format, always <= 1.0 . */
  1102. /* */
  1103. static FT_Long
  1104. Current_Ratio( EXEC_OP )
  1105. {
  1106. if ( !CUR.tt_metrics.ratio )
  1107. {
  1108. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  1109. if ( CUR.face->unpatented_hinting )
  1110. {
  1111. if ( CUR.GS.both_x_axis )
  1112. CUR.tt_metrics.ratio = CUR.tt_metrics.x_ratio;
  1113. else
  1114. CUR.tt_metrics.ratio = CUR.tt_metrics.y_ratio;
  1115. }
  1116. else
  1117. #endif
  1118. {
  1119. if ( CUR.GS.projVector.y == 0 )
  1120. CUR.tt_metrics.ratio = CUR.tt_metrics.x_ratio;
  1121. else if ( CUR.GS.projVector.x == 0 )
  1122. CUR.tt_metrics.ratio = CUR.tt_metrics.y_ratio;
  1123. else
  1124. {
  1125. FT_Long x, y;
  1126. x = TT_MULDIV( CUR.GS.projVector.x,
  1127. CUR.tt_metrics.x_ratio, 0x4000 );
  1128. y = TT_MULDIV( CUR.GS.projVector.y,
  1129. CUR.tt_metrics.y_ratio, 0x4000 );
  1130. CUR.tt_metrics.ratio = TT_VecLen( x, y );
  1131. }
  1132. }
  1133. }
  1134. return CUR.tt_metrics.ratio;
  1135. }
  1136. static FT_Long
  1137. Current_Ppem( EXEC_OP )
  1138. {
  1139. return TT_MULFIX( CUR.tt_metrics.ppem, CURRENT_Ratio() );
  1140. }
  1141. /*************************************************************************/
  1142. /* */
  1143. /* Functions related to the control value table (CVT). */
  1144. /* */
  1145. /*************************************************************************/
  1146. FT_CALLBACK_DEF( FT_F26Dot6 )
  1147. Read_CVT( EXEC_OP_ FT_ULong idx )
  1148. {
  1149. return CUR.cvt[idx];
  1150. }
  1151. FT_CALLBACK_DEF( FT_F26Dot6 )
  1152. Read_CVT_Stretched( EXEC_OP_ FT_ULong idx )
  1153. {
  1154. return TT_MULFIX( CUR.cvt[idx], CURRENT_Ratio() );
  1155. }
  1156. FT_CALLBACK_DEF( void )
  1157. Write_CVT( EXEC_OP_ FT_ULong idx,
  1158. FT_F26Dot6 value )
  1159. {
  1160. CUR.cvt[idx] = value;
  1161. }
  1162. FT_CALLBACK_DEF( void )
  1163. Write_CVT_Stretched( EXEC_OP_ FT_ULong idx,
  1164. FT_F26Dot6 value )
  1165. {
  1166. CUR.cvt[idx] = FT_DivFix( value, CURRENT_Ratio() );
  1167. }
  1168. FT_CALLBACK_DEF( void )
  1169. Move_CVT( EXEC_OP_ FT_ULong idx,
  1170. FT_F26Dot6 value )
  1171. {
  1172. CUR.cvt[idx] += value;
  1173. }
  1174. FT_CALLBACK_DEF( void )
  1175. Move_CVT_Stretched( EXEC_OP_ FT_ULong idx,
  1176. FT_F26Dot6 value )
  1177. {
  1178. CUR.cvt[idx] += FT_DivFix( value, CURRENT_Ratio() );
  1179. }
  1180. /*************************************************************************/
  1181. /* */
  1182. /* <Function> */
  1183. /* GetShortIns */
  1184. /* */
  1185. /* <Description> */
  1186. /* Returns a short integer taken from the instruction stream at */
  1187. /* address IP. */
  1188. /* */
  1189. /* <Return> */
  1190. /* Short read at code[IP]. */
  1191. /* */
  1192. /* <Note> */
  1193. /* This one could become a macro. */
  1194. /* */
  1195. static FT_Short
  1196. GetShortIns( EXEC_OP )
  1197. {
  1198. /* Reading a byte stream so there is no endianess (DaveP) */
  1199. CUR.IP += 2;
  1200. return (FT_Short)( ( CUR.code[CUR.IP - 2] << 8 ) +
  1201. CUR.code[CUR.IP - 1] );
  1202. }
  1203. /*************************************************************************/
  1204. /* */
  1205. /* <Function> */
  1206. /* Ins_Goto_CodeRange */
  1207. /* */
  1208. /* <Description> */
  1209. /* Goes to a certain code range in the instruction stream. */
  1210. /* */
  1211. /* <Input> */
  1212. /* aRange :: The index of the code range. */
  1213. /* */
  1214. /* aIP :: The new IP address in the code range. */
  1215. /* */
  1216. /* <Return> */
  1217. /* SUCCESS or FAILURE. */
  1218. /* */
  1219. static FT_Bool
  1220. Ins_Goto_CodeRange( EXEC_OP_ FT_Int aRange,
  1221. FT_ULong aIP )
  1222. {
  1223. TT_CodeRange* range;
  1224. if ( aRange < 1 || aRange > 3 )
  1225. {
  1226. CUR.error = TT_Err_Bad_Argument;
  1227. return FAILURE;
  1228. }
  1229. range = &CUR.codeRangeTable[aRange - 1];
  1230. if ( range->base == NULL ) /* invalid coderange */
  1231. {
  1232. CUR.error = TT_Err_Invalid_CodeRange;
  1233. return FAILURE;
  1234. }
  1235. /* NOTE: Because the last instruction of a program may be a CALL */
  1236. /* which will return to the first byte *after* the code */
  1237. /* range, we test for AIP <= Size, instead of AIP < Size. */
  1238. if ( aIP > range->size )
  1239. {
  1240. CUR.error = TT_Err_Code_Overflow;
  1241. return FAILURE;
  1242. }
  1243. CUR.code = range->base;
  1244. CUR.codeSize = range->size;
  1245. CUR.IP = aIP;
  1246. CUR.curRange = aRange;
  1247. return SUCCESS;
  1248. }
  1249. /*************************************************************************/
  1250. /* */
  1251. /* <Function> */
  1252. /* Direct_Move */
  1253. /* */
  1254. /* <Description> */
  1255. /* Moves a point by a given distance along the freedom vector. The */
  1256. /* point will be `touched'. */
  1257. /* */
  1258. /* <Input> */
  1259. /* point :: The index of the point to move. */
  1260. /* */
  1261. /* distance :: The distance to apply. */
  1262. /* */
  1263. /* <InOut> */
  1264. /* zone :: The affected glyph zone. */
  1265. /* */
  1266. static void
  1267. Direct_Move( EXEC_OP_ TT_GlyphZone zone,
  1268. FT_UShort point,
  1269. FT_F26Dot6 distance )
  1270. {
  1271. FT_F26Dot6 v;
  1272. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  1273. FT_ASSERT( !CUR.face->unpatented_hinting );
  1274. #endif
  1275. v = CUR.GS.freeVector.x;
  1276. if ( v != 0 )
  1277. {
  1278. zone->cur[point].x += TT_MULDIV( distance,
  1279. v * 0x10000L,
  1280. CUR.F_dot_P );
  1281. zone->tags[point] |= FT_CURVE_TAG_TOUCH_X;
  1282. }
  1283. v = CUR.GS.freeVector.y;
  1284. if ( v != 0 )
  1285. {
  1286. zone->cur[point].y += TT_MULDIV( distance,
  1287. v * 0x10000L,
  1288. CUR.F_dot_P );
  1289. zone->tags[point] |= FT_CURVE_TAG_TOUCH_Y;
  1290. }
  1291. }
  1292. /*************************************************************************/
  1293. /* */
  1294. /* <Function> */
  1295. /* Direct_Move_Orig */
  1296. /* */
  1297. /* <Description> */
  1298. /* Moves the *original* position of a point by a given distance along */
  1299. /* the freedom vector. Obviously, the point will not be `touched'. */
  1300. /* */
  1301. /* <Input> */
  1302. /* point :: The index of the point to move. */
  1303. /* */
  1304. /* distance :: The distance to apply. */
  1305. /* */
  1306. /* <InOut> */
  1307. /* zone :: The affected glyph zone. */
  1308. /* */
  1309. static void
  1310. Direct_Move_Orig( EXEC_OP_ TT_GlyphZone zone,
  1311. FT_UShort point,
  1312. FT_F26Dot6 distance )
  1313. {
  1314. FT_F26Dot6 v;
  1315. #ifdef TT_CONFIG_OPTION_UNPATENTED_HINTING
  1316. FT_ASSERT( !CUR.face->unpatented_hinting );
  1317. #endif
  1318. v = CUR.GS.freeVector.x;
  1319. if ( v != 0 )
  1320. zone->org[point].x += TT_MULDIV( distance,
  1321. v * 0x10000L,
  1322. CUR.F_dot_P );
  1323. v = CUR.GS.freeVector.y;
  1324. if ( v != 0 )
  1325. zone->org[point].y += TT_MULDIV( distance,
  1326. v * 0x10000L,
  1327. CUR.F_dot_P );
  1328. }
  1329. /*************************************************************************/
  1330. /* */
  1331. /* Special versions of Direct_Move() */
  1332. /* */
  1333. /* The following versions are used whenever both vectors are both */
  1334. /* along one of the coordinate unit vectors, i.e. in 90% of the cases. */
  1335. /* */
  1336. /*************************************************************************/
  1337. static void
  1338. Direct_Move_X( EXEC_OP_ TT_GlyphZone zone,
  1339. FT_UShort point,
  1340. FT_F26Dot6 distance )
  1341. {
  1342. FT_UNUSED_EXEC;
  1343. zone->cur[point].x += distance;
  1344. zone->tags[point] |= FT_CURVE_TAG_TOUCH_X;
  1345. }
  1346. static void
  1347. Direct_Move_Y( EXEC_OP_ TT_GlyphZone zone,
  1348. FT_UShort point,
  1349. FT_F26Dot6 distance )
  1350. {
  1351. FT_UNUSED_EXEC;
  1352. zone->cur[point].y += distance;
  1353. zone->tags[point] |= FT_CURVE_TAG_TOUCH_Y;
  1354. }
  1355. /*************************************************************************/
  1356. /* */
  1357. /* Special versions of Direct_Move_Orig() */
  1358. /* */
  1359. /* The following versions are used whenever both vectors are both */
  1360. /* along one of the coordinate unit vectors, i.e. in 90% of the cases. */
  1361. /* */
  1362. /*************************************************************************/
  1363. static void
  1364. Direct_Move_Orig_X( EXEC_OP_ TT_GlyphZone zone,
  1365. FT_UShort point,
  1366. FT_F26Dot6 distance )
  1367. {
  1368. FT_UNUSED_EXEC;
  1369. zone->org[point].x += distance;
  1370. }
  1371. static void
  1372. Direct_Move_Orig_Y( EXEC_OP_ TT_GlyphZone zone,
  1373. FT_UShort point,
  1374. FT_F26Dot6 distance )
  1375. {
  1376. FT_UNUSED_EXEC;
  1377. zone->org[point].y += distance;
  1378. }
  1379. /*************************************************************************/
  1380. /* */
  1381. /* <Function> */
  1382. /* Round_None */
  1383. /* */
  1384. /* <Description> */
  1385. /* Does not round, but adds engine compensation. */
  1386. /* */
  1387. /* <Input> */
  1388. /* distance :: The distance (not) to round. */
  1389. /* */
  1390. /* compensation :: The engine compensation. */
  1391. /* */
  1392. /* <Return> */
  1393. /* The compensated distance. */
  1394. /* */
  1395. /* <Note> */
  1396. /* The TrueType specification says very few about the relationship */
  1397. /* between rounding and engine compensation. However, it seems from */
  1398. /* the description of super round that we should add the compensation */
  1399. /* before rounding.