gl-main.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281
  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 "gl-app.h"
  23. #include <limits.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <time.h>
  28. #ifndef _WIN32
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <signal.h>
  32. #endif
  33. #include "mupdf/helpers/pkcs7-openssl.h"
  34. #if FZ_ENABLE_JS
  35. #include "mujs.h"
  36. #endif
  37. #ifndef _WIN32
  38. #include <sys/stat.h> /* for mkdir */
  39. #include <unistd.h> /* for getcwd */
  40. #include <spawn.h> /* for posix_spawn */
  41. extern char **environ; /* see environ (7) */
  42. #else
  43. #include <direct.h> /* for getcwd */
  44. #endif
  45. #ifdef __APPLE__
  46. static void cleanup(void);
  47. void glutLeaveMainLoop(void)
  48. {
  49. cleanup();
  50. exit(0);
  51. }
  52. #endif
  53. fz_context *ctx = NULL;
  54. fz_colorspace *profile = NULL;
  55. pdf_document *pdf = NULL;
  56. pdf_page *page = NULL;
  57. fz_stext_page *page_text = NULL;
  58. fz_matrix draw_page_ctm, view_page_ctm, view_page_inv_ctm;
  59. fz_rect page_bounds, draw_page_bounds, view_page_bounds;
  60. fz_irect view_page_area;
  61. char filename[PATH_MAX];
  62. enum
  63. {
  64. /* Screen furniture: aggregate size of unusable space from title bars, task bars, window borders, etc */
  65. SCREEN_FURNITURE_W = 20,
  66. SCREEN_FURNITURE_H = 40,
  67. };
  68. static void open_browser(const char *uri)
  69. {
  70. #ifndef _WIN32
  71. char *argv[3];
  72. #endif
  73. char buf[PATH_MAX];
  74. #ifndef _WIN32
  75. pid_t pid;
  76. int err;
  77. #endif
  78. /* Relative file: URI, make it absolute! */
  79. if (!strncmp(uri, "file:", 5) && uri[5] != '/')
  80. {
  81. char buf_base[PATH_MAX];
  82. char buf_cwd[PATH_MAX];
  83. fz_dirname(buf_base, filename, sizeof buf_base);
  84. if (getcwd(buf_cwd, sizeof buf_cwd))
  85. {
  86. fz_snprintf(buf, sizeof buf, "file://%s/%s/%s", buf_cwd, buf_base, uri+5);
  87. fz_cleanname(buf+7);
  88. uri = buf;
  89. }
  90. }
  91. if (strncmp(uri, "file://", 7) && strncmp(uri, "http://", 7) && strncmp(uri, "https://", 8) && strncmp(uri, "mailto:", 7))
  92. {
  93. fz_warn(ctx, "refusing to open unknown link (%s)", uri);
  94. return;
  95. }
  96. #ifdef _WIN32
  97. ShellExecuteA(NULL, "open", uri, 0, 0, SW_SHOWNORMAL);
  98. #else
  99. const char *browser = getenv("BROWSER");
  100. if (!browser)
  101. {
  102. #ifdef __APPLE__
  103. browser = "open";
  104. #else
  105. browser = "xdg-open";
  106. #endif
  107. }
  108. argv[0] = (char*) browser;
  109. argv[1] = (char*) uri;
  110. argv[2] = NULL;
  111. err = posix_spawnp(&pid, browser, NULL, NULL, argv, environ);
  112. if (err)
  113. fz_warn(ctx, "cannot spawn browser '%s': %s", browser, strerror(err));
  114. #endif
  115. }
  116. static const int zoom_list[] = {
  117. 6, 12, 24, 36, 48, 60, 72, 84, 96, 108,
  118. 120, 144, 168, 192, 228, 264,
  119. 300, 350, 400, 450, 500, 550, 600
  120. };
  121. static int zoom_in(int oldres)
  122. {
  123. int i;
  124. for (i = 0; i < (int)nelem(zoom_list) - 1; ++i)
  125. if (zoom_list[i] <= oldres && zoom_list[i+1] > oldres)
  126. return zoom_list[i+1];
  127. return zoom_list[i];
  128. }
  129. static int zoom_out(int oldres)
  130. {
  131. int i;
  132. for (i = 0; i < (int)nelem(zoom_list) - 1; ++i)
  133. if (zoom_list[i] < oldres && zoom_list[i+1] >= oldres)
  134. return zoom_list[i];
  135. return zoom_list[0];
  136. }
  137. static const char *paper_size_name(int w, int h)
  138. {
  139. /* ISO A */
  140. if (w == 2384 && h == 3370) return "A0";
  141. if (w == 1684 && h == 2384) return "A1";
  142. if (w == 1191 && h == 1684) return "A2";
  143. if (w == 842 && h == 1191) return "A3";
  144. if (w == 595 && h == 842) return "A4";
  145. if (w == 420 && h == 595) return "A5";
  146. if (w == 297 && h == 420) return "A6";
  147. /* US */
  148. if (w == 612 && h == 792) return "Letter";
  149. if (w == 612 && h == 1008) return "Legal";
  150. if (w == 792 && h == 1224) return "Ledger";
  151. if (w == 1224 && h == 792) return "Tabloid";
  152. return NULL;
  153. }
  154. #define MINRES (zoom_list[0])
  155. #define MAXRES (zoom_list[nelem(zoom_list)-1])
  156. #define DEFRES 96
  157. static char *password = "";
  158. static char *anchor = NULL;
  159. static float layout_w = FZ_DEFAULT_LAYOUT_W;
  160. static float layout_h = FZ_DEFAULT_LAYOUT_H;
  161. static float layout_em = FZ_DEFAULT_LAYOUT_EM;
  162. static char *layout_css = NULL;
  163. static int layout_use_doc_css = 1;
  164. static int enable_js = 1;
  165. static int tint_white = 0xFFFFF0;
  166. static int tint_black = 0x303030;
  167. static fz_document *doc = NULL;
  168. static fz_page *fzpage = NULL;
  169. static fz_separations *seps = NULL;
  170. static fz_outline *outline = NULL;
  171. static fz_link *links = NULL;
  172. static int number = 0;
  173. static fz_pixmap *page_contents = NULL;
  174. static struct texture page_tex = { 0 };
  175. static int screen_w = 0, screen_h = 0;
  176. static int scroll_x = 0, scroll_y = 0;
  177. static int canvas_x = 0, canvas_w = 100;
  178. static int canvas_y = 0, canvas_h = 100;
  179. static int outline_w = 14; /* to be scaled by lineheight */
  180. static int annotate_w = 12; /* to be scaled by lineheight */
  181. static int outline_start_x = 0;
  182. static int oldbox = FZ_CROP_BOX, currentbox = FZ_CROP_BOX;
  183. static int oldtint = 0, currenttint = 0;
  184. static int oldinvert = 0, currentinvert = 0;
  185. static int oldicc = 1, currenticc = 1;
  186. static int oldaa = 8, currentaa = 8;
  187. static int oldseparations = 1, currentseparations = 1;
  188. static fz_location oldpage = {0,0}, currentpage = {0,0};
  189. static float oldzoom = DEFRES, currentzoom = DEFRES;
  190. static float oldrotate = 0, currentrotate = 0;
  191. int page_contents_changed = 0;
  192. int page_annots_changed = 0;
  193. static fz_output *trace_file = NULL;
  194. static char *reflow_options = NULL;
  195. static int isfullscreen = 0;
  196. static int showoutline = 0;
  197. static int showundo = 0;
  198. static int showlayers = 0;
  199. static int showlinks = 0;
  200. static int showsearch = 0;
  201. int showannotate = 0;
  202. int showform = 0;
  203. #if FZ_ENABLE_JS
  204. static int showconsole = 0;
  205. static int console_h = 14; /* to be scaled by lineheight */
  206. static pdf_js_console gl_js_console;
  207. static int console_start_y = 0;
  208. #endif
  209. static const char *tooltip = NULL;
  210. struct mark
  211. {
  212. fz_location loc;
  213. fz_point scroll;
  214. };
  215. static int history_count = 0;
  216. static struct mark history[256];
  217. static int future_count = 0;
  218. static struct mark future[256];
  219. static struct mark marks[10];
  220. static char *get_history_filename(void)
  221. {
  222. static char history_path[PATH_MAX];
  223. static int once = 0;
  224. if (!once)
  225. {
  226. char *home = getenv("MUPDF_HISTORY");
  227. if (home)
  228. return home;
  229. home = getenv("XDG_CACHE_HOME");
  230. if (!home)
  231. home = getenv("HOME");
  232. if (!home)
  233. home = getenv("USERPROFILE");
  234. if (!home)
  235. home = "/tmp";
  236. fz_snprintf(history_path, sizeof history_path, "%s/.mupdf.history", home);
  237. fz_cleanname(history_path);
  238. once = 1;
  239. }
  240. return history_path;
  241. }
  242. static fz_json *read_history_file_as_json(fz_pool *pool)
  243. {
  244. fz_buffer *buf = NULL;
  245. const char *json = "{}";
  246. const char *history_file;
  247. fz_json *result = NULL;
  248. fz_var(buf);
  249. history_file = get_history_filename();
  250. if (strlen(history_file) == 0)
  251. return NULL;
  252. if (fz_file_exists(ctx, history_file))
  253. {
  254. fz_try(ctx)
  255. {
  256. buf = fz_read_file(ctx, history_file);
  257. json = fz_string_from_buffer(ctx, buf);
  258. }
  259. fz_catch(ctx)
  260. ;
  261. }
  262. fz_try(ctx)
  263. {
  264. result = fz_parse_json(ctx, pool, json);
  265. }
  266. fz_catch(ctx)
  267. {
  268. fz_report_error(ctx);
  269. fz_warn(ctx, "can't parse history file");
  270. result = NULL;
  271. }
  272. fz_drop_buffer(ctx, buf);
  273. if (result == NULL || result->type != FZ_JSON_OBJECT)
  274. result = fz_json_new_object(ctx, pool);
  275. return result;
  276. }
  277. static fz_location load_location(fz_json *val)
  278. {
  279. if (fz_json_is_number(ctx, val))
  280. return fz_make_location(0, fz_json_to_number(ctx, val) - 1);
  281. if (fz_json_is_array(ctx, val))
  282. return fz_make_location(
  283. fz_json_to_number(ctx, fz_json_array_get(ctx, val, 0)) - 1,
  284. fz_json_to_number(ctx, fz_json_array_get(ctx, val, 1)) - 1
  285. );
  286. return fz_make_location(0, 0);
  287. }
  288. static fz_json *save_location(fz_pool *pool, fz_location loc)
  289. {
  290. fz_json *arr;
  291. if (loc.chapter == 0)
  292. {
  293. return fz_json_new_number(ctx, pool, loc.page + 1);
  294. }
  295. else
  296. {
  297. arr = fz_json_new_array(ctx, pool);
  298. fz_json_array_push(ctx, pool, arr, fz_json_new_number(ctx, pool, loc.chapter + 1));
  299. fz_json_array_push(ctx, pool, arr, fz_json_new_number(ctx, pool, loc.page + 1));
  300. return arr;
  301. }
  302. }
  303. static void load_history(void)
  304. {
  305. char absname[PATH_MAX];
  306. fz_pool *pool = NULL;
  307. fz_json *json, *item, *arr, *val;
  308. int i, n;
  309. fz_var(pool);
  310. if (!fz_realpath(filename, absname))
  311. return;
  312. fz_try(ctx)
  313. {
  314. pool = fz_new_pool(ctx);
  315. json = read_history_file_as_json(pool);
  316. if (json)
  317. {
  318. item = fz_json_object_get(ctx, json, absname);
  319. if (item)
  320. {
  321. val = fz_json_object_get(ctx, item, "current");
  322. if (val)
  323. currentpage = load_location(val);
  324. arr = fz_json_object_get(ctx, item, "history");
  325. if (fz_json_is_array(ctx, arr))
  326. {
  327. history_count = fz_clampi(fz_json_array_length(ctx, arr), 0, nelem(history));
  328. for (i = 0; i < history_count; ++i)
  329. history[i].loc = load_location(fz_json_array_get(ctx, arr, i));
  330. }
  331. arr = fz_json_object_get(ctx, item, "future");
  332. if (fz_json_is_array(ctx, arr))
  333. {
  334. future_count = fz_clampi(fz_json_array_length(ctx, arr), 0, nelem(future));
  335. for (i = 0; i < future_count; ++i)
  336. future[i].loc = load_location(fz_json_array_get(ctx, arr, i));
  337. }
  338. arr = fz_json_object_get(ctx, item, "marks");
  339. if (fz_json_is_array(ctx, arr))
  340. {
  341. n = fz_clampi(fz_json_array_length(ctx, arr), 0, nelem(marks));
  342. for (i = 0; i < n; ++i)
  343. marks[i].loc = load_location(fz_json_array_get(ctx, arr, i));
  344. }
  345. }
  346. }
  347. }
  348. fz_always(ctx)
  349. {
  350. fz_drop_pool(ctx, pool);
  351. }
  352. fz_catch(ctx)
  353. {
  354. fz_report_error(ctx);
  355. fz_warn(ctx, "Can't read history file.");
  356. }
  357. }
  358. static void save_history(void)
  359. {
  360. fz_pool *pool;
  361. char absname[PATH_MAX];
  362. fz_output *out = NULL;
  363. fz_json *json, *item, *arr;
  364. const char *history_file;
  365. int i;
  366. fz_var(pool);
  367. fz_var(out);
  368. if (!doc)
  369. return;
  370. if (!fz_realpath(filename, absname))
  371. return;
  372. fz_try(ctx)
  373. {
  374. pool = fz_new_pool(ctx);
  375. json = read_history_file_as_json(pool);
  376. if (json)
  377. {
  378. item = fz_json_new_object(ctx, pool);
  379. fz_json_object_set(ctx, pool, item, "current", save_location(pool, currentpage));
  380. arr = fz_json_new_array(ctx, pool);
  381. for (i = 0; i < history_count; ++i)
  382. fz_json_array_push(ctx, pool, arr, save_location(pool, history[i].loc));
  383. fz_json_object_set(ctx, pool, item, "history", arr);
  384. arr = fz_json_new_array(ctx, pool);
  385. for (i = 0; i < future_count; ++i)
  386. fz_json_array_push(ctx, pool, arr, save_location(pool, future[i].loc));
  387. fz_json_object_set(ctx, pool, item, "future", arr);
  388. arr = fz_json_new_array(ctx, pool);
  389. for (i = 0; i < (int)nelem(marks); ++i)
  390. fz_json_array_push(ctx, pool, arr, save_location(pool, marks[i].loc));
  391. fz_json_object_set(ctx, pool, item, "marks", arr);
  392. fz_json_object_set(ctx, pool, json, absname, item);
  393. history_file = get_history_filename();
  394. if (strlen(history_file) > 0) {
  395. out = fz_new_output_with_path(ctx, history_file, 0);
  396. fz_write_json(ctx, out, json);
  397. fz_write_byte(ctx, out, '\n');
  398. fz_close_output(ctx, out);
  399. }
  400. }
  401. }
  402. fz_always(ctx)
  403. {
  404. fz_drop_pool(ctx, pool);
  405. fz_drop_output(ctx, out);
  406. }
  407. fz_catch(ctx)
  408. {
  409. fz_report_error(ctx);
  410. fz_warn(ctx, "Can't write history file.");
  411. }
  412. }
  413. static int create_accel_path(char outname[], size_t len, int create, const char *absname, ...)
  414. {
  415. va_list args;
  416. char *s = outname;
  417. size_t z, remain = len;
  418. char *arg;
  419. va_start(args, absname);
  420. while ((arg = va_arg(args, char *)) != NULL)
  421. {
  422. z = fz_snprintf(s, remain, "%s", arg);
  423. if (z+1 > remain)
  424. goto fail; /* won't fit */
  425. if (create)
  426. (void) fz_mkdir(outname);
  427. if (!fz_is_directory(ctx, outname))
  428. goto fail; /* directory creation failed, or that dir doesn't exist! */
  429. #ifdef _WIN32
  430. s[z] = '\\';
  431. #else
  432. s[z] = '/';
  433. #endif
  434. s[z+1] = 0;
  435. s += z+1;
  436. remain -= z+1;
  437. }
  438. if (fz_snprintf(s, remain, "%s.accel", absname) >= remain)
  439. goto fail; /* won't fit */
  440. va_end(args);
  441. return 1;
  442. fail:
  443. va_end(args);
  444. return 0;
  445. }
  446. static int convert_to_accel_path(char outname[], char *absname, size_t len, int create)
  447. {
  448. char *tmpdir;
  449. char *s;
  450. if (absname[0] == '/' || absname[0] == '\\')
  451. ++absname;
  452. s = absname;
  453. while (*s) {
  454. if (*s == '/' || *s == '\\' || *s == ':')
  455. *s = '%';
  456. ++s;
  457. }
  458. #ifdef _WIN32
  459. tmpdir = getenv("USERPROFILE");
  460. if (tmpdir && create_accel_path(outname, len, create, absname, tmpdir, ".config", "mupdf", NULL))
  461. return 1; /* OK! */
  462. /* TEMP and TMP are user-specific on modern windows. */
  463. tmpdir = getenv("TEMP");
  464. if (tmpdir && create_accel_path(outname, len, create, absname, tmpdir, "mupdf", NULL))
  465. return 1; /* OK! */
  466. tmpdir = getenv("TMP");
  467. if (tmpdir && create_accel_path(outname, len, create, absname, tmpdir, "mupdf", NULL))
  468. return 1; /* OK! */
  469. #else
  470. tmpdir = getenv("XDG_CACHE_HOME");
  471. if (tmpdir && create_accel_path(outname, len, create, absname, tmpdir, "mupdf", NULL))
  472. return 1; /* OK! */
  473. tmpdir = getenv("HOME");
  474. if (tmpdir && create_accel_path(outname, len, create, absname, tmpdir, ".cache", "mupdf", NULL))
  475. return 1; /* OK! */
  476. #endif
  477. return 0; /* Fail */
  478. }
  479. static int get_accelerator_filename(char outname[], size_t len, int create)
  480. {
  481. char absname[PATH_MAX];
  482. if (!fz_realpath(filename, absname))
  483. return 0;
  484. if (!convert_to_accel_path(outname, absname, len, create))
  485. return 0;
  486. return 1;
  487. }
  488. static void save_accelerator(void)
  489. {
  490. char absname[PATH_MAX];
  491. if (!doc)
  492. return;
  493. if (!fz_document_supports_accelerator(ctx, doc))
  494. return;
  495. if (!get_accelerator_filename(absname, sizeof(absname), 1))
  496. return;
  497. fz_save_accelerator(ctx, doc, absname);
  498. }
  499. static struct input search_input = { { 0 }, 0 };
  500. static int search_dir = 1;
  501. static fz_location search_page = {-1, -1};
  502. static fz_location search_hit_page = {-1, -1};
  503. static int search_active = 0;
  504. char *search_needle = 0;
  505. int search_hit_count = 0;
  506. fz_quad search_hit_quads[5000];
  507. static char *help_dialog_text =
  508. "The middle mouse button (scroll wheel button) pans the document view. "
  509. "The right mouse button selects a region and copies the marked text to the clipboard."
  510. "\n"
  511. "\n"
  512. "F1 - show this message\n"
  513. "` F12 - show javascript console\n"
  514. "i - show document information\n"
  515. "o - show document outline\n"
  516. "u - show undo history\n"
  517. "Y - show layer list\n"
  518. "a - show annotation editor\n"
  519. "R - show redaction editor\n"
  520. "L - highlight links\n"
  521. "F - highlight form fields\n"
  522. "r - reload file\n"
  523. "S - save file (only for PDF)\n"
  524. "q - quit\n"
  525. "\n"
  526. "< - decrease E-book font size\n"
  527. "> - increase E-book font size\n"
  528. "B - cycle between MediaBox, CropBox, ArtBox, etc.\n"
  529. "A - toggle anti-aliasing\n"
  530. "I - toggle inverted color mode\n"
  531. "C - toggle tinted color mode\n"
  532. "E - toggle ICC color management\n"
  533. "e - toggle spot color emulation\n"
  534. "\n"
  535. "f - fullscreen window\n"
  536. "w - shrink wrap window\n"
  537. "W - fit to width\n"
  538. "H - fit to height\n"
  539. "Z - fit to page\n"
  540. "z - reset zoom\n"
  541. "[number] z - set zoom resolution in DPI\n"
  542. "plus - zoom in\n"
  543. "minus - zoom out\n"
  544. "[ - rotate counter-clockwise\n"
  545. "] - rotate clockwise\n"
  546. "arrow keys - scroll in small increments\n"
  547. "h, j, k, l - scroll in small increments\n"
  548. "\n"
  549. "b - smart move backward\n"
  550. "space - smart move forward\n"
  551. "comma or page up - go backward\n"
  552. "period or page down - go forward\n"
  553. "g - go to first page\n"
  554. "G - go to last page\n"
  555. "[number] g - go to page number\n"
  556. "\n"
  557. "m - save current location in history\n"
  558. "t - go backward in history\n"
  559. "T - go forward in history\n"
  560. "[number] m - save current location in numbered bookmark\n"
  561. "[number] t - go to numbered bookmark\n"
  562. "\n"
  563. "/ - search for text forward\n"
  564. "? - search for text backward\n"
  565. "n - repeat search\n"
  566. "N - repeat search in reverse direction"
  567. ;
  568. static void help_dialog(void)
  569. {
  570. static int scroll;
  571. ui_dialog_begin(ui.gridsize*20, ui.gridsize*40);
  572. ui_layout(T, X, W, ui.padsize, ui.padsize);
  573. ui_label("MuPDF %s", FZ_VERSION);
  574. ui_spacer();
  575. ui_layout(B, NONE, S, ui.padsize, ui.padsize);
  576. if (ui_button("Okay") || ui.key == KEY_ENTER || ui.key == KEY_ESCAPE)
  577. ui.dialog = NULL;
  578. ui_spacer();
  579. ui_layout(ALL, BOTH, CENTER, ui.padsize, ui.padsize);
  580. ui_label_with_scrollbar(help_dialog_text, 0, 0, &scroll, NULL);
  581. ui_dialog_end();
  582. }
  583. static fz_buffer *format_info_text();
  584. static void info_dialog(void)
  585. {
  586. static int scroll;
  587. fz_buffer *info_text;
  588. ui_dialog_begin(ui.gridsize*20, ui.gridsize*20);
  589. ui_layout(B, NONE, S, ui.padsize, ui.padsize);
  590. if (ui_button("Okay") || ui.key == KEY_ENTER || ui.key == KEY_ESCAPE)
  591. ui.dialog = NULL;
  592. ui_spacer();
  593. ui_layout(ALL, BOTH, CENTER, ui.padsize, ui.padsize);
  594. info_text = format_info_text();
  595. ui_label_with_scrollbar((char*)fz_string_from_buffer(ctx, info_text), 0, 0, &scroll, NULL);
  596. fz_drop_buffer(ctx, info_text);
  597. ui_dialog_end();
  598. }
  599. static char error_message[256];
  600. static void error_dialog(void)
  601. {
  602. ui_dialog_begin(ui.gridsize*20, (ui.gridsize+ui.padsize*2)*4);
  603. ui_layout(T, NONE, NW, ui.padsize, ui.padsize);
  604. ui_label("%C %s", 0x1f4a3, error_message); /* BOMB */
  605. ui_layout(B, NONE, S, ui.padsize, ui.padsize);
  606. if (ui_button("Quit") || ui.key == KEY_ENTER || ui.key == KEY_ESCAPE || ui.key == 'q')
  607. glutLeaveMainLoop();
  608. ui_dialog_end();
  609. }
  610. void ui_show_error_dialog(const char *fmt, ...)
  611. {
  612. va_list ap;
  613. va_start(ap, fmt);
  614. fz_vsnprintf(error_message, sizeof error_message, fmt, ap);
  615. va_end(ap);
  616. ui.dialog = error_dialog;
  617. }
  618. static char warning_message[256];
  619. static void warning_dialog(void)
  620. {
  621. ui_dialog_begin(ui.gridsize*20, (ui.gridsize+ui.padsize*2)*4);
  622. ui_layout(T, NONE, NW, ui.padsize, ui.padsize);
  623. ui_label("%C %s", 0x26a0, warning_message); /* WARNING SIGN */
  624. ui_layout(B, NONE, S, ui.padsize, ui.padsize);
  625. if (ui_button("Okay") || ui.key == KEY_ENTER || ui.key == KEY_ESCAPE)
  626. ui.dialog = NULL;
  627. ui_dialog_end();
  628. }
  629. void ui_show_warning_dialog(const char *fmt, ...)
  630. {
  631. va_list ap;
  632. va_start(ap, fmt);
  633. fz_vsnprintf(warning_message, sizeof warning_message, fmt, ap);
  634. va_end(ap);
  635. ui.dialog = warning_dialog;
  636. }
  637. static void quit_dialog(void)
  638. {
  639. ui_dialog_begin(ui.gridsize*20, (ui.gridsize+ui.padsize*2)*3);
  640. ui_layout(T, NONE, NW, ui.padsize, ui.padsize);
  641. ui_label("%C The document has unsaved changes. Are you sure you want to quit?", 0x26a0); /* WARNING SIGN */
  642. ui_layout(B, X, S, ui.padsize, ui.padsize);
  643. ui_panel_begin(0, ui.gridsize, 0, 0, 0);
  644. {
  645. ui_layout(R, NONE, S, 0, 0);
  646. if (ui_button("Save"))
  647. do_save_pdf_file();
  648. ui_spacer();
  649. if (ui_button("Discard") || ui.key == 'q')
  650. glutLeaveMainLoop();
  651. ui_layout(L, NONE, S, 0, 0);
  652. if (ui_button("Cancel") || ui.key == KEY_ESCAPE)
  653. ui.dialog = NULL;
  654. }
  655. ui_panel_end();
  656. ui_dialog_end();
  657. }
  658. static void quit(void)
  659. {
  660. if (pdf && pdf_has_unsaved_changes(ctx, pdf))
  661. ui.dialog = quit_dialog;
  662. else
  663. glutLeaveMainLoop();
  664. }
  665. static void reload_dialog(void)
  666. {
  667. ui_dialog_begin(ui.gridsize*20, (ui.gridsize+ui.padsize*2)*3);
  668. ui_layout(T, NONE, NW, ui.padsize, ui.padsize);
  669. ui_label("%C The document has unsaved changes. Are you sure you want to reload?", 0x26a0); /* WARNING SIGN */
  670. ui_layout(B, X, S, ui.padsize, ui.padsize);
  671. ui_panel_begin(0, ui.gridsize, 0, 0, 0);
  672. {
  673. ui_layout(R, NONE, S, 0, 0);
  674. if (ui_button("Save"))
  675. do_save_pdf_file();
  676. ui_spacer();
  677. if (ui_button("Reload") || ui.key == 'q')
  678. {
  679. ui.dialog = NULL;
  680. reload_document();
  681. }
  682. ui_layout(L, NONE, S, 0, 0);
  683. if (ui_button("Cancel") || ui.key == KEY_ESCAPE)
  684. ui.dialog = NULL;
  685. }
  686. ui_panel_end();
  687. ui_dialog_end();
  688. }
  689. void reload(void)
  690. {
  691. if (pdf && pdf_has_unsaved_changes(ctx, pdf))
  692. ui.dialog = reload_dialog;
  693. else
  694. reload_document();
  695. }
  696. void trace_action(const char *fmt, ...)
  697. {
  698. va_list args;
  699. if (trace_file)
  700. {
  701. va_start(args, fmt);
  702. fz_write_vprintf(ctx, trace_file, fmt, args);
  703. fz_flush_output(ctx, trace_file);
  704. va_end(args);
  705. va_start(args, fmt);
  706. fz_write_vprintf(ctx, fz_stdout(ctx), fmt, args);
  707. fz_flush_output(ctx, fz_stdout(ctx));
  708. va_end(args);
  709. }
  710. }
  711. void trace_page_update(void)
  712. {
  713. trace_action("page.update();\n");
  714. }
  715. void trace_save_snapshot(void)
  716. {
  717. static int trace_idx = 1;
  718. trace_action("page.toPixmap(Matrix.identity, ColorSpace.DeviceRGB).saveAsPNG(\"trace-%03d.png\");\n", trace_idx++);
  719. }
  720. static int document_shown_as_dirty = 0;
  721. void update_title(void)
  722. {
  723. char buf[256];
  724. const char *title = "MuPDF/GL";
  725. char *extra = "";
  726. size_t n;
  727. int nc = fz_count_chapters(ctx, doc);
  728. title = fz_basename(filename);
  729. document_shown_as_dirty = pdf && pdf_has_unsaved_changes(ctx, pdf);
  730. if (document_shown_as_dirty)
  731. extra = "*";
  732. n = strlen(title);
  733. if (n > 50)
  734. {
  735. if (nc == 1)
  736. sprintf(buf, "...%s%s - %d/%d", title + n - 50, extra, currentpage.page + 1, fz_count_pages(ctx, doc));
  737. else
  738. sprintf(buf, "...%s%s - %d/%d - %d/%d", title + n - 50, extra,
  739. currentpage.chapter + 1, nc,
  740. currentpage.page + 1, fz_count_chapter_pages(ctx, doc, currentpage.chapter));
  741. }
  742. else
  743. {
  744. if (nc == 1)
  745. sprintf(buf, "%s%s - %d/%d", title, extra, currentpage.page + 1, fz_count_pages(ctx, doc));
  746. else
  747. sprintf(buf, "%s%s - %d/%d - %d/%d", title, extra,
  748. currentpage.chapter + 1, nc,
  749. currentpage.page + 1, fz_count_chapter_pages(ctx, doc, currentpage.chapter));
  750. }
  751. glutSetWindowTitle(buf);
  752. glutSetIconTitle(buf);
  753. }
  754. void transform_page(void)
  755. {
  756. draw_page_ctm = fz_transform_page(page_bounds, currentzoom, currentrotate);
  757. draw_page_bounds = fz_transform_rect(page_bounds, draw_page_ctm);
  758. }
  759. static void clear_selected_annot(void)
  760. {
  761. /* clear all editor selections */
  762. if (ui.selected_annot && pdf_annot_type(ctx, ui.selected_annot) == PDF_ANNOT_WIDGET)
  763. pdf_annot_event_blur(ctx, ui.selected_annot);
  764. ui_select_annot(NULL);
  765. }
  766. void load_page(void)
  767. {
  768. fz_irect area;
  769. clear_selected_annot();
  770. if (trace_file)
  771. trace_action("page = doc.loadPage(%d);\n", fz_page_number_from_location(ctx, doc, currentpage));
  772. fz_drop_stext_page(ctx, page_text);
  773. page_text = NULL;
  774. fz_drop_separations(ctx, seps);
  775. seps = NULL;
  776. fz_drop_link(ctx, links);
  777. links = NULL;
  778. fz_drop_page(ctx, fzpage);
  779. fzpage = NULL;
  780. fzpage = fz_load_chapter_page(ctx, doc, currentpage.chapter, currentpage.page);
  781. if (pdf)
  782. page = (pdf_page*)fzpage;
  783. if (trace_file)
  784. {
  785. pdf_annot *w;
  786. int i, s;
  787. for (i = 0, s = 0, w = pdf_first_widget(ctx, page); w != NULL; i++, w = pdf_next_widget(ctx, w))
  788. if (pdf_widget_type(ctx, w) == PDF_WIDGET_TYPE_SIGNATURE)
  789. {
  790. int is_signed;
  791. s++;
  792. trace_action("widget = page.getWidgets()[%d];\n", i);
  793. trace_action("widgetstr = 'Signature %d on page %d';\n",
  794. s, fz_page_number_from_location(ctx, doc, currentpage));
  795. is_signed = pdf_widget_is_signed(ctx, w);
  796. trace_action("tmp = widget.isSigned();\n");
  797. trace_action("if (tmp != %d)\n", is_signed);
  798. trace_action(" throw new RegressionError(widgetstr, 'is signed:', tmp|0, 'expected:', %d);\n", is_signed);
  799. if (is_signed)
  800. {
  801. int valid_until, is_readonly;
  802. char *cert_error, *digest_error;
  803. pdf_pkcs7_distinguished_name *dn;
  804. pdf_pkcs7_verifier *verifier;
  805. char *signatory = NULL;
  806. char buf[500];
  807. valid_until = pdf_validate_signature(ctx, w);
  808. is_readonly = pdf_widget_is_readonly(ctx, w);
  809. verifier = pkcs7_openssl_new_verifier(ctx);
  810. cert_error = pdf_signature_error_description(pdf_check_widget_certificate(ctx, verifier, w));
  811. digest_error = pdf_signature_error_description(pdf_check_widget_digest(ctx, verifier, w));
  812. dn = pdf_signature_get_widget_signatory(ctx, verifier, w);
  813. if (dn)
  814. {
  815. char *s = pdf_signature_format_distinguished_name(ctx, dn);
  816. fz_strlcpy(buf, s, sizeof buf);
  817. fz_free(ctx, s);
  818. pdf_signature_drop_distinguished_name(ctx, dn);
  819. }
  820. else
  821. {
  822. fz_strlcpy(buf, "Signature information missing.", sizeof buf);
  823. }
  824. signatory = &buf[0];
  825. pdf_drop_verifier(ctx, verifier);
  826. trace_action("tmp = widget.validateSignature();\n");
  827. trace_action("if (tmp != %d)\n", valid_until);
  828. trace_action(" throw new RegressionError(widgetstr, 'valid until:', tmp, 'expected:', %d);\n", valid_until);
  829. trace_action("tmp = widget.isReadOnly();\n");
  830. trace_action("if (tmp != %d)\n", is_readonly);
  831. trace_action(" throw new RegressionError(widgetstr, 'is read-only:', tmp, 'expected:', %d);\n", is_readonly);
  832. trace_action("tmp = widget.checkCertificate();\n");
  833. trace_action("if (tmp != '%s')\n", cert_error);
  834. trace_action(" throw new RegressionError(widgetstr, 'certificate error:', tmp, 'expected:', %d);\n", cert_error);
  835. trace_action("tmp = widget.checkDigest();\n");
  836. trace_action("if (tmp != %q)\n", digest_error);
  837. trace_action(" throw new RegressionError(widgetstr, 'digest error:', tmp, 'expected:', %q);\n", digest_error);
  838. trace_action("tmp = widget.getSignatory();\n");
  839. trace_action("if (tmp != '%s')\n", signatory);
  840. trace_action(" throw new RegressionError(widgetstr, 'signatory:', '[', tmp, ']', 'expected:', '[', %q, ']');\n", signatory);
  841. }
  842. }
  843. }
  844. links = fz_load_links(ctx, fzpage);
  845. page_text = fz_new_stext_page_from_page(ctx, fzpage, NULL);
  846. if (currenticc)
  847. fz_enable_icc(ctx);
  848. else
  849. fz_disable_icc(ctx);
  850. if (currentseparations)
  851. {
  852. seps = fz_page_separations(ctx, fzpage);
  853. if (seps)
  854. {
  855. int i, n = fz_count_separations(ctx, seps);
  856. for (i = 0; i < n; i++)
  857. fz_set_separation_behavior(ctx, seps, i, FZ_SEPARATION_COMPOSITE);
  858. }
  859. else if (fz_page_uses_overprint(ctx, fzpage))
  860. seps = fz_new_separations(ctx, 0);
  861. else if (fz_document_output_intent(ctx, doc))
  862. seps = fz_new_separations(ctx, 0);
  863. }
  864. /* compute bounds here for initial window size */
  865. page_bounds = fz_bound_page_box(ctx, fzpage, currentbox);
  866. transform_page();
  867. area = fz_irect_from_rect(draw_page_bounds);
  868. page_tex.w = area.x1 - area.x0;
  869. page_tex.h = area.y1 - area.y0;
  870. page_contents_changed = 1;
  871. }
  872. static void render_page(void)
  873. {
  874. fz_irect bbox;
  875. fz_pixmap *pix;
  876. fz_device *dev;
  877. page_bounds = fz_bound_page_box(ctx, fzpage, currentbox);
  878. transform_page();
  879. fz_set_aa_level(ctx, currentaa);
  880. if (page_contents_changed)
  881. {
  882. fz_drop_pixmap(ctx, page_contents);
  883. page_contents = NULL;
  884. bbox = fz_round_rect(fz_transform_rect(fz_bound_page_box(ctx, fzpage, currentbox), draw_page_ctm));
  885. page_contents = fz_new_pixmap_with_bbox(ctx, profile, bbox, seps, 0);
  886. fz_clear_pixmap(ctx, page_contents);
  887. dev = fz_new_draw_device(ctx, draw_page_ctm, page_contents);
  888. fz_try(ctx)
  889. {
  890. fz_run_page_contents(ctx, fzpage, dev, fz_identity, NULL);
  891. fz_close_device(ctx, dev);
  892. }
  893. fz_always(ctx)
  894. fz_drop_device(ctx, dev);
  895. fz_catch(ctx)
  896. fz_rethrow(ctx);
  897. }
  898. pix = fz_clone_pixmap_area_with_different_seps(ctx, page_contents, NULL, profile, NULL, fz_default_color_params, NULL);
  899. {
  900. dev = fz_new_draw_device(ctx, draw_page_ctm, pix);
  901. fz_try(ctx)
  902. {
  903. fz_run_page_annots(ctx, fzpage, dev, fz_identity, NULL);
  904. fz_run_page_widgets(ctx, fzpage, dev, fz_identity, NULL);
  905. fz_close_device(ctx, dev);
  906. }
  907. fz_always(ctx)
  908. fz_drop_device(ctx, dev);
  909. fz_catch(ctx)
  910. fz_rethrow(ctx);
  911. }
  912. if (currentinvert)
  913. {
  914. fz_invert_pixmap_luminance(ctx, pix);
  915. fz_gamma_pixmap(ctx, pix, 1 / 1.4f);
  916. }
  917. if (currenttint)
  918. {
  919. fz_tint_pixmap(ctx, pix, tint_black, tint_white);
  920. }
  921. ui_texture_from_pixmap(&page_tex, pix);
  922. fz_drop_pixmap(ctx, pix);
  923. FZ_LOG_DUMP_STORE(ctx, "Store state after page render:\n");
  924. }
  925. void render_page_if_changed(void)
  926. {
  927. if (pdf)
  928. {
  929. if (pdf_update_page(ctx, page))
  930. {
  931. trace_page_update();
  932. page_annots_changed = 1;
  933. }
  934. }
  935. if (oldpage.chapter != currentpage.chapter ||
  936. oldpage.page != currentpage.page ||
  937. oldzoom != currentzoom ||
  938. oldrotate != currentrotate ||
  939. oldinvert != currentinvert ||
  940. oldtint != currenttint ||
  941. oldicc != currenticc ||
  942. oldseparations != currentseparations ||
  943. oldaa != currentaa ||
  944. oldbox != currentbox)
  945. {
  946. page_contents_changed = 1;
  947. }
  948. if (page_contents_changed || page_annots_changed)
  949. {
  950. render_page();
  951. oldpage = currentpage;
  952. oldzoom = currentzoom;
  953. oldrotate = currentrotate;
  954. oldinvert = currentinvert;
  955. oldtint = currenttint;
  956. oldicc = currenticc;
  957. oldseparations = currentseparations;
  958. oldaa = currentaa;
  959. oldbox = currentbox;
  960. page_contents_changed = 0;
  961. page_annots_changed = 0;
  962. }
  963. }
  964. static struct mark save_mark()
  965. {
  966. struct mark mark;
  967. mark.loc = currentpage;
  968. mark.scroll = fz_transform_point_xy(scroll_x, scroll_y, view_page_inv_ctm);
  969. return mark;
  970. }
  971. static void restore_mark(struct mark mark)
  972. {
  973. currentpage = mark.loc;
  974. mark.scroll = fz_transform_point(mark.scroll, draw_page_ctm);
  975. scroll_x = mark.scroll.x;
  976. scroll_y = mark.scroll.y;
  977. }
  978. static int eqloc(fz_location a, fz_location b)
  979. {
  980. return a.chapter == b.chapter && a.page == b.page;
  981. }
  982. int search_has_results(void)
  983. {
  984. return !search_active && eqloc(search_hit_page, currentpage) && search_hit_count > 0;
  985. }
  986. static int is_first_page(fz_location loc)
  987. {
  988. return (loc.chapter == 0 && loc.page == 0);
  989. }
  990. static int is_last_page(fz_location loc)
  991. {
  992. fz_location last = fz_last_page(ctx, doc);
  993. return (loc.chapter == last.chapter && loc.page == last.page);
  994. }
  995. static void push_history(void)
  996. {
  997. if (history_count > 0 && eqloc(history[history_count-1].loc, currentpage))
  998. return;
  999. if (history_count + 1 >= (int)nelem(history))
  1000. {
  1001. memmove(history, history + 1, sizeof *history * (nelem(history) - 1));
  1002. history[history_count] = save_mark();
  1003. }
  1004. else
  1005. {
  1006. history[history_count++] = save_mark();
  1007. }
  1008. }
  1009. static void push_future(void)
  1010. {
  1011. if (future_count + 1 >= (int)nelem(future))
  1012. {
  1013. memmove(future, future + 1, sizeof *future * (nelem(future) - 1));
  1014. future[future_count] = save_mark();
  1015. }
  1016. else
  1017. {
  1018. future[future_count++] = save_mark();
  1019. }
  1020. }
  1021. static void clear_future(void)
  1022. {
  1023. future_count = 0;
  1024. }
  1025. static void jump_to_location(fz_location loc)
  1026. {
  1027. clear_future();
  1028. push_history();
  1029. currentpage = fz_clamp_location(ctx, doc, loc);
  1030. push_history();
  1031. }
  1032. static void jump_to_location_xy(fz_location loc, float x, float y)
  1033. {
  1034. fz_point p = fz_transform_point_xy(x, y, draw_page_ctm);
  1035. clear_future();
  1036. push_history();
  1037. currentpage = fz_clamp_location(ctx, doc, loc);
  1038. scroll_x = p.x;
  1039. scroll_y = p.y;
  1040. push_history();
  1041. }
  1042. static void jump_to_page(int newpage)
  1043. {
  1044. clear_future();
  1045. push_history();
  1046. currentpage = fz_location_from_page_number(ctx, doc, newpage);
  1047. currentpage = fz_clamp_location(ctx, doc, currentpage);
  1048. push_history();
  1049. }
  1050. static void jump_to_page_xy(int newpage, float x, float y)
  1051. {
  1052. fz_point p = fz_transform_point_xy(x, y, draw_page_ctm);
  1053. clear_future();
  1054. push_history();
  1055. currentpage = fz_location_from_page_number(ctx, doc, newpage);
  1056. currentpage = fz_clamp_location(ctx, doc, currentpage);
  1057. scroll_x = p.x;
  1058. scroll_y = p.y;
  1059. push_history();
  1060. }
  1061. static void pop_history(void)
  1062. {
  1063. fz_location here = currentpage;
  1064. push_future();
  1065. while (history_count > 0 && eqloc(currentpage, here))
  1066. restore_mark(history[--history_count]);
  1067. }
  1068. static void pop_future(void)
  1069. {
  1070. fz_location here = currentpage;
  1071. push_history();
  1072. while (future_count > 0 && eqloc(currentpage, here))
  1073. restore_mark(future[--future_count]);
  1074. push_history();
  1075. }
  1076. static void relayout(void)
  1077. {
  1078. if (layout_em < 6) layout_em = 6;
  1079. if (layout_em > 36) layout_em = 36;
  1080. if (fz_is_document_reflowable(ctx, doc))
  1081. {
  1082. fz_bookmark mark = fz_make_bookmark(ctx, doc, currentpage);
  1083. fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
  1084. currentpage = fz_lookup_bookmark(ctx, doc, mark);
  1085. history_count = 0;
  1086. future_count = 0;
  1087. load_page();
  1088. update_title();
  1089. }
  1090. }
  1091. static int count_outline(fz_outline *node, int end)
  1092. {
  1093. int is_selected, n, p, np;
  1094. int count = 0;
  1095. if (!node)
  1096. return 0;
  1097. np = fz_page_number_from_location(ctx, doc, node->page);
  1098. do
  1099. {
  1100. p = np;
  1101. count += 1;
  1102. n = end;
  1103. if (node->next && (np = fz_page_number_from_location(ctx, doc, node->next->page)) >= 0)
  1104. n = fz_page_number_from_location(ctx, doc, node->next->page);
  1105. is_selected = 0;
  1106. if (fz_count_chapters(ctx, doc) == 1)
  1107. is_selected = (p>=0) && (currentpage.page == p || (currentpage.page > p && currentpage.page < n));
  1108. if (node->down && (node->is_open || is_selected))
  1109. count += count_outline(node->down, end);
  1110. node = node->next;
  1111. }
  1112. while (node);
  1113. return count;
  1114. }
  1115. static void do_outline_imp(struct list *list, int end, fz_outline *node, int depth)
  1116. {
  1117. int is_selected, is_open, was_open, n, np;
  1118. if (!node)
  1119. return;
  1120. np = fz_page_number_from_location(ctx, doc, node->page);
  1121. do
  1122. {
  1123. int p = np;
  1124. n = end;
  1125. if (node->next && (np = fz_page_number_from_location(ctx, doc, node->next->page)) >= 0)
  1126. n = np;
  1127. is_open = was_open = node->is_open;
  1128. is_selected = 0;
  1129. if (fz_count_chapters(ctx, doc) == 1)
  1130. is_selected = (p>=0) && (currentpage.page == p || (currentpage.page > p && currentpage.page < n));
  1131. if (ui_tree_item(list, node, node->title, is_selected, depth, !!node->down, &is_open))
  1132. {
  1133. node->is_open = is_open;
  1134. if (p < 0)
  1135. {
  1136. currentpage = fz_resolve_link(ctx, doc, node->uri, &node->x, &node->y);
  1137. jump_to_location_xy(currentpage, node->x, node->y);
  1138. }
  1139. else
  1140. {
  1141. jump_to_page_xy(p, node->x, node->y);
  1142. }
  1143. }
  1144. node->is_open = is_open;
  1145. if (node->down && (was_open || is_selected))
  1146. do_outline_imp(list, n, node->down, depth + 1);
  1147. node = node->next;
  1148. }
  1149. while (node);
  1150. }
  1151. static void do_outline(fz_outline *node)
  1152. {
  1153. static struct list list;
  1154. ui_layout(L, BOTH, NW, 0, 0);
  1155. ui_tree_begin(&list, count_outline(node, 65535), outline_w, 0, 1);
  1156. do_outline_imp(&list, 65535, node, 0);
  1157. ui_tree_end(&list);
  1158. }
  1159. static void do_undo(void)
  1160. {
  1161. static struct list list;
  1162. int count = 0;
  1163. int pos;
  1164. int i;
  1165. int desired = -1;
  1166. if (pdf)
  1167. pos = pdf_undoredo_state(ctx, pdf, &count);
  1168. else
  1169. pos = 0;
  1170. ui_layout(L, BOTH, NW, 0, 0);
  1171. ui_panel_begin(outline_w, 0, ui.padsize*2, ui.padsize*2, 1);
  1172. ui_layout(T, X, NW, ui.padsize, ui.padsize);
  1173. ui_label("Undo history:");
  1174. ui_layout(B, X, NW, ui.padsize, ui.padsize);
  1175. if (ui_button_aux("Redo", pos == count))
  1176. desired = pos+1;
  1177. if (ui_button_aux("Undo", pos == 0))
  1178. desired = pos-1;
  1179. ui_layout(ALL, BOTH, NW, ui.padsize, ui.padsize);
  1180. ui_list_begin(&list, count+1, 0, ui.lineheight * 4 + 4);
  1181. for (i = 0; i < count+1; i++)
  1182. {
  1183. const char *op;
  1184. if (i == 0)
  1185. op = "Original Document";
  1186. else
  1187. op = pdf_undoredo_step(ctx, pdf, i-1);
  1188. if (ui_list_item(&list, (void *)(intptr_t)(i+1), op, i <= pos))
  1189. {
  1190. desired = i;
  1191. }
  1192. }
  1193. ui_list_end(&list);
  1194. if (desired != -1 && desired != pos)
  1195. {
  1196. clear_selected_annot();
  1197. page_contents_changed = 1;
  1198. while (pos > desired)
  1199. {
  1200. trace_action("doc.undo();\n");
  1201. pdf_undo(ctx, pdf);
  1202. pos--;
  1203. }
  1204. while (pos < desired)
  1205. {
  1206. trace_action("doc.redo();\n");
  1207. pdf_redo(ctx, pdf);
  1208. pos++;
  1209. }
  1210. load_page();
  1211. }
  1212. ui_panel_end();
  1213. }
  1214. static void do_layers(void)
  1215. {
  1216. const char *name;
  1217. int n, i, on;
  1218. ui_layout(L, BOTH, NW, 0, 0);
  1219. ui_panel_begin(outline_w, 0, ui.padsize*2, ui.padsize*2, 1);
  1220. ui_layout(T, X, NW, ui.padsize, ui.padsize);
  1221. ui_label("Layers:");
  1222. ui_layout(T, X, NW, ui.padsize*2, ui.padsize);
  1223. if (pdf)
  1224. {
  1225. n = pdf_count_layers(ctx, pdf);
  1226. for (i = 0; i < n; ++i)
  1227. {
  1228. name = pdf_layer_name(ctx, pdf, i);
  1229. on = pdf_layer_is_enabled(ctx, pdf, i);
  1230. if (ui_checkbox(name, &on))
  1231. {
  1232. pdf_enable_layer(ctx, pdf, i, on);
  1233. page_contents_changed = 1;
  1234. }
  1235. }
  1236. if (n == 0)
  1237. ui_label("None");
  1238. }
  1239. else
  1240. {
  1241. ui_label("None");
  1242. }
  1243. ui_panel_end();
  1244. }
  1245. static void do_links(fz_link *link)
  1246. {
  1247. fz_rect bounds;
  1248. fz_irect area;
  1249. float link_x, link_y;
  1250. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1251. glEnable(GL_BLEND);
  1252. tooltip = NULL;
  1253. while (link)
  1254. {
  1255. bounds = link->rect;
  1256. bounds = fz_transform_rect(link->rect, view_page_ctm);
  1257. area = fz_irect_from_rect(bounds);
  1258. if (ui_mouse_inside(area))
  1259. {
  1260. if (!tooltip)
  1261. tooltip = link->uri;
  1262. ui.hot = link;
  1263. if (!ui.active && ui.down)
  1264. ui.active = link;
  1265. }
  1266. if (ui.hot == link || showlinks)
  1267. {
  1268. if (ui.active == link && ui.hot == link)
  1269. glColor4f(0, 0, 1, 0.4f);
  1270. else if (ui.hot == link)
  1271. glColor4f(0, 0, 1, 0.2f);
  1272. else
  1273. glColor4f(0, 0, 1, 0.1f);
  1274. glRectf(area.x0, area.y0, area.x1, area.y1);
  1275. }
  1276. if (ui.active == link && !ui.down)
  1277. {
  1278. if (ui.hot == link)
  1279. {
  1280. if (fz_is_external_link(ctx, link->uri))
  1281. open_browser(link->uri);
  1282. else
  1283. {
  1284. fz_location loc = fz_resolve_link(ctx, doc, link->uri, &link_x, &link_y);
  1285. jump_to_location_xy(loc, link_x, link_y);
  1286. }
  1287. }
  1288. }
  1289. link = link->next;
  1290. }
  1291. glDisable(GL_BLEND);
  1292. }
  1293. static void do_page_selection(void)
  1294. {
  1295. static fz_point pt = { 0, 0 };
  1296. static fz_quad hits[1000];
  1297. fz_rect rect;
  1298. int i, n;
  1299. if (ui_mouse_inside(view_page_area))
  1300. {
  1301. ui.hot = &pt;
  1302. if (!ui.active && ui.right)
  1303. {
  1304. ui.active = &pt;
  1305. pt.x = ui.x;
  1306. pt.y = ui.y;
  1307. }
  1308. }
  1309. if (ui.active == &pt)
  1310. {
  1311. fz_point page_a = { pt.x, pt.y };
  1312. fz_point page_b = { ui.x, ui.y };
  1313. page_a = fz_transform_point(page_a, view_page_inv_ctm);
  1314. page_b = fz_transform_point(page_b, view_page_inv_ctm);
  1315. if (ui.mod == GLUT_ACTIVE_CTRL)
  1316. fz_snap_selection(ctx, page_text, &page_a, &page_b, FZ_SELECT_WORDS);
  1317. else if (ui.mod == GLUT_ACTIVE_CTRL + GLUT_ACTIVE_SHIFT)
  1318. fz_snap_selection(ctx, page_text, &page_a, &page_b, FZ_SELECT_LINES);
  1319. if (ui.mod == GLUT_ACTIVE_SHIFT)
  1320. {
  1321. rect = fz_make_rect(
  1322. fz_min(page_a.x, page_b.x),
  1323. fz_min(page_a.y, page_b.y),
  1324. fz_max(page_a.x, page_b.x),
  1325. fz_max(page_a.y, page_b.y));
  1326. n = 1;
  1327. hits[0] = fz_quad_from_rect(rect);
  1328. }
  1329. else
  1330. {
  1331. n = fz_highlight_selection(ctx, page_text, page_a, page_b, hits, nelem(hits));
  1332. }
  1333. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1334. glEnable(GL_BLEND);
  1335. glColor4f(0.0, 0.1, 0.4, 0.3f);
  1336. glBegin(GL_QUADS);
  1337. for (i = 0; i < n; ++i)
  1338. {
  1339. fz_quad thit = fz_transform_quad(hits[i], view_page_ctm);
  1340. glVertex2f(thit.ul.x, thit.ul.y);
  1341. glVertex2f(thit.ur.x, thit.ur.y);
  1342. glVertex2f(thit.lr.x, thit.lr.y);
  1343. glVertex2f(thit.ll.x, thit.ll.y);
  1344. }
  1345. glEnd();
  1346. glDisable(GL_BLEND);
  1347. if (!ui.right)
  1348. {
  1349. char *s;
  1350. #ifdef _WIN32
  1351. if (ui.mod == GLUT_ACTIVE_SHIFT)
  1352. s = fz_copy_rectangle(ctx, page_text, rect, 1);
  1353. else
  1354. s = fz_copy_selection(ctx, page_text, page_a, page_b, 1);
  1355. #else
  1356. if (ui.mod == GLUT_ACTIVE_SHIFT)
  1357. s = fz_copy_rectangle(ctx, page_text, rect, 0);
  1358. else
  1359. s = fz_copy_selection(ctx, page_text, page_a, page_b, 0);
  1360. #endif
  1361. ui_set_clipboard(s);
  1362. fz_free(ctx, s);
  1363. }
  1364. }
  1365. }
  1366. static void do_search_hits(void)
  1367. {
  1368. int i;
  1369. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1370. glEnable(GL_BLEND);
  1371. glColor4f(1, 0, 0, 0.4f);
  1372. glBegin(GL_QUADS);
  1373. for (i = 0; i < search_hit_count; ++i)
  1374. {
  1375. fz_quad thit = fz_transform_quad(search_hit_quads[i], view_page_ctm);
  1376. glVertex2f(thit.ul.x, thit.ul.y);
  1377. glVertex2f(thit.ur.x, thit.ur.y);
  1378. glVertex2f(thit.lr.x, thit.lr.y);
  1379. glVertex2f(thit.ll.x, thit.ll.y);
  1380. }
  1381. glEnd();
  1382. glDisable(GL_BLEND);
  1383. }
  1384. static void toggle_fullscreen(void)
  1385. {
  1386. static int win_x = 0, win_y = 0;
  1387. static int win_w = 100, win_h = 100;
  1388. if (!isfullscreen)
  1389. {
  1390. win_w = glutGet(GLUT_WINDOW_WIDTH);
  1391. win_h = glutGet(GLUT_WINDOW_HEIGHT);
  1392. win_x = glutGet(GLUT_WINDOW_X);
  1393. win_y = glutGet(GLUT_WINDOW_Y);
  1394. glutFullScreen();
  1395. isfullscreen = 1;
  1396. }
  1397. else
  1398. {
  1399. glutPositionWindow(win_x, win_y);
  1400. glutReshapeWindow(win_w, win_h);
  1401. isfullscreen = 0;
  1402. }
  1403. }
  1404. static void shrinkwrap(void)
  1405. {
  1406. int w = page_tex.w;
  1407. int h = page_tex.h;
  1408. if (showoutline || showundo || showlayers)
  1409. w += outline_w + 4;
  1410. if (showannotate)
  1411. w += annotate_w;
  1412. #if FZ_ENABLE_JS
  1413. if (showconsole)
  1414. h += console_h;
  1415. #endif
  1416. if (screen_w > 0 && w > screen_w)
  1417. w = screen_w;
  1418. if (screen_h > 0 && h > screen_h)
  1419. h = screen_h;
  1420. if (isfullscreen)
  1421. toggle_fullscreen();
  1422. glutReshapeWindow(w, h);
  1423. }
  1424. static struct input input_password;
  1425. static void password_dialog(void)
  1426. {
  1427. int is;
  1428. ui_dialog_begin(ui.gridsize*16, (ui.gridsize+ui.padsize*2)*3);
  1429. {
  1430. ui_layout(T, X, NW, ui.padsize, ui.padsize);
  1431. ui_label("Password:");
  1432. is = ui_input(&input_password, 200, 1);
  1433. ui_layout(B, X, NW, ui.padsize, ui.padsize);
  1434. ui_panel_begin(0, ui.gridsize, 0, 0, 0);
  1435. {
  1436. ui_layout(R, NONE, S, 0, 0);
  1437. if (ui_button("Cancel") || (!ui.focus && ui.key == KEY_ESCAPE))
  1438. glutLeaveMainLoop();
  1439. ui_spacer();
  1440. if (ui_button("Okay") || is == UI_INPUT_ACCEPT)
  1441. {
  1442. password = input_password.text;
  1443. ui.dialog = NULL;
  1444. reload_document();
  1445. shrinkwrap();
  1446. }
  1447. }
  1448. ui_panel_end();
  1449. }
  1450. ui_dialog_end();
  1451. }
  1452. /* Parse "chapter:page" from anchor. "chapter:" is also accepted,
  1453. * meaning first page. Return 1 if parsing succeeded, 0 if failed.
  1454. */
  1455. static int
  1456. parse_location(const char *anc, fz_location *loc)
  1457. {
  1458. const char *s, *p;
  1459. if (anc == NULL)
  1460. return 0;
  1461. s = anc;
  1462. while (*s >= '0' && *s <= '9')
  1463. s++;
  1464. loc->chapter = fz_atoi(anc)-1;
  1465. if (*s == 0)
  1466. {
  1467. *loc = fz_location_from_page_number(ctx, doc, loc->chapter);
  1468. return 1;
  1469. }
  1470. if (*s != ':')
  1471. return 0;
  1472. p = ++s;
  1473. while (*s >= '0' && *s <= '9')
  1474. s++;
  1475. if (s == p)
  1476. loc->page = 0;
  1477. else
  1478. loc->page = fz_atoi(p)-1;
  1479. return 1;
  1480. }
  1481. static void
  1482. reload_or_start_journalling(void)
  1483. {
  1484. char journal[PATH_MAX];
  1485. fz_strlcpy(journal, filename, sizeof(journal));
  1486. fz_strlcat(journal, ".journal", sizeof(journal));
  1487. fz_try(ctx)
  1488. {
  1489. /* Probe with fz_file_exists to avoid 'can't find' errors. */
  1490. if (fz_file_exists(ctx, journal))
  1491. pdf_load_journal(ctx, pdf, journal);
  1492. }
  1493. fz_catch(ctx)
  1494. {
  1495. /* Ignore any failures here. */
  1496. }
  1497. trace_action("doc.enableJournal();\n");
  1498. pdf_enable_journal(ctx, pdf);
  1499. }
  1500. static void alert_box(const char *fmt, const char *str)
  1501. {
  1502. #ifdef _WIN32
  1503. MessageBoxA(NULL, str, "MuPDF Alert", MB_ICONERROR);
  1504. #else
  1505. fprintf(stderr, "MuPDF Alert: %s\n", str);
  1506. #endif
  1507. }
  1508. static void event_cb(fz_context *callback_ctx, pdf_document *callback_doc, pdf_doc_event *evt, void *data)
  1509. {
  1510. switch (evt->type)
  1511. {
  1512. case PDF_DOCUMENT_EVENT_ALERT:
  1513. {
  1514. pdf_alert_event *alert = pdf_access_alert_event(callback_ctx, evt);
  1515. alert_box("%s", alert->message);
  1516. }
  1517. break;
  1518. default:
  1519. fz_throw(callback_ctx, FZ_ERROR_UNSUPPORTED, "event not yet implemented");
  1520. break;
  1521. }
  1522. }
  1523. static void load_document(void)
  1524. {
  1525. char accelpath[PATH_MAX];
  1526. char *accel = NULL;
  1527. time_t atime;
  1528. time_t dtime;
  1529. fz_location location;
  1530. fz_drop_outline(ctx, outline);
  1531. outline = NULL;
  1532. fz_drop_document(ctx, doc);
  1533. doc = NULL;
  1534. if (!strncmp(filename, "file://", 7))
  1535. {
  1536. anchor = strchr(filename + 7, '#');
  1537. if (anchor)
  1538. {
  1539. memmove(anchor + 1, anchor, strlen(anchor) + 1);
  1540. *anchor = 0;
  1541. anchor++;
  1542. }
  1543. memmove(filename, filename + 7, strlen(filename));
  1544. }
  1545. /* If there was an accelerator to load, what would it be called? */
  1546. if (get_accelerator_filename(accelpath, sizeof(accelpath), 0))
  1547. {
  1548. /* Check whether that file exists, and isn't older than
  1549. * the document. */
  1550. atime = fz_stat_mtime(accelpath);
  1551. dtime = fz_stat_mtime(filename);
  1552. if (atime == 0)
  1553. {
  1554. /* No accelerator */
  1555. }
  1556. else if (atime > dtime)
  1557. accel = accelpath;
  1558. else
  1559. {
  1560. /* Accelerator data is out of date */
  1561. #ifdef _WIN32
  1562. fz_remove_utf8(accelpath);
  1563. #else
  1564. remove(accelpath);
  1565. #endif
  1566. accel = NULL; /* In case we have jumped up from below */
  1567. }
  1568. }
  1569. trace_action("doc = Document.openDocument(%q);\n", filename);
  1570. doc = fz_open_accelerated_document(ctx, filename, accel);
  1571. pdf = pdf_specifics(ctx, doc);
  1572. if (pdf && trace_file)
  1573. {
  1574. int needspass = pdf_needs_password(ctx, pdf);
  1575. trace_action(
  1576. "tmp = doc.needsPassword();\n"
  1577. "if (tmp != %s)\n"
  1578. " throw new RegressionError('Document password needed:', tmp, 'expected:', %s);\n",
  1579. needspass ? "true" : "false",
  1580. needspass ? "true" : "false");
  1581. }
  1582. if (fz_needs_password(ctx, doc))
  1583. {
  1584. int result = fz_authenticate_password(ctx, doc, password);
  1585. if (pdf && trace_file)
  1586. {
  1587. trace_action(
  1588. "tmp = doc.authenticatePassword(%q);\n"
  1589. "if (tmp != %s)\n"
  1590. " throw new RegressionError('Open document with password %q result: %s', 'expected:', '%s');\n",
  1591. password,
  1592. result ? "true" : "false",
  1593. password,
  1594. !result ? "pass" : "fail",
  1595. result ? "pass" : "fail");
  1596. }
  1597. if (!result)
  1598. {
  1599. fz_drop_document(ctx, doc);
  1600. doc = NULL;
  1601. ui_input_init(&input_password, "");
  1602. ui.focus = &input_password;
  1603. ui.dialog = password_dialog;
  1604. return;
  1605. }
  1606. }
  1607. fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
  1608. fz_try(ctx)
  1609. outline = fz_load_outline(ctx, doc);
  1610. fz_catch(ctx)
  1611. {
  1612. fz_report_error(ctx);
  1613. outline = NULL;
  1614. }
  1615. load_history();
  1616. if (pdf)
  1617. {
  1618. #if FZ_ENABLE_JS
  1619. if (enable_js)
  1620. {
  1621. trace_action("doc.enableJS();\n");
  1622. pdf_enable_js(ctx, pdf);
  1623. pdf_js_set_console(ctx, pdf, &gl_js_console, NULL);
  1624. }
  1625. #endif
  1626. reload_or_start_journalling();
  1627. if (trace_file)
  1628. {
  1629. int vsns = pdf_count_versions(ctx, pdf);
  1630. trace_action(
  1631. "tmp = doc.countVersions();\n"
  1632. "if (tmp != %d)\n"
  1633. " throw new RegressionError('Document versions:', tmp, 'expected:', %d);\n",
  1634. vsns, vsns);
  1635. if (vsns > 1)
  1636. {
  1637. int valid = pdf_validate_change_history(ctx, pdf);
  1638. trace_action("tmp = doc.validateChangeHistory();\n");
  1639. trace_action("if (tmp != %d)\n", valid);
  1640. trace_action(" throw new RegressionError('History validation:', tmp, 'expected:', %d);\n", valid);
  1641. }
  1642. }
  1643. }
  1644. if (anchor)
  1645. {
  1646. if (parse_location(anchor, &location))
  1647. jump_to_location(location);
  1648. else
  1649. {
  1650. location = fz_resolve_link(ctx, doc, anchor, NULL, NULL);
  1651. if (location.page < 0)
  1652. fz_warn(ctx, "cannot find location: %s", anchor);
  1653. else
  1654. jump_to_location(location);
  1655. }
  1656. }
  1657. anchor = NULL;
  1658. oldpage = currentpage = fz_clamp_location(ctx, doc, currentpage);
  1659. if (pdf)
  1660. pdf_set_doc_event_callback(ctx, pdf, event_cb, NULL, NULL);
  1661. }
  1662. static void reflow_document(void)
  1663. {
  1664. char buf[256];
  1665. fz_document *new_doc;
  1666. fz_stext_options opts;
  1667. if (fz_is_document_reflowable(ctx, doc))
  1668. return;
  1669. fz_drop_outline(ctx, outline);
  1670. outline = NULL;
  1671. fz_parse_stext_options(ctx, &opts, reflow_options);
  1672. new_doc = fz_open_reflowed_document(ctx, doc, &opts);
  1673. fz_drop_document(ctx, doc);
  1674. doc = new_doc;
  1675. pdf = NULL;
  1676. page = NULL;
  1677. fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
  1678. fz_try(ctx)
  1679. outline = fz_load_outline(ctx, doc);
  1680. fz_catch(ctx)
  1681. outline = NULL;
  1682. fz_strlcpy(buf, filename, sizeof buf);
  1683. fz_snprintf(filename, sizeof filename, "%s.xhtml", buf);
  1684. load_history();
  1685. if (anchor)
  1686. jump_to_page(fz_atoi(anchor) - 1);
  1687. anchor = NULL;
  1688. currentpage = fz_clamp_location(ctx, doc, currentpage);
  1689. }
  1690. void reload_document(void)
  1691. {
  1692. save_history();
  1693. save_accelerator();
  1694. load_document();
  1695. if (doc)
  1696. {
  1697. if (reflow_options)
  1698. reflow_document();
  1699. load_page();
  1700. update_title();
  1701. }
  1702. }
  1703. static void toggle_outline(void)
  1704. {
  1705. if (outline)
  1706. {
  1707. showoutline = !showoutline;
  1708. showundo = showlayers = 0;
  1709. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1710. shrinkwrap();
  1711. }
  1712. }
  1713. static void toggle_undo(void)
  1714. {
  1715. showundo = !showundo;
  1716. showoutline = showlayers = 0;
  1717. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1718. shrinkwrap();
  1719. }
  1720. static void toggle_layers(void)
  1721. {
  1722. showlayers = !showlayers;
  1723. showoutline = showundo = 0;
  1724. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1725. shrinkwrap();
  1726. }
  1727. void toggle_annotate(int mode)
  1728. {
  1729. if (pdf)
  1730. {
  1731. if (showannotate != mode)
  1732. showannotate = mode;
  1733. else
  1734. showannotate = ANNOTATE_MODE_NONE;
  1735. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1736. shrinkwrap();
  1737. }
  1738. }
  1739. static void set_zoom(int z, int cx, int cy)
  1740. {
  1741. z = fz_clamp(z, MINRES, MAXRES);
  1742. scroll_x = (scroll_x + cx - canvas_x) * z / currentzoom - cx + canvas_x;
  1743. scroll_y = (scroll_y + cy - canvas_y) * z / currentzoom - cy + canvas_y;
  1744. currentzoom = z;
  1745. }
  1746. static void auto_zoom_w(void)
  1747. {
  1748. currentzoom = fz_clamp(currentzoom * canvas_w / page_tex.w, MINRES, MAXRES);
  1749. }
  1750. static void auto_zoom_h(void)
  1751. {
  1752. currentzoom = fz_clamp(currentzoom * canvas_h / page_tex.h, MINRES, MAXRES);
  1753. }
  1754. static void auto_zoom(void)
  1755. {
  1756. float page_a = (float) page_tex.w / page_tex.h;
  1757. float screen_a = (float) canvas_w / canvas_h;
  1758. if (page_a > screen_a)
  1759. auto_zoom_w();
  1760. else
  1761. auto_zoom_h();
  1762. }
  1763. static void smart_move_backward(void)
  1764. {
  1765. int slop_x = page_tex.w / 20;
  1766. int slop_y = page_tex.h / 20;
  1767. if (scroll_y <= slop_y)
  1768. {
  1769. if (scroll_x <= slop_x)
  1770. {
  1771. fz_location prev = fz_previous_page(ctx, doc, currentpage);
  1772. if (!eqloc(currentpage, prev))
  1773. {
  1774. scroll_x = (page_tex.w <= canvas_w) ? 0 : page_tex.w - canvas_w;
  1775. scroll_y = (page_tex.h <= canvas_h) ? 0 : page_tex.h - canvas_h;
  1776. currentpage = prev;
  1777. }
  1778. }
  1779. else
  1780. {
  1781. scroll_y = page_tex.h;
  1782. scroll_x -= canvas_w * 9 / 10;
  1783. }
  1784. }
  1785. else
  1786. {
  1787. scroll_y -= canvas_h * 9 / 10;
  1788. }
  1789. }
  1790. static void smart_move_forward(void)
  1791. {
  1792. int slop_x = page_tex.w / 20;
  1793. int slop_y = page_tex.h / 20;
  1794. if (scroll_y + canvas_h >= page_tex.h - slop_y)
  1795. {
  1796. if (scroll_x + canvas_w >= page_tex.w - slop_x)
  1797. {
  1798. fz_location next = fz_next_page(ctx, doc, currentpage);
  1799. if (!eqloc(currentpage, next))
  1800. {
  1801. scroll_x = 0;
  1802. scroll_y = 0;
  1803. currentpage = next;
  1804. }
  1805. }
  1806. else
  1807. {
  1808. scroll_y = 0;
  1809. scroll_x += canvas_w * 9 / 10;
  1810. }
  1811. }
  1812. else
  1813. {
  1814. scroll_y += canvas_h * 9 / 10;
  1815. }
  1816. }
  1817. static void clear_search(void)
  1818. {
  1819. showsearch = 0;
  1820. search_page = currentpage;
  1821. search_hit_page = fz_make_location(-1, -1);
  1822. search_hit_count = 0;
  1823. }
  1824. #if FZ_ENABLE_JS
  1825. #define MAX_CONSOLE_LINES 500
  1826. static fz_buffer *console_buffer;
  1827. static int console_scroll = 0;
  1828. static int console_sticky = 1;
  1829. static int console_lines = 0;
  1830. static struct readline console_readline;
  1831. static void (*warning_callback)(void *, const char *) = NULL;
  1832. static void (*error_callback)(void *, const char *) = NULL;
  1833. static void *warning_user = NULL;
  1834. static void *error_user = NULL;
  1835. static void
  1836. remove_oldest_console_line()
  1837. {
  1838. unsigned char *s;
  1839. size_t size = fz_buffer_storage(ctx, console_buffer, &s);
  1840. unsigned char *p = s;
  1841. unsigned char *e = s + size;
  1842. while (p < e && *p != '\n')
  1843. p++;
  1844. if (p < e && *p == '\n')
  1845. {
  1846. p++;
  1847. memmove(s, p, e - p);
  1848. fz_resize_buffer(ctx, console_buffer, e - p);
  1849. console_lines--;
  1850. }
  1851. }
  1852. static void
  1853. gl_js_console_write(void *user, const char *message)
  1854. {
  1855. const char *p = NULL;
  1856. if (message == NULL)
  1857. return;
  1858. p = message;
  1859. while (*p)
  1860. {
  1861. if (*p == '\n')
  1862. console_lines++;
  1863. if (console_lines >= MAX_CONSOLE_LINES)
  1864. remove_oldest_console_line();
  1865. if (*p)
  1866. fz_append_byte(ctx, console_buffer, *p);
  1867. p++;
  1868. }
  1869. }
  1870. static void
  1871. gl_js_console_show(void *user)
  1872. {
  1873. if (showconsole)
  1874. return;
  1875. showconsole = 1;
  1876. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1877. shrinkwrap();
  1878. ui.focus = &console_readline;
  1879. }
  1880. static void
  1881. gl_js_console_hide(void *user)
  1882. {
  1883. if (!showconsole)
  1884. return;
  1885. showconsole = 0;
  1886. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1887. shrinkwrap();
  1888. ui.focus = NULL;
  1889. }
  1890. static void
  1891. gl_js_console_clear(void *user)
  1892. {
  1893. fz_resize_buffer(ctx, console_buffer, 0);
  1894. console_lines = 0;
  1895. }
  1896. static void console_warn(void *user, const char *message)
  1897. {
  1898. gl_js_console_write(ctx, "\nwarning: ");
  1899. gl_js_console_write(ctx, message);
  1900. if (warning_callback)
  1901. warning_callback(warning_user, message);
  1902. }
  1903. static void console_err(void *user, const char *message)
  1904. {
  1905. gl_js_console_write(ctx, "\nerror: ");
  1906. gl_js_console_write(ctx, message);
  1907. if (error_callback)
  1908. error_callback(error_user, message);
  1909. }
  1910. static void console_init(void)
  1911. {
  1912. ui_readline_init(&console_readline, NULL);
  1913. console_buffer = fz_new_buffer(ctx, 0);
  1914. fz_append_printf(ctx, console_buffer, "Welcome to MuPDF %s with MuJS %d.%d.%d",
  1915. FZ_VERSION,
  1916. JS_VERSION_MAJOR, JS_VERSION_MINOR, JS_VERSION_PATCH);
  1917. warning_callback = fz_warning_callback(ctx, &warning_user);
  1918. fz_set_warning_callback(ctx, console_warn, NULL);
  1919. error_callback = fz_error_callback(ctx, &error_user);
  1920. fz_set_error_callback(ctx, console_err, NULL);
  1921. }
  1922. static void console_fin(void)
  1923. {
  1924. fz_set_warning_callback(ctx, warning_callback, warning_user);
  1925. fz_set_error_callback(ctx, error_callback, error_user);
  1926. fz_drop_buffer(ctx, console_buffer);
  1927. console_buffer = NULL;
  1928. }
  1929. static pdf_js_console gl_js_console = {
  1930. NULL,
  1931. gl_js_console_show,
  1932. gl_js_console_hide,
  1933. gl_js_console_clear,
  1934. gl_js_console_write,
  1935. };
  1936. static void toggle_console(void)
  1937. {
  1938. showconsole = !showconsole;
  1939. if (showconsole)
  1940. ui.focus = &console_readline;
  1941. if (canvas_w == page_tex.w && canvas_h == page_tex.h)
  1942. shrinkwrap();
  1943. }
  1944. void do_console(void)
  1945. {
  1946. pdf_js_console *console = pdf_js_get_console(ctx, pdf);
  1947. char *result = NULL;
  1948. const char *accepted = NULL;
  1949. fz_var(result);
  1950. ui_layout(B, BOTH, NW, 0, 0);
  1951. ui_panel_begin(canvas_w, console_h, ui.padsize, ui.padsize, 1);
  1952. ui_layout(B, X, NW, 0, 0);
  1953. accepted = ui_readline(&console_readline, 0);
  1954. if (accepted != NULL)
  1955. {
  1956. ui.focus = &console_readline;
  1957. if (console_readline.input.text[0])
  1958. {
  1959. fz_try(ctx)
  1960. {
  1961. if (console && console->write)
  1962. {
  1963. console->write(ctx, "\n> ");
  1964. console->write(ctx, console_readline.input.text);
  1965. }
  1966. pdf_js_execute(pdf ? pdf->js : NULL, "console", console_readline.input.text, &result);
  1967. if (result && console && console->write)
  1968. {
  1969. console->write(ctx, "\n");
  1970. console->write(ctx, result);
  1971. }
  1972. }
  1973. fz_always(ctx)
  1974. fz_free(ctx, result);
  1975. fz_catch(ctx)
  1976. {
  1977. if (console)
  1978. {
  1979. console->write(ctx, "\nError: ");
  1980. console->write(ctx, fz_caught_message(ctx));
  1981. fz_report_error(ctx);
  1982. }
  1983. }
  1984. fz_flush_warnings(ctx);
  1985. ui_input_init(&console_readline.input, "");
  1986. }
  1987. }
  1988. ui_layout(ALL, BOTH, NW, ui.padsize, ui.padsize);
  1989. // White background!
  1990. glColorHex(0xF5F5F5);
  1991. glRectf(ui.cavity->x0, ui.cavity->y0, ui.cavity->x1, ui.cavity->y1);
  1992. char *console_string = (char *) fz_string_from_buffer(ctx, console_buffer);
  1993. ui_label_with_scrollbar(console_string, 0, 10, &console_scroll, &console_sticky);
  1994. ui_panel_end();
  1995. }
  1996. #endif
  1997. static void do_app(void)
  1998. {
  1999. if (ui.mod == GLUT_ACTIVE_ALT)
  2000. {
  2001. if (ui.key == KEY_F4)
  2002. quit();
  2003. if (ui.key == KEY_LEFT)
  2004. ui.key = 't', ui.mod = 0, ui.plain = 1;
  2005. if (ui.key == KEY_RIGHT)
  2006. ui.key = 'T', ui.mod = 0, ui.plain = 1;
  2007. }
  2008. if (trace_file && ui.key == KEY_CTL_P)
  2009. trace_save_snapshot();
  2010. if (!ui.focus && ui.key && ui.plain)
  2011. {
  2012. switch (ui.key)
  2013. {
  2014. case KEY_ESCAPE: clear_search(); ui_select_annot(NULL); break;
  2015. case KEY_F1: ui.dialog = help_dialog; break;
  2016. case 'a': toggle_annotate(ANNOTATE_MODE_NORMAL); break;
  2017. case 'R': toggle_annotate(ANNOTATE_MODE_REDACT); break;
  2018. case 'o': toggle_outline(); break;
  2019. case 'u': toggle_undo(); break;
  2020. case 'Y': toggle_layers(); break;
  2021. case 'L': showlinks = !showlinks; break;
  2022. case 'F': showform = !showform; break;
  2023. case 'i': ui.dialog = info_dialog; break;
  2024. #if FZ_ENABLE_JS
  2025. case '`': case KEY_F12: toggle_console(); break;
  2026. #endif
  2027. case 'r': reload(); break;
  2028. case 'q': quit(); break;
  2029. case 'S': do_save_pdf_file(); break;
  2030. case '>': layout_em = number > 0 ? number : layout_em + 1; relayout(); break;
  2031. case '<': layout_em = number > 0 ? number : layout_em - 1; relayout(); break;
  2032. case 'C': currenttint = !currenttint; break;
  2033. case 'I': currentinvert = !currentinvert; break;
  2034. case 'e': currentseparations = !currentseparations; break;
  2035. case 'E': currenticc = !currenticc; break;
  2036. case 'f': toggle_fullscreen(); break;
  2037. case 'w': shrinkwrap(); break;
  2038. case 'W': auto_zoom_w(); break;
  2039. case 'H': auto_zoom_h(); break;
  2040. case 'Z': auto_zoom(); break;
  2041. case 'z': set_zoom(number > 0 ? number : DEFRES, canvas_w/2, canvas_h/2); break;
  2042. case '+': set_zoom(zoom_in(currentzoom), ui.x, ui.y); break;
  2043. case '-': set_zoom(zoom_out(currentzoom), ui.x, ui.y); break;
  2044. case '[': currentrotate -= 90; break;
  2045. case ']': currentrotate += 90; break;
  2046. case 'k': case KEY_UP: scroll_y -= canvas_h/10; break;
  2047. case 'j': case KEY_DOWN: scroll_y += canvas_h/10; break;
  2048. case 'h': case KEY_LEFT: scroll_x -= canvas_w/10; break;
  2049. case 'l': case KEY_RIGHT: scroll_x += canvas_w/10; break;
  2050. case 'b': number = fz_maxi(number, 1); while (number--) smart_move_backward(); break;
  2051. case ' ': number = fz_maxi(number, 1); while (number--) smart_move_forward(); break;
  2052. case 'g': jump_to_page(number - 1); break;
  2053. case 'G': jump_to_location(fz_last_page(ctx, doc)); break;
  2054. case ',': case KEY_PAGE_UP:
  2055. number = fz_maxi(number, 1);
  2056. while (number--)
  2057. currentpage = fz_previous_page(ctx, doc, currentpage);
  2058. break;
  2059. case '.': case KEY_PAGE_DOWN:
  2060. number = fz_maxi(number, 1);
  2061. while (number--)
  2062. currentpage = fz_next_page(ctx, doc, currentpage);
  2063. break;
  2064. case 'A':
  2065. if (number == 0)
  2066. currentaa = (currentaa == 8 ? 0 : 8);
  2067. else
  2068. currentaa = number;
  2069. break;
  2070. case 'B':
  2071. currentbox += 1;
  2072. if (currentbox >= FZ_UNKNOWN_BOX)
  2073. currentbox = FZ_MEDIA_BOX;
  2074. break;
  2075. case 'm':
  2076. if (number == 0)
  2077. push_history();
  2078. else if (number > 0 && number < (int)nelem(marks))
  2079. marks[number] = save_mark();
  2080. break;
  2081. case 't':
  2082. if (number == 0)
  2083. {
  2084. if (history_count > 0)
  2085. pop_history();
  2086. }
  2087. else if (number > 0 && number < (int)nelem(marks))
  2088. {
  2089. struct mark mark = marks[number];
  2090. restore_mark(mark);
  2091. jump_to_location(mark.loc);
  2092. }
  2093. break;
  2094. case 'T':
  2095. if (number == 0)
  2096. {
  2097. if (future_count > 0)
  2098. pop_future();
  2099. }
  2100. break;
  2101. case '/':
  2102. clear_search();
  2103. search_dir = 1;
  2104. showsearch = 1;
  2105. ui.focus = &search_input;
  2106. search_input.p = search_input.text;
  2107. search_input.q = search_input.end;
  2108. break;
  2109. case '?':
  2110. clear_search();
  2111. search_dir = -1;
  2112. showsearch = 1;
  2113. ui.focus = &search_input;
  2114. search_input.p = search_input.text;
  2115. search_input.q = search_input.end;
  2116. break;
  2117. case 'N':
  2118. search_dir = -1;
  2119. search_active = !!search_needle;
  2120. if (eqloc(search_hit_page, currentpage))
  2121. {
  2122. if (is_first_page(search_page))
  2123. search_active = 0;
  2124. else
  2125. search_page = fz_previous_page(ctx, doc, currentpage);
  2126. }
  2127. else
  2128. {
  2129. search_page = currentpage;
  2130. }
  2131. search_hit_page = fz_make_location(-1, -1);
  2132. break;
  2133. case 'n':
  2134. search_dir = 1;
  2135. search_active = !!search_needle;
  2136. if (eqloc(search_hit_page, currentpage))
  2137. {
  2138. if (is_last_page(search_page))
  2139. search_active = 0;
  2140. else
  2141. search_page = fz_next_page(ctx, doc, currentpage);
  2142. }
  2143. else
  2144. {
  2145. search_page = currentpage;
  2146. }
  2147. search_hit_page = fz_make_location(-1, -1);
  2148. break;
  2149. default:
  2150. if (ui.key < '0' || ui.key > '9')
  2151. {
  2152. number = 0;
  2153. return; /* unrecognized key, pass it through */
  2154. }
  2155. }
  2156. if (ui.key >= '0' && ui.key <= '9')
  2157. number = number * 10 + ui.key - '0';
  2158. else
  2159. number = 0;
  2160. currentpage = fz_clamp_location(ctx, doc, currentpage);
  2161. while (currentrotate < 0) currentrotate += 360;
  2162. while (currentrotate >= 360) currentrotate -= 360;
  2163. if (!eqloc(search_hit_page, currentpage))
  2164. search_hit_page = fz_make_location(-1, -1); /* clear highlights when navigating */
  2165. ui.key = 0; /* we ate the key event, so zap it */
  2166. }
  2167. }
  2168. typedef struct
  2169. {
  2170. int max;
  2171. int len;
  2172. pdf_obj **sig;
  2173. } sigs_list;
  2174. static void
  2175. process_sigs(fz_context *ctx_, pdf_obj *field, void *arg, pdf_obj **ft)
  2176. {
  2177. sigs_list *sigs = (sigs_list *)arg;
  2178. if (!pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Type)), PDF_NAME(Annot)) ||
  2179. !pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Subtype)), PDF_NAME(Widget)) ||
  2180. !pdf_name_eq(ctx, *ft, PDF_NAME(Sig)))
  2181. return;
  2182. if (sigs->len == sigs->max)
  2183. {
  2184. int newsize = sigs->max * 2;
  2185. if (newsize == 0)
  2186. newsize = 4;
  2187. sigs->sig = fz_realloc_array(ctx, sigs->sig, newsize, pdf_obj *);
  2188. sigs->max = newsize;
  2189. }
  2190. sigs->sig[sigs->len++] = field;
  2191. }
  2192. static char *short_signature_error_desc(pdf_signature_error err)
  2193. {
  2194. switch (err)
  2195. {
  2196. case PDF_SIGNATURE_ERROR_OKAY:
  2197. return "OK";
  2198. case PDF_SIGNATURE_ERROR_NO_SIGNATURES:
  2199. return "No signatures";
  2200. case PDF_SIGNATURE_ERROR_NO_CERTIFICATE:
  2201. return "No certificate";
  2202. case PDF_SIGNATURE_ERROR_DIGEST_FAILURE:
  2203. return "Invalid";
  2204. case PDF_SIGNATURE_ERROR_SELF_SIGNED:
  2205. return "Self-signed";
  2206. case PDF_SIGNATURE_ERROR_SELF_SIGNED_IN_CHAIN:
  2207. return "Self-signed in chain";
  2208. case PDF_SIGNATURE_ERROR_NOT_TRUSTED:
  2209. return "Untrusted";
  2210. case PDF_SIGNATURE_ERROR_NOT_SIGNED:
  2211. return "Not signed";
  2212. default:
  2213. case PDF_SIGNATURE_ERROR_UNKNOWN:
  2214. return "Unknown error";
  2215. }
  2216. }
  2217. const char *format_date(int64_t secs64)
  2218. {
  2219. static char buf[100];
  2220. #ifdef _POSIX_SOURCE
  2221. struct tm tmbuf, *tm;
  2222. #else
  2223. struct tm *tm;
  2224. #endif
  2225. time_t secs = (time_t)secs64;
  2226. if (secs <= 0)
  2227. return NULL;
  2228. #ifdef _POSIX_SOURCE
  2229. tm = gmtime_r(&secs, &tmbuf);
  2230. #else
  2231. tm = gmtime(&secs);
  2232. #endif
  2233. if (!tm)
  2234. return NULL;
  2235. strftime(buf, sizeof buf, "%Y-%m-%d %H:%M UTC", tm);
  2236. return buf;
  2237. }
  2238. static fz_buffer *format_info_text()
  2239. {
  2240. fz_buffer *out = fz_new_buffer(ctx, 4096);
  2241. pdf_document *pdoc = pdf_specifics(ctx, doc);
  2242. sigs_list list = { 0, 0, NULL };
  2243. char buf[100];
  2244. if (pdoc)
  2245. {
  2246. static pdf_obj *ft_list[2] = { PDF_NAME(FT), NULL };
  2247. pdf_obj *ft = NULL;
  2248. pdf_obj *form_fields = pdf_dict_getp(ctx, pdf_trailer(ctx, pdoc), "Root/AcroForm/Fields");
  2249. pdf_walk_tree(ctx, form_fields, PDF_NAME(Kids), process_sigs, NULL, &list, &ft_list[0], &ft);
  2250. }
  2251. fz_append_printf(ctx, out, "File: %s\n\n", filename);
  2252. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_TITLE, buf, sizeof buf) > 0)
  2253. fz_append_printf(ctx, out, "Title: %s\n", buf);
  2254. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_AUTHOR, buf, sizeof buf) > 0)
  2255. fz_append_printf(ctx, out, "Author: %s\n", buf);
  2256. if (fz_lookup_metadata(ctx, doc, FZ_META_FORMAT, buf, sizeof buf) > 0)
  2257. fz_append_printf(ctx, out, "Format: %s\n", buf);
  2258. if (fz_lookup_metadata(ctx, doc, FZ_META_ENCRYPTION, buf, sizeof buf) > 0)
  2259. fz_append_printf(ctx, out, "Encryption: %s\n", buf);
  2260. fz_append_string(ctx, out, "\n");
  2261. if (pdoc)
  2262. {
  2263. int updates = pdf_count_versions(ctx, pdoc);
  2264. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_CREATOR, buf, sizeof buf) > 0)
  2265. fz_append_printf(ctx, out, "PDF Creator: %s\n", buf);
  2266. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_PRODUCER, buf, sizeof buf) > 0)
  2267. fz_append_printf(ctx, out, "PDF Producer: %s\n", buf);
  2268. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_SUBJECT, buf, sizeof buf) > 0)
  2269. fz_append_printf(ctx, out, "Subject: %s\n", buf);
  2270. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_KEYWORDS, buf, sizeof buf) > 0)
  2271. fz_append_printf(ctx, out, "Keywords: %s\n", buf);
  2272. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_CREATIONDATE, buf, sizeof buf) > 0)
  2273. {
  2274. const char *s = format_date(pdf_parse_date(ctx, buf));
  2275. if (s)
  2276. fz_append_printf(ctx, out, "Creation date: %s\n", s);
  2277. }
  2278. if (fz_lookup_metadata(ctx, doc, FZ_META_INFO_MODIFICATIONDATE, buf, sizeof buf) > 0)
  2279. {
  2280. const char *s = format_date(pdf_parse_date(ctx, buf));
  2281. if (s)
  2282. fz_append_printf(ctx, out, "Modification date: %s\n", s);
  2283. }
  2284. buf[0] = 0;
  2285. if (fz_has_permission(ctx, doc, FZ_PERMISSION_PRINT))
  2286. fz_strlcat(buf, "print, ", sizeof buf);
  2287. if (fz_has_permission(ctx, doc, FZ_PERMISSION_COPY))
  2288. fz_strlcat(buf, "copy, ", sizeof buf);
  2289. if (fz_has_permission(ctx, doc, FZ_PERMISSION_EDIT))
  2290. fz_strlcat(buf, "edit, ", sizeof buf);
  2291. if (fz_has_permission(ctx, doc, FZ_PERMISSION_ANNOTATE))
  2292. fz_strlcat(buf, "annotate, ", sizeof buf);
  2293. if (fz_has_permission(ctx, doc, FZ_PERMISSION_FORM))
  2294. fz_strlcat(buf, "form, ", sizeof buf);
  2295. if (fz_has_permission(ctx, doc, FZ_PERMISSION_ACCESSIBILITY))
  2296. fz_strlcat(buf, "accessibility, ", sizeof buf);
  2297. if (fz_has_permission(ctx, doc, FZ_PERMISSION_ASSEMBLE))
  2298. fz_strlcat(buf, "assemble, ", sizeof buf);
  2299. if (fz_has_permission(ctx, doc, FZ_PERMISSION_PRINT_HQ))
  2300. fz_strlcat(buf, "print-hq, ", sizeof buf);
  2301. if (strlen(buf) > 2)
  2302. buf[strlen(buf)-2] = 0;
  2303. else
  2304. fz_strlcat(buf, "none", sizeof buf);
  2305. fz_append_printf(ctx, out, "Permissions: %s\n", buf);
  2306. fz_append_printf(ctx, out, "PDF %sdocument with %d update%s\n",
  2307. pdf_doc_was_linearized(ctx, pdoc) ? "linearized " : "",
  2308. updates, updates > 1 ? "s" : "");
  2309. if (updates > 0)
  2310. {
  2311. int n = pdf_validate_change_history(ctx, pdoc);
  2312. if (n == 0)
  2313. fz_append_printf(ctx, out, "Change history seems valid.\n");
  2314. else if (n == 1)
  2315. fz_append_printf(ctx, out, "Invalid changes made to the document in the last update.\n");
  2316. else if (n == 2)
  2317. fz_append_printf(ctx, out, "Invalid changes made to the document in the penultimate update.\n");
  2318. else
  2319. fz_append_printf(ctx, out, "Invalid changes made to the document %d updates ago.\n", n);
  2320. }
  2321. if (list.len)
  2322. {
  2323. int i;
  2324. for (i = 0; i < list.len; i++)
  2325. {
  2326. pdf_obj *field = list.sig[i];
  2327. fz_try(ctx)
  2328. {
  2329. if (pdf_signature_is_signed(ctx, pdf, field))
  2330. {
  2331. pdf_pkcs7_verifier *verifier = pkcs7_openssl_new_verifier(ctx);
  2332. pdf_signature_error sig_cert_error = pdf_check_certificate(ctx, verifier, pdf, field);
  2333. pdf_signature_error sig_digest_error = pdf_check_digest(ctx, verifier, pdf, field);
  2334. fz_append_printf(ctx, out, "Signature %d: CERT: %s, DIGEST: %s%s\n", i+1,
  2335. short_signature_error_desc(sig_cert_error),
  2336. short_signature_error_desc(sig_digest_error),
  2337. pdf_signature_incremental_change_since_signing(ctx, pdf, field) ? ", Changed since": "");
  2338. pdf_drop_verifier(ctx, verifier);
  2339. }
  2340. else
  2341. fz_append_printf(ctx, out, "Signature %d: Unsigned\n", i+1);
  2342. }
  2343. fz_catch(ctx)
  2344. fz_append_printf(ctx, out, "Signature %d: Error\n", i+1);
  2345. }
  2346. fz_free(ctx, list.sig);
  2347. if (updates == 0)
  2348. fz_append_printf(ctx, out, "No updates since document creation\n");
  2349. else
  2350. {
  2351. int n = pdf_validate_change_history(ctx, pdf);
  2352. if (n == 0)
  2353. fz_append_printf(ctx, out, "Document changes conform to permissions\n");
  2354. else
  2355. fz_append_printf(ctx, out, "Document permissions violated %d updates ago\n", n);
  2356. }
  2357. }
  2358. fz_append_string(ctx, out, "\n");
  2359. }
  2360. fz_append_printf(ctx, out, "Page: %d / %d\n", fz_page_number_from_location(ctx, doc, currentpage)+1, fz_count_pages(ctx, doc));
  2361. fz_append_printf(ctx, out, "Page Label: %s\n", fz_page_label(ctx, fzpage, buf, sizeof buf));
  2362. {
  2363. int w = (int)(page_bounds.x1 - page_bounds.x0 + 0.5f);
  2364. int h = (int)(page_bounds.y1 - page_bounds.y0 + 0.5f);
  2365. const char *size = paper_size_name(w, h);
  2366. if (!size)
  2367. size = paper_size_name(h, w);
  2368. if (size)
  2369. fz_append_printf(ctx, out, "Size: %d x %d (%s - %s)\n", w, h, fz_string_from_box_type(currentbox), size);
  2370. else
  2371. fz_append_printf(ctx, out, "Size: %d x %d (%s)\n", w, h, fz_string_from_box_type(currentbox));
  2372. }
  2373. fz_append_printf(ctx, out, "ICC rendering: %s.\n", currenticc ? "on" : "off");
  2374. fz_append_printf(ctx, out, "Spot rendering: %s.\n", currentseparations ? "on" : "off");
  2375. if (fz_is_document_reflowable(ctx, doc))
  2376. fz_append_printf(ctx, out, "Em size: %g\n", layout_em);
  2377. return out;
  2378. }
  2379. static void do_canvas(void)
  2380. {
  2381. static int saved_scroll_x = 0;
  2382. static int saved_scroll_y = 0;
  2383. static int saved_ui_x = 0;
  2384. static int saved_ui_y = 0;
  2385. fz_irect area;
  2386. int page_x, page_y;
  2387. tooltip = NULL;
  2388. ui_layout(ALL, BOTH, NW, 0, 0);
  2389. ui_pack_push(area = ui_pack(0, 0));
  2390. glScissor(area.x0, ui.window_h-area.y1, area.x1-area.x0, area.y1-area.y0);
  2391. glEnable(GL_SCISSOR_TEST);
  2392. canvas_x = area.x0;
  2393. canvas_y = area.y0;
  2394. canvas_w = area.x1 - area.x0;
  2395. canvas_h = area.y1 - area.y0;
  2396. if (ui_mouse_inside(area))
  2397. {
  2398. ui.hot = doc;
  2399. if (!ui.active && ui.middle)
  2400. {
  2401. ui.active = doc;
  2402. saved_scroll_x = scroll_x;
  2403. saved_scroll_y = scroll_y;
  2404. saved_ui_x = ui.x;
  2405. saved_ui_y = ui.y;
  2406. }
  2407. }
  2408. if (ui.hot == doc)
  2409. {
  2410. if (ui.mod == 0)
  2411. {
  2412. scroll_x -= ui.scroll_x * ui.lineheight * 3;
  2413. scroll_y -= ui.scroll_y * ui.lineheight * 3;
  2414. }
  2415. else if (ui.mod == GLUT_ACTIVE_CTRL)
  2416. {
  2417. if (ui.scroll_y > 0) set_zoom(zoom_in(currentzoom), ui.x, ui.y);
  2418. if (ui.scroll_y < 0) set_zoom(zoom_out(currentzoom), ui.x, ui.y);
  2419. }
  2420. }
  2421. render_page_if_changed();
  2422. if (ui.active == doc)
  2423. {
  2424. scroll_x = saved_scroll_x + saved_ui_x - ui.x;
  2425. scroll_y = saved_scroll_y + saved_ui_y - ui.y;
  2426. }
  2427. if (page_tex.w <= canvas_w)
  2428. {
  2429. scroll_x = 0;
  2430. page_x = canvas_x + (canvas_w - page_tex.w) / 2;
  2431. }
  2432. else
  2433. {
  2434. scroll_x = fz_clamp(scroll_x, 0, page_tex.w - canvas_w);
  2435. page_x = canvas_x - scroll_x;
  2436. }
  2437. if (page_tex.h <= canvas_h)
  2438. {
  2439. scroll_y = 0;
  2440. page_y = canvas_y + (canvas_h - page_tex.h) / 2;
  2441. }
  2442. else
  2443. {
  2444. scroll_y = fz_clamp(scroll_y, 0, page_tex.h - canvas_h);
  2445. page_y = canvas_y - scroll_y;
  2446. }
  2447. view_page_ctm = draw_page_ctm;
  2448. view_page_ctm.e += page_x;
  2449. view_page_ctm.f += page_y;
  2450. view_page_inv_ctm = fz_invert_matrix(view_page_ctm);
  2451. view_page_bounds = fz_transform_rect(page_bounds, view_page_ctm);
  2452. view_page_area = fz_irect_from_rect(view_page_bounds);
  2453. ui_draw_image(&page_tex, page_x, page_y);
  2454. if (search_active)
  2455. {
  2456. int chapters = fz_count_chapters(ctx, doc);
  2457. ui_layout(T, X, NW, 0, 0);
  2458. ui_panel_begin(0, ui.gridsize + ui.padsize*4, ui.padsize*2, ui.padsize*2, 1);
  2459. ui_layout(L, NONE, W, ui.padsize, 0);
  2460. if (chapters == 1 && search_page.chapter == 0)
  2461. ui_label("Searching page %d...", search_page.page);
  2462. else
  2463. ui_label("Searching chapter %d page %d...", search_page.chapter, search_page.page);
  2464. ui_panel_end();
  2465. }
  2466. else
  2467. {
  2468. if (pdf)
  2469. {
  2470. do_annotate_canvas(area);
  2471. do_widget_canvas(area);
  2472. }
  2473. do_links(links);
  2474. do_page_selection();
  2475. if (eqloc(search_hit_page, currentpage) && search_hit_count > 0)
  2476. do_search_hits();
  2477. }
  2478. if (showsearch)
  2479. {
  2480. ui_layout(T, X, NW, 0, 0);
  2481. ui_panel_begin(0, ui.gridsize + ui.padsize*4, ui.padsize*2, ui.padsize*2, 1);
  2482. ui_layout(L, NONE, W, ui.padsize, 0);
  2483. ui_label("Search:");
  2484. ui_layout(ALL, X, E, ui.padsize, 0);
  2485. if (ui_input(&search_input, 0, 1) == UI_INPUT_ACCEPT)
  2486. {
  2487. showsearch = 0;
  2488. search_page = fz_make_location(-1, -1);
  2489. if (search_needle)
  2490. {
  2491. fz_free(ctx, search_needle);
  2492. search_needle = NULL;
  2493. }
  2494. if (search_input.end > search_input.text)
  2495. {
  2496. search_needle = fz_strdup(ctx, search_input.text);
  2497. search_active = 1;
  2498. search_page = currentpage;
  2499. }
  2500. }
  2501. if (ui.focus != &search_input)
  2502. showsearch = 0;
  2503. ui_panel_end();
  2504. }
  2505. if (tooltip)
  2506. {
  2507. ui_layout(B, X, N, 0, 0);
  2508. ui_panel_begin(0, ui.gridsize, ui.padsize*2, ui.padsize*2, 1);
  2509. ui_layout(L, NONE, W, ui.padsize, 0);
  2510. ui_label("%s", tooltip);
  2511. ui_panel_end();
  2512. }
  2513. ui_pack_pop();
  2514. glDisable(GL_SCISSOR_TEST);
  2515. }
  2516. void do_main(void)
  2517. {
  2518. if (search_active)
  2519. {
  2520. int start_time = glutGet(GLUT_ELAPSED_TIME);
  2521. if (ui.key == KEY_ESCAPE)
  2522. search_active = 0;
  2523. /* ignore events during search */
  2524. ui.key = ui.mod = ui.plain = 0;
  2525. ui.down = ui.middle = ui.right = 0;
  2526. while (search_active && glutGet(GLUT_ELAPSED_TIME) < start_time + 200)
  2527. {
  2528. search_hit_count = fz_search_chapter_page_number(ctx, doc,
  2529. search_page.chapter, search_page.page,
  2530. search_needle,
  2531. NULL, search_hit_quads, nelem(search_hit_quads));
  2532. trace_action("hits = doc.loadPage(%d).search(%q);\n", fz_page_number_from_location(ctx, doc, search_page), search_needle);
  2533. trace_action("print('Search page %d:', repr(%q), hits.length, repr(hits));\n", fz_page_number_from_location(ctx, doc, search_page), search_needle);
  2534. if (search_hit_count)
  2535. {
  2536. float search_hit_x = search_hit_quads[0].ul.x;
  2537. float search_hit_y = search_hit_quads[0].ul.y;
  2538. search_active = 0;
  2539. search_hit_page = search_page;
  2540. jump_to_location_xy(search_hit_page, search_hit_x, search_hit_y);
  2541. }
  2542. else
  2543. {
  2544. if (search_dir > 0)
  2545. {
  2546. if (is_last_page(search_page))
  2547. search_active = 0;
  2548. else
  2549. search_page = fz_next_page(ctx, doc, search_page);
  2550. }
  2551. else
  2552. {
  2553. if (is_first_page(search_page))
  2554. search_active = 0;
  2555. else
  2556. search_page = fz_previous_page(ctx, doc, search_page);
  2557. }
  2558. }
  2559. }
  2560. /* keep searching later */
  2561. if (search_active)
  2562. glutPostRedisplay();
  2563. }
  2564. do_app();
  2565. if (showoutline)
  2566. do_outline(outline);
  2567. else if (showundo)
  2568. do_undo();
  2569. else if (showlayers)
  2570. do_layers();
  2571. if (showoutline || showundo || showlayers)
  2572. ui_splitter(&outline_start_x, &outline_w, 6*ui.gridsize, 20*ui.gridsize, R);
  2573. if (!eqloc(oldpage, currentpage) || oldseparations != currentseparations || oldicc != currenticc)
  2574. {
  2575. load_page();
  2576. update_title();
  2577. }
  2578. if (showannotate)
  2579. {
  2580. ui_layout(R, BOTH, NW, 0, 0);
  2581. ui_panel_begin(annotate_w, 0, ui.padsize*2, ui.padsize*2, 1);
  2582. if (showannotate == ANNOTATE_MODE_NORMAL)
  2583. do_annotate_panel();
  2584. else
  2585. do_redact_panel();
  2586. ui_panel_end();
  2587. }
  2588. #if FZ_ENABLE_JS
  2589. if (showconsole)
  2590. {
  2591. do_console();
  2592. ui_splitter(&console_start_y, &console_h, 6*ui.lineheight, 25*ui.lineheight, T);
  2593. }
  2594. #endif
  2595. do_canvas();
  2596. if (pdf)
  2597. {
  2598. if (document_shown_as_dirty != pdf_has_unsaved_changes(ctx, pdf))
  2599. update_title();
  2600. }
  2601. }
  2602. void run_main_loop(void)
  2603. {
  2604. if (currentinvert)
  2605. glClearColor(0, 0, 0, 1);
  2606. else
  2607. glClearColor(0.3f, 0.3f, 0.3f, 1);
  2608. ui_begin();
  2609. fz_try(ctx)
  2610. {
  2611. if (ui.dialog)
  2612. ui.dialog();
  2613. else
  2614. do_main();
  2615. }
  2616. fz_catch(ctx)
  2617. {
  2618. ui_show_error_dialog("%s", fz_caught_message(ctx));
  2619. fz_report_error(ctx);
  2620. }
  2621. ui_end();
  2622. }
  2623. static void usage(const char *argv0)
  2624. {
  2625. fprintf(stderr, "mupdf-gl version %s\n", FZ_VERSION);
  2626. fprintf(stderr, "usage: %s [options] document [page]\n", argv0);
  2627. fprintf(stderr, "\t-p -\tpassword\n");
  2628. fprintf(stderr, "\t-r -\tresolution\n");
  2629. fprintf(stderr, "\t-c -\tdisplay ICC profile\n");
  2630. fprintf(stderr, "\t-b -\tuse named page box (MediaBox, CropBox, BleedBox, TrimBox, or ArtBox)\n");
  2631. fprintf(stderr, "\t-I\tinvert colors\n");
  2632. fprintf(stderr, "\t-W -\tpage width for EPUB layout\n");
  2633. fprintf(stderr, "\t-H -\tpage height for EPUB layout\n");
  2634. fprintf(stderr, "\t-S -\tfont size for EPUB layout\n");
  2635. fprintf(stderr, "\t-U -\tuser style sheet for EPUB layout\n");
  2636. fprintf(stderr, "\t-X\tdisable document styles for EPUB layout\n");
  2637. fprintf(stderr, "\t-J\tdisable javascript in PDF forms\n");
  2638. fprintf(stderr, "\t-A -\tset anti-aliasing level (0-8,9,10)\n");
  2639. fprintf(stderr, "\t-B -\tset black tint color (default: 303030)\n");
  2640. fprintf(stderr, "\t-C -\tset white tint color (default: FFFFF0)\n");
  2641. fprintf(stderr, "\t-Y -\tset the UI scaling factor\n");
  2642. fprintf(stderr, "\t-R -\tenable reflow and set the text extraction options\n");
  2643. fprintf(stderr, "\t\t\texample: -R dehyphenate,preserve-images\n");
  2644. exit(1);
  2645. }
  2646. static int document_filter(const char *fname)
  2647. {
  2648. return !!fz_recognize_document(ctx, fname);
  2649. }
  2650. static void do_open_document_dialog(void)
  2651. {
  2652. if (ui_open_file(filename, "Select a document to open:"))
  2653. {
  2654. ui.dialog = NULL;
  2655. if (filename[0] == 0)
  2656. glutLeaveMainLoop();
  2657. else
  2658. {
  2659. load_document();
  2660. if (doc)
  2661. {
  2662. if (reflow_options)
  2663. reflow_document();
  2664. load_page();
  2665. shrinkwrap();
  2666. update_title();
  2667. }
  2668. }
  2669. }
  2670. }
  2671. static void cleanup(void)
  2672. {
  2673. save_history();
  2674. fz_try(ctx)
  2675. save_accelerator();
  2676. fz_catch(ctx)
  2677. fz_warn(ctx, "cannot save accelerator file");
  2678. ui_finish();
  2679. fz_drop_pixmap(ctx, page_contents);
  2680. page_contents = NULL;
  2681. #ifndef NDEBUG
  2682. if (fz_atoi(getenv("FZ_DEBUG_STORE")))
  2683. fz_debug_store(ctx, fz_stdout(ctx));
  2684. #endif
  2685. trace_action("quit(0);\n");
  2686. fz_flush_warnings(ctx);
  2687. #if FZ_ENABLE_JS
  2688. console_fin();
  2689. #endif
  2690. fz_drop_output(ctx, trace_file);
  2691. fz_drop_stext_page(ctx, page_text);
  2692. fz_drop_separations(ctx, seps);
  2693. fz_drop_link(ctx, links);
  2694. fz_drop_page(ctx, fzpage);
  2695. fz_drop_outline(ctx, outline);
  2696. fz_drop_document(ctx, doc);
  2697. fz_drop_context(ctx);
  2698. }
  2699. int reloadrequested = 0;
  2700. #ifndef _WIN32
  2701. static void signal_handler(int signal)
  2702. {
  2703. if (signal == SIGHUP)
  2704. reloadrequested = 1;
  2705. }
  2706. #endif
  2707. #ifdef _MSC_VER
  2708. int main_utf8(int argc, char **argv)
  2709. #else
  2710. int main(int argc, char **argv)
  2711. #endif
  2712. {
  2713. const char *trace_file_name = NULL;
  2714. const char *profile_name = NULL;
  2715. float scale = 0;
  2716. int c;
  2717. #ifndef _WIN32
  2718. /* Never wait for termination of child processes. */
  2719. struct sigaction arg = {
  2720. .sa_handler=SIG_IGN,
  2721. .sa_flags=SA_NOCLDWAIT
  2722. };
  2723. sigaction(SIGCHLD, &arg, NULL);
  2724. signal(SIGHUP, signal_handler);
  2725. #endif
  2726. glutInit(&argc, argv);
  2727. while ((c = fz_getopt(argc, argv, "p:r:IW:H:S:U:XJb:A:B:C:T:Y:R:c:")) != -1)
  2728. {
  2729. switch (c)
  2730. {
  2731. default: usage(argv[0]); break;
  2732. case 'p': password = fz_optarg; break;
  2733. case 'r': currentzoom = fz_atof(fz_optarg); break;
  2734. case 'c': profile_name = fz_optarg; break;
  2735. case 'I': currentinvert = !currentinvert; break;
  2736. case 'b': currentbox = fz_box_type_from_string(fz_optarg); break;
  2737. case 'W': layout_w = fz_atof(fz_optarg); break;
  2738. case 'H': layout_h = fz_atof(fz_optarg); break;
  2739. case 'S': layout_em = fz_atof(fz_optarg); break;
  2740. case 'U': layout_css = fz_optarg; break;
  2741. case 'X': layout_use_doc_css = 0; break;
  2742. case 'J': enable_js = !enable_js; break;
  2743. case 'A': currentaa = fz_atoi(fz_optarg); break;
  2744. case 'C': currenttint = 1; tint_white = strtol(fz_optarg, NULL, 16); break;
  2745. case 'B': currenttint = 1; tint_black = strtol(fz_optarg, NULL, 16); break;
  2746. case 'R': reflow_options = fz_optarg; break;
  2747. case 'T': trace_file_name = fz_optarg; break;
  2748. case 'Y': scale = fz_atof(fz_optarg); break;
  2749. }
  2750. }
  2751. screen_w = glutGet(GLUT_SCREEN_WIDTH) - SCREEN_FURNITURE_W;
  2752. screen_h = glutGet(GLUT_SCREEN_HEIGHT) - SCREEN_FURNITURE_H;
  2753. ui_init_dpi(scale);
  2754. oldzoom = currentzoom = currentzoom * ui.scale;
  2755. ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
  2756. #ifdef _WIN32
  2757. /* stderr goes nowhere. Get us a debug stream we have a chance
  2758. * of seeing. */
  2759. fz_set_stddbg(ctx, fz_stdods(ctx));
  2760. #endif
  2761. #if FZ_ENABLE_JS
  2762. console_init();
  2763. #endif
  2764. fz_register_document_handlers(ctx);
  2765. if (trace_file_name)
  2766. {
  2767. if (!strcmp(trace_file_name, "-"))
  2768. trace_file = fz_stdout(ctx);
  2769. else
  2770. trace_file = fz_new_output_with_path(ctx, trace_file_name, 0);
  2771. trace_action("var doc, page, annot, widget, widgetstr, hits, tmp;\n");
  2772. trace_action("function RegressionError() {\n");
  2773. trace_action(" var err = new Error(Array.prototype.join.call(arguments, ' '));\n");
  2774. trace_action(" err.name = 'RegressionError';\n");
  2775. trace_action(" return err;\n");
  2776. trace_action("}\n");
  2777. }
  2778. if (profile_name)
  2779. {
  2780. fz_buffer *profile_data = fz_read_file(ctx, profile_name);
  2781. profile = fz_new_icc_colorspace(ctx, FZ_COLORSPACE_RGB, 0, NULL, profile_data);
  2782. fz_drop_buffer(ctx, profile_data);
  2783. }
  2784. else
  2785. {
  2786. profile = fz_device_rgb(ctx);
  2787. }
  2788. if (layout_css)
  2789. fz_load_user_css(ctx, layout_css);
  2790. fz_set_use_document_css(ctx, layout_use_doc_css);
  2791. if (fz_optind < argc)
  2792. {
  2793. fz_strlcpy(filename, argv[fz_optind++], sizeof filename);
  2794. if (fz_optind < argc)
  2795. anchor = argv[fz_optind++];
  2796. if (fz_optind < argc)
  2797. usage(argv[0]);
  2798. fz_try(ctx)
  2799. {
  2800. page_tex.w = 600;
  2801. page_tex.h = 700;
  2802. load_document();
  2803. if (doc)
  2804. {
  2805. if (reflow_options)
  2806. reflow_document();
  2807. load_page();
  2808. }
  2809. }
  2810. fz_always(ctx)
  2811. {
  2812. float sx = 1, sy = 1;
  2813. if (screen_w > 0 && page_tex.w > screen_w)
  2814. sx = (float)screen_w / page_tex.w;
  2815. if (screen_h > 0 && page_tex.h > screen_h)
  2816. sy = (float)screen_h / page_tex.h;
  2817. if (sy < sx)
  2818. sx = sy;
  2819. if (sx < 1)
  2820. {
  2821. fz_irect area;
  2822. currentzoom *= sx;
  2823. oldzoom = currentzoom;
  2824. /* compute bounds here for initial window size */
  2825. page_bounds = fz_bound_page_box(ctx, fzpage, currentbox);
  2826. transform_page();
  2827. area = fz_irect_from_rect(draw_page_bounds);
  2828. page_tex.w = area.x1 - area.x0;
  2829. page_tex.h = area.y1 - area.y0;
  2830. }
  2831. ui_init(page_tex.w, page_tex.h, "MuPDF: Loading...");
  2832. ui_input_init(&search_input, "");
  2833. }
  2834. fz_catch(ctx)
  2835. {
  2836. ui_show_error_dialog("%s", fz_caught_message(ctx));
  2837. fz_report_error(ctx);
  2838. }
  2839. fz_try(ctx)
  2840. {
  2841. if (doc)
  2842. update_title();
  2843. }
  2844. fz_catch(ctx)
  2845. {
  2846. ui_show_error_dialog("%s", fz_caught_message(ctx));
  2847. fz_report_error(ctx);
  2848. }
  2849. }
  2850. else
  2851. {
  2852. #ifdef _WIN32
  2853. win_install();
  2854. #endif
  2855. ui_init(ui.gridsize * 26, ui.gridsize * 26, "MuPDF: Open document");
  2856. ui_input_init(&search_input, "");
  2857. ui_init_open_file(".", document_filter);
  2858. ui.dialog = do_open_document_dialog;
  2859. }
  2860. annotate_w *= ui.lineheight;
  2861. outline_w *= ui.lineheight;
  2862. #if FZ_ENABLE_JS
  2863. console_h *= ui.lineheight;
  2864. #endif
  2865. glutMainLoop();
  2866. cleanup();
  2867. return 0;
  2868. }
  2869. #ifdef _WIN32
  2870. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  2871. {
  2872. int argc;
  2873. LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
  2874. char **argv = fz_argv_from_wargv(argc, wargv);
  2875. int ret = main_utf8(argc, argv);
  2876. fz_free_argv(argc, argv);
  2877. return ret;
  2878. }
  2879. #endif