gdevpdte.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /* Copyright (C) 2002-2004 artofcode LLC. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpdte.c,v 1.70 2005/03/03 13:15:55 igor Exp $ */
  14. /* Encoding-based (Type 1/2/42) text processing for pdfwrite. */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gsutil.h"
  20. #include "gxfcmap.h"
  21. #include "gxfcopy.h"
  22. #include "gxfont.h"
  23. #include "gxfont0.h"
  24. #include "gxfont0c.h"
  25. #include "gxpath.h" /* for getting current point */
  26. #include "gdevpsf.h"
  27. #include "gdevpdfx.h"
  28. #include "gdevpdfg.h"
  29. #include "gdevpdtx.h"
  30. #include "gdevpdtd.h"
  31. #include "gdevpdtf.h"
  32. #include "gdevpdts.h"
  33. #include "gdevpdtt.h"
  34. private int pdf_char_widths(gx_device_pdf *const pdev,
  35. pdf_font_resource_t *pdfont, int ch,
  36. gs_font_base *font,
  37. pdf_glyph_widths_t *pwidths /* may be NULL */);
  38. private int pdf_encode_string(gx_device_pdf *pdev, pdf_text_enum_t *penum,
  39. const gs_string *pstr, const gs_glyph *gdata,
  40. pdf_font_resource_t **ppdfont);
  41. private int pdf_process_string(pdf_text_enum_t *penum, gs_string *pstr,
  42. pdf_font_resource_t *pdfont,
  43. const gs_matrix *pfmat,
  44. pdf_text_process_state_t *ppts);
  45. /*
  46. * Encode and process a string with a simple gs_font.
  47. */
  48. int
  49. pdf_encode_process_string(pdf_text_enum_t *penum, gs_string *pstr,
  50. const gs_glyph *gdata, const gs_matrix *pfmat,
  51. pdf_text_process_state_t *ppts)
  52. {
  53. gx_device_pdf *const pdev = (gx_device_pdf *)penum->dev;
  54. pdf_font_resource_t *pdfont;
  55. gs_font_base *font;
  56. int code = 0;
  57. switch (penum->current_font->FontType) {
  58. case ft_TrueType:
  59. case ft_encrypted:
  60. case ft_encrypted2:
  61. case ft_user_defined:
  62. break;
  63. default:
  64. return_error(gs_error_rangecheck);
  65. }
  66. font = (gs_font_base *)penum->current_font;
  67. code = pdf_encode_string(pdev, penum, pstr, gdata, &pdfont); /* Must not change penum. */
  68. if (code < 0)
  69. return code;
  70. return pdf_process_string(penum, pstr, pdfont, pfmat, ppts);
  71. }
  72. /*
  73. * Add char code pair to ToUnicode CMap,
  74. * creating the CMap on neccessity.
  75. */
  76. int
  77. pdf_add_ToUnicode(gx_device_pdf *pdev, gs_font *font, pdf_font_resource_t *pdfont,
  78. gs_glyph glyph, gs_char ch)
  79. { int code;
  80. gs_char unicode;
  81. if (glyph == GS_NO_GLYPH)
  82. return 0;
  83. unicode = font->procs.decode_glyph((gs_font *)font, glyph);
  84. if (unicode != GS_NO_CHAR) {
  85. if (pdfont->cmap_ToUnicode == NULL) {
  86. uint num_codes = 256, key_size = 1;
  87. if (font->FontType == ft_CID_encrypted) {
  88. gs_font_cid0 *pfcid = (gs_font_cid0 *)font;
  89. num_codes = pfcid->cidata.common.CIDCount;
  90. key_size = 2;
  91. } else if (font->FontType == ft_CID_TrueType) {
  92. #if 0
  93. gs_font_cid2 *pfcid = (gs_font_cid2 *)font;
  94. num_codes = pfcid->cidata.common.CIDCount;
  95. #else
  96. /* Since PScript5.dll creates GlyphNames2Unicode with character codes
  97. instead CIDs, and with the WinCharSetFFFF-H2 CMap
  98. character codes appears from the range 0-0xFFFF (Bug 687954),
  99. we must use the maximal character code value for the ToUnicode
  100. code count. */
  101. num_codes = 65536;
  102. #endif
  103. key_size = 2;
  104. }
  105. code = gs_cmap_ToUnicode_alloc(pdev->pdf_memory, pdfont->rid, num_codes, key_size,
  106. &pdfont->cmap_ToUnicode);
  107. if (code < 0)
  108. return code;
  109. }
  110. if (pdfont->cmap_ToUnicode != NULL)
  111. gs_cmap_ToUnicode_add_pair(pdfont->cmap_ToUnicode, ch, unicode);
  112. }
  113. return 0;
  114. }
  115. /*
  116. * If the current substream is a charproc, register a font used in it.
  117. */
  118. int
  119. pdf_register_charproc_resource(gx_device_pdf *pdev, gs_id id, pdf_resource_type_t type)
  120. {
  121. if (pdev->font3 != 0) {
  122. pdf_font_resource_t *pdfont = (pdf_font_resource_t *)pdev->font3;
  123. pdf_resource_ref_t *used_resources = pdfont->u.simple.s.type3.used_resources;
  124. int i, used_resources_count = pdfont->u.simple.s.type3.used_resources_count;
  125. int used_resources_max = pdfont->u.simple.s.type3.used_resources_max;
  126. for (i = 0; i < used_resources_count; i++)
  127. if (used_resources[i].id == id && used_resources[i].type == type)
  128. return 0;
  129. if (used_resources_count >= used_resources_max) {
  130. used_resources_max += 10;
  131. used_resources = (pdf_resource_ref_t *)gs_alloc_bytes(pdev->pdf_memory,
  132. sizeof(pdf_resource_ref_t) * used_resources_max,
  133. "pdf_register_charproc_resource");
  134. if (!used_resources)
  135. return_error(gs_error_VMerror);
  136. if (used_resources_count) {
  137. memcpy(used_resources, pdfont->u.simple.s.type3.used_resources,
  138. sizeof(pdf_resource_ref_t) * used_resources_count);
  139. gs_free_object(pdev->pdf_memory, pdfont->u.simple.s.type3.used_resources,
  140. "pdf_register_charproc_resource");
  141. }
  142. pdfont->u.simple.s.type3.used_resources = used_resources;
  143. pdfont->u.simple.s.type3.used_resources_max = used_resources_max;
  144. }
  145. used_resources[used_resources_count].id = id;
  146. used_resources[used_resources_count].type = type;
  147. pdfont->u.simple.s.type3.used_resources_count = used_resources_count + 1;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Register charproc fonts with the page or substream.
  153. */
  154. int
  155. pdf_used_charproc_resources(gx_device_pdf *pdev, pdf_font_resource_t *pdfont)
  156. {
  157. if (pdfont->where_used & pdev->used_mask)
  158. return 0;
  159. pdfont->where_used |= pdev->used_mask;
  160. if (pdfont->FontType == ft_user_defined) {
  161. pdf_resource_ref_t *used_resources = pdfont->u.simple.s.type3.used_resources;
  162. int i, used_resources_count = pdfont->u.simple.s.type3.used_resources_count;
  163. for (i = 0; i < used_resources_count; i++) {
  164. pdf_resource_t *pres =
  165. pdf_find_resource_by_resource_id(pdev,
  166. used_resources[i].type, used_resources[i].id);
  167. if (pres == NULL)
  168. return_error(gs_error_unregistered); /* Must not happen. */
  169. pres->where_used |= pdev->used_mask;
  170. }
  171. }
  172. return 0;
  173. }
  174. /*
  175. * Given a text string and a simple gs_font, return a font resource suitable
  176. * for the text string, possibly re-encoding the string. This
  177. * may involve creating a font resource and/or adding glyphs and/or Encoding
  178. * entries to it.
  179. *
  180. * Sets *ppdfont.
  181. */
  182. private int
  183. pdf_encode_string(gx_device_pdf *pdev, pdf_text_enum_t *penum,
  184. const gs_string *pstr, const gs_glyph *gdata,
  185. pdf_font_resource_t **ppdfont)
  186. {
  187. gs_font *font = (gs_font *)penum->current_font;
  188. pdf_font_resource_t *pdfont = 0;
  189. gs_font_base *cfont, *ccfont;
  190. int code, i;
  191. /*
  192. * In contradiction with pre-7.20 versions of pdfwrite,
  193. * we never re-encode texts due to possible encoding conflict while font merging.
  194. */
  195. code = pdf_obtain_font_resource(penum, pstr, &pdfont);
  196. if (code < 0)
  197. return code;
  198. code = pdf_add_resource(pdev, pdev->substream_Resources, "/Font", (pdf_resource_t *)pdfont);
  199. if (code < 0)
  200. return code;
  201. code = pdf_register_charproc_resource(pdev, pdf_resource_id((pdf_resource_t *)pdfont), resourceFont);
  202. if (code < 0)
  203. return code;
  204. cfont = pdf_font_resource_font(pdfont, false);
  205. ccfont = pdf_font_resource_font(pdfont, true);
  206. for (i = 0; i < pstr->size; ++i) {
  207. int ch = pstr->data[i];
  208. pdf_encoding_element_t *pet = &pdfont->u.simple.Encoding[ch];
  209. gs_glyph glyph = (gdata == NULL
  210. ? font->procs.encode_char(font, ch, GLYPH_SPACE_NAME)
  211. : gdata[i]);
  212. gs_glyph copied_glyph;
  213. gs_const_string gnstr;
  214. if (glyph == GS_NO_GLYPH || glyph == pet->glyph)
  215. continue;
  216. if (pet->glyph != GS_NO_GLYPH) { /* encoding conflict */
  217. return_error(gs_error_rangecheck);
  218. /* Must not happen because pdf_obtain_font_resource
  219. * checks for encoding compatibility.
  220. */
  221. }
  222. code = font->procs.glyph_name(font, glyph, &gnstr);
  223. if (code < 0)
  224. return code; /* can't get name of glyph */
  225. if (font->FontType != ft_user_defined) {
  226. /* The standard 14 fonts don't have a FontDescriptor. */
  227. code = (pdfont->base_font != 0 ?
  228. pdf_base_font_copy_glyph(pdfont->base_font, glyph, (gs_font_base *)font) :
  229. pdf_font_used_glyph(pdfont->FontDescriptor, glyph, (gs_font_base *)font));
  230. if (code < 0 && code != gs_error_undefined)
  231. return code;
  232. if (code == gs_error_undefined) {
  233. /* PS font has no such glyph. */
  234. if (bytes_compare(gnstr.data, gnstr.size, (const byte *)".notdef", 7)) {
  235. pet->glyph = glyph;
  236. pet->str = gnstr;
  237. pet->is_difference = true;
  238. }
  239. } else if (pdfont->base_font == NULL && ccfont != NULL &&
  240. (gs_copy_glyph_options(font, glyph, (gs_font *)ccfont, COPY_GLYPH_NO_NEW) != 1 ||
  241. gs_copied_font_add_encoding((gs_font *)ccfont, ch, glyph) < 0)) {
  242. /*
  243. * The "complete" copy of the font appears incomplete
  244. * due to incrementally added glyphs. Drop the "complete"
  245. * copy now and continue with subset font only.
  246. *
  247. * Note that we need to add the glyph to the encoding of the
  248. * "complete" font, because "PPI-ProPag 2.6.1.4 (archivePg)"
  249. * creates multiple font copies with reduced encodings
  250. * (we believe it is poorly designed),
  251. * and we can merge the copies back to a single font (see Bug 686875).
  252. * We also check whether the encoding is compatible.
  253. * It must be compatible here due to the pdf_obtain_font_resource
  254. * and ccfont logics, but we want to ensure for safety reason.
  255. */
  256. ccfont = NULL;
  257. pdf_font_descriptor_drop_complete_font(pdfont->FontDescriptor);
  258. }
  259. /*
  260. * We arbitrarily allow the first encoded character in a given
  261. * position to determine the encoding associated with the copied
  262. * font.
  263. */
  264. copied_glyph = cfont->procs.encode_char((gs_font *)cfont, ch,
  265. GLYPH_SPACE_NAME);
  266. if (glyph != copied_glyph &&
  267. gs_copied_font_add_encoding((gs_font *)cfont, ch, glyph) < 0
  268. )
  269. pet->is_difference = true;
  270. pdfont->used[ch >> 3] |= 0x80 >> (ch & 7);
  271. }
  272. /*
  273. * We always generate ToUnicode for simple fonts, because
  274. * we can't detemine in advance, which glyphs the font actually uses.
  275. * The decision about writing it out is deferred until pdf_write_font_resource.
  276. */
  277. code = pdf_add_ToUnicode(pdev, font, pdfont, glyph, ch);
  278. if (code < 0)
  279. return code;
  280. pet->glyph = glyph;
  281. pet->str = gnstr;
  282. }
  283. *ppdfont = pdfont;
  284. return 0;
  285. }
  286. /*
  287. * Estimate text bbox.
  288. */
  289. private int
  290. process_text_estimate_bbox(pdf_text_enum_t *pte, gs_font_base *font,
  291. const gs_const_string *pstr,
  292. const gs_matrix *pfmat,
  293. gs_rect *text_bbox, gs_point *pdpt)
  294. {
  295. int i;
  296. int space_char =
  297. (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH ?
  298. pte->text.space.s_char : -1);
  299. int WMode = font->WMode;
  300. int code = 0;
  301. gs_point total = {0, 0};
  302. gs_fixed_point origin;
  303. gs_matrix m;
  304. int xy_index = pte->xy_index;
  305. if (font->FontBBox.p.x == font->FontBBox.q.x ||
  306. font->FontBBox.p.y == font->FontBBox.q.y)
  307. return_error(gs_error_undefined);
  308. code = gx_path_current_point(pte->path, &origin);
  309. if (code < 0)
  310. return code;
  311. m = ctm_only(pte->pis);
  312. m.tx = fixed2float(origin.x);
  313. m.ty = fixed2float(origin.y);
  314. gs_matrix_multiply(pfmat, &m, &m);
  315. for (i = 0; i < pstr->size; ++i) {
  316. byte c = pstr->data[i];
  317. gs_rect bbox;
  318. gs_point wanted, tpt, p0, p1, p2, p3;
  319. gs_glyph glyph = font->procs.encode_char((gs_font *)font, c,
  320. GLYPH_SPACE_NAME);
  321. gs_glyph_info_t info;
  322. int code = font->procs.glyph_info((gs_font *)font, glyph, NULL,
  323. GLYPH_INFO_WIDTH0 << WMode,
  324. &info);
  325. if (code < 0)
  326. return code;
  327. gs_point_transform(font->FontBBox.p.x, font->FontBBox.p.y, &m, &p0);
  328. gs_point_transform(font->FontBBox.p.x, font->FontBBox.q.y, &m, &p1);
  329. gs_point_transform(font->FontBBox.q.x, font->FontBBox.p.y, &m, &p2);
  330. gs_point_transform(font->FontBBox.q.x, font->FontBBox.q.y, &m, &p3);
  331. bbox.p.x = min(min(p0.x, p1.x), min(p1.x, p2.x)) + total.x;
  332. bbox.p.y = min(min(p0.y, p1.y), min(p1.y, p2.y)) + total.y;
  333. bbox.q.x = max(max(p0.x, p1.x), max(p1.x, p2.x)) + total.x;
  334. bbox.q.y = max(max(p0.y, p1.y), max(p1.y, p2.y)) + total.y;
  335. if (i == 0)
  336. *text_bbox = bbox;
  337. else
  338. rect_merge(*text_bbox, bbox);
  339. if (pte->text.operation & TEXT_REPLACE_WIDTHS) {
  340. gs_text_replaced_width(&pte->text, xy_index++, &tpt);
  341. gs_distance_transform(tpt.x, tpt.y, &ctm_only(pte->pis), &wanted);
  342. } else {
  343. gs_distance_transform(info.width[WMode].x,
  344. info.width[WMode].y,
  345. &m, &wanted);
  346. if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
  347. gs_distance_transform(pte->text.delta_all.x,
  348. pte->text.delta_all.y,
  349. &ctm_only(pte->pis), &tpt);
  350. wanted.x += tpt.x;
  351. wanted.y += tpt.y;
  352. }
  353. if (pstr->data[i] == space_char && pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
  354. gs_distance_transform(pte->text.delta_space.x,
  355. pte->text.delta_space.y,
  356. &ctm_only(pte->pis), &tpt);
  357. wanted.x += tpt.x;
  358. wanted.y += tpt.y;
  359. }
  360. }
  361. total.x += wanted.x;
  362. total.y += wanted.y;
  363. }
  364. *pdpt = total;
  365. return 0;
  366. }
  367. private void
  368. adjust_first_last_char(pdf_font_resource_t *pdfont, byte *str, int size)
  369. {
  370. int i;
  371. for (i = 0; i < size; ++i) {
  372. int chr = str[i];
  373. if (chr < pdfont->u.simple.FirstChar)
  374. pdfont->u.simple.FirstChar = chr;
  375. if (chr > pdfont->u.simple.LastChar)
  376. pdfont->u.simple.LastChar = chr;
  377. }
  378. }
  379. int
  380. pdf_shift_text_currentpoint(pdf_text_enum_t *penum, gs_point *wpt)
  381. {
  382. gs_state *pgs;
  383. extern_st(st_gs_state);
  384. if (gs_object_type(penum->dev->memory, penum->pis) != &st_gs_state) {
  385. /* Probably never happens. Not sure though. */
  386. return_error(gs_error_unregistered);
  387. }
  388. pgs = (gs_state *)penum->pis;
  389. return gs_moveto_aux(penum->pis, gx_current_path(pgs),
  390. fixed2float(penum->origin.x) + wpt->x,
  391. fixed2float(penum->origin.y) + wpt->y);
  392. }
  393. /*
  394. * Internal procedure to process a string in a non-composite font.
  395. * Doesn't use or set pte->{data,size,index}; may use/set pte->xy_index;
  396. * may set penum->returned.total_width. Sets ppts->values.
  397. *
  398. * Note that the caller is responsible for re-encoding the string, if
  399. * necessary; for adding Encoding entries in pdfont; and for copying any
  400. * necessary glyphs. penum->current_font provides the gs_font for getting
  401. * glyph metrics, but this font's Encoding is not used.
  402. */
  403. private int process_text_return_width(const pdf_text_enum_t *pte,
  404. gs_font_base *font,
  405. pdf_text_process_state_t *ppts,
  406. const gs_const_string *pstr,
  407. gs_point *pdpt, int *accepted);
  408. private int
  409. pdf_process_string(pdf_text_enum_t *penum, gs_string *pstr,
  410. pdf_font_resource_t *pdfont, const gs_matrix *pfmat,
  411. pdf_text_process_state_t *ppts)
  412. {
  413. gx_device_pdf *const pdev = (gx_device_pdf *)penum->dev;
  414. gs_font_base *font = (gs_font_base *)penum->current_font;
  415. const gs_text_params_t *text = &penum->text;
  416. int code = 0, mask;
  417. gs_point width_pt;
  418. gs_rect text_bbox;
  419. int accepted;
  420. if (pfmat == 0)
  421. pfmat = &font->FontMatrix;
  422. if (text->operation & TEXT_RETURN_WIDTH) {
  423. code = gx_path_current_point(penum->path, &penum->origin);
  424. if (code < 0)
  425. return code;
  426. }
  427. if (text->size == 0)
  428. return 0;
  429. if (penum->pis->text_rendering_mode != 3 && !(text->operation & TEXT_DO_NONE)) {
  430. /*
  431. * Acrobat Reader can't handle text with huge coordinates,
  432. * so skip the text if it is outside the clip bbox
  433. * (Note : it ever fails with type 3 fonts).
  434. */
  435. code = process_text_estimate_bbox(penum, font, (gs_const_string *)pstr, pfmat,
  436. &text_bbox, &width_pt);
  437. if (code == 0) {
  438. gs_fixed_rect clip_bbox;
  439. gs_rect rect;
  440. gx_cpath_outer_box(penum->pcpath, &clip_bbox);
  441. rect.p.x = fixed2float(clip_bbox.p.x);
  442. rect.p.y = fixed2float(clip_bbox.p.y);
  443. rect.q.x = fixed2float(clip_bbox.q.x);
  444. rect.q.y = fixed2float(clip_bbox.q.y);
  445. rect_intersect(rect, text_bbox);
  446. if (rect.p.x > rect.q.x || rect.p.y > rect.q.y) {
  447. penum->index += pstr->size;
  448. goto finish;
  449. }
  450. }
  451. } else {
  452. /* We have no penum->pcpath. */
  453. }
  454. /*
  455. * Note that pdf_update_text_state sets all the members of ppts->values
  456. * to their current values.
  457. */
  458. code = pdf_update_text_state(ppts, penum, pdfont, pfmat);
  459. if (code > 0) {
  460. /* Try not to emulate ADD_TO_WIDTH if we don't have to. */
  461. if (code & TEXT_ADD_TO_SPACE_WIDTH) {
  462. if (!memchr(pstr->data, penum->text.space.s_char, pstr->size))
  463. code &= ~TEXT_ADD_TO_SPACE_WIDTH;
  464. }
  465. }
  466. if (code < 0)
  467. return code;
  468. mask = code;
  469. if (text->operation & TEXT_REPLACE_WIDTHS)
  470. mask |= TEXT_REPLACE_WIDTHS;
  471. /*
  472. * The only operations left to handle are TEXT_DO_DRAW and
  473. * TEXT_RETURN_WIDTH.
  474. */
  475. if (mask == 0) {
  476. /*
  477. * If any character has real_width != Width, we have to process
  478. * the string character-by-character. process_text_return_width
  479. * will tell us what we need to know.
  480. */
  481. if (!(text->operation & (TEXT_DO_DRAW | TEXT_RETURN_WIDTH)))
  482. return 0;
  483. code = process_text_return_width(penum, font, ppts,
  484. (gs_const_string *)pstr,
  485. &width_pt, &accepted);
  486. if (code < 0)
  487. return code;
  488. if (code == 0) {
  489. /* No characters with redefined widths -- the fast case. */
  490. if (text->operation & TEXT_DO_DRAW || penum->pis->text_rendering_mode == 3) {
  491. code = pdf_append_chars(pdev, pstr->data, accepted,
  492. width_pt.x, width_pt.y, false);
  493. if (code < 0)
  494. return code;
  495. adjust_first_last_char(pdfont, pstr->data, accepted);
  496. penum->index += accepted;
  497. } else if (text->operation & TEXT_DO_NONE)
  498. penum->index += accepted;
  499. } else {
  500. /* Use the slow case. Set mask to any non-zero value. */
  501. mask = TEXT_RETURN_WIDTH;
  502. }
  503. }
  504. if (mask) {
  505. /* process_text_modify_width destroys text parameters, save them now. */
  506. int index0 = penum->index, xy_index = penum->xy_index;
  507. gs_text_params_t text = penum->text;
  508. int xy_index_step = (penum->text.x_widths != NULL && /* see gs_text_replaced_width */
  509. penum->text.x_widths == penum->text.y_widths ? 2 : 1);
  510. if (penum->text.x_widths != NULL) {
  511. penum->text.x_widths += xy_index * xy_index_step;
  512. }
  513. if (penum->text.y_widths != NULL)
  514. penum->text.y_widths += xy_index * xy_index_step;
  515. penum->xy_index = 0;
  516. code = process_text_modify_width(penum, (gs_font *)font, ppts,
  517. (gs_const_string *)pstr,
  518. &width_pt);
  519. if (penum->text.x_widths != NULL)
  520. penum->text.x_widths -= xy_index * xy_index_step;
  521. if (penum->text.y_widths != NULL)
  522. penum->text.y_widths -= xy_index * xy_index_step;
  523. penum->xy_index += xy_index;
  524. adjust_first_last_char(pdfont, pstr->data, penum->index);
  525. penum->text = text;
  526. penum->index += index0;
  527. if (code < 0)
  528. return code;
  529. }
  530. finish:
  531. /* Finally, return the total width if requested. */
  532. if (!(text->operation & TEXT_RETURN_WIDTH))
  533. return 0;
  534. if (text->operation & TEXT_DO_NONE) {
  535. /* stringwidth needs to transform to user space. */
  536. gs_point p;
  537. gs_distance_transform_inverse(width_pt.x, width_pt.y, &ctm_only(penum->pis), &p);
  538. penum->returned.total_width.x += p.x;
  539. penum->returned.total_width.y += p.y;
  540. } else
  541. penum->returned.total_width = width_pt;
  542. return pdf_shift_text_currentpoint(penum, &width_pt);
  543. }
  544. /*
  545. * Get the widths (unmodified and possibly modified) of a given character
  546. * in a simple font. May add the widths to the widths cache (pdfont->Widths
  547. * and pdf_font_cache_elem::real_widths). Return 1 if the widths were not cached.
  548. */
  549. private int
  550. pdf_char_widths(gx_device_pdf *const pdev,
  551. pdf_font_resource_t *pdfont, int ch, gs_font_base *font,
  552. pdf_glyph_widths_t *pwidths /* may be NULL */)
  553. {
  554. pdf_glyph_widths_t widths;
  555. int code;
  556. byte *glyph_usage;
  557. double *real_widths;
  558. int char_cache_size, width_cache_size;
  559. pdf_font_resource_t *pdfont1;
  560. code = pdf_attached_font_resource(pdev, (gs_font *)font, &pdfont1,
  561. &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
  562. if (code < 0)
  563. return code;
  564. if (pdfont1 != pdfont)
  565. return_error(gs_error_unregistered); /* Must not happen. */
  566. if (ch < 0 || ch > 255)
  567. return_error(gs_error_rangecheck);
  568. if (ch >= width_cache_size)
  569. return_error(gs_error_unregistered); /* Must not happen. */
  570. if (pwidths == 0)
  571. pwidths = &widths;
  572. if (font->FontType != ft_user_defined && real_widths[ch] == 0) {
  573. /* Might be an unused char, or just not cached. */
  574. gs_glyph glyph = pdfont->u.simple.Encoding[ch].glyph;
  575. code = pdf_glyph_widths(pdfont, font->WMode, glyph, (gs_font *)font, pwidths, NULL);
  576. if (code < 0)
  577. return code;
  578. if (font->WMode != 0 && code > 0 && !pwidths->replaced_v) {
  579. /*
  580. * The font has no Metrics2, so it must write
  581. * horizontally due to PS spec.
  582. * Therefore we need to fill the Widths array,
  583. * which is required by PDF spec.
  584. * Take it from WMode==0.
  585. */
  586. code = pdf_glyph_widths(pdfont, 0, glyph, (gs_font *)font, pwidths, NULL);
  587. }
  588. if (pwidths->replaced_v) {
  589. pdfont->u.simple.v[ch].x = pwidths->real_width.v.x - pwidths->Width.v.x;
  590. pdfont->u.simple.v[ch].y = pwidths->real_width.v.y - pwidths->Width.v.y;
  591. } else
  592. pdfont->u.simple.v[ch].x = pdfont->u.simple.v[ch].y = 0;
  593. if (code == 0) {
  594. pdfont->Widths[ch] = pwidths->Width.w;
  595. real_widths[ch] = pwidths->real_width.w;
  596. }
  597. } else {
  598. if (font->FontType == ft_user_defined) {
  599. if (!(pdfont->used[ch >> 3] & 0x80 >> (ch & 7)))
  600. return gs_error_undefined; /* The charproc was not accumulated. */
  601. if (!pdev->charproc_just_accumulated &&
  602. !(pdfont->u.simple.s.type3.cached[ch >> 3] & 0x80 >> (ch & 7))) {
  603. /* The charproc uses setcharwidth.
  604. Need to accumulate again to check for a glyph variation. */
  605. return gs_error_undefined;
  606. }
  607. }
  608. pwidths->Width.w = pdfont->Widths[ch];
  609. pwidths->Width.v = pdfont->u.simple.v[ch];
  610. if (font->FontType == ft_user_defined) {
  611. pwidths->real_width.w = real_widths[ch * 2];
  612. pwidths->Width.xy.x = pwidths->Width.w;
  613. pwidths->Width.xy.y = 0;
  614. pwidths->real_width.xy.x = real_widths[ch * 2 + 0];
  615. pwidths->real_width.xy.y = real_widths[ch * 2 + 1];
  616. } else if (font->WMode) {
  617. pwidths->real_width.w = real_widths[ch];
  618. pwidths->Width.xy.x = 0;
  619. pwidths->Width.xy.y = pwidths->Width.w;
  620. pwidths->real_width.xy.x = 0;
  621. pwidths->real_width.xy.y = pwidths->real_width.w;
  622. } else {
  623. pwidths->real_width.w = real_widths[ch];
  624. pwidths->Width.xy.x = pwidths->Width.w;
  625. pwidths->Width.xy.y = 0;
  626. pwidths->real_width.xy.x = pwidths->real_width.w;
  627. pwidths->real_width.xy.y = 0;
  628. }
  629. code = 0;
  630. }
  631. return code;
  632. }
  633. /*
  634. * Compute the total text width (in user space). Return 1 if any
  635. * character had real_width != Width, otherwise 0.
  636. */
  637. private int
  638. process_text_return_width(const pdf_text_enum_t *pte, gs_font_base *font,
  639. pdf_text_process_state_t *ppts,
  640. const gs_const_string *pstr,
  641. gs_point *pdpt, int *accepted)
  642. {
  643. int i;
  644. gs_point w;
  645. double scale;
  646. gs_point dpt;
  647. int num_spaces = 0;
  648. int space_char =
  649. (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH ?
  650. pte->text.space.s_char : -1);
  651. int widths_differ = 0, code;
  652. gx_device_pdf *pdev = (gx_device_pdf *)pte->dev;
  653. pdf_font_resource_t *pdfont;
  654. code = pdf_attached_font_resource(pdev, (gs_font *)font, &pdfont, NULL, NULL, NULL, NULL);
  655. if (code < 0)
  656. return code;
  657. if (font->FontType == ft_user_defined) {
  658. pdf_font3_scale(pdev, (gs_font *)font, &scale);
  659. scale *= ppts->values.size;
  660. } else
  661. scale = 0.001 * ppts->values.size;
  662. for (i = 0, w.x = w.y = 0; i < pstr->size; ++i) {
  663. pdf_glyph_widths_t cw;
  664. gs_char ch = pstr->data[i];
  665. if (font->FontType == ft_user_defined &&
  666. (i > 0 || !pdev->charproc_just_accumulated) &&
  667. !(pdfont->u.simple.s.type3.cached[ch >> 3] & (0x80 >> (ch & 7))))
  668. code = gs_error_undefined;
  669. else
  670. code = pdf_char_widths((gx_device_pdf *)pte->dev,
  671. ppts->values.pdfont, ch, font,
  672. &cw);
  673. if (code < 0) {
  674. if (i)
  675. break;
  676. *accepted = 0;
  677. return code;
  678. }
  679. w.x += cw.real_width.xy.x;
  680. w.y += cw.real_width.xy.y;
  681. if (cw.real_width.xy.x != cw.Width.xy.x ||
  682. cw.real_width.xy.y != cw.Width.xy.y
  683. )
  684. widths_differ = 1;
  685. if (pstr->data[i] == space_char)
  686. ++num_spaces;
  687. }
  688. *accepted = i;
  689. gs_distance_transform(w.x * scale, w.y * scale,
  690. &ppts->values.matrix, &dpt);
  691. if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
  692. int num_chars = *accepted;
  693. gs_point tpt;
  694. gs_distance_transform(pte->text.delta_all.x, pte->text.delta_all.y,
  695. &ctm_only(pte->pis), &tpt);
  696. dpt.x += tpt.x * num_chars;
  697. dpt.y += tpt.y * num_chars;
  698. }
  699. if (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
  700. gs_point tpt;
  701. gs_distance_transform(pte->text.delta_space.x, pte->text.delta_space.y,
  702. &ctm_only(pte->pis), &tpt);
  703. dpt.x += tpt.x * num_spaces;
  704. dpt.y += tpt.y * num_spaces;
  705. }
  706. *pdpt = dpt;
  707. return widths_differ;
  708. }
  709. #define RIGHT_SBW 1 /* Old code = 0, new code = 1. */
  710. #if !RIGHT_SBW
  711. /*
  712. * Retrieve glyph origing shift for WMode = 1 in design units.
  713. */
  714. private void
  715. pdf_glyph_origin(pdf_font_resource_t *pdfont, int ch, int WMode, gs_point *p)
  716. {
  717. /* For CID fonts PDF viewers provide glyph origin shift automatically.
  718. * Therefore we only need to do for non-CID fonts.
  719. */
  720. switch (pdfont->FontType) {
  721. case ft_encrypted:
  722. case ft_encrypted2:
  723. case ft_TrueType:
  724. case ft_user_defined:
  725. *p = pdfont->u.simple.v[ch];
  726. break;
  727. default:
  728. p->x = p->y = 0;
  729. break;
  730. }
  731. }
  732. #endif
  733. /*
  734. * Emulate TEXT_ADD_TO_ALL_WIDTHS and/or TEXT_ADD_TO_SPACE_WIDTH,
  735. * and implement TEXT_REPLACE_WIDTHS if requested.
  736. * Uses and updates ppts->values.matrix; uses ppts->values.pdfont.
  737. *
  738. * Destroys the text parameters in *pte.
  739. * The caller must restore them.
  740. */
  741. int
  742. process_text_modify_width(pdf_text_enum_t *pte, gs_font *font,
  743. pdf_text_process_state_t *ppts,
  744. const gs_const_string *pstr,
  745. gs_point *pdpt)
  746. {
  747. gx_device_pdf *const pdev = (gx_device_pdf *)pte->dev;
  748. double scale;
  749. int space_char =
  750. (pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH ?
  751. pte->text.space.s_char : -1);
  752. int code = 0;
  753. gs_point start, total;
  754. bool composite = (ppts->values.pdfont->FontType == ft_composite);
  755. if (font->FontType == ft_user_defined) {
  756. gx_device_pdf *pdev = (gx_device_pdf *)pte->dev;
  757. pdf_font3_scale(pdev, font, &scale);
  758. scale *= ppts->values.size;
  759. } else
  760. scale = 0.001 * ppts->values.size;
  761. pte->text.data.bytes = pstr->data;
  762. pte->text.size = pstr->size;
  763. pte->index = 0;
  764. pte->text.operation &= ~TEXT_FROM_ANY;
  765. pte->text.operation |= TEXT_FROM_STRING;
  766. start.x = ppts->values.matrix.tx;
  767. start.y = ppts->values.matrix.ty;
  768. total.x = total.y = 0; /* user space */
  769. /*
  770. * Note that character widths are in design space, but text.delta_*
  771. * values and the width value returned in *pdpt are in user space,
  772. * and the width values for pdf_append_chars are in device space.
  773. */
  774. for (;;) {
  775. pdf_glyph_widths_t cw; /* design space */
  776. gs_point did, wanted, tpt; /* user space */
  777. gs_point v = {0, 0}; /* design space */
  778. gs_char chr;
  779. gs_glyph glyph;
  780. int code, index = pte->index;
  781. gs_text_enum_t pte1 = *(gs_text_enum_t *)pte;
  782. int FontType;
  783. #if RIGHT_SBW
  784. bool use_cached_v = true;
  785. #endif
  786. code = pte1.orig_font->procs.next_char_glyph(&pte1, &chr, &glyph);
  787. if (code == 2) { /* end of string */
  788. gs_text_enum_copy_dynamic((gs_text_enum_t *)pte, &pte1, true);
  789. break;
  790. }
  791. if (code < 0)
  792. return code;
  793. if (composite) { /* from process_cmap_text */
  794. gs_font *subfont = pte1.fstack.items[pte1.fstack.depth].font;
  795. pdf_font_resource_t *pdsubf = ppts->values.pdfont->u.type0.DescendantFont;
  796. FontType = pdsubf->FontType;
  797. code = pdf_glyph_widths(pdsubf, font->WMode, glyph, subfont, &cw,
  798. pte->cdevproc_callout ? pte->cdevproc_result : NULL);
  799. } else {/* must be a base font */
  800. FontType = font->FontType;
  801. if (chr == GS_NO_CHAR && glyph != GS_NO_GLYPH) {
  802. /* glyphshow, we have no char code. Bug 686988.*/
  803. code = pdf_glyph_widths(ppts->values.pdfont, font->WMode, glyph, font, &cw, NULL);
  804. use_cached_v = false; /* Since we have no chr and don't call pdf_char_widths. */
  805. } else
  806. code = pdf_char_widths((gx_device_pdf *)pte->dev,
  807. ppts->values.pdfont, chr, (gs_font_base *)font,
  808. &cw);
  809. }
  810. if (code < 0) {
  811. if (index > 0)
  812. break;
  813. return code;
  814. }
  815. gs_text_enum_copy_dynamic((gs_text_enum_t *)pte, &pte1, true);
  816. #if RIGHT_SBW
  817. if (composite || !use_cached_v) {
  818. if (cw.replaced_v) {
  819. v.x = cw.real_width.v.x - cw.Width.v.x;
  820. v.y = cw.real_width.v.y - cw.Width.v.y;
  821. }
  822. } else
  823. v = ppts->values.pdfont->u.simple.v[chr];
  824. if (font->WMode) {
  825. /* With WMode 1 v-vector is (WMode 1 origin) - (WMode 0 origin).
  826. The glyph shifts in the opposite direction. */
  827. v.x = - v.x;
  828. v.y = - v.y;
  829. } else {
  830. /* With WMode 0 v-vector is (Metrics sb) - (native sb).
  831. The glyph shifts in same direction. */
  832. }
  833. /* pdf_glyph_origin is not longer used. */
  834. #else
  835. if ((pte->text.operation & TEXT_FROM_SINGLE_GLYPH) ||
  836. (pte->text.operation & TEXT_FROM_GLYPHS)) {
  837. v.x = v.y = 0;
  838. } else if (composite) {
  839. if (cw.replaced_v) {
  840. v.x = cw.real_width.v.x - cw.Width.v.x;
  841. v.y = cw.real_width.v.y - cw.Width.v.y;
  842. }
  843. } else
  844. pdf_glyph_origin(ppts->values.pdfont, chr, font->WMode, &v);
  845. #endif
  846. if (v.x != 0 || v.y != 0) {
  847. gs_point glyph_origin_shift;
  848. double scale0;
  849. if (FontType == ft_TrueType || FontType == ft_CID_TrueType)
  850. scale0 = (float)0.001;
  851. else
  852. scale0 = 1;
  853. #if RIGHT_SBW
  854. glyph_origin_shift.x = v.x * scale0;
  855. glyph_origin_shift.y = v.y * scale0;
  856. #else
  857. glyph_origin_shift.x = - v.x * scale0;
  858. glyph_origin_shift.y = - v.y * scale0;
  859. #endif
  860. if (composite) {
  861. gs_font *subfont = pte->fstack.items[pte->fstack.depth].font;
  862. gs_distance_transform(glyph_origin_shift.x, glyph_origin_shift.y,
  863. &subfont->FontMatrix, &glyph_origin_shift);
  864. }
  865. gs_distance_transform(glyph_origin_shift.x, glyph_origin_shift.y,
  866. &font->FontMatrix, &glyph_origin_shift);
  867. gs_distance_transform(glyph_origin_shift.x, glyph_origin_shift.y,
  868. &ctm_only(pte->pis), &glyph_origin_shift);
  869. if (glyph_origin_shift.x != 0 || glyph_origin_shift.y != 0) {
  870. ppts->values.matrix.tx = start.x + total.x + glyph_origin_shift.x;
  871. ppts->values.matrix.ty = start.y + total.y + glyph_origin_shift.y;
  872. code = pdf_set_text_state_values(pdev, &ppts->values);
  873. if (code < 0)
  874. break;
  875. }
  876. }
  877. if (pte->text.operation & TEXT_DO_DRAW) {
  878. gs_distance_transform(cw.Width.xy.x * scale,
  879. cw.Width.xy.y * scale,
  880. &ppts->values.matrix, &did);
  881. gs_distance_transform((font->WMode ? 0 : ppts->values.character_spacing),
  882. (font->WMode ? ppts->values.character_spacing : 0),
  883. &ppts->values.matrix, &tpt);
  884. did.x += tpt.x;
  885. did.y += tpt.y;
  886. if (chr == space_char) {
  887. gs_distance_transform((font->WMode ? 0 : ppts->values.word_spacing),
  888. (font->WMode ? ppts->values.word_spacing : 0),
  889. &ppts->values.matrix, &tpt);
  890. did.x += tpt.x;
  891. did.y += tpt.y;
  892. }
  893. code = pdf_append_chars(pdev, pstr->data + index, pte->index - index, did.x, did.y, composite);
  894. if (code < 0)
  895. break;
  896. } else
  897. did.x = did.y = 0;
  898. if (pte->text.operation & TEXT_REPLACE_WIDTHS) {
  899. gs_point dpt;
  900. code = gs_text_replaced_width(&pte->text, pte->xy_index++, &dpt);
  901. if (code < 0)
  902. return_error(gs_error_unregistered);
  903. gs_distance_transform(dpt.x, dpt.y, &ctm_only(pte->pis), &wanted);
  904. } else {
  905. gs_distance_transform(cw.real_width.xy.x * scale,
  906. cw.real_width.xy.y * scale,
  907. &ppts->values.matrix, &wanted);
  908. if (pte->text.operation & TEXT_ADD_TO_ALL_WIDTHS) {
  909. gs_distance_transform(pte->text.delta_all.x,
  910. pte->text.delta_all.y,
  911. &ctm_only(pte->pis), &tpt);
  912. wanted.x += tpt.x;
  913. wanted.y += tpt.y;
  914. }
  915. if (chr == space_char && pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH) {
  916. gs_distance_transform(pte->text.delta_space.x,
  917. pte->text.delta_space.y,
  918. &ctm_only(pte->pis), &tpt);
  919. wanted.x += tpt.x;
  920. wanted.y += tpt.y;
  921. }
  922. }
  923. total.x += wanted.x;
  924. total.y += wanted.y;
  925. if (wanted.x != did.x || wanted.y != did.y) {
  926. ppts->values.matrix.tx = start.x + total.x;
  927. ppts->values.matrix.ty = start.y + total.y;
  928. code = pdf_set_text_state_values(pdev, &ppts->values);
  929. if (code < 0)
  930. break;
  931. }
  932. pdev->charproc_just_accumulated = false;
  933. }
  934. *pdpt = total;
  935. return code;
  936. }
  937. /*
  938. * Get character code from a glyph code.
  939. * An usage of this function is very undesirable,
  940. * because a glyph may be unlisted in Encoding.
  941. */
  942. int
  943. pdf_encode_glyph(gs_font_base *bfont, gs_glyph glyph0,
  944. byte *buf, int buf_size, int *char_code_length)
  945. {
  946. gs_char c;
  947. *char_code_length = 1;
  948. if (*char_code_length > buf_size)
  949. return_error(gs_error_rangecheck); /* Must not happen. */
  950. for (c = 0; c < 255; c++) {
  951. gs_glyph glyph1 = bfont->procs.encode_char((gs_font *)bfont, c,
  952. GLYPH_SPACE_NAME);
  953. if (glyph1 == glyph0) {
  954. buf[0] = (byte)c;
  955. return 0;
  956. }
  957. }
  958. return_error(gs_error_rangecheck); /* Can't encode. */
  959. }
  960. /* ---------------- Type 1 or TrueType font ---------------- */
  961. /*
  962. * Process a text string in a simple font.
  963. */
  964. int
  965. process_plain_text(gs_text_enum_t *pte, void *vbuf, uint bsize)
  966. {
  967. byte *const buf = vbuf;
  968. uint count;
  969. uint operation = pte->text.operation;
  970. pdf_text_enum_t *penum = (pdf_text_enum_t *)pte;
  971. int code;
  972. gs_string str;
  973. pdf_text_process_state_t text_state;
  974. const gs_glyph *gdata = NULL;
  975. if (operation & (TEXT_FROM_STRING | TEXT_FROM_BYTES)) {
  976. count = pte->text.size - pte->index;
  977. if (bsize < count)
  978. return_error(gs_error_unregistered); /* Must not happen. */
  979. memcpy(buf, (const byte *)pte->text.data.bytes + pte->index, count);
  980. } else if (operation & (TEXT_FROM_CHARS | TEXT_FROM_SINGLE_CHAR)) {
  981. /* Check that all chars fit in a single byte. */
  982. const gs_char *cdata;
  983. int i;
  984. if (operation & TEXT_FROM_CHARS) {
  985. cdata = pte->text.data.chars;
  986. count = (pte->text.size - pte->index);
  987. } else {
  988. cdata = &pte->text.data.d_char;
  989. count = 1;
  990. }
  991. if (bsize < count * sizeof(gs_char))
  992. return_error(gs_error_unregistered); /* Must not happen. */
  993. for (i = 0; i < count; ++i) {
  994. gs_char chr = cdata[pte->index + i];
  995. if (chr & ~0xff)
  996. return_error(gs_error_rangecheck);
  997. buf[i] = (byte)chr;
  998. }
  999. } else if (operation & (TEXT_FROM_GLYPHS | TEXT_FROM_SINGLE_GLYPH)) {
  1000. /*
  1001. * Since PDF has no analogue of 'glyphshow',
  1002. * we try to encode glyphs with the current
  1003. * font's encoding. If the current font has no encoding,
  1004. * or the encoding doesn't contain necessary glyphs,
  1005. * the text will be represented with a Type 3 font with
  1006. * bitmaps or outlines.
  1007. *
  1008. * When we fail with encoding (136-01.ps is an example),
  1009. * we could locate a PDF font resource or create a new one
  1010. * with same outlines and an appropriate encoding.
  1011. * Also we could change .notdef entries in the
  1012. * copied font (assuming that document designer didn't use
  1013. * .notdef for a meanful printing).
  1014. * fixme: Not implemented yet.
  1015. */
  1016. gs_font *font = pte->current_font;
  1017. uint size;
  1018. int i;
  1019. if (operation & TEXT_FROM_GLYPHS) {
  1020. gdata = pte->text.data.glyphs;
  1021. size = pte->text.size - pte->index;
  1022. } else {
  1023. gdata = &pte->text.data.d_glyph;
  1024. size = 1;
  1025. }
  1026. if (!pdf_is_simple_font(font))
  1027. return_error(gs_error_unregistered); /* Must not happen. */
  1028. count = 0;
  1029. for (i = 0; i < size; ++i) {
  1030. gs_glyph glyph = gdata[pte->index + i];
  1031. int char_code_length;
  1032. code = pdf_encode_glyph((gs_font_base *)font, glyph,
  1033. buf + count, size - count, &char_code_length);
  1034. if (code < 0)
  1035. break;
  1036. count += char_code_length;
  1037. if (operation & TEXT_INTERVENE)
  1038. break; /* Just do one character. */
  1039. }
  1040. if (i < size) {
  1041. pdf_font_resource_t *pdfont;
  1042. str.data = buf;
  1043. str.size = size;
  1044. if (pdf_obtain_font_resource_unencoded(penum, &str, &pdfont, gdata) != 0) {
  1045. /*
  1046. * pdf_text_process will fall back
  1047. * to default implementation.
  1048. */
  1049. return code;
  1050. }
  1051. count = size;
  1052. }
  1053. /* So far we will use TEXT_FROM_STRING instead
  1054. TEXT_FROM_*_GLYPH*. Since we used a single
  1055. byte encoding, the character index appears invariant
  1056. during this substitution.
  1057. */
  1058. } else
  1059. return_error(gs_error_rangecheck);
  1060. str.data = buf;
  1061. if (count > 1 && (operation & TEXT_INTERVENE)) {
  1062. /* Just do one character. */
  1063. str.size = 1;
  1064. code = pdf_encode_process_string(penum, &str, gdata, NULL, &text_state);
  1065. if (code >= 0) {
  1066. pte->returned.current_char = buf[0];
  1067. code = TEXT_PROCESS_INTERVENE;
  1068. }
  1069. } else {
  1070. str.size = count;
  1071. code = pdf_encode_process_string(penum, &str, gdata, NULL, &text_state);
  1072. }
  1073. return code;
  1074. }