pdf-device.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. #include <ft2build.h>
  25. #include FT_FREETYPE_H
  26. #include FT_ADVANCES_H
  27. #define ALLOWED_TEXT_POS_ERROR (0.001f)
  28. #define ENC_IDENTITY 0
  29. #define ENC_UNICODE 1
  30. typedef struct pdf_device pdf_device;
  31. typedef struct
  32. {
  33. /* The first few entries aren't really graphics state things, but
  34. * they are recorded here as they are fundamentally intertwined with
  35. * the push/pulling of the gstates. */
  36. fz_buffer *buf;
  37. void (*on_pop)(fz_context*,pdf_device*,void *);
  38. void *on_pop_arg;
  39. /* The graphics state proper */
  40. fz_matrix ctm;
  41. fz_colorspace *colorspace[2];
  42. float color[2][4];
  43. float alpha[2];
  44. fz_stroke_state *stroke_state;
  45. int font;
  46. float font_size;
  47. int text_rendering_mode;
  48. int knockout;
  49. } gstate;
  50. /* The image digest information, object reference, as well as indirect reference
  51. * ID are all stored in doc->resources->image, and so they are maintained
  52. * through the life of the document not just this page level device. As we
  53. * encounter images on a page, we will add to the hash table if they are not
  54. * already present. When we have an image on a particular page, the resource
  55. * dict will be updated with the proper indirect reference across the document.
  56. * We do need to maintain some information as to what image resources we have
  57. * already specified for this page which is the purpose of image_indices
  58. */
  59. typedef struct
  60. {
  61. float alpha;
  62. int stroke;
  63. } alpha_entry;
  64. typedef struct
  65. {
  66. int alpha;
  67. int isolated;
  68. int knockout;
  69. fz_colorspace *colorspace;
  70. pdf_obj *ref;
  71. } group_entry;
  72. struct pdf_device
  73. {
  74. fz_device super;
  75. pdf_document *doc;
  76. pdf_obj *resources;
  77. int in_text;
  78. int num_forms;
  79. int num_smasks;
  80. int num_gstates;
  81. int max_gstates;
  82. gstate *gstates;
  83. int num_imgs;
  84. int max_imgs;
  85. int *image_indices;
  86. int num_cid_fonts;
  87. int max_cid_fonts;
  88. fz_font **cid_fonts;
  89. int *cid_fonts_enc;
  90. int num_alphas;
  91. int max_alphas;
  92. alpha_entry *alphas;
  93. int num_groups;
  94. int max_groups;
  95. group_entry *groups;
  96. };
  97. #define CURRENT_GSTATE(pdev) (&(pdev)->gstates[(pdev)->num_gstates-1])
  98. /* Helper functions */
  99. static void
  100. pdf_dev_stroke_state(fz_context *ctx, pdf_device *pdev, const fz_stroke_state *stroke_state)
  101. {
  102. gstate *gs = CURRENT_GSTATE(pdev);
  103. if (stroke_state == gs->stroke_state)
  104. return;
  105. if (gs->stroke_state && !memcmp(stroke_state, gs->stroke_state, sizeof(*stroke_state)))
  106. return;
  107. if (!gs->stroke_state || gs->stroke_state->linewidth != stroke_state->linewidth)
  108. {
  109. fz_append_printf(ctx, gs->buf, "%g w\n", stroke_state->linewidth);
  110. }
  111. if (!gs->stroke_state || gs->stroke_state->start_cap != stroke_state->start_cap)
  112. {
  113. int cap = stroke_state->start_cap;
  114. /* FIXME: Triangle caps aren't supported in pdf */
  115. if (cap == FZ_LINECAP_TRIANGLE)
  116. cap = FZ_LINECAP_BUTT;
  117. fz_append_printf(ctx, gs->buf, "%d J\n", cap);
  118. }
  119. if (!gs->stroke_state || gs->stroke_state->linejoin != stroke_state->linejoin)
  120. {
  121. int join = stroke_state->linejoin;
  122. if (join == FZ_LINEJOIN_MITER_XPS)
  123. join = FZ_LINEJOIN_MITER;
  124. fz_append_printf(ctx, gs->buf, "%d j\n", join);
  125. }
  126. if (!gs->stroke_state || gs->stroke_state->miterlimit != stroke_state->miterlimit)
  127. {
  128. fz_append_printf(ctx, gs->buf, "%g M\n", stroke_state->miterlimit);
  129. }
  130. if (gs->stroke_state == NULL && stroke_state->dash_len == 0)
  131. {
  132. /* No stroke details. */
  133. }
  134. else if (!gs->stroke_state || gs->stroke_state->dash_phase != stroke_state->dash_phase || gs->stroke_state->dash_len != stroke_state->dash_len ||
  135. memcmp(gs->stroke_state->dash_list, stroke_state->dash_list, sizeof(float)*stroke_state->dash_len))
  136. {
  137. int i;
  138. fz_append_byte(ctx, gs->buf, '[');
  139. for (i = 0; i < stroke_state->dash_len; i++)
  140. {
  141. if (i > 0)
  142. fz_append_byte(ctx, gs->buf, ' ');
  143. fz_append_printf(ctx, gs->buf, "%g", stroke_state->dash_list[i]);
  144. }
  145. fz_append_printf(ctx, gs->buf, "]%g d\n", stroke_state->dash_phase);
  146. }
  147. fz_drop_stroke_state(ctx, gs->stroke_state);
  148. gs->stroke_state = fz_keep_stroke_state(ctx, stroke_state);
  149. }
  150. typedef struct
  151. {
  152. fz_context *ctx;
  153. fz_buffer *buf;
  154. } pdf_dev_path_arg;
  155. static void
  156. pdf_dev_path_moveto(fz_context *ctx, void *arg, float x, float y)
  157. {
  158. fz_buffer *buf = (fz_buffer *)arg;
  159. fz_append_printf(ctx, buf, "%g %g m\n", x, y);
  160. }
  161. static void
  162. pdf_dev_path_lineto(fz_context *ctx, void *arg, float x, float y)
  163. {
  164. fz_buffer *buf = (fz_buffer *)arg;
  165. fz_append_printf(ctx, buf, "%g %g l\n", x, y);
  166. }
  167. static void
  168. pdf_dev_path_curveto(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2, float x3, float y3)
  169. {
  170. fz_buffer *buf = (fz_buffer *)arg;
  171. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3);
  172. }
  173. static void
  174. pdf_dev_path_close(fz_context *ctx, void *arg)
  175. {
  176. fz_buffer *buf = (fz_buffer *)arg;
  177. fz_append_string(ctx, buf, "h\n");
  178. }
  179. static const fz_path_walker pdf_dev_path_proc =
  180. {
  181. pdf_dev_path_moveto,
  182. pdf_dev_path_lineto,
  183. pdf_dev_path_curveto,
  184. pdf_dev_path_close
  185. };
  186. static void
  187. pdf_dev_path(fz_context *ctx, pdf_device *pdev, const fz_path *path)
  188. {
  189. gstate *gs = CURRENT_GSTATE(pdev);
  190. fz_rect bounds;
  191. if (fz_path_is_rect_with_bounds(ctx, path, fz_identity, &bounds))
  192. {
  193. fz_append_printf(ctx, gs->buf, "%g %g %g %g re\n", bounds.x0, bounds.y0, bounds.x1-bounds.x0, bounds.y1-bounds.y0);
  194. return;
  195. }
  196. fz_walk_path(ctx, path, &pdf_dev_path_proc, (void *)gs->buf);
  197. }
  198. static void
  199. pdf_dev_ctm(fz_context *ctx, pdf_device *pdev, fz_matrix ctm)
  200. {
  201. fz_matrix inverse;
  202. gstate *gs = CURRENT_GSTATE(pdev);
  203. if (memcmp(&gs->ctm, &ctm, sizeof(ctm)) == 0)
  204. return;
  205. inverse = fz_invert_matrix(gs->ctm);
  206. inverse = fz_concat(ctm, inverse);
  207. gs->ctm = ctm;
  208. fz_append_printf(ctx, gs->buf, "%M cm\n", &inverse);
  209. }
  210. static void
  211. pdf_dev_color(fz_context *ctx, pdf_device *pdev, fz_colorspace *colorspace, const float *color, int stroke, fz_color_params color_params)
  212. {
  213. int diff = 0;
  214. int i;
  215. int cspace = 0;
  216. float rgb[FZ_MAX_COLORS];
  217. gstate *gs = CURRENT_GSTATE(pdev);
  218. if (colorspace == fz_device_gray(ctx))
  219. cspace = 1;
  220. else if (colorspace == fz_device_rgb(ctx))
  221. cspace = 3;
  222. else if (colorspace == fz_device_cmyk(ctx))
  223. cspace = 4;
  224. if (cspace == 0)
  225. {
  226. /* If it's an unknown colorspace, fallback to rgb */
  227. fz_convert_color(ctx, colorspace, color, fz_device_rgb(ctx), rgb, NULL, color_params);
  228. color = rgb;
  229. colorspace = fz_device_rgb(ctx);
  230. cspace = 3;
  231. }
  232. if (gs->colorspace[stroke] != colorspace)
  233. {
  234. gs->colorspace[stroke] = colorspace;
  235. diff = 1;
  236. }
  237. for (i=0; i < cspace; i++)
  238. if (gs->color[stroke][i] != color[i])
  239. {
  240. gs->color[stroke][i] = color[i];
  241. diff = 1;
  242. }
  243. if (diff == 0)
  244. return;
  245. switch (cspace + stroke*8)
  246. {
  247. case 1:
  248. fz_append_printf(ctx, gs->buf, "%g g\n", color[0]);
  249. break;
  250. case 3:
  251. fz_append_printf(ctx, gs->buf, "%g %g %g rg\n", color[0], color[1], color[2]);
  252. break;
  253. case 4:
  254. fz_append_printf(ctx, gs->buf, "%g %g %g %g k\n", color[0], color[1], color[2], color[3]);
  255. break;
  256. case 1+8:
  257. fz_append_printf(ctx, gs->buf, "%g G\n", color[0]);
  258. break;
  259. case 3+8:
  260. fz_append_printf(ctx, gs->buf, "%g %g %g RG\n", color[0], color[1], color[2]);
  261. break;
  262. case 4+8:
  263. fz_append_printf(ctx, gs->buf, "%g %g %g %g K\n", color[0], color[1], color[2], color[3]);
  264. break;
  265. }
  266. }
  267. static void
  268. pdf_dev_alpha(fz_context *ctx, pdf_device *pdev, float alpha, int stroke)
  269. {
  270. int i;
  271. pdf_document *doc = pdev->doc;
  272. gstate *gs = CURRENT_GSTATE(pdev);
  273. /* If the alpha is unchanged, nothing to do */
  274. if (gs->alpha[stroke] == alpha)
  275. return;
  276. gs->alpha[stroke] = alpha;
  277. /* Have we sent such an alpha before? */
  278. for (i = 0; i < pdev->num_alphas; i++)
  279. if (pdev->alphas[i].alpha == alpha && pdev->alphas[i].stroke == stroke)
  280. break;
  281. if (i == pdev->num_alphas)
  282. {
  283. pdf_obj *o, *ref;
  284. /* No. Need to make a new one */
  285. if (pdev->num_alphas == pdev->max_alphas)
  286. {
  287. int newmax = pdev->max_alphas * 2;
  288. if (newmax == 0)
  289. newmax = 4;
  290. pdev->alphas = fz_realloc_array(ctx, pdev->alphas, newmax, alpha_entry);
  291. pdev->max_alphas = newmax;
  292. }
  293. pdev->alphas[i].alpha = alpha;
  294. pdev->alphas[i].stroke = stroke;
  295. o = pdf_new_dict(ctx, doc, 1);
  296. fz_try(ctx)
  297. {
  298. char text[32];
  299. pdf_dict_put_real(ctx, o, (stroke ? PDF_NAME(CA) : PDF_NAME(ca)), alpha);
  300. fz_snprintf(text, sizeof(text), "ExtGState/Alp%d", i);
  301. ref = pdf_add_object(ctx, doc, o);
  302. pdf_dict_putp_drop(ctx, pdev->resources, text, ref);
  303. }
  304. fz_always(ctx)
  305. {
  306. pdf_drop_obj(ctx, o);
  307. }
  308. fz_catch(ctx)
  309. {
  310. fz_rethrow(ctx);
  311. }
  312. pdev->num_alphas++;
  313. }
  314. fz_append_printf(ctx, gs->buf, "/Alp%d gs\n", i);
  315. }
  316. static int
  317. pdf_dev_find_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font)
  318. {
  319. int k;
  320. /* Check if we already had this one */
  321. for (k = 0; k < pdev->num_cid_fonts; k++)
  322. if (pdev->cid_fonts[k] == font)
  323. return k;
  324. return -1;
  325. }
  326. static int
  327. pdf_dev_add_font_res_imp(fz_context *ctx, pdf_device *pdev, fz_font *font, pdf_obj *fres, int enc)
  328. {
  329. char text[32];
  330. int num;
  331. /* Not there so add to resources */
  332. fz_snprintf(text, sizeof(text), "Font/F%d", pdev->num_cid_fonts);
  333. pdf_dict_putp_drop(ctx, pdev->resources, text, fres);
  334. /* And add index to our list for this page */
  335. if (pdev->num_cid_fonts == pdev->max_cid_fonts)
  336. {
  337. int newmax = pdev->max_cid_fonts * 2;
  338. if (newmax == 0)
  339. newmax = 4;
  340. pdev->cid_fonts = fz_realloc_array(ctx, pdev->cid_fonts, newmax, fz_font*);
  341. pdev->cid_fonts_enc = fz_realloc_array(ctx, pdev->cid_fonts_enc, newmax, int);
  342. pdev->max_cid_fonts = newmax;
  343. }
  344. num = pdev->num_cid_fonts++;
  345. pdev->cid_fonts[num] = fz_keep_font(ctx, font);
  346. pdev->cid_fonts_enc[num] = enc;
  347. return num;
  348. }
  349. static int
  350. pdf_dev_add_substitute_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font)
  351. {
  352. pdf_obj *fres;
  353. int k;
  354. /* Check if we already had this one */
  355. k = pdf_dev_find_font_res(ctx, pdev, font);
  356. if (k >= 0)
  357. return k;
  358. /* This will add it to the xref if needed */
  359. if (font->flags.cjk)
  360. fres = pdf_add_cjk_font(ctx, pdev->doc, font, font->flags.cjk_lang, 0, font->flags.is_serif);
  361. else
  362. fres = pdf_add_substitute_font(ctx, pdev->doc, font);
  363. /* And add to the resource dictionary. */
  364. return pdf_dev_add_font_res_imp(ctx, pdev, font, fres, ENC_UNICODE);
  365. }
  366. static int
  367. pdf_dev_add_embedded_font_res(fz_context *ctx, pdf_device *pdev, fz_font *font)
  368. {
  369. pdf_obj *fres;
  370. int k;
  371. /* Check if we already had this one */
  372. k = pdf_dev_find_font_res(ctx, pdev, font);
  373. if (k >= 0)
  374. return k;
  375. /* This will add it to the xref if needed */
  376. fres = pdf_add_cid_font(ctx, pdev->doc, font);
  377. /* And add to the resource dictionary. */
  378. return pdf_dev_add_font_res_imp(ctx, pdev, font, fres, ENC_IDENTITY);
  379. }
  380. static void
  381. pdf_dev_font(fz_context *ctx, pdf_device *pdev, fz_font *font, fz_matrix trm)
  382. {
  383. gstate *gs = CURRENT_GSTATE(pdev);
  384. float font_size = fz_matrix_expansion(trm);
  385. /* If the font is unchanged, nothing to do */
  386. if (gs->font >= 0 && pdev->cid_fonts[gs->font] == font && gs->font_size == font_size)
  387. return;
  388. // TODO: vertical wmode
  389. if (fz_font_t3_procs(ctx, font))
  390. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "pdf device does not support type 3 fonts");
  391. if (fz_font_flags(font)->ft_substitute || !pdf_font_writing_supported(ctx, font))
  392. gs->font = pdf_dev_add_substitute_font_res(ctx, pdev, font);
  393. else
  394. gs->font = pdf_dev_add_embedded_font_res(ctx, pdev, font);
  395. gs->font_size = font_size;
  396. fz_append_printf(ctx, gs->buf, "/F%d %g Tf\n", gs->font, gs->font_size);
  397. }
  398. static void
  399. pdf_dev_push_new_buf(fz_context *ctx, pdf_device *pdev, fz_buffer *buf, void (*on_pop)(fz_context*,pdf_device*,void*), void *on_pop_arg)
  400. {
  401. if (pdev->num_gstates == pdev->max_gstates)
  402. {
  403. int newmax = pdev->max_gstates*2;
  404. pdev->gstates = fz_realloc_array(ctx, pdev->gstates, newmax, gstate);
  405. pdev->max_gstates = newmax;
  406. }
  407. memcpy(&pdev->gstates[pdev->num_gstates], &pdev->gstates[pdev->num_gstates-1], sizeof(*pdev->gstates));
  408. fz_keep_stroke_state(ctx, pdev->gstates[pdev->num_gstates].stroke_state);
  409. if (buf)
  410. pdev->gstates[pdev->num_gstates].buf = buf;
  411. else
  412. fz_keep_buffer(ctx, pdev->gstates[pdev->num_gstates].buf);
  413. pdev->gstates[pdev->num_gstates].on_pop = on_pop;
  414. pdev->gstates[pdev->num_gstates].on_pop_arg = on_pop_arg;
  415. fz_append_string(ctx, pdev->gstates[pdev->num_gstates].buf, "q\n");
  416. pdev->num_gstates++;
  417. }
  418. static void
  419. pdf_dev_push(fz_context *ctx, pdf_device *pdev)
  420. {
  421. pdf_dev_push_new_buf(ctx, pdev, NULL, NULL, NULL);
  422. }
  423. static void *
  424. pdf_dev_pop(fz_context *ctx, pdf_device *pdev)
  425. {
  426. gstate *gs = CURRENT_GSTATE(pdev);
  427. void *arg = gs->on_pop_arg;
  428. fz_append_string(ctx, gs->buf, "Q\n");
  429. if (gs->on_pop)
  430. gs->on_pop(ctx, pdev, arg);
  431. pdev->num_gstates--;
  432. fz_drop_stroke_state(ctx, pdev->gstates[pdev->num_gstates].stroke_state);
  433. fz_drop_buffer(ctx, pdev->gstates[pdev->num_gstates].buf);
  434. return arg;
  435. }
  436. static void
  437. pdf_dev_text_span(fz_context *ctx, pdf_device *pdev, fz_text_span *span)
  438. {
  439. gstate *gs = CURRENT_GSTATE(pdev);
  440. fz_matrix trm, tm, tlm, inv_trm, inv_tm;
  441. fz_matrix inv_tfs;
  442. fz_point d;
  443. float adv;
  444. int enc;
  445. int dx, dy;
  446. int i;
  447. if (span->len == 0)
  448. return;
  449. inv_tfs = fz_scale(1 / gs->font_size, 1 / gs->font_size);
  450. trm = span->trm;
  451. trm.e = span->items[0].x;
  452. trm.f = span->items[0].y;
  453. tm = fz_concat(inv_tfs, trm);
  454. tlm = tm;
  455. inv_tm = fz_invert_matrix(tm);
  456. inv_trm = fz_invert_matrix(trm);
  457. enc = pdev->cid_fonts_enc[gs->font];
  458. fz_append_printf(ctx, gs->buf, "%M Tm\n[<", &tm);
  459. for (i = 0; i < span->len; ++i)
  460. {
  461. fz_text_item *it = &span->items[i];
  462. if (enc == ENC_IDENTITY && it->gid < 0)
  463. continue;
  464. if (enc == ENC_UNICODE && it->ucs < 0)
  465. continue;
  466. /* transform difference from expected pen position into font units. */
  467. d.x = it->x - trm.e;
  468. d.y = it->y - trm.f;
  469. d = fz_transform_vector(d, inv_trm);
  470. dx = (int)(d.x * 1000 + (d.x < 0 ? -0.5f : 0.5f));
  471. dy = (int)(d.y * 1000 + (d.y < 0 ? -0.5f : 0.5f));
  472. trm.e = it->x;
  473. trm.f = it->y;
  474. if (dx != 0 || dy != 0)
  475. {
  476. if (span->wmode == 0 && dy == 0)
  477. fz_append_printf(ctx, gs->buf, ">%d<", -dx);
  478. else if (span->wmode == 1 && dx == 0)
  479. fz_append_printf(ctx, gs->buf, ">%d<", -dy);
  480. else
  481. {
  482. /* Calculate offset from start of the previous line */
  483. tm = fz_concat(inv_tfs, trm);
  484. d.x = tm.e - tlm.e;
  485. d.y = tm.f - tlm.f;
  486. d = fz_transform_vector(d, inv_tm);
  487. fz_append_printf(ctx, gs->buf, ">]TJ\n%g %g Td\n[<", d.x, d.y);
  488. tlm = tm;
  489. }
  490. }
  491. if (fz_font_t3_procs(ctx, span->font))
  492. fz_append_printf(ctx, gs->buf, "%02x", it->gid);
  493. else if (enc == ENC_IDENTITY)
  494. fz_append_printf(ctx, gs->buf, "%04x", it->gid);
  495. else if (enc == ENC_UNICODE)
  496. fz_append_printf(ctx, gs->buf, "%04x", it->ucs);
  497. if (it->gid != -1)
  498. {
  499. adv = fz_advance_glyph(ctx, span->font, it->gid, span->wmode);
  500. if (span->wmode == 0)
  501. trm = fz_pre_translate(trm, adv, 0);
  502. else
  503. trm = fz_pre_translate(trm, 0, adv);
  504. }
  505. }
  506. fz_append_string(ctx, gs->buf, ">]TJ\n");
  507. }
  508. static void
  509. pdf_dev_trm(fz_context *ctx, pdf_device *pdev, int trm)
  510. {
  511. gstate *gs = CURRENT_GSTATE(pdev);
  512. if (gs->text_rendering_mode == trm)
  513. return;
  514. gs->text_rendering_mode = trm;
  515. fz_append_printf(ctx, gs->buf, "%d Tr\n", trm);
  516. }
  517. static void
  518. pdf_dev_begin_text(fz_context *ctx, pdf_device *pdev, int trm)
  519. {
  520. pdf_dev_trm(ctx, pdev, trm);
  521. if (!pdev->in_text)
  522. {
  523. gstate *gs = CURRENT_GSTATE(pdev);
  524. fz_append_string(ctx, gs->buf, "BT\n");
  525. pdev->in_text = 1;
  526. }
  527. }
  528. static void
  529. pdf_dev_end_text(fz_context *ctx, pdf_device *pdev)
  530. {
  531. gstate *gs = CURRENT_GSTATE(pdev);
  532. if (!pdev->in_text)
  533. return;
  534. pdev->in_text = 0;
  535. fz_append_string(ctx, gs->buf, "ET\n");
  536. }
  537. static int
  538. pdf_dev_new_form(fz_context *ctx, pdf_obj **form_ref, pdf_device *pdev, fz_rect bbox, int isolated, int knockout, float alpha, fz_colorspace *colorspace)
  539. {
  540. pdf_document *doc = pdev->doc;
  541. int num;
  542. pdf_obj *group_ref = NULL;
  543. pdf_obj *group;
  544. pdf_obj *form;
  545. *form_ref = NULL;
  546. /* Find (or make) a new group with the required options. */
  547. for(num = 0; num < pdev->num_groups; num++)
  548. {
  549. group_entry *g = &pdev->groups[num];
  550. if (g->isolated == isolated && g->knockout == knockout && g->alpha == alpha && g->colorspace == colorspace)
  551. {
  552. group_ref = pdev->groups[num].ref;
  553. break;
  554. }
  555. }
  556. /* If we didn't find one, make one */
  557. if (num == pdev->num_groups)
  558. {
  559. if (pdev->num_groups == pdev->max_groups)
  560. {
  561. int newmax = pdev->max_groups * 2;
  562. if (newmax == 0)
  563. newmax = 4;
  564. pdev->groups = fz_realloc_array(ctx, pdev->groups, newmax, group_entry);
  565. pdev->max_groups = newmax;
  566. }
  567. pdev->num_groups++;
  568. pdev->groups[num].isolated = isolated;
  569. pdev->groups[num].knockout = knockout;
  570. pdev->groups[num].alpha = alpha;
  571. pdev->groups[num].colorspace = fz_keep_colorspace(ctx, colorspace);
  572. pdev->groups[num].ref = NULL;
  573. group = pdf_new_dict(ctx, doc, 5);
  574. fz_try(ctx)
  575. {
  576. pdf_dict_put(ctx, group, PDF_NAME(Type), PDF_NAME(Group));
  577. pdf_dict_put(ctx, group, PDF_NAME(S), PDF_NAME(Transparency));
  578. pdf_dict_put_bool(ctx, group, PDF_NAME(K), knockout);
  579. pdf_dict_put_bool(ctx, group, PDF_NAME(I), isolated);
  580. switch (fz_colorspace_type(ctx, colorspace))
  581. {
  582. case FZ_COLORSPACE_GRAY:
  583. pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceGray));
  584. break;
  585. case FZ_COLORSPACE_RGB:
  586. pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceRGB));
  587. break;
  588. case FZ_COLORSPACE_CMYK:
  589. pdf_dict_put(ctx, group, PDF_NAME(CS), PDF_NAME(DeviceCMYK));
  590. break;
  591. default:
  592. break;
  593. }
  594. group_ref = pdev->groups[num].ref = pdf_add_object(ctx, doc, group);
  595. }
  596. fz_always(ctx)
  597. {
  598. pdf_drop_obj(ctx, group);
  599. }
  600. fz_catch(ctx)
  601. {
  602. fz_rethrow(ctx);
  603. }
  604. }
  605. /* Make us a new Forms object that points to that group, and change
  606. * to writing into the buffer for that Forms object. */
  607. form = pdf_new_dict(ctx, doc, 4);
  608. fz_try(ctx)
  609. {
  610. pdf_dict_put(ctx, form, PDF_NAME(Subtype), PDF_NAME(Form));
  611. pdf_dict_put(ctx, form, PDF_NAME(Group), group_ref);
  612. pdf_dict_put_int(ctx, form, PDF_NAME(FormType), 1);
  613. pdf_dict_put_rect(ctx, form, PDF_NAME(BBox), bbox);
  614. *form_ref = pdf_add_object(ctx, doc, form);
  615. }
  616. fz_always(ctx)
  617. {
  618. pdf_drop_obj(ctx, form);
  619. }
  620. fz_catch(ctx)
  621. {
  622. fz_rethrow(ctx);
  623. }
  624. /* Insert the new form object into the resources */
  625. {
  626. char text[32];
  627. num = pdev->num_forms++;
  628. fz_snprintf(text, sizeof(text), "XObject/Fm%d", num);
  629. pdf_dict_putp(ctx, pdev->resources, text, *form_ref);
  630. }
  631. return num;
  632. }
  633. /* Entry points */
  634. static void
  635. pdf_dev_fill_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm,
  636. fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
  637. {
  638. pdf_device *pdev = (pdf_device*)dev;
  639. gstate *gs = CURRENT_GSTATE(pdev);
  640. pdf_dev_end_text(ctx, pdev);
  641. pdf_dev_alpha(ctx, pdev, alpha, 0);
  642. pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params);
  643. pdf_dev_ctm(ctx, pdev, ctm);
  644. pdf_dev_path(ctx, pdev, path);
  645. fz_append_string(ctx, gs->buf, (even_odd ? "f*\n" : "f\n"));
  646. }
  647. static void
  648. pdf_dev_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm,
  649. fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
  650. {
  651. pdf_device *pdev = (pdf_device*)dev;
  652. gstate *gs = CURRENT_GSTATE(pdev);
  653. pdf_dev_end_text(ctx, pdev);
  654. pdf_dev_alpha(ctx, pdev, alpha, 1);
  655. pdf_dev_color(ctx, pdev, colorspace, color, 1, color_params);
  656. pdf_dev_ctm(ctx, pdev, ctm);
  657. pdf_dev_stroke_state(ctx, pdev, stroke);
  658. pdf_dev_path(ctx, pdev, path);
  659. fz_append_string(ctx, gs->buf, "S\n");
  660. }
  661. static void
  662. pdf_dev_clip_path(fz_context *ctx, fz_device *dev, const fz_path *path, int even_odd, fz_matrix ctm, fz_rect scissor)
  663. {
  664. pdf_device *pdev = (pdf_device*)dev;
  665. gstate *gs;
  666. pdf_dev_end_text(ctx, pdev);
  667. pdf_dev_push(ctx, pdev);
  668. pdf_dev_ctm(ctx, pdev, ctm);
  669. pdf_dev_path(ctx, pdev, path);
  670. gs = CURRENT_GSTATE(pdev);
  671. fz_append_string(ctx, gs->buf, (even_odd ? "W* n\n" : "W n\n"));
  672. }
  673. static void
  674. pdf_dev_clip_stroke_path(fz_context *ctx, fz_device *dev, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor)
  675. {
  676. pdf_device *pdev = (pdf_device*)dev;
  677. gstate *gs;
  678. pdf_dev_end_text(ctx, pdev);
  679. pdf_dev_push(ctx, pdev);
  680. /* FIXME: Need to push a group, select a pattern (or shading) here,
  681. * stroke with the pattern/shading. Then move to defining that pattern
  682. * with the next calls to the device interface until the next pop
  683. * when we pop the group. */
  684. pdf_dev_ctm(ctx, pdev, ctm);
  685. pdf_dev_path(ctx, pdev, path);
  686. gs = CURRENT_GSTATE(pdev);
  687. fz_append_string(ctx, gs->buf, "W n\n");
  688. }
  689. static void
  690. pdf_dev_fill_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm,
  691. fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
  692. {
  693. pdf_device *pdev = (pdf_device*)dev;
  694. fz_text_span *span;
  695. pdf_dev_ctm(ctx, pdev, ctm);
  696. pdf_dev_alpha(ctx, pdev, alpha, 0);
  697. pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params);
  698. for (span = text->head; span; span = span->next)
  699. {
  700. pdf_dev_begin_text(ctx, pdev, 0);
  701. pdf_dev_font(ctx, pdev, span->font, span->trm);
  702. pdf_dev_text_span(ctx, pdev, span);
  703. }
  704. }
  705. static void
  706. pdf_dev_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm,
  707. fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
  708. {
  709. pdf_device *pdev = (pdf_device*)dev;
  710. fz_text_span *span;
  711. pdf_dev_ctm(ctx, pdev, ctm);
  712. pdf_dev_alpha(ctx, pdev, alpha, 1);
  713. pdf_dev_color(ctx, pdev, colorspace, color, 1, color_params);
  714. pdf_dev_stroke_state(ctx, pdev, stroke);
  715. for (span = text->head; span; span = span->next)
  716. {
  717. pdf_dev_begin_text(ctx, pdev, 1);
  718. pdf_dev_font(ctx, pdev, span->font, span->trm);
  719. pdf_dev_text_span(ctx, pdev, span);
  720. }
  721. }
  722. static void
  723. pdf_dev_clip_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm, fz_rect scissor)
  724. {
  725. pdf_device *pdev = (pdf_device*)dev;
  726. fz_text_span *span;
  727. pdf_dev_end_text(ctx, pdev);
  728. pdf_dev_push(ctx, pdev);
  729. pdf_dev_ctm(ctx, pdev, ctm);
  730. for (span = text->head; span; span = span->next)
  731. {
  732. pdf_dev_begin_text(ctx, pdev, 7);
  733. pdf_dev_font(ctx, pdev, span->font, span->trm);
  734. pdf_dev_text_span(ctx, pdev, span);
  735. }
  736. }
  737. static void
  738. pdf_dev_clip_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor)
  739. {
  740. pdf_device *pdev = (pdf_device*)dev;
  741. fz_text_span *span;
  742. pdf_dev_end_text(ctx, pdev);
  743. pdf_dev_push(ctx, pdev);
  744. pdf_dev_ctm(ctx, pdev, ctm);
  745. for (span = text->head; span; span = span->next)
  746. {
  747. pdf_dev_begin_text(ctx, pdev, 7);
  748. pdf_dev_font(ctx, pdev, span->font, span->trm);
  749. pdf_dev_text_span(ctx, pdev, span);
  750. }
  751. }
  752. static void
  753. pdf_dev_ignore_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matrix ctm)
  754. {
  755. pdf_device *pdev = (pdf_device*)dev;
  756. fz_text_span *span;
  757. pdf_dev_ctm(ctx, pdev, ctm);
  758. for (span = text->head; span; span = span->next)
  759. {
  760. pdf_dev_begin_text(ctx, pdev, 0);
  761. pdf_dev_font(ctx, pdev, span->font, span->trm);
  762. pdf_dev_text_span(ctx, pdev, span);
  763. }
  764. }
  765. static void
  766. pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_obj *im_res)
  767. {
  768. char text[32];
  769. pdf_device *pdev = (pdf_device*)dev;
  770. int k;
  771. int num;
  772. /* Check if we already had this one */
  773. for (k = 0; k < pdev->num_imgs; k++)
  774. {
  775. if (pdev->image_indices[k] == pdf_to_num(ctx, im_res))
  776. return;
  777. }
  778. /* Not there so add to resources */
  779. fz_snprintf(text, sizeof(text), "XObject/Img%d", pdf_to_num(ctx, im_res));
  780. pdf_dict_putp(ctx, pdev->resources, text, im_res);
  781. /* And add index to our list for this page */
  782. if (pdev->num_imgs == pdev->max_imgs)
  783. {
  784. int newmax = pdev->max_imgs * 2;
  785. if (newmax == 0)
  786. newmax = 4;
  787. pdev->image_indices = fz_realloc_array(ctx, pdev->image_indices, newmax, int);
  788. pdev->max_imgs = newmax;
  789. }
  790. num = pdev->num_imgs++;
  791. pdev->image_indices[num] = pdf_to_num(ctx, im_res);
  792. }
  793. static void
  794. pdf_dev_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, float alpha, fz_color_params color_params)
  795. {
  796. pdf_device *pdev = (pdf_device*)dev;
  797. pdf_obj *im_res;
  798. gstate *gs = CURRENT_GSTATE(pdev);
  799. pdf_dev_end_text(ctx, pdev);
  800. im_res = pdf_add_image(ctx, pdev->doc, image);
  801. if (im_res == NULL)
  802. {
  803. fz_warn(ctx, "pdf_add_image: problem adding image resource");
  804. return;
  805. }
  806. fz_try(ctx)
  807. {
  808. pdf_dev_alpha(ctx, pdev, alpha, 0);
  809. /* PDF images are upside down, so fiddle the ctm */
  810. ctm = fz_pre_scale(ctm, 1, -1);
  811. ctm = fz_pre_translate(ctm, 0, -1);
  812. pdf_dev_ctm(ctx, pdev, ctm);
  813. fz_append_printf(ctx, gs->buf, "/Img%d Do\n", pdf_to_num(ctx, im_res));
  814. /* Possibly add to page resources */
  815. pdf_dev_add_image_res(ctx, dev, im_res);
  816. }
  817. fz_always(ctx)
  818. pdf_drop_obj(ctx, im_res);
  819. fz_catch(ctx)
  820. fz_rethrow(ctx);
  821. }
  822. static void
  823. pdf_dev_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha, fz_color_params color_params)
  824. {
  825. pdf_device *pdev = (pdf_device*)dev;
  826. fz_warn(ctx, "the pdf device does not support shadings; output may be incomplete");
  827. /* FIXME */
  828. pdf_dev_end_text(ctx, pdev);
  829. }
  830. static void
  831. pdf_dev_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm,
  832. fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
  833. {
  834. pdf_device *pdev = (pdf_device*)dev;
  835. pdf_obj *im_res = NULL;
  836. gstate *gs = CURRENT_GSTATE(pdev);
  837. pdf_dev_end_text(ctx, pdev);
  838. im_res = pdf_add_image(ctx, pdev->doc, image);
  839. if (im_res == NULL)
  840. {
  841. fz_warn(ctx, "pdf_add_image: problem adding image resource");
  842. return;
  843. }
  844. fz_try(ctx)
  845. {
  846. fz_append_string(ctx, gs->buf, "q\n");
  847. pdf_dev_alpha(ctx, pdev, alpha, 0);
  848. pdf_dev_color(ctx, pdev, colorspace, color, 0, color_params);
  849. /* PDF images are upside down, so fiddle the ctm */
  850. ctm = fz_pre_scale(ctm, 1, -1);
  851. ctm = fz_pre_translate(ctm, 0, -1);
  852. pdf_dev_ctm(ctx, pdev, ctm);
  853. fz_append_printf(ctx, gs->buf, "/Img%d Do Q\n", pdf_to_num(ctx, im_res));
  854. /* Possibly add to page resources */
  855. pdf_dev_add_image_res(ctx, dev, im_res);
  856. }
  857. fz_always(ctx)
  858. pdf_drop_obj(ctx, im_res);
  859. fz_catch(ctx)
  860. fz_rethrow(ctx);
  861. }
  862. static void
  863. pdf_dev_clip_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, fz_matrix ctm, fz_rect scissor)
  864. {
  865. pdf_device *pdev = (pdf_device*)dev;
  866. fz_warn(ctx, "the pdf device does not support image masks; output may be incomplete");
  867. /* FIXME */
  868. pdf_dev_end_text(ctx, pdev);
  869. pdf_dev_push(ctx, pdev);
  870. }
  871. static void
  872. pdf_dev_pop_clip(fz_context *ctx, fz_device *dev)
  873. {
  874. pdf_device *pdev = (pdf_device*)dev;
  875. /* FIXME */
  876. pdf_dev_end_text(ctx, pdev);
  877. pdf_dev_pop(ctx, pdev);
  878. }
  879. static void
  880. pdf_dev_begin_mask(fz_context *ctx, fz_device *dev, fz_rect bbox, int luminosity, fz_colorspace *colorspace, const float *color, fz_color_params color_params)
  881. {
  882. pdf_device *pdev = (pdf_device*)dev;
  883. gstate *gs;
  884. pdf_obj *smask = NULL;
  885. char egsname[32];
  886. pdf_obj *egs = NULL;
  887. pdf_obj *egss;
  888. pdf_obj *form_ref;
  889. pdf_obj *color_obj = NULL;
  890. int i, n;
  891. fz_var(smask);
  892. fz_var(egs);
  893. fz_var(color_obj);
  894. pdf_dev_end_text(ctx, pdev);
  895. pdf_dev_ctm(ctx, pdev, fz_identity);
  896. /* Make a new form to contain the contents of the softmask */
  897. pdf_dev_new_form(ctx, &form_ref, pdev, bbox, 0, 0, 1, colorspace);
  898. fz_try(ctx)
  899. {
  900. fz_snprintf(egsname, sizeof(egsname), "SM%d", pdev->num_smasks++);
  901. egss = pdf_dict_get(ctx, pdev->resources, PDF_NAME(ExtGState));
  902. if (!egss)
  903. egss = pdf_dict_put_dict(ctx, pdev->resources, PDF_NAME(ExtGState), 10);
  904. egs = pdf_dict_puts_dict(ctx, egss, egsname, 1);
  905. pdf_dict_put(ctx, egs, PDF_NAME(Type), PDF_NAME(ExtGState));
  906. smask = pdf_dict_put_dict(ctx, egs, PDF_NAME(SMask), 4);
  907. pdf_dict_put(ctx, smask, PDF_NAME(Type), PDF_NAME(Mask));
  908. pdf_dict_put(ctx, smask, PDF_NAME(S), (luminosity ? PDF_NAME(Luminosity) : PDF_NAME(Alpha)));
  909. pdf_dict_put(ctx, smask, PDF_NAME(G), form_ref);
  910. n = fz_colorspace_n(ctx, colorspace);
  911. color_obj = pdf_dict_put_array(ctx, smask, PDF_NAME(BC), n);
  912. for (i = 0; i < n; i++)
  913. pdf_array_push_real(ctx, color_obj, color[i]);
  914. gs = CURRENT_GSTATE(pdev);
  915. fz_append_printf(ctx, gs->buf, "/SM%d gs\n", pdev->num_smasks-1);
  916. }
  917. fz_catch(ctx)
  918. {
  919. pdf_drop_obj(ctx, form_ref);
  920. fz_rethrow(ctx);
  921. }
  922. /* Now, everything we get until the end_mask needs to go into a
  923. * new buffer, which will be the stream contents for the form. */
  924. pdf_dev_push_new_buf(ctx, pdev, fz_new_buffer(ctx, 1024), NULL, form_ref);
  925. }
  926. static void
  927. pdf_dev_end_mask(fz_context *ctx, fz_device *dev, fz_function *tr)
  928. {
  929. pdf_device *pdev = (pdf_device*)dev;
  930. pdf_document *doc = pdev->doc;
  931. gstate *gs = CURRENT_GSTATE(pdev);
  932. pdf_obj *form_ref = (pdf_obj *)gs->on_pop_arg;
  933. if (tr)
  934. fz_warn(ctx, "Ignoring Transfer function");
  935. /* Here we do part of the pop, but not all of it. */
  936. pdf_dev_end_text(ctx, pdev);
  937. fz_append_string(ctx, gs->buf, "Q\n");
  938. pdf_update_stream(ctx, doc, form_ref, gs->buf, 0);
  939. fz_drop_buffer(ctx, gs->buf);
  940. gs->buf = fz_keep_buffer(ctx, gs[-1].buf);
  941. gs->on_pop_arg = NULL;
  942. pdf_drop_obj(ctx, form_ref);
  943. fz_append_string(ctx, gs->buf, "q\n");
  944. }
  945. static void
  946. pdf_dev_begin_group(fz_context *ctx, fz_device *dev, fz_rect bbox, fz_colorspace *cs, int isolated, int knockout, int blendmode, float alpha)
  947. {
  948. pdf_device *pdev = (pdf_device*)dev;
  949. int num;
  950. pdf_obj *form_ref;
  951. gstate *gs;
  952. pdf_dev_end_text(ctx, pdev);
  953. pdf_dev_ctm(ctx, pdev, fz_identity);
  954. num = pdf_dev_new_form(ctx, &form_ref, pdev, bbox, isolated, knockout, alpha, cs);
  955. /* Do we have an appropriate blending extgstate already? */
  956. {
  957. char text[32];
  958. pdf_obj *obj;
  959. pdf_obj *egs = pdf_dict_get(ctx, pdev->resources, PDF_NAME(ExtGState));
  960. if (egs == NULL)
  961. egs = pdf_dict_put_dict(ctx, pdev->resources, PDF_NAME(ExtGState), 4);
  962. fz_snprintf(text, sizeof(text), "BlendMode%d", blendmode);
  963. obj = pdf_dict_gets(ctx, egs, text);
  964. if (obj == NULL)
  965. {
  966. /* No, better make one */
  967. obj = pdf_dict_puts_dict(ctx, egs, text, 2);
  968. pdf_dict_put(ctx, obj, PDF_NAME(Type), PDF_NAME(ExtGState));
  969. pdf_dict_put_name(ctx, obj, PDF_NAME(BM), fz_blendmode_name(blendmode));
  970. }
  971. }
  972. /* Add the call to this group */
  973. gs = CURRENT_GSTATE(pdev);
  974. fz_append_printf(ctx, gs->buf, "/BlendMode%d gs /Fm%d Do\n", blendmode, num);
  975. /* Now, everything we get until the end of group needs to go into a
  976. * new buffer, which will be the stream contents for the form. */
  977. pdf_dev_push_new_buf(ctx, pdev, fz_new_buffer(ctx, 1024), NULL, form_ref);
  978. }
  979. static void
  980. pdf_dev_end_group(fz_context *ctx, fz_device *dev)
  981. {
  982. pdf_device *pdev = (pdf_device*)dev;
  983. pdf_document *doc = pdev->doc;
  984. gstate *gs = CURRENT_GSTATE(pdev);
  985. fz_buffer *buf = fz_keep_buffer(ctx, gs->buf);
  986. pdf_obj *form_ref;
  987. pdf_dev_end_text(ctx, pdev);
  988. form_ref = (pdf_obj *)pdf_dev_pop(ctx, pdev);
  989. pdf_update_stream(ctx, doc, form_ref, buf, 0);
  990. fz_drop_buffer(ctx, buf);
  991. pdf_drop_obj(ctx, form_ref);
  992. }
  993. static int
  994. pdf_dev_begin_tile(fz_context *ctx, fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm, int id)
  995. {
  996. pdf_device *pdev = (pdf_device*)dev;
  997. /* FIXME */
  998. pdf_dev_end_text(ctx, pdev);
  999. return 0;
  1000. }
  1001. static void
  1002. pdf_dev_end_tile(fz_context *ctx, fz_device *dev)
  1003. {
  1004. pdf_device *pdev = (pdf_device*)dev;
  1005. /* FIXME */
  1006. pdf_dev_end_text(ctx, pdev);
  1007. }
  1008. static void
  1009. pdf_dev_close_device(fz_context *ctx, fz_device *dev)
  1010. {
  1011. pdf_device *pdev = (pdf_device*)dev;
  1012. pdf_dev_end_text(ctx, pdev);
  1013. }
  1014. static void
  1015. pdf_dev_drop_device(fz_context *ctx, fz_device *dev)
  1016. {
  1017. pdf_device *pdev = (pdf_device*)dev;
  1018. int i;
  1019. for (i = pdev->num_gstates-1; i >= 0; i--)
  1020. {
  1021. fz_drop_buffer(ctx, pdev->gstates[i].buf);
  1022. fz_drop_stroke_state(ctx, pdev->gstates[i].stroke_state);
  1023. }
  1024. for (i = pdev->num_cid_fonts-1; i >= 0; i--)
  1025. fz_drop_font(ctx, pdev->cid_fonts[i]);
  1026. for (i = pdev->num_groups - 1; i >= 0; i--)
  1027. {
  1028. pdf_drop_obj(ctx, pdev->groups[i].ref);
  1029. fz_drop_colorspace(ctx, pdev->groups[i].colorspace);
  1030. }
  1031. pdf_drop_obj(ctx, pdev->resources);
  1032. fz_free(ctx, pdev->cid_fonts);
  1033. fz_free(ctx, pdev->cid_fonts_enc);
  1034. fz_free(ctx, pdev->image_indices);
  1035. fz_free(ctx, pdev->groups);
  1036. fz_free(ctx, pdev->alphas);
  1037. fz_free(ctx, pdev->gstates);
  1038. }
  1039. fz_device *pdf_new_pdf_device(fz_context *ctx, pdf_document *doc, fz_matrix topctm, pdf_obj *resources, fz_buffer *buf)
  1040. {
  1041. pdf_device *dev = fz_new_derived_device(ctx, pdf_device);
  1042. dev->super.close_device = pdf_dev_close_device;
  1043. dev->super.drop_device = pdf_dev_drop_device;
  1044. dev->super.fill_path = pdf_dev_fill_path;
  1045. dev->super.stroke_path = pdf_dev_stroke_path;
  1046. dev->super.clip_path = pdf_dev_clip_path;
  1047. dev->super.clip_stroke_path = pdf_dev_clip_stroke_path;
  1048. dev->super.fill_text = pdf_dev_fill_text;
  1049. dev->super.stroke_text = pdf_dev_stroke_text;
  1050. dev->super.clip_text = pdf_dev_clip_text;
  1051. dev->super.clip_stroke_text = pdf_dev_clip_stroke_text;
  1052. dev->super.ignore_text = pdf_dev_ignore_text;
  1053. dev->super.fill_shade = pdf_dev_fill_shade;
  1054. dev->super.fill_image = pdf_dev_fill_image;
  1055. dev->super.fill_image_mask = pdf_dev_fill_image_mask;
  1056. dev->super.clip_image_mask = pdf_dev_clip_image_mask;
  1057. dev->super.pop_clip = pdf_dev_pop_clip;
  1058. dev->super.begin_mask = pdf_dev_begin_mask;
  1059. dev->super.end_mask = pdf_dev_end_mask;
  1060. dev->super.begin_group = pdf_dev_begin_group;
  1061. dev->super.end_group = pdf_dev_end_group;
  1062. dev->super.begin_tile = pdf_dev_begin_tile;
  1063. dev->super.end_tile = pdf_dev_end_tile;
  1064. fz_var(buf);
  1065. fz_try(ctx)
  1066. {
  1067. dev->doc = doc;
  1068. dev->resources = pdf_keep_obj(ctx, resources);
  1069. dev->gstates = fz_malloc_struct(ctx, gstate);
  1070. if (buf)
  1071. dev->gstates[0].buf = fz_keep_buffer(ctx, buf);
  1072. else
  1073. dev->gstates[0].buf = fz_new_buffer(ctx, 256);
  1074. dev->gstates[0].ctm = fz_identity; // XXX
  1075. dev->gstates[0].colorspace[0] = fz_device_gray(ctx);
  1076. dev->gstates[0].colorspace[1] = fz_device_gray(ctx);
  1077. dev->gstates[0].color[0][0] = 0;
  1078. dev->gstates[0].color[1][0] = 0;
  1079. dev->gstates[0].alpha[0] = 1.0f;
  1080. dev->gstates[0].alpha[1] = 1.0f;
  1081. dev->gstates[0].font = -1;
  1082. dev->num_gstates = 1;
  1083. dev->max_gstates = 1;
  1084. if (!fz_is_identity(topctm))
  1085. fz_append_printf(ctx, dev->gstates[0].buf, "%M cm\n", &topctm);
  1086. }
  1087. fz_catch(ctx)
  1088. {
  1089. fz_drop_device(ctx, &dev->super);
  1090. fz_rethrow(ctx);
  1091. }
  1092. return (fz_device*)dev;
  1093. }
  1094. fz_device *pdf_page_write(fz_context *ctx, pdf_document *doc, fz_rect mediabox, pdf_obj **presources, fz_buffer **pcontents)
  1095. {
  1096. fz_matrix pagectm = { 1, 0, 0, -1, -mediabox.x0, mediabox.y1 };
  1097. if (!*presources)
  1098. *presources = pdf_new_dict(ctx, doc, 0);
  1099. if (!*pcontents)
  1100. *pcontents = fz_new_buffer(ctx, 0);
  1101. return pdf_new_pdf_device(ctx, doc, pagectm, *presources, *pcontents);
  1102. }