gdevpdfs.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /* Copyright (C) 1996, 2000, 2001 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevpdfs.c,v 1.7 2001/08/06 19:36:01 lpd Exp $ */
  16. /* Text string processing for PDF-writing driver. */
  17. #include "math_.h"
  18. #include "memory_.h"
  19. #include "string_.h"
  20. #include "gx.h"
  21. #include "gserrors.h"
  22. #include "gsmatrix.h"
  23. #include "gsutil.h" /* for bytes_compare */
  24. #include "gxfixed.h" /* for gxfcache.h */
  25. #include "gxfont.h"
  26. #include "gxfont0.h"
  27. #include "gxfont1.h"
  28. #include "gxfont42.h"
  29. #include "gxfcache.h" /* for orig_fonts list */
  30. #include "gxfcid.h"
  31. #include "gxfcmap.h"
  32. #include "gxpath.h" /* for getting current point */
  33. #include "gdevpsf.h"
  34. #include "gdevpdfx.h"
  35. #include "gdevpdff.h"
  36. #include "gdevpdfg.h"
  37. #include "gdevpdfo.h" /* only to mark CMap as written */
  38. #include "scommon.h"
  39. /*
  40. * Continue processing text. Per the check in pdf_text_begin, we know the
  41. * operation is not a charpath, but it could be anything else.
  42. */
  43. /*
  44. * Define quantities derived from the current font and CTM, used within
  45. * the text processing loop.
  46. */
  47. typedef struct pdf_text_process_state_s {
  48. float chars; /* scaled character spacing (Tc) */
  49. float words; /* scaled word spacing (Tw) */
  50. float size; /* font size for Tf */
  51. gs_matrix text_matrix; /* normalized FontMatrix * CTM for Tm */
  52. int mode; /* render mode (Tr) */
  53. gs_font *font;
  54. pdf_font_t *pdfont;
  55. } pdf_text_process_state_t;
  56. /*
  57. * Declare the procedures for processing different species of text.
  58. * These procedures may, but need not, copy pte->text into buf
  59. * (a caller-supplied buffer large enough to hold the string).
  60. */
  61. #define PROCESS_TEXT_PROC(proc)\
  62. int proc(P4(gs_text_enum_t *pte, const void *vdata, void *vbuf, uint size))
  63. private PROCESS_TEXT_PROC(process_plain_text);
  64. private PROCESS_TEXT_PROC(process_composite_text);
  65. private PROCESS_TEXT_PROC(process_cmap_text);
  66. private PROCESS_TEXT_PROC(process_cid_text);
  67. /* Other forward declarations */
  68. private int encoding_find_glyph(P3(gs_font_base *bfont, gs_glyph font_glyph,
  69. gs_encoding_index_t index));
  70. private int pdf_process_string(P6(pdf_text_enum_t *penum, gs_string *pstr,
  71. const gs_matrix *pfmat, bool encoded,
  72. pdf_text_process_state_t *pts, int *pindex));
  73. private int pdf_update_text_state(P3(pdf_text_process_state_t *ppts,
  74. const pdf_text_enum_t *penum,
  75. const gs_matrix *pfmat));
  76. private int pdf_encode_char(P4(gx_device_pdf *pdev, int chr,
  77. gs_font_base *bfont, pdf_font_t *ppf));
  78. private int pdf_encode_glyph(P5(gx_device_pdf *pdev, int chr, gs_glyph glyph,
  79. gs_font_base *bfont, pdf_font_t *ppf));
  80. private int pdf_write_text_process_state(P4(gx_device_pdf *pdev,
  81. const gs_text_enum_t *pte, /* for pdcolor, pis */
  82. const pdf_text_process_state_t *ppts,
  83. const gs_const_string *pstr));
  84. private int process_text_return_width(P7(const gs_text_enum_t *pte,
  85. gs_font *font, pdf_font_t *pdfont,
  86. const gs_matrix *pfmat,
  87. const gs_const_string *pstr,
  88. int *pindex, gs_point *pdpt));
  89. private int process_text_add_width(P7(gs_text_enum_t *pte,
  90. gs_font *font, const gs_matrix *pfmat,
  91. const pdf_text_process_state_t *ppts,
  92. const gs_const_string *pstr,
  93. int *pindex, gs_point *pdpt));
  94. /*
  95. * Continue processing text. This is the 'process' procedure in the text
  96. * enumerator.
  97. */
  98. private int
  99. pdf_default_text_begin(gs_text_enum_t *pte, const gs_text_params_t *text,
  100. gs_text_enum_t **ppte)
  101. {
  102. return gx_default_text_begin(pte->dev, pte->pis, text, pte->current_font,
  103. pte->path, pte->pdcolor, pte->pcpath,
  104. pte->memory, ppte);
  105. }
  106. int
  107. pdf_text_process(gs_text_enum_t *pte)
  108. {
  109. pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
  110. uint operation = pte->text.operation;
  111. const void *vdata;
  112. uint size = pte->text.size - pte->index;
  113. gs_text_enum_t *pte_default;
  114. PROCESS_TEXT_PROC((*process));
  115. int code = -1; /* to force default implementation */
  116. #define BUF_SIZE 100 /* arbitrary > 0 */
  117. /*
  118. * If we fell back to the default implementation, continue using it.
  119. */
  120. top:
  121. pte_default = penum->pte_default;
  122. if (pte_default) {
  123. code = gs_text_process(pte_default);
  124. gs_text_enum_copy_dynamic(pte, pte_default, true);
  125. if (code)
  126. return code;
  127. gs_text_release(pte_default, "pdf_text_process");
  128. penum->pte_default = 0;
  129. return 0;
  130. }
  131. {
  132. gs_font *font = pte->current_font;
  133. switch (font->FontType) {
  134. case ft_CID_encrypted:
  135. case ft_CID_TrueType:
  136. process = process_cid_text;
  137. break;
  138. case ft_encrypted:
  139. case ft_encrypted2:
  140. case ft_TrueType:
  141. /* The data may be either glyphs or characters. */
  142. process = process_plain_text;
  143. break;
  144. case ft_composite:
  145. process =
  146. (((gs_font_type0 *)font)->data.FMapType == fmap_CMap ?
  147. process_cmap_text :
  148. process_composite_text);
  149. break;
  150. default:
  151. goto skip;
  152. }
  153. }
  154. /*
  155. * We want to process the entire string in a single call, but we may
  156. * need to modify it. Copy it to a buffer. Note that it may consist
  157. * of bytes, gs_chars, or gs_glyphs.
  158. */
  159. if (operation & (TEXT_FROM_STRING | TEXT_FROM_BYTES))
  160. vdata = pte->text.data.bytes;
  161. else if (operation & TEXT_FROM_CHARS)
  162. vdata = pte->text.data.chars, size *= sizeof(gs_char);
  163. else if (operation & TEXT_FROM_SINGLE_CHAR)
  164. vdata = &pte->text.data.d_char, size = sizeof(gs_char);
  165. else if (operation & TEXT_FROM_GLYPHS)
  166. vdata = pte->text.data.glyphs, size *= sizeof(gs_glyph);
  167. else if (operation & TEXT_FROM_SINGLE_GLYPH)
  168. vdata = &pte->text.data.d_glyph, size = sizeof(gs_glyph);
  169. else
  170. goto skip;
  171. if (size <= BUF_SIZE) {
  172. /* Use a union to ensure alignment. */
  173. union bu_ {
  174. byte bytes[BUF_SIZE];
  175. gs_char chars[BUF_SIZE / sizeof(gs_char)];
  176. gs_glyph glyphs[BUF_SIZE / sizeof(gs_glyph)];
  177. } buf;
  178. code = process(pte, vdata, buf.bytes, size);
  179. } else {
  180. byte *buf = gs_alloc_string(pte->memory, size, "pdf_text_process");
  181. if (buf == 0)
  182. return_error(gs_error_VMerror);
  183. code = process(pte, vdata, buf, size);
  184. gs_free_string(pte->memory, buf, size, "pdf_text_process");
  185. }
  186. skip:
  187. if (code < 0) {
  188. /* Fall back to the default implementation. */
  189. code = pdf_default_text_begin(pte, &pte->text, &pte_default);
  190. if (code < 0)
  191. return code;
  192. penum->pte_default = pte_default;
  193. gs_text_enum_copy_dynamic(pte_default, pte, false);
  194. }
  195. /* The 'process' procedure might also have set pte_default itself. */
  196. if (penum->pte_default && !code)
  197. goto top;
  198. return code;
  199. }
  200. /*
  201. * Process a text string in an ordinary font.
  202. */
  203. private int
  204. process_plain_text(gs_text_enum_t *pte, const void *vdata, void *vbuf,
  205. uint size)
  206. {
  207. byte *const buf = vbuf;
  208. uint operation = pte->text.operation;
  209. pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
  210. int index = 0, code;
  211. gs_string str;
  212. bool encoded;
  213. pdf_text_process_state_t text_state;
  214. if (operation & (TEXT_FROM_STRING | TEXT_FROM_BYTES)) {
  215. memcpy(buf, (const byte *)vdata + pte->index, size);
  216. encoded = false;
  217. } else if (operation & (TEXT_FROM_CHARS | TEXT_FROM_SINGLE_CHAR)) {
  218. /* Check that all chars fit in a single byte. */
  219. const gs_char *const cdata = vdata;
  220. int i;
  221. size /= sizeof(gs_char);
  222. for (i = 0; i < size; ++i) {
  223. gs_char chr = cdata[pte->index + i];
  224. if (chr & ~0xff)
  225. return_error(gs_error_rangecheck);
  226. buf[i] = (byte)chr;
  227. }
  228. encoded = false;
  229. } else if (operation & (TEXT_FROM_GLYPHS | TEXT_FROM_SINGLE_GLYPH)) {
  230. /*
  231. * Reverse-map the glyphs into the encoding. Eventually, assign
  232. * them to empty slots if missing.
  233. */
  234. const gs_glyph *const gdata = vdata;
  235. gs_font *font = pte->current_font;
  236. int i;
  237. size /= sizeof(gs_glyph);
  238. code = pdf_update_text_state(&text_state, penum, &font->FontMatrix);
  239. if (code < 0)
  240. return code;
  241. for (i = 0; i < size; ++i) {
  242. gs_glyph glyph = gdata[pte->index + i];
  243. int chr = pdf_encode_glyph((gx_device_pdf *)pte->dev, -1, glyph,
  244. (gs_font_base *)font,
  245. text_state.pdfont);
  246. if (chr < 0)
  247. return_error(gs_error_rangecheck);
  248. buf[i] = (byte)chr;
  249. }
  250. encoded = true;
  251. } else
  252. return_error(gs_error_rangecheck);
  253. str.data = buf;
  254. if (size > 1 && (pte->text.operation & TEXT_INTERVENE)) {
  255. /* Just do one character. */
  256. str.size = 1;
  257. code = pdf_process_string(penum, &str, NULL, encoded, &text_state,
  258. &index);
  259. if (code >= 0) {
  260. pte->returned.current_char = buf[0];
  261. code = TEXT_PROCESS_INTERVENE;
  262. }
  263. } else {
  264. str.size = size;
  265. code = pdf_process_string(penum, &str, NULL, encoded, &text_state,
  266. &index);
  267. }
  268. pte->index += index;
  269. return code;
  270. }
  271. /*
  272. * Process a text string in a composite font with FMapType != 9 (CMap).
  273. */
  274. private int
  275. process_composite_text(gs_text_enum_t *pte, const void *vdata, void *vbuf,
  276. uint size)
  277. {
  278. byte *const buf = vbuf;
  279. pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
  280. int index = 0, code;
  281. gs_string str;
  282. pdf_text_process_state_t text_state;
  283. pdf_text_enum_t curr, prev;
  284. gs_point total_width;
  285. str.data = buf;
  286. if (pte->text.operation &
  287. (TEXT_FROM_ANY - (TEXT_FROM_STRING | TEXT_FROM_BYTES))
  288. )
  289. return_error(gs_error_rangecheck);
  290. if (pte->text.operation & TEXT_INTERVENE) {
  291. /* Not implemented. (PostScript doesn't even allow this case.) */
  292. return_error(gs_error_rangecheck);
  293. }
  294. total_width.x = total_width.y = 0;
  295. curr = *penum;
  296. prev = curr;
  297. prev.current_font = 0;
  298. /* Scan runs of characters in the same leaf font. */
  299. for ( ; ; ) {
  300. int font_code, buf_index;
  301. gs_font *new_font;
  302. for (buf_index = 0; ; ++buf_index) {
  303. gs_char chr;
  304. gs_glyph glyph;
  305. gs_text_enum_copy_dynamic((gs_text_enum_t *)&prev,
  306. (gs_text_enum_t *)&curr, false);
  307. font_code = pte->orig_font->procs.next_char_glyph
  308. ((gs_text_enum_t *)&curr, &chr, &glyph);
  309. /*
  310. * We check for a font change by comparing the current
  311. * font, rather than testing the return code, because
  312. * it makes the control structure a little simpler.
  313. */
  314. switch (font_code) {
  315. case 0: /* no font change */
  316. case 1: /* font change */
  317. new_font = curr.fstack.items[curr.fstack.depth].font;
  318. if (new_font != prev.current_font)
  319. break;
  320. if (chr != (byte)chr) /* probably can't happen */
  321. return_error(gs_error_rangecheck);
  322. buf[buf_index] = (byte)chr;
  323. continue;
  324. case 2: /* end of string */
  325. break;
  326. default: /* error */
  327. return font_code;
  328. }
  329. break;
  330. }
  331. str.size = buf_index;
  332. gs_text_enum_copy_dynamic((gs_text_enum_t *)&curr,
  333. (gs_text_enum_t *)&prev, false);
  334. if (buf_index) {
  335. /* buf_index == 0 is only possible the very first time. */
  336. /*
  337. * The FontMatrix of leaf descendant fonts is not updated
  338. * by scalefont. Compute the effective FontMatrix now.
  339. */
  340. const gs_matrix *psmat =
  341. &curr.fstack.items[curr.fstack.depth - 1].font->FontMatrix;
  342. gs_matrix fmat;
  343. gs_matrix_multiply(&curr.current_font->FontMatrix, psmat,
  344. &fmat);
  345. code = pdf_process_string(&curr, &str, &fmat, false, &text_state,
  346. &index);
  347. if (code < 0)
  348. return code;
  349. gs_text_enum_copy_dynamic(pte, (gs_text_enum_t *)&curr, true);
  350. if (pte->text.operation & TEXT_RETURN_WIDTH) {
  351. pte->returned.total_width.x = total_width.x +=
  352. curr.returned.total_width.x;
  353. pte->returned.total_width.y = total_width.y +=
  354. curr.returned.total_width.y;
  355. }
  356. }
  357. if (font_code == 2)
  358. break;
  359. curr.current_font = new_font;
  360. }
  361. return code;
  362. }
  363. /*
  364. * Process a text string in a composite font with FMapType == 9 (CMap).
  365. */
  366. private const char *const standard_cmap_names[] = {
  367. "Identity-H", "Identity-V",
  368. "GB-EUC-H", "GB-EUC-V",
  369. "GBpc-EUC-H", "GBpc-EUC-V",
  370. "GBK-EUC-H", "GBK-EUC-V",
  371. "UniGB-UCS2-H", "UniGB-UCS2-V",
  372. "B5pc-H", "B5pc-V",
  373. "ETen-B5-H", "ETen-B5-V",
  374. "ETenms-B5-H", "ETenms-B5-V",
  375. "CNS-EUC-H", "CNS-EUC-V",
  376. "UniCNS-UCS2-H", "UniCNS-UCS2-V",
  377. "83pv-RKSJ-H",
  378. "90ms-RKSJ-H", "90ms-RKSJ-V",
  379. "90msp-RKSJ-H", "90msp-RKSJ-V",
  380. "90pv-RKSJ-H",
  381. "Add-RKSJ-H", "Add-RKSJ-V",
  382. "EUC-H", "EUC-V",
  383. "Ext-RKSJ-H", "Ext-RKSJ-V",
  384. "H", "V",
  385. "UniJIS-UCS2-H", "UniJIS-UCS2-V",
  386. "UniJIS-UCS2-HW-H", "UniJIS-UCS2-HW-V",
  387. "KSC-EUC-H", "KSC-EUC-V",
  388. "KSCms-UHC-H", "KSCms-UHC-V",
  389. "KSCms-UHC-HW-H", "KSCms-UHC-HW-V",
  390. "KSCpc-EUC-H",
  391. "UniKS-UCS2-H", "UniKS-UCS2-V",
  392. 0
  393. };
  394. /* Mark a glyph and all its pieces as used. */
  395. private int
  396. mark_glyphs_used(gs_font *font, gs_glyph orig_glyph,
  397. const pdf_font_descriptor_t *pfd)
  398. {
  399. #define MAX_GLYPH_PIECES 10 /* arbitrary */
  400. gs_glyph glyphs[MAX_GLYPH_PIECES];
  401. int count = 1;
  402. glyphs[0] = orig_glyph;
  403. while (count > 0) {
  404. gs_glyph glyph = glyphs[--count];
  405. if (glyph >= gs_min_cid_glyph &&
  406. glyph - gs_min_cid_glyph < pfd->glyphs_count
  407. ) {
  408. int gid = glyph - gs_min_cid_glyph;
  409. int index = gid >> 3, mask = 0x80 >> (gid & 7);
  410. if (!(pfd->glyphs_used.data[index] & mask)) {
  411. uint piece_count = 1;
  412. uint max_pieces = MAX_GLYPH_PIECES - 1 - count;
  413. psf_add_subset_pieces(&glyphs[count], &piece_count,
  414. max_pieces, max_pieces, font);
  415. count += piece_count - 1;
  416. if (count > MAX_GLYPH_PIECES)
  417. return_error(gs_error_limitcheck);
  418. pfd->glyphs_used.data[index] |= mask;
  419. }
  420. }
  421. }
  422. #undef MAX_GLYPH_PIECES
  423. return 0;
  424. }
  425. /* Record widths and CID => GID mappings. */
  426. private int
  427. scan_cmap_text(gs_text_enum_t *pte, gs_font *font, pdf_font_t *psubf,
  428. gs_font *subfont)
  429. {
  430. pdf_font_descriptor_t *pfd = psubf->FontDescriptor;
  431. for ( ; ; ) {
  432. gs_char chr;
  433. gs_glyph glyph;
  434. int code = font->procs.next_char_glyph(pte, &chr, &glyph);
  435. if (code == 2) /* end of string */
  436. break;
  437. if (code < 0)
  438. return code;
  439. if (glyph >= gs_min_cid_glyph) {
  440. uint cid = glyph - gs_min_cid_glyph;
  441. if (cid < pfd->chars_count) {
  442. int index = cid >> 3, mask = 0x80 >> (cid & 7);
  443. if (!(pfd->chars_used.data[index] & mask)) {
  444. pfd->chars_used.data[index] |= mask;
  445. if (psubf->CIDToGIDMap) {
  446. gs_font_cid2 *const subfont2 =
  447. (gs_font_cid2 *)subfont;
  448. int gid =
  449. subfont2->cidata.CIDMap_proc(subfont2, glyph);
  450. if (gid >= 0) {
  451. psubf->CIDToGIDMap[cid] = gid;
  452. mark_glyphs_used(subfont, gid + gs_min_cid_glyph,
  453. pfd);
  454. }
  455. }
  456. }
  457. if (!(psubf->widths_known[index] & mask)) {
  458. int width;
  459. code = pdf_glyph_width(psubf, glyph, subfont, &width);
  460. if (code == 0) {
  461. psubf->Widths[cid] = width;
  462. psubf->widths_known[index] |= mask;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. return 0;
  469. }
  470. private int
  471. process_cmap_text(gs_text_enum_t *pte, const void *vdata, void *vbuf,
  472. uint size)
  473. {
  474. gx_device_pdf *const pdev = (gx_device_pdf *)pte->dev;
  475. pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
  476. gs_font *font = pte->current_font;
  477. gs_font_type0 *const pfont = (gs_font_type0 *)font;
  478. gs_font *subfont = pfont->data.FDepVector[0];
  479. const gs_cmap_t *pcmap = pfont->data.CMap;
  480. const char *const *pcmn = standard_cmap_names;
  481. pdf_resource_t *pcmres = 0;
  482. pdf_font_t *ppf;
  483. pdf_font_t *psubf;
  484. pdf_text_process_state_t text_state;
  485. int code;
  486. if (pte->text.operation &
  487. (TEXT_FROM_ANY - (TEXT_FROM_STRING | TEXT_FROM_BYTES))
  488. )
  489. return_error(gs_error_rangecheck);
  490. if (pte->text.operation & TEXT_INTERVENE) {
  491. /* Not implemented. (PostScript doesn't allow TEXT_INTERVENE.) */
  492. return_error(gs_error_rangecheck);
  493. }
  494. /* Require a single CID-keyed DescendantFont. */
  495. if (pfont->data.fdep_size != 1)
  496. return_error(gs_error_rangecheck);
  497. switch (subfont->FontType) {
  498. case ft_CID_encrypted:
  499. case ft_CID_TrueType:
  500. break;
  501. default:
  502. return_error(gs_error_rangecheck);
  503. }
  504. /*
  505. * If the CMap isn't standard, write it out if necessary.
  506. */
  507. for (; *pcmn != 0; ++pcmn)
  508. if (!bytes_compare((const byte *)*pcmn, strlen(*pcmn),
  509. pcmap->CMapName.data, pcmap->CMapName.size))
  510. break;
  511. if (*pcmn == 0) { /* not standard */
  512. pcmres = pdf_find_resource_by_gs_id(pdev, resourceCMap, pcmap->id);
  513. if (pcmres == 0) {
  514. /*
  515. * Create the CMap object, and write the stream.
  516. */
  517. pdf_data_writer_t writer;
  518. stream *s;
  519. code = pdf_begin_resource(pdev, resourceCMap, pcmap->id, &pcmres);
  520. if (code < 0)
  521. return code;
  522. s = pdev->strm;
  523. pprintd1(s, "/WMode %d/CMapName", pcmap->WMode);
  524. pdf_put_name(pdev, pcmap->CMapName.data, pcmap->CMapName.size);
  525. stream_puts(s, "/CIDSystemInfo");
  526. code = pdf_write_CMap_system_info(pdev, pcmap);
  527. if (code < 0)
  528. return code;
  529. code = pdf_begin_data_binary(pdev, &writer, false);
  530. if (code < 0)
  531. return code;
  532. code = psf_write_cmap(writer.binary.strm, pcmap,
  533. pdf_put_name_chars_proc(pdev), NULL);
  534. if (code < 0)
  535. return code;
  536. code = pdf_end_data(&writer);
  537. if (code < 0)
  538. return code;
  539. pcmres->object->written = true; /* don't try to write at end */
  540. }
  541. }
  542. /* Create pdf_font_t objects for the composite font and the CIDFont. */
  543. pte->current_font = subfont; /* CIDFont */
  544. {
  545. /* See process_composite_text re the following matrix computation. */
  546. gs_matrix fmat;
  547. gs_matrix_multiply(&subfont->FontMatrix, &font->FontMatrix, &fmat);
  548. code = pdf_update_text_state(&text_state, penum, &fmat);
  549. }
  550. pte->current_font = font; /* restore composite font */
  551. if (code < 0)
  552. return code;
  553. psubf = text_state.pdfont;
  554. /* Make sure we always write glyph 0. */
  555. psubf->FontDescriptor->chars_used.data[0] |= 0x80;
  556. if (psubf->FontDescriptor->glyphs_used.data)
  557. psubf->FontDescriptor->glyphs_used.data[0] |= 0x80;
  558. code = pdf_update_text_state(&text_state, penum, &font->FontMatrix);
  559. if (code < 0)
  560. return code;
  561. if (code > 0)
  562. return_error(gs_error_rangecheck);
  563. ppf = text_state.pdfont;
  564. ppf->DescendantFont = psubf;
  565. /* Copy the subfont name and type to the composite font. */
  566. ppf->fname = psubf->FontDescriptor->FontName;
  567. ppf->sub_font_type = subfont->FontType;
  568. if (pcmres) {
  569. sprintf(ppf->cmapname, "%ld 0 R", pdf_resource_id(pcmres));
  570. } else {
  571. strcpy(ppf->cmapname, "/");
  572. strcat(ppf->cmapname, *pcmn);
  573. }
  574. /*
  575. * Scan the string to mark CIDs as used, record widths, and
  576. * (for CIDFontType 2 fonts) record CID => GID mappings and mark
  577. * glyphs as used.
  578. */
  579. {
  580. gs_text_enum_t orig;
  581. gs_text_enum_copy_dynamic(&orig, pte, false);
  582. code = scan_cmap_text(pte, font, psubf, subfont);
  583. gs_text_enum_copy_dynamic(pte, &orig, false);
  584. }
  585. /*
  586. * Let the default implementation do the rest of the work, but don't
  587. * allow it to draw anything in the output; instead, append the
  588. * characters to the output now.
  589. */
  590. {
  591. gs_text_params_t text;
  592. gs_text_enum_t *pte_default;
  593. gs_const_string str;
  594. int acode;
  595. text = pte->text;
  596. if (text.operation & TEXT_DO_DRAW)
  597. text.operation =
  598. (text.operation - TEXT_DO_DRAW) | TEXT_DO_CHARWIDTH;
  599. code = pdf_default_text_begin(pte, &text, &pte_default);
  600. if (code < 0)
  601. return code;
  602. str.data = vdata;
  603. str.size = size;
  604. if ((acode = pdf_write_text_process_state(pdev, pte, &text_state, &str)) < 0 ||
  605. (acode = pdf_append_chars(pdev, str.data, str.size)) < 0
  606. ) {
  607. gs_text_release(pte_default, "process_cmap_text");
  608. return acode;
  609. }
  610. /* Let the caller (pdf_text_process) handle pte_default. */
  611. gs_text_enum_copy_dynamic(pte_default, pte, false);
  612. penum->pte_default = pte_default;
  613. return code;
  614. }
  615. }
  616. /*
  617. * Process a text string in a CIDFont. (Only glyphshow is supported.)
  618. */
  619. private int
  620. process_cid_text(gs_text_enum_t *pte, const void *vdata, void *vbuf,
  621. uint size)
  622. {
  623. uint operation = pte->text.operation;
  624. gs_text_enum_t save;
  625. gs_font *scaled_font = pte->current_font;
  626. gs_font *font; /* unscaled font */
  627. gx_device_pdf *pdev = (gx_device_pdf *)pte->dev;
  628. const gs_glyph *glyphs = (const gs_glyph *)vdata;
  629. gs_matrix scale_matrix;
  630. pdf_font_t *ppf;
  631. int code;
  632. if (!(operation & (TEXT_FROM_GLYPHS | TEXT_FROM_SINGLE_GLYPH)))
  633. return_error(gs_error_rangecheck);
  634. /*
  635. * PDF doesn't support glyphshow directly: we need to create a Type 0
  636. * font with an Identity CMap. Make sure all the glyph numbers fit
  637. * into 16 bits. (Eventually we should support wider glyphs too.)
  638. */
  639. {
  640. int i;
  641. byte *pchars = vbuf;
  642. for (i = 0; i * sizeof(gs_glyph) < size; ++i) {
  643. ulong gnum = glyphs[i] - gs_min_cid_glyph;
  644. if (gnum & ~0xffffL)
  645. return_error(gs_error_rangecheck);
  646. *pchars++ = (byte)(gnum >> 8);
  647. *pchars++ = (byte)gnum;
  648. }
  649. }
  650. /* Find the original (unscaled) version of this font. */
  651. for (font = scaled_font; font->base != font; )
  652. font = font->base;
  653. /* Compute the scaling matrix. */
  654. gs_matrix_invert(&font->FontMatrix, &scale_matrix);
  655. gs_matrix_multiply(&scale_matrix, &scaled_font->FontMatrix, &scale_matrix);
  656. /* Find or create the CIDFont resource. */
  657. ppf = (pdf_font_t *)
  658. pdf_find_resource_by_gs_id(pdev, resourceCIDFont, font->id);
  659. /* Check for the possibility that the base font has been freed. */
  660. if (ppf && ppf->FontDescriptor->written)
  661. ppf = 0;
  662. if (ppf == 0 || ppf->font == 0) {
  663. code = pdf_create_pdf_font(pdev, font, &font->FontMatrix, &ppf);
  664. if (code < 0)
  665. return code;
  666. }
  667. /* Create the CMap and Type 0 font if they don't exist already. */
  668. if (!ppf->glyphshow_font) {
  669. gs_memory_t *mem = font->memory;
  670. gs_font_type0 *font0 = (gs_font_type0 *)
  671. gs_font_alloc(mem, &st_gs_font_type0,
  672. &gs_font_procs_default, NULL, "process_cid_text");
  673. /* We allocate Encoding dynamically only for the sake of the GC. */
  674. uint *encoding = (uint *)
  675. gs_alloc_bytes(mem, sizeof(uint), "process_cid_text");
  676. gs_font **fdep = gs_alloc_struct_array(mem, 1, gs_font *,
  677. &st_gs_font_ptr_element,
  678. "process_cid_text");
  679. gs_cmap_t *pcmap;
  680. pdf_font_t *pgsf;
  681. if (font0 == 0 || encoding == 0 || fdep == 0)
  682. return_error(gs_error_VMerror);
  683. code = gs_cmap_create_identity(&pcmap, 2, font->WMode, mem);
  684. if (code < 0)
  685. return code;
  686. font0->FontMatrix = scale_matrix;
  687. font0->FontType = ft_composite;
  688. font0->procs.init_fstack = gs_type0_init_fstack;
  689. font0->procs.define_font = 0; /* not called */
  690. font0->procs.make_font = 0; /* not called */
  691. font0->procs.next_char_glyph = gs_type0_next_char_glyph;
  692. font0->key_name = font->key_name;
  693. font0->font_name = font->font_name;
  694. font0->data.FMapType = fmap_CMap;
  695. encoding[0] = 0;
  696. font0->data.Encoding = encoding;
  697. font0->data.encoding_size = 1;
  698. fdep[0] = font;
  699. font0->data.FDepVector = fdep;
  700. font0->data.fdep_size = 1;
  701. font0->data.CMap = pcmap;
  702. code = pdf_create_pdf_font(pdev, (gs_font *)font0, &font0->FontMatrix,
  703. &pgsf);
  704. if (code < 0)
  705. return code;
  706. pgsf->DescendantFont = ppf;
  707. ppf->glyphshow_font = pgsf;
  708. }
  709. /* Now handle the glyphshow as a show in the Type 0 font. */
  710. save = *pte;
  711. pte->current_font = ppf->glyphshow_font->font;
  712. /* Patch the operation temporarily for init_fstack. */
  713. pte->text.operation = (operation & ~TEXT_FROM_ANY) | TEXT_FROM_BYTES;
  714. /* Patch the data for process_cmap_text. */
  715. pte->text.data.bytes = vbuf;
  716. pte->text.size = size / sizeof(gs_glyph) * 2;
  717. pte->index = 0;
  718. gs_type0_init_fstack(pte, pte->current_font);
  719. code = process_cmap_text(pte, vbuf, vbuf, pte->text.size);
  720. pte->current_font = font;
  721. pte->text = save.text;
  722. pte->index = save.index + pte->index / 2;
  723. pte->fstack = save.fstack;
  724. return code;
  725. }
  726. /*
  727. * Internal procedure to process a string in a non-composite font.
  728. * Doesn't use or set pte->{data,size,index}; may use/set pte->xy_index;
  729. * may set pte->returned.total_width.
  730. */
  731. private int
  732. pdf_process_string(pdf_text_enum_t *penum, gs_string *pstr,
  733. const gs_matrix *pfmat, bool encoded,
  734. pdf_text_process_state_t *pts, int *pindex)
  735. {
  736. gs_text_enum_t *const pte = (gs_text_enum_t *)penum;
  737. gx_device_pdf *const pdev = (gx_device_pdf *)penum->dev;
  738. gs_font *font;
  739. const gs_text_params_t *text = &pte->text;
  740. int i;
  741. int code = 0, mask;
  742. font = penum->current_font;
  743. if (pfmat == 0)
  744. pfmat = &font->FontMatrix;
  745. if (text->operation & TEXT_RETURN_WIDTH)
  746. gx_path_current_point(pte->path, &penum->origin);
  747. switch (font->FontType) {
  748. case ft_TrueType:
  749. case ft_encrypted:
  750. case ft_encrypted2:
  751. break;
  752. default:
  753. return_error(gs_error_rangecheck);
  754. }
  755. code = pdf_update_text_state(pts, penum, pfmat);
  756. if (code > 0) {
  757. /* Try not to emulate ADD_TO_WIDTH if we don't have to. */
  758. if (code & TEXT_ADD_TO_SPACE_WIDTH) {
  759. if (!memchr(pstr->data, pte->text.space.s_char, pstr->size))
  760. code &= ~TEXT_ADD_TO_SPACE_WIDTH;
  761. }
  762. }
  763. if (code < 0)
  764. return code;
  765. mask = code;
  766. /* Check that all characters can be encoded. */
  767. for (i = 0; i < pstr->size; ++i) {
  768. int chr = pstr->data[i];
  769. pdf_font_t *pdfont = pts->pdfont;
  770. int code =
  771. (encoded ? chr :
  772. pdf_encode_char(pdev, chr, (gs_font_base *)font, pdfont));
  773. if (code < 0)
  774. return code;
  775. /*
  776. * For incrementally loaded fonts, expand FirstChar..LastChar
  777. * if needed.
  778. */
  779. if (code < pdfont->FirstChar)
  780. pdfont->FirstChar = code;
  781. if (code > pdfont->LastChar)
  782. pdfont->LastChar = code;
  783. pstr->data[i] = (byte)code;
  784. }
  785. /* Bring the text-related parameters in the output up to date. */
  786. code = pdf_write_text_process_state(pdev, pte, pts,
  787. (gs_const_string *)pstr);
  788. if (code < 0)
  789. return code;
  790. if (text->operation & TEXT_REPLACE_WIDTHS) {
  791. gs_point w;
  792. gs_matrix tmat;
  793. w.x = w.y = 0;
  794. tmat = pts->text_matrix;
  795. for (i = 0; i < pstr->size; *pindex = ++i, pte->xy_index++) {
  796. gs_point d, dpt;
  797. if (text->operation & TEXT_DO_DRAW) {
  798. code = pdf_append_chars(pdev, pstr->data + i, 1);
  799. if (code < 0)
  800. return code;
  801. }
  802. gs_text_replaced_width(&pte->text, pte->xy_index, &d);
  803. w.x += d.x, w.y += d.y;
  804. gs_distance_transform(d.x, d.y, &ctm_only(pte->pis), &dpt);
  805. tmat.tx += dpt.x;
  806. tmat.ty += dpt.y;
  807. if (i + 1 < pstr->size) {
  808. code = pdf_set_text_matrix(pdev, &tmat);
  809. if (code < 0)
  810. return code;
  811. }
  812. }
  813. pte->returned.total_width = w;
  814. if (text->operation & TEXT_RETURN_WIDTH)
  815. code = gx_path_add_point(pte->path, float2fixed(tmat.tx),
  816. float2fixed(tmat.ty));
  817. return code;
  818. }
  819. /* Write out the characters, unless we have to emulate ADD_TO_WIDTH. */
  820. if (mask == 0 && (text->operation & TEXT_DO_DRAW)) {
  821. code = pdf_append_chars(pdev, pstr->data, pstr->size);
  822. if (code < 0)
  823. return code;
  824. }
  825. /*
  826. * If we don't need the total width, and don't have to emulate
  827. * ADD_TO_WIDTH, return now. If the widths are available directly from
  828. * the font, place the characters and/or compute and return the total
  829. * width now. Otherwise, call the default implementation.
  830. */
  831. {
  832. gs_point dpt;
  833. if (mask) {
  834. /*
  835. * Cancel the word and character spacing, since we're going
  836. * to emulate them.
  837. */
  838. pts->words = pts->chars = 0;
  839. code = pdf_write_text_process_state(pdev, pte, pts,
  840. (gs_const_string *)pstr);
  841. if (code < 0)
  842. return code;
  843. code = process_text_add_width(pte, font, pfmat, pts,
  844. (gs_const_string *)pstr,
  845. pindex, &dpt);
  846. if (code < 0)
  847. return code;
  848. if (!(text->operation & TEXT_RETURN_WIDTH))
  849. return 0;
  850. } else if (!(text->operation & TEXT_RETURN_WIDTH))
  851. return 0;
  852. else {
  853. code = process_text_return_width(pte, font, pts->pdfont, pfmat,
  854. (gs_const_string *)pstr,
  855. pindex, &dpt);
  856. if (code < 0)
  857. return code;
  858. }
  859. pte->returned.total_width = dpt;
  860. gs_distance_transform(dpt.x, dpt.y, &ctm_only(pte->pis), &dpt);
  861. return gx_path_add_point(pte->path,
  862. penum->origin.x + float2fixed(dpt.x),
  863. penum->origin.y + float2fixed(dpt.y));
  864. }
  865. }
  866. /*
  867. * Compute the cached values in the text processing state from the text
  868. * parameters, current_font, and pis->ctm. Return either an error code (<
  869. * 0) or a mask of operation attributes that the caller must emulate.
  870. * Currently the only such attributes are TEXT_ADD_TO_ALL_WIDTHS and
  871. * TEXT_ADD_TO_SPACE_WIDTH.
  872. */
  873. private int
  874. transform_delta_inverse(const gs_point *pdelta, const gs_matrix *pmat,
  875. gs_point *ppt)
  876. {
  877. int code = gs_distance_transform_inverse(pdelta->x, pdelta->y, pmat, ppt);
  878. gs_point delta;
  879. if (code < 0)
  880. return code;
  881. if (ppt->y == 0)
  882. return 0;
  883. /* Check for numerical fuzz. */
  884. code = gs_distance_transform(ppt->x, 0.0, pmat, &delta);
  885. if (code < 0)
  886. return 0; /* punt */
  887. if (fabs(delta.x - pdelta->x) < 0.01 && fabs(delta.y - pdelta->y) < 0.01) {
  888. /* Close enough to y == 0: device space error < 0.01 pixel. */
  889. ppt->y = 0;
  890. }
  891. return 0;
  892. }
  893. private int
  894. pdf_update_text_state(pdf_text_process_state_t *ppts,
  895. const pdf_text_enum_t *penum, const gs_matrix *pfmat)
  896. {
  897. gx_device_pdf *const pdev = (gx_device_pdf *)penum->dev;
  898. gs_font *font = penum->current_font;
  899. pdf_font_t *ppf;
  900. gs_fixed_point cpt;
  901. gs_matrix orig_matrix, smat, tmat;
  902. double
  903. sx = pdev->HWResolution[0] / 72.0,
  904. sy = pdev->HWResolution[1] / 72.0;
  905. float chars = 0, words = 0, size;
  906. int mask = 0;
  907. int code = gx_path_current_point(penum->path, &cpt);
  908. if (code < 0)
  909. return code;
  910. pdf_font_orig_matrix(font, &orig_matrix);
  911. DISCARD(pdf_find_orig_font(pdev, font, &orig_matrix));
  912. /* Compute the scaling matrix and combined matrix. */
  913. gs_matrix_invert(&orig_matrix, &smat);
  914. gs_matrix_multiply(&smat, pfmat, &smat);
  915. tmat = ctm_only(penum->pis);
  916. tmat.tx = tmat.ty = 0;
  917. gs_matrix_multiply(&smat, &tmat, &tmat);
  918. /* Try to find a reasonable size value. This isn't necessary, */
  919. /* but it's worth a little effort. */
  920. size = fabs(tmat.yy) / sy;
  921. if (size < 0.01)
  922. size = fabs(tmat.xx) / sx;
  923. if (size < 0.01)
  924. size = 1;
  925. /* Check for spacing parameters we can handle, and transform them. */
  926. if (penum->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
  927. gs_point pt;
  928. code = transform_delta_inverse(&penum->text.delta_all, &smat, &pt);
  929. if (code >= 0 && pt.y == 0)
  930. chars = pt.x * size;
  931. else
  932. mask |= TEXT_ADD_TO_ALL_WIDTHS;
  933. }
  934. if (penum->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
  935. gs_point pt;
  936. code = transform_delta_inverse(&penum->text.delta_space, &smat, &pt);
  937. if (code >= 0 && pt.y == 0 && penum->text.space.s_char == 32)
  938. words = pt.x * size;
  939. else
  940. mask |= TEXT_ADD_TO_SPACE_WIDTH;
  941. }
  942. /* Find or create the font resource. */
  943. ppf = (pdf_font_t *)
  944. pdf_find_resource_by_gs_id(pdev, pdf_font_resource_type(font),
  945. font->id);
  946. /* Check for the possibility that the base font has been freed. */
  947. if (ppf && ppf->FontDescriptor && ppf->FontDescriptor->written)
  948. ppf = 0;
  949. if (ppf == 0 || ppf->font == 0) {
  950. code = pdf_create_pdf_font(pdev, font, &orig_matrix, &ppf);
  951. if (code < 0)
  952. return code;
  953. }
  954. /* Store the updated values. */
  955. tmat.xx /= size;
  956. tmat.xy /= size;
  957. tmat.yx /= size;
  958. tmat.yy /= size;
  959. tmat.tx += fixed2float(cpt.x);
  960. tmat.ty += fixed2float(cpt.y);
  961. ppts->chars = chars;
  962. ppts->words = words;
  963. ppts->size = size;
  964. ppts->text_matrix = tmat;
  965. ppts->mode = (font->PaintType == 0 ? 0 : 1);
  966. ppts->font = font;
  967. ppts->pdfont = ppf;
  968. return mask;
  969. }
  970. /*
  971. * For a given character, check whether the encoding of bfont (the current
  972. * font) is compatible with that of the underlying unscaled, possibly
  973. * standard, base font, and if not, whether we can re-encode the character
  974. * using the base font's encoding. Return the (possibly re-encoded)
  975. * character if successful.
  976. *
  977. * We have to handle re-encoded Type 1 fonts, because TeX output makes
  978. * constant use of them. We have several alternatives for how to handle
  979. * characters whose encoding doesn't match their encoding in the base font's
  980. * built-in encoding. If a character's glyph doesn't match the character's
  981. * glyph in the encoding built up so far, we check if the font has that
  982. * glyph at all; if not, we fall back to a bitmap. Otherwise, we use one or
  983. * both of the following algorithms:
  984. *
  985. * 1. If this is the first time a character at this position has been
  986. * seen, assign its glyph to that position in the encoding.
  987. * We do this step if the device parameter ReAssignCharacters is true.
  988. * (This is the default.)
  989. *
  990. * 2. If the glyph is present in the encoding at some other position,
  991. * substitute that position for the character; otherwise, assign the
  992. * glyph to an unoccupied (.notdef) position.
  993. * We do this step if the device parameter ReEncodeCharacters is true.
  994. * (This is the default.)
  995. *
  996. * 3. Finally, fall back to using a bitmap.
  997. *
  998. * If it is essential that all strings in the output contain exactly the
  999. * same character codes as the input, set ReEncodeCharacters to false. If
  1000. * it is important that strings be searchable, but some non-searchable
  1001. * strings can be tolerated, the defaults are appropriate. If searchability
  1002. * is not important, set ReAssignCharacters to false.
  1003. *
  1004. * Since the chars_used bit vector keeps track of characters by their
  1005. * position in the Encoding, not by their glyph identifier, it can't record
  1006. * use of unencoded characters. Therefore, if we ever use an unencoded
  1007. * character in an embedded font, we force writing the entire font rather
  1008. * than a subset. This is inelegant but (for the moment) too awkward to
  1009. * fix.
  1010. *
  1011. * Acrobat Reader 3's Print function has a bug that make re-encoded
  1012. * characters print as blank if the font is substituted (not embedded or one
  1013. * of the base 14). To work around this bug, when CompatibilityLevel <= 1.2,
  1014. * for non-embedded non-base fonts, no substitutions or re-encodings are
  1015. * allowed.
  1016. */
  1017. inline private void
  1018. record_used(pdf_font_descriptor_t *pfd, int c)
  1019. {
  1020. pfd->chars_used.data[c >> 3] |= 0x80 >> (c & 7);
  1021. }
  1022. inline private gs_encoding_index_t
  1023. base_encoding_index(const pdf_font_t *ppf)
  1024. {
  1025. return
  1026. (ppf->BaseEncoding != ENCODING_INDEX_UNKNOWN ? ppf->BaseEncoding :
  1027. ppf->index >= 0 ? pdf_standard_fonts[ppf->index].base_encoding :
  1028. ENCODING_INDEX_UNKNOWN);
  1029. }
  1030. private int
  1031. pdf_encode_char(gx_device_pdf *pdev, int chr, gs_font_base *bfont,
  1032. pdf_font_t *ppf)
  1033. {
  1034. pdf_font_descriptor_t *const pfd = ppf->FontDescriptor;
  1035. /*
  1036. * bfont is the current font in which the text is being shown.
  1037. * ei is its encoding_index.
  1038. */
  1039. gs_encoding_index_t ei = bfont->encoding_index;
  1040. /*
  1041. * base_font is the font that underlies this PDF font (i.e., this PDF
  1042. * font is base_font plus some possible Encoding and Widths differences,
  1043. * and possibly a different FontMatrix). base_font is 0 iff this PDF
  1044. * font is one of the standard 14 (i.e., ppf->index >= 0). bei is the
  1045. * index of the BaseEncoding (explicit or, for the standard fonts,
  1046. * implicit) that will be written in the PDF file: it is not necessarily
  1047. * the same as base_font->encoding_index, or even
  1048. * base_font->nearest_encoding_index.
  1049. */
  1050. gs_font *base_font = pfd->base_font;
  1051. bool have_font = base_font != 0 && base_font->FontType != ft_composite;
  1052. bool is_standard = ppf->index >= 0;
  1053. gs_encoding_index_t bei = base_encoding_index(ppf);
  1054. pdf_encoding_element_t *pdiff = ppf->Differences;
  1055. /*
  1056. * If set, font_glyph is the glyph currently associated with chr in
  1057. * base_font + bei + diffs; glyph is the glyph corresponding to chr in
  1058. * bfont.
  1059. */
  1060. gs_glyph font_glyph, glyph;
  1061. #define IS_USED(c)\
  1062. (((pfd)->chars_used.data[(c) >> 3] & (0x80 >> ((c) & 7))) != 0)
  1063. if (ei == bei && ei != ENCODING_INDEX_UNKNOWN && pdiff == 0) {
  1064. /*
  1065. * Just note that the character has been used with its original
  1066. * encoding.
  1067. */
  1068. record_used(pfd, chr);
  1069. return chr;
  1070. }
  1071. if (!is_standard && !have_font)
  1072. return_error(gs_error_undefined); /* can't encode */
  1073. #define ENCODE_NO_DIFF(ch)\
  1074. (bei != ENCODING_INDEX_UNKNOWN ?\
  1075. bfont->procs.callbacks.known_encode((gs_char)(ch), bei) :\
  1076. /* have_font */ bfont->procs.encode_char(base_font, ch, GLYPH_SPACE_NAME))
  1077. #define HAS_DIFF(ch) (pdiff != 0 && pdiff[ch].str.data != 0)
  1078. #define ENCODE_DIFF(ch) (pdiff[ch].glyph)
  1079. #define ENCODE(ch)\
  1080. (HAS_DIFF(ch) ? ENCODE_DIFF(ch) : ENCODE_NO_DIFF(ch))
  1081. font_glyph = ENCODE(chr);
  1082. glyph =
  1083. (ei == ENCODING_INDEX_UNKNOWN ?
  1084. bfont->procs.encode_char((gs_font *)bfont, chr, GLYPH_SPACE_NAME) :
  1085. bfont->procs.callbacks.known_encode(chr, ei));
  1086. if (glyph == font_glyph) {
  1087. record_used(pfd, chr);
  1088. return chr;
  1089. }
  1090. return pdf_encode_glyph(pdev, chr, glyph, bfont, ppf);
  1091. }
  1092. /*
  1093. * Encode a glyph that doesn't appear in the default place in an encoding.
  1094. * chr will be -1 if we are encoding a glyph for glyphshow.
  1095. */
  1096. private int
  1097. pdf_encode_glyph(gx_device_pdf *pdev, int chr, gs_glyph glyph,
  1098. gs_font_base *bfont, pdf_font_t *ppf)
  1099. {
  1100. pdf_font_descriptor_t *const pfd = ppf->FontDescriptor;
  1101. pdf_encoding_element_t *pdiff = ppf->Differences;
  1102. gs_font *base_font = pfd->base_font;
  1103. gs_encoding_index_t bei = base_encoding_index(ppf);
  1104. if (ppf->index < 0 && pfd->FontFile_id == 0 &&
  1105. pdev->CompatibilityLevel <= 1.2
  1106. ) {
  1107. /*
  1108. * Work around the bug in Acrobat Reader 3's Print function
  1109. * that makes re-encoded characters in substituted fonts
  1110. * print as blank.
  1111. */
  1112. return_error(gs_error_undefined);
  1113. }
  1114. /*
  1115. * TrueType fonts don't allow Differences in the encoding, but we might
  1116. * be able to re-encode the character if it appears elsewhere in the
  1117. * encoding.
  1118. */
  1119. if (bfont->FontType == ft_TrueType) {
  1120. if (pdev->ReEncodeCharacters) {
  1121. int c;
  1122. for (c = 0; c < 256; ++c)
  1123. if (ENCODE_NO_DIFF(c) == glyph) {
  1124. record_used(pfd, c);
  1125. return c;
  1126. }
  1127. }
  1128. return_error(gs_error_undefined);
  1129. }
  1130. /*
  1131. * If the font isn't going to be embedded, check whether this glyph is
  1132. * available in the base font's glyph set at all.
  1133. */
  1134. if (pfd->FontFile_id == 0) {
  1135. switch (bei) {
  1136. case ENCODING_INDEX_STANDARD:
  1137. case ENCODING_INDEX_ISOLATIN1:
  1138. case ENCODING_INDEX_WINANSI:
  1139. case ENCODING_INDEX_MACROMAN:
  1140. if (encoding_find_glyph(bfont, glyph, ENCODING_INDEX_ALOGLYPH) < 0
  1141. /*
  1142. * One would expect that the standard Latin character set in PDF
  1143. * 1.3, which was released after PostScript 3, would be the full
  1144. * 315-character PostScript 3 set. However, the PDF 1.3
  1145. * reference manual clearly specifies that the PDF 1.3 Latin
  1146. * character set is the smaller PostScript Level 2 set,
  1147. * and we have verified that this is the case in Acrobat 4.
  1148. * Therefore, we have commented out the second part of the
  1149. * conditional below.
  1150. */
  1151. #if 0
  1152. &&
  1153. (pdev->CompatibilityLevel < 1.3 ||
  1154. encoding_find_glyph(bfont, glyph, ENCODING_INDEX_ALXGLYPH) < 0)
  1155. #endif
  1156. )
  1157. return_error(gs_error_undefined);
  1158. default:
  1159. break;
  1160. }
  1161. }
  1162. if (pdev->ReAssignCharacters && chr >= 0) {
  1163. /*
  1164. * If this is the first time we've seen this character,
  1165. * assign the glyph to its position in the encoding.
  1166. */
  1167. if (!HAS_DIFF(chr) && !IS_USED(chr)) {
  1168. int code =
  1169. pdf_add_encoding_difference(pdev, ppf, chr, bfont, glyph);
  1170. if (code >= 0) {
  1171. /*
  1172. * As noted in the comments at the beginning of this file,
  1173. * we don't have any way to record, for the purpose of
  1174. * subsetting, the fact that a particular unencoded glyph
  1175. * was used. If this glyph doesn't appear anywhere else in
  1176. * the base encoding, fall back to writing the entire font.
  1177. */
  1178. int c;
  1179. for (c = 0; c < 256; ++c)
  1180. if (ENCODE_NO_DIFF(c) == glyph)
  1181. break;
  1182. if (c < 256) /* found */
  1183. record_used(pfd, c);
  1184. else { /* not found */
  1185. if (pfd->do_subset == FONT_SUBSET_YES)
  1186. return_error(gs_error_undefined);
  1187. pfd->do_subset = FONT_SUBSET_NO;
  1188. }
  1189. return chr;
  1190. }
  1191. }
  1192. }
  1193. if (pdev->ReEncodeCharacters || chr < 0) {
  1194. /*
  1195. * Look for the glyph at some other position in the encoding.
  1196. */
  1197. int c, code;
  1198. for (c = 0; c < 256; ++c) {
  1199. if (HAS_DIFF(c)) {
  1200. if (ENCODE_DIFF(c) == glyph)
  1201. return c;
  1202. } else if (ENCODE_NO_DIFF(c) == glyph) {
  1203. record_used(pfd, c);
  1204. return c;
  1205. }
  1206. }
  1207. /*
  1208. * The glyph isn't encoded anywhere. Look for a
  1209. * never-referenced .notdef position where we can put it.
  1210. */
  1211. for (c = 0; c < 256; ++c) {
  1212. gs_glyph font_glyph;
  1213. if (HAS_DIFF(c) || IS_USED(c))
  1214. continue; /* slot already referenced */
  1215. font_glyph = ENCODE_NO_DIFF(c);
  1216. if (font_glyph == gs_no_glyph ||
  1217. gs_font_glyph_is_notdef(bfont, font_glyph)
  1218. )
  1219. break;
  1220. }
  1221. if (c == 256) /* no .notdef positions left */
  1222. return_error(gs_error_undefined);
  1223. code = pdf_add_encoding_difference(pdev, ppf, c, bfont, glyph);
  1224. if (code < 0)
  1225. return code;
  1226. /* See under ReAssignCharacters above regarding the following: */
  1227. if (pfd->do_subset == FONT_SUBSET_YES)
  1228. return_error(gs_error_undefined);
  1229. pfd->do_subset = FONT_SUBSET_NO;
  1230. return c;
  1231. }
  1232. return_error(gs_error_undefined);
  1233. #undef IS_USED
  1234. #undef ENCODE_NO_DIFF
  1235. #undef HAS_DIFF
  1236. #undef ENCODE_DIFF
  1237. #undef ENCODE
  1238. }
  1239. /*
  1240. * Write out commands to make the output state match the processing state.
  1241. */
  1242. private double
  1243. font_matrix_scaling(const gs_font *font)
  1244. {
  1245. return fabs((font->FontMatrix.yy != 0 ? font->FontMatrix.yy :
  1246. font->FontMatrix.yx));
  1247. }
  1248. private int
  1249. pdf_write_text_process_state(gx_device_pdf *pdev,
  1250. const gs_text_enum_t *pte, /* for pdcolor, pis */
  1251. const pdf_text_process_state_t *ppts,
  1252. const gs_const_string *pstr)
  1253. {
  1254. int code;
  1255. pdf_set_font_and_size(pdev, ppts->pdfont, ppts->size);
  1256. code = pdf_set_text_matrix(pdev, &ppts->text_matrix);
  1257. if (code < 0)
  1258. return code;
  1259. if (pdev->text.character_spacing != ppts->chars &&
  1260. pstr->size + pdev->text.buffer_count > 1
  1261. ) {
  1262. code = pdf_open_page(pdev, PDF_IN_TEXT);
  1263. if (code < 0)
  1264. return code;
  1265. pprintg1(pdev->strm, "%g Tc\n", ppts->chars);
  1266. pdev->text.character_spacing = ppts->chars;
  1267. }
  1268. if (pdev->text.word_spacing != ppts->words &&
  1269. (memchr(pstr->data, 32, pstr->size) ||
  1270. memchr(pdev->text.buffer, 32, pdev->text.buffer_count))
  1271. ) {
  1272. code = pdf_open_page(pdev, PDF_IN_TEXT);
  1273. if (code < 0)
  1274. return code;
  1275. pprintg1(pdev->strm, "%g Tw\n", ppts->words);
  1276. pdev->text.word_spacing = ppts->words;
  1277. }
  1278. if (pdev->text.render_mode != ppts->mode) {
  1279. code = pdf_open_page(pdev, PDF_IN_TEXT);
  1280. if (code < 0)
  1281. return code;
  1282. pprintd1(pdev->strm, "%d Tr\n", ppts->mode);
  1283. if (ppts->mode) {
  1284. /* Also write all the parameters for stroking. */
  1285. gs_imager_state *pis = pte->pis;
  1286. float save_width = pis->line_params.half_width;
  1287. const gs_font *font = ppts->font;
  1288. const gs_font *bfont = font;
  1289. double scaled_width = font->StrokeWidth;
  1290. /*
  1291. * The font's StrokeWidth is in the character coordinate system,
  1292. * which means that it should be scaled by the inverse of any
  1293. * scaling in the FontMatrix. Do the best we can with this.
  1294. */
  1295. while (bfont != bfont->base)
  1296. bfont = bfont->base;
  1297. scaled_width *= font_matrix_scaling(bfont) /
  1298. font_matrix_scaling(font);
  1299. pis->line_params.half_width = scaled_width / 2;
  1300. code = pdf_prepare_stroke(pdev, pis);
  1301. if (code >= 0) {
  1302. /*
  1303. * See stream_to_text in gdevpdfu.c re the computation of
  1304. * the scaling value.
  1305. */
  1306. double scale = 72.0 / pdev->HWResolution[1];
  1307. code = gdev_vector_prepare_stroke((gx_device_vector *)pdev,
  1308. pis, NULL, NULL, scale);
  1309. }
  1310. pis->line_params.half_width = save_width;
  1311. if (code < 0)
  1312. return code;
  1313. }
  1314. pdev->text.render_mode = ppts->mode;
  1315. }
  1316. return 0;
  1317. }
  1318. /* Compute the total text width (in user space). */
  1319. private int
  1320. process_text_return_width(const gs_text_enum_t *pte, gs_font *font,
  1321. pdf_font_t *pdfont, const gs_matrix *pfmat,
  1322. const gs_const_string *pstr,
  1323. int *pindex, gs_point *pdpt)
  1324. {
  1325. int i, w;
  1326. double scale = (font->FontType == ft_TrueType ? 0.001 : 1.0);
  1327. gs_point dpt;
  1328. int num_spaces = 0;
  1329. int space_char =
  1330. (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH ?
  1331. pte->text.space.s_char : -1);
  1332. for (i = *pindex, w = 0; i < pstr->size; ++i) {
  1333. int cw;
  1334. int code = pdf_char_width(pdfont, pstr->data[i], font, &cw);
  1335. if (code < 0)
  1336. return code;
  1337. w += cw;
  1338. if (pstr->data[i] == space_char)
  1339. ++num_spaces;
  1340. }
  1341. gs_distance_transform(w * scale, 0.0, pfmat, &dpt);
  1342. if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
  1343. int num_chars = pstr->size - pte->index;
  1344. dpt.x += pte->text.delta_all.x * num_chars;
  1345. dpt.y += pte->text.delta_all.y * num_chars;
  1346. }
  1347. if (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
  1348. dpt.x += pte->text.delta_space.x * num_spaces;
  1349. dpt.y += pte->text.delta_space.y * num_spaces;
  1350. }
  1351. *pindex = i;
  1352. *pdpt = dpt;
  1353. return 0;
  1354. }
  1355. /*
  1356. * Emulate TEXT_ADD_TO_ALL_WIDTHS and/or TEXT_ADD_TO_SPACE_WIDTH.
  1357. * We know that the Tw and Tc values are zero.
  1358. */
  1359. private int
  1360. process_text_add_width(gs_text_enum_t *pte, gs_font *font,
  1361. const gs_matrix *pfmat,
  1362. const pdf_text_process_state_t *ppts,
  1363. const gs_const_string *pstr,
  1364. int *pindex, gs_point *pdpt)
  1365. {
  1366. gx_device_pdf *const pdev = (gx_device_pdf *)pte->dev;
  1367. int i, w;
  1368. double scale = (font->FontType == ft_TrueType ? 0.001 : 1.0);
  1369. gs_point dpt;
  1370. gs_matrix tmat;
  1371. int space_char =
  1372. (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH ?
  1373. pte->text.space.s_char : -1);
  1374. int code = 0;
  1375. bool move = false;
  1376. dpt.x = dpt.y = 0;
  1377. tmat = ppts->text_matrix;
  1378. for (i = *pindex, w = 0; i < pstr->size; ++i) {
  1379. int cw;
  1380. int code = pdf_char_width(ppts->pdfont, pstr->data[i], font, &cw);
  1381. gs_point wpt;
  1382. if (code < 0)
  1383. break;
  1384. if (move) {
  1385. gs_point mpt;
  1386. gs_distance_transform(dpt.x, dpt.y, &ctm_only(pte->pis), &mpt);
  1387. tmat.tx = ppts->text_matrix.tx + mpt.x;
  1388. tmat.ty = ppts->text_matrix.ty + mpt.y;
  1389. code = pdf_set_text_matrix(pdev, &tmat);
  1390. if (code < 0)
  1391. break;
  1392. move = false;
  1393. }
  1394. if (pte->text.operation & TEXT_DO_DRAW) {
  1395. code = pdf_append_chars(pdev, &pstr->data[i], 1);
  1396. if (code < 0)
  1397. break;
  1398. }
  1399. gs_distance_transform(cw * scale, 0.0, pfmat, &wpt);
  1400. dpt.x += wpt.x, dpt.y += wpt.y;
  1401. if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
  1402. dpt.x += pte->text.delta_all.x;
  1403. dpt.y += pte->text.delta_all.y;
  1404. move = true;
  1405. }
  1406. if (pstr->data[i] == space_char) {
  1407. dpt.x += pte->text.delta_space.x;
  1408. dpt.y += pte->text.delta_space.y;
  1409. move = true;
  1410. }
  1411. }
  1412. *pindex = i; /* only do the part we haven't done yet */
  1413. *pdpt = dpt;
  1414. return code;
  1415. }
  1416. /* ---------------- Text and font utilities ---------------- */
  1417. /*
  1418. * Try to find a glyph in a (pseudo-)encoding. If present, return the
  1419. * index (character code); if absent, return -1.
  1420. */
  1421. private int
  1422. encoding_find_glyph(gs_font_base *bfont, gs_glyph font_glyph,
  1423. gs_encoding_index_t index)
  1424. {
  1425. int ch;
  1426. gs_glyph glyph;
  1427. for (ch = 0;
  1428. (glyph = bfont->procs.callbacks.known_encode((gs_char)ch, index)) !=
  1429. gs_no_glyph;
  1430. ++ch)
  1431. if (glyph == font_glyph)
  1432. return ch;
  1433. return -1;
  1434. }