subset-cff.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <math.h>
  24. /*
  25. For the purposes of this code, and to save my tiny brain from
  26. overload, we will adopt the following notation:
  27. 1) The PDF file contains bytes of data. These bytes are looked
  28. up in the MuPDF font handling to resolve to 'glyph ids' (gids).
  29. These account for all the different encodings etc in use,
  30. including the encoding table within the font.
  31. (For CIDFonts, Cid = Gid, and there is no encoding table).
  32. 2) We are given the list of gids that are used in the document.
  33. Unlike for simple TTFs, we don't map these down to the bottom of the
  34. range, we just remove the definitions for them.
  35. For now, I'm leaving zero size charstrings for subsetted glyphs.
  36. This may need to be changed to be predefined charstrings that
  37. just set a zero width if this is illegal.
  38. Similarly, for now, we don't attempt to compact either the local
  39. or global subrs.
  40. */
  41. /*
  42. In CFF files, we have:
  43. Charset: Maps from gid <-> glyph name
  44. Encoding: Maps from code <-> gid
  45. plus supplemental code -> glyph name (which must have been used already in the map)
  46. */
  47. /* Contains 1-4, to tell us the size of offsets used. */
  48. typedef uint8_t offsize_t;
  49. typedef struct
  50. {
  51. /* Index position and length in the original */
  52. uint32_t index_offset;
  53. uint32_t index_size;
  54. /* Fields read from the index */
  55. uint16_t count;
  56. offsize_t offsize;
  57. const uint8_t *offset; /* A pointer to the offset table, not to the data table! */
  58. /* The offset of the byte before the data. The offset of the first
  59. * object is always 1. Add the offset of any given object to this
  60. * and you get the offset within the block. */
  61. uint32_t data_offset;
  62. } index_t;
  63. typedef struct
  64. {
  65. uint8_t scanned;
  66. uint16_t num;
  67. } usage_t;
  68. typedef struct
  69. {
  70. int len;
  71. int max;
  72. usage_t *list;
  73. } usage_list_t;
  74. typedef struct
  75. {
  76. uint8_t *base;
  77. size_t len;
  78. int symbolic;
  79. int is_cidfont;
  80. uint8_t major;
  81. uint8_t minor;
  82. uint8_t headersize;
  83. offsize_t offsize;
  84. offsize_t new_offsize;
  85. index_t name_index;
  86. index_t top_dict_index;
  87. index_t string_index;
  88. index_t global_index;
  89. index_t charstrings_index;
  90. index_t local_index;
  91. index_t fdarray_index;
  92. uint16_t gsubr_bias;
  93. uint16_t subr_bias;
  94. uint32_t top_dict_index_offset;
  95. uint32_t string_index_offset;
  96. uint32_t global_index_offset;
  97. uint32_t encoding_offset;
  98. uint32_t encoding_len;
  99. uint32_t charset_offset;
  100. uint32_t charset_len;
  101. uint32_t charstrings_index_offset;
  102. uint32_t private_offset;
  103. uint32_t private_len;
  104. uint32_t local_index_offset;
  105. uint32_t fdselect_offset;
  106. uint32_t fdselect_len;
  107. uint32_t fdarray_index_offset;
  108. uint32_t charstring_type;
  109. uint16_t unpacked_charset_len;
  110. uint16_t unpacked_charset_max;
  111. uint16_t *unpacked_charset;
  112. struct
  113. {
  114. fz_buffer *rewritten_dict;
  115. fz_buffer *rewritten_private;
  116. uint32_t offset;
  117. uint32_t len;
  118. uint32_t fixup;
  119. uint32_t local_index_offset;
  120. index_t local_index;
  121. usage_list_t local_usage;
  122. uint16_t subr_bias;
  123. fz_buffer *local_subset;
  124. } *fdarray;
  125. struct
  126. {
  127. uint32_t charset;
  128. uint32_t encoding;
  129. uint32_t charstrings;
  130. uint32_t privat;
  131. uint32_t fdselect;
  132. uint32_t fdarray;
  133. } top_dict_fixup_offsets;
  134. fz_buffer *charstrings_subset;
  135. fz_buffer *top_dict_subset;
  136. fz_buffer *private_subset;
  137. fz_buffer *local_subset;
  138. fz_buffer *global_subset;
  139. usage_list_t local_usage;
  140. usage_list_t global_usage;
  141. usage_list_t gids_to_keep;
  142. usage_list_t extra_gids_to_keep;
  143. uint16_t *gid_to_cid;
  144. uint8_t *gid_to_font;
  145. } cff_t;
  146. /* cid -> gid */
  147. static const uint8_t standard_encoding[256] =
  148. {
  149. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  150. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  151. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  152. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  153. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  154. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
  155. 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  156. 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  158. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  159. 0, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
  160. 0, 111, 112, 113, 114, 0, 115, 116, 117, 118, 119, 120, 121, 122, 0, 123,
  161. 0, 124, 125, 126, 127, 128, 129, 130, 131, 0, 132, 133, 0, 134, 135, 136,
  162. 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  163. 0, 138, 0, 139, 0, 0, 0, 0, 140, 141, 142, 143, 0, 0, 0, 0,
  164. 0, 144, 0, 0, 0, 145, 0, 0, 146, 147, 148, 149, 0, 0, 0, 0
  165. };
  166. /* Simple functions for bigendian fetching/putting */
  167. static uint32_t get16(const uint8_t *d)
  168. {
  169. return (d[0]<<8)|d[1];
  170. }
  171. static void put32(uint8_t *d, uint32_t v)
  172. {
  173. d[0] = v>>24;
  174. d[1] = v>>16;
  175. d[2] = v>>8;
  176. d[3] = v;
  177. }
  178. static void put16(uint8_t *d, uint32_t v)
  179. {
  180. d[0] = v>>8;
  181. d[1] = v;
  182. }
  183. static void put8(uint8_t *d, uint32_t v)
  184. {
  185. d[0] = v;
  186. }
  187. static uint32_t
  188. get_offset(const uint8_t *d, offsize_t os)
  189. {
  190. uint32_t v = *d++;
  191. if (os > 1)
  192. v = (v<<8) | *d++;;
  193. if (os > 2)
  194. v = (v<<8) | *d++;;
  195. if (os > 3)
  196. v = (v<<8) | *d++;;
  197. return v;
  198. }
  199. static void
  200. put_offset(uint8_t *d, offsize_t os, uint32_t v)
  201. {
  202. if (os > 3)
  203. d[3] = v, v >>= 8;
  204. if (os > 2)
  205. d[2] = v, v >>= 8;
  206. if (os > 1)
  207. d[1] = v, v >>= 8;
  208. d[0] = v;
  209. }
  210. static uint8_t
  211. offsize_for_offset(uint32_t offset)
  212. {
  213. if (offset < 256)
  214. return 1;
  215. if (offset < 65536)
  216. return 2;
  217. if (offset < (1<<24))
  218. return 3;
  219. return 4;
  220. }
  221. uint16_t
  222. subr_bias(fz_context *ctx, cff_t *cff, uint16_t count)
  223. {
  224. if (cff->charstring_type == 1)
  225. return 0;
  226. else if (count < 1240)
  227. return 107;
  228. else if (count < 33900)
  229. return 1131;
  230. else
  231. return 32768;
  232. }
  233. /* Index functions */
  234. /* "Load" an index and check it for plausibility (no overflows etc) */
  235. static uint32_t
  236. index_load(fz_context *ctx, index_t *index, const uint8_t *base, uint32_t len, uint32_t offset)
  237. {
  238. uint32_t data_offset, i, v, prev;
  239. offsize_t os;
  240. const uint8_t *data = base + offset;
  241. const uint8_t *data0 = data;
  242. /* Non-existent tables leave the index empty */
  243. if (offset == 0 || len == 0)
  244. {
  245. memset(index, 0, sizeof(*index));
  246. return 0;
  247. }
  248. index->index_offset = offset;
  249. if (offset >= len || len-offset < 2)
  250. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated index");
  251. index->count = get16(data);
  252. if (index->count == 0)
  253. return offset+2;
  254. if (offset + 4 >= len)
  255. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated index");
  256. os = index->offsize = data[2];
  257. if (index->offsize < 1 || index->offsize > 4)
  258. fz_throw(ctx, FZ_ERROR_FORMAT, "Illegal offsize");
  259. index->offset = data + 3;
  260. data_offset = 3 + (index->count+1) * os - 1;
  261. index->data_offset = data_offset + offset;
  262. if (data_offset > len)
  263. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated index");
  264. data += 3;
  265. prev = get_offset(data, os);
  266. if (prev != 1)
  267. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt index");
  268. data += os;
  269. for (i = index->count; i > 0; i--)
  270. {
  271. v = get_offset(data, os);
  272. data += os;
  273. if (v < prev)
  274. fz_throw(ctx, FZ_ERROR_FORMAT, "Index not monotonic");
  275. prev = v;
  276. }
  277. if (v > len)
  278. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated index");
  279. data += prev - 1;
  280. index->index_size = data - data0;
  281. return index->index_size + offset;
  282. }
  283. static uint32_t
  284. index_get(fz_context *ctx, index_t *index, int idx)
  285. {
  286. int os;
  287. uint32_t v;
  288. if (idx < 0 || idx > index->count || index->count == 0)
  289. fz_throw(ctx, FZ_ERROR_FORMAT, "Index bounds");
  290. os = index->offsize;
  291. idx *= os;
  292. v = get_offset(&index->offset[idx], index->offsize);
  293. return index->data_offset + v;
  294. }
  295. /* DICT handling structures and functions */
  296. #define HIOP(A) (A+22)
  297. typedef enum
  298. {
  299. /* Top DICT Operators */
  300. DICT_OP_version = 0,
  301. DICT_OP_Notice = 1,
  302. DICT_OP_Copyright = HIOP(0),
  303. DICT_OP_FullName = 2,
  304. DICT_OP_FamilyName = 3,
  305. DICT_OP_Weight = 4,
  306. DICT_OP_isFixedPitch = HIOP(1),
  307. DICT_OP_ItalicAngle = HIOP(2),
  308. DICT_OP_UnderlinePosition = HIOP(3),
  309. DICT_OP_UnderlineThickness = HIOP(4),
  310. DICT_OP_PaintType = HIOP(5),
  311. DICT_OP_CharstringType = HIOP(6),
  312. DICT_OP_FontMatrix = HIOP(7),
  313. DICT_OP_UniqueID = 13,
  314. DICT_OP_FontBBox = 5,
  315. DICT_OP_StrokeWidth = HIOP(8),
  316. DICT_OP_XUID = 14,
  317. DICT_OP_charset = 15,
  318. DICT_OP_Encoding = 16,
  319. DICT_OP_CharStrings = 17,
  320. DICT_OP_Private = 18,
  321. DICT_OP_SyntheticBase = HIOP(20),
  322. DICT_OP_Postscript = HIOP(21),
  323. DICT_OP_BaseFontName = HIOP(22),
  324. DICT_OP_BaseFontBlend = HIOP(23),
  325. /* CIDFont Operators */
  326. DICT_OP_ROS = HIOP(30),
  327. DICT_OP_CIDFontVersion = HIOP(31),
  328. DICT_OP_CIDFontRevision = HIOP(32),
  329. DICT_OP_CIDFontType = HIOP(33),
  330. DICT_OP_CIDCount = HIOP(34),
  331. DICT_OP_UIDBase = HIOP(35),
  332. DICT_OP_FDArray = HIOP(36),
  333. DICT_OP_FDSelect = HIOP(37),
  334. DICT_OP_FontName = HIOP(38),
  335. /* Private DICT Operators */
  336. DICT_OP_BlueValues = 6,
  337. DICT_OP_OtherBlues = 7,
  338. DICT_OP_FamilyBlues = 8,
  339. DICT_OP_FamilyOtherBlues = 9,
  340. DICT_OP_BlueScale = HIOP(9),
  341. DICT_OP_BlueShift = HIOP(10),
  342. DICT_OP_BlueFuzz = HIOP(11),
  343. DICT_OP_StdHW = 10,
  344. DICT_OP_StdVW = 11,
  345. DICT_OP_StemSnapH = HIOP(12),
  346. DICT_OP_StemSnapV = HIOP(13),
  347. DICT_OP_ForceBold = HIOP(14),
  348. DICT_OP_LanguageGroup = HIOP(17),
  349. DICT_OP_ExpansionFactor = HIOP(18),
  350. DICT_OP_initialRandomSeed = HIOP(19),
  351. DICT_OP_Subrs = 19,
  352. DICT_OP_defaultWidthX = 20,
  353. DICT_OP_nominalWidthX = 21
  354. } dict_operator;
  355. typedef enum
  356. {
  357. da_int = 0,
  358. da_real = 1,
  359. da_operator = 2
  360. } dict_arg_type;
  361. typedef struct {
  362. dict_arg_type type;
  363. union {
  364. uint32_t i;
  365. float f;
  366. } u;
  367. } dict_arg;
  368. #define DICT_MAX_ARGS 48
  369. typedef struct {
  370. const uint8_t *base;
  371. size_t len;
  372. uint32_t offset;
  373. uint32_t end_offset;
  374. uint8_t *val;
  375. int eod;
  376. int num_args;
  377. dict_arg arg[DICT_MAX_ARGS+1];
  378. } dict_iterator;
  379. static uint8_t
  380. dict_get_byte(fz_context *ctx, dict_iterator *di)
  381. {
  382. uint8_t b;
  383. if (di->offset == di->end_offset)
  384. di->eod = 1;
  385. if (di->eod)
  386. fz_throw(ctx, FZ_ERROR_FORMAT, "Overlong DICT data");
  387. b = di->base[di->offset++];
  388. return b;
  389. }
  390. static dict_arg
  391. dict_get_arg(fz_context *ctx, dict_iterator *di)
  392. {
  393. uint8_t b0, b1, b2, b3, b4;
  394. dict_arg d;
  395. b0 = dict_get_byte(ctx, di);
  396. if (b0 == 12)
  397. {
  398. b1 = dict_get_byte(ctx, di);
  399. d.type = da_operator;
  400. d.u.i = HIOP(b1);
  401. return d;
  402. }
  403. else if (b0 <= 21)
  404. {
  405. d.type = da_operator;
  406. d.u.i = b0;
  407. return d;
  408. }
  409. else if (b0 <= 27)
  410. {
  411. malformed:
  412. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed DICT");
  413. }
  414. else if (b0 == 28)
  415. {
  416. b1 = dict_get_byte(ctx, di);
  417. b2 = dict_get_byte(ctx, di);
  418. d.type = da_int;
  419. d.u.i = (b1<<8) | b2;
  420. }
  421. else if (b0 == 29)
  422. {
  423. b1 = dict_get_byte(ctx, di);
  424. b2 = dict_get_byte(ctx, di);
  425. b3 = dict_get_byte(ctx, di);
  426. b4 = dict_get_byte(ctx, di);
  427. d.type = da_int;
  428. d.u.i = (b1<<24) | (b2<<16) | (b3<<8) | b4;
  429. }
  430. else if (b0 == 30)
  431. {
  432. char cheap[32+5];
  433. unsigned int i;
  434. for (i = 0; i < sizeof(cheap)-5; )
  435. {
  436. static const char *dict = "0123456789.EE -f";
  437. uint8_t b = dict_get_byte(ctx, di);
  438. if ((b>>4) == 0xf)
  439. break;
  440. cheap[i++] = dict[b>>4];
  441. if ((b>>4) == 0xc)
  442. cheap[i++] = '-';
  443. b &= 15;
  444. if (b == 0xf)
  445. break;
  446. cheap[i++] = dict[b];
  447. if (b == 0xc)
  448. cheap[i++] = '-';
  449. }
  450. cheap[i++] = 0;
  451. d.type = da_real;
  452. d.u.f = fz_atof(cheap);
  453. }
  454. else if (b0 == 31)
  455. {
  456. goto malformed;
  457. }
  458. else if (b0 <= 246)
  459. {
  460. d.type = da_int;
  461. d.u.i = b0-139;
  462. }
  463. else if (b0 <= 250)
  464. {
  465. b1 = dict_get_byte(ctx, di);
  466. d.type = da_int;
  467. d.u.i = ((b0-247)<<8) + b1 + 108;
  468. }
  469. else if (b0 <= 254)
  470. {
  471. b1 = dict_get_byte(ctx, di);
  472. d.type = da_int;
  473. d.u.i = -((b0-251)<<8) - b1 - 108;
  474. }
  475. else
  476. goto malformed;
  477. return d;
  478. }
  479. static dict_operator
  480. dict_next(fz_context *ctx, dict_iterator *di)
  481. {
  482. int n;
  483. if (di->offset >= di->end_offset)
  484. {
  485. di->eod = 1;
  486. return 0;
  487. }
  488. n = 0;
  489. while (di->offset < di->end_offset)
  490. {
  491. di->arg[n] = dict_get_arg(ctx, di);
  492. if (di->arg[n].type == da_operator)
  493. {
  494. /* Sorted! Terminate loop. */
  495. break;
  496. }
  497. if (n == DICT_MAX_ARGS)
  498. fz_throw(ctx, FZ_ERROR_FORMAT, "Too many operands");
  499. n++;
  500. }
  501. di->num_args = n;
  502. return (dict_operator)di->arg[n].u.i;
  503. }
  504. static dict_operator
  505. dict_init(fz_context *ctx, dict_iterator *di, const uint8_t *base, size_t len, uint32_t offset, uint32_t end)
  506. {
  507. di->base = base;
  508. di->len = len;
  509. di->offset = offset;
  510. di->end_offset = end;
  511. di->eod = (di->offset == di->end_offset);
  512. if (di->offset > len || end > len)
  513. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed DICT");
  514. return dict_next(ctx, di);
  515. }
  516. static int
  517. dict_more(dict_iterator *di)
  518. {
  519. return !di->eod;
  520. }
  521. static uint32_t
  522. dict_arg_int(fz_context *ctx, dict_iterator *di, int idx)
  523. {
  524. if (idx < 0 || idx >= di->num_args)
  525. fz_throw(ctx, FZ_ERROR_FORMAT, "Missing dict arg");
  526. if (di->arg[idx].type != da_int)
  527. fz_throw(ctx, FZ_ERROR_FORMAT, "DICT arg not an int");
  528. return di->arg[idx].u.i;
  529. }
  530. static void
  531. dict_write_arg(fz_context *ctx, fz_output *out, dict_arg d)
  532. {
  533. int si;
  534. uint32_t i = d.u.i;
  535. if (d.type == da_operator)
  536. {
  537. if (i >= HIOP(0))
  538. {
  539. fz_write_byte(ctx, out, 12);
  540. i -= HIOP(0);
  541. }
  542. fz_write_byte(ctx, out, i);
  543. return;
  544. }
  545. if (d.type == da_real)
  546. {
  547. char text[32];
  548. unsigned int k, j;
  549. uint8_t v;
  550. fz_snprintf(text, sizeof(text)-1, "%g", d.u.f);
  551. fz_write_byte(ctx, out, 30);
  552. j = 4;
  553. v = 0;
  554. for (k = 0; k < sizeof(text)-1;)
  555. {
  556. char c = text[k++];
  557. if (c >= '0' && c <= '9')
  558. v |= (c - '0')<<j;
  559. else if (c == '.')
  560. v |= 0xa<<j;
  561. else if (c == 'e' || c == 'E')
  562. {
  563. if (text[k] == '-')
  564. {
  565. v |= 0xc<<j;
  566. k++;
  567. }
  568. else
  569. {
  570. v |= 0xb<<j;
  571. }
  572. }
  573. else if (c == '-')
  574. {
  575. v |= 0xe<<j;
  576. }
  577. else if (c == 0)
  578. break;
  579. if (j == 0)
  580. {
  581. fz_write_byte(ctx, out, v);
  582. v = 0;
  583. }
  584. j ^= 4;
  585. }
  586. if (j == 4)
  587. v = 0xff;
  588. else
  589. v |= 0xf;
  590. fz_write_byte(ctx, out, v);
  591. return;
  592. }
  593. /* Must be an int. */
  594. si = (int)i;
  595. if (-107 <= si && si <= 107)
  596. fz_write_byte(ctx, out, si+139);
  597. else if (108 <= si && si <= 1131)
  598. {
  599. si -= 108;
  600. fz_write_byte(ctx, out, (si>>8)+247);
  601. fz_write_byte(ctx, out, si);
  602. }
  603. else if (-1131 <= si && si <= -108)
  604. {
  605. si = -si - 108;
  606. fz_write_byte(ctx, out, (si>>8)+251);
  607. fz_write_byte(ctx, out, si);
  608. }
  609. else if (-32768 <= si && si <= 32767)
  610. {
  611. fz_write_byte(ctx, out, 28);
  612. fz_write_byte(ctx, out, si>>8);
  613. fz_write_byte(ctx, out, si);
  614. }
  615. else
  616. {
  617. fz_write_byte(ctx, out, 29);
  618. fz_write_byte(ctx, out, si>>24);
  619. fz_write_byte(ctx, out, si>>16);
  620. fz_write_byte(ctx, out, si>>8);
  621. fz_write_byte(ctx, out, si);
  622. }
  623. }
  624. static void
  625. dict_write_args(fz_context *ctx, fz_output *out, dict_iterator *di)
  626. {
  627. int i;
  628. for (i = 0; i <= di->num_args; i++)
  629. {
  630. dict_write_arg(ctx, out, di->arg[i]);
  631. }
  632. }
  633. static void
  634. do_subset(fz_context *ctx, cff_t *cff, fz_buffer **buffer, usage_list_t *keep_list, index_t *index, int keep_notdef)
  635. {
  636. uint8_t *d, *strings;
  637. uint32_t i, offset, end;
  638. uint32_t required, offset_size, fill;
  639. uint32_t num_charstrings = index->count;
  640. int gid;
  641. int num_gids = keep_list->len;
  642. const usage_t *gids = keep_list->list;
  643. if (num_charstrings == 0)
  644. return;
  645. /* First count the required size. */
  646. offset = index_get(ctx, index, 0);
  647. required = 0;
  648. gid = 0;
  649. for (i = 0; i < num_charstrings; offset = end, i++)
  650. {
  651. end = index_get(ctx, index, i+1);
  652. if (gid < num_gids && i == gids[gid].num)
  653. {
  654. /* Keep this */
  655. gid++;
  656. }
  657. else if (keep_notdef && i == 0)
  658. {
  659. /* Keep this. */
  660. }
  661. else
  662. {
  663. /* Drop this */
  664. required += 1;
  665. continue;
  666. }
  667. required += end-offset;
  668. }
  669. /* So we need 'required' bytes of space for the strings themselves */
  670. /* Do not forget to increment by one byte! This is because the
  671. last entry in the offset table points to one byte beyond the end of
  672. the required string data. Consider if the required string data occupies
  673. 255 bytes, then each offset for each of the required entries can be
  674. represented by a single byte, but the last table entry would need to
  675. point to offset 256, which cannot be represented by a single byte. */
  676. offset_size = offsize_for_offset(required + 1);
  677. required += 2 + 1 + (num_charstrings+1)*offset_size;
  678. *buffer = fz_new_buffer(ctx, required);
  679. d = (*buffer)->data;
  680. (*buffer)->len = required;
  681. /* Write out the index header */
  682. put16(d, num_charstrings); /* count */
  683. d +=2;
  684. put8(d, offset_size); /* offset size */
  685. d += 1;
  686. /* Now copy the charstrings themselves */
  687. strings = d + offset_size * (num_charstrings+1) - 1;
  688. gid = 0;
  689. fill = 1;
  690. offset = index_get(ctx, index, 0);
  691. for (i = 0; i < num_charstrings; offset = end, i++)
  692. {
  693. end = index_get(ctx, index, i+1);
  694. if (gid < num_gids && gids[gid].num == i)
  695. {
  696. /* Keep this */
  697. gid++;
  698. }
  699. else if (keep_notdef && i == 0)
  700. {
  701. /* Keep this */
  702. }
  703. else
  704. {
  705. /* Drop this */
  706. put_offset(d, offset_size, fill);
  707. d += offset_size;
  708. strings[fill++] = 0x0e; /* endchar */
  709. continue;
  710. }
  711. memcpy(strings + fill, &cff->base[offset], end-offset);
  712. put_offset(d, offset_size, fill);
  713. d += offset_size;
  714. fill += end-offset;
  715. }
  716. put_offset(d, offset_size, fill);
  717. }
  718. static void
  719. subset_charstrings(fz_context *ctx, cff_t *cff)
  720. {
  721. do_subset(ctx, cff, &cff->charstrings_subset, &cff->gids_to_keep, &cff->charstrings_index, 1);
  722. }
  723. static void
  724. subset_locals(fz_context *ctx, cff_t *cff)
  725. {
  726. do_subset(ctx, cff, &cff->local_subset, &cff->local_usage, &cff->local_index, 0);
  727. }
  728. static void
  729. subset_globals(fz_context *ctx, cff_t *cff)
  730. {
  731. do_subset(ctx, cff, &cff->global_subset, &cff->global_usage, &cff->global_index, 0);
  732. }
  733. static void
  734. subset_fdarray_locals(fz_context *ctx, cff_t *cff)
  735. {
  736. uint16_t i, n = cff->fdarray_index.count;
  737. for (i = 0; i < n; i++)
  738. do_subset(ctx, cff, &cff->fdarray[i].local_subset, &cff->fdarray[i].local_usage, &cff->fdarray[i].local_index, 0);
  739. }
  740. /* Charstring "executing" functions */
  741. static int
  742. usage_list_find(fz_context *ctx, usage_list_t *list, int value)
  743. {
  744. /* are we on the list already? */
  745. int lo = 0;
  746. int hi = list->len;
  747. while (lo < hi)
  748. {
  749. int mid = (lo + hi)>>1;
  750. int v = list->list[mid].num;
  751. if (v < value)
  752. lo = mid+1;
  753. else if (v > value)
  754. hi = mid;
  755. else
  756. return mid;
  757. }
  758. return lo;
  759. }
  760. static int
  761. usage_list_contains(fz_context *ctx, usage_list_t *list, int value)
  762. {
  763. int lo = usage_list_find(ctx, list, value);
  764. return (lo < list->len && list->list[lo].num == value);
  765. }
  766. static void
  767. usage_list_add(fz_context *ctx, usage_list_t *list, int value)
  768. {
  769. int lo = usage_list_find(ctx, list, value);
  770. if (lo < list->len && list->list[lo].num == value)
  771. return;
  772. if (list->len == list->max)
  773. {
  774. int newmax = list->max * 2;
  775. if (newmax == 0)
  776. newmax = 32;
  777. list->list = fz_realloc(ctx, list->list, sizeof(*list->list) * newmax);
  778. list->max = newmax;
  779. }
  780. memmove(&list->list[lo+1], &list->list[lo], (list->len - lo) * sizeof(*list->list));
  781. list->list[lo].num = value;
  782. list->list[lo].scanned = 0;
  783. list->len++;
  784. }
  785. static void
  786. drop_usage_list(fz_context *ctx, usage_list_t *list)
  787. {
  788. if (!list)
  789. return;
  790. fz_free(ctx, list->list);
  791. list->list = NULL;
  792. }
  793. static void
  794. mark_subr_used(fz_context *ctx, cff_t *cff, int subr, int global, int local_subr_bias, usage_list_t *local_usage)
  795. {
  796. usage_list_t *list = global ? &cff->global_usage : local_usage;
  797. subr += global ? cff->gsubr_bias : local_subr_bias;
  798. usage_list_add(ctx, list, subr);
  799. }
  800. static void
  801. use_sub_char(fz_context *ctx, cff_t *cff, int code)
  802. {
  803. /* code is a character code in 'standard encoding'. We
  804. * need to map that to whatever glyph that would be in
  805. * standard encoding, and mark that glyph as being used. */
  806. uint32_t i, gid;
  807. if (code < 0 || code > 255)
  808. return;
  809. i = standard_encoding[code];
  810. if (i == 0)
  811. return;
  812. for (gid = 0; gid < cff->unpacked_charset_len; gid++)
  813. {
  814. if (cff->unpacked_charset[gid] == i)
  815. break;
  816. }
  817. if (gid == cff->unpacked_charset_len)
  818. {
  819. fz_warn(ctx, "subsidiary char out of range");
  820. return;
  821. }
  822. if (usage_list_contains(ctx, &cff->gids_to_keep, gid))
  823. return;
  824. usage_list_add(ctx, &cff->extra_gids_to_keep, gid);
  825. }
  826. #define ATLEAST(n) if (sp < n) goto atleast_fail;
  827. #define POP(n) if (sp < n) goto atleast_fail;
  828. #define PUSH(n) \
  829. do { if (sp + n > (int)(sizeof(stack)/sizeof(*stack))) fz_throw(ctx, FZ_ERROR_FORMAT, "Stack overflow"); sp += n; } while (0)
  830. static void
  831. execute_charstring(fz_context *ctx, cff_t *cff, const uint8_t *pc, const uint8_t *end, uint16_t subr_bias, usage_list_t *local_usage)
  832. {
  833. double trans[32] = { 0 };
  834. double stack[513];
  835. int sp = 0;
  836. int stem_hints = 0;
  837. uint8_t c;
  838. /* 0 => starting, 1 => had hstem, 2 => anything else */
  839. int start = 0;
  840. while (pc < end)
  841. {
  842. c = *pc++;
  843. /* An operator other than one of the hint ones immediately
  844. * disqualifies us from being in the hint extension state. */
  845. if (c < 32 && (c != 1 && c != 18 && c != 19 && c != 20))
  846. start = 2;
  847. switch (c)
  848. {
  849. case 0:
  850. case 2:
  851. case 9:
  852. case 13:
  853. case 17:
  854. fz_throw(ctx, FZ_ERROR_FORMAT, "Reserved charstring byte");
  855. break;
  856. /* Deal with all the hints together */
  857. case 18: /* hstemhm */
  858. case 1: /* hstem */
  859. start = 1;
  860. case 23: /* vstemhm */
  861. case 3: /* vstem */
  862. stem_hints += (sp/2);
  863. goto clear;
  864. case 19: /* hintmask */
  865. case 20: /* cntrmask */
  866. if (start == 1)
  867. stem_hints += (sp/2);
  868. pc += (stem_hints+7)>>3;
  869. if (pc > end)
  870. goto overflow;
  871. start = 2;
  872. goto clear;
  873. /* The operators all clear the stack. */
  874. case 4: /* vmoveto */
  875. case 5: /* rlineto */
  876. case 6: /* hlineto */
  877. case 7: /* vlineto */
  878. case 8: /* rrcurveto */
  879. case 15: /* vsindex */
  880. case 21: /* rmoveto */
  881. case 22: /* hmoveto */
  882. case 24: /* rcurveline */
  883. case 25: /* rlinecurve */
  884. case 26: /* vvcurveto */
  885. case 27: /* hhcurveto */
  886. case 30: /* vhcurveto */
  887. case 31: /* hvcurveto */
  888. clear:
  889. sp = 0;
  890. break;
  891. case 10: /* callsubr */
  892. ATLEAST(1);
  893. mark_subr_used(ctx, cff, stack[sp-1], 0, subr_bias, local_usage);
  894. sp--;
  895. break;
  896. case 11: /* return */
  897. pc = end;
  898. sp = 0;
  899. break;
  900. case 12: /* escape */
  901. {
  902. if (pc == end)
  903. {
  904. overflow:
  905. fz_throw(ctx, FZ_ERROR_FORMAT, "Buffer overflow in charstring");
  906. }
  907. c = *pc++;
  908. switch (c)
  909. {
  910. case 0: /* dotsection: deprecated, nop */
  911. sp = 0;
  912. break;
  913. case 3: /* and */
  914. ATLEAST(2);
  915. stack[sp-2] = (stack[sp-1] != 0 && stack[sp-2] != 0);
  916. sp--;
  917. break;
  918. case 4: /* or */
  919. ATLEAST(2);
  920. stack[sp-2] = (stack[sp-1] != 0 || stack[sp-2] != 0);
  921. sp--;
  922. break;
  923. case 5: /* not */
  924. ATLEAST(1);
  925. stack[sp-1] = (stack[sp-1] == 0);
  926. break;
  927. case 9: /* abs */
  928. ATLEAST(1);
  929. if (stack[sp-1] < 0)
  930. stack[sp-1] = -stack[sp-1];
  931. break;
  932. case 10: /* add */
  933. ATLEAST(2);
  934. stack[sp-2] += stack[sp-1];
  935. sp--;
  936. break;
  937. case 11: /* sub */
  938. ATLEAST(2);
  939. stack[sp-2] -= stack[sp-1];
  940. sp--;
  941. break;
  942. case 12: /* div */
  943. ATLEAST(2);
  944. if (stack[sp-2] != 0)
  945. stack[sp-2] /= stack[sp-1];
  946. sp--;
  947. break;
  948. case 14: /* neg */
  949. ATLEAST(1);
  950. stack[sp-1] = -stack[sp-1];
  951. break;
  952. case 15: /* eq */
  953. ATLEAST(2);
  954. stack[sp-2] = (stack[sp-1] == stack[sp-2]);
  955. sp--;
  956. break;
  957. case 18: /* drop */
  958. POP(1);
  959. break;
  960. case 20: /* put */
  961. ATLEAST(2);
  962. if ((int)stack[sp-1] < 0 || (unsigned int)stack[sp-1] > sizeof(trans)/sizeof(*trans))
  963. fz_throw(ctx, FZ_ERROR_FORMAT, "Transient array over/underflow");
  964. trans[(int)stack[sp-1]] = stack[sp-2];
  965. sp -= 2;
  966. break;
  967. case 21: /* get */
  968. ATLEAST(1);
  969. if ((int)stack[sp-1] < 0 || (unsigned int)stack[sp-1] > sizeof(trans)/sizeof(*trans))
  970. fz_throw(ctx, FZ_ERROR_FORMAT, "Transient array over/underflow");
  971. stack[sp-1] = trans[(int)stack[sp-1]];
  972. break;
  973. case 22: /* ifelse */
  974. ATLEAST(4);
  975. if (stack[sp-2] > stack[sp-1])
  976. stack[sp-4] = stack[sp-3];
  977. sp -= 3;
  978. break;
  979. case 23: /* random */
  980. PUSH(1);
  981. stack[sp-1] = 0.5;
  982. break;
  983. case 24: /* mul */
  984. ATLEAST(2);
  985. stack[sp-2] *= stack[sp-1];
  986. break;
  987. case 26: /* sqrt */
  988. ATLEAST(1);
  989. if (stack[sp-1] >= 0)
  990. stack[sp-1] = sqrtf(stack[sp-1]);
  991. break;
  992. case 27: /* dup */
  993. ATLEAST(1);
  994. PUSH(1);
  995. stack[sp-1] = stack[sp-2];
  996. break;
  997. case 28: /* exch */
  998. {
  999. double d;
  1000. ATLEAST(2);
  1001. d = stack[sp-1];
  1002. stack[sp-1] = stack[sp-2];
  1003. stack[sp-2] = d;
  1004. break;
  1005. }
  1006. case 29: /* index */
  1007. {
  1008. int i;
  1009. ATLEAST(1);
  1010. i = (int)stack[sp-1];
  1011. ATLEAST(i+1);
  1012. if (i < 0 || i > sp-1)
  1013. i = 0;
  1014. stack[sp-1] = stack[sp-2-i];
  1015. break;
  1016. }
  1017. case 30: /* roll */
  1018. {
  1019. int N, J;
  1020. ATLEAST(2);
  1021. J = stack[sp-1];
  1022. N = stack[sp-2];
  1023. if (N == 0)
  1024. break;
  1025. if (N < 0)
  1026. fz_throw(ctx, FZ_ERROR_FORMAT, "Invalid roll");
  1027. ATLEAST(2+N);
  1028. if (J < 0)
  1029. {
  1030. J = N - ((-J) % N);
  1031. if (J == 0)
  1032. break;
  1033. }
  1034. while (J--)
  1035. {
  1036. double t = stack[sp-2];
  1037. int i;
  1038. for (i = N-1; i > 0; i--)
  1039. {
  1040. stack[sp-2-i] = stack[sp-3-i];
  1041. }
  1042. stack[sp-2-N] = t;
  1043. }
  1044. break;
  1045. }
  1046. case 34: /* hflex */
  1047. case 35: /* flex */
  1048. case 36: /* hflex1 */
  1049. case 37: /* flex1 */
  1050. sp = 0;
  1051. break;
  1052. default:
  1053. fz_throw(ctx, FZ_ERROR_FORMAT, "Reserved charstring byte");
  1054. }
  1055. break;
  1056. }
  1057. case 14: /* endchar */
  1058. pc = end;
  1059. if (sp >= 4)
  1060. {
  1061. use_sub_char(ctx, cff, stack[sp-1]);
  1062. use_sub_char(ctx, cff, stack[sp-2]);
  1063. }
  1064. sp = 0;
  1065. break;
  1066. case 16: /* blend */
  1067. /* Consumes a lot of operators, leaves n, where n = stack[sp-1]. */
  1068. ATLEAST(1);
  1069. sp = stack[sp-1];
  1070. break;
  1071. case 29: /* callgsubr */
  1072. ATLEAST(1);
  1073. mark_subr_used(ctx, cff, stack[sp-1], 1, subr_bias, local_usage);
  1074. sp--;
  1075. break;
  1076. case 28: /* shortint */
  1077. if (pc + 2 >= end)
  1078. {
  1079. pc = end;
  1080. break;
  1081. }
  1082. PUSH(1);
  1083. stack[sp-1] = (pc[0]<<8) | pc[1];
  1084. pc += 2;
  1085. break;
  1086. case 255: /* number */
  1087. if (pc + 4 >= end)
  1088. {
  1089. pc = end;
  1090. break;
  1091. }
  1092. PUSH(1);
  1093. stack[sp-1] = ((pc[0]<<24) | (pc[1]<<16) | (pc[2]<<8) | pc[3]) / 65536.0;
  1094. pc += 4;
  1095. break;
  1096. case 247: case 248: case 249: case 250: /* number */
  1097. PUSH(1);
  1098. stack[sp-1] = (c-247) * 256 + 108;
  1099. if (pc >= end)
  1100. break;
  1101. stack[sp-1] += *pc++;
  1102. break;
  1103. case 251: case 252: case 253: case 254: /* number */
  1104. PUSH(1);
  1105. stack[sp-1] = -((c-251) * 256 + 108);
  1106. if (pc >= end)
  1107. break;
  1108. stack[sp-1] -= *pc++;
  1109. break;
  1110. default: /* 32-246 */
  1111. PUSH(1);
  1112. stack[sp-1] = c-139;
  1113. break;
  1114. }
  1115. }
  1116. return;
  1117. atleast_fail:
  1118. fz_throw(ctx, FZ_ERROR_FORMAT, "Insufficient operators on the stack: op=%d", c);
  1119. }
  1120. usage_list_t *
  1121. get_font_locals(fz_context *ctx, cff_t *cff, int gid, int is_pdf_cidfont, uint16_t *subr_bias)
  1122. {
  1123. usage_t *gids = cff->gids_to_keep.list;
  1124. int num_gids = cff->gids_to_keep.len;
  1125. if (is_pdf_cidfont && cff->is_cidfont)
  1126. {
  1127. uint8_t font = 0;
  1128. if (gid < num_gids && gids[gid].num < cff->charstrings_index.count)
  1129. font = cff->gid_to_font[gids[gid].num];
  1130. else if (gid == 0)
  1131. font = cff->gid_to_font[gid];
  1132. if (font >= cff->fdarray_index.count)
  1133. font = 0;
  1134. if (subr_bias)
  1135. *subr_bias = cff->fdarray[font].subr_bias;
  1136. return &cff->fdarray[font].local_usage;
  1137. }
  1138. if (subr_bias)
  1139. *subr_bias = cff->subr_bias;
  1140. return &cff->local_usage;
  1141. }
  1142. static void
  1143. scan_charstrings(fz_context *ctx, cff_t *cff, int is_pdf_cidfont)
  1144. {
  1145. uint32_t offset, end;
  1146. int num_charstrings = (int)cff->charstrings_index.count;
  1147. int i, gid, font;
  1148. usage_t *gids = cff->gids_to_keep.list;
  1149. int num_gids = cff->gids_to_keep.len;
  1150. int changed;
  1151. uint16_t subr_bias;
  1152. usage_list_t *local_usage = NULL;
  1153. /* Scan through the charstrings.*/
  1154. offset = index_get(ctx, &cff->charstrings_index, 0);
  1155. gid = 0;
  1156. for (i = 0; i < num_charstrings; offset = end, i++)
  1157. {
  1158. end = index_get(ctx, &cff->charstrings_index, i+1);
  1159. if (gid < num_gids && i == gids[gid].num)
  1160. {
  1161. /* Keep this */
  1162. gid++;
  1163. }
  1164. else if (i == 0)
  1165. {
  1166. /* Keep this. */
  1167. }
  1168. else
  1169. {
  1170. /* Drop this */
  1171. continue;
  1172. }
  1173. local_usage = get_font_locals(ctx, cff, gid, is_pdf_cidfont, &subr_bias);
  1174. execute_charstring(ctx, cff, &cff->base[offset], &cff->base[end], subr_bias, local_usage);
  1175. }
  1176. /* Now we search the 'extra' ones, the 'subrs' (local) and 'gsubrs' (globals)
  1177. * that are used. Searching each of these might find more that need to be
  1178. * searched, so we use a loop. */
  1179. do
  1180. {
  1181. changed = 0;
  1182. /* Extra (subsidiary) glyphs */
  1183. for (i = 0; i < cff->extra_gids_to_keep.len; i++)
  1184. {
  1185. if (cff->extra_gids_to_keep.list[i].scanned)
  1186. continue;
  1187. cff->extra_gids_to_keep.list[i].scanned = 1;
  1188. gid = cff->extra_gids_to_keep.list[i].num;
  1189. usage_list_add(ctx, &cff->gids_to_keep, gid);
  1190. offset = index_get(ctx, &cff->charstrings_index, gid);
  1191. end = index_get(ctx, &cff->charstrings_index, gid+1);
  1192. local_usage = get_font_locals(ctx, cff, gid, is_pdf_cidfont, &subr_bias);
  1193. execute_charstring(ctx, cff, &cff->base[offset], &cff->base[end], subr_bias, local_usage);
  1194. changed = 1;
  1195. }
  1196. /* Now, run through the locals, seeing what locals and globals they call. */
  1197. for (i = 0; i < cff->local_usage.len; i++)
  1198. {
  1199. if (cff->local_usage.list[i].scanned)
  1200. continue;
  1201. cff->local_usage.list[i].scanned = 1;
  1202. gid = cff->local_usage.list[i].num;
  1203. offset = index_get(ctx, &cff->local_index, gid);
  1204. end = index_get(ctx, &cff->local_index, gid+1);
  1205. local_usage = get_font_locals(ctx, cff, gid, is_pdf_cidfont, &subr_bias);
  1206. execute_charstring(ctx, cff, &cff->base[offset], &cff->base[end], subr_bias, local_usage);
  1207. changed = 1;
  1208. }
  1209. /* Now, run through the per-font locals, seeing what per-font locals and globals they call. */
  1210. for (font = 0; font < cff->fdarray_index.count; font++)
  1211. {
  1212. for (i = 0; i < cff->fdarray[font].local_usage.len; i++)
  1213. {
  1214. gid = cff->fdarray[font].local_usage.list[i].num;
  1215. if (cff->fdarray[font].local_usage.list[i].scanned)
  1216. continue;
  1217. cff->fdarray[font].local_usage.list[i].scanned = 1;
  1218. gid = cff->fdarray[font].local_usage.list[i].num;
  1219. offset = index_get(ctx, &cff->fdarray[font].local_index, gid);
  1220. end = index_get(ctx, &cff->fdarray[font].local_index, gid+1);
  1221. local_usage = get_font_locals(ctx, cff, gid, is_pdf_cidfont, &subr_bias);
  1222. execute_charstring(ctx, cff, &cff->base[offset], &cff->base[end], subr_bias, local_usage);
  1223. changed = 1;
  1224. }
  1225. }
  1226. /* Now, run through the globals, seeing what globals they call. */
  1227. for (i = 0; i < cff->global_usage.len; i++)
  1228. {
  1229. if (cff->global_usage.list[i].scanned)
  1230. continue;
  1231. cff->global_usage.list[i].scanned = 1;
  1232. gid = cff->global_usage.list[i].num;
  1233. offset = index_get(ctx, &cff->global_index, gid);
  1234. end = index_get(ctx, &cff->global_index, gid+1);
  1235. local_usage = get_font_locals(ctx, cff, gid, is_pdf_cidfont, &subr_bias);
  1236. execute_charstring(ctx, cff, &cff->base[offset], &cff->base[end], subr_bias, local_usage);
  1237. changed = 1;
  1238. }
  1239. }
  1240. while (changed);
  1241. }
  1242. static void
  1243. get_encoding_len(fz_context *ctx, cff_t *cff)
  1244. {
  1245. uint32_t encoding_offset = cff->encoding_offset;
  1246. const uint8_t *d = cff->base + encoding_offset;
  1247. uint8_t fmt;
  1248. uint8_t n;
  1249. uint32_t size;
  1250. if (encoding_offset < 2)
  1251. {
  1252. cff->encoding_len = 0;
  1253. return;
  1254. }
  1255. if (encoding_offset + 2 > cff->len)
  1256. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt encoding");
  1257. fmt = *d++;
  1258. n = *d++;
  1259. switch (fmt & 127)
  1260. {
  1261. case 0:
  1262. size = 2 + n;
  1263. break;
  1264. case 1:
  1265. size = 2 + n * 2;
  1266. break;
  1267. case 2:
  1268. size = 2 + n * 3;
  1269. break;
  1270. default:
  1271. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad format encoding");
  1272. }
  1273. if (encoding_offset + size > cff->len)
  1274. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt encoding");
  1275. if (fmt & 128)
  1276. {
  1277. if (encoding_offset + size + 1 > cff->len)
  1278. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt encoding");
  1279. n = *d++;
  1280. size += 1 + n*3;
  1281. if (encoding_offset + size > cff->len)
  1282. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt encoding");
  1283. }
  1284. cff->encoding_len = size;
  1285. }
  1286. static void
  1287. get_charset_len(fz_context *ctx, cff_t *cff)
  1288. {
  1289. uint32_t charset_offset = cff->charset_offset;
  1290. const uint8_t *d = cff->base + charset_offset;
  1291. const uint8_t *d0 = d;
  1292. uint8_t fmt;
  1293. uint32_t i, n;
  1294. if (charset_offset < 2)
  1295. {
  1296. cff->charset_len = 0;
  1297. return;
  1298. }
  1299. if (charset_offset + 1 > cff->len)
  1300. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1301. fmt = *d++;
  1302. n = cff->charstrings_index.count;
  1303. if (fmt == 0)
  1304. {
  1305. cff->unpacked_charset = fz_malloc(ctx, sizeof(uint16_t) * n);
  1306. cff->unpacked_charset_len = cff->unpacked_charset_max = n;
  1307. cff->unpacked_charset[0] = 0;
  1308. for (i = 1; i < n; i++)
  1309. {
  1310. cff->unpacked_charset[i] = get16(d);
  1311. d += 2;
  1312. }
  1313. }
  1314. else if (fmt == 1)
  1315. {
  1316. cff->unpacked_charset = fz_malloc(ctx, sizeof(uint16_t) * 256);
  1317. cff->unpacked_charset_max = 256;
  1318. cff->unpacked_charset_len = 1;
  1319. cff->unpacked_charset[0] = 0;
  1320. n--;
  1321. while (n > 0)
  1322. {
  1323. uint16_t first;
  1324. uint32_t nleft;
  1325. if (d + 3>= cff->base + cff->len)
  1326. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1327. first = get16(d);
  1328. nleft = d[2] + 1;
  1329. d += 3;
  1330. if (nleft > n)
  1331. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1332. n -= nleft;
  1333. while (nleft)
  1334. {
  1335. if (cff->unpacked_charset_len == cff->unpacked_charset_max)
  1336. {
  1337. cff->unpacked_charset = fz_realloc(ctx, cff->unpacked_charset, sizeof(uint16_t) * 2 * cff->unpacked_charset_max);
  1338. cff->unpacked_charset_max *= 2;
  1339. }
  1340. cff->unpacked_charset[cff->unpacked_charset_len++] = first;
  1341. first++;
  1342. nleft--;
  1343. }
  1344. }
  1345. }
  1346. else if (fmt == 2)
  1347. {
  1348. cff->unpacked_charset = fz_malloc(ctx, sizeof(uint16_t) * 256);
  1349. cff->unpacked_charset_max = 256;
  1350. cff->unpacked_charset_len = 1;
  1351. cff->unpacked_charset[0] = 0;
  1352. n--;
  1353. while (n > 0)
  1354. {
  1355. uint16_t first;
  1356. uint32_t nleft;
  1357. if (d + 4 >= cff->base + cff->len)
  1358. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1359. first = get16(d);
  1360. nleft = get16(d+2) + 1;
  1361. d += 4;
  1362. if (nleft > n)
  1363. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1364. n -= nleft;
  1365. while (nleft)
  1366. {
  1367. if (cff->unpacked_charset_len == cff->unpacked_charset_max)
  1368. {
  1369. cff->unpacked_charset = fz_realloc(ctx, cff->unpacked_charset, sizeof(uint16_t) * 2 * cff->unpacked_charset_max);
  1370. cff->unpacked_charset_max *= 2;
  1371. }
  1372. cff->unpacked_charset[cff->unpacked_charset_len++] = first;
  1373. first++;
  1374. nleft--;
  1375. }
  1376. }
  1377. }
  1378. else
  1379. {
  1380. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad charset format");
  1381. }
  1382. cff->charset_len = (uint32_t)(d - d0);
  1383. }
  1384. static void
  1385. read_fdselect(fz_context *ctx, cff_t *cff)
  1386. {
  1387. uint32_t fdselect_offset = cff->fdselect_offset;
  1388. const uint8_t *d = cff->base + fdselect_offset;
  1389. const uint8_t *d0 = d;
  1390. uint8_t fmt;
  1391. uint16_t n, m, i, first, last, k;
  1392. if (fdselect_offset == 0)
  1393. {
  1394. cff->fdselect_len = 0;
  1395. return;
  1396. }
  1397. if (fdselect_offset + 1 > cff->len)
  1398. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt fdselect");
  1399. fmt = *d++;
  1400. n = cff->charstrings_index.count;
  1401. cff->gid_to_font = fz_calloc(ctx, n, sizeof(*cff->gid_to_font));
  1402. if (fmt == 0)
  1403. {
  1404. for (i = 0; i < n; i++)
  1405. {
  1406. if (d >= cff->base + cff->len)
  1407. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt fdselect");
  1408. cff->gid_to_font[i] = d[0];
  1409. d++;
  1410. }
  1411. }
  1412. else if (fmt == 3)
  1413. {
  1414. if (d + 2 >= cff->base + cff->len)
  1415. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt fdselect");
  1416. m = get16(d);
  1417. d += 2;
  1418. if (m > cff->charstrings_index.count)
  1419. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt fdselect");
  1420. for (i = 0; i < m; i++)
  1421. {
  1422. if (d + 5 >= cff->base + cff->len)
  1423. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt fdselect");
  1424. first = get16(d);
  1425. last = get16(d + 3);
  1426. if (first >= cff->charstrings_index.count || last > cff->charstrings_index.count || first >= last)
  1427. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt fdselect");
  1428. for (k = first; k < last; k++)
  1429. cff->gid_to_font[k] = d[2];
  1430. d += 3;
  1431. }
  1432. }
  1433. cff->fdselect_len = (uint32_t)(d - d0);
  1434. }
  1435. static void
  1436. load_charset_for_cidfont(fz_context *ctx, cff_t *cff)
  1437. {
  1438. uint32_t charset_offset = cff->charset_offset;
  1439. const uint8_t *d = cff->base + charset_offset;
  1440. uint8_t fmt;
  1441. uint32_t n = cff->charstrings_index.count;
  1442. uint32_t i;
  1443. if (charset_offset + 1 > cff->len)
  1444. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1445. fmt = *d++;
  1446. cff->gid_to_cid = fz_calloc(ctx, n, sizeof(*cff->gid_to_cid));
  1447. cff->gid_to_cid[0] = 0;
  1448. if (fmt == 0)
  1449. {
  1450. for (i = 1; i < n; i++)
  1451. {
  1452. cff->gid_to_cid[i] = get16(d);
  1453. d += 2;
  1454. }
  1455. }
  1456. else if (fmt == 1)
  1457. {
  1458. for (i = 1; i < n;)
  1459. {
  1460. uint16_t first;
  1461. int32_t nleft;
  1462. if (d + 3 >= cff->base + cff->len)
  1463. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1464. first = get16(d);
  1465. nleft = d[2] + 1;
  1466. d += 3;
  1467. while (nleft-- && i < n)
  1468. {
  1469. cff->gid_to_cid[i++] = first++;
  1470. }
  1471. }
  1472. }
  1473. else if (fmt == 2)
  1474. {
  1475. for (i = 1; i < n;)
  1476. {
  1477. uint16_t first;
  1478. int32_t nleft;
  1479. if (d + 4 >= cff->base + cff->len)
  1480. fz_throw(ctx, FZ_ERROR_FORMAT, "corrupt charset");
  1481. first = get16(d);
  1482. nleft = get16(d+2) + 1;
  1483. d += 4;
  1484. while (nleft-- && i < n)
  1485. {
  1486. cff->gid_to_cid[i++] = first++;
  1487. }
  1488. }
  1489. }
  1490. else
  1491. {
  1492. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad charset format");
  1493. }
  1494. }
  1495. static void
  1496. write_offset(fz_context *ctx, fz_output *out, uint8_t os, uint32_t v)
  1497. {
  1498. if (os > 3)
  1499. fz_write_byte(ctx, out, v>>24);
  1500. if (os > 2)
  1501. fz_write_byte(ctx, out, v>>16);
  1502. if (os > 1)
  1503. fz_write_byte(ctx, out, v>>8);
  1504. fz_write_byte(ctx, out, v);
  1505. }
  1506. static void
  1507. output_name_index(fz_context *ctx, cff_t *cff, fz_output *out)
  1508. {
  1509. uint32_t name0 = index_get(ctx, &cff->name_index, 0);
  1510. uint32_t name1 = index_get(ctx, &cff->name_index, 1);
  1511. uint8_t os;
  1512. /* Turn name1 back into an offset from the index. */
  1513. name1 -= name0;
  1514. name1++;
  1515. os = offsize_for_offset(name1);
  1516. fz_write_uint16_be(ctx, out, 1); /* Count */
  1517. fz_write_byte(ctx, out, os); /* offsize */
  1518. write_offset(ctx, out, os, 1); /* index[0] = 1 */
  1519. write_offset(ctx, out, os, name1); /* index[1] = end */
  1520. fz_write_data(ctx, out, cff->base + name0, name1-1);
  1521. }
  1522. static void
  1523. output_top_dict_index(fz_context *ctx, cff_t *cff, fz_output *out)
  1524. {
  1525. uint32_t top_dict_len = (uint32_t)cff->top_dict_subset->len;
  1526. uint8_t os = offsize_for_offset((uint32_t)(1 + top_dict_len));
  1527. fz_write_uint16_be(ctx, out, 1); /* Count */
  1528. fz_write_byte(ctx, out, os); /* offsize */
  1529. write_offset(ctx, out, os, 1);
  1530. write_offset(ctx, out, os, (uint32_t)(1 + cff->top_dict_subset->len));
  1531. /* And copy the updated top dict. */
  1532. fz_write_data(ctx, out, cff->top_dict_subset->data, cff->top_dict_subset->len);
  1533. }
  1534. static uint32_t
  1535. rewrite_fdarray(fz_context *ctx, cff_t *cff, uint32_t offset0)
  1536. {
  1537. /* fdarray_index will start at offset0. */
  1538. uint16_t i;
  1539. uint16_t n = cff->fdarray_index.count;
  1540. uint32_t len = 0;
  1541. uint8_t os;
  1542. size_t offset;
  1543. if (cff->fdarray == NULL)
  1544. fz_throw(ctx, FZ_ERROR_FORMAT, "Expected to rewrite an fdarray");
  1545. /* Count how many bytes the index will require. */
  1546. for (i = 0; i < n; i++)
  1547. {
  1548. len += (uint32_t)cff->fdarray[i].rewritten_dict->len;
  1549. }
  1550. os = offsize_for_offset(len+1);
  1551. len += 2 + 1 + (n+1)*os;
  1552. /* Now offset0 + len points to where the private dicts
  1553. * will go. Run through, fixing up the offsets in the
  1554. * font dicts (this won't change the length). */
  1555. offset = offset0 + len;
  1556. for (i = 0; i < n; i++)
  1557. {
  1558. assert(cff->fdarray[i].rewritten_dict->data[cff->fdarray[i].fixup] == 29);
  1559. assert(cff->fdarray[i].rewritten_dict->data[cff->fdarray[i].fixup+5] == 29);
  1560. put32(&cff->fdarray[i].rewritten_dict->data[cff->fdarray[i].fixup+1], (uint32_t)cff->fdarray[i].rewritten_private->len);
  1561. put32(&cff->fdarray[i].rewritten_dict->data[cff->fdarray[i].fixup+6], (uint32_t)offset);
  1562. offset += cff->fdarray[i].rewritten_private->len;
  1563. if (cff->fdarray[i].local_subset)
  1564. {
  1565. offset += cff->fdarray[i].local_subset->len;
  1566. }
  1567. else
  1568. {
  1569. offset += 2;
  1570. }
  1571. }
  1572. return (uint32_t)offset;
  1573. }
  1574. static void
  1575. update_dicts(fz_context *ctx, cff_t *cff, uint32_t offset)
  1576. {
  1577. uint8_t *top_dict_data = cff->top_dict_subset->data;
  1578. uint32_t top_dict_len = (uint32_t)cff->top_dict_subset->len;
  1579. /* Update the offsets */
  1580. /* Header
  1581. Name Index
  1582. Top Dict Index
  1583. (Top Dict)
  1584. String Index
  1585. Global Subr Index
  1586. Encodings
  1587. Charsets
  1588. FDSelect
  1589. CharStrings Index
  1590. Font DICT Index
  1591. (Font Dict)
  1592. Private DICT
  1593. Local Subr Index
  1594. */
  1595. offset += 2 + 1 + 2 * offsize_for_offset(top_dict_len+1); /* offset = start of top_dict_index data */
  1596. offset += top_dict_len; /* offset = end of top_dict */
  1597. if (cff->string_index.index_size)
  1598. offset += cff->string_index.index_size;
  1599. else
  1600. offset += 2;
  1601. if (cff->global_subset)
  1602. offset += (uint32_t)cff->global_subset->len;
  1603. else if (cff->global_index.index_size)
  1604. offset += cff->global_index.index_size;
  1605. else
  1606. offset += 2;
  1607. if (cff->top_dict_fixup_offsets.encoding)
  1608. {
  1609. assert(top_dict_data[cff->top_dict_fixup_offsets.encoding] == 29);
  1610. put32(top_dict_data + cff->top_dict_fixup_offsets.encoding+1, offset);
  1611. offset += cff->encoding_len;
  1612. }
  1613. if (cff->top_dict_fixup_offsets.charset)
  1614. {
  1615. assert(top_dict_data[cff->top_dict_fixup_offsets.charset] == 29);
  1616. put32(top_dict_data + cff->top_dict_fixup_offsets.charset+1, offset);
  1617. offset += cff->charset_len;
  1618. }
  1619. if (cff->top_dict_fixup_offsets.fdselect)
  1620. {
  1621. assert(top_dict_data[cff->top_dict_fixup_offsets.fdselect] == 29);
  1622. put32(top_dict_data + cff->top_dict_fixup_offsets.fdselect+1, offset);
  1623. offset += cff->fdselect_len;
  1624. }
  1625. assert(top_dict_data[cff->top_dict_fixup_offsets.charstrings] == 29);
  1626. put32(top_dict_data + cff->top_dict_fixup_offsets.charstrings+1, offset);
  1627. if (cff->charstrings_subset)
  1628. offset += (uint32_t)cff->charstrings_subset->len;
  1629. else if (cff->charstrings_index.index_size)
  1630. offset += cff->charstrings_index.index_size;
  1631. else
  1632. offset += 2;
  1633. if (cff->top_dict_fixup_offsets.fdarray)
  1634. {
  1635. assert(top_dict_data[cff->top_dict_fixup_offsets.fdarray] == 29);
  1636. put32(top_dict_data + cff->top_dict_fixup_offsets.fdarray+1, offset);
  1637. offset = rewrite_fdarray(ctx, cff, offset);
  1638. }
  1639. if (cff->top_dict_fixup_offsets.privat)
  1640. {
  1641. assert(top_dict_data[cff->top_dict_fixup_offsets.privat] == 29);
  1642. put32(top_dict_data + cff->top_dict_fixup_offsets.privat+1, (uint32_t)cff->private_subset->len);
  1643. put32(top_dict_data + cff->top_dict_fixup_offsets.privat+6, offset);
  1644. }
  1645. }
  1646. static void
  1647. read_top_dict(fz_context *ctx, cff_t *cff, int idx)
  1648. {
  1649. dict_iterator di;
  1650. dict_operator k;
  1651. uint32_t top_dict_offset = index_get(ctx, &cff->top_dict_index, idx);
  1652. uint32_t top_dict_end = index_get(ctx, &cff->top_dict_index, idx+1);
  1653. for (k = dict_init(ctx, &di, cff->base, cff->len, top_dict_offset, top_dict_end); dict_more(&di); k = dict_next(ctx, &di))
  1654. {
  1655. switch (k)
  1656. {
  1657. case DICT_OP_ROS:
  1658. cff->is_cidfont = 1;
  1659. break;
  1660. case DICT_OP_charset:
  1661. cff->charset_offset = dict_arg_int(ctx, &di, 0);
  1662. break;
  1663. case DICT_OP_Encoding:
  1664. cff->encoding_offset = dict_arg_int(ctx, &di, 0);
  1665. break;
  1666. case DICT_OP_CharstringType:
  1667. cff->charstring_type = 1;
  1668. break;
  1669. case DICT_OP_CharStrings:
  1670. cff->charstrings_index_offset = dict_arg_int(ctx, &di, 0);
  1671. break;
  1672. case DICT_OP_Private:
  1673. cff->private_len = dict_arg_int(ctx, &di, 0);
  1674. cff->private_offset = dict_arg_int(ctx, &di, 1);
  1675. break;
  1676. case DICT_OP_FDSelect:
  1677. cff->fdselect_offset = dict_arg_int(ctx, &di, 0);
  1678. break;
  1679. case DICT_OP_FDArray:
  1680. cff->fdarray_index_offset = dict_arg_int(ctx, &di, 0);
  1681. break;
  1682. default:
  1683. break;
  1684. }
  1685. }
  1686. for (k = dict_init(ctx, &di, cff->base, cff->len, cff->private_offset, cff->private_offset + cff->private_len); dict_more(&di); k = dict_next(ctx, &di))
  1687. {
  1688. switch (k)
  1689. {
  1690. case DICT_OP_Subrs:
  1691. cff->local_index_offset = dict_arg_int(ctx, &di, 0) + cff->private_offset;
  1692. break;
  1693. default:
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. static void
  1699. make_new_top_dict(fz_context *ctx, cff_t *cff)
  1700. {
  1701. dict_iterator di;
  1702. dict_operator k;
  1703. uint32_t top_dict_offset = index_get(ctx, &cff->top_dict_index, 0);
  1704. uint32_t top_dict_end = index_get(ctx, &cff->top_dict_index, 1);
  1705. fz_output *out = NULL;
  1706. cff->top_dict_subset = fz_new_buffer(ctx, 1024);
  1707. fz_var(out);
  1708. fz_try(ctx)
  1709. {
  1710. out = fz_new_output_with_buffer(ctx, cff->top_dict_subset);
  1711. for (k = dict_init(ctx, &di, cff->base, cff->len, top_dict_offset, top_dict_end); dict_more(&di); k = dict_next(ctx, &di))
  1712. {
  1713. switch (k)
  1714. {
  1715. case DICT_OP_charset:
  1716. if (cff->charset_offset < 2)
  1717. di.arg[0].u.i = cff->charset_offset;
  1718. else
  1719. {
  1720. di.arg[0].u.i = 0x80000000;
  1721. cff->top_dict_fixup_offsets.charset = fz_tell_output(ctx, out);
  1722. }
  1723. break;
  1724. case DICT_OP_Encoding:
  1725. if (cff->encoding_offset < 2)
  1726. di.arg[0].u.i = cff->encoding_offset;
  1727. else
  1728. {
  1729. di.arg[0].u.i = 0x80000000;
  1730. cff->top_dict_fixup_offsets.encoding = fz_tell_output(ctx, out);
  1731. }
  1732. break;
  1733. case DICT_OP_CharStrings:
  1734. di.arg[0].u.i = 0x80000000;
  1735. cff->top_dict_fixup_offsets.charstrings = fz_tell_output(ctx, out);
  1736. break;
  1737. case DICT_OP_Private:
  1738. di.arg[0].u.i = 0x80000000;
  1739. di.arg[1].u.i = 0x80000000;
  1740. cff->top_dict_fixup_offsets.privat = fz_tell_output(ctx, out);
  1741. break;
  1742. case DICT_OP_FDSelect:
  1743. di.arg[0].u.i = 0x80000000;
  1744. cff->top_dict_fixup_offsets.fdselect = fz_tell_output(ctx, out);
  1745. break;
  1746. case DICT_OP_FDArray:
  1747. di.arg[0].u.i = 0x80000000;
  1748. cff->top_dict_fixup_offsets.fdarray = fz_tell_output(ctx, out);
  1749. break;
  1750. default:
  1751. break;
  1752. }
  1753. dict_write_args(ctx, out, &di);
  1754. }
  1755. fz_close_output(ctx, out);
  1756. }
  1757. fz_always(ctx)
  1758. fz_drop_output(ctx, out);
  1759. fz_catch(ctx)
  1760. fz_rethrow(ctx);
  1761. }
  1762. static void
  1763. make_new_private_dict(fz_context *ctx, cff_t *cff)
  1764. {
  1765. dict_iterator di;
  1766. dict_operator k;
  1767. fz_output *out = NULL;
  1768. int64_t len;
  1769. cff->private_subset = fz_new_buffer(ctx, 1024);
  1770. fz_var(out);
  1771. fz_try(ctx)
  1772. {
  1773. int subrs = 0;
  1774. out = fz_new_output_with_buffer(ctx, cff->private_subset);
  1775. for (k = dict_init(ctx, &di, cff->base, cff->len, cff->private_offset, cff->private_offset + cff->private_len); dict_more(&di); k = dict_next(ctx, &di))
  1776. {
  1777. switch (k)
  1778. {
  1779. case DICT_OP_Subrs:
  1780. subrs = 1;
  1781. break;
  1782. default:
  1783. dict_write_args(ctx, out, &di);
  1784. }
  1785. }
  1786. if (subrs != 0)
  1787. {
  1788. /* Everything is in the DICT except for the local subr offset. Insert
  1789. * that now. This is tricky, because what is the offset? It depends on
  1790. * the size of the dict we are creating now, and the size of the dict
  1791. * we are creating now depends on the size of the offset! */
  1792. /* Length so far */
  1793. len = fz_tell_output(ctx, out);
  1794. /* We have to encode an offset, plus the Subrs token (19). Offset
  1795. * can take up to 5 bytes. */
  1796. if (len+2 < 107)
  1797. {
  1798. /* We can code it with a single byte encoding */
  1799. len += 2;
  1800. fz_write_byte(ctx, out, len + 139);
  1801. }
  1802. else if (len+3 < 1131)
  1803. {
  1804. /* We can code it with a 2 byte encoding */
  1805. /* (b0-247) * 256 + b1 + 108 == len+3 */
  1806. len = len+3 - 108;
  1807. fz_write_byte(ctx, out, (len>>8) + 247);
  1808. fz_write_byte(ctx, out, len);
  1809. }
  1810. else if (len+4 < 32767)
  1811. {
  1812. /* We can code it with a 3 byte encoding */
  1813. len += 4;
  1814. fz_write_byte(ctx, out, 28);
  1815. fz_write_byte(ctx, out, len>>8);
  1816. fz_write_byte(ctx, out, len);
  1817. }
  1818. else
  1819. {
  1820. /* We can code it with a 5 byte encoding */
  1821. len += 5;
  1822. fz_write_byte(ctx, out, 29);
  1823. fz_write_byte(ctx, out, len>>24);
  1824. fz_write_byte(ctx, out, len>>16);
  1825. fz_write_byte(ctx, out, len>>8);
  1826. fz_write_byte(ctx, out, len);
  1827. }
  1828. fz_write_byte(ctx, out, DICT_OP_Subrs);
  1829. }
  1830. fz_close_output(ctx, out);
  1831. }
  1832. fz_always(ctx)
  1833. fz_drop_output(ctx, out);
  1834. fz_catch(ctx)
  1835. fz_rethrow(ctx);
  1836. }
  1837. static void
  1838. read_fdarray_and_privates(fz_context *ctx, cff_t *cff)
  1839. {
  1840. dict_iterator di;
  1841. dict_operator k;
  1842. uint16_t i;
  1843. uint16_t n = cff->fdarray_index.count;
  1844. int subrs;
  1845. int64_t len;
  1846. cff->fdarray = fz_calloc(ctx, n, sizeof(*cff->fdarray));
  1847. for (i = 0; i < n; i++)
  1848. {
  1849. uint32_t offset = index_get(ctx, &cff->fdarray_index, i);
  1850. uint32_t end = index_get(ctx, &cff->fdarray_index, i+1);
  1851. fz_output *out = NULL;
  1852. cff->fdarray[i].rewritten_dict = fz_new_buffer(ctx, 1024);
  1853. fz_var(out);
  1854. fz_try(ctx)
  1855. {
  1856. out = fz_new_output_with_buffer(ctx, cff->fdarray[i].rewritten_dict);
  1857. for (k = dict_init(ctx, &di, cff->base, cff->len, offset, end); dict_more(&di); k = dict_next(ctx, &di))
  1858. {
  1859. switch (k)
  1860. {
  1861. case DICT_OP_Private:
  1862. cff->fdarray[i].len = di.arg[0].u.i;
  1863. cff->fdarray[i].offset = di.arg[1].u.i;
  1864. di.arg[0].u.i = 0x80000000;
  1865. di.arg[1].u.i = 0x80000000;
  1866. cff->fdarray[i].fixup = fz_tell_output(ctx, out);
  1867. break;
  1868. default:
  1869. break;
  1870. }
  1871. dict_write_args(ctx, out, &di);
  1872. }
  1873. fz_close_output(ctx, out);
  1874. }
  1875. fz_always(ctx)
  1876. fz_drop_output(ctx, out);
  1877. fz_catch(ctx)
  1878. fz_rethrow(ctx);
  1879. offset = cff->fdarray[i].offset;
  1880. end = cff->fdarray[i].offset + cff->fdarray[i].len;
  1881. fz_try(ctx)
  1882. {
  1883. cff->fdarray[i].rewritten_private = fz_new_buffer(ctx, 1024);
  1884. out = fz_new_output_with_buffer(ctx, cff->fdarray[i].rewritten_private);
  1885. cff->fdarray[i].local_index_offset = 0;
  1886. subrs = 0;
  1887. for (k = dict_init(ctx, &di, cff->base, cff->len, offset, end); dict_more(&di); k = dict_next(ctx, &di))
  1888. {
  1889. switch (k)
  1890. {
  1891. case DICT_OP_Subrs:
  1892. subrs = 1;
  1893. cff->fdarray[i].local_index_offset = dict_arg_int(ctx, &di, 0) + offset;
  1894. break;
  1895. default:
  1896. dict_write_args(ctx, out, &di);
  1897. break;
  1898. }
  1899. }
  1900. if (subrs != 0)
  1901. {
  1902. /* Everything is in the DICT except for the local subr offset. Insert
  1903. * that now. This is tricky, because what is the offset? It depends on
  1904. * the size of he dict we are creating now, and the size of the dict
  1905. * we are creating now depends on the size of the offset! */
  1906. /* Length so far */
  1907. len = fz_tell_output(ctx, out);
  1908. /* We have to encode an offset, plus the Subrs token (19). Offset
  1909. * can take up to 5 bytes. */
  1910. if (len+2 < 107)
  1911. {
  1912. /* We can code it with a single byte encoding */
  1913. len += 2;
  1914. fz_write_byte(ctx, out, len + 139);
  1915. }
  1916. else if (len+3 < 1131)
  1917. {
  1918. /* We can code it with a 2 byte encoding */
  1919. /* (b0-247) * 256 + b1 + 108 == len+3 */
  1920. len = len+3 - 108;
  1921. fz_write_byte(ctx, out, (len>>8) + 247);
  1922. fz_write_byte(ctx, out, len);
  1923. }
  1924. else if (len+4 < 32767)
  1925. {
  1926. /* We can code it with a 3 byte encoding */
  1927. len += 4;
  1928. fz_write_byte(ctx, out, 28);
  1929. fz_write_byte(ctx, out, len>>8);
  1930. fz_write_byte(ctx, out, len);
  1931. }
  1932. else
  1933. {
  1934. /* We can code it with a 5 byte encoding */
  1935. len += 5;
  1936. fz_write_byte(ctx, out, 29);
  1937. fz_write_byte(ctx, out, len>>24);
  1938. fz_write_byte(ctx, out, len>>16);
  1939. fz_write_byte(ctx, out, len>>8);
  1940. fz_write_byte(ctx, out, len);
  1941. }
  1942. fz_write_byte(ctx, out, DICT_OP_Subrs);
  1943. }
  1944. fz_close_output(ctx, out);
  1945. }
  1946. fz_always(ctx)
  1947. fz_drop_output(ctx, out);
  1948. fz_catch(ctx)
  1949. fz_rethrow(ctx);
  1950. if (cff->fdarray[i].local_index_offset != 0)
  1951. {
  1952. index_load(ctx, &cff->fdarray[i].local_index, cff->base, (uint32_t)cff->len, cff->fdarray[i].local_index_offset);
  1953. cff->fdarray[i].subr_bias = subr_bias(ctx, cff, cff->fdarray[i].local_index.count);
  1954. }
  1955. }
  1956. }
  1957. static void
  1958. output_fdarray(fz_context *ctx, fz_output *out, cff_t *cff)
  1959. {
  1960. uint16_t i;
  1961. uint16_t n = cff->fdarray_index.count;
  1962. uint8_t os;
  1963. uint32_t offset = 1;
  1964. uint32_t len = 0;
  1965. for (i = 0; i < n; i++)
  1966. {
  1967. len += (uint32_t)cff->fdarray[i].rewritten_dict->len;
  1968. }
  1969. os = offsize_for_offset(len+1);
  1970. fz_write_uint16_be(ctx, out, cff->fdarray_index.count); /* Count */
  1971. fz_write_byte(ctx, out, os); /* offsize */
  1972. /* First we write out the offsets of the rewritten dicts. */
  1973. for (i = 0; i < n; i++)
  1974. {
  1975. write_offset(ctx, out, os, offset);
  1976. offset += (uint32_t)cff->fdarray[i].rewritten_dict->len;
  1977. }
  1978. write_offset(ctx, out, os, offset);
  1979. /* Now write the dicts themselves. */
  1980. for (i = 0; i < n; i++)
  1981. {
  1982. fz_write_data(ctx, out, cff->fdarray[i].rewritten_dict->data, cff->fdarray[i].rewritten_dict->len);
  1983. }
  1984. /* Now we can write out the private dicts, unchanged from the original file. */
  1985. for (i = 0; i < n; i++)
  1986. {
  1987. fz_write_data(ctx, out, cff->fdarray[i].rewritten_private->data, cff->fdarray[i].rewritten_private->len);
  1988. if (cff->fdarray[i].local_subset)
  1989. fz_write_data(ctx, out, cff->fdarray[i].local_subset->data, cff->fdarray[i].local_subset->len);
  1990. else
  1991. fz_write_uint16_be(ctx, out, 0);
  1992. }
  1993. }
  1994. /* Nasty O(n^2) thing. */
  1995. static uint16_t
  1996. cid_to_gid(fz_context *ctx, cff_t *cff, uint16_t cid)
  1997. {
  1998. uint32_t n = cff->charstrings_index.count;
  1999. uint32_t i;
  2000. for (i = 0; i < n; i++)
  2001. {
  2002. if (cff->gid_to_cid[i] == cid)
  2003. return i;
  2004. }
  2005. return 0;
  2006. }
  2007. fz_buffer *
  2008. fz_subset_cff_for_gids(fz_context *ctx, fz_buffer *orig, int *gids, int num_gids, int symbolic, int is_pdf_cidfont)
  2009. {
  2010. cff_t cff = { 0 };
  2011. fz_buffer *newbuf = NULL;
  2012. uint8_t *base;
  2013. size_t len;
  2014. fz_output *out = NULL;
  2015. int i;
  2016. uint16_t n, k;
  2017. fz_var(newbuf);
  2018. fz_var(out);
  2019. if (orig == NULL)
  2020. return NULL;
  2021. base = orig->data;
  2022. len = orig->len;
  2023. fz_try(ctx)
  2024. {
  2025. cff.base = base;
  2026. cff.len = len;
  2027. cff.symbolic = symbolic;
  2028. if (len < 4)
  2029. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated CFF");
  2030. cff.major = base[0];
  2031. cff.minor = base[1];
  2032. cff.headersize = base[2];
  2033. cff.offsize = base[3];
  2034. if (cff.offsize > 4)
  2035. fz_throw(ctx, FZ_ERROR_FORMAT, "Invalid offsize in CFF");
  2036. if (len > UINT32_MAX)
  2037. fz_throw(ctx, FZ_ERROR_FORMAT, "CFF too large");
  2038. /* First, the name index */
  2039. cff.top_dict_index_offset = index_load(ctx, &cff.name_index, base, (uint32_t)len, cff.headersize);
  2040. /* Next, the top dict index */
  2041. cff.string_index_offset = index_load(ctx, &cff.top_dict_index, base, (uint32_t)len, cff.top_dict_index_offset);
  2042. /* Next, the string index */
  2043. cff.global_index_offset = index_load(ctx, &cff.string_index, base, (uint32_t)len, cff.string_index_offset);
  2044. /* Next the Global subr index */
  2045. index_load(ctx, &cff.global_index, base, (uint32_t)len, cff.global_index_offset);
  2046. /* Default value, possibly updated by top dict entries */
  2047. cff.charstring_type = 2;
  2048. /* CFF files can contain several fonts, but we only want the first one. */
  2049. read_top_dict(ctx, &cff, 0);
  2050. cff.gsubr_bias = subr_bias(ctx, &cff, cff.global_index.count);
  2051. if (cff.charstrings_index_offset == 0)
  2052. fz_throw(ctx, FZ_ERROR_FORMAT, "Missing charstrings table");
  2053. index_load(ctx, &cff.charstrings_index, base, (uint32_t)len, cff.charstrings_index_offset);
  2054. index_load(ctx, &cff.local_index, base, (uint32_t)len, cff.local_index_offset);
  2055. cff.subr_bias = subr_bias(ctx, &cff, cff.local_index.count);
  2056. index_load(ctx, &cff.fdarray_index, base, (uint32_t)len, cff.fdarray_index_offset);
  2057. get_encoding_len(ctx, &cff);
  2058. get_charset_len(ctx, &cff);
  2059. if (is_pdf_cidfont && cff.is_cidfont)
  2060. {
  2061. read_fdselect(ctx, &cff);
  2062. read_fdarray_and_privates(ctx, &cff);
  2063. }
  2064. /* Move our list of gids into our own storage. */
  2065. if (is_pdf_cidfont && cff.is_cidfont)
  2066. {
  2067. /* For CIDFontType0 FontDescriptor with a CFF that uses CIDFont operators,
  2068. * we are given CIDs here, not GIDs. Accordingly
  2069. * we need to look them up in the CharSet.
  2070. */
  2071. load_charset_for_cidfont(ctx, &cff);
  2072. for (i = 0; i < num_gids; i++)
  2073. usage_list_add(ctx, &cff.gids_to_keep, cid_to_gid(ctx, &cff, gids[i]));
  2074. }
  2075. else
  2076. {
  2077. /* For CIDFontType0 FontDescriptor with a CFF that DOES NOT use CIDFont operators,
  2078. * and for Type1 FontDescriptors, we are given GIDs directly.
  2079. */
  2080. for (i = 0; i < num_gids; i++)
  2081. usage_list_add(ctx, &cff.gids_to_keep, gids[i]);
  2082. }
  2083. /* Scan charstrings. */
  2084. scan_charstrings(ctx, &cff, is_pdf_cidfont);
  2085. /* Now subset the data. */
  2086. subset_charstrings(ctx, &cff);
  2087. if (is_pdf_cidfont && cff.is_cidfont)
  2088. subset_fdarray_locals(ctx, &cff);
  2089. subset_locals(ctx, &cff);
  2090. subset_globals(ctx, &cff);
  2091. /* FIXME: cull the strings? */
  2092. /* Now, rewrite the font.
  2093. There are various sections for this, as follows:
  2094. SECTION CIDFonts Dict
  2095. (Subsection) only Contains
  2096. absolute
  2097. offsets?
  2098. Header
  2099. Name Index
  2100. Top Dict Index
  2101. (Top Dict) Y
  2102. String Index
  2103. Global Subr Index
  2104. Encodings
  2105. Charsets
  2106. FDSelect Y
  2107. CharStrings Index
  2108. Font DICT Index Y
  2109. (Font Dict) N
  2110. Private DICT N
  2111. Local Subr Index
  2112. The size of global offsets varies according to how large the file is,
  2113. therefore we need to take care.
  2114. The 'suffix' of sections from String Index onwards are independent of
  2115. this global offset size, so we finalise those sections first.
  2116. We can then use this size to inform our choice of offset size for the
  2117. top dictionary.
  2118. So, layout the sections from the end backwards.
  2119. */
  2120. /* Local Subr Index */
  2121. /* Private DICT */
  2122. make_new_private_dict(ctx, &cff);
  2123. /* Font DICT - CIDFont only */
  2124. /* Charstrings - already done */
  2125. /* FDSelect - CIDFont only */
  2126. /* Charsets - unchanged */
  2127. /* Encoding - unchanged */
  2128. /* Globals */
  2129. /* Strings - unchanged */
  2130. make_new_top_dict(ctx, &cff);
  2131. newbuf = fz_new_buffer(ctx, 1024);
  2132. out = fz_new_output_with_buffer(ctx, newbuf);
  2133. /* Copy header */
  2134. fz_write_byte(ctx, out, cff.major);
  2135. fz_write_byte(ctx, out, cff.minor);
  2136. fz_write_byte(ctx, out, 4);
  2137. fz_write_byte(ctx, out, cff.offsize);
  2138. output_name_index(ctx, &cff, out);
  2139. update_dicts(ctx, &cff, fz_tell_output(ctx, out));
  2140. output_top_dict_index(ctx, &cff, out);
  2141. /* Copy strings index */
  2142. if (cff.string_index.index_size)
  2143. fz_write_data(ctx, out, base + cff.string_index.index_offset, cff.string_index.index_size);
  2144. else
  2145. fz_write_uint16_be(ctx, out, 0);
  2146. /* Copy globals index (if there is one) */
  2147. if (cff.global_subset)
  2148. fz_write_data(ctx, out, cff.global_subset->data, cff.global_subset->len);
  2149. else if (cff.global_index.index_size)
  2150. fz_write_data(ctx, out, base + cff.global_index.index_offset, cff.global_index.index_size);
  2151. else
  2152. fz_write_uint16_be(ctx, out, 0);
  2153. /* Copy encoding */
  2154. if (cff.encoding_offset > 2)
  2155. fz_write_data(ctx, out, base + cff.encoding_offset, cff.encoding_len);
  2156. /* Copy charset */
  2157. if (cff.charset_offset > 2)
  2158. fz_write_data(ctx, out, base + cff.charset_offset, cff.charset_len);
  2159. if (cff.fdselect_offset)
  2160. fz_write_data(ctx, out, base + cff.fdselect_offset, cff.fdselect_len);
  2161. /* Copy charstrings */
  2162. if (cff.charstrings_subset)
  2163. fz_write_data(ctx, out, cff.charstrings_subset->data, cff.charstrings_subset->len);
  2164. else if (cff.charstrings_index.index_size)
  2165. fz_write_data(ctx, out, base + cff.charstrings_index.index_offset, cff.charstrings_index.index_size);
  2166. else
  2167. fz_write_uint16_be(ctx, out, 0);
  2168. if (cff.fdarray)
  2169. output_fdarray(ctx, out, &cff);
  2170. /* Copy Private dict */
  2171. fz_write_data(ctx, out, cff.private_subset->data, cff.private_subset->len);
  2172. /* Copy the local table - subsetted if there is one, original if not, or maybe none! */
  2173. if (cff.local_subset)
  2174. fz_write_data(ctx, out, cff.local_subset->data, cff.local_subset->len);
  2175. else if (cff.local_index.index_size)
  2176. fz_write_data(ctx, out, base + cff.local_index.index_offset, cff.local_index.index_size);
  2177. fz_close_output(ctx, out);
  2178. }
  2179. fz_always(ctx)
  2180. {
  2181. fz_drop_output(ctx, out);
  2182. fz_drop_buffer(ctx, cff.private_subset);
  2183. fz_drop_buffer(ctx, cff.charstrings_subset);
  2184. fz_drop_buffer(ctx, cff.top_dict_subset);
  2185. fz_drop_buffer(ctx, cff.local_subset);
  2186. fz_drop_buffer(ctx, cff.global_subset);
  2187. fz_free(ctx, cff.gid_to_cid);
  2188. fz_free(ctx, cff.gid_to_font);
  2189. drop_usage_list(ctx, &cff.local_usage);
  2190. drop_usage_list(ctx, &cff.global_usage);
  2191. drop_usage_list(ctx, &cff.gids_to_keep);
  2192. drop_usage_list(ctx, &cff.extra_gids_to_keep);
  2193. if (cff.fdarray)
  2194. {
  2195. n = cff.fdarray_index.count;
  2196. for (k = 0; k < n; k++)
  2197. {
  2198. fz_drop_buffer(ctx, cff.fdarray[k].rewritten_dict);
  2199. fz_drop_buffer(ctx, cff.fdarray[k].rewritten_private);
  2200. fz_drop_buffer(ctx, cff.fdarray[k].local_subset);
  2201. drop_usage_list(ctx, &cff.fdarray[k].local_usage);
  2202. }
  2203. fz_free(ctx, cff.fdarray);
  2204. }
  2205. fz_free(ctx, cff.unpacked_charset);
  2206. }
  2207. fz_catch(ctx)
  2208. {
  2209. fz_drop_buffer(ctx, newbuf);
  2210. fz_rethrow(ctx);
  2211. }
  2212. return newbuf;
  2213. }