subset-ttf.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  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. /*
  24. For the purposes of this code, and to save my tiny brain from
  25. overload, we will adopt the following notation:
  26. 1) The PDF file contains bytes of data. These bytes are looked
  27. up in the MuPDF font handling to resolve to 'glyph ids' (gids).
  28. These account for all the different encodings etc in use,
  29. including the 'cmap' table within the font.
  30. 2) We are given the list of gids that are used in the document.
  31. We arrange to keep any entries in the cmap or post tables that
  32. maps to these gids.
  33. We map the gids to the bottom of the range. This means that the
  34. cmap and post tables need to be updated.
  35. A similar optimisation would be to compress the range of cids
  36. used to a prefix of the range used. This would mean that the
  37. calling code needs to rewrite the data within the PDF file -
  38. both in terms of the strings used with the PDF streams, and in
  39. terms of the ToUnicode tables there (and the Widths etc).
  40. For now, we'll ignore this optimisation.
  41. Possibly, in the case of 'Identity' Tounicode mappings we
  42. wouldn't actually want to do this range compression? It'd only
  43. make the file larger.
  44. */
  45. typedef struct
  46. {
  47. uint16_t pid;
  48. uint16_t psid;
  49. uint32_t max;
  50. uint16_t gid[256];
  51. } encoding_t;
  52. typedef struct
  53. {
  54. uint32_t tag;
  55. uint32_t checksum;
  56. fz_buffer *tab;
  57. } tagged_table_t;
  58. typedef struct
  59. {
  60. int is_otf;
  61. int symbolic;
  62. encoding_t *encoding;
  63. uint16_t orig_num_glyphs;
  64. uint16_t new_num_glyphs;
  65. uint16_t index_to_loc_format;
  66. uint8_t *index_to_loc_formatp;
  67. uint16_t orig_num_long_hor_metrics;
  68. uint16_t new_num_long_hor_metrics;
  69. /* Pointer to the old tables (in the tagged table below) */
  70. uint8_t *loca;
  71. size_t *loca_len;
  72. uint8_t *maxp;
  73. /* Maps from old gid to new gid */
  74. uint16_t *gid_renum;
  75. int max;
  76. int len;
  77. tagged_table_t *table;
  78. } ttf_t;
  79. static uint32_t
  80. checksum(fz_buffer *buf)
  81. {
  82. size_t i;
  83. const uint8_t *d = (const uint8_t *)buf->data;
  84. uint32_t cs = 0;
  85. for (i = buf->len>>2; i > 0; i--)
  86. {
  87. cs += d[0]<<24;
  88. cs += d[1]<<16;
  89. cs += d[2]<<8;
  90. cs += d[3];
  91. d += 4;
  92. }
  93. i = buf->len - (buf->len & ~3);
  94. switch (i)
  95. {
  96. case 3:
  97. cs += d[2]<<8;
  98. /* fallthrough */
  99. case 2:
  100. cs += d[1]<<16;
  101. /* fallthrough */
  102. case 1:
  103. cs += d[0]<<24;
  104. default:
  105. break;
  106. }
  107. return cs;
  108. }
  109. static uint32_t
  110. find_table(fz_context *ctx, fz_stream *stm, uint32_t tag, uint32_t *len)
  111. {
  112. int num_tables;
  113. int i;
  114. fz_seek(ctx, stm, 4, SEEK_SET);
  115. num_tables = fz_read_int16(ctx, stm);
  116. fz_seek(ctx, stm, 12, SEEK_SET);
  117. for (i = 0; i < num_tables; i++)
  118. {
  119. uint32_t t = fz_read_uint32(ctx, stm);
  120. uint32_t cs = fz_read_uint32(ctx, stm);
  121. uint32_t off = fz_read_uint32(ctx, stm);
  122. (void) cs; /* UNUSED */
  123. *len = fz_read_uint32(ctx, stm);
  124. if (t == tag)
  125. return off;
  126. }
  127. return 0;
  128. }
  129. static fz_buffer *
  130. read_table(fz_context *ctx, fz_stream *stm, uint32_t tag, int compulsory)
  131. {
  132. uint32_t size;
  133. uint32_t off = find_table(ctx, stm, tag, &size);
  134. fz_buffer *buf;
  135. if (off == 0)
  136. {
  137. if (compulsory)
  138. fz_throw(ctx, FZ_ERROR_FORMAT, "Required %c%c%c%c table missing", tag>>24, (tag>>16)&0xff, (tag>>8)&0xff, tag & 0xff);
  139. return NULL;
  140. }
  141. fz_seek(ctx, stm, off, SEEK_SET);
  142. buf = fz_new_buffer(ctx, size);
  143. fz_try(ctx)
  144. {
  145. fz_read(ctx, stm, buf->data, size);
  146. buf->len = size;
  147. }
  148. fz_catch(ctx)
  149. {
  150. fz_drop_buffer(ctx, buf);
  151. fz_rethrow(ctx);
  152. }
  153. return buf;
  154. }
  155. #define TAG(s) \
  156. ( (((uint8_t)s[0])<<24) | \
  157. (((uint8_t)s[1])<<16) | \
  158. (((uint8_t)s[2])<<8) | \
  159. (((uint8_t)s[3])))
  160. static void
  161. add_table(fz_context *ctx, ttf_t *ttf, uint32_t tag, fz_buffer *tab)
  162. {
  163. fz_try(ctx)
  164. {
  165. if (ttf->max == ttf->len)
  166. {
  167. int n = ttf->max * 2;
  168. if (n == 0)
  169. n = 16;
  170. ttf->table = fz_realloc(ctx, ttf->table, sizeof(*ttf->table) * n);
  171. ttf->max = n;
  172. }
  173. ttf->table[ttf->len].tag = tag;
  174. ttf->table[ttf->len].tab = tab;
  175. ttf->len++;
  176. }
  177. fz_catch(ctx)
  178. {
  179. fz_drop_buffer(ctx, tab);
  180. fz_rethrow(ctx);
  181. }
  182. }
  183. static void
  184. copy_table(fz_context *ctx, ttf_t *ttf, fz_stream *stm, uint32_t tag, int compulsory)
  185. {
  186. fz_buffer *t;
  187. t = read_table(ctx, stm, tag, compulsory);
  188. if (t)
  189. add_table(ctx, ttf, tag, t);
  190. }
  191. static int
  192. tabcmp(const void *a_, const void *b_)
  193. {
  194. const tagged_table_t *a = (const tagged_table_t *)a_;
  195. const tagged_table_t *b = (const tagged_table_t *)b_;
  196. return (a->tag - b->tag);
  197. }
  198. static void
  199. sort_tables(fz_context *ctx, ttf_t *ttf)
  200. {
  201. /* Avoid scanbuild/coverity false warning with this unnecessary test */
  202. if (ttf->table == NULL || ttf->len == 0)
  203. return;
  204. qsort(ttf->table, ttf->len, sizeof(tagged_table_t), tabcmp);
  205. }
  206. static void
  207. checksum_tables(fz_context *ctx, ttf_t *ttf)
  208. {
  209. int i;
  210. for (i = 0; i < ttf->len; i++)
  211. ttf->table[i].checksum = checksum(ttf->table[i].tab);
  212. }
  213. static void
  214. write_tables(fz_context *ctx, ttf_t *ttf, fz_output *out)
  215. {
  216. int i = 0;
  217. uint32_t offset;
  218. /* scalar type - TTF for now - may need to cope with other types later. */
  219. if (ttf->is_otf)
  220. fz_write_int32_be(ctx, out, 0x4f54544f);
  221. else
  222. fz_write_int32_be(ctx, out, 0x00010000);
  223. /* number of tables */
  224. fz_write_uint16_be(ctx, out, ttf->len);
  225. while (1<<(i+1) <= ttf->len)
  226. i++;
  227. /* searchRange */
  228. fz_write_uint16_be(ctx, out, (1<<i)<<4);
  229. /* entrySelector */
  230. fz_write_uint16_be(ctx, out, i);
  231. /* rangeShift*/
  232. fz_write_uint16_be(ctx, out, (ttf->len - (1<<i))<<4);
  233. /* Table directory */
  234. offset = 12 + ttf->len * 16;
  235. for (i = 0; i < ttf->len; i++)
  236. {
  237. fz_write_uint32_be(ctx, out, ttf->table[i].tag);
  238. fz_write_uint32_be(ctx, out, ttf->table[i].checksum);
  239. fz_write_uint32_be(ctx, out, offset);
  240. fz_write_uint32_be(ctx, out, (uint32_t)ttf->table[i].tab->len);
  241. offset += (uint32_t)ttf->table[i].tab->len;
  242. }
  243. /* Now the tables in turn */
  244. for (i = 0; i < ttf->len; i++)
  245. {
  246. fz_write_buffer(ctx, out, ttf->table[i].tab);
  247. }
  248. }
  249. static void
  250. fix_checksum(fz_context *ctx, fz_buffer *buf)
  251. {
  252. uint8_t *data;
  253. uint32_t sum = 0;
  254. size_t len = fz_buffer_storage(ctx, buf, &data);
  255. uint32_t namesize;
  256. fz_stream *stm = fz_open_buffer(ctx, buf);
  257. uint32_t csumpos = find_table(ctx, stm, TAG("head"), &namesize) + 8;
  258. (void) len; // UNUSED
  259. fz_drop_stream(ctx, stm);
  260. /* First off, blat the old checksum */
  261. memset(data+csumpos, 0, 4);
  262. sum = checksum(buf);
  263. sum = 0xb1b0afba-sum;
  264. /* Insert it. */
  265. data[csumpos] = sum>>24;
  266. data[csumpos+1] = sum>>16;
  267. data[csumpos+2] = sum>>8;
  268. data[csumpos+3] = sum;
  269. }
  270. typedef struct
  271. {
  272. uint16_t platform_id;
  273. uint16_t platform_specific_id;
  274. uint16_t language_id;
  275. uint16_t name_id;
  276. uint16_t len;
  277. uint16_t offset;
  278. } name_record_t;
  279. static uint32_t get32(const uint8_t *d)
  280. {
  281. return (d[0]<<24)|(d[1]<<16)|(d[2]<<8)|d[3];
  282. }
  283. static uint32_t get16(const uint8_t *d)
  284. {
  285. return (d[0]<<8)|d[1];
  286. }
  287. static void put32(uint8_t *d, uint32_t v)
  288. {
  289. d[0] = v>>24;
  290. d[1] = v>>16;
  291. d[2] = v>>8;
  292. d[3] = v;
  293. }
  294. static void put16(uint8_t *d, uint32_t v)
  295. {
  296. d[0] = v>>8;
  297. d[1] = v;
  298. }
  299. typedef struct
  300. {
  301. /* First 2 fields aren't actually needed for the pointer list
  302. * operation, but they serve as bounds for all the offsets used
  303. * within the ptr list. */
  304. uint8_t *block;
  305. size_t block_len;
  306. uint32_t len;
  307. uint32_t max;
  308. uint8_t **ptr;
  309. } ptr_list_t;
  310. static void
  311. ptr_list_add(fz_context *ctx, ptr_list_t *pl, uint8_t *ptr)
  312. {
  313. if (pl->len == pl->max)
  314. {
  315. int n = pl->max * 2;
  316. if (n == 0)
  317. n = 32;
  318. pl->ptr = fz_realloc(ctx, pl->ptr, sizeof(*pl->ptr) * n);
  319. pl->max = n;
  320. }
  321. pl->ptr[pl->len++] = ptr;
  322. }
  323. typedef int (cmp_t)(const uint8_t **a, const uint8_t **b);
  324. typedef int (void_cmp_t)(const void *, const void *);
  325. static void
  326. ptr_list_sort(fz_context *ctx, ptr_list_t *pl, cmp_t *cmp)
  327. {
  328. /* Avoid scanbuild/coverity false warning with this unnecessary test */
  329. if (pl->ptr == NULL || pl->len == 0)
  330. return;
  331. qsort(pl->ptr, pl->len, sizeof(*pl->ptr), (void_cmp_t *)cmp);
  332. }
  333. static void
  334. drop_ptr_list(fz_context *ctx, ptr_list_t *pl)
  335. {
  336. fz_free(ctx, pl->ptr);
  337. }
  338. /* return 1 to keep, 0 to drop. */
  339. typedef int (filter_t)(const uint8_t *ptr, const uint8_t *blk, size_t len);
  340. /* This makes a pointer list from a filtered block, moving the underlying data as it filters. */
  341. static void
  342. ptr_list_compact(fz_context *ctx, ptr_list_t *pl, filter_t *fil, uint8_t *base, int n, size_t eltsize, uint8_t *block, size_t block_len)
  343. {
  344. int i;
  345. uint8_t *s = base;
  346. uint8_t *d = base;
  347. pl->block = block;
  348. pl->block_len = block_len;
  349. if (base < block || (size_t)(base - block) > block_len || (size_t)(base - block) + n * eltsize >= block_len)
  350. fz_throw(ctx, FZ_ERROR_FORMAT, "Ptr List creation failed");
  351. for (i = 0; i < n; i++)
  352. {
  353. if (fil(s, block, block_len))
  354. {
  355. ptr_list_add(ctx, pl, d);
  356. if (s != d)
  357. memmove(d, s, eltsize);
  358. d += eltsize;
  359. }
  360. s += eltsize;
  361. }
  362. }
  363. static int
  364. names_by_size(const uint8_t **a, const uint8_t **b)
  365. {
  366. return get16((*b)+8) - get16((*a)+8);
  367. }
  368. static int
  369. filter_name_tables(const uint8_t *ptr, const uint8_t *block, size_t block_len)
  370. {
  371. /* FIXME: For now, we keep everything. */
  372. return 1;
  373. }
  374. #define UNFOUND ((uint32_t)-1)
  375. static uint32_t
  376. find_string_in_block(const uint8_t *str, size_t str_len, const uint8_t *block, size_t block_len)
  377. {
  378. const uint8_t *b = block;
  379. if (block_len == 0)
  380. return UNFOUND;
  381. assert(block_len >= str_len);
  382. block_len -= str_len-1;
  383. while (block_len--)
  384. {
  385. if (!memcmp(str, b, str_len))
  386. return (uint32_t)(b - block);
  387. b++;
  388. }
  389. return UNFOUND;
  390. }
  391. static void
  392. subset_name_table(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  393. {
  394. fz_buffer *t = read_table(ctx, stm, TAG("name"), 0);
  395. uint8_t *d;
  396. uint32_t i, n, off;
  397. ptr_list_t pl = { 0 };
  398. size_t name_data_size;
  399. uint8_t *new_name_data = NULL;
  400. size_t new_len;
  401. if (t == NULL)
  402. return; /* No name table */
  403. d = t->data;
  404. fz_var(new_name_data);
  405. fz_try(ctx)
  406. {
  407. if (get16(d) != 0 || t->len < 6)
  408. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported name table format");
  409. n = get16(d+2);
  410. off = get16(d+4);
  411. name_data_size = t->len - 6 - 12*n;
  412. if (t->len < 6 + 12*n)
  413. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated name table");
  414. ptr_list_compact(ctx, &pl, filter_name_tables, d+6, n, 12, d, t->len);
  415. /* Sort our list so that the ones with the largest name data blocks come first. */
  416. ptr_list_sort(ctx, &pl, names_by_size);
  417. new_name_data = fz_malloc(ctx, name_data_size);
  418. new_len = 0;
  419. for (i = 0; i < pl.len; i++)
  420. {
  421. uint32_t name_len, offset, name_off;
  422. uint8_t *name;
  423. if (t->len < (size_t) (pl.ptr[i] - t->data) + 8 + 2)
  424. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated name length in name table");
  425. name_len = get16(pl.ptr[i] + 8);
  426. if (t->len < (size_t) (pl.ptr[i] - t->data) + 10 + 2)
  427. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated name offset in name table");
  428. name_off = off + get16(pl.ptr[i] + 10);
  429. name = d + name_off;
  430. if (t->len < name_off + name_len)
  431. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated name in name table");
  432. offset = find_string_in_block(name, name_len, new_name_data, new_len);
  433. if (offset == UNFOUND)
  434. {
  435. if (name_data_size < new_len + name_len)
  436. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad name table in TTF");
  437. memcpy(new_name_data + new_len, name, name_len);
  438. offset = (uint32_t)new_len;
  439. new_len += name_len;
  440. }
  441. put16(pl.ptr[i]+10, offset);
  442. }
  443. memcpy(d + 6 + 12*pl.len, new_name_data, new_len);
  444. t->len = 6 + 12*pl.len + new_len;
  445. put16(d+4, 6 + 12*pl.len);
  446. }
  447. fz_always(ctx)
  448. {
  449. drop_ptr_list(ctx, &pl);
  450. fz_free(ctx, new_name_data);
  451. }
  452. fz_catch(ctx)
  453. {
  454. fz_drop_buffer(ctx, t);
  455. fz_rethrow(ctx);
  456. }
  457. add_table(ctx, ttf, TAG("name"), t);
  458. }
  459. static encoding_t *
  460. load_enc_tab0(fz_context *ctx, uint8_t *d, size_t data_size, uint32_t offset)
  461. {
  462. encoding_t *enc;
  463. int i;
  464. if (data_size < 262)
  465. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated cmap 0 format table");
  466. enc = fz_malloc_struct(ctx, encoding_t);
  467. d += offset + 6;
  468. enc->max = 256;
  469. for (i = 0; i < 256; i++)
  470. enc->gid[i] = d[i];
  471. return enc;
  472. }
  473. static encoding_t *
  474. load_enc_tab4(fz_context *ctx, uint8_t *d, size_t data_size, uint32_t offset)
  475. {
  476. encoding_t *enc;
  477. uint16_t seg_count;
  478. uint32_t i;
  479. if (data_size < offset + 26)
  480. fz_throw(ctx, FZ_ERROR_FORMAT, "cmap4 too small");
  481. seg_count = get16(d+offset+6); /* 2 * seg_count */
  482. if (seg_count & 1)
  483. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed cmap4 table");
  484. seg_count >>= 1;
  485. enc = fz_calloc(ctx, 1, sizeof(encoding_t) + sizeof(uint16_t) * (65536 - 256));
  486. enc->max = 65536;
  487. fz_try(ctx)
  488. {
  489. /* Run through the segments, counting how many are used. */
  490. for (i = 0; i < seg_count; i++)
  491. {
  492. uint16_t seg_end, seg_start, delta, target, inner_offset;
  493. uint32_t offset_ptr, s;
  494. if (data_size < offset + 14 + 6 * seg_count + 2 + 2 * i + 2)
  495. fz_throw(ctx, FZ_ERROR_FORMAT, "cmap4 too small");
  496. seg_end = get16(d + offset + 14 + 2 * i);
  497. seg_start = get16(d + offset + 14 + 2 * seg_count + 2 + 2 * i);
  498. delta = get16(d + offset + 14 + 4 * seg_count + 2 + 2 * i);
  499. offset_ptr = offset + 14 + 6 * seg_count + 2 + 2 * i;
  500. inner_offset = get16(d + offset_ptr);
  501. if (seg_start >= enc->max || seg_end >= enc->max || seg_end < seg_start)
  502. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed cmap4 table.");
  503. for (s = seg_start; s <= seg_end; s++)
  504. {
  505. if (inner_offset == 0)
  506. {
  507. target = delta + s;
  508. }
  509. else
  510. {
  511. if (data_size < offset_ptr + inner_offset + 2 * (s - seg_start) + 2)
  512. fz_throw(ctx, FZ_ERROR_FORMAT, "cmap4 too small");
  513. /* Yes. This is very screwy. The inner_offset is from the offset_ptr in use. */
  514. target = get16(d + offset_ptr + inner_offset + 2 * (s - seg_start));
  515. if (target != 0)
  516. target += delta;
  517. }
  518. if (target != 0)
  519. enc->gid[s] = target;
  520. }
  521. }
  522. }
  523. fz_catch(ctx)
  524. {
  525. fz_free(ctx, enc);
  526. fz_rethrow(ctx);
  527. }
  528. return enc;
  529. }
  530. static encoding_t *
  531. load_enc_tab6(fz_context *ctx, uint8_t *d, size_t data_size, uint32_t offset)
  532. {
  533. encoding_t *enc;
  534. uint16_t first_code;
  535. uint16_t entry_count;
  536. uint16_t length;
  537. uint32_t i;
  538. if (data_size < 10)
  539. fz_throw(ctx, FZ_ERROR_FORMAT, "cmap6 too small");
  540. length = get16(d+offset+2);
  541. first_code = get16(d+offset+6);
  542. entry_count = get16(d+offset+8);
  543. if (length < entry_count*2 + 10)
  544. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed cmap6 table");
  545. enc = fz_calloc(ctx, 1, sizeof(encoding_t) + sizeof(uint16_t) * (first_code + entry_count - 256));
  546. enc->max = first_code + entry_count;
  547. /* Run through the segments, counting how many are used. */
  548. for (i = 0; i < entry_count; i++)
  549. {
  550. enc->gid[first_code+i] = get16(d+offset+10+i*2);
  551. }
  552. return enc;
  553. }
  554. static int
  555. is_encoding_all_zeros(fz_context *ctx, encoding_t *enc)
  556. {
  557. uint32_t i;
  558. if (enc != NULL)
  559. for (i = 0; i < enc->max; i++)
  560. if (enc->gid[i] != 0)
  561. return 0;
  562. return 1;
  563. }
  564. static encoding_t *
  565. load_enc(fz_context *ctx, fz_buffer *t, int pid, int psid)
  566. {
  567. uint8_t *d = t->data;
  568. size_t data_size = t->len;
  569. uint32_t i, n;
  570. if (data_size < 6 || get16(d) != 0)
  571. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported cmap table format");
  572. n = get16(d+2);
  573. if (data_size < 4 + 8*n)
  574. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated cmap table");
  575. for (i = 0; i < n; i++)
  576. {
  577. uint16_t plat_id = get16(d + 4 + i * 8);
  578. uint16_t plat_spec_id = get16(d + 4 + i * 8 + 2);
  579. uint32_t offset = get32(d + 4 + i * 8 + 4);
  580. uint16_t fmt;
  581. encoding_t *enc;
  582. if (plat_id != pid || plat_spec_id != psid)
  583. continue;
  584. if (offset < 4 + 8 * n || offset + 2 >= data_size)
  585. fz_throw(ctx, FZ_ERROR_FORMAT, "cmap table data out of range");
  586. fmt = get16(d+offset);
  587. switch(fmt)
  588. {
  589. case 0:
  590. enc = load_enc_tab0(ctx, d, data_size, offset);
  591. break;
  592. case 4:
  593. enc = load_enc_tab4(ctx, d, data_size, offset);
  594. break;
  595. case 6:
  596. enc = load_enc_tab6(ctx, d, data_size, offset);
  597. break;
  598. default:
  599. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported cmap table format %d", fmt);
  600. }
  601. enc->pid = pid;
  602. enc->psid = psid;
  603. if (is_encoding_all_zeros(ctx, enc))
  604. {
  605. // ignore any encoding that is all zeros
  606. fz_free(ctx, enc);
  607. enc = NULL;
  608. }
  609. return enc;
  610. }
  611. return NULL;
  612. }
  613. static void
  614. load_encoding(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  615. {
  616. fz_buffer *t = read_table(ctx, stm, TAG("cmap"), 1);
  617. encoding_t *enc = NULL;
  618. fz_var(enc);
  619. fz_try(ctx)
  620. {
  621. if (ttf->symbolic)
  622. {
  623. /* For symbolic fonts, we look for (3,0) as per PDF Spec, then (1,0). */
  624. enc = load_enc(ctx, t, 3, 0);
  625. if (!enc)
  626. enc = load_enc(ctx, t, 1, 0);
  627. }
  628. else
  629. {
  630. /* For non symbolic fonts, we look for (3,1) then (1,0), then (0,1), and finally (0,3). */
  631. enc = load_enc(ctx, t, 3, 1);
  632. if (!enc)
  633. enc = load_enc(ctx, t, 1, 0);
  634. if (!enc)
  635. enc = load_enc(ctx, t, 0, 1);
  636. if (!enc)
  637. enc = load_enc(ctx, t, 0, 3);
  638. }
  639. if (!enc)
  640. fz_throw(ctx, FZ_ERROR_FORMAT, "No suitable cmap table found");
  641. }
  642. fz_always(ctx)
  643. {
  644. fz_drop_buffer(ctx, t);
  645. }
  646. fz_catch(ctx)
  647. {
  648. fz_rethrow(ctx);
  649. }
  650. ttf->encoding = enc;
  651. }
  652. static void
  653. reduce_encoding(fz_context *ctx, ttf_t *ttf, int *gids, int num_gids)
  654. {
  655. int i;
  656. encoding_t *enc = ttf->encoding;
  657. int n = enc->max;
  658. for (i = 0; i < n; i++)
  659. {
  660. int gid = enc->gid[i];
  661. int lo, hi;
  662. if (gid == 0)
  663. continue;
  664. lo = 0;
  665. hi = num_gids;
  666. while (lo < hi)
  667. {
  668. int mid = (lo + hi)>>1;
  669. int g = gids[mid];
  670. if (g < gid)
  671. lo = mid+1;
  672. else if (g > gid)
  673. hi = mid;
  674. else
  675. goto found; /* Leave this one as is. */
  676. }
  677. /* Not found */
  678. enc->gid[i] = 0;
  679. found:
  680. {}
  681. }
  682. }
  683. static void
  684. make_cmap(fz_context *ctx, ttf_t *ttf)
  685. {
  686. uint32_t i;
  687. uint32_t len;
  688. uint32_t segs = 0;
  689. uint32_t seg, seg_start, seg_end;
  690. encoding_t *enc = ttf->encoding;
  691. uint32_t n = enc->max;
  692. uint32_t entries = 0;
  693. fz_buffer *buf;
  694. uint8_t *d;
  695. uint32_t offset;
  696. /* Make a type 4 table. */
  697. /* Count the number of segments. */
  698. for (i = 0; i < n; i++)
  699. {
  700. if (enc->gid[i] == 0)
  701. continue;
  702. seg_start = i;
  703. seg_end = i;
  704. for (i++; i<n; i++)
  705. {
  706. if (enc->gid[i] != 0)
  707. seg_end = i;
  708. else if (i - seg_end > 4)
  709. break;
  710. }
  711. entries += seg_end - seg_start + 1;
  712. segs++;
  713. }
  714. segs++; /* For the terminator */
  715. len = 12 + 14 + 2 + segs * 2 * 4 + entries * 2;
  716. buf = fz_new_buffer(ctx, len);
  717. d = buf->data;
  718. /* cmap header */
  719. put16(d, 0); /* version */
  720. put16(d+2, 1); /* num sub tables */
  721. put16(d+4, enc->pid);
  722. put16(d+6, enc->psid);
  723. put32(d+8, 12); /* offset */
  724. d += 12;
  725. put16(d, 4); /* Format */
  726. put16(d + 2, len-12); /* Length */
  727. put16(d + 4, 0); /* FIXME: Language */
  728. put16(d + 6, segs * 2);
  729. i = 0;
  730. while (1U<<(i+1) <= segs)
  731. i++;
  732. /* So 1<<i <= segs < 1<<(i+1) */
  733. put16(d + 8, 1<<(i+1)); /* searchRange */
  734. put16(d + 10, i); /* entrySelector */
  735. put16(d + 12, 2 * segs - (1<<(i+1))); /* rangeShift */
  736. put16(d + 14 + segs * 2, 0); /* reserved */
  737. /* Now output the segment data */
  738. entries = 14 + segs * 2 * 4 + 2; /* offset of where to put entries.*/
  739. seg = 0;
  740. for (i = 0; i < n; i++)
  741. {
  742. if (enc->gid[i] == 0)
  743. continue;
  744. seg_start = i;
  745. seg_end = i;
  746. offset = 14 + segs * 2 * 3 + 2 + seg * 2;
  747. put16(d + offset - segs * 2, 0); /* Delta - always 0 for now. */
  748. put16(d + offset, entries - offset); /* offset */
  749. /* Insert an entry */
  750. if (!ttf->is_otf && ttf->gid_renum && i < enc->max && enc->gid[i] < ttf->orig_num_glyphs)
  751. put16(d + entries, (ttf->is_otf || ttf->gid_renum == NULL) ? enc->gid[i] : ttf->gid_renum[enc->gid[i]]);
  752. else
  753. put16(d + entries, enc->gid[i]);
  754. entries += 2;
  755. for (i++; i < n; i++)
  756. {
  757. if (enc->gid[i] != 0)
  758. {
  759. /* Include i in the range, which means we need to add entries for
  760. * seg_end to i inclusive. */
  761. while (seg_end < i)
  762. {
  763. seg_end++;
  764. if (!ttf->is_otf && ttf->gid_renum && seg_end < enc->max && enc->gid[seg_end] < ttf->orig_num_glyphs)
  765. put16(d + entries, ttf->gid_renum[enc->gid[seg_end]]);
  766. else
  767. put16(d + entries, enc->gid[seg_end]);
  768. entries += 2;
  769. }
  770. }
  771. else if (i - seg_end > 4)
  772. break;
  773. }
  774. put16(d + 14 + segs * 2 + seg * 2 + 2, seg_start);
  775. put16(d + 14 + seg * 2, seg_end);
  776. seg++;
  777. }
  778. offset = 14 + segs * 2 * 3 + 2 + seg * 2;
  779. put16(d + 14 + segs * 2 + seg * 2 + 2, 0xffff);
  780. put16(d + 14 + seg * 2, 0xffff);
  781. put16(d + offset - segs * 2, 1); /* Delta */
  782. put16(d + offset, 0); /* offset */
  783. buf->len = entries + 12;
  784. assert(buf->len == buf->cap);
  785. add_table(ctx, ttf, TAG("cmap"), buf);
  786. }
  787. static void
  788. read_maxp(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  789. {
  790. fz_buffer *t = read_table(ctx, stm, TAG("maxp"), 1);
  791. if (t->len < 6)
  792. {
  793. fz_drop_buffer(ctx, t);
  794. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated maxp table");
  795. }
  796. ttf->orig_num_glyphs = get16(t->data+4);
  797. add_table(ctx, ttf, TAG("maxp"), t);
  798. ttf->maxp = t->data;
  799. }
  800. static void
  801. read_head(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  802. {
  803. uint32_t version;
  804. fz_buffer *t = read_table(ctx, stm, TAG("head"), 1);
  805. if (t->len < 54)
  806. {
  807. fz_drop_buffer(ctx, t);
  808. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated head table");
  809. }
  810. version = get32(t->data);
  811. if (version != 0x00010000)
  812. {
  813. fz_drop_buffer(ctx, t);
  814. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported head table version 0x%08x", version);
  815. }
  816. ttf->index_to_loc_formatp = t->data+50;
  817. ttf->index_to_loc_format = get16(ttf->index_to_loc_formatp);
  818. if (ttf->index_to_loc_format & ~1)
  819. {
  820. fz_drop_buffer(ctx, t);
  821. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported index_to_loc_format 0x%04x", ttf->index_to_loc_format);
  822. }
  823. add_table(ctx, ttf, TAG("head"), t);
  824. }
  825. static void
  826. read_loca(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  827. {
  828. fz_buffer *t;
  829. uint32_t len = (2<<ttf->index_to_loc_format) * (ttf->orig_num_glyphs+1);
  830. t = read_table(ctx, stm, TAG("loca"), 1);
  831. if (t->len < len)
  832. {
  833. fz_drop_buffer(ctx, t);
  834. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated loca table");
  835. }
  836. ttf->loca = t->data;
  837. ttf->loca_len = &t->len;
  838. add_table(ctx, ttf, TAG("loca"), t);
  839. }
  840. static void
  841. read_hhea(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  842. {
  843. uint32_t version;
  844. fz_buffer *t = read_table(ctx, stm, TAG("hhea"), 1);
  845. uint16_t i;
  846. if (t->len < 36)
  847. {
  848. fz_drop_buffer(ctx, t);
  849. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated hhea table");
  850. }
  851. version = get32(t->data);
  852. if (version != 0x00010000)
  853. {
  854. fz_drop_buffer(ctx, t);
  855. fz_throw(ctx, FZ_ERROR_FORMAT, "Unsupported hhea table version 0x%08x", version);
  856. }
  857. ttf->orig_num_long_hor_metrics = get16(t->data+34);
  858. if (ttf->orig_num_long_hor_metrics > ttf->orig_num_glyphs)
  859. {
  860. fz_drop_buffer(ctx, t);
  861. fz_throw(ctx, FZ_ERROR_FORMAT, "Overlong hhea table");
  862. }
  863. add_table(ctx, ttf, TAG("hhea"), t);
  864. /* Previously gids 0 to orig_num_long_hor_metrics-1 were described with
  865. * hor metrics, and the ones afterwards were fixed widths. Find where
  866. * that dividing line is in our new reduced set. */
  867. if (ttf->encoding && !ttf->is_otf && ttf->orig_num_long_hor_metrics > 0)
  868. {
  869. /* i = 0 is always kept long in subset_hmtx(). */
  870. ttf->new_num_long_hor_metrics = 1;
  871. for (i = ttf->orig_num_long_hor_metrics-1; i > 0; i--)
  872. if (ttf->gid_renum[i])
  873. {
  874. ttf->new_num_long_hor_metrics = ttf->gid_renum[i]+1;
  875. break;
  876. }
  877. put16(t->data+34, ttf->new_num_long_hor_metrics);
  878. }
  879. else
  880. {
  881. ttf->new_num_long_hor_metrics = ttf->orig_num_long_hor_metrics;
  882. }
  883. }
  884. static uint32_t
  885. get_loca(fz_context *ctx, ttf_t *ttf, uint32_t n)
  886. {
  887. if (ttf->index_to_loc_format == 0)
  888. {
  889. /* Short index - convert from words to bytes */
  890. return get16(ttf->loca + n*2) * 2;
  891. }
  892. else
  893. {
  894. /* Long index - in bytes already */
  895. return get32(ttf->loca + n*4);
  896. }
  897. }
  898. static void
  899. put_loca(fz_context *ctx, ttf_t *ttf, uint32_t n, uint32_t off)
  900. {
  901. if (ttf->index_to_loc_format == 0)
  902. {
  903. /* Short index - convert from bytes to words */
  904. assert((off & 1) == 0);
  905. put16(ttf->loca + n*2, off/2);
  906. }
  907. else
  908. {
  909. /* Long index - in bytes already */
  910. put32(ttf->loca + n*4, off);
  911. }
  912. }
  913. static void
  914. glyph_used(fz_context *ctx, ttf_t *ttf, fz_buffer *glyf, uint16_t i)
  915. {
  916. uint32_t offset, len;
  917. const uint8_t *data;
  918. uint16_t flags;
  919. if (i >= ttf->orig_num_glyphs)
  920. {
  921. fz_warn(ctx, "TTF subsetting; gid >= num_gids!");
  922. return;
  923. }
  924. if (ttf->gid_renum[i] != 0)
  925. return;
  926. ttf->gid_renum[i] = 1;
  927. /* If this glyf is composite, then we need to add any dependencies of it. */
  928. offset = get_loca(ctx, ttf, i);
  929. len = get_loca(ctx, ttf, i+1) - offset;
  930. if (len == 0)
  931. return;
  932. if (offset+2 > glyf->len)
  933. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  934. data = glyf->data + offset;
  935. if ((int16_t)get16(data) >= 0)
  936. return; /* Single glyph - no dependencies */
  937. data += 4 * 2 + 2;
  938. if (len < 4*2 + 2)
  939. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  940. len -= 4 * 2 + 2;
  941. do
  942. {
  943. uint16_t idx, skip;
  944. if (len < 4)
  945. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  946. flags = get16(data);
  947. idx = get16(data+2);
  948. glyph_used(ctx, ttf, glyf, idx);
  949. #define ARGS_1_AND_2_ARE_WORDS 1
  950. #define ARGS_ARE_XY_VALUES 2
  951. #define WE_HAVE_A_SCALE 8
  952. #define MORE_COMPONENTS 32
  953. #define WE_HAVE_AN_X_AND_Y_SCALE 64
  954. #define WE_HAVE_A_TWO_BY_TWO 128
  955. /* Skip the X and Y offsets */
  956. if (flags & ARGS_1_AND_2_ARE_WORDS)
  957. skip = 4 + 4;
  958. else
  959. skip = 4 + 2;
  960. /* Skip the transformation */
  961. switch (flags & (WE_HAVE_A_SCALE + WE_HAVE_AN_X_AND_Y_SCALE + WE_HAVE_A_TWO_BY_TWO))
  962. {
  963. case 0:
  964. /* No extra to skip */
  965. break;
  966. case WE_HAVE_A_SCALE:
  967. skip += 2;
  968. break;
  969. case WE_HAVE_AN_X_AND_Y_SCALE:
  970. skip += 4;
  971. break;
  972. case WE_HAVE_A_TWO_BY_TWO:
  973. skip += 8;
  974. break;
  975. }
  976. if (len < skip)
  977. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  978. data += skip;
  979. len -= skip;
  980. }
  981. while(flags & MORE_COMPONENTS);
  982. }
  983. static void
  984. renumber_composite(fz_context *ctx, ttf_t *ttf, uint8_t *data, uint32_t len)
  985. {
  986. uint16_t flags;
  987. uint16_t x;
  988. data += 4 * 2 + 2;
  989. if (len < 4*2 + 2)
  990. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  991. len -= 4 * 2 + 2;
  992. do
  993. {
  994. uint16_t skip;
  995. if (len < 4)
  996. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  997. flags = get16(data);
  998. x = get16(data+2);
  999. if (x >= ttf->orig_num_glyphs)
  1000. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  1001. put16(data+2, ttf->gid_renum[x]);
  1002. /* Skip the X and Y offsets */
  1003. if (flags & ARGS_1_AND_2_ARE_WORDS)
  1004. skip = 4 + 4;
  1005. else
  1006. skip = 4 + 2;
  1007. /* Skip the transformation */
  1008. switch (flags & (WE_HAVE_A_SCALE + WE_HAVE_AN_X_AND_Y_SCALE + WE_HAVE_A_TWO_BY_TWO))
  1009. {
  1010. case 0:
  1011. /* No extra to skip */
  1012. break;
  1013. case WE_HAVE_A_SCALE:
  1014. skip += 2;
  1015. break;
  1016. case WE_HAVE_AN_X_AND_Y_SCALE:
  1017. skip += 4;
  1018. break;
  1019. case WE_HAVE_A_TWO_BY_TWO:
  1020. skip += 8;
  1021. break;
  1022. }
  1023. if (len < skip)
  1024. fz_throw(ctx, FZ_ERROR_FORMAT, "Corrupt glyf data");
  1025. data += skip;
  1026. len -= skip;
  1027. }
  1028. while(flags & MORE_COMPONENTS);
  1029. }
  1030. static void
  1031. read_glyf(fz_context *ctx, ttf_t *ttf, fz_stream *stm, int *gids, int num_gids)
  1032. {
  1033. uint32_t len = get_loca(ctx, ttf, ttf->orig_num_glyphs);
  1034. fz_buffer *t = read_table(ctx, stm, TAG("glyf"), 1);
  1035. encoding_t *enc = ttf->encoding;
  1036. uint32_t last_loca, i, j, k;
  1037. uint32_t new_start, old_start, old_end, last_loca_ofs;
  1038. if (t->len < len)
  1039. {
  1040. fz_drop_buffer(ctx, t);
  1041. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated glyf table");
  1042. }
  1043. add_table(ctx, ttf, TAG("glyf"), t);
  1044. /* Now, make the renumber list for the glyphs. */
  1045. ttf->gid_renum = fz_calloc(ctx, ttf->orig_num_glyphs, sizeof(uint16_t));
  1046. /* Initially, we'll use it just as a usage list. 0 = unused, 1 used */
  1047. /* glyph 0 is always used. */
  1048. glyph_used(ctx, ttf, t, 0);
  1049. if (enc)
  1050. {
  1051. uint32_t n = enc->max;
  1052. /* If we have an encoding table, run through it, and keep anything needed from there. */
  1053. for (i = 0; i < n; i++)
  1054. if (enc->gid[i])
  1055. glyph_used(ctx, ttf, t, enc->gid[i]);
  1056. /* Now convert from a usage table to a renumbering table. */
  1057. if (ttf->orig_num_glyphs > 0)
  1058. {
  1059. ttf->gid_renum[0] = 0;
  1060. j = 1;
  1061. for (i = 1; i < ttf->orig_num_glyphs; i++)
  1062. if (ttf->gid_renum[i])
  1063. ttf->gid_renum[i] = j++;
  1064. ttf->new_num_glyphs = j;
  1065. }
  1066. else
  1067. {
  1068. ttf->new_num_glyphs = 0;
  1069. }
  1070. }
  1071. else
  1072. {
  1073. /* We're a cid font. The cids are gids. */
  1074. for (i = 0; i < (uint32_t)num_gids; i++)
  1075. glyph_used(ctx, ttf, t, gids[i]);
  1076. ttf->new_num_glyphs = ttf->orig_num_glyphs;
  1077. }
  1078. /* Now subset the glyf table. */
  1079. if (enc)
  1080. {
  1081. old_start = get_loca(ctx, ttf, 0);
  1082. if (old_start > t->len)
  1083. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad loca value");
  1084. old_end = get_loca(ctx, ttf, 1);
  1085. if (old_end > t->len || old_end < old_start)
  1086. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad loca value");
  1087. len = old_end - old_start;
  1088. new_start = 0;
  1089. put_loca(ctx, ttf, 0, new_start);
  1090. last_loca = 0;
  1091. last_loca_ofs = len;
  1092. for (i = 0; i < ttf->orig_num_glyphs; i++)
  1093. {
  1094. old_end = get_loca(ctx, ttf, i + 1);
  1095. if (old_end > t->len || old_end < old_start)
  1096. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad loca value");
  1097. len = old_end - old_start;
  1098. if (len > 0 && (i == 0 || ttf->gid_renum[i] != 0))
  1099. {
  1100. memmove(t->data + new_start, t->data + old_start, len);
  1101. if ((int16_t)get16(t->data + new_start) < 0)
  1102. renumber_composite(ctx, ttf, t->data + new_start, len);
  1103. for (k = last_loca + 1; k <= ttf->gid_renum[i]; k++)
  1104. put_loca(ctx, ttf, k, last_loca_ofs);
  1105. new_start += len;
  1106. last_loca = ttf->gid_renum[i];
  1107. last_loca_ofs = new_start;
  1108. }
  1109. old_start = old_end;
  1110. }
  1111. for (k = last_loca + 1; k <= ttf->new_num_glyphs; k++)
  1112. put_loca(ctx, ttf, k, last_loca_ofs);
  1113. }
  1114. else
  1115. {
  1116. new_start = 0;
  1117. old_start = get_loca(ctx, ttf, 0);
  1118. if (old_start > t->len)
  1119. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad loca value");
  1120. for (i = 0; i < ttf->orig_num_glyphs; i++)
  1121. {
  1122. old_end = get_loca(ctx, ttf, i + 1);
  1123. if (old_end > t->len || old_end < old_start)
  1124. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad loca value");
  1125. len = old_end - old_start;
  1126. if (len > 0 && ttf->gid_renum[i] != 0)
  1127. {
  1128. memmove(t->data + new_start, t->data + old_start, len);
  1129. put_loca(ctx, ttf, i, new_start);
  1130. new_start += len;
  1131. }
  1132. else
  1133. {
  1134. put_loca(ctx, ttf, i, new_start);
  1135. }
  1136. old_start = old_end;
  1137. }
  1138. put_loca(ctx, ttf, ttf->orig_num_glyphs, new_start);
  1139. }
  1140. *ttf->loca_len = (size_t) (ttf->new_num_glyphs + 1) * (2<<ttf->index_to_loc_format);
  1141. t->len = new_start;
  1142. }
  1143. static void
  1144. update_num_glyphs(fz_context *ctx, ttf_t *ttf)
  1145. {
  1146. put16(ttf->maxp + 4, ttf->new_num_glyphs);
  1147. }
  1148. static void
  1149. subset_hmtx(fz_context *ctx, ttf_t *ttf, fz_stream *stm)
  1150. {
  1151. fz_buffer *t = read_table(ctx, stm, TAG("hmtx"), 1);
  1152. uint16_t long_metrics, short_metrics, i, k;
  1153. uint8_t *s = t->data;
  1154. uint8_t *d = t->data;
  1155. int cidfont = (ttf->encoding == NULL);
  1156. long_metrics = ttf->orig_num_long_hor_metrics;
  1157. if (long_metrics > ttf->orig_num_glyphs)
  1158. long_metrics = ttf->orig_num_glyphs;
  1159. if (long_metrics > t->len / 4)
  1160. long_metrics = (uint16_t)(t->len / 4);
  1161. short_metrics = (uint16_t)((t->len - long_metrics * 4) / 2);
  1162. if (short_metrics > ttf->orig_num_glyphs - long_metrics)
  1163. short_metrics = ttf->orig_num_glyphs - long_metrics;
  1164. for (i = 0; i < long_metrics; i++)
  1165. {
  1166. if (i == 0 || ttf->is_otf || (i < ttf->orig_num_glyphs && ttf->gid_renum[i]))
  1167. {
  1168. put32(d, get32(s));
  1169. d += 4;
  1170. }
  1171. else if (cidfont)
  1172. {
  1173. put32(d, 0);
  1174. d += 4;
  1175. }
  1176. s += 4;
  1177. }
  1178. for (k = 0 ; k < short_metrics; k++, i++)
  1179. {
  1180. if (i == 0 || ttf->is_otf || (i < ttf->orig_num_glyphs && ttf->gid_renum[i]))
  1181. {
  1182. put16(d, get16(s));
  1183. d += 2;
  1184. }
  1185. else if (cidfont)
  1186. {
  1187. put16(d, 0);
  1188. d += 2;
  1189. }
  1190. s += 2;
  1191. }
  1192. t->len = (d - t->data);
  1193. add_table(ctx, ttf, TAG("hmtx"), t);
  1194. }
  1195. static void
  1196. shrink_loca_if_possible(fz_context *ctx, ttf_t *ttf)
  1197. {
  1198. uint32_t len;
  1199. uint16_t i, n;
  1200. uint8_t *loca;
  1201. if (ttf->index_to_loc_format == 0)
  1202. return; /* Can't shrink cos it's already shrunk! */
  1203. n = ttf->new_num_glyphs;
  1204. len = get_loca(ctx, ttf, n);
  1205. if (len >= 65536)
  1206. return; /* We can't shrink it, cos it's too big. */
  1207. loca = ttf->loca;
  1208. for (i = 0; i <= n; i++)
  1209. {
  1210. if (get32(loca + 4*i) & 1)
  1211. return; /* Can't shrink it, because an offset is not even */
  1212. }
  1213. for (i = 0; i <= n; i++)
  1214. {
  1215. put16(loca + 2*i, get32(loca + 4*i)/2);
  1216. }
  1217. *ttf->loca_len = 2*(n+1);
  1218. put16(ttf->index_to_loc_formatp, 0);
  1219. }
  1220. static struct { const char *charname; int idx; } macroman[] =
  1221. {
  1222. { ".notdef", 0},
  1223. { ".null", 1},
  1224. { "A", 36},
  1225. { "AE", 144},
  1226. { "Aacute", 201},
  1227. { "Acircumflex", 199},
  1228. { "Adieresis", 98},
  1229. { "Agrave", 173},
  1230. { "Aring", 99},
  1231. { "Atilde", 174},
  1232. { "B", 37},
  1233. { "C", 38},
  1234. { "Cacute", 253},
  1235. { "Ccaron", 255},
  1236. { "Ccedilla", 100},
  1237. { "D", 39},
  1238. { "Delta", 168},
  1239. { "E", 40},
  1240. { "Eacute", 101},
  1241. { "Ecircumflex", 200},
  1242. { "Edieresis", 202},
  1243. { "Egrave", 203},
  1244. { "Eth", 233},
  1245. { "F", 41},
  1246. { "G", 42},
  1247. { "Gbreve", 248},
  1248. { "H", 43},
  1249. { "I", 44},
  1250. { "Iacute", 204},
  1251. { "Icircumflex", 205},
  1252. { "Idieresis", 206},
  1253. { "Idotaccent", 250},
  1254. { "Igrave", 207},
  1255. { "J", 45},
  1256. { "K", 46},
  1257. { "L", 47},
  1258. { "Lslash", 226},
  1259. { "M", 48},
  1260. { "N", 49},
  1261. { "Ntilde", 102},
  1262. { "O", 50},
  1263. { "OE", 176},
  1264. { "Oacute", 208},
  1265. { "Ocircumflex", 209},
  1266. { "Odieresis", 103},
  1267. { "Ograve", 211},
  1268. { "Omega", 159},
  1269. { "Oslash", 145},
  1270. { "Otilde", 175},
  1271. { "P", 51},
  1272. { "Q", 52},
  1273. { "R", 53},
  1274. { "S", 54},
  1275. { "Scaron", 228},
  1276. { "Scedilla", 251},
  1277. { "T", 55},
  1278. { "Thorn", 237},
  1279. { "U", 56},
  1280. { "Uacute", 212},
  1281. { "Ucircumflex", 213},
  1282. { "Udieresis", 104},
  1283. { "Ugrave", 214},
  1284. { "V", 57},
  1285. { "W", 58},
  1286. { "X", 59},
  1287. { "Y", 60},
  1288. { "Yacute", 235},
  1289. { "Ydieresis", 187},
  1290. { "Z", 61},
  1291. { "Zcaron", 230},
  1292. { "a", 68},
  1293. { "aacute", 105},
  1294. { "acircumflex", 107},
  1295. { "acute", 141},
  1296. { "adieresis", 108},
  1297. { "ae", 160},
  1298. { "agrave", 106},
  1299. { "ampersand", 9},
  1300. { "apple", 210},
  1301. { "approxequal", 167},
  1302. { "aring", 110},
  1303. { "asciicircum", 65},
  1304. { "asciitilde", 97},
  1305. { "asterisk", 13},
  1306. { "at", 35},
  1307. { "atilde", 109},
  1308. { "b", 69},
  1309. { "backslash", 63},
  1310. { "bar", 95},
  1311. { "braceleft", 94},
  1312. { "braceright", 96},
  1313. { "bracketleft", 62},
  1314. { "bracketright", 64},
  1315. { "breve", 219},
  1316. { "brokenbar", 232},
  1317. { "bullet", 135},
  1318. { "c", 70},
  1319. { "cacute", 254},
  1320. { "caron", 225},
  1321. { "ccaron", 256},
  1322. { "ccedilla", 111},
  1323. { "cedilla", 222},
  1324. { "cent", 132},
  1325. { "circumflex", 216},
  1326. { "colon", 29},
  1327. { "comma", 15},
  1328. { "copyright", 139},
  1329. { "currency", 189},
  1330. { "d", 71},
  1331. { "dagger", 130},
  1332. { "daggerdbl", 194},
  1333. { "dcroat", 257},
  1334. { "degree", 131},
  1335. { "dieresis", 142},
  1336. { "divide", 184},
  1337. { "dollar", 7},
  1338. { "dotaccent", 220},
  1339. { "dotlessi", 215},
  1340. { "e", 72},
  1341. { "eacute", 112},
  1342. { "ecircumflex", 114},
  1343. { "edieresis", 115},
  1344. { "egrave", 113},
  1345. { "eight", 27},
  1346. { "ellipsis", 171},
  1347. { "emdash", 179},
  1348. { "endash", 178},
  1349. { "equal", 32},
  1350. { "eth", 234},
  1351. { "exclam", 4},
  1352. { "exclamdown", 163},
  1353. { "f", 73},
  1354. { "fi", 192},
  1355. { "five", 24},
  1356. { "fl", 193},
  1357. { "florin", 166},
  1358. { "four", 23},
  1359. { "fraction", 188},
  1360. { "franc", 247},
  1361. { "g", 74},
  1362. { "gbreve", 249},
  1363. { "germandbls", 137},
  1364. { "grave", 67},
  1365. { "greater", 33},
  1366. { "greaterequal", 149},
  1367. { "guillemotleft", 169},
  1368. { "guillemotright", 170},
  1369. { "guilsinglleft", 190},
  1370. { "guilsinglright", 191},
  1371. { "h", 75},
  1372. { "hungarumlaut", 223},
  1373. { "hyphen", 16},
  1374. { "i", 76},
  1375. { "iacute", 116},
  1376. { "icircumflex", 118},
  1377. { "idieresis", 119},
  1378. { "igrave", 117},
  1379. { "infinity", 146},
  1380. { "integral", 156},
  1381. { "j", 77},
  1382. { "k", 78},
  1383. { "l", 79},
  1384. { "less", 31},
  1385. { "lessequal", 148},
  1386. { "logicalnot", 164},
  1387. { "lozenge", 185},
  1388. { "lslash", 227},
  1389. { "m", 80},
  1390. { "macron", 218},
  1391. { "minus", 239},
  1392. { "mu", 151},
  1393. { "multiply", 240},
  1394. { "n", 81},
  1395. { "nine", 28},
  1396. { "nonbreakingspace", 172},
  1397. { "nonmarkingreturn", 2},
  1398. { "notequal", 143},
  1399. { "ntilde", 120},
  1400. { "numbersign", 6},
  1401. { "o", 82},
  1402. { "oacute", 121},
  1403. { "ocircumflex", 123},
  1404. { "odieresis", 124},
  1405. { "oe", 177},
  1406. { "ogonek", 224},
  1407. { "ograve", 122},
  1408. { "one", 20},
  1409. { "onehalf", 244},
  1410. { "onequarter", 245},
  1411. { "onesuperior", 241},
  1412. { "ordfeminine", 157},
  1413. { "ordmasculine", 158},
  1414. { "oslash", 161},
  1415. { "otilde", 125},
  1416. { "p", 83},
  1417. { "paragraph", 136},
  1418. { "parenleft", 11},
  1419. { "parenright", 12},
  1420. { "partialdiff", 152},
  1421. { "percent", 8},
  1422. { "period", 17},
  1423. { "periodcentered", 195},
  1424. { "perthousand", 198},
  1425. { "pi", 155},
  1426. { "plus", 14},
  1427. { "plusminus", 147},
  1428. { "product", 154},
  1429. { "q", 84},
  1430. { "question", 34},
  1431. { "questiondown", 162},
  1432. { "quotedbl", 5},
  1433. { "quotedblbase", 197},
  1434. { "quotedblleft", 180},
  1435. { "quotedblright", 181},
  1436. { "quoteleft", 182},
  1437. { "quoteright", 183},
  1438. { "quotesinglbase", 196},
  1439. { "quotesingle", 10},
  1440. { "r", 85},
  1441. { "radical", 165},
  1442. { "registered", 138},
  1443. { "ring", 221},
  1444. { "s", 86},
  1445. { "scaron", 229},
  1446. { "scedilla", 252},
  1447. { "section", 134},
  1448. { "semicolon", 30},
  1449. { "seven", 26},
  1450. { "six", 25},
  1451. { "slash", 18},
  1452. { "space", 3},
  1453. { "sterling", 133},
  1454. { "summation", 153},
  1455. { "t", 87},
  1456. { "thorn", 238},
  1457. { "three", 22},
  1458. { "threequarters", 246},
  1459. { "threesuperior", 243},
  1460. { "tilde", 217},
  1461. { "trademark", 140},
  1462. { "two", 21},
  1463. { "twosuperior", 242},
  1464. { "u", 88},
  1465. { "uacute", 126},
  1466. { "ucircumflex", 128},
  1467. { "udieresis", 129},
  1468. { "ugrave", 127},
  1469. { "underscore", 66},
  1470. { "v", 89},
  1471. { "w", 90},
  1472. { "x", 91},
  1473. { "y", 92},
  1474. { "yacute", 236},
  1475. { "ydieresis", 186},
  1476. { "yen", 150},
  1477. { "z", 93},
  1478. { "zcaron", 231},
  1479. { "zero", 19},
  1480. };
  1481. static int
  1482. find_macroman_string(const char *s)
  1483. {
  1484. int l, r, m;
  1485. int comparison;
  1486. l = 0;
  1487. r = nelem(macroman);
  1488. while (l <= r)
  1489. {
  1490. m = (l + r) >> 1;
  1491. comparison = strcmp(s, macroman[m].charname);
  1492. if (comparison < 0)
  1493. r = m - 1;
  1494. else if (comparison > 0)
  1495. l = m + 1;
  1496. else
  1497. return macroman[m].idx;
  1498. }
  1499. return -1;
  1500. }
  1501. static size_t
  1502. subset_post2(fz_context *ctx, ttf_t *ttf, uint8_t *d, size_t len, int *gids, int num_gids)
  1503. {
  1504. int i, n, new_glyphs, old_strings, new_strings;
  1505. int j;
  1506. fz_int2_heap heap = { 0 };
  1507. uint8_t *d0, *e, *p;
  1508. if (len < (size_t) 2 + 2 * ttf->orig_num_glyphs)
  1509. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated post table");
  1510. n = get16(d);
  1511. if ((uint32_t)n != ttf->orig_num_glyphs)
  1512. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed post table");
  1513. d0 = d;
  1514. d += 2; len -= 2;
  1515. e = d;
  1516. p = d;
  1517. /* Store all kept indexes. */
  1518. if (len < (size_t)n*2)
  1519. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed post table");
  1520. old_strings = 0;
  1521. new_strings = 0;
  1522. new_glyphs = 0;
  1523. j = 0;
  1524. len -= (size_t)n*2;
  1525. for (i = 0; i < n; i++)
  1526. {
  1527. uint16_t o = get16(d);
  1528. fz_int2 i2;
  1529. p += 2;
  1530. if (o >= 258)
  1531. old_strings++;
  1532. /* We're only keeping gids we want. */
  1533. /* Note we need to keep both the gids we were given by the caller, but also
  1534. * those required as composites (in gid_renum, if we have it). */
  1535. if (i != 0 && (j >= num_gids || gids[j] != i) && (ttf->gid_renum == NULL || ttf->gid_renum[i] == 0))
  1536. {
  1537. memmove(d, d + 2, (n - i - 1) * 2);
  1538. continue;
  1539. }
  1540. if (j < num_gids && gids[j] == i)
  1541. j++;
  1542. d += 2;
  1543. e += 2;
  1544. /* We want this gid. */
  1545. new_glyphs++;
  1546. /* 257 or smaller: same as in the basic order, keep it as such. */
  1547. if (o <= 257)
  1548. continue;
  1549. /* check if string is one of the macroman standard ones, and use its index if so. */
  1550. {
  1551. uint8_t *q = d0 + 2 + (size_t) n * 2;
  1552. int k;
  1553. char buf[257] = { 0 };
  1554. int macidx;
  1555. for (k = 0; k < o - 258; k++)
  1556. q += 1 + *q;
  1557. for (k = 0; k < *q; k++)
  1558. buf[k] = *(q + 1 + k);
  1559. macidx = find_macroman_string(buf);
  1560. if (macidx >= 0)
  1561. {
  1562. put16(d - 2, macidx);
  1563. continue;
  1564. }
  1565. }
  1566. /* We want this gid, and it is a string. */
  1567. new_strings++;
  1568. /* Store the index. */
  1569. i2.a = o - 258;
  1570. i2.b = i;
  1571. fz_int2_heap_insert(ctx, &heap, i2);
  1572. /* Update string index value in table entry. */
  1573. put16(d - 2, 257 + new_strings);
  1574. }
  1575. d = p;
  1576. /* Update number of indexes */
  1577. put16(d0, new_glyphs);
  1578. fz_int2_heap_sort(ctx, &heap);
  1579. /* So, the heap is sorted on i2.a (the string indexes we want to keep),
  1580. * and i2.b is the gid that refers to that index. */
  1581. /* Run through the list moving the strings down that we care about. */
  1582. j = 0;
  1583. n = old_strings;
  1584. for (i = 0; i < n; i++)
  1585. {
  1586. uint8_t slen;
  1587. if (len < 1)
  1588. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed post table");
  1589. slen = *d+1;
  1590. if (len < slen)
  1591. fz_throw(ctx, FZ_ERROR_FORMAT, "Malformed post table");
  1592. len -= slen;
  1593. if (j >= heap.len || heap.heap[j].a != i)
  1594. {
  1595. /* Drop this one. */
  1596. d += slen;
  1597. continue;
  1598. }
  1599. memmove(e, d, slen);
  1600. d += slen;
  1601. e += slen;
  1602. j++;
  1603. }
  1604. fz_free(ctx, heap.heap);
  1605. return e - d0;
  1606. }
  1607. static void
  1608. subset_post(fz_context *ctx, ttf_t *ttf, fz_stream *stm, int *gids, int num_gids)
  1609. {
  1610. fz_buffer *t = read_table(ctx, stm, TAG("post"), 0);
  1611. uint8_t *d;
  1612. size_t len;
  1613. uint32_t fmt;
  1614. if (t == NULL)
  1615. return;
  1616. d = t->data;
  1617. len = t->len;
  1618. if (len < 32)
  1619. {
  1620. fz_drop_buffer(ctx, t);
  1621. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated post table");
  1622. }
  1623. fmt = get32(d);
  1624. if (fmt != 0x00020000)
  1625. {
  1626. /* Fmt 1: Nothing to be gained by having this table. The cmap should
  1627. * have all the mappings anyway, and we'll have broken it by renumbering
  1628. * the gids down anyway. */
  1629. /* Fmt 2.5 deprecated. */
  1630. /* Fmt 3 and 4: should not be used for PDF. */
  1631. /* No other formats defined. */
  1632. fz_drop_buffer(ctx, t);
  1633. return;
  1634. }
  1635. d += 32; len -= 32;
  1636. fz_try(ctx)
  1637. len = subset_post2(ctx, ttf, d, len, gids, num_gids);
  1638. fz_catch(ctx)
  1639. {
  1640. fz_drop_buffer(ctx, t);
  1641. fz_rethrow(ctx);
  1642. }
  1643. t->len = 32 + len;
  1644. add_table(ctx, ttf, TAG("post"), t);
  1645. }
  1646. static void
  1647. subset_CFF(fz_context *ctx, ttf_t *ttf, fz_stream *stm, int *gids, int num_gids, int symbolic, int cidfont)
  1648. {
  1649. fz_buffer *t = read_table(ctx, stm, TAG("CFF "), 1);
  1650. fz_buffer *sub = NULL;
  1651. fz_var(sub);
  1652. fz_try(ctx)
  1653. sub = fz_subset_cff_for_gids(ctx, t, gids, num_gids, symbolic, cidfont);
  1654. fz_always(ctx)
  1655. fz_drop_buffer(ctx, t);
  1656. fz_catch(ctx)
  1657. fz_rethrow(ctx);
  1658. add_table(ctx, ttf, TAG("CFF "), sub);
  1659. }
  1660. fz_buffer *
  1661. fz_subset_ttf_for_gids(fz_context *ctx, fz_buffer *orig, int *gids, int num_gids, int symbolic, int cidfont)
  1662. {
  1663. fz_stream *stm = fz_open_buffer(ctx, orig);
  1664. ttf_t ttf = { 0 };
  1665. fz_buffer *newbuf = NULL;
  1666. fz_output *out = NULL;
  1667. fz_var(newbuf);
  1668. fz_var(out);
  1669. fz_try(ctx)
  1670. {
  1671. ttf.is_otf = (fz_read_uint32_le(ctx, stm) == 0x4f54544f);
  1672. ttf.symbolic = symbolic;
  1673. /* Subset the name table. No other dependencies. */
  1674. subset_name_table(ctx, &ttf, stm);
  1675. if (!cidfont)
  1676. {
  1677. /* Load the encoding. Populates the encoding table from the cmap table
  1678. * in the original. cmap table is then discarded. */
  1679. load_encoding(ctx, &ttf, stm);
  1680. /* Blank out the bits of the encoding we don't need. */
  1681. reduce_encoding(ctx, &ttf, gids, num_gids);
  1682. }
  1683. /* Read maxp and store the table. Remember orig_num_glyphs. */
  1684. read_maxp(ctx, &ttf, stm);
  1685. /* Read head and store the table. Remember the loca index size. */
  1686. read_head(ctx, &ttf, stm);
  1687. if (ttf.is_otf)
  1688. {
  1689. subset_CFF(ctx, &ttf, stm, gids, num_gids, symbolic, cidfont);
  1690. }
  1691. /* Read loca and store it. Stash a pointer to the table for quick access. */
  1692. if (!ttf.is_otf)
  1693. {
  1694. read_loca(ctx, &ttf, stm);
  1695. /* Read the glyf data, and scan it for composites. This makes the gid_renum table,
  1696. * subsets the glyf data, and rewrites the loca table. */
  1697. read_glyf(ctx, &ttf, stm, gids, num_gids);
  1698. }
  1699. /* Read hhea and store it. Remember numOfLongHorMetrics. */
  1700. read_hhea(ctx, &ttf, stm);
  1701. /* Read and subset hmtx. */
  1702. subset_hmtx(ctx, &ttf, stm);
  1703. #ifdef DEBUG_SUBSETTING
  1704. if (!cidfont)
  1705. {
  1706. encoding_t *enc = ttf.encoding;
  1707. uint32_t i, n = enc->max;
  1708. for (i = 0; i < n; i++)
  1709. if (enc->gid[i])
  1710. printf("cid %x '%c'-> orig gid %d -> gid %d\n", i, (char)i, enc->gid[i], ttf.gid_renum[enc->gid[i]]);
  1711. }
  1712. {
  1713. uint32_t i;
  1714. for (i = 0; i < ttf.orig_num_glyphs; i++)
  1715. if (ttf.gid_renum[i])
  1716. printf("gid %d -> %d\n", i, ttf.gid_renum[i]);
  1717. for (i = 0; i <= ttf.new_num_glyphs; i++)
  1718. printf("LOCA %d = %x\n", i, get_loca(ctx, &ttf, i));
  1719. }
  1720. #endif
  1721. if (!ttf.is_otf)
  1722. {
  1723. shrink_loca_if_possible(ctx, &ttf);
  1724. update_num_glyphs(ctx, &ttf);
  1725. }
  1726. if (!cidfont)
  1727. {
  1728. /* Now we can make the new cmap. */
  1729. make_cmap(ctx, &ttf);
  1730. }
  1731. if (!cidfont)
  1732. {
  1733. /* subset the post table */
  1734. subset_post(ctx, &ttf, stm, gids, num_gids);
  1735. }
  1736. copy_table(ctx, &ttf, stm, TAG("OS/2"), 0);
  1737. copy_table(ctx, &ttf, stm, TAG("cvt "), 0);
  1738. copy_table(ctx, &ttf, stm, TAG("fpgm"), 0);
  1739. copy_table(ctx, &ttf, stm, TAG("prep"), 0);
  1740. sort_tables(ctx, &ttf);
  1741. checksum_tables(ctx, &ttf);
  1742. newbuf = fz_new_buffer(ctx, 1024);
  1743. out = fz_new_output_with_buffer(ctx, newbuf);
  1744. write_tables(ctx, &ttf, out);
  1745. fz_close_output(ctx, out);
  1746. fix_checksum(ctx, newbuf);
  1747. }
  1748. fz_always(ctx)
  1749. {
  1750. int i;
  1751. fz_drop_output(ctx, out);
  1752. fz_drop_stream(ctx, stm);
  1753. for (i = 0; i < ttf.len; i++)
  1754. fz_drop_buffer(ctx, ttf.table[i].tab);
  1755. fz_free(ctx, ttf.table);
  1756. fz_free(ctx, ttf.gid_renum);
  1757. fz_free(ctx, ttf.encoding);
  1758. }
  1759. fz_catch(ctx)
  1760. {
  1761. fz_drop_buffer(ctx, newbuf);
  1762. fz_rethrow(ctx);
  1763. }
  1764. return newbuf;
  1765. }