pdf-form.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  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 "pdf-annot-imp.h"
  24. #include <string.h>
  25. /* Must be kept in sync with definitions in pdf_util.js */
  26. enum
  27. {
  28. Display_Visible,
  29. Display_Hidden,
  30. Display_NoPrint,
  31. Display_NoView
  32. };
  33. enum
  34. {
  35. SigFlag_SignaturesExist = 1,
  36. SigFlag_AppendOnly = 2
  37. };
  38. const char *pdf_field_value(fz_context *ctx, pdf_obj *field)
  39. {
  40. pdf_obj *v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
  41. if (pdf_is_name(ctx, v))
  42. return pdf_to_name(ctx, v);
  43. if (pdf_is_stream(ctx, v))
  44. {
  45. // FIXME: pdf_dict_put_inheritable...
  46. char *str = pdf_new_utf8_from_pdf_stream_obj(ctx, v);
  47. fz_try(ctx)
  48. pdf_dict_put_text_string(ctx, field, PDF_NAME(V), str);
  49. fz_always(ctx)
  50. fz_free(ctx, str);
  51. fz_catch(ctx)
  52. fz_rethrow(ctx);
  53. v = pdf_dict_get(ctx, field, PDF_NAME(V));
  54. }
  55. return pdf_to_text_string(ctx, v);
  56. }
  57. int pdf_field_flags(fz_context *ctx, pdf_obj *obj)
  58. {
  59. return pdf_dict_get_inheritable_int(ctx, obj, PDF_NAME(Ff));
  60. }
  61. int pdf_field_type(fz_context *ctx, pdf_obj *obj)
  62. {
  63. pdf_obj *type = pdf_dict_get_inheritable(ctx, obj, PDF_NAME(FT));
  64. int flags = pdf_field_flags(ctx, obj);
  65. if (pdf_name_eq(ctx, type, PDF_NAME(Btn)))
  66. {
  67. if (flags & PDF_BTN_FIELD_IS_PUSHBUTTON)
  68. return PDF_WIDGET_TYPE_BUTTON;
  69. else if (flags & PDF_BTN_FIELD_IS_RADIO)
  70. return PDF_WIDGET_TYPE_RADIOBUTTON;
  71. else
  72. return PDF_WIDGET_TYPE_CHECKBOX;
  73. }
  74. else if (pdf_name_eq(ctx, type, PDF_NAME(Tx)))
  75. return PDF_WIDGET_TYPE_TEXT;
  76. else if (pdf_name_eq(ctx, type, PDF_NAME(Ch)))
  77. {
  78. if (flags & PDF_CH_FIELD_IS_COMBO)
  79. return PDF_WIDGET_TYPE_COMBOBOX;
  80. else
  81. return PDF_WIDGET_TYPE_LISTBOX;
  82. }
  83. else if (pdf_name_eq(ctx, type, PDF_NAME(Sig)))
  84. return PDF_WIDGET_TYPE_SIGNATURE;
  85. else
  86. return PDF_WIDGET_TYPE_BUTTON;
  87. }
  88. const char *pdf_field_type_string(fz_context *ctx, pdf_obj *obj)
  89. {
  90. switch (pdf_field_type(ctx, obj))
  91. {
  92. default:
  93. case PDF_WIDGET_TYPE_BUTTON: return "button";
  94. case PDF_WIDGET_TYPE_CHECKBOX: return "checkbox";
  95. case PDF_WIDGET_TYPE_COMBOBOX: return "combobox";
  96. case PDF_WIDGET_TYPE_LISTBOX: return "listbox";
  97. case PDF_WIDGET_TYPE_RADIOBUTTON: return "radiobutton";
  98. case PDF_WIDGET_TYPE_SIGNATURE: return "signature";
  99. case PDF_WIDGET_TYPE_TEXT: return "text";
  100. }
  101. }
  102. /* Find the point in a field hierarchy where all descendants
  103. * share the same name */
  104. static pdf_obj *find_head_of_field_group(fz_context *ctx, pdf_obj *obj)
  105. {
  106. if (obj == NULL || pdf_dict_get(ctx, obj, PDF_NAME(T)))
  107. return obj;
  108. else
  109. return find_head_of_field_group(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Parent)));
  110. }
  111. static void pdf_field_mark_dirty(fz_context *ctx, pdf_obj *field)
  112. {
  113. pdf_document *doc = pdf_get_bound_document(ctx, field);
  114. pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
  115. if (kids)
  116. {
  117. int i, n = pdf_array_len(ctx, kids);
  118. for (i = 0; i < n; i++)
  119. pdf_field_mark_dirty(ctx, pdf_array_get(ctx, kids, i));
  120. }
  121. pdf_dirty_obj(ctx, field);
  122. if (doc)
  123. doc->resynth_required = 1;
  124. }
  125. static void update_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *obj, const char *text)
  126. {
  127. const char *old_text;
  128. pdf_obj *grp;
  129. if (!text)
  130. text = "";
  131. /* All fields of the same name should be updated, so
  132. * set the value at the head of the group */
  133. grp = find_head_of_field_group(ctx, obj);
  134. if (grp)
  135. obj = grp;
  136. /* Only update if we change the actual value. */
  137. old_text = pdf_dict_get_text_string(ctx, obj, PDF_NAME(V));
  138. if (old_text && !strcmp(old_text, text))
  139. return;
  140. pdf_dict_put_text_string(ctx, obj, PDF_NAME(V), text);
  141. pdf_field_mark_dirty(ctx, obj);
  142. }
  143. static pdf_obj *
  144. pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up);
  145. static pdf_obj *
  146. lookup_field_sub(fz_context *ctx, pdf_obj *dict, const char *str, pdf_cycle_list *cycle_up)
  147. {
  148. pdf_obj *kids;
  149. pdf_obj *name;
  150. name = pdf_dict_get(ctx, dict, PDF_NAME(T));
  151. /* If we have a name, check it matches. If it matches, consume that
  152. * portion of str. If not, exit. */
  153. if (name)
  154. {
  155. const char *match = pdf_to_text_string(ctx, name);
  156. const char *e = str;
  157. size_t len;
  158. while (*e && *e != '.')
  159. e++;
  160. len = e-str;
  161. if (strncmp(str, match, len) != 0 || (match[len] != 0 && match[len] != '.'))
  162. /* name doesn't match. */
  163. return NULL;
  164. str = e;
  165. if (*str == '.')
  166. str++;
  167. }
  168. /* If there is a kids array, but the search string is not empty, we have
  169. encountered an internal field which represents a set of terminal fields. */
  170. /* If there is a kids array and the search string is not empty,
  171. walk those looking for the appropriate one. */
  172. kids = pdf_dict_get(ctx, dict, PDF_NAME(Kids));
  173. if (kids && *str != 0)
  174. return pdf_lookup_field_imp(ctx, kids, str, cycle_up);
  175. /* The field may be a terminal or an internal field at this point.
  176. Accept it as the match if the match string is exhausted. */
  177. if (*str == 0)
  178. return dict;
  179. return NULL;
  180. }
  181. static pdf_obj *
  182. pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up)
  183. {
  184. pdf_cycle_list cycle;
  185. int len = pdf_array_len(ctx, arr);
  186. int i;
  187. for (i = 0; i < len; i++)
  188. {
  189. pdf_obj *k = pdf_array_get(ctx, arr, i);
  190. pdf_obj *found;
  191. if (pdf_cycle(ctx, &cycle, cycle_up, k))
  192. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in fields");
  193. found = lookup_field_sub(ctx, k, str, &cycle);
  194. if (found)
  195. return found;
  196. }
  197. return NULL;
  198. }
  199. pdf_obj *
  200. pdf_lookup_field(fz_context *ctx, pdf_obj *arr, const char *str)
  201. {
  202. return pdf_lookup_field_imp(ctx, arr, str, NULL);
  203. }
  204. static void reset_form_field(fz_context *ctx, pdf_document *doc, pdf_obj *field)
  205. {
  206. /* Set V to DV wherever DV is present, and delete V where DV is not.
  207. * FIXME: we assume for now that V has not been set unequal
  208. * to DV higher in the hierarchy than "field".
  209. *
  210. * At the bottom of the hierarchy we may find widget annotations
  211. * that aren't also fields, but DV and V will not be present in their
  212. * dictionaries, and attempts to remove V will be harmless. */
  213. pdf_obj *dv = pdf_dict_get(ctx, field, PDF_NAME(DV));
  214. pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
  215. if (dv)
  216. pdf_dict_put(ctx, field, PDF_NAME(V), dv);
  217. else
  218. pdf_dict_del(ctx, field, PDF_NAME(V));
  219. if (kids == NULL)
  220. {
  221. /* The leaves of the tree are widget annotations
  222. * In some cases we need to update the appearance state;
  223. * in others we need to mark the field as dirty so that
  224. * the appearance stream will be regenerated. */
  225. switch (pdf_field_type(ctx, field))
  226. {
  227. case PDF_WIDGET_TYPE_CHECKBOX:
  228. case PDF_WIDGET_TYPE_RADIOBUTTON:
  229. {
  230. pdf_obj *leafv = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
  231. pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
  232. pdf_obj *n = pdf_dict_get(ctx, ap, PDF_NAME(N));
  233. /* Value does not refer to any appearance state in the
  234. normal appearance stream dictionary, default to Off instead. */
  235. if (pdf_is_dict(ctx, n) && !pdf_dict_get(ctx, n, leafv))
  236. leafv = NULL;
  237. if (!pdf_is_name(ctx, leafv))
  238. leafv = PDF_NAME(Off);
  239. pdf_dict_put(ctx, field, PDF_NAME(AS), leafv);
  240. }
  241. pdf_field_mark_dirty(ctx, field);
  242. break;
  243. case PDF_WIDGET_TYPE_BUTTON:
  244. case PDF_WIDGET_TYPE_SIGNATURE:
  245. /* Pushbuttons and signatures have no value to reset. */
  246. break;
  247. default:
  248. pdf_field_mark_dirty(ctx, field);
  249. break;
  250. }
  251. }
  252. }
  253. void pdf_field_reset(fz_context *ctx, pdf_document *doc, pdf_obj *field)
  254. {
  255. pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
  256. reset_form_field(ctx, doc, field);
  257. if (kids)
  258. {
  259. int i, n = pdf_array_len(ctx, kids);
  260. for (i = 0; i < n; i++)
  261. pdf_field_reset(ctx, doc, pdf_array_get(ctx, kids, i));
  262. }
  263. }
  264. static void add_field_hierarchy_to_array(fz_context *ctx, pdf_obj *array, pdf_obj *field, pdf_obj *fields, int exclude)
  265. {
  266. pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
  267. char *needle = pdf_load_field_name(ctx, field);
  268. int i, n;
  269. fz_try(ctx)
  270. {
  271. n = pdf_array_len(ctx, fields);
  272. for (i = 0; i < n; i++)
  273. {
  274. char *name = pdf_load_field_name(ctx, pdf_array_get(ctx, fields, i));
  275. int found = !strcmp(needle, name);
  276. fz_free(ctx, name);
  277. if (found)
  278. break;
  279. }
  280. }
  281. fz_always(ctx)
  282. fz_free(ctx, needle);
  283. fz_catch(ctx)
  284. fz_rethrow(ctx);
  285. if ((exclude && i < n) || (!exclude && i == n))
  286. return;
  287. pdf_array_push(ctx, array, field);
  288. if (kids)
  289. {
  290. n = pdf_array_len(ctx, kids);
  291. for (i = 0; i < n; i++)
  292. add_field_hierarchy_to_array(ctx, array, pdf_array_get(ctx, kids, i), fields, exclude);
  293. }
  294. }
  295. /*
  296. When resetting or submitting a form, the fields to act upon are defined
  297. by an array of either field references or field names, plus a flag determining
  298. whether to act upon the fields in the array, or all fields other than those in
  299. the array. specified_fields interprets this information and produces the array
  300. of fields to be acted upon.
  301. */
  302. static pdf_obj *specified_fields(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
  303. {
  304. pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
  305. int i, n;
  306. pdf_obj *result = pdf_new_array(ctx, doc, 0);
  307. fz_try(ctx)
  308. {
  309. n = pdf_array_len(ctx, fields);
  310. for (i = 0; i < n; i++)
  311. {
  312. pdf_obj *field = pdf_array_get(ctx, fields, i);
  313. if (pdf_is_string(ctx, field))
  314. field = pdf_lookup_field(ctx, form, pdf_to_str_buf(ctx, field));
  315. if (field)
  316. add_field_hierarchy_to_array(ctx, result, field, fields, exclude);
  317. }
  318. }
  319. fz_catch(ctx)
  320. {
  321. pdf_drop_obj(ctx, result);
  322. fz_rethrow(ctx);
  323. }
  324. return result;
  325. }
  326. void pdf_reset_form(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
  327. {
  328. pdf_obj *sfields = specified_fields(ctx, doc, fields, exclude);
  329. fz_try(ctx)
  330. {
  331. int i, n = pdf_array_len(ctx, sfields);
  332. for (i = 0; i < n; i++)
  333. reset_form_field(ctx, doc, pdf_array_get(ctx, sfields, i));
  334. doc->recalculate = 1;
  335. }
  336. fz_always(ctx)
  337. pdf_drop_obj(ctx, sfields);
  338. fz_catch(ctx)
  339. fz_rethrow(ctx);
  340. }
  341. typedef struct
  342. {
  343. pdf_obj *pageobj;
  344. pdf_obj *chk;
  345. } lookup_state;
  346. static void *find_widget_on_page(fz_context *ctx, fz_page *page_, void *state_)
  347. {
  348. lookup_state *state = (lookup_state *) state_;
  349. pdf_page *page = (pdf_page *) page_;
  350. pdf_annot *widget;
  351. if (state->pageobj && pdf_objcmp_resolve(ctx, state->pageobj, page->obj))
  352. return NULL;
  353. for (widget = pdf_first_widget(ctx, page); widget != NULL; widget = pdf_next_widget(ctx, widget))
  354. {
  355. if (!pdf_objcmp_resolve(ctx, state->chk, widget->obj))
  356. return widget;
  357. }
  358. return NULL;
  359. }
  360. static pdf_annot *find_widget(fz_context *ctx, pdf_document *doc, pdf_obj *chk)
  361. {
  362. lookup_state state;
  363. state.pageobj = pdf_dict_get(ctx, chk, PDF_NAME(P));
  364. state.chk = chk;
  365. return fz_process_opened_pages(ctx, (fz_document *) doc, find_widget_on_page, &state);
  366. }
  367. static void set_check(fz_context *ctx, pdf_document *doc, pdf_obj *chk, pdf_obj *name)
  368. {
  369. pdf_obj *n = pdf_dict_getp(ctx, chk, "AP/N");
  370. pdf_obj *val;
  371. /* If name is a possible value of this check
  372. * box then use it, otherwise use "Off" */
  373. if (pdf_dict_get(ctx, n, name))
  374. val = name;
  375. else
  376. val = PDF_NAME(Off);
  377. if (pdf_name_eq(ctx, pdf_dict_get(ctx, chk, PDF_NAME(AS)), val))
  378. return;
  379. pdf_dict_put(ctx, chk, PDF_NAME(AS), val);
  380. pdf_set_annot_has_changed(ctx, find_widget(ctx, doc, chk));
  381. }
  382. /* Set the values of all fields in a group defined by a node
  383. * in the hierarchy */
  384. static void set_check_grp(fz_context *ctx, pdf_document *doc, pdf_obj *grp, pdf_obj *val)
  385. {
  386. pdf_obj *kids = pdf_dict_get(ctx, grp, PDF_NAME(Kids));
  387. if (kids == NULL)
  388. {
  389. set_check(ctx, doc, grp, val);
  390. }
  391. else
  392. {
  393. int i, n = pdf_array_len(ctx, kids);
  394. for (i = 0; i < n; i++)
  395. set_check_grp(ctx, doc, pdf_array_get(ctx, kids, i), val);
  396. }
  397. }
  398. void pdf_calculate_form(fz_context *ctx, pdf_document *doc)
  399. {
  400. if (doc->js)
  401. {
  402. fz_try(ctx)
  403. {
  404. pdf_obj *co = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/CO");
  405. int i, n = pdf_array_len(ctx, co);
  406. for (i = 0; i < n; i++)
  407. {
  408. pdf_obj *field = pdf_array_get(ctx, co, i);
  409. pdf_field_event_calculate(ctx, doc, field);
  410. }
  411. }
  412. fz_always(ctx)
  413. doc->recalculate = 0;
  414. fz_catch(ctx)
  415. fz_rethrow(ctx);
  416. }
  417. }
  418. static pdf_obj *find_on_state(fz_context *ctx, pdf_obj *dict)
  419. {
  420. int i, n = pdf_dict_len(ctx, dict);
  421. for (i = 0; i < n; ++i)
  422. {
  423. pdf_obj *key = pdf_dict_get_key(ctx, dict, i);
  424. if (key != PDF_NAME(Off))
  425. return key;
  426. }
  427. return NULL;
  428. }
  429. pdf_obj *pdf_button_field_on_state(fz_context *ctx, pdf_obj *field)
  430. {
  431. pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
  432. pdf_obj *on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(N)));
  433. if (!on) on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(D)));
  434. if (!on) on = PDF_NAME(Yes);
  435. return on;
  436. }
  437. static void
  438. begin_annot_op(fz_context *ctx, pdf_annot *annot, const char *op)
  439. {
  440. if (!annot->page)
  441. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
  442. pdf_begin_operation(ctx, annot->page->doc, op);
  443. }
  444. static void
  445. end_annot_op(fz_context *ctx, pdf_annot *annot)
  446. {
  447. pdf_end_operation(ctx, annot->page->doc);
  448. }
  449. static void
  450. abandon_annot_op(fz_context *ctx, pdf_annot *annot)
  451. {
  452. pdf_abandon_operation(ctx, annot->page->doc);
  453. }
  454. static void toggle_check_box(fz_context *ctx, pdf_annot *annot)
  455. {
  456. pdf_document *doc = annot->page->doc;
  457. begin_annot_op(ctx, annot, "Toggle checkbox");
  458. fz_try(ctx)
  459. {
  460. pdf_obj *field = annot->obj;
  461. int ff = pdf_field_flags(ctx, field);
  462. int is_radio = (ff & PDF_BTN_FIELD_IS_RADIO);
  463. int is_no_toggle_to_off = (ff & PDF_BTN_FIELD_IS_NO_TOGGLE_TO_OFF);
  464. pdf_obj *grp, *as, *val;
  465. grp = find_head_of_field_group(ctx, field);
  466. if (!grp)
  467. grp = field;
  468. /* TODO: check V value as well as or instead of AS? */
  469. as = pdf_dict_get(ctx, field, PDF_NAME(AS));
  470. if (as && as != PDF_NAME(Off))
  471. {
  472. if (is_radio && is_no_toggle_to_off)
  473. {
  474. end_annot_op(ctx, annot);
  475. break;
  476. }
  477. val = PDF_NAME(Off);
  478. }
  479. else
  480. {
  481. val = pdf_button_field_on_state(ctx, field);
  482. }
  483. pdf_dict_put(ctx, grp, PDF_NAME(V), val);
  484. set_check_grp(ctx, doc, grp, val);
  485. doc->recalculate = 1;
  486. end_annot_op(ctx, annot);
  487. }
  488. fz_catch(ctx)
  489. {
  490. abandon_annot_op(ctx, annot);
  491. fz_rethrow(ctx);
  492. }
  493. pdf_set_annot_has_changed(ctx, annot);
  494. }
  495. int pdf_has_unsaved_changes(fz_context *ctx, pdf_document *doc)
  496. {
  497. int i;
  498. if (doc->num_incremental_sections == 0)
  499. return 0;
  500. for (i = 0; i < doc->xref_sections->num_objects; i++)
  501. if (doc->xref_sections->subsec->table[i].type != 0)
  502. break;
  503. return i != doc->xref_sections->num_objects;
  504. }
  505. int pdf_was_repaired(fz_context *ctx, pdf_document *doc)
  506. {
  507. return doc->repair_attempted;
  508. }
  509. int pdf_toggle_widget(fz_context *ctx, pdf_annot *widget)
  510. {
  511. switch (pdf_widget_type(ctx, widget))
  512. {
  513. default:
  514. return 0;
  515. case PDF_WIDGET_TYPE_CHECKBOX:
  516. case PDF_WIDGET_TYPE_RADIOBUTTON:
  517. toggle_check_box(ctx, widget);
  518. return 1;
  519. }
  520. }
  521. int
  522. pdf_update_page(fz_context *ctx, pdf_page *page)
  523. {
  524. pdf_annot *annot;
  525. pdf_annot *widget;
  526. int changed = 0;
  527. fz_try(ctx)
  528. {
  529. pdf_begin_implicit_operation(ctx, page->doc);
  530. if (page->doc->recalculate)
  531. pdf_calculate_form(ctx, page->doc);
  532. for (annot = page->annots; annot; annot = annot->next)
  533. if (pdf_update_annot(ctx, annot))
  534. changed = 1;
  535. for (widget = page->widgets; widget; widget = widget->next)
  536. if (pdf_update_annot(ctx, widget))
  537. changed = 1;
  538. pdf_end_operation(ctx, page->doc);
  539. }
  540. fz_catch(ctx)
  541. {
  542. pdf_abandon_operation(ctx, page->doc);
  543. fz_rethrow(ctx);
  544. }
  545. return changed;
  546. }
  547. pdf_annot *pdf_first_widget(fz_context *ctx, pdf_page *page)
  548. {
  549. return page->widgets;
  550. }
  551. pdf_annot *pdf_next_widget(fz_context *ctx, pdf_annot *widget)
  552. {
  553. return widget->next;
  554. }
  555. enum pdf_widget_type pdf_widget_type(fz_context *ctx, pdf_annot *widget)
  556. {
  557. enum pdf_widget_type ret = PDF_WIDGET_TYPE_BUTTON;
  558. pdf_annot_push_local_xref(ctx, widget);
  559. fz_try(ctx)
  560. {
  561. pdf_obj *subtype = pdf_dict_get(ctx, widget->obj, PDF_NAME(Subtype));
  562. if (pdf_name_eq(ctx, subtype, PDF_NAME(Widget)))
  563. ret = pdf_field_type(ctx, widget->obj);
  564. }
  565. fz_always(ctx)
  566. pdf_annot_pop_local_xref(ctx, widget);
  567. fz_catch(ctx)
  568. fz_rethrow(ctx);
  569. return ret;
  570. }
  571. static int set_validated_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
  572. {
  573. char *newtext = NULL;
  574. if (!ignore_trigger_events)
  575. {
  576. if (!pdf_field_event_validate(ctx, doc, field, text, &newtext))
  577. return 0;
  578. }
  579. update_field_value(ctx, doc, field, newtext ? newtext : text);
  580. fz_free(ctx, newtext);
  581. return 1;
  582. }
  583. static void update_checkbox_selector(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
  584. {
  585. pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
  586. if (kids)
  587. {
  588. int i, n = pdf_array_len(ctx, kids);
  589. for (i = 0; i < n; i++)
  590. update_checkbox_selector(ctx, doc, pdf_array_get(ctx, kids, i), val);
  591. }
  592. else
  593. {
  594. pdf_obj *n = pdf_dict_getp(ctx, field, "AP/N");
  595. pdf_obj *oval;
  596. if (pdf_dict_gets(ctx, n, val))
  597. oval = pdf_new_name(ctx, val);
  598. else
  599. oval = PDF_NAME(Off);
  600. pdf_dict_put_drop(ctx, field, PDF_NAME(AS), oval);
  601. }
  602. }
  603. static int set_checkbox_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
  604. {
  605. update_checkbox_selector(ctx, doc, field, val);
  606. update_field_value(ctx, doc, field, val);
  607. return 1;
  608. }
  609. int pdf_set_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
  610. {
  611. int accepted = 0;
  612. switch (pdf_field_type(ctx, field))
  613. {
  614. case PDF_WIDGET_TYPE_TEXT:
  615. case PDF_WIDGET_TYPE_COMBOBOX:
  616. case PDF_WIDGET_TYPE_LISTBOX:
  617. accepted = set_validated_field_value(ctx, doc, field, text, ignore_trigger_events);
  618. break;
  619. case PDF_WIDGET_TYPE_CHECKBOX:
  620. case PDF_WIDGET_TYPE_RADIOBUTTON:
  621. accepted = set_checkbox_value(ctx, doc, field, text);
  622. break;
  623. default:
  624. update_field_value(ctx, doc, field, text);
  625. accepted = 1;
  626. break;
  627. }
  628. if (!ignore_trigger_events)
  629. doc->recalculate = 1;
  630. return accepted;
  631. }
  632. char *pdf_field_border_style(fz_context *ctx, pdf_obj *field)
  633. {
  634. const char *bs = pdf_to_name(ctx, pdf_dict_getl(ctx, field, PDF_NAME(BS), PDF_NAME(S), NULL));
  635. switch (*bs)
  636. {
  637. case 'S': return "Solid";
  638. case 'D': return "Dashed";
  639. case 'B': return "Beveled";
  640. case 'I': return "Inset";
  641. case 'U': return "Underline";
  642. }
  643. return "Solid";
  644. }
  645. void pdf_field_set_border_style(fz_context *ctx, pdf_obj *field, const char *text)
  646. {
  647. pdf_obj *val;
  648. if (!strcmp(text, "Solid"))
  649. val = PDF_NAME(S);
  650. else if (!strcmp(text, "Dashed"))
  651. val = PDF_NAME(D);
  652. else if (!strcmp(text, "Beveled"))
  653. val = PDF_NAME(B);
  654. else if (!strcmp(text, "Inset"))
  655. val = PDF_NAME(I);
  656. else if (!strcmp(text, "Underline"))
  657. val = PDF_NAME(U);
  658. else
  659. return;
  660. pdf_dict_putl_drop(ctx, field, val, PDF_NAME(BS), PDF_NAME(S), NULL);
  661. pdf_field_mark_dirty(ctx, field);
  662. }
  663. void pdf_field_set_button_caption(fz_context *ctx, pdf_obj *field, const char *text)
  664. {
  665. if (pdf_field_type(ctx, field) == PDF_WIDGET_TYPE_BUTTON)
  666. {
  667. pdf_obj *val = pdf_new_text_string(ctx, text);
  668. pdf_dict_putl_drop(ctx, field, val, PDF_NAME(MK), PDF_NAME(CA), NULL);
  669. pdf_field_mark_dirty(ctx, field);
  670. }
  671. }
  672. int pdf_field_display(fz_context *ctx, pdf_obj *field)
  673. {
  674. pdf_obj *kids;
  675. int f, res = Display_Visible;
  676. /* Base response on first of children. Not ideal,
  677. * but not clear how to handle children with
  678. * differing values */
  679. while ((kids = pdf_dict_get(ctx, field, PDF_NAME(Kids))) != NULL)
  680. field = pdf_array_get(ctx, kids, 0);
  681. f = pdf_dict_get_int(ctx, field, PDF_NAME(F));
  682. if (f & PDF_ANNOT_IS_HIDDEN)
  683. {
  684. res = Display_Hidden;
  685. }
  686. else if (f & PDF_ANNOT_IS_PRINT)
  687. {
  688. if (f & PDF_ANNOT_IS_NO_VIEW)
  689. res = Display_NoView;
  690. }
  691. else
  692. {
  693. if (f & PDF_ANNOT_IS_NO_VIEW)
  694. res = Display_Hidden;
  695. else
  696. res = Display_NoPrint;
  697. }
  698. return res;
  699. }
  700. /*
  701. * get the field name in a char buffer that has spare room to
  702. * add more characters at the end.
  703. */
  704. static char *load_field_name(fz_context *ctx, pdf_obj *field, int spare, pdf_cycle_list *cycle_up)
  705. {
  706. pdf_cycle_list cycle;
  707. char *res = NULL;
  708. pdf_obj *parent;
  709. const char *lname;
  710. int llen;
  711. if (pdf_cycle(ctx, &cycle, cycle_up, field))
  712. fz_throw(ctx, FZ_ERROR_FORMAT, "Cycle in field parents");
  713. parent = pdf_dict_get(ctx, field, PDF_NAME(Parent));
  714. lname = pdf_dict_get_text_string(ctx, field, PDF_NAME(T));
  715. llen = (int)strlen(lname);
  716. // Limit fields to 16K
  717. if (llen > (16 << 10) || llen + spare > (16 << 10))
  718. fz_throw(ctx, FZ_ERROR_LIMIT, "Field name too long");
  719. /*
  720. * If we found a name at this point in the field hierarchy
  721. * then we'll need extra space for it and a dot
  722. */
  723. if (llen)
  724. spare += llen+1;
  725. if (parent)
  726. {
  727. res = load_field_name(ctx, parent, spare, &cycle);
  728. }
  729. else
  730. {
  731. res = Memento_label(fz_malloc(ctx, spare+1), "form_field_name");
  732. res[0] = 0;
  733. }
  734. if (llen)
  735. {
  736. if (res[0])
  737. strcat(res, ".");
  738. strcat(res, lname);
  739. }
  740. return res;
  741. }
  742. char *pdf_load_field_name(fz_context *ctx, pdf_obj *field)
  743. {
  744. return load_field_name(ctx, field, 0, NULL);
  745. }
  746. void pdf_create_field_name(fz_context *ctx, pdf_document *doc, const char *prefix, char *buf, size_t len)
  747. {
  748. pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc),
  749. PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
  750. int i;
  751. for (i = 0; i < 65536; ++i) {
  752. fz_snprintf(buf, len, "%s%d", prefix, i);
  753. if (!pdf_lookup_field(ctx, form, buf))
  754. return;
  755. }
  756. fz_throw(ctx, FZ_ERROR_LIMIT, "Could not create unique field name.");
  757. }
  758. const char *pdf_field_label(fz_context *ctx, pdf_obj *field)
  759. {
  760. pdf_obj *label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(TU));
  761. if (!label)
  762. label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(T));
  763. if (label)
  764. return pdf_to_text_string(ctx, label);
  765. return "Unnamed";
  766. }
  767. void pdf_field_set_display(fz_context *ctx, pdf_obj *field, int d)
  768. {
  769. pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
  770. if (!kids)
  771. {
  772. int mask = (PDF_ANNOT_IS_HIDDEN|PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
  773. int f = pdf_dict_get_int(ctx, field, PDF_NAME(F)) & ~mask;
  774. switch (d)
  775. {
  776. case Display_Visible:
  777. f |= PDF_ANNOT_IS_PRINT;
  778. break;
  779. case Display_Hidden:
  780. f |= PDF_ANNOT_IS_HIDDEN;
  781. break;
  782. case Display_NoView:
  783. f |= (PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
  784. break;
  785. case Display_NoPrint:
  786. break;
  787. }
  788. pdf_dict_put_int(ctx, field, PDF_NAME(F), f);
  789. }
  790. else
  791. {
  792. int i, n = pdf_array_len(ctx, kids);
  793. for (i = 0; i < n; i++)
  794. pdf_field_set_display(ctx, pdf_array_get(ctx, kids, i), d);
  795. }
  796. }
  797. void pdf_field_set_fill_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
  798. {
  799. /* col == NULL mean transparent, but we can simply pass it on as with
  800. * non-NULL values because pdf_dict_putp interprets a NULL value as
  801. * delete */
  802. pdf_dict_putl(ctx, field, col, PDF_NAME(MK), PDF_NAME(BG), NULL);
  803. pdf_field_mark_dirty(ctx, field);
  804. }
  805. void pdf_field_set_text_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
  806. {
  807. char buf[100];
  808. const char *font;
  809. float size, color[4];
  810. /* TODO? */
  811. const char *da = pdf_to_str_buf(ctx, pdf_dict_get_inheritable(ctx, field, PDF_NAME(DA)));
  812. int n;
  813. pdf_parse_default_appearance(ctx, da, &font, &size, &n, color);
  814. switch (pdf_array_len(ctx, col))
  815. {
  816. default:
  817. n = 0;
  818. color[0] = color[1] = color[2] = color[3] = 0;
  819. break;
  820. case 1:
  821. n = 1;
  822. color[0] = pdf_array_get_real(ctx, col, 0);
  823. break;
  824. case 3:
  825. n = 3;
  826. color[0] = pdf_array_get_real(ctx, col, 0);
  827. color[1] = pdf_array_get_real(ctx, col, 1);
  828. color[2] = pdf_array_get_real(ctx, col, 2);
  829. break;
  830. case 4:
  831. n = 4;
  832. color[0] = pdf_array_get_real(ctx, col, 0);
  833. color[1] = pdf_array_get_real(ctx, col, 1);
  834. color[2] = pdf_array_get_real(ctx, col, 2);
  835. color[3] = pdf_array_get_real(ctx, col, 3);
  836. break;
  837. }
  838. pdf_print_default_appearance(ctx, buf, sizeof buf, font, size, n, color);
  839. pdf_dict_put_string(ctx, field, PDF_NAME(DA), buf, strlen(buf));
  840. pdf_field_mark_dirty(ctx, field);
  841. }
  842. pdf_annot *
  843. pdf_keep_widget(fz_context *ctx, pdf_annot *widget)
  844. {
  845. return pdf_keep_annot(ctx, widget);
  846. }
  847. void
  848. pdf_drop_widget(fz_context *ctx, pdf_annot *widget)
  849. {
  850. pdf_drop_annot(ctx, widget);
  851. }
  852. void
  853. pdf_drop_widgets(fz_context *ctx, pdf_annot *widget)
  854. {
  855. while (widget)
  856. {
  857. pdf_annot *next = widget->next;
  858. pdf_drop_widget(ctx, widget);
  859. widget = next;
  860. }
  861. }
  862. pdf_annot *
  863. pdf_create_signature_widget(fz_context *ctx, pdf_page *page, char *name)
  864. {
  865. fz_rect rect = { 12, 12, 12+100, 12+50 };
  866. pdf_annot *annot;
  867. pdf_begin_operation(ctx, page->doc, "Create signature");
  868. annot = pdf_create_annot_raw(ctx, page, PDF_ANNOT_WIDGET);
  869. fz_try(ctx)
  870. {
  871. pdf_obj *obj = annot->obj;
  872. pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, page->doc), PDF_NAME(Root));
  873. pdf_obj *acroform = pdf_dict_get(ctx, root, PDF_NAME(AcroForm));
  874. pdf_obj *fields, *lock;
  875. if (!acroform)
  876. {
  877. acroform = pdf_new_dict(ctx, page->doc, 1);
  878. pdf_dict_put_drop(ctx, root, PDF_NAME(AcroForm), acroform);
  879. }
  880. fields = pdf_dict_get(ctx, acroform, PDF_NAME(Fields));
  881. if (!fields)
  882. {
  883. fields = pdf_new_array(ctx, page->doc, 1);
  884. pdf_dict_put_drop(ctx, acroform, PDF_NAME(Fields), fields);
  885. }
  886. pdf_set_annot_rect(ctx, annot, rect);
  887. pdf_dict_put(ctx, obj, PDF_NAME(FT), PDF_NAME(Sig));
  888. pdf_dict_put_int(ctx, obj, PDF_NAME(F), PDF_ANNOT_IS_PRINT);
  889. pdf_dict_put_text_string(ctx, obj, PDF_NAME(DA), "/Helv 0 Tf 0 g");
  890. pdf_dict_put_text_string(ctx, obj, PDF_NAME(T), name);
  891. pdf_array_push(ctx, fields, obj);
  892. lock = pdf_dict_put_dict(ctx, obj, PDF_NAME(Lock), 1);
  893. pdf_dict_put(ctx, lock, PDF_NAME(Action), PDF_NAME(All));
  894. pdf_end_operation(ctx, page->doc);
  895. }
  896. fz_catch(ctx)
  897. {
  898. pdf_abandon_operation(ctx, page->doc);
  899. pdf_delete_annot(ctx, page, annot);
  900. }
  901. return (pdf_annot *)annot;
  902. }
  903. fz_rect
  904. pdf_bound_widget(fz_context *ctx, pdf_annot *widget)
  905. {
  906. return pdf_bound_annot(ctx, widget);
  907. }
  908. int
  909. pdf_update_widget(fz_context *ctx, pdf_annot *widget)
  910. {
  911. return pdf_update_annot(ctx, widget);
  912. }
  913. int pdf_text_widget_max_len(fz_context *ctx, pdf_annot *tw)
  914. {
  915. pdf_annot *annot = (pdf_annot *)tw;
  916. return pdf_dict_get_inheritable_int(ctx, annot->obj, PDF_NAME(MaxLen));
  917. }
  918. int pdf_text_widget_format(fz_context *ctx, pdf_annot *tw)
  919. {
  920. pdf_annot *annot = (pdf_annot *)tw;
  921. int type = PDF_WIDGET_TX_FORMAT_NONE;
  922. pdf_obj *js = pdf_dict_getl(ctx, annot->obj, PDF_NAME(AA), PDF_NAME(F), PDF_NAME(JS), NULL);
  923. if (js)
  924. {
  925. char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
  926. if (strstr(code, "AFNumber_Format"))
  927. type = PDF_WIDGET_TX_FORMAT_NUMBER;
  928. else if (strstr(code, "AFSpecial_Format"))
  929. type = PDF_WIDGET_TX_FORMAT_SPECIAL;
  930. else if (strstr(code, "AFDate_FormatEx"))
  931. type = PDF_WIDGET_TX_FORMAT_DATE;
  932. else if (strstr(code, "AFTime_FormatEx"))
  933. type = PDF_WIDGET_TX_FORMAT_TIME;
  934. fz_free(ctx, code);
  935. }
  936. return type;
  937. }
  938. static char *
  939. merge_changes(fz_context *ctx, const char *value, int start, int end, const char *change)
  940. {
  941. int changelen = change ? (int)strlen(change) : 0;
  942. int valuelen = value ? (int)strlen(value) : 0;
  943. int prelen = (start >= 0 ? (start < valuelen ? start : valuelen) : 0);
  944. int postlen = (end >= 0 && end <= valuelen ? valuelen - end : 0);
  945. int newlen = prelen + changelen + postlen + 1;
  946. char *merged = fz_malloc(ctx, newlen);
  947. char *m = merged;
  948. if (prelen)
  949. {
  950. memcpy(m, value, prelen);
  951. m += prelen;
  952. }
  953. if (changelen)
  954. {
  955. memcpy(m, change, changelen);
  956. m += changelen;
  957. }
  958. if (postlen)
  959. {
  960. memcpy(m, &value[end], postlen);
  961. m += postlen;
  962. }
  963. *m = 0;
  964. return merged;
  965. }
  966. int pdf_set_text_field_value(fz_context *ctx, pdf_annot *widget, const char *update)
  967. {
  968. pdf_document *doc;
  969. pdf_keystroke_event evt = { 0 };
  970. char *new_change = NULL;
  971. char *new_value = NULL;
  972. char *merged_value = NULL;
  973. int rc = 1;
  974. begin_annot_op(ctx, widget, "Edit text field");
  975. doc = widget->page->doc;
  976. fz_var(new_value);
  977. fz_var(new_change);
  978. fz_var(merged_value);
  979. fz_try(ctx)
  980. {
  981. if (!widget->ignore_trigger_events)
  982. {
  983. evt.value = pdf_annot_field_value(ctx, widget);
  984. evt.change = update;
  985. evt.selStart = 0;
  986. evt.selEnd = (int)strlen(evt.value);
  987. evt.willCommit = 0;
  988. rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
  989. new_change = evt.newChange;
  990. new_value = evt.newValue;
  991. evt.newValue = NULL;
  992. evt.newChange = NULL;
  993. if (rc)
  994. {
  995. merged_value = merge_changes(ctx, new_value, evt.selStart, evt.selEnd, new_change);
  996. evt.value = merged_value;
  997. evt.change = "";
  998. evt.selStart = -1;
  999. evt.selEnd = -1;
  1000. evt.willCommit = 1;
  1001. rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
  1002. if (rc)
  1003. rc = pdf_set_annot_field_value(ctx, doc, widget, evt.newValue, 0);
  1004. }
  1005. }
  1006. else
  1007. {
  1008. rc = pdf_set_annot_field_value(ctx, doc, widget, update, 1);
  1009. }
  1010. end_annot_op(ctx, widget);
  1011. }
  1012. fz_always(ctx)
  1013. {
  1014. fz_free(ctx, new_value);
  1015. fz_free(ctx, evt.newValue);
  1016. fz_free(ctx, new_change);
  1017. fz_free(ctx, evt.newChange);
  1018. fz_free(ctx, merged_value);
  1019. }
  1020. fz_catch(ctx)
  1021. {
  1022. abandon_annot_op(ctx, widget);
  1023. fz_warn(ctx, "could not set widget text");
  1024. rc = 0;
  1025. }
  1026. return rc;
  1027. }
  1028. int pdf_edit_text_field_value(fz_context *ctx, pdf_annot *widget, const char *value, const char *change, int *selStart, int *selEnd, char **result)
  1029. {
  1030. pdf_document *doc = widget->page->doc;
  1031. pdf_keystroke_event evt = {0};
  1032. int rc = 1;
  1033. pdf_begin_operation(ctx, doc, "Text field keystroke");
  1034. fz_try(ctx)
  1035. {
  1036. if (!widget->ignore_trigger_events)
  1037. {
  1038. evt.value = value;
  1039. evt.change = change;
  1040. evt.selStart = *selStart;
  1041. evt.selEnd = *selEnd;
  1042. evt.willCommit = 0;
  1043. rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
  1044. if (rc)
  1045. {
  1046. *result = merge_changes(ctx, evt.newValue, evt.selStart, evt.selEnd, evt.newChange);
  1047. *selStart = evt.selStart + (int)strlen(evt.newChange);
  1048. *selEnd = *selStart;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. *result = merge_changes(ctx, value, *selStart, *selEnd, change);
  1054. *selStart = evt.selStart + (int)strlen(change);
  1055. *selEnd = *selStart;
  1056. }
  1057. pdf_end_operation(ctx, doc);
  1058. }
  1059. fz_always(ctx)
  1060. {
  1061. fz_free(ctx, evt.newValue);
  1062. fz_free(ctx, evt.newChange);
  1063. }
  1064. fz_catch(ctx)
  1065. {
  1066. pdf_abandon_operation(ctx, doc);
  1067. fz_warn(ctx, "could not process text widget keystroke");
  1068. rc = 0;
  1069. }
  1070. return rc;
  1071. }
  1072. int pdf_set_choice_field_value(fz_context *ctx, pdf_annot *widget, const char *new_value)
  1073. {
  1074. /* Choice widgets use almost the same keystroke processing as text fields. */
  1075. return pdf_set_text_field_value(ctx, widget, new_value);
  1076. }
  1077. int pdf_choice_widget_options(fz_context *ctx, pdf_annot *tw, int exportval, const char *opts[])
  1078. {
  1079. pdf_annot *annot = (pdf_annot *)tw;
  1080. pdf_obj *optarr;
  1081. int i, n, m;
  1082. optarr = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(Opt));
  1083. n = pdf_array_len(ctx, optarr);
  1084. if (opts)
  1085. {
  1086. for (i = 0; i < n; i++)
  1087. {
  1088. m = pdf_array_len(ctx, pdf_array_get(ctx, optarr, i));
  1089. /* If it is a two element array, the second item is the one that we want if we want the listing value. */
  1090. if (m == 2)
  1091. if (exportval)
  1092. opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 0);
  1093. else
  1094. opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 1);
  1095. else
  1096. opts[i] = pdf_array_get_text_string(ctx, optarr, i);
  1097. }
  1098. }
  1099. return n;
  1100. }
  1101. int pdf_choice_field_option_count(fz_context *ctx, pdf_obj *field)
  1102. {
  1103. pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
  1104. return pdf_array_len(ctx, opt);
  1105. }
  1106. const char *pdf_choice_field_option(fz_context *ctx, pdf_obj *field, int export, int i)
  1107. {
  1108. pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
  1109. pdf_obj *ent = pdf_array_get(ctx, opt, i);
  1110. if (pdf_array_len(ctx, ent) == 2)
  1111. return pdf_array_get_text_string(ctx, ent, export ? 0 : 1);
  1112. else
  1113. return pdf_to_text_string(ctx, ent);
  1114. }
  1115. int pdf_choice_widget_is_multiselect(fz_context *ctx, pdf_annot *tw)
  1116. {
  1117. pdf_annot *annot = (pdf_annot *)tw;
  1118. if (!annot) return 0;
  1119. switch (pdf_field_type(ctx, annot->obj))
  1120. {
  1121. case PDF_WIDGET_TYPE_LISTBOX:
  1122. return (pdf_field_flags(ctx, annot->obj) & PDF_CH_FIELD_IS_MULTI_SELECT) != 0;
  1123. default:
  1124. return 0;
  1125. }
  1126. }
  1127. int pdf_choice_widget_value(fz_context *ctx, pdf_annot *tw, const char *opts[])
  1128. {
  1129. pdf_annot *annot = (pdf_annot *)tw;
  1130. pdf_obj *optarr;
  1131. int i, n;
  1132. if (!annot)
  1133. return 0;
  1134. optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(V));
  1135. if (pdf_is_string(ctx, optarr))
  1136. {
  1137. if (opts)
  1138. opts[0] = pdf_to_text_string(ctx, optarr);
  1139. return 1;
  1140. }
  1141. else
  1142. {
  1143. n = pdf_array_len(ctx, optarr);
  1144. if (opts)
  1145. {
  1146. for (i = 0; i < n; i++)
  1147. {
  1148. pdf_obj *elem = pdf_array_get(ctx, optarr, i);
  1149. if (pdf_is_array(ctx, elem))
  1150. elem = pdf_array_get(ctx, elem, 1);
  1151. opts[i] = pdf_to_text_string(ctx, elem);
  1152. }
  1153. }
  1154. return n;
  1155. }
  1156. }
  1157. void pdf_choice_widget_set_value(fz_context *ctx, pdf_annot *tw, int n, const char *opts[])
  1158. {
  1159. pdf_annot *annot = (pdf_annot *)tw;
  1160. pdf_obj *optarr = NULL;
  1161. int i;
  1162. if (!annot)
  1163. return;
  1164. begin_annot_op(ctx, annot, "Set choice");
  1165. fz_var(optarr);
  1166. fz_try(ctx)
  1167. {
  1168. if (n != 1)
  1169. {
  1170. optarr = pdf_new_array(ctx, annot->page->doc, n);
  1171. for (i = 0; i < n; i++)
  1172. pdf_array_push_text_string(ctx, optarr, opts[i]);
  1173. pdf_dict_put_drop(ctx, annot->obj, PDF_NAME(V), optarr);
  1174. }
  1175. else
  1176. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(V), opts[0]);
  1177. /* FIXME: when n > 1, we should be regenerating the indexes */
  1178. pdf_dict_del(ctx, annot->obj, PDF_NAME(I));
  1179. pdf_field_mark_dirty(ctx, annot->obj);
  1180. end_annot_op(ctx, annot);
  1181. }
  1182. fz_catch(ctx)
  1183. {
  1184. abandon_annot_op(ctx, annot);
  1185. pdf_drop_obj(ctx, optarr);
  1186. fz_rethrow(ctx);
  1187. }
  1188. }
  1189. int pdf_signature_byte_range(fz_context *ctx, pdf_document *doc, pdf_obj *signature, fz_range *byte_range)
  1190. {
  1191. pdf_obj *br = pdf_dict_getl(ctx, signature, PDF_NAME(V), PDF_NAME(ByteRange), NULL);
  1192. int i, n = pdf_array_len(ctx, br)/2;
  1193. if (byte_range)
  1194. {
  1195. for (i = 0; i < n; i++)
  1196. {
  1197. int64_t offset = pdf_array_get_int(ctx, br, 2*i);
  1198. int length = pdf_array_get_int(ctx, br, 2*i+1);
  1199. if (offset < 0 || offset > doc->file_size)
  1200. fz_throw(ctx, FZ_ERROR_FORMAT, "offset of signature byte range outside of file");
  1201. else if (length < 0)
  1202. fz_throw(ctx, FZ_ERROR_FORMAT, "length of signature byte range negative");
  1203. else if (offset + length > doc->file_size)
  1204. fz_throw(ctx, FZ_ERROR_FORMAT, "signature byte range extends past end of file");
  1205. byte_range[i].offset = offset;
  1206. byte_range[i].length = length;
  1207. }
  1208. }
  1209. return n;
  1210. }
  1211. static int is_white(int c)
  1212. {
  1213. return c == '\x00' || c == '\x09' || c == '\x0a' || c == '\x0c' || c == '\x0d' || c == '\x20';
  1214. }
  1215. static int is_hex_or_white(int c)
  1216. {
  1217. return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9') || is_white(c);
  1218. }
  1219. static void validate_certificate_data(fz_context *ctx, pdf_document *doc, fz_range *hole)
  1220. {
  1221. fz_stream *stm;
  1222. int c;
  1223. stm = fz_open_range_filter(ctx, doc->file, hole, 1);
  1224. fz_try(ctx)
  1225. {
  1226. while (is_white((c = fz_read_byte(ctx, stm))))
  1227. ;
  1228. if (c == '<')
  1229. c = fz_read_byte(ctx, stm);
  1230. while (is_hex_or_white(c))
  1231. c = fz_read_byte(ctx, stm);
  1232. if (c == '>')
  1233. c = fz_read_byte(ctx, stm);
  1234. while (is_white(c))
  1235. c = fz_read_byte(ctx, stm);
  1236. if (c != EOF)
  1237. fz_throw(ctx, FZ_ERROR_FORMAT, "signature certificate data contains invalid character");
  1238. if ((size_t)fz_tell(ctx, stm) != hole->length)
  1239. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of signature certificate data");
  1240. }
  1241. fz_always(ctx)
  1242. fz_drop_stream(ctx, stm);
  1243. fz_catch(ctx)
  1244. fz_rethrow(ctx);
  1245. }
  1246. static int rangecmp(const void *a_, const void *b_)
  1247. {
  1248. const fz_range *a = (const fz_range *) a_;
  1249. const fz_range *b = (const fz_range *) b_;
  1250. return (int) (a->offset - b->offset);
  1251. }
  1252. static void validate_byte_ranges(fz_context *ctx, pdf_document *doc, fz_range *unsorted, int nranges)
  1253. {
  1254. int64_t offset = 0;
  1255. fz_range *sorted;
  1256. int i;
  1257. sorted = fz_calloc(ctx, nranges, sizeof(*sorted));
  1258. memcpy(sorted, unsorted, nranges * sizeof(*sorted));
  1259. qsort(sorted, nranges, sizeof(*sorted), rangecmp);
  1260. fz_try(ctx)
  1261. {
  1262. offset = 0;
  1263. for (i = 0; i < nranges; i++)
  1264. {
  1265. if (sorted[i].offset > offset)
  1266. {
  1267. fz_range hole;
  1268. hole.offset = offset;
  1269. hole.length = sorted[i].offset - offset;
  1270. validate_certificate_data(ctx, doc, &hole);
  1271. }
  1272. offset = fz_maxi64(offset, sorted[i].offset + sorted[i].length);
  1273. }
  1274. }
  1275. fz_always(ctx)
  1276. fz_free(ctx, sorted);
  1277. fz_catch(ctx)
  1278. fz_rethrow(ctx);
  1279. }
  1280. fz_stream *pdf_signature_hash_bytes(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
  1281. {
  1282. fz_range *byte_range = NULL;
  1283. int byte_range_len;
  1284. fz_stream *bytes = NULL;
  1285. fz_var(byte_range);
  1286. fz_try(ctx)
  1287. {
  1288. byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
  1289. if (byte_range_len)
  1290. {
  1291. byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
  1292. pdf_signature_byte_range(ctx, doc, signature, byte_range);
  1293. }
  1294. validate_byte_ranges(ctx, doc, byte_range, byte_range_len);
  1295. bytes = fz_open_range_filter(ctx, doc->file, byte_range, byte_range_len);
  1296. }
  1297. fz_always(ctx)
  1298. {
  1299. fz_free(ctx, byte_range);
  1300. }
  1301. fz_catch(ctx)
  1302. {
  1303. fz_rethrow(ctx);
  1304. }
  1305. return bytes;
  1306. }
  1307. int pdf_incremental_change_since_signing_widget(fz_context *ctx, pdf_annot *widget)
  1308. {
  1309. if (!widget->page)
  1310. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
  1311. return pdf_signature_incremental_change_since_signing(ctx, widget->page->doc, widget->obj);
  1312. }
  1313. int pdf_signature_incremental_change_since_signing(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
  1314. {
  1315. fz_range *byte_range = NULL;
  1316. int byte_range_len;
  1317. int changed = 0;
  1318. if (pdf_dict_get_inheritable(ctx, signature, PDF_NAME(FT)) != PDF_NAME(Sig))
  1319. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation is not a signature widget");
  1320. if (!pdf_signature_is_signed(ctx, doc, signature))
  1321. return 0;
  1322. fz_var(byte_range);
  1323. fz_try(ctx)
  1324. {
  1325. byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
  1326. if (byte_range_len)
  1327. {
  1328. fz_range *last_range;
  1329. int64_t end_of_range;
  1330. byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
  1331. pdf_signature_byte_range(ctx, doc, signature, byte_range);
  1332. last_range = &byte_range[byte_range_len -1];
  1333. end_of_range = last_range->offset + last_range->length;
  1334. /* We can see how long the document was when signed by inspecting the byte
  1335. * ranges of the signature. The document, when read in, may have already
  1336. * had changes tagged on to it, past its extent when signed, or we may have
  1337. * made changes since reading it, which will be held in a new incremental
  1338. * xref section. */
  1339. if (doc->file_size > end_of_range || doc->num_incremental_sections > 0)
  1340. changed = 1;
  1341. }
  1342. }
  1343. fz_always(ctx)
  1344. {
  1345. fz_free(ctx, byte_range);
  1346. }
  1347. fz_catch(ctx)
  1348. {
  1349. fz_rethrow(ctx);
  1350. }
  1351. return changed;
  1352. }
  1353. int pdf_signature_is_signed(fz_context *ctx, pdf_document *doc, pdf_obj *field)
  1354. {
  1355. pdf_obj *v;
  1356. pdf_obj* vtype;
  1357. if (pdf_dict_get_inheritable(ctx, field, PDF_NAME(FT)) != PDF_NAME(Sig))
  1358. return 0;
  1359. /* Signatures can only be signed if the value is a dictionary,
  1360. * and if the value has a Type, it should be Sig. */
  1361. v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
  1362. vtype = pdf_dict_get(ctx, v, PDF_NAME(Type));
  1363. return pdf_is_dict(ctx, v) && (vtype ? pdf_name_eq(ctx, vtype, PDF_NAME(Sig)) : 1);
  1364. }
  1365. int pdf_widget_is_signed(fz_context *ctx, pdf_annot *widget)
  1366. {
  1367. if (widget == NULL)
  1368. return 0;
  1369. if (!widget->page)
  1370. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
  1371. return pdf_signature_is_signed(ctx, widget->page->doc, widget->obj);
  1372. }
  1373. int pdf_widget_is_readonly(fz_context *ctx, pdf_annot *widget)
  1374. {
  1375. int fflags;
  1376. if (widget == NULL)
  1377. return 0;
  1378. fflags = pdf_field_flags(ctx, ((pdf_annot *) widget)->obj);
  1379. return fflags & PDF_FIELD_IS_READ_ONLY;
  1380. }
  1381. size_t pdf_signature_contents(fz_context *ctx, pdf_document *doc, pdf_obj *signature, char **contents)
  1382. {
  1383. pdf_obj *v_ref = pdf_dict_get_inheritable(ctx, signature, PDF_NAME(V));
  1384. pdf_obj *v_obj = pdf_load_unencrypted_object(ctx, doc, pdf_to_num(ctx, v_ref));
  1385. char *copy = NULL;
  1386. size_t len;
  1387. fz_var(copy);
  1388. fz_try(ctx)
  1389. {
  1390. pdf_obj *c = pdf_dict_get(ctx, v_obj, PDF_NAME(Contents));
  1391. char *s;
  1392. s = pdf_to_str_buf(ctx, c);
  1393. len = pdf_to_str_len(ctx, c);
  1394. if (contents)
  1395. {
  1396. copy = Memento_label(fz_malloc(ctx, len), "sig_contents");
  1397. memcpy(copy, s, len);
  1398. }
  1399. }
  1400. fz_always(ctx)
  1401. pdf_drop_obj(ctx, v_obj);
  1402. fz_catch(ctx)
  1403. {
  1404. fz_free(ctx, copy);
  1405. fz_rethrow(ctx);
  1406. }
  1407. if (contents)
  1408. *contents = copy;
  1409. return len;
  1410. }
  1411. static fz_xml_doc *load_xfa(fz_context *ctx, pdf_document *doc)
  1412. {
  1413. pdf_obj *xfa;
  1414. fz_buffer *buf = NULL;
  1415. fz_buffer *packet = NULL;
  1416. int i;
  1417. if (doc->xfa)
  1418. return doc->xfa; /* Already loaded, and present. */
  1419. xfa = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/XFA");
  1420. if (!pdf_is_array(ctx, xfa) && !pdf_is_stream(ctx, xfa))
  1421. return NULL; /* No XFA */
  1422. fz_var(buf);
  1423. fz_var(packet);
  1424. fz_try(ctx)
  1425. {
  1426. if (pdf_is_stream(ctx, xfa))
  1427. {
  1428. /* Load entire XFA resource */
  1429. buf = pdf_load_stream(ctx, xfa);
  1430. }
  1431. else
  1432. {
  1433. /* Concatenate packets to create entire XFA resource */
  1434. buf = fz_new_buffer(ctx, 1024);
  1435. for(i = 0; i < pdf_array_len(ctx, xfa); ++i)
  1436. {
  1437. pdf_obj *ref = pdf_array_get(ctx, xfa, i);
  1438. if (pdf_is_stream(ctx, ref))
  1439. {
  1440. packet = pdf_load_stream(ctx, ref);
  1441. fz_append_buffer(ctx, buf, packet);
  1442. fz_drop_buffer(ctx, packet);
  1443. packet = NULL;
  1444. }
  1445. }
  1446. }
  1447. /* Parse and stow away XFA resource in document */
  1448. doc->xfa = fz_parse_xml(ctx, buf, 0);
  1449. }
  1450. fz_always(ctx)
  1451. {
  1452. fz_drop_buffer(ctx, packet);
  1453. fz_drop_buffer(ctx, buf);
  1454. }
  1455. fz_catch(ctx)
  1456. {
  1457. fz_rethrow(ctx);
  1458. }
  1459. return doc->xfa;
  1460. }
  1461. static fz_xml *
  1462. get_xfa_resource(fz_context *ctx, pdf_document *doc, const char *str)
  1463. {
  1464. fz_xml_doc *xfa;
  1465. xfa = load_xfa(ctx, doc);
  1466. if (!xfa)
  1467. return NULL;
  1468. return fz_xml_find_down(fz_xml_root(xfa), str);
  1469. }
  1470. static int
  1471. find_name_component(char **np, char **sp, char **ep)
  1472. {
  1473. char *n = *np;
  1474. char *s, *e;
  1475. int idx = 0;
  1476. if (*n == '.')
  1477. n++;
  1478. /* Find the next name we are looking for. */
  1479. s = e = n;
  1480. while (*e && *e != '[' && *e != '.')
  1481. e++;
  1482. /* So the next name is s..e */
  1483. n = e;
  1484. if (*n == '[')
  1485. {
  1486. n++;
  1487. while (*n >= '0' && *n <= '9')
  1488. idx = idx*10 + *n++ - '0';
  1489. while (*n && *n != ']')
  1490. n++;
  1491. if (*n == ']')
  1492. n++;
  1493. }
  1494. *np = n;
  1495. *sp = s;
  1496. *ep = e;
  1497. return idx;
  1498. }
  1499. static pdf_obj *
  1500. annot_from_name(fz_context *ctx, pdf_document *doc, const char *str)
  1501. {
  1502. pdf_obj *fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
  1503. if (strncmp(str, "xfa[0].", 7) == 0)
  1504. str += 7;
  1505. if (strncmp(str, "template[0].", 12) == 0)
  1506. str += 12;
  1507. return pdf_lookup_field(ctx, fields, str);
  1508. }
  1509. static pdf_obj *
  1510. get_locked_fields_from_xfa(fz_context *ctx, pdf_document *doc, pdf_obj *field)
  1511. {
  1512. char *name = pdf_load_field_name(ctx, field);
  1513. char *n = name;
  1514. const char *use;
  1515. fz_xml *node;
  1516. if (name == NULL)
  1517. return NULL;
  1518. fz_try(ctx)
  1519. {
  1520. node = get_xfa_resource(ctx, doc, "template");
  1521. do
  1522. {
  1523. char c, *s, *e;
  1524. int idx = 0;
  1525. char *key;
  1526. idx = find_name_component(&n, &s, &e);
  1527. /* We want the idx'th occurrence of s..e */
  1528. /* Hacky */
  1529. c = *e;
  1530. *e = 0;
  1531. key = *n ? "subform" : "field";
  1532. node = fz_xml_find_down_match(node, key, "name", s);
  1533. while (node && idx > 0)
  1534. {
  1535. node = fz_xml_find_next_match(node, key, "name", s);
  1536. idx--;
  1537. }
  1538. *e = c;
  1539. }
  1540. while (node && *n == '.');
  1541. }
  1542. fz_always(ctx)
  1543. fz_free(ctx, name);
  1544. fz_catch(ctx)
  1545. fz_rethrow(ctx);
  1546. if (node == NULL)
  1547. return NULL;
  1548. node = fz_xml_find_down(node, "ui");
  1549. node = fz_xml_find_down(node, "signature");
  1550. node = fz_xml_find_down(node, "manifest");
  1551. use = fz_xml_att(node, "use");
  1552. if (use == NULL)
  1553. return NULL;
  1554. if (*use == '#')
  1555. use++;
  1556. /* Now look for a variables entry in a subform that defines this. */
  1557. while (node)
  1558. {
  1559. fz_xml *variables, *manifest, *ref;
  1560. pdf_obj *arr;
  1561. /* Find the enclosing subform */
  1562. do {
  1563. node = fz_xml_up(node);
  1564. } while (node && strcmp(fz_xml_tag(node), "subform"));
  1565. /* Look for a variables within that. */
  1566. variables = fz_xml_find_down(node, "variables");
  1567. if (variables == NULL)
  1568. continue;
  1569. manifest = fz_xml_find_down_match(variables, "manifest", "id", use);
  1570. if (manifest == NULL)
  1571. continue;
  1572. arr = pdf_new_array(ctx, doc, 16);
  1573. fz_try(ctx)
  1574. {
  1575. ref = fz_xml_find_down(manifest, "ref");
  1576. while (ref)
  1577. {
  1578. const char *s = fz_xml_text(fz_xml_down(ref));
  1579. pdf_array_push(ctx, arr, annot_from_name(ctx, doc, s));
  1580. ref = fz_xml_find_next(ref, "ref");
  1581. }
  1582. }
  1583. fz_catch(ctx)
  1584. {
  1585. pdf_drop_obj(ctx, arr);
  1586. fz_rethrow(ctx);
  1587. }
  1588. return arr;
  1589. }
  1590. return NULL;
  1591. }
  1592. static void
  1593. lock_field(fz_context *ctx, pdf_obj *f)
  1594. {
  1595. int ff = pdf_dict_get_inheritable_int(ctx, f, PDF_NAME(Ff));
  1596. if ((ff & PDF_FIELD_IS_READ_ONLY) ||
  1597. !pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Type)), PDF_NAME(Annot)) ||
  1598. !pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Subtype)), PDF_NAME(Widget)))
  1599. return;
  1600. pdf_dict_put_int(ctx, f, PDF_NAME(Ff), ff | PDF_FIELD_IS_READ_ONLY);
  1601. }
  1602. static void
  1603. lock_xfa_locked_fields(fz_context *ctx, pdf_obj *a)
  1604. {
  1605. int i;
  1606. int len = pdf_array_len(ctx, a);
  1607. for (i = 0; i < len; i++)
  1608. {
  1609. lock_field(ctx, pdf_array_get(ctx, a, i));
  1610. }
  1611. }
  1612. void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer, int64_t stime)
  1613. {
  1614. pdf_obj *v = NULL;
  1615. pdf_obj *o = NULL;
  1616. pdf_obj *r = NULL;
  1617. pdf_obj *t = NULL;
  1618. pdf_obj *a = NULL;
  1619. pdf_obj *b = NULL;
  1620. pdf_obj *l = NULL;
  1621. pdf_obj *indv;
  1622. int vnum;
  1623. size_t max_digest_size;
  1624. char *buf = NULL;
  1625. vnum = pdf_create_object(ctx, doc);
  1626. indv = pdf_new_indirect(ctx, doc, vnum, 0);
  1627. pdf_dict_put_drop(ctx, field, PDF_NAME(V), indv);
  1628. max_digest_size = signer->max_digest_size(ctx, signer);
  1629. fz_var(v);
  1630. fz_var(o);
  1631. fz_var(r);
  1632. fz_var(t);
  1633. fz_var(a);
  1634. fz_var(b);
  1635. fz_var(l);
  1636. fz_var(buf);
  1637. fz_try(ctx)
  1638. {
  1639. v = pdf_new_dict(ctx, doc, 4);
  1640. pdf_update_object(ctx, doc, vnum, v);
  1641. buf = fz_calloc(ctx, max_digest_size, 1);
  1642. /* Ensure that the /Filter entry is the first entry in the
  1643. dictionary after the digest contents since we look for
  1644. this tag when completing signatures in pdf-write.c in order
  1645. to generate the correct byte range. */
  1646. pdf_dict_put_array(ctx, v, PDF_NAME(ByteRange), 4);
  1647. pdf_dict_put_string(ctx, v, PDF_NAME(Contents), buf, max_digest_size);
  1648. pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite));
  1649. pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached));
  1650. pdf_dict_put(ctx, v, PDF_NAME(Type), PDF_NAME(Sig));
  1651. pdf_dict_put_date(ctx, v, PDF_NAME(M), stime);
  1652. o = pdf_dict_put_array(ctx, v, PDF_NAME(Reference), 1);
  1653. r = pdf_array_put_dict(ctx, o, 0, 4);
  1654. pdf_dict_put(ctx, r, PDF_NAME(Data), pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)));
  1655. pdf_dict_put(ctx, r, PDF_NAME(TransformMethod), PDF_NAME(FieldMDP));
  1656. pdf_dict_put(ctx, r, PDF_NAME(Type), PDF_NAME(SigRef));
  1657. t = pdf_dict_put_dict(ctx, r, PDF_NAME(TransformParams), 5);
  1658. l = pdf_dict_getp(ctx, field, "Lock/Action");
  1659. if (l)
  1660. {
  1661. a = pdf_dict_getp(ctx, field, "Lock/Fields");
  1662. }
  1663. else
  1664. {
  1665. /* Lock action wasn't specified so we need to encode an Include.
  1666. * Before we just use an empty array, check in the XFA for locking
  1667. * details. */
  1668. a = get_locked_fields_from_xfa(ctx, doc, field);
  1669. if (a)
  1670. lock_xfa_locked_fields(ctx, a);
  1671. /* If we don't get a result from the XFA, just encode an empty array
  1672. * (leave a == NULL), even if Lock/Fields exists because we don't really
  1673. * know what to do with the information if the action isn't defined. */
  1674. l = PDF_NAME(Include);
  1675. }
  1676. pdf_dict_put(ctx, t, PDF_NAME(Action), l);
  1677. if (pdf_name_eq(ctx, l, PDF_NAME(Include)) || pdf_name_eq(ctx, l, PDF_NAME(Exclude)))
  1678. {
  1679. /* For action Include and Exclude, we need to encode a Fields array */
  1680. if (!a)
  1681. {
  1682. /* If one wasn't defined or we chose to ignore it because no action
  1683. * was defined then use an empty one. */
  1684. b = pdf_new_array(ctx, doc, 0);
  1685. a = b;
  1686. }
  1687. pdf_dict_put_drop(ctx, t, PDF_NAME(Fields), pdf_copy_array(ctx, a));
  1688. }
  1689. pdf_dict_put(ctx, t, PDF_NAME(Type), PDF_NAME(TransformParams));
  1690. pdf_dict_put(ctx, t, PDF_NAME(V), PDF_NAME(1_2));
  1691. /* Record details within the document structure so that contents
  1692. * and byte_range can be updated with their correct values at
  1693. * saving time */
  1694. pdf_xref_store_unsaved_signature(ctx, doc, field, signer);
  1695. }
  1696. fz_always(ctx)
  1697. {
  1698. pdf_drop_obj(ctx, v);
  1699. pdf_drop_obj(ctx, b);
  1700. fz_free(ctx, buf);
  1701. }
  1702. fz_catch(ctx)
  1703. {
  1704. fz_rethrow(ctx);
  1705. }
  1706. }
  1707. void pdf_set_widget_editing_state(fz_context *ctx, pdf_annot *widget, int editing)
  1708. {
  1709. widget->ignore_trigger_events = editing;
  1710. }
  1711. int pdf_get_widget_editing_state(fz_context *ctx, pdf_annot *widget)
  1712. {
  1713. return widget->ignore_trigger_events;
  1714. }
  1715. static void pdf_execute_js_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *js)
  1716. {
  1717. if (js)
  1718. {
  1719. char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
  1720. int in_op = 0;
  1721. fz_var(in_op);
  1722. fz_try(ctx)
  1723. {
  1724. char buf[100];
  1725. fz_snprintf(buf, sizeof buf, "%d/%s", pdf_to_num(ctx, target), path);
  1726. pdf_begin_operation(ctx, doc, "Javascript Event");
  1727. in_op = 1;
  1728. pdf_js_execute(doc->js, buf, code, NULL);
  1729. pdf_end_operation(ctx, doc);
  1730. }
  1731. fz_always(ctx)
  1732. {
  1733. fz_free(ctx, code);
  1734. }
  1735. fz_catch(ctx)
  1736. {
  1737. if (in_op)
  1738. pdf_abandon_operation(ctx, doc);
  1739. fz_rethrow(ctx);
  1740. }
  1741. }
  1742. }
  1743. static void pdf_execute_action_imp(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action)
  1744. {
  1745. pdf_obj *S = pdf_dict_get(ctx, action, PDF_NAME(S));
  1746. if (pdf_name_eq(ctx, S, PDF_NAME(JavaScript)))
  1747. {
  1748. if (doc->js)
  1749. pdf_execute_js_action(ctx, doc, target, path, pdf_dict_get(ctx, action, PDF_NAME(JS)));
  1750. }
  1751. if (pdf_name_eq(ctx, S, PDF_NAME(ResetForm)))
  1752. {
  1753. pdf_obj *fields = pdf_dict_get(ctx, action, PDF_NAME(Fields));
  1754. int flags = pdf_dict_get_int(ctx, action, PDF_NAME(Flags));
  1755. pdf_reset_form(ctx, doc, fields, flags & 1);
  1756. }
  1757. }
  1758. static void pdf_execute_action_chain(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action, pdf_cycle_list *cycle_up)
  1759. {
  1760. pdf_cycle_list cycle;
  1761. pdf_obj *next;
  1762. if (pdf_cycle(ctx, &cycle, cycle_up, action))
  1763. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in action chain");
  1764. if (pdf_is_array(ctx, action))
  1765. {
  1766. int i, n = pdf_array_len(ctx, action);
  1767. for (i = 0; i < n; ++i)
  1768. pdf_execute_action_chain(ctx, doc, target, path, pdf_array_get(ctx, action, i), &cycle);
  1769. }
  1770. else
  1771. {
  1772. pdf_execute_action_imp(ctx, doc, target, path, action);
  1773. next = pdf_dict_get(ctx, action, PDF_NAME(Next));
  1774. if (next)
  1775. pdf_execute_action_chain(ctx, doc, target, path, next, &cycle);
  1776. }
  1777. }
  1778. static void pdf_execute_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path)
  1779. {
  1780. pdf_obj *action = pdf_dict_getp_inheritable(ctx, target, path);
  1781. if (action)
  1782. pdf_execute_action_chain(ctx, doc, target, path, action, NULL);
  1783. }
  1784. void pdf_document_event_will_close(fz_context *ctx, pdf_document *doc)
  1785. {
  1786. pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WC");
  1787. }
  1788. void pdf_document_event_will_save(fz_context *ctx, pdf_document *doc)
  1789. {
  1790. pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WS");
  1791. }
  1792. void pdf_document_event_did_save(fz_context *ctx, pdf_document *doc)
  1793. {
  1794. pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DS");
  1795. }
  1796. void pdf_document_event_will_print(fz_context *ctx, pdf_document *doc)
  1797. {
  1798. pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WP");
  1799. }
  1800. void pdf_document_event_did_print(fz_context *ctx, pdf_document *doc)
  1801. {
  1802. pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DP");
  1803. }
  1804. void pdf_page_event_open(fz_context *ctx, pdf_page *page)
  1805. {
  1806. pdf_execute_action(ctx, page->doc, page->obj, "AA/O");
  1807. }
  1808. void pdf_page_event_close(fz_context *ctx, pdf_page *page)
  1809. {
  1810. pdf_execute_action(ctx, page->doc, page->obj, "AA/C");
  1811. }
  1812. static void
  1813. annot_execute_action(fz_context *ctx, pdf_annot *annot, const char *act)
  1814. {
  1815. begin_annot_op(ctx, annot, "JavaScript action");
  1816. fz_try(ctx)
  1817. {
  1818. pdf_execute_action(ctx, annot->page->doc, annot->obj, act);
  1819. end_annot_op(ctx, annot);
  1820. }
  1821. fz_catch(ctx)
  1822. {
  1823. abandon_annot_op(ctx, annot);
  1824. fz_rethrow(ctx);
  1825. }
  1826. }
  1827. void pdf_annot_event_enter(fz_context *ctx, pdf_annot *annot)
  1828. {
  1829. annot_execute_action(ctx, annot, "AA/E");
  1830. }
  1831. void pdf_annot_event_exit(fz_context *ctx, pdf_annot *annot)
  1832. {
  1833. annot_execute_action(ctx, annot, "AA/X");
  1834. }
  1835. void pdf_annot_event_down(fz_context *ctx, pdf_annot *annot)
  1836. {
  1837. annot_execute_action(ctx, annot, "AA/D");
  1838. }
  1839. void pdf_annot_event_up(fz_context *ctx, pdf_annot *annot)
  1840. {
  1841. pdf_obj *action;
  1842. begin_annot_op(ctx, annot, "JavaScript action");
  1843. fz_try(ctx)
  1844. {
  1845. action = pdf_dict_get(ctx, annot->obj, PDF_NAME(A));
  1846. if (action)
  1847. pdf_execute_action_chain(ctx, annot->page->doc, annot->obj, "A", action, NULL);
  1848. else
  1849. pdf_execute_action(ctx, annot->page->doc, annot->obj, "AA/U");
  1850. end_annot_op(ctx, annot);
  1851. }
  1852. fz_catch(ctx)
  1853. {
  1854. abandon_annot_op(ctx, annot);
  1855. fz_rethrow(ctx);
  1856. }
  1857. }
  1858. void pdf_annot_event_focus(fz_context *ctx, pdf_annot *annot)
  1859. {
  1860. annot_execute_action(ctx, annot, "AA/Fo");
  1861. }
  1862. void pdf_annot_event_blur(fz_context *ctx, pdf_annot *annot)
  1863. {
  1864. annot_execute_action(ctx, annot, "AA/Bl");
  1865. }
  1866. void pdf_annot_event_page_open(fz_context *ctx, pdf_annot *annot)
  1867. {
  1868. annot_execute_action(ctx, annot, "AA/PO");
  1869. }
  1870. void pdf_annot_event_page_close(fz_context *ctx, pdf_annot *annot)
  1871. {
  1872. annot_execute_action(ctx, annot, "AA/PC");
  1873. }
  1874. void pdf_annot_event_page_visible(fz_context *ctx, pdf_annot *annot)
  1875. {
  1876. annot_execute_action(ctx, annot, "AA/PV");
  1877. }
  1878. void pdf_annot_event_page_invisible(fz_context *ctx, pdf_annot *annot)
  1879. {
  1880. annot_execute_action(ctx, annot, "AA/PI");
  1881. }
  1882. int pdf_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_keystroke_event *evt)
  1883. {
  1884. pdf_js *js = doc->js;
  1885. if (js)
  1886. {
  1887. pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/K/JS");
  1888. if (action)
  1889. {
  1890. pdf_js_event_init_keystroke(js, field, evt);
  1891. pdf_execute_js_action(ctx, doc, field, "AA/K/JS", action);
  1892. return pdf_js_event_result_keystroke(js, evt);
  1893. }
  1894. }
  1895. evt->newChange = fz_strdup(ctx, evt->change);
  1896. evt->newValue = fz_strdup(ctx, evt->value);
  1897. return 1;
  1898. }
  1899. int pdf_annot_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_annot *annot, pdf_keystroke_event *evt)
  1900. {
  1901. int ret;
  1902. pdf_annot_push_local_xref(ctx, annot);
  1903. fz_try(ctx)
  1904. ret = pdf_field_event_keystroke(ctx, doc, annot->obj, evt);
  1905. fz_always(ctx)
  1906. pdf_annot_pop_local_xref(ctx, annot);
  1907. fz_catch(ctx)
  1908. fz_rethrow(ctx);
  1909. return ret;
  1910. }
  1911. char *pdf_field_event_format(fz_context *ctx, pdf_document *doc, pdf_obj *field)
  1912. {
  1913. pdf_js *js = doc->js;
  1914. if (js)
  1915. {
  1916. pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/F/JS");
  1917. if (action)
  1918. {
  1919. const char *value = pdf_field_value(ctx, field);
  1920. pdf_js_event_init(js, field, value, 1);
  1921. pdf_execute_js_action(ctx, doc, field, "AA/F/JS", action);
  1922. return pdf_js_event_value(js);
  1923. }
  1924. }
  1925. return NULL;
  1926. }
  1927. int pdf_field_event_validate(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *value, char **newvalue)
  1928. {
  1929. pdf_js *js = doc->js;
  1930. *newvalue = NULL;
  1931. if (js)
  1932. {
  1933. pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/V/JS");
  1934. if (action)
  1935. {
  1936. pdf_js_event_init(js, field, value, 1);
  1937. pdf_execute_js_action(ctx, doc, field, "AA/V/JS", action);
  1938. return pdf_js_event_result_validate(js, newvalue);
  1939. }
  1940. }
  1941. return 1;
  1942. }
  1943. void pdf_field_event_calculate(fz_context *ctx, pdf_document *doc, pdf_obj *field)
  1944. {
  1945. pdf_js *js = doc->js;
  1946. if (js)
  1947. {
  1948. pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/C/JS");
  1949. if (action)
  1950. {
  1951. char *old_value = fz_strdup(ctx, pdf_field_value(ctx, field));
  1952. char *new_value = NULL;
  1953. fz_var(new_value);
  1954. fz_try(ctx)
  1955. {
  1956. pdf_js_event_init(js, field, old_value, 1);
  1957. pdf_execute_js_action(ctx, doc, field, "AA/C/JS", action);
  1958. if (pdf_js_event_result(js))
  1959. {
  1960. new_value = pdf_js_event_value(js);
  1961. if (strcmp(old_value, new_value))
  1962. pdf_set_field_value(ctx, doc, field, new_value, 0);
  1963. }
  1964. }
  1965. fz_always(ctx)
  1966. {
  1967. fz_free(ctx, old_value);
  1968. fz_free(ctx, new_value);
  1969. }
  1970. fz_catch(ctx)
  1971. fz_rethrow(ctx);
  1972. }
  1973. }
  1974. }
  1975. static void
  1976. count_sigs(fz_context *ctx, pdf_obj *field, void *arg, pdf_obj **ft)
  1977. {
  1978. int *n = (int *)arg;
  1979. if (!pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Type)), PDF_NAME(Annot)) ||
  1980. !pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Subtype)), PDF_NAME(Widget)) ||
  1981. !pdf_name_eq(ctx, *ft, PDF_NAME(Sig)))
  1982. return;
  1983. (*n)++;
  1984. }
  1985. static pdf_obj *ft_name[2] = { PDF_NAME(FT), NULL };
  1986. int pdf_count_signatures(fz_context *ctx, pdf_document *doc)
  1987. {
  1988. int n = 0;
  1989. pdf_obj *ft = NULL;
  1990. pdf_obj *form_fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
  1991. pdf_walk_tree(ctx, form_fields, PDF_NAME(Kids), count_sigs, NULL, &n, ft_name, &ft);
  1992. return n;
  1993. }
  1994. /*
  1995. * Bake interactive form fields into static content.
  1996. */
  1997. static pdf_obj *get_annot_ap(fz_context *ctx, pdf_obj *annot)
  1998. {
  1999. pdf_obj *ap = pdf_dict_get(ctx, annot, PDF_NAME(AP));
  2000. pdf_obj *as = pdf_dict_get(ctx, annot, PDF_NAME(AS));
  2001. if (ap)
  2002. {
  2003. ap = pdf_dict_get(ctx, ap, PDF_NAME(N));
  2004. if (pdf_is_stream(ctx, ap))
  2005. return ap;
  2006. ap = pdf_dict_get(ctx, ap, as);
  2007. if (pdf_is_stream(ctx, ap))
  2008. return ap;
  2009. }
  2010. return NULL;
  2011. }
  2012. static fz_matrix get_annot_transform(fz_context *ctx, pdf_obj *annot, pdf_obj *ap)
  2013. {
  2014. float w, h, x, y;
  2015. fz_matrix transform;
  2016. fz_rect bbox;
  2017. fz_rect rect;
  2018. rect = pdf_dict_get_rect(ctx, annot, PDF_NAME(Rect));
  2019. bbox = pdf_dict_get_rect(ctx, ap, PDF_NAME(BBox));
  2020. transform = pdf_dict_get_matrix(ctx, ap, PDF_NAME(Matrix));
  2021. bbox = fz_transform_rect(bbox, transform);
  2022. w = (rect.x1 - rect.x0) / (bbox.x1 - bbox.x0);
  2023. h = (rect.y1 - rect.y0) / (bbox.y1 - bbox.y0);
  2024. x = rect.x0 - bbox.x0 * w;
  2025. y = rect.y0 - bbox.y0 * h;
  2026. return fz_make_matrix(w, 0, 0, h, x, y);
  2027. }
  2028. static void pdf_bake_annot(fz_context *ctx, fz_buffer *buf, pdf_document *doc, pdf_obj *page, pdf_obj *res_xobj, pdf_obj *annot)
  2029. {
  2030. fz_matrix m;
  2031. pdf_obj *ap;
  2032. char name[20];
  2033. ap = get_annot_ap(ctx, annot);
  2034. if (ap)
  2035. {
  2036. fz_snprintf(name, sizeof name, "Annot%d", pdf_to_num(ctx, annot));
  2037. pdf_dict_puts(ctx, res_xobj, name, ap);
  2038. pdf_dict_put(ctx, ap, PDF_NAME(Type), PDF_NAME(XObject));
  2039. pdf_dict_put(ctx, ap, PDF_NAME(Subtype), PDF_NAME(Form));
  2040. m = get_annot_transform(ctx, annot, ap);
  2041. fz_append_printf(ctx, buf,
  2042. "q\n%g %g %g %g %g %g cm\n/%s Do\nQ\n",
  2043. m.a, m.b, m.c, m.d, m.e, m.f,
  2044. name
  2045. );
  2046. }
  2047. }
  2048. static void pdf_bake_page(fz_context *ctx, pdf_document *doc, pdf_obj *page, int bake_annots, int bake_widgets)
  2049. {
  2050. pdf_obj *res;
  2051. pdf_obj *res_xobj;
  2052. pdf_obj *contents;
  2053. pdf_obj *new_contents = NULL;
  2054. pdf_obj *annots;
  2055. pdf_obj *annot;
  2056. pdf_obj *subtype;
  2057. pdf_obj *prologue = NULL;
  2058. fz_buffer *buf = NULL;
  2059. int prepend, append;
  2060. int i;
  2061. fz_var(buf);
  2062. fz_var(prologue);
  2063. fz_var(new_contents);
  2064. annots = pdf_dict_get(ctx, page, PDF_NAME(Annots));
  2065. if (pdf_array_len(ctx, annots) == 0)
  2066. return;
  2067. res = pdf_dict_get(ctx, page, PDF_NAME(Resources));
  2068. if (!res)
  2069. res = pdf_dict_put_dict(ctx, page, PDF_NAME(Resources), 4);
  2070. res_xobj = pdf_dict_get(ctx, res, PDF_NAME(XObject));
  2071. if (!res_xobj)
  2072. res_xobj = pdf_dict_put_dict(ctx, res, PDF_NAME(XObject), 8);
  2073. fz_try(ctx)
  2074. {
  2075. // Ensure that the graphics state is balanced.
  2076. contents = pdf_dict_get(ctx, page, PDF_NAME(Contents));
  2077. pdf_count_q_balance(ctx, doc, res, contents, &prepend, &append);
  2078. if (prepend)
  2079. {
  2080. // Prepend enough 'q' to ensure we can get back to initial state.
  2081. buf = fz_new_buffer(ctx, 1024);
  2082. while (prepend-- > 0)
  2083. fz_append_string(ctx, buf, "q\n");
  2084. prologue = pdf_add_stream(ctx, doc, buf, NULL, 0);
  2085. fz_drop_buffer(ctx, buf);
  2086. buf = NULL;
  2087. }
  2088. // Append enough 'Q' to get back to initial state.
  2089. buf = fz_new_buffer(ctx, 1024);
  2090. while (append-- > 0)
  2091. fz_append_string(ctx, buf, "Q\n");
  2092. for (i = 0; i < pdf_array_len(ctx, annots); )
  2093. {
  2094. annot = pdf_array_get(ctx, annots, i);
  2095. subtype = pdf_dict_get(ctx, annot, PDF_NAME(Subtype));
  2096. if (subtype == PDF_NAME(Link))
  2097. {
  2098. ++i;
  2099. }
  2100. else if (subtype == PDF_NAME(Widget))
  2101. {
  2102. if (bake_widgets)
  2103. {
  2104. pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
  2105. pdf_array_delete(ctx, annots, i);
  2106. }
  2107. else
  2108. {
  2109. ++i;
  2110. }
  2111. }
  2112. else
  2113. {
  2114. if (bake_annots)
  2115. {
  2116. pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
  2117. pdf_array_delete(ctx, annots, i);
  2118. }
  2119. else
  2120. {
  2121. ++i;
  2122. }
  2123. }
  2124. }
  2125. if (!pdf_is_array(ctx, contents))
  2126. {
  2127. new_contents = pdf_new_array(ctx, doc, 10);
  2128. if (prologue)
  2129. pdf_array_push(ctx, new_contents, prologue);
  2130. if (contents)
  2131. pdf_array_push(ctx, new_contents, contents);
  2132. pdf_dict_put(ctx, page, PDF_NAME(Contents), new_contents);
  2133. pdf_drop_obj(ctx, new_contents);
  2134. contents = new_contents;
  2135. new_contents = NULL;
  2136. }
  2137. else if (prologue)
  2138. {
  2139. pdf_array_insert(ctx, contents, prologue, 0);
  2140. }
  2141. pdf_array_push_drop(ctx, contents, pdf_add_stream(ctx, doc, buf, NULL, 0));
  2142. }
  2143. fz_always(ctx)
  2144. {
  2145. fz_drop_buffer(ctx, buf);
  2146. pdf_drop_obj(ctx, prologue);
  2147. pdf_drop_obj(ctx, new_contents);
  2148. }
  2149. fz_catch(ctx)
  2150. {
  2151. fz_rethrow(ctx);
  2152. }
  2153. }
  2154. void pdf_bake_document(fz_context *ctx, pdf_document *doc, int bake_annots, int bake_widgets)
  2155. {
  2156. pdf_page *page = NULL;
  2157. pdf_annot *annot;
  2158. int i, n;
  2159. fz_var(page);
  2160. pdf_begin_operation(ctx, doc, "Bake interactive content");
  2161. fz_try(ctx)
  2162. {
  2163. n = pdf_count_pages(ctx, doc);
  2164. for (i = 0; i < n; ++i)
  2165. {
  2166. page = pdf_load_page(ctx, doc, i);
  2167. if (bake_annots)
  2168. for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
  2169. pdf_annot_request_synthesis(ctx, annot);
  2170. if (bake_widgets)
  2171. for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
  2172. pdf_annot_request_synthesis(ctx, annot);
  2173. pdf_update_page(ctx, page);
  2174. pdf_bake_page(ctx, doc, page->obj, bake_annots, bake_widgets);
  2175. fz_drop_page(ctx, (fz_page*)page);
  2176. page = NULL;
  2177. }
  2178. if (bake_widgets)
  2179. {
  2180. pdf_obj *trailer = pdf_trailer(ctx, doc);
  2181. pdf_obj *root = pdf_dict_get(ctx, trailer, PDF_NAME(Root));
  2182. pdf_dict_del(ctx, root, PDF_NAME(AcroForm));
  2183. }
  2184. pdf_end_operation(ctx, doc);
  2185. }
  2186. fz_always(ctx)
  2187. {
  2188. fz_drop_page(ctx, (fz_page*)page);
  2189. }
  2190. fz_catch(ctx)
  2191. {
  2192. pdf_abandon_operation(ctx, doc);
  2193. }
  2194. }