cffgload.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  1. /***************************************************************************/
  2. /* */
  3. /* cffgload.c */
  4. /* */
  5. /* OpenType Glyph Loader (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002 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_OUTLINE_H
  23. #include FT_TRUETYPE_TAGS_H
  24. #include FT_INTERNAL_POSTSCRIPT_HINTS_H
  25. #include "cffobjs.h"
  26. #include "cffload.h"
  27. #include "cffgload.h"
  28. #include "cfferrs.h"
  29. /*************************************************************************/
  30. /* */
  31. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  32. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  33. /* messages during execution. */
  34. /* */
  35. #undef FT_COMPONENT
  36. #define FT_COMPONENT trace_cffgload
  37. typedef enum CFF_Operator_
  38. {
  39. cff_op_unknown = 0,
  40. cff_op_rmoveto,
  41. cff_op_hmoveto,
  42. cff_op_vmoveto,
  43. cff_op_rlineto,
  44. cff_op_hlineto,
  45. cff_op_vlineto,
  46. cff_op_rrcurveto,
  47. cff_op_hhcurveto,
  48. cff_op_hvcurveto,
  49. cff_op_rcurveline,
  50. cff_op_rlinecurve,
  51. cff_op_vhcurveto,
  52. cff_op_vvcurveto,
  53. cff_op_flex,
  54. cff_op_hflex,
  55. cff_op_hflex1,
  56. cff_op_flex1,
  57. cff_op_endchar,
  58. cff_op_hstem,
  59. cff_op_vstem,
  60. cff_op_hstemhm,
  61. cff_op_vstemhm,
  62. cff_op_hintmask,
  63. cff_op_cntrmask,
  64. cff_op_dotsection, /* deprecated, acts as no-op */
  65. cff_op_abs,
  66. cff_op_add,
  67. cff_op_sub,
  68. cff_op_div,
  69. cff_op_neg,
  70. cff_op_random,
  71. cff_op_mul,
  72. cff_op_sqrt,
  73. cff_op_blend,
  74. cff_op_drop,
  75. cff_op_exch,
  76. cff_op_index,
  77. cff_op_roll,
  78. cff_op_dup,
  79. cff_op_put,
  80. cff_op_get,
  81. cff_op_store,
  82. cff_op_load,
  83. cff_op_and,
  84. cff_op_or,
  85. cff_op_not,
  86. cff_op_eq,
  87. cff_op_ifelse,
  88. cff_op_callsubr,
  89. cff_op_callgsubr,
  90. cff_op_return,
  91. /* do not remove */
  92. cff_op_max
  93. } CFF_Operator;
  94. #define CFF_COUNT_CHECK_WIDTH 0x80
  95. #define CFF_COUNT_EXACT 0x40
  96. #define CFF_COUNT_CLEAR_STACK 0x20
  97. static const FT_Byte cff_argument_counts[] =
  98. {
  99. 0, /* unknown */
  100. 2 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT, /* rmoveto */
  101. 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
  102. 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
  103. 0 | CFF_COUNT_CLEAR_STACK, /* rlineto */
  104. 0 | CFF_COUNT_CLEAR_STACK,
  105. 0 | CFF_COUNT_CLEAR_STACK,
  106. 0 | CFF_COUNT_CLEAR_STACK, /* rrcurveto */
  107. 0 | CFF_COUNT_CLEAR_STACK,
  108. 0 | CFF_COUNT_CLEAR_STACK,
  109. 0 | CFF_COUNT_CLEAR_STACK,
  110. 0 | CFF_COUNT_CLEAR_STACK,
  111. 0 | CFF_COUNT_CLEAR_STACK,
  112. 0 | CFF_COUNT_CLEAR_STACK,
  113. 13, /* flex */
  114. 7,
  115. 9,
  116. 11,
  117. 0 | CFF_COUNT_CHECK_WIDTH, /* endchar */
  118. 2 | CFF_COUNT_CHECK_WIDTH, /* hstem */
  119. 2 | CFF_COUNT_CHECK_WIDTH,
  120. 2 | CFF_COUNT_CHECK_WIDTH,
  121. 2 | CFF_COUNT_CHECK_WIDTH,
  122. 0 | CFF_COUNT_CHECK_WIDTH, /* hintmask */
  123. 0 | CFF_COUNT_CHECK_WIDTH, /* cntrmask */
  124. 0, /* dotsection */
  125. 1, /* abs */
  126. 2,
  127. 2,
  128. 2,
  129. 1,
  130. 0,
  131. 2,
  132. 1,
  133. 1, /* blend */
  134. 1, /* drop */
  135. 2,
  136. 1,
  137. 2,
  138. 1,
  139. 2, /* put */
  140. 1,
  141. 4,
  142. 3,
  143. 2, /* and */
  144. 2,
  145. 1,
  146. 2,
  147. 4,
  148. 1, /* callsubr */
  149. 1,
  150. 0
  151. };
  152. /*************************************************************************/
  153. /*************************************************************************/
  154. /*************************************************************************/
  155. /********** *********/
  156. /********** *********/
  157. /********** GENERIC CHARSTRING PARSING *********/
  158. /********** *********/
  159. /********** *********/
  160. /*************************************************************************/
  161. /*************************************************************************/
  162. /*************************************************************************/
  163. /*************************************************************************/
  164. /* */
  165. /* <Function> */
  166. /* cff_builder_init */
  167. /* */
  168. /* <Description> */
  169. /* Initializes a given glyph builder. */
  170. /* */
  171. /* <InOut> */
  172. /* builder :: A pointer to the glyph builder to initialize. */
  173. /* */
  174. /* <Input> */
  175. /* face :: The current face object. */
  176. /* */
  177. /* size :: The current size object. */
  178. /* */
  179. /* glyph :: The current glyph object. */
  180. /* */
  181. static void
  182. cff_builder_init( CFF_Builder* builder,
  183. TT_Face face,
  184. CFF_Size size,
  185. CFF_GlyphSlot glyph,
  186. FT_Bool hinting )
  187. {
  188. builder->path_begun = 0;
  189. builder->load_points = 1;
  190. builder->face = face;
  191. builder->glyph = glyph;
  192. builder->memory = face->root.memory;
  193. if ( glyph )
  194. {
  195. FT_GlyphLoader loader = glyph->root.internal->loader;
  196. builder->loader = loader;
  197. builder->base = &loader->base.outline;
  198. builder->current = &loader->current.outline;
  199. FT_GlyphLoader_Rewind( loader );
  200. builder->hint_flags = FT_FACE(face)->internal->hint_flags;
  201. builder->hints_globals = 0;
  202. builder->hints_funcs = 0;
  203. if ( hinting && size )
  204. {
  205. builder->hints_globals = size->internal;
  206. builder->hints_funcs = glyph->root.internal->glyph_hints;
  207. }
  208. }
  209. if ( size )
  210. {
  211. builder->scale_x = size->metrics.x_scale;
  212. builder->scale_y = size->metrics.y_scale;
  213. }
  214. builder->pos_x = 0;
  215. builder->pos_y = 0;
  216. builder->left_bearing.x = 0;
  217. builder->left_bearing.y = 0;
  218. builder->advance.x = 0;
  219. builder->advance.y = 0;
  220. }
  221. /*************************************************************************/
  222. /* */
  223. /* <Function> */
  224. /* cff_builder_done */
  225. /* */
  226. /* <Description> */
  227. /* Finalizes a given glyph builder. Its contents can still be used */
  228. /* after the call, but the function saves important information */
  229. /* within the corresponding glyph slot. */
  230. /* */
  231. /* <Input> */
  232. /* builder :: A pointer to the glyph builder to finalize. */
  233. /* */
  234. static void
  235. cff_builder_done( CFF_Builder* builder )
  236. {
  237. CFF_GlyphSlot glyph = builder->glyph;
  238. if ( glyph )
  239. glyph->root.outline = *builder->base;
  240. }
  241. /*************************************************************************/
  242. /* */
  243. /* <Function> */
  244. /* cff_compute_bias */
  245. /* */
  246. /* <Description> */
  247. /* Computes the bias value in dependence of the number of glyph */
  248. /* subroutines. */
  249. /* */
  250. /* <Input> */
  251. /* num_subrs :: The number of glyph subroutines. */
  252. /* */
  253. /* <Return> */
  254. /* The bias value. */
  255. static FT_Int
  256. cff_compute_bias( FT_UInt num_subrs )
  257. {
  258. FT_Int result;
  259. if ( num_subrs < 1240 )
  260. result = 107;
  261. else if ( num_subrs < 33900U )
  262. result = 1131;
  263. else
  264. result = 32768U;
  265. return result;
  266. }
  267. /*************************************************************************/
  268. /* */
  269. /* <Function> */
  270. /* cff_decoder_init */
  271. /* */
  272. /* <Description> */
  273. /* Initializes a given glyph decoder. */
  274. /* */
  275. /* <InOut> */
  276. /* decoder :: A pointer to the glyph builder to initialize. */
  277. /* */
  278. /* <Input> */
  279. /* face :: The current face object. */
  280. /* */
  281. /* size :: The current size object. */
  282. /* */
  283. /* slot :: The current glyph object. */
  284. /* */
  285. FT_LOCAL_DEF( void )
  286. cff_decoder_init( CFF_Decoder* decoder,
  287. TT_Face face,
  288. CFF_Size size,
  289. CFF_GlyphSlot slot,
  290. FT_Bool hinting,
  291. FT_Render_Mode hint_mode )
  292. {
  293. CFF_Font cff = (CFF_Font)face->extra.data;
  294. /* clear everything */
  295. FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
  296. /* initialize builder */
  297. cff_builder_init( &decoder->builder, face, size, slot, hinting );
  298. /* initialize Type2 decoder */
  299. decoder->num_globals = cff->num_global_subrs;
  300. decoder->globals = cff->global_subrs;
  301. decoder->globals_bias = cff_compute_bias( decoder->num_globals );
  302. decoder->hint_mode = hint_mode;
  303. }
  304. /* this function is used to select the locals subrs array */
  305. FT_LOCAL_DEF( void )
  306. cff_decoder_prepare( CFF_Decoder* decoder,
  307. FT_UInt glyph_index )
  308. {
  309. CFF_Font cff = (CFF_Font)decoder->builder.face->extra.data;
  310. CFF_SubFont sub = &cff->top_font;
  311. /* manage CID fonts */
  312. if ( cff->num_subfonts >= 1 )
  313. {
  314. FT_Byte fd_index = cff_fd_select_get( &cff->fd_select, glyph_index );
  315. sub = cff->subfonts[fd_index];
  316. }
  317. decoder->num_locals = sub->num_local_subrs;
  318. decoder->locals = sub->local_subrs;
  319. decoder->locals_bias = cff_compute_bias( decoder->num_locals );
  320. decoder->glyph_width = sub->private_dict.default_width;
  321. decoder->nominal_width = sub->private_dict.nominal_width;
  322. }
  323. /* check that there is enough room for `count' more points */
  324. static FT_Error
  325. check_points( CFF_Builder* builder,
  326. FT_Int count )
  327. {
  328. return FT_GlyphLoader_CheckPoints( builder->loader, count, 0 );
  329. }
  330. /* add a new point, do not check space */
  331. static void
  332. cff_builder_add_point( CFF_Builder* builder,
  333. FT_Pos x,
  334. FT_Pos y,
  335. FT_Byte flag )
  336. {
  337. FT_Outline* outline = builder->current;
  338. if ( builder->load_points )
  339. {
  340. FT_Vector* point = outline->points + outline->n_points;
  341. FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
  342. point->x = x >> 16;
  343. point->y = y >> 16;
  344. *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
  345. builder->last = *point;
  346. }
  347. outline->n_points++;
  348. }
  349. /* check space for a new on-curve point, then add it */
  350. static FT_Error
  351. cff_builder_add_point1( CFF_Builder* builder,
  352. FT_Pos x,
  353. FT_Pos y )
  354. {
  355. FT_Error error;
  356. error = check_points( builder, 1 );
  357. if ( !error )
  358. cff_builder_add_point( builder, x, y, 1 );
  359. return error;
  360. }
  361. /* check room for a new contour, then add it */
  362. static FT_Error
  363. cff_builder_add_contour( CFF_Builder* builder )
  364. {
  365. FT_Outline* outline = builder->current;
  366. FT_Error error;
  367. if ( !builder->load_points )
  368. {
  369. outline->n_contours++;
  370. return CFF_Err_Ok;
  371. }
  372. error = FT_GlyphLoader_CheckPoints( builder->loader, 0, 1 );
  373. if ( !error )
  374. {
  375. if ( outline->n_contours > 0 )
  376. outline->contours[outline->n_contours - 1] =
  377. (short)( outline->n_points - 1 );
  378. outline->n_contours++;
  379. }
  380. return error;
  381. }
  382. /* if a path was begun, add its first on-curve point */
  383. static FT_Error
  384. cff_builder_start_point( CFF_Builder* builder,
  385. FT_Pos x,
  386. FT_Pos y )
  387. {
  388. FT_Error error = 0;
  389. /* test whether we are building a new contour */
  390. if ( !builder->path_begun )
  391. {
  392. builder->path_begun = 1;
  393. error = cff_builder_add_contour( builder );
  394. if ( !error )
  395. error = cff_builder_add_point1( builder, x, y );
  396. }
  397. return error;
  398. }
  399. /* close the current contour */
  400. static void
  401. cff_builder_close_contour( CFF_Builder* builder )
  402. {
  403. FT_Outline* outline = builder->current;
  404. /* XXXX: We must not include the last point in the path if it */
  405. /* is located on the first point. */
  406. if ( outline->n_points > 1 )
  407. {
  408. FT_Int first = 0;
  409. FT_Vector* p1 = outline->points + first;
  410. FT_Vector* p2 = outline->points + outline->n_points - 1;
  411. FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
  412. if ( outline->n_contours > 1 )
  413. {
  414. first = outline->contours[outline->n_contours - 2] + 1;
  415. p1 = outline->points + first;
  416. }
  417. /* `delete' last point only if it coincides with the first */
  418. /* point and if it is not a control point (which can happen). */
  419. if ( p1->x == p2->x && p1->y == p2->y )
  420. if ( *control == FT_CURVE_TAG_ON )
  421. outline->n_points--;
  422. }
  423. if ( outline->n_contours > 0 )
  424. outline->contours[outline->n_contours - 1] =
  425. (short)( outline->n_points - 1 );
  426. }
  427. static FT_Int
  428. cff_lookup_glyph_by_stdcharcode( CFF_Font cff,
  429. FT_Int charcode )
  430. {
  431. FT_UInt n;
  432. FT_UShort glyph_sid;
  433. /* check range of standard char code */
  434. if ( charcode < 0 || charcode > 255 )
  435. return -1;
  436. /* Get code to SID mapping from `cff_standard_encoding'. */
  437. glyph_sid = cff_get_standard_encoding( (FT_UInt)charcode );
  438. for ( n = 0; n < cff->num_glyphs; n++ )
  439. {
  440. if ( cff->charset.sids[n] == glyph_sid )
  441. return n;
  442. }
  443. return -1;
  444. }
  445. static FT_Error
  446. cff_get_glyph_data( TT_Face face,
  447. FT_UInt glyph_index,
  448. FT_Byte** pointer,
  449. FT_ULong* length )
  450. {
  451. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  452. /* For incremental fonts get the character data using the */
  453. /* callback function. */
  454. if ( face->root.internal->incremental_interface )
  455. {
  456. FT_Data data;
  457. FT_Error error =
  458. face->root.internal->incremental_interface->funcs->get_glyph_data(
  459. face->root.internal->incremental_interface->object,
  460. glyph_index, &data );
  461. *pointer = (FT_Byte*)data.pointer;
  462. *length = data.length;
  463. return error;
  464. }
  465. else
  466. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  467. {
  468. CFF_Font cff = (CFF_Font)(face->extra.data);
  469. return cff_index_access_element( &cff->charstrings_index, glyph_index,
  470. pointer, length );
  471. }
  472. }
  473. static void
  474. cff_free_glyph_data( TT_Face face,
  475. FT_Byte** pointer,
  476. FT_ULong length )
  477. {
  478. #ifndef FT_CONFIG_OPTION_INCREMENTAL
  479. FT_UNUSED( length );
  480. #endif
  481. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  482. /* For incremental fonts get the character data using the */
  483. /* callback function. */
  484. if ( face->root.internal->incremental_interface )
  485. {
  486. FT_Data data;
  487. data.pointer = *pointer;
  488. data.length = length;
  489. face->root.internal->incremental_interface->funcs->free_glyph_data(
  490. face->root.internal->incremental_interface->object,&data );
  491. }
  492. else
  493. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  494. {
  495. CFF_Font cff = (CFF_Font)(face->extra.data);
  496. cff_index_forget_element( &cff->charstrings_index, pointer );
  497. }
  498. }
  499. static FT_Error
  500. cff_operator_seac( CFF_Decoder* decoder,
  501. FT_Pos adx,
  502. FT_Pos ady,
  503. FT_Int bchar,
  504. FT_Int achar )
  505. {
  506. FT_Error error;
  507. FT_Int bchar_index, achar_index, n_base_points;
  508. FT_Outline* base = decoder->builder.base;
  509. TT_Face face = decoder->builder.face;
  510. FT_Vector left_bearing, advance;
  511. FT_Byte* charstring;
  512. FT_ULong charstring_len;
  513. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  514. /* Incremental fonts don't necessarily have valid charsets. */
  515. /* They use the character code, not the glyph index, in this case. */
  516. if ( face->root.internal->incremental_interface )
  517. {
  518. bchar_index = bchar;
  519. achar_index = achar;
  520. }
  521. else
  522. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  523. {
  524. CFF_Font cff = (CFF_Font)(face->extra.data);
  525. bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
  526. achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
  527. }
  528. if ( bchar_index < 0 || achar_index < 0 )
  529. {
  530. FT_ERROR(( "cff_operator_seac:" ));
  531. FT_ERROR(( " invalid seac character code arguments\n" ));
  532. return CFF_Err_Syntax_Error;
  533. }
  534. /* If we are trying to load a composite glyph, do not load the */
  535. /* accent character and return the array of subglyphs. */
  536. if ( decoder->builder.no_recurse )
  537. {
  538. FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
  539. FT_GlyphLoader loader = glyph->internal->loader;
  540. FT_SubGlyph subg;
  541. /* reallocate subglyph array if necessary */
  542. error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
  543. if ( error )
  544. goto Exit;
  545. subg = loader->current.subglyphs;
  546. /* subglyph 0 = base character */
  547. subg->index = bchar_index;
  548. subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
  549. FT_SUBGLYPH_FLAG_USE_MY_METRICS;
  550. subg->arg1 = 0;
  551. subg->arg2 = 0;
  552. subg++;
  553. /* subglyph 1 = accent character */
  554. subg->index = achar_index;
  555. subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
  556. subg->arg1 = (FT_Int)adx;
  557. subg->arg2 = (FT_Int)ady;
  558. /* set up remaining glyph fields */
  559. glyph->num_subglyphs = 2;
  560. glyph->subglyphs = loader->base.subglyphs;
  561. glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
  562. loader->current.num_subglyphs = 2;
  563. }
  564. /* First load `bchar' in builder */
  565. error = cff_get_glyph_data( face, bchar_index,
  566. &charstring, &charstring_len );
  567. if ( !error )
  568. {
  569. error = cff_decoder_parse_charstrings( decoder, charstring,
  570. charstring_len );
  571. if ( error )
  572. goto Exit;
  573. cff_free_glyph_data( face, &charstring, charstring_len );
  574. }
  575. n_base_points = base->n_points;
  576. /* Save the left bearing and width of the base character */
  577. /* as they will be erased by the next load. */
  578. left_bearing = decoder->builder.left_bearing;
  579. advance = decoder->builder.advance;
  580. decoder->builder.left_bearing.x = 0;
  581. decoder->builder.left_bearing.y = 0;
  582. /* Now load `achar' on top of the base outline. */
  583. error = cff_get_glyph_data( face, achar_index,
  584. &charstring, &charstring_len );
  585. if ( !error )
  586. {
  587. error = cff_decoder_parse_charstrings( decoder, charstring,
  588. charstring_len );
  589. if ( error )
  590. goto Exit;
  591. cff_free_glyph_data( face, &charstring, charstring_len );
  592. }
  593. /* Restore the left side bearing and advance width */
  594. /* of the base character. */
  595. decoder->builder.left_bearing = left_bearing;
  596. decoder->builder.advance = advance;
  597. /* Finally, move the accent. */
  598. if ( decoder->builder.load_points )
  599. {
  600. FT_Outline dummy;
  601. dummy.n_points = (short)( base->n_points - n_base_points );
  602. dummy.points = base->points + n_base_points;
  603. FT_Outline_Translate( &dummy, adx, ady );
  604. }
  605. Exit:
  606. return error;
  607. }
  608. /*************************************************************************/
  609. /* */
  610. /* <Function> */
  611. /* cff_decoder_parse_charstrings */
  612. /* */
  613. /* <Description> */
  614. /* Parses a given Type 2 charstrings program. */
  615. /* */
  616. /* <InOut> */
  617. /* decoder :: The current Type 1 decoder. */
  618. /* */
  619. /* <Input> */
  620. /* charstring_base :: The base of the charstring stream. */
  621. /* */
  622. /* charstring_len :: The length in bytes of the charstring stream. */
  623. /* */
  624. /* <Return> */
  625. /* FreeType error code. 0 means success. */
  626. /* */
  627. FT_LOCAL_DEF( FT_Error )
  628. cff_decoder_parse_charstrings( CFF_Decoder* decoder,
  629. FT_Byte* charstring_base,
  630. FT_ULong charstring_len )
  631. {
  632. FT_Error error;
  633. CFF_Decoder_Zone* zone;
  634. FT_Byte* ip;
  635. FT_Byte* limit;
  636. CFF_Builder* builder = &decoder->builder;
  637. FT_Pos x, y;
  638. FT_Fixed seed;
  639. FT_Fixed* stack;
  640. T2_Hints_Funcs hinter;
  641. /* set default width */
  642. decoder->num_hints = 0;
  643. decoder->read_width = 1;
  644. /* compute random seed from stack address of parameter */
  645. seed = (FT_Fixed)(char*)&seed ^
  646. (FT_Fixed)(char*)&decoder ^
  647. (FT_Fixed)(char*)&charstring_base;
  648. seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFF;
  649. if ( seed == 0 )
  650. seed = 0x7384;
  651. /* initialize the decoder */
  652. decoder->top = decoder->stack;
  653. decoder->zone = decoder->zones;
  654. zone = decoder->zones;
  655. stack = decoder->top;
  656. hinter = (T2_Hints_Funcs) builder->hints_funcs;
  657. builder->path_begun = 0;
  658. zone->base = charstring_base;
  659. limit = zone->limit = charstring_base + charstring_len;
  660. ip = zone->cursor = zone->base;
  661. error = CFF_Err_Ok;
  662. x = builder->pos_x;
  663. y = builder->pos_y;
  664. /* begin hints recording session, if any */
  665. if ( hinter )
  666. hinter->open( hinter->hints );
  667. /* now, execute loop */
  668. while ( ip < limit )
  669. {
  670. CFF_Operator op;
  671. FT_Byte v;
  672. /********************************************************************/
  673. /* */
  674. /* Decode operator or operand */
  675. /* */
  676. v = *ip++;
  677. if ( v >= 32 || v == 28 )
  678. {
  679. FT_Int shift = 16;
  680. FT_Int32 val;
  681. /* this is an operand, push it on the stack */
  682. if ( v == 28 )
  683. {
  684. if ( ip + 1 >= limit )
  685. goto Syntax_Error;
  686. val = (FT_Short)( ( (FT_Short)ip[0] << 8 ) | ip[1] );
  687. ip += 2;
  688. }
  689. else if ( v < 247 )
  690. val = (FT_Long)v - 139;
  691. else if ( v < 251 )
  692. {
  693. if ( ip >= limit )
  694. goto Syntax_Error;
  695. val = ( (FT_Long)v - 247 ) * 256 + *ip++ + 108;
  696. }
  697. else if ( v < 255 )
  698. {
  699. if ( ip >= limit )
  700. goto Syntax_Error;
  701. val = -( (FT_Long)v - 251 ) * 256 - *ip++ - 108;
  702. }
  703. else
  704. {
  705. if ( ip + 3 >= limit )
  706. goto Syntax_Error;
  707. val = ( (FT_Int32)ip[0] << 24 ) |
  708. ( (FT_Int32)ip[1] << 16 ) |
  709. ( (FT_Int32)ip[2] << 8 ) |
  710. ip[3];
  711. ip += 4;
  712. shift = 0;
  713. }
  714. if ( decoder->top - stack >= CFF_MAX_OPERANDS )
  715. goto Stack_Overflow;
  716. val <<= shift;
  717. *decoder->top++ = val;
  718. #ifdef FT_DEBUG_LEVEL_TRACE
  719. if ( !( val & 0xFFFF ) )
  720. FT_TRACE4(( " %d", (FT_Int32)( val >> 16 ) ));
  721. else
  722. FT_TRACE4(( " %.2f", val / 65536.0 ));
  723. #endif
  724. }
  725. else
  726. {
  727. FT_Fixed* args = decoder->top;
  728. FT_Int num_args = (FT_Int)( args - decoder->stack );
  729. FT_Int req_args;
  730. /* find operator */
  731. op = cff_op_unknown;
  732. switch ( v )
  733. {
  734. case 1:
  735. op = cff_op_hstem;
  736. break;
  737. case 3:
  738. op = cff_op_vstem;
  739. break;
  740. case 4:
  741. op = cff_op_vmoveto;
  742. break;
  743. case 5:
  744. op = cff_op_rlineto;
  745. break;
  746. case 6:
  747. op = cff_op_hlineto;
  748. break;
  749. case 7:
  750. op = cff_op_vlineto;
  751. break;
  752. case 8:
  753. op = cff_op_rrcurveto;
  754. break;
  755. case 10:
  756. op = cff_op_callsubr;
  757. break;
  758. case 11:
  759. op = cff_op_return;
  760. break;
  761. case 12:
  762. {
  763. if ( ip >= limit )
  764. goto Syntax_Error;
  765. v = *ip++;
  766. switch ( v )
  767. {
  768. case 0:
  769. op = cff_op_dotsection;
  770. break;
  771. case 3:
  772. op = cff_op_and;
  773. break;
  774. case 4:
  775. op = cff_op_or;
  776. break;
  777. case 5:
  778. op = cff_op_not;
  779. break;
  780. case 8:
  781. op = cff_op_store;
  782. break;
  783. case 9:
  784. op = cff_op_abs;
  785. break;
  786. case 10:
  787. op = cff_op_add;
  788. break;
  789. case 11:
  790. op = cff_op_sub;
  791. break;
  792. case 12:
  793. op = cff_op_div;
  794. break;
  795. case 13:
  796. op = cff_op_load;
  797. break;
  798. case 14:
  799. op = cff_op_neg;
  800. break;
  801. case 15:
  802. op = cff_op_eq;
  803. break;
  804. case 18:
  805. op = cff_op_drop;
  806. break;
  807. case 20:
  808. op = cff_op_put;
  809. break;
  810. case 21:
  811. op = cff_op_get;
  812. break;
  813. case 22:
  814. op = cff_op_ifelse;
  815. break;
  816. case 23:
  817. op = cff_op_random;
  818. break;
  819. case 24:
  820. op = cff_op_mul;
  821. break;
  822. case 26:
  823. op = cff_op_sqrt;
  824. break;
  825. case 27:
  826. op = cff_op_dup;
  827. break;
  828. case 28:
  829. op = cff_op_exch;
  830. break;
  831. case 29:
  832. op = cff_op_index;
  833. break;
  834. case 30:
  835. op = cff_op_roll;
  836. break;
  837. case 34:
  838. op = cff_op_hflex;
  839. break;
  840. case 35:
  841. op = cff_op_flex;
  842. break;
  843. case 36:
  844. op = cff_op_hflex1;
  845. break;
  846. case 37:
  847. op = cff_op_flex1;
  848. break;
  849. default:
  850. /* decrement ip for syntax error message */
  851. ip--;
  852. }
  853. }
  854. break;
  855. case 14:
  856. op = cff_op_endchar;
  857. break;
  858. case 16:
  859. op = cff_op_blend;
  860. break;
  861. case 18:
  862. op = cff_op_hstemhm;
  863. break;
  864. case 19:
  865. op = cff_op_hintmask;
  866. break;
  867. case 20:
  868. op = cff_op_cntrmask;
  869. break;
  870. case 21:
  871. op = cff_op_rmoveto;
  872. break;
  873. case 22:
  874. op = cff_op_hmoveto;
  875. break;
  876. case 23:
  877. op = cff_op_vstemhm;
  878. break;
  879. case 24:
  880. op = cff_op_rcurveline;
  881. break;
  882. case 25:
  883. op = cff_op_rlinecurve;
  884. break;
  885. case 26:
  886. op = cff_op_vvcurveto;
  887. break;
  888. case 27:
  889. op = cff_op_hhcurveto;
  890. break;
  891. case 29:
  892. op = cff_op_callgsubr;
  893. break;
  894. case 30:
  895. op = cff_op_vhcurveto;
  896. break;
  897. case 31:
  898. op = cff_op_hvcurveto;
  899. break;
  900. default:
  901. ;
  902. }
  903. if ( op == cff_op_unknown )
  904. goto Syntax_Error;
  905. /* check arguments */
  906. req_args = cff_argument_counts[op];
  907. if ( req_args & CFF_COUNT_CHECK_WIDTH )
  908. {
  909. args = stack;
  910. if ( num_args > 0 && decoder->read_width )
  911. {
  912. /* If `nominal_width' is non-zero, the number is really a */
  913. /* difference against `nominal_width'. Else, the number here */
  914. /* is truly a width, not a difference against `nominal_width'. */
  915. /* If the font does not set `nominal_width', then */
  916. /* `nominal_width' defaults to zero, and so we can set */
  917. /* `glyph_width' to `nominal_width' plus number on the stack */
  918. /* -- for either case. */
  919. FT_Int set_width_ok;
  920. switch ( op )
  921. {
  922. case cff_op_hmoveto:
  923. case cff_op_vmoveto:
  924. set_width_ok = num_args & 2;
  925. break;
  926. case cff_op_hstem:
  927. case cff_op_vstem:
  928. case cff_op_hstemhm:
  929. case cff_op_vstemhm:
  930. case cff_op_rmoveto:
  931. set_width_ok = num_args & 1;
  932. break;
  933. case cff_op_endchar:
  934. /* If there is a width specified for endchar, we either have */
  935. /* 1 argument or 5 arguments. We like to argue. */
  936. set_width_ok = ( ( num_args == 5 ) || ( num_args == 1 ) );
  937. break;
  938. default:
  939. set_width_ok = 0;
  940. break;
  941. }
  942. if ( set_width_ok )
  943. {
  944. decoder->glyph_width = decoder->nominal_width +
  945. ( stack[0] >> 16 );
  946. /* Consumed an argument. */
  947. num_args--;
  948. args++;
  949. }
  950. }
  951. decoder->read_width = 0;
  952. req_args = 0;
  953. }
  954. req_args &= 15;
  955. if ( num_args < req_args )
  956. goto Stack_Underflow;
  957. args -= req_args;
  958. num_args -= req_args;
  959. switch ( op )
  960. {
  961. case cff_op_hstem:
  962. case cff_op_vstem:
  963. case cff_op_hstemhm:
  964. case cff_op_vstemhm:
  965. /* the number of arguments is always even here */
  966. FT_TRACE4(( op == cff_op_hstem ? " hstem" :
  967. ( op == cff_op_vstem ? " vstem" :
  968. ( op == cff_op_hstemhm ? " hstemhm" : " vstemhm" ) ) ));
  969. if ( hinter )
  970. hinter->stems( hinter->hints,
  971. ( op == cff_op_hstem || op == cff_op_hstemhm ),
  972. num_args / 2,
  973. args );
  974. decoder->num_hints += num_args / 2;
  975. args = stack;
  976. break;
  977. case cff_op_hintmask:
  978. case cff_op_cntrmask:
  979. FT_TRACE4(( op == cff_op_hintmask ? " hintmask" : " cntrmask" ));
  980. /* implement vstem when needed -- */
  981. /* the specification doesn't say it, but this also works */
  982. /* with the 'cntrmask' operator */
  983. /* */
  984. if ( num_args > 0 )
  985. {
  986. if ( hinter )
  987. hinter->stems( hinter->hints,
  988. 0,
  989. num_args / 2,
  990. args );
  991. decoder->num_hints += num_args / 2;
  992. }
  993. if ( hinter )
  994. {
  995. if ( op == cff_op_hintmask )
  996. hinter->hintmask( hinter->hints,
  997. builder->current->n_points,
  998. decoder->num_hints,
  999. ip );
  1000. else
  1001. hinter->counter( hinter->hints,
  1002. decoder->num_hints,
  1003. ip );
  1004. }
  1005. #ifdef FT_DEBUG_LEVEL_TRACE
  1006. {
  1007. FT_UInt maskbyte;
  1008. FT_TRACE4(( " " ));
  1009. for ( maskbyte = 0;
  1010. maskbyte < (FT_UInt)(( decoder->num_hints + 7 ) >> 3);
  1011. maskbyte++, ip++ )
  1012. FT_TRACE4(( "%02X", *ip ));
  1013. }
  1014. #else
  1015. ip += ( decoder->num_hints + 7 ) >> 3;
  1016. #endif
  1017. if ( ip >= limit )
  1018. goto Syntax_Error;
  1019. args = stack;
  1020. break;
  1021. case cff_op_rmoveto:
  1022. FT_TRACE4(( " rmoveto" ));
  1023. cff_builder_close_contour( builder );
  1024. builder->path_begun = 0;
  1025. x += args[0];
  1026. y += args[1];
  1027. args = stack;
  1028. break;
  1029. case cff_op_vmoveto:
  1030. FT_TRACE4(( " vmoveto" ));
  1031. cff_builder_close_contour( builder );
  1032. builder->path_begun = 0;
  1033. y += args[0];
  1034. args = stack;
  1035. break;
  1036. case cff_op_hmoveto:
  1037. FT_TRACE4(( " hmoveto" ));
  1038. cff_builder_close_contour( builder );
  1039. builder->path_begun = 0;
  1040. x += args[0];
  1041. args = stack;
  1042. break;
  1043. case cff_op_rlineto:
  1044. FT_TRACE4(( " rlineto" ));
  1045. if ( cff_builder_start_point ( builder, x, y ) ||
  1046. check_points( builder, num_args / 2 ) )
  1047. goto Memory_Error;
  1048. if ( num_args < 2 || num_args & 1 )
  1049. goto Stack_Underflow;
  1050. args = stack;
  1051. while ( args < decoder->top )
  1052. {
  1053. x += args[0];
  1054. y += args[1];
  1055. cff_builder_add_point( builder, x, y, 1 );
  1056. args += 2;
  1057. }
  1058. args = stack;
  1059. break;
  1060. case cff_op_hlineto:
  1061. case cff_op_vlineto:
  1062. {
  1063. FT_Int phase = ( op == cff_op_hlineto );
  1064. FT_TRACE4(( op == cff_op_hlineto ? " hlineto"
  1065. : " vlineto" ));
  1066. if ( cff_builder_start_point ( builder, x, y ) ||
  1067. check_points( builder, num_args ) )
  1068. goto Memory_Error;
  1069. args = stack;
  1070. while (args < decoder->top )
  1071. {
  1072. if ( phase )
  1073. x += args[0];
  1074. else
  1075. y += args[0];
  1076. if ( cff_builder_add_point1( builder, x, y ) )
  1077. goto Memory_Error;
  1078. args++;
  1079. phase ^= 1;
  1080. }
  1081. args = stack;
  1082. }
  1083. break;
  1084. case cff_op_rrcurveto:
  1085. FT_TRACE4(( " rrcurveto" ));
  1086. /* check number of arguments; must be a multiple of 6 */
  1087. if ( num_args % 6 != 0 )
  1088. goto Stack_Underflow;
  1089. if ( cff_builder_start_point ( builder, x, y ) ||
  1090. check_points( builder, num_args / 2 ) )
  1091. goto Memory_Error;
  1092. args = stack;
  1093. while ( args < decoder->top )
  1094. {
  1095. x += args[0];
  1096. y += args[1];
  1097. cff_builder_add_point( builder, x, y, 0 );
  1098. x += args[2];
  1099. y += args[3];
  1100. cff_builder_add_point( builder, x, y, 0 );
  1101. x += args[4];
  1102. y += args[5];
  1103. cff_builder_add_point( builder, x, y, 1 );
  1104. args += 6;
  1105. }
  1106. args = stack;
  1107. break;
  1108. case cff_op_vvcurveto:
  1109. FT_TRACE4(( " vvcurveto" ));
  1110. if ( cff_builder_start_point ( builder, x, y ) )
  1111. goto Memory_Error;
  1112. args = stack;
  1113. if ( num_args & 1 )
  1114. {
  1115. x += args[0];
  1116. args++;
  1117. num_args--;
  1118. }
  1119. if ( num_args % 4 != 0 )
  1120. goto Stack_Underflow;
  1121. if ( check_points( builder, 3 * ( num_args / 4 ) ) )
  1122. goto Memory_Error;
  1123. while ( args < decoder->top )
  1124. {
  1125. y += args[0];
  1126. cff_builder_add_point( builder, x, y, 0 );
  1127. x += args[1];
  1128. y += args[2];
  1129. cff_builder_add_point( builder, x, y, 0 );
  1130. y += args[3];
  1131. cff_builder_add_point( builder, x, y, 1 );
  1132. args += 4;
  1133. }
  1134. args = stack;
  1135. break;
  1136. case cff_op_hhcurveto:
  1137. FT_TRACE4(( " hhcurveto" ));
  1138. if ( cff_builder_start_point ( builder, x, y ) )
  1139. goto Memory_Error;
  1140. args = stack;
  1141. if ( num_args & 1 )
  1142. {
  1143. y += args[0];
  1144. args++;
  1145. num_args--;
  1146. }
  1147. if ( num_args % 4 != 0 )
  1148. goto Stack_Underflow;
  1149. if ( check_points( builder, 3 * ( num_args / 4 ) ) )
  1150. goto Memory_Error;
  1151. while ( args < decoder->top )
  1152. {
  1153. x += args[0];
  1154. cff_builder_add_point( builder, x, y, 0 );
  1155. x += args[1];
  1156. y += args[2];
  1157. cff_builder_add_point( builder, x, y, 0 );
  1158. x += args[3];
  1159. cff_builder_add_point( builder, x, y, 1 );
  1160. args += 4;
  1161. }
  1162. args = stack;
  1163. break;
  1164. case cff_op_vhcurveto:
  1165. case cff_op_hvcurveto:
  1166. {
  1167. FT_Int phase;
  1168. FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto"
  1169. : " hvcurveto" ));
  1170. if ( cff_builder_start_point ( builder, x, y ) )
  1171. goto Memory_Error;
  1172. args = stack;
  1173. if (num_args < 4 || ( num_args % 4 ) > 1 )
  1174. goto Stack_Underflow;
  1175. if ( check_points( builder, ( num_args / 4 ) * 3 ) )
  1176. goto Stack_Underflow;
  1177. phase = ( op == cff_op_hvcurveto );
  1178. while ( num_args >= 4 )
  1179. {
  1180. num_args -= 4;
  1181. if ( phase )
  1182. {
  1183. x += args[0];
  1184. cff_builder_add_point( builder, x, y, 0 );
  1185. x += args[1];
  1186. y += args[2];
  1187. cff_builder_add_point( builder, x, y, 0 );
  1188. y += args[3];
  1189. if ( num_args == 1 )
  1190. x += args[4];
  1191. cff_builder_add_point( builder, x, y, 1 );
  1192. }
  1193. else
  1194. {
  1195. y += args[0];
  1196. cff_builder_add_point( builder, x, y, 0 );
  1197. x += args[1];
  1198. y += args[2];
  1199. cff_builder_add_point( builder, x, y, 0 );
  1200. x += args[3];
  1201. if ( num_args == 1 )
  1202. y += args[4];
  1203. cff_builder_add_point( builder, x, y, 1 );
  1204. }
  1205. args += 4;
  1206. phase ^= 1;
  1207. }
  1208. args = stack;
  1209. }
  1210. break;
  1211. case cff_op_rlinecurve:
  1212. {
  1213. FT_Int num_lines = ( num_args - 6 ) / 2;
  1214. FT_TRACE4(( " rlinecurve" ));
  1215. if ( num_args < 8 || ( num_args - 6 ) & 1 )
  1216. goto Stack_Underflow;
  1217. if ( cff_builder_start_point( builder, x, y ) ||
  1218. check_points( builder, num_lines + 3 ) )
  1219. goto Memory_Error;
  1220. args = stack;
  1221. /* first, add the line segments */
  1222. while ( num_lines > 0 )
  1223. {
  1224. x += args[0];
  1225. y += args[1];
  1226. cff_builder_add_point( builder, x, y, 1 );
  1227. args += 2;
  1228. num_lines--;
  1229. }
  1230. /* then the curve */
  1231. x += args[0];
  1232. y += args[1];
  1233. cff_builder_add_point( builder, x, y, 0 );
  1234. x += args[2];
  1235. y += args[3];
  1236. cff_builder_add_point( builder, x, y, 0 );
  1237. x += args[4];
  1238. y += args[5];
  1239. cff_builder_add_point( builder, x, y, 1 );
  1240. args = stack;
  1241. }
  1242. break;
  1243. case cff_op_rcurveline:
  1244. {
  1245. FT_Int num_curves = ( num_args - 2 ) / 6;
  1246. FT_TRACE4(( " rcurveline" ));
  1247. if ( num_args < 8 || ( num_args - 2 ) % 6 )
  1248. goto Stack_Underflow;
  1249. if ( cff_builder_start_point ( builder, x, y ) ||
  1250. check_points( builder, num_curves*3 + 2 ) )
  1251. goto Memory_Error;
  1252. args = stack;
  1253. /* first, add the curves */
  1254. while ( num_curves > 0 )
  1255. {
  1256. x += args[0];
  1257. y += args[1];
  1258. cff_builder_add_point( builder, x, y, 0 );
  1259. x += args[2];
  1260. y += args[3];
  1261. cff_builder_add_point( builder, x, y, 0 );
  1262. x += args[4];
  1263. y += args[5];
  1264. cff_builder_add_point( builder, x, y, 1 );
  1265. args += 6;
  1266. num_curves--;
  1267. }
  1268. /* then the final line */
  1269. x += args[0];
  1270. y += args[1];
  1271. cff_builder_add_point( builder, x, y, 1 );
  1272. args = stack;
  1273. }
  1274. break;
  1275. case cff_op_hflex1:
  1276. {
  1277. FT_Pos start_y;
  1278. FT_TRACE4(( " hflex1" ));
  1279. args = stack;
  1280. /* adding five more points; 4 control points, 1 on-curve point */
  1281. /* make sure we have enough space for the start point if it */
  1282. /* needs to be added.. */
  1283. if ( cff_builder_start_point( builder, x, y ) ||
  1284. check_points( builder, 6 ) )
  1285. goto Memory_Error;
  1286. /* Record the starting point's y postion for later use */
  1287. start_y = y;
  1288. /* first control point */
  1289. x += args[0];
  1290. y += args[1];
  1291. cff_builder_add_point( builder, x, y, 0 );
  1292. /* second control point */
  1293. x += args[2];
  1294. y += args[3];
  1295. cff_builder_add_point( builder, x, y, 0 );
  1296. /* join point; on curve, with y-value the same as the last */
  1297. /* control point's y-value */
  1298. x += args[4];
  1299. cff_builder_add_point( builder, x, y, 1 );
  1300. /* third control point, with y-value the same as the join */
  1301. /* point's y-value */
  1302. x += args[5];
  1303. cff_builder_add_point( builder, x, y, 0 );
  1304. /* fourth control point */
  1305. x += args[6];
  1306. y += args[7];
  1307. cff_builder_add_point( builder, x, y, 0 );
  1308. /* ending point, with y-value the same as the start */
  1309. x += args[8];
  1310. y = start_y;
  1311. cff_builder_add_point( builder, x, y, 1 );
  1312. args = stack;
  1313. break;
  1314. }
  1315. case cff_op_hflex:
  1316. {
  1317. FT_Pos start_y;
  1318. FT_TRACE4(( " hflex" ));
  1319. args = stack;
  1320. /* adding six more points; 4 control points, 2 on-curve points */
  1321. if ( cff_builder_start_point( builder, x, y ) ||
  1322. check_points ( builder, 6 ) )
  1323. goto Memory_Error;
  1324. /* record the starting point's y-position for later use */
  1325. start_y = y;
  1326. /* first control point */
  1327. x += args[0];
  1328. cff_builder_add_point( builder, x, y, 0 );
  1329. /* second control point */
  1330. x += args[1];
  1331. y += args[2];
  1332. cff_builder_add_point( builder, x, y, 0 );
  1333. /* join point; on curve, with y-value the same as the last */
  1334. /* control point's y-value */
  1335. x += args[3];
  1336. cff_builder_add_point( builder, x, y, 1 );
  1337. /* third control point, with y-value the same as the join */
  1338. /* point's y-value */
  1339. x += args[4];
  1340. cff_builder_add_point( builder, x, y, 0 );
  1341. /* fourth control point */
  1342. x += args[5];
  1343. y = start_y;
  1344. cff_builder_add_point( builder, x, y, 0 );
  1345. /* ending point, with y-value the same as the start point's */
  1346. /* y-value -- we don't add this point, though */
  1347. x += args[6];
  1348. cff_builder_add_point( builder, x, y, 1 );
  1349. args = stack;
  1350. break;
  1351. }
  1352. case cff_op_flex1:
  1353. {
  1354. FT_Pos start_x, start_y; /* record start x, y values for alter */
  1355. /* use */
  1356. FT_Int dx = 0, dy = 0; /* used in horizontal/vertical */
  1357. /* algorithm below */
  1358. FT_Int horizontal, count;
  1359. FT_TRACE4(( " flex1" ));
  1360. /* adding six more points; 4 control points, 2 on-curve points */
  1361. if ( cff_builder_start_point( builder, x, y ) ||
  1362. check_points( builder, 6 ) )
  1363. goto Memory_Error;
  1364. /* record the starting point's x, y postion for later use */
  1365. start_x = x;
  1366. start_y = y;
  1367. /* XXX: figure out whether this is supposed to be a horizontal */
  1368. /* or vertical flex; the Type 2 specification is vague... */
  1369. args = stack;
  1370. /* grab up to the last argument */
  1371. for ( count = 5; count > 0; count-- )
  1372. {
  1373. dx += (FT_Int)args[0];
  1374. dy += (FT_Int)args[1];
  1375. args += 2;
  1376. }
  1377. /* rewind */
  1378. args = stack;
  1379. if ( dx < 0 ) dx = -dx;
  1380. if ( dy < 0 ) dy = -dy;
  1381. /* strange test, but here it is... */
  1382. horizontal = ( dx > dy );
  1383. for ( count = 5; count > 0; count-- )
  1384. {
  1385. x += args[0];
  1386. y += args[1];
  1387. cff_builder_add_point( builder, x, y, (FT_Bool)( count == 3 ) );
  1388. args += 2;
  1389. }
  1390. /* is last operand an x- or y-delta? */
  1391. if ( horizontal )
  1392. {
  1393. x += args[0];
  1394. y = start_y;
  1395. }
  1396. else
  1397. {
  1398. x = start_x;
  1399. y += args[0];
  1400. }
  1401. cff_builder_add_point( builder, x, y, 1 );
  1402. args = stack;
  1403. break;
  1404. }
  1405. case cff_op_flex:
  1406. {
  1407. FT_UInt count;
  1408. FT_TRACE4(( " flex" ));
  1409. if ( cff_builder_start_point( builder, x, y ) ||
  1410. check_points( builder, 6 ) )
  1411. goto Memory_Error;
  1412. args = stack;
  1413. for ( count = 6; count > 0; count-- )
  1414. {
  1415. x += args[0];
  1416. y += args[1];
  1417. cff_builder_add_point( builder, x, y,
  1418. (FT_Bool)( count == 3 || count == 0 ) );
  1419. args += 2;
  1420. }
  1421. args = stack;
  1422. }
  1423. break;
  1424. case cff_op_endchar:
  1425. FT_TRACE4(( " endchar" ));
  1426. /* We are going to emulate the seac operator. */
  1427. if ( num_args == 4 )
  1428. {
  1429. error = cff_operator_seac( decoder,
  1430. args[0] >> 16,
  1431. args[1] >> 16,
  1432. (FT_Int)( args[2] >> 16 ),
  1433. (FT_Int)( args[3] >> 16 ) );
  1434. args += 4;
  1435. }
  1436. if ( !error )
  1437. error = CFF_Err_Ok;
  1438. cff_builder_close_contour( builder );
  1439. /* close hints recording session */
  1440. if ( hinter )
  1441. {
  1442. if (hinter->close( hinter->hints, builder->current->n_points ) )
  1443. goto Syntax_Error;
  1444. /* apply hints to the loaded glyph outline now */
  1445. hinter->apply( hinter->hints,
  1446. builder->current,
  1447. (PSH_Globals)builder->hints_globals,
  1448. builder->hint_flags );
  1449. }
  1450. /* add current outline to the glyph slot */
  1451. FT_GlyphLoader_Add( builder->loader );
  1452. /* return now! */
  1453. FT_TRACE4(( "\n\n" ));
  1454. return error;
  1455. case cff_op_abs:
  1456. FT_TRACE4(( " abs" ));
  1457. if ( args[0] < 0 )
  1458. args[0] = -args[0];
  1459. args++;
  1460. break;
  1461. case cff_op_add:
  1462. FT_TRACE4(( " add" ));
  1463. args[0] += args[1];
  1464. args++;
  1465. break;
  1466. case cff_op_sub:
  1467. FT_TRACE4(( " sub" ));
  1468. args[0] -= args[1];
  1469. args++;
  1470. break;
  1471. case cff_op_div:
  1472. FT_TRACE4(( " div" ));
  1473. args[0] = FT_DivFix( args[0], args[1] );
  1474. args++;
  1475. break;
  1476. case cff_op_neg:
  1477. FT_TRACE4(( " neg" ));
  1478. args[0] = -args[0];
  1479. args++;
  1480. break;
  1481. case cff_op_random:
  1482. {
  1483. FT_Fixed Rand;
  1484. FT_TRACE4(( " rand" ));
  1485. Rand = seed;
  1486. if ( Rand >= 0x8000 )
  1487. Rand++;
  1488. args[0] = Rand;
  1489. seed = FT_MulFix( seed, 0x10000L - seed );
  1490. if ( seed == 0 )
  1491. seed += 0x2873;
  1492. args++;
  1493. }
  1494. break;
  1495. case cff_op_mul:
  1496. FT_TRACE4(( " mul" ));
  1497. args[0] = FT_MulFix( args[0], args[1] );
  1498. args++;
  1499. break;
  1500. case cff_op_sqrt:
  1501. FT_TRACE4(( " sqrt" ));
  1502. if ( args[0] > 0 )
  1503. {
  1504. FT_Int count = 9;
  1505. FT_Fixed root = args[0];
  1506. FT_Fixed new_root;
  1507. for (;;)
  1508. {
  1509. new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
  1510. if ( new_root == root || count <= 0 )
  1511. break;
  1512. root = new_root;
  1513. }
  1514. args[0] = new_root;
  1515. }
  1516. else
  1517. args[0] = 0;
  1518. args++;
  1519. break;
  1520. case cff_op_drop:
  1521. /* nothing */
  1522. FT_TRACE4(( " drop" ));
  1523. break;
  1524. case cff_op_exch:
  1525. {
  1526. FT_Fixed tmp;
  1527. FT_TRACE4(( " exch" ));
  1528. tmp = args[0];
  1529. args[0] = args[1];
  1530. args[1] = tmp;
  1531. args += 2;
  1532. }
  1533. break;
  1534. case cff_op_index:
  1535. {
  1536. FT_Int idx = (FT_Int)( args[0] >> 16 );
  1537. FT_TRACE4(( " index" ));
  1538. if ( idx < 0 )
  1539. idx = 0;
  1540. else if ( idx > num_args - 2 )
  1541. idx = num_args - 2;
  1542. args[0] = args[-( idx + 1 )];
  1543. args++;
  1544. }
  1545. break;
  1546. case cff_op_roll:
  1547. {
  1548. FT_Int count = (FT_Int)( args[0] >> 16 );
  1549. FT_Int idx = (FT_Int)( args[1] >> 16 );
  1550. FT_TRACE4(( " roll" ));
  1551. if ( count <= 0 )
  1552. count = 1;
  1553. args -= count;
  1554. if ( args < stack )
  1555. goto Stack_Underflow;
  1556. if ( idx >= 0 )
  1557. {
  1558. while ( idx > 0 )
  1559. {
  1560. FT_Fixed tmp = args[count - 1];
  1561. FT_Int i;
  1562. for ( i = count - 2; i >= 0; i-- )
  1563. args[i + 1] = args[i];
  1564. args[0] = tmp;
  1565. idx--;
  1566. }
  1567. }
  1568. else
  1569. {
  1570. while ( idx < 0 )
  1571. {
  1572. FT_Fixed tmp = args[0];
  1573. FT_Int i;
  1574. for ( i = 0; i < count - 1; i++ )
  1575. args[i] = args[i + 1];
  1576. args[count - 1] = tmp;
  1577. idx++;
  1578. }
  1579. }
  1580. args += count;
  1581. }
  1582. break;
  1583. case cff_op_dup:
  1584. FT_TRACE4(( " dup" ));
  1585. args[1] = args[0];
  1586. args++;
  1587. break;
  1588. case cff_op_put:
  1589. {
  1590. FT_Fixed val = args[0];
  1591. FT_Int idx = (FT_Int)( args[1] >> 16 );
  1592. FT_TRACE4(( " put" ));
  1593. if ( idx >= 0 && idx < decoder->len_buildchar )
  1594. decoder->buildchar[idx] = val;
  1595. }
  1596. break;
  1597. case cff_op_get:
  1598. {
  1599. FT_Int idx = (FT_Int)( args[0] >> 16 );
  1600. FT_Fixed val = 0;
  1601. FT_TRACE4(( " get" ));
  1602. if ( idx >= 0 && idx < decoder->len_buildchar )
  1603. val = decoder->buildchar[idx];
  1604. args[0] = val;
  1605. args++;
  1606. }
  1607. break;
  1608. case cff_op_store:
  1609. FT_TRACE4(( " store "));
  1610. goto Unimplemented;
  1611. case cff_op_load:
  1612. FT_TRACE4(( " load" ));
  1613. goto Unimplemented;
  1614. case cff_op_dotsection:
  1615. /* this operator is deprecated and ignored by the parser */
  1616. FT_TRACE4(( " dotsection" ));
  1617. break;
  1618. case cff_op_and:
  1619. {
  1620. FT_Fixed cond = args[0] && args[1];
  1621. FT_TRACE4(( " and" ));
  1622. args[0] = cond ? 0x10000L : 0;
  1623. args++;
  1624. }
  1625. break;
  1626. case cff_op_or:
  1627. {
  1628. FT_Fixed cond = args[0] || args[1];
  1629. FT_TRACE4(( " or" ));
  1630. args[0] = cond ? 0x10000L : 0;
  1631. args++;
  1632. }
  1633. break;
  1634. case cff_op_eq:
  1635. {
  1636. FT_Fixed cond = !args[0];
  1637. FT_TRACE4(( " eq" ));
  1638. args[0] = cond ? 0x10000L : 0;
  1639. args++;
  1640. }
  1641. break;
  1642. case cff_op_ifelse:
  1643. {
  1644. FT_Fixed cond = (args[2] <= args[3]);
  1645. FT_TRACE4(( " ifelse" ));
  1646. if ( !cond )
  1647. args[0] = args[1];
  1648. args++;
  1649. }
  1650. break;
  1651. case cff_op_callsubr:
  1652. {
  1653. FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
  1654. decoder->locals_bias );
  1655. FT_TRACE4(( " callsubr(%d)", idx ));
  1656. if ( idx >= decoder->num_locals )
  1657. {
  1658. FT_ERROR(( "cff_decoder_parse_charstrings:" ));
  1659. FT_ERROR(( " invalid local subr index\n" ));
  1660. goto Syntax_Error;
  1661. }
  1662. if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
  1663. {
  1664. FT_ERROR(( "cff_decoder_parse_charstrings:"
  1665. " too many nested subrs\n" ));
  1666. goto Syntax_Error;
  1667. }
  1668. zone->cursor = ip; /* save current instruction pointer */
  1669. zone++;
  1670. zone->base = decoder->locals[idx];
  1671. zone->limit = decoder->locals[idx + 1];
  1672. zone->cursor = zone->base;
  1673. if ( !zone->base )
  1674. {
  1675. FT_ERROR(( "cff_decoder_parse_charstrings:"
  1676. " invoking empty subrs!\n" ));
  1677. goto Syntax_Error;
  1678. }
  1679. decoder->zone = zone;
  1680. ip = zone->base;
  1681. limit = zone->limit;
  1682. }
  1683. break;
  1684. case cff_op_callgsubr:
  1685. {
  1686. FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
  1687. decoder->globals_bias );
  1688. FT_TRACE4(( " callgsubr(%d)", idx ));
  1689. if ( idx >= decoder->num_globals )
  1690. {
  1691. FT_ERROR(( "cff_decoder_parse_charstrings:" ));
  1692. FT_ERROR(( " invalid global subr index\n" ));
  1693. goto Syntax_Error;
  1694. }
  1695. if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
  1696. {
  1697. FT_ERROR(( "cff_decoder_parse_charstrings:"
  1698. " too many nested subrs\n" ));
  1699. goto Syntax_Error;
  1700. }
  1701. zone->cursor = ip; /* save current instruction pointer */
  1702. zone++;
  1703. zone->base = decoder->globals[idx];
  1704. zone->limit = decoder->globals[idx + 1];
  1705. zone->cursor = zone->base;
  1706. if ( !zone->base )
  1707. {
  1708. FT_ERROR(( "cff_decoder_parse_charstrings:"
  1709. " invoking empty subrs!\n" ));
  1710. goto Syntax_Error;
  1711. }
  1712. decoder->zone = zone;
  1713. ip = zone->base;
  1714. limit = zone->limit;
  1715. }
  1716. break;
  1717. case cff_op_return:
  1718. FT_TRACE4(( " return" ));
  1719. if ( decoder->zone <= decoder->zones )
  1720. {
  1721. FT_ERROR(( "cff_decoder_parse_charstrings:"
  1722. " unexpected return\n" ));
  1723. goto Syntax_Error;
  1724. }
  1725. decoder->zone--;
  1726. zone = decoder->zone;
  1727. ip = zone->cursor;
  1728. limit = zone->limit;
  1729. break;
  1730. default:
  1731. Unimplemented:
  1732. FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
  1733. if ( ip[-1] == 12 )
  1734. FT_ERROR(( " %d", ip[0] ));
  1735. FT_ERROR(( "\n" ));
  1736. return CFF_Err_Unimplemented_Feature;
  1737. }
  1738. decoder->top = args;
  1739. } /* general operator processing */
  1740. } /* while ip < limit */
  1741. FT_TRACE4(( "..end..\n\n" ));
  1742. return error;
  1743. Syntax_Error:
  1744. FT_TRACE4(( "cff_decoder_parse_charstrings: syntax error!" ));
  1745. return CFF_Err_Invalid_File_Format;
  1746. Stack_Underflow:
  1747. FT_TRACE4(( "cff_decoder_parse_charstrings: stack underflow!" ));
  1748. return CFF_Err_Too_Few_Arguments;
  1749. Stack_Overflow:
  1750. FT_TRACE4(( "cff_decoder_parse_charstrings: stack overflow!" ));
  1751. return CFF_Err_Stack_Overflow;
  1752. Memory_Error:
  1753. return builder->error;
  1754. }
  1755. /*************************************************************************/
  1756. /*************************************************************************/
  1757. /*************************************************************************/
  1758. /********** *********/
  1759. /********** *********/
  1760. /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
  1761. /********** *********/
  1762. /********** The following code is in charge of computing *********/
  1763. /********** the maximum advance width of the font. It *********/
  1764. /********** quickly processes each glyph charstring to *********/
  1765. /********** extract the value from either a `sbw' or `seac' *********/
  1766. /********** operator. *********/
  1767. /********** *********/
  1768. /*************************************************************************/
  1769. /*************************************************************************/
  1770. /*************************************************************************/
  1771. #if 0 /* unused until we support pure CFF fonts */
  1772. FT_LOCAL_DEF( FT_Error )
  1773. cff_compute_max_advance( TT_Face face,
  1774. FT_Int* max_advance )
  1775. {
  1776. FT_Error error = 0;
  1777. CFF_Decoder decoder;
  1778. FT_Int glyph_index;
  1779. CFF_Font cff = (CFF_Font)face->other;
  1780. *max_advance = 0;
  1781. /* Initialize load decoder */
  1782. cff_decoder_init( &decoder, face, 0, 0, 0, 0 );
  1783. decoder.builder.metrics_only = 1;
  1784. decoder.builder.load_points = 0;
  1785. /* For each glyph, parse the glyph charstring and extract */
  1786. /* the advance width. */
  1787. for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
  1788. glyph_index++ )
  1789. {
  1790. FT_Byte* charstring;
  1791. FT_ULong charstring_len;
  1792. /* now get load the unscaled outline */
  1793. error = cff_get_glyph_data( face, glyph_index,
  1794. &charstring, &charstring_len );
  1795. if ( !error )
  1796. {
  1797. cff_decoder_prepare( &decoder, glyph_index );
  1798. error = cff_decoder_parse_charstrings( &decoder,
  1799. charstring, charstring_len );
  1800. cff_free_glyph_data( face, &charstring, &charstring_len );
  1801. }
  1802. /* ignore the error if one has occurred -- skip to next glyph */
  1803. error = 0;
  1804. }
  1805. *max_advance = decoder.builder.advance.x;
  1806. return CFF_Err_Ok;
  1807. }
  1808. #endif /* 0 */
  1809. /*************************************************************************/
  1810. /*************************************************************************/
  1811. /*************************************************************************/
  1812. /********** *********/
  1813. /********** *********/
  1814. /********** UNHINTED GLYPH LOADER *********/
  1815. /********** *********/
  1816. /********** The following code is in charge of loading a *********/
  1817. /********** single outline. It completely ignores hinting *********/
  1818. /********** and is used when FT_LOAD_NO_HINTING is set. *********/
  1819. /********** *********/
  1820. /*************************************************************************/
  1821. /*************************************************************************/
  1822. /*************************************************************************/
  1823. FT_LOCAL_DEF( FT_Error )
  1824. cff_slot_load( CFF_GlyphSlot glyph,
  1825. CFF_Size size,
  1826. FT_Int glyph_index,
  1827. FT_Int32 load_flags )
  1828. {
  1829. FT_Error error;
  1830. CFF_Decoder decoder;
  1831. TT_Face face = (TT_Face)glyph->root.face;
  1832. FT_Bool hinting;
  1833. CFF_Font cff = (CFF_Font)face->extra.data;
  1834. FT_Matrix font_matrix;
  1835. FT_Vector font_offset;
  1836. if ( load_flags & FT_LOAD_NO_RECURSE )
  1837. load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
  1838. glyph->x_scale = 0x10000L;
  1839. glyph->y_scale = 0x10000L;
  1840. if ( size )
  1841. {
  1842. glyph->x_scale = size->metrics.x_scale;
  1843. glyph->y_scale = size->metrics.y_scale;
  1844. }
  1845. glyph->root.outline.n_points = 0;
  1846. glyph->root.outline.n_contours = 0;
  1847. hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
  1848. ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
  1849. glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */
  1850. {
  1851. FT_Byte* charstring;
  1852. FT_ULong charstring_len;
  1853. cff_decoder_init( &decoder, face, size, glyph, hinting,
  1854. FT_LOAD_TARGET_MODE(load_flags) );
  1855. decoder.builder.no_recurse =
  1856. (FT_Bool)( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
  1857. /* now load the unscaled outline */
  1858. error = cff_get_glyph_data( face, glyph_index,
  1859. &charstring, &charstring_len );
  1860. if ( !error )
  1861. {
  1862. cff_decoder_prepare( &decoder, glyph_index );
  1863. error = cff_decoder_parse_charstrings( &decoder,
  1864. charstring, charstring_len );
  1865. cff_free_glyph_data( face, &charstring, charstring_len );
  1866. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1867. /* Control data and length may not be available for incremental */
  1868. /* fonts. */
  1869. if ( face->root.internal->incremental_interface )
  1870. {
  1871. glyph->root.control_data = 0;
  1872. glyph->root.control_len = 0;
  1873. }
  1874. else
  1875. #endif /* FT_CONFIG_OPTION_INCREMENTAL */
  1876. /* We set control_data and control_len if charstrings is loaded. */
  1877. /* See how charstring loads at cff_index_access_element() in */
  1878. /* cffload.c. */
  1879. {
  1880. CFF_IndexRec csindex = cff->charstrings_index;
  1881. glyph->root.control_data =
  1882. csindex.bytes + csindex.offsets[glyph_index] - 1;
  1883. glyph->root.control_len =
  1884. charstring_len;
  1885. }
  1886. }
  1887. /* save new glyph tables */
  1888. cff_builder_done( &decoder.builder );
  1889. }
  1890. font_matrix = cff->top_font.font_dict.font_matrix;
  1891. font_offset = cff->top_font.font_dict.font_offset;
  1892. /* Now, set the metrics -- this is rather simple, as */
  1893. /* the left side bearing is the xMin, and the top side */
  1894. /* bearing the yMax. */
  1895. if ( !error )
  1896. {
  1897. /* For composite glyphs, return only left side bearing and */
  1898. /* advance width. */
  1899. if ( load_flags & FT_LOAD_NO_RECURSE )
  1900. {
  1901. FT_Slot_Internal internal = glyph->root.internal;
  1902. glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
  1903. glyph->root.metrics.horiAdvance = decoder.glyph_width;
  1904. internal->glyph_matrix = font_matrix;
  1905. internal->glyph_delta = font_offset;
  1906. internal->glyph_transformed = 1;
  1907. }
  1908. else
  1909. {
  1910. FT_BBox cbox;
  1911. FT_Glyph_Metrics* metrics = &glyph->root.metrics;
  1912. /* copy the _unscaled_ advance width */
  1913. metrics->horiAdvance = decoder.glyph_width;
  1914. glyph->root.linearHoriAdvance = decoder.glyph_width;
  1915. glyph->root.internal->glyph_transformed = 0;
  1916. /* make up vertical metrics */
  1917. metrics->vertBearingX = 0;
  1918. metrics->vertBearingY = 0;
  1919. metrics->vertAdvance = 0;
  1920. glyph->root.linearVertAdvance = 0;
  1921. glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
  1922. glyph->root.outline.flags = 0;
  1923. if ( size && size->metrics.y_ppem < 24 )
  1924. glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
  1925. glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
  1926. /* apply the font matrix */
  1927. FT_Outline_Transform( &glyph->root.outline, &font_matrix );
  1928. FT_Outline_Translate( &glyph->root.outline,
  1929. font_offset.x,
  1930. font_offset.y );
  1931. if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
  1932. {
  1933. /* scale the outline and the metrics */
  1934. FT_Int n;
  1935. FT_Outline* cur = &glyph->root.outline;
  1936. FT_Vector* vec = cur->points;
  1937. FT_Fixed x_scale = glyph->x_scale;
  1938. FT_Fixed y_scale = glyph->y_scale;
  1939. /* First of all, scale the points */
  1940. if ( !hinting )
  1941. for ( n = cur->n_points; n > 0; n--, vec++ )
  1942. {
  1943. vec->x = FT_MulFix( vec->x, x_scale );
  1944. vec->y = FT_MulFix( vec->y, y_scale );
  1945. }
  1946. FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
  1947. /* Then scale the metrics */
  1948. metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
  1949. metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
  1950. metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
  1951. metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
  1952. if ( hinting )
  1953. {
  1954. metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64;
  1955. metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64;
  1956. metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64;
  1957. metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64;
  1958. }
  1959. }
  1960. /* compute the other metrics */
  1961. FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
  1962. /* grid fit the bounding box if necessary */
  1963. if ( hinting )
  1964. {
  1965. cbox.xMin &= -64;
  1966. cbox.yMin &= -64;
  1967. cbox.xMax = ( cbox.xMax + 63 ) & -64;
  1968. cbox.yMax = ( cbox.yMax + 63 ) & -64;
  1969. }
  1970. metrics->width = cbox.xMax - cbox.xMin;
  1971. metrics->height = cbox.yMax - cbox.yMin;
  1972. metrics->horiBearingX = cbox.xMin;
  1973. metrics->horiBearingY = cbox.yMax;
  1974. }
  1975. }
  1976. return error;
  1977. }
  1978. /* END */