pdf-font-add.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 "mupdf/pdf.h"
  24. #include <ft2build.h>
  25. #include FT_FREETYPE_H
  26. #ifdef FT_FONT_FORMATS_H
  27. #include FT_FONT_FORMATS_H
  28. #else
  29. #include FT_XFREE86_H
  30. #endif
  31. #include FT_TRUETYPE_TABLES_H
  32. #ifndef FT_SFNT_HEAD
  33. #define FT_SFNT_HEAD ft_sfnt_head
  34. #endif
  35. static int ft_font_file_kind(fz_context *ctx, FT_Face face)
  36. {
  37. const char *kind;
  38. fz_ft_lock(ctx);
  39. #ifdef FT_FONT_FORMATS_H
  40. kind = FT_Get_Font_Format(face);
  41. #else
  42. kind = FT_Get_X11_Font_Format(face);
  43. #endif
  44. fz_ft_unlock(ctx);
  45. if (!strcmp(kind, "TrueType")) return 2;
  46. if (!strcmp(kind, "Type 1")) return 1;
  47. if (!strcmp(kind, "CFF")) return 3;
  48. if (!strcmp(kind, "CID Type 1")) return 1;
  49. return 0;
  50. }
  51. static int is_ttc(fz_font *font)
  52. {
  53. if (!font || !font->buffer || font->buffer->len < 4)
  54. return 0;
  55. return !memcmp(font->buffer->data, "ttcf", 4);
  56. }
  57. static int is_truetype(fz_context *ctx, FT_Face face)
  58. {
  59. return ft_font_file_kind(ctx, face) == 2;
  60. }
  61. static int is_postscript(fz_context *ctx, FT_Face face)
  62. {
  63. int kind = ft_font_file_kind(ctx, face);
  64. return (kind == 1 || kind == 3);
  65. }
  66. static int is_builtin_font(fz_context *ctx, fz_font *font)
  67. {
  68. int size;
  69. unsigned char *data;
  70. if (!font->buffer)
  71. return 0;
  72. fz_buffer_storage(ctx, font->buffer, &data);
  73. return fz_lookup_base14_font(ctx, pdf_clean_font_name(font->name), &size) == data;
  74. }
  75. static pdf_obj*
  76. pdf_add_font_file(fz_context *ctx, pdf_document *doc, fz_font *font)
  77. {
  78. fz_buffer *buf = font->buffer;
  79. pdf_obj *obj = NULL;
  80. pdf_obj *ref = NULL;
  81. int drop_buf = 0;
  82. fz_var(obj);
  83. fz_var(ref);
  84. /* Check for substitute fonts */
  85. if (font->flags.ft_substitute)
  86. return NULL;
  87. if (is_ttc(font))
  88. {
  89. buf = NULL;
  90. drop_buf = 1;
  91. buf = fz_extract_ttf_from_ttc(ctx, font);
  92. }
  93. fz_try(ctx)
  94. {
  95. size_t len = fz_buffer_storage(ctx, buf, NULL);
  96. int is_opentype;
  97. obj = pdf_new_dict(ctx, doc, 3);
  98. pdf_dict_put_int(ctx, obj, PDF_NAME(Length1), (int)len);
  99. switch (ft_font_file_kind(ctx, font->ft_face))
  100. {
  101. case 1:
  102. /* TODO: these may not be the correct values, but I doubt it matters */
  103. pdf_dict_put_int(ctx, obj, PDF_NAME(Length2), len);
  104. pdf_dict_put_int(ctx, obj, PDF_NAME(Length3), 0);
  105. break;
  106. case 2:
  107. break;
  108. case 3:
  109. fz_ft_lock(ctx);
  110. is_opentype = !!FT_Get_Sfnt_Table(font->ft_face, FT_SFNT_HEAD);
  111. fz_ft_unlock(ctx);
  112. if (is_opentype)
  113. pdf_dict_put(ctx, obj, PDF_NAME(Subtype), PDF_NAME(OpenType));
  114. else
  115. pdf_dict_put(ctx, obj, PDF_NAME(Subtype), PDF_NAME(CIDFontType0C));
  116. break;
  117. }
  118. ref = pdf_add_object(ctx, doc, obj);
  119. pdf_update_stream(ctx, doc, ref, buf, 0);
  120. }
  121. fz_always(ctx)
  122. {
  123. pdf_drop_obj(ctx, obj);
  124. if (drop_buf)
  125. fz_drop_buffer(ctx, buf);
  126. }
  127. fz_catch(ctx)
  128. {
  129. pdf_drop_obj(ctx, ref);
  130. fz_rethrow(ctx);
  131. }
  132. return ref;
  133. }
  134. static void
  135. pdf_add_font_descriptor(fz_context *ctx, pdf_document *doc, pdf_obj *fobj, fz_font *font)
  136. {
  137. FT_Face face = font->ft_face;
  138. pdf_obj *fdobj = NULL;
  139. pdf_obj *fileref;
  140. fz_rect bbox;
  141. fdobj = pdf_new_dict(ctx, doc, 10);
  142. fz_try(ctx)
  143. {
  144. pdf_dict_put(ctx, fdobj, PDF_NAME(Type), PDF_NAME(FontDescriptor));
  145. pdf_dict_put_name(ctx, fdobj, PDF_NAME(FontName), font->name);
  146. bbox.x0 = font->bbox.x0 * 1000;
  147. bbox.y0 = font->bbox.y0 * 1000;
  148. bbox.x1 = font->bbox.x1 * 1000;
  149. bbox.y1 = font->bbox.y1 * 1000;
  150. pdf_dict_put_rect(ctx, fdobj, PDF_NAME(FontBBox), bbox);
  151. pdf_dict_put_int(ctx, fdobj, PDF_NAME(ItalicAngle), 0);
  152. pdf_dict_put_int(ctx, fdobj, PDF_NAME(Ascent), face->ascender * 1000.0f / face->units_per_EM);
  153. pdf_dict_put_int(ctx, fdobj, PDF_NAME(Descent), face->descender * 1000.0f / face->units_per_EM);
  154. pdf_dict_put_int(ctx, fdobj, PDF_NAME(StemV), 80);
  155. pdf_dict_put_int(ctx, fdobj, PDF_NAME(Flags), PDF_FD_NONSYMBOLIC);
  156. fileref = pdf_add_font_file(ctx, doc, font);
  157. if (fileref)
  158. {
  159. switch (ft_font_file_kind(ctx, face))
  160. {
  161. default:
  162. case 1: pdf_dict_put_drop(ctx, fdobj, PDF_NAME(FontFile), fileref); break;
  163. case 2: pdf_dict_put_drop(ctx, fdobj, PDF_NAME(FontFile2), fileref); break;
  164. case 3: pdf_dict_put_drop(ctx, fdobj, PDF_NAME(FontFile3), fileref); break;
  165. }
  166. }
  167. pdf_dict_put_drop(ctx, fobj, PDF_NAME(FontDescriptor), pdf_add_object(ctx, doc, fdobj));
  168. }
  169. fz_always(ctx)
  170. pdf_drop_obj(ctx, fdobj);
  171. fz_catch(ctx)
  172. fz_rethrow(ctx);
  173. }
  174. static void
  175. pdf_add_simple_font_widths(fz_context *ctx, pdf_document *doc, pdf_obj *fobj, fz_font *font, const char * const encoding[])
  176. {
  177. int width_table[256];
  178. pdf_obj *widths;
  179. int i, first, last;
  180. first = 0;
  181. last = 0;
  182. for (i = 0; i < 256; ++i)
  183. {
  184. int glyph = 0;
  185. if (encoding[i])
  186. {
  187. glyph = fz_encode_character_by_glyph_name(ctx, font, encoding[i]);
  188. }
  189. if (glyph > 0)
  190. {
  191. if (!first)
  192. first = i;
  193. last = i;
  194. width_table[i] = fz_advance_glyph(ctx, font, glyph, 0) * 1000;
  195. }
  196. else
  197. width_table[i] = 0;
  198. }
  199. widths = pdf_new_array(ctx, doc, last - first + 1);
  200. pdf_dict_put_drop(ctx, fobj, PDF_NAME(Widths), widths);
  201. for (i = first; i <= last; ++i)
  202. pdf_array_push_int(ctx, widths, width_table[i]);
  203. pdf_dict_put_int(ctx, fobj, PDF_NAME(FirstChar), first);
  204. pdf_dict_put_int(ctx, fobj, PDF_NAME(LastChar), last);
  205. }
  206. static void
  207. pdf_add_cid_system_info(fz_context *ctx, pdf_document *doc, pdf_obj *fobj, const char *reg, const char *ord, int supp)
  208. {
  209. pdf_obj *csi = pdf_dict_put_dict(ctx, fobj, PDF_NAME(CIDSystemInfo), 3);
  210. pdf_dict_put_string(ctx, csi, PDF_NAME(Registry), reg, strlen(reg));
  211. pdf_dict_put_string(ctx, csi, PDF_NAME(Ordering), ord, strlen(ord));
  212. pdf_dict_put_int(ctx, csi, PDF_NAME(Supplement), supp);
  213. }
  214. /* Different states of starting, same width as last, or consecutive glyph */
  215. enum { FW_START = 0, FW_SAME, FW_DIFFER };
  216. /* ToDo: Ignore the default sized characters */
  217. static void
  218. pdf_add_cid_font_widths(fz_context *ctx, pdf_document *doc, pdf_obj *fobj, fz_font *font)
  219. {
  220. FT_Face face = font->ft_face;
  221. pdf_obj *run_obj = NULL;
  222. pdf_obj *fw;
  223. int curr_code;
  224. int curr_size;
  225. int first_code;
  226. int state = FW_START;
  227. fz_var(run_obj);
  228. fw = pdf_add_new_array(ctx, doc, 10);
  229. fz_try(ctx)
  230. {
  231. curr_code = 0;
  232. curr_size = fz_advance_glyph(ctx, font, 0, 0) * 1000;
  233. first_code = 0;
  234. for (curr_code = 1; curr_code < face->num_glyphs; curr_code++)
  235. {
  236. int prev_size = curr_size;
  237. curr_size = fz_advance_glyph(ctx, font, curr_code, 0) * 1000;
  238. /* So each time around the loop when we reach here, we have sizes
  239. * for curr_code-1 (prev_size) and curr_code (curr_size), neither
  240. * of which have been published yet. By the time we reach the end
  241. * of the loop we must have disposed of prev_size. */
  242. switch (state)
  243. {
  244. case FW_SAME:
  245. /* Until now, we've been in a run of identical values, extending
  246. * from first_code to curr_code-1. If the current and prev sizes
  247. * match, then this now extends from first_code to curr_code and
  248. * we don't need to do anything. If not, we need to flush and
  249. * restart. */
  250. if (curr_size != prev_size)
  251. {
  252. /* Add three entries. First cid, last cid and width */
  253. pdf_array_push_int(ctx, fw, first_code);
  254. pdf_array_push_int(ctx, fw, curr_code-1);
  255. pdf_array_push_int(ctx, fw, prev_size);
  256. /* And the new first code is our current code. */
  257. first_code = curr_code;
  258. state = FW_START;
  259. }
  260. break;
  261. case FW_DIFFER:
  262. /* Until now, we've been in a run of differing values, extending
  263. * from first_code to curr_code-1 (though prev_size, the size for
  264. * curr_code-1 has not yet been pushed). */
  265. if (curr_size == prev_size)
  266. {
  267. /* Same width, so flush the run of differences. */
  268. pdf_array_push_int(ctx, fw, first_code);
  269. pdf_array_push(ctx, fw, run_obj);
  270. pdf_drop_obj(ctx, run_obj);
  271. run_obj = NULL;
  272. /* Start a new 'same' entry starting with curr_code-1.
  273. * i.e. the prev size is not put in the run. */
  274. state = FW_SAME;
  275. first_code = curr_code-1;
  276. }
  277. else
  278. {
  279. /* Continue our differing run by adding prev size to run_obj. */
  280. pdf_array_push_int(ctx, run_obj, prev_size);
  281. }
  282. break;
  283. case FW_START:
  284. /* Starting fresh. Determine our state. */
  285. if (curr_size == prev_size)
  286. {
  287. state = FW_SAME;
  288. }
  289. else
  290. {
  291. run_obj = pdf_new_array(ctx, doc, 10);
  292. pdf_array_push_int(ctx, run_obj, prev_size);
  293. state = FW_DIFFER;
  294. }
  295. break;
  296. }
  297. }
  298. /* So curr_code-1 is the last valid char, and curr_size was its size. */
  299. switch (state)
  300. {
  301. case FW_SAME:
  302. /* We have an unflushed run of same entries. */
  303. if (first_code != curr_code-1)
  304. {
  305. pdf_array_push_int(ctx, fw, first_code);
  306. pdf_array_push_int(ctx, fw, curr_code-1);
  307. pdf_array_push_int(ctx, fw, curr_size);
  308. }
  309. break;
  310. case FW_DIFFER:
  311. /* We have not yet pushed curr_size to the object. */
  312. pdf_array_push_int(ctx, fw, first_code);
  313. pdf_array_push_int(ctx, run_obj, curr_size);
  314. pdf_array_push(ctx, fw, run_obj);
  315. pdf_drop_obj(ctx, run_obj);
  316. run_obj = NULL;
  317. break;
  318. case FW_START:
  319. /* Lone wolf! */
  320. pdf_array_push_int(ctx, fw, curr_code-1);
  321. pdf_array_push_int(ctx, fw, curr_code-1);
  322. pdf_array_push_int(ctx, fw, curr_size);
  323. break;
  324. }
  325. if (font->width_table != NULL)
  326. pdf_dict_put_int(ctx, fobj, PDF_NAME(DW), font->width_default);
  327. if (pdf_array_len(ctx, fw) > 0)
  328. pdf_dict_put(ctx, fobj, PDF_NAME(W), fw);
  329. }
  330. fz_always(ctx)
  331. {
  332. pdf_drop_obj(ctx, fw);
  333. pdf_drop_obj(ctx, run_obj);
  334. }
  335. fz_catch(ctx)
  336. fz_rethrow(ctx);
  337. }
  338. /* Descendant font construction used for CID font creation from ttf or Adobe type1 */
  339. static pdf_obj*
  340. pdf_add_descendant_cid_font(fz_context *ctx, pdf_document *doc, fz_font *font)
  341. {
  342. FT_Face face = font->ft_face;
  343. pdf_obj *fobj, *fref;
  344. const char *ps_name;
  345. fobj = pdf_new_dict(ctx, doc, 3);
  346. fz_try(ctx)
  347. {
  348. pdf_dict_put(ctx, fobj, PDF_NAME(Type), PDF_NAME(Font));
  349. if (is_truetype(ctx, face))
  350. pdf_dict_put(ctx, fobj, PDF_NAME(Subtype), PDF_NAME(CIDFontType2));
  351. else
  352. pdf_dict_put(ctx, fobj, PDF_NAME(Subtype), PDF_NAME(CIDFontType0));
  353. pdf_add_cid_system_info(ctx, doc, fobj, "Adobe", "Identity", 0);
  354. fz_ft_lock(ctx);
  355. ps_name = FT_Get_Postscript_Name(face);
  356. fz_ft_unlock(ctx);
  357. if (ps_name)
  358. pdf_dict_put_name(ctx, fobj, PDF_NAME(BaseFont), ps_name);
  359. else
  360. pdf_dict_put_name(ctx, fobj, PDF_NAME(BaseFont), font->name);
  361. pdf_add_font_descriptor(ctx, doc, fobj, font);
  362. /* We may have a cid font already with width info in source font and no cmap in the ft face */
  363. pdf_add_cid_font_widths(ctx, doc, fobj, font);
  364. fref = pdf_add_object(ctx, doc, fobj);
  365. }
  366. fz_always(ctx)
  367. pdf_drop_obj(ctx, fobj);
  368. fz_catch(ctx)
  369. fz_rethrow(ctx);
  370. return fref;
  371. }
  372. static int next_range(int *table, int size, int k)
  373. {
  374. int n;
  375. for (n = 1; k + n < size; ++n)
  376. {
  377. if ((k & 0xFF00) != ((k+n) & 0xFF00)) /* high byte changes */
  378. break;
  379. if (table[k] + n != table[k+n])
  380. break;
  381. }
  382. return n;
  383. }
  384. /* Create the ToUnicode CMap. */
  385. static void
  386. pdf_add_to_unicode(fz_context *ctx, pdf_document *doc, pdf_obj *fobj, fz_font *font)
  387. {
  388. FT_Face face = font->ft_face;
  389. fz_buffer *buf = NULL;
  390. int *table;
  391. int num_seq = 0;
  392. int num_chr = 0;
  393. int n, k;
  394. /* Populate reverse cmap table */
  395. {
  396. FT_ULong ucs;
  397. FT_UInt gid;
  398. table = fz_calloc(ctx, face->num_glyphs, sizeof *table);
  399. fz_ft_lock(ctx);
  400. ucs = FT_Get_First_Char(face, &gid);
  401. while (gid > 0)
  402. {
  403. if (gid < (FT_ULong)face->num_glyphs && face->num_glyphs > 0)
  404. table[gid] = ucs;
  405. ucs = FT_Get_Next_Char(face, ucs, &gid);
  406. }
  407. fz_ft_unlock(ctx);
  408. }
  409. for (k = 0; k < face->num_glyphs; k += n)
  410. {
  411. n = next_range(table, face->num_glyphs, k);
  412. if (n > 1)
  413. ++num_seq;
  414. else if (table[k] > 0)
  415. ++num_chr;
  416. }
  417. /* No mappings available... */
  418. if (num_seq + num_chr == 0)
  419. {
  420. fz_warn(ctx, "cannot create ToUnicode mapping for %s", font->name);
  421. fz_free(ctx, table);
  422. return;
  423. }
  424. fz_var(buf);
  425. fz_try(ctx)
  426. {
  427. buf = fz_new_buffer(ctx, 0);
  428. /* Header boiler plate */
  429. fz_append_string(ctx, buf, "/CIDInit /ProcSet findresource begin\n");
  430. fz_append_string(ctx, buf, "12 dict begin\n");
  431. fz_append_string(ctx, buf, "begincmap\n");
  432. fz_append_string(ctx, buf, "/CIDSystemInfo <</Registry(Adobe)/Ordering(UCS)/Supplement 0>> def\n");
  433. fz_append_string(ctx, buf, "/CMapName /Adobe-Identity-UCS def\n");
  434. fz_append_string(ctx, buf, "/CMapType 2 def\n");
  435. fz_append_string(ctx, buf, "1 begincodespacerange\n");
  436. fz_append_string(ctx, buf, "<0000> <FFFF>\n");
  437. fz_append_string(ctx, buf, "endcodespacerange\n");
  438. /* Note to have a valid CMap, the number of entries in table set can
  439. * not exceed 100, so we have to break into multiple tables. Also, note
  440. * that to reduce the file size we should be looking for sequential
  441. * ranges. Per Adobe technical note #5411, we can't have a range
  442. * cross a boundary where the high order byte changes */
  443. /* First the ranges */
  444. if (num_seq > 0)
  445. {
  446. int count = 0;
  447. if (num_seq > 100)
  448. {
  449. fz_append_string(ctx, buf, "100 beginbfrange\n");
  450. num_seq -= 100;
  451. }
  452. else
  453. fz_append_printf(ctx, buf, "%d beginbfrange\n", num_seq);
  454. for (k = 0; k < face->num_glyphs; k += n)
  455. {
  456. n = next_range(table, face->num_glyphs, k);
  457. if (n > 1)
  458. {
  459. if (count == 100)
  460. {
  461. fz_append_string(ctx, buf, "endbfrange\n");
  462. if (num_seq > 100)
  463. {
  464. fz_append_string(ctx, buf, "100 beginbfrange\n");
  465. num_seq -= 100;
  466. }
  467. else
  468. fz_append_printf(ctx, buf, "%d beginbfrange\n", num_seq);
  469. count = 0;
  470. }
  471. fz_append_printf(ctx, buf, "<%04x> <%04x> <%04x>\n", k, k+n-1, table[k]);
  472. ++count;
  473. }
  474. }
  475. fz_append_string(ctx, buf, "endbfrange\n");
  476. }
  477. /* Then the singles */
  478. if (num_chr > 0)
  479. {
  480. int count = 0;
  481. if (num_chr > 100)
  482. {
  483. fz_append_string(ctx, buf, "100 beginbfchar\n");
  484. num_chr -= 100;
  485. }
  486. else
  487. fz_append_printf(ctx, buf, "%d beginbfchar\n", num_chr);
  488. for (k = 0; k < face->num_glyphs; k += n)
  489. {
  490. n = next_range(table, face->num_glyphs, k);
  491. if (n == 1 && table[k] > 0)
  492. {
  493. if (count == 100)
  494. {
  495. fz_append_string(ctx, buf, "endbfchar\n");
  496. if (num_chr > 100)
  497. {
  498. fz_append_string(ctx, buf, "100 beginbfchar\n");
  499. num_chr -= 100;
  500. }
  501. else
  502. fz_append_printf(ctx, buf, "%d beginbfchar\n", num_chr);
  503. count = 0;
  504. }
  505. fz_append_printf(ctx, buf, "<%04x> <%04x>\n", k, table[k]);
  506. ++count;
  507. }
  508. }
  509. fz_append_string(ctx, buf, "endbfchar\n");
  510. }
  511. /* Trailer boiler plate */
  512. fz_append_string(ctx, buf, "endcmap\n");
  513. fz_append_string(ctx, buf, "CMapName currentdict /CMap defineresource pop\n");
  514. fz_append_string(ctx, buf, "end\nend\n");
  515. pdf_dict_put_drop(ctx, fobj, PDF_NAME(ToUnicode), pdf_add_stream(ctx, doc, buf, NULL, 0));
  516. }
  517. fz_always(ctx)
  518. {
  519. fz_free(ctx, table);
  520. fz_drop_buffer(ctx, buf);
  521. }
  522. fz_catch(ctx)
  523. fz_rethrow(ctx);
  524. }
  525. pdf_obj *
  526. pdf_add_cid_font(fz_context *ctx, pdf_document *doc, fz_font *font)
  527. {
  528. pdf_obj *fobj = NULL;
  529. pdf_obj *fref = NULL;
  530. pdf_obj *dfonts = NULL;
  531. pdf_font_resource_key key;
  532. fref = pdf_find_font_resource(ctx, doc, PDF_CID_FONT_RESOURCE, 0, font, &key);
  533. if (fref)
  534. return fref;
  535. fobj = pdf_add_new_dict(ctx, doc, 10);
  536. fz_try(ctx)
  537. {
  538. pdf_dict_put(ctx, fobj, PDF_NAME(Type), PDF_NAME(Font));
  539. pdf_dict_put(ctx, fobj, PDF_NAME(Subtype), PDF_NAME(Type0));
  540. pdf_dict_put_name(ctx, fobj, PDF_NAME(BaseFont), font->name);
  541. pdf_dict_put(ctx, fobj, PDF_NAME(Encoding), PDF_NAME(Identity_H));
  542. pdf_add_to_unicode(ctx, doc, fobj, font);
  543. dfonts = pdf_dict_put_array(ctx, fobj, PDF_NAME(DescendantFonts), 1);
  544. pdf_array_push_drop(ctx, dfonts, pdf_add_descendant_cid_font(ctx, doc, font));
  545. fref = pdf_insert_font_resource(ctx, doc, &key, fobj);
  546. }
  547. fz_always(ctx)
  548. pdf_drop_obj(ctx, fobj);
  549. fz_catch(ctx)
  550. fz_rethrow(ctx);
  551. return fref;
  552. }
  553. /* Create simple (8-bit encoding) fonts */
  554. static void
  555. pdf_add_simple_font_encoding_imp(fz_context *ctx, pdf_document *doc, pdf_obj *font, const char *glyph_names[])
  556. {
  557. pdf_obj *enc, *diff;
  558. int i, last;
  559. enc = pdf_dict_put_dict(ctx, font, PDF_NAME(Encoding), 2);
  560. pdf_dict_put(ctx, enc, PDF_NAME(BaseEncoding), PDF_NAME(WinAnsiEncoding));
  561. diff = pdf_dict_put_array(ctx, enc, PDF_NAME(Differences), 129);
  562. last = 0;
  563. for (i = 128; i < 256; ++i)
  564. {
  565. const char *glyph = glyph_names[i];
  566. if (glyph)
  567. {
  568. if (last != i-1)
  569. pdf_array_push_int(ctx, diff, i);
  570. last = i;
  571. pdf_array_push_name(ctx, diff, glyph);
  572. }
  573. }
  574. }
  575. static void
  576. pdf_add_simple_font_encoding(fz_context *ctx, pdf_document *doc, pdf_obj *fobj, int encoding)
  577. {
  578. switch (encoding)
  579. {
  580. default:
  581. case PDF_SIMPLE_ENCODING_LATIN:
  582. pdf_dict_put(ctx, fobj, PDF_NAME(Encoding), PDF_NAME(WinAnsiEncoding));
  583. break;
  584. case PDF_SIMPLE_ENCODING_GREEK:
  585. pdf_add_simple_font_encoding_imp(ctx, doc, fobj, fz_glyph_name_from_iso8859_7);
  586. break;
  587. case PDF_SIMPLE_ENCODING_CYRILLIC:
  588. pdf_add_simple_font_encoding_imp(ctx, doc, fobj, fz_glyph_name_from_koi8u);
  589. break;
  590. }
  591. }
  592. pdf_obj *
  593. pdf_add_simple_font(fz_context *ctx, pdf_document *doc, fz_font *font, int encoding)
  594. {
  595. FT_Face face = font->ft_face;
  596. pdf_obj *fobj = NULL;
  597. pdf_obj *fref = NULL;
  598. const char **enc;
  599. pdf_font_resource_key key;
  600. fref = pdf_find_font_resource(ctx, doc, PDF_SIMPLE_FONT_RESOURCE, encoding, font, &key);
  601. if (fref)
  602. return fref;
  603. switch (encoding)
  604. {
  605. default:
  606. case PDF_SIMPLE_ENCODING_LATIN: enc = fz_glyph_name_from_windows_1252; break;
  607. case PDF_SIMPLE_ENCODING_GREEK: enc = fz_glyph_name_from_iso8859_7; break;
  608. case PDF_SIMPLE_ENCODING_CYRILLIC: enc = fz_glyph_name_from_koi8u; break;
  609. }
  610. fobj = pdf_add_new_dict(ctx, doc, 10);
  611. fz_try(ctx)
  612. {
  613. pdf_dict_put(ctx, fobj, PDF_NAME(Type), PDF_NAME(Font));
  614. if (is_truetype(ctx, face))
  615. pdf_dict_put(ctx, fobj, PDF_NAME(Subtype), PDF_NAME(TrueType));
  616. else
  617. pdf_dict_put(ctx, fobj, PDF_NAME(Subtype), PDF_NAME(Type1));
  618. if (!is_builtin_font(ctx, font))
  619. {
  620. const char *ps_name;
  621. fz_ft_lock(ctx);
  622. ps_name = FT_Get_Postscript_Name(face);
  623. fz_ft_unlock(ctx);
  624. if (!ps_name)
  625. ps_name = font->name;
  626. pdf_dict_put_name(ctx, fobj, PDF_NAME(BaseFont), ps_name);
  627. pdf_add_simple_font_encoding(ctx, doc, fobj, encoding);
  628. pdf_add_simple_font_widths(ctx, doc, fobj, font, enc);
  629. pdf_add_font_descriptor(ctx, doc, fobj, font);
  630. }
  631. else
  632. {
  633. pdf_dict_put_name(ctx, fobj, PDF_NAME(BaseFont), pdf_clean_font_name(font->name));
  634. pdf_add_simple_font_encoding(ctx, doc, fobj, encoding);
  635. if (encoding != PDF_SIMPLE_ENCODING_LATIN)
  636. pdf_add_simple_font_widths(ctx, doc, fobj, font, enc);
  637. }
  638. fref = pdf_insert_font_resource(ctx, doc, &key, fobj);
  639. }
  640. fz_always(ctx)
  641. {
  642. pdf_drop_obj(ctx, fobj);
  643. }
  644. fz_catch(ctx)
  645. fz_rethrow(ctx);
  646. return fref;
  647. }
  648. int
  649. pdf_font_writing_supported(fz_context *ctx, fz_font *font)
  650. {
  651. if (font->ft_face == NULL || font->buffer == NULL || font->buffer->len < 4 || !font->flags.embed || font->flags.never_embed)
  652. return 0;
  653. if (is_ttc(font))
  654. return 1;
  655. if (is_truetype(ctx, font->ft_face))
  656. return 1;
  657. if (is_postscript(ctx, font->ft_face))
  658. return 1;
  659. return 0;
  660. }
  661. pdf_obj *
  662. pdf_add_cjk_font(fz_context *ctx, pdf_document *doc, fz_font *fzfont, int script, int wmode, int serif)
  663. {
  664. pdf_obj *fref, *font, *subfont, *fontdesc;
  665. pdf_obj *dfonts;
  666. fz_rect bbox = { -200, -200, 1200, 1200 };
  667. pdf_font_resource_key key;
  668. int flags;
  669. const char *basefont, *encoding, *ordering;
  670. int supplement;
  671. switch (script)
  672. {
  673. default:
  674. script = FZ_ADOBE_CNS;
  675. /* fall through */
  676. case FZ_ADOBE_CNS: /* traditional chinese */
  677. basefont = serif ? "Ming" : "Fangti";
  678. encoding = wmode ? "UniCNS-UTF16-V" : "UniCNS-UTF16-H";
  679. ordering = "CNS1";
  680. supplement = 7;
  681. break;
  682. case FZ_ADOBE_GB: /* simplified chinese */
  683. basefont = serif ? "Song" : "Heiti";
  684. encoding = wmode ? "UniGB-UTF16-V" : "UniGB-UTF16-H";
  685. ordering = "GB1";
  686. supplement = 5;
  687. break;
  688. case FZ_ADOBE_JAPAN:
  689. basefont = serif ? "Mincho" : "Gothic";
  690. encoding = wmode ? "UniJIS-UTF16-V" : "UniJIS-UTF16-H";
  691. ordering = "Japan1";
  692. supplement = 6;
  693. break;
  694. case FZ_ADOBE_KOREA:
  695. basefont = serif ? "Batang" : "Dotum";
  696. encoding = wmode ? "UniKS-UTF16-V" : "UniKS-UTF16-H";
  697. ordering = "Korea1";
  698. supplement = 2;
  699. break;
  700. }
  701. flags = PDF_FD_SYMBOLIC;
  702. if (serif)
  703. flags |= PDF_FD_SERIF;
  704. fref = pdf_find_font_resource(ctx, doc, PDF_CJK_FONT_RESOURCE, script, fzfont, &key);
  705. if (fref)
  706. return fref;
  707. font = pdf_add_new_dict(ctx, doc, 5);
  708. fz_try(ctx)
  709. {
  710. pdf_dict_put(ctx, font, PDF_NAME(Type), PDF_NAME(Font));
  711. pdf_dict_put(ctx, font, PDF_NAME(Subtype), PDF_NAME(Type0));
  712. pdf_dict_put_name(ctx, font, PDF_NAME(BaseFont), basefont);
  713. pdf_dict_put_name(ctx, font, PDF_NAME(Encoding), encoding);
  714. dfonts = pdf_dict_put_array(ctx, font, PDF_NAME(DescendantFonts), 1);
  715. pdf_array_push_drop(ctx, dfonts, subfont = pdf_add_new_dict(ctx, doc, 5));
  716. {
  717. pdf_dict_put(ctx, subfont, PDF_NAME(Type), PDF_NAME(Font));
  718. pdf_dict_put(ctx, subfont, PDF_NAME(Subtype), PDF_NAME(CIDFontType0));
  719. pdf_dict_put_name(ctx, subfont, PDF_NAME(BaseFont), basefont);
  720. pdf_add_cid_system_info(ctx, doc, subfont, "Adobe", ordering, supplement);
  721. fontdesc = pdf_add_new_dict(ctx, doc, 8);
  722. pdf_dict_put_drop(ctx, subfont, PDF_NAME(FontDescriptor), fontdesc);
  723. {
  724. pdf_dict_put(ctx, fontdesc, PDF_NAME(Type), PDF_NAME(FontDescriptor));
  725. pdf_dict_put_text_string(ctx, fontdesc, PDF_NAME(FontName), basefont);
  726. pdf_dict_put_rect(ctx, fontdesc, PDF_NAME(FontBBox), bbox);
  727. pdf_dict_put_int(ctx, fontdesc, PDF_NAME(Flags), flags);
  728. pdf_dict_put_int(ctx, fontdesc, PDF_NAME(ItalicAngle), 0);
  729. pdf_dict_put_int(ctx, fontdesc, PDF_NAME(Ascent), 1000);
  730. pdf_dict_put_int(ctx, fontdesc, PDF_NAME(Descent), -200);
  731. pdf_dict_put_int(ctx, fontdesc, PDF_NAME(StemV), 80);
  732. }
  733. }
  734. fref = pdf_insert_font_resource(ctx, doc, &key, font);
  735. }
  736. fz_always(ctx)
  737. pdf_drop_obj(ctx, font);
  738. fz_catch(ctx)
  739. fz_rethrow(ctx);
  740. return fref;
  741. }
  742. pdf_obj *
  743. pdf_add_substitute_font(fz_context *ctx, pdf_document *doc, fz_font *font)
  744. {
  745. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "substitute font creation is not implemented yet");
  746. }