pdf-appearance.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910
  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 "mupdf/ucdn.h"
  25. #include <float.h>
  26. #include <limits.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <stdio.h>
  30. #include "annotation-icons.h"
  31. /* #define PDF_DEBUG_APPEARANCE_SYNTHESIS */
  32. #define REPLACEMENT 0xB7
  33. #define CIRCLE_MAGIC 0.551915f
  34. static fz_point rect_center(const fz_rect rect)
  35. {
  36. fz_point c;
  37. c.x = (rect.x0 + rect.x1) / 2.0f;
  38. c.y = (rect.y0 + rect.y1) / 2.0f;
  39. return c;
  40. }
  41. static fz_matrix center_rect_within_rect(const fz_rect tofit, const fz_rect within)
  42. {
  43. float xscale = (within.x1 - within.x0) / (tofit.x1 - tofit.x0);
  44. float yscale = (within.y1 - within.y0) / (tofit.y1 - tofit.y0);
  45. float scale = fz_min(xscale, yscale);
  46. fz_point tofit_center;
  47. fz_point within_center;
  48. within_center = rect_center(within);
  49. tofit_center = rect_center(tofit);
  50. /* Translate "tofit" to be centered on the origin
  51. * Scale "tofit" to a size that fits within "within"
  52. * Translate "tofit" to "within's" center
  53. * Do all the above in reverse order so that we can use the fz_pre_xx functions */
  54. return fz_pre_translate(fz_pre_scale(fz_translate(within_center.x, within_center.y), scale, -scale), -tofit_center.x, -tofit_center.y);
  55. }
  56. static void
  57. draw_circle(fz_context *ctx, fz_buffer *buf, float rx, float ry, float cx, float cy)
  58. {
  59. float mx = rx * CIRCLE_MAGIC;
  60. float my = ry * CIRCLE_MAGIC;
  61. fz_append_printf(ctx, buf, "%g %g m\n", cx, cy+ry);
  62. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", cx+mx, cy+ry, cx+rx, cy+my, cx+rx, cy);
  63. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", cx+rx, cy-my, cx+mx, cy-ry, cx, cy-ry);
  64. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", cx-mx, cy-ry, cx-rx, cy-my, cx-rx, cy);
  65. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", cx-rx, cy+my, cx-mx, cy+ry, cx, cy+ry);
  66. }
  67. static void
  68. draw_circle_in_box(fz_context *ctx, fz_buffer *buf, float lw, float x0, float y0, float x1, float y1)
  69. {
  70. float rx = (x1 - x0) / 2 - lw/2;
  71. float ry = (y1 - y0) / 2 - lw/2;
  72. float cx = x0 + lw/2 + rx;
  73. float cy = y0 + lw/2 + ry;
  74. draw_circle(ctx, buf, rx, ry, cx, cy);
  75. }
  76. static void
  77. draw_arc_seg(fz_context *ctx, fz_buffer *buf, float r, float xc, float yc, float th0, float th1, int move)
  78. {
  79. float x1 = xc + r * cosf(th0);
  80. float y1 = yc + r * sinf(th0);
  81. float x4 = xc + r * cosf(th1);
  82. float y4 = yc + r * sinf(th1);
  83. float ax = x1 - xc;
  84. float ay = y1 - yc;
  85. float bx = x4 - xc;
  86. float by = y4 - yc;
  87. float q1 = ax * ax + ay * ay;
  88. float q2 = q1 + ax * bx + ay * by;
  89. float k2 = (4.0f/3.0f) * (sqrtf(2 * q1 * q2) - q2) / (ax * by - ay * bx);
  90. float x2 = xc + ax - k2 * ay;
  91. float y2 = yc + ay + k2 * ax;
  92. float x3 = xc + bx + k2 * by;
  93. float y3 = yc + by - k2 * bx;
  94. if (move)
  95. fz_append_printf(ctx, buf, "%g %g m\n", x1, y1);
  96. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x2, y2, x3, y3, x4, y4);
  97. }
  98. static void
  99. draw_arc(fz_context *ctx, fz_buffer *buf, float r, float xc, float yc, float th0, float th1, int move)
  100. {
  101. float d = th0 - th1;
  102. if (d > FZ_PI / 4)
  103. {
  104. draw_arc(ctx, buf, r, xc, yc, th0, th0 - d / 2, move);
  105. draw_arc(ctx, buf, r, xc, yc, th0 - d / 2, th1, 0);
  106. }
  107. else
  108. {
  109. draw_arc_seg(ctx, buf, r, xc, yc, th0, th1, move);
  110. }
  111. }
  112. static void
  113. draw_arc_tail(fz_context *ctx, fz_buffer *buf, float r, float xc, float yc, float th0, float th1, int fill)
  114. {
  115. draw_arc_seg(ctx, buf, r, xc, yc, th0, th1, 0);
  116. if (fill)
  117. draw_arc_seg(ctx, buf, r, xc, yc, th1, th0, 0);
  118. }
  119. static void
  120. pdf_write_opacity_blend_mode(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, pdf_obj **res, int bm)
  121. {
  122. pdf_obj *res_egs, *res_egs_h;
  123. float opacity = pdf_annot_opacity(ctx, annot);
  124. if (bm == FZ_BLEND_NORMAL && opacity == 1)
  125. return;
  126. /* /Resources << /ExtGState << /H << /Type/ExtGState /BM/Multiply /CA %g /ca %g >> >> >> */
  127. if (!*res)
  128. *res = pdf_new_dict(ctx, annot->page->doc, 1);
  129. res_egs = pdf_dict_put_dict(ctx, *res, PDF_NAME(ExtGState), 1);
  130. res_egs_h = pdf_dict_put_dict(ctx, res_egs, PDF_NAME(H), 2);
  131. pdf_dict_put(ctx, res_egs_h, PDF_NAME(Type), PDF_NAME(ExtGState));
  132. if (bm == FZ_BLEND_MULTIPLY)
  133. {
  134. pdf_dict_put(ctx, res_egs_h, PDF_NAME(BM), PDF_NAME(Multiply));
  135. }
  136. if (opacity < 1)
  137. {
  138. pdf_dict_put_real(ctx, res_egs_h, PDF_NAME(CA), opacity);
  139. pdf_dict_put_real(ctx, res_egs_h, PDF_NAME(ca), opacity);
  140. }
  141. fz_append_printf(ctx, buf, "/H gs\n");
  142. }
  143. static void
  144. pdf_write_opacity(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, pdf_obj **res)
  145. {
  146. pdf_write_opacity_blend_mode(ctx, annot, buf, res, FZ_BLEND_NORMAL);
  147. }
  148. static void
  149. pdf_write_dash_pattern(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, pdf_obj **res)
  150. {
  151. int count = pdf_annot_border_dash_count(ctx, annot);
  152. int i;
  153. if (count == 0)
  154. return;
  155. fz_append_printf(ctx, buf, "[");
  156. for (i = 0; i < count; ++i)
  157. {
  158. float length = pdf_annot_border_dash_item(ctx, annot, i);
  159. if (i == 0)
  160. fz_append_printf(ctx, buf, "%g", length);
  161. else
  162. fz_append_printf(ctx, buf, " %g", length);
  163. }
  164. fz_append_printf(ctx, buf, "]0 d\n");
  165. }
  166. static float pdf_write_border_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf)
  167. {
  168. float w = pdf_annot_border_width(ctx, annot);
  169. fz_append_printf(ctx, buf, "%g w\n", w);
  170. return w;
  171. }
  172. static int
  173. write_color(fz_context *ctx, fz_buffer *buf, int n, float *color, int stroke)
  174. {
  175. if (n == 4)
  176. fz_append_printf(ctx, buf, "%g %g %g %g %c\n", color[0], color[1], color[2], color[3], stroke ? 'K' : 'k');
  177. else if (n == 3)
  178. fz_append_printf(ctx, buf, "%g %g %g %s\n", color[0], color[1], color[2], stroke ? "RG" : "rg");
  179. else if (n == 1)
  180. fz_append_printf(ctx, buf, "%g %c\n", color[0], stroke ? 'G' : 'g');
  181. else
  182. return 0;
  183. return 1;
  184. }
  185. static void
  186. write_color0(fz_context *ctx, fz_buffer *buf, int n, float *color, int stroke)
  187. {
  188. if (n == 0)
  189. fz_append_printf(ctx, buf, "0 %c\n", stroke ? 'G' : 'g');
  190. else
  191. write_color(ctx, buf, n, color, stroke);
  192. }
  193. static int pdf_write_stroke_color_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf)
  194. {
  195. float color[4];
  196. int n;
  197. pdf_annot_color(ctx, annot, &n, color);
  198. return write_color(ctx, buf, n, color, 1);
  199. }
  200. static int pdf_is_dark_fill_color(fz_context *ctx, pdf_annot *annot)
  201. {
  202. float color[4], gray;
  203. int n;
  204. pdf_annot_color(ctx, annot, &n, color);
  205. switch (n)
  206. {
  207. default:
  208. gray = 1;
  209. break;
  210. case 1:
  211. gray = color[0];
  212. break;
  213. case 3:
  214. gray = color[0] * 0.3f + color[1] * 0.59f + color[2] * 0.11f;
  215. break;
  216. case 4:
  217. gray = color[0] * 0.3f + color[1] * 0.59f + color[2] * 0.11f + color[3];
  218. gray = 1 - fz_min(gray, 1);
  219. break;
  220. }
  221. return gray < 0.25f;
  222. }
  223. static int pdf_write_fill_color_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf)
  224. {
  225. float color[4];
  226. int n;
  227. pdf_annot_color(ctx, annot, &n, color);
  228. return write_color(ctx, buf, n, color, 0);
  229. }
  230. static int pdf_write_interior_fill_color_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf)
  231. {
  232. float color[4];
  233. int n;
  234. pdf_annot_interior_color(ctx, annot, &n, color);
  235. return write_color(ctx, buf, n, color, 0);
  236. }
  237. static int pdf_write_MK_BG_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf)
  238. {
  239. float color[4];
  240. int n;
  241. pdf_annot_MK_BG(ctx, annot, &n, color);
  242. return write_color(ctx, buf, n, color, 0);
  243. }
  244. static int pdf_write_MK_BC_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf)
  245. {
  246. float color[4];
  247. int n;
  248. pdf_annot_MK_BC(ctx, annot, &n, color);
  249. return write_color(ctx, buf, n, color, 1);
  250. }
  251. static void maybe_stroke_and_fill(fz_context *ctx, fz_buffer *buf, int sc, int ic)
  252. {
  253. if (sc)
  254. fz_append_string(ctx, buf, ic ? "b\n" : "S\n");
  255. else
  256. fz_append_string(ctx, buf, ic ? "f\n" : "n\n");
  257. }
  258. static void maybe_stroke(fz_context *ctx, fz_buffer *buf, int sc)
  259. {
  260. fz_append_string(ctx, buf, sc ? "S\n" : "n\n");
  261. }
  262. static fz_point rotate_vector(float angle, float x, float y)
  263. {
  264. float ca = cosf(angle);
  265. float sa = sinf(angle);
  266. return fz_make_point(x*ca - y*sa, x*sa + y*ca);
  267. }
  268. static void pdf_write_arrow_appearance(fz_context *ctx, fz_buffer *buf, fz_rect *rect, float x, float y, float dx, float dy, float w, int close)
  269. {
  270. float r = fz_max(1, w);
  271. float angle = atan2f(dy, dx);
  272. fz_point v, a, b;
  273. v = rotate_vector(angle, 8.8f*r, 4.5f*r);
  274. a = fz_make_point(x + v.x, y + v.y);
  275. v = rotate_vector(angle, 8.8f*r, -4.5f*r);
  276. b = fz_make_point(x + v.x, y + v.y);
  277. *rect = fz_include_point_in_rect(*rect, a);
  278. *rect = fz_include_point_in_rect(*rect, b);
  279. *rect = fz_expand_rect(*rect, w);
  280. fz_append_printf(ctx, buf, "%g %g m\n", a.x, a.y);
  281. fz_append_printf(ctx, buf, "%g %g l\n", x, y);
  282. fz_append_printf(ctx, buf, "%g %g l\n", b.x, b.y);
  283. if (close)
  284. fz_append_printf(ctx, buf, "h\n");
  285. }
  286. static void include_cap(fz_rect *rect, float x, float y, float r)
  287. {
  288. rect->x0 = fz_min(rect->x0, x-r);
  289. rect->y0 = fz_min(rect->y0, y-r);
  290. rect->x1 = fz_max(rect->x1, x+r);
  291. rect->y1 = fz_max(rect->y1, y+r);
  292. }
  293. static void
  294. pdf_write_line_cap_appearance(fz_context *ctx, fz_buffer *buf, fz_rect *rect,
  295. float x, float y, float dx, float dy, float w,
  296. int sc, int ic, pdf_obj *cap)
  297. {
  298. float l = sqrtf(dx*dx + dy*dy);
  299. dx = dx / l;
  300. dy = dy / l;
  301. if (cap == PDF_NAME(Square))
  302. {
  303. float r = fz_max(3.0f, w * 3.0f);
  304. fz_append_printf(ctx, buf, "%g %g %g %g re\n", x-r, y-r, r*2, r*2);
  305. maybe_stroke_and_fill(ctx, buf, sc, ic);
  306. include_cap(rect, x, y, r + w/2);
  307. }
  308. else if (cap == PDF_NAME(Circle))
  309. {
  310. float r = fz_max(3.0f, w * 3.0f);
  311. float m = r * CIRCLE_MAGIC;
  312. fz_append_printf(ctx, buf, "%g %g m\n", x, y+r);
  313. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x+m, y+r, x+r, y+m, x+r, y);
  314. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x+r, y-m, x+m, y-r, x, y-r);
  315. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x-m, y-r, x-r, y-m, x-r, y);
  316. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n", x-r, y+m, x-m, y+r, x, y+r);
  317. maybe_stroke_and_fill(ctx, buf, sc, ic);
  318. include_cap(rect, x, y, r + w/2);
  319. }
  320. else if (cap == PDF_NAME(Diamond))
  321. {
  322. float r = fz_max(3.0f, w * 3.0f);
  323. fz_append_printf(ctx, buf, "%g %g m\n", x, y+r);
  324. fz_append_printf(ctx, buf, "%g %g l\n", x+r, y);
  325. fz_append_printf(ctx, buf, "%g %g l\n", x, y-r);
  326. fz_append_printf(ctx, buf, "%g %g l\n", x-r, y);
  327. fz_append_printf(ctx, buf, "h\n");
  328. maybe_stroke_and_fill(ctx, buf, sc, ic);
  329. include_cap(rect, x, y, r + w/sqrtf(2));
  330. }
  331. else if (cap == PDF_NAME(OpenArrow))
  332. {
  333. pdf_write_arrow_appearance(ctx, buf, rect, x, y, dx, dy, w, 0);
  334. maybe_stroke(ctx, buf, sc);
  335. }
  336. else if (cap == PDF_NAME(ClosedArrow))
  337. {
  338. pdf_write_arrow_appearance(ctx, buf, rect, x, y, dx, dy, w, 1);
  339. maybe_stroke_and_fill(ctx, buf, sc, ic);
  340. }
  341. /* PDF 1.5 */
  342. else if (cap == PDF_NAME(Butt))
  343. {
  344. float r = fz_max(3, w * 3);
  345. fz_point a = { x-dy*r, y+dx*r };
  346. fz_point b = { x+dy*r, y-dx*r };
  347. fz_append_printf(ctx, buf, "%g %g m\n", a.x, a.y);
  348. fz_append_printf(ctx, buf, "%g %g l\n", b.x, b.y);
  349. maybe_stroke(ctx, buf, sc);
  350. *rect = fz_include_point_in_rect(*rect, a);
  351. *rect = fz_include_point_in_rect(*rect, a);
  352. *rect = fz_expand_rect(*rect, w);
  353. }
  354. else if (cap == PDF_NAME(ROpenArrow))
  355. {
  356. pdf_write_arrow_appearance(ctx, buf, rect, x, y, -dx, -dy, w, 0);
  357. maybe_stroke(ctx, buf, sc);
  358. }
  359. else if (cap == PDF_NAME(RClosedArrow))
  360. {
  361. pdf_write_arrow_appearance(ctx, buf, rect, x, y, -dx, -dy, w, 1);
  362. maybe_stroke_and_fill(ctx, buf, sc, ic);
  363. }
  364. /* PDF 1.6 */
  365. else if (cap == PDF_NAME(Slash))
  366. {
  367. float r = fz_max(5, w * 5);
  368. float angle = atan2f(dy, dx) - (30 * FZ_PI / 180);
  369. fz_point a, b, v;
  370. v = rotate_vector(angle, 0, r);
  371. a = fz_make_point(x + v.x, y + v.y);
  372. v = rotate_vector(angle, 0, -r);
  373. b = fz_make_point(x + v.x, y + v.y);
  374. fz_append_printf(ctx, buf, "%g %g m\n", a.x, a.y);
  375. fz_append_printf(ctx, buf, "%g %g l\n", b.x, b.y);
  376. maybe_stroke(ctx, buf, sc);
  377. *rect = fz_include_point_in_rect(*rect, a);
  378. *rect = fz_include_point_in_rect(*rect, b);
  379. *rect = fz_expand_rect(*rect, w);
  380. }
  381. }
  382. static float pdf_write_line_caption(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res, fz_point a, fz_point b);
  383. static void
  384. pdf_write_line_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  385. {
  386. pdf_obj *line, *le;
  387. float w;
  388. fz_point a, b, aa, ab, ba, bb;
  389. float dx, dy, line_length, ll, lle, llo;
  390. int sc;
  391. int ic;
  392. // The start and end point of the actual line (a, b)
  393. // The start and end points of the leader line (aa, ab, ba, bb)
  394. pdf_write_opacity(ctx, annot, buf, res);
  395. pdf_write_dash_pattern(ctx, annot, buf, res);
  396. w = pdf_write_border_appearance(ctx, annot, buf);
  397. sc = pdf_write_stroke_color_appearance(ctx, annot, buf);
  398. ic = pdf_write_interior_fill_color_appearance(ctx, annot, buf);
  399. line = pdf_dict_get(ctx, annot->obj, PDF_NAME(L));
  400. a.x = pdf_array_get_real(ctx, line, 0);
  401. a.y = pdf_array_get_real(ctx, line, 1);
  402. b.x = pdf_array_get_real(ctx, line, 2);
  403. b.y = pdf_array_get_real(ctx, line, 3);
  404. /* vector of line */
  405. dx = b.x - a.x;
  406. dy = b.y - a.y;
  407. line_length = hypotf(dx, dy);
  408. dx /= line_length;
  409. dy /= line_length;
  410. rect->x0 = fz_min(a.x, b.x);
  411. rect->y0 = fz_min(a.y, b.y);
  412. rect->x1 = fz_max(a.x, b.x);
  413. rect->y1 = fz_max(a.y, b.y);
  414. ll = pdf_dict_get_real(ctx, annot->obj, PDF_NAME(LL));
  415. if (ll != 0)
  416. {
  417. lle = pdf_dict_get_real(ctx, annot->obj, PDF_NAME(LLE));
  418. llo = pdf_dict_get_real(ctx, annot->obj, PDF_NAME(LLO));
  419. if (ll < 0) {
  420. lle = -lle;
  421. llo = -llo;
  422. }
  423. aa = fz_make_point(a.x - dy * (llo), a.y + dx * (llo));
  424. ba = fz_make_point(b.x - dy * (llo), b.y + dx * (llo));
  425. ab = fz_make_point(a.x - dy * (llo + ll + lle), a.y + dx * (llo + ll + lle));
  426. bb = fz_make_point(b.x - dy * (llo + ll + lle), b.y + dx * (llo + ll + lle));
  427. a = fz_make_point(a.x - dy * (llo + ll), a.y + dx * (llo + ll));
  428. b = fz_make_point(b.x - dy * (llo + ll), b.y + dx * (llo + ll));
  429. fz_append_printf(ctx, buf, "%g %g m\n%g %g l\n", aa.x, aa.y, ab.x, ab.y);
  430. fz_append_printf(ctx, buf, "%g %g m\n%g %g l\n", ba.x, ba.y, bb.x, bb.y);
  431. *rect = fz_include_point_in_rect(*rect, a);
  432. *rect = fz_include_point_in_rect(*rect, b);
  433. *rect = fz_include_point_in_rect(*rect, ab);
  434. *rect = fz_include_point_in_rect(*rect, bb);
  435. }
  436. if (pdf_dict_get_bool(ctx, annot->obj, PDF_NAME(Cap)))
  437. {
  438. float gap = pdf_write_line_caption(ctx, annot, buf, rect, res, a, b);
  439. if (gap > 0)
  440. {
  441. fz_point m = fz_make_point((a.x + b.x) / 2, (a.y + b.y) / 2);
  442. fz_point ca = fz_make_point(m.x - dx * gap, m.y - dy * gap);
  443. fz_point cb = fz_make_point(m.x + dx * gap, m.y + dy * gap);
  444. fz_append_printf(ctx, buf, "%g %g m\n%g %g l\n", a.x, a.y, ca.x, ca.y);
  445. fz_append_printf(ctx, buf, "%g %g m\n%g %g l\n", cb.x, cb.y, b.x, b.y);
  446. }
  447. else
  448. {
  449. fz_append_printf(ctx, buf, "%g %g m\n%g %g l\n", a.x, a.y, b.x, b.y);
  450. }
  451. }
  452. else
  453. {
  454. fz_append_printf(ctx, buf, "%g %g m\n%g %g l\n", a.x, a.y, b.x, b.y);
  455. }
  456. maybe_stroke(ctx, buf, sc);
  457. le = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  458. if (pdf_array_len(ctx, le) == 2)
  459. {
  460. dx = b.x - a.x;
  461. dy = b.y - a.y;
  462. pdf_write_line_cap_appearance(ctx, buf, rect, a.x, a.y, dx, dy, w, sc, ic, pdf_array_get(ctx, le, 0));
  463. pdf_write_line_cap_appearance(ctx, buf, rect, b.x, b.y, -dx, -dy, w, sc, ic, pdf_array_get(ctx, le, 1));
  464. }
  465. *rect = fz_expand_rect(*rect, fz_max(1, w));
  466. }
  467. /* The rect diff is NOT an fz_rect. It's differences between
  468. * 2 rects. We return it as a rect for convenience. */
  469. fz_rect
  470. pdf_annot_rect_diff(fz_context *ctx, pdf_annot *annot)
  471. {
  472. pdf_obj *rd_obj = pdf_dict_get(ctx, annot->obj, PDF_NAME(RD));
  473. fz_rect rd;
  474. if (!pdf_is_array(ctx, rd_obj))
  475. return fz_make_rect(0, 0, 0, 0);
  476. rd.x0 = pdf_array_get_real(ctx, rd_obj, 0);
  477. rd.y0 = pdf_array_get_real(ctx, rd_obj, 1);
  478. rd.x1 = pdf_array_get_real(ctx, rd_obj, 2);
  479. rd.y1 = pdf_array_get_real(ctx, rd_obj, 3);
  480. return rd;
  481. }
  482. static float
  483. cloud_intensity(fz_context *ctx, pdf_annot *annot)
  484. {
  485. if (pdf_annot_border_effect(ctx, annot) == PDF_BORDER_EFFECT_CLOUDY)
  486. return pdf_annot_border_effect_intensity(ctx, annot);
  487. return 0;
  488. }
  489. struct cloud_list {
  490. // store first 2 and latest 3 points
  491. fz_point data[5];
  492. int len;
  493. int first;
  494. int fill;
  495. float spacing, radius, phase;
  496. };
  497. static float intersect_cloud(fz_point p0, fz_point p1, float r, int sel)
  498. {
  499. float dx = p1.x - p0.x;
  500. float dy = p1.y - p0.y;
  501. float d = sqrtf(dx * dx + dy * dy);
  502. float x2, y2, x3, y3;
  503. float a, h;
  504. if (d >= r + r) return 0;
  505. if (d <= 0) return 0;
  506. a = d / 2;
  507. h = sqrtf(r * r - a * a);
  508. x2 = (p0.x + p1.x) / 2;
  509. y2 = (p0.y + p1.y) / 2;
  510. x3 = x2 - h * (p1.y - p0.y) / d;
  511. y3 = y2 + h * (p1.x - p0.x) / d;
  512. if (sel == 0)
  513. return atan2(y3 - p1.y, x3 - p1.x);
  514. else
  515. return atan2(y3 - p0.y, x3 - p0.x);
  516. }
  517. static void start_cloud(fz_context *ctx, struct cloud_list *list, fz_buffer *buf, float border, float intensity, int fill)
  518. {
  519. // Constants measured from Acrobat Reader
  520. list->spacing = intensity * 6.666667f + border * 0.8333333f;
  521. list->radius = intensity * 4.0f + border * 0.5f;
  522. list->phase = 0;
  523. list->len = 0;
  524. list->first = 1;
  525. list->fill = fill;
  526. fz_append_string(ctx, buf, "2 j\n"); // bevel join
  527. }
  528. static void emit_cloud(fz_context *ctx, struct cloud_list *list, fz_buffer *buf, fz_point a, fz_point b, fz_point c)
  529. {
  530. float th0 = intersect_cloud(a, b, list->radius, 0);
  531. float th1 = intersect_cloud(b, c, list->radius, 1);
  532. while (th1 > th0)
  533. th1 -= FZ_PI * 2;
  534. draw_arc(ctx, buf, list->radius, b.x, b.y, th0, th1, list->first || !list->fill);
  535. draw_arc_tail(ctx, buf, list->radius, b.x, b.y, th1, th1 - FZ_PI / 8, list->fill);
  536. list->first = 0;
  537. }
  538. static void add_cloud(fz_context *ctx, struct cloud_list *list, fz_buffer *buf, float x, float y)
  539. {
  540. if (list->len < 5)
  541. {
  542. list->data[list->len].x = x;
  543. list->data[list->len].y = y;
  544. list->len++;
  545. }
  546. else
  547. {
  548. list->data[2] = list->data[3];
  549. list->data[3] = list->data[4];
  550. list->data[4].x = x;
  551. list->data[4].y = y;
  552. }
  553. if (list->len >= 3)
  554. {
  555. emit_cloud(ctx, list, buf, list->data[list->len-3], list->data[list->len-2], list->data[list->len-1]);
  556. }
  557. }
  558. static void add_cloud_line(fz_context *ctx, struct cloud_list *list, fz_buffer *buf, float x0, float y0, float x1, float y1)
  559. {
  560. float dx = x1 - x0;
  561. float dy = y1 - y0;
  562. float total = hypotf(dx, dy);
  563. float used = 0;
  564. float t;
  565. if (list->phase == 0)
  566. add_cloud(ctx, list, buf, x0, y0);
  567. while (total - used > list->spacing - list->phase)
  568. {
  569. used += list->spacing - list->phase;
  570. t = used / total;
  571. add_cloud(ctx, list, buf, x0 + dx * t, y0 + dy * t);
  572. list->phase = 0;
  573. }
  574. list->phase += total - used;
  575. }
  576. static void add_cloud_circle(fz_context *ctx, struct cloud_list *list, fz_buffer *buf, float x0, float y0, float x1, float y1)
  577. {
  578. float cx = (x0 + x1) / 2;
  579. float cy = (y0 + y1) / 2;
  580. float rx = (x1 - x0) / 2;
  581. float ry = (y1 - y0) / 2;
  582. float da = -FZ_PI * 2 / 32;
  583. int i;
  584. for (i = 1; i <= 32; ++i) {
  585. float ax = cx + cosf((i-1) * da) * rx;
  586. float ay = cy + sinf((i-1) * da) * ry;
  587. float bx = cx + cosf(i * da) * rx;
  588. float by = cy + sinf(i * da) * ry;
  589. add_cloud_line(ctx, list, buf, ax, ay, bx, by);
  590. }
  591. }
  592. static void end_cloud(fz_context *ctx, struct cloud_list *list, fz_buffer *buf)
  593. {
  594. // Join up with last and first circles.
  595. switch (list->len)
  596. {
  597. case 0:
  598. break;
  599. case 1:
  600. draw_circle(ctx, buf, list->radius, list->radius, list->data[0].x, list->data[0].y);
  601. break;
  602. case 2:
  603. emit_cloud(ctx, list, buf, list->data[0], list->data[1], list->data[0]);
  604. emit_cloud(ctx, list, buf, list->data[1], list->data[0], list->data[1]);
  605. break;
  606. case 3:
  607. emit_cloud(ctx, list, buf, list->data[1], list->data[2], list->data[0]);
  608. emit_cloud(ctx, list, buf, list->data[2], list->data[0], list->data[1]);
  609. break;
  610. case 4:
  611. emit_cloud(ctx, list, buf, list->data[2], list->data[3], list->data[0]);
  612. emit_cloud(ctx, list, buf, list->data[3], list->data[0], list->data[1]);
  613. break;
  614. case 5:
  615. emit_cloud(ctx, list, buf, list->data[3], list->data[4], list->data[0]);
  616. emit_cloud(ctx, list, buf, list->data[4], list->data[0], list->data[1]);
  617. break;
  618. }
  619. }
  620. static void
  621. pdf_write_square_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  622. {
  623. struct cloud_list cloud_list;
  624. fz_rect orect, rd;
  625. float x, y, w, h;
  626. float lw;
  627. int sc;
  628. int ic;
  629. float cloud;
  630. float exp;
  631. pdf_write_opacity(ctx, annot, buf, res);
  632. pdf_write_dash_pattern(ctx, annot, buf, res);
  633. lw = pdf_write_border_appearance(ctx, annot, buf);
  634. sc = pdf_write_stroke_color_appearance(ctx, annot, buf);
  635. ic = pdf_write_interior_fill_color_appearance(ctx, annot, buf);
  636. orect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  637. rd = pdf_annot_rect_diff(ctx, annot);
  638. /* We have various rules that we need to follow here:
  639. * 1) No part of what we draw should extend outside of 'Rect'.
  640. * 2) The 'centre' of the border should be on 'Rect+RD'.
  641. * 3) RD and linewidth will therefore have problems if they are too large.
  642. * We do our best to cope with all of these.
  643. */
  644. exp = lw/2;
  645. if (rd.x0 < exp)
  646. rd.x0 = exp;
  647. if (rd.x1 < exp)
  648. rd.x1 = exp;
  649. if (rd.y0 < exp)
  650. rd.y0 = exp;
  651. if (rd.y1 < exp)
  652. rd.y1 = exp;
  653. x = orect.x0 + rd.x0;
  654. y = orect.y0 + rd.y0;
  655. w = orect.x1 - orect.x0 - rd.x0 - rd.x1;
  656. h = orect.y1 - orect.y0 - rd.y0 - rd.y1;
  657. if (w < 1) w = 1;
  658. if (h < 1) h = 1;
  659. cloud = cloud_intensity(ctx, annot);
  660. if (cloud > 0)
  661. {
  662. start_cloud(ctx, &cloud_list, buf, lw, cloud, ic);
  663. add_cloud_line(ctx, &cloud_list, buf, x, y, x, y+h);
  664. cloud_list.phase = 0;
  665. add_cloud_line(ctx, &cloud_list, buf, x, y+h, x+w, y+h);
  666. cloud_list.phase = 0;
  667. add_cloud_line(ctx, &cloud_list, buf, x+w, y+h, x+w, y);
  668. cloud_list.phase = 0;
  669. add_cloud_line(ctx, &cloud_list, buf, x+w, y, x, y);
  670. end_cloud(ctx, &cloud_list, buf);
  671. exp += cloud_list.radius;
  672. if (rd.x0 < exp)
  673. rd.x0 = exp;
  674. if (rd.x1 < exp)
  675. rd.x1 = exp;
  676. if (rd.y0 < exp)
  677. rd.y0 = exp;
  678. if (rd.y1 < exp)
  679. rd.y1 = exp;
  680. }
  681. else
  682. {
  683. fz_append_printf(ctx, buf, "%g %g %g %g re\n", x, y, w, h);
  684. }
  685. maybe_stroke_and_fill(ctx, buf, sc, ic);
  686. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), rd);
  687. rect->x0 = x - rd.x0;
  688. rect->y0 = y - rd.y0;
  689. rect->x1 = x + w + rd.x1;
  690. rect->y1 = y + h + rd.y1;
  691. }
  692. static void
  693. pdf_write_circle_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  694. {
  695. struct cloud_list cloud_list;
  696. fz_rect orect, rd;
  697. float x, y, w, h;
  698. float lw;
  699. int sc;
  700. int ic;
  701. float cloud;
  702. float exp;
  703. pdf_write_opacity(ctx, annot, buf, res);
  704. pdf_write_dash_pattern(ctx, annot, buf, res);
  705. lw = pdf_write_border_appearance(ctx, annot, buf);
  706. sc = pdf_write_stroke_color_appearance(ctx, annot, buf);
  707. ic = pdf_write_interior_fill_color_appearance(ctx, annot, buf);
  708. orect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  709. rd = pdf_annot_rect_diff(ctx, annot);
  710. /* We have various rules that we need to follow here:
  711. * 1) No part of what we draw should extend outside of 'Rect'.
  712. * 2) The 'centre' of the border should be on 'Rect+RD'.
  713. * 3) RD and linewidth will therefore have problems if they are too large.
  714. * We do our best to cope with all of these.
  715. */
  716. exp = lw/2;
  717. if (rd.x0 < exp)
  718. rd.x0 = exp;
  719. if (rd.x1 < exp)
  720. rd.x1 = exp;
  721. if (rd.y0 < exp)
  722. rd.y0 = exp;
  723. if (rd.y1 < exp)
  724. rd.y1 = exp;
  725. x = orect.x0 + rd.x0;
  726. y = orect.y0 + rd.y0;
  727. w = orect.x1 - orect.x0 - rd.x0 - rd.x1;
  728. h = orect.y1 - orect.y0 - rd.y0 - rd.y1;
  729. if (w < 1) w = 1;
  730. if (h < 1) h = 1;
  731. cloud = cloud_intensity(ctx, annot);
  732. if (cloud > 0)
  733. {
  734. start_cloud(ctx, &cloud_list, buf, lw, cloud, ic);
  735. add_cloud_circle(ctx, &cloud_list, buf, x, y, x+w, y+h);
  736. end_cloud(ctx, &cloud_list, buf);
  737. exp += cloud_list.radius;
  738. if (rd.x0 < exp)
  739. rd.x0 = exp;
  740. if (rd.x1 < exp)
  741. rd.x1 = exp;
  742. if (rd.y0 < exp)
  743. rd.y0 = exp;
  744. if (rd.y1 < exp)
  745. rd.y1 = exp;
  746. }
  747. else
  748. {
  749. draw_circle(ctx, buf, w / 2, h / 2, x + w / 2, y + h / 2);
  750. }
  751. maybe_stroke_and_fill(ctx, buf, sc, ic);
  752. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), rd);
  753. rect->x0 = x - rd.x0;
  754. rect->y0 = y - rd.x1;
  755. rect->x1 = x + w + rd.x1;
  756. rect->y1 = y + h + rd.y1;
  757. }
  758. /*
  759. * This doesn't quite match Acrobat, but I haven't really got a clue
  760. * what magic algorithm Acrobat is using. It'll match for all 'simple'
  761. * polygons, and makes a reasonable stab for complex ones.
  762. *
  763. * Find the y position of the centre of the polygon. Sum the
  764. * area * winding_number across that line. (Effectively an integration?)
  765. * Then pick cw or ccw according to what will be leave the largest
  766. * area correct.
  767. */
  768. static int
  769. polygon_winding(fz_context *ctx, pdf_obj *v, int n)
  770. {
  771. int i;
  772. float mid_y = 0;
  773. float min_x = 0;
  774. fz_point q, r;
  775. float area = 0;
  776. /* Find the centre for y, and min x. */
  777. for (i = 0; i < n; i++)
  778. {
  779. float x = pdf_array_get_real(ctx, v, 2*i);
  780. if (x < min_x || i == 0)
  781. min_x = x;
  782. mid_y += pdf_array_get_real(ctx, v, 2*i+1);
  783. }
  784. mid_y /= n;
  785. /* Now run through finding the weighted area across that middle line. */
  786. q.x = pdf_array_get_real(ctx, v, 0);
  787. q.y = pdf_array_get_real(ctx, v, 1);
  788. for (i = 1; i < n; i++)
  789. {
  790. r.x = pdf_array_get_real(ctx, v, 2*i);
  791. r.y = pdf_array_get_real(ctx, v, 2*i+1);
  792. if ((r.y > mid_y && mid_y > q.y) || (r.y < mid_y && mid_y < q.y))
  793. {
  794. int w = (r.y > mid_y) ? 1 : -1;
  795. float x = r.x + (q.x - r.x) * (r.y - mid_y) / (r.y - q.y);
  796. area += (x - min_x) * w;
  797. }
  798. q = r;
  799. }
  800. return area < 0;
  801. }
  802. static void
  803. pdf_write_polygon_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res, int close)
  804. {
  805. struct cloud_list cloud_list;
  806. float cloud = 0;
  807. pdf_obj *verts, *le;
  808. fz_point p, first = {0,0}, last = {0,0};
  809. int i, n;
  810. float lw;
  811. int sc, ic;
  812. int i0, i1, is;
  813. float exp = 0;
  814. pdf_write_opacity(ctx, annot, buf, res);
  815. pdf_write_dash_pattern(ctx, annot, buf, res);
  816. lw = pdf_write_border_appearance(ctx, annot, buf);
  817. sc = pdf_write_stroke_color_appearance(ctx, annot, buf);
  818. ic = pdf_write_interior_fill_color_appearance(ctx, annot, buf);
  819. *rect = fz_empty_rect;
  820. if (close)
  821. cloud = cloud_intensity(ctx, annot);
  822. verts = pdf_dict_get(ctx, annot->obj, PDF_NAME(Vertices));
  823. n = pdf_array_len(ctx, verts) / 2;
  824. if (n > 0)
  825. {
  826. if (polygon_winding(ctx, verts, n))
  827. i0 = 0, i1 = n, is = 1;
  828. else
  829. i0 = n-1, i1 = -1, is = -1;
  830. if (cloud > 0)
  831. {
  832. start_cloud(ctx, &cloud_list, buf, lw, cloud, ic);
  833. }
  834. for (i = i0; i != i1; i += is)
  835. {
  836. p.x = pdf_array_get_real(ctx, verts, i*2+0);
  837. p.y = pdf_array_get_real(ctx, verts, i*2+1);
  838. if (i == i0)
  839. {
  840. rect->x0 = rect->x1 = p.x;
  841. rect->y0 = rect->y1 = p.y;
  842. }
  843. else
  844. {
  845. *rect = fz_include_point_in_rect(*rect, p);
  846. }
  847. if (cloud > 0)
  848. {
  849. if (i == i0)
  850. first = p;
  851. else
  852. add_cloud_line(ctx, &cloud_list, buf, last.x, last.y, p.x, p.y);
  853. last = p;
  854. }
  855. else
  856. {
  857. if (i == i0)
  858. fz_append_printf(ctx, buf, "%g %g m\n", p.x, p.y);
  859. else
  860. fz_append_printf(ctx, buf, "%g %g l\n", p.x, p.y);
  861. }
  862. }
  863. if (cloud > 0)
  864. {
  865. add_cloud_line(ctx, &cloud_list, buf, last.x, last.y, first.x, first.y);
  866. end_cloud(ctx, &cloud_list, buf);
  867. }
  868. else
  869. {
  870. if (close)
  871. fz_append_string(ctx, buf, "h\n");
  872. }
  873. if (close)
  874. maybe_stroke_and_fill(ctx, buf, sc, ic);
  875. else
  876. maybe_stroke(ctx, buf, sc);
  877. exp = lw;
  878. if (cloud > 0)
  879. exp += cloud_list.radius;
  880. *rect = fz_expand_rect(*rect, exp);
  881. }
  882. le = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  883. if (!close && n >= 2 && pdf_array_len(ctx, le) == 2)
  884. {
  885. float dx, dy;
  886. fz_point a, b;
  887. a.x = pdf_array_get_real(ctx, verts, 0*2+0);
  888. a.y = pdf_array_get_real(ctx, verts, 0*2+1);
  889. b.x = pdf_array_get_real(ctx, verts, 1*2+0);
  890. b.y = pdf_array_get_real(ctx, verts, 1*2+1);
  891. dx = b.x - a.x;
  892. dy = b.y - a.y;
  893. pdf_write_line_cap_appearance(ctx, buf, rect, a.x, a.y, dx, dy, lw, sc, ic, pdf_array_get(ctx, le, 0));
  894. a.x = pdf_array_get_real(ctx, verts, (n-1)*2+0);
  895. a.y = pdf_array_get_real(ctx, verts, (n-1)*2+1);
  896. b.x = pdf_array_get_real(ctx, verts, (n-2)*2+0);
  897. b.y = pdf_array_get_real(ctx, verts, (n-2)*2+1);
  898. dx = b.x - a.x;
  899. dy = b.y - a.y;
  900. pdf_write_line_cap_appearance(ctx, buf, rect, a.x, a.y, dx, dy, lw, sc, ic, pdf_array_get(ctx, le, 1));
  901. }
  902. if (exp == 0)
  903. pdf_dict_del(ctx, annot->obj, PDF_NAME(RD));
  904. else
  905. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), fz_make_rect(exp, exp, exp, exp));
  906. }
  907. static void
  908. pdf_write_ink_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  909. {
  910. pdf_obj *ink_list, *stroke;
  911. int i, n, k, m;
  912. float lw;
  913. fz_point p;
  914. int sc;
  915. pdf_write_opacity(ctx, annot, buf, res);
  916. pdf_write_dash_pattern(ctx, annot, buf, res);
  917. lw = pdf_write_border_appearance(ctx, annot, buf);
  918. sc = pdf_write_stroke_color_appearance(ctx, annot, buf);
  919. *rect = fz_empty_rect;
  920. fz_append_printf(ctx, buf, "1 J\n1 j\n");
  921. ink_list = pdf_dict_get(ctx, annot->obj, PDF_NAME(InkList));
  922. n = pdf_array_len(ctx, ink_list);
  923. for (i = 0; i < n; ++i)
  924. {
  925. stroke = pdf_array_get(ctx, ink_list, i);
  926. m = pdf_array_len(ctx, stroke) / 2;
  927. for (k = 0; k < m; ++k)
  928. {
  929. p.x = pdf_array_get_real(ctx, stroke, k*2+0);
  930. p.y = pdf_array_get_real(ctx, stroke, k*2+1);
  931. if (i == 0 && k == 0)
  932. {
  933. rect->x0 = rect->x1 = p.x;
  934. rect->y0 = rect->y1 = p.y;
  935. }
  936. else
  937. *rect = fz_include_point_in_rect(*rect, p);
  938. fz_append_printf(ctx, buf, "%g %g %c\n", p.x, p.y, k == 0 ? 'm' : 'l');
  939. }
  940. if (m == 1)
  941. fz_append_printf(ctx, buf, "%g %g %c\n", p.x, p.y, 'l');
  942. }
  943. maybe_stroke(ctx, buf, sc);
  944. /* Account for line width and add an extra 6 points of whitespace padding.
  945. * In case the annotation is a dot or a perfectly horizontal/vertical line,
  946. * we need some extra size to allow selecting it easily.
  947. */
  948. *rect = fz_expand_rect(*rect, lw + 6);
  949. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), fz_make_rect(lw + 6, lw + 6, lw + 6, lw + 6));
  950. }
  951. /* Contrary to the specification, the points within a QuadPoint are NOT
  952. * ordered in a counter-clockwise fashion starting with the lower left.
  953. * Experiments with Adobe's implementation indicates a cross-wise
  954. * ordering is intended: ul, ur, ll, lr.
  955. */
  956. enum { UL, UR, LL, LR };
  957. static float
  958. extract_quad(fz_context *ctx, fz_point *quad, pdf_obj *obj, int i)
  959. {
  960. float dx, dy;
  961. quad[0].x = pdf_array_get_real(ctx, obj, i+0);
  962. quad[0].y = pdf_array_get_real(ctx, obj, i+1);
  963. quad[1].x = pdf_array_get_real(ctx, obj, i+2);
  964. quad[1].y = pdf_array_get_real(ctx, obj, i+3);
  965. quad[2].x = pdf_array_get_real(ctx, obj, i+4);
  966. quad[2].y = pdf_array_get_real(ctx, obj, i+5);
  967. quad[3].x = pdf_array_get_real(ctx, obj, i+6);
  968. quad[3].y = pdf_array_get_real(ctx, obj, i+7);
  969. dx = quad[UL].x - quad[LL].x;
  970. dy = quad[UL].y - quad[LL].y;
  971. return sqrtf(dx * dx + dy * dy);
  972. }
  973. static void
  974. union_quad(fz_rect *rect, const fz_point quad[4], float lw)
  975. {
  976. fz_rect qbox;
  977. qbox.x0 = fz_min(fz_min(quad[0].x, quad[1].x), fz_min(quad[2].x, quad[3].x));
  978. qbox.y0 = fz_min(fz_min(quad[0].y, quad[1].y), fz_min(quad[2].y, quad[3].y));
  979. qbox.x1 = fz_max(fz_max(quad[0].x, quad[1].x), fz_max(quad[2].x, quad[3].x));
  980. qbox.y1 = fz_max(fz_max(quad[0].y, quad[1].y), fz_max(quad[2].y, quad[3].y));
  981. *rect = fz_union_rect(*rect, fz_expand_rect(qbox, lw));
  982. }
  983. static fz_point
  984. lerp_point(fz_point a, fz_point b, float t)
  985. {
  986. return fz_make_point(a.x + t * (b.x - a.x), a.y + t * (b.y - a.y));
  987. }
  988. static void
  989. pdf_write_highlight_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  990. {
  991. pdf_obj *qp;
  992. fz_point quad[4], mquad[4], v;
  993. float h, m, dx, dy, vn;
  994. int i, n;
  995. *rect = fz_empty_rect;
  996. pdf_write_opacity_blend_mode(ctx, annot, buf, res, FZ_BLEND_MULTIPLY);
  997. pdf_write_fill_color_appearance(ctx, annot, buf);
  998. qp = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  999. n = pdf_array_len(ctx, qp);
  1000. if (n > 0)
  1001. {
  1002. for (i = 0; i < n; i += 8)
  1003. {
  1004. h = extract_quad(ctx, quad, qp, i);
  1005. m = h / 4.2425f; /* magic number that matches adobe's appearance */
  1006. dx = quad[LR].x - quad[LL].x;
  1007. dy = quad[LR].y - quad[LL].y;
  1008. vn = sqrtf(dx * dx + dy * dy);
  1009. v = fz_make_point(dx * m / vn, dy * m / vn);
  1010. mquad[LL].x = quad[LL].x - v.x - v.y;
  1011. mquad[LL].y = quad[LL].y - v.y + v.x;
  1012. mquad[UL].x = quad[UL].x - v.x + v.y;
  1013. mquad[UL].y = quad[UL].y - v.y - v.x;
  1014. mquad[LR].x = quad[LR].x + v.x - v.y;
  1015. mquad[LR].y = quad[LR].y + v.y + v.x;
  1016. mquad[UR].x = quad[UR].x + v.x + v.y;
  1017. mquad[UR].y = quad[UR].y + v.y - v.x;
  1018. fz_append_printf(ctx, buf, "%g %g m\n", quad[LL].x, quad[LL].y);
  1019. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n",
  1020. mquad[LL].x, mquad[LL].y,
  1021. mquad[UL].x, mquad[UL].y,
  1022. quad[UL].x, quad[UL].y);
  1023. fz_append_printf(ctx, buf, "%g %g l\n", quad[UR].x, quad[UR].y);
  1024. fz_append_printf(ctx, buf, "%g %g %g %g %g %g c\n",
  1025. mquad[UR].x, mquad[UR].y,
  1026. mquad[LR].x, mquad[LR].y,
  1027. quad[LR].x, quad[LR].y);
  1028. fz_append_printf(ctx, buf, "f\n");
  1029. union_quad(rect, quad, h/16);
  1030. union_quad(rect, mquad, 0);
  1031. }
  1032. }
  1033. }
  1034. static void
  1035. pdf_write_underline_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  1036. {
  1037. fz_point quad[4], a, b;
  1038. float h;
  1039. pdf_obj *qp;
  1040. int i, n;
  1041. *rect = fz_empty_rect;
  1042. pdf_write_opacity(ctx, annot, buf, res);
  1043. pdf_write_stroke_color_appearance(ctx, annot, buf);
  1044. qp = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  1045. n = pdf_array_len(ctx, qp);
  1046. if (n > 0)
  1047. {
  1048. for (i = 0; i < n; i += 8)
  1049. {
  1050. /* Acrobat draws the line at 1/7 of the box width from the bottom
  1051. * of the box and 1/16 thick of the box width. */
  1052. h = extract_quad(ctx, quad, qp, i);
  1053. a = lerp_point(quad[LL], quad[UL], 1/7.0f);
  1054. b = lerp_point(quad[LR], quad[UR], 1/7.0f);
  1055. fz_append_printf(ctx, buf, "%g w\n", h/16);
  1056. fz_append_printf(ctx, buf, "%g %g m\n", a.x, a.y);
  1057. fz_append_printf(ctx, buf, "%g %g l\n", b.x, b.y);
  1058. fz_append_printf(ctx, buf, "S\n");
  1059. union_quad(rect, quad, h/16);
  1060. }
  1061. }
  1062. }
  1063. static void
  1064. pdf_write_strike_out_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  1065. {
  1066. fz_point quad[4], a, b;
  1067. float h;
  1068. pdf_obj *qp;
  1069. int i, n;
  1070. pdf_write_opacity(ctx, annot, buf, res);
  1071. pdf_write_stroke_color_appearance(ctx, annot, buf);
  1072. qp = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  1073. n = pdf_array_len(ctx, qp);
  1074. if (n > 0)
  1075. {
  1076. *rect = fz_empty_rect;
  1077. for (i = 0; i < n; i += 8)
  1078. {
  1079. /* Acrobat draws the line at 3/7 of the box width from the bottom
  1080. * of the box and 1/16 thick of the box width. */
  1081. h = extract_quad(ctx, quad, qp, i);
  1082. a = lerp_point(quad[LL], quad[UL], 3/7.0f);
  1083. b = lerp_point(quad[LR], quad[UR], 3/7.0f);
  1084. fz_append_printf(ctx, buf, "%g w\n", h/16);
  1085. fz_append_printf(ctx, buf, "%g %g m\n", a.x, a.y);
  1086. fz_append_printf(ctx, buf, "%g %g l\n", b.x, b.y);
  1087. fz_append_printf(ctx, buf, "S\n");
  1088. union_quad(rect, quad, h/16);
  1089. }
  1090. }
  1091. }
  1092. static void
  1093. pdf_write_squiggly_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  1094. {
  1095. fz_point quad[4], a, b, c, v;
  1096. float h, x, w;
  1097. pdf_obj *qp;
  1098. int i, n;
  1099. *rect = fz_empty_rect;
  1100. pdf_write_opacity(ctx, annot, buf, res);
  1101. pdf_write_stroke_color_appearance(ctx, annot, buf);
  1102. qp = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  1103. n = pdf_array_len(ctx, qp);
  1104. if (n > 0)
  1105. {
  1106. for (i = 0; i < n; i += 8)
  1107. {
  1108. int up = 1;
  1109. h = extract_quad(ctx, quad, qp, i);
  1110. v = fz_make_point(quad[LR].x - quad[LL].x, quad[LR].y - quad[LL].y);
  1111. w = sqrtf(v.x * v.x + v.y * v.y);
  1112. x = 0;
  1113. fz_append_printf(ctx, buf, "%g w\n", h/16);
  1114. fz_append_printf(ctx, buf, "%g %g m\n", quad[LL].x, quad[LL].y);
  1115. while (x < w)
  1116. {
  1117. x += h/7;
  1118. a = lerp_point(quad[LL], quad[LR], x/w);
  1119. if (up)
  1120. {
  1121. b = lerp_point(quad[UL], quad[UR], x/w);
  1122. c = lerp_point(a, b, 1/7.0f);
  1123. fz_append_printf(ctx, buf, "%g %g l\n", c.x, c.y);
  1124. }
  1125. else
  1126. fz_append_printf(ctx, buf, "%g %g l\n", a.x, a.y);
  1127. up = !up;
  1128. }
  1129. fz_append_printf(ctx, buf, "S\n");
  1130. union_quad(rect, quad, h/16);
  1131. }
  1132. }
  1133. }
  1134. static void
  1135. pdf_write_redact_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res)
  1136. {
  1137. fz_point quad[4];
  1138. pdf_obj *qp;
  1139. int i, n;
  1140. pdf_write_opacity(ctx, annot, buf, res);
  1141. fz_append_printf(ctx, buf, "1 0 0 RG\n");
  1142. qp = pdf_dict_get(ctx, annot->obj, PDF_NAME(QuadPoints));
  1143. n = pdf_array_len(ctx, qp);
  1144. if (n > 0)
  1145. {
  1146. *rect = fz_empty_rect;
  1147. for (i = 0; i < n; i += 8)
  1148. {
  1149. extract_quad(ctx, quad, qp, i);
  1150. fz_append_printf(ctx, buf, "%g %g m\n", quad[LL].x, quad[LL].y);
  1151. fz_append_printf(ctx, buf, "%g %g l\n", quad[LR].x, quad[LR].y);
  1152. fz_append_printf(ctx, buf, "%g %g l\n", quad[UR].x, quad[UR].y);
  1153. fz_append_printf(ctx, buf, "%g %g l\n", quad[UL].x, quad[UL].y);
  1154. fz_append_printf(ctx, buf, "s\n");
  1155. union_quad(rect, quad, 1);
  1156. }
  1157. }
  1158. else
  1159. {
  1160. fz_append_printf(ctx, buf, "%g %g m\n", rect->x0+1, rect->y0+1);
  1161. fz_append_printf(ctx, buf, "%g %g l\n", rect->x1-1, rect->y0+1);
  1162. fz_append_printf(ctx, buf, "%g %g l\n", rect->x1-1, rect->y1-1);
  1163. fz_append_printf(ctx, buf, "%g %g l\n", rect->x0+1, rect->y1-1);
  1164. fz_append_printf(ctx, buf, "s\n");
  1165. }
  1166. }
  1167. static void
  1168. pdf_write_caret_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, fz_rect *bbox, pdf_obj **res)
  1169. {
  1170. float xc = (rect->x0 + rect->x1) / 2;
  1171. float yc = (rect->y0 + rect->y1) / 2;
  1172. pdf_write_opacity(ctx, annot, buf, res);
  1173. pdf_write_fill_color_appearance(ctx, annot, buf);
  1174. fz_append_string(ctx, buf, "0 0 m\n");
  1175. fz_append_string(ctx, buf, "10 0 10 7 10 14 c\n");
  1176. fz_append_string(ctx, buf, "10 7 10 0 20 0 c\n");
  1177. fz_append_string(ctx, buf, "f\n");
  1178. *rect = fz_make_rect(xc - 10, yc - 7, xc + 10, yc + 7);
  1179. *bbox = fz_make_rect(0, 0, 20, 14);
  1180. }
  1181. static void
  1182. pdf_write_icon_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, fz_rect *bbox, pdf_obj **res)
  1183. {
  1184. const char *name;
  1185. float xc = (rect->x0 + rect->x1) / 2;
  1186. float yc = (rect->y0 + rect->y1) / 2;
  1187. pdf_write_opacity(ctx, annot, buf, res);
  1188. if (!pdf_write_fill_color_appearance(ctx, annot, buf))
  1189. fz_append_string(ctx, buf, "1 g\n");
  1190. fz_append_string(ctx, buf, "1 w\n0.5 0.5 15 15 re\nb\n");
  1191. fz_append_string(ctx, buf, "1 0 0 -1 4 12 cm\n");
  1192. if (pdf_is_dark_fill_color(ctx, annot))
  1193. fz_append_string(ctx, buf, "1 g\n");
  1194. else
  1195. fz_append_string(ctx, buf, "0 g\n");
  1196. name = pdf_annot_icon_name(ctx, annot);
  1197. /* Text names */
  1198. if (!strcmp(name, "Comment"))
  1199. fz_append_string(ctx, buf, icon_comment);
  1200. else if (!strcmp(name, "Key"))
  1201. fz_append_string(ctx, buf, icon_key);
  1202. else if (!strcmp(name, "Note"))
  1203. fz_append_string(ctx, buf, icon_note);
  1204. else if (!strcmp(name, "Help"))
  1205. fz_append_string(ctx, buf, icon_help);
  1206. else if (!strcmp(name, "NewParagraph"))
  1207. fz_append_string(ctx, buf, icon_new_paragraph);
  1208. else if (!strcmp(name, "Paragraph"))
  1209. fz_append_string(ctx, buf, icon_paragraph);
  1210. else if (!strcmp(name, "Insert"))
  1211. fz_append_string(ctx, buf, icon_insert);
  1212. /* FileAttachment names */
  1213. else if (!strcmp(name, "Graph"))
  1214. fz_append_string(ctx, buf, icon_graph);
  1215. else if (!strcmp(name, "PushPin"))
  1216. fz_append_string(ctx, buf, icon_push_pin);
  1217. else if (!strcmp(name, "Paperclip"))
  1218. fz_append_string(ctx, buf, icon_paperclip);
  1219. else if (!strcmp(name, "Tag"))
  1220. fz_append_string(ctx, buf, icon_tag);
  1221. /* Sound names */
  1222. else if (!strcmp(name, "Speaker"))
  1223. fz_append_string(ctx, buf, icon_speaker);
  1224. else if (!strcmp(name, "Mic"))
  1225. fz_append_string(ctx, buf, icon_mic);
  1226. /* Unknown */
  1227. else
  1228. fz_append_string(ctx, buf, icon_star);
  1229. *rect = fz_make_rect(xc - 9, yc - 9, xc + 9, yc + 9);
  1230. *bbox = fz_make_rect(0, 0, 16, 16);
  1231. }
  1232. static float
  1233. measure_stamp_string(fz_context *ctx, fz_font *font, const char *text)
  1234. {
  1235. float w = 0;
  1236. while (*text)
  1237. {
  1238. int c, g;
  1239. text += fz_chartorune(&c, text);
  1240. if (fz_windows_1252_from_unicode(c) < 0)
  1241. c = REPLACEMENT;
  1242. g = fz_encode_character(ctx, font, c);
  1243. w += fz_advance_glyph(ctx, font, g, 0);
  1244. }
  1245. return w;
  1246. }
  1247. static void
  1248. write_stamp_string(fz_context *ctx, fz_buffer *buf, fz_font *font, const char *text)
  1249. {
  1250. fz_append_byte(ctx, buf, '(');
  1251. while (*text)
  1252. {
  1253. int c;
  1254. text += fz_chartorune(&c, text);
  1255. c = fz_windows_1252_from_unicode(c);
  1256. if (c < 0) c = REPLACEMENT;
  1257. if (c == '(' || c == ')' || c == '\\')
  1258. fz_append_byte(ctx, buf, '\\');
  1259. fz_append_byte(ctx, buf, c);
  1260. }
  1261. fz_append_byte(ctx, buf, ')');
  1262. }
  1263. static void
  1264. write_stamp(fz_context *ctx, fz_buffer *buf, fz_font *font, const char *text, float y, float h)
  1265. {
  1266. float tw = measure_stamp_string(ctx, font, text) * h;
  1267. fz_append_string(ctx, buf, "BT\n");
  1268. fz_append_printf(ctx, buf, "/Times %g Tf\n", h);
  1269. fz_append_printf(ctx, buf, "%g %g Td\n", (190-tw)/2, y);
  1270. write_stamp_string(ctx, buf, font, text);
  1271. fz_append_string(ctx, buf, " Tj\n");
  1272. fz_append_string(ctx, buf, "ET\n");
  1273. }
  1274. static void
  1275. pdf_write_stamp_appearance_rubber(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  1276. {
  1277. fz_font *font;
  1278. pdf_obj *res_font;
  1279. pdf_obj *name;
  1280. float w, h, xs, ys;
  1281. fz_matrix rotate;
  1282. name = pdf_dict_get(ctx, annot->obj, PDF_NAME(Name));
  1283. if (!name)
  1284. name = PDF_NAME(Draft);
  1285. h = rect->y1 - rect->y0;
  1286. w = rect->x1 - rect->x0;
  1287. xs = w / 190;
  1288. ys = h / 50;
  1289. font = fz_new_base14_font(ctx, "Times-Bold");
  1290. fz_try(ctx)
  1291. {
  1292. /* /Resources << /Font << /Times %d 0 R >> >> */
  1293. if (!*res)
  1294. *res = pdf_new_dict(ctx, annot->page->doc, 1);
  1295. res_font = pdf_dict_put_dict(ctx, *res, PDF_NAME(Font), 1);
  1296. pdf_dict_put_drop(ctx, res_font, PDF_NAME(Times), pdf_add_simple_font(ctx, annot->page->doc, font, 0));
  1297. pdf_write_opacity(ctx, annot, buf, res);
  1298. pdf_write_fill_color_appearance(ctx, annot, buf);
  1299. pdf_write_stroke_color_appearance(ctx, annot, buf);
  1300. rotate = fz_rotate(0.6f);
  1301. fz_append_printf(ctx, buf, "%M cm\n", &rotate);
  1302. fz_append_string(ctx, buf, "2 w\n2 2 186 44 re\nS\n");
  1303. if (name == PDF_NAME(Approved))
  1304. write_stamp(ctx, buf, font, "APPROVED", 13, 30);
  1305. else if (name == PDF_NAME(AsIs))
  1306. write_stamp(ctx, buf, font, "AS IS", 13, 30);
  1307. else if (name == PDF_NAME(Confidential))
  1308. write_stamp(ctx, buf, font, "CONFIDENTIAL", 17, 20);
  1309. else if (name == PDF_NAME(Departmental))
  1310. write_stamp(ctx, buf, font, "DEPARTMENTAL", 17, 20);
  1311. else if (name == PDF_NAME(Experimental))
  1312. write_stamp(ctx, buf, font, "EXPERIMENTAL", 17, 20);
  1313. else if (name == PDF_NAME(Expired))
  1314. write_stamp(ctx, buf, font, "EXPIRED", 13, 30);
  1315. else if (name == PDF_NAME(Final))
  1316. write_stamp(ctx, buf, font, "FINAL", 13, 30);
  1317. else if (name == PDF_NAME(ForComment))
  1318. write_stamp(ctx, buf, font, "FOR COMMENT", 17, 20);
  1319. else if (name == PDF_NAME(ForPublicRelease))
  1320. {
  1321. write_stamp(ctx, buf, font, "FOR PUBLIC", 26, 18);
  1322. write_stamp(ctx, buf, font, "RELEASE", 8.5f, 18);
  1323. }
  1324. else if (name == PDF_NAME(NotApproved))
  1325. write_stamp(ctx, buf, font, "NOT APPROVED", 17, 20);
  1326. else if (name == PDF_NAME(NotForPublicRelease))
  1327. {
  1328. write_stamp(ctx, buf, font, "NOT FOR", 26, 18);
  1329. write_stamp(ctx, buf, font, "PUBLIC RELEASE", 8.5, 18);
  1330. }
  1331. else if (name == PDF_NAME(Sold))
  1332. write_stamp(ctx, buf, font, "SOLD", 13, 30);
  1333. else if (name == PDF_NAME(TopSecret))
  1334. write_stamp(ctx, buf, font, "TOP SECRET", 14, 26);
  1335. else if (name == PDF_NAME(Draft))
  1336. write_stamp(ctx, buf, font, "DRAFT", 13, 30);
  1337. else
  1338. write_stamp(ctx, buf, font, pdf_to_name(ctx, name), 17, 20);
  1339. }
  1340. fz_always(ctx)
  1341. fz_drop_font(ctx, font);
  1342. fz_catch(ctx)
  1343. fz_rethrow(ctx);
  1344. *bbox = fz_make_rect(0, 0, 190, 50);
  1345. if (xs > ys)
  1346. {
  1347. float xc = (rect->x1+rect->x0) / 2;
  1348. rect->x0 = xc - 95 * ys;
  1349. rect->x1 = xc + 95 * ys;
  1350. }
  1351. else
  1352. {
  1353. float yc = (rect->y1+rect->y0) / 2;
  1354. rect->y0 = yc - 25 * xs;
  1355. rect->y1 = yc + 25 * xs;
  1356. }
  1357. *matrix = fz_identity;
  1358. }
  1359. static void
  1360. pdf_write_stamp_appearance_image(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res, pdf_obj *img)
  1361. {
  1362. pdf_obj *res_xobj;
  1363. if (!*res)
  1364. *res = pdf_new_dict(ctx, annot->page->doc, 1);
  1365. res_xobj = pdf_dict_put_dict(ctx, *res, PDF_NAME(XObject), 1);
  1366. pdf_dict_put(ctx, res_xobj, PDF_NAME(I), img);
  1367. pdf_write_opacity(ctx, annot, buf, res);
  1368. fz_append_string(ctx, buf, "/I Do\n");
  1369. *bbox = fz_unit_rect;
  1370. *matrix = fz_identity;
  1371. }
  1372. static void
  1373. pdf_write_stamp_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  1374. {
  1375. pdf_obj *img = pdf_annot_stamp_image_obj(ctx, annot);
  1376. if (img)
  1377. pdf_write_stamp_appearance_image(ctx, annot, buf, rect, bbox, matrix, res, img);
  1378. else
  1379. pdf_write_stamp_appearance_rubber(ctx, annot, buf, rect, bbox, matrix, res);
  1380. }
  1381. static void
  1382. add_required_fonts(fz_context *ctx, pdf_document *doc, pdf_obj *res_font,
  1383. fz_text_language lang, fz_font *font, const char *fontname, const char *text)
  1384. {
  1385. fz_font *cjk_font;
  1386. char buf[40];
  1387. int add_latin = 0;
  1388. int add_greek = 0;
  1389. int add_cyrillic = 0;
  1390. int add_korean = 0;
  1391. int add_japanese = 0;
  1392. int add_bopomofo = 0;
  1393. int add_han = 0;
  1394. int add_hans = 0;
  1395. int add_hant = 0;
  1396. while (*text)
  1397. {
  1398. int c;
  1399. text += fz_chartorune(&c, text);
  1400. switch (ucdn_get_script(c))
  1401. {
  1402. default: add_latin = 1; /* for fallback bullet character */ break;
  1403. case UCDN_SCRIPT_COMMON: break;
  1404. case UCDN_SCRIPT_INHERITED: break;
  1405. case UCDN_SCRIPT_LATIN: add_latin = 1; break;
  1406. case UCDN_SCRIPT_GREEK: add_greek = 1; break;
  1407. case UCDN_SCRIPT_CYRILLIC: add_cyrillic = 1; break;
  1408. case UCDN_SCRIPT_HANGUL: add_korean = 1; break;
  1409. case UCDN_SCRIPT_HIRAGANA: add_japanese = 1; break;
  1410. case UCDN_SCRIPT_KATAKANA: add_japanese = 1; break;
  1411. case UCDN_SCRIPT_BOPOMOFO: add_bopomofo = 1; break;
  1412. case UCDN_SCRIPT_HAN: add_han = 1; break;
  1413. }
  1414. }
  1415. if (add_han)
  1416. {
  1417. switch (lang)
  1418. {
  1419. case FZ_LANG_ko: add_korean = 1; break;
  1420. default: /* fall through */
  1421. case FZ_LANG_ja: add_japanese = 1; break;
  1422. case FZ_LANG_zh: /* fall through */
  1423. case FZ_LANG_zh_Hant: add_hant = 1; break;
  1424. case FZ_LANG_zh_Hans: add_hans = 1; break;
  1425. }
  1426. }
  1427. if (add_bopomofo)
  1428. {
  1429. if (lang == FZ_LANG_zh_Hans)
  1430. add_hans = 1;
  1431. else
  1432. add_hant = 1;
  1433. }
  1434. if (!add_greek && !add_cyrillic && !add_korean && !add_japanese && !add_hant && !add_hans)
  1435. add_latin = 1;
  1436. if (add_latin)
  1437. {
  1438. if (!pdf_dict_gets(ctx, res_font, fontname))
  1439. pdf_dict_puts_drop(ctx, res_font, fontname,
  1440. pdf_add_simple_font(ctx, doc, font, PDF_SIMPLE_ENCODING_LATIN));
  1441. }
  1442. if (add_greek)
  1443. {
  1444. fz_snprintf(buf, sizeof buf, "%sGRK", fontname);
  1445. if (!pdf_dict_gets(ctx, res_font, buf))
  1446. pdf_dict_puts_drop(ctx, res_font, buf,
  1447. pdf_add_simple_font(ctx, doc, font, PDF_SIMPLE_ENCODING_GREEK));
  1448. }
  1449. if (add_cyrillic)
  1450. {
  1451. fz_snprintf(buf, sizeof buf, "%sCYR", fontname);
  1452. if (!pdf_dict_gets(ctx, res_font, buf))
  1453. pdf_dict_puts_drop(ctx, res_font, buf,
  1454. pdf_add_simple_font(ctx, doc, font, PDF_SIMPLE_ENCODING_CYRILLIC));
  1455. }
  1456. if (add_korean && !pdf_dict_gets(ctx, res_font, "Batang"))
  1457. {
  1458. cjk_font = fz_new_cjk_font(ctx, FZ_ADOBE_KOREA);
  1459. pdf_dict_puts_drop(ctx, res_font, "Batang",
  1460. pdf_add_cjk_font(ctx, doc, font, FZ_ADOBE_KOREA, 0, 1));
  1461. fz_drop_font(ctx, cjk_font);
  1462. }
  1463. if (add_japanese && !pdf_dict_gets(ctx, res_font, "Mincho"))
  1464. {
  1465. cjk_font = fz_new_cjk_font(ctx, FZ_ADOBE_JAPAN);
  1466. pdf_dict_puts_drop(ctx, res_font, "Mincho",
  1467. pdf_add_cjk_font(ctx, doc, font, FZ_ADOBE_JAPAN, 0, 1));
  1468. fz_drop_font(ctx, cjk_font);
  1469. }
  1470. if (add_hant && !pdf_dict_gets(ctx, res_font, "Ming"))
  1471. {
  1472. cjk_font = fz_new_cjk_font(ctx, FZ_ADOBE_CNS);
  1473. pdf_dict_puts_drop(ctx, res_font, "Ming",
  1474. pdf_add_cjk_font(ctx, doc, font, FZ_ADOBE_CNS, 0, 1));
  1475. fz_drop_font(ctx, cjk_font);
  1476. }
  1477. if (add_hans && !pdf_dict_gets(ctx, res_font, "Song"))
  1478. {
  1479. cjk_font = fz_new_cjk_font(ctx, FZ_ADOBE_GB);
  1480. pdf_dict_puts_drop(ctx, res_font, "Song",
  1481. pdf_add_cjk_font(ctx, doc, font, FZ_ADOBE_GB, 0, 1));
  1482. fz_drop_font(ctx, cjk_font);
  1483. }
  1484. }
  1485. static int find_initial_script(const char *text)
  1486. {
  1487. int script = UCDN_SCRIPT_COMMON;
  1488. int c;
  1489. while (*text)
  1490. {
  1491. text += fz_chartorune(&c, text);
  1492. script = ucdn_get_script(c);
  1493. if (script != UCDN_SCRIPT_COMMON && script != UCDN_SCRIPT_INHERITED)
  1494. break;
  1495. }
  1496. if (script == UCDN_SCRIPT_COMMON || script == UCDN_SCRIPT_INHERITED)
  1497. script = UCDN_SCRIPT_LATIN;
  1498. return script;
  1499. }
  1500. enum { ENC_LATIN = 1, ENC_GREEK, ENC_CYRILLIC, ENC_KOREAN, ENC_JAPANESE, ENC_HANT, ENC_HANS };
  1501. struct text_walk_state
  1502. {
  1503. const char *text, *end;
  1504. fz_font *font;
  1505. fz_text_language lang;
  1506. int enc, u, c, n, last_script;
  1507. float w;
  1508. };
  1509. static void init_text_walk(fz_context *ctx, struct text_walk_state *state, fz_text_language lang, fz_font *font, const char *text, const char *end)
  1510. {
  1511. state->text = text;
  1512. state->end = end ? end : text + strlen(text);
  1513. state->lang = lang;
  1514. state->font = font;
  1515. state->last_script = find_initial_script(text);
  1516. state->n = 0;
  1517. }
  1518. static int next_text_walk(fz_context *ctx, struct text_walk_state *state)
  1519. {
  1520. int script, g;
  1521. state->text += state->n;
  1522. if (state->text >= state->end)
  1523. {
  1524. state->n = 0;
  1525. return 0;
  1526. }
  1527. state->n = fz_chartorune(&state->u, state->text);
  1528. script = ucdn_get_script(state->u);
  1529. if (script == UCDN_SCRIPT_COMMON || script == UCDN_SCRIPT_INHERITED)
  1530. script = state->last_script;
  1531. state->last_script = script;
  1532. switch (script)
  1533. {
  1534. default:
  1535. state->enc = ENC_LATIN;
  1536. state->c = REPLACEMENT;
  1537. break;
  1538. case UCDN_SCRIPT_LATIN:
  1539. state->enc = ENC_LATIN;
  1540. state->c = fz_windows_1252_from_unicode(state->u);
  1541. break;
  1542. case UCDN_SCRIPT_GREEK:
  1543. state->enc = ENC_GREEK;
  1544. state->c = fz_iso8859_7_from_unicode(state->u);
  1545. break;
  1546. case UCDN_SCRIPT_CYRILLIC:
  1547. state->enc = ENC_CYRILLIC;
  1548. state->c = fz_koi8u_from_unicode(state->u);
  1549. break;
  1550. case UCDN_SCRIPT_HANGUL:
  1551. state->enc = ENC_KOREAN;
  1552. state->c = state->u;
  1553. break;
  1554. case UCDN_SCRIPT_HIRAGANA:
  1555. case UCDN_SCRIPT_KATAKANA:
  1556. state->enc = ENC_JAPANESE;
  1557. state->c = state->u;
  1558. break;
  1559. case UCDN_SCRIPT_BOPOMOFO:
  1560. state->enc = (state->lang == FZ_LANG_zh_Hans) ? ENC_HANS : ENC_HANT;
  1561. state->c = state->u;
  1562. break;
  1563. case UCDN_SCRIPT_HAN:
  1564. switch (state->lang)
  1565. {
  1566. case FZ_LANG_ko: state->enc = ENC_KOREAN; break;
  1567. default: /* fall through */
  1568. case FZ_LANG_ja: state->enc = ENC_JAPANESE; break;
  1569. case FZ_LANG_zh: /* fall through */
  1570. case FZ_LANG_zh_Hant: state->enc = ENC_HANT; break;
  1571. case FZ_LANG_zh_Hans: state->enc = ENC_HANS; break;
  1572. }
  1573. state->c = state->u;
  1574. break;
  1575. }
  1576. /* TODO: check that character is encodable with ENC_KOREAN/etc */
  1577. if (state->c < 0)
  1578. {
  1579. state->enc = ENC_LATIN;
  1580. state->c = REPLACEMENT;
  1581. }
  1582. if (state->enc >= ENC_KOREAN)
  1583. {
  1584. state->w = 1;
  1585. }
  1586. else
  1587. {
  1588. if (state->font != NULL)
  1589. {
  1590. g = fz_encode_character(ctx, state->font, state->u);
  1591. state->w = fz_advance_glyph(ctx, state->font, g, 0);
  1592. }
  1593. }
  1594. return 1;
  1595. }
  1596. static float
  1597. measure_string(fz_context *ctx, fz_text_language lang, fz_font *font, const char *a)
  1598. {
  1599. struct text_walk_state state;
  1600. float w = 0;
  1601. init_text_walk(ctx, &state, lang, font, a, NULL);
  1602. while (next_text_walk(ctx, &state))
  1603. w += state.w;
  1604. return w;
  1605. }
  1606. static float
  1607. break_string(fz_context *ctx, fz_text_language lang, fz_font *font, float size, const char *text, const char **endp, float maxw)
  1608. {
  1609. struct text_walk_state state;
  1610. const char *space = NULL;
  1611. float space_x, x = 0;
  1612. init_text_walk(ctx, &state, lang, font, text, NULL);
  1613. while (next_text_walk(ctx, &state))
  1614. {
  1615. if (state.u == '\n' || state.u == '\r')
  1616. break;
  1617. if (state.u == ' ')
  1618. {
  1619. space = state.text + state.n;
  1620. space_x = x;
  1621. }
  1622. x += state.w * size;
  1623. if (space && x > maxw)
  1624. return *endp = space, space_x;
  1625. }
  1626. return *endp = state.text + state.n, x;
  1627. }
  1628. static void
  1629. write_string(fz_context *ctx, fz_buffer *buf,
  1630. fz_text_language lang, fz_font *font, const char *fontname, float size, const char *text, const char *end)
  1631. {
  1632. struct text_walk_state state;
  1633. int last_enc = 0;
  1634. init_text_walk(ctx, &state, lang, font, text, end);
  1635. while (next_text_walk(ctx, &state))
  1636. {
  1637. if (state.enc != last_enc)
  1638. {
  1639. if (last_enc)
  1640. {
  1641. if (last_enc < ENC_KOREAN)
  1642. fz_append_byte(ctx, buf, ')');
  1643. else
  1644. fz_append_byte(ctx, buf, '>');
  1645. fz_append_string(ctx, buf, " Tj\n");
  1646. }
  1647. switch (state.enc)
  1648. {
  1649. case ENC_LATIN: fz_append_printf(ctx, buf, "/%s %g Tf\n", fontname, size); break;
  1650. case ENC_GREEK: fz_append_printf(ctx, buf, "/%sGRK %g Tf\n", fontname, size); break;
  1651. case ENC_CYRILLIC: fz_append_printf(ctx, buf, "/%sCYR %g Tf\n", fontname, size); break;
  1652. case ENC_KOREAN: fz_append_printf(ctx, buf, "/Batang %g Tf\n", size); break;
  1653. case ENC_JAPANESE: fz_append_printf(ctx, buf, "/Mincho %g Tf\n", size); break;
  1654. case ENC_HANT: fz_append_printf(ctx, buf, "/Ming %g Tf\n", size); break;
  1655. case ENC_HANS: fz_append_printf(ctx, buf, "/Song %g Tf\n", size); break;
  1656. }
  1657. if (state.enc < ENC_KOREAN)
  1658. fz_append_byte(ctx, buf, '(');
  1659. else
  1660. fz_append_byte(ctx, buf, '<');
  1661. last_enc = state.enc;
  1662. }
  1663. if (state.enc < ENC_KOREAN)
  1664. {
  1665. if (state.c == '(' || state.c == ')' || state.c == '\\')
  1666. fz_append_byte(ctx, buf, '\\');
  1667. fz_append_byte(ctx, buf, state.c);
  1668. }
  1669. else
  1670. {
  1671. fz_append_printf(ctx, buf, "%04x", state.c);
  1672. }
  1673. }
  1674. if (last_enc)
  1675. {
  1676. if (last_enc < ENC_KOREAN)
  1677. fz_append_byte(ctx, buf, ')');
  1678. else
  1679. fz_append_byte(ctx, buf, '>');
  1680. fz_append_string(ctx, buf, " Tj\n");
  1681. }
  1682. }
  1683. static void
  1684. write_string_with_quadding(fz_context *ctx, fz_buffer *buf,
  1685. fz_text_language lang, const char *fontname,
  1686. fz_font *font, float size, float lineheight,
  1687. const char *a, float maxw, int q)
  1688. {
  1689. const char *b;
  1690. float px = 0, x = 0, w;
  1691. while (*a)
  1692. {
  1693. w = break_string(ctx, lang, font, size, a, &b, maxw);
  1694. if (b > a)
  1695. {
  1696. if (q == 0)
  1697. x = 0;
  1698. else if (q == 1)
  1699. x = (maxw - w) / 2;
  1700. else
  1701. x = (maxw - w);
  1702. fz_append_printf(ctx, buf, "%g %g Td\n", x - px, -lineheight);
  1703. if (b[-1] == '\n' || b[-1] == '\r')
  1704. write_string(ctx, buf, lang, font, fontname, size, a, b-1);
  1705. else
  1706. write_string(ctx, buf, lang, font, fontname, size, a, b);
  1707. a = b;
  1708. px = x;
  1709. }
  1710. }
  1711. }
  1712. static void
  1713. write_comb_string(fz_context *ctx, fz_buffer *buf,
  1714. fz_text_language lang, const char *fontname,
  1715. fz_font *font, float size, const char *text, float cell_w)
  1716. {
  1717. struct text_walk_state state;
  1718. int last_enc = 0;
  1719. float pad, carry = 0;
  1720. init_text_walk(ctx, &state, lang, font, text, text + strlen(text));
  1721. while (next_text_walk(ctx, &state))
  1722. {
  1723. if (state.enc != last_enc)
  1724. {
  1725. if (last_enc)
  1726. fz_append_string(ctx, buf, "] TJ\n");
  1727. switch (state.enc)
  1728. {
  1729. case ENC_LATIN: fz_append_printf(ctx, buf, "/%s %g Tf\n", fontname, size); break;
  1730. case ENC_GREEK: fz_append_printf(ctx, buf, "/%sGRK %g Tf\n", fontname, size); break;
  1731. case ENC_CYRILLIC: fz_append_printf(ctx, buf, "/%sCYR %g Tf\n", fontname, size); break;
  1732. case ENC_KOREAN: fz_append_printf(ctx, buf, "/Batang %g Tf\n", size); break;
  1733. case ENC_JAPANESE: fz_append_printf(ctx, buf, "/Mincho %g Tf\n", size); break;
  1734. case ENC_HANT: fz_append_printf(ctx, buf, "/Ming %g Tf\n", size); break;
  1735. case ENC_HANS: fz_append_printf(ctx, buf, "/Song %g Tf\n", size); break;
  1736. }
  1737. fz_append_byte(ctx, buf, '[');
  1738. last_enc = state.enc;
  1739. }
  1740. pad = (cell_w - state.w * 1000) / 2;
  1741. fz_append_printf(ctx, buf, "%g", -(carry + pad));
  1742. carry = pad;
  1743. if (state.enc < ENC_KOREAN)
  1744. {
  1745. fz_append_byte(ctx, buf, '(');
  1746. if (state.c == '(' || state.c == ')' || state.c == '\\')
  1747. fz_append_byte(ctx, buf, '\\');
  1748. fz_append_byte(ctx, buf, state.c);
  1749. fz_append_byte(ctx, buf, ')');
  1750. }
  1751. else
  1752. {
  1753. fz_append_printf(ctx, buf, "<%04x>", state.c);
  1754. }
  1755. }
  1756. if (last_enc)
  1757. fz_append_string(ctx, buf, "] TJ\n");
  1758. }
  1759. static void
  1760. layout_comb_string(fz_context *ctx, fz_layout_block *out, float x, float y,
  1761. const char *a, const char *b, fz_font *font, float size, float cell_w)
  1762. {
  1763. int n, c, g;
  1764. int first = 1;
  1765. float w;
  1766. if (a == b)
  1767. fz_add_layout_line(ctx, out, x + cell_w / 2, y, size, a);
  1768. while (a < b)
  1769. {
  1770. n = fz_chartorune(&c, a);
  1771. c = fz_windows_1252_from_unicode(c);
  1772. if (c < 0) c = REPLACEMENT;
  1773. g = fz_encode_character(ctx, font, c);
  1774. w = fz_advance_glyph(ctx, font, g, 0) * size;
  1775. if (first)
  1776. {
  1777. fz_add_layout_line(ctx, out, x + (cell_w - w) / 2, y, size, a);
  1778. first = 0;
  1779. }
  1780. fz_add_layout_char(ctx, out, x + (cell_w - w) / 2, w, a);
  1781. a += n;
  1782. x += cell_w;
  1783. }
  1784. }
  1785. static void
  1786. layout_string(fz_context *ctx, fz_layout_block *out,
  1787. fz_text_language lang, fz_font *font, float size,
  1788. float x, float y, const char *a, const char *b)
  1789. {
  1790. struct text_walk_state state;
  1791. fz_add_layout_line(ctx, out, x, y, size, a);
  1792. init_text_walk(ctx, &state, lang, font, a, b);
  1793. while (next_text_walk(ctx, &state))
  1794. {
  1795. fz_add_layout_char(ctx, out, x, state.w * size, state.text);
  1796. x += state.w * size;
  1797. }
  1798. }
  1799. static void
  1800. layout_string_with_quadding(fz_context *ctx, fz_layout_block *out,
  1801. fz_text_language lang, fz_font *font, float size, float lineheight,
  1802. float xorig, float y, const char *a, float maxw, int q)
  1803. {
  1804. const char *b;
  1805. float x = 0, w;
  1806. int add_line_at_end = 0;
  1807. if (!*a)
  1808. add_line_at_end = 1;
  1809. while (*a)
  1810. {
  1811. w = break_string(ctx, lang, font, size, a, &b, maxw);
  1812. if (b > a)
  1813. {
  1814. if (q > 0)
  1815. {
  1816. if (q == 1)
  1817. x = (maxw - w) / 2;
  1818. else
  1819. x = (maxw - w);
  1820. }
  1821. if (b[-1] == '\n' || b[-1] == '\r')
  1822. {
  1823. layout_string(ctx, out, lang, font, size, xorig+x, y, a, b-1);
  1824. add_line_at_end = 1;
  1825. }
  1826. else
  1827. {
  1828. layout_string(ctx, out, lang, font, size, xorig+x, y, a, b);
  1829. add_line_at_end = 0;
  1830. }
  1831. a = b;
  1832. y -= lineheight;
  1833. }
  1834. }
  1835. if (add_line_at_end)
  1836. fz_add_layout_line(ctx, out, xorig, y, size, a);
  1837. }
  1838. static const char *full_font_name(const char **name)
  1839. {
  1840. if (!strcmp(*name, "Cour")) return "Courier";
  1841. if (!strcmp(*name, "Helv")) return "Helvetica";
  1842. if (!strcmp(*name, "TiRo")) return "Times-Roman";
  1843. if (!strcmp(*name, "Symb")) return "Symbol";
  1844. if (!strcmp(*name, "ZaDb")) return "ZapfDingbats";
  1845. return *name = "Helv", "Helvetica";
  1846. }
  1847. static void
  1848. write_variable_text(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, pdf_obj **res,
  1849. fz_text_language lang, const char *text,
  1850. const char *fontname, float size, int n, float *color, int q,
  1851. float w, float h, float padding, float baseline, float lineheight,
  1852. int multiline, int comb, int adjust_baseline)
  1853. {
  1854. fz_font *font;
  1855. pdf_obj *res_font;
  1856. w -= padding * 2;
  1857. h -= padding * 2;
  1858. font = fz_new_base14_font(ctx, full_font_name(&fontname));
  1859. fz_try(ctx)
  1860. {
  1861. if (!*res)
  1862. *res = pdf_new_dict(ctx, annot->page->doc, 1);
  1863. res_font = pdf_dict_put_dict(ctx, *res, PDF_NAME(Font), 1);
  1864. add_required_fonts(ctx, annot->page->doc, res_font, lang, font, fontname, text);
  1865. if (size == 0)
  1866. {
  1867. if (multiline)
  1868. size = 12;
  1869. else
  1870. {
  1871. size = w / measure_string(ctx, lang, font, text);
  1872. if (size > h)
  1873. size = h;
  1874. }
  1875. }
  1876. lineheight = size * lineheight;
  1877. baseline = size * baseline;
  1878. if (adjust_baseline)
  1879. {
  1880. /* Make sure baseline is inside rectangle */
  1881. if (baseline + 0.2f * size > h)
  1882. baseline = h - 0.2f * size;
  1883. }
  1884. fz_append_string(ctx, buf, "BT\n");
  1885. write_color0(ctx, buf, n, color, 0);
  1886. if (multiline)
  1887. {
  1888. fz_append_printf(ctx, buf, "%g %g Td\n", padding, padding+h-baseline+lineheight);
  1889. write_string_with_quadding(ctx, buf, lang, fontname, font, size, lineheight, text, w, q);
  1890. }
  1891. else if (comb > 0)
  1892. {
  1893. float ty = (h - size) / 2;
  1894. fz_append_printf(ctx, buf, "%g %g Td\n", padding, padding+h-baseline-ty);
  1895. write_comb_string(ctx, buf, lang, fontname, font, size, text, (w * 1000 / size) / comb);
  1896. }
  1897. else
  1898. {
  1899. float tx = 0, ty = (h - size) / 2;
  1900. if (q > 0)
  1901. {
  1902. float tw = measure_string(ctx, lang, font, text) * size;
  1903. if (q == 1)
  1904. tx = (w - tw) / 2;
  1905. else
  1906. tx = (w - tw);
  1907. }
  1908. fz_append_printf(ctx, buf, "%g %g Td\n", padding+tx, padding+h-baseline-ty);
  1909. write_string(ctx, buf, lang, font, fontname, size, text, text + strlen(text));
  1910. }
  1911. fz_append_string(ctx, buf, "ET\n");
  1912. }
  1913. fz_always(ctx)
  1914. fz_drop_font(ctx, font);
  1915. fz_catch(ctx)
  1916. fz_rethrow(ctx);
  1917. }
  1918. static void
  1919. layout_variable_text(fz_context *ctx, fz_layout_block *out,
  1920. const char *text, fz_text_language lang, const char *fontname, float size, int q,
  1921. float x, float y, float w, float h, float padding, float baseline, float lineheight,
  1922. int multiline, int comb, int adjust_baseline)
  1923. {
  1924. fz_font *font;
  1925. w -= padding * 2;
  1926. h -= padding * 2;
  1927. font = fz_new_base14_font(ctx, full_font_name(&fontname));
  1928. fz_try(ctx)
  1929. {
  1930. if (size == 0)
  1931. {
  1932. if (multiline)
  1933. size = 12;
  1934. else
  1935. {
  1936. size = w / measure_string(ctx, lang, font, text);
  1937. if (size > h)
  1938. size = h;
  1939. }
  1940. }
  1941. lineheight = size * lineheight;
  1942. baseline = size * baseline;
  1943. if (adjust_baseline)
  1944. {
  1945. /* Make sure baseline is inside rectangle */
  1946. if (baseline + 0.2f * size > h)
  1947. baseline = h - 0.2f * size;
  1948. }
  1949. if (multiline)
  1950. {
  1951. x += padding;
  1952. y += padding + h - baseline;
  1953. layout_string_with_quadding(ctx, out, lang, font, size, lineheight, x, y, text, w, q);
  1954. }
  1955. else if (comb > 0)
  1956. {
  1957. float ty = (h - size) / 2;
  1958. x += padding;
  1959. y += padding + h - baseline - ty;
  1960. layout_comb_string(ctx, out, x, y, text, text + strlen(text), font, size, w / comb);
  1961. }
  1962. else
  1963. {
  1964. float tx = 0, ty = (h - size) / 2;
  1965. if (q > 0)
  1966. {
  1967. float tw = measure_string(ctx, lang, font, text) * size;
  1968. if (q == 1)
  1969. tx = (w - tw) / 2;
  1970. else
  1971. tx = (w - tw);
  1972. }
  1973. x += padding + tx;
  1974. y += padding + h - baseline - ty;
  1975. layout_string(ctx, out, lang, font, size, x, y, text, text + strlen(text));
  1976. }
  1977. }
  1978. fz_always(ctx)
  1979. fz_drop_font(ctx, font);
  1980. fz_catch(ctx)
  1981. fz_rethrow(ctx);
  1982. }
  1983. #if FZ_ENABLE_HTML_ENGINE
  1984. static void
  1985. write_rich_content(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, pdf_obj **res, const char *rc, const char *ds, float size, float w, float h, float b, int multiline)
  1986. {
  1987. /* border_box is the actual rectangle that we are writing into.
  1988. * We use this to feed to the pdfwriting device. */
  1989. fz_rect border_box = { 0, 0, w, h };
  1990. /* content_box is adjusted for padding and has added height.
  1991. * We know a clipping rectangle will have been set to the proper rectangle
  1992. * so we can allow text to flow out the bottom of the rectangle rather than
  1993. * just missing it out. This matches adobe. */
  1994. fz_rect content_box = fz_make_rect(b, b, w - b*2, h + size * 2);
  1995. fz_buffer *inbuf = fz_new_buffer_from_copied_data(ctx, (const unsigned char *)rc, strlen(rc)+1);
  1996. fz_story *story = NULL;
  1997. fz_device *dev = NULL;
  1998. fz_buffer *buf2 = NULL;
  1999. const char *default_css = "@page{margin:0} body{margin:0;line-height:1.2;white-space:pre-wrap;} p{margin:0}";
  2000. char *css = NULL;
  2001. fz_var(story);
  2002. fz_var(dev);
  2003. fz_var(res);
  2004. fz_var(buf2);
  2005. fz_var(css);
  2006. // single-line should be centered in the box. adjust content box accordingly.
  2007. // this matches the math in write_variable_text.
  2008. if (!multiline)
  2009. {
  2010. float ty = ((h - b * 2) - size) / 2;
  2011. content_box.y0 = h - b - 0.8f * size + ty;
  2012. content_box.y1 = content_box.y0 + size * 2;
  2013. }
  2014. fz_try(ctx)
  2015. {
  2016. if (ds)
  2017. css = fz_asprintf(ctx, "%s body{%s}", default_css, ds);
  2018. story = fz_new_story(ctx, inbuf, css ? css : default_css, size, NULL);
  2019. dev = pdf_page_write(ctx, annot->page->doc, border_box, res, &buf2);
  2020. fz_place_story(ctx, story, content_box, NULL);
  2021. fz_draw_story(ctx, story, dev, fz_identity);
  2022. fz_close_device(ctx, dev);
  2023. fz_append_buffer(ctx, buf, buf2);
  2024. }
  2025. fz_always(ctx)
  2026. {
  2027. fz_drop_device(ctx, dev);
  2028. fz_drop_buffer(ctx, buf2);
  2029. fz_drop_story(ctx, story);
  2030. fz_drop_buffer(ctx, inbuf);
  2031. fz_free(ctx, css);
  2032. }
  2033. fz_catch(ctx)
  2034. fz_rethrow(ctx);
  2035. }
  2036. #endif
  2037. #if FZ_ENABLE_HTML_ENGINE
  2038. static char *
  2039. escape_text(fz_context *ctx, const char *s)
  2040. {
  2041. size_t len = 1;
  2042. char c;
  2043. const char *s2;
  2044. char *d, *d2;
  2045. for (s2 = s; (c = *s2++) != 0; len++)
  2046. {
  2047. if (c == '<')
  2048. len += 3; /* &lt; */
  2049. else if (c == '>')
  2050. len += 3; /* &gt; */
  2051. else if (c == '&')
  2052. len += 4; /* &amp; */
  2053. }
  2054. d = d2 = fz_malloc(ctx, len);
  2055. for (s2 = s; (c = *s2++) != 0; )
  2056. {
  2057. if (c == '<')
  2058. {
  2059. *d++ = '&';
  2060. *d++ = 'l';
  2061. *d++ = 't';
  2062. *d++ = ';';
  2063. }
  2064. else if (c == '>')
  2065. {
  2066. *d++ = '&';
  2067. *d++ = 'g';
  2068. *d++ = 't';
  2069. *d++ = ';';
  2070. }
  2071. else if (c == '&')
  2072. {
  2073. *d++ = '&';
  2074. *d++ = 'a';
  2075. *d++ = 'm';
  2076. *d++ = 'p';
  2077. *d++ = ';';
  2078. }
  2079. else
  2080. *d++ = c;
  2081. }
  2082. *d++ = 0;
  2083. return d2;
  2084. }
  2085. int text_needs_rich_layout(fz_context *ctx, const char *s)
  2086. {
  2087. int c, script;
  2088. while (*s)
  2089. {
  2090. s += fz_chartorune(&c, s);
  2091. // base 14 fonts
  2092. if (fz_windows_1252_from_unicode(c) > 0)
  2093. continue;
  2094. if (fz_iso8859_7_from_unicode(c) > 0)
  2095. continue;
  2096. if (fz_koi8u_from_unicode(c) > 0)
  2097. continue;
  2098. // cjk fonts
  2099. script = ucdn_get_script(c);
  2100. if (
  2101. script == UCDN_SCRIPT_HANGUL ||
  2102. script == UCDN_SCRIPT_HIRAGANA ||
  2103. script == UCDN_SCRIPT_KATAKANA ||
  2104. script == UCDN_SCRIPT_BOPOMOFO ||
  2105. script == UCDN_SCRIPT_HAN
  2106. )
  2107. continue;
  2108. return 1;
  2109. }
  2110. return 0;
  2111. }
  2112. static unsigned int hex_from_color(fz_context *ctx, int n, float color[4])
  2113. {
  2114. float rgb[4];
  2115. int r, g, b;
  2116. switch (n)
  2117. {
  2118. default:
  2119. r = g = b = 0;
  2120. break;
  2121. case 1:
  2122. r = g = b = color[0] * 255;
  2123. break;
  2124. case 3:
  2125. r = color[0] * 255;
  2126. g = color[1] * 255;
  2127. b = color[2] * 255;
  2128. break;
  2129. case 4:
  2130. fz_convert_color(ctx, fz_device_cmyk(ctx), color, fz_device_rgb(ctx), rgb, NULL, fz_default_color_params);
  2131. r = rgb[0] * 255;
  2132. g = rgb[1] * 255;
  2133. b = rgb[2] * 255;
  2134. break;
  2135. }
  2136. return (r<<16) | (g<<8) | b;
  2137. }
  2138. #endif
  2139. static float
  2140. pdf_write_line_caption(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, pdf_obj **res, fz_point a, fz_point b)
  2141. {
  2142. float dx, dy, line_length;
  2143. fz_point co;
  2144. float size;
  2145. const char *text;
  2146. pdf_obj *res_font;
  2147. fz_font *font = NULL;
  2148. fz_matrix tm;
  2149. int lang;
  2150. int top;
  2151. float tw;
  2152. fz_var(font);
  2153. fz_try(ctx)
  2154. {
  2155. // vector of line
  2156. dx = b.x - a.x;
  2157. dy = b.y - a.y;
  2158. line_length = hypotf(dx, dy);
  2159. dx /= line_length;
  2160. dy /= line_length;
  2161. text = pdf_annot_contents(ctx, annot);
  2162. lang = pdf_annot_language(ctx, annot);
  2163. co = pdf_dict_get_point(ctx, annot->obj, PDF_NAME(CO));
  2164. font = fz_new_base14_font(ctx, "Helvetica");
  2165. if (!*res)
  2166. *res = pdf_new_dict(ctx, annot->page->doc, 1);
  2167. res_font = pdf_dict_put_dict(ctx, *res, PDF_NAME(Font), 1);
  2168. add_required_fonts(ctx, annot->page->doc, res_font, lang, font, "Helv", text);
  2169. size = 12;
  2170. tw = measure_string(ctx, lang, font, text) * size;
  2171. // don't inline if CP says so
  2172. top = 0;
  2173. if (pdf_dict_get(ctx, annot->obj, PDF_NAME(CP)) == PDF_NAME(Top))
  2174. top = 1;
  2175. // don't inline if caption wouldn't fit
  2176. if (tw + size > line_length)
  2177. top = 1;
  2178. tm = fz_rotate(atan2(dy, dx) * 180 / M_PI);
  2179. tm.e = (a.x + b.x) / 2 - dx * (tw / 2);
  2180. tm.f = (a.y + b.y) / 2 - dy * (tw / 2);
  2181. // caption offset
  2182. if (co.x || co.y)
  2183. {
  2184. // don't write text inline
  2185. top = 1;
  2186. if (co.y < 0)
  2187. co.y -= size;
  2188. tm.e += co.x * dx - co.y * dy;
  2189. tm.f += co.x * dy + co.y * dx;
  2190. }
  2191. else if (top)
  2192. {
  2193. tm.e -= dy * size * 0.2f;
  2194. tm.f += dx * size * 0.2f;
  2195. }
  2196. else
  2197. {
  2198. tm.e += dy * size * 0.3f;
  2199. tm.f -= dx * size * 0.3f;
  2200. }
  2201. fz_append_printf(ctx, buf, "q\n%M cm\n", &tm);
  2202. fz_append_string(ctx, buf, "0 g\n"); // Acrobat always draws captions in black
  2203. fz_append_printf(ctx, buf, "BT\n");
  2204. write_string(ctx, buf, lang, font, "Helv", size, text, text + strlen(text));
  2205. fz_append_printf(ctx, buf, "ET\n");
  2206. fz_append_printf(ctx, buf, "Q\n");
  2207. *rect = fz_include_point_in_rect(*rect, fz_make_point(tm.e - dx * tw/2, tm.f - dy * tw/2));
  2208. *rect = fz_include_point_in_rect(*rect, fz_make_point(tm.e + dx * tw/2, tm.f + dy * tw/2));
  2209. *rect = fz_expand_rect(*rect, size);
  2210. }
  2211. fz_always(ctx)
  2212. {
  2213. fz_drop_font(ctx, font);
  2214. }
  2215. fz_catch(ctx)
  2216. fz_rethrow(ctx);
  2217. if (!top)
  2218. return (tw + size / 2) / 2;
  2219. return 0;
  2220. }
  2221. static void
  2222. pdf_write_free_text_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf,
  2223. fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  2224. {
  2225. const char *font;
  2226. float size, color[4];
  2227. const char *text;
  2228. float w, h, b;
  2229. int q, r, n;
  2230. int lang;
  2231. fz_rect rd;
  2232. pdf_obj *le;
  2233. int ic;
  2234. fz_rect text_box;
  2235. fz_matrix tfm;
  2236. #if FZ_ENABLE_HTML_ENGINE
  2237. const char *rc, *ds;
  2238. char *free_rc = NULL;
  2239. char ds_buf[400];
  2240. #endif
  2241. text = pdf_annot_contents(ctx, annot);
  2242. q = pdf_annot_quadding(ctx, annot);
  2243. pdf_annot_default_appearance(ctx, annot, &font, &size, &n, color);
  2244. lang = pdf_annot_language(ctx, annot);
  2245. rd = pdf_annot_rect_diff(ctx, annot);
  2246. /* /Rotate is an undocumented annotation property supported by Adobe.
  2247. * When Rotate is used, neither the box, nor the arrow move at all.
  2248. * Only the position of the text moves within the box. Thus we don't
  2249. * need to adjust rd at all! */
  2250. r = pdf_dict_get_int(ctx, annot->obj, PDF_NAME(Rotate));
  2251. // Adjust input Rect for RD to get the bounds of the text box area
  2252. text_box = *rect;
  2253. text_box.x0 += rd.x0;
  2254. text_box.y0 += rd.y0;
  2255. text_box.x1 -= rd.x1;
  2256. text_box.y1 -= rd.y1;
  2257. *rect = text_box;
  2258. // Size of text box area (including padding and border)
  2259. w = text_box.x1 - text_box.x0;
  2260. h = text_box.y1 - text_box.y0;
  2261. pdf_write_opacity(ctx, annot, buf, res);
  2262. pdf_write_dash_pattern(ctx, annot, buf, res);
  2263. // Set stroke and fill colors for box and callout line
  2264. ic = pdf_write_fill_color_appearance(ctx, annot, buf);
  2265. write_color0(ctx, buf, n, color, 1);
  2266. b = pdf_write_border_appearance(ctx, annot, buf);
  2267. // Draw Callout line
  2268. if (pdf_name_eq(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(IT)), PDF_NAME(FreeTextCallout)))
  2269. {
  2270. pdf_obj *cl = pdf_dict_get(ctx, annot->obj, PDF_NAME(CL));
  2271. int i, len = pdf_array_len(ctx, cl);
  2272. if (len == 4 || len == 6)
  2273. {
  2274. float xy[6];
  2275. for (i = 0; i < len; i += 2)
  2276. {
  2277. float x = xy[i+0] = pdf_array_get_real(ctx, cl, i+0);
  2278. float y = xy[i+1] = pdf_array_get_real(ctx, cl, i+1);
  2279. if (x < rect->x0) rect->x0 = x;
  2280. if (y < rect->y0) rect->y0 = y;
  2281. if (x > rect->x1) rect->x1 = x;
  2282. if (y > rect->y1) rect->y1 = y;
  2283. }
  2284. fz_append_printf(ctx, buf, "%g %g m\n", xy[0], xy[1]);
  2285. for (i = 2; i < len; i += 2)
  2286. fz_append_printf(ctx, buf, "%g %g l\n", xy[i+0], xy[i+1]);
  2287. fz_append_printf(ctx, buf, "S\n");
  2288. le = pdf_dict_get(ctx, annot->obj, PDF_NAME(LE));
  2289. pdf_write_line_cap_appearance(ctx, buf, rect,
  2290. xy[0], xy[1],
  2291. xy[2] - xy[0], xy[3] - xy[1], b,
  2292. 1, ic, le);
  2293. }
  2294. }
  2295. // Draw text box background
  2296. if (ic)
  2297. fz_append_printf(ctx, buf, "%g %g %g %g re\nf\n", text_box.x0, text_box.y0, w, h);
  2298. // Draw text box border
  2299. if (b > 0)
  2300. fz_append_printf(ctx, buf, "%g %g %g %g re\nS\n", text_box.x0 + b/2, text_box.y0 + b/2, w - b, h - b);
  2301. // Clip text to box
  2302. fz_append_printf(ctx, buf, "%g %g %g %g re\nW\nn\n", text_box.x0 + b, text_box.y0 + b, w - b * 2, h - b * 2);
  2303. // Recompute Rect and RD to account for Callout line
  2304. rd.x0 = text_box.x0 - rect->x0;
  2305. rd.y0 = text_box.y0 - rect->y0;
  2306. rd.x1 = rect->x1 - text_box.x1;
  2307. rd.y1 = rect->y1 - text_box.y1;
  2308. // Compute rotation (and offset) transform for text
  2309. if (r == 90 || r == 270)
  2310. {
  2311. float t = w; w = h; h = t;
  2312. }
  2313. tfm = fz_rotate(r);
  2314. if (r == 270)
  2315. tfm.e += 0, tfm.f += w;
  2316. else if (r == 90)
  2317. tfm.e += h, tfm.f -= 0;
  2318. else if (r == 180)
  2319. tfm.e += w, tfm.f += h;
  2320. tfm.e += text_box.x0;
  2321. tfm.f += text_box.y0;
  2322. fz_append_printf(ctx, buf, "q\n%g %g %g %g %g %g cm\n", tfm.a, tfm.b, tfm.c, tfm.d, tfm.e, tfm.f);
  2323. #if FZ_ENABLE_HTML_ENGINE
  2324. ds = pdf_dict_get_text_string_opt(ctx, annot->obj, PDF_NAME(DS));
  2325. rc = pdf_dict_get_text_string_opt(ctx, annot->obj, PDF_NAME(RC));
  2326. if (!rc && (ds || text_needs_rich_layout(ctx, text)))
  2327. {
  2328. rc = free_rc = escape_text(ctx, text);
  2329. if (!ds)
  2330. {
  2331. fz_snprintf(ds_buf, sizeof ds_buf,
  2332. "font-family:%s;font-size:%gpt;color:#%06x;text-align:%s;",
  2333. full_font_name(&font),
  2334. size,
  2335. hex_from_color(ctx, n, color),
  2336. (q == 0 ? "left" : q == 1 ? "center" : "right")
  2337. );
  2338. ds = ds_buf;
  2339. }
  2340. }
  2341. if (rc)
  2342. {
  2343. fz_try(ctx)
  2344. write_rich_content(ctx, annot, buf, res, rc ? rc : text, ds, size, w, h, b * 2, 1);
  2345. fz_always(ctx)
  2346. fz_free(ctx, free_rc);
  2347. fz_catch(ctx)
  2348. fz_rethrow(ctx);
  2349. }
  2350. else
  2351. #endif
  2352. {
  2353. write_variable_text(ctx, annot, buf, res, lang, text, font, size, n, color, q, w, h, b*2,
  2354. 0.8f, 1.2f, 1, 0, 0);
  2355. }
  2356. fz_append_printf(ctx, buf, "Q\n");
  2357. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(RD), rd);
  2358. *matrix = fz_identity;
  2359. *bbox = *rect;
  2360. }
  2361. static void
  2362. pdf_write_tx_widget_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf,
  2363. const fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res,
  2364. const char *text, int ff)
  2365. {
  2366. fz_text_language lang;
  2367. const char *font;
  2368. float size, color[4];
  2369. float w, h, t, b;
  2370. int has_bc = 0;
  2371. int q, r, n;
  2372. #if FZ_ENABLE_HTML_ENGINE
  2373. const char *rc, *ds;
  2374. char *free_rc = NULL;
  2375. char ds_buf[400];
  2376. #endif
  2377. r = pdf_dict_get_int(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(R));
  2378. q = pdf_annot_quadding(ctx, annot);
  2379. pdf_annot_default_appearance(ctx, annot, &font, &size, &n, color);
  2380. lang = pdf_annot_language(ctx, annot);
  2381. w = rect->x1 - rect->x0;
  2382. h = rect->y1 - rect->y0;
  2383. r = r % 360;
  2384. if (r == 90 || r == 270)
  2385. t = h, h = w, w = t;
  2386. *matrix = fz_rotate(r);
  2387. *bbox = fz_make_rect(0, 0, w, h);
  2388. fz_append_string(ctx, buf, "/Tx BMC\nq\n");
  2389. if (pdf_write_MK_BG_appearance(ctx, annot, buf))
  2390. fz_append_printf(ctx, buf, "0 0 %g %g re\nf\n", w, h);
  2391. b = pdf_write_border_appearance(ctx, annot, buf);
  2392. if (b > 0 && pdf_write_MK_BC_appearance(ctx, annot, buf))
  2393. {
  2394. fz_append_printf(ctx, buf, "%g %g %g %g re\ns\n", b/2, b/2, w-b, h-b);
  2395. has_bc = 1;
  2396. }
  2397. fz_append_printf(ctx, buf, "%g %g %g %g re\nW\nn\n", b, b, w-b*2, h-b*2);
  2398. #if FZ_ENABLE_HTML_ENGINE
  2399. ds = pdf_dict_get_text_string_opt(ctx, annot->obj, PDF_NAME(DS));
  2400. rc = pdf_dict_get_text_string_opt(ctx, annot->obj, PDF_NAME(RV));
  2401. if (!rc && (ds || text_needs_rich_layout(ctx, text)))
  2402. {
  2403. rc = free_rc = escape_text(ctx, text);
  2404. if (!ds)
  2405. {
  2406. fz_snprintf(ds_buf, sizeof ds_buf,
  2407. "font-family:%s;font-size:%gpt;color:#%06x;text-align:%s",
  2408. full_font_name(&font),
  2409. size,
  2410. hex_from_color(ctx, n, color),
  2411. (q == 0 ? "left" : q == 1 ? "center" : "right")
  2412. );
  2413. ds = ds_buf;
  2414. }
  2415. }
  2416. if (rc)
  2417. {
  2418. fz_try(ctx)
  2419. write_rich_content(ctx, annot, buf, res, rc ? rc : text, ds, size, w, h, b * 2,
  2420. (ff & PDF_TX_FIELD_IS_MULTILINE));
  2421. fz_always(ctx)
  2422. fz_free(ctx, free_rc);
  2423. fz_catch(ctx)
  2424. fz_rethrow(ctx);
  2425. }
  2426. else
  2427. #endif
  2428. if (ff & PDF_TX_FIELD_IS_MULTILINE)
  2429. {
  2430. write_variable_text(ctx, annot, buf, res, lang, text, font, size, n, color, q, w, h, b*2,
  2431. 1.116f, 1.116f, 1, 0, 1);
  2432. }
  2433. else if (ff & PDF_TX_FIELD_IS_COMB)
  2434. {
  2435. int maxlen = pdf_dict_get_inheritable_int(ctx, annot->obj, PDF_NAME(MaxLen));
  2436. if (has_bc && maxlen > 1)
  2437. {
  2438. float cell_w = (w - 2 * b) / maxlen;
  2439. int i;
  2440. for (i = 1; i < maxlen; ++i)
  2441. {
  2442. float x = b + cell_w * i;
  2443. fz_append_printf(ctx, buf, "%g %g m %g %g l s\n", x, b, x, h-b);
  2444. }
  2445. }
  2446. write_variable_text(ctx, annot, buf, res, lang, text, font, size, n, color, q, w, h, 0,
  2447. 0.8f, 1.2f, 0, maxlen, 0);
  2448. }
  2449. else
  2450. {
  2451. write_variable_text(ctx, annot, buf, res, lang, text, font, size, n, color, q, w, h, b*2,
  2452. 0.8f, 1.2f, 0, 0, 0);
  2453. }
  2454. fz_append_string(ctx, buf, "Q\nEMC\n");
  2455. }
  2456. fz_layout_block *
  2457. pdf_layout_text_widget(fz_context *ctx, pdf_annot *annot)
  2458. {
  2459. fz_text_language lang;
  2460. fz_layout_block *out;
  2461. const char *font;
  2462. const char *text;
  2463. fz_rect rect;
  2464. float size, color[4];
  2465. float w, h, t, b, x, y;
  2466. int q, r, n;
  2467. int ff;
  2468. rect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  2469. text = pdf_field_value(ctx, annot->obj);
  2470. ff = pdf_field_flags(ctx, annot->obj);
  2471. b = pdf_annot_border_width(ctx, annot);
  2472. r = pdf_dict_get_int(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(R));
  2473. q = pdf_annot_quadding(ctx, annot);
  2474. pdf_annot_default_appearance(ctx, annot, &font, &size, &n, color);
  2475. lang = pdf_annot_language(ctx, annot);
  2476. w = rect.x1 - rect.x0;
  2477. h = rect.y1 - rect.y0;
  2478. r = r % 360;
  2479. if (r == 90 || r == 270)
  2480. t = h, h = w, w = t;
  2481. x = rect.x0;
  2482. y = rect.y0;
  2483. out = fz_new_layout(ctx);
  2484. fz_try(ctx)
  2485. {
  2486. pdf_page_transform(ctx, annot->page, NULL, &out->matrix);
  2487. out->matrix = fz_concat(out->matrix, fz_rotate(r));
  2488. out->inv_matrix = fz_invert_matrix(out->matrix);
  2489. if (ff & PDF_TX_FIELD_IS_MULTILINE)
  2490. {
  2491. layout_variable_text(ctx, out, text, lang, font, size, q, x, y, w, h, b*2, 1.116f, 1.116f, 1, 0, 1);
  2492. }
  2493. else if (ff & PDF_TX_FIELD_IS_COMB)
  2494. {
  2495. int maxlen = pdf_dict_get_inheritable_int(ctx, annot->obj, PDF_NAME(MaxLen));
  2496. layout_variable_text(ctx, out, text, lang, font, size, q, x, y, w, h, 0, 0.8f, 1.2f, 0, maxlen, 0);
  2497. }
  2498. else
  2499. {
  2500. layout_variable_text(ctx, out, text, lang, font, size, q, x, y, w, h, b*2, 0.8f, 1.2f, 0, 0, 0);
  2501. }
  2502. }
  2503. fz_catch(ctx)
  2504. {
  2505. fz_drop_layout(ctx, out);
  2506. fz_rethrow(ctx);
  2507. }
  2508. return out;
  2509. }
  2510. static void
  2511. pdf_write_ch_widget_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf,
  2512. const fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  2513. {
  2514. int ff = pdf_field_flags(ctx, annot->obj);
  2515. if (ff & PDF_CH_FIELD_IS_COMBO)
  2516. {
  2517. /* TODO: Pop-down arrow */
  2518. pdf_write_tx_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res,
  2519. pdf_field_value(ctx, annot->obj), 0);
  2520. }
  2521. else
  2522. {
  2523. fz_buffer *text = fz_new_buffer(ctx, 1024);
  2524. fz_try(ctx)
  2525. {
  2526. pdf_obj *opt = pdf_dict_get(ctx, annot->obj, PDF_NAME(Opt));
  2527. int i = pdf_dict_get_int(ctx, annot->obj, PDF_NAME(TI));
  2528. int n = pdf_array_len(ctx, opt);
  2529. /* TODO: Scrollbar */
  2530. /* TODO: Highlight selected items */
  2531. if (i < 0)
  2532. i = 0;
  2533. for (; i < n; ++i)
  2534. {
  2535. pdf_obj *val = pdf_array_get(ctx, opt, i);
  2536. if (pdf_is_array(ctx, val))
  2537. fz_append_string(ctx, text, pdf_array_get_text_string(ctx, val, 1));
  2538. else
  2539. fz_append_string(ctx, text, pdf_to_text_string(ctx, val));
  2540. fz_append_byte(ctx, text, '\n');
  2541. }
  2542. pdf_write_tx_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res,
  2543. fz_string_from_buffer(ctx, text), PDF_TX_FIELD_IS_MULTILINE);
  2544. }
  2545. fz_always(ctx)
  2546. fz_drop_buffer(ctx, text);
  2547. fz_catch(ctx)
  2548. fz_rethrow(ctx);
  2549. }
  2550. }
  2551. static void
  2552. pdf_write_sig_widget_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf,
  2553. const fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  2554. {
  2555. float x0 = rect->x0 + 1;
  2556. float y0 = rect->y0 + 1;
  2557. float x1 = rect->x1 - 1;
  2558. float y1 = rect->y1 - 1;
  2559. float w = x1 - x0;
  2560. float h = y1 - y0;
  2561. fz_append_printf(ctx, buf, "1 w\n0 G\n");
  2562. fz_append_printf(ctx, buf, "%g %g %g %g re\n", x0, y0, w, h);
  2563. fz_append_printf(ctx, buf, "%g %g m %g %g l\n", x0, y0, x1, y1);
  2564. fz_append_printf(ctx, buf, "%g %g m %g %g l\n", x1, y0, x0, y1);
  2565. fz_append_printf(ctx, buf, "s\n");
  2566. *bbox = *rect;
  2567. *matrix = fz_identity;
  2568. }
  2569. static void
  2570. pdf_write_widget_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf,
  2571. fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  2572. {
  2573. pdf_obj *ft = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(FT));
  2574. if (pdf_name_eq(ctx, ft, PDF_NAME(Tx)))
  2575. {
  2576. int ff = pdf_field_flags(ctx, annot->obj);
  2577. char *format = NULL;
  2578. const char *text = NULL;
  2579. if (!annot->ignore_trigger_events)
  2580. {
  2581. format = pdf_field_event_format(ctx, annot->page->doc, annot->obj);
  2582. if (format)
  2583. text = format;
  2584. else
  2585. text = pdf_field_value(ctx, annot->obj);
  2586. }
  2587. else
  2588. {
  2589. text = pdf_field_value(ctx, annot->obj);
  2590. }
  2591. fz_try(ctx)
  2592. pdf_write_tx_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res, text, ff);
  2593. fz_always(ctx)
  2594. fz_free(ctx, format);
  2595. fz_catch(ctx)
  2596. fz_rethrow(ctx);
  2597. }
  2598. else if (pdf_name_eq(ctx, ft, PDF_NAME(Ch)))
  2599. {
  2600. pdf_write_ch_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res);
  2601. }
  2602. else if (pdf_name_eq(ctx, ft, PDF_NAME(Sig)))
  2603. {
  2604. pdf_write_sig_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res);
  2605. }
  2606. else
  2607. {
  2608. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create appearance stream for %s widgets", pdf_to_name(ctx, ft));
  2609. }
  2610. }
  2611. static void
  2612. pdf_write_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf,
  2613. fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res)
  2614. {
  2615. switch (pdf_annot_type(ctx, annot))
  2616. {
  2617. default:
  2618. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "cannot create appearance stream for %s annotations",
  2619. pdf_dict_get_name(ctx, annot->obj, PDF_NAME(Subtype)));
  2620. case PDF_ANNOT_WIDGET:
  2621. pdf_write_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res);
  2622. break;
  2623. case PDF_ANNOT_INK:
  2624. pdf_write_ink_appearance(ctx, annot, buf, rect, res);
  2625. *matrix = fz_identity;
  2626. *bbox = *rect;
  2627. break;
  2628. case PDF_ANNOT_POLYGON:
  2629. pdf_write_polygon_appearance(ctx, annot, buf, rect, res, 1);
  2630. *matrix = fz_identity;
  2631. *bbox = *rect;
  2632. break;
  2633. case PDF_ANNOT_POLY_LINE:
  2634. pdf_write_polygon_appearance(ctx, annot, buf, rect, res, 0);
  2635. *matrix = fz_identity;
  2636. *bbox = *rect;
  2637. break;
  2638. case PDF_ANNOT_LINE:
  2639. pdf_write_line_appearance(ctx, annot, buf, rect, res);
  2640. *matrix = fz_identity;
  2641. *bbox = *rect;
  2642. break;
  2643. case PDF_ANNOT_SQUARE:
  2644. pdf_write_square_appearance(ctx, annot, buf, rect, res);
  2645. *matrix = fz_identity;
  2646. *bbox = *rect;
  2647. break;
  2648. case PDF_ANNOT_CIRCLE:
  2649. pdf_write_circle_appearance(ctx, annot, buf, rect, res);
  2650. *matrix = fz_identity;
  2651. *bbox = *rect;
  2652. break;
  2653. case PDF_ANNOT_CARET:
  2654. pdf_write_caret_appearance(ctx, annot, buf, rect, bbox, res);
  2655. *matrix = fz_identity;
  2656. break;
  2657. case PDF_ANNOT_TEXT:
  2658. case PDF_ANNOT_FILE_ATTACHMENT:
  2659. case PDF_ANNOT_SOUND:
  2660. pdf_write_icon_appearance(ctx, annot, buf, rect, bbox, res);
  2661. *matrix = fz_identity;
  2662. break;
  2663. case PDF_ANNOT_HIGHLIGHT:
  2664. pdf_write_highlight_appearance(ctx, annot, buf, rect, res);
  2665. *matrix = fz_identity;
  2666. *bbox = *rect;
  2667. break;
  2668. case PDF_ANNOT_UNDERLINE:
  2669. pdf_write_underline_appearance(ctx, annot, buf, rect, res);
  2670. *matrix = fz_identity;
  2671. *bbox = *rect;
  2672. break;
  2673. case PDF_ANNOT_STRIKE_OUT:
  2674. pdf_write_strike_out_appearance(ctx, annot, buf, rect, res);
  2675. *matrix = fz_identity;
  2676. *bbox = *rect;
  2677. break;
  2678. case PDF_ANNOT_SQUIGGLY:
  2679. pdf_write_squiggly_appearance(ctx, annot, buf, rect, res);
  2680. *matrix = fz_identity;
  2681. *bbox = *rect;
  2682. break;
  2683. case PDF_ANNOT_REDACT:
  2684. pdf_write_redact_appearance(ctx, annot, buf, rect, res);
  2685. *matrix = fz_identity;
  2686. *bbox = *rect;
  2687. break;
  2688. case PDF_ANNOT_STAMP:
  2689. pdf_write_stamp_appearance(ctx, annot, buf, rect, bbox, matrix, res);
  2690. break;
  2691. case PDF_ANNOT_FREE_TEXT:
  2692. pdf_write_free_text_appearance(ctx, annot, buf, rect, bbox, matrix, res);
  2693. break;
  2694. }
  2695. }
  2696. static pdf_obj *draw_push_button(fz_context *ctx, pdf_annot *annot, fz_rect bbox, fz_matrix matrix, float w, float h,
  2697. const char *caption, const char *font, float size, int n, float *color,
  2698. int down)
  2699. {
  2700. pdf_obj *ap, *res = NULL;
  2701. fz_buffer *buf;
  2702. float bc[3] = { 0, 0, 0 };
  2703. float bg[3] = { 0.8f, 0.8f, 0.8f };
  2704. float hi[3], sh[3];
  2705. int has_bg, has_bc;
  2706. float b;
  2707. int i;
  2708. buf = fz_new_buffer(ctx, 1024);
  2709. fz_var(res);
  2710. fz_try(ctx)
  2711. {
  2712. b = pdf_annot_border_width(ctx, annot);
  2713. has_bc = pdf_annot_MK_BC_rgb(ctx, annot, bc);
  2714. has_bg = pdf_annot_MK_BG_rgb(ctx, annot, bg);
  2715. for (i = 0; i < 3; ++i)
  2716. {
  2717. if (down)
  2718. {
  2719. sh[i] = 1 - (1 - bg[i]) / 2;
  2720. hi[i] = bg[i] / 2;
  2721. }
  2722. else
  2723. {
  2724. hi[i] = 1 - (1 - bg[i]) / 2;
  2725. sh[i] = bg[i] / 2;
  2726. }
  2727. }
  2728. fz_append_string(ctx, buf, "q\n");
  2729. fz_append_printf(ctx, buf, "%g w\n", b);
  2730. if (has_bg)
  2731. {
  2732. fz_append_printf(ctx, buf, "%g %g %g rg\n", bg[0], bg[1], bg[2]);
  2733. fz_append_printf(ctx, buf, "0 0 %g %g re\nf\n", 0, 0, w, h);
  2734. }
  2735. if (has_bc && b > 0)
  2736. {
  2737. fz_append_printf(ctx, buf, "%g %g %g RG\n", bc[0], bc[1], bc[2]);
  2738. fz_append_printf(ctx, buf, "%g %g %g %g re\nS\n", b/2, b/2, w-b, h-b);
  2739. }
  2740. if (has_bg)
  2741. {
  2742. fz_append_printf(ctx, buf, "%g %g %g rg\n", hi[0], hi[1], hi[2]);
  2743. fz_append_printf(ctx, buf, "%g %g m %g %g l %g %g l %g %g l %g %g l %g %g l f\n",
  2744. b, b, b, h-b, w-b, h-b, w-b-2, h-b-2, b+2, h-b-2, b+2, b+2);
  2745. fz_append_printf(ctx, buf, "%g %g %g rg\n", sh[0], sh[1], sh[2]);
  2746. fz_append_printf(ctx, buf, "%g %g m %g %g l %g %g l %g %g l %g %g l %g %g l f\n",
  2747. b, b, b+2, b+2, w-b-2, b+2, w-b-2, h-b-2, w-b, h-b, w-b, b);
  2748. }
  2749. if (down)
  2750. fz_append_string(ctx, buf, "1 0 0 1 2 -2 cm\n");
  2751. write_variable_text(ctx, annot, buf, &res, FZ_LANG_UNSET, caption, font, size, n, color, 1, w, h, b+6, 0.8f, 1.2f, 0, 0, 0);
  2752. fz_append_string(ctx, buf, "Q\n");
  2753. ap = pdf_new_xobject(ctx, annot->page->doc, bbox, matrix, res, buf);
  2754. }
  2755. fz_always(ctx)
  2756. {
  2757. pdf_drop_obj(ctx, res);
  2758. fz_drop_buffer(ctx, buf);
  2759. }
  2760. fz_catch(ctx)
  2761. fz_rethrow(ctx);
  2762. return ap;
  2763. }
  2764. static pdf_obj *draw_radio_button(fz_context *ctx, pdf_annot *annot, fz_rect bbox, fz_matrix matrix, float w, float h, int yes)
  2765. {
  2766. pdf_obj *ap;
  2767. fz_buffer *buf;
  2768. float b;
  2769. buf = fz_new_buffer(ctx, 1024);
  2770. fz_try(ctx)
  2771. {
  2772. fz_append_string(ctx, buf, "q\n");
  2773. if (pdf_write_MK_BG_appearance(ctx, annot, buf))
  2774. {
  2775. draw_circle_in_box(ctx, buf, 0, 0, 0, w, h);
  2776. fz_append_string(ctx, buf, "f\n");
  2777. }
  2778. b = pdf_write_border_appearance(ctx, annot, buf);
  2779. if (b > 0 && pdf_write_MK_BC_appearance(ctx, annot, buf))
  2780. {
  2781. draw_circle_in_box(ctx, buf, b, 0, 0, w, h);
  2782. fz_append_string(ctx, buf, "s\n");
  2783. }
  2784. if (yes)
  2785. {
  2786. fz_append_string(ctx, buf, "0 g\n");
  2787. draw_circle(ctx, buf, (w-b*2)/4, (h-b*2)/4, w/2, h/2);
  2788. fz_append_string(ctx, buf, "f\n");
  2789. }
  2790. fz_append_string(ctx, buf, "Q\n");
  2791. ap = pdf_new_xobject(ctx, annot->page->doc, bbox, matrix, NULL, buf);
  2792. }
  2793. fz_always(ctx)
  2794. fz_drop_buffer(ctx, buf);
  2795. fz_catch(ctx)
  2796. fz_rethrow(ctx);
  2797. return ap;
  2798. }
  2799. static pdf_obj *draw_check_button(fz_context *ctx, pdf_annot *annot, fz_rect bbox, fz_matrix matrix, float w, float h, int yes)
  2800. {
  2801. float black[1] = { 0 };
  2802. pdf_obj *ap, *res = NULL;
  2803. fz_buffer *buf;
  2804. float b;
  2805. fz_var(res);
  2806. buf = fz_new_buffer(ctx, 1024);
  2807. fz_try(ctx)
  2808. {
  2809. fz_append_string(ctx, buf, "q\n");
  2810. if (pdf_write_MK_BG_appearance(ctx, annot, buf))
  2811. fz_append_printf(ctx, buf, "0 0 %g %g re\nf\n", w, h);
  2812. b = pdf_write_border_appearance(ctx, annot, buf);
  2813. if (b > 0 && pdf_write_MK_BC_appearance(ctx, annot, buf))
  2814. fz_append_printf(ctx, buf, "%g %g %g %g re\nS\n", b/2, b/2, w-b, h-b);
  2815. if (yes)
  2816. write_variable_text(ctx, annot, buf, &res, FZ_LANG_UNSET, "3", "ZaDb", h, nelem(black), black, 0, w, h, b+h/10, 0.8f, 1.2f, 0, 0, 0);
  2817. fz_append_string(ctx, buf, "Q\n");
  2818. ap = pdf_new_xobject(ctx, annot->page->doc, bbox, matrix, res, buf);
  2819. }
  2820. fz_always(ctx)
  2821. {
  2822. pdf_drop_obj(ctx, res);
  2823. fz_drop_buffer(ctx, buf);
  2824. }
  2825. fz_catch(ctx)
  2826. fz_rethrow(ctx);
  2827. return ap;
  2828. }
  2829. static void pdf_update_button_appearance(fz_context *ctx, pdf_annot *annot)
  2830. {
  2831. int ff = pdf_field_flags(ctx, annot->obj);
  2832. fz_rect rect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  2833. fz_matrix matrix;
  2834. fz_rect bbox;
  2835. float w, h, t;
  2836. int r;
  2837. r = pdf_dict_get_int(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(MK)), PDF_NAME(R));
  2838. w = rect.x1 - rect.x0;
  2839. h = rect.y1 - rect.y0;
  2840. r = r % 360;
  2841. if (r == 90 || r == 270)
  2842. t = h, h = w, w = t;
  2843. matrix = fz_rotate(r);
  2844. bbox = fz_make_rect(0, 0, w, h);
  2845. if (ff & PDF_BTN_FIELD_IS_PUSHBUTTON)
  2846. {
  2847. pdf_obj *ap_n = NULL;
  2848. pdf_obj *ap_d = NULL;
  2849. fz_var(ap_n);
  2850. fz_var(ap_d);
  2851. fz_try(ctx)
  2852. {
  2853. pdf_obj *ap, *MK, *CA, *AC;
  2854. const char *font;
  2855. const char *label;
  2856. float size, color[4];
  2857. int n;
  2858. pdf_annot_default_appearance(ctx, annot, &font, &size, &n, color);
  2859. MK = pdf_dict_get(ctx, annot->obj, PDF_NAME(MK));
  2860. CA = pdf_dict_get(ctx, MK, PDF_NAME(CA));
  2861. AC = pdf_dict_get(ctx, MK, PDF_NAME(AC));
  2862. label = pdf_to_text_string(ctx, CA);
  2863. ap_n = draw_push_button(ctx, annot, bbox, matrix, w, h, label, font, size, n, color, 0);
  2864. label = pdf_to_text_string(ctx, AC ? AC : CA);
  2865. ap_d = draw_push_button(ctx, annot, bbox, matrix, w, h, label, font, size, n, color, 1);
  2866. ap = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(AP), 2);
  2867. pdf_dict_put(ctx, ap, PDF_NAME(N), ap_n);
  2868. pdf_dict_put(ctx, ap, PDF_NAME(D), ap_d);
  2869. }
  2870. fz_always(ctx)
  2871. {
  2872. pdf_drop_obj(ctx, ap_n);
  2873. pdf_drop_obj(ctx, ap_d);
  2874. }
  2875. fz_catch(ctx)
  2876. fz_rethrow(ctx);
  2877. }
  2878. else
  2879. {
  2880. pdf_obj *as_yes = NULL;
  2881. pdf_obj *ap_off = NULL;
  2882. pdf_obj *ap_yes = NULL;
  2883. fz_var(ap_off);
  2884. fz_var(ap_yes);
  2885. fz_var(as_yes);
  2886. fz_try(ctx)
  2887. {
  2888. pdf_obj *ap, *ap_n, *as;
  2889. if (w > h) w = h;
  2890. if (h > w) h = w;
  2891. if (ff & PDF_BTN_FIELD_IS_RADIO)
  2892. {
  2893. ap_off = draw_radio_button(ctx, annot, bbox, matrix, w, h, 0);
  2894. ap_yes = draw_radio_button(ctx, annot, bbox, matrix, w, h, 1);
  2895. }
  2896. else
  2897. {
  2898. ap_off = draw_check_button(ctx, annot, bbox, matrix, w, h, 0);
  2899. ap_yes = draw_check_button(ctx, annot, bbox, matrix, w, h, 1);
  2900. }
  2901. as = pdf_dict_get(ctx, annot->obj, PDF_NAME(AS));
  2902. if (!as)
  2903. {
  2904. pdf_dict_put(ctx, annot->obj, PDF_NAME(AS), PDF_NAME(Off));
  2905. as = PDF_NAME(Off);
  2906. }
  2907. if (as == PDF_NAME(Off))
  2908. as_yes = pdf_keep_obj(ctx, pdf_button_field_on_state(ctx, annot->obj));
  2909. else
  2910. as_yes = pdf_keep_obj(ctx, as);
  2911. ap = pdf_dict_put_dict(ctx, annot->obj, PDF_NAME(AP), 2);
  2912. ap_n = pdf_dict_put_dict(ctx, ap, PDF_NAME(N), 2);
  2913. pdf_dict_put(ctx, ap_n, PDF_NAME(Off), ap_off);
  2914. pdf_dict_put(ctx, ap_n, as_yes, ap_yes);
  2915. }
  2916. fz_always(ctx)
  2917. {
  2918. pdf_drop_obj(ctx, as_yes);
  2919. pdf_drop_obj(ctx, ap_yes);
  2920. pdf_drop_obj(ctx, ap_off);
  2921. }
  2922. fz_catch(ctx)
  2923. {
  2924. fz_rethrow(ctx);
  2925. }
  2926. }
  2927. pdf_set_annot_resynthesised(ctx, annot);
  2928. }
  2929. static void draw_logo(fz_context *ctx, fz_path *path)
  2930. {
  2931. /* Use mupdf logo for signature appearance background. */
  2932. fz_moveto(ctx, path, 122.25f, 0.0f);
  2933. fz_lineto(ctx, path, 122.25f, 14.249f);
  2934. fz_curveto(ctx, path, 125.98f, 13.842f, 129.73f, 13.518f, 133.5f, 13.277f);
  2935. fz_lineto(ctx, path, 133.5f, 0.0f);
  2936. fz_lineto(ctx, path, 122.25f, 0.0f);
  2937. fz_closepath(ctx, path);
  2938. fz_moveto(ctx, path, 140.251f, 0.0f);
  2939. fz_lineto(ctx, path, 140.251f, 12.935f);
  2940. fz_curveto(ctx, path, 152.534f, 12.477f, 165.03f, 12.899f, 177.75f, 14.249f);
  2941. fz_lineto(ctx, path, 177.75f, 21.749f);
  2942. fz_curveto(ctx, path, 165.304f, 20.413f, 152.809f, 19.871f, 140.251f, 20.348f);
  2943. fz_lineto(ctx, path, 140.251f, 39.0f);
  2944. fz_lineto(ctx, path, 133.5f, 39.0f);
  2945. fz_lineto(ctx, path, 133.5f, 20.704f);
  2946. fz_curveto(ctx, path, 129.756f, 20.956f, 126.006f, 21.302f, 122.25f, 21.749f);
  2947. fz_lineto(ctx, path, 122.25f, 50.999f);
  2948. fz_lineto(ctx, path, 177.751f, 50.999f);
  2949. fz_lineto(ctx, path, 177.751f, 0.0f);
  2950. fz_lineto(ctx, path, 140.251f, 0.0f);
  2951. fz_closepath(ctx, path);
  2952. fz_moveto(ctx, path, 23.482f, 129.419f);
  2953. fz_curveto(ctx, path, -20.999f, 199.258f, -0.418f, 292.039f, 69.42f, 336.519f);
  2954. fz_curveto(ctx, path, 139.259f, 381.0f, 232.04f, 360.419f, 276.52f, 290.581f);
  2955. fz_curveto(ctx, path, 321.001f, 220.742f, 300.42f, 127.961f, 230.582f, 83.481f);
  2956. fz_curveto(ctx, path, 160.743f, 39.0f, 67.962f, 59.581f, 23.482f, 129.419f);
  2957. fz_closepath(ctx, path);
  2958. fz_moveto(ctx, path, 254.751f, 128.492f);
  2959. fz_curveto(ctx, path, 303.074f, 182.82f, 295.364f, 263.762f, 237.541f, 309.165f);
  2960. fz_curveto(ctx, path, 179.718f, 354.568f, 93.57f, 347.324f, 45.247f, 292.996f);
  2961. fz_curveto(ctx, path, -3.076f, 238.668f, 4.634f, 157.726f, 62.457f, 112.323f);
  2962. fz_curveto(ctx, path, 120.28f, 66.92f, 206.428f, 74.164f, 254.751f, 128.492f);
  2963. fz_closepath(ctx, path);
  2964. fz_moveto(ctx, path, 111.0f, 98.999f);
  2965. fz_curveto(ctx, path, 87.424f, 106.253f, 68.25f, 122.249f, 51.75f, 144.749f);
  2966. fz_lineto(ctx, path, 103.5f, 297.749f);
  2967. fz_lineto(ctx, path, 213.75f, 298.499f);
  2968. fz_curveto(ctx, path, 206.25f, 306.749f, 195.744f, 311.478f, 185.25f, 314.249f);
  2969. fz_curveto(ctx, path, 164.22f, 319.802f, 141.22f, 319.775f, 120.0f, 314.999f);
  2970. fz_curveto(ctx, path, 96.658f, 309.745f, 77.25f, 298.499f, 55.5f, 283.499f);
  2971. fz_curveto(ctx, path, 69.75f, 299.249f, 84.617f, 311.546f, 102.75f, 319.499f);
  2972. fz_curveto(ctx, path, 117.166f, 325.822f, 133.509f, 327.689f, 149.25f, 327.749f);
  2973. fz_curveto(ctx, path, 164.21f, 327.806f, 179.924f, 326.532f, 193.5f, 320.249f);
  2974. fz_curveto(ctx, path, 213.95f, 310.785f, 232.5f, 294.749f, 245.25f, 276.749f);
  2975. fz_lineto(ctx, path, 227.25f, 276.749f);
  2976. fz_curveto(ctx, path, 213.963f, 276.749f, 197.25f, 263.786f, 197.25f, 250.499f);
  2977. fz_lineto(ctx, path, 197.25f, 112.499f);
  2978. fz_curveto(ctx, path, 213.75f, 114.749f, 228.0f, 127.499f, 241.5f, 140.999f);
  2979. fz_curveto(ctx, path, 231.75f, 121.499f, 215.175f, 109.723f, 197.25f, 101.249f);
  2980. fz_curveto(ctx, path, 181.5f, 95.249f, 168.412f, 94.775f, 153.0f, 94.499f);
  2981. fz_curveto(ctx, path, 139.42f, 94.256f, 120.75f, 95.999f, 111.0f, 98.999f);
  2982. fz_closepath(ctx, path);
  2983. fz_moveto(ctx, path, 125.25f, 105.749f);
  2984. fz_lineto(ctx, path, 125.25f, 202.499f);
  2985. fz_lineto(ctx, path, 95.25f, 117.749f);
  2986. fz_curveto(ctx, path, 105.75f, 108.749f, 114.0f, 105.749f, 125.25f, 105.749f);
  2987. fz_closepath(ctx, path);
  2988. };
  2989. static float logo_color[3] = { (float)0xa4 / (float)0xFF, (float)0xca / (float)0xFF, (float)0xf5 / (float)0xFF };
  2990. fz_display_list *
  2991. pdf_signature_appearance_signed(fz_context *ctx, fz_rect rect, fz_text_language lang, fz_image *img, const char *left_text, const char *right_text, int include_logo)
  2992. {
  2993. fz_display_list *dlist = NULL;
  2994. fz_device *dev = NULL;
  2995. fz_text *text = NULL;
  2996. fz_colorspace *cs = NULL;
  2997. fz_path *path = NULL;
  2998. fz_font *font = NULL;
  2999. fz_var(path);
  3000. fz_var(dlist);
  3001. fz_var(dev);
  3002. fz_var(text);
  3003. fz_var(font);
  3004. fz_try(ctx)
  3005. {
  3006. fz_rect prect;
  3007. fz_rect logo_bounds;
  3008. fz_matrix logo_tm;
  3009. float color[] = { 0.0, 0.0, 0.0 };
  3010. font = fz_new_base14_font(ctx, "Helvetica");
  3011. dlist = fz_new_display_list(ctx, rect);
  3012. dev = fz_new_list_device(ctx, dlist);
  3013. cs = fz_device_rgb(ctx);
  3014. if (include_logo)
  3015. {
  3016. path = fz_new_path(ctx);
  3017. draw_logo(ctx, path);
  3018. logo_bounds = fz_bound_path(ctx, path, NULL, fz_identity);
  3019. logo_tm = center_rect_within_rect(logo_bounds, rect);
  3020. fz_fill_path(ctx, dev, path, 0, logo_tm, cs, logo_color, 1.0f, fz_default_color_params);
  3021. }
  3022. prect = rect;
  3023. /* If there is to be info on the right then use only the left half of the rectangle for
  3024. * what is intended for the left */
  3025. if (right_text)
  3026. prect.x1 = (prect.x0 + prect.x1) / 2.0f;
  3027. if (img)
  3028. {
  3029. float img_aspect = (float) img->w / img->h;
  3030. float rectw = prect.x1 - prect.x0;
  3031. float recth = prect.y1 - prect.y0;
  3032. float midx = (prect.x0 + prect.x1) / 2.0f;
  3033. float midy = (prect.y0 + prect.y1) / 2.0f;
  3034. float rect_aspect = rectw / recth;
  3035. float scale = img_aspect > rect_aspect ? rectw / img->w : recth / img->h;
  3036. fz_matrix ctm = fz_pre_translate(fz_pre_scale(fz_translate(midx, midy), scale * img->w, scale * img->h), -0.5, -0.5);
  3037. fz_fill_image(ctx, dev, img, ctm, 1.0f, fz_default_color_params);
  3038. }
  3039. if (left_text)
  3040. {
  3041. text = pdf_layout_fit_text(ctx, font, lang, left_text, prect);
  3042. fz_fill_text(ctx, dev, text, fz_identity, cs, color, 1.0f, fz_default_color_params);
  3043. fz_drop_text(ctx, text);
  3044. text = NULL;
  3045. }
  3046. prect = rect;
  3047. /* If there is to be info on the left then use only the right half of the rectangle for
  3048. * what is intended for the right */
  3049. if (img || left_text)
  3050. prect.x0 = (prect.x0 + prect.x1) / 2.0f;
  3051. if (right_text)
  3052. {
  3053. text = pdf_layout_fit_text(ctx, font, lang, right_text, prect);
  3054. fz_fill_text(ctx, dev, text, fz_identity, cs, color, 1.0f, fz_default_color_params);
  3055. }
  3056. }
  3057. fz_always(ctx)
  3058. {
  3059. fz_drop_device(ctx, dev);
  3060. fz_drop_path(ctx, path);
  3061. fz_drop_text(ctx, text);
  3062. fz_drop_font(ctx, font);
  3063. }
  3064. fz_catch(ctx)
  3065. {
  3066. fz_drop_display_list(ctx, dlist);
  3067. fz_rethrow(ctx);
  3068. }
  3069. return dlist;
  3070. }
  3071. fz_display_list *
  3072. pdf_signature_appearance_unsigned(fz_context *ctx, fz_rect rect, fz_text_language lang)
  3073. {
  3074. fz_display_list *dlist = NULL;
  3075. fz_device *dev = NULL;
  3076. fz_text *text = NULL;
  3077. fz_colorspace *cs = NULL;
  3078. fz_path *path = NULL;
  3079. fz_font *font = NULL;
  3080. fz_var(path);
  3081. fz_var(dlist);
  3082. fz_var(dev);
  3083. fz_var(text);
  3084. fz_var(font);
  3085. fz_try(ctx)
  3086. {
  3087. float text_color[] = { 1.0f, 1.0f, 1.0f };
  3088. float arrow_color[] = { 0.95f, 0.33f, 0.18f };
  3089. dlist = fz_new_display_list(ctx, rect);
  3090. dev = fz_new_list_device(ctx, dlist);
  3091. rect.y1 = rect.y0 + (rect.y1 - rect.y0) / 6;
  3092. rect.x1 = rect.x0 + (rect.y1 - rect.y0) * 4;
  3093. font = fz_new_base14_font(ctx, "Helvetica");
  3094. path = fz_new_path(ctx);
  3095. /* Draw a rectangle with a protrusion to the right [xxxxx> */
  3096. fz_moveto(ctx, path, rect.x0, rect.y0);
  3097. fz_lineto(ctx, path, rect.x1, rect.y0);
  3098. fz_lineto(ctx, path, rect.x1 + (rect.y1 - rect.y0) / 2.0f, (rect.y0 + rect.y1) / 2.0f);
  3099. fz_lineto(ctx, path, rect.x1, rect.y1);
  3100. fz_lineto(ctx, path, rect.x0, rect.y1);
  3101. fz_closepath(ctx, path);
  3102. cs = fz_device_rgb(ctx);
  3103. fz_fill_path(ctx, dev, path, 0, fz_identity, cs, arrow_color, 1.0f, fz_default_color_params);
  3104. text = pdf_layout_fit_text(ctx, font, lang, "SIGN", rect);
  3105. fz_fill_text(ctx, dev, text, fz_identity, cs, text_color, 1.0f, fz_default_color_params);
  3106. fz_drop_text(ctx, text);
  3107. text = NULL;
  3108. }
  3109. fz_always(ctx)
  3110. {
  3111. fz_drop_device(ctx, dev);
  3112. fz_drop_path(ctx, path);
  3113. fz_drop_text(ctx, text);
  3114. fz_drop_font(ctx, font);
  3115. }
  3116. fz_catch(ctx)
  3117. {
  3118. fz_drop_display_list(ctx, dlist);
  3119. fz_rethrow(ctx);
  3120. }
  3121. return dlist;
  3122. }
  3123. char *
  3124. pdf_signature_info(fz_context *ctx, const char *name, pdf_pkcs7_distinguished_name *dn, const char *reason, const char *location, int64_t date, int include_labels)
  3125. {
  3126. fz_buffer *fzbuf = NULL;
  3127. char *dn_str = NULL;
  3128. char *full_str = NULL;
  3129. time_t tdate = (time_t)date;
  3130. fz_var(fzbuf);
  3131. fz_var(dn_str);
  3132. fz_try(ctx)
  3133. {
  3134. #ifdef _POSIX_SOURCE
  3135. struct tm tmbuf, *tm = localtime_r(&tdate, &tmbuf);
  3136. #else
  3137. struct tm *tm = localtime(&tdate);
  3138. #endif
  3139. char now_str[40];
  3140. size_t len = 0;
  3141. #ifdef CLUSTER
  3142. memset(&date, 0, sizeof(date));
  3143. memset(tm, 0, sizeof(*tm));
  3144. #endif
  3145. fzbuf = fz_new_buffer(ctx, 256);
  3146. if (name && strlen(name) > 0)
  3147. {
  3148. if (include_labels)
  3149. fz_append_string(ctx, fzbuf, "Digitally signed by ");
  3150. fz_append_string(ctx, fzbuf, name);
  3151. }
  3152. if (dn)
  3153. {
  3154. fz_append_string(ctx, fzbuf, "\n");
  3155. if (include_labels)
  3156. fz_append_string(ctx, fzbuf, "DN: ");
  3157. dn_str = pdf_signature_format_distinguished_name(ctx, dn);
  3158. fz_append_string(ctx, fzbuf, dn_str);
  3159. }
  3160. if (reason && strlen(reason) > 0)
  3161. {
  3162. fz_append_string(ctx, fzbuf, "\n");
  3163. if (include_labels)
  3164. fz_append_string(ctx, fzbuf, "Reason: ");
  3165. fz_append_string(ctx, fzbuf, reason);
  3166. }
  3167. if (location && strlen(location) > 0)
  3168. {
  3169. fz_append_string(ctx, fzbuf, "\n");
  3170. if (include_labels)
  3171. fz_append_string(ctx, fzbuf, "Location: ");
  3172. fz_append_string(ctx, fzbuf, location);
  3173. }
  3174. if (date >= 0)
  3175. {
  3176. len = strftime(now_str, sizeof now_str, "%FT%T%z", tm);
  3177. if (len)
  3178. {
  3179. fz_append_string(ctx, fzbuf, "\n");
  3180. if (include_labels)
  3181. fz_append_string(ctx, fzbuf, "Date: ");
  3182. fz_append_string(ctx, fzbuf, now_str);
  3183. }
  3184. }
  3185. fz_terminate_buffer(ctx, fzbuf);
  3186. (void)fz_buffer_extract(ctx, fzbuf, (unsigned char **)&full_str);
  3187. }
  3188. fz_always(ctx)
  3189. {
  3190. fz_drop_buffer(ctx, fzbuf);
  3191. fz_free(ctx, dn_str);
  3192. }
  3193. fz_catch(ctx)
  3194. {
  3195. fz_rethrow(ctx);
  3196. }
  3197. return full_str;
  3198. }
  3199. void
  3200. pdf_annot_push_local_xref(fz_context *ctx, pdf_annot *annot)
  3201. {
  3202. pdf_document *doc;
  3203. if (!annot->page)
  3204. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
  3205. doc = annot->page->doc;
  3206. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3207. if (doc->local_xref_nesting == 0 && doc->local_xref)
  3208. fz_write_printf(ctx, fz_stddbg(ctx), "push local_xref for annot\n");
  3209. #endif
  3210. doc->local_xref_nesting++;
  3211. }
  3212. void
  3213. pdf_annot_ensure_local_xref(fz_context *ctx, pdf_annot *annot)
  3214. {
  3215. pdf_document *doc = annot->page->doc;
  3216. if (doc->local_xref != NULL)
  3217. return;
  3218. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3219. fz_write_printf(ctx, fz_stddbg(ctx), "creating local_xref\n");
  3220. #endif
  3221. /* We have no local_xref, but we want to be using one. */
  3222. /* First off, create one. */
  3223. doc->local_xref = pdf_new_local_xref(ctx, doc);
  3224. }
  3225. void
  3226. pdf_annot_pop_local_xref(fz_context *ctx, pdf_annot *annot)
  3227. {
  3228. pdf_document *doc = annot->page->doc;
  3229. --doc->local_xref_nesting;
  3230. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3231. if (doc->local_xref_nesting == 0 && doc->local_xref)
  3232. fz_write_printf(ctx, fz_stddbg(ctx), "pop local_xref for annot\n");
  3233. #endif
  3234. }
  3235. void pdf_annot_pop_and_discard_local_xref(fz_context *ctx, pdf_annot *annot)
  3236. {
  3237. pdf_document *doc = annot->page->doc;
  3238. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3239. if (doc->local_xref)
  3240. fz_write_printf(ctx, fz_stddbg(ctx), "pop and discard local_xref for annot\n");
  3241. #endif
  3242. --doc->local_xref_nesting;
  3243. assert(doc->local_xref_nesting == 0);
  3244. pdf_drop_local_xref_and_resources(ctx, doc);
  3245. }
  3246. static void pdf_update_appearance(fz_context *ctx, pdf_annot *annot)
  3247. {
  3248. pdf_obj *subtype;
  3249. pdf_obj *ft = NULL;
  3250. pdf_obj *ap_n;
  3251. int pop_local_xref = 1;
  3252. retry_after_repair:
  3253. /* Must have any local xref in place in order to check if it's dirty. */
  3254. pdf_annot_push_local_xref(ctx, annot);
  3255. pdf_begin_implicit_operation(ctx, annot->page->doc);
  3256. fz_start_throw_on_repair(ctx);
  3257. fz_var(pop_local_xref);
  3258. fz_try(ctx)
  3259. {
  3260. int needs_resynth;
  3261. int local_synthesis = 0;
  3262. /* Never update Popup and Link annotations */
  3263. subtype = pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype));
  3264. if (subtype == PDF_NAME(Popup) || subtype == PDF_NAME(Link))
  3265. {
  3266. pdf_end_operation(ctx, annot->page->doc);
  3267. break;
  3268. }
  3269. /* Never update signed Signature widgets */
  3270. if (subtype == PDF_NAME(Widget))
  3271. {
  3272. ft = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(FT));
  3273. if (ft == PDF_NAME(Sig))
  3274. {
  3275. /* We cannot synthesise an appearance for a signed Sig, so don't even try. */
  3276. if (pdf_signature_is_signed(ctx, annot->page->doc, annot->obj))
  3277. {
  3278. pdf_end_operation(ctx, annot->page->doc);
  3279. break;
  3280. }
  3281. }
  3282. }
  3283. /* Check if the field is dirtied by JS events */
  3284. if (pdf_obj_is_dirty(ctx, annot->obj))
  3285. pdf_annot_request_resynthesis(ctx, annot);
  3286. /* Find the current appearance stream, if one exists. */
  3287. ap_n = pdf_annot_ap(ctx, annot);
  3288. /* If there is no appearance stream, we need to create a local one for display purposes. */
  3289. if (!ap_n)
  3290. local_synthesis = 1;
  3291. /* Ignore appearance streams not created by us (so not local)
  3292. * for unsigned digital signature widgets. They are often blank
  3293. * and we want the "sign" arrow to be visible. Never write back
  3294. * the forced appearance stream for unsigned signatures. */
  3295. if (subtype == PDF_NAME(Widget) && ft == PDF_NAME(Sig))
  3296. {
  3297. if (ap_n && !pdf_is_local_object(ctx, annot->page->doc, ap_n))
  3298. local_synthesis = 1;
  3299. }
  3300. /* We need to put this appearance stream back into the document. */
  3301. needs_resynth = pdf_annot_needs_resynthesis(ctx, annot);
  3302. if (needs_resynth)
  3303. local_synthesis = 0;
  3304. /* Some appearances can NEVER be resynthesised. Spot those here. */
  3305. if (needs_resynth)
  3306. {
  3307. if (pdf_name_eq(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME(Subtype)), PDF_NAME(Stamp)))
  3308. {
  3309. /* Don't resynthesize Stamps with non-standard names if
  3310. * they already have an appearance. These usually have
  3311. * custom images already set for their appearance.
  3312. */
  3313. if (!pdf_annot_is_standard_stamp(ctx, annot) && ap_n)
  3314. {
  3315. /* However, we allow changing the Rect even if we don't
  3316. * resynthesize the appearance. This should also count
  3317. * as having a changed appearance. */
  3318. pdf_set_annot_resynthesised(ctx, annot);
  3319. needs_resynth = 0;
  3320. }
  3321. }
  3322. }
  3323. if (local_synthesis || needs_resynth)
  3324. {
  3325. fz_display_list *dlist;
  3326. fz_rect rect, bbox;
  3327. fz_buffer *buf;
  3328. pdf_obj *res = NULL;
  3329. pdf_obj *new_ap_n = NULL;
  3330. fz_var(res);
  3331. fz_var(new_ap_n);
  3332. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3333. fz_write_printf(ctx, fz_stddbg(ctx), "Update Appearance: %d\n", pdf_to_num(ctx, annot->obj));
  3334. pdf_debug_obj(ctx, annot->obj);
  3335. fz_write_printf(ctx, fz_stddbg(ctx), "\n");
  3336. #endif
  3337. if (local_synthesis)
  3338. {
  3339. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3340. fz_write_printf(ctx, fz_stddbg(ctx), "Local synthesis\n");
  3341. #endif
  3342. pdf_annot_ensure_local_xref(ctx, annot);
  3343. }
  3344. else
  3345. {
  3346. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3347. fz_write_printf(ctx, fz_stddbg(ctx), "Non-Local synthesis\n");
  3348. #endif
  3349. /* We don't want to be using any local xref, so
  3350. * bin any that we have. */
  3351. pdf_annot_pop_and_discard_local_xref(ctx, annot);
  3352. /* Binning the xref may leave ap_n holding an invalid pointer.
  3353. * We don't use it from this point onwards anymore, but beware
  3354. * in future code changes. */
  3355. pop_local_xref = 0;
  3356. }
  3357. if (subtype == PDF_NAME(Widget) && ft == PDF_NAME(Btn))
  3358. {
  3359. /* Special case for Btn widgets that need multiple appearance streams. */
  3360. pdf_update_button_appearance(ctx, annot);
  3361. }
  3362. else if (subtype == PDF_NAME(Widget) && ft == PDF_NAME(Sig))
  3363. {
  3364. /* Special case for unsigned signature widgets,
  3365. * which are most easily created via a display list. */
  3366. rect = pdf_annot_rect(ctx, annot);
  3367. dlist = pdf_signature_appearance_unsigned(ctx, rect, pdf_annot_language(ctx, annot));
  3368. fz_try(ctx)
  3369. pdf_set_annot_appearance_from_display_list(ctx, annot, "N", NULL, fz_identity, dlist);
  3370. fz_always(ctx)
  3371. fz_drop_display_list(ctx, dlist);
  3372. fz_catch(ctx)
  3373. fz_rethrow(ctx);
  3374. }
  3375. else
  3376. {
  3377. buf = fz_new_buffer(ctx, 1024);
  3378. fz_try(ctx)
  3379. {
  3380. fz_matrix matrix = fz_identity;
  3381. rect = pdf_dict_get_rect(ctx, annot->obj, PDF_NAME(Rect));
  3382. pdf_write_appearance(ctx, annot, buf, &rect, &bbox, &matrix, &res);
  3383. pdf_dict_put_rect(ctx, annot->obj, PDF_NAME(Rect), rect);
  3384. pdf_set_annot_appearance(ctx, annot, "N", NULL, matrix, bbox, res, buf);
  3385. }
  3386. fz_always(ctx)
  3387. {
  3388. fz_drop_buffer(ctx, buf);
  3389. pdf_drop_obj(ctx, res);
  3390. pdf_drop_obj(ctx, new_ap_n);
  3391. }
  3392. fz_catch(ctx)
  3393. {
  3394. fz_rethrow_if(ctx, FZ_ERROR_REPAIRED);
  3395. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  3396. fz_report_error(ctx);
  3397. fz_warn(ctx, "cannot create appearance stream");
  3398. }
  3399. }
  3400. #ifdef PDF_DEBUG_APPEARANCE_SYNTHESIS
  3401. fz_write_printf(ctx, fz_stddbg(ctx), "Annot obj after synthesis\n");
  3402. pdf_debug_obj(ctx, annot->obj);
  3403. fz_write_printf(ctx, fz_stddbg(ctx), "\nAppearance after synthesis\n");
  3404. pdf_debug_obj(ctx, pdf_dict_getp(ctx, annot->obj, "AP/N"));
  3405. fz_write_printf(ctx, fz_stddbg(ctx), "\n");
  3406. #endif
  3407. }
  3408. pdf_clean_obj(ctx, annot->obj);
  3409. pdf_end_operation(ctx, annot->page->doc);
  3410. }
  3411. fz_always(ctx)
  3412. {
  3413. if (pop_local_xref)
  3414. pdf_annot_pop_local_xref(ctx, annot);
  3415. fz_end_throw_on_repair(ctx);
  3416. }
  3417. fz_catch(ctx)
  3418. {
  3419. pdf_abandon_operation(ctx, annot->page->doc);
  3420. /* If we hit a repair while synthesising, we need to give it another
  3421. * go. Do that directly here, rather than waiting for the next time
  3422. * we are called, because we don't want to risk discarding any
  3423. * local_xrefs on the second pass through the list of annotations.
  3424. * Repairs only ever happen once for a document, so no infinite
  3425. * loop potential here. */
  3426. if (fz_caught(ctx) == FZ_ERROR_REPAIRED)
  3427. {
  3428. fz_report_error(ctx);
  3429. goto retry_after_repair;
  3430. }
  3431. fz_rethrow(ctx);
  3432. }
  3433. }
  3434. static void *
  3435. update_appearances(fz_context *ctx, fz_page *page_, void *state)
  3436. {
  3437. pdf_page *page = (pdf_page *)page_;
  3438. pdf_annot *annot;
  3439. for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
  3440. pdf_update_appearance(ctx, annot);
  3441. for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
  3442. pdf_update_appearance(ctx, annot);
  3443. return NULL;
  3444. }
  3445. static void
  3446. update_all_appearances(fz_context *ctx, pdf_page *page)
  3447. {
  3448. pdf_document *doc = page->doc;
  3449. /* Update all the annotations on all the pages open in the document.
  3450. * At least one annotation should be resynthesised because we'll
  3451. * only reach here if resynth_required was set. Any such resynthesis
  3452. * that changes the document will throw away any local_xref. */
  3453. fz_process_opened_pages(ctx, &doc->super, update_appearances, NULL);
  3454. /* If the page isn't linked in yet (as is the case whilst loading
  3455. * the annots for a page), process that too. */
  3456. if (page->super.prev == NULL && page->super.next == NULL)
  3457. update_appearances(ctx, &page->super, NULL);
  3458. /* Run it a second time, so that any annotations whose synthesised
  3459. * appearances live in the local_xref (such as unsigned sigs) can
  3460. * be regenerated too. Running this for annots which are up to date
  3461. * should be fast. */
  3462. fz_process_opened_pages(ctx, &doc->super, update_appearances, NULL);
  3463. /* And cope with a non-linked in page again. */
  3464. if (page->super.prev == NULL && page->super.next == NULL)
  3465. update_appearances(ctx, &page->super, NULL);
  3466. doc->resynth_required = 0;
  3467. }
  3468. int
  3469. pdf_update_annot(fz_context *ctx, pdf_annot *annot)
  3470. {
  3471. int changed;
  3472. if (!annot->page)
  3473. fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
  3474. if (annot->page->doc->resynth_required)
  3475. update_all_appearances(ctx, annot->page);
  3476. changed = annot->has_new_ap;
  3477. annot->has_new_ap = 0;
  3478. return changed;
  3479. }