t1load.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /***************************************************************************/
  2. /* */
  3. /* t1load.c */
  4. /* */
  5. /* Type 1 font 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. /*************************************************************************/
  18. /* */
  19. /* This is the new and improved Type 1 data loader for FreeType 2. The */
  20. /* old loader has several problems: it is slow, complex, difficult to */
  21. /* maintain, and contains incredible hacks to make it accept some */
  22. /* ill-formed Type 1 fonts without hiccup-ing. Moreover, about 5% of */
  23. /* the Type 1 fonts on my machine still aren't loaded correctly by it. */
  24. /* */
  25. /* This version is much simpler, much faster and also easier to read and */
  26. /* maintain by a great order of magnitude. The idea behind it is to */
  27. /* _not_ try to read the Type 1 token stream with a state machine (i.e. */
  28. /* a Postscript-like interpreter) but rather to perform simple pattern */
  29. /* matching. */
  30. /* */
  31. /* Indeed, nearly all data definitions follow a simple pattern like */
  32. /* */
  33. /* ... /Field <data> ... */
  34. /* */
  35. /* where <data> can be a number, a boolean, a string, or an array of */
  36. /* numbers. There are a few exceptions, namely the encoding, font name, */
  37. /* charstrings, and subrs; they are handled with a special pattern */
  38. /* matching routine. */
  39. /* */
  40. /* All other common cases are handled very simply. The matching rules */
  41. /* are defined in the file `t1tokens.h' through the use of several */
  42. /* macros calls PARSE_XXX. */
  43. /* */
  44. /* This file is included twice here; the first time to generate parsing */
  45. /* callback functions, the second to generate a table of keywords (with */
  46. /* pointers to the associated callback). */
  47. /* */
  48. /* The function `parse_dict' simply scans *linearly* a given dictionary */
  49. /* (either the top-level or private one) and calls the appropriate */
  50. /* callback when it encounters an immediate keyword. */
  51. /* */
  52. /* This is by far the fastest way one can find to parse and read all */
  53. /* data. */
  54. /* */
  55. /* This led to tremendous code size reduction. Note that later, the */
  56. /* glyph loader will also be _greatly_ simplified, and the automatic */
  57. /* hinter will replace the clumsy `t1hinter'. */
  58. /* */
  59. /*************************************************************************/
  60. #include <ft2build.h>
  61. #include FT_INTERNAL_DEBUG_H
  62. #include FT_CONFIG_CONFIG_H
  63. #include FT_MULTIPLE_MASTERS_H
  64. #include FT_INTERNAL_TYPE1_TYPES_H
  65. #include "t1load.h"
  66. #include "t1errors.h"
  67. /*************************************************************************/
  68. /* */
  69. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  70. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  71. /* messages during execution. */
  72. /* */
  73. #undef FT_COMPONENT
  74. #define FT_COMPONENT trace_t1load
  75. #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
  76. /*************************************************************************/
  77. /*************************************************************************/
  78. /***** *****/
  79. /***** MULTIPLE MASTERS SUPPORT *****/
  80. /***** *****/
  81. /*************************************************************************/
  82. /*************************************************************************/
  83. static FT_Error
  84. t1_allocate_blend( T1_Face face,
  85. FT_UInt num_designs,
  86. FT_UInt num_axis )
  87. {
  88. PS_Blend blend;
  89. FT_Memory memory = face->root.memory;
  90. FT_Error error = 0;
  91. blend = face->blend;
  92. if ( !blend )
  93. {
  94. if ( FT_NEW( blend ) )
  95. goto Exit;
  96. face->blend = blend;
  97. }
  98. /* allocate design data if needed */
  99. if ( num_designs > 0 )
  100. {
  101. if ( blend->num_designs == 0 )
  102. {
  103. FT_UInt nn;
  104. /* allocate the blend `private' and `font_info' dictionaries */
  105. if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs ) ||
  106. FT_NEW_ARRAY( blend->privates[1], num_designs ) ||
  107. FT_NEW_ARRAY( blend->bboxes[1], num_designs ) ||
  108. FT_NEW_ARRAY( blend->weight_vector, num_designs * 2 ) )
  109. goto Exit;
  110. blend->default_weight_vector = blend->weight_vector + num_designs;
  111. blend->font_infos[0] = &face->type1.font_info;
  112. blend->privates [0] = &face->type1.private_dict;
  113. blend->bboxes [0] = &face->type1.font_bbox;
  114. for ( nn = 2; nn <= num_designs; nn++ )
  115. {
  116. blend->privates[nn] = blend->privates [nn - 1] + 1;
  117. blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
  118. blend->bboxes[nn] = blend->bboxes [nn - 1] + 1;
  119. }
  120. blend->num_designs = num_designs;
  121. }
  122. else if ( blend->num_designs != num_designs )
  123. goto Fail;
  124. }
  125. /* allocate axis data if needed */
  126. if ( num_axis > 0 )
  127. {
  128. if ( blend->num_axis != 0 && blend->num_axis != num_axis )
  129. goto Fail;
  130. blend->num_axis = num_axis;
  131. }
  132. /* allocate the blend design pos table if needed */
  133. num_designs = blend->num_designs;
  134. num_axis = blend->num_axis;
  135. if ( num_designs && num_axis && blend->design_pos[0] == 0 )
  136. {
  137. FT_UInt n;
  138. if ( FT_NEW_ARRAY( blend->design_pos[0], num_designs * num_axis ) )
  139. goto Exit;
  140. for ( n = 1; n < num_designs; n++ )
  141. blend->design_pos[n] = blend->design_pos[0] + num_axis * n;
  142. }
  143. Exit:
  144. return error;
  145. Fail:
  146. error = -1;
  147. goto Exit;
  148. }
  149. FT_LOCAL_DEF( FT_Error )
  150. T1_Get_Multi_Master( T1_Face face,
  151. FT_Multi_Master* master )
  152. {
  153. PS_Blend blend = face->blend;
  154. FT_UInt n;
  155. FT_Error error;
  156. error = T1_Err_Invalid_Argument;
  157. if ( blend )
  158. {
  159. master->num_axis = blend->num_axis;
  160. master->num_designs = blend->num_designs;
  161. for ( n = 0; n < blend->num_axis; n++ )
  162. {
  163. FT_MM_Axis* axis = master->axis + n;
  164. PS_DesignMap map = blend->design_map + n;
  165. axis->name = blend->axis_names[n];
  166. axis->minimum = map->design_points[0];
  167. axis->maximum = map->design_points[map->num_points - 1];
  168. }
  169. error = 0;
  170. }
  171. return error;
  172. }
  173. FT_LOCAL_DEF( FT_Error )
  174. T1_Set_MM_Blend( T1_Face face,
  175. FT_UInt num_coords,
  176. FT_Fixed* coords )
  177. {
  178. PS_Blend blend = face->blend;
  179. FT_Error error;
  180. FT_UInt n, m;
  181. error = T1_Err_Invalid_Argument;
  182. if ( blend && blend->num_axis == num_coords )
  183. {
  184. /* recompute the weight vector from the blend coordinates */
  185. error = T1_Err_Ok;
  186. for ( n = 0; n < blend->num_designs; n++ )
  187. {
  188. FT_Fixed result = 0x10000L; /* 1.0 fixed */
  189. for ( m = 0; m < blend->num_axis; m++ )
  190. {
  191. FT_Fixed factor;
  192. /* get current blend axis position */
  193. factor = coords[m];
  194. if ( factor < 0 ) factor = 0;
  195. if ( factor > 0x10000L ) factor = 0x10000L;
  196. if ( ( n & ( 1 << m ) ) == 0 )
  197. factor = 0x10000L - factor;
  198. result = FT_MulFix( result, factor );
  199. }
  200. blend->weight_vector[n] = result;
  201. }
  202. error = T1_Err_Ok;
  203. }
  204. return error;
  205. }
  206. FT_LOCAL_DEF( FT_Error )
  207. T1_Set_MM_Design( T1_Face face,
  208. FT_UInt num_coords,
  209. FT_Long* coords )
  210. {
  211. PS_Blend blend = face->blend;
  212. FT_Error error;
  213. FT_UInt n, p;
  214. error = T1_Err_Invalid_Argument;
  215. if ( blend && blend->num_axis == num_coords )
  216. {
  217. /* compute the blend coordinates through the blend design map */
  218. FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
  219. for ( n = 0; n < blend->num_axis; n++ )
  220. {
  221. FT_Long design = coords[n];
  222. FT_Fixed the_blend;
  223. PS_DesignMap map = blend->design_map + n;
  224. FT_Fixed* designs = map->design_points;
  225. FT_Fixed* blends = map->blend_points;
  226. FT_Int before = -1, after = -1;
  227. for ( p = 0; p < (FT_UInt)map->num_points; p++ )
  228. {
  229. FT_Fixed p_design = designs[p];
  230. /* exact match ? */
  231. if ( design == p_design )
  232. {
  233. the_blend = blends[p];
  234. goto Found;
  235. }
  236. if ( design < p_design )
  237. {
  238. after = p;
  239. break;
  240. }
  241. before = p;
  242. }
  243. /* now, interpolate if needed */
  244. if ( before < 0 )
  245. the_blend = blends[0];
  246. else if ( after < 0 )
  247. the_blend = blends[map->num_points - 1];
  248. else
  249. the_blend = FT_MulDiv( design - designs[before],
  250. blends [after] - blends [before],
  251. designs[after] - designs[before] );
  252. Found:
  253. final_blends[n] = the_blend;
  254. }
  255. error = T1_Set_MM_Blend( face, num_coords, final_blends );
  256. }
  257. return error;
  258. }
  259. FT_LOCAL_DEF( void )
  260. T1_Done_Blend( T1_Face face )
  261. {
  262. FT_Memory memory = face->root.memory;
  263. PS_Blend blend = face->blend;
  264. if ( blend )
  265. {
  266. FT_UInt num_designs = blend->num_designs;
  267. FT_UInt num_axis = blend->num_axis;
  268. FT_UInt n;
  269. /* release design pos table */
  270. FT_FREE( blend->design_pos[0] );
  271. for ( n = 1; n < num_designs; n++ )
  272. blend->design_pos[n] = 0;
  273. /* release blend `private' and `font info' dictionaries */
  274. FT_FREE( blend->privates[1] );
  275. FT_FREE( blend->font_infos[1] );
  276. FT_FREE( blend->bboxes[1] );
  277. for ( n = 0; n < num_designs; n++ )
  278. {
  279. blend->privates [n] = 0;
  280. blend->font_infos[n] = 0;
  281. blend->bboxes [n] = 0;
  282. }
  283. /* release weight vectors */
  284. FT_FREE( blend->weight_vector );
  285. blend->default_weight_vector = 0;
  286. /* release axis names */
  287. for ( n = 0; n < num_axis; n++ )
  288. FT_FREE( blend->axis_names[n] );
  289. /* release design map */
  290. for ( n = 0; n < num_axis; n++ )
  291. {
  292. PS_DesignMap dmap = blend->design_map + n;
  293. FT_FREE( dmap->design_points );
  294. dmap->num_points = 0;
  295. }
  296. FT_FREE( face->blend );
  297. }
  298. }
  299. static void
  300. parse_blend_axis_types( T1_Face face,
  301. T1_Loader loader )
  302. {
  303. T1_TokenRec axis_tokens[ T1_MAX_MM_AXIS ];
  304. FT_Int n, num_axis;
  305. FT_Error error = 0;
  306. PS_Blend blend;
  307. FT_Memory memory;
  308. /* take an array of objects */
  309. T1_ToTokenArray( &loader->parser, axis_tokens,
  310. T1_MAX_MM_AXIS, &num_axis );
  311. if ( num_axis <= 0 || num_axis > T1_MAX_MM_AXIS )
  312. {
  313. FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
  314. num_axis ));
  315. error = T1_Err_Invalid_File_Format;
  316. goto Exit;
  317. }
  318. /* allocate blend if necessary */
  319. error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
  320. if ( error )
  321. goto Exit;
  322. blend = face->blend;
  323. memory = face->root.memory;
  324. /* each token is an immediate containing the name of the axis */
  325. for ( n = 0; n < num_axis; n++ )
  326. {
  327. T1_Token token = axis_tokens + n;
  328. FT_Byte* name;
  329. FT_PtrDist len;
  330. /* skip first slash, if any */
  331. if ( token->start[0] == '/' )
  332. token->start++;
  333. len = token->limit - token->start;
  334. if ( len <= 0 )
  335. {
  336. error = T1_Err_Invalid_File_Format;
  337. goto Exit;
  338. }
  339. if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
  340. goto Exit;
  341. name = (FT_Byte*)blend->axis_names[n];
  342. FT_MEM_COPY( name, token->start, len );
  343. name[len] = 0;
  344. }
  345. Exit:
  346. loader->parser.root.error = error;
  347. }
  348. static void
  349. parse_blend_design_positions( T1_Face face,
  350. T1_Loader loader )
  351. {
  352. T1_TokenRec design_tokens[ T1_MAX_MM_DESIGNS ];
  353. FT_Int num_designs;
  354. FT_Int num_axis;
  355. T1_Parser parser = &loader->parser;
  356. FT_Error error = 0;
  357. PS_Blend blend;
  358. /* get the array of design tokens - compute number of designs */
  359. T1_ToTokenArray( parser, design_tokens, T1_MAX_MM_DESIGNS, &num_designs );
  360. if ( num_designs <= 0 || num_designs > T1_MAX_MM_DESIGNS )
  361. {
  362. FT_ERROR(( "parse_blend_design_positions:" ));
  363. FT_ERROR(( " incorrect number of designs: %d\n",
  364. num_designs ));
  365. error = T1_Err_Invalid_File_Format;
  366. goto Exit;
  367. }
  368. {
  369. FT_Byte* old_cursor = parser->root.cursor;
  370. FT_Byte* old_limit = parser->root.limit;
  371. FT_UInt n;
  372. blend = face->blend;
  373. num_axis = 0; /* make compiler happy */
  374. for ( n = 0; n < (FT_UInt)num_designs; n++ )
  375. {
  376. T1_TokenRec axis_tokens[ T1_MAX_MM_DESIGNS ];
  377. T1_Token token;
  378. FT_Int axis, n_axis;
  379. /* read axis/coordinates tokens */
  380. token = design_tokens + n;
  381. parser->root.cursor = token->start - 1;
  382. parser->root.limit = token->limit + 1;
  383. T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
  384. if ( n == 0 )
  385. {
  386. num_axis = n_axis;
  387. error = t1_allocate_blend( face, num_designs, num_axis );
  388. if ( error )
  389. goto Exit;
  390. blend = face->blend;
  391. }
  392. else if ( n_axis != num_axis )
  393. {
  394. FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
  395. error = T1_Err_Invalid_File_Format;
  396. goto Exit;
  397. }
  398. /* now, read each axis token into the design position */
  399. for ( axis = 0; axis < n_axis; axis++ )
  400. {
  401. T1_Token token2 = axis_tokens + axis;
  402. parser->root.cursor = token2->start;
  403. parser->root.limit = token2->limit;
  404. blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
  405. }
  406. }
  407. loader->parser.root.cursor = old_cursor;
  408. loader->parser.root.limit = old_limit;
  409. }
  410. Exit:
  411. loader->parser.root.error = error;
  412. }
  413. static void
  414. parse_blend_design_map( T1_Face face,
  415. T1_Loader loader )
  416. {
  417. FT_Error error = 0;
  418. T1_Parser parser = &loader->parser;
  419. PS_Blend blend;
  420. T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
  421. FT_Int n, num_axis;
  422. FT_Byte* old_cursor;
  423. FT_Byte* old_limit;
  424. FT_Memory memory = face->root.memory;
  425. T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &num_axis );
  426. if ( num_axis <= 0 || num_axis > T1_MAX_MM_AXIS )
  427. {
  428. FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
  429. num_axis ));
  430. error = T1_Err_Invalid_File_Format;
  431. goto Exit;
  432. }
  433. old_cursor = parser->root.cursor;
  434. old_limit = parser->root.limit;
  435. error = t1_allocate_blend( face, 0, num_axis );
  436. if ( error )
  437. goto Exit;
  438. blend = face->blend;
  439. /* now, read each axis design map */
  440. for ( n = 0; n < num_axis; n++ )
  441. {
  442. PS_DesignMap map = blend->design_map + n;
  443. T1_Token token;
  444. FT_Int p, num_points;
  445. token = axis_tokens + n;
  446. parser->root.cursor = token->start;
  447. parser->root.limit = token->limit;
  448. /* count the number of map points */
  449. {
  450. FT_Byte* ptr = token->start;
  451. FT_Byte* limit = token->limit;
  452. num_points = 0;
  453. for ( ; ptr < limit; ptr++ )
  454. if ( ptr[0] == '[' )
  455. num_points++;
  456. }
  457. if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
  458. {
  459. FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
  460. error = T1_Err_Invalid_File_Format;
  461. goto Exit;
  462. }
  463. /* allocate design map data */
  464. if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
  465. goto Exit;
  466. map->blend_points = map->design_points + num_points;
  467. map->num_points = (FT_Byte)num_points;
  468. for ( p = 0; p < num_points; p++ )
  469. {
  470. map->design_points[p] = T1_ToInt( parser );
  471. map->blend_points [p] = T1_ToFixed( parser, 0 );
  472. }
  473. }
  474. parser->root.cursor = old_cursor;
  475. parser->root.limit = old_limit;
  476. Exit:
  477. parser->root.error = error;
  478. }
  479. static void
  480. parse_weight_vector( T1_Face face,
  481. T1_Loader loader )
  482. {
  483. FT_Error error = 0;
  484. T1_Parser parser = &loader->parser;
  485. PS_Blend blend = face->blend;
  486. T1_TokenRec master;
  487. FT_UInt n;
  488. FT_Byte* old_cursor;
  489. FT_Byte* old_limit;
  490. if ( !blend || blend->num_designs == 0 )
  491. {
  492. FT_ERROR(( "parse_weight_vector: too early!\n" ));
  493. error = T1_Err_Invalid_File_Format;
  494. goto Exit;
  495. }
  496. T1_ToToken( parser, &master );
  497. if ( master.type != T1_TOKEN_TYPE_ARRAY )
  498. {
  499. FT_ERROR(( "parse_weight_vector: incorrect format!\n" ));
  500. error = T1_Err_Invalid_File_Format;
  501. goto Exit;
  502. }
  503. old_cursor = parser->root.cursor;
  504. old_limit = parser->root.limit;
  505. parser->root.cursor = master.start;
  506. parser->root.limit = master.limit;
  507. for ( n = 0; n < blend->num_designs; n++ )
  508. {
  509. blend->default_weight_vector[n] =
  510. blend->weight_vector[n] = T1_ToFixed( parser, 0 );
  511. }
  512. parser->root.cursor = old_cursor;
  513. parser->root.limit = old_limit;
  514. Exit:
  515. parser->root.error = error;
  516. }
  517. /* the keyword `/shareddict' appears in some multiple master fonts */
  518. /* with a lot of Postscript garbage behind it (that's completely out */
  519. /* of spec!); we detect it and terminate the parsing */
  520. /* */
  521. static void
  522. parse_shared_dict( T1_Face face,
  523. T1_Loader loader )
  524. {
  525. T1_Parser parser = &loader->parser;
  526. FT_UNUSED( face );
  527. parser->root.cursor = parser->root.limit;
  528. parser->root.error = 0;
  529. }
  530. #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
  531. /*************************************************************************/
  532. /*************************************************************************/
  533. /***** *****/
  534. /***** TYPE 1 SYMBOL PARSING *****/
  535. /***** *****/
  536. /*************************************************************************/
  537. /*************************************************************************/
  538. /*************************************************************************/
  539. /* */
  540. /* First of all, define the token field static variables. This is a set */
  541. /* of T1_FieldRec variables used later. */
  542. /* */
  543. /*************************************************************************/
  544. static FT_Error
  545. t1_load_keyword( T1_Face face,
  546. T1_Loader loader,
  547. T1_Field field )
  548. {
  549. FT_Error error;
  550. void* dummy_object;
  551. void** objects;
  552. FT_UInt max_objects;
  553. PS_Blend blend = face->blend;
  554. /* if the keyword has a dedicated callback, call it */
  555. if ( field->type == T1_FIELD_TYPE_CALLBACK )
  556. {
  557. field->reader( (FT_Face)face, loader );
  558. error = loader->parser.root.error;
  559. goto Exit;
  560. }
  561. /* now, the keyword is either a simple field, or a table of fields; */
  562. /* we are now going to take care of it */
  563. switch ( field->location )
  564. {
  565. case T1_FIELD_LOCATION_FONT_INFO:
  566. dummy_object = &face->type1.font_info;
  567. objects = &dummy_object;
  568. max_objects = 0;
  569. if ( blend )
  570. {
  571. objects = (void**)blend->font_infos;
  572. max_objects = blend->num_designs;
  573. }
  574. break;
  575. case T1_FIELD_LOCATION_PRIVATE:
  576. dummy_object = &face->type1.private_dict;
  577. objects = &dummy_object;
  578. max_objects = 0;
  579. if ( blend )
  580. {
  581. objects = (void**)blend->privates;
  582. max_objects = blend->num_designs;
  583. }
  584. break;
  585. case T1_FIELD_LOCATION_BBOX:
  586. dummy_object = &face->type1.font_bbox;
  587. objects = &dummy_object;
  588. max_objects = 0;
  589. if ( blend )
  590. {
  591. objects = (void**)blend->bboxes;
  592. max_objects = blend->num_designs;
  593. }
  594. break;
  595. default:
  596. dummy_object = &face->type1;
  597. objects = &dummy_object;
  598. max_objects = 0;
  599. }
  600. if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
  601. field->type == T1_FIELD_TYPE_FIXED_ARRAY )
  602. error = T1_Load_Field_Table( &loader->parser, field,
  603. objects, max_objects, 0 );
  604. else
  605. error = T1_Load_Field( &loader->parser, field,
  606. objects, max_objects, 0 );
  607. Exit:
  608. return error;
  609. }
  610. static int
  611. is_space( FT_Byte c )
  612. {
  613. return ( c == ' ' || c == '\t' || c == '\r' || c == '\n' );
  614. }
  615. static int
  616. is_alpha( FT_Byte c )
  617. {
  618. /* Note: we must accept "+" as a valid character, as it is used in */
  619. /* embedded type1 fonts in PDF documents. */
  620. /* */
  621. return ( ft_isalnum( c ) ||
  622. c == '.' ||
  623. c == '_' ||
  624. c == '-' ||
  625. c == '+' );
  626. }
  627. static int
  628. read_binary_data( T1_Parser parser,
  629. FT_Long* size,
  630. FT_Byte** base )
  631. {
  632. FT_Byte* cur;
  633. FT_Byte* limit = parser->root.limit;
  634. /* the binary data has the following format */
  635. /* */
  636. /* `size' [white*] RD white ....... ND */
  637. /* */
  638. T1_Skip_Spaces( parser );
  639. cur = parser->root.cursor;
  640. if ( cur < limit && (FT_Byte)( *cur - '0' ) < 10 )
  641. {
  642. *size = T1_ToInt( parser );
  643. T1_Skip_Spaces( parser );
  644. T1_Skip_Alpha ( parser ); /* `RD' or `-|' or something else */
  645. /* there is only one whitespace char after the */
  646. /* `RD' or `-|' token */
  647. *base = parser->root.cursor + 1;
  648. parser->root.cursor += *size + 1;
  649. return 1;
  650. }
  651. FT_ERROR(( "read_binary_data: invalid size field\n" ));
  652. parser->root.error = T1_Err_Invalid_File_Format;
  653. return 0;
  654. }
  655. /* we will now define the routines used to handle */
  656. /* the `/Encoding', `/Subrs', and `/CharStrings' */
  657. /* dictionaries */
  658. static void
  659. parse_font_name( T1_Face face,
  660. T1_Loader loader )
  661. {
  662. T1_Parser parser = &loader->parser;
  663. FT_Error error;
  664. FT_Memory memory = parser->root.memory;
  665. FT_PtrDist len;
  666. FT_Byte* cur;
  667. FT_Byte* cur2;
  668. FT_Byte* limit;
  669. if ( face->type1.font_name )
  670. /* with synthetic fonts, it's possible we get here twice */
  671. return;
  672. T1_Skip_Spaces( parser );
  673. cur = parser->root.cursor;
  674. limit = parser->root.limit;
  675. if ( cur >= limit - 1 || *cur != '/' )
  676. return;
  677. cur++;
  678. cur2 = cur;
  679. while ( cur2 < limit && is_alpha( *cur2 ) )
  680. cur2++;
  681. len = cur2 - cur;
  682. if ( len > 0 )
  683. {
  684. if ( FT_ALLOC( face->type1.font_name, len + 1 ) )
  685. {
  686. parser->root.error = error;
  687. return;
  688. }
  689. FT_MEM_COPY( face->type1.font_name, cur, len );
  690. face->type1.font_name[len] = '\0';
  691. }
  692. parser->root.cursor = cur2;
  693. }
  694. #if 0
  695. static void
  696. parse_font_bbox( T1_Face face,
  697. T1_Loader loader )
  698. {
  699. T1_Parser parser = &loader->parser;
  700. FT_Fixed temp[4];
  701. FT_BBox* bbox = &face->type1.font_bbox;
  702. (void)T1_ToFixedArray( parser, 4, temp, 0 );
  703. bbox->xMin = FT_RoundFix( temp[0] );
  704. bbox->yMin = FT_RoundFix( temp[1] );
  705. bbox->xMax = FT_RoundFix( temp[2] );
  706. bbox->yMax = FT_RoundFix( temp[3] );
  707. }
  708. #endif
  709. static void
  710. parse_font_matrix( T1_Face face,
  711. T1_Loader loader )
  712. {
  713. T1_Parser parser = &loader->parser;
  714. FT_Matrix* matrix = &face->type1.font_matrix;
  715. FT_Vector* offset = &face->type1.font_offset;
  716. FT_Face root = (FT_Face)&face->root;
  717. FT_Fixed temp[6];
  718. FT_Fixed temp_scale;
  719. if ( matrix->xx || matrix->yx )
  720. /* with synthetic fonts, it's possible we get here twice */
  721. return;
  722. (void)T1_ToFixedArray( parser, 6, temp, 3 );
  723. temp_scale = ABS( temp[3] );
  724. /* Set Units per EM based on FontMatrix values. We set the value to */
  725. /* 1000 / temp_scale, because temp_scale was already multiplied by */
  726. /* 1000 (in t1_tofixed, from psobjs.c). */
  727. root->units_per_EM = (FT_UShort)( FT_DivFix( 1000 * 0x10000L,
  728. temp_scale ) >> 16 );
  729. /* we need to scale the values by 1.0/temp_scale */
  730. if ( temp_scale != 0x10000L )
  731. {
  732. temp[0] = FT_DivFix( temp[0], temp_scale );
  733. temp[1] = FT_DivFix( temp[1], temp_scale );
  734. temp[2] = FT_DivFix( temp[2], temp_scale );
  735. temp[4] = FT_DivFix( temp[4], temp_scale );
  736. temp[5] = FT_DivFix( temp[5], temp_scale );
  737. temp[3] = 0x10000L;
  738. }
  739. matrix->xx = temp[0];
  740. matrix->yx = temp[1];
  741. matrix->xy = temp[2];
  742. matrix->yy = temp[3];
  743. /* note that the offsets must be expressed in integer font units */
  744. offset->x = temp[4] >> 16;
  745. offset->y = temp[5] >> 16;
  746. }
  747. static void
  748. parse_encoding( T1_Face face,
  749. T1_Loader loader )
  750. {
  751. T1_Parser parser = &loader->parser;
  752. FT_Byte* cur = parser->root.cursor;
  753. FT_Byte* limit = parser->root.limit;
  754. PSAux_Service psaux = (PSAux_Service)face->psaux;
  755. /* skip whitespace */
  756. while ( is_space( *cur ) )
  757. {
  758. cur++;
  759. if ( cur >= limit )
  760. {
  761. FT_ERROR(( "parse_encoding: out of bounds!\n" ));
  762. parser->root.error = T1_Err_Invalid_File_Format;
  763. return;
  764. }
  765. }
  766. /* if we have a number, then the encoding is an array, */
  767. /* and we must load it now */
  768. if ( (FT_Byte)( *cur - '0' ) < 10 )
  769. {
  770. T1_Encoding encode = &face->type1.encoding;
  771. FT_Int count, n;
  772. PS_Table char_table = &loader->encoding_table;
  773. FT_Memory memory = parser->root.memory;
  774. FT_Error error;
  775. if ( encode->char_index )
  776. /* with synthetic fonts, it's possible we get here twice */
  777. return;
  778. /* read the number of entries in the encoding, should be 256 */
  779. count = (FT_Int)T1_ToInt( parser );
  780. if ( parser->root.error )
  781. return;
  782. /* we use a T1_Table to store our charnames */
  783. loader->num_chars = encode->num_chars = count;
  784. if ( FT_NEW_ARRAY( encode->char_index, count ) ||
  785. FT_NEW_ARRAY( encode->char_name, count ) ||
  786. FT_SET_ERROR( psaux->ps_table_funcs->init(
  787. char_table, count, memory ) ) )
  788. {
  789. parser->root.error = error;
  790. return;
  791. }
  792. /* We need to `zero' out encoding_table.elements */
  793. for ( n = 0; n < count; n++ )
  794. {
  795. char* notdef = (char *)".notdef";
  796. T1_Add_Table( char_table, n, notdef, 8 );
  797. }
  798. /* Now, we will need to read a record of the form */
  799. /* ... charcode /charname ... for each entry in our table */
  800. /* */
  801. /* We simply look for a number followed by an immediate */
  802. /* name. Note that this ignores correctly the sequence */
  803. /* that is often seen in type1 fonts: */
  804. /* */
  805. /* 0 1 255 { 1 index exch /.notdef put } for dup */
  806. /* */
  807. /* used to clean the encoding array before anything else. */
  808. /* */
  809. /* We stop when we encounter a `def'. */
  810. cur = parser->root.cursor;
  811. limit = parser->root.limit;
  812. n = 0;
  813. for ( ; cur < limit; )
  814. {
  815. FT_Byte c;
  816. c = *cur;
  817. /* we stop when we encounter a `def' */
  818. if ( c == 'd' && cur + 3 < limit )
  819. {
  820. if ( cur[1] == 'e' &&
  821. cur[2] == 'f' &&
  822. is_space( cur[-1] ) &&
  823. is_space( cur[3] ) )
  824. {
  825. FT_TRACE6(( "encoding end\n" ));
  826. break;
  827. }
  828. }
  829. /* otherwise, we must find a number before anything else */
  830. if ( (FT_Byte)( c - '0' ) < 10 )
  831. {
  832. FT_Int charcode;
  833. parser->root.cursor = cur;
  834. charcode = (FT_Int)T1_ToInt( parser );
  835. cur = parser->root.cursor;
  836. /* skip whitespace */
  837. while ( cur < limit && is_space( *cur ) )
  838. cur++;
  839. if ( cur < limit && *cur == '/' )
  840. {
  841. /* bingo, we have an immediate name -- it must be a */
  842. /* character name */
  843. FT_Byte* cur2 = cur + 1;
  844. FT_PtrDist len;
  845. while ( cur2 < limit && is_alpha( *cur2 ) )
  846. cur2++;
  847. len = cur2 - cur - 1;
  848. parser->root.error = T1_Add_Table( char_table, charcode,
  849. cur + 1, len + 1 );
  850. char_table->elements[charcode][len] = '\0';
  851. if ( parser->root.error )
  852. return;
  853. cur = cur2;
  854. }
  855. }
  856. else
  857. cur++;
  858. }
  859. face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
  860. parser->root.cursor = cur;
  861. }
  862. /* Otherwise, we should have either `StandardEncoding', */
  863. /* `ExpertEncoding', or `ISOLatin1Encoding' */
  864. else
  865. {
  866. if ( cur + 17 < limit &&
  867. ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
  868. face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
  869. else if ( cur + 15 < limit &&
  870. ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
  871. face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
  872. else if ( cur + 18 < limit &&
  873. ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
  874. face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
  875. else
  876. {
  877. FT_ERROR(( "parse_encoding: invalid token!\n" ));
  878. parser->root.error = T1_Err_Invalid_File_Format;
  879. }
  880. }
  881. }
  882. static void
  883. parse_subrs( T1_Face face,
  884. T1_Loader loader )
  885. {
  886. T1_Parser parser = &loader->parser;
  887. PS_Table table = &loader->subrs;
  888. FT_Memory memory = parser->root.memory;
  889. FT_Error error;
  890. FT_Int n;
  891. PSAux_Service psaux = (PSAux_Service)face->psaux;
  892. if ( loader->num_subrs )
  893. /* with synthetic fonts, it's possible we get here twice */
  894. return;
  895. loader->num_subrs = (FT_Int)T1_ToInt( parser );
  896. if ( parser->root.error )
  897. return;
  898. /* position the parser right before the `dup' of the first subr */
  899. T1_Skip_Spaces( parser );
  900. T1_Skip_Alpha( parser ); /* `array' */
  901. T1_Skip_Spaces( parser );
  902. /* initialize subrs array */
  903. error = psaux->ps_table_funcs->init( table, loader->num_subrs, memory );
  904. if ( error )
  905. goto Fail;
  906. /* the format is simple: */
  907. /* */
  908. /* `index' + binary data */
  909. /* */
  910. for ( n = 0; n < loader->num_subrs; n++ )
  911. {
  912. FT_Long idx, size;
  913. FT_Byte* base;
  914. /* If the next token isn't `dup', we are also done. This */
  915. /* happens when there are `holes' in the Subrs array. */
  916. if ( ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
  917. break;
  918. idx = T1_ToInt( parser );
  919. if ( !read_binary_data( parser, &size, &base ) )
  920. return;
  921. /* The binary string is followed by one token, e.g. `NP' */
  922. /* (bound to `noaccess put') or by two separate tokens: */
  923. /* `noaccess' & `put'. We position the parser right */
  924. /* before the next `dup', if any. */
  925. T1_Skip_Spaces( parser );
  926. T1_Skip_Alpha( parser ); /* `NP' or `I' or `noaccess' */
  927. T1_Skip_Spaces( parser );
  928. if ( ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
  929. {
  930. T1_Skip_Alpha( parser ); /* skip `put' */
  931. T1_Skip_Spaces( parser );
  932. }
  933. /* some fonts use a value of -1 for lenIV to indicate that */
  934. /* the charstrings are unencoded */
  935. /* */
  936. /* thanks to Tom Kacvinsky for pointing this out */
  937. /* */
  938. if ( face->type1.private_dict.lenIV >= 0 )
  939. {
  940. FT_Byte* temp;
  941. /* t1_decrypt() shouldn't write to base -- make temporary copy */
  942. if ( FT_ALLOC( temp, size ) )
  943. goto Fail;
  944. FT_MEM_COPY( temp, base, size );
  945. psaux->t1_decrypt( temp, size, 4330 );
  946. size -= face->type1.private_dict.lenIV;
  947. error = T1_Add_Table( table, idx,
  948. temp + face->type1.private_dict.lenIV, size );
  949. FT_FREE( temp );
  950. }
  951. else
  952. error = T1_Add_Table( table, idx, base, size );
  953. if ( error )
  954. goto Fail;
  955. }
  956. return;
  957. Fail:
  958. parser->root.error = error;
  959. }
  960. static void
  961. parse_charstrings( T1_Face face,
  962. T1_Loader loader )
  963. {
  964. T1_Parser parser = &loader->parser;
  965. PS_Table code_table = &loader->charstrings;
  966. PS_Table name_table = &loader->glyph_names;
  967. PS_Table swap_table = &loader->swap_table;
  968. FT_Memory memory = parser->root.memory;
  969. FT_Error error;
  970. PSAux_Service psaux = (PSAux_Service)face->psaux;
  971. FT_Byte* cur;
  972. FT_Byte* limit = parser->root.limit;
  973. FT_Int n;
  974. FT_UInt notdef_index = 0;
  975. FT_Byte notdef_found = 0;
  976. if ( loader->num_glyphs )
  977. /* with synthetic fonts, it's possible we get here twice */
  978. return;
  979. loader->num_glyphs = (FT_Int)T1_ToInt( parser );
  980. if ( parser->root.error )
  981. return;
  982. /* initialize tables (leaving room for addition of .notdef, */
  983. /* if necessary). */
  984. error = psaux->ps_table_funcs->init( code_table,
  985. loader->num_glyphs + 1,
  986. memory );
  987. if ( error )
  988. goto Fail;
  989. error = psaux->ps_table_funcs->init( name_table,
  990. loader->num_glyphs + 1,
  991. memory );
  992. if ( error )
  993. goto Fail;
  994. /* Initialize table for swapping index notdef_index and */
  995. /* index 0 names and codes (if necessary). */
  996. error = psaux->ps_table_funcs->init( swap_table, 4, memory );
  997. if ( error )
  998. goto Fail;
  999. n = 0;
  1000. for (;;)
  1001. {
  1002. FT_Long size;
  1003. FT_Byte* base;
  1004. /* the format is simple: */
  1005. /* `/glyphname' + binary data */
  1006. /* */
  1007. /* note that we stop when we find a `def' */
  1008. /* */
  1009. T1_Skip_Spaces( parser );
  1010. cur = parser->root.cursor;
  1011. if ( cur >= limit )
  1012. break;
  1013. /* we stop when we find a `def' or `end' keyword */
  1014. if ( *cur == 'd' &&
  1015. cur + 3 < limit &&
  1016. cur[1] == 'e' &&
  1017. cur[2] == 'f' )
  1018. break;
  1019. if ( *cur == 'e' &&
  1020. cur + 3 < limit &&
  1021. cur[1] == 'n' &&
  1022. cur[2] == 'd' )
  1023. break;
  1024. if ( *cur != '/' )
  1025. T1_Skip_Alpha( parser );
  1026. else
  1027. {
  1028. FT_Byte* cur2 = cur + 1;
  1029. FT_PtrDist len;
  1030. while ( cur2 < limit && is_alpha( *cur2 ) )
  1031. cur2++;
  1032. len = cur2 - cur - 1;
  1033. error = T1_Add_Table( name_table, n, cur + 1, len + 1 );
  1034. if ( error )
  1035. goto Fail;
  1036. /* add a trailing zero to the name table */
  1037. name_table->elements[n][len] = '\0';
  1038. /* record index of /.notdef */
  1039. if ( ft_strcmp( (const char*)".notdef",
  1040. (const char*)(name_table->elements[n]) ) == 0 )
  1041. {
  1042. notdef_index = n;
  1043. notdef_found = 1;
  1044. }
  1045. parser->root.cursor = cur2;
  1046. if ( !read_binary_data( parser, &size, &base ) )
  1047. return;
  1048. if ( face->type1.private_dict.lenIV >= 0 )
  1049. {
  1050. FT_Byte* temp;
  1051. /* t1_decrypt() shouldn't write to base -- make temporary copy */
  1052. if ( FT_ALLOC( temp, size ) )
  1053. goto Fail;
  1054. FT_MEM_COPY( temp, base, size );
  1055. psaux->t1_decrypt( temp, size, 4330 );
  1056. size -= face->type1.private_dict.lenIV;
  1057. error = T1_Add_Table( code_table, n,
  1058. temp + face->type1.private_dict.lenIV, size );
  1059. FT_FREE( temp );
  1060. }
  1061. else
  1062. error = T1_Add_Table( code_table, n, base, size );
  1063. if ( error )
  1064. goto Fail;
  1065. n++;
  1066. if ( n >= loader->num_glyphs )
  1067. break;
  1068. }
  1069. }
  1070. loader->num_glyphs = n;
  1071. /* if /.notdef is found but does not occupy index 0, do our magic. */
  1072. if ( ft_strcmp( (const char*)".notdef",
  1073. (const char*)name_table->elements[0] ) &&
  1074. notdef_found )
  1075. {
  1076. /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
  1077. /* name and code entries to swap_table. Then place notdef_index name */
  1078. /* and code entries into swap_table. Then swap name and code */
  1079. /* entries at indices notdef_index and 0 using values stored in */
  1080. /* swap_table. */
  1081. /* Index 0 name */
  1082. error = T1_Add_Table( swap_table, 0,
  1083. name_table->elements[0],
  1084. name_table->lengths [0] );
  1085. if ( error )
  1086. goto Fail;
  1087. /* Index 0 code */
  1088. error = T1_Add_Table( swap_table, 1,
  1089. code_table->elements[0],
  1090. code_table->lengths [0] );
  1091. if ( error )
  1092. goto Fail;
  1093. /* Index notdef_index name */
  1094. error = T1_Add_Table( swap_table, 2,
  1095. name_table->elements[notdef_index],
  1096. name_table->lengths [notdef_index] );
  1097. if ( error )
  1098. goto Fail;
  1099. /* Index notdef_index code */
  1100. error = T1_Add_Table( swap_table, 3,
  1101. code_table->elements[notdef_index],
  1102. code_table->lengths [notdef_index] );
  1103. if ( error )
  1104. goto Fail;
  1105. error = T1_Add_Table( name_table, notdef_index,
  1106. swap_table->elements[0],
  1107. swap_table->lengths [0] );
  1108. if ( error )
  1109. goto Fail;
  1110. error = T1_Add_Table( code_table, notdef_index,
  1111. swap_table->elements[1],
  1112. swap_table->lengths [1] );
  1113. if ( error )
  1114. goto Fail;
  1115. error = T1_Add_Table( name_table, 0,
  1116. swap_table->elements[2],
  1117. swap_table->lengths [2] );
  1118. if ( error )
  1119. goto Fail;
  1120. error = T1_Add_Table( code_table, 0,
  1121. swap_table->elements[3],
  1122. swap_table->lengths [3] );
  1123. if ( error )
  1124. goto Fail;
  1125. }
  1126. else if ( !notdef_found )
  1127. {
  1128. /* notdef_index is already 0, or /.notdef is undefined in */
  1129. /* charstrings dictionary. Worry about /.notdef undefined. */
  1130. /* We take index 0 and add it to the end of the table(s) */
  1131. /* and add our own /.notdef glyph to index 0. */
  1132. /* 0 333 hsbw endchar */
  1133. FT_Byte notdef_glyph[] = {0x8B, 0xF7, 0xE1, 0x0D, 0x0E};
  1134. char* notdef_name = (char *)".notdef";
  1135. error = T1_Add_Table( swap_table, 0,
  1136. name_table->elements[0],
  1137. name_table->lengths [0] );
  1138. if ( error )
  1139. goto Fail;
  1140. error = T1_Add_Table( swap_table, 1,
  1141. code_table->elements[0],
  1142. code_table->lengths [0] );
  1143. if ( error )
  1144. goto Fail;
  1145. error = T1_Add_Table( name_table, 0, notdef_name, 8 );
  1146. if ( error )
  1147. goto Fail;
  1148. error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
  1149. if ( error )
  1150. goto Fail;
  1151. error = T1_Add_Table( name_table, n,
  1152. swap_table->elements[0],
  1153. swap_table->lengths [0] );
  1154. if ( error )
  1155. goto Fail;
  1156. error = T1_Add_Table( code_table, n,
  1157. swap_table->elements[1],
  1158. swap_table->lengths [1] );
  1159. if ( error )
  1160. goto Fail;
  1161. /* we added a glyph. */
  1162. loader->num_glyphs = n + 1;
  1163. }
  1164. return;
  1165. Fail:
  1166. parser->root.error = error;
  1167. }
  1168. static
  1169. const T1_FieldRec t1_keywords[] =
  1170. {
  1171. #include "t1tokens.h"
  1172. /* now add the special functions... */
  1173. T1_FIELD_CALLBACK( "FontName", parse_font_name )
  1174. #if 0
  1175. T1_FIELD_CALLBACK( "FontBBox", parse_font_bbox )
  1176. #endif
  1177. T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix )
  1178. T1_FIELD_CALLBACK( "Encoding", parse_encoding )
  1179. T1_FIELD_CALLBACK( "Subrs", parse_subrs )
  1180. T1_FIELD_CALLBACK( "CharStrings", parse_charstrings )
  1181. #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
  1182. T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions )
  1183. T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map )
  1184. T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types )
  1185. T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector )
  1186. T1_FIELD_CALLBACK( "shareddict", parse_shared_dict )
  1187. #endif
  1188. { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0 }
  1189. };
  1190. static FT_Error
  1191. parse_dict( T1_Face face,
  1192. T1_Loader loader,
  1193. FT_Byte* base,
  1194. FT_Long size )
  1195. {
  1196. T1_Parser parser = &loader->parser;
  1197. parser->root.cursor = base;
  1198. parser->root.limit = base + size;
  1199. parser->root.error = 0;
  1200. {
  1201. FT_Byte* cur = base;
  1202. FT_Byte* limit = cur + size;
  1203. for ( ; cur < limit; cur++ )
  1204. {
  1205. /* look for `FontDirectory', which causes problems on some fonts */
  1206. if ( *cur == 'F' && cur + 25 < limit &&
  1207. ft_strncmp( (char*)cur, "FontDirectory", 13 ) == 0 )
  1208. {
  1209. FT_Byte* cur2;
  1210. /* skip the `FontDirectory' keyword */
  1211. cur += 13;
  1212. cur2 = cur;
  1213. /* lookup the `known' keyword */
  1214. while ( cur < limit && *cur != 'k' &&
  1215. ft_strncmp( (char*)cur, "known", 5 ) )
  1216. cur++;
  1217. if ( cur < limit )
  1218. {
  1219. T1_TokenRec token;
  1220. /* skip the `known' keyword and the token following it */
  1221. cur += 5;
  1222. loader->parser.root.cursor = cur;
  1223. T1_ToToken( &loader->parser, &token );
  1224. /* if the last token was an array, skip it! */
  1225. if ( token.type == T1_TOKEN_TYPE_ARRAY )
  1226. cur2 = parser->root.cursor;
  1227. }
  1228. cur = cur2;
  1229. }
  1230. /* look for immediates */
  1231. else if ( *cur == '/' && cur + 2 < limit )
  1232. {
  1233. FT_Byte* cur2;
  1234. FT_PtrDist len;
  1235. cur++;
  1236. cur2 = cur;
  1237. while ( cur2 < limit && is_alpha( *cur2 ) )
  1238. cur2++;
  1239. len = cur2 - cur;
  1240. if ( len > 0 && len < 22 )
  1241. {
  1242. {
  1243. /* now, compare the immediate name to the keyword table */
  1244. T1_Field keyword = (T1_Field)t1_keywords;
  1245. for (;;)
  1246. {
  1247. FT_Byte* name;
  1248. name = (FT_Byte*)keyword->ident;
  1249. if ( !name )
  1250. break;
  1251. if ( cur[0] == name[0] &&
  1252. len == ft_strlen( (const char*)name ) )
  1253. {
  1254. FT_PtrDist n;
  1255. for ( n = 1; n < len; n++ )
  1256. if ( cur[n] != name[n] )
  1257. break;
  1258. if ( n >= len )
  1259. {
  1260. /* we found it -- run the parsing callback! */
  1261. parser->root.cursor = cur2;
  1262. T1_Skip_Spaces( parser );
  1263. parser->root.error = t1_load_keyword( face,
  1264. loader,
  1265. keyword );
  1266. if ( parser->root.error )
  1267. return parser->root.error;
  1268. cur = parser->root.cursor;
  1269. break;
  1270. }
  1271. }
  1272. keyword++;
  1273. }
  1274. }
  1275. }
  1276. }
  1277. }
  1278. }
  1279. return parser->root.error;
  1280. }
  1281. static void
  1282. t1_init_loader( T1_Loader loader,
  1283. T1_Face face )
  1284. {
  1285. FT_UNUSED( face );
  1286. FT_MEM_ZERO( loader, sizeof ( *loader ) );
  1287. loader->num_glyphs = 0;
  1288. loader->num_chars = 0;
  1289. /* initialize the tables -- simply set their `init' field to 0 */
  1290. loader->encoding_table.init = 0;
  1291. loader->charstrings.init = 0;
  1292. loader->glyph_names.init = 0;
  1293. loader->subrs.init = 0;
  1294. loader->swap_table.init = 0;
  1295. loader->fontdata = 0;
  1296. }
  1297. static void
  1298. t1_done_loader( T1_Loader loader )
  1299. {
  1300. T1_Parser parser = &loader->parser;
  1301. /* finalize tables */
  1302. T1_Release_Table( &loader->encoding_table );
  1303. T1_Release_Table( &loader->charstrings );
  1304. T1_Release_Table( &loader->glyph_names );
  1305. T1_Release_Table( &loader->swap_table );
  1306. T1_Release_Table( &loader->subrs );
  1307. /* finalize parser */
  1308. T1_Finalize_Parser( parser );
  1309. }
  1310. FT_LOCAL_DEF( FT_Error )
  1311. T1_Open_Face( T1_Face face )
  1312. {
  1313. T1_LoaderRec loader;
  1314. T1_Parser parser;
  1315. T1_Font type1 = &face->type1;
  1316. FT_Error error;
  1317. PSAux_Service psaux = (PSAux_Service)face->psaux;
  1318. t1_init_loader( &loader, face );
  1319. /* default lenIV */
  1320. type1->private_dict.lenIV = 4;
  1321. /* default blue fuzz, we put it there since 0 is a valid value */
  1322. type1->private_dict.blue_fuzz = 1;
  1323. parser = &loader.parser;
  1324. error = T1_New_Parser( parser,
  1325. face->root.stream,
  1326. face->root.memory,
  1327. psaux );
  1328. if ( error )
  1329. goto Exit;
  1330. error = parse_dict( face, &loader, parser->base_dict, parser->base_len );
  1331. if ( error )
  1332. goto Exit;
  1333. error = T1_Get_Private_Dict( parser, psaux );
  1334. if ( error )
  1335. goto Exit;
  1336. error = parse_dict( face, &loader, parser->private_dict,
  1337. parser->private_len );
  1338. if ( error )
  1339. goto Exit;
  1340. /* now, propagate the subrs, charstrings, and glyphnames tables */
  1341. /* to the Type1 data */
  1342. type1->num_glyphs = loader.num_glyphs;
  1343. if ( loader.subrs.init )
  1344. {
  1345. loader.subrs.init = 0;
  1346. type1->num_subrs = loader.num_subrs;
  1347. type1->subrs_block = loader.subrs.block;
  1348. type1->subrs = loader.subrs.elements;
  1349. type1->subrs_len = loader.subrs.lengths;
  1350. }
  1351. #ifdef FT_CONFIG_OPTION_INCREMENTAL
  1352. if ( !face->root.internal->incremental_interface )
  1353. #endif
  1354. if ( !loader.charstrings.init )
  1355. {
  1356. FT_ERROR(( "T1_Open_Face: no charstrings array in face!\n" ));
  1357. error = T1_Err_Invalid_File_Format;
  1358. }
  1359. loader.charstrings.init = 0;
  1360. type1->charstrings_block = loader.charstrings.block;
  1361. type1->charstrings = loader.charstrings.elements;
  1362. type1->charstrings_len = loader.charstrings.lengths;
  1363. /* we copy the glyph names `block' and `elements' fields; */
  1364. /* the `lengths' field must be released later */
  1365. type1->glyph_names_block = loader.glyph_names.block;
  1366. type1->glyph_names = (FT_String**)loader.glyph_names.elements;
  1367. loader.glyph_names.block = 0;
  1368. loader.glyph_names.elements = 0;
  1369. /* we must now build type1.encoding when we have a custom array */
  1370. if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
  1371. {
  1372. FT_Int charcode, idx, min_char, max_char;
  1373. FT_Byte* char_name;
  1374. FT_Byte* glyph_name;
  1375. /* OK, we do the following: for each element in the encoding */
  1376. /* table, look up the index of the glyph having the same name */
  1377. /* the index is then stored in type1.encoding.char_index, and */
  1378. /* a the name to type1.encoding.char_name */
  1379. min_char = +32000;
  1380. max_char = -32000;
  1381. charcode = 0;
  1382. for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
  1383. {
  1384. type1->encoding.char_index[charcode] = 0;
  1385. type1->encoding.char_name [charcode] = (char *)".notdef";
  1386. char_name = loader.encoding_table.elements[charcode];
  1387. if ( char_name )
  1388. for ( idx = 0; idx < type1->num_glyphs; idx++ )
  1389. {
  1390. glyph_name = (FT_Byte*)type1->glyph_names[idx];
  1391. if ( ft_strcmp( (const char*)char_name,
  1392. (const char*)glyph_name ) == 0 )
  1393. {
  1394. type1->encoding.char_index[charcode] = (FT_UShort)idx;
  1395. type1->encoding.char_name [charcode] = (char*)glyph_name;
  1396. /* Change min/max encoded char only if glyph name is */
  1397. /* not /.notdef */
  1398. if ( ft_strcmp( (const char*)".notdef",
  1399. (const char*)glyph_name ) != 0 )
  1400. {
  1401. if ( charcode < min_char ) min_char = charcode;
  1402. if ( charcode > max_char ) max_char = charcode;
  1403. }
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. type1->encoding.code_first = min_char;
  1409. type1->encoding.code_last = max_char;
  1410. type1->encoding.num_chars = loader.num_chars;
  1411. }
  1412. Exit:
  1413. t1_done_loader( &loader );
  1414. return error;
  1415. }
  1416. /* END */