pdf-annot.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392
  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. #include <float.h>
  26. pdf_annot *
  27. pdf_keep_annot(fz_context *ctx, pdf_annot *annot)
  28. {
  29. return fz_keep_imp(ctx, annot, &annot->refs);
  30. }
  31. void
  32. pdf_drop_annot(fz_context *ctx, pdf_annot *annot)
  33. {
  34. if (fz_drop_imp(ctx, annot, &annot->refs))
  35. {
  36. pdf_drop_obj(ctx, annot->obj);
  37. fz_free(ctx, annot);
  38. }
  39. }
  40. void
  41. pdf_drop_annots(fz_context *ctx, pdf_annot *annot)
  42. {
  43. while (annot)
  44. {
  45. pdf_annot *next = annot->next;
  46. pdf_drop_annot(ctx, annot);
  47. annot = next;
  48. }
  49. }
  50. pdf_obj *
  51. pdf_annot_ap(fz_context *ctx, pdf_annot *annot)
  52. {
  53. int flags = pdf_dict_get_int(ctx, annot->obj, PDF_NAME(F));
  54. int readonly = flags & PDF_ANNOT_IS_READ_ONLY;
  55. pdf_obj *ap = pdf_dict_get(ctx, annot->obj, PDF_NAME(AP));
  56. pdf_obj *ap_n = pdf_dict_get(ctx, ap, PDF_NAME(N));
  57. pdf_obj *ap_r = pdf_dict_get(ctx, ap, PDF_NAME(R));
  58. pdf_obj *ap_d = pdf_dict_get(ctx, ap, PDF_NAME(D));
  59. if (!readonly && annot->is_hot && annot->is_active && ap_d)
  60. ap = ap_d;
  61. else if (!readonly && annot->is_hot && ap_r)
  62. ap = ap_r;
  63. else
  64. ap = ap_n;
  65. /* AP/N, AP/R and AP/D may be streams, or dictionaries of streams. */
  66. /* If it's a stream, we have a winner! */
  67. if (pdf_is_indirect(ctx, ap) && pdf_obj_num_is_stream(ctx, annot->page->doc, pdf_to_num(ctx, ap)))
  68. return ap;
  69. /* If it's not a stream, it may be a dictionary containing
  70. * a range of possible values, that should be indexed by
  71. * AS. */
  72. return pdf_dict_get(ctx, ap, pdf_dict_get(ctx, annot->obj, PDF_NAME(AS)));
  73. }
  74. int pdf_annot_active(fz_context *ctx, pdf_annot *annot)
  75. {
  76. return annot ? annot->is_active : 0;
  77. }
  78. void pdf_set_annot_active(fz_context *ctx, pdf_annot *annot, int active)
  79. {
  80. int old;
  81. if (!annot)
  82. return;
  83. old = annot->is_active;
  84. annot->is_active = !!active;
  85. if (old != annot->is_active)
  86. pdf_set_annot_has_changed(ctx, annot);
  87. }
  88. int pdf_annot_hot(fz_context *ctx, pdf_annot *annot)
  89. {
  90. return annot ? annot->is_hot : 0;
  91. }
  92. void pdf_set_annot_hot(fz_context *ctx, pdf_annot *annot, int hot)
  93. {
  94. int old;
  95. if (!annot)
  96. return;
  97. old = annot->is_hot;
  98. annot->is_hot = !!hot;
  99. if (old != annot->is_hot)
  100. pdf_set_annot_has_changed(ctx, annot);
  101. }
  102. fz_matrix
  103. pdf_annot_transform(fz_context *ctx, pdf_annot *annot)
  104. {
  105. fz_rect bbox, rect;
  106. fz_matrix matrix;
  107. float w, h, x, y;
  108. pdf_obj *ap = pdf_annot_ap(ctx, annot);
  109. rect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  110. bbox = pdf_xobject_bbox(ctx, ap);
  111. matrix = pdf_xobject_matrix(ctx, ap);
  112. bbox = fz_transform_rect(bbox, matrix);
  113. if (bbox.x1 == bbox.x0)
  114. w = 0;
  115. else
  116. w = (rect.x1 - rect.x0) / (bbox.x1 - bbox.x0);
  117. if (bbox.y1 == bbox.y0)
  118. h = 0;
  119. else
  120. h = (rect.y1 - rect.y0) / (bbox.y1 - bbox.y0);
  121. x = rect.x0 - (bbox.x0 * w);
  122. y = rect.y0 - (bbox.y0 * h);
  123. return fz_pre_scale(fz_translate(x, y), w, h);
  124. }
  125. /*
  126. Internal function for creating a new pdf annotation.
  127. */
  128. static pdf_annot *
  129. pdf_new_annot(fz_context *ctx, pdf_page *page, pdf_obj *obj)
  130. {
  131. pdf_annot *annot;
  132. annot = fz_malloc_struct(ctx, pdf_annot);
  133. annot->refs = 1;
  134. annot->page = page; /* only borrowed, as the page owns the annot */
  135. annot->obj = pdf_keep_obj(ctx, obj);
  136. return annot;
  137. }
  138. void
  139. pdf_nuke_annots(fz_context *ctx, pdf_page *page)
  140. {
  141. pdf_annot *annot;
  142. for (annot = page->annots; annot; annot = annot->next)
  143. {
  144. pdf_drop_obj(ctx, annot->obj);
  145. annot->obj = NULL;
  146. pdf_drop_annot(ctx, annot);
  147. }
  148. for (annot = page->widgets; annot; annot = annot->next)
  149. {
  150. pdf_drop_obj(ctx, annot->obj);
  151. annot->obj = NULL;
  152. pdf_drop_annot(ctx, annot);
  153. }
  154. page->annots = NULL;
  155. page->widgets = NULL;
  156. page->annot_tailp = &page->annots;
  157. page->widget_tailp = &page->widgets;
  158. }
  159. static pdf_annot *find_and_unlink_annot_from_list(fz_context *ctx, pdf_annot **prev, pdf_obj *obj)
  160. {
  161. int num = pdf_to_num(ctx, obj);
  162. pdf_annot *node;
  163. node = *prev;
  164. while (node)
  165. {
  166. if (pdf_to_num(ctx, node->obj) == num)
  167. {
  168. *prev = node->next;
  169. node->next = NULL;
  170. return node;
  171. }
  172. else
  173. {
  174. prev = &node->next;
  175. node = node->next;
  176. }
  177. }
  178. return NULL;
  179. }
  180. void
  181. pdf_sync_annots(fz_context *ctx, pdf_page *page)
  182. {
  183. pdf_annot *old_annots;
  184. pdf_annot *old_widgets;
  185. pdf_annot *annot, *next;
  186. pdf_obj *annots;
  187. pdf_obj *subtype;
  188. int i, n;
  189. // Save list of annots loaded last time (if any).
  190. old_annots = page->annots;
  191. old_widgets = page->widgets;
  192. page->annots = NULL;
  193. page->widgets = NULL;
  194. page->annot_tailp = &page->annots;
  195. page->widget_tailp = &page->widgets;
  196. // Create new list of annots (reusing old annots when possible)
  197. annots = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots));
  198. n = pdf_array_len(ctx, annots);
  199. for (i = 0; i < n; ++i)
  200. {
  201. pdf_obj *obj = pdf_array_get(ctx, annots, i);
  202. if (pdf_is_dict(ctx, obj))
  203. {
  204. subtype = pdf_dict_get(ctx, obj, PDF_NAME(Subtype));
  205. if (pdf_name_eq(ctx, subtype, PDF_NAME(Link)))
  206. continue;
  207. if (pdf_name_eq(ctx, subtype, PDF_NAME(Popup)))
  208. continue;
  209. if (pdf_name_eq(ctx, subtype, PDF_NAME(Widget)))
  210. {
  211. annot = find_and_unlink_annot_from_list(ctx, &old_widgets, obj);
  212. if (!annot)
  213. annot = pdf_new_annot(ctx, page, obj);
  214. *page->widget_tailp = annot;
  215. page->widget_tailp = &annot->next;
  216. }
  217. else
  218. {
  219. annot = find_and_unlink_annot_from_list(ctx, &old_annots, obj);
  220. if (!annot)
  221. annot = pdf_new_annot(ctx, page, obj);
  222. *page->annot_tailp = annot;
  223. page->annot_tailp = &annot->next;
  224. }
  225. }
  226. }
  227. // Nuke the annot structs that are no longer used on the page
  228. for (annot = old_annots; annot; annot = next)
  229. {
  230. next = annot->next;
  231. pdf_drop_obj(ctx, annot->obj);
  232. annot->obj = NULL;
  233. pdf_drop_annot(ctx, annot);
  234. }
  235. for (annot = old_widgets; annot; annot = next)
  236. {
  237. next = annot->next;
  238. pdf_drop_obj(ctx, annot->obj);
  239. annot->obj = NULL;
  240. pdf_drop_annot(ctx, annot);
  241. }
  242. }
  243. void
  244. pdf_load_annots(fz_context *ctx, pdf_page *page)
  245. {
  246. pdf_sync_annots(ctx, page);
  247. /* We need to run a resynth pass on the annotations on this
  248. * page. That means rerunning it on the complete document. */
  249. page->doc->resynth_required = 1;
  250. /* And actually update the page so that any annotations required
  251. * get synthesised. */
  252. pdf_update_page(ctx, page);
  253. }
  254. pdf_annot *
  255. pdf_first_annot(fz_context *ctx, pdf_page *page)
  256. {
  257. return page ? page->annots : NULL;
  258. }
  259. pdf_annot *
  260. pdf_next_annot(fz_context *ctx, pdf_annot *annot)
  261. {
  262. return annot ? annot->next : NULL;
  263. }
  264. pdf_obj *pdf_annot_obj(fz_context *ctx, pdf_annot *annot)
  265. {
  266. return annot ? annot->obj : NULL;
  267. }
  268. pdf_page *pdf_annot_page(fz_context *ctx, pdf_annot *annot)
  269. {
  270. return annot ? annot->page : NULL;
  271. }
  272. fz_rect
  273. pdf_bound_annot(fz_context *ctx, pdf_annot *annot)
  274. {
  275. fz_matrix page_ctm;
  276. fz_rect rect;
  277. int flags;
  278. pdf_annot_push_local_xref(ctx, annot);
  279. fz_try(ctx)
  280. {
  281. rect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  282. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  283. flags = pdf_dict_get_int(ctx, annot->obj, PDF_NAME(F));
  284. if (flags & PDF_ANNOT_IS_NO_ROTATE)
  285. {
  286. int rotate = pdf_dict_get_inheritable_int(ctx, annot->page->obj, PDF_NAME(Rotate));
  287. fz_point tp = fz_transform_point_xy(rect.x0, rect.y1, page_ctm);
  288. page_ctm = fz_concat(page_ctm, fz_translate(-tp.x, -tp.y));
  289. page_ctm = fz_concat(page_ctm, fz_rotate(-rotate));
  290. page_ctm = fz_concat(page_ctm, fz_translate(tp.x, tp.y));
  291. }
  292. }
  293. fz_always(ctx)
  294. pdf_annot_pop_local_xref(ctx, annot);
  295. fz_catch(ctx)
  296. fz_rethrow(ctx);
  297. return fz_transform_rect(rect, page_ctm);
  298. }
  299. void
  300. pdf_annot_request_resynthesis(fz_context *ctx, pdf_annot *annot)
  301. {
  302. if (annot == NULL)
  303. return;
  304. annot->needs_new_ap = 1;
  305. annot->page->doc->resynth_required = 1;
  306. }
  307. void
  308. pdf_annot_request_synthesis(fz_context *ctx, pdf_annot *annot)
  309. {
  310. if (annot == NULL)
  311. return;
  312. if (!pdf_annot_ap(ctx, annot))
  313. pdf_annot_request_resynthesis(ctx, annot);
  314. }
  315. int
  316. pdf_annot_needs_resynthesis(fz_context *ctx, pdf_annot *annot)
  317. {
  318. return annot ? annot->needs_new_ap : 0;
  319. }
  320. void pdf_set_annot_resynthesised(fz_context *ctx, pdf_annot *annot)
  321. {
  322. if (annot == NULL)
  323. return;
  324. annot->needs_new_ap = 0;
  325. pdf_set_annot_has_changed(ctx, annot);
  326. }
  327. void pdf_set_annot_has_changed(fz_context *ctx, pdf_annot *annot)
  328. {
  329. if (annot == NULL)
  330. return;
  331. annot->has_new_ap = 1;
  332. }
  333. void
  334. pdf_dirty_annot(fz_context *ctx, pdf_annot *annot)
  335. {
  336. pdf_annot_request_resynthesis(ctx, annot);
  337. }
  338. const char *
  339. pdf_string_from_annot_type(fz_context *ctx, enum pdf_annot_type type)
  340. {
  341. switch (type)
  342. {
  343. case PDF_ANNOT_TEXT: return "Text";
  344. case PDF_ANNOT_LINK: return "Link";
  345. case PDF_ANNOT_FREE_TEXT: return "FreeText";
  346. case PDF_ANNOT_LINE: return "Line";
  347. case PDF_ANNOT_SQUARE: return "Square";
  348. case PDF_ANNOT_CIRCLE: return "Circle";
  349. case PDF_ANNOT_POLYGON: return "Polygon";
  350. case PDF_ANNOT_POLY_LINE: return "PolyLine";
  351. case PDF_ANNOT_HIGHLIGHT: return "Highlight";
  352. case PDF_ANNOT_UNDERLINE: return "Underline";
  353. case PDF_ANNOT_SQUIGGLY: return "Squiggly";
  354. case PDF_ANNOT_STRIKE_OUT: return "StrikeOut";
  355. case PDF_ANNOT_REDACT: return "Redact";
  356. case PDF_ANNOT_STAMP: return "Stamp";
  357. case PDF_ANNOT_CARET: return "Caret";
  358. case PDF_ANNOT_INK: return "Ink";
  359. case PDF_ANNOT_POPUP: return "Popup";
  360. case PDF_ANNOT_FILE_ATTACHMENT: return "FileAttachment";
  361. case PDF_ANNOT_SOUND: return "Sound";
  362. case PDF_ANNOT_MOVIE: return "Movie";
  363. case PDF_ANNOT_RICH_MEDIA: return "RichMedia";
  364. case PDF_ANNOT_WIDGET: return "Widget";
  365. case PDF_ANNOT_SCREEN: return "Screen";
  366. case PDF_ANNOT_PRINTER_MARK: return "PrinterMark";
  367. case PDF_ANNOT_TRAP_NET: return "TrapNet";
  368. case PDF_ANNOT_WATERMARK: return "Watermark";
  369. case PDF_ANNOT_3D: return "3D";
  370. case PDF_ANNOT_PROJECTION: return "Projection";
  371. default: return "UNKNOWN";
  372. }
  373. }
  374. enum pdf_annot_type
  375. pdf_annot_type_from_string(fz_context *ctx, const char *subtype)
  376. {
  377. if (!strcmp("Text", subtype)) return PDF_ANNOT_TEXT;
  378. if (!strcmp("Link", subtype)) return PDF_ANNOT_LINK;
  379. if (!strcmp("FreeText", subtype)) return PDF_ANNOT_FREE_TEXT;
  380. if (!strcmp("Line", subtype)) return PDF_ANNOT_LINE;
  381. if (!strcmp("Square", subtype)) return PDF_ANNOT_SQUARE;
  382. if (!strcmp("Circle", subtype)) return PDF_ANNOT_CIRCLE;
  383. if (!strcmp("Polygon", subtype)) return PDF_ANNOT_POLYGON;
  384. if (!strcmp("PolyLine", subtype)) return PDF_ANNOT_POLY_LINE;
  385. if (!strcmp("Highlight", subtype)) return PDF_ANNOT_HIGHLIGHT;
  386. if (!strcmp("Underline", subtype)) return PDF_ANNOT_UNDERLINE;
  387. if (!strcmp("Squiggly", subtype)) return PDF_ANNOT_SQUIGGLY;
  388. if (!strcmp("StrikeOut", subtype)) return PDF_ANNOT_STRIKE_OUT;
  389. if (!strcmp("Redact", subtype)) return PDF_ANNOT_REDACT;
  390. if (!strcmp("Stamp", subtype)) return PDF_ANNOT_STAMP;
  391. if (!strcmp("Caret", subtype)) return PDF_ANNOT_CARET;
  392. if (!strcmp("Ink", subtype)) return PDF_ANNOT_INK;
  393. if (!strcmp("Popup", subtype)) return PDF_ANNOT_POPUP;
  394. if (!strcmp("FileAttachment", subtype)) return PDF_ANNOT_FILE_ATTACHMENT;
  395. if (!strcmp("Sound", subtype)) return PDF_ANNOT_SOUND;
  396. if (!strcmp("Movie", subtype)) return PDF_ANNOT_MOVIE;
  397. if (!strcmp("RichMedia", subtype)) return PDF_ANNOT_RICH_MEDIA;
  398. if (!strcmp("Widget", subtype)) return PDF_ANNOT_WIDGET;
  399. if (!strcmp("Screen", subtype)) return PDF_ANNOT_SCREEN;
  400. if (!strcmp("PrinterMark", subtype)) return PDF_ANNOT_PRINTER_MARK;
  401. if (!strcmp("TrapNet", subtype)) return PDF_ANNOT_TRAP_NET;
  402. if (!strcmp("Watermark", subtype)) return PDF_ANNOT_WATERMARK;
  403. if (!strcmp("3D", subtype)) return PDF_ANNOT_3D;
  404. if (!strcmp("Projection", subtype)) return PDF_ANNOT_PROJECTION;
  405. return PDF_ANNOT_UNKNOWN;
  406. }
  407. static void
  408. begin_annot_op(fz_context *ctx, pdf_annot *annot, const char *op)
  409. {
  410. if (!annot->page)
  411. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
  412. pdf_begin_operation(ctx, annot->page->doc, op);
  413. }
  414. static void
  415. end_annot_op(fz_context *ctx, pdf_annot *annot)
  416. {
  417. pdf_end_operation(ctx, annot->page->doc);
  418. }
  419. static void
  420. abandon_annot_op(fz_context *ctx, pdf_annot *annot)
  421. {
  422. pdf_abandon_operation(ctx, annot->page->doc);
  423. }
  424. static int is_allowed_subtype(fz_context *ctx, pdf_annot *annot, pdf_obj *property, pdf_obj **allowed)
  425. {
  426. pdf_obj *subtype;
  427. subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  428. while (*allowed) {
  429. if (pdf_name_eq(ctx, subtype, *allowed))
  430. return 1;
  431. allowed++;
  432. }
  433. return 0;
  434. }
  435. static int is_allowed_subtype_wrap(fz_context *ctx, pdf_annot *annot, pdf_obj *property, pdf_obj **allowed)
  436. {
  437. int ret;
  438. pdf_annot_push_local_xref(ctx, annot);
  439. fz_try(ctx)
  440. ret = is_allowed_subtype(ctx, annot, property, allowed);
  441. fz_always(ctx)
  442. pdf_annot_pop_local_xref(ctx, annot);
  443. fz_catch(ctx)
  444. fz_rethrow(ctx);
  445. return ret;
  446. }
  447. static void check_allowed_subtypes(fz_context *ctx, pdf_annot *annot, pdf_obj *property, pdf_obj **allowed)
  448. {
  449. pdf_obj *subtype;
  450. subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  451. if (!is_allowed_subtype(ctx, annot, property, allowed))
  452. fz_throw(ctx, FZ_ERROR_ARGUMENT, "%s annotations have no %s property", pdf_to_name(ctx, subtype), pdf_to_name(ctx, property));
  453. }
  454. pdf_annot *
  455. pdf_create_annot_raw(fz_context *ctx, pdf_page *page, enum pdf_annot_type type)
  456. {
  457. pdf_annot *annot = NULL;
  458. pdf_document *doc = page->doc;
  459. pdf_obj *annot_obj = pdf_new_dict(ctx, doc, 0);
  460. pdf_obj *ind_obj = NULL;
  461. pdf_obj *free_arr = NULL;
  462. fz_var(annot);
  463. fz_var(ind_obj);
  464. fz_var(free_arr);
  465. fz_try(ctx)
  466. {
  467. int ind_obj_num;
  468. const char *type_str;
  469. pdf_obj *annot_arr;
  470. type_str = pdf_string_from_annot_type(ctx, type);
  471. if (type == PDF_ANNOT_UNKNOWN)
  472. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create unknown annotation");
  473. annot_arr = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots));
  474. if (!pdf_is_array(ctx, annot_arr))
  475. annot_arr = pdf_dict_put_array(ctx, page->obj, PDF_NAME(Annots), 0);
  476. else if (pdf_is_indirect(ctx, annot_arr))
  477. {
  478. free_arr = annot_arr = pdf_copy_array(ctx, annot_arr);
  479. pdf_dict_put(ctx, page->obj, PDF_NAME(Annots), annot_arr);
  480. }
  481. pdf_dict_put(ctx, annot_obj, PDF_NAME(Type), PDF_NAME(Annot));
  482. pdf_dict_put_name(ctx, annot_obj, PDF_NAME(Subtype), type_str);
  483. /*
  484. Both annotation object and annotation structure are now created.
  485. Insert the object in the hierarchy and the structure in the
  486. page's array.
  487. */
  488. ind_obj_num = pdf_create_object(ctx, doc);
  489. pdf_update_object(ctx, doc, ind_obj_num, annot_obj);
  490. ind_obj = pdf_new_indirect(ctx, doc, ind_obj_num, 0);
  491. pdf_array_push(ctx, annot_arr, ind_obj);
  492. annot = pdf_new_annot(ctx, page, ind_obj);
  493. /*
  494. Linking must be done after any call that might throw because
  495. pdf_drop_annots below actually frees a list. Put the new annot
  496. at the end of the list, so that it will be drawn last.
  497. */
  498. if (type == PDF_ANNOT_WIDGET)
  499. {
  500. *page->widget_tailp = annot;
  501. page->widget_tailp = &annot->next;
  502. }
  503. else
  504. {
  505. *page->annot_tailp = annot;
  506. page->annot_tailp = &annot->next;
  507. }
  508. }
  509. fz_always(ctx)
  510. {
  511. pdf_drop_obj(ctx, free_arr);
  512. pdf_drop_obj(ctx, annot_obj);
  513. pdf_drop_obj(ctx, ind_obj);
  514. }
  515. fz_catch(ctx)
  516. {
  517. pdf_drop_annots(ctx, annot);
  518. fz_rethrow(ctx);
  519. }
  520. return pdf_keep_annot(ctx, annot);
  521. }
  522. fz_link *
  523. pdf_create_link(fz_context *ctx, pdf_page *page, fz_rect bbox, const char *uri)
  524. {
  525. fz_link *link = NULL;
  526. pdf_document *doc = page->doc;
  527. pdf_obj *annot_obj = pdf_new_dict(ctx, doc, 0);
  528. pdf_obj *ind_obj = NULL;
  529. pdf_obj *bs = NULL;
  530. pdf_obj *a = NULL;
  531. fz_link **linkp;
  532. fz_rect page_mediabox;
  533. fz_matrix page_ctm;
  534. fz_rect rect;
  535. pdf_obj *free_arr = NULL;
  536. fz_var(link);
  537. fz_var(ind_obj);
  538. fz_var(bs);
  539. fz_var(a);
  540. fz_var(free_arr);
  541. pdf_begin_operation(ctx, page->doc, "Create Link");
  542. fz_try(ctx)
  543. {
  544. int ind_obj_num;
  545. pdf_obj *annot_arr;
  546. pdf_page_transform(ctx, page, &page_mediabox, &page_ctm);
  547. page_ctm = fz_invert_matrix(page_ctm);
  548. rect = fz_transform_rect(bbox, page_ctm);
  549. annot_arr = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots));
  550. if (!pdf_is_array(ctx, annot_arr))
  551. annot_arr = pdf_dict_put_array(ctx, page->obj, PDF_NAME(Annots), 0);
  552. else if (pdf_is_indirect(ctx, annot_arr))
  553. {
  554. free_arr = annot_arr = pdf_copy_array(ctx, annot_arr);
  555. pdf_dict_put(ctx, page->obj, PDF_NAME(Annots), annot_arr);
  556. }
  557. pdf_dict_put(ctx, annot_obj, PDF_NAME(Type), PDF_NAME(Annot));
  558. pdf_dict_put(ctx, annot_obj, PDF_NAME(Subtype), PDF_NAME(Link));
  559. pdf_dict_put_rect(ctx, annot_obj, PDF_NAME(Rect), rect);
  560. bs = pdf_dict_put_dict(ctx, annot_obj, PDF_NAME(BS), 4);
  561. pdf_dict_put(ctx, bs, PDF_NAME(S), PDF_NAME(S));
  562. pdf_dict_put(ctx, bs, PDF_NAME(Type), PDF_NAME(Border));
  563. pdf_dict_put_int(ctx, bs, PDF_NAME(W), 0);
  564. pdf_dict_put_drop(ctx, annot_obj, PDF_NAME(A),
  565. pdf_new_action_from_link(ctx, doc, uri));
  566. /*
  567. Both annotation object and annotation structure are now created.
  568. Insert the object in the hierarchy and the structure in the
  569. page's array.
  570. */
  571. ind_obj_num = pdf_create_object(ctx, doc);
  572. pdf_update_object(ctx, doc, ind_obj_num, annot_obj);
  573. ind_obj = pdf_new_indirect(ctx, doc, ind_obj_num, 0);
  574. pdf_array_push(ctx, annot_arr, ind_obj);
  575. link = (fz_link *) pdf_new_link(ctx, page, bbox, uri, annot_obj);
  576. linkp = &page->links;
  577. while (*linkp != NULL)
  578. linkp = &(*linkp)->next;
  579. *linkp = link;
  580. pdf_end_operation(ctx, page->doc);
  581. }
  582. fz_always(ctx)
  583. {
  584. pdf_drop_obj(ctx, free_arr);
  585. pdf_drop_obj(ctx, annot_obj);
  586. pdf_drop_obj(ctx, ind_obj);
  587. }
  588. fz_catch(ctx)
  589. {
  590. pdf_abandon_operation(ctx, page->doc);
  591. fz_rethrow(ctx);
  592. }
  593. return fz_keep_link(ctx, link);
  594. }
  595. void pdf_delete_link(fz_context *ctx, pdf_page *page, fz_link *link)
  596. {
  597. fz_link **linkptr;
  598. pdf_obj *annots;
  599. int i;
  600. if (link == NULL || page == NULL || page != ((pdf_link *) link)->page)
  601. return;
  602. for (linkptr = &page->links; *linkptr; linkptr = &((*linkptr)->next))
  603. {
  604. if (*linkptr == link)
  605. break;
  606. }
  607. if (*linkptr == NULL)
  608. return;
  609. /* Link may no longer borrow page pointer, since they are separated */
  610. ((pdf_link *) link)->page = NULL;
  611. pdf_begin_operation(ctx, page->doc, "Delete Link");
  612. fz_try(ctx)
  613. {
  614. annots = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots));
  615. i = pdf_array_find(ctx, annots, ((pdf_link *) link)->obj);
  616. if (i >= 0)
  617. pdf_array_delete(ctx, annots, i);
  618. *linkptr = link->next;
  619. link->next = NULL;
  620. fz_drop_link(ctx, link);
  621. pdf_end_operation(ctx, page->doc);
  622. }
  623. fz_catch(ctx)
  624. {
  625. pdf_abandon_operation(ctx, page->doc);
  626. fz_rethrow(ctx);
  627. }
  628. }
  629. static pdf_obj *
  630. pdf_add_popup_annot(fz_context *ctx, pdf_annot *annot)
  631. {
  632. pdf_obj *annots, *popup;
  633. popup = pdf_dict_get(ctx, annot->obj, PDF_NAME(Popup));
  634. if (popup)
  635. return popup;
  636. annots = pdf_dict_get(ctx, annot->page->obj, PDF_NAME(Annots));
  637. if (!annots)
  638. return NULL;
  639. popup = pdf_add_new_dict(ctx, annot->page->doc, 4);
  640. pdf_array_push_drop(ctx, annots, popup);
  641. pdf_dict_put(ctx, popup, PDF_NAME(Type), PDF_NAME(Annot));
  642. pdf_dict_put(ctx, popup, PDF_NAME(Subtype), PDF_NAME(Popup));
  643. pdf_dict_put(ctx, popup, PDF_NAME(Parent), annot->obj);
  644. pdf_dict_put_rect(ctx, popup, PDF_NAME(Rect), fz_make_rect(0,0,0,0));
  645. pdf_dict_put(ctx, annot->obj, PDF_NAME(Popup), popup);
  646. return popup;
  647. }
  648. static pdf_obj *popup_subtypes[] = {
  649. PDF_NAME(Text),
  650. PDF_NAME(FreeText),
  651. PDF_NAME(Line),
  652. PDF_NAME(Square),
  653. PDF_NAME(Circle),
  654. PDF_NAME(Polygon),
  655. PDF_NAME(PolyLine),
  656. PDF_NAME(Highlight),
  657. PDF_NAME(Underline),
  658. PDF_NAME(Squiggly),
  659. PDF_NAME(StrikeOut),
  660. PDF_NAME(Stamp),
  661. PDF_NAME(Caret),
  662. PDF_NAME(Ink),
  663. PDF_NAME(FileAttachment),
  664. PDF_NAME(Sound),
  665. PDF_NAME(Redact),
  666. NULL,
  667. };
  668. int
  669. pdf_annot_has_popup(fz_context *ctx, pdf_annot *annot)
  670. {
  671. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(Popup), popup_subtypes);
  672. }
  673. void pdf_set_annot_popup(fz_context *ctx, pdf_annot *annot, fz_rect rect)
  674. {
  675. fz_matrix page_ctm, inv_page_ctm;
  676. pdf_obj *popup;
  677. begin_annot_op(ctx, annot, "Set popup");
  678. fz_try(ctx)
  679. {
  680. check_allowed_subtypes(ctx, annot, PDF_NAME(Popup), popup_subtypes);
  681. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  682. inv_page_ctm = fz_invert_matrix(page_ctm);
  683. rect = fz_transform_rect(rect, inv_page_ctm);
  684. popup = pdf_add_popup_annot(ctx, annot);
  685. pdf_dict_put_rect(ctx, popup, PDF_NAME(Rect), rect);
  686. end_annot_op(ctx, annot);
  687. }
  688. fz_catch(ctx)
  689. {
  690. abandon_annot_op(ctx, annot);
  691. fz_rethrow(ctx);
  692. }
  693. pdf_dirty_annot(ctx, annot);
  694. }
  695. fz_rect pdf_annot_popup(fz_context *ctx, pdf_annot *annot)
  696. {
  697. fz_matrix page_ctm;
  698. fz_rect rect;
  699. pdf_obj *popup;
  700. pdf_annot_push_local_xref(ctx, annot);
  701. fz_try(ctx)
  702. {
  703. check_allowed_subtypes(ctx, annot, PDF_NAME(Popup), popup_subtypes);
  704. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  705. popup = pdf_dict_get(ctx, annot->obj, PDF_NAME(Popup));
  706. rect = pdf_dict_get_rect(ctx, popup, PDF_NAME(Rect));
  707. rect = fz_transform_rect(rect, page_ctm);
  708. }
  709. fz_always(ctx)
  710. pdf_annot_pop_local_xref(ctx, annot);
  711. fz_catch(ctx)
  712. fz_rethrow(ctx);
  713. return rect;
  714. }
  715. pdf_annot *
  716. pdf_create_annot(fz_context *ctx, pdf_page *page, enum pdf_annot_type type)
  717. {
  718. static const float black[3] = { 0, 0, 0 };
  719. static const float red[3] = { 1, 0, 0 };
  720. static const float green[3] = { 0, 1, 0 };
  721. static const float blue[3] = { 0, 0, 1 };
  722. static const float yellow[3] = { 1, 1, 0 };
  723. static const float magenta[3] = { 1, 0, 1 };
  724. int flags = PDF_ANNOT_IS_PRINT; /* Make printable as default */
  725. pdf_annot *annot = NULL;
  726. fz_var(annot);
  727. pdf_begin_operation(ctx, page->doc, "Create Annotation");
  728. fz_try(ctx)
  729. {
  730. annot = pdf_create_annot_raw(ctx, page, type);
  731. switch (type)
  732. {
  733. default:
  734. break;
  735. case PDF_ANNOT_TEXT:
  736. case PDF_ANNOT_FILE_ATTACHMENT:
  737. case PDF_ANNOT_SOUND:
  738. {
  739. fz_rect icon_rect = { 12, 12, 12+20, 12+20 };
  740. flags = PDF_ANNOT_IS_PRINT | PDF_ANNOT_IS_NO_ZOOM | PDF_ANNOT_IS_NO_ROTATE;
  741. pdf_set_annot_rect(ctx, annot, icon_rect);
  742. pdf_set_annot_color(ctx, annot, 3, yellow);
  743. pdf_set_annot_popup(ctx, annot, fz_make_rect(32, 12, 32+200, 12+100));
  744. }
  745. break;
  746. case PDF_ANNOT_FREE_TEXT:
  747. {
  748. fz_rect text_rect = { 12, 12, 12+200, 12+100 };
  749. /* Use undocumented Adobe property to match page rotation. */
  750. int rot = pdf_dict_get_inheritable_int(ctx, page->obj, PDF_NAME(Rotate));
  751. if (rot != 0)
  752. pdf_dict_put_int(ctx, annot->obj, PDF_NAME(Rotate), rot);
  753. pdf_set_annot_rect(ctx, annot, text_rect);
  754. pdf_set_annot_border_width(ctx, annot, 0);
  755. pdf_set_annot_default_appearance(ctx, annot, "Helv", 12, nelem(black), black);
  756. }
  757. break;
  758. case PDF_ANNOT_STAMP:
  759. {
  760. fz_rect stamp_rect = { 12, 12, 12+190, 12+50 };
  761. pdf_set_annot_rect(ctx, annot, stamp_rect);
  762. pdf_set_annot_color(ctx, annot, 3, red);
  763. pdf_set_annot_icon_name(ctx, annot, "Draft");
  764. }
  765. break;
  766. case PDF_ANNOT_CARET:
  767. {
  768. fz_rect caret_rect = { 12, 12, 12+18, 12+15 };
  769. pdf_set_annot_rect(ctx, annot, caret_rect);
  770. pdf_set_annot_color(ctx, annot, 3, blue);
  771. }
  772. break;
  773. case PDF_ANNOT_LINE:
  774. {
  775. fz_point a = { 12, 12 }, b = { 12 + 100, 12 + 50 };
  776. pdf_set_annot_line(ctx, annot, a, b);
  777. pdf_set_annot_border_width(ctx, annot, 1);
  778. pdf_set_annot_color(ctx, annot, 3, red);
  779. }
  780. break;
  781. case PDF_ANNOT_SQUARE:
  782. case PDF_ANNOT_CIRCLE:
  783. {
  784. fz_rect shape_rect = { 12, 12, 12+100, 12+50 };
  785. fz_rect rd = { 0.5, 0.5, 0.5, 0.5 };
  786. pdf_set_annot_rect(ctx, annot, shape_rect);
  787. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), rd);
  788. pdf_set_annot_border_width(ctx, annot, 1);
  789. pdf_set_annot_color(ctx, annot, 3, red);
  790. }
  791. break;
  792. case PDF_ANNOT_POLYGON:
  793. case PDF_ANNOT_POLY_LINE:
  794. case PDF_ANNOT_INK:
  795. {
  796. fz_rect rd = { 0.5, 0.5, 0.5, 0.5 };
  797. pdf_set_annot_border_width(ctx, annot, 1);
  798. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), rd);
  799. pdf_set_annot_color(ctx, annot, 3, red);
  800. }
  801. break;
  802. case PDF_ANNOT_HIGHLIGHT:
  803. pdf_set_annot_color(ctx, annot, 3, yellow);
  804. break;
  805. case PDF_ANNOT_UNDERLINE:
  806. pdf_set_annot_color(ctx, annot, 3, green);
  807. break;
  808. case PDF_ANNOT_STRIKE_OUT:
  809. pdf_set_annot_color(ctx, annot, 3, red);
  810. break;
  811. case PDF_ANNOT_SQUIGGLY:
  812. pdf_set_annot_color(ctx, annot, 3, magenta);
  813. break;
  814. }
  815. pdf_dict_put(ctx, annot->obj, PDF_NAME(P), page->obj);
  816. pdf_dict_put_int(ctx, annot->obj, PDF_NAME(F), flags);
  817. pdf_end_operation(ctx, page->doc);
  818. }
  819. fz_catch(ctx)
  820. {
  821. pdf_drop_annot(ctx, annot);
  822. pdf_abandon_operation(ctx, page->doc);
  823. fz_rethrow(ctx);
  824. }
  825. return annot;
  826. }
  827. static int
  828. remove_from_tree(fz_context *ctx, pdf_obj *arr, pdf_obj *item, pdf_cycle_list *cycle_up)
  829. {
  830. pdf_cycle_list cycle;
  831. int i, n, res = 0;
  832. if (arr == NULL || pdf_cycle(ctx, &cycle, cycle_up, arr))
  833. return 0;
  834. n = pdf_array_len(ctx, arr);
  835. for (i = 0; i < n; ++i)
  836. {
  837. pdf_obj *obj = pdf_array_get(ctx, arr, i);
  838. if (!pdf_objcmp(ctx, obj, item))
  839. {
  840. pdf_array_delete(ctx, arr, i);
  841. res = 1;
  842. break;
  843. }
  844. if (remove_from_tree(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Kids)), item, &cycle))
  845. {
  846. res = 1;
  847. break;
  848. }
  849. }
  850. return res;
  851. }
  852. void
  853. pdf_delete_annot(fz_context *ctx, pdf_page *page, pdf_annot *annot)
  854. {
  855. pdf_document *doc;
  856. pdf_annot **annotptr;
  857. pdf_obj *annot_arr, *popup;
  858. int i;
  859. int is_widget = 0;
  860. if (annot == NULL || page == NULL || page != annot->page)
  861. return;
  862. doc = page->doc;
  863. /* Look for the annot in the page's list */
  864. for (annotptr = &page->annots; *annotptr; annotptr = &(*annotptr)->next)
  865. {
  866. if (*annotptr == annot)
  867. break;
  868. }
  869. if (*annotptr == NULL)
  870. {
  871. is_widget = 1;
  872. /* Look also in the widget list*/
  873. for (annotptr = &page->widgets; *annotptr; annotptr = &(*annotptr)->next)
  874. {
  875. if (*annotptr == annot)
  876. break;
  877. }
  878. }
  879. /* Check the passed annotation was of this page */
  880. if (*annotptr == NULL)
  881. return;
  882. pdf_begin_operation(ctx, page->doc, "Delete Annotation");
  883. /* Remove annot from page's list */
  884. *annotptr = annot->next;
  885. /* Annot may no longer borrow page pointer, since they are separated */
  886. annot->page = NULL;
  887. /* If the removed annotation was the last in the list adjust the end pointer */
  888. if (*annotptr == NULL)
  889. {
  890. if (is_widget)
  891. page->widget_tailp = annotptr;
  892. else
  893. page->annot_tailp = annotptr;
  894. }
  895. fz_try(ctx)
  896. {
  897. /* Remove the annot from the "Annots" array. */
  898. annot_arr = pdf_dict_get(ctx, page->obj, PDF_NAME(Annots));
  899. i = pdf_array_find(ctx, annot_arr, annot->obj);
  900. if (i >= 0)
  901. pdf_array_delete(ctx, annot_arr, i);
  902. /* Remove the associated Popup annotation from the Annots array */
  903. popup = pdf_dict_get(ctx, annot->obj, PDF_NAME(Popup));
  904. if (popup)
  905. {
  906. i = pdf_array_find(ctx, annot_arr, popup);
  907. if (i >= 0)
  908. pdf_array_delete(ctx, annot_arr, i);
  909. }
  910. /* For a widget, remove also from the AcroForm tree */
  911. if (is_widget)
  912. {
  913. pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root));
  914. pdf_obj *acroform = pdf_dict_get(ctx, root, PDF_NAME(AcroForm));
  915. pdf_obj *fields = pdf_dict_get(ctx, acroform, PDF_NAME(Fields));
  916. (void)remove_from_tree(ctx, fields, annot->obj, NULL);
  917. }
  918. /* The garbage collection pass when saving will remove the annot object,
  919. * removing it here may break files if multiple pages use the same annot. */
  920. pdf_end_operation(ctx, page->doc);
  921. }
  922. fz_always(ctx)
  923. {
  924. /* Drop the reference to annot previously held by the page list. */
  925. pdf_drop_annot(ctx, annot);
  926. }
  927. fz_catch(ctx)
  928. {
  929. pdf_abandon_operation(ctx, page->doc);
  930. fz_rethrow(ctx);
  931. }
  932. }
  933. enum pdf_annot_type
  934. pdf_annot_type(fz_context *ctx, pdf_annot *annot)
  935. {
  936. enum pdf_annot_type ret;
  937. pdf_annot_push_local_xref(ctx, annot);
  938. fz_try(ctx)
  939. {
  940. const char *subtype = pdf_dict_get_name(ctx, annot->obj, PDF_NAME(Subtype));
  941. ret = pdf_annot_type_from_string(ctx, subtype);
  942. }
  943. fz_always(ctx)
  944. pdf_annot_pop_local_xref(ctx, annot);
  945. fz_catch(ctx)
  946. fz_rethrow(ctx);
  947. return ret;
  948. }
  949. int
  950. pdf_annot_flags(fz_context *ctx, pdf_annot *annot)
  951. {
  952. int ret;
  953. pdf_annot_push_local_xref(ctx, annot);
  954. fz_try(ctx)
  955. ret = pdf_dict_get_int(ctx, annot->obj, PDF_NAME(F));
  956. fz_always(ctx)
  957. pdf_annot_pop_local_xref(ctx, annot);
  958. fz_catch(ctx)
  959. fz_rethrow(ctx);
  960. return ret;
  961. }
  962. void
  963. pdf_set_annot_flags(fz_context *ctx, pdf_annot *annot, int flags)
  964. {
  965. begin_annot_op(ctx, annot, "Set flags");
  966. fz_try(ctx)
  967. {
  968. pdf_dict_put_int(ctx, annot->obj, PDF_NAME(F), flags);
  969. end_annot_op(ctx, annot);
  970. }
  971. fz_catch(ctx)
  972. {
  973. abandon_annot_op(ctx, annot);
  974. fz_rethrow(ctx);
  975. }
  976. pdf_dirty_annot(ctx, annot);
  977. }
  978. static pdf_obj *rect_subtypes[] = {
  979. PDF_NAME(Text),
  980. PDF_NAME(FreeText),
  981. PDF_NAME(Square),
  982. PDF_NAME(Circle),
  983. PDF_NAME(Redact),
  984. PDF_NAME(Stamp),
  985. PDF_NAME(Caret),
  986. PDF_NAME(Popup),
  987. PDF_NAME(FileAttachment),
  988. PDF_NAME(Sound),
  989. PDF_NAME(Movie),
  990. PDF_NAME(Widget),
  991. NULL,
  992. };
  993. int
  994. pdf_annot_has_rect(fz_context *ctx, pdf_annot *annot)
  995. {
  996. /* True for annotations where the user can manipulate the size or location
  997. * of the annotation through the Rect.
  998. * False for annotations where the Rect is computed from other
  999. * annotation data such as InkList, QuadPoints, and Vertices.
  1000. */
  1001. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(Rect), rect_subtypes);
  1002. }
  1003. fz_rect
  1004. pdf_annot_rect(fz_context *ctx, pdf_annot *annot)
  1005. {
  1006. fz_matrix page_ctm;
  1007. fz_rect rect;
  1008. fz_rect rd;
  1009. pdf_annot_push_local_xref(ctx, annot);
  1010. fz_try(ctx)
  1011. {
  1012. check_allowed_subtypes(ctx, annot, PDF_NAME(Rect), rect_subtypes);
  1013. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  1014. rect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  1015. /* remove RD adjustment from bounding box to get design box */
  1016. rd = pdf_annot_rect_diff(ctx, annot);
  1017. rect.x0 += rd.x0;
  1018. rect.x1 -= rd.x1;
  1019. rect.y0 += rd.y0;
  1020. rect.y1 -= rd.y1;
  1021. }
  1022. fz_always(ctx)
  1023. pdf_annot_pop_local_xref(ctx, annot);
  1024. fz_catch(ctx)
  1025. fz_rethrow(ctx);
  1026. return fz_transform_rect(rect, page_ctm);
  1027. }
  1028. void
  1029. pdf_set_annot_rect(fz_context *ctx, pdf_annot *annot, fz_rect rect)
  1030. {
  1031. fz_matrix page_ctm, inv_page_ctm;
  1032. fz_rect rd;
  1033. begin_annot_op(ctx, annot, "Set rectangle");
  1034. fz_try(ctx)
  1035. {
  1036. check_allowed_subtypes(ctx, annot, PDF_NAME(Rect), rect_subtypes);
  1037. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  1038. inv_page_ctm = fz_invert_matrix(page_ctm);
  1039. rect = fz_transform_rect(rect, inv_page_ctm);
  1040. /* add RD adjustment to design box to get bounding box */
  1041. rd = pdf_annot_rect_diff(ctx, annot);
  1042. rect.x0 -= rd.x0;
  1043. rect.x1 += rd.x1;
  1044. rect.y0 -= rd.y0;
  1045. rect.y1 += rd.y1;
  1046. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(Rect), rect);
  1047. /* recalculate callout line as necessary */
  1048. if (pdf_annot_has_callout(ctx, annot))
  1049. {
  1050. pdf_set_annot_callout_point(ctx, annot, pdf_annot_callout_point(ctx, annot));
  1051. }
  1052. pdf_dirty_annot(ctx, annot);
  1053. end_annot_op(ctx, annot);
  1054. }
  1055. fz_catch(ctx)
  1056. {
  1057. abandon_annot_op(ctx, annot);
  1058. fz_rethrow(ctx);
  1059. }
  1060. }
  1061. const char *
  1062. pdf_annot_contents(fz_context *ctx, pdf_annot *annot)
  1063. {
  1064. return pdf_dict_get_text_string(ctx, annot->obj, PDF_NAME(Contents));
  1065. }
  1066. void
  1067. pdf_set_annot_contents(fz_context *ctx, pdf_annot *annot, const char *text)
  1068. {
  1069. begin_annot_op(ctx, annot, "Set contents");
  1070. fz_try(ctx)
  1071. {
  1072. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(Contents), text);
  1073. pdf_dict_del(ctx, annot->obj, PDF_NAME(RC)); /* not supported */
  1074. pdf_dirty_annot(ctx, annot);
  1075. end_annot_op(ctx, annot);
  1076. }
  1077. fz_catch(ctx)
  1078. {
  1079. abandon_annot_op(ctx, annot);
  1080. fz_rethrow(ctx);
  1081. }
  1082. }
  1083. static pdf_obj *markup_subtypes[] = {
  1084. PDF_NAME(Text),
  1085. PDF_NAME(FreeText),
  1086. PDF_NAME(Line),
  1087. PDF_NAME(Square),
  1088. PDF_NAME(Circle),
  1089. PDF_NAME(Polygon),
  1090. PDF_NAME(PolyLine),
  1091. PDF_NAME(Highlight),
  1092. PDF_NAME(Underline),
  1093. PDF_NAME(Squiggly),
  1094. PDF_NAME(StrikeOut),
  1095. PDF_NAME(Redact),
  1096. PDF_NAME(Stamp),
  1097. PDF_NAME(Caret),
  1098. PDF_NAME(Ink),
  1099. PDF_NAME(FileAttachment),
  1100. PDF_NAME(Sound),
  1101. NULL,
  1102. };
  1103. int
  1104. pdf_annot_has_rich_contents(fz_context *ctx, pdf_annot *annot)
  1105. {
  1106. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(RC), markup_subtypes);
  1107. }
  1108. const char *
  1109. pdf_annot_rich_contents(fz_context *ctx, pdf_annot *annot)
  1110. {
  1111. check_allowed_subtypes(ctx, annot, PDF_NAME(RC), markup_subtypes);
  1112. return pdf_dict_get_text_string(ctx, annot->obj, PDF_NAME(RC));
  1113. }
  1114. void
  1115. pdf_set_annot_rich_contents(fz_context *ctx, pdf_annot *annot, const char *plain, const char *rich)
  1116. {
  1117. begin_annot_op(ctx, annot, "Set rich contents");
  1118. fz_try(ctx)
  1119. {
  1120. check_allowed_subtypes(ctx, annot, PDF_NAME(RC), markup_subtypes);
  1121. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(Contents), plain);
  1122. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(RC), rich);
  1123. pdf_dirty_annot(ctx, annot);
  1124. end_annot_op(ctx, annot);
  1125. }
  1126. fz_catch(ctx)
  1127. {
  1128. abandon_annot_op(ctx, annot);
  1129. fz_rethrow(ctx);
  1130. }
  1131. }
  1132. static pdf_obj *default_style_subtypes[] = {
  1133. PDF_NAME(FreeText),
  1134. PDF_NAME(Widget),
  1135. NULL,
  1136. };
  1137. int
  1138. pdf_annot_has_rich_defaults(fz_context *ctx, pdf_annot *annot)
  1139. {
  1140. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(DS), default_style_subtypes);
  1141. }
  1142. const char *
  1143. pdf_annot_rich_defaults(fz_context *ctx, pdf_annot *annot)
  1144. {
  1145. check_allowed_subtypes(ctx, annot, PDF_NAME(DS), default_style_subtypes);
  1146. return pdf_dict_get_text_string(ctx, annot->obj, PDF_NAME(DS));
  1147. }
  1148. void
  1149. pdf_set_annot_rich_defaults(fz_context *ctx, pdf_annot *annot, const char *style)
  1150. {
  1151. begin_annot_op(ctx, annot, "Set rich defaults");
  1152. fz_try(ctx)
  1153. {
  1154. check_allowed_subtypes(ctx, annot, PDF_NAME(DS), default_style_subtypes);
  1155. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(DS), style);
  1156. pdf_dirty_annot(ctx, annot);
  1157. end_annot_op(ctx, annot);
  1158. }
  1159. fz_catch(ctx)
  1160. {
  1161. abandon_annot_op(ctx, annot);
  1162. fz_rethrow(ctx);
  1163. }
  1164. }
  1165. int
  1166. pdf_annot_has_open(fz_context *ctx, pdf_annot *annot)
  1167. {
  1168. int ret;
  1169. pdf_annot_push_local_xref(ctx, annot);
  1170. fz_try(ctx)
  1171. {
  1172. pdf_obj *subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  1173. pdf_obj *popup = pdf_dict_get(ctx, annot->obj, PDF_NAME(Popup));
  1174. ret = (subtype == PDF_NAME(Text) || popup);
  1175. }
  1176. fz_always(ctx)
  1177. pdf_annot_pop_local_xref(ctx, annot);
  1178. fz_catch(ctx)
  1179. fz_rethrow(ctx);
  1180. return ret;
  1181. }
  1182. int
  1183. pdf_annot_is_open(fz_context *ctx, pdf_annot *annot)
  1184. {
  1185. int ret = 0;
  1186. pdf_annot_push_local_xref(ctx, annot);
  1187. fz_try(ctx)
  1188. {
  1189. pdf_obj *subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  1190. pdf_obj *popup = pdf_dict_get(ctx, annot->obj, PDF_NAME(Popup));
  1191. if (popup)
  1192. ret = pdf_dict_get_bool(ctx, popup, PDF_NAME(Open));
  1193. else if (subtype == PDF_NAME(Text))
  1194. ret = pdf_dict_get_bool(ctx, annot->obj, PDF_NAME(Open));
  1195. }
  1196. fz_always(ctx)
  1197. pdf_annot_pop_local_xref(ctx, annot);
  1198. fz_catch(ctx)
  1199. fz_rethrow(ctx);
  1200. return ret;
  1201. }
  1202. void
  1203. pdf_set_annot_is_open(fz_context *ctx, pdf_annot *annot, int is_open)
  1204. {
  1205. begin_annot_op(ctx, annot, is_open ? "Open" : "Close");
  1206. fz_try(ctx)
  1207. {
  1208. pdf_obj *subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  1209. pdf_obj *popup = pdf_dict_get(ctx, annot->obj, PDF_NAME(Popup));
  1210. if (popup)
  1211. {
  1212. pdf_dict_put_bool(ctx, popup, PDF_NAME(Open), is_open);
  1213. pdf_dirty_annot(ctx, annot);
  1214. }
  1215. else if (subtype == PDF_NAME(Text))
  1216. {
  1217. pdf_dict_put_bool(ctx, annot->obj, PDF_NAME(Open), is_open);
  1218. pdf_dirty_annot(ctx, annot);
  1219. }
  1220. end_annot_op(ctx, annot);
  1221. }
  1222. fz_catch(ctx)
  1223. {
  1224. abandon_annot_op(ctx, annot);
  1225. fz_rethrow(ctx);
  1226. }
  1227. }
  1228. static pdf_obj *icon_name_subtypes[] = {
  1229. PDF_NAME(FileAttachment),
  1230. PDF_NAME(Sound),
  1231. PDF_NAME(Stamp),
  1232. PDF_NAME(Text),
  1233. NULL,
  1234. };
  1235. int
  1236. pdf_annot_has_icon_name(fz_context *ctx, pdf_annot *annot)
  1237. {
  1238. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(Name), icon_name_subtypes);
  1239. }
  1240. const char *
  1241. pdf_annot_icon_name(fz_context *ctx, pdf_annot *annot)
  1242. {
  1243. const char *ret;
  1244. pdf_obj *name;
  1245. pdf_annot_push_local_xref(ctx, annot);
  1246. fz_try(ctx)
  1247. {
  1248. check_allowed_subtypes(ctx, annot, PDF_NAME(Name), icon_name_subtypes);
  1249. name = pdf_dict_get(ctx, annot->obj, PDF_NAME(Name));
  1250. if (!name)
  1251. {
  1252. pdf_obj *subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  1253. if (pdf_name_eq(ctx, subtype, PDF_NAME(Text)))
  1254. {
  1255. ret = "Note";
  1256. break;
  1257. }
  1258. if (pdf_name_eq(ctx, subtype, PDF_NAME(Stamp)))
  1259. {
  1260. ret = ""; // Should be "Draft" according to spec
  1261. break;
  1262. }
  1263. if (pdf_name_eq(ctx, subtype, PDF_NAME(FileAttachment)))
  1264. {
  1265. ret = "PushPin";
  1266. break;
  1267. }
  1268. if (pdf_name_eq(ctx, subtype, PDF_NAME(Sound)))
  1269. {
  1270. ret = "Speaker";
  1271. break;
  1272. }
  1273. }
  1274. ret = pdf_to_name(ctx, name);
  1275. }
  1276. fz_always(ctx)
  1277. pdf_annot_pop_local_xref(ctx, annot);
  1278. fz_catch(ctx)
  1279. fz_rethrow(ctx);
  1280. return ret;
  1281. }
  1282. void
  1283. pdf_set_annot_icon_name(fz_context *ctx, pdf_annot *annot, const char *name)
  1284. {
  1285. begin_annot_op(ctx, annot, "Set icon name");
  1286. fz_try(ctx)
  1287. {
  1288. check_allowed_subtypes(ctx, annot, PDF_NAME(Name), icon_name_subtypes);
  1289. if (name)
  1290. pdf_dict_put_name(ctx, annot->obj, PDF_NAME(Name), name);
  1291. else
  1292. pdf_dict_del(ctx, annot->obj, PDF_NAME(Name));
  1293. end_annot_op(ctx, annot);
  1294. }
  1295. fz_catch(ctx)
  1296. {
  1297. abandon_annot_op(ctx, annot);
  1298. fz_rethrow(ctx);
  1299. }
  1300. pdf_dirty_annot(ctx, annot);
  1301. }
  1302. int
  1303. pdf_annot_is_standard_stamp(fz_context *ctx, pdf_annot *annot)
  1304. {
  1305. pdf_obj *name = pdf_dict_get(ctx, annot->obj, PDF_NAME(Name));
  1306. if (pdf_name_eq(ctx, name, PDF_NAME(Approved))) return 1;
  1307. else if (pdf_name_eq(ctx, name, PDF_NAME(AsIs))) return 1;
  1308. else if (pdf_name_eq(ctx, name, PDF_NAME(Confidential))) return 1;
  1309. else if (pdf_name_eq(ctx, name, PDF_NAME(Departmental))) return 1;
  1310. else if (pdf_name_eq(ctx, name, PDF_NAME(Draft))) return 1;
  1311. else if (pdf_name_eq(ctx, name, PDF_NAME(Experimental))) return 1;
  1312. else if (pdf_name_eq(ctx, name, PDF_NAME(Expired))) return 1;
  1313. else if (pdf_name_eq(ctx, name, PDF_NAME(Final))) return 1;
  1314. else if (pdf_name_eq(ctx, name, PDF_NAME(ForComment))) return 1;
  1315. else if (pdf_name_eq(ctx, name, PDF_NAME(ForPublicRelease))) return 1;
  1316. else if (pdf_name_eq(ctx, name, PDF_NAME(NotApproved))) return 1;
  1317. else if (pdf_name_eq(ctx, name, PDF_NAME(NotForPublicRelease))) return 1;
  1318. else if (pdf_name_eq(ctx, name, PDF_NAME(Sold))) return 1;
  1319. else if (pdf_name_eq(ctx, name, PDF_NAME(TopSecret))) return 1;
  1320. else if (pdf_annot_stamp_image_obj(ctx, annot) != NULL) return 1;
  1321. else return 0;
  1322. }
  1323. enum pdf_line_ending pdf_line_ending_from_name(fz_context *ctx, pdf_obj *end)
  1324. {
  1325. if (pdf_name_eq(ctx, end, PDF_NAME(None))) return PDF_ANNOT_LE_NONE;
  1326. else if (pdf_name_eq(ctx, end, PDF_NAME(Square))) return PDF_ANNOT_LE_SQUARE;
  1327. else if (pdf_name_eq(ctx, end, PDF_NAME(Circle))) return PDF_ANNOT_LE_CIRCLE;
  1328. else if (pdf_name_eq(ctx, end, PDF_NAME(Diamond))) return PDF_ANNOT_LE_DIAMOND;
  1329. else if (pdf_name_eq(ctx, end, PDF_NAME(OpenArrow))) return PDF_ANNOT_LE_OPEN_ARROW;
  1330. else if (pdf_name_eq(ctx, end, PDF_NAME(ClosedArrow))) return PDF_ANNOT_LE_CLOSED_ARROW;
  1331. else if (pdf_name_eq(ctx, end, PDF_NAME(Butt))) return PDF_ANNOT_LE_BUTT;
  1332. else if (pdf_name_eq(ctx, end, PDF_NAME(ROpenArrow))) return PDF_ANNOT_LE_R_OPEN_ARROW;
  1333. else if (pdf_name_eq(ctx, end, PDF_NAME(RClosedArrow))) return PDF_ANNOT_LE_R_CLOSED_ARROW;
  1334. else if (pdf_name_eq(ctx, end, PDF_NAME(Slash))) return PDF_ANNOT_LE_SLASH;
  1335. else return PDF_ANNOT_LE_NONE;
  1336. }
  1337. enum pdf_line_ending pdf_line_ending_from_string(fz_context *ctx, const char *end)
  1338. {
  1339. if (!strcmp(end, "None")) return PDF_ANNOT_LE_NONE;
  1340. else if (!strcmp(end, "Square")) return PDF_ANNOT_LE_SQUARE;
  1341. else if (!strcmp(end, "Circle")) return PDF_ANNOT_LE_CIRCLE;
  1342. else if (!strcmp(end, "Diamond")) return PDF_ANNOT_LE_DIAMOND;
  1343. else if (!strcmp(end, "OpenArrow")) return PDF_ANNOT_LE_OPEN_ARROW;
  1344. else if (!strcmp(end, "ClosedArrow")) return PDF_ANNOT_LE_CLOSED_ARROW;
  1345. else if (!strcmp(end, "Butt")) return PDF_ANNOT_LE_BUTT;
  1346. else if (!strcmp(end, "ROpenArrow")) return PDF_ANNOT_LE_R_OPEN_ARROW;
  1347. else if (!strcmp(end, "RClosedArrow")) return PDF_ANNOT_LE_R_CLOSED_ARROW;
  1348. else if (!strcmp(end, "Slash")) return PDF_ANNOT_LE_SLASH;
  1349. else return PDF_ANNOT_LE_NONE;
  1350. }
  1351. pdf_obj *pdf_name_from_line_ending(fz_context *ctx, enum pdf_line_ending end)
  1352. {
  1353. switch (end)
  1354. {
  1355. default:
  1356. case PDF_ANNOT_LE_NONE: return PDF_NAME(None);
  1357. case PDF_ANNOT_LE_SQUARE: return PDF_NAME(Square);
  1358. case PDF_ANNOT_LE_CIRCLE: return PDF_NAME(Circle);
  1359. case PDF_ANNOT_LE_DIAMOND: return PDF_NAME(Diamond);
  1360. case PDF_ANNOT_LE_OPEN_ARROW: return PDF_NAME(OpenArrow);
  1361. case PDF_ANNOT_LE_CLOSED_ARROW: return PDF_NAME(ClosedArrow);
  1362. case PDF_ANNOT_LE_BUTT: return PDF_NAME(Butt);
  1363. case PDF_ANNOT_LE_R_OPEN_ARROW: return PDF_NAME(ROpenArrow);
  1364. case PDF_ANNOT_LE_R_CLOSED_ARROW: return PDF_NAME(RClosedArrow);
  1365. case PDF_ANNOT_LE_SLASH: return PDF_NAME(Slash);
  1366. }
  1367. }
  1368. const char *pdf_string_from_line_ending(fz_context *ctx, enum pdf_line_ending end)
  1369. {
  1370. switch (end)
  1371. {
  1372. default:
  1373. case PDF_ANNOT_LE_NONE: return "None";
  1374. case PDF_ANNOT_LE_SQUARE: return "Square";
  1375. case PDF_ANNOT_LE_CIRCLE: return "Circle";
  1376. case PDF_ANNOT_LE_DIAMOND: return "Diamond";
  1377. case PDF_ANNOT_LE_OPEN_ARROW: return "OpenArrow";
  1378. case PDF_ANNOT_LE_CLOSED_ARROW: return "ClosedArrow";
  1379. case PDF_ANNOT_LE_BUTT: return "Butt";
  1380. case PDF_ANNOT_LE_R_OPEN_ARROW: return "ROpenArrow";
  1381. case PDF_ANNOT_LE_R_CLOSED_ARROW: return "RClosedArrow";
  1382. case PDF_ANNOT_LE_SLASH: return "Slash";
  1383. }
  1384. }
  1385. static pdf_obj *line_ending_subtypes[] = {
  1386. PDF_NAME(FreeText),
  1387. PDF_NAME(Line),
  1388. PDF_NAME(PolyLine),
  1389. PDF_NAME(Polygon),
  1390. NULL,
  1391. };
  1392. int
  1393. pdf_annot_has_line_ending_styles(fz_context *ctx, pdf_annot *annot)
  1394. {
  1395. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(LE), line_ending_subtypes);
  1396. }
  1397. void
  1398. pdf_annot_line_ending_styles(fz_context *ctx, pdf_annot *annot,
  1399. enum pdf_line_ending *start_style,
  1400. enum pdf_line_ending *end_style)
  1401. {
  1402. pdf_obj *style;
  1403. pdf_annot_push_local_xref(ctx, annot);
  1404. fz_try(ctx)
  1405. {
  1406. check_allowed_subtypes(ctx, annot, PDF_NAME(LE), line_ending_subtypes);
  1407. style = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  1408. *start_style = pdf_line_ending_from_name(ctx, pdf_array_get(ctx, style, 0));
  1409. *end_style = pdf_line_ending_from_name(ctx, pdf_array_get(ctx, style, 1));
  1410. }
  1411. fz_always(ctx)
  1412. pdf_annot_pop_local_xref(ctx, annot);
  1413. fz_catch(ctx)
  1414. fz_rethrow(ctx);
  1415. }
  1416. enum pdf_line_ending
  1417. pdf_annot_line_start_style(fz_context *ctx, pdf_annot *annot)
  1418. {
  1419. pdf_obj *le = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  1420. return pdf_line_ending_from_name(ctx, pdf_array_get(ctx, le, 0));
  1421. }
  1422. enum pdf_line_ending
  1423. pdf_annot_line_end_style(fz_context *ctx, pdf_annot *annot)
  1424. {
  1425. pdf_obj *le = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  1426. return pdf_line_ending_from_name(ctx, pdf_array_get(ctx, le, 1));
  1427. }
  1428. void
  1429. pdf_set_annot_line_ending_styles(fz_context *ctx, pdf_annot *annot,
  1430. enum pdf_line_ending start_style,
  1431. enum pdf_line_ending end_style)
  1432. {
  1433. pdf_obj *style;
  1434. begin_annot_op(ctx, annot, "Set line endings");
  1435. fz_try(ctx)
  1436. {
  1437. check_allowed_subtypes(ctx, annot, PDF_NAME(LE), line_ending_subtypes);
  1438. style = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(LE), 2);
  1439. pdf_array_put_drop(ctx, style, 0, pdf_name_from_line_ending(ctx, start_style));
  1440. pdf_array_put_drop(ctx, style, 1, pdf_name_from_line_ending(ctx, end_style));
  1441. end_annot_op(ctx, annot);
  1442. }
  1443. fz_catch(ctx)
  1444. {
  1445. abandon_annot_op(ctx, annot);
  1446. fz_rethrow(ctx);
  1447. }
  1448. pdf_dirty_annot(ctx, annot);
  1449. }
  1450. void
  1451. pdf_set_annot_line_start_style(fz_context *ctx, pdf_annot *annot, enum pdf_line_ending s)
  1452. {
  1453. enum pdf_line_ending e = pdf_annot_line_end_style(ctx, annot);
  1454. pdf_set_annot_line_ending_styles(ctx, annot, s, e);
  1455. }
  1456. void
  1457. pdf_set_annot_line_end_style(fz_context *ctx, pdf_annot *annot, enum pdf_line_ending e)
  1458. {
  1459. enum pdf_line_ending s = pdf_annot_line_start_style(ctx, annot);
  1460. pdf_set_annot_line_ending_styles(ctx, annot, s, e);
  1461. }
  1462. static pdf_obj *border_style_subtypes[] = {
  1463. PDF_NAME(Circle),
  1464. PDF_NAME(FreeText),
  1465. PDF_NAME(Ink),
  1466. PDF_NAME(Line),
  1467. PDF_NAME(Polygon),
  1468. PDF_NAME(PolyLine),
  1469. PDF_NAME(Square),
  1470. PDF_NAME(Widget),
  1471. NULL,
  1472. };
  1473. static pdf_obj *border_effect_subtypes[] = {
  1474. PDF_NAME(Circle),
  1475. PDF_NAME(FreeText),
  1476. PDF_NAME(Polygon),
  1477. PDF_NAME(Square),
  1478. NULL,
  1479. };
  1480. int
  1481. pdf_annot_has_border(fz_context *ctx, pdf_annot *annot)
  1482. {
  1483. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1484. }
  1485. int
  1486. pdf_annot_has_border_effect(fz_context *ctx, pdf_annot *annot)
  1487. {
  1488. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(BE), border_effect_subtypes);
  1489. }
  1490. enum pdf_border_style
  1491. pdf_annot_border_style(fz_context *ctx, pdf_annot *annot)
  1492. {
  1493. pdf_obj *bs, *s;
  1494. enum pdf_border_style style;
  1495. pdf_annot_push_local_xref(ctx, annot);
  1496. fz_try(ctx)
  1497. {
  1498. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1499. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1500. s = pdf_dict_get(ctx, bs, PDF_NAME(S));
  1501. if (s == PDF_NAME(D))
  1502. style = PDF_BORDER_STYLE_DASHED;
  1503. else if (s == PDF_NAME(B))
  1504. style = PDF_BORDER_STYLE_BEVELED;
  1505. else if (s == PDF_NAME(I))
  1506. style = PDF_BORDER_STYLE_INSET;
  1507. else if (s == PDF_NAME(U))
  1508. style = PDF_BORDER_STYLE_UNDERLINE;
  1509. else
  1510. style = PDF_BORDER_STYLE_SOLID;
  1511. }
  1512. fz_always(ctx)
  1513. pdf_annot_pop_local_xref(ctx, annot);
  1514. fz_catch(ctx)
  1515. fz_rethrow(ctx);
  1516. return style;
  1517. }
  1518. float
  1519. pdf_annot_border_width(fz_context *ctx, pdf_annot *annot)
  1520. {
  1521. pdf_obj *bs, *bs_w, *border;
  1522. float ret = 1;
  1523. pdf_annot_push_local_xref(ctx, annot);
  1524. fz_try(ctx)
  1525. {
  1526. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1527. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1528. bs_w = pdf_dict_get(ctx, bs, PDF_NAME(W));
  1529. if (pdf_is_number(ctx, bs_w))
  1530. {
  1531. ret = pdf_to_real(ctx, bs_w);
  1532. break;
  1533. }
  1534. border = pdf_dict_get(ctx, annot->obj, PDF_NAME(Border));
  1535. bs_w = pdf_array_get(ctx, border, 2);
  1536. if (pdf_is_number(ctx, bs_w))
  1537. ret = pdf_to_real(ctx, bs_w);
  1538. }
  1539. fz_always(ctx)
  1540. pdf_annot_pop_local_xref(ctx, annot);
  1541. fz_catch(ctx)
  1542. fz_rethrow(ctx);
  1543. return ret;
  1544. }
  1545. float
  1546. pdf_annot_border(fz_context *ctx, pdf_annot *annot)
  1547. {
  1548. /* DEPRECATED */
  1549. return pdf_annot_border_width(ctx, annot);
  1550. }
  1551. int
  1552. pdf_annot_border_dash_count(fz_context *ctx, pdf_annot *annot)
  1553. {
  1554. pdf_obj *bs, *d, *border;
  1555. int count;
  1556. pdf_annot_push_local_xref(ctx, annot);
  1557. fz_try(ctx)
  1558. {
  1559. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1560. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1561. d = pdf_dict_get(ctx, bs, PDF_NAME(D));
  1562. /* Query legacy dash pattern as a fallback */
  1563. border = pdf_dict_get(ctx, annot->obj, PDF_NAME(Border));
  1564. if (!pdf_is_array(ctx, d) && pdf_is_array(ctx, border))
  1565. d = pdf_array_get(ctx, border, 3);
  1566. count = pdf_array_len(ctx, d);
  1567. }
  1568. fz_always(ctx)
  1569. pdf_annot_pop_local_xref(ctx, annot);
  1570. fz_catch(ctx)
  1571. fz_rethrow(ctx);
  1572. return count;
  1573. }
  1574. float
  1575. pdf_annot_border_dash_item(fz_context *ctx, pdf_annot *annot, int i)
  1576. {
  1577. pdf_obj *bs, *d, *border;
  1578. float length;
  1579. pdf_annot_push_local_xref(ctx, annot);
  1580. fz_try(ctx)
  1581. {
  1582. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1583. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1584. d = pdf_dict_get(ctx, bs, PDF_NAME(D));
  1585. /* Query legacy dash pattern as a fallback */
  1586. border = pdf_dict_get(ctx, annot->obj, PDF_NAME(Border));
  1587. if (!pdf_is_array(ctx, d) && pdf_is_array(ctx, border))
  1588. d = pdf_array_get(ctx, border, 3);
  1589. length = pdf_array_get_real(ctx, d, i);
  1590. }
  1591. fz_always(ctx)
  1592. pdf_annot_pop_local_xref(ctx, annot);
  1593. fz_catch(ctx)
  1594. fz_rethrow(ctx);
  1595. return length;
  1596. }
  1597. enum pdf_border_effect
  1598. pdf_annot_border_effect(fz_context *ctx, pdf_annot *annot)
  1599. {
  1600. pdf_obj *be;
  1601. enum pdf_border_effect effect;
  1602. pdf_annot_push_local_xref(ctx, annot);
  1603. fz_try(ctx)
  1604. {
  1605. check_allowed_subtypes(ctx, annot, PDF_NAME(BE), border_effect_subtypes);
  1606. be = pdf_dict_get(ctx, annot->obj, PDF_NAME(BE));
  1607. if (pdf_dict_get(ctx, be, PDF_NAME(S)) == PDF_NAME(C))
  1608. effect = PDF_BORDER_EFFECT_CLOUDY;
  1609. else
  1610. effect = PDF_BORDER_EFFECT_NONE;
  1611. }
  1612. fz_always(ctx)
  1613. pdf_annot_pop_local_xref(ctx, annot);
  1614. fz_catch(ctx)
  1615. fz_rethrow(ctx);
  1616. return effect;
  1617. }
  1618. float
  1619. pdf_annot_border_effect_intensity(fz_context *ctx, pdf_annot *annot)
  1620. {
  1621. pdf_obj *be;
  1622. float intensity;
  1623. pdf_annot_push_local_xref(ctx, annot);
  1624. fz_try(ctx)
  1625. {
  1626. check_allowed_subtypes(ctx, annot, PDF_NAME(BE), border_effect_subtypes);
  1627. be = pdf_dict_get(ctx, annot->obj, PDF_NAME(BE));
  1628. intensity = pdf_dict_get_real(ctx, be, PDF_NAME(I));
  1629. }
  1630. fz_always(ctx)
  1631. pdf_annot_pop_local_xref(ctx, annot);
  1632. fz_catch(ctx)
  1633. fz_rethrow(ctx);
  1634. return intensity;
  1635. }
  1636. void
  1637. pdf_set_annot_border_width(fz_context *ctx, pdf_annot *annot, float width)
  1638. {
  1639. pdf_obj *bs;
  1640. pdf_obj *type;
  1641. float old_width, adj;
  1642. pdf_obj *rectobj;
  1643. begin_annot_op(ctx, annot, "Set border width");
  1644. fz_try(ctx)
  1645. {
  1646. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1647. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1648. if (!pdf_is_dict(ctx, bs))
  1649. bs = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(BS), 1);
  1650. pdf_dict_put(ctx, bs, PDF_NAME(Type), PDF_NAME(Border));
  1651. old_width = pdf_dict_get_real(ctx, bs, PDF_NAME(W));
  1652. pdf_dict_put_real(ctx, bs, PDF_NAME(W), width);
  1653. rectobj = pdf_dict_get(ctx, annot->obj, PDF_NAME(Rect));
  1654. if (pdf_is_array(ctx, rectobj)) {
  1655. fz_rect rect = pdf_to_rect(ctx, rectobj);
  1656. adj = (width - old_width)/2;
  1657. rect.x0 -= adj;
  1658. rect.x1 += adj;
  1659. rect.y0 -= adj;
  1660. rect.y1 += adj;
  1661. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(Rect), rect);
  1662. /* For any of these types, we want to adjust the Rect and RD
  1663. * together so that Rect+RD doesn't change, but the border stroke
  1664. * stays centred on the same point. */
  1665. type = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  1666. if (pdf_name_eq(ctx, type, PDF_NAME(Square)) ||
  1667. pdf_name_eq(ctx, type, PDF_NAME(Circle)))
  1668. {
  1669. fz_rect rd = pdf_annot_rect_diff(ctx, annot);
  1670. rd.x0 += adj;
  1671. rd.x1 += adj;
  1672. rd.y0 += adj;
  1673. rd.y1 += adj;
  1674. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), rd);
  1675. }
  1676. }
  1677. pdf_dict_del(ctx, annot->obj, PDF_NAME(Border)); /* deprecated */
  1678. end_annot_op(ctx, annot);
  1679. }
  1680. fz_catch(ctx)
  1681. {
  1682. abandon_annot_op(ctx, annot);
  1683. fz_rethrow(ctx);
  1684. }
  1685. pdf_dirty_annot(ctx, annot);
  1686. }
  1687. void
  1688. pdf_set_annot_border(fz_context *ctx, pdf_annot *annot, float w)
  1689. {
  1690. /* DEPRECATED */
  1691. pdf_set_annot_border_width(ctx, annot, w);
  1692. }
  1693. void
  1694. pdf_set_annot_border_style(fz_context *ctx, pdf_annot *annot, enum pdf_border_style style)
  1695. {
  1696. pdf_obj *bs, *s;
  1697. begin_annot_op(ctx, annot, "Set border style");
  1698. fz_try(ctx)
  1699. {
  1700. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1701. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1702. if (!pdf_is_dict(ctx, bs))
  1703. bs = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(BS), 1);
  1704. pdf_dict_put(ctx, bs, PDF_NAME(Type), PDF_NAME(Border));
  1705. switch (style)
  1706. {
  1707. default:
  1708. case PDF_BORDER_STYLE_SOLID: s = PDF_NAME(S); break;
  1709. case PDF_BORDER_STYLE_DASHED: s = PDF_NAME(D); break;
  1710. case PDF_BORDER_STYLE_BEVELED: s = PDF_NAME(B); break;
  1711. case PDF_BORDER_STYLE_INSET: s = PDF_NAME(I); break;
  1712. case PDF_BORDER_STYLE_UNDERLINE: s = PDF_NAME(U); break;
  1713. }
  1714. pdf_dict_put(ctx, bs, PDF_NAME(S), s);
  1715. end_annot_op(ctx, annot);
  1716. }
  1717. fz_catch(ctx)
  1718. {
  1719. abandon_annot_op(ctx, annot);
  1720. fz_rethrow(ctx);
  1721. }
  1722. pdf_dirty_annot(ctx, annot);
  1723. }
  1724. void
  1725. pdf_clear_annot_border_dash(fz_context *ctx, pdf_annot *annot)
  1726. {
  1727. pdf_obj *bs, *border;
  1728. begin_annot_op(ctx, annot, "Clear border dash pattern");
  1729. fz_try(ctx)
  1730. {
  1731. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1732. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1733. if (!pdf_is_dict(ctx, bs))
  1734. bs = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(BS), 1);
  1735. pdf_dict_del(ctx, bs, PDF_NAME(D));
  1736. /* Remove legacy dash pattern */
  1737. border = pdf_dict_get(ctx, annot->obj, PDF_NAME(Border));
  1738. if (pdf_is_array(ctx, border))
  1739. pdf_array_delete(ctx, border, 3);
  1740. end_annot_op(ctx, annot);
  1741. }
  1742. fz_catch(ctx)
  1743. {
  1744. abandon_annot_op(ctx, annot);
  1745. fz_rethrow(ctx);
  1746. }
  1747. pdf_dirty_annot(ctx, annot);
  1748. }
  1749. void
  1750. pdf_add_annot_border_dash_item(fz_context *ctx, pdf_annot *annot, float length)
  1751. {
  1752. pdf_obj *bs, *d, *border;
  1753. begin_annot_op(ctx, annot, "Add border dash pattern item");
  1754. fz_try(ctx)
  1755. {
  1756. check_allowed_subtypes(ctx, annot, PDF_NAME(BS), border_style_subtypes);
  1757. bs = pdf_dict_get(ctx, annot->obj, PDF_NAME(BS));
  1758. if (!pdf_is_dict(ctx, bs))
  1759. bs = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(BS), 1);
  1760. d = pdf_dict_get(ctx, bs, PDF_NAME(D));
  1761. if (!pdf_is_array(ctx, d))
  1762. d = pdf_dict_put_array(ctx, bs, PDF_NAME(D), 1);
  1763. pdf_array_push_real(ctx, d, length);
  1764. /* Remove legacy dash pattern */
  1765. border = pdf_dict_get(ctx, annot->obj, PDF_NAME(Border));
  1766. if (pdf_is_array(ctx, border))
  1767. pdf_array_delete(ctx, border, 3);
  1768. end_annot_op(ctx, annot);
  1769. }
  1770. fz_catch(ctx)
  1771. {
  1772. abandon_annot_op(ctx, annot);
  1773. fz_rethrow(ctx);
  1774. }
  1775. pdf_dirty_annot(ctx, annot);
  1776. }
  1777. void
  1778. pdf_set_annot_border_effect(fz_context *ctx, pdf_annot *annot, enum pdf_border_effect effect)
  1779. {
  1780. pdf_obj *be, *s;
  1781. begin_annot_op(ctx, annot, "Set border effect");
  1782. fz_try(ctx)
  1783. {
  1784. check_allowed_subtypes(ctx, annot, PDF_NAME(BE), border_effect_subtypes);
  1785. be = pdf_dict_get(ctx, annot->obj, PDF_NAME(BE));
  1786. if (!pdf_is_dict(ctx, be))
  1787. be = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(BE), 1);
  1788. switch (effect)
  1789. {
  1790. default:
  1791. case PDF_BORDER_EFFECT_NONE: s = PDF_NAME(S); break;
  1792. case PDF_BORDER_EFFECT_CLOUDY: s = PDF_NAME(C); break;
  1793. }
  1794. pdf_dict_put(ctx, be, PDF_NAME(S), s);
  1795. end_annot_op(ctx, annot);
  1796. }
  1797. fz_catch(ctx)
  1798. {
  1799. abandon_annot_op(ctx, annot);
  1800. fz_rethrow(ctx);
  1801. }
  1802. pdf_dirty_annot(ctx, annot);
  1803. }
  1804. void
  1805. pdf_set_annot_border_effect_intensity(fz_context *ctx, pdf_annot *annot, float intensity)
  1806. {
  1807. pdf_obj *be;
  1808. begin_annot_op(ctx, annot, "Set border effect intensity");
  1809. fz_try(ctx)
  1810. {
  1811. check_allowed_subtypes(ctx, annot, PDF_NAME(BE), border_effect_subtypes);
  1812. be = pdf_dict_get(ctx, annot->obj, PDF_NAME(BE));
  1813. if (!pdf_is_dict(ctx, be))
  1814. be = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(BE), 1);
  1815. pdf_dict_put_real(ctx, be, PDF_NAME(I), intensity);
  1816. end_annot_op(ctx, annot);
  1817. }
  1818. fz_catch(ctx)
  1819. {
  1820. abandon_annot_op(ctx, annot);
  1821. fz_rethrow(ctx);
  1822. }
  1823. pdf_dirty_annot(ctx, annot);
  1824. }
  1825. fz_text_language
  1826. pdf_document_language(fz_context *ctx, pdf_document *doc)
  1827. {
  1828. pdf_obj *trailer = pdf_trailer(ctx, doc);
  1829. pdf_obj *root = pdf_dict_get(ctx, trailer, PDF_NAME(Root));
  1830. pdf_obj *lang = pdf_dict_get(ctx, root, PDF_NAME(Lang));
  1831. return fz_text_language_from_string(pdf_to_text_string(ctx, lang));
  1832. }
  1833. void pdf_set_document_language(fz_context *ctx, pdf_document *doc, fz_text_language lang)
  1834. {
  1835. pdf_obj *trailer = pdf_trailer(ctx, doc);
  1836. pdf_obj *root = pdf_dict_get(ctx, trailer, PDF_NAME(Root));
  1837. char buf[8];
  1838. if (lang == FZ_LANG_UNSET)
  1839. pdf_dict_del(ctx, root, PDF_NAME(Lang));
  1840. else
  1841. pdf_dict_put_text_string(ctx, root, PDF_NAME(Lang), fz_string_from_text_language(buf, lang));
  1842. }
  1843. fz_text_language
  1844. pdf_annot_language(fz_context *ctx, pdf_annot *annot)
  1845. {
  1846. fz_text_language ret;
  1847. pdf_annot_push_local_xref(ctx, annot);
  1848. fz_try(ctx)
  1849. {
  1850. pdf_obj *lang = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(Lang));
  1851. if (lang)
  1852. ret = fz_text_language_from_string(pdf_to_str_buf(ctx, lang));
  1853. else
  1854. ret = pdf_document_language(ctx, annot->page->doc);
  1855. }
  1856. fz_always(ctx)
  1857. pdf_annot_pop_local_xref(ctx, annot);
  1858. fz_catch(ctx)
  1859. fz_rethrow(ctx);
  1860. return ret;
  1861. }
  1862. void
  1863. pdf_set_annot_language(fz_context *ctx, pdf_annot *annot, fz_text_language lang)
  1864. {
  1865. char buf[8];
  1866. begin_annot_op(ctx, annot, "Set language");
  1867. fz_try(ctx)
  1868. {
  1869. if (lang == FZ_LANG_UNSET)
  1870. pdf_dict_del(ctx, annot->obj, PDF_NAME(Lang));
  1871. else
  1872. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(Lang), fz_string_from_text_language(buf, lang));
  1873. end_annot_op(ctx, annot);
  1874. }
  1875. fz_catch(ctx)
  1876. {
  1877. abandon_annot_op(ctx, annot);
  1878. fz_rethrow(ctx);
  1879. }
  1880. pdf_dirty_annot(ctx, annot);
  1881. }
  1882. static pdf_obj *quadding_subtypes[] = {
  1883. PDF_NAME(FreeText),
  1884. PDF_NAME(Widget),
  1885. NULL,
  1886. };
  1887. int
  1888. pdf_annot_has_quadding(fz_context *ctx, pdf_annot *annot)
  1889. {
  1890. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(Q), quadding_subtypes);
  1891. }
  1892. int
  1893. pdf_annot_quadding(fz_context *ctx, pdf_annot *annot)
  1894. {
  1895. int q;
  1896. pdf_annot_push_local_xref(ctx, annot);
  1897. fz_try(ctx)
  1898. {
  1899. check_allowed_subtypes(ctx, annot, PDF_NAME(Q), quadding_subtypes);
  1900. q = pdf_dict_get_int(ctx, annot->obj, PDF_NAME(Q));
  1901. q = (q < 0 || q > 2) ? 0 : q;
  1902. }
  1903. fz_always(ctx)
  1904. pdf_annot_pop_local_xref(ctx, annot);
  1905. fz_catch(ctx)
  1906. fz_rethrow(ctx);
  1907. return q;
  1908. }
  1909. void
  1910. pdf_set_annot_quadding(fz_context *ctx, pdf_annot *annot, int q)
  1911. {
  1912. q = (q < 0 || q > 2) ? 0 : q;
  1913. begin_annot_op(ctx, annot, "Set quadding");
  1914. fz_try(ctx)
  1915. {
  1916. check_allowed_subtypes(ctx, annot, PDF_NAME(Q), quadding_subtypes);
  1917. pdf_dict_put_int(ctx, annot->obj, PDF_NAME(Q), q);
  1918. end_annot_op(ctx, annot);
  1919. }
  1920. fz_catch(ctx)
  1921. {
  1922. abandon_annot_op(ctx, annot);
  1923. fz_rethrow(ctx);
  1924. }
  1925. pdf_dirty_annot(ctx, annot);
  1926. }
  1927. float pdf_annot_opacity(fz_context *ctx, pdf_annot *annot)
  1928. {
  1929. float ret;
  1930. pdf_annot_push_local_xref(ctx, annot);
  1931. fz_try(ctx)
  1932. ret = pdf_dict_get_real_default(ctx, annot->obj, PDF_NAME(CA), 1);
  1933. fz_always(ctx)
  1934. pdf_annot_pop_local_xref(ctx, annot);
  1935. fz_catch(ctx)
  1936. fz_rethrow(ctx);
  1937. return ret;
  1938. }
  1939. void pdf_set_annot_opacity(fz_context *ctx, pdf_annot *annot, float opacity)
  1940. {
  1941. begin_annot_op(ctx, annot, "Set opacity");
  1942. fz_try(ctx)
  1943. {
  1944. if (opacity != 1)
  1945. pdf_dict_put_real(ctx, annot->obj, PDF_NAME(CA), opacity);
  1946. else
  1947. pdf_dict_del(ctx, annot->obj, PDF_NAME(CA));
  1948. end_annot_op(ctx, annot);
  1949. }
  1950. fz_catch(ctx)
  1951. {
  1952. abandon_annot_op(ctx, annot);
  1953. fz_rethrow(ctx);
  1954. }
  1955. pdf_dirty_annot(ctx, annot);
  1956. }
  1957. static void pdf_annot_color_imp(fz_context *ctx, pdf_obj *arr, int *n, float color[4])
  1958. {
  1959. switch (pdf_array_len(ctx, arr))
  1960. {
  1961. case 0:
  1962. if (n)
  1963. *n = 0;
  1964. break;
  1965. case 1:
  1966. case 2:
  1967. if (n)
  1968. *n = 1;
  1969. if (color)
  1970. color[0] = pdf_array_get_real(ctx, arr, 0);
  1971. break;
  1972. case 3:
  1973. if (n)
  1974. *n = 3;
  1975. if (color)
  1976. {
  1977. color[0] = pdf_array_get_real(ctx, arr, 0);
  1978. color[1] = pdf_array_get_real(ctx, arr, 1);
  1979. color[2] = pdf_array_get_real(ctx, arr, 2);
  1980. }
  1981. break;
  1982. case 4:
  1983. default:
  1984. if (n)
  1985. *n = 4;
  1986. if (color)
  1987. {
  1988. color[0] = pdf_array_get_real(ctx, arr, 0);
  1989. color[1] = pdf_array_get_real(ctx, arr, 1);
  1990. color[2] = pdf_array_get_real(ctx, arr, 2);
  1991. color[3] = pdf_array_get_real(ctx, arr, 3);
  1992. }
  1993. break;
  1994. }
  1995. }
  1996. static int pdf_annot_color_rgb(fz_context *ctx, pdf_obj *arr, float rgb[3])
  1997. {
  1998. float color[4];
  1999. int n;
  2000. pdf_annot_color_imp(ctx, arr, &n, color);
  2001. if (n == 0)
  2002. {
  2003. return 0;
  2004. }
  2005. else if (n == 1)
  2006. {
  2007. rgb[0] = rgb[1] = rgb[2] = color[0];
  2008. }
  2009. else if (n == 3)
  2010. {
  2011. rgb[0] = color[0];
  2012. rgb[1] = color[1];
  2013. rgb[2] = color[2];
  2014. }
  2015. else if (n == 4)
  2016. {
  2017. rgb[0] = 1 - fz_min(1, color[0] + color[3]);
  2018. rgb[1] = 1 - fz_min(1, color[1] + color[3]);
  2019. rgb[2] = 1 - fz_min(1, color[2] + color[3]);
  2020. }
  2021. return 1;
  2022. }
  2023. static void pdf_set_annot_color_imp(fz_context *ctx, pdf_annot *annot, pdf_obj *key, int n, const float *color, pdf_obj **allowed)
  2024. {
  2025. pdf_obj *arr;
  2026. if (allowed)
  2027. check_allowed_subtypes(ctx, annot, key, allowed);
  2028. if (n != 0 && n != 1 && n != 3 && n != 4)
  2029. fz_throw(ctx, FZ_ERROR_ARGUMENT, "color must be 0, 1, 3 or 4 components");
  2030. if (!color)
  2031. fz_throw(ctx, FZ_ERROR_ARGUMENT, "no color given");
  2032. arr = pdf_dict_put_array(ctx, annot->obj, key, n);
  2033. fz_try(ctx)
  2034. {
  2035. switch (n)
  2036. {
  2037. case 1:
  2038. pdf_array_push_real(ctx, arr, color[0]);
  2039. break;
  2040. case 3:
  2041. pdf_array_push_real(ctx, arr, color[0]);
  2042. pdf_array_push_real(ctx, arr, color[1]);
  2043. pdf_array_push_real(ctx, arr, color[2]);
  2044. break;
  2045. case 4:
  2046. pdf_array_push_real(ctx, arr, color[0]);
  2047. pdf_array_push_real(ctx, arr, color[1]);
  2048. pdf_array_push_real(ctx, arr, color[2]);
  2049. pdf_array_push_real(ctx, arr, color[3]);
  2050. break;
  2051. }
  2052. }
  2053. fz_catch(ctx)
  2054. fz_rethrow(ctx);
  2055. pdf_dirty_annot(ctx, annot);
  2056. }
  2057. static void
  2058. do_pdf_annot_color(fz_context *ctx, pdf_annot *annot, int *n, float color[4], pdf_obj *name)
  2059. {
  2060. pdf_annot_push_local_xref(ctx, annot);
  2061. fz_try(ctx)
  2062. {
  2063. pdf_obj *c = pdf_dict_get(ctx, annot->obj, name);
  2064. pdf_annot_color_imp(ctx, c, n, color);
  2065. }
  2066. fz_always(ctx)
  2067. pdf_annot_pop_local_xref(ctx, annot);
  2068. fz_catch(ctx)
  2069. fz_rethrow(ctx);
  2070. }
  2071. void
  2072. pdf_annot_color(fz_context *ctx, pdf_annot *annot, int *n, float color[4])
  2073. {
  2074. do_pdf_annot_color(ctx, annot, n, color, PDF_NAME(C));
  2075. }
  2076. void
  2077. pdf_annot_MK_BG(fz_context *ctx, pdf_annot *annot, int *n, float color[4])
  2078. {
  2079. pdf_annot_push_local_xref(ctx, annot);
  2080. fz_try(ctx)
  2081. {
  2082. pdf_obj *mk_bg = pdf_dict_get(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(BG));
  2083. pdf_annot_color_imp(ctx, mk_bg, n, color);
  2084. }
  2085. fz_always(ctx)
  2086. pdf_annot_pop_local_xref(ctx, annot);
  2087. fz_catch(ctx)
  2088. fz_rethrow(ctx);
  2089. }
  2090. int
  2091. pdf_annot_MK_BG_rgb(fz_context *ctx, pdf_annot *annot, float rgb[3])
  2092. {
  2093. int ret;
  2094. pdf_annot_push_local_xref(ctx, annot);
  2095. fz_try(ctx)
  2096. {
  2097. pdf_obj *mk_bg = pdf_dict_get(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(BG));
  2098. ret = pdf_annot_color_rgb(ctx, mk_bg, rgb);
  2099. }
  2100. fz_always(ctx)
  2101. pdf_annot_pop_local_xref(ctx, annot);
  2102. fz_catch(ctx)
  2103. fz_rethrow(ctx);
  2104. return ret;
  2105. }
  2106. void
  2107. pdf_annot_MK_BC(fz_context *ctx, pdf_annot *annot, int *n, float color[4])
  2108. {
  2109. pdf_annot_push_local_xref(ctx, annot);
  2110. fz_try(ctx)
  2111. {
  2112. pdf_obj *mk_bc = pdf_dict_get(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(BC));
  2113. pdf_annot_color_imp(ctx, mk_bc, n, color);
  2114. }
  2115. fz_always(ctx)
  2116. pdf_annot_pop_local_xref(ctx, annot);
  2117. fz_catch(ctx)
  2118. fz_rethrow(ctx);
  2119. }
  2120. int
  2121. pdf_annot_MK_BC_rgb(fz_context *ctx, pdf_annot *annot, float rgb[3])
  2122. {
  2123. int ret;
  2124. pdf_annot_push_local_xref(ctx, annot);
  2125. fz_try(ctx)
  2126. {
  2127. pdf_obj *mk_bc = pdf_dict_get(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(BC));
  2128. ret = pdf_annot_color_rgb(ctx, mk_bc, rgb);
  2129. }
  2130. fz_always(ctx)
  2131. pdf_annot_pop_local_xref(ctx, annot);
  2132. fz_catch(ctx)
  2133. fz_rethrow(ctx);
  2134. return ret;
  2135. }
  2136. void
  2137. pdf_set_annot_color(fz_context *ctx, pdf_annot *annot, int n, const float *color)
  2138. {
  2139. begin_annot_op(ctx, annot, "Set color");
  2140. fz_try(ctx)
  2141. {
  2142. pdf_set_annot_color_imp(ctx, annot, PDF_NAME(C), n, color, NULL);
  2143. end_annot_op(ctx, annot);
  2144. }
  2145. fz_catch(ctx)
  2146. {
  2147. abandon_annot_op(ctx, annot);
  2148. fz_rethrow(ctx);
  2149. }
  2150. }
  2151. static pdf_obj *interior_color_subtypes[] = {
  2152. PDF_NAME(Circle),
  2153. PDF_NAME(Line),
  2154. PDF_NAME(PolyLine),
  2155. PDF_NAME(Polygon),
  2156. PDF_NAME(Square),
  2157. NULL,
  2158. };
  2159. int
  2160. pdf_annot_has_interior_color(fz_context *ctx, pdf_annot *annot)
  2161. {
  2162. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(IC), interior_color_subtypes);
  2163. }
  2164. void
  2165. pdf_annot_interior_color(fz_context *ctx, pdf_annot *annot, int *n, float color[4])
  2166. {
  2167. check_allowed_subtypes(ctx, annot, PDF_NAME(IC), interior_color_subtypes);
  2168. do_pdf_annot_color(ctx, annot, n, color, PDF_NAME(IC));
  2169. }
  2170. void
  2171. pdf_set_annot_interior_color(fz_context *ctx, pdf_annot *annot, int n, const float *color)
  2172. {
  2173. begin_annot_op(ctx, annot, "Set interior color");
  2174. fz_try(ctx)
  2175. {
  2176. check_allowed_subtypes(ctx, annot, PDF_NAME(IC), interior_color_subtypes);
  2177. pdf_set_annot_color_imp(ctx, annot, PDF_NAME(IC), n, color, interior_color_subtypes);
  2178. end_annot_op(ctx, annot);
  2179. }
  2180. fz_catch(ctx)
  2181. {
  2182. abandon_annot_op(ctx, annot);
  2183. fz_rethrow(ctx);
  2184. }
  2185. }
  2186. static pdf_obj *line_subtypes[] = {
  2187. PDF_NAME(Line),
  2188. NULL,
  2189. };
  2190. int
  2191. pdf_annot_has_line(fz_context *ctx, pdf_annot *annot)
  2192. {
  2193. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(L), line_subtypes);
  2194. }
  2195. void
  2196. pdf_annot_line(fz_context *ctx, pdf_annot *annot, fz_point *a, fz_point *b)
  2197. {
  2198. fz_matrix page_ctm;
  2199. pdf_obj *line;
  2200. pdf_annot_push_local_xref(ctx, annot);
  2201. fz_try(ctx)
  2202. {
  2203. check_allowed_subtypes(ctx, annot, PDF_NAME(L), line_subtypes);
  2204. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2205. line = pdf_dict_get(ctx, annot->obj, PDF_NAME(L));
  2206. a->x = pdf_array_get_real(ctx, line, 0);
  2207. a->y = pdf_array_get_real(ctx, line, 1);
  2208. b->x = pdf_array_get_real(ctx, line, 2);
  2209. b->y = pdf_array_get_real(ctx, line, 3);
  2210. *a = fz_transform_point(*a, page_ctm);
  2211. *b = fz_transform_point(*b, page_ctm);
  2212. }
  2213. fz_always(ctx)
  2214. pdf_annot_pop_local_xref(ctx, annot);
  2215. fz_catch(ctx)
  2216. fz_rethrow(ctx);
  2217. }
  2218. void
  2219. pdf_set_annot_line(fz_context *ctx, pdf_annot *annot, fz_point a, fz_point b)
  2220. {
  2221. fz_matrix page_ctm, inv_page_ctm;
  2222. pdf_obj *line;
  2223. begin_annot_op(ctx, annot, "Set line");
  2224. fz_try(ctx)
  2225. {
  2226. check_allowed_subtypes(ctx, annot, PDF_NAME(L), line_subtypes);
  2227. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2228. inv_page_ctm = fz_invert_matrix(page_ctm);
  2229. a = fz_transform_point(a, inv_page_ctm);
  2230. b = fz_transform_point(b, inv_page_ctm);
  2231. line = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(L), 4);
  2232. pdf_array_push_real(ctx, line, a.x);
  2233. pdf_array_push_real(ctx, line, a.y);
  2234. pdf_array_push_real(ctx, line, b.x);
  2235. pdf_array_push_real(ctx, line, b.y);
  2236. end_annot_op(ctx, annot);
  2237. }
  2238. fz_catch(ctx)
  2239. {
  2240. abandon_annot_op(ctx, annot);
  2241. fz_rethrow(ctx);
  2242. }
  2243. pdf_dirty_annot(ctx, annot);
  2244. }
  2245. float
  2246. pdf_annot_line_leader(fz_context *ctx, pdf_annot *annot)
  2247. {
  2248. float value;
  2249. pdf_annot_push_local_xref(ctx, annot);
  2250. fz_try(ctx)
  2251. {
  2252. check_allowed_subtypes(ctx, annot, PDF_NAME(LL), line_subtypes);
  2253. value = pdf_dict_get_real(ctx, annot->obj, PDF_NAME(LL));
  2254. }
  2255. fz_always(ctx)
  2256. pdf_annot_pop_local_xref(ctx, annot);
  2257. fz_catch(ctx)
  2258. fz_rethrow(ctx);
  2259. return value;
  2260. }
  2261. float
  2262. pdf_annot_line_leader_extension(fz_context *ctx, pdf_annot *annot)
  2263. {
  2264. float value;
  2265. pdf_annot_push_local_xref(ctx, annot);
  2266. fz_try(ctx)
  2267. {
  2268. check_allowed_subtypes(ctx, annot, PDF_NAME(LLE), line_subtypes);
  2269. value = pdf_dict_get_real(ctx, annot->obj, PDF_NAME(LLE));
  2270. }
  2271. fz_always(ctx)
  2272. pdf_annot_pop_local_xref(ctx, annot);
  2273. fz_catch(ctx)
  2274. fz_rethrow(ctx);
  2275. return value;
  2276. }
  2277. float
  2278. pdf_annot_line_leader_offset(fz_context *ctx, pdf_annot *annot)
  2279. {
  2280. float value;
  2281. pdf_annot_push_local_xref(ctx, annot);
  2282. fz_try(ctx)
  2283. {
  2284. check_allowed_subtypes(ctx, annot, PDF_NAME(LLO), line_subtypes);
  2285. value = pdf_dict_get_real(ctx, annot->obj, PDF_NAME(LLO));
  2286. }
  2287. fz_always(ctx)
  2288. pdf_annot_pop_local_xref(ctx, annot);
  2289. fz_catch(ctx)
  2290. fz_rethrow(ctx);
  2291. return value;
  2292. }
  2293. void
  2294. pdf_set_annot_line_leader(fz_context *ctx, pdf_annot *annot, float value)
  2295. {
  2296. begin_annot_op(ctx, annot, "Set line leader");
  2297. fz_try(ctx)
  2298. {
  2299. check_allowed_subtypes(ctx, annot, PDF_NAME(LL), line_subtypes);
  2300. if (value)
  2301. pdf_dict_put_real(ctx, annot->obj, PDF_NAME(LL), value);
  2302. else
  2303. pdf_dict_del(ctx, annot->obj, PDF_NAME(LL));
  2304. end_annot_op(ctx, annot);
  2305. }
  2306. fz_catch(ctx)
  2307. {
  2308. abandon_annot_op(ctx, annot);
  2309. fz_rethrow(ctx);
  2310. }
  2311. pdf_dirty_annot(ctx, annot);
  2312. }
  2313. void
  2314. pdf_set_annot_line_leader_extension(fz_context *ctx, pdf_annot *annot, float value)
  2315. {
  2316. begin_annot_op(ctx, annot, "Set line leader_extension");
  2317. fz_try(ctx)
  2318. {
  2319. check_allowed_subtypes(ctx, annot, PDF_NAME(LLE), line_subtypes);
  2320. if (value)
  2321. pdf_dict_put_real(ctx, annot->obj, PDF_NAME(LLE), value);
  2322. else
  2323. pdf_dict_del(ctx, annot->obj, PDF_NAME(LLE));
  2324. end_annot_op(ctx, annot);
  2325. }
  2326. fz_catch(ctx)
  2327. {
  2328. abandon_annot_op(ctx, annot);
  2329. fz_rethrow(ctx);
  2330. }
  2331. pdf_dirty_annot(ctx, annot);
  2332. }
  2333. void
  2334. pdf_set_annot_line_leader_offset(fz_context *ctx, pdf_annot *annot, float value)
  2335. {
  2336. begin_annot_op(ctx, annot, "Set line leader offset");
  2337. fz_try(ctx)
  2338. {
  2339. check_allowed_subtypes(ctx, annot, PDF_NAME(LLO), line_subtypes);
  2340. if (value)
  2341. pdf_dict_put_real(ctx, annot->obj, PDF_NAME(LLO), value);
  2342. else
  2343. pdf_dict_del(ctx, annot->obj, PDF_NAME(LLO));
  2344. end_annot_op(ctx, annot);
  2345. }
  2346. fz_catch(ctx)
  2347. {
  2348. abandon_annot_op(ctx, annot);
  2349. fz_rethrow(ctx);
  2350. }
  2351. pdf_dirty_annot(ctx, annot);
  2352. }
  2353. int
  2354. pdf_annot_line_caption(fz_context *ctx, pdf_annot *annot)
  2355. {
  2356. int cap = 0;
  2357. pdf_annot_push_local_xref(ctx, annot);
  2358. fz_try(ctx)
  2359. {
  2360. check_allowed_subtypes(ctx, annot, PDF_NAME(Cap), line_subtypes);
  2361. cap = pdf_dict_get_bool(ctx, annot->obj, PDF_NAME(Cap));
  2362. }
  2363. fz_always(ctx)
  2364. pdf_annot_pop_local_xref(ctx, annot);
  2365. fz_catch(ctx)
  2366. fz_rethrow(ctx);
  2367. return cap;
  2368. }
  2369. void
  2370. pdf_set_annot_line_caption(fz_context *ctx, pdf_annot *annot, int cap)
  2371. {
  2372. begin_annot_op(ctx, annot, "Set line caption");
  2373. fz_try(ctx)
  2374. {
  2375. check_allowed_subtypes(ctx, annot, PDF_NAME(Cap), line_subtypes);
  2376. pdf_dict_put_bool(ctx, annot->obj, PDF_NAME(Cap), cap);
  2377. end_annot_op(ctx, annot);
  2378. }
  2379. fz_catch(ctx)
  2380. {
  2381. abandon_annot_op(ctx, annot);
  2382. fz_rethrow(ctx);
  2383. }
  2384. pdf_dirty_annot(ctx, annot);
  2385. }
  2386. fz_point
  2387. pdf_annot_line_caption_offset(fz_context *ctx, pdf_annot *annot)
  2388. {
  2389. fz_point offset = fz_make_point(0, 0);
  2390. pdf_annot_push_local_xref(ctx, annot);
  2391. fz_try(ctx)
  2392. {
  2393. check_allowed_subtypes(ctx, annot, PDF_NAME(CO), line_subtypes);
  2394. offset = pdf_dict_get_point(ctx, annot->obj, PDF_NAME(CO));
  2395. }
  2396. fz_always(ctx)
  2397. pdf_annot_pop_local_xref(ctx, annot);
  2398. fz_catch(ctx)
  2399. fz_rethrow(ctx);
  2400. return offset;
  2401. }
  2402. void
  2403. pdf_set_annot_line_caption_offset(fz_context *ctx, pdf_annot *annot, fz_point offset)
  2404. {
  2405. begin_annot_op(ctx, annot, "Set line caption");
  2406. fz_try(ctx)
  2407. {
  2408. check_allowed_subtypes(ctx, annot, PDF_NAME(CO), line_subtypes);
  2409. if (offset.x == 0 && offset.y == 0)
  2410. pdf_dict_del(ctx, annot->obj, PDF_NAME(CO));
  2411. else
  2412. pdf_dict_put_point(ctx, annot->obj, PDF_NAME(CO), offset);
  2413. end_annot_op(ctx, annot);
  2414. }
  2415. fz_catch(ctx)
  2416. {
  2417. abandon_annot_op(ctx, annot);
  2418. fz_rethrow(ctx);
  2419. }
  2420. pdf_dirty_annot(ctx, annot);
  2421. }
  2422. static pdf_obj *vertices_subtypes[] = {
  2423. PDF_NAME(PolyLine),
  2424. PDF_NAME(Polygon),
  2425. NULL,
  2426. };
  2427. int
  2428. pdf_annot_has_vertices(fz_context *ctx, pdf_annot *annot)
  2429. {
  2430. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2431. }
  2432. int
  2433. pdf_annot_vertex_count(fz_context *ctx, pdf_annot *annot)
  2434. {
  2435. pdf_obj *vertices;
  2436. int ret;
  2437. pdf_annot_push_local_xref(ctx, annot);
  2438. fz_try(ctx)
  2439. {
  2440. check_allowed_subtypes(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2441. vertices = pdf_dict_get(ctx, annot->obj, PDF_NAME(Vertices));
  2442. ret = pdf_array_len(ctx, vertices) / 2;
  2443. }
  2444. fz_always(ctx)
  2445. pdf_annot_pop_local_xref(ctx, annot);
  2446. fz_catch(ctx)
  2447. fz_rethrow(ctx);
  2448. return ret;
  2449. }
  2450. fz_point
  2451. pdf_annot_vertex(fz_context *ctx, pdf_annot *annot, int i)
  2452. {
  2453. pdf_obj *vertices;
  2454. fz_matrix page_ctm;
  2455. fz_point point;
  2456. pdf_annot_push_local_xref(ctx, annot);
  2457. fz_try(ctx)
  2458. {
  2459. check_allowed_subtypes(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2460. vertices = pdf_dict_get(ctx, annot->obj, PDF_NAME(Vertices));
  2461. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2462. point.x = pdf_array_get_real(ctx, vertices, i * 2);
  2463. point.y = pdf_array_get_real(ctx, vertices, i * 2 + 1);
  2464. }
  2465. fz_always(ctx)
  2466. pdf_annot_pop_local_xref(ctx, annot);
  2467. fz_catch(ctx)
  2468. fz_rethrow(ctx);
  2469. return fz_transform_point(point, page_ctm);
  2470. }
  2471. void
  2472. pdf_set_annot_vertices(fz_context *ctx, pdf_annot *annot, int n, const fz_point *v)
  2473. {
  2474. fz_matrix page_ctm, inv_page_ctm;
  2475. pdf_obj *vertices;
  2476. fz_point point;
  2477. int i;
  2478. begin_annot_op(ctx, annot, "Set points");
  2479. fz_try(ctx)
  2480. {
  2481. check_allowed_subtypes(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2482. if (n <= 0 || !v)
  2483. fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid number of vertices");
  2484. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2485. inv_page_ctm = fz_invert_matrix(page_ctm);
  2486. vertices = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(Vertices), n * 2);
  2487. for (i = 0; i < n; ++i)
  2488. {
  2489. point = fz_transform_point(v[i], inv_page_ctm);
  2490. pdf_array_push_real(ctx, vertices, point.x);
  2491. pdf_array_push_real(ctx, vertices, point.y);
  2492. }
  2493. end_annot_op(ctx, annot);
  2494. }
  2495. fz_catch(ctx)
  2496. {
  2497. abandon_annot_op(ctx, annot);
  2498. fz_rethrow(ctx);
  2499. }
  2500. pdf_dirty_annot(ctx, annot);
  2501. }
  2502. void pdf_clear_annot_vertices(fz_context *ctx, pdf_annot *annot)
  2503. {
  2504. begin_annot_op(ctx, annot, "Clear vertices");
  2505. fz_try(ctx)
  2506. {
  2507. check_allowed_subtypes(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2508. pdf_dict_del(ctx, annot->obj, PDF_NAME(Vertices));
  2509. end_annot_op(ctx, annot);
  2510. }
  2511. fz_catch(ctx)
  2512. {
  2513. abandon_annot_op(ctx, annot);
  2514. fz_rethrow(ctx);
  2515. }
  2516. pdf_dirty_annot(ctx, annot);
  2517. }
  2518. void pdf_add_annot_vertex(fz_context *ctx, pdf_annot *annot, fz_point p)
  2519. {
  2520. fz_matrix page_ctm, inv_page_ctm;
  2521. pdf_obj *vertices;
  2522. begin_annot_op(ctx, annot, "Add point");
  2523. fz_try(ctx)
  2524. {
  2525. check_allowed_subtypes(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2526. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2527. inv_page_ctm = fz_invert_matrix(page_ctm);
  2528. vertices = pdf_dict_get(ctx, annot->obj, PDF_NAME(Vertices));
  2529. if (!pdf_is_array(ctx, vertices))
  2530. vertices = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(Vertices), 32);
  2531. p = fz_transform_point(p, inv_page_ctm);
  2532. pdf_array_push_real(ctx, vertices, p.x);
  2533. pdf_array_push_real(ctx, vertices, p.y);
  2534. end_annot_op(ctx, annot);
  2535. }
  2536. fz_catch(ctx)
  2537. {
  2538. abandon_annot_op(ctx, annot);
  2539. fz_rethrow(ctx);
  2540. }
  2541. pdf_dirty_annot(ctx, annot);
  2542. }
  2543. void pdf_set_annot_vertex(fz_context *ctx, pdf_annot *annot, int i, fz_point p)
  2544. {
  2545. fz_matrix page_ctm, inv_page_ctm;
  2546. pdf_obj *vertices;
  2547. begin_annot_op(ctx, annot, "Set point");
  2548. fz_try(ctx)
  2549. {
  2550. check_allowed_subtypes(ctx, annot, PDF_NAME(Vertices), vertices_subtypes);
  2551. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2552. inv_page_ctm = fz_invert_matrix(page_ctm);
  2553. p = fz_transform_point(p, inv_page_ctm);
  2554. vertices = pdf_dict_get(ctx, annot->obj, PDF_NAME(Vertices));
  2555. pdf_array_put_real(ctx, vertices, i * 2 + 0, p.x);
  2556. pdf_array_put_real(ctx, vertices, i * 2 + 1, p.x);
  2557. end_annot_op(ctx, annot);
  2558. }
  2559. fz_catch(ctx)
  2560. {
  2561. abandon_annot_op(ctx, annot);
  2562. fz_rethrow(ctx);
  2563. }
  2564. }
  2565. static pdf_obj *quad_point_subtypes[] = {
  2566. PDF_NAME(Highlight),
  2567. PDF_NAME(Link),
  2568. PDF_NAME(Squiggly),
  2569. PDF_NAME(StrikeOut),
  2570. PDF_NAME(Underline),
  2571. PDF_NAME(Redact),
  2572. NULL,
  2573. };
  2574. int
  2575. pdf_annot_has_quad_points(fz_context *ctx, pdf_annot *annot)
  2576. {
  2577. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(QuadPoints), quad_point_subtypes);
  2578. }
  2579. int
  2580. pdf_annot_quad_point_count(fz_context *ctx, pdf_annot *annot)
  2581. {
  2582. pdf_obj *quad_points;
  2583. int ret;
  2584. pdf_annot_push_local_xref(ctx, annot);
  2585. fz_try(ctx)
  2586. {
  2587. check_allowed_subtypes(ctx, annot, PDF_NAME(QuadPoints), quad_point_subtypes);
  2588. quad_points = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  2589. ret = pdf_array_len(ctx, quad_points) / 8;
  2590. }
  2591. fz_always(ctx)
  2592. pdf_annot_pop_local_xref(ctx, annot);
  2593. fz_catch(ctx)
  2594. fz_rethrow(ctx);
  2595. return ret;
  2596. }
  2597. fz_quad
  2598. pdf_annot_quad_point(fz_context *ctx, pdf_annot *annot, int idx)
  2599. {
  2600. pdf_obj *quad_points;
  2601. fz_matrix page_ctm;
  2602. float v[8];
  2603. int i;
  2604. pdf_annot_push_local_xref(ctx, annot);
  2605. fz_try(ctx)
  2606. {
  2607. check_allowed_subtypes(ctx, annot, PDF_NAME(QuadPoints), quad_point_subtypes);
  2608. quad_points = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  2609. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2610. for (i = 0; i < 8; i += 2)
  2611. {
  2612. fz_point point;
  2613. point.x = pdf_array_get_real(ctx, quad_points, idx * 8 + i + 0);
  2614. point.y = pdf_array_get_real(ctx, quad_points, idx * 8 + i + 1);
  2615. point = fz_transform_point(point, page_ctm);
  2616. v[i+0] = point.x;
  2617. v[i+1] = point.y;
  2618. }
  2619. }
  2620. fz_always(ctx)
  2621. pdf_annot_pop_local_xref(ctx, annot);
  2622. fz_catch(ctx)
  2623. fz_rethrow(ctx);
  2624. return fz_make_quad(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  2625. }
  2626. void
  2627. pdf_set_annot_quad_points(fz_context *ctx, pdf_annot *annot, int n, const fz_quad *q)
  2628. {
  2629. fz_matrix page_ctm, inv_page_ctm;
  2630. pdf_obj *quad_points;
  2631. fz_quad quad;
  2632. int i;
  2633. begin_annot_op(ctx, annot, "Set quad points");
  2634. fz_try(ctx)
  2635. {
  2636. check_allowed_subtypes(ctx, annot, PDF_NAME(QuadPoints), quad_point_subtypes);
  2637. if (n <= 0 || !q)
  2638. fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid number of quadrilaterals");
  2639. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2640. inv_page_ctm = fz_invert_matrix(page_ctm);
  2641. quad_points = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(QuadPoints), n);
  2642. for (i = 0; i < n; ++i)
  2643. {
  2644. quad = fz_transform_quad(q[i], inv_page_ctm);
  2645. pdf_array_push_real(ctx, quad_points, quad.ul.x);
  2646. pdf_array_push_real(ctx, quad_points, quad.ul.y);
  2647. pdf_array_push_real(ctx, quad_points, quad.ur.x);
  2648. pdf_array_push_real(ctx, quad_points, quad.ur.y);
  2649. pdf_array_push_real(ctx, quad_points, quad.ll.x);
  2650. pdf_array_push_real(ctx, quad_points, quad.ll.y);
  2651. pdf_array_push_real(ctx, quad_points, quad.lr.x);
  2652. pdf_array_push_real(ctx, quad_points, quad.lr.y);
  2653. }
  2654. end_annot_op(ctx, annot);
  2655. }
  2656. fz_catch(ctx)
  2657. {
  2658. abandon_annot_op(ctx, annot);
  2659. fz_rethrow(ctx);
  2660. }
  2661. pdf_dirty_annot(ctx, annot);
  2662. }
  2663. void
  2664. pdf_clear_annot_quad_points(fz_context *ctx, pdf_annot *annot)
  2665. {
  2666. begin_annot_op(ctx, annot, "Clear quad points");
  2667. fz_try(ctx)
  2668. {
  2669. check_allowed_subtypes(ctx, annot, PDF_NAME(QuadPoints), quad_point_subtypes);
  2670. pdf_dict_del(ctx, annot->obj, PDF_NAME(QuadPoints));
  2671. end_annot_op(ctx, annot);
  2672. }
  2673. fz_catch(ctx)
  2674. {
  2675. abandon_annot_op(ctx, annot);
  2676. fz_rethrow(ctx);
  2677. }
  2678. pdf_dirty_annot(ctx, annot);
  2679. }
  2680. void
  2681. pdf_add_annot_quad_point(fz_context *ctx, pdf_annot *annot, fz_quad quad)
  2682. {
  2683. fz_matrix page_ctm, inv_page_ctm;
  2684. pdf_obj *quad_points;
  2685. begin_annot_op(ctx, annot, "Add quad point");
  2686. fz_try(ctx)
  2687. {
  2688. check_allowed_subtypes(ctx, annot, PDF_NAME(QuadPoints), quad_point_subtypes);
  2689. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2690. inv_page_ctm = fz_invert_matrix(page_ctm);
  2691. quad_points = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  2692. if (!pdf_is_array(ctx, quad_points))
  2693. quad_points = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(QuadPoints), 8);
  2694. /* Contrary to the specification, the points within a QuadPoint are NOT ordered
  2695. * in a counterclockwise fashion. Experiments with Adobe's implementation
  2696. * indicates a cross-wise ordering is intended: ul, ur, ll, lr.
  2697. */
  2698. quad = fz_transform_quad(quad, inv_page_ctm);
  2699. pdf_array_push_real(ctx, quad_points, quad.ul.x);
  2700. pdf_array_push_real(ctx, quad_points, quad.ul.y);
  2701. pdf_array_push_real(ctx, quad_points, quad.ur.x);
  2702. pdf_array_push_real(ctx, quad_points, quad.ur.y);
  2703. pdf_array_push_real(ctx, quad_points, quad.ll.x);
  2704. pdf_array_push_real(ctx, quad_points, quad.ll.y);
  2705. pdf_array_push_real(ctx, quad_points, quad.lr.x);
  2706. pdf_array_push_real(ctx, quad_points, quad.lr.y);
  2707. end_annot_op(ctx, annot);
  2708. }
  2709. fz_catch(ctx)
  2710. {
  2711. abandon_annot_op(ctx, annot);
  2712. fz_rethrow(ctx);
  2713. }
  2714. pdf_dirty_annot(ctx, annot);
  2715. }
  2716. static pdf_obj *ink_list_subtypes[] = {
  2717. PDF_NAME(Ink),
  2718. NULL,
  2719. };
  2720. int
  2721. pdf_annot_has_ink_list(fz_context *ctx, pdf_annot *annot)
  2722. {
  2723. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2724. }
  2725. int
  2726. pdf_annot_ink_list_count(fz_context *ctx, pdf_annot *annot)
  2727. {
  2728. int ret;
  2729. pdf_annot_push_local_xref(ctx, annot);
  2730. fz_try(ctx)
  2731. {
  2732. pdf_obj *ink_list;
  2733. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2734. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  2735. ret = pdf_array_len(ctx, ink_list);
  2736. }
  2737. fz_always(ctx)
  2738. pdf_annot_pop_local_xref(ctx, annot);
  2739. fz_catch(ctx)
  2740. fz_rethrow(ctx);
  2741. return ret;
  2742. }
  2743. int
  2744. pdf_annot_ink_list_stroke_count(fz_context *ctx, pdf_annot *annot, int i)
  2745. {
  2746. pdf_obj *ink_list;
  2747. pdf_obj *stroke;
  2748. int ret;
  2749. pdf_annot_push_local_xref(ctx, annot);
  2750. fz_try(ctx)
  2751. {
  2752. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2753. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  2754. stroke = pdf_array_get(ctx, ink_list, i);
  2755. ret = pdf_array_len(ctx, stroke) / 2;
  2756. }
  2757. fz_always(ctx)
  2758. pdf_annot_pop_local_xref(ctx, annot);
  2759. fz_catch(ctx)
  2760. fz_rethrow(ctx);
  2761. return ret;
  2762. }
  2763. fz_point
  2764. pdf_annot_ink_list_stroke_vertex(fz_context *ctx, pdf_annot *annot, int i, int k)
  2765. {
  2766. pdf_obj *ink_list;
  2767. pdf_obj *stroke;
  2768. fz_matrix page_ctm;
  2769. fz_point point;
  2770. pdf_annot_push_local_xref(ctx, annot);
  2771. fz_try(ctx)
  2772. {
  2773. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2774. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  2775. stroke = pdf_array_get(ctx, ink_list, i);
  2776. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2777. point.x = pdf_array_get_real(ctx, stroke, k * 2 + 0);
  2778. point.y = pdf_array_get_real(ctx, stroke, k * 2 + 1);
  2779. }
  2780. fz_always(ctx)
  2781. pdf_annot_pop_local_xref(ctx, annot);
  2782. fz_catch(ctx)
  2783. fz_rethrow(ctx);
  2784. return fz_transform_point(point, page_ctm);
  2785. }
  2786. /* FIXME: try/catch required for memory exhaustion */
  2787. void
  2788. pdf_set_annot_ink_list(fz_context *ctx, pdf_annot *annot, int n, const int *count, const fz_point *v)
  2789. {
  2790. fz_matrix page_ctm, inv_page_ctm;
  2791. pdf_obj *ink_list = NULL, *stroke;
  2792. fz_point point;
  2793. int i, k;
  2794. fz_var(ink_list);
  2795. begin_annot_op(ctx, annot, "Set ink list");
  2796. fz_try(ctx)
  2797. {
  2798. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2799. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2800. inv_page_ctm = fz_invert_matrix(page_ctm);
  2801. ink_list = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(InkList), n);
  2802. for (i = 0; i < n; ++i)
  2803. {
  2804. stroke = pdf_array_push_array(ctx, ink_list, count[i] * 2);
  2805. /* Although we have dropped our reference to stroke,
  2806. * it's still valid because we ink_list holds one, and
  2807. * we hold a reference to that. */
  2808. for (k = 0; k < count[i]; ++k)
  2809. {
  2810. point = fz_transform_point(*v++, inv_page_ctm);
  2811. pdf_array_push_real(ctx, stroke, point.x);
  2812. pdf_array_push_real(ctx, stroke, point.y);
  2813. }
  2814. }
  2815. end_annot_op(ctx, annot);
  2816. }
  2817. fz_catch(ctx)
  2818. {
  2819. abandon_annot_op(ctx, annot);
  2820. fz_rethrow(ctx);
  2821. }
  2822. pdf_dirty_annot(ctx, annot);
  2823. }
  2824. void
  2825. pdf_clear_annot_ink_list(fz_context *ctx, pdf_annot *annot)
  2826. {
  2827. begin_annot_op(ctx, annot, "Clear ink list");
  2828. fz_try(ctx)
  2829. {
  2830. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2831. pdf_dict_del(ctx, annot->obj, PDF_NAME(InkList));
  2832. end_annot_op(ctx, annot);
  2833. }
  2834. fz_catch(ctx)
  2835. {
  2836. abandon_annot_op(ctx, annot);
  2837. fz_rethrow(ctx);
  2838. }
  2839. pdf_dirty_annot(ctx, annot);
  2840. }
  2841. void pdf_add_annot_ink_list_stroke(fz_context *ctx, pdf_annot *annot)
  2842. {
  2843. pdf_obj *ink_list;
  2844. begin_annot_op(ctx, annot, "Add ink list stroke");
  2845. fz_try(ctx)
  2846. {
  2847. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2848. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  2849. if (!pdf_is_array(ctx, ink_list))
  2850. ink_list = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(InkList), 10);
  2851. pdf_array_push_array(ctx, ink_list, 16);
  2852. end_annot_op(ctx, annot);
  2853. }
  2854. fz_catch(ctx)
  2855. {
  2856. abandon_annot_op(ctx, annot);
  2857. fz_rethrow(ctx);
  2858. }
  2859. pdf_dirty_annot(ctx, annot);
  2860. }
  2861. void pdf_add_annot_ink_list_stroke_vertex(fz_context *ctx, pdf_annot *annot, fz_point p)
  2862. {
  2863. fz_matrix page_ctm, inv_page_ctm;
  2864. pdf_obj *ink_list, *stroke;
  2865. begin_annot_op(ctx, annot, "Add ink list stroke point");
  2866. fz_try(ctx)
  2867. {
  2868. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2869. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2870. inv_page_ctm = fz_invert_matrix(page_ctm);
  2871. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  2872. if (!pdf_is_array(ctx, ink_list))
  2873. ink_list = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(InkList), 10);
  2874. stroke = pdf_array_get(ctx, ink_list, pdf_array_len(ctx, ink_list)-1);
  2875. if (!pdf_is_array(ctx, stroke))
  2876. {
  2877. int len = pdf_array_len(ctx, ink_list);
  2878. stroke = pdf_new_array(ctx, pdf_get_bound_document(ctx, ink_list), 16);
  2879. pdf_array_put_drop(ctx, ink_list, len ? len-1 : 0, stroke);
  2880. }
  2881. p = fz_transform_point(p, inv_page_ctm);
  2882. pdf_array_push_real(ctx, stroke, p.x);
  2883. pdf_array_push_real(ctx, stroke, p.y);
  2884. end_annot_op(ctx, annot);
  2885. }
  2886. fz_catch(ctx)
  2887. {
  2888. abandon_annot_op(ctx, annot);
  2889. fz_rethrow(ctx);
  2890. }
  2891. pdf_dirty_annot(ctx, annot);
  2892. }
  2893. void
  2894. pdf_add_annot_ink_list(fz_context *ctx, pdf_annot *annot, int n, fz_point p[])
  2895. {
  2896. fz_matrix page_ctm, inv_page_ctm;
  2897. pdf_obj *ink_list, *stroke;
  2898. int i;
  2899. begin_annot_op(ctx, annot, "Add ink list");
  2900. fz_try(ctx)
  2901. {
  2902. check_allowed_subtypes(ctx, annot, PDF_NAME(InkList), ink_list_subtypes);
  2903. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  2904. inv_page_ctm = fz_invert_matrix(page_ctm);
  2905. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  2906. if (!pdf_is_array(ctx, ink_list))
  2907. ink_list = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(InkList), 10);
  2908. stroke = pdf_array_push_array(ctx, ink_list, n * 2);
  2909. for (i = 0; i < n; ++i)
  2910. {
  2911. fz_point tp = fz_transform_point(p[i], inv_page_ctm);
  2912. pdf_array_push_real(ctx, stroke, tp.x);
  2913. pdf_array_push_real(ctx, stroke, tp.y);
  2914. }
  2915. end_annot_op(ctx, annot);
  2916. }
  2917. fz_catch(ctx)
  2918. {
  2919. abandon_annot_op(ctx, annot);
  2920. fz_rethrow(ctx);
  2921. }
  2922. pdf_dirty_annot(ctx, annot);
  2923. }
  2924. /*
  2925. Get annotation's modification date in seconds since the epoch.
  2926. */
  2927. int64_t
  2928. pdf_annot_modification_date(fz_context *ctx, pdf_annot *annot)
  2929. {
  2930. int64_t ret;
  2931. pdf_annot_push_local_xref(ctx, annot);
  2932. fz_try(ctx)
  2933. {
  2934. ret = pdf_dict_get_date(ctx, annot->obj, PDF_NAME(M));
  2935. }
  2936. fz_always(ctx)
  2937. pdf_annot_pop_local_xref(ctx, annot);
  2938. fz_catch(ctx)
  2939. fz_rethrow(ctx);
  2940. return ret;
  2941. }
  2942. /*
  2943. Get annotation's creation date in seconds since the epoch.
  2944. */
  2945. int64_t
  2946. pdf_annot_creation_date(fz_context *ctx, pdf_annot *annot)
  2947. {
  2948. int64_t ret;
  2949. pdf_annot_push_local_xref(ctx, annot);
  2950. fz_try(ctx)
  2951. ret = pdf_dict_get_date(ctx, annot->obj, PDF_NAME(CreationDate));
  2952. fz_always(ctx)
  2953. pdf_annot_pop_local_xref(ctx, annot);
  2954. fz_catch(ctx)
  2955. fz_rethrow(ctx);
  2956. return ret;
  2957. }
  2958. /*
  2959. Set annotation's modification date in seconds since the epoch.
  2960. */
  2961. void
  2962. pdf_set_annot_modification_date(fz_context *ctx, pdf_annot *annot, int64_t secs)
  2963. {
  2964. begin_annot_op(ctx, annot, "Set modification date");
  2965. fz_try(ctx)
  2966. {
  2967. pdf_dict_put_date(ctx, annot->obj, PDF_NAME(M), secs);
  2968. end_annot_op(ctx, annot);
  2969. }
  2970. fz_catch(ctx)
  2971. {
  2972. abandon_annot_op(ctx, annot);
  2973. fz_rethrow(ctx);
  2974. }
  2975. pdf_dirty_annot(ctx, annot);
  2976. }
  2977. /*
  2978. Set annotation's creation date in seconds since the epoch.
  2979. */
  2980. void
  2981. pdf_set_annot_creation_date(fz_context *ctx, pdf_annot *annot, int64_t secs)
  2982. {
  2983. begin_annot_op(ctx, annot, "Set creation date");
  2984. fz_try(ctx)
  2985. {
  2986. check_allowed_subtypes(ctx, annot, PDF_NAME(CreationDate), markup_subtypes);
  2987. pdf_dict_put_date(ctx, annot->obj, PDF_NAME(CreationDate), secs);
  2988. end_annot_op(ctx, annot);
  2989. }
  2990. fz_catch(ctx)
  2991. {
  2992. abandon_annot_op(ctx, annot);
  2993. fz_rethrow(ctx);
  2994. }
  2995. pdf_dirty_annot(ctx, annot);
  2996. }
  2997. int
  2998. pdf_annot_has_author(fz_context *ctx, pdf_annot *annot)
  2999. {
  3000. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(T), markup_subtypes);
  3001. }
  3002. const char *
  3003. pdf_annot_author(fz_context *ctx, pdf_annot *annot)
  3004. {
  3005. const char *ret;
  3006. pdf_annot_push_local_xref(ctx, annot);
  3007. fz_try(ctx)
  3008. {
  3009. check_allowed_subtypes(ctx, annot, PDF_NAME(T), markup_subtypes);
  3010. ret = pdf_dict_get_text_string(ctx, annot->obj, PDF_NAME(T));
  3011. }
  3012. fz_always(ctx)
  3013. pdf_annot_pop_local_xref(ctx, annot);
  3014. fz_catch(ctx)
  3015. fz_rethrow(ctx);
  3016. return ret;
  3017. }
  3018. void
  3019. pdf_set_annot_author(fz_context *ctx, pdf_annot *annot, const char *author)
  3020. {
  3021. begin_annot_op(ctx, annot, "Set author");
  3022. fz_try(ctx)
  3023. {
  3024. check_allowed_subtypes(ctx, annot, PDF_NAME(T), markup_subtypes);
  3025. pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(T), author);
  3026. pdf_dirty_annot(ctx, annot);
  3027. end_annot_op(ctx, annot);
  3028. }
  3029. fz_catch(ctx)
  3030. {
  3031. abandon_annot_op(ctx, annot);
  3032. fz_rethrow(ctx);
  3033. }
  3034. }
  3035. static pdf_obj *intent_subtypes[] = {
  3036. PDF_NAME(FreeText),
  3037. PDF_NAME(Line),
  3038. PDF_NAME(Polygon),
  3039. PDF_NAME(PolyLine),
  3040. PDF_NAME(Stamp),
  3041. NULL,
  3042. };
  3043. enum pdf_intent pdf_intent_from_name(fz_context *ctx, pdf_obj *it)
  3044. {
  3045. if (
  3046. it == PDF_NULL ||
  3047. it == PDF_NAME(FreeText) ||
  3048. it == PDF_NAME(Line) ||
  3049. it == PDF_NAME(PolyLine) ||
  3050. it == PDF_NAME(Polygon) ||
  3051. it == PDF_NAME(Stamp)
  3052. )
  3053. return PDF_ANNOT_IT_DEFAULT;
  3054. if (it == PDF_NAME(FreeTextCallout))
  3055. return PDF_ANNOT_IT_FREETEXT_CALLOUT;
  3056. if (it == PDF_NAME(FreeTextTypeWriter))
  3057. return PDF_ANNOT_IT_FREETEXT_TYPEWRITER;
  3058. if (it == PDF_NAME(LineArrow))
  3059. return PDF_ANNOT_IT_LINE_ARROW;
  3060. if (it == PDF_NAME(LineDimension))
  3061. return PDF_ANNOT_IT_LINE_DIMENSION;
  3062. if (it == PDF_NAME(PolyLineDimension))
  3063. return PDF_ANNOT_IT_POLYLINE_DIMENSION;
  3064. if (it == PDF_NAME(PolygonCloud))
  3065. return PDF_ANNOT_IT_POLYGON_CLOUD;
  3066. if (it == PDF_NAME(PolygonDimension))
  3067. return PDF_ANNOT_IT_POLYGON_DIMENSION;
  3068. if (it == PDF_NAME(StampImage))
  3069. return PDF_ANNOT_IT_STAMP_IMAGE;
  3070. if (it == PDF_NAME(StampSnapshot))
  3071. return PDF_ANNOT_IT_STAMP_SNAPSHOT;
  3072. return PDF_ANNOT_IT_UNKNOWN;
  3073. }
  3074. enum pdf_intent pdf_intent_from_string(fz_context *ctx, const char *it)
  3075. {
  3076. if (
  3077. it == NULL ||
  3078. !strcmp(it, "FreeText") ||
  3079. !strcmp(it, "Line") ||
  3080. !strcmp(it, "PolyLine") ||
  3081. !strcmp(it, "Polygon") ||
  3082. !strcmp(it, "Stamp")
  3083. )
  3084. return PDF_ANNOT_IT_DEFAULT;
  3085. if (!strcmp(it, "FreeTextCallout"))
  3086. return PDF_ANNOT_IT_FREETEXT_CALLOUT;
  3087. if (!strcmp(it, "FreeTextTypeWriter"))
  3088. return PDF_ANNOT_IT_FREETEXT_TYPEWRITER;
  3089. if (!strcmp(it, "LineArrow"))
  3090. return PDF_ANNOT_IT_LINE_ARROW;
  3091. if (!strcmp(it, "LineDimension"))
  3092. return PDF_ANNOT_IT_LINE_DIMENSION;
  3093. if (!strcmp(it, "PolyLineDimension"))
  3094. return PDF_ANNOT_IT_POLYLINE_DIMENSION;
  3095. if (!strcmp(it, "PolygonCloud"))
  3096. return PDF_ANNOT_IT_POLYGON_CLOUD;
  3097. if (!strcmp(it, "PolygonDimension"))
  3098. return PDF_ANNOT_IT_POLYGON_DIMENSION;
  3099. if (!strcmp(it, "StampImage"))
  3100. return PDF_ANNOT_IT_STAMP_IMAGE;
  3101. if (!strcmp(it, "StampSnapshot"))
  3102. return PDF_ANNOT_IT_STAMP_SNAPSHOT;
  3103. return PDF_ANNOT_IT_UNKNOWN;
  3104. }
  3105. pdf_obj *pdf_name_from_intent(fz_context *ctx, enum pdf_intent it)
  3106. {
  3107. switch (it)
  3108. {
  3109. default:
  3110. case PDF_ANNOT_IT_DEFAULT: return PDF_NULL;
  3111. case PDF_ANNOT_IT_FREETEXT_CALLOUT: return PDF_NAME(FreeTextCallout);
  3112. case PDF_ANNOT_IT_FREETEXT_TYPEWRITER: return PDF_NAME(FreeTextTypeWriter);
  3113. case PDF_ANNOT_IT_LINE_ARROW: return PDF_NAME(LineArrow);
  3114. case PDF_ANNOT_IT_LINE_DIMENSION: return PDF_NAME(LineDimension);
  3115. case PDF_ANNOT_IT_POLYLINE_DIMENSION: return PDF_NAME(PolyLineDimension);
  3116. case PDF_ANNOT_IT_POLYGON_CLOUD: return PDF_NAME(PolygonCloud);
  3117. case PDF_ANNOT_IT_POLYGON_DIMENSION: return PDF_NAME(PolygonDimension);
  3118. }
  3119. }
  3120. const char *pdf_string_from_intent(fz_context *ctx, enum pdf_intent it)
  3121. {
  3122. switch (it)
  3123. {
  3124. default:
  3125. case PDF_ANNOT_IT_DEFAULT: return NULL;
  3126. case PDF_ANNOT_IT_FREETEXT_CALLOUT: return "FreeTextCallout";
  3127. case PDF_ANNOT_IT_FREETEXT_TYPEWRITER: return "FreeTextTypeWriter";
  3128. case PDF_ANNOT_IT_LINE_ARROW: return "LineArrow";
  3129. case PDF_ANNOT_IT_LINE_DIMENSION: return "LineDimension";
  3130. case PDF_ANNOT_IT_POLYLINE_DIMENSION: return "PolyLineDimension";
  3131. case PDF_ANNOT_IT_POLYGON_CLOUD: return "PolygonCloud";
  3132. case PDF_ANNOT_IT_POLYGON_DIMENSION: return "PolygonDimension";
  3133. }
  3134. }
  3135. int
  3136. pdf_annot_has_intent(fz_context *ctx, pdf_annot *annot)
  3137. {
  3138. /* Only a subset of intents are defined in the spec, so we limit this API to the defined ones.
  3139. * FreeText: Callout, TypeWriter
  3140. * Line: Arrow, Dimension
  3141. * Polygon: Cloud, Dimension
  3142. * PolyLine: Dimension
  3143. */
  3144. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(IT), intent_subtypes);
  3145. }
  3146. enum pdf_intent
  3147. pdf_annot_intent(fz_context *ctx, pdf_annot *annot)
  3148. {
  3149. enum pdf_intent ret;
  3150. pdf_annot_push_local_xref(ctx, annot);
  3151. fz_try(ctx)
  3152. {
  3153. check_allowed_subtypes(ctx, annot, PDF_NAME(IT), intent_subtypes);
  3154. ret = pdf_intent_from_name(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(IT)));
  3155. }
  3156. fz_always(ctx)
  3157. pdf_annot_pop_local_xref(ctx, annot);
  3158. fz_catch(ctx)
  3159. fz_rethrow(ctx);
  3160. return ret;
  3161. }
  3162. void
  3163. pdf_set_annot_intent(fz_context *ctx, pdf_annot *annot, enum pdf_intent it)
  3164. {
  3165. begin_annot_op(ctx, annot, "Set intent");
  3166. fz_try(ctx)
  3167. {
  3168. check_allowed_subtypes(ctx, annot, PDF_NAME(IT), intent_subtypes);
  3169. pdf_dict_put(ctx, annot->obj, PDF_NAME(IT), pdf_name_from_intent(ctx, it));
  3170. pdf_dirty_annot(ctx, annot);
  3171. end_annot_op(ctx, annot);
  3172. }
  3173. fz_catch(ctx)
  3174. {
  3175. abandon_annot_op(ctx, annot);
  3176. fz_rethrow(ctx);
  3177. }
  3178. }
  3179. static pdf_obj *callout_subtypes[] = {
  3180. PDF_NAME(FreeText),
  3181. NULL,
  3182. };
  3183. int pdf_annot_has_callout(fz_context *ctx, pdf_annot *annot)
  3184. {
  3185. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(CL), callout_subtypes);
  3186. }
  3187. enum pdf_line_ending pdf_annot_callout_style(fz_context *ctx, pdf_annot *annot)
  3188. {
  3189. enum pdf_line_ending style = PDF_ANNOT_LE_NONE;
  3190. pdf_obj *obj;
  3191. pdf_annot_push_local_xref(ctx, annot);
  3192. fz_try(ctx)
  3193. {
  3194. check_allowed_subtypes(ctx, annot, PDF_NAME(CL), callout_subtypes);
  3195. obj = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  3196. style = pdf_line_ending_from_name(ctx, obj);
  3197. }
  3198. fz_always(ctx)
  3199. pdf_annot_pop_local_xref(ctx, annot);
  3200. fz_catch(ctx)
  3201. fz_rethrow(ctx);
  3202. return style;
  3203. }
  3204. void pdf_set_annot_callout_style(fz_context *ctx, pdf_annot *annot, enum pdf_line_ending style)
  3205. {
  3206. begin_annot_op(ctx, annot, "Set callout style");
  3207. fz_try(ctx)
  3208. {
  3209. check_allowed_subtypes(ctx, annot, PDF_NAME(CL), callout_subtypes);
  3210. pdf_dict_put(ctx, annot->obj, PDF_NAME(LE), pdf_name_from_line_ending(ctx, style));
  3211. }
  3212. fz_catch(ctx)
  3213. {
  3214. abandon_annot_op(ctx, annot);
  3215. fz_rethrow(ctx);
  3216. }
  3217. pdf_dirty_annot(ctx, annot);
  3218. }
  3219. void pdf_annot_callout_line(fz_context *ctx, pdf_annot *annot, fz_point callout[3], int *np)
  3220. {
  3221. pdf_annot_push_local_xref(ctx, annot);
  3222. fz_try(ctx)
  3223. {
  3224. fz_matrix page_ctm;
  3225. pdf_obj *obj;
  3226. int n;
  3227. check_allowed_subtypes(ctx, annot, PDF_NAME(CL), callout_subtypes);
  3228. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  3229. obj = pdf_dict_get(ctx, annot->obj, PDF_NAME(CL));
  3230. n = pdf_array_len(ctx, obj);
  3231. if (n == 4 || n == 6)
  3232. {
  3233. callout[0] = fz_transform_point_xy(pdf_array_get_real(ctx, obj, 0), pdf_array_get_real(ctx, obj, 1), page_ctm);
  3234. callout[1] = fz_transform_point_xy(pdf_array_get_real(ctx, obj, 2), pdf_array_get_real(ctx, obj, 3), page_ctm);
  3235. if (n == 4)
  3236. {
  3237. *np = 2;
  3238. callout[2] = fz_make_point(0, 0);
  3239. }
  3240. else
  3241. {
  3242. *np = 3;
  3243. callout[2] = fz_transform_point_xy(pdf_array_get_real(ctx, obj, 4), pdf_array_get_real(ctx, obj, 5), page_ctm);
  3244. }
  3245. }
  3246. else
  3247. {
  3248. callout[0] = fz_make_point(0, 0);
  3249. callout[1] = fz_make_point(0, 0);
  3250. callout[2] = fz_make_point(0, 0);
  3251. *np = 0;
  3252. }
  3253. }
  3254. fz_always(ctx)
  3255. pdf_annot_pop_local_xref(ctx, annot);
  3256. fz_catch(ctx)
  3257. fz_rethrow(ctx);
  3258. }
  3259. void pdf_set_annot_callout_line(fz_context *ctx, pdf_annot *annot, fz_point callout[3], int n)
  3260. {
  3261. begin_annot_op(ctx, annot, "Set callout");
  3262. fz_try(ctx)
  3263. {
  3264. fz_matrix page_ctm;
  3265. fz_point p;
  3266. int i;
  3267. pdf_obj *obj;
  3268. check_allowed_subtypes(ctx, annot, PDF_NAME(CL), callout_subtypes);
  3269. if (n == 2 || n == 3)
  3270. {
  3271. pdf_page_transform(ctx, annot->page, NULL, &page_ctm);
  3272. obj = pdf_dict_put_array(ctx, annot->obj, PDF_NAME(CL), n * 2);
  3273. for (i = 0; i < n; ++i)
  3274. {
  3275. p = fz_transform_point(callout[i], page_ctm);
  3276. pdf_array_push_real(ctx, obj, p.x);
  3277. pdf_array_push_real(ctx, obj, p.y);
  3278. }
  3279. }
  3280. else
  3281. {
  3282. pdf_dict_del(ctx, annot->obj, PDF_NAME(CL));
  3283. }
  3284. pdf_dirty_annot(ctx, annot);
  3285. end_annot_op(ctx, annot);
  3286. }
  3287. fz_catch(ctx)
  3288. {
  3289. abandon_annot_op(ctx, annot);
  3290. fz_rethrow(ctx);
  3291. }
  3292. }
  3293. fz_point pdf_annot_callout_point(fz_context *ctx, pdf_annot *annot)
  3294. {
  3295. fz_point line[3];
  3296. int n;
  3297. pdf_annot_callout_line(ctx, annot, line, &n);
  3298. if (n > 0)
  3299. return line[0];
  3300. return fz_make_point(0, 0);
  3301. }
  3302. void pdf_set_annot_callout_point(fz_context *ctx, pdf_annot *annot, fz_point p)
  3303. {
  3304. fz_rect rect;
  3305. fz_point a, b, line[3];
  3306. float m;
  3307. rect = pdf_annot_rect(ctx, annot);
  3308. // Make line from center of text box to designated end point.
  3309. a = fz_make_point((rect.x0 + rect.x1) / 2, (rect.y0 + rect.y1) / 2);
  3310. b = p;
  3311. // No CalloutLine if end point is within the text box itself.
  3312. if (fz_is_point_inside_rect(p, rect))
  3313. {
  3314. line[0] = p;
  3315. line[1] = a;
  3316. pdf_set_annot_callout_line(ctx, annot, line, 2);
  3317. return;
  3318. }
  3319. // Simplified Cohen-Sutherland algorithm to find intersection of line and text box.
  3320. m = (b.y - a.y) / (b.x - a.x);
  3321. for (;;)
  3322. {
  3323. if (b.y < rect.y0) {
  3324. b.x = a.x + (rect.y0 - a.y) / m;
  3325. b.y = rect.y0;
  3326. }
  3327. else
  3328. if (b.y > rect.y1) {
  3329. b.x = a.x + (rect.y1 - a.y) / m;
  3330. b.y = rect.y1;
  3331. }
  3332. else
  3333. if (b.x < rect.x0) {
  3334. b.y = a.y + (rect.x0 - a.x) * m;
  3335. b.x = rect.x0;
  3336. }
  3337. else
  3338. if (b.x > rect.x1) {
  3339. b.y = a.y + (rect.x1 - a.x) * m;
  3340. b.x = rect.x1;
  3341. }
  3342. else
  3343. break;
  3344. }
  3345. // Draw line from intersection to end point.
  3346. line[0] = p;
  3347. line[1] = b;
  3348. pdf_set_annot_callout_line(ctx, annot, line, 2);
  3349. }
  3350. void
  3351. pdf_parse_default_appearance_unmapped(fz_context *ctx, const char *da, char *font_name, int font_name_size, float *size, int *n, float color[4])
  3352. {
  3353. char buf[100], *p = buf, *tok, *end;
  3354. float stack[4] = { 0, 0, 0, 0 };
  3355. int top = 0;
  3356. fz_strlcpy(font_name, "Helv", font_name_size);
  3357. *size = 12;
  3358. *n = 0;
  3359. color[0] = color[1] = color[2] = color[3] = 0;
  3360. fz_strlcpy(buf, da, sizeof buf);
  3361. while ((tok = fz_strsep(&p, " \n\r\t")) != NULL)
  3362. {
  3363. if (tok[0] == 0)
  3364. ;
  3365. else if (tok[0] == '/')
  3366. {
  3367. fz_strlcpy(font_name, tok+1, font_name_size);
  3368. }
  3369. else if (!strcmp(tok, "Tf"))
  3370. {
  3371. *size = stack[0];
  3372. top = 0;
  3373. }
  3374. else if (!strcmp(tok, "g"))
  3375. {
  3376. *n = 1;
  3377. color[0] = stack[0];
  3378. top = 0;
  3379. }
  3380. else if (!strcmp(tok, "rg"))
  3381. {
  3382. *n = 3;
  3383. color[0] = stack[0];
  3384. color[1] = stack[1];
  3385. color[2] = stack[2];
  3386. top=0;
  3387. }
  3388. else if (!strcmp(tok, "k"))
  3389. {
  3390. *n = 4;
  3391. color[0] = stack[0];
  3392. color[1] = stack[1];
  3393. color[2] = stack[2];
  3394. color[3] = stack[3];
  3395. top=0;
  3396. }
  3397. else
  3398. {
  3399. float v = fz_strtof(tok, &end);
  3400. if (top < 4)
  3401. stack[top] = v;
  3402. if (*end == 0)
  3403. ++top;
  3404. else
  3405. top = 0;
  3406. }
  3407. }
  3408. }
  3409. void
  3410. pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font, float *size, int *n, float color[4])
  3411. {
  3412. char font_name[100];
  3413. pdf_parse_default_appearance_unmapped(ctx, da, font_name, sizeof font_name, size, n, color);
  3414. if (!strcmp(font_name, "Cour")) *font = "Cour";
  3415. else if (!strcmp(font_name, "Helv")) *font = "Helv";
  3416. else if (!strcmp(font_name, "TiRo")) *font = "TiRo";
  3417. else if (!strcmp(font_name, "Symb")) *font = "Symb";
  3418. else if (!strcmp(font_name, "ZaDb")) *font = "ZaDb";
  3419. else *font = "Helv";
  3420. }
  3421. void
  3422. pdf_print_default_appearance(fz_context *ctx, char *buf, int nbuf, const char *font, float size, int n, const float *color)
  3423. {
  3424. if (n == 4)
  3425. fz_snprintf(buf, nbuf, "/%s %g Tf %g %g %g %g k", font, size, color[0], color[1], color[2], color[3]);
  3426. else if (n == 3)
  3427. fz_snprintf(buf, nbuf, "/%s %g Tf %g %g %g rg", font, size, color[0], color[1], color[2]);
  3428. else if (n == 1)
  3429. fz_snprintf(buf, nbuf, "/%s %g Tf %g g", font, size, color[0]);
  3430. else
  3431. fz_snprintf(buf, nbuf, "/%s %g Tf", font, size);
  3432. }
  3433. void
  3434. pdf_annot_default_appearance_unmapped(fz_context *ctx, pdf_annot *annot, char *font_name, int font_name_len, float *size, int *n, float color[4])
  3435. {
  3436. pdf_obj *da;
  3437. pdf_annot_push_local_xref(ctx, annot);
  3438. fz_try(ctx)
  3439. {
  3440. da = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(DA));
  3441. if (!da)
  3442. {
  3443. pdf_obj *trailer = pdf_trailer(ctx, annot->page->doc);
  3444. da = pdf_dict_getl(ctx, trailer, PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DA), NULL);
  3445. }
  3446. pdf_parse_default_appearance_unmapped(ctx, pdf_to_str_buf(ctx, da), font_name, font_name_len, size, n, color);
  3447. }
  3448. fz_always(ctx)
  3449. pdf_annot_pop_local_xref(ctx, annot);
  3450. fz_catch(ctx)
  3451. fz_rethrow(ctx);
  3452. }
  3453. static pdf_obj *default_appearance_subtypes[] = {
  3454. PDF_NAME(FreeText),
  3455. PDF_NAME(Widget),
  3456. NULL,
  3457. };
  3458. int
  3459. pdf_annot_has_default_appearance(fz_context *ctx, pdf_annot *annot)
  3460. {
  3461. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(DA), default_appearance_subtypes);
  3462. }
  3463. void
  3464. pdf_annot_default_appearance(fz_context *ctx, pdf_annot *annot, const char **font, float *size, int *n, float color[4])
  3465. {
  3466. pdf_obj *da;
  3467. pdf_annot_push_local_xref(ctx, annot);
  3468. fz_try(ctx)
  3469. {
  3470. check_allowed_subtypes(ctx, annot, PDF_NAME(DA), default_appearance_subtypes);
  3471. da = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(DA));
  3472. if (!da)
  3473. {
  3474. pdf_obj *trailer = pdf_trailer(ctx, annot->page->doc);
  3475. da = pdf_dict_getl(ctx, trailer, PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(DA), NULL);
  3476. }
  3477. pdf_parse_default_appearance(ctx, pdf_to_str_buf(ctx, da), font, size, n, color);
  3478. }
  3479. fz_always(ctx)
  3480. pdf_annot_pop_local_xref(ctx, annot);
  3481. fz_catch(ctx)
  3482. fz_rethrow(ctx);
  3483. }
  3484. void
  3485. pdf_set_annot_default_appearance(fz_context *ctx, pdf_annot *annot, const char *font, float size, int n, const float *color)
  3486. {
  3487. char buf[100];
  3488. begin_annot_op(ctx, annot, "Set default appearance");
  3489. fz_try(ctx)
  3490. {
  3491. check_allowed_subtypes(ctx, annot, PDF_NAME(DA), default_appearance_subtypes);
  3492. pdf_print_default_appearance(ctx, buf, sizeof buf, font, size, n, color);
  3493. pdf_dict_put_string(ctx, annot->obj, PDF_NAME(DA), buf, strlen(buf));
  3494. pdf_dict_del(ctx, annot->obj, PDF_NAME(DS)); /* not supported */
  3495. pdf_dict_del(ctx, annot->obj, PDF_NAME(RC)); /* not supported */
  3496. end_annot_op(ctx, annot);
  3497. }
  3498. fz_catch(ctx)
  3499. {
  3500. abandon_annot_op(ctx, annot);
  3501. fz_rethrow(ctx);
  3502. }
  3503. pdf_dirty_annot(ctx, annot);
  3504. }
  3505. int pdf_annot_field_flags(fz_context *ctx, pdf_annot *annot)
  3506. {
  3507. int ret;
  3508. pdf_annot_push_local_xref(ctx, annot);
  3509. fz_try(ctx)
  3510. ret = pdf_field_flags(ctx, annot->obj);
  3511. fz_always(ctx)
  3512. pdf_annot_pop_local_xref(ctx, annot);
  3513. fz_catch(ctx)
  3514. fz_rethrow(ctx);
  3515. return ret;
  3516. }
  3517. const char *pdf_annot_field_value(fz_context *ctx, pdf_annot *widget)
  3518. {
  3519. const char *ret;
  3520. pdf_annot_push_local_xref(ctx, widget);
  3521. fz_try(ctx)
  3522. ret = pdf_field_value(ctx, widget->obj);
  3523. fz_always(ctx)
  3524. pdf_annot_pop_local_xref(ctx, widget);
  3525. fz_catch(ctx)
  3526. fz_rethrow(ctx);
  3527. return ret;
  3528. }
  3529. const char *pdf_annot_field_label(fz_context *ctx, pdf_annot *widget)
  3530. {
  3531. const char *ret;
  3532. pdf_annot_push_local_xref(ctx, widget);
  3533. fz_try(ctx)
  3534. ret = pdf_field_label(ctx, widget->obj);
  3535. fz_always(ctx)
  3536. pdf_annot_pop_local_xref(ctx, widget);
  3537. fz_catch(ctx)
  3538. fz_rethrow(ctx);
  3539. return ret;
  3540. }
  3541. int pdf_set_annot_field_value(fz_context *ctx, pdf_document *doc, pdf_annot *annot, const char *text, int ignore_trigger_events)
  3542. {
  3543. int ret;
  3544. begin_annot_op(ctx, annot, "Set field value");
  3545. fz_try(ctx)
  3546. {
  3547. ret = pdf_set_field_value(ctx, doc, annot->obj, text, ignore_trigger_events);
  3548. end_annot_op(ctx, annot);
  3549. }
  3550. fz_catch(ctx)
  3551. {
  3552. abandon_annot_op(ctx, annot);
  3553. fz_rethrow(ctx);
  3554. }
  3555. pdf_dirty_annot(ctx, annot);
  3556. return ret;
  3557. }
  3558. void
  3559. pdf_set_annot_appearance(fz_context *ctx, pdf_annot *annot, const char *appearance, const char *state, fz_matrix ctm, fz_rect bbox, pdf_obj *res, fz_buffer *contents)
  3560. {
  3561. pdf_obj *form = NULL;
  3562. pdf_obj *ap, *app;
  3563. pdf_obj *app_name = NULL;
  3564. begin_annot_op(ctx, annot, "Set appearance stream");
  3565. if (!appearance)
  3566. appearance = "N";
  3567. fz_var(form);
  3568. fz_var(app_name);
  3569. fz_try(ctx)
  3570. {
  3571. ap = pdf_dict_get(ctx, annot->obj, PDF_NAME(AP));
  3572. if (!ap)
  3573. ap = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(AP), 1);
  3574. if (!state)
  3575. form = pdf_keep_obj(ctx, pdf_dict_gets(ctx, ap, appearance));
  3576. else
  3577. {
  3578. if (strcmp(appearance, "N") && strcmp(appearance, "R") && strcmp(appearance, "D"))
  3579. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Unknown annotation appearance");
  3580. app_name = pdf_new_name(ctx, appearance);
  3581. app = pdf_dict_get(ctx, ap, app_name);
  3582. if (!app)
  3583. app = pdf_dict_put_dict(ctx, ap, app_name, 2);
  3584. form = pdf_keep_obj(ctx, pdf_dict_gets(ctx, ap, appearance));
  3585. }
  3586. /* Care required here. Some files have multiple annotations, which share
  3587. * appearance streams. As such, we must NOT reuse such appearance streams.
  3588. * On the other hand, we cannot afford to always recreate appearance
  3589. * streams, as this can lead to leakage of partial edits into the document.
  3590. * Any appearance we generate will be in the incremental section, and we
  3591. * will never generate shared appearances. As such, we can reuse an
  3592. * appearance object only if it is in the incremental section. */
  3593. if (!pdf_obj_is_incremental(ctx, form))
  3594. {
  3595. pdf_drop_obj(ctx, form);
  3596. form = NULL;
  3597. }
  3598. if (!pdf_is_dict(ctx, form))
  3599. {
  3600. pdf_drop_obj(ctx, form);
  3601. form = NULL;
  3602. form = pdf_new_xobject(ctx, annot->page->doc, bbox, ctm, res, contents);
  3603. }
  3604. else
  3605. pdf_update_xobject(ctx, annot->page->doc, form, bbox, ctm, res, contents);
  3606. if (!state)
  3607. pdf_dict_puts(ctx, ap, appearance, form);
  3608. else
  3609. pdf_dict_puts(ctx, app, state, form);
  3610. end_annot_op(ctx, annot);
  3611. }
  3612. fz_always(ctx)
  3613. {
  3614. pdf_drop_obj(ctx, form);
  3615. pdf_drop_obj(ctx, app_name);
  3616. }
  3617. fz_catch(ctx)
  3618. {
  3619. abandon_annot_op(ctx, annot);
  3620. fz_rethrow(ctx);
  3621. }
  3622. pdf_set_annot_resynthesised(ctx, annot);
  3623. }
  3624. void
  3625. pdf_set_annot_appearance_from_display_list(fz_context *ctx, pdf_annot *annot, const char *appearance, const char *state, fz_matrix ctm, fz_display_list *list)
  3626. {
  3627. pdf_document *doc;
  3628. fz_device *dev = NULL;
  3629. pdf_obj *res = NULL;
  3630. fz_buffer *contents = NULL;
  3631. /* Convert fitz-space mediabox to pdf-space bbox */
  3632. fz_rect mediabox = fz_bound_display_list(ctx, list);
  3633. fz_matrix transform = { 1, 0, 0, -1, -mediabox.x0, mediabox.y1 };
  3634. fz_rect bbox = fz_transform_rect(mediabox, transform);
  3635. fz_var(dev);
  3636. fz_var(contents);
  3637. fz_var(res);
  3638. begin_annot_op(ctx, annot, "Set appearance stream");
  3639. doc = annot->page->doc;
  3640. fz_try(ctx)
  3641. {
  3642. res = pdf_new_dict(ctx, doc, 1);
  3643. contents = fz_new_buffer(ctx, 0);
  3644. dev = pdf_new_pdf_device(ctx, doc, transform, res, contents);
  3645. fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
  3646. fz_close_device(ctx, dev);
  3647. fz_drop_device(ctx, dev);
  3648. dev = NULL;
  3649. pdf_set_annot_appearance(ctx, annot, appearance, state, ctm, bbox, res, contents);
  3650. end_annot_op(ctx, annot);
  3651. }
  3652. fz_always(ctx)
  3653. {
  3654. fz_drop_device(ctx, dev);
  3655. fz_drop_buffer(ctx, contents);
  3656. pdf_drop_obj(ctx, res);
  3657. }
  3658. fz_catch(ctx)
  3659. {
  3660. abandon_annot_op(ctx, annot);
  3661. fz_rethrow(ctx);
  3662. }
  3663. }
  3664. static pdf_obj *stamp_subtypes[] = {
  3665. PDF_NAME(Stamp),
  3666. NULL,
  3667. };
  3668. pdf_obj *
  3669. pdf_annot_stamp_image_obj(fz_context *ctx, pdf_annot *annot)
  3670. {
  3671. pdf_obj *obj, *imgobj = NULL;
  3672. pdf_annot_push_local_xref(ctx, annot);
  3673. fz_try(ctx)
  3674. {
  3675. obj = pdf_dict_getp(ctx, annot->obj, "AP/N/Resources/XObject");
  3676. if (pdf_dict_len(ctx, obj) == 1)
  3677. {
  3678. obj = pdf_dict_get_val(ctx, obj, 0);
  3679. if (pdf_is_image_stream(ctx, obj))
  3680. imgobj = obj;
  3681. }
  3682. }
  3683. fz_always(ctx)
  3684. pdf_annot_pop_local_xref(ctx, annot);
  3685. fz_catch(ctx)
  3686. fz_rethrow(ctx);
  3687. return imgobj;
  3688. }
  3689. void pdf_set_annot_stamp_image_obj(fz_context *ctx, pdf_annot *annot, pdf_obj *ref)
  3690. {
  3691. begin_annot_op(ctx, annot, "Set stamp image");
  3692. fz_try(ctx)
  3693. {
  3694. check_allowed_subtypes(ctx, annot, PDF_NAME(Stamp), stamp_subtypes);
  3695. pdf_dict_del(ctx, annot->obj, PDF_NAME(AP));
  3696. pdf_dict_putp(ctx, annot->obj, "AP/N/Resources/XObject/I", ref);
  3697. end_annot_op(ctx, annot);
  3698. }
  3699. fz_catch(ctx)
  3700. {
  3701. abandon_annot_op(ctx, annot);
  3702. fz_rethrow(ctx);
  3703. }
  3704. pdf_dirty_annot(ctx, annot);
  3705. }
  3706. void pdf_set_annot_stamp_image(fz_context *ctx, pdf_annot *annot, fz_image *img)
  3707. {
  3708. pdf_obj *ref = pdf_add_image(ctx, annot->page->doc, img);
  3709. fz_try(ctx)
  3710. pdf_set_annot_stamp_image_obj(ctx, annot, ref);
  3711. fz_always(ctx)
  3712. pdf_drop_obj(ctx, ref);
  3713. fz_catch(ctx)
  3714. fz_rethrow(ctx);
  3715. }
  3716. static pdf_obj *filespec_subtypes[] = {
  3717. PDF_NAME(FileAttachment),
  3718. NULL,
  3719. };
  3720. int
  3721. pdf_annot_has_filespec(fz_context *ctx, pdf_annot *annot)
  3722. {
  3723. return is_allowed_subtype_wrap(ctx, annot, PDF_NAME(FS), filespec_subtypes);
  3724. }
  3725. pdf_obj *
  3726. pdf_annot_filespec(fz_context *ctx, pdf_annot *annot)
  3727. {
  3728. pdf_obj *filespec;
  3729. pdf_annot_push_local_xref(ctx, annot);
  3730. fz_try(ctx)
  3731. {
  3732. check_allowed_subtypes(ctx, annot, PDF_NAME(FS), filespec_subtypes);
  3733. filespec = pdf_dict_get(ctx, annot->obj, PDF_NAME(FS));
  3734. }
  3735. fz_always(ctx)
  3736. pdf_annot_pop_local_xref(ctx, annot);
  3737. fz_catch(ctx)
  3738. fz_rethrow(ctx);
  3739. return filespec;
  3740. }
  3741. void
  3742. pdf_set_annot_filespec(fz_context *ctx, pdf_annot *annot, pdf_obj *fs)
  3743. {
  3744. if (fs != PDF_NULL && !pdf_is_embedded_file(ctx, fs))
  3745. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot set non-filespec as annotation filespec");
  3746. begin_annot_op(ctx, annot, "Set filespec");
  3747. fz_try(ctx)
  3748. {
  3749. check_allowed_subtypes(ctx, annot, PDF_NAME(M), filespec_subtypes);
  3750. pdf_dict_put(ctx, pdf_annot_obj(ctx, annot), PDF_NAME(FS), fs);
  3751. end_annot_op(ctx, annot);
  3752. }
  3753. fz_catch(ctx)
  3754. {
  3755. abandon_annot_op(ctx, annot);
  3756. fz_rethrow(ctx);
  3757. }
  3758. pdf_dirty_annot(ctx, annot);
  3759. }
  3760. int
  3761. pdf_annot_hidden_for_editing(fz_context *ctx, pdf_annot *annot)
  3762. {
  3763. return annot->hidden_editing;
  3764. }
  3765. void
  3766. pdf_set_annot_hidden_for_editing(fz_context *ctx, pdf_annot *annot, int hidden)
  3767. {
  3768. annot->hidden_editing = hidden;
  3769. }