pdf-op-color.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. // Copyright (C) 2004-2024 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 <string.h>
  25. enum {
  26. UNMARKED_STROKE = 1,
  27. UNMARKED_FILL = 2
  28. };
  29. typedef struct gstate_stack
  30. {
  31. struct gstate_stack *next;
  32. pdf_obj *cs_stroke;
  33. pdf_obj *cs_fill;
  34. int unmarked;
  35. fz_matrix ctm;
  36. } gstate_stack;
  37. typedef struct resources_stack
  38. {
  39. struct resources_stack *next;
  40. pdf_obj *old_rdb;
  41. pdf_obj *new_rdb;
  42. } resources_stack;
  43. typedef struct
  44. {
  45. pdf_obj *cs;
  46. float color[FZ_MAX_COLORS];
  47. } cs_color;
  48. #define MAX_REWRITTEN_NAME 32
  49. typedef struct
  50. {
  51. pdf_obj *im_obj;
  52. fz_image *after;
  53. char name[MAX_REWRITTEN_NAME];
  54. } rewritten_image;
  55. typedef struct
  56. {
  57. int max;
  58. int len;
  59. rewritten_image *res;
  60. } rewritten_images;
  61. typedef struct
  62. {
  63. pdf_obj *before;
  64. fz_shade *after;
  65. char name[MAX_REWRITTEN_NAME];
  66. } rewritten_shade;
  67. typedef struct
  68. {
  69. int max;
  70. int len;
  71. rewritten_shade *res;
  72. } rewritten_shades;
  73. typedef struct
  74. {
  75. pdf_processor super;
  76. pdf_document *doc;
  77. int structparents;
  78. pdf_processor *chain;
  79. pdf_filter_options *global_options;
  80. pdf_color_filter_options *options;
  81. resources_stack *rstack;
  82. gstate_stack *gstate;
  83. cs_color *stroke;
  84. cs_color *fill;
  85. rewritten_images images;
  86. rewritten_shades shades;
  87. } pdf_color_processor;
  88. static void
  89. push_rewritten_image(fz_context *ctx, pdf_color_processor *p, pdf_obj *im_obj, fz_image *after, char *name)
  90. {
  91. rewritten_images *list = &p->images;
  92. if (list->max == list->len)
  93. {
  94. int new_max = list->max * 2;
  95. if (new_max == 0)
  96. new_max = 32;
  97. list->res = fz_realloc(ctx, list->res, sizeof(*list->res) * new_max);
  98. list->max = new_max;
  99. }
  100. list->res[list->len].im_obj = pdf_keep_obj(ctx, im_obj);
  101. list->res[list->len].after = fz_keep_image(ctx, after);
  102. memcpy(list->res[list->len].name, name, MAX_REWRITTEN_NAME);
  103. list->len++;
  104. }
  105. static fz_image *
  106. find_rewritten_image(fz_context *ctx, pdf_color_processor *p, pdf_obj *im_obj, char *name)
  107. {
  108. rewritten_images *list = &p->images;
  109. int i;
  110. for (i = 0; i < list->len; i++)
  111. if (list->res[i].im_obj == im_obj)
  112. {
  113. memcpy(name, list->res[i].name, MAX_REWRITTEN_NAME);
  114. return list->res[i].after;
  115. }
  116. return NULL;
  117. }
  118. static void
  119. drop_rewritten_images(fz_context *ctx, pdf_color_processor *p)
  120. {
  121. rewritten_images *list = &p->images;
  122. int i;
  123. for (i = 0; i < list->len; i++)
  124. {
  125. pdf_drop_obj(ctx, list->res[i].im_obj);
  126. fz_drop_image(ctx, list->res[i].after);
  127. }
  128. fz_free(ctx, list->res);
  129. list->res = NULL;
  130. list->len = 0;
  131. list->max = 0;
  132. }
  133. static void
  134. push_rewritten_shade(fz_context *ctx, pdf_color_processor *p, pdf_obj *before, fz_shade *after, char *name)
  135. {
  136. rewritten_shades *list = &p->shades;
  137. if (list->max == list->len)
  138. {
  139. int new_max = list->max * 2;
  140. if (new_max == 0)
  141. new_max = 32;
  142. list->res = fz_realloc(ctx, list->res, sizeof(*list->res) * new_max);
  143. list->max = new_max;
  144. }
  145. list->res[list->len].before = pdf_keep_obj(ctx, before);
  146. list->res[list->len].after = fz_keep_shade(ctx, after);
  147. memcpy(list->res[list->len].name, name, MAX_REWRITTEN_NAME);
  148. list->len++;
  149. }
  150. static fz_shade *
  151. find_rewritten_shade(fz_context *ctx, pdf_color_processor *p, pdf_obj *before, char *name)
  152. {
  153. rewritten_shades *list = &p->shades;
  154. int i;
  155. for (i = 0; i < list->len; i++)
  156. if (list->res[i].before == before)
  157. {
  158. memcpy(name, list->res[i].name, MAX_REWRITTEN_NAME);
  159. return list->res[i].after;
  160. }
  161. return NULL;
  162. }
  163. static void
  164. drop_rewritten_shades(fz_context *ctx, pdf_color_processor *p)
  165. {
  166. rewritten_shades *list = &p->shades;
  167. int i;
  168. for (i = 0; i < list->len; i++)
  169. {
  170. pdf_drop_obj(ctx, list->res[i].before);
  171. fz_drop_shade(ctx, list->res[i].after);
  172. }
  173. fz_free(ctx, list->res);
  174. list->res = NULL;
  175. list->len = 0;
  176. list->max = 0;
  177. }
  178. static void
  179. make_resource_instance(fz_context *ctx, pdf_color_processor *p, pdf_obj *key, const char *prefix, char *buf, int len, pdf_obj *target)
  180. {
  181. int i;
  182. /* key gives us our category. Make sure we have such a category. */
  183. pdf_obj *res = pdf_dict_get(ctx, p->rstack->new_rdb, key);
  184. if (!res)
  185. res = pdf_dict_put_dict(ctx, p->rstack->new_rdb, key, 8);
  186. /* Now check through the category for each possible prefixed name
  187. * in turn. */
  188. for (i = 1; i < 65536; ++i)
  189. {
  190. pdf_obj *obj;
  191. fz_snprintf(buf, len, "%s%d", prefix, i);
  192. obj = pdf_dict_gets(ctx, res, buf);
  193. if (!obj)
  194. {
  195. /* We've run out of names. At least that means we haven't
  196. * previously added one ourselves. So add it now. */
  197. pdf_dict_puts(ctx, res, buf, target);
  198. return;
  199. }
  200. if (pdf_objcmp_resolve(ctx, obj, target) == 0)
  201. {
  202. /* We've found this one before! */
  203. return;
  204. }
  205. }
  206. fz_throw(ctx, FZ_ERROR_LIMIT, "Cannot create unique resource name");
  207. }
  208. static void
  209. rewrite_cs(fz_context *ctx, pdf_color_processor *p, pdf_obj *cs_obj, int n, float *color, int stroking)
  210. {
  211. char new_name[MAX_REWRITTEN_NAME];
  212. fz_colorspace *cs = NULL;
  213. pdf_pattern *pat = NULL;
  214. fz_shade *shade = NULL;
  215. int type;
  216. if (stroking)
  217. p->gstate->unmarked &= ~UNMARKED_STROKE;
  218. else
  219. p->gstate->unmarked &= ~UNMARKED_FILL;
  220. /* Otherwise, if it's a name, look it up as a colorspace. */
  221. if (pdf_name_eq(ctx, cs_obj, PDF_NAME(DeviceGray)) ||
  222. pdf_name_eq(ctx, cs_obj, PDF_NAME(DeviceCMYK)) ||
  223. pdf_name_eq(ctx, cs_obj, PDF_NAME(DeviceRGB)) ||
  224. pdf_name_eq(ctx, cs_obj, PDF_NAME(Pattern)))
  225. {
  226. /* These names should not be looked up. */
  227. }
  228. else if (pdf_is_name(ctx, cs_obj))
  229. {
  230. /* Any other names should be looked up in the resource dict,
  231. * because our rewrite function doesn't have access to that. */
  232. cs_obj = pdf_dict_get(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(ColorSpace)), cs_obj);
  233. }
  234. /* Until now, cs_obj has been a borrowed reference. Make it a real one. */
  235. pdf_keep_obj(ctx, cs_obj);
  236. /* Whatever happens, from here on in, we must drop cs_obj. */
  237. fz_var(cs);
  238. fz_var(pat);
  239. fz_var(shade);
  240. fz_try(ctx)
  241. {
  242. /* Our gstate always has to contain colorspaces BEFORE rewriting.
  243. * Consider the case where we are given a separation space, and
  244. * we rewrite it to be RGB. Then we change the 'amount' of that
  245. * separation; we can't do that with the RGB value. */
  246. if (stroking)
  247. {
  248. pdf_drop_obj(ctx, p->gstate->cs_stroke);
  249. p->gstate->cs_stroke = pdf_keep_obj(ctx, cs_obj);
  250. }
  251. else
  252. {
  253. pdf_drop_obj(ctx, p->gstate->cs_fill);
  254. p->gstate->cs_fill = pdf_keep_obj(ctx, cs_obj);
  255. }
  256. /* Now, do any rewriting. This might drop the reference to cs_obj and
  257. * return with a different one. */
  258. if (p->options->color_rewrite)
  259. p->options->color_rewrite(ctx, p->options->opaque, &cs_obj, &n, color);
  260. /* If we've rewritten it to be a simple name, great! */
  261. if (pdf_name_eq(ctx, cs_obj, PDF_NAME(DeviceGray)))
  262. {
  263. if (stroking)
  264. {
  265. if (n == 1)
  266. p->chain->op_G(ctx, p->chain, color[0]);
  267. else
  268. p->chain->op_CS(ctx, p->chain, "DeviceGray", fz_device_gray(ctx));
  269. }
  270. else
  271. {
  272. if (n == 1)
  273. p->chain->op_g(ctx, p->chain, color[0]);
  274. else
  275. p->chain->op_cs(ctx, p->chain, "DeviceGray", fz_device_gray(ctx));
  276. }
  277. break;
  278. }
  279. if (pdf_name_eq(ctx, cs_obj, PDF_NAME(DeviceRGB)))
  280. {
  281. if (stroking)
  282. {
  283. if (n == 3)
  284. p->chain->op_RG(ctx, p->chain, color[0], color[1], color[2]);
  285. else
  286. p->chain->op_CS(ctx, p->chain, "DeviceRGB", fz_device_rgb(ctx));
  287. }
  288. else
  289. {
  290. if (n == 3)
  291. p->chain->op_rg(ctx, p->chain, color[0], color[1], color[2]);
  292. else
  293. p->chain->op_cs(ctx, p->chain, "DeviceRGB", fz_device_rgb(ctx));
  294. }
  295. break;
  296. }
  297. if (pdf_name_eq(ctx, cs_obj, PDF_NAME(DeviceCMYK)))
  298. {
  299. if (stroking)
  300. {
  301. if (n == 4)
  302. p->chain->op_K(ctx, p->chain, color[0], color[1], color[2], color[3]);
  303. else
  304. p->chain->op_CS(ctx, p->chain, "DeviceCMYK", fz_device_cmyk(ctx));
  305. }
  306. else
  307. {
  308. if (n == 4)
  309. p->chain->op_k(ctx, p->chain, color[0], color[1], color[2], color[3]);
  310. else
  311. p->chain->op_cs(ctx, p->chain, "DeviceCMYK", fz_device_cmyk(ctx));
  312. }
  313. break;
  314. }
  315. /* Accept both /Pattern and [ /Pattern ] */
  316. if (pdf_name_eq(ctx, cs_obj, PDF_NAME(Pattern)) ||
  317. (pdf_array_len(ctx, cs_obj) == 1 && pdf_name_eq(ctx, pdf_array_get(ctx, cs_obj, 0), PDF_NAME(Pattern))))
  318. {
  319. assert(n == 0);
  320. if (stroking)
  321. p->chain->op_CS(ctx, p->chain, "Pattern", NULL);
  322. else
  323. p->chain->op_cs(ctx, p->chain, "Pattern", NULL);
  324. break;
  325. }
  326. /* Has it been rewritten to be an array? */
  327. if (pdf_is_array(ctx, cs_obj))
  328. {
  329. /* Make a new entry (or find an existing one), and send that. */
  330. make_resource_instance(ctx, p, PDF_NAME(ColorSpace), "CS", new_name, sizeof(new_name), cs_obj);
  331. cs = pdf_load_colorspace(ctx, cs_obj);
  332. if (stroking)
  333. p->chain->op_CS(ctx, p->chain, new_name, cs);
  334. else
  335. p->chain->op_cs(ctx, p->chain, new_name, cs);
  336. if (n > 0)
  337. {
  338. if (stroking)
  339. p->chain->op_SC_color(ctx, p->chain, n, color);
  340. else
  341. p->chain->op_sc_color(ctx, p->chain, n, color);
  342. }
  343. break;
  344. }
  345. /* Has it been rewritten to be a pattern? */
  346. type = pdf_dict_get_int(ctx, cs_obj, PDF_NAME(PatternType));
  347. if (type < 1 || type > 2)
  348. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad PatternType");
  349. /* Make a new entry (or find an existing one), and send that. */
  350. make_resource_instance(ctx, p, PDF_NAME(Pattern), "Pa", new_name, sizeof(new_name), cs_obj);
  351. if (type == 1)
  352. {
  353. pat = pdf_load_pattern(ctx, p->doc, cs_obj);
  354. if (stroking)
  355. p->chain->op_SC_pattern(ctx, p->chain, new_name, pat, n, color);
  356. else
  357. p->chain->op_sc_pattern(ctx, p->chain, new_name, pat, n, color);
  358. break;
  359. }
  360. else if (type == 2)
  361. {
  362. shade = pdf_load_shading(ctx, p->doc, cs_obj);
  363. if (stroking)
  364. p->chain->op_SC_shade(ctx, p->chain, new_name, shade);
  365. else
  366. p->chain->op_sc_shade(ctx, p->chain, new_name, shade);
  367. break;
  368. }
  369. fz_throw(ctx, FZ_ERROR_FORMAT, "Illegal rewritten colorspace");
  370. }
  371. fz_always(ctx)
  372. {
  373. fz_drop_shade(ctx, shade);
  374. fz_drop_colorspace(ctx, cs);
  375. pdf_drop_pattern(ctx, pat);
  376. pdf_drop_obj(ctx, cs_obj);
  377. }
  378. fz_catch(ctx)
  379. fz_rethrow(ctx);
  380. }
  381. static void
  382. mark_stroke(fz_context *ctx, pdf_color_processor *p)
  383. {
  384. float zero[FZ_MAX_COLORS] = { 0 };
  385. rewrite_cs(ctx, p, PDF_NAME(DeviceGray), 1, zero, 1);
  386. p->gstate->unmarked &= ~UNMARKED_STROKE;
  387. }
  388. static void
  389. mark_fill(fz_context *ctx, pdf_color_processor *p)
  390. {
  391. float zero[FZ_MAX_COLORS] = { 0 };
  392. rewrite_cs(ctx, p, PDF_NAME(DeviceGray), 1, zero, 0);
  393. p->gstate->unmarked &= ~UNMARKED_FILL;
  394. }
  395. /* general graphics state */
  396. static void
  397. pdf_color_w(fz_context *ctx, pdf_processor *proc, float linewidth)
  398. {
  399. pdf_color_processor *p = (pdf_color_processor*)proc;
  400. if (p->chain->op_w)
  401. p->chain->op_w(ctx, p->chain, linewidth);
  402. }
  403. static void
  404. pdf_color_j(fz_context *ctx, pdf_processor *proc, int linejoin)
  405. {
  406. pdf_color_processor *p = (pdf_color_processor*)proc;
  407. if (p->chain->op_j)
  408. p->chain->op_j(ctx, p->chain, linejoin);
  409. }
  410. static void
  411. pdf_color_J(fz_context *ctx, pdf_processor *proc, int linecap)
  412. {
  413. pdf_color_processor *p = (pdf_color_processor*)proc;
  414. if (p->chain->op_J)
  415. p->chain->op_J(ctx, p->chain, linecap);
  416. }
  417. static void
  418. pdf_color_M(fz_context *ctx, pdf_processor *proc, float miterlimit)
  419. {
  420. pdf_color_processor *p = (pdf_color_processor*)proc;
  421. if (p->chain->op_M)
  422. p->chain->op_M(ctx, p->chain, miterlimit);
  423. }
  424. static void
  425. pdf_color_d(fz_context *ctx, pdf_processor *proc, pdf_obj *array, float phase)
  426. {
  427. pdf_color_processor *p = (pdf_color_processor*)proc;
  428. if (p->chain->op_d)
  429. p->chain->op_d(ctx, p->chain, array, phase);
  430. }
  431. static void
  432. pdf_color_ri(fz_context *ctx, pdf_processor *proc, const char *intent)
  433. {
  434. pdf_color_processor *p = (pdf_color_processor*)proc;
  435. if (p->chain->op_ri)
  436. p->chain->op_ri(ctx, p->chain, intent);
  437. }
  438. static void
  439. pdf_color_gs_OP(fz_context *ctx, pdf_processor *proc, int b)
  440. {
  441. pdf_color_processor *p = (pdf_color_processor*)proc;
  442. if (p->chain->op_gs_OP)
  443. p->chain->op_gs_OP(ctx, p->chain, b);
  444. }
  445. static void
  446. pdf_color_gs_op(fz_context *ctx, pdf_processor *proc, int b)
  447. {
  448. pdf_color_processor *p = (pdf_color_processor*)proc;
  449. if (p->chain->op_gs_op)
  450. p->chain->op_gs_op(ctx, p->chain, b);
  451. }
  452. static void
  453. pdf_color_gs_OPM(fz_context *ctx, pdf_processor *proc, int i)
  454. {
  455. pdf_color_processor *p = (pdf_color_processor*)proc;
  456. if (p->chain->op_gs_OPM)
  457. p->chain->op_gs_OPM(ctx, p->chain, i);
  458. }
  459. static void
  460. pdf_color_gs_UseBlackPtComp(fz_context *ctx, pdf_processor *proc, pdf_obj *name)
  461. {
  462. pdf_color_processor *p = (pdf_color_processor*)proc;
  463. if (p->chain->op_gs_UseBlackPtComp)
  464. p->chain->op_gs_UseBlackPtComp(ctx, p->chain, name);
  465. }
  466. static void
  467. pdf_color_i(fz_context *ctx, pdf_processor *proc, float flatness)
  468. {
  469. pdf_color_processor *p = (pdf_color_processor*)proc;
  470. if (p->chain->op_i)
  471. p->chain->op_i(ctx, p->chain, flatness);
  472. }
  473. static void
  474. pdf_color_gs_begin(fz_context *ctx, pdf_processor *proc, const char *name, pdf_obj *extgstate)
  475. {
  476. pdf_color_processor *p = (pdf_color_processor*)proc;
  477. if (p->chain->op_gs_begin)
  478. p->chain->op_gs_begin(ctx, p->chain, name, extgstate);
  479. }
  480. static void
  481. pdf_color_gs_BM(fz_context *ctx, pdf_processor *proc, const char *blendmode)
  482. {
  483. pdf_color_processor *p = (pdf_color_processor*)proc;
  484. if (p->chain->op_gs_BM)
  485. p->chain->op_gs_BM(ctx, p->chain, blendmode);
  486. }
  487. static void
  488. pdf_color_gs_CA(fz_context *ctx, pdf_processor *proc, float alpha)
  489. {
  490. pdf_color_processor *p = (pdf_color_processor*)proc;
  491. if (p->chain->op_gs_CA)
  492. p->chain->op_gs_CA(ctx, p->chain, alpha);
  493. }
  494. static void
  495. pdf_color_gs_ca(fz_context *ctx, pdf_processor *proc, float alpha)
  496. {
  497. pdf_color_processor *p = (pdf_color_processor*)proc;
  498. if (p->chain->op_gs_ca)
  499. p->chain->op_gs_ca(ctx, p->chain, alpha);
  500. }
  501. static void
  502. pdf_color_gs_SMask(fz_context *ctx, pdf_processor *proc, pdf_obj *smask, fz_colorspace *smask_cs, float *bc, int luminosity, pdf_obj *tr)
  503. {
  504. pdf_color_processor *p = (pdf_color_processor*)proc;
  505. if (p->chain->op_gs_SMask)
  506. p->chain->op_gs_SMask(ctx, p->chain, smask, smask_cs, bc, luminosity, tr);
  507. }
  508. static void
  509. pdf_color_gs_end(fz_context *ctx, pdf_processor *proc)
  510. {
  511. pdf_color_processor *p = (pdf_color_processor*)proc;
  512. if (p->chain->op_gs_end)
  513. p->chain->op_gs_end(ctx, p->chain);
  514. }
  515. /* special graphics state */
  516. static void
  517. pdf_color_q(fz_context *ctx, pdf_processor *proc)
  518. {
  519. pdf_color_processor *p = (pdf_color_processor*)proc;
  520. gstate_stack *gs = fz_malloc_struct(ctx, gstate_stack);
  521. gs->next = p->gstate;
  522. gs->cs_fill = pdf_keep_obj(ctx, p->gstate->cs_fill);
  523. gs->cs_stroke = pdf_keep_obj(ctx, p->gstate->cs_stroke);
  524. gs->unmarked = p->gstate->unmarked;
  525. gs->ctm = p->gstate->ctm;
  526. p->gstate = gs;
  527. if (p->chain->op_q)
  528. p->chain->op_q(ctx, p->chain);
  529. }
  530. static void
  531. pdf_color_cm(fz_context *ctx, pdf_processor *proc, float a, float b, float c, float d, float e, float f)
  532. {
  533. pdf_color_processor *p = (pdf_color_processor*)proc;
  534. fz_matrix m;
  535. m.a = a;
  536. m.b = b;
  537. m.c = c;
  538. m.d = d;
  539. m.e = e;
  540. m.f = f;
  541. p->gstate->ctm = fz_concat(m, p->gstate->ctm);
  542. if (p->chain->op_cm)
  543. p->chain->op_cm(ctx, p->chain, a, b, c, d, e, f);
  544. }
  545. /* path construction */
  546. static void
  547. pdf_color_m(fz_context *ctx, pdf_processor *proc, float x, float y)
  548. {
  549. pdf_color_processor *p = (pdf_color_processor*)proc;
  550. if (p->chain->op_m)
  551. p->chain->op_m(ctx, p->chain, x, y);
  552. }
  553. static void
  554. pdf_color_l(fz_context *ctx, pdf_processor *proc, float x, float y)
  555. {
  556. pdf_color_processor *p = (pdf_color_processor*)proc;
  557. if (p->chain->op_l)
  558. p->chain->op_l(ctx, p->chain, x, y);
  559. }
  560. static void
  561. pdf_color_c(fz_context *ctx, pdf_processor *proc, float x1, float y1, float x2, float y2, float x3, float y3)
  562. {
  563. pdf_color_processor *p = (pdf_color_processor*)proc;
  564. if (p->chain->op_c)
  565. p->chain->op_c(ctx, p->chain, x1, y1, x2, y2, x3, y3);
  566. }
  567. static void
  568. pdf_color_v(fz_context *ctx, pdf_processor *proc, float x2, float y2, float x3, float y3)
  569. {
  570. pdf_color_processor *p = (pdf_color_processor*)proc;
  571. if (p->chain->op_v)
  572. p->chain->op_v(ctx, p->chain, x2, y2, x3, y3);
  573. }
  574. static void
  575. pdf_color_y(fz_context *ctx, pdf_processor *proc, float x1, float y1, float x3, float y3)
  576. {
  577. pdf_color_processor *p = (pdf_color_processor*)proc;
  578. if (p->chain->op_y)
  579. p->chain->op_y(ctx, p->chain, x1, y1, x3, y3);
  580. }
  581. static void
  582. pdf_color_h(fz_context *ctx, pdf_processor *proc)
  583. {
  584. pdf_color_processor *p = (pdf_color_processor*)proc;
  585. if (p->chain->op_h)
  586. p->chain->op_h(ctx, p->chain);
  587. }
  588. static void
  589. pdf_color_re(fz_context *ctx, pdf_processor *proc, float x, float y, float w, float h)
  590. {
  591. pdf_color_processor *p = (pdf_color_processor*)proc;
  592. if (p->chain->op_re)
  593. p->chain->op_re(ctx, p->chain, x, y, w, h);
  594. }
  595. /* path painting */
  596. static void
  597. pdf_color_S(fz_context *ctx, pdf_processor *proc)
  598. {
  599. pdf_color_processor *p = (pdf_color_processor*)proc;
  600. if (p->gstate->unmarked & UNMARKED_STROKE)
  601. mark_stroke(ctx, p);
  602. if (p->chain->op_S)
  603. p->chain->op_S(ctx, p->chain);
  604. }
  605. static void
  606. pdf_color_s(fz_context *ctx, pdf_processor *proc)
  607. {
  608. pdf_color_processor *p = (pdf_color_processor*)proc;
  609. if (p->gstate->unmarked & UNMARKED_FILL)
  610. mark_fill(ctx, p);
  611. if (p->chain->op_s)
  612. p->chain->op_s(ctx, p->chain);
  613. }
  614. static void
  615. pdf_color_F(fz_context *ctx, pdf_processor *proc)
  616. {
  617. pdf_color_processor *p = (pdf_color_processor*)proc;
  618. if (p->gstate->unmarked & UNMARKED_FILL)
  619. mark_fill(ctx, p);
  620. if (p->chain->op_F)
  621. p->chain->op_F(ctx, p->chain);
  622. }
  623. static void
  624. pdf_color_f(fz_context *ctx, pdf_processor *proc)
  625. {
  626. pdf_color_processor *p = (pdf_color_processor*)proc;
  627. if (p->gstate->unmarked & UNMARKED_FILL)
  628. mark_fill(ctx, p);
  629. if (p->chain->op_f)
  630. p->chain->op_f(ctx, p->chain);
  631. }
  632. static void
  633. pdf_color_fstar(fz_context *ctx, pdf_processor *proc)
  634. {
  635. pdf_color_processor *p = (pdf_color_processor*)proc;
  636. if (p->gstate->unmarked & UNMARKED_FILL)
  637. mark_fill(ctx, p);
  638. if (p->chain->op_fstar)
  639. p->chain->op_fstar(ctx, p->chain);
  640. }
  641. static void
  642. pdf_color_B(fz_context *ctx, pdf_processor *proc)
  643. {
  644. pdf_color_processor *p = (pdf_color_processor*)proc;
  645. if (p->gstate->unmarked & UNMARKED_STROKE)
  646. mark_stroke(ctx, p);
  647. if (p->gstate->unmarked & UNMARKED_FILL)
  648. mark_fill(ctx, p);
  649. if (p->chain->op_B)
  650. p->chain->op_B(ctx, p->chain);
  651. }
  652. static void
  653. pdf_color_Bstar(fz_context *ctx, pdf_processor *proc)
  654. {
  655. pdf_color_processor *p = (pdf_color_processor*)proc;
  656. if (p->gstate->unmarked & UNMARKED_STROKE)
  657. mark_stroke(ctx, p);
  658. if (p->gstate->unmarked & UNMARKED_FILL)
  659. mark_fill(ctx, p);
  660. if (p->chain->op_Bstar)
  661. p->chain->op_Bstar(ctx, p->chain);
  662. }
  663. static void
  664. pdf_color_b(fz_context *ctx, pdf_processor *proc)
  665. {
  666. pdf_color_processor *p = (pdf_color_processor*)proc;
  667. if (p->gstate->unmarked & UNMARKED_STROKE)
  668. mark_stroke(ctx, p);
  669. if (p->gstate->unmarked & UNMARKED_FILL)
  670. mark_fill(ctx, p);
  671. if (p->chain->op_b)
  672. p->chain->op_b(ctx, p->chain);
  673. }
  674. static void
  675. pdf_color_bstar(fz_context *ctx, pdf_processor *proc)
  676. {
  677. pdf_color_processor *p = (pdf_color_processor*)proc;
  678. if (p->gstate->unmarked & UNMARKED_STROKE)
  679. mark_stroke(ctx, p);
  680. if (p->gstate->unmarked & UNMARKED_FILL)
  681. mark_fill(ctx, p);
  682. if (p->chain->op_bstar)
  683. p->chain->op_bstar(ctx, p->chain);
  684. }
  685. static void
  686. pdf_color_n(fz_context *ctx, pdf_processor *proc)
  687. {
  688. pdf_color_processor *p = (pdf_color_processor*)proc;
  689. if (p->chain->op_n)
  690. p->chain->op_n(ctx, p->chain);
  691. }
  692. /* clipping paths */
  693. static void
  694. pdf_color_W(fz_context *ctx, pdf_processor *proc)
  695. {
  696. pdf_color_processor *p = (pdf_color_processor*)proc;
  697. if (p->chain->op_W)
  698. p->chain->op_W(ctx, p->chain);
  699. }
  700. static void
  701. pdf_color_Wstar(fz_context *ctx, pdf_processor *proc)
  702. {
  703. pdf_color_processor *p = (pdf_color_processor*)proc;
  704. if (p->chain->op_Wstar)
  705. p->chain->op_Wstar(ctx, p->chain);
  706. }
  707. /* text objects */
  708. static void
  709. pdf_color_BT(fz_context *ctx, pdf_processor *proc)
  710. {
  711. pdf_color_processor *p = (pdf_color_processor*)proc;
  712. if (p->chain->op_BT)
  713. p->chain->op_BT(ctx, p->chain);
  714. }
  715. static void
  716. pdf_color_ET(fz_context *ctx, pdf_processor *proc)
  717. {
  718. pdf_color_processor *p = (pdf_color_processor*)proc;
  719. if (p->chain->op_ET)
  720. p->chain->op_ET(ctx, p->chain);
  721. }
  722. static void
  723. pdf_color_Q(fz_context *ctx, pdf_processor *proc)
  724. {
  725. pdf_color_processor *p = (pdf_color_processor*)proc;
  726. gstate_stack *gs = p->gstate;
  727. p->gstate = gs->next;
  728. pdf_drop_obj(ctx, gs->cs_fill);
  729. pdf_drop_obj(ctx, gs->cs_stroke);
  730. fz_try(ctx)
  731. if (p->chain->op_Q)
  732. p->chain->op_Q(ctx, p->chain);
  733. fz_always(ctx)
  734. fz_free(ctx, gs);
  735. fz_catch(ctx)
  736. fz_rethrow(ctx);
  737. }
  738. /* text state */
  739. static void
  740. pdf_color_Tc(fz_context *ctx, pdf_processor *proc, float charspace)
  741. {
  742. pdf_color_processor *p = (pdf_color_processor*)proc;
  743. if (p->chain->op_Tc)
  744. p->chain->op_Tc(ctx, p->chain, charspace);
  745. }
  746. static void
  747. pdf_color_Tw(fz_context *ctx, pdf_processor *proc, float wordspace)
  748. {
  749. pdf_color_processor *p = (pdf_color_processor*)proc;
  750. if (p->chain->op_Tw)
  751. p->chain->op_Tw(ctx, p->chain, wordspace);
  752. }
  753. static void
  754. pdf_color_Tz(fz_context *ctx, pdf_processor *proc, float scale)
  755. {
  756. pdf_color_processor *p = (pdf_color_processor*)proc;
  757. if (p->chain->op_Tz)
  758. p->chain->op_Tz(ctx, p->chain, scale);
  759. }
  760. static void
  761. pdf_color_TL(fz_context *ctx, pdf_processor *proc, float leading)
  762. {
  763. pdf_color_processor *p = (pdf_color_processor*)proc;
  764. if (p->chain->op_TL)
  765. p->chain->op_TL(ctx, p->chain, leading);
  766. }
  767. static void
  768. pdf_color_Tf(fz_context *ctx, pdf_processor *proc, const char *name, pdf_font_desc *font, float size)
  769. {
  770. pdf_color_processor *p = (pdf_color_processor*)proc;
  771. if (p->chain->op_Tf)
  772. p->chain->op_Tf(ctx, p->chain, name, font, size);
  773. }
  774. static void
  775. pdf_color_Tr(fz_context *ctx, pdf_processor *proc, int render)
  776. {
  777. pdf_color_processor *p = (pdf_color_processor*)proc;
  778. if (p->chain->op_Tr)
  779. p->chain->op_Tr(ctx, p->chain, render);
  780. }
  781. static void
  782. pdf_color_Ts(fz_context *ctx, pdf_processor *proc, float rise)
  783. {
  784. pdf_color_processor *p = (pdf_color_processor*)proc;
  785. if (p->chain->op_Ts)
  786. p->chain->op_Ts(ctx, p->chain, rise);
  787. }
  788. /* text positioning */
  789. static void
  790. pdf_color_Td(fz_context *ctx, pdf_processor *proc, float tx, float ty)
  791. {
  792. pdf_color_processor *p = (pdf_color_processor*)proc;
  793. if (p->chain->op_Td)
  794. p->chain->op_Td(ctx, p->chain, tx, ty);
  795. }
  796. static void
  797. pdf_color_TD(fz_context *ctx, pdf_processor *proc, float tx, float ty)
  798. {
  799. pdf_color_processor *p = (pdf_color_processor*)proc;
  800. if (p->chain->op_TD)
  801. p->chain->op_TD(ctx, p->chain, tx, ty);
  802. }
  803. static void
  804. pdf_color_Tm(fz_context *ctx, pdf_processor *proc, float a, float b, float c, float d, float e, float f)
  805. {
  806. pdf_color_processor *p = (pdf_color_processor*)proc;
  807. if (p->chain->op_Tm)
  808. p->chain->op_Tm(ctx, p->chain, a, b, c, d, e, f);
  809. }
  810. static void
  811. pdf_color_Tstar(fz_context *ctx, pdf_processor *proc)
  812. {
  813. pdf_color_processor *p = (pdf_color_processor*)proc;
  814. if (p->chain->op_Tstar)
  815. p->chain->op_Tstar(ctx, p->chain);
  816. }
  817. /* text showing */
  818. static void
  819. pdf_color_TJ(fz_context *ctx, pdf_processor *proc, pdf_obj *array)
  820. {
  821. pdf_color_processor *p = (pdf_color_processor*)proc;
  822. /* FIXME: We could optimise this if we knew the Tr, maybe. */
  823. if (p->gstate->unmarked & UNMARKED_STROKE)
  824. mark_stroke(ctx, p);
  825. if (p->gstate->unmarked & UNMARKED_FILL)
  826. mark_fill(ctx, p);
  827. if (p->chain->op_TJ)
  828. p->chain->op_TJ(ctx, p->chain, array);
  829. }
  830. static void
  831. pdf_color_Tj(fz_context *ctx, pdf_processor *proc, char *str, size_t len)
  832. {
  833. pdf_color_processor *p = (pdf_color_processor*)proc;
  834. /* FIXME: We could optimise this if we knew the Tr, maybe. */
  835. if (p->gstate->unmarked & UNMARKED_STROKE)
  836. mark_stroke(ctx, p);
  837. if (p->gstate->unmarked & UNMARKED_FILL)
  838. mark_fill(ctx, p);
  839. if (p->chain->op_Tj)
  840. p->chain->op_Tj(ctx, p->chain, str, len);
  841. }
  842. static void
  843. pdf_color_squote(fz_context *ctx, pdf_processor *proc, char *str, size_t len)
  844. {
  845. pdf_color_processor *p = (pdf_color_processor*)proc;
  846. /* FIXME: We could optimise this if we knew the Tr, maybe. */
  847. if (p->gstate->unmarked & UNMARKED_STROKE)
  848. mark_stroke(ctx, p);
  849. if (p->gstate->unmarked & UNMARKED_FILL)
  850. mark_fill(ctx, p);
  851. if (p->chain->op_squote)
  852. p->chain->op_squote(ctx, p->chain, str, len);
  853. }
  854. static void
  855. pdf_color_dquote(fz_context *ctx, pdf_processor *proc, float aw, float ac, char *str, size_t len)
  856. {
  857. pdf_color_processor *p = (pdf_color_processor*)proc;
  858. /* FIXME: We could optimise this if we knew the Tr, maybe. */
  859. if (p->gstate->unmarked & UNMARKED_STROKE)
  860. mark_stroke(ctx, p);
  861. if (p->gstate->unmarked & UNMARKED_FILL)
  862. mark_fill(ctx, p);
  863. if (p->chain->op_dquote)
  864. p->chain->op_dquote(ctx, p->chain, aw, ac, str, len);
  865. }
  866. /* type 3 fonts */
  867. static void
  868. pdf_color_d0(fz_context *ctx, pdf_processor *proc, float wx, float wy)
  869. {
  870. pdf_color_processor *p = (pdf_color_processor*)proc;
  871. if (p->chain->op_d0)
  872. p->chain->op_d0(ctx, p->chain, wx, wy);
  873. }
  874. static void
  875. pdf_color_d1(fz_context *ctx, pdf_processor *proc, float wx, float wy, float llx, float lly, float urx, float ury)
  876. {
  877. pdf_color_processor *p = (pdf_color_processor*)proc;
  878. if (p->chain->op_d1)
  879. p->chain->op_d1(ctx, p->chain, wx, wy, llx, lly, urx, ury);
  880. }
  881. /* color */
  882. static void
  883. pdf_color_CS(fz_context *ctx, pdf_processor *proc, const char *name, fz_colorspace *cs)
  884. {
  885. pdf_color_processor *p = (pdf_color_processor *)proc;
  886. pdf_obj *cs_obj = pdf_new_name(ctx, name);
  887. float color[FZ_MAX_COLORS] = { 1 };
  888. fz_try(ctx)
  889. rewrite_cs(ctx, p, cs_obj, 0, color, 1);
  890. fz_always(ctx)
  891. pdf_drop_obj(ctx, cs_obj);
  892. fz_catch(ctx)
  893. fz_rethrow(ctx);
  894. }
  895. static void
  896. pdf_color_cs(fz_context *ctx, pdf_processor *proc, const char *name, fz_colorspace *cs)
  897. {
  898. pdf_color_processor *p = (pdf_color_processor*)proc;
  899. pdf_obj *cs_obj = pdf_new_name(ctx, name);
  900. float color[FZ_MAX_COLORS] = { 1 };
  901. fz_try(ctx)
  902. rewrite_cs(ctx, p, cs_obj, 0, color, 0);
  903. fz_always(ctx)
  904. pdf_drop_obj(ctx, cs_obj);
  905. fz_catch(ctx)
  906. fz_rethrow(ctx);
  907. }
  908. static void
  909. pdf_color_SC_pattern(fz_context *ctx, pdf_processor *proc, const char *name, pdf_pattern *pat, int n, float *color)
  910. {
  911. pdf_color_processor *p = (pdf_color_processor*)proc;
  912. float local_color[FZ_MAX_COLORS] = { 0 };
  913. pdf_obj *cs_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Pattern)), name);
  914. memcpy(local_color, color, sizeof(float) * n);
  915. rewrite_cs(ctx, p, cs_obj, n, local_color, 1);
  916. }
  917. static void
  918. pdf_color_sc_pattern(fz_context *ctx, pdf_processor *proc, const char *name, pdf_pattern *pat, int n, float *color)
  919. {
  920. pdf_color_processor *p = (pdf_color_processor*)proc;
  921. float local_color[FZ_MAX_COLORS] = { 0 };
  922. pdf_obj *cs_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Pattern)), name);
  923. memcpy(local_color, color, sizeof(float) * n);
  924. rewrite_cs(ctx, p, cs_obj, n, local_color, 0);
  925. }
  926. static void
  927. pdf_color_SC_shade(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade *shade)
  928. {
  929. pdf_color_processor *p = (pdf_color_processor*)proc;
  930. pdf_obj *orig;
  931. pdf_obj *dict = NULL;
  932. pdf_obj *dict2 = NULL;
  933. char new_name[MAX_REWRITTEN_NAME];
  934. pdf_obj *rewritten;
  935. fz_shade *new_shade = NULL;
  936. if (p->options->shade_rewrite == NULL)
  937. {
  938. /* Must copy shading over to new resources dict. */
  939. pdf_obj *old_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Shading)), name);
  940. pdf_obj *new_shading_dict = pdf_dict_get(ctx, p->rstack->new_rdb, PDF_NAME(Shading));
  941. if (new_shading_dict == NULL)
  942. pdf_dict_put_drop(ctx, p->rstack->new_rdb, PDF_NAME(Shading), new_shading_dict = pdf_new_dict(ctx, p->doc, 4));
  943. pdf_dict_puts(ctx, new_shading_dict, name, old_obj);
  944. if (p->chain->op_SC_shade)
  945. p->chain->op_SC_shade(ctx, p->chain, name, shade);
  946. return;
  947. }
  948. orig = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Pattern)), name);
  949. orig = pdf_dict_get(ctx, orig, PDF_NAME(Shading));
  950. new_shade = find_rewritten_shade(ctx, p, orig, new_name);
  951. if (new_shade)
  952. {
  953. /* Must copy shading over to new resources dict. */
  954. pdf_obj *old_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Shading)), name);
  955. pdf_obj *new_shading_dict = pdf_dict_get(ctx, p->rstack->new_rdb, PDF_NAME(Shading));
  956. if (new_shading_dict == NULL)
  957. pdf_dict_put_drop(ctx, p->rstack->new_rdb, PDF_NAME(Shading), new_shading_dict = pdf_new_dict(ctx, p->doc, 4));
  958. pdf_dict_puts(ctx, new_shading_dict, name, old_obj);
  959. if (p->chain->op_SC_shade)
  960. p->chain->op_SC_shade(ctx, p->chain, new_name, new_shade);
  961. return;
  962. }
  963. rewritten = pdf_recolor_shade(ctx, orig, p->options->shade_rewrite, p->options->opaque);
  964. fz_var(new_shade);
  965. fz_var(dict);
  966. fz_var(dict2);
  967. fz_try(ctx)
  968. {
  969. dict = pdf_new_dict(ctx, p->doc, 1);
  970. pdf_dict_put_int(ctx, dict, PDF_NAME(PatternType), 2);
  971. pdf_dict_put(ctx, dict, PDF_NAME(Shading), rewritten);
  972. dict2 = pdf_add_object(ctx, p->doc, dict);
  973. make_resource_instance(ctx, p, PDF_NAME(Pattern), "Pa", new_name, sizeof(new_name), dict2);
  974. new_shade = pdf_load_shading(ctx, p->doc, rewritten);
  975. /* Remember that we've done this one before. */
  976. push_rewritten_shade(ctx, p, orig, new_shade, new_name);
  977. if (p->chain->op_sh)
  978. p->chain->op_SC_shade(ctx, p->chain, new_name, new_shade);
  979. }
  980. fz_always(ctx)
  981. {
  982. fz_drop_shade(ctx, new_shade);
  983. pdf_drop_obj(ctx, rewritten);
  984. pdf_drop_obj(ctx, dict);
  985. pdf_drop_obj(ctx, dict2);
  986. }
  987. fz_catch(ctx)
  988. fz_rethrow(ctx);
  989. }
  990. static void
  991. pdf_color_sc_shade(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade *shade)
  992. {
  993. pdf_color_processor *p = (pdf_color_processor*)proc;
  994. pdf_obj *orig;
  995. pdf_obj *dict = NULL;
  996. pdf_obj *dict2 = NULL;
  997. char new_name[MAX_REWRITTEN_NAME];
  998. pdf_obj *rewritten;
  999. fz_shade *new_shade = NULL;
  1000. if (p->options->shade_rewrite == NULL)
  1001. {
  1002. /* Must copy shading over to new resources dict. */
  1003. pdf_obj *old_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Shading)), name);
  1004. pdf_obj *new_shading_dict = pdf_dict_get(ctx, p->rstack->new_rdb, PDF_NAME(Shading));
  1005. if (new_shading_dict == NULL)
  1006. pdf_dict_put_drop(ctx, p->rstack->new_rdb, PDF_NAME(Shading), new_shading_dict = pdf_new_dict(ctx, p->doc, 4));
  1007. pdf_dict_puts(ctx, new_shading_dict, name, old_obj);
  1008. if (p->chain->op_sc_shade)
  1009. p->chain->op_sc_shade(ctx, p->chain, name, shade);
  1010. return;
  1011. }
  1012. orig = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Pattern)), name);
  1013. orig = pdf_dict_get(ctx, orig, PDF_NAME(Shading));
  1014. new_shade = find_rewritten_shade(ctx, p, orig, new_name);
  1015. if (new_shade)
  1016. {
  1017. if (p->chain->op_sc_shade)
  1018. p->chain->op_sc_shade(ctx, p->chain, new_name, new_shade);
  1019. return;
  1020. }
  1021. rewritten = pdf_recolor_shade(ctx, orig, p->options->shade_rewrite, p->options->opaque);
  1022. fz_var(new_shade);
  1023. fz_var(dict);
  1024. fz_var(dict2);
  1025. fz_try(ctx)
  1026. {
  1027. dict = pdf_new_dict(ctx, p->doc, 1);
  1028. pdf_dict_put_int(ctx, dict, PDF_NAME(PatternType), 2);
  1029. pdf_dict_put(ctx, dict, PDF_NAME(Shading), rewritten);
  1030. dict2 = pdf_add_object(ctx, p->doc, dict);
  1031. make_resource_instance(ctx, p, PDF_NAME(Pattern), "Pa", new_name, sizeof(new_name), dict2);
  1032. new_shade = pdf_load_shading(ctx, p->doc, rewritten);
  1033. /* Remember that we've done this one before. */
  1034. push_rewritten_shade(ctx, p, orig, new_shade, new_name);
  1035. if (p->chain->op_sh)
  1036. p->chain->op_sc_shade(ctx, p->chain, new_name, new_shade);
  1037. }
  1038. fz_always(ctx)
  1039. {
  1040. fz_drop_shade(ctx, new_shade);
  1041. pdf_drop_obj(ctx, rewritten);
  1042. pdf_drop_obj(ctx, dict);
  1043. pdf_drop_obj(ctx, dict2);
  1044. }
  1045. fz_catch(ctx)
  1046. fz_rethrow(ctx);
  1047. }
  1048. static void
  1049. pdf_color_SC_color(fz_context *ctx, pdf_processor *proc, int n, float *color)
  1050. {
  1051. pdf_color_processor *p = (pdf_color_processor*)proc;
  1052. float local_color[FZ_MAX_COLORS] = { 0 };
  1053. pdf_obj *cs_obj = p->gstate->cs_stroke;
  1054. memcpy(local_color, color, sizeof(float) * n);
  1055. rewrite_cs(ctx, p, cs_obj, n, local_color, 1);
  1056. }
  1057. static void
  1058. pdf_color_sc_color(fz_context *ctx, pdf_processor *proc, int n, float *color)
  1059. {
  1060. pdf_color_processor *p = (pdf_color_processor*)proc;
  1061. float local_color[FZ_MAX_COLORS] = { 0 };
  1062. pdf_obj *cs_obj = p->gstate->cs_fill;
  1063. memcpy(local_color, color, sizeof(float) * n);
  1064. rewrite_cs(ctx, p, cs_obj, n, local_color, 0);
  1065. }
  1066. static void
  1067. pdf_color_G(fz_context *ctx, pdf_processor *proc, float g)
  1068. {
  1069. pdf_color_processor *p = (pdf_color_processor*)proc;
  1070. float local_color[FZ_MAX_COLORS] = { g };
  1071. rewrite_cs(ctx, p, PDF_NAME(DeviceGray), 1, local_color, 1);
  1072. }
  1073. static void
  1074. pdf_color_g(fz_context *ctx, pdf_processor *proc, float g)
  1075. {
  1076. pdf_color_processor *p = (pdf_color_processor*)proc;
  1077. float local_color[FZ_MAX_COLORS] = { g };
  1078. rewrite_cs(ctx, p, PDF_NAME(DeviceGray), 1, local_color, 0);
  1079. }
  1080. static void
  1081. pdf_color_RG(fz_context *ctx, pdf_processor *proc, float r, float g, float b)
  1082. {
  1083. pdf_color_processor *p = (pdf_color_processor*)proc;
  1084. float local_color[FZ_MAX_COLORS] = { r, g, b };
  1085. rewrite_cs(ctx, p, PDF_NAME(DeviceRGB), 3, local_color, 1);
  1086. }
  1087. static void
  1088. pdf_color_rg(fz_context *ctx, pdf_processor *proc, float r, float g, float b)
  1089. {
  1090. pdf_color_processor *p = (pdf_color_processor*)proc;
  1091. float local_color[FZ_MAX_COLORS] = { r, g, b };
  1092. rewrite_cs(ctx, p, PDF_NAME(DeviceRGB), 3, local_color, 0);
  1093. }
  1094. static void
  1095. pdf_color_K(fz_context *ctx, pdf_processor *proc, float c, float m, float y, float k)
  1096. {
  1097. pdf_color_processor *p = (pdf_color_processor*)proc;
  1098. float local_color[FZ_MAX_COLORS] = { c, m, y, k };
  1099. rewrite_cs(ctx, p, PDF_NAME(DeviceCMYK), 4, local_color, 1);
  1100. }
  1101. static void
  1102. pdf_color_k(fz_context *ctx, pdf_processor *proc, float c, float m, float y, float k)
  1103. {
  1104. pdf_color_processor *p = (pdf_color_processor*)proc;
  1105. float local_color[FZ_MAX_COLORS] = { c, m, y, k };
  1106. rewrite_cs(ctx, p, PDF_NAME(DeviceCMYK), 4, local_color, 0);
  1107. }
  1108. /* shadings, images, xobjects */
  1109. static void
  1110. pdf_color_BI(fz_context *ctx, pdf_processor *proc, fz_image *image, const char *colorspace)
  1111. {
  1112. pdf_color_processor *p = (pdf_color_processor*)proc;
  1113. if (image->imagemask)
  1114. {
  1115. /* Imagemasks require the color to have been set. */
  1116. if (p->gstate->unmarked & UNMARKED_FILL)
  1117. mark_fill(ctx, p);
  1118. }
  1119. fz_keep_image(ctx, image);
  1120. if (p->options->image_rewrite)
  1121. p->options->image_rewrite(ctx, p->options->opaque, &image, p->gstate->ctm, NULL);
  1122. if (p->chain->op_BI)
  1123. p->chain->op_BI(ctx, p->chain, image, colorspace);
  1124. fz_drop_image(ctx, image);
  1125. }
  1126. static void
  1127. pdf_color_sh(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade *shade)
  1128. {
  1129. pdf_color_processor *p = (pdf_color_processor*)proc;
  1130. pdf_obj *orig;
  1131. char new_name[MAX_REWRITTEN_NAME];
  1132. pdf_obj *rewritten;
  1133. fz_shade *new_shade = NULL;
  1134. if (p->options->shade_rewrite == NULL)
  1135. {
  1136. /* Must copy shading over to new resources dict. */
  1137. pdf_obj *old_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Shading)), name);
  1138. pdf_obj *new_shading_dict = pdf_dict_get(ctx, p->rstack->new_rdb, PDF_NAME(Shading));
  1139. if (new_shading_dict == NULL)
  1140. pdf_dict_put_drop(ctx, p->rstack->new_rdb, PDF_NAME(Shading), new_shading_dict = pdf_new_dict(ctx, p->doc, 4));
  1141. pdf_dict_puts(ctx, new_shading_dict, name, old_obj);
  1142. if (p->chain->op_sh)
  1143. p->chain->op_sh(ctx, p->chain, name, shade);
  1144. return;
  1145. }
  1146. orig = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(Shading)), name);
  1147. new_shade = find_rewritten_shade(ctx, p, orig, new_name);
  1148. if (new_shade)
  1149. {
  1150. if (p->chain->op_sh)
  1151. p->chain->op_sh(ctx, p->chain, new_name, new_shade);
  1152. return;
  1153. }
  1154. rewritten = pdf_recolor_shade(ctx, orig, p->options->shade_rewrite, p->options->opaque);
  1155. fz_var(new_shade);
  1156. fz_try(ctx)
  1157. {
  1158. make_resource_instance(ctx, p, PDF_NAME(Shading), "Sh", new_name, sizeof(new_name), rewritten);
  1159. new_shade = pdf_load_shading(ctx, p->doc, rewritten);
  1160. /* Remember that we've done this one before. */
  1161. push_rewritten_shade(ctx, p, orig, new_shade, new_name);
  1162. if (p->chain->op_sh)
  1163. p->chain->op_sh(ctx, p->chain, new_name, new_shade);
  1164. }
  1165. fz_always(ctx)
  1166. {
  1167. fz_drop_shade(ctx, new_shade);
  1168. pdf_drop_obj(ctx, rewritten);
  1169. }
  1170. fz_catch(ctx)
  1171. fz_rethrow(ctx);
  1172. }
  1173. static void
  1174. pdf_color_Do_image(fz_context *ctx, pdf_processor *proc, const char *name, fz_image *image)
  1175. {
  1176. pdf_color_processor *p = (pdf_color_processor*)proc;
  1177. fz_image *orig = image;
  1178. char new_name[MAX_REWRITTEN_NAME];
  1179. pdf_obj *im_obj = pdf_dict_gets(ctx, pdf_dict_get(ctx, p->rstack->old_rdb, PDF_NAME(XObject)), name);
  1180. /* Have we done this one before? */
  1181. if (!p->options->repeated_image_rewrite)
  1182. {
  1183. image = find_rewritten_image(ctx, p, im_obj, new_name);
  1184. if (image)
  1185. {
  1186. if (p->chain->op_Do_image)
  1187. p->chain->op_Do_image(ctx, p->chain, new_name, image);
  1188. return;
  1189. }
  1190. }
  1191. image = orig;
  1192. fz_keep_image(ctx, image);
  1193. pdf_keep_obj(ctx, im_obj);
  1194. fz_var(im_obj);
  1195. fz_try(ctx)
  1196. {
  1197. if (image->imagemask)
  1198. {
  1199. /* Imagemasks require the color to have been set. */
  1200. if (p->gstate->unmarked & UNMARKED_FILL)
  1201. mark_fill(ctx, p);
  1202. }
  1203. else
  1204. {
  1205. if (p->options->image_rewrite)
  1206. p->options->image_rewrite(ctx, p->options->opaque, &image, p->gstate->ctm, im_obj);
  1207. }
  1208. /* If it's been rewritten add the new one, otherwise copy the old one across. */
  1209. if (image != orig)
  1210. {
  1211. pdf_drop_obj(ctx, im_obj);
  1212. im_obj = NULL;
  1213. im_obj = pdf_add_image(ctx, p->doc, image);
  1214. }
  1215. make_resource_instance(ctx, p, PDF_NAME(XObject), "Im", new_name, sizeof(new_name), im_obj);
  1216. if (!p->options->repeated_image_rewrite)
  1217. {
  1218. /* Remember that we've done this one before. */
  1219. push_rewritten_image(ctx, p, im_obj, image, new_name);
  1220. }
  1221. if (p->chain->op_Do_image)
  1222. p->chain->op_Do_image(ctx, p->chain, new_name, image);
  1223. }
  1224. fz_always(ctx)
  1225. {
  1226. pdf_drop_obj(ctx, im_obj);
  1227. fz_drop_image(ctx, image);
  1228. }
  1229. fz_catch(ctx)
  1230. fz_rethrow(ctx);
  1231. }
  1232. static void
  1233. pdf_color_Do_form(fz_context *ctx, pdf_processor *proc, const char *name, pdf_obj *xobj)
  1234. {
  1235. pdf_color_processor *p = (pdf_color_processor*)proc;
  1236. char new_name[MAX_REWRITTEN_NAME];
  1237. pdf_obj *xres;
  1238. pdf_obj *new_xres = NULL;
  1239. fz_var(new_xres);
  1240. /* FIXME: Ideally we'd look at p->global_options->instance_forms here
  1241. * to avoid flattening all the XObjects. This is non-trivial, and
  1242. * currently incomplete, hence disabled. We have to find some way to
  1243. * tell the underlying filters (in particular the output filter) that
  1244. * we are in a new XObject (so the output filter can start a new buffer).
  1245. * Maybe it could look at p->global_options->instance_forms too on a
  1246. * push/pop of the resources? For now, let's just leave it disabled. */
  1247. if (0 && p->global_options->instance_forms)
  1248. {
  1249. make_resource_instance(ctx, p, PDF_NAME(XObject), "Xo", new_name, sizeof(new_name), xobj);
  1250. xres = pdf_xobject_resources(ctx, xobj);
  1251. fz_try(ctx)
  1252. pdf_process_contents(ctx, (pdf_processor *)p, p->doc, xres, xobj, NULL, &new_xres);
  1253. fz_catch(ctx)
  1254. {
  1255. pdf_drop_obj(ctx, new_xres);
  1256. fz_rethrow(ctx);
  1257. }
  1258. pdf_dict_put_drop(ctx, xobj, PDF_NAME(Resources), new_xres);
  1259. if (p->chain->op_Do_form)
  1260. p->chain->op_Do_form(ctx, p->chain, new_name, xobj);
  1261. }
  1262. else
  1263. {
  1264. /* In this case, we just copy the XObject across (renaming it). Our caller will arrange to
  1265. * filter it. */
  1266. make_resource_instance(ctx, p, PDF_NAME(XObject), "Xo", new_name, sizeof(new_name), xobj);
  1267. if (p->chain->op_Do_form)
  1268. p->chain->op_Do_form(ctx, p->chain, new_name, xobj);
  1269. }
  1270. }
  1271. /* marked content */
  1272. static void
  1273. pdf_color_MP(fz_context *ctx, pdf_processor *proc, const char *tag)
  1274. {
  1275. pdf_color_processor *p = (pdf_color_processor*)proc;
  1276. if (p->chain->op_MP)
  1277. p->chain->op_MP(ctx, p->chain, tag);
  1278. }
  1279. static void
  1280. pdf_color_DP(fz_context *ctx, pdf_processor *proc, const char *tag, pdf_obj *raw, pdf_obj *cooked)
  1281. {
  1282. pdf_color_processor *p = (pdf_color_processor*)proc;
  1283. if (p->chain->op_DP)
  1284. p->chain->op_DP(ctx, p->chain, tag, raw, cooked);
  1285. }
  1286. static void
  1287. pdf_color_BMC(fz_context *ctx, pdf_processor *proc, const char *tag)
  1288. {
  1289. pdf_color_processor *p = (pdf_color_processor*)proc;
  1290. if (p->chain->op_BMC)
  1291. p->chain->op_BMC(ctx, p->chain, tag);
  1292. }
  1293. static void
  1294. pdf_color_BDC(fz_context *ctx, pdf_processor *proc, const char *tag, pdf_obj *raw, pdf_obj *cooked)
  1295. {
  1296. pdf_color_processor *p = (pdf_color_processor*)proc;
  1297. if (p->chain->op_BDC)
  1298. p->chain->op_BDC(ctx, p->chain, tag, raw, cooked);
  1299. }
  1300. static void
  1301. pdf_color_EMC(fz_context *ctx, pdf_processor *proc)
  1302. {
  1303. pdf_color_processor *p = (pdf_color_processor*)proc;
  1304. if (p->chain->op_EMC)
  1305. p->chain->op_EMC(ctx, p->chain);
  1306. }
  1307. /* compatibility */
  1308. static void
  1309. pdf_color_BX(fz_context *ctx, pdf_processor *proc)
  1310. {
  1311. pdf_color_processor *p = (pdf_color_processor*)proc;
  1312. if (p->chain->op_BX)
  1313. p->chain->op_BX(ctx, p->chain);
  1314. }
  1315. static void
  1316. pdf_color_EX(fz_context *ctx, pdf_processor *proc)
  1317. {
  1318. pdf_color_processor *p = (pdf_color_processor*)proc;
  1319. if (p->chain->op_EX)
  1320. p->chain->op_EX(ctx, p->chain);
  1321. }
  1322. static void
  1323. pdf_color_END(fz_context *ctx, pdf_processor *proc)
  1324. {
  1325. pdf_color_processor *p = (pdf_color_processor*)proc;
  1326. if (p->chain->op_END)
  1327. p->chain->op_END(ctx, p->chain);
  1328. }
  1329. static void
  1330. pdf_close_color_processor(fz_context *ctx, pdf_processor *proc)
  1331. {
  1332. pdf_color_processor *p = (pdf_color_processor*)proc;
  1333. pdf_close_processor(ctx, p->chain);
  1334. }
  1335. static void
  1336. pdf_drop_color_processor(fz_context *ctx, pdf_processor *proc)
  1337. {
  1338. pdf_color_processor *p = (pdf_color_processor*)proc;
  1339. gstate_stack *gs = p->gstate;
  1340. while (gs)
  1341. {
  1342. gstate_stack *gs_next = gs->next;
  1343. pdf_drop_obj(ctx, gs->cs_fill);
  1344. pdf_drop_obj(ctx, gs->cs_stroke);
  1345. fz_free(ctx, gs);
  1346. gs = gs_next;
  1347. }
  1348. drop_rewritten_images(ctx, p);
  1349. drop_rewritten_shades(ctx, p);
  1350. pdf_drop_document(ctx, p->doc);
  1351. }
  1352. static void
  1353. pdf_color_push_resources(fz_context *ctx, pdf_processor *proc, pdf_obj *res)
  1354. {
  1355. pdf_color_processor *p = (pdf_color_processor*)proc;
  1356. resources_stack *stk = fz_malloc_struct(ctx, resources_stack);
  1357. pdf_obj *obj;
  1358. p->gstate->unmarked = UNMARKED_STROKE | UNMARKED_FILL;
  1359. stk->next = p->rstack;
  1360. p->rstack = stk;
  1361. fz_try(ctx)
  1362. {
  1363. stk->old_rdb = pdf_keep_obj(ctx, res);
  1364. /* At the moment we know that we'll always be flattening XObjects.
  1365. * So only the top level 'push' makes a new resource dict. Any
  1366. * subsequent one will share the previous levels one. */
  1367. if (stk->next)
  1368. stk->new_rdb = pdf_keep_obj(ctx, stk->next->new_rdb);
  1369. else
  1370. stk->new_rdb = pdf_new_dict(ctx, p->doc, 1);
  1371. obj = pdf_dict_get(ctx, res, PDF_NAME(Properties));
  1372. if (obj)
  1373. pdf_dict_put(ctx, stk->new_rdb, PDF_NAME(Properties), obj);
  1374. obj = pdf_dict_get(ctx, res, PDF_NAME(ExtGState));
  1375. if (obj)
  1376. pdf_dict_put(ctx, stk->new_rdb, PDF_NAME(ExtGState), obj);
  1377. obj = pdf_dict_get(ctx, res, PDF_NAME(Font));
  1378. if (obj)
  1379. pdf_dict_put(ctx, stk->new_rdb, PDF_NAME(Font), obj);
  1380. pdf_processor_push_resources(ctx, p->chain, stk->new_rdb);
  1381. }
  1382. fz_catch(ctx)
  1383. {
  1384. pdf_drop_obj(ctx, stk->old_rdb);
  1385. pdf_drop_obj(ctx, stk->new_rdb);
  1386. fz_free(ctx, stk);
  1387. p->rstack = stk->next;
  1388. fz_rethrow(ctx);
  1389. }
  1390. }
  1391. static pdf_obj *
  1392. pdf_color_pop_resources(fz_context *ctx, pdf_processor *proc)
  1393. {
  1394. pdf_color_processor *p = (pdf_color_processor*)proc;
  1395. resources_stack *stk = p->rstack;
  1396. p->rstack = stk->next;
  1397. pdf_drop_obj(ctx, stk->old_rdb);
  1398. pdf_drop_obj(ctx, stk->new_rdb);
  1399. fz_free(ctx, stk);
  1400. return pdf_processor_pop_resources(ctx, p->chain);
  1401. }
  1402. static void
  1403. pdf_reset_color_processor(fz_context *ctx, pdf_processor *proc)
  1404. {
  1405. pdf_color_processor *p = (pdf_color_processor*)proc;
  1406. pdf_reset_processor(ctx, p->chain);
  1407. }
  1408. pdf_processor *
  1409. pdf_new_color_filter(
  1410. fz_context *ctx,
  1411. pdf_document *doc,
  1412. pdf_processor *chain,
  1413. int struct_parents,
  1414. fz_matrix transform,
  1415. pdf_filter_options *global_options,
  1416. void *options_)
  1417. {
  1418. pdf_color_processor *proc = pdf_new_processor(ctx, sizeof * proc);
  1419. pdf_color_filter_options *options = (pdf_color_filter_options *)options_;
  1420. proc->super.close_processor = pdf_close_color_processor;
  1421. proc->super.drop_processor = pdf_drop_color_processor;
  1422. proc->super.reset_processor = pdf_reset_color_processor;
  1423. proc->super.push_resources = pdf_color_push_resources;
  1424. proc->super.pop_resources = pdf_color_pop_resources;
  1425. /* general graphics state */
  1426. proc->super.op_w = pdf_color_w;
  1427. proc->super.op_j = pdf_color_j;
  1428. proc->super.op_J = pdf_color_J;
  1429. proc->super.op_M = pdf_color_M;
  1430. proc->super.op_d = pdf_color_d;
  1431. proc->super.op_ri = pdf_color_ri;
  1432. proc->super.op_i = pdf_color_i;
  1433. proc->super.op_gs_begin = pdf_color_gs_begin;
  1434. proc->super.op_gs_end = pdf_color_gs_end;
  1435. /* transparency graphics state */
  1436. proc->super.op_gs_BM = pdf_color_gs_BM;
  1437. proc->super.op_gs_CA = pdf_color_gs_CA;
  1438. proc->super.op_gs_ca = pdf_color_gs_ca;
  1439. proc->super.op_gs_SMask = pdf_color_gs_SMask;
  1440. /* special graphics state */
  1441. proc->super.op_q = pdf_color_q;
  1442. proc->super.op_Q = pdf_color_Q;
  1443. proc->super.op_cm = pdf_color_cm;
  1444. /* path construction */
  1445. proc->super.op_m = pdf_color_m;
  1446. proc->super.op_l = pdf_color_l;
  1447. proc->super.op_c = pdf_color_c;
  1448. proc->super.op_v = pdf_color_v;
  1449. proc->super.op_y = pdf_color_y;
  1450. proc->super.op_h = pdf_color_h;
  1451. proc->super.op_re = pdf_color_re;
  1452. /* path painting */
  1453. proc->super.op_S = pdf_color_S;
  1454. proc->super.op_s = pdf_color_s;
  1455. proc->super.op_F = pdf_color_F;
  1456. proc->super.op_f = pdf_color_f;
  1457. proc->super.op_fstar = pdf_color_fstar;
  1458. proc->super.op_B = pdf_color_B;
  1459. proc->super.op_Bstar = pdf_color_Bstar;
  1460. proc->super.op_b = pdf_color_b;
  1461. proc->super.op_bstar = pdf_color_bstar;
  1462. proc->super.op_n = pdf_color_n;
  1463. /* clipping paths */
  1464. proc->super.op_W = pdf_color_W;
  1465. proc->super.op_Wstar = pdf_color_Wstar;
  1466. /* text objects */
  1467. proc->super.op_BT = pdf_color_BT;
  1468. proc->super.op_ET = pdf_color_ET;
  1469. /* text state */
  1470. proc->super.op_Tc = pdf_color_Tc;
  1471. proc->super.op_Tw = pdf_color_Tw;
  1472. proc->super.op_Tz = pdf_color_Tz;
  1473. proc->super.op_TL = pdf_color_TL;
  1474. proc->super.op_Tf = pdf_color_Tf;
  1475. proc->super.op_Tr = pdf_color_Tr;
  1476. proc->super.op_Ts = pdf_color_Ts;
  1477. /* text positioning */
  1478. proc->super.op_Td = pdf_color_Td;
  1479. proc->super.op_TD = pdf_color_TD;
  1480. proc->super.op_Tm = pdf_color_Tm;
  1481. proc->super.op_Tstar = pdf_color_Tstar;
  1482. /* text showing */
  1483. proc->super.op_TJ = pdf_color_TJ;
  1484. proc->super.op_Tj = pdf_color_Tj;
  1485. proc->super.op_squote = pdf_color_squote;
  1486. proc->super.op_dquote = pdf_color_dquote;
  1487. /* type 3 fonts */
  1488. proc->super.op_d0 = pdf_color_d0;
  1489. proc->super.op_d1 = pdf_color_d1;
  1490. /* color */
  1491. proc->super.op_CS = pdf_color_CS;
  1492. proc->super.op_cs = pdf_color_cs;
  1493. proc->super.op_SC_color = pdf_color_SC_color;
  1494. proc->super.op_sc_color = pdf_color_sc_color;
  1495. proc->super.op_SC_pattern = pdf_color_SC_pattern;
  1496. proc->super.op_sc_pattern = pdf_color_sc_pattern;
  1497. proc->super.op_SC_shade = pdf_color_SC_shade;
  1498. proc->super.op_sc_shade = pdf_color_sc_shade;
  1499. proc->super.op_G = pdf_color_G;
  1500. proc->super.op_g = pdf_color_g;
  1501. proc->super.op_RG = pdf_color_RG;
  1502. proc->super.op_rg = pdf_color_rg;
  1503. proc->super.op_K = pdf_color_K;
  1504. proc->super.op_k = pdf_color_k;
  1505. /* shadings, images, xobjects */
  1506. proc->super.op_BI = pdf_color_BI;
  1507. proc->super.op_sh = pdf_color_sh;
  1508. proc->super.op_Do_image = pdf_color_Do_image;
  1509. proc->super.op_Do_form = pdf_color_Do_form;
  1510. /* marked content */
  1511. proc->super.op_MP = pdf_color_MP;
  1512. proc->super.op_DP = pdf_color_DP;
  1513. proc->super.op_BMC = pdf_color_BMC;
  1514. proc->super.op_BDC = pdf_color_BDC;
  1515. proc->super.op_EMC = pdf_color_EMC;
  1516. /* compatibility */
  1517. proc->super.op_BX = pdf_color_BX;
  1518. proc->super.op_EX = pdf_color_EX;
  1519. /* extgstate */
  1520. proc->super.op_gs_OP = pdf_color_gs_OP;
  1521. proc->super.op_gs_op = pdf_color_gs_op;
  1522. proc->super.op_gs_OPM = pdf_color_gs_OPM;
  1523. proc->super.op_gs_UseBlackPtComp = pdf_color_gs_UseBlackPtComp;
  1524. proc->super.op_END = pdf_color_END;
  1525. fz_try(ctx)
  1526. proc->gstate = fz_malloc_struct(ctx, gstate_stack);
  1527. fz_catch(ctx)
  1528. {
  1529. fz_free(ctx, proc);
  1530. fz_rethrow(ctx);
  1531. }
  1532. proc->gstate->ctm = fz_identity;
  1533. proc->doc = pdf_keep_document(ctx, doc);
  1534. proc->chain = chain;
  1535. proc->global_options = global_options;
  1536. proc->options = options;
  1537. proc->super.requirements = PDF_PROCESSOR_REQUIRES_DECODED_IMAGES | proc->chain->requirements;
  1538. return (pdf_processor*)proc;
  1539. }