pdf-op-run.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383
  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 <string.h>
  25. #include <math.h>
  26. #include "mupdf/ucdn.h"
  27. #define TILE
  28. /* Enable this to watch changes in the structure stack. */
  29. #undef DEBUG_STRUCTURE
  30. /*
  31. * Emit graphics calls to device.
  32. */
  33. typedef struct pdf_run_processor pdf_run_processor;
  34. static void pdf_run_xobject(fz_context *ctx, pdf_run_processor *proc, pdf_obj *xobj, pdf_obj *page_resources, fz_matrix transform, int is_smask);
  35. enum
  36. {
  37. PDF_FILL,
  38. PDF_STROKE,
  39. };
  40. enum
  41. {
  42. PDF_MAT_NONE,
  43. PDF_MAT_COLOR,
  44. PDF_MAT_PATTERN,
  45. PDF_MAT_SHADE,
  46. };
  47. typedef struct
  48. {
  49. int kind;
  50. fz_colorspace *colorspace;
  51. pdf_pattern *pattern;
  52. fz_shade *shade;
  53. int gstate_num;
  54. fz_color_params color_params;
  55. float alpha;
  56. float v[FZ_MAX_COLORS];
  57. } pdf_material;
  58. struct pdf_gstate
  59. {
  60. fz_matrix ctm;
  61. int clip_depth;
  62. /* path stroking */
  63. fz_stroke_state *stroke_state;
  64. /* materials */
  65. pdf_material stroke;
  66. pdf_material fill;
  67. /* pattern paint type 2 */
  68. int ismask;
  69. /* text state */
  70. pdf_text_state text;
  71. /* transparency */
  72. int blendmode;
  73. pdf_obj *softmask;
  74. pdf_obj *softmask_resources;
  75. pdf_obj *softmask_tr;
  76. fz_matrix softmask_ctm;
  77. fz_colorspace *softmask_cs;
  78. float softmask_bc[FZ_MAX_COLORS];
  79. int luminosity;
  80. };
  81. typedef struct resources_stack
  82. {
  83. struct resources_stack *next;
  84. pdf_obj *resources;
  85. } resources_stack;
  86. typedef struct marked_content_stack
  87. {
  88. struct marked_content_stack *next;
  89. pdf_obj *tag;
  90. pdf_obj *val;
  91. int structure_pushed;
  92. } marked_content_stack;
  93. typedef struct begin_layer_stack
  94. {
  95. struct begin_layer_stack *next;
  96. char *layer;
  97. } begin_layer_stack;
  98. struct pdf_run_processor
  99. {
  100. pdf_processor super;
  101. pdf_document *doc;
  102. fz_device *dev;
  103. fz_cookie *cookie;
  104. fz_default_colorspaces *default_cs;
  105. resources_stack *rstack;
  106. /* path object state */
  107. fz_path *path;
  108. int clip;
  109. int clip_even_odd;
  110. /* text object state */
  111. pdf_text_object_state tos;
  112. int bidi;
  113. /* graphics state */
  114. pdf_gstate *gstate;
  115. int gcap;
  116. int gtop;
  117. int gbot;
  118. int gparent;
  119. /* xobject cycle detector */
  120. pdf_cycle_list *cycle;
  121. pdf_obj *role_map;
  122. marked_content_stack *marked_content;
  123. pdf_obj *mcid_sent;
  124. pdf_obj *pending_mcid_pop;
  125. int struct_parent;
  126. int broken_struct_tree;
  127. /* Pending begin layers */
  128. begin_layer_stack *begin_layer;
  129. begin_layer_stack **next_begin_layer;
  130. int mc_depth;
  131. /* The nest_mark array holds a record of the way in which clips and
  132. * marked content are nested to ensure we pop stuff in the same order
  133. * that we push it - i.e. to keep calls nested nicely. An entry x,
  134. * where x >= 0 represents that a push has happened for mc_depth == x.
  135. * An entry x, where x < 0 means that -x clips have happened at this
  136. * position. */
  137. int nest_depth;
  138. int nest_mark[1024];
  139. };
  140. /* Forward definition */
  141. static void
  142. pop_any_pending_mcid_changes(fz_context *ctx, pdf_run_processor *pr);
  143. static void
  144. push_begin_layer(fz_context *ctx, pdf_run_processor *proc, const char *str)
  145. {
  146. begin_layer_stack *s = fz_malloc_struct(ctx, begin_layer_stack);
  147. fz_try(ctx)
  148. s->layer = fz_strdup(ctx, str);
  149. fz_catch(ctx)
  150. {
  151. fz_free(ctx, s);
  152. fz_rethrow(ctx);
  153. }
  154. s->next = NULL;
  155. *proc->next_begin_layer = s;
  156. proc->next_begin_layer = &s->next;
  157. }
  158. static void
  159. flush_begin_layer(fz_context *ctx, pdf_run_processor *proc)
  160. {
  161. begin_layer_stack *s;
  162. while (proc->begin_layer)
  163. {
  164. s = proc->begin_layer;
  165. if (proc->nest_depth == nelem(proc->nest_mark))
  166. fz_throw(ctx, FZ_ERROR_LIMIT, "layer/clip nesting too deep");
  167. proc->nest_mark[proc->nest_depth++] = ++proc->mc_depth;
  168. fz_begin_layer(ctx, proc->dev, s->layer);
  169. proc->begin_layer = s->next;
  170. fz_free(ctx, s->layer);
  171. fz_free(ctx, s);
  172. }
  173. proc->next_begin_layer = &proc->begin_layer;
  174. }
  175. static void nest_layer_clip(fz_context *ctx, pdf_run_processor *proc)
  176. {
  177. if (proc->nest_depth == nelem(proc->nest_mark))
  178. fz_throw(ctx, FZ_ERROR_LIMIT, "layer/clip nesting too deep");
  179. if (proc->nest_depth > 0 && proc->nest_mark[proc->nest_depth-1] < 0)
  180. {
  181. /* The last mark was a clip. Just increase that count. */
  182. proc->nest_mark[proc->nest_depth-1]--;
  183. }
  184. else
  185. {
  186. /* Create a new entry for a single clip. */
  187. proc->nest_mark[proc->nest_depth++] = -1;
  188. }
  189. }
  190. static void
  191. do_end_layer(fz_context *ctx, pdf_run_processor *proc)
  192. {
  193. if (proc->nest_depth > 0 && proc->nest_mark[proc->nest_depth-1] == proc->mc_depth)
  194. {
  195. fz_end_layer(ctx, proc->dev);
  196. proc->nest_depth--;
  197. }
  198. else
  199. {
  200. /* If EMC is unbalanced with q/Q, we will emit the end layer
  201. * device call before or after the Q operator instead of its true location
  202. */
  203. fz_warn(ctx, "invalid marked content and clip nesting");
  204. }
  205. if (proc->mc_depth > 0)
  206. proc->mc_depth--;
  207. }
  208. typedef struct
  209. {
  210. pdf_obj *softmask;
  211. fz_colorspace *softmask_cs;
  212. pdf_obj *page_resources;
  213. fz_matrix ctm;
  214. } softmask_save;
  215. static fz_function *
  216. load_transfer_function(fz_context *ctx, pdf_obj *obj)
  217. {
  218. if (obj == NULL || pdf_name_eq(ctx, obj, PDF_NAME(Identity)))
  219. return NULL;
  220. return (fz_function *)pdf_load_function(ctx, obj, 1, 1);
  221. }
  222. static pdf_gstate *
  223. begin_softmask(fz_context *ctx, pdf_run_processor *pr, softmask_save *save, fz_rect bbox)
  224. {
  225. pdf_gstate *gstate = pr->gstate + pr->gtop;
  226. pdf_obj *softmask = gstate->softmask;
  227. fz_colorspace *softmask_cs = gstate->softmask_cs;
  228. fz_rect mask_bbox;
  229. fz_matrix tos_save[2], save_ctm;
  230. fz_matrix mask_matrix;
  231. fz_colorspace *mask_colorspace;
  232. int saved_blendmode;
  233. fz_function *tr = NULL;
  234. fz_var(tr);
  235. save->softmask = softmask;
  236. if (softmask == NULL)
  237. return gstate;
  238. save->softmask_cs = softmask_cs;
  239. save->page_resources = gstate->softmask_resources;
  240. save->ctm = gstate->softmask_ctm;
  241. save_ctm = gstate->ctm;
  242. mask_bbox = pdf_xobject_bbox(ctx, softmask);
  243. mask_matrix = pdf_xobject_matrix(ctx, softmask);
  244. pdf_tos_save(ctx, &pr->tos, tos_save);
  245. mask_colorspace = gstate->softmask_cs;
  246. if (gstate->luminosity && !mask_colorspace)
  247. mask_colorspace = fz_device_gray(ctx);
  248. if (gstate->luminosity)
  249. mask_bbox = fz_infinite_rect;
  250. else
  251. {
  252. mask_bbox = fz_transform_rect(mask_bbox, mask_matrix);
  253. mask_bbox = fz_transform_rect(mask_bbox, gstate->softmask_ctm);
  254. }
  255. mask_bbox = fz_intersect_rect(mask_bbox, bbox);
  256. gstate->softmask = NULL;
  257. gstate->softmask_cs = NULL;
  258. gstate->softmask_resources = NULL;
  259. gstate->ctm = gstate->softmask_ctm;
  260. saved_blendmode = gstate->blendmode;
  261. fz_try(ctx)
  262. {
  263. if (gstate->softmask_tr)
  264. {
  265. tr = load_transfer_function(ctx, gstate->softmask_tr);
  266. pdf_drop_obj(ctx, gstate->softmask_tr);
  267. gstate->softmask_tr = NULL;
  268. }
  269. fz_begin_mask(ctx, pr->dev, mask_bbox, gstate->luminosity, mask_colorspace, gstate->softmask_bc, gstate->fill.color_params);
  270. gstate->blendmode = 0;
  271. pdf_run_xobject(ctx, pr, softmask, save->page_resources, fz_identity, 1);
  272. gstate = pr->gstate + pr->gtop;
  273. gstate->blendmode = saved_blendmode;
  274. fz_end_mask_tr(ctx, pr->dev, tr);
  275. }
  276. fz_always(ctx)
  277. {
  278. fz_drop_function(ctx, tr);
  279. }
  280. fz_catch(ctx)
  281. fz_rethrow(ctx);
  282. pdf_tos_restore(ctx, &pr->tos, tos_save);
  283. gstate = pr->gstate + pr->gtop;
  284. gstate->ctm = save_ctm;
  285. return gstate;
  286. }
  287. static void
  288. end_softmask(fz_context *ctx, pdf_run_processor *pr, softmask_save *save)
  289. {
  290. pdf_gstate *gstate = pr->gstate + pr->gtop;
  291. if (save->softmask == NULL)
  292. return;
  293. gstate->softmask = save->softmask;
  294. gstate->softmask_cs = save->softmask_cs;
  295. gstate->softmask_resources = save->page_resources;
  296. gstate->softmask_ctm = save->ctm;
  297. save->softmask = NULL;
  298. save->page_resources = NULL;
  299. fz_pop_clip(ctx, pr->dev);
  300. }
  301. static pdf_gstate *
  302. pdf_begin_group(fz_context *ctx, pdf_run_processor *pr, fz_rect bbox, softmask_save *softmask)
  303. {
  304. pdf_gstate *gstate = begin_softmask(ctx, pr, softmask, bbox);
  305. if (gstate->blendmode)
  306. fz_begin_group(ctx, pr->dev, bbox, NULL, 0, 0, gstate->blendmode, 1);
  307. return pr->gstate + pr->gtop;
  308. }
  309. static void
  310. pdf_end_group(fz_context *ctx, pdf_run_processor *pr, softmask_save *softmask)
  311. {
  312. pdf_gstate *gstate = pr->gstate + pr->gtop;
  313. if (gstate->blendmode)
  314. fz_end_group(ctx, pr->dev);
  315. end_softmask(ctx, pr, softmask);
  316. }
  317. static void
  318. pdf_show_shade(fz_context *ctx, pdf_run_processor *pr, fz_shade *shd)
  319. {
  320. pdf_gstate *gstate = pr->gstate + pr->gtop;
  321. fz_rect bbox;
  322. softmask_save softmask = { NULL };
  323. if (pr->super.hidden)
  324. return;
  325. bbox = fz_bound_shade(ctx, shd, gstate->ctm);
  326. fz_try(ctx)
  327. {
  328. gstate = pdf_begin_group(ctx, pr, bbox, &softmask);
  329. /* FIXME: The gstate->ctm in the next line may be wrong; maybe
  330. * it should be the parent gstates ctm? */
  331. fz_fill_shade(ctx, pr->dev, shd, gstate->ctm, gstate->fill.alpha, gstate->fill.color_params);
  332. pdf_end_group(ctx, pr, &softmask);
  333. }
  334. fz_catch(ctx)
  335. {
  336. pdf_drop_obj(ctx, softmask.softmask);
  337. fz_drop_colorspace(ctx, softmask.softmask_cs);
  338. pdf_drop_obj(ctx, softmask.page_resources);
  339. fz_rethrow(ctx);
  340. }
  341. }
  342. static pdf_material *
  343. pdf_keep_material(fz_context *ctx, pdf_material *mat)
  344. {
  345. if (mat->colorspace)
  346. fz_keep_colorspace(ctx, mat->colorspace);
  347. if (mat->pattern)
  348. pdf_keep_pattern(ctx, mat->pattern);
  349. if (mat->shade)
  350. fz_keep_shade(ctx, mat->shade);
  351. return mat;
  352. }
  353. static pdf_material *
  354. pdf_drop_material(fz_context *ctx, pdf_material *mat)
  355. {
  356. fz_drop_colorspace(ctx, mat->colorspace);
  357. pdf_drop_pattern(ctx, mat->pattern);
  358. fz_drop_shade(ctx, mat->shade);
  359. return mat;
  360. }
  361. static void
  362. pdf_copy_pattern_gstate(fz_context *ctx, pdf_gstate *dst, const pdf_gstate *src)
  363. {
  364. pdf_font_desc *old_font = dst->text.font;
  365. dst->ctm = src->ctm;
  366. dst->text = src->text;
  367. pdf_keep_font(ctx, src->text.font);
  368. pdf_drop_font(ctx, old_font);
  369. pdf_drop_obj(ctx, dst->softmask);
  370. dst->softmask = pdf_keep_obj(ctx, src->softmask);
  371. pdf_drop_obj(ctx, dst->softmask_resources);
  372. dst->softmask_resources = pdf_keep_obj(ctx, src->softmask_resources);
  373. fz_drop_colorspace(ctx, dst->softmask_cs);
  374. dst->softmask_cs = fz_keep_colorspace(ctx, src->softmask_cs);
  375. fz_drop_stroke_state(ctx, dst->stroke_state);
  376. dst->stroke_state = fz_keep_stroke_state(ctx, src->stroke_state);
  377. }
  378. static void
  379. pdf_unset_pattern(fz_context *ctx, pdf_run_processor *pr, int what)
  380. {
  381. pdf_gstate *gs = pr->gstate + pr->gtop;
  382. pdf_material *mat;
  383. mat = what == PDF_FILL ? &gs->fill : &gs->stroke;
  384. if (mat->kind == PDF_MAT_PATTERN)
  385. {
  386. pdf_drop_pattern(ctx, mat->pattern);
  387. mat->pattern = NULL;
  388. mat->kind = PDF_MAT_COLOR;
  389. }
  390. }
  391. static void
  392. pdf_keep_gstate(fz_context *ctx, pdf_gstate *gs)
  393. {
  394. pdf_keep_material(ctx, &gs->stroke);
  395. pdf_keep_material(ctx, &gs->fill);
  396. if (gs->text.font)
  397. pdf_keep_font(ctx, gs->text.font);
  398. if (gs->softmask)
  399. pdf_keep_obj(ctx, gs->softmask);
  400. if (gs->softmask_cs)
  401. fz_keep_colorspace(ctx, gs->softmask_cs);
  402. if (gs->softmask_resources)
  403. pdf_keep_obj(ctx, gs->softmask_resources);
  404. fz_keep_stroke_state(ctx, gs->stroke_state);
  405. pdf_keep_obj(ctx, gs->softmask_tr);
  406. }
  407. static void
  408. pdf_drop_gstate(fz_context *ctx, pdf_gstate *gs)
  409. {
  410. pdf_drop_material(ctx, &gs->stroke);
  411. pdf_drop_material(ctx, &gs->fill);
  412. pdf_drop_font(ctx, gs->text.font);
  413. pdf_drop_obj(ctx, gs->softmask);
  414. fz_drop_colorspace(ctx, gs->softmask_cs);
  415. pdf_drop_obj(ctx, gs->softmask_resources);
  416. fz_drop_stroke_state(ctx, gs->stroke_state);
  417. pdf_drop_obj(ctx, gs->softmask_tr);
  418. }
  419. static void
  420. pdf_gsave(fz_context *ctx, pdf_run_processor *pr)
  421. {
  422. if (pr->gtop == pr->gcap-1)
  423. {
  424. if (pr->gcap * 2 >= 4096)
  425. fz_throw(ctx, FZ_ERROR_LIMIT, "too many nested graphics states");
  426. pr->gstate = fz_realloc_array(ctx, pr->gstate, pr->gcap*2, pdf_gstate);
  427. pr->gcap *= 2;
  428. }
  429. memcpy(&pr->gstate[pr->gtop + 1], &pr->gstate[pr->gtop], sizeof(pdf_gstate));
  430. pr->gtop++;
  431. pdf_keep_gstate(ctx, &pr->gstate[pr->gtop]);
  432. }
  433. static void
  434. pdf_grestore(fz_context *ctx, pdf_run_processor *pr)
  435. {
  436. pdf_gstate *gs = pr->gstate + pr->gtop;
  437. int clip_depth = gs->clip_depth;
  438. if (pr->gtop <= pr->gbot)
  439. {
  440. fz_warn(ctx, "gstate underflow in content stream");
  441. return;
  442. }
  443. pdf_drop_gstate(ctx, gs);
  444. pr->gtop --;
  445. gs = pr->gstate + pr->gtop;
  446. while (clip_depth > gs->clip_depth)
  447. {
  448. fz_try(ctx)
  449. {
  450. // End layer early (before Q) if unbalanced Q appears between BMC and EMC.
  451. while (pr->nest_depth > 0 && pr->nest_mark[pr->nest_depth-1] >= 0)
  452. {
  453. fz_end_layer(ctx, pr->dev);
  454. pr->nest_depth--;
  455. }
  456. if (pr->nest_depth > 0)
  457. {
  458. /* So this one must be a clip record. */
  459. fz_pop_clip(ctx, pr->dev);
  460. /* Pop a single clip record off. */
  461. pr->nest_mark[pr->nest_depth-1]++;
  462. if (pr->nest_mark[pr->nest_depth-1] == 0)
  463. pr->nest_depth--;
  464. }
  465. // End layer late (after Q) if unbalanced EMC appears between q and Q.
  466. while (pr->nest_depth > 0 && pr->nest_mark[pr->nest_depth-1] > pr->mc_depth)
  467. {
  468. fz_end_layer(ctx, pr->dev);
  469. pr->nest_depth--;
  470. }
  471. }
  472. fz_catch(ctx)
  473. {
  474. /* Silently swallow the problem - restores must
  475. * never throw! */
  476. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); // FIXME - unsure if we can throw here?
  477. fz_report_error(ctx);
  478. }
  479. clip_depth--;
  480. }
  481. }
  482. static pdf_gstate *
  483. pdf_show_pattern(fz_context *ctx, pdf_run_processor *pr, pdf_pattern *pat, int pat_gstate_num, fz_rect area, int what)
  484. {
  485. pdf_gstate *gstate;
  486. pdf_gstate *pat_gstate;
  487. int gparent_save;
  488. fz_matrix ptm, invptm, gparent_save_ctm;
  489. int x0, y0, x1, y1;
  490. float fx0, fy0, fx1, fy1;
  491. fz_rect local_area;
  492. int oldbot;
  493. int id;
  494. pdf_gsave(ctx, pr);
  495. gstate = pr->gstate + pr->gtop;
  496. pat_gstate = pr->gstate + pat_gstate_num;
  497. /* Patterns are run with the gstate of the parent */
  498. pdf_copy_pattern_gstate(ctx, gstate, pat_gstate);
  499. if (pat->ismask)
  500. {
  501. /* Inhibit any changes to the color since we're drawing an uncolored pattern. */
  502. gstate->ismask = 1;
  503. pdf_unset_pattern(ctx, pr, PDF_FILL);
  504. pdf_unset_pattern(ctx, pr, PDF_STROKE);
  505. if (what == PDF_FILL)
  506. {
  507. pdf_drop_material(ctx, &gstate->stroke);
  508. pdf_keep_material(ctx, &gstate->fill);
  509. gstate->stroke = gstate->fill;
  510. }
  511. if (what == PDF_STROKE)
  512. {
  513. pdf_drop_material(ctx, &gstate->fill);
  514. pdf_keep_material(ctx, &gstate->stroke);
  515. gstate->fill = gstate->stroke;
  516. }
  517. id = 0; /* don't cache uncolored patterns, since we colorize them when drawing */
  518. }
  519. else
  520. {
  521. // TODO: unset only the current fill/stroke or both?
  522. pdf_unset_pattern(ctx, pr, what);
  523. id = pat->id;
  524. }
  525. /* don't apply soft masks to objects in the pattern as well */
  526. if (gstate->softmask)
  527. {
  528. pdf_drop_obj(ctx, gstate->softmask);
  529. gstate->softmask = NULL;
  530. }
  531. ptm = fz_concat(pat->matrix, pat_gstate->ctm);
  532. invptm = fz_invert_matrix(ptm);
  533. /* The parent_ctm is amended with our pattern matrix */
  534. gparent_save = pr->gparent;
  535. pr->gparent = pr->gtop-1;
  536. gparent_save_ctm = pr->gstate[pr->gparent].ctm;
  537. pr->gstate[pr->gparent].ctm = ptm;
  538. /* patterns are painted using the parent_ctm. area = bbox of
  539. * shape to be filled in device space. Map it back to pattern
  540. * space. */
  541. local_area = fz_transform_rect(area, invptm);
  542. fx0 = (local_area.x0 - pat->bbox.x0) / pat->xstep;
  543. fy0 = (local_area.y0 - pat->bbox.y0) / pat->ystep;
  544. fx1 = (local_area.x1 - pat->bbox.x0) / pat->xstep;
  545. fy1 = (local_area.y1 - pat->bbox.y0) / pat->ystep;
  546. if (fx0 > fx1)
  547. {
  548. float t = fx0; fx0 = fx1; fx1 = t;
  549. }
  550. if (fy0 > fy1)
  551. {
  552. float t = fy0; fy0 = fy1; fy1 = t;
  553. }
  554. #ifdef TILE
  555. /* We have tried various formulations in the past, but this one is
  556. * best we've found; only use it as a tile if a whole repeat is
  557. * required in at least one direction. Note, that this allows for
  558. * 'sections' of 4 tiles to be show, but all non-overlapping. */
  559. if (fx1-fx0 > 1 || fy1-fy0 > 1)
  560. #else
  561. if (0)
  562. #endif
  563. {
  564. int cached = fz_begin_tile_id(ctx, pr->dev, local_area, pat->bbox, pat->xstep, pat->ystep, ptm, id);
  565. if (!cached)
  566. {
  567. gstate->ctm = ptm;
  568. oldbot = pr->gbot;
  569. pr->gbot = pr->gtop;
  570. pdf_gsave(ctx, pr);
  571. pdf_process_contents(ctx, (pdf_processor*)pr, pat->document, pat->resources, pat->contents, NULL, NULL);
  572. pdf_grestore(ctx, pr);
  573. while (pr->gtop > pr->gbot)
  574. pdf_grestore(ctx, pr);
  575. pr->gbot = oldbot;
  576. }
  577. fz_end_tile(ctx, pr->dev);
  578. }
  579. else
  580. {
  581. int x, y;
  582. /* When calculating the number of tiles required, we adjust by
  583. * a small amount to allow for rounding errors. By choosing
  584. * this amount to be smaller than 1/256, we guarantee we won't
  585. * cause problems that will be visible even under our most
  586. * extreme antialiasing. */
  587. x0 = floorf(fx0 + 0.001f);
  588. y0 = floorf(fy0 + 0.001f);
  589. x1 = ceilf(fx1 - 0.001f);
  590. y1 = ceilf(fy1 - 0.001f);
  591. /* The above adjustments cause problems for sufficiently
  592. * large values for xstep/ystep which may be used if the
  593. * pattern is expected to be rendered exactly once. */
  594. if (fx1 > fx0 && x1 == x0)
  595. x1 = x0 + 1;
  596. if (fy1 > fy0 && y1 == y0)
  597. y1 = y0 + 1;
  598. for (y = y0; y < y1; y++)
  599. {
  600. for (x = x0; x < x1; x++)
  601. {
  602. /* Calls to pdf_process_contents may cause the
  603. * gstate array to be realloced to be larger.
  604. * That can invalidate gstate. Hence reload
  605. * it each time round the loop. */
  606. gstate = pr->gstate + pr->gtop;
  607. gstate->ctm = fz_pre_translate(ptm, x * pat->xstep, y * pat->ystep);
  608. oldbot = pr->gbot;
  609. pr->gbot = pr->gtop;
  610. pdf_gsave(ctx, pr);
  611. pdf_process_contents(ctx, (pdf_processor*)pr, pat->document, pat->resources, pat->contents, NULL, NULL);
  612. pdf_grestore(ctx, pr);
  613. while (pr->gtop > pr->gbot)
  614. pdf_grestore(ctx, pr);
  615. pr->gbot = oldbot;
  616. }
  617. }
  618. }
  619. pr->gstate[pr->gparent].ctm = gparent_save_ctm;
  620. pr->gparent = gparent_save;
  621. pdf_grestore(ctx, pr);
  622. return pr->gstate + pr->gtop;
  623. }
  624. static void
  625. pdf_show_image_imp(fz_context *ctx, pdf_run_processor *pr, fz_image *image, fz_matrix image_ctm, fz_rect bbox)
  626. {
  627. pdf_gstate *gstate = pr->gstate + pr->gtop;
  628. fz_color_params cp = gstate->fill.color_params;
  629. if (image->has_intent)
  630. cp.ri = image->intent;
  631. if (image->colorspace)
  632. {
  633. fz_fill_image(ctx, pr->dev, image, image_ctm, gstate->fill.alpha, cp);
  634. }
  635. else if (gstate->fill.kind == PDF_MAT_COLOR)
  636. {
  637. fz_fill_image_mask(ctx, pr->dev, image, image_ctm, gstate->fill.colorspace, gstate->fill.v, gstate->fill.alpha, cp);
  638. }
  639. else if (gstate->fill.kind == PDF_MAT_PATTERN && gstate->fill.pattern)
  640. {
  641. fz_clip_image_mask(ctx, pr->dev, image, image_ctm, bbox);
  642. gstate = pdf_show_pattern(ctx, pr, gstate->fill.pattern, gstate->fill.gstate_num, bbox, PDF_FILL);
  643. fz_pop_clip(ctx, pr->dev);
  644. }
  645. else if (gstate->fill.kind == PDF_MAT_SHADE && gstate->fill.shade)
  646. {
  647. fz_clip_image_mask(ctx, pr->dev, image, image_ctm, bbox);
  648. fz_fill_shade(ctx, pr->dev, gstate->fill.shade, pr->gstate[gstate->fill.gstate_num].ctm, gstate->fill.alpha, cp);
  649. fz_pop_clip(ctx, pr->dev);
  650. }
  651. }
  652. static void
  653. pdf_show_image(fz_context *ctx, pdf_run_processor *pr, fz_image *image)
  654. {
  655. pdf_gstate *gstate = pr->gstate + pr->gtop;
  656. fz_matrix image_ctm;
  657. fz_rect bbox;
  658. if (pr->super.hidden)
  659. return;
  660. /* image can be NULL here if we are, for example, running to an
  661. * stext device. */
  662. if (image == NULL)
  663. return;
  664. pop_any_pending_mcid_changes(ctx, pr);
  665. flush_begin_layer(ctx, pr);
  666. /* PDF has images bottom-up, so flip them right side up here */
  667. image_ctm = fz_pre_scale(fz_pre_translate(gstate->ctm, 0, 1), 1, -1);
  668. bbox = fz_transform_rect(fz_unit_rect, image_ctm);
  669. if (image->mask && gstate->blendmode)
  670. {
  671. /* apply blend group even though we skip the soft mask */
  672. fz_begin_group(ctx, pr->dev, bbox, NULL, 0, 0, gstate->blendmode, 1);
  673. fz_clip_image_mask(ctx, pr->dev, image->mask, image_ctm, bbox);
  674. pdf_show_image_imp(ctx, pr, image, image_ctm, bbox);
  675. fz_pop_clip(ctx, pr->dev);
  676. fz_end_group(ctx, pr->dev);
  677. }
  678. else if (image->mask)
  679. {
  680. fz_clip_image_mask(ctx, pr->dev, image->mask, image_ctm, bbox);
  681. pdf_show_image_imp(ctx, pr, image, image_ctm, bbox);
  682. fz_pop_clip(ctx, pr->dev);
  683. }
  684. else
  685. {
  686. softmask_save softmask = { NULL };
  687. fz_try(ctx)
  688. {
  689. gstate = pdf_begin_group(ctx, pr, bbox, &softmask);
  690. pdf_show_image_imp(ctx, pr, image, image_ctm, bbox);
  691. pdf_end_group(ctx, pr, &softmask);
  692. }
  693. fz_catch(ctx)
  694. {
  695. pdf_drop_obj(ctx, softmask.softmask);
  696. fz_drop_colorspace(ctx, softmask.softmask_cs);
  697. pdf_drop_obj(ctx, softmask.page_resources);
  698. fz_rethrow(ctx);
  699. }
  700. }
  701. }
  702. static void
  703. pdf_show_path(fz_context *ctx, pdf_run_processor *pr, int doclose, int dofill, int dostroke, int even_odd)
  704. {
  705. pdf_gstate *gstate = pr->gstate + pr->gtop;
  706. fz_path *path;
  707. fz_rect bbox;
  708. softmask_save softmask = { NULL };
  709. int knockout_group = 0;
  710. pop_any_pending_mcid_changes(ctx, pr);
  711. flush_begin_layer(ctx, pr);
  712. if (dostroke) {
  713. if (pr->dev->flags & (FZ_DEVFLAG_STROKECOLOR_UNDEFINED | FZ_DEVFLAG_LINEJOIN_UNDEFINED | FZ_DEVFLAG_LINEWIDTH_UNDEFINED | FZ_DEVFLAG_DASH_PATTERN_UNDEFINED))
  714. pr->dev->flags |= FZ_DEVFLAG_UNCACHEABLE;
  715. else if (gstate->stroke_state->dash_len != 0 && pr->dev->flags & (FZ_DEVFLAG_STARTCAP_UNDEFINED | FZ_DEVFLAG_DASHCAP_UNDEFINED | FZ_DEVFLAG_ENDCAP_UNDEFINED))
  716. pr->dev->flags |= FZ_DEVFLAG_UNCACHEABLE;
  717. else if (gstate->stroke_state->linejoin == FZ_LINEJOIN_MITER && (pr->dev->flags & FZ_DEVFLAG_MITERLIMIT_UNDEFINED))
  718. pr->dev->flags |= FZ_DEVFLAG_UNCACHEABLE;
  719. }
  720. if (dofill) {
  721. if (pr->dev->flags & FZ_DEVFLAG_FILLCOLOR_UNDEFINED)
  722. pr->dev->flags |= FZ_DEVFLAG_UNCACHEABLE;
  723. }
  724. path = pr->path;
  725. pr->path = fz_new_path(ctx);
  726. fz_try(ctx)
  727. {
  728. if (doclose)
  729. fz_closepath(ctx, path);
  730. bbox = fz_bound_path(ctx, path, (dostroke ? gstate->stroke_state : NULL), gstate->ctm);
  731. if (pr->super.hidden)
  732. dostroke = dofill = 0;
  733. if (dofill || dostroke)
  734. gstate = pdf_begin_group(ctx, pr, bbox, &softmask);
  735. if (dofill && dostroke)
  736. {
  737. /* We may need to push a knockout group */
  738. if (gstate->stroke.alpha == 0)
  739. {
  740. /* No need for group, as stroke won't do anything */
  741. }
  742. else if (gstate->stroke.alpha == 1.0f && gstate->blendmode == FZ_BLEND_NORMAL)
  743. {
  744. /* No need for group, as stroke won't show up */
  745. }
  746. else
  747. {
  748. knockout_group = 1;
  749. fz_begin_group(ctx, pr->dev, bbox, NULL, 0, 1, FZ_BLEND_NORMAL, 1);
  750. }
  751. }
  752. if (dofill)
  753. {
  754. switch (gstate->fill.kind)
  755. {
  756. case PDF_MAT_NONE:
  757. break;
  758. case PDF_MAT_COLOR:
  759. fz_fill_path(ctx, pr->dev, path, even_odd, gstate->ctm,
  760. gstate->fill.colorspace, gstate->fill.v, gstate->fill.alpha, gstate->fill.color_params);
  761. break;
  762. case PDF_MAT_PATTERN:
  763. if (gstate->fill.pattern)
  764. {
  765. fz_clip_path(ctx, pr->dev, path, even_odd, gstate->ctm, bbox);
  766. gstate = pdf_show_pattern(ctx, pr, gstate->fill.pattern, gstate->fill.gstate_num, bbox, PDF_FILL);
  767. fz_pop_clip(ctx, pr->dev);
  768. }
  769. break;
  770. case PDF_MAT_SHADE:
  771. if (gstate->fill.shade)
  772. {
  773. fz_clip_path(ctx, pr->dev, path, even_odd, gstate->ctm, bbox);
  774. /* The cluster and page 2 of patterns.pdf shows that fz_fill_shade should NOT be called with gstate->ctm. */
  775. fz_fill_shade(ctx, pr->dev, gstate->fill.shade, pr->gstate[gstate->fill.gstate_num].ctm, gstate->fill.alpha, gstate->fill.color_params);
  776. fz_pop_clip(ctx, pr->dev);
  777. }
  778. break;
  779. }
  780. }
  781. if (dostroke)
  782. {
  783. switch (gstate->stroke.kind)
  784. {
  785. case PDF_MAT_NONE:
  786. break;
  787. case PDF_MAT_COLOR:
  788. fz_stroke_path(ctx, pr->dev, path, gstate->stroke_state, gstate->ctm,
  789. gstate->stroke.colorspace, gstate->stroke.v, gstate->stroke.alpha, gstate->stroke.color_params);
  790. break;
  791. case PDF_MAT_PATTERN:
  792. if (gstate->stroke.pattern)
  793. {
  794. fz_clip_stroke_path(ctx, pr->dev, path, gstate->stroke_state, gstate->ctm, bbox);
  795. gstate = pdf_show_pattern(ctx, pr, gstate->stroke.pattern, gstate->stroke.gstate_num, bbox, PDF_STROKE);
  796. fz_pop_clip(ctx, pr->dev);
  797. }
  798. break;
  799. case PDF_MAT_SHADE:
  800. if (gstate->stroke.shade)
  801. {
  802. fz_clip_stroke_path(ctx, pr->dev, path, gstate->stroke_state, gstate->ctm, bbox);
  803. fz_fill_shade(ctx, pr->dev, gstate->stroke.shade, pr->gstate[gstate->stroke.gstate_num].ctm, gstate->stroke.alpha, gstate->stroke.color_params);
  804. fz_pop_clip(ctx, pr->dev);
  805. }
  806. break;
  807. }
  808. }
  809. if (knockout_group)
  810. fz_end_group(ctx, pr->dev);
  811. if (dofill || dostroke)
  812. pdf_end_group(ctx, pr, &softmask);
  813. if (pr->clip)
  814. {
  815. nest_layer_clip(ctx, pr);
  816. gstate->clip_depth++;
  817. fz_clip_path(ctx, pr->dev, path, pr->clip_even_odd, gstate->ctm, bbox);
  818. pr->clip = 0;
  819. }
  820. }
  821. fz_always(ctx)
  822. {
  823. fz_drop_path(ctx, path);
  824. }
  825. fz_catch(ctx)
  826. {
  827. pdf_drop_obj(ctx, softmask.softmask);
  828. fz_drop_colorspace(ctx, softmask.softmask_cs);
  829. pdf_drop_obj(ctx, softmask.page_resources);
  830. fz_rethrow(ctx);
  831. }
  832. }
  833. /*
  834. * Assemble and emit text
  835. */
  836. static pdf_gstate *
  837. pdf_flush_text(fz_context *ctx, pdf_run_processor *pr)
  838. {
  839. pdf_gstate *gstate = pr->gstate + pr->gtop;
  840. fz_text *text;
  841. int dofill;
  842. int dostroke;
  843. int doclip;
  844. int doinvisible;
  845. softmask_save softmask = { NULL };
  846. int knockout_group = 0;
  847. text = pdf_tos_get_text(ctx, &pr->tos);
  848. if (!text)
  849. return gstate;
  850. pop_any_pending_mcid_changes(ctx, pr);
  851. /* If we are going to output text, we need to have flushed any begin layers first. */
  852. flush_begin_layer(ctx, pr);
  853. dofill = dostroke = doclip = doinvisible = 0;
  854. switch (pr->tos.text_mode)
  855. {
  856. case 0: dofill = 1; break;
  857. case 1: dostroke = 1; break;
  858. case 2: dofill = dostroke = 1; break;
  859. case 3: doinvisible = 1; break;
  860. case 4: dofill = doclip = 1; break;
  861. case 5: dostroke = doclip = 1; break;
  862. case 6: dofill = dostroke = doclip = 1; break;
  863. case 7: doclip = 1; break;
  864. }
  865. if (pr->super.hidden)
  866. dostroke = dofill = 0;
  867. fz_try(ctx)
  868. {
  869. fz_rect tb = fz_transform_rect(pr->tos.text_bbox, gstate->ctm);
  870. if (dostroke)
  871. tb = fz_adjust_rect_for_stroke(ctx, tb, gstate->stroke_state, gstate->ctm);
  872. /* Don't bother sending a text group with nothing in it */
  873. if (!text->head)
  874. break;
  875. if (dofill || dostroke)
  876. gstate = pdf_begin_group(ctx, pr, tb, &softmask);
  877. if (dofill && dostroke)
  878. {
  879. /* We may need to push a knockout group */
  880. if (gstate->stroke.alpha == 0)
  881. {
  882. /* No need for group, as stroke won't do anything */
  883. }
  884. else if (gstate->stroke.alpha == 1.0f && gstate->blendmode == FZ_BLEND_NORMAL)
  885. {
  886. /* No need for group, as stroke won't show up */
  887. }
  888. else
  889. {
  890. knockout_group = 1;
  891. fz_begin_group(ctx, pr->dev, tb, NULL, 0, 1, FZ_BLEND_NORMAL, 1);
  892. }
  893. }
  894. if (doinvisible)
  895. fz_ignore_text(ctx, pr->dev, text, gstate->ctm);
  896. if (dofill)
  897. {
  898. switch (gstate->fill.kind)
  899. {
  900. case PDF_MAT_NONE:
  901. break;
  902. case PDF_MAT_COLOR:
  903. fz_fill_text(ctx, pr->dev, text, gstate->ctm,
  904. gstate->fill.colorspace, gstate->fill.v, gstate->fill.alpha, gstate->fill.color_params);
  905. break;
  906. case PDF_MAT_PATTERN:
  907. if (gstate->fill.pattern)
  908. {
  909. fz_clip_text(ctx, pr->dev, text, gstate->ctm, tb);
  910. gstate = pdf_show_pattern(ctx, pr, gstate->fill.pattern, gstate->fill.gstate_num, tb, PDF_FILL);
  911. fz_pop_clip(ctx, pr->dev);
  912. }
  913. break;
  914. case PDF_MAT_SHADE:
  915. if (gstate->fill.shade)
  916. {
  917. fz_clip_text(ctx, pr->dev, text, gstate->ctm, tb);
  918. /* Page 2 of patterns.pdf shows that fz_fill_shade should NOT be called with gstate->ctm */
  919. fz_fill_shade(ctx, pr->dev, gstate->fill.shade, pr->gstate[gstate->fill.gstate_num].ctm, gstate->fill.alpha, gstate->fill.color_params);
  920. fz_pop_clip(ctx, pr->dev);
  921. }
  922. break;
  923. }
  924. }
  925. if (dostroke)
  926. {
  927. switch (gstate->stroke.kind)
  928. {
  929. case PDF_MAT_NONE:
  930. break;
  931. case PDF_MAT_COLOR:
  932. fz_stroke_text(ctx, pr->dev, text, gstate->stroke_state, gstate->ctm,
  933. gstate->stroke.colorspace, gstate->stroke.v, gstate->stroke.alpha, gstate->stroke.color_params);
  934. break;
  935. case PDF_MAT_PATTERN:
  936. if (gstate->stroke.pattern)
  937. {
  938. fz_clip_stroke_text(ctx, pr->dev, text, gstate->stroke_state, gstate->ctm, tb);
  939. gstate = pdf_show_pattern(ctx, pr, gstate->stroke.pattern, gstate->stroke.gstate_num, tb, PDF_STROKE);
  940. fz_pop_clip(ctx, pr->dev);
  941. }
  942. break;
  943. case PDF_MAT_SHADE:
  944. if (gstate->stroke.shade)
  945. {
  946. fz_clip_stroke_text(ctx, pr->dev, text, gstate->stroke_state, gstate->ctm, tb);
  947. fz_fill_shade(ctx, pr->dev, gstate->stroke.shade, pr->gstate[gstate->stroke.gstate_num].ctm, gstate->stroke.alpha, gstate->stroke.color_params);
  948. fz_pop_clip(ctx, pr->dev);
  949. }
  950. break;
  951. }
  952. }
  953. if (knockout_group)
  954. fz_end_group(ctx, pr->dev);
  955. if (dofill || dostroke)
  956. pdf_end_group(ctx, pr, &softmask);
  957. if (doclip)
  958. {
  959. nest_layer_clip(ctx, pr);
  960. gstate->clip_depth++;
  961. fz_clip_text(ctx, pr->dev, text, gstate->ctm, tb);
  962. }
  963. }
  964. fz_always(ctx)
  965. {
  966. fz_drop_text(ctx, text);
  967. }
  968. fz_catch(ctx)
  969. {
  970. pdf_drop_obj(ctx, softmask.softmask);
  971. fz_drop_colorspace(ctx, softmask.softmask_cs);
  972. pdf_drop_obj(ctx, softmask.page_resources);
  973. fz_rethrow(ctx);
  974. }
  975. return pr->gstate + pr->gtop;
  976. }
  977. static int
  978. guess_bidi_level(int bidiclass, int cur_bidi)
  979. {
  980. switch (bidiclass)
  981. {
  982. /* strong */
  983. case UCDN_BIDI_CLASS_L: return 0;
  984. case UCDN_BIDI_CLASS_R: return 1;
  985. case UCDN_BIDI_CLASS_AL: return 1;
  986. /* weak */
  987. case UCDN_BIDI_CLASS_EN:
  988. case UCDN_BIDI_CLASS_ES:
  989. case UCDN_BIDI_CLASS_ET:
  990. return 0;
  991. case UCDN_BIDI_CLASS_AN:
  992. return 1;
  993. case UCDN_BIDI_CLASS_CS:
  994. case UCDN_BIDI_CLASS_NSM:
  995. case UCDN_BIDI_CLASS_BN:
  996. return cur_bidi;
  997. /* neutral */
  998. case UCDN_BIDI_CLASS_B:
  999. case UCDN_BIDI_CLASS_S:
  1000. case UCDN_BIDI_CLASS_WS:
  1001. case UCDN_BIDI_CLASS_ON:
  1002. return cur_bidi;
  1003. /* embedding, override, pop ... we don't support them */
  1004. default:
  1005. return 0;
  1006. }
  1007. }
  1008. static void
  1009. pdf_show_char(fz_context *ctx, pdf_run_processor *pr, int cid, fz_text_language lang)
  1010. {
  1011. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1012. pdf_font_desc *fontdesc = gstate->text.font;
  1013. fz_matrix trm;
  1014. float adv;
  1015. int gid;
  1016. int ucsbuf[PDF_MRANGE_CAP];
  1017. int ucslen;
  1018. int i;
  1019. int render_direct;
  1020. int type3_hitr;
  1021. gid = pdf_tos_make_trm(ctx, &pr->tos, &gstate->text, fontdesc, cid, &trm, &adv);
  1022. /* If we are uncachable, then render direct. */
  1023. render_direct = !fz_glyph_cacheable(ctx, fontdesc->font, gid);
  1024. /* PDF spec: ISO 32000-2 latest version at the time of writing:
  1025. * Section 9.3.6:
  1026. * Where text is drawn using a Type 3 font:
  1027. * + if text rendering mode is set to a value of 3 or 7, the text shall not be rendered.
  1028. * + if text rendering mode is set to a value other than 3 or 7, the text shall be rendered using the glyph descriptions in the Type 3 font.
  1029. * + If text rendering mode is set to a value of 4, 5, 6 or 7, nothing shall be added to the clipping path.
  1030. */
  1031. type3_hitr = (fontdesc->font->t3procs && pr->tos.text_mode >= 4);
  1032. /* flush buffered text if rendermode has changed */
  1033. if (!pr->tos.text || gstate->text.render != pr->tos.text_mode || render_direct || type3_hitr)
  1034. {
  1035. gstate = pdf_flush_text(ctx, pr);
  1036. pdf_tos_reset(ctx, &pr->tos, gstate->text.render);
  1037. }
  1038. /* If Type3 and tr >= 4, then ignore the clipping path part. */
  1039. if (type3_hitr)
  1040. pr->tos.text_mode -= 4;
  1041. if (render_direct && pr->tos.text_mode != 3 /* or 7, by type3_hitr */)
  1042. {
  1043. /* Render the glyph stream direct here (only happens for
  1044. * type3 glyphs that seem to inherit current graphics
  1045. * attributes, or type 3 glyphs within type3 glyphs). */
  1046. fz_matrix composed = fz_concat(trm, gstate->ctm);
  1047. /* Whatever problems the underlying char has is no concern of
  1048. * ours. Store the flags, restore them afterwards. */
  1049. int old_flags = pr->dev->flags;
  1050. pdf_gstate *fill_gstate = NULL;
  1051. pdf_gstate *stroke_gstate = NULL;
  1052. pdf_gsave(ctx, pr);
  1053. gstate = pr->gstate + pr->gtop;
  1054. if (gstate->fill.kind == PDF_MAT_PATTERN && gstate->fill.gstate_num >= 0)
  1055. fill_gstate = pr->gstate + gstate->fill.gstate_num;
  1056. if (gstate->stroke.kind == PDF_MAT_PATTERN && gstate->stroke.gstate_num >= 0)
  1057. stroke_gstate = pr->gstate + gstate->stroke.gstate_num;
  1058. pdf_drop_font(ctx, gstate->text.font);
  1059. gstate->text.font = NULL; /* don't inherit the current font... */
  1060. fz_render_t3_glyph_direct(ctx, pr->dev, fontdesc->font, gid, composed, gstate, pr->default_cs, fill_gstate, stroke_gstate);
  1061. pr->dev->flags = old_flags;
  1062. pdf_grestore(ctx, pr);
  1063. /* Render text invisibly so that it can still be extracted. */
  1064. pr->tos.text_mode = 3;
  1065. }
  1066. ucslen = 0;
  1067. if (fontdesc->to_unicode)
  1068. ucslen = pdf_lookup_cmap_full(fontdesc->to_unicode, cid, ucsbuf);
  1069. /* convert ascii whitespace control characters to spaces */
  1070. if (ucslen == 1 && (ucsbuf[0] >= 8 && ucsbuf[0] <= 13))
  1071. ucsbuf[0] = ' ';
  1072. /* ignore obviously bad values in ToUnicode, fall back to the cid_to_ucs table */
  1073. if (ucslen == 1 && (ucsbuf[0] < 32 || (ucsbuf[0] >= 127 && ucsbuf[0] < 160)))
  1074. ucslen = 0;
  1075. if (ucslen == 0 && (size_t)cid < fontdesc->cid_to_ucs_len)
  1076. {
  1077. ucsbuf[0] = fontdesc->cid_to_ucs[cid];
  1078. ucslen = 1;
  1079. }
  1080. if (ucslen == 0 || (ucslen == 1 && ucsbuf[0] == 0))
  1081. {
  1082. ucsbuf[0] = FZ_REPLACEMENT_CHARACTER;
  1083. ucslen = 1;
  1084. }
  1085. /* guess bidi level from unicode value */
  1086. pr->bidi = guess_bidi_level(ucdn_get_bidi_class(ucsbuf[0]), pr->bidi);
  1087. /* add glyph to textobject */
  1088. fz_show_glyph_aux(ctx, pr->tos.text, fontdesc->font, trm, adv, gid, ucsbuf[0], cid, fontdesc->wmode, pr->bidi, FZ_BIDI_NEUTRAL, lang);
  1089. /* add filler glyphs for one-to-many unicode mapping */
  1090. for (i = 1; i < ucslen; i++)
  1091. fz_show_glyph_aux(ctx, pr->tos.text, fontdesc->font, trm, 0, -1, ucsbuf[i], -1, fontdesc->wmode, pr->bidi, FZ_BIDI_NEUTRAL, lang);
  1092. pdf_tos_move_after_char(ctx, &pr->tos);
  1093. }
  1094. static void
  1095. pdf_show_space(fz_context *ctx, pdf_run_processor *pr, float tadj)
  1096. {
  1097. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1098. pdf_font_desc *fontdesc = gstate->text.font;
  1099. if (fontdesc->wmode == 0)
  1100. pr->tos.tm = fz_pre_translate(pr->tos.tm, tadj * gstate->text.scale, 0);
  1101. else
  1102. pr->tos.tm = fz_pre_translate(pr->tos.tm, 0, tadj);
  1103. }
  1104. static int
  1105. int_in_singleton_or_array(fz_context *ctx, pdf_obj *k, int id)
  1106. {
  1107. /* In the most common case the /K value will be id. */
  1108. if (pdf_is_int(ctx, k) && pdf_to_int(ctx, k) == id)
  1109. return 1;
  1110. /* In the next most common case, there will be an array of
  1111. * items, one of which is k. */
  1112. if (pdf_is_array(ctx, k))
  1113. {
  1114. int i, n = pdf_array_len(ctx, k);
  1115. for (i = 0; i < n; i++)
  1116. {
  1117. pdf_obj *o = pdf_array_get(ctx, k, i);
  1118. if (pdf_is_int(ctx, o) && pdf_to_int(ctx, o) == id)
  1119. return 1;
  1120. }
  1121. }
  1122. return 0;
  1123. }
  1124. pdf_obj *
  1125. pdf_lookup_mcid_in_mcids(fz_context *ctx, int id, pdf_obj *mcids)
  1126. {
  1127. pdf_obj *mcid = pdf_array_get(ctx, mcids, id);
  1128. pdf_obj *k = pdf_dict_get(ctx, mcid, PDF_NAME(K));
  1129. int i, n;
  1130. if (int_in_singleton_or_array(ctx, k, id))
  1131. return mcid;
  1132. /* At this point, something has gone wrong. One common case that
  1133. * appears to fail is where the MCIDs array has the right things
  1134. * in, but at the wrong indexes. So do some searching. */
  1135. n = pdf_array_len(ctx, mcids);
  1136. for (i = 0; i < n; i++)
  1137. {
  1138. pdf_obj *o = pdf_array_get(ctx, mcids, i);
  1139. if (int_in_singleton_or_array(ctx, pdf_dict_get(ctx, o, PDF_NAME(K)), id))
  1140. return o;
  1141. }
  1142. return NULL;
  1143. }
  1144. static pdf_obj *
  1145. lookup_mcid(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val)
  1146. {
  1147. pdf_obj *mcid;
  1148. int id;
  1149. pdf_obj *mcids;
  1150. if (proc->struct_parent == -1)
  1151. return NULL;
  1152. mcid = pdf_dict_get(ctx, val, PDF_NAME(MCID));
  1153. if (!mcid)
  1154. return NULL;
  1155. if (!pdf_is_number(ctx, mcid))
  1156. return NULL;
  1157. id = pdf_to_int(ctx, mcid);
  1158. mcids = pdf_lookup_number(ctx, pdf_dict_getl(ctx, pdf_trailer(ctx, proc->doc), PDF_NAME(Root), PDF_NAME(StructTreeRoot), PDF_NAME(ParentTree), NULL), proc->struct_parent);
  1159. return pdf_lookup_mcid_in_mcids(ctx, id, mcids);
  1160. }
  1161. static fz_text_language
  1162. find_lang_from_mc(fz_context *ctx, pdf_run_processor *pr)
  1163. {
  1164. marked_content_stack *mc;
  1165. for (mc = pr->marked_content; mc != NULL; mc = mc->next)
  1166. {
  1167. size_t len;
  1168. const char *lang;
  1169. lang = pdf_dict_get_string(ctx, mc->val, PDF_NAME(Lang), &len);
  1170. if (!lang)
  1171. lang = pdf_dict_get_string(ctx, lookup_mcid(ctx, pr, mc->val), PDF_NAME(Lang), &len);
  1172. if (lang)
  1173. {
  1174. char text[8];
  1175. memcpy(text, lang, len < 8 ? len : 7);
  1176. text[len < 8 ? len : 7] = 0;
  1177. return fz_text_language_from_string(text);
  1178. }
  1179. }
  1180. return FZ_LANG_UNSET;
  1181. }
  1182. static void
  1183. show_string(fz_context *ctx, pdf_run_processor *pr, unsigned char *buf, size_t len)
  1184. {
  1185. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1186. pdf_font_desc *fontdesc = gstate->text.font;
  1187. unsigned char *end = buf + len;
  1188. unsigned int cpt;
  1189. int cid;
  1190. fz_text_language lang = find_lang_from_mc(ctx, pr);
  1191. pop_any_pending_mcid_changes(ctx, pr);
  1192. flush_begin_layer(ctx, pr);
  1193. while (buf < end)
  1194. {
  1195. int w = pdf_decode_cmap(fontdesc->encoding, buf, end, &cpt);
  1196. buf += w;
  1197. cid = pdf_lookup_cmap(fontdesc->encoding, cpt);
  1198. if (cid >= 0)
  1199. pdf_show_char(ctx, pr, cid, lang);
  1200. else
  1201. fz_warn(ctx, "cannot encode character");
  1202. if (cpt == 32 && w == 1)
  1203. {
  1204. /* Bug 703151: pdf_show_char can realloc gstate. */
  1205. gstate = pr->gstate + pr->gtop;
  1206. pdf_show_space(ctx, pr, gstate->text.word_space);
  1207. }
  1208. }
  1209. }
  1210. static void
  1211. pdf_show_string(fz_context *ctx, pdf_run_processor *pr, unsigned char *buf, size_t len)
  1212. {
  1213. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1214. pdf_font_desc *fontdesc = gstate->text.font;
  1215. if (!fontdesc)
  1216. {
  1217. fz_warn(ctx, "cannot draw text since font and size not set");
  1218. return;
  1219. }
  1220. show_string(ctx, pr, buf, len);
  1221. }
  1222. static void
  1223. pdf_show_text(fz_context *ctx, pdf_run_processor *pr, pdf_obj *text)
  1224. {
  1225. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1226. pdf_font_desc *fontdesc = gstate->text.font;
  1227. int i;
  1228. if (!fontdesc)
  1229. {
  1230. fz_warn(ctx, "cannot draw text since font and size not set");
  1231. return;
  1232. }
  1233. if (pdf_is_array(ctx, text))
  1234. {
  1235. int n = pdf_array_len(ctx, text);
  1236. for (i = 0; i < n; i++)
  1237. {
  1238. pdf_obj *item = pdf_array_get(ctx, text, i);
  1239. if (pdf_is_string(ctx, item))
  1240. show_string(ctx, pr, (unsigned char *)pdf_to_str_buf(ctx, item), pdf_to_str_len(ctx, item));
  1241. else
  1242. pdf_show_space(ctx, pr, - pdf_to_real(ctx, item) * gstate->text.size * 0.001f);
  1243. }
  1244. }
  1245. else if (pdf_is_string(ctx, text))
  1246. {
  1247. pdf_show_string(ctx, pr, (unsigned char *)pdf_to_str_buf(ctx, text), pdf_to_str_len(ctx, text));
  1248. }
  1249. }
  1250. /*
  1251. * Interpreter and graphics state stack.
  1252. */
  1253. static void
  1254. pdf_init_gstate(fz_context *ctx, pdf_gstate *gs, fz_matrix ctm)
  1255. {
  1256. gs->ctm = ctm;
  1257. gs->clip_depth = 0;
  1258. gs->stroke_state = fz_new_stroke_state(ctx);
  1259. gs->stroke.kind = PDF_MAT_COLOR;
  1260. gs->stroke.colorspace = fz_keep_colorspace(ctx, fz_device_gray(ctx));
  1261. gs->stroke.v[0] = 0;
  1262. gs->stroke.pattern = NULL;
  1263. gs->stroke.shade = NULL;
  1264. gs->stroke.alpha = 1;
  1265. gs->stroke.gstate_num = -1;
  1266. gs->fill.kind = PDF_MAT_COLOR;
  1267. gs->fill.colorspace = fz_keep_colorspace(ctx, fz_device_gray(ctx));
  1268. gs->fill.v[0] = 0;
  1269. gs->fill.pattern = NULL;
  1270. gs->fill.shade = NULL;
  1271. gs->fill.alpha = 1;
  1272. gs->fill.gstate_num = -1;
  1273. gs->text.char_space = 0;
  1274. gs->text.word_space = 0;
  1275. gs->text.scale = 1;
  1276. gs->text.leading = 0;
  1277. gs->text.font = NULL;
  1278. gs->text.size = -1;
  1279. gs->text.render = 0;
  1280. gs->text.rise = 0;
  1281. gs->blendmode = 0;
  1282. gs->softmask = NULL;
  1283. gs->softmask_cs = NULL;
  1284. gs->softmask_resources = NULL;
  1285. gs->softmask_ctm = fz_identity;
  1286. gs->luminosity = 0;
  1287. gs->fill.color_params = fz_default_color_params;
  1288. gs->stroke.color_params = fz_default_color_params;
  1289. gs->ismask = 0;
  1290. }
  1291. static void
  1292. pdf_copy_gstate(fz_context *ctx, pdf_gstate *dst, pdf_gstate *src)
  1293. {
  1294. pdf_drop_gstate(ctx, dst);
  1295. *dst = *src;
  1296. pdf_keep_gstate(ctx, dst);
  1297. }
  1298. /*
  1299. * Material state
  1300. */
  1301. static void
  1302. pdf_set_colorspace(fz_context *ctx, pdf_run_processor *pr, int what, fz_colorspace *colorspace)
  1303. {
  1304. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1305. pdf_material *mat;
  1306. int n = fz_colorspace_n(ctx, colorspace);
  1307. gstate = pdf_flush_text(ctx, pr);
  1308. /* Don't change color if we're drawing an uncolored pattern tile! */
  1309. if (gstate->ismask)
  1310. return;
  1311. mat = what == PDF_FILL ? &gstate->fill : &gstate->stroke;
  1312. fz_drop_colorspace(ctx, mat->colorspace);
  1313. mat->kind = PDF_MAT_COLOR;
  1314. mat->colorspace = fz_keep_colorspace(ctx, colorspace);
  1315. mat->v[0] = 0;
  1316. mat->v[1] = 0;
  1317. mat->v[2] = 0;
  1318. mat->v[3] = 1;
  1319. if (pdf_is_tint_colorspace(ctx, colorspace))
  1320. {
  1321. int i;
  1322. for (i = 0; i < n; i++)
  1323. mat->v[i] = 1.0f;
  1324. }
  1325. }
  1326. static void
  1327. pdf_set_color(fz_context *ctx, pdf_run_processor *pr, int what, float *v)
  1328. {
  1329. pdf_gstate *gstate = pr->gstate + pr->gtop;
  1330. pdf_material *mat;
  1331. gstate = pdf_flush_text(ctx, pr);
  1332. /* Don't change color if we're drawing an uncolored pattern tile! */
  1333. if (gstate->ismask)
  1334. return;
  1335. mat = what == PDF_FILL ? &gstate->fill : &gstate->stroke;
  1336. switch (mat->kind)
  1337. {
  1338. case PDF_MAT_PATTERN:
  1339. case PDF_MAT_COLOR:
  1340. fz_clamp_color(ctx, mat->colorspace, v, mat->v);
  1341. break;
  1342. default:
  1343. fz_warn(ctx, "color incompatible with material");
  1344. }
  1345. mat->gstate_num = pr->gparent;
  1346. }
  1347. static void
  1348. pdf_set_shade(fz_context *ctx, pdf_run_processor *pr, int what, fz_shade *shade)
  1349. {
  1350. pdf_gstate *gs;
  1351. pdf_material *mat;
  1352. gs = pdf_flush_text(ctx, pr);
  1353. mat = what == PDF_FILL ? &gs->fill : &gs->stroke;
  1354. fz_drop_shade(ctx, mat->shade);
  1355. mat->kind = PDF_MAT_SHADE;
  1356. mat->shade = fz_keep_shade(ctx, shade);
  1357. mat->gstate_num = pr->gparent;
  1358. }
  1359. static void
  1360. pdf_set_pattern(fz_context *ctx, pdf_run_processor *pr, int what, pdf_pattern *pat, float *v)
  1361. {
  1362. pdf_gstate *gs;
  1363. pdf_material *mat;
  1364. gs = pdf_flush_text(ctx, pr);
  1365. mat = what == PDF_FILL ? &gs->fill : &gs->stroke;
  1366. pdf_drop_pattern(ctx, mat->pattern);
  1367. mat->pattern = NULL;
  1368. mat->kind = PDF_MAT_PATTERN;
  1369. if (pat)
  1370. mat->pattern = pdf_keep_pattern(ctx, pat);
  1371. if (v)
  1372. pdf_set_color(ctx, pr, what, v);
  1373. mat->gstate_num = pr->gparent;
  1374. }
  1375. static void
  1376. begin_metatext(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val, pdf_obj *mcid, fz_metatext meta, pdf_obj *name)
  1377. {
  1378. pdf_obj *text = pdf_dict_get(ctx, val, name);
  1379. if (!text)
  1380. text = pdf_dict_get(ctx, mcid, name);
  1381. if (!text)
  1382. return;
  1383. pdf_flush_text(ctx, proc);
  1384. fz_begin_metatext(ctx, proc->dev, meta, pdf_to_text_string(ctx, text));
  1385. }
  1386. static void
  1387. end_metatext(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val, pdf_obj *mcid, pdf_obj *name)
  1388. {
  1389. pdf_obj *text = pdf_dict_get(ctx, val, name);
  1390. if (!text)
  1391. text = pdf_dict_get(ctx, mcid, name);
  1392. if (!text)
  1393. return;
  1394. pdf_flush_text(ctx, proc);
  1395. fz_end_metatext(ctx, proc->dev);
  1396. }
  1397. static void
  1398. begin_oc(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val, pdf_cycle_list *cycle_up)
  1399. {
  1400. /* val has been resolved to a dict for us by the originally specified name
  1401. * having been looked up in Properties already for us. Either there will
  1402. * be a Name entry, or there will be an OCGs and it'll be a group one. */
  1403. pdf_cycle_list cycle;
  1404. pdf_obj *obj;
  1405. int i, n;
  1406. if (pdf_cycle(ctx, &cycle, cycle_up, val))
  1407. return;
  1408. obj = pdf_dict_get(ctx, val, PDF_NAME(Name));
  1409. if (obj)
  1410. {
  1411. const char *name = "";
  1412. pdf_flush_text(ctx, proc);
  1413. if (pdf_is_name(ctx, obj))
  1414. name = pdf_to_name(ctx, obj);
  1415. else if (pdf_is_string(ctx, obj))
  1416. name = pdf_to_text_string(ctx, obj);
  1417. push_begin_layer(ctx, proc, name);
  1418. return;
  1419. }
  1420. obj = pdf_dict_get(ctx, val, PDF_NAME(OCGs));
  1421. n = pdf_array_len(ctx, obj);
  1422. for (i = 0; i < n; i++)
  1423. {
  1424. begin_oc(ctx, proc, pdf_array_get(ctx, obj, i), &cycle);
  1425. }
  1426. }
  1427. static void
  1428. end_oc(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val, pdf_cycle_list *cycle_up)
  1429. {
  1430. /* val has been resolved to a dict for us by the originally specified name
  1431. * having been looked up in Properties already for us. Either there will
  1432. * be a Name entry, or there will be an OCGs and it'll be a group one. */
  1433. pdf_cycle_list cycle;
  1434. pdf_obj *obj;
  1435. int i, n;
  1436. if (pdf_cycle(ctx, &cycle, cycle_up, val))
  1437. return;
  1438. obj = pdf_dict_get(ctx, val, PDF_NAME(Name));
  1439. if (obj)
  1440. {
  1441. flush_begin_layer(ctx, proc);
  1442. do_end_layer(ctx, proc);
  1443. return;
  1444. }
  1445. obj = pdf_dict_get(ctx, val, PDF_NAME(OCGs));
  1446. n = pdf_array_len(ctx, obj);
  1447. for (i = n-1; i >= 0; i--)
  1448. {
  1449. end_oc(ctx, proc, pdf_array_get(ctx, obj, i), &cycle);
  1450. }
  1451. }
  1452. static void
  1453. begin_layer(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val)
  1454. {
  1455. /* val has been resolved to a dict for us by the originally specified name
  1456. * having been looked up in Properties already for us. Go with the 'Title'
  1457. * entry. */
  1458. pdf_obj *obj = pdf_dict_get(ctx, val, PDF_NAME(Title));
  1459. if (obj)
  1460. {
  1461. pdf_flush_text(ctx, proc);
  1462. push_begin_layer(ctx, proc, pdf_to_text_string(ctx, obj));
  1463. }
  1464. }
  1465. static void
  1466. end_layer(fz_context *ctx, pdf_run_processor *proc, pdf_obj *val)
  1467. {
  1468. /* val has been resolved to a dict for us by the originally specified name
  1469. * having been looked up in Properties already for us. Go with the 'Title'
  1470. * entry. */
  1471. pdf_obj *obj = pdf_dict_get(ctx, val, PDF_NAME(Title));
  1472. if (obj)
  1473. {
  1474. do_end_layer(ctx, proc);
  1475. }
  1476. }
  1477. #ifdef DEBUG_STRUCTURE
  1478. static void
  1479. structure_dump(fz_context *ctx, const char *str, pdf_obj *obj)
  1480. {
  1481. fprintf(stderr, "%s STACK=", str);
  1482. if (obj == NULL)
  1483. {
  1484. fprintf(stderr, "empty\n");
  1485. return;
  1486. }
  1487. do
  1488. {
  1489. pdf_obj *s = pdf_dict_get(ctx, obj, PDF_NAME(S));
  1490. int n = pdf_to_num(ctx, obj);
  1491. fprintf(stderr, " %d", n);
  1492. if (s)
  1493. fprintf(stderr, "[%s]", pdf_to_name(ctx, s));
  1494. obj = pdf_dict_get(ctx, obj, PDF_NAME(P));
  1495. }
  1496. while (obj);
  1497. fprintf(stderr, "\n");
  1498. }
  1499. #endif
  1500. static void
  1501. pop_structure_to(fz_context *ctx, pdf_run_processor *proc, pdf_obj *common)
  1502. {
  1503. pdf_obj *struct_tree_root = pdf_dict_getl(ctx, pdf_trailer(ctx, proc->doc), PDF_NAME(Root), PDF_NAME(StructTreeRoot), NULL);
  1504. #ifdef DEBUG_STRUCTURE
  1505. structure_dump(ctx, "pop_structure_to (before)", proc->mcid_sent);
  1506. {
  1507. int n = pdf_to_num(ctx, common);
  1508. fprintf(stderr, "Popping until %d\n", n);
  1509. }
  1510. #endif
  1511. while (proc->mcid_sent != NULL && pdf_objcmp(ctx, proc->mcid_sent, common))
  1512. {
  1513. pdf_obj *p = pdf_dict_get(ctx, proc->mcid_sent, PDF_NAME(P));
  1514. pdf_obj *tag = pdf_dict_get(ctx, proc->mcid_sent, PDF_NAME(S));
  1515. fz_structure standard = pdf_structure_type(ctx, proc->role_map, tag);
  1516. #ifdef DEBUG_STRUCTURE
  1517. fprintf(stderr, "sending pop [tag=%s][std=%d]\n", pdf_to_name(ctx, tag) ? pdf_to_name(ctx, tag) : "null", standard);
  1518. #endif
  1519. if (standard != FZ_STRUCTURE_INVALID)
  1520. fz_end_structure(ctx, proc->dev);
  1521. pdf_drop_obj(ctx, proc->mcid_sent);
  1522. proc->mcid_sent = pdf_keep_obj(ctx, p);
  1523. if (!pdf_objcmp(ctx, p, struct_tree_root))
  1524. {
  1525. pdf_drop_obj(ctx, proc->mcid_sent);
  1526. proc->mcid_sent = NULL;
  1527. break;
  1528. }
  1529. }
  1530. #ifdef DEBUG_STRUCTURE
  1531. structure_dump(ctx, "pop_structure_to (after)", proc->mcid_sent);
  1532. #endif
  1533. }
  1534. static void
  1535. pop_any_pending_mcid_changes(fz_context *ctx, pdf_run_processor *pr)
  1536. {
  1537. if (pr->pending_mcid_pop == NULL)
  1538. return;
  1539. pop_structure_to(ctx, pr, pr->pending_mcid_pop);
  1540. pr->pending_mcid_pop = NULL;
  1541. }
  1542. struct line
  1543. {
  1544. pdf_obj *obj;
  1545. struct line *child;
  1546. };
  1547. static pdf_obj *
  1548. find_most_recent_common_ancestor_imp(fz_context *ctx, pdf_obj *a, struct line *line_a, pdf_obj *b, struct line *line_b, pdf_cycle_list *cycle_up_a, pdf_cycle_list *cycle_up_b)
  1549. {
  1550. struct line line;
  1551. pdf_obj *common = NULL;
  1552. pdf_cycle_list cycle;
  1553. pdf_obj *parent;
  1554. /* First ascend one lineage. */
  1555. if (pdf_is_dict(ctx, a))
  1556. {
  1557. if (pdf_cycle(ctx, &cycle, cycle_up_a, a))
  1558. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in structure tree");
  1559. line.obj = a;
  1560. line.child = line_a;
  1561. parent = pdf_dict_get(ctx, a, PDF_NAME(P));
  1562. return find_most_recent_common_ancestor_imp(ctx, parent, &line, b, NULL, &cycle, NULL);
  1563. }
  1564. /* Then ascend the other lineage. */
  1565. else if (pdf_is_dict(ctx, b))
  1566. {
  1567. if (pdf_cycle(ctx, &cycle, cycle_up_b, b))
  1568. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in structure tree");
  1569. line.obj = b;
  1570. line.child = line_b;
  1571. parent = pdf_dict_get(ctx, b, PDF_NAME(P));
  1572. return find_most_recent_common_ancestor_imp(ctx, a, line_a, parent, &line, cycle_up_a, &cycle);
  1573. }
  1574. /* Once both lineages are know, traverse top-down to find most recent common ancestor. */
  1575. while (line_a && line_b && !pdf_objcmp(ctx, line_a->obj, line_b->obj))
  1576. {
  1577. common = line_a->obj;
  1578. line_a = line_a->child;
  1579. line_b = line_b->child;
  1580. }
  1581. return common;
  1582. }
  1583. static pdf_obj *
  1584. find_most_recent_common_ancestor(fz_context *ctx, pdf_obj *a, pdf_obj *b)
  1585. {
  1586. if (!pdf_is_dict(ctx, a) || !pdf_is_dict(ctx, b))
  1587. return NULL;
  1588. return find_most_recent_common_ancestor_imp(ctx, a, NULL, b, NULL, NULL, NULL);
  1589. }
  1590. static int
  1591. get_struct_index(fz_context *ctx, pdf_obj *send)
  1592. {
  1593. pdf_obj *p = pdf_dict_get(ctx, send, PDF_NAME(P));
  1594. pdf_obj *k;
  1595. int i, n;
  1596. if (p == NULL)
  1597. return 0; /* Presumably the StructTreeRoot */
  1598. /* So, get the kids array. */
  1599. k = pdf_dict_get(ctx, p, PDF_NAME(K));
  1600. n = pdf_array_len(ctx, k);
  1601. if (n == 0)
  1602. {
  1603. /* Not an array, presumably a singleton. */
  1604. if (pdf_objcmp(ctx, k, send) == 0)
  1605. return 0;
  1606. return -1;
  1607. }
  1608. for (i = 0; i < n; i++)
  1609. {
  1610. if (pdf_objcmp(ctx, pdf_array_get(ctx, k, i), send) == 0)
  1611. return i;
  1612. }
  1613. return -1;
  1614. }
  1615. static int
  1616. send_begin_structure(fz_context *ctx, pdf_run_processor *proc, pdf_obj *mc_dict)
  1617. {
  1618. pdf_obj *common = NULL;
  1619. #ifdef DEBUG_STRUCTURE
  1620. fprintf(stderr, "send_begin_structure %d\n", pdf_to_num(ctx, mc_dict));
  1621. structure_dump(ctx, "on entry", proc->mcid_sent);
  1622. #endif
  1623. /* We are currently nested in A,B,C,...E,F,mcid_sent. We want to update to
  1624. * being in A,B,C,...G,H,mc_dict. So we need to find the lowest common point. */
  1625. common = find_most_recent_common_ancestor(ctx, proc->mcid_sent, mc_dict);
  1626. /* So, we need to pop everything up to common (i.e. everything below common will be closed). */
  1627. pop_structure_to(ctx, proc, common);
  1628. #ifdef DEBUG_STRUCTURE
  1629. structure_dump(ctx, "after popping", proc->mcid_sent);
  1630. #endif
  1631. /* Now we need to send everything between common (proc->mcid_sent) and mc_dict.
  1632. * Again, n^2 will do... */
  1633. while (pdf_objcmp(ctx, proc->mcid_sent, mc_dict))
  1634. {
  1635. pdf_obj *send = mc_dict;
  1636. fz_structure standard;
  1637. pdf_obj *tag;
  1638. int idx;
  1639. pdf_obj *slowptr = send;
  1640. int slow = 0;
  1641. /* Run up the ancestor stack, looking for the first child of mcid_sent.
  1642. * That's the one we need to send next. */
  1643. while (1) {
  1644. pdf_obj *p = pdf_dict_get(ctx, send, PDF_NAME(P));
  1645. /* If we ever fail to find a dict, then do not step down lest
  1646. * we can't get back later! */
  1647. if (!pdf_is_dict(ctx, send))
  1648. {
  1649. fz_warn(ctx, "Bad parent link in structure tree. Ignoring structure.");
  1650. proc->broken_struct_tree = 1;
  1651. return 0;
  1652. }
  1653. /* If p is the one we last sent, then we want to send 'send'
  1654. * next. Exit the loop. */
  1655. if (!pdf_objcmp(ctx, p, proc->mcid_sent))
  1656. break;
  1657. /* We need to go at least one step further up the stack. */
  1658. send = p;
  1659. /* Check for a loop in the parent tree. */
  1660. slow ^= 1;
  1661. if (slow == 0)
  1662. slowptr = pdf_dict_get(ctx, slowptr, PDF_NAME(P));
  1663. if (!pdf_objcmp(ctx, send, slowptr))
  1664. {
  1665. fz_warn(ctx, "Loop found in structure tree. Ignoring structure.");
  1666. proc->broken_struct_tree = 1;
  1667. return 0;
  1668. }
  1669. }
  1670. idx = get_struct_index(ctx, send);
  1671. tag = pdf_dict_get(ctx, send, PDF_NAME(S));
  1672. standard = pdf_structure_type(ctx, proc->role_map, tag);
  1673. #ifdef DEBUG_STRUCTURE
  1674. fprintf(stderr, "sending %d[idx=%d][tag=%s][std=%d]\n", pdf_to_num(ctx, send), idx, pdf_to_name(ctx, tag) ? pdf_to_name(ctx, tag) : "null", standard);
  1675. #endif
  1676. if (standard != FZ_STRUCTURE_INVALID)
  1677. fz_begin_structure(ctx, proc->dev, standard, pdf_to_name(ctx, tag), idx);
  1678. pdf_drop_obj(ctx, proc->mcid_sent);
  1679. proc->mcid_sent = pdf_keep_obj(ctx, send);
  1680. }
  1681. #ifdef DEBUG_STRUCTURE
  1682. structure_dump(ctx, "on exit", proc->mcid_sent);
  1683. #endif
  1684. return 1;
  1685. }
  1686. static void
  1687. push_marked_content(fz_context *ctx, pdf_run_processor *proc, const char *tagstr, pdf_obj *val)
  1688. {
  1689. pdf_obj *tag;
  1690. marked_content_stack *mc = NULL;
  1691. int drop_tag = 1;
  1692. pdf_obj *mc_dict = NULL;
  1693. /* Ignore any pending pops. */
  1694. proc->pending_mcid_pop = NULL;
  1695. /* Flush any pending text so it's not in the wrong layer. */
  1696. pdf_flush_text(ctx, proc);
  1697. if (!tagstr)
  1698. tagstr = "Untitled";
  1699. tag = pdf_new_name(ctx, tagstr);
  1700. fz_var(drop_tag);
  1701. fz_try(ctx)
  1702. {
  1703. /* First, push it on the stack. */
  1704. mc = fz_malloc_struct(ctx, marked_content_stack);
  1705. mc->next = proc->marked_content;
  1706. mc->tag = tag;
  1707. mc->val = pdf_keep_obj(ctx, val);
  1708. mc->structure_pushed = 0;
  1709. proc->marked_content = mc;
  1710. drop_tag = 0;
  1711. /* Check to see if val contains an MCID. */
  1712. mc_dict = lookup_mcid(ctx, proc, val);
  1713. /* Start any optional content layers. */
  1714. if (pdf_name_eq(ctx, tag, PDF_NAME(OC)))
  1715. begin_oc(ctx, proc, val, NULL);
  1716. /* Special handling for common non-spec extension. */
  1717. if (pdf_name_eq(ctx, tag, PDF_NAME(Layer)))
  1718. begin_layer(ctx, proc, val);
  1719. /* Structure */
  1720. if (mc_dict && !proc->broken_struct_tree)
  1721. {
  1722. fz_try(ctx)
  1723. mc->structure_pushed = send_begin_structure(ctx, proc, mc_dict);
  1724. fz_catch(ctx)
  1725. {
  1726. fz_report_error(ctx);
  1727. fz_warn(ctx, "structure tree broken, assume tree is missing");
  1728. proc->broken_struct_tree = 1;
  1729. }
  1730. }
  1731. /* Previously, I'd tried to send stuff like:
  1732. * /Artifact <</Type/Pagination>>BDC
  1733. * as a structure entry, lured by the fact that 'Artifact' is a
  1734. * structure tag. I now believe this is wrong. Only stuff with
  1735. * an MCID pointer should be sent using the structure mechanism.
  1736. */
  1737. /* ActualText */
  1738. begin_metatext(ctx, proc, val, mc_dict, FZ_METATEXT_ACTUALTEXT, PDF_NAME(ActualText));
  1739. /* Alt */
  1740. begin_metatext(ctx, proc, val, mc_dict, FZ_METATEXT_ALT, PDF_NAME(Alt));
  1741. /* Abbreviation */
  1742. begin_metatext(ctx, proc, val, mc_dict, FZ_METATEXT_ABBREVIATION, PDF_NAME(E));
  1743. /* Title */
  1744. begin_metatext(ctx, proc, val, mc_dict, FZ_METATEXT_TITLE, PDF_NAME(T));
  1745. }
  1746. fz_catch(ctx)
  1747. {
  1748. if (drop_tag)
  1749. pdf_drop_obj(ctx, tag);
  1750. fz_rethrow(ctx);
  1751. }
  1752. }
  1753. static void
  1754. pop_marked_content(fz_context *ctx, pdf_run_processor *proc, int neat)
  1755. {
  1756. marked_content_stack *mc = proc->marked_content;
  1757. pdf_obj *val, *tag;
  1758. pdf_obj *mc_dict = NULL;
  1759. int pushed;
  1760. if (mc == NULL)
  1761. return;
  1762. proc->marked_content = mc->next;
  1763. tag = mc->tag;
  1764. val = mc->val;
  1765. pushed = mc->structure_pushed;
  1766. fz_free(ctx, mc);
  1767. /* If we're not interested in neatly closing any open layers etc
  1768. * in the processor, (maybe we've had errors already), then just
  1769. * exit here. */
  1770. if (!neat)
  1771. {
  1772. pdf_drop_obj(ctx, tag);
  1773. pdf_drop_obj(ctx, val);
  1774. return;
  1775. }
  1776. /* Close structure/layers here, in reverse order to how we opened them. */
  1777. fz_try(ctx)
  1778. {
  1779. /* Make sure that any pending text is written into the correct layer. */
  1780. pdf_flush_text(ctx, proc);
  1781. /* Check to see if val contains an MCID. */
  1782. mc_dict = lookup_mcid(ctx, proc, val);
  1783. /* Title */
  1784. end_metatext(ctx, proc, val, mc_dict, PDF_NAME(T));
  1785. /* Abbreviation */
  1786. end_metatext(ctx, proc, val, mc_dict, PDF_NAME(E));
  1787. /* Alt */
  1788. end_metatext(ctx, proc, val, mc_dict, PDF_NAME(Alt));
  1789. /* ActualText */
  1790. end_metatext(ctx, proc, val, mc_dict, PDF_NAME(ActualText));
  1791. /* Structure */
  1792. if (mc_dict && !proc->broken_struct_tree && pushed)
  1793. {
  1794. /* Is there a nested mc_dict? If so we want to pop back to that.
  1795. * If not, we want to pop back to the top.
  1796. * proc->marked_content = the previous one, but maybe not the
  1797. * previous one with an mc_dict. So we may need to search further.
  1798. */
  1799. pdf_obj *previous_mcid = NULL;
  1800. marked_content_stack *mc_with_mcid = proc->marked_content;
  1801. while (mc_with_mcid)
  1802. {
  1803. previous_mcid = lookup_mcid(ctx, proc, mc_with_mcid->val);
  1804. if (previous_mcid != NULL)
  1805. break;
  1806. mc_with_mcid = mc_with_mcid->next;
  1807. }
  1808. proc->pending_mcid_pop = previous_mcid;
  1809. }
  1810. /* Finally, close any layers. */
  1811. if (pdf_name_eq(ctx, tag, PDF_NAME(Layer)))
  1812. end_layer(ctx, proc, val);
  1813. if (pdf_name_eq(ctx, tag, PDF_NAME(OC)))
  1814. end_oc(ctx, proc, val, NULL);
  1815. }
  1816. fz_always(ctx)
  1817. {
  1818. pdf_drop_obj(ctx, tag);
  1819. pdf_drop_obj(ctx, val);
  1820. }
  1821. fz_catch(ctx)
  1822. fz_rethrow(ctx);
  1823. }
  1824. static void
  1825. clear_marked_content(fz_context *ctx, pdf_run_processor *pr)
  1826. {
  1827. if (pr->marked_content == NULL)
  1828. return;
  1829. fz_try(ctx)
  1830. while (pr->marked_content)
  1831. pop_marked_content(ctx, pr, 1);
  1832. fz_always(ctx)
  1833. while (pr->marked_content)
  1834. pop_marked_content(ctx, pr, 0);
  1835. fz_catch(ctx)
  1836. fz_rethrow(ctx);
  1837. }
  1838. static void
  1839. pdf_run_xobject(fz_context *ctx, pdf_run_processor *pr, pdf_obj *xobj, pdf_obj *page_resources, fz_matrix transform, int is_smask)
  1840. {
  1841. pdf_cycle_list cycle_here;
  1842. pdf_gstate *gstate = NULL;
  1843. int oldtop = 0;
  1844. int oldbot = -1;
  1845. softmask_save softmask = { NULL };
  1846. int gparent_save;
  1847. fz_matrix gparent_save_ctm;
  1848. pdf_obj *resources;
  1849. fz_rect xobj_bbox;
  1850. fz_matrix xobj_matrix;
  1851. int transparency = 0;
  1852. pdf_document *doc;
  1853. fz_colorspace *cs = NULL;
  1854. fz_default_colorspaces *save_default_cs = NULL;
  1855. fz_default_colorspaces *xobj_default_cs = NULL;
  1856. marked_content_stack *save_marked_content = NULL;
  1857. int save_struct_parent;
  1858. pdf_obj *oc;
  1859. /* Avoid infinite recursion */
  1860. pdf_cycle_list *cycle_up = pr->cycle;
  1861. if (xobj == NULL || pdf_cycle(ctx, &cycle_here, cycle_up, xobj))
  1862. return;
  1863. pr->cycle = &cycle_here;
  1864. pop_any_pending_mcid_changes(ctx, pr);
  1865. flush_begin_layer(ctx, pr);
  1866. fz_var(cs);
  1867. fz_var(xobj_default_cs);
  1868. gparent_save = pr->gparent;
  1869. pr->gparent = pr->gtop;
  1870. oldtop = pr->gtop;
  1871. save_default_cs = pr->default_cs;
  1872. save_marked_content = pr->marked_content;
  1873. pr->marked_content = NULL;
  1874. save_struct_parent = pr->struct_parent;
  1875. fz_try(ctx)
  1876. {
  1877. pr->struct_parent = pdf_dict_get_int_default(ctx, xobj, PDF_NAME(StructParent), -1);
  1878. oc = pdf_dict_get(ctx, xobj, PDF_NAME(OC));
  1879. if (oc)
  1880. begin_oc(ctx, pr, oc, NULL);
  1881. pdf_gsave(ctx, pr);
  1882. gstate = pr->gstate + pr->gtop;
  1883. xobj_bbox = pdf_xobject_bbox(ctx, xobj);
  1884. xobj_matrix = pdf_xobject_matrix(ctx, xobj);
  1885. transparency = pdf_xobject_transparency(ctx, xobj);
  1886. /* apply xobject's transform matrix */
  1887. transform = fz_concat(xobj_matrix, transform);
  1888. gstate->ctm = fz_concat(transform, gstate->ctm);
  1889. /* The gparent is updated with the modified ctm */
  1890. gparent_save_ctm = pr->gstate[pr->gparent].ctm;
  1891. pr->gstate[pr->gparent].ctm = gstate->ctm;
  1892. /* apply soft mask, create transparency group and reset state */
  1893. if (transparency)
  1894. {
  1895. int isolated = pdf_xobject_isolated(ctx, xobj);
  1896. fz_rect bbox = fz_transform_rect(xobj_bbox, gstate->ctm);
  1897. gstate = begin_softmask(ctx, pr, &softmask, bbox);
  1898. if (isolated)
  1899. cs = pdf_xobject_colorspace(ctx, xobj);
  1900. fz_begin_group(ctx, pr->dev, bbox,
  1901. cs,
  1902. (is_smask ? 1 : isolated),
  1903. pdf_xobject_knockout(ctx, xobj),
  1904. gstate->blendmode, gstate->fill.alpha);
  1905. gstate->blendmode = 0;
  1906. gstate->stroke.alpha = 1;
  1907. gstate->fill.alpha = 1;
  1908. }
  1909. pdf_gsave(ctx, pr); /* Save here so the clippath doesn't persist */
  1910. /* clip to the bounds */
  1911. fz_moveto(ctx, pr->path, xobj_bbox.x0, xobj_bbox.y0);
  1912. fz_lineto(ctx, pr->path, xobj_bbox.x1, xobj_bbox.y0);
  1913. fz_lineto(ctx, pr->path, xobj_bbox.x1, xobj_bbox.y1);
  1914. fz_lineto(ctx, pr->path, xobj_bbox.x0, xobj_bbox.y1);
  1915. fz_closepath(ctx, pr->path);
  1916. pr->clip = 1;
  1917. pdf_show_path(ctx, pr, 0, 0, 0, 0);
  1918. /* run contents */
  1919. resources = pdf_xobject_resources(ctx, xobj);
  1920. if (!resources)
  1921. resources = page_resources;
  1922. fz_try(ctx)
  1923. xobj_default_cs = pdf_update_default_colorspaces(ctx, pr->default_cs, resources);
  1924. fz_catch(ctx)
  1925. {
  1926. fz_rethrow_unless(ctx, FZ_ERROR_TRYLATER);
  1927. fz_ignore_error(ctx);
  1928. if (pr->cookie)
  1929. pr->cookie->incomplete = 1;
  1930. }
  1931. if (xobj_default_cs != save_default_cs)
  1932. {
  1933. fz_set_default_colorspaces(ctx, pr->dev, xobj_default_cs);
  1934. pr->default_cs = xobj_default_cs;
  1935. }
  1936. doc = pdf_get_bound_document(ctx, xobj);
  1937. oldbot = pr->gbot;
  1938. pr->gbot = pr->gtop;
  1939. pdf_process_contents(ctx, (pdf_processor*)pr, doc, resources, xobj, pr->cookie, NULL);
  1940. /* Undo any gstate mismatches due to the pdf_process_contents call */
  1941. if (oldbot != -1)
  1942. {
  1943. while (pr->gtop > pr->gbot)
  1944. {
  1945. pdf_grestore(ctx, pr);
  1946. }
  1947. pr->gbot = oldbot;
  1948. }
  1949. pdf_grestore(ctx, pr); /* Remove the state we pushed for the clippath */
  1950. /* wrap up transparency stacks */
  1951. if (transparency)
  1952. {
  1953. fz_end_group(ctx, pr->dev);
  1954. end_softmask(ctx, pr, &softmask);
  1955. }
  1956. pr->gstate[pr->gparent].ctm = gparent_save_ctm;
  1957. pr->gparent = gparent_save;
  1958. while (oldtop < pr->gtop)
  1959. pdf_grestore(ctx, pr);
  1960. if (oc)
  1961. end_oc(ctx, pr, oc, NULL);
  1962. if (xobj_default_cs != save_default_cs)
  1963. {
  1964. fz_set_default_colorspaces(ctx, pr->dev, save_default_cs);
  1965. }
  1966. }
  1967. fz_always(ctx)
  1968. {
  1969. clear_marked_content(ctx, pr);
  1970. pr->marked_content = save_marked_content;
  1971. pr->default_cs = save_default_cs;
  1972. fz_drop_default_colorspaces(ctx, xobj_default_cs);
  1973. fz_drop_colorspace(ctx, cs);
  1974. pr->cycle = cycle_up;
  1975. pr->struct_parent = save_struct_parent;
  1976. }
  1977. fz_catch(ctx)
  1978. {
  1979. pdf_drop_obj(ctx, softmask.softmask);
  1980. fz_drop_colorspace(ctx, softmask.softmask_cs);
  1981. pdf_drop_obj(ctx, softmask.page_resources);
  1982. /* Note: Any SYNTAX errors should have been swallowed
  1983. * by pdf_process_contents, but in case any escape from other
  1984. * functions, recast the error type here to be safe. */
  1985. fz_morph_error(ctx, FZ_ERROR_SYNTAX, FZ_ERROR_FORMAT);
  1986. fz_rethrow(ctx);
  1987. }
  1988. }
  1989. /* general graphics state */
  1990. static void pdf_run_w(fz_context *ctx, pdf_processor *proc, float linewidth)
  1991. {
  1992. pdf_run_processor *pr = (pdf_run_processor *)proc;
  1993. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  1994. pop_any_pending_mcid_changes(ctx, pr);
  1995. flush_begin_layer(ctx, pr);
  1996. pr->dev->flags &= ~FZ_DEVFLAG_LINEWIDTH_UNDEFINED;
  1997. gstate->stroke_state = fz_unshare_stroke_state(ctx, gstate->stroke_state);
  1998. gstate->stroke_state->linewidth = linewidth;
  1999. }
  2000. static void pdf_run_j(fz_context *ctx, pdf_processor *proc, int linejoin)
  2001. {
  2002. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2003. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2004. pr->dev->flags &= ~FZ_DEVFLAG_LINEJOIN_UNDEFINED;
  2005. gstate->stroke_state = fz_unshare_stroke_state(ctx, gstate->stroke_state);
  2006. gstate->stroke_state->linejoin = linejoin;
  2007. }
  2008. static void pdf_run_J(fz_context *ctx, pdf_processor *proc, int linecap)
  2009. {
  2010. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2011. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2012. pr->dev->flags &= ~(FZ_DEVFLAG_STARTCAP_UNDEFINED | FZ_DEVFLAG_DASHCAP_UNDEFINED | FZ_DEVFLAG_ENDCAP_UNDEFINED);
  2013. gstate->stroke_state = fz_unshare_stroke_state(ctx, gstate->stroke_state);
  2014. gstate->stroke_state->start_cap = linecap;
  2015. gstate->stroke_state->dash_cap = linecap;
  2016. gstate->stroke_state->end_cap = linecap;
  2017. }
  2018. static void pdf_run_M(fz_context *ctx, pdf_processor *proc, float miterlimit)
  2019. {
  2020. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2021. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2022. pr->dev->flags &= ~FZ_DEVFLAG_MITERLIMIT_UNDEFINED;
  2023. gstate->stroke_state = fz_unshare_stroke_state(ctx, gstate->stroke_state);
  2024. gstate->stroke_state->miterlimit = miterlimit;
  2025. }
  2026. static void pdf_run_d(fz_context *ctx, pdf_processor *proc, pdf_obj *array, float phase)
  2027. {
  2028. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2029. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2030. int len, i;
  2031. pr->dev->flags &= ~FZ_DEVFLAG_DASH_PATTERN_UNDEFINED;
  2032. len = pdf_array_len(ctx, array);
  2033. gstate->stroke_state = fz_unshare_stroke_state_with_dash_len(ctx, gstate->stroke_state, len);
  2034. for (i = 0; i < len; i++)
  2035. gstate->stroke_state->dash_list[i] = pdf_array_get_real(ctx, array, i);
  2036. gstate->stroke_state->dash_phase = phase;
  2037. }
  2038. static void pdf_run_ri(fz_context *ctx, pdf_processor *proc, const char *intent)
  2039. {
  2040. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2041. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2042. gstate->fill.color_params.ri = fz_lookup_rendering_intent(intent);
  2043. gstate->stroke.color_params.ri = gstate->fill.color_params.ri;
  2044. }
  2045. static void pdf_run_gs_OP(fz_context *ctx, pdf_processor *proc, int b)
  2046. {
  2047. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2048. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2049. gstate->stroke.color_params.op = b;
  2050. gstate->fill.color_params.op = b;
  2051. }
  2052. static void pdf_run_gs_op(fz_context *ctx, pdf_processor *proc, int b)
  2053. {
  2054. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2055. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2056. gstate->fill.color_params.op = b;
  2057. }
  2058. static void pdf_run_gs_OPM(fz_context *ctx, pdf_processor *proc, int i)
  2059. {
  2060. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2061. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2062. gstate->stroke.color_params.opm = i;
  2063. gstate->fill.color_params.opm = i;
  2064. }
  2065. static void pdf_run_gs_UseBlackPtComp(fz_context *ctx, pdf_processor *proc, pdf_obj *obj)
  2066. {
  2067. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2068. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2069. int on = pdf_name_eq(ctx, obj, PDF_NAME(ON));
  2070. /* The spec says that "ON" means on, "OFF" means "Off", and
  2071. * "Default" or anything else means "Meh, do what you want." */
  2072. gstate->stroke.color_params.bp = on;
  2073. gstate->fill.color_params.bp = on;
  2074. }
  2075. static void pdf_run_i(fz_context *ctx, pdf_processor *proc, float flatness)
  2076. {
  2077. }
  2078. static void pdf_run_gs_begin(fz_context *ctx, pdf_processor *proc, const char *name, pdf_obj *extgstate)
  2079. {
  2080. }
  2081. static void pdf_run_gs_end(fz_context *ctx, pdf_processor *proc)
  2082. {
  2083. }
  2084. /* transparency graphics state */
  2085. static void pdf_run_gs_BM(fz_context *ctx, pdf_processor *proc, const char *blendmode)
  2086. {
  2087. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2088. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2089. gstate->blendmode = fz_lookup_blendmode(blendmode);
  2090. }
  2091. static void pdf_run_gs_CA(fz_context *ctx, pdf_processor *proc, float alpha)
  2092. {
  2093. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2094. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2095. gstate->stroke.alpha = fz_clamp(alpha, 0, 1);
  2096. }
  2097. static void pdf_run_gs_ca(fz_context *ctx, pdf_processor *proc, float alpha)
  2098. {
  2099. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2100. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2101. gstate->fill.alpha = fz_clamp(alpha, 0, 1);
  2102. }
  2103. static void pdf_run_gs_SMask(fz_context *ctx, pdf_processor *proc, pdf_obj *smask, fz_colorspace *smask_cs, float *bc, int luminosity, pdf_obj *tr)
  2104. {
  2105. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2106. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2107. int i;
  2108. if (gstate->softmask)
  2109. {
  2110. pdf_drop_obj(ctx, gstate->softmask);
  2111. gstate->softmask = NULL;
  2112. fz_drop_colorspace(ctx, gstate->softmask_cs);
  2113. gstate->softmask_cs = NULL;
  2114. pdf_drop_obj(ctx, gstate->softmask_resources);
  2115. gstate->softmask_resources = NULL;
  2116. }
  2117. if (smask)
  2118. {
  2119. int cs_n = fz_colorspace_n(ctx, smask_cs);
  2120. gstate->softmask_ctm = gstate->ctm;
  2121. gstate->softmask = pdf_keep_obj(ctx, smask);
  2122. gstate->softmask_cs = fz_keep_colorspace(ctx, smask_cs);
  2123. gstate->softmask_resources = pdf_keep_obj(ctx, pr->rstack->resources);
  2124. pdf_drop_obj(ctx, gstate->softmask_tr);
  2125. gstate->softmask_tr = NULL;
  2126. if (tr)
  2127. gstate->softmask_tr = pdf_keep_obj(ctx, tr);
  2128. for (i = 0; i < cs_n; ++i)
  2129. gstate->softmask_bc[i] = bc[i];
  2130. gstate->luminosity = luminosity;
  2131. }
  2132. }
  2133. /* special graphics state */
  2134. static void pdf_run_q(fz_context *ctx, pdf_processor *proc)
  2135. {
  2136. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2137. flush_begin_layer(ctx, pr);
  2138. pdf_gsave(ctx, pr);
  2139. }
  2140. static void pdf_run_Q(fz_context *ctx, pdf_processor *proc)
  2141. {
  2142. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2143. pdf_flush_text(ctx, pr);
  2144. pdf_grestore(ctx, pr);
  2145. }
  2146. static void pdf_run_cm(fz_context *ctx, pdf_processor *proc, float a, float b, float c, float d, float e, float f)
  2147. {
  2148. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2149. pdf_gstate *gstate = pdf_flush_text(ctx, pr);
  2150. fz_matrix m;
  2151. m.a = a;
  2152. m.b = b;
  2153. m.c = c;
  2154. m.d = d;
  2155. m.e = e;
  2156. m.f = f;
  2157. gstate->ctm = fz_concat(m, gstate->ctm);
  2158. }
  2159. /* path construction */
  2160. static void pdf_run_m(fz_context *ctx, pdf_processor *proc, float x, float y)
  2161. {
  2162. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2163. fz_moveto(ctx, pr->path, x, y);
  2164. }
  2165. static void pdf_run_l(fz_context *ctx, pdf_processor *proc, float x, float y)
  2166. {
  2167. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2168. fz_lineto(ctx, pr->path, x, y);
  2169. }
  2170. static void pdf_run_c(fz_context *ctx, pdf_processor *proc, float x1, float y1, float x2, float y2, float x3, float y3)
  2171. {
  2172. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2173. fz_curveto(ctx, pr->path, x1, y1, x2, y2, x3, y3);
  2174. }
  2175. static void pdf_run_v(fz_context *ctx, pdf_processor *proc, float x2, float y2, float x3, float y3)
  2176. {
  2177. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2178. fz_curvetov(ctx, pr->path, x2, y2, x3, y3);
  2179. }
  2180. static void pdf_run_y(fz_context *ctx, pdf_processor *proc, float x1, float y1, float x3, float y3)
  2181. {
  2182. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2183. fz_curvetoy(ctx, pr->path, x1, y1, x3, y3);
  2184. }
  2185. static void pdf_run_h(fz_context *ctx, pdf_processor *proc)
  2186. {
  2187. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2188. fz_closepath(ctx, pr->path);
  2189. }
  2190. static void pdf_run_re(fz_context *ctx, pdf_processor *proc, float x, float y, float w, float h)
  2191. {
  2192. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2193. fz_rectto(ctx, pr->path, x, y, x+w, y+h);
  2194. }
  2195. /* path painting */
  2196. static void pdf_run_S(fz_context *ctx, pdf_processor *proc)
  2197. {
  2198. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2199. pdf_show_path(ctx, pr, 0, 0, 1, 0);
  2200. }
  2201. static void pdf_run_s(fz_context *ctx, pdf_processor *proc)
  2202. {
  2203. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2204. pdf_show_path(ctx, pr, 1, 0, 1, 0);
  2205. }
  2206. static void pdf_run_F(fz_context *ctx, pdf_processor *proc)
  2207. {
  2208. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2209. pdf_show_path(ctx, pr, 0, 1, 0, 0);
  2210. }
  2211. static void pdf_run_f(fz_context *ctx, pdf_processor *proc)
  2212. {
  2213. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2214. pdf_show_path(ctx, pr, 0, 1, 0, 0);
  2215. }
  2216. static void pdf_run_fstar(fz_context *ctx, pdf_processor *proc)
  2217. {
  2218. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2219. pdf_show_path(ctx, pr, 0, 1, 0, 1);
  2220. }
  2221. static void pdf_run_B(fz_context *ctx, pdf_processor *proc)
  2222. {
  2223. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2224. pdf_show_path(ctx, pr, 0, 1, 1, 0);
  2225. }
  2226. static void pdf_run_Bstar(fz_context *ctx, pdf_processor *proc)
  2227. {
  2228. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2229. pdf_show_path(ctx, pr, 0, 1, 1, 1);
  2230. }
  2231. static void pdf_run_b(fz_context *ctx, pdf_processor *proc)
  2232. {
  2233. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2234. pdf_show_path(ctx, pr, 1, 1, 1, 0);
  2235. }
  2236. static void pdf_run_bstar(fz_context *ctx, pdf_processor *proc)
  2237. {
  2238. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2239. pdf_show_path(ctx, pr, 1, 1, 1, 1);
  2240. }
  2241. static void pdf_run_n(fz_context *ctx, pdf_processor *proc)
  2242. {
  2243. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2244. pdf_show_path(ctx, pr, 0, 0, 0, 0);
  2245. }
  2246. /* clipping paths */
  2247. static void pdf_run_W(fz_context *ctx, pdf_processor *proc)
  2248. {
  2249. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2250. pdf_flush_text(ctx, pr);
  2251. pr->clip = 1;
  2252. pr->clip_even_odd = 0;
  2253. }
  2254. static void pdf_run_Wstar(fz_context *ctx, pdf_processor *proc)
  2255. {
  2256. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2257. pdf_flush_text(ctx, pr);
  2258. pr->clip = 1;
  2259. pr->clip_even_odd = 1;
  2260. }
  2261. /* text objects */
  2262. static void pdf_run_BT(fz_context *ctx, pdf_processor *proc)
  2263. {
  2264. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2265. pr->tos.tm = fz_identity;
  2266. pr->tos.tlm = fz_identity;
  2267. pr->bidi = 0;
  2268. }
  2269. static void pdf_run_ET(fz_context *ctx, pdf_processor *proc)
  2270. {
  2271. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2272. pdf_flush_text(ctx, pr);
  2273. }
  2274. /* text state */
  2275. static void pdf_run_Tc(fz_context *ctx, pdf_processor *proc, float charspace)
  2276. {
  2277. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2278. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2279. gstate->text.char_space = charspace;
  2280. }
  2281. static void pdf_run_Tw(fz_context *ctx, pdf_processor *proc, float wordspace)
  2282. {
  2283. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2284. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2285. gstate->text.word_space = wordspace;
  2286. }
  2287. static void pdf_run_Tz(fz_context *ctx, pdf_processor *proc, float scale)
  2288. {
  2289. /* scale is as written in the file. It is 100 times smaller in
  2290. * the gstate. */
  2291. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2292. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2293. gstate->text.scale = scale / 100;
  2294. }
  2295. static void pdf_run_TL(fz_context *ctx, pdf_processor *proc, float leading)
  2296. {
  2297. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2298. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2299. gstate->text.leading = leading;
  2300. }
  2301. static void pdf_run_Tf(fz_context *ctx, pdf_processor *proc, const char *name, pdf_font_desc *font, float size)
  2302. {
  2303. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2304. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2305. pdf_drop_font(ctx, gstate->text.font);
  2306. gstate->text.font = pdf_keep_font(ctx, font);
  2307. gstate->text.size = size;
  2308. }
  2309. static void pdf_run_Tr(fz_context *ctx, pdf_processor *proc, int render)
  2310. {
  2311. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2312. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2313. gstate->text.render = render;
  2314. }
  2315. static void pdf_run_Ts(fz_context *ctx, pdf_processor *proc, float rise)
  2316. {
  2317. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2318. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2319. gstate->text.rise = rise;
  2320. }
  2321. /* text positioning */
  2322. static void pdf_run_Td(fz_context *ctx, pdf_processor *proc, float tx, float ty)
  2323. {
  2324. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2325. pdf_tos_translate(&pr->tos, tx, ty);
  2326. }
  2327. static void pdf_run_TD(fz_context *ctx, pdf_processor *proc, float tx, float ty)
  2328. {
  2329. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2330. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2331. gstate->text.leading = -ty;
  2332. pdf_tos_translate(&pr->tos, tx, ty);
  2333. }
  2334. static void pdf_run_Tm(fz_context *ctx, pdf_processor *proc, float a, float b, float c, float d, float e, float f)
  2335. {
  2336. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2337. pdf_tos_set_matrix(&pr->tos, a, b, c, d, e, f);
  2338. }
  2339. static void pdf_run_Tstar(fz_context *ctx, pdf_processor *proc)
  2340. {
  2341. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2342. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2343. pdf_tos_newline(&pr->tos, gstate->text.leading);
  2344. }
  2345. /* text showing */
  2346. static void pdf_run_TJ(fz_context *ctx, pdf_processor *proc, pdf_obj *obj)
  2347. {
  2348. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2349. pdf_show_text(ctx, pr, obj);
  2350. }
  2351. static void pdf_run_Tj(fz_context *ctx, pdf_processor *proc, char *string, size_t string_len)
  2352. {
  2353. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2354. pdf_show_string(ctx, pr, (unsigned char *)string, string_len);
  2355. }
  2356. static void pdf_run_squote(fz_context *ctx, pdf_processor *proc, char *string, size_t string_len)
  2357. {
  2358. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2359. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2360. pdf_tos_newline(&pr->tos, gstate->text.leading);
  2361. pdf_show_string(ctx, pr, (unsigned char*)string, string_len);
  2362. }
  2363. static void pdf_run_dquote(fz_context *ctx, pdf_processor *proc, float aw, float ac, char *string, size_t string_len)
  2364. {
  2365. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2366. pdf_gstate *gstate = pr->gstate + pr->gtop;
  2367. gstate->text.word_space = aw;
  2368. gstate->text.char_space = ac;
  2369. pdf_tos_newline(&pr->tos, gstate->text.leading);
  2370. pdf_show_string(ctx, pr, (unsigned char*)string, string_len);
  2371. }
  2372. /* type 3 fonts */
  2373. static void pdf_run_d0(fz_context *ctx, pdf_processor *proc, float wx, float wy)
  2374. {
  2375. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2376. pr->dev->flags |= FZ_DEVFLAG_COLOR;
  2377. }
  2378. static void pdf_run_d1(fz_context *ctx, pdf_processor *proc, float wx, float wy, float llx, float lly, float urx, float ury)
  2379. {
  2380. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2381. pr->dev->flags |= FZ_DEVFLAG_MASK | FZ_DEVFLAG_BBOX_DEFINED;
  2382. pr->dev->d1_rect.x0 = fz_min(llx, urx);
  2383. pr->dev->d1_rect.y0 = fz_min(lly, ury);
  2384. pr->dev->d1_rect.x1 = fz_max(llx, urx);
  2385. pr->dev->d1_rect.y1 = fz_max(lly, ury);
  2386. }
  2387. /* color */
  2388. static void pdf_run_CS(fz_context *ctx, pdf_processor *proc, const char *name, fz_colorspace *colorspace)
  2389. {
  2390. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2391. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2392. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2393. return;
  2394. if (!strcmp(name, "Pattern"))
  2395. pdf_set_pattern(ctx, pr, PDF_STROKE, NULL, NULL);
  2396. else
  2397. pdf_set_colorspace(ctx, pr, PDF_STROKE, colorspace);
  2398. }
  2399. static void pdf_run_cs(fz_context *ctx, pdf_processor *proc, const char *name, fz_colorspace *colorspace)
  2400. {
  2401. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2402. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2403. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2404. return;
  2405. if (!strcmp(name, "Pattern"))
  2406. pdf_set_pattern(ctx, pr, PDF_FILL, NULL, NULL);
  2407. else
  2408. pdf_set_colorspace(ctx, pr, PDF_FILL, colorspace);
  2409. }
  2410. static void pdf_run_SC_color(fz_context *ctx, pdf_processor *proc, int n, float *color)
  2411. {
  2412. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2413. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2414. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2415. return;
  2416. pdf_set_color(ctx, pr, PDF_STROKE, color);
  2417. }
  2418. static void pdf_run_sc_color(fz_context *ctx, pdf_processor *proc, int n, float *color)
  2419. {
  2420. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2421. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2422. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2423. return;
  2424. pdf_set_color(ctx, pr, PDF_FILL, color);
  2425. }
  2426. static void pdf_run_SC_pattern(fz_context *ctx, pdf_processor *proc, const char *name, pdf_pattern *pat, int n, float *color)
  2427. {
  2428. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2429. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2430. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2431. return;
  2432. pdf_set_pattern(ctx, pr, PDF_STROKE, pat, color);
  2433. }
  2434. static void pdf_run_sc_pattern(fz_context *ctx, pdf_processor *proc, const char *name, pdf_pattern *pat, int n, float *color)
  2435. {
  2436. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2437. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2438. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2439. return;
  2440. pdf_set_pattern(ctx, pr, PDF_FILL, pat, color);
  2441. }
  2442. static void pdf_run_SC_shade(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade *shade)
  2443. {
  2444. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2445. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2446. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2447. return;
  2448. pdf_set_shade(ctx, pr, PDF_STROKE, shade);
  2449. }
  2450. static void pdf_run_sc_shade(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade *shade)
  2451. {
  2452. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2453. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2454. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2455. return;
  2456. pdf_set_shade(ctx, pr, PDF_FILL, shade);
  2457. }
  2458. static void pdf_run_G(fz_context *ctx, pdf_processor *proc, float g)
  2459. {
  2460. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2461. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2462. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2463. return;
  2464. pdf_set_colorspace(ctx, pr, PDF_STROKE, fz_device_gray(ctx));
  2465. pdf_set_color(ctx, pr, PDF_STROKE, &g);
  2466. }
  2467. static void pdf_run_g(fz_context *ctx, pdf_processor *proc, float g)
  2468. {
  2469. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2470. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2471. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2472. return;
  2473. pdf_set_colorspace(ctx, pr, PDF_FILL, fz_device_gray(ctx));
  2474. pdf_set_color(ctx, pr, PDF_FILL, &g);
  2475. }
  2476. static void pdf_run_K(fz_context *ctx, pdf_processor *proc, float c, float m, float y, float k)
  2477. {
  2478. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2479. float color[4] = {c, m, y, k};
  2480. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2481. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2482. return;
  2483. pdf_set_colorspace(ctx, pr, PDF_STROKE, fz_device_cmyk(ctx));
  2484. pdf_set_color(ctx, pr, PDF_STROKE, color);
  2485. }
  2486. static void pdf_run_k(fz_context *ctx, pdf_processor *proc, float c, float m, float y, float k)
  2487. {
  2488. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2489. float color[4] = {c, m, y, k};
  2490. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2491. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2492. return;
  2493. pdf_set_colorspace(ctx, pr, PDF_FILL, fz_device_cmyk(ctx));
  2494. pdf_set_color(ctx, pr, PDF_FILL, color);
  2495. }
  2496. static void pdf_run_RG(fz_context *ctx, pdf_processor *proc, float r, float g, float b)
  2497. {
  2498. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2499. float color[3] = {r, g, b};
  2500. pr->dev->flags &= ~FZ_DEVFLAG_STROKECOLOR_UNDEFINED;
  2501. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2502. return;
  2503. pdf_set_colorspace(ctx, pr, PDF_STROKE, fz_device_rgb(ctx));
  2504. pdf_set_color(ctx, pr, PDF_STROKE, color);
  2505. }
  2506. static void pdf_run_rg(fz_context *ctx, pdf_processor *proc, float r, float g, float b)
  2507. {
  2508. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2509. float color[3] = {r, g, b};
  2510. pr->dev->flags &= ~FZ_DEVFLAG_FILLCOLOR_UNDEFINED;
  2511. if (pr->dev->flags & FZ_DEVFLAG_MASK)
  2512. return;
  2513. pdf_set_colorspace(ctx, pr, PDF_FILL, fz_device_rgb(ctx));
  2514. pdf_set_color(ctx, pr, PDF_FILL, color);
  2515. }
  2516. /* shadings, images, xobjects */
  2517. static void pdf_run_BI(fz_context *ctx, pdf_processor *proc, fz_image *image, const char *colorspace)
  2518. {
  2519. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2520. pdf_show_image(ctx, pr, image);
  2521. }
  2522. static void pdf_run_sh(fz_context *ctx, pdf_processor *proc, const char *name, fz_shade *shade)
  2523. {
  2524. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2525. pop_any_pending_mcid_changes(ctx, pr);
  2526. flush_begin_layer(ctx, pr);
  2527. pdf_show_shade(ctx, pr, shade);
  2528. }
  2529. static void pdf_run_Do_image(fz_context *ctx, pdf_processor *proc, const char *name, fz_image *image)
  2530. {
  2531. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2532. pdf_show_image(ctx, pr, image);
  2533. }
  2534. static void pdf_run_Do_form(fz_context *ctx, pdf_processor *proc, const char *name, pdf_obj *xobj)
  2535. {
  2536. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2537. pdf_run_xobject(ctx, (pdf_run_processor*)proc, xobj, pr->rstack->resources, fz_identity, 0);
  2538. }
  2539. /* marked content */
  2540. static void pdf_run_BMC(fz_context *ctx, pdf_processor *proc, const char *tag)
  2541. {
  2542. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2543. push_marked_content(ctx, pr, tag, NULL);
  2544. }
  2545. static void pdf_run_BDC(fz_context *ctx, pdf_processor *proc, const char *tag, pdf_obj *raw, pdf_obj *cooked)
  2546. {
  2547. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2548. push_marked_content(ctx, pr, tag, cooked);
  2549. }
  2550. static void pdf_run_EMC(fz_context *ctx, pdf_processor *proc)
  2551. {
  2552. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2553. pop_marked_content(ctx, pr, 1);
  2554. }
  2555. static void pdf_run_MP(fz_context *ctx, pdf_processor *proc, const char *tag)
  2556. {
  2557. pdf_run_BMC(ctx, proc, tag);
  2558. pdf_run_EMC(ctx, proc);
  2559. }
  2560. static void pdf_run_DP(fz_context *ctx, pdf_processor *proc, const char *tag, pdf_obj *raw, pdf_obj *cooked)
  2561. {
  2562. pdf_run_BDC(ctx, proc, tag, raw, cooked);
  2563. pdf_run_EMC(ctx, proc);
  2564. }
  2565. /* compatibility */
  2566. static void pdf_run_BX(fz_context *ctx, pdf_processor *proc)
  2567. {
  2568. }
  2569. static void pdf_run_EX(fz_context *ctx, pdf_processor *proc)
  2570. {
  2571. }
  2572. static void pdf_run_END(fz_context *ctx, pdf_processor *proc)
  2573. {
  2574. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2575. pdf_flush_text(ctx, pr);
  2576. }
  2577. static void
  2578. pdf_close_run_processor(fz_context *ctx, pdf_processor *proc)
  2579. {
  2580. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2581. while (pr->gtop)
  2582. pdf_grestore(ctx, pr);
  2583. while (pr->nest_depth > 0)
  2584. {
  2585. if (pr->nest_mark[pr->nest_depth-1] < 0)
  2586. {
  2587. /* It's a clip. */
  2588. fz_pop_clip(ctx, pr->dev);
  2589. pr->nest_mark[pr->nest_depth-1]++;
  2590. if (pr->nest_mark[pr->nest_depth-1] == 0)
  2591. pr->nest_depth--;
  2592. }
  2593. else
  2594. {
  2595. /* It's a layer. */
  2596. fz_end_layer(ctx, pr->dev);
  2597. pr->nest_depth--;
  2598. }
  2599. }
  2600. pop_structure_to(ctx, pr, NULL);
  2601. clear_marked_content(ctx, pr);
  2602. }
  2603. static void
  2604. pdf_drop_run_processor(fz_context *ctx, pdf_processor *proc)
  2605. {
  2606. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2607. while (pr->gtop >= 0)
  2608. {
  2609. pdf_drop_gstate(ctx, &pr->gstate[pr->gtop]);
  2610. pr->gtop--;
  2611. }
  2612. fz_drop_path(ctx, pr->path);
  2613. fz_drop_text(ctx, pr->tos.text);
  2614. fz_drop_default_colorspaces(ctx, pr->default_cs);
  2615. fz_free(ctx, pr->gstate);
  2616. while (pr->rstack)
  2617. {
  2618. resources_stack *stk = pr->rstack;
  2619. pr->rstack = stk->next;
  2620. pdf_drop_obj(ctx, stk->resources);
  2621. fz_free(ctx, stk);
  2622. }
  2623. while (pr->begin_layer)
  2624. {
  2625. begin_layer_stack *stk = pr->begin_layer;
  2626. pr->begin_layer = stk->next;
  2627. fz_free(ctx, stk->layer);
  2628. fz_free(ctx, stk);
  2629. }
  2630. while (pr->marked_content)
  2631. pop_marked_content(ctx, pr, 0);
  2632. pdf_drop_obj(ctx, pr->mcid_sent);
  2633. pdf_drop_document(ctx, pr->doc);
  2634. pdf_drop_obj(ctx, pr->role_map);
  2635. }
  2636. static void
  2637. pdf_run_push_resources(fz_context *ctx, pdf_processor *proc, pdf_obj *resources)
  2638. {
  2639. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2640. resources_stack *stk = fz_malloc_struct(ctx, resources_stack);
  2641. stk->next = pr->rstack;
  2642. pr->rstack = stk;
  2643. stk->resources = pdf_keep_obj(ctx, resources);
  2644. }
  2645. static pdf_obj *
  2646. pdf_run_pop_resources(fz_context *ctx, pdf_processor *proc)
  2647. {
  2648. pdf_run_processor *pr = (pdf_run_processor *)proc;
  2649. resources_stack *stk = pr->rstack;
  2650. if (stk)
  2651. {
  2652. pr->rstack = stk->next;
  2653. pdf_drop_obj(ctx, stk->resources);
  2654. fz_free(ctx, stk);
  2655. }
  2656. return NULL;
  2657. }
  2658. /*
  2659. Create a new "run" processor. This maps
  2660. from PDF operators to fz_device level calls.
  2661. dev: The device to which the resulting device calls are to be
  2662. sent.
  2663. ctm: The initial transformation matrix to use.
  2664. usage: A NULL terminated string that describes the 'usage' of
  2665. this interpretation. Typically 'View', though 'Print' is also
  2666. defined within the PDF reference manual, and others are possible.
  2667. gstate: The initial graphics state.
  2668. */
  2669. pdf_processor *
  2670. pdf_new_run_processor(fz_context *ctx, pdf_document *doc, fz_device *dev, fz_matrix ctm, int struct_parent, const char *usage, pdf_gstate *gstate, fz_default_colorspaces *default_cs, fz_cookie *cookie, pdf_gstate *fill_gstate, pdf_gstate *stroke_gstate)
  2671. {
  2672. pdf_run_processor *proc = pdf_new_processor(ctx, sizeof *proc);
  2673. {
  2674. proc->super.usage = usage;
  2675. proc->super.close_processor = pdf_close_run_processor;
  2676. proc->super.drop_processor = pdf_drop_run_processor;
  2677. proc->super.push_resources = pdf_run_push_resources;
  2678. proc->super.pop_resources = pdf_run_pop_resources;
  2679. /* general graphics state */
  2680. proc->super.op_w = pdf_run_w;
  2681. proc->super.op_j = pdf_run_j;
  2682. proc->super.op_J = pdf_run_J;
  2683. proc->super.op_M = pdf_run_M;
  2684. proc->super.op_d = pdf_run_d;
  2685. proc->super.op_ri = pdf_run_ri;
  2686. proc->super.op_i = pdf_run_i;
  2687. proc->super.op_gs_begin = pdf_run_gs_begin;
  2688. proc->super.op_gs_end = pdf_run_gs_end;
  2689. /* transparency graphics state */
  2690. proc->super.op_gs_BM = pdf_run_gs_BM;
  2691. proc->super.op_gs_CA = pdf_run_gs_CA;
  2692. proc->super.op_gs_ca = pdf_run_gs_ca;
  2693. proc->super.op_gs_SMask = pdf_run_gs_SMask;
  2694. /* special graphics state */
  2695. proc->super.op_q = pdf_run_q;
  2696. proc->super.op_Q = pdf_run_Q;
  2697. proc->super.op_cm = pdf_run_cm;
  2698. /* path construction */
  2699. proc->super.op_m = pdf_run_m;
  2700. proc->super.op_l = pdf_run_l;
  2701. proc->super.op_c = pdf_run_c;
  2702. proc->super.op_v = pdf_run_v;
  2703. proc->super.op_y = pdf_run_y;
  2704. proc->super.op_h = pdf_run_h;
  2705. proc->super.op_re = pdf_run_re;
  2706. /* path painting */
  2707. proc->super.op_S = pdf_run_S;
  2708. proc->super.op_s = pdf_run_s;
  2709. proc->super.op_F = pdf_run_F;
  2710. proc->super.op_f = pdf_run_f;
  2711. proc->super.op_fstar = pdf_run_fstar;
  2712. proc->super.op_B = pdf_run_B;
  2713. proc->super.op_Bstar = pdf_run_Bstar;
  2714. proc->super.op_b = pdf_run_b;
  2715. proc->super.op_bstar = pdf_run_bstar;
  2716. proc->super.op_n = pdf_run_n;
  2717. /* clipping paths */
  2718. proc->super.op_W = pdf_run_W;
  2719. proc->super.op_Wstar = pdf_run_Wstar;
  2720. /* text objects */
  2721. proc->super.op_BT = pdf_run_BT;
  2722. proc->super.op_ET = pdf_run_ET;
  2723. /* text state */
  2724. proc->super.op_Tc = pdf_run_Tc;
  2725. proc->super.op_Tw = pdf_run_Tw;
  2726. proc->super.op_Tz = pdf_run_Tz;
  2727. proc->super.op_TL = pdf_run_TL;
  2728. proc->super.op_Tf = pdf_run_Tf;
  2729. proc->super.op_Tr = pdf_run_Tr;
  2730. proc->super.op_Ts = pdf_run_Ts;
  2731. /* text positioning */
  2732. proc->super.op_Td = pdf_run_Td;
  2733. proc->super.op_TD = pdf_run_TD;
  2734. proc->super.op_Tm = pdf_run_Tm;
  2735. proc->super.op_Tstar = pdf_run_Tstar;
  2736. /* text showing */
  2737. proc->super.op_TJ = pdf_run_TJ;
  2738. proc->super.op_Tj = pdf_run_Tj;
  2739. proc->super.op_squote = pdf_run_squote;
  2740. proc->super.op_dquote = pdf_run_dquote;
  2741. /* type 3 fonts */
  2742. proc->super.op_d0 = pdf_run_d0;
  2743. proc->super.op_d1 = pdf_run_d1;
  2744. /* color */
  2745. proc->super.op_CS = pdf_run_CS;
  2746. proc->super.op_cs = pdf_run_cs;
  2747. proc->super.op_SC_color = pdf_run_SC_color;
  2748. proc->super.op_sc_color = pdf_run_sc_color;
  2749. proc->super.op_SC_pattern = pdf_run_SC_pattern;
  2750. proc->super.op_sc_pattern = pdf_run_sc_pattern;
  2751. proc->super.op_SC_shade = pdf_run_SC_shade;
  2752. proc->super.op_sc_shade = pdf_run_sc_shade;
  2753. proc->super.op_G = pdf_run_G;
  2754. proc->super.op_g = pdf_run_g;
  2755. proc->super.op_RG = pdf_run_RG;
  2756. proc->super.op_rg = pdf_run_rg;
  2757. proc->super.op_K = pdf_run_K;
  2758. proc->super.op_k = pdf_run_k;
  2759. /* shadings, images, xobjects */
  2760. proc->super.op_sh = pdf_run_sh;
  2761. if (dev->fill_image || dev->fill_image_mask || dev->clip_image_mask)
  2762. {
  2763. proc->super.op_BI = pdf_run_BI;
  2764. proc->super.op_Do_image = pdf_run_Do_image;
  2765. }
  2766. proc->super.op_Do_form = pdf_run_Do_form;
  2767. /* marked content */
  2768. proc->super.op_MP = pdf_run_MP;
  2769. proc->super.op_DP = pdf_run_DP;
  2770. proc->super.op_BMC = pdf_run_BMC;
  2771. proc->super.op_BDC = pdf_run_BDC;
  2772. proc->super.op_EMC = pdf_run_EMC;
  2773. /* compatibility */
  2774. proc->super.op_BX = pdf_run_BX;
  2775. proc->super.op_EX = pdf_run_EX;
  2776. /* extgstate */
  2777. proc->super.op_gs_OP = pdf_run_gs_OP;
  2778. proc->super.op_gs_op = pdf_run_gs_op;
  2779. proc->super.op_gs_OPM = pdf_run_gs_OPM;
  2780. proc->super.op_gs_UseBlackPtComp = pdf_run_gs_UseBlackPtComp;
  2781. proc->super.op_END = pdf_run_END;
  2782. }
  2783. proc->super.requirements = 0;
  2784. if ((dev->hints & FZ_DONT_DECODE_IMAGES) == 0)
  2785. proc->super.requirements |= PDF_PROCESSOR_REQUIRES_DECODED_IMAGES;
  2786. proc->doc = pdf_keep_document(ctx, doc);
  2787. proc->dev = dev;
  2788. proc->cookie = cookie;
  2789. proc->default_cs = fz_keep_default_colorspaces(ctx, default_cs);
  2790. proc->path = NULL;
  2791. proc->clip = 0;
  2792. proc->clip_even_odd = 0;
  2793. proc->tos.text = NULL;
  2794. proc->tos.tlm = fz_identity;
  2795. proc->tos.tm = fz_identity;
  2796. proc->tos.text_mode = 0;
  2797. proc->gtop = -1;
  2798. proc->marked_content = NULL;
  2799. proc->next_begin_layer = &proc->begin_layer;
  2800. fz_try(ctx)
  2801. {
  2802. proc->path = fz_new_path(ctx);
  2803. proc->gcap = 64;
  2804. proc->gstate = fz_malloc_struct_array(ctx, proc->gcap, pdf_gstate);
  2805. proc->gtop = 0;
  2806. pdf_init_gstate(ctx, &proc->gstate[0], ctm);
  2807. if (fill_gstate)
  2808. {
  2809. pdf_copy_gstate(ctx, &proc->gstate[0], fill_gstate);
  2810. proc->gstate[0].clip_depth = 0;
  2811. proc->gtop++;
  2812. }
  2813. if (stroke_gstate)
  2814. {
  2815. pdf_copy_gstate(ctx, &proc->gstate[proc->gtop], stroke_gstate);
  2816. proc->gstate[proc->gtop].clip_depth = 0;
  2817. proc->gtop++;
  2818. }
  2819. if (gstate)
  2820. {
  2821. pdf_copy_gstate(ctx, &proc->gstate[proc->gtop], gstate);
  2822. proc->gstate[proc->gtop].clip_depth = 0;
  2823. proc->gstate[proc->gtop].ctm = ctm;
  2824. }
  2825. proc->gparent = proc->gtop;
  2826. if (fill_gstate)
  2827. proc->gstate[proc->gtop].fill.gstate_num = 0;
  2828. if (stroke_gstate)
  2829. proc->gstate[proc->gtop].fill.gstate_num = (fill_gstate != NULL);
  2830. /* We need to save an extra level to allow for the parent gstate level. */
  2831. pdf_gsave(ctx, proc);
  2832. /* Structure details */
  2833. {
  2834. pdf_obj *struct_tree_root = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(StructTreeRoot), NULL);
  2835. proc->struct_parent = struct_parent;
  2836. proc->role_map = pdf_keep_obj(ctx, pdf_dict_get(ctx, struct_tree_root, PDF_NAME(RoleMap)));
  2837. /* Annotations and XObjects can be their own content items. We spot this by
  2838. * the struct_parent looking up to be a singular object. */
  2839. if (struct_parent != -1 && struct_tree_root)
  2840. {
  2841. pdf_obj *struct_obj = pdf_lookup_number(ctx, pdf_dict_get(ctx, struct_tree_root, PDF_NAME(ParentTree)), struct_parent);
  2842. if (pdf_is_dict(ctx, struct_obj))
  2843. send_begin_structure(ctx, proc, struct_obj);
  2844. /* We always end structure as required on closedown, so this is safe. */
  2845. }
  2846. }
  2847. }
  2848. fz_catch(ctx)
  2849. {
  2850. pdf_drop_run_processor(ctx, (pdf_processor *) proc);
  2851. fz_free(ctx, proc);
  2852. fz_rethrow(ctx);
  2853. }
  2854. return (pdf_processor*)proc;
  2855. }