pdfapp.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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 "pdfapp.h"
  23. #include "curl_stream.h"
  24. #include "mupdf/helpers/pkcs7-openssl.h"
  25. #include <string.h>
  26. #include <limits.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #ifdef _WIN32
  30. #include <windows.h>
  31. #include <direct.h> /* for getcwd */
  32. #else
  33. #include <unistd.h> /* for getcwd */
  34. #include <sys/stat.h> /* for mkdir */
  35. #endif
  36. #define BEYOND_THRESHHOLD 40
  37. #ifndef MAX
  38. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  39. #endif
  40. static int create_accel_path(fz_context *ctx, char outname[], size_t len, int create, const char *absname, ...)
  41. {
  42. va_list args;
  43. char *s = outname;
  44. size_t z, remain = len;
  45. char *arg;
  46. va_start(args, absname);
  47. while ((arg = va_arg(args, char *)) != NULL)
  48. {
  49. z = fz_snprintf(s, remain, "%s", arg);
  50. if (z+1 > remain)
  51. goto fail; /* won't fit */
  52. if (create)
  53. (void) fz_mkdir(outname);
  54. if (!fz_is_directory(ctx, outname))
  55. goto fail; /* directory creation failed, or that dir doesn't exist! */
  56. #ifdef _WIN32
  57. s[z] = '\\';
  58. #else
  59. s[z] = '/';
  60. #endif
  61. s[z+1] = 0;
  62. s += z+1;
  63. remain -= z+1;
  64. }
  65. if (fz_snprintf(s, remain, "%s.accel", absname) >= remain)
  66. goto fail; /* won't fit */
  67. va_end(args);
  68. return 1;
  69. fail:
  70. va_end(args);
  71. return 0;
  72. }
  73. static int convert_to_accel_path(fz_context *ctx, char outname[], char *absname, size_t len, int create)
  74. {
  75. char *tmpdir;
  76. char *s;
  77. if (absname[0] == '/' || absname[0] == '\\')
  78. ++absname;
  79. s = absname;
  80. while (*s) {
  81. if (*s == '/' || *s == '\\' || *s == ':')
  82. *s = '%';
  83. ++s;
  84. }
  85. #ifdef _WIN32
  86. tmpdir = getenv("USERPROFILE");
  87. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, ".config", "mupdf", NULL))
  88. return 1; /* OK! */
  89. /* TEMP and TMP are user-specific on modern windows. */
  90. tmpdir = getenv("TEMP");
  91. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, "mupdf", NULL))
  92. return 1; /* OK! */
  93. tmpdir = getenv("TMP");
  94. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, "mupdf", NULL))
  95. return 1; /* OK! */
  96. #else
  97. tmpdir = getenv("XDG_CACHE_HOME");
  98. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, "mupdf", NULL))
  99. return 1; /* OK! */
  100. tmpdir = getenv("HOME");
  101. if (tmpdir && create_accel_path(ctx, outname, len, create, absname, tmpdir, ".cache", "mupdf", NULL))
  102. return 1; /* OK! */
  103. #endif
  104. return 0; /* Fail */
  105. }
  106. static int get_accelerator_filename(fz_context *ctx, char outname[], size_t len, const char *filename, int create)
  107. {
  108. char absname[PATH_MAX];
  109. if (!fz_realpath(filename, absname))
  110. return 0;
  111. if (!convert_to_accel_path(ctx, outname, absname, len, create))
  112. return 0;
  113. return 1;
  114. }
  115. static void save_accelerator(fz_context *ctx, fz_document *doc, const char *filename)
  116. {
  117. char absname[PATH_MAX];
  118. if (!doc)
  119. return;
  120. if (!fz_document_supports_accelerator(ctx, doc))
  121. return;
  122. if (!get_accelerator_filename(ctx, absname, sizeof(absname), filename, 1))
  123. return;
  124. fz_save_accelerator(ctx, doc, absname);
  125. }
  126. enum panning
  127. {
  128. DONT_PAN = 0,
  129. PAN_TO_TOP,
  130. PAN_TO_BOTTOM
  131. };
  132. enum
  133. {
  134. PDFAPP_OUTLINE_DEFERRED = 1,
  135. PDFAPP_OUTLINE_LOAD_NOW = 2
  136. };
  137. #ifdef HAVE_CURL
  138. static void pdfapp_sleep(int ms)
  139. {
  140. #ifdef _WIN32
  141. Sleep(ms);
  142. #else
  143. usleep(ms * 1000);
  144. #endif
  145. }
  146. #endif
  147. static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repaint, int transition, int searching);
  148. static const int zoomlist[] = {
  149. 18, 24, 36, 54, 72, 96, 120, 144, 180,
  150. 216, 288, 360, 432, 504, 576, 648, 720,
  151. 792, 864, 936, 1008, 1080, 1152
  152. };
  153. static int zoom_in(int oldres)
  154. {
  155. int i;
  156. for (i = 0; i < (int)nelem(zoomlist) - 1; ++i)
  157. if (zoomlist[i] <= oldres && zoomlist[i+1] > oldres)
  158. return zoomlist[i+1];
  159. return zoomlist[i];
  160. }
  161. static int zoom_out(int oldres)
  162. {
  163. int i;
  164. for (i = 0; i < (int)nelem(zoomlist) - 1; ++i)
  165. if (zoomlist[i] < oldres && zoomlist[i+1] >= oldres)
  166. return zoomlist[i];
  167. return zoomlist[0];
  168. }
  169. void pdfapp_warn(pdfapp_t *app, const char *fmt, ...)
  170. {
  171. char buf[1024];
  172. va_list ap;
  173. va_start(ap, fmt);
  174. fz_vsnprintf(buf, sizeof(buf), fmt, ap);
  175. va_end(ap);
  176. buf[sizeof(buf)-1] = 0;
  177. winwarn(app, buf);
  178. }
  179. void pdfapp_error(pdfapp_t *app, char *msg)
  180. {
  181. winerror(app, msg);
  182. }
  183. char *pdfapp_version(pdfapp_t *app)
  184. {
  185. return
  186. "MuPDF " FZ_VERSION "\n"
  187. "Copyright 2006-2022 Artifex Software, Inc.\n";
  188. }
  189. char *pdfapp_usage(pdfapp_t *app)
  190. {
  191. return
  192. "[\t\t-- rotate left\n"
  193. "]\t\t-- rotate right\n"
  194. "h left\t\t-- scroll left\n"
  195. "j down\t\t-- scroll down\n"
  196. "k up\t\t-- scroll up\n"
  197. "l right\t\t-- scroll right\n"
  198. "+\t\t-- zoom in\n"
  199. "-\t\t-- zoom out\n"
  200. "W\t\t-- zoom to fit window width\n"
  201. "H\t\t-- zoom to fit window height\n"
  202. "Z\t\t-- zoom to fit page\n"
  203. "z\t\t-- reset zoom\n"
  204. "<\t\t-- decrease font size (EPUB only)\n"
  205. ">\t\t-- increase font size (EPUB only)\n"
  206. "w\t\t-- shrinkwrap\n"
  207. "f\t\t-- fullscreen\n"
  208. "r\t\t-- reload file\n"
  209. ". pgdn space\t-- next page\n"
  210. ", pgup b\t-- previous page\n"
  211. "m\t\t-- mark page for snap back\n"
  212. "t\t\t-- pop back to latest mark\n"
  213. "1m\t\t-- mark page in register 1\n"
  214. "1t\t\t-- go to page in register 1\n"
  215. "G\t\t-- go to last page\n"
  216. "123g\t\t-- go to page 123\n"
  217. "/\t\t-- search forwards for text\n"
  218. "?\t\t-- search backwards for text\n"
  219. "n\t\t-- find next search result\n"
  220. "N\t\t-- find previous search result\n"
  221. "c\t\t-- toggle between color and grayscale\n"
  222. "I\t\t-- toggle inverted color mode\n"
  223. "C\t\t-- toggle tinted color mode\n"
  224. "E\t\t-- enable/disable ICC color mode\n"
  225. "e\t\t-- enable/disable spot color mode\n"
  226. "q\t\t-- quit\n"
  227. ;
  228. }
  229. void pdfapp_init(fz_context *ctx, pdfapp_t *app)
  230. {
  231. memset(app, 0, sizeof(pdfapp_t));
  232. app->scrw = 640;
  233. app->scrh = 480;
  234. app->resolution = 72;
  235. app->ctx = ctx;
  236. app->layout_w = FZ_DEFAULT_LAYOUT_W;
  237. app->layout_h = FZ_DEFAULT_LAYOUT_H;
  238. app->layout_em = FZ_DEFAULT_LAYOUT_EM;
  239. app->layout_css = NULL;
  240. app->layout_use_doc_css = 1;
  241. app->transition.duration = 0.25f;
  242. app->transition.type = FZ_TRANSITION_FADE;
  243. #ifdef _WIN32
  244. app->colorspace = fz_device_bgr(ctx);
  245. #else
  246. app->colorspace = fz_device_rgb(ctx);
  247. #endif
  248. app->tint_white = 0xFFFAF0;
  249. app->useicc = 1;
  250. app->useseparations = 0;
  251. app->aalevel = 8;
  252. }
  253. void pdfapp_setresolution(pdfapp_t *app, int res)
  254. {
  255. app->default_resolution = res;
  256. app->resolution = res;
  257. }
  258. void pdfapp_invert(pdfapp_t *app, fz_rect rect)
  259. {
  260. fz_invert_pixmap_rect(app->ctx, app->image, fz_round_rect(rect));
  261. }
  262. void pdfapp_reloadfile(pdfapp_t *app)
  263. {
  264. char filename[PATH_MAX];
  265. fz_strlcpy(filename, app->docpath, PATH_MAX);
  266. pdfapp_close(app);
  267. pdfapp_open(app, filename, 1);
  268. }
  269. static void event_cb(fz_context *ctx, pdf_document *doc, pdf_doc_event *evt, void *data)
  270. {
  271. pdfapp_t *app = (pdfapp_t *)data;
  272. switch (evt->type)
  273. {
  274. case PDF_DOCUMENT_EVENT_ALERT:
  275. {
  276. pdf_alert_event *alert = pdf_access_alert_event(ctx, evt);
  277. winalert(app, alert);
  278. }
  279. break;
  280. case PDF_DOCUMENT_EVENT_PRINT:
  281. winprint(app);
  282. break;
  283. case PDF_DOCUMENT_EVENT_EXEC_MENU_ITEM:
  284. {
  285. const char *item = pdf_access_exec_menu_item_event(ctx, evt);
  286. if (!strcmp(item, "Print"))
  287. winprint(app);
  288. else
  289. pdfapp_warn(app, "The document attempted to execute menu item: %s. (Not supported)", item);
  290. }
  291. break;
  292. case PDF_DOCUMENT_EVENT_LAUNCH_URL:
  293. {
  294. pdf_launch_url_event *launch_url = pdf_access_launch_url_event(ctx, evt);
  295. pdfapp_warn(app, "The document attempted to open url: %s. (Not supported by app)", launch_url->url);
  296. }
  297. break;
  298. case PDF_DOCUMENT_EVENT_MAIL_DOC:
  299. {
  300. pdf_mail_doc_event *mail_doc = pdf_access_mail_doc_event(ctx, evt);
  301. pdfapp_warn(app, "The document attempted to mail the document%s%s%s%s%s%s%s%s (Not supported)",
  302. mail_doc->to[0]?", To: ":"", mail_doc->to,
  303. mail_doc->cc[0]?", Cc: ":"", mail_doc->cc,
  304. mail_doc->bcc[0]?", Bcc: ":"", mail_doc->bcc,
  305. mail_doc->subject[0]?", Subject: ":"", mail_doc->subject);
  306. }
  307. break;
  308. }
  309. }
  310. void pdfapp_open(pdfapp_t *app, char *filename, int reload)
  311. {
  312. pdfapp_open_progressive(app, filename, reload, 0);
  313. }
  314. #ifdef HAVE_CURL
  315. static void
  316. pdfapp_more_data(void *app_, int complete)
  317. {
  318. pdfapp_t *app = (pdfapp_t *)app_;
  319. if (complete && app->outline_deferred == PDFAPP_OUTLINE_DEFERRED)
  320. {
  321. app->outline_deferred = PDFAPP_OUTLINE_LOAD_NOW;
  322. winreloadpage(app);
  323. }
  324. else if (app->incomplete)
  325. winreloadpage(app);
  326. }
  327. #endif
  328. static int make_fake_doc(pdfapp_t *app)
  329. {
  330. fz_context *ctx = app->ctx;
  331. pdf_document *pdf = NULL;
  332. fz_buffer *contents = NULL;
  333. pdf_obj *page_obj = NULL;
  334. fz_var(contents);
  335. fz_var(page_obj);
  336. fz_try(ctx)
  337. {
  338. fz_rect mediabox = { 0, 0, app->winw, app->winh };
  339. int i;
  340. pdf = pdf_create_document(ctx);
  341. contents = fz_new_buffer(ctx, 100);
  342. fz_append_printf(ctx, contents, "1 0 0 RG %g w 0 0 m %g %g l 0 %g m %g 0 l s\n",
  343. fz_min(mediabox.x1, mediabox.y1) / 20,
  344. mediabox.x1, mediabox.y1,
  345. mediabox.y1, mediabox.x1);
  346. /* Create enough copies of our blank(ish) page so that the
  347. * page number is preserved if and when a subsequent load
  348. * works. */
  349. page_obj = pdf_add_page(ctx, pdf, mediabox, 0, NULL, contents);
  350. for (i = 0; i < app->pagecount; i++)
  351. pdf_insert_page(ctx, pdf, -1, page_obj);
  352. }
  353. fz_always(ctx)
  354. {
  355. pdf_drop_obj(ctx, page_obj);
  356. fz_drop_buffer(ctx, contents);
  357. }
  358. fz_catch(ctx)
  359. {
  360. fz_report_error(ctx);
  361. fz_drop_document(ctx, (fz_document *) pdf);
  362. return 1;
  363. }
  364. app->doc = (fz_document*)pdf;
  365. return 0;
  366. }
  367. void pdfapp_open_progressive(pdfapp_t *app, char *filename, int reload, int kbps)
  368. {
  369. fz_context *ctx = app->ctx;
  370. char *password = "";
  371. pdf_document *idoc;
  372. fz_try(ctx)
  373. {
  374. fz_register_document_handlers(ctx);
  375. if (app->layout_css)
  376. fz_load_user_css(ctx, app->layout_css);
  377. fz_set_use_document_css(ctx, app->layout_use_doc_css);
  378. #ifdef HAVE_CURL
  379. if (!strncmp(filename, "http://", 7) || !strncmp(filename, "https://", 8))
  380. {
  381. app->stream = fz_open_url(ctx, filename, kbps, pdfapp_more_data, app);
  382. while (1)
  383. {
  384. fz_try(ctx)
  385. {
  386. fz_seek(ctx, app->stream, 0, SEEK_SET);
  387. app->doc = fz_open_document_with_stream(ctx, filename, app->stream);
  388. }
  389. fz_catch(ctx)
  390. {
  391. if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
  392. {
  393. fz_ignore_error(ctx);
  394. pdfapp_sleep(100);
  395. continue;
  396. }
  397. fz_rethrow(ctx);
  398. }
  399. break;
  400. }
  401. }
  402. else if (kbps > 0)
  403. {
  404. app->stream = fz_open_file_progressive(ctx, filename, kbps, pdfapp_more_data, app);
  405. while (1)
  406. {
  407. fz_try(ctx)
  408. {
  409. fz_seek(ctx, app->stream, 0, SEEK_SET);
  410. app->doc = fz_open_document_with_stream(ctx, filename, app->stream);
  411. }
  412. fz_catch(ctx)
  413. {
  414. if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
  415. {
  416. fz_ignore_error(ctx);
  417. pdfapp_sleep(100);
  418. continue;
  419. }
  420. fz_rethrow(ctx);
  421. }
  422. break;
  423. }
  424. }
  425. else
  426. #endif
  427. {
  428. char accelpath[PATH_MAX];
  429. char *accel = NULL;
  430. time_t atime;
  431. time_t dtime;
  432. /* If there was an accelerator to load, what would it be called? */
  433. if (get_accelerator_filename(ctx, accelpath, sizeof(accelpath), filename, 0))
  434. {
  435. /* Check whether that file exists, and isn't older than
  436. * the document. */
  437. atime = fz_stat_mtime(accelpath);
  438. dtime = fz_stat_mtime(filename);
  439. if (atime == 0)
  440. {
  441. /* No accelerator */
  442. }
  443. else if (atime > dtime)
  444. accel = accelpath;
  445. else
  446. {
  447. /* Accelerator data is out of date */
  448. #ifdef _WIN32
  449. fz_remove_utf8(accelpath);
  450. #else
  451. remove(accelpath);
  452. #endif
  453. accel = NULL; /* In case we have jumped up from below */
  454. }
  455. }
  456. app->doc = fz_open_accelerated_document(ctx, filename, accel);
  457. }
  458. }
  459. fz_catch(ctx)
  460. {
  461. fz_report_error(ctx);
  462. if (!reload || make_fake_doc(app))
  463. {
  464. fz_report_error(ctx);
  465. pdfapp_error(app, "cannot open document");
  466. }
  467. }
  468. idoc = pdf_specifics(app->ctx, app->doc);
  469. if (idoc)
  470. {
  471. fz_try(ctx)
  472. {
  473. pdf_enable_js(ctx, idoc);
  474. pdf_set_doc_event_callback(ctx, idoc, event_cb, NULL, app);
  475. }
  476. fz_catch(ctx)
  477. {
  478. fz_report_error(ctx);
  479. pdfapp_error(app, "cannot load javascript embedded in document");
  480. }
  481. }
  482. fz_try(ctx)
  483. {
  484. if (fz_needs_password(app->ctx, app->doc))
  485. {
  486. int okay = fz_authenticate_password(app->ctx, app->doc, password);
  487. while (!okay)
  488. {
  489. password = winpassword(app, filename);
  490. if (!password)
  491. fz_throw(ctx, FZ_ERROR_GENERIC, "Needs a password");
  492. okay = fz_authenticate_password(app->ctx, app->doc, password);
  493. if (!okay)
  494. pdfapp_warn(app, "Invalid password.");
  495. }
  496. }
  497. app->docpath = fz_strdup(ctx, filename);
  498. app->doctitle = fz_strdup(ctx, fz_basename(filename));
  499. fz_layout_document(app->ctx, app->doc, app->layout_w, app->layout_h, app->layout_em);
  500. while (1)
  501. {
  502. fz_try(ctx)
  503. {
  504. app->pagecount = fz_count_pages(app->ctx, app->doc);
  505. if (app->pagecount <= 0)
  506. fz_throw(ctx, FZ_ERROR_GENERIC, "No pages in document");
  507. }
  508. fz_catch(ctx)
  509. {
  510. if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
  511. {
  512. fz_ignore_error(ctx);
  513. continue;
  514. }
  515. fz_rethrow(ctx);
  516. }
  517. break;
  518. }
  519. while (1)
  520. {
  521. fz_try(ctx)
  522. {
  523. app->outline = fz_load_outline(app->ctx, app->doc);
  524. }
  525. fz_catch(ctx)
  526. {
  527. app->outline = NULL;
  528. if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
  529. {
  530. fz_ignore_error(ctx);
  531. app->outline_deferred = PDFAPP_OUTLINE_DEFERRED;
  532. }
  533. else
  534. {
  535. fz_report_error(ctx);
  536. pdfapp_warn(app, "Failed to load outline.");
  537. }
  538. }
  539. break;
  540. }
  541. }
  542. fz_catch(ctx)
  543. {
  544. fz_report_error(ctx);
  545. pdfapp_error(app, "cannot open document");
  546. }
  547. if (app->pageno < 1)
  548. app->pageno = 1;
  549. if (app->pageno > app->pagecount)
  550. app->pageno = app->pagecount;
  551. if (app->resolution < MINRES)
  552. app->resolution = MINRES;
  553. if (app->resolution > MAXRES)
  554. app->resolution = MAXRES;
  555. if (!reload)
  556. {
  557. app->shrinkwrap = 1;
  558. app->rotate = 0;
  559. app->panx = 0;
  560. app->pany = 0;
  561. }
  562. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  563. }
  564. void pdfapp_close(pdfapp_t *app)
  565. {
  566. fz_drop_display_list(app->ctx, app->page_list);
  567. app->page_list = NULL;
  568. fz_drop_display_list(app->ctx, app->annotations_list);
  569. app->annotations_list = NULL;
  570. fz_drop_separations(app->ctx, app->seps);
  571. app->seps = NULL;
  572. fz_drop_stext_page(app->ctx, app->page_text);
  573. app->page_text = NULL;
  574. fz_drop_link(app->ctx, app->page_links);
  575. app->page_links = NULL;
  576. fz_free(app->ctx, app->doctitle);
  577. app->doctitle = NULL;
  578. fz_free(app->ctx, app->docpath);
  579. app->docpath = NULL;
  580. fz_drop_pixmap(app->ctx, app->image);
  581. app->image = NULL;
  582. fz_drop_pixmap(app->ctx, app->new_image);
  583. app->new_image = NULL;
  584. fz_drop_pixmap(app->ctx, app->old_image);
  585. app->old_image = NULL;
  586. fz_drop_outline(app->ctx, app->outline);
  587. app->outline = NULL;
  588. fz_drop_page(app->ctx, app->page);
  589. app->page = NULL;
  590. fz_drop_document(app->ctx, app->doc);
  591. app->doc = NULL;
  592. #ifdef HAVE_CURL
  593. fz_drop_stream(app->ctx, app->stream);
  594. #endif
  595. fz_flush_warnings(app->ctx);
  596. }
  597. static int gen_tmp_file(char *buf, int len)
  598. {
  599. int i;
  600. char *name = strrchr(buf, '/');
  601. if (name == NULL)
  602. name = strrchr(buf, '\\');
  603. if (name != NULL)
  604. name++;
  605. else
  606. name = buf;
  607. for (i = 0; i < 10000; i++)
  608. {
  609. FILE *f;
  610. sprintf(name, "tmp%04d", i);
  611. f = fopen(buf, "r");
  612. if (f == NULL)
  613. return 1;
  614. fclose(f);
  615. }
  616. return 0;
  617. }
  618. static int pdfapp_save(pdfapp_t *app)
  619. {
  620. char buf[PATH_MAX];
  621. pdf_document *idoc = pdf_specifics(app->ctx, app->doc);
  622. if (!idoc)
  623. return 0;
  624. if (wingetsavepath(app, buf, PATH_MAX))
  625. {
  626. pdf_write_options opts = pdf_default_write_options;
  627. opts.do_incremental = pdf_can_be_saved_incrementally(app->ctx, idoc);
  628. if (strcmp(buf, app->docpath) != 0)
  629. {
  630. wincopyfile(app, app->docpath, buf);
  631. pdf_save_document(app->ctx, idoc, buf, &opts);
  632. pdfapp_close(app);
  633. pdfapp_open(app, buf, 1);
  634. return 1;
  635. }
  636. if (gen_tmp_file(buf, PATH_MAX))
  637. {
  638. int written = 0;
  639. fz_try(app->ctx)
  640. {
  641. wincopyfile(app, app->docpath, buf);
  642. pdf_save_document(app->ctx, idoc, buf, &opts);
  643. written = 1;
  644. }
  645. fz_catch(app->ctx)
  646. {
  647. fz_report_error(app->ctx);
  648. /* Ignore any error, so we drop out with
  649. * failure below. */
  650. }
  651. if (written)
  652. {
  653. char buf2[PATH_MAX];
  654. fz_strlcpy(buf2, app->docpath, PATH_MAX);
  655. pdfapp_close(app);
  656. winreplacefile(app, buf, buf2);
  657. pdfapp_open(app, buf2, 1);
  658. return written;
  659. }
  660. }
  661. }
  662. return 0;
  663. }
  664. int pdfapp_preclose(pdfapp_t *app)
  665. {
  666. pdf_document *idoc = pdf_specifics(app->ctx, app->doc);
  667. if (idoc && pdf_has_unsaved_changes(app->ctx, idoc))
  668. {
  669. switch (winsavequery(app))
  670. {
  671. case DISCARD:
  672. return 1;
  673. case CANCEL:
  674. return 0;
  675. case SAVE:
  676. return pdfapp_save(app);
  677. }
  678. }
  679. return 1;
  680. }
  681. static fz_matrix pdfapp_viewctm(pdfapp_t *app)
  682. {
  683. return fz_transform_page(app->page_bbox, app->resolution, app->rotate);
  684. }
  685. static void pdfapp_panview(pdfapp_t *app, int newx, int newy)
  686. {
  687. if (newx > 0)
  688. newx = 0;
  689. if (newy > 0)
  690. newy = 0;
  691. if (newx + app->imgw < app->winw)
  692. newx = app->winw - app->imgw;
  693. if (newy + app->imgh < app->winh)
  694. newy = app->winh - app->imgh;
  695. if (app->winw >= app->imgw)
  696. newx = (app->winw - app->imgw) / 2;
  697. if (app->winh >= app->imgh)
  698. newy = (app->winh - app->imgh) / 2;
  699. if (newx != app->panx || newy != app->pany)
  700. winrepaint(app);
  701. app->panx = newx;
  702. app->pany = newy;
  703. }
  704. static void pdfapp_loadpage(pdfapp_t *app, int no_cache)
  705. {
  706. fz_device *mdev = NULL;
  707. int errored = 0;
  708. fz_cookie cookie = { 0 };
  709. fz_var(mdev);
  710. fz_drop_display_list(app->ctx, app->page_list);
  711. fz_drop_display_list(app->ctx, app->annotations_list);
  712. fz_drop_separations(app->ctx, app->seps);
  713. fz_drop_stext_page(app->ctx, app->page_text);
  714. fz_drop_link(app->ctx, app->page_links);
  715. fz_drop_page(app->ctx, app->page);
  716. app->page_list = NULL;
  717. app->annotations_list = NULL;
  718. app->seps = NULL;
  719. app->page_text = NULL;
  720. app->page_links = NULL;
  721. app->page = NULL;
  722. app->page_bbox.x0 = 0;
  723. app->page_bbox.y0 = 0;
  724. app->page_bbox.x1 = 100;
  725. app->page_bbox.y1 = 100;
  726. app->incomplete = 0;
  727. fz_try(app->ctx)
  728. {
  729. app->page = fz_load_page(app->ctx, app->doc, app->pageno - 1);
  730. if (app->page && app->page->incomplete)
  731. app->incomplete = 1;
  732. app->page_bbox = fz_bound_page(app->ctx, app->page);
  733. app->page_links = fz_load_links(app->ctx, app->page);
  734. }
  735. fz_catch(app->ctx)
  736. {
  737. if (fz_caught(app->ctx) == FZ_ERROR_TRYLATER)
  738. {
  739. fz_ignore_error(app->ctx);
  740. app->incomplete = 1;
  741. }
  742. else
  743. {
  744. fz_report_error(app->ctx);
  745. pdfapp_warn(app, "Failed to load page.");
  746. }
  747. return;
  748. }
  749. if (app->useicc)
  750. fz_enable_icc(app->ctx);
  751. else
  752. fz_disable_icc(app->ctx);
  753. fz_set_aa_level(app->ctx, app->aalevel);
  754. if (app->useseparations)
  755. {
  756. fz_try(app->ctx)
  757. {
  758. app->seps = fz_page_separations(app->ctx, app->page);
  759. if (app->seps)
  760. {
  761. int i, n = fz_count_separations(app->ctx, app->seps);
  762. for (i = 0; i < n; i++)
  763. fz_set_separation_behavior(app->ctx, app->seps, i, FZ_SEPARATION_COMPOSITE);
  764. }
  765. else if (fz_page_uses_overprint(app->ctx, app->page))
  766. {
  767. /* This page uses overprint, so we need an empty
  768. * sep object to force the overprint simulation on. */
  769. app->seps = fz_new_separations(app->ctx, 0);
  770. }
  771. else if (fz_document_output_intent(app->ctx, app->doc))
  772. {
  773. /* We have an output intent. Force the overprint
  774. *simulation on, because this ensures that
  775. * we 'simulate' the output intent too. */
  776. app->seps = fz_new_separations(app->ctx, 0);
  777. }
  778. }
  779. fz_catch(app->ctx)
  780. {
  781. if (fz_caught(app->ctx) == FZ_ERROR_TRYLATER)
  782. {
  783. fz_ignore_error(app->ctx);
  784. app->incomplete = 1;
  785. }
  786. else
  787. {
  788. fz_report_error(app->ctx);
  789. pdfapp_warn(app, "Failed to load page.");
  790. }
  791. errored = 1;
  792. }
  793. }
  794. fz_try(app->ctx)
  795. {
  796. /* Create display lists */
  797. app->page_list = fz_new_display_list(app->ctx, fz_infinite_rect);
  798. mdev = fz_new_list_device(app->ctx, app->page_list);
  799. if (no_cache)
  800. fz_enable_device_hints(app->ctx, mdev, FZ_NO_CACHE);
  801. fz_run_page_contents(app->ctx, app->page, mdev, fz_identity, &cookie);
  802. fz_close_device(app->ctx, mdev);
  803. fz_drop_device(app->ctx, mdev);
  804. mdev = NULL;
  805. app->annotations_list = fz_new_display_list(app->ctx, fz_infinite_rect);
  806. mdev = fz_new_list_device(app->ctx, app->annotations_list);
  807. fz_run_page_annots(app->ctx, app->page, mdev, fz_identity, &cookie);
  808. fz_run_page_widgets(app->ctx, app->page, mdev, fz_identity, &cookie);
  809. if (cookie.incomplete)
  810. {
  811. app->incomplete = 1;
  812. }
  813. else if (cookie.errors)
  814. {
  815. pdfapp_warn(app, "Errors found on page.");
  816. errored = 1;
  817. }
  818. fz_close_device(app->ctx, mdev);
  819. }
  820. fz_always(app->ctx)
  821. {
  822. fz_drop_device(app->ctx, mdev);
  823. }
  824. fz_catch(app->ctx)
  825. {
  826. if (fz_caught(app->ctx) == FZ_ERROR_TRYLATER)
  827. {
  828. fz_ignore_error(app->ctx);
  829. app->incomplete = 1;
  830. }
  831. else
  832. {
  833. fz_report_error(app->ctx);
  834. pdfapp_warn(app, "Failed to load page.");
  835. }
  836. errored = 1;
  837. }
  838. app->errored = errored;
  839. }
  840. static void pdfapp_runpage(pdfapp_t *app, fz_device *dev, const fz_matrix ctm, fz_rect scissor, fz_cookie *cookie)
  841. {
  842. if (app->page_list)
  843. fz_run_display_list(app->ctx, app->page_list, dev, ctm, scissor, cookie);
  844. if (app->annotations_list)
  845. fz_run_display_list(app->ctx, app->annotations_list, dev, ctm, scissor, cookie);
  846. }
  847. #define MAX_TITLE 256
  848. void pdfapp_reloadpage(pdfapp_t *app)
  849. {
  850. if (app->outline_deferred == PDFAPP_OUTLINE_LOAD_NOW)
  851. {
  852. fz_try(app->ctx)
  853. {
  854. app->outline = fz_load_outline(app->ctx, app->doc);
  855. }
  856. fz_catch(app->ctx)
  857. {
  858. fz_report_error(app->ctx);
  859. app->outline = NULL;
  860. }
  861. app->outline_deferred = 0;
  862. }
  863. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  864. }
  865. static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repaint, int transition, int searching)
  866. {
  867. char buf[MAX_TITLE];
  868. fz_device *idev = NULL;
  869. fz_device *tdev;
  870. fz_colorspace *colorspace;
  871. fz_matrix ctm;
  872. fz_rect bounds;
  873. fz_irect ibounds;
  874. fz_cookie cookie = { 0 };
  875. if (!app->nowaitcursor)
  876. wincursor(app, WAIT);
  877. if (!app->transitions_enabled || !app->presentation_mode)
  878. transition = 0;
  879. if (transition)
  880. {
  881. app->old_image = app->image;
  882. app->image = NULL;
  883. app->imgw = 0;
  884. app->imgh = 0;
  885. }
  886. /* Always reload page if it was flagged incomplete */
  887. if (app->incomplete)
  888. loadpage = 1;
  889. if (loadpage)
  890. {
  891. fz_rect mediabox;
  892. pdfapp_loadpage(app, searching);
  893. /* Zero search hit position */
  894. app->hit_count = 0;
  895. /* Extract text */
  896. fz_try(app->ctx)
  897. mediabox = fz_bound_page(app->ctx, app->page);
  898. fz_catch(app->ctx)
  899. {
  900. fz_rethrow_unless(app->ctx, FZ_ERROR_TRYLATER);
  901. fz_ignore_error(app->ctx);
  902. mediabox = fz_make_rect(0, 0, 100, 100);
  903. app->incomplete = 1;
  904. }
  905. app->page_text = fz_new_stext_page(app->ctx, mediabox);
  906. if (app->page_list || app->annotations_list)
  907. {
  908. tdev = fz_new_stext_device(app->ctx, app->page_text, NULL);
  909. fz_try(app->ctx)
  910. {
  911. pdfapp_runpage(app, tdev, fz_identity, fz_infinite_rect, &cookie);
  912. fz_close_device(app->ctx, tdev);
  913. }
  914. fz_always(app->ctx)
  915. fz_drop_device(app->ctx, tdev);
  916. fz_catch(app->ctx)
  917. fz_rethrow(app->ctx);
  918. }
  919. }
  920. if (drawpage)
  921. {
  922. char buf2[64];
  923. size_t len;
  924. while (!winisresolutionacceptable(app, pdfapp_viewctm(app)))
  925. {
  926. app->resolution = zoom_out(app->resolution);
  927. }
  928. sprintf(buf2, " - %d/%d (%g dpi)",
  929. app->pageno, app->pagecount, app->resolution);
  930. len = MAX_TITLE-strlen(buf2);
  931. if (strlen(app->doctitle) >= len)
  932. {
  933. fz_strlcpy(buf, app->doctitle, len-3);
  934. fz_strlcat(buf, "...", MAX_TITLE);
  935. fz_strlcat(buf, buf2, MAX_TITLE);
  936. }
  937. else
  938. {
  939. fz_strlcpy(buf, app->doctitle, MAX_TITLE);
  940. fz_strlcat(buf, buf2, MAX_TITLE);
  941. }
  942. wintitle(app, buf);
  943. ctm = pdfapp_viewctm(app);
  944. bounds = fz_transform_rect(app->page_bbox, ctm);
  945. ibounds = fz_round_rect(bounds);
  946. bounds = fz_rect_from_irect(ibounds);
  947. /* Draw */
  948. fz_drop_pixmap(app->ctx, app->image);
  949. if (app->grayscale)
  950. colorspace = fz_device_gray(app->ctx);
  951. else
  952. colorspace = app->colorspace;
  953. app->image = NULL;
  954. app->imgw = 0;
  955. app->imgh = 0;
  956. fz_var(app->image);
  957. fz_var(idev);
  958. fz_try(app->ctx)
  959. {
  960. app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, ibounds, app->seps, 1);
  961. app->imgw = fz_pixmap_width(app->ctx, app->image);
  962. app->imgh = fz_pixmap_height(app->ctx, app->image);
  963. fz_clear_pixmap_with_value(app->ctx, app->image, 255);
  964. if (app->page_list || app->annotations_list)
  965. {
  966. idev = fz_new_draw_device(app->ctx, fz_identity, app->image);
  967. pdfapp_runpage(app, idev, ctm, bounds, &cookie);
  968. fz_close_device(app->ctx, idev);
  969. }
  970. if (app->invert)
  971. {
  972. fz_invert_pixmap_luminance(app->ctx, app->image);
  973. fz_gamma_pixmap(app->ctx, app->image, 1 / 1.4f);
  974. }
  975. if (app->tint)
  976. fz_tint_pixmap(app->ctx, app->image, 0, app->tint_white);
  977. }
  978. fz_always(app->ctx)
  979. fz_drop_device(app->ctx, idev);
  980. fz_catch(app->ctx)
  981. {
  982. fz_report_error(app->ctx);
  983. cookie.errors++;
  984. }
  985. }
  986. if (transition && drawpage)
  987. {
  988. app->new_image = app->image;
  989. app->image = NULL;
  990. app->imgw = 0;
  991. app->imgh = 0;
  992. if (app->grayscale)
  993. colorspace = fz_device_gray(app->ctx);
  994. else
  995. colorspace = app->colorspace;
  996. app->image = fz_new_pixmap_with_bbox(app->ctx, colorspace, ibounds, app->seps, 1);
  997. app->imgw = fz_pixmap_width(app->ctx, app->image);
  998. app->imgh = fz_pixmap_height(app->ctx, app->image);
  999. app->duration = 0;
  1000. fz_page_presentation(app->ctx, app->page, &app->transition, &app->duration);
  1001. if (app->duration == 0)
  1002. app->duration = app->presentation_time_in_seconds;
  1003. app->in_transit = fz_generate_transition(app->ctx, app->image, app->old_image, app->new_image, 0, &app->transition);
  1004. if (!app->in_transit)
  1005. {
  1006. if (app->duration != 0)
  1007. winadvancetimer(app, app->duration);
  1008. }
  1009. app->start_time = clock();
  1010. }
  1011. if (repaint)
  1012. {
  1013. pdfapp_panview(app, app->panx, app->pany);
  1014. if (!app->image)
  1015. {
  1016. /* there is no image to blit, but there might be an error message */
  1017. winresize(app, app->layout_w, app->layout_h);
  1018. }
  1019. else if (app->shrinkwrap)
  1020. {
  1021. int w = app->imgw;
  1022. int h = app->imgh;
  1023. if (app->winw == w)
  1024. app->panx = 0;
  1025. if (app->winh == h)
  1026. app->pany = 0;
  1027. if (w > app->scrw * 90 / 100)
  1028. w = app->scrw * 90 / 100;
  1029. if (h > app->scrh * 90 / 100)
  1030. h = app->scrh * 90 / 100;
  1031. if (w != app->winw || h != app->winh)
  1032. winresize(app, w, h);
  1033. }
  1034. winrepaint(app);
  1035. wincursor(app, ARROW);
  1036. }
  1037. if (cookie.errors && app->errored == 0)
  1038. {
  1039. app->errored = 1;
  1040. pdfapp_warn(app, "Errors found on page. Page rendering may be incomplete.");
  1041. }
  1042. fz_flush_warnings(app->ctx);
  1043. }
  1044. static void pdfapp_gotouri(pdfapp_t *app, char *uri)
  1045. {
  1046. char buf[PATH_MAX];
  1047. /* Relative file:// URI, make it absolute! */
  1048. if (!strncmp(uri, "file://", 7) && uri[7] != '/')
  1049. {
  1050. char buf_base[PATH_MAX];
  1051. char buf_cwd[PATH_MAX];
  1052. if (getcwd(buf_cwd, sizeof buf_cwd))
  1053. {
  1054. fz_dirname(buf_base, app->docpath, sizeof buf_base);
  1055. fz_snprintf(buf, sizeof buf, "file://%s/%s/%s", buf_cwd, buf_base, uri+7);
  1056. fz_cleanname(buf+7);
  1057. uri = buf;
  1058. }
  1059. }
  1060. if (strncmp(uri, "file://", 7) && strncmp(uri, "http://", 7) && strncmp(uri, "https://", 8) && strncmp(uri, "mailto:", 7))
  1061. {
  1062. fz_warn(app->ctx, "refusing to open unknown link (%s)", uri);
  1063. return;
  1064. }
  1065. winopenuri(app, uri);
  1066. }
  1067. void pdfapp_gotopage(pdfapp_t *app, int number)
  1068. {
  1069. app->issearching = 0;
  1070. winrepaint(app);
  1071. if (number < 1)
  1072. number = 1;
  1073. if (number > app->pagecount)
  1074. number = app->pagecount;
  1075. if (number == app->pageno)
  1076. return;
  1077. if (app->histlen + 1 == 256)
  1078. {
  1079. memmove(app->hist, app->hist + 1, sizeof(int) * 255);
  1080. app->histlen --;
  1081. }
  1082. app->hist[app->histlen++] = app->pageno;
  1083. app->pageno = number;
  1084. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1085. }
  1086. void pdfapp_inverthit(pdfapp_t *app)
  1087. {
  1088. fz_rect bbox;
  1089. fz_matrix ctm;
  1090. int i;
  1091. ctm = pdfapp_viewctm(app);
  1092. for (i = 0; i < app->hit_count; i++)
  1093. {
  1094. bbox = fz_rect_from_quad(app->hit_bbox[i]);
  1095. bbox = fz_transform_rect(bbox, ctm);
  1096. pdfapp_invert(app, bbox);
  1097. }
  1098. }
  1099. static void pdfapp_search_in_direction(pdfapp_t *app, enum panning *panto, int dir)
  1100. {
  1101. int firstpage, page;
  1102. /* abort if no search string */
  1103. if (app->search[0] == 0)
  1104. {
  1105. winrepaint(app);
  1106. return;
  1107. }
  1108. wincursor(app, WAIT);
  1109. firstpage = app->pageno;
  1110. if (app->searchpage == app->pageno)
  1111. page = app->pageno + dir;
  1112. else
  1113. page = app->pageno;
  1114. if (page < 1) page = app->pagecount;
  1115. if (page > app->pagecount) page = 1;
  1116. do
  1117. {
  1118. if (page != app->pageno)
  1119. {
  1120. app->pageno = page;
  1121. pdfapp_showpage(app, 1, 0, 0, 0, 1);
  1122. }
  1123. app->hit_count = fz_search_stext_page(app->ctx, app->page_text, app->search, NULL, app->hit_bbox, nelem(app->hit_bbox));
  1124. if (app->hit_count > 0)
  1125. {
  1126. *panto = dir == 1 ? PAN_TO_TOP : PAN_TO_BOTTOM;
  1127. app->searchpage = app->pageno;
  1128. wincursor(app, HAND);
  1129. winrepaint(app);
  1130. return;
  1131. }
  1132. page += dir;
  1133. if (page < 1) page = app->pagecount;
  1134. if (page > app->pagecount) page = 1;
  1135. } while (page != firstpage);
  1136. pdfapp_warn(app, "String '%s' not found.", app->search);
  1137. app->pageno = firstpage;
  1138. pdfapp_showpage(app, 1, 0, 0, 0, 0);
  1139. wincursor(app, HAND);
  1140. winrepaint(app);
  1141. }
  1142. void pdfapp_onresize(pdfapp_t *app, int w, int h)
  1143. {
  1144. if (app->winw != w || app->winh != h)
  1145. {
  1146. app->winw = w;
  1147. app->winh = h;
  1148. pdfapp_panview(app, app->panx, app->pany);
  1149. winrepaint(app);
  1150. }
  1151. }
  1152. void pdfapp_autozoom_vertical(pdfapp_t *app)
  1153. {
  1154. app->resolution *= (float) app->winh / app->imgh;
  1155. if (app->resolution > MAXRES)
  1156. app->resolution = MAXRES;
  1157. else if (app->resolution < MINRES)
  1158. app->resolution = MINRES;
  1159. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1160. }
  1161. void pdfapp_autozoom_horizontal(pdfapp_t *app)
  1162. {
  1163. app->resolution *= (float) app->winw / app->imgw;
  1164. if (app->resolution > MAXRES)
  1165. app->resolution = MAXRES;
  1166. else if (app->resolution < MINRES)
  1167. app->resolution = MINRES;
  1168. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1169. }
  1170. void pdfapp_autozoom(pdfapp_t *app)
  1171. {
  1172. float page_aspect = (float) app->imgw / app->imgh;
  1173. float win_aspect = (float) app->winw / app->winh;
  1174. if (page_aspect > win_aspect)
  1175. pdfapp_autozoom_horizontal(app);
  1176. else
  1177. pdfapp_autozoom_vertical(app);
  1178. }
  1179. void pdfapp_onkey(pdfapp_t *app, int c, int modifiers)
  1180. {
  1181. int oldpage = app->pageno;
  1182. enum panning panto = PAN_TO_TOP;
  1183. int loadpage = 1;
  1184. if (app->issearching)
  1185. {
  1186. size_t n = strlen(app->search);
  1187. if (c < ' ')
  1188. {
  1189. if (c == '\b' && n > 0)
  1190. {
  1191. app->search[n - 1] = 0;
  1192. winrepaintsearch(app);
  1193. }
  1194. if (c == '\n' || c == '\r')
  1195. {
  1196. app->issearching = 0;
  1197. if (n > 0)
  1198. {
  1199. winrepaintsearch(app);
  1200. if (app->searchdir < 0)
  1201. {
  1202. if (app->pageno == 1)
  1203. app->pageno = app->pagecount;
  1204. else
  1205. app->pageno--;
  1206. pdfapp_showpage(app, 1, 1, 0, 0, 1);
  1207. }
  1208. pdfapp_onkey(app, 'n', 0);
  1209. }
  1210. else
  1211. winrepaint(app);
  1212. }
  1213. if (c == '\033')
  1214. {
  1215. app->issearching = 0;
  1216. winrepaint(app);
  1217. }
  1218. }
  1219. else
  1220. {
  1221. if (n + 2 < sizeof app->search)
  1222. {
  1223. app->search[n] = c;
  1224. app->search[n + 1] = 0;
  1225. winrepaintsearch(app);
  1226. }
  1227. }
  1228. return;
  1229. }
  1230. /*
  1231. * Save numbers typed for later
  1232. */
  1233. if (c >= '0' && c <= '9')
  1234. {
  1235. app->number[app->numberlen++] = c;
  1236. app->number[app->numberlen] = '\0';
  1237. }
  1238. key_rewritten:
  1239. switch (c)
  1240. {
  1241. case 'q':
  1242. save_accelerator(app->ctx, app->doc, app->docpath);
  1243. winclose(app);
  1244. break;
  1245. case '<':
  1246. if (fz_is_document_reflowable(app->ctx, app->doc) && app->layout_em > 6)
  1247. {
  1248. fz_bookmark mark = fz_make_bookmark(app->ctx, app->doc, fz_location_from_page_number(app->ctx, app->doc, app->pageno));
  1249. app->layout_em -= 1;
  1250. fz_layout_document(app->ctx, app->doc, app->layout_w, app->layout_h, app->layout_em);
  1251. app->pagecount = fz_count_pages(app->ctx, app->doc);
  1252. app->pageno = fz_page_number_from_location(app->ctx, app->doc, fz_lookup_bookmark(app->ctx, app->doc, mark));
  1253. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1254. }
  1255. break;
  1256. case '>':
  1257. if (fz_is_document_reflowable(app->ctx, app->doc) && app->layout_em < 36)
  1258. {
  1259. fz_bookmark mark = fz_make_bookmark(app->ctx, app->doc, fz_location_from_page_number(app->ctx, app->doc, app->pageno));
  1260. app->layout_em += 1;
  1261. fz_layout_document(app->ctx, app->doc, app->layout_w, app->layout_h, app->layout_em);
  1262. app->pagecount = fz_count_pages(app->ctx, app->doc);
  1263. app->pageno = fz_page_number_from_location(app->ctx, app->doc, fz_lookup_bookmark(app->ctx, app->doc, mark));
  1264. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1265. }
  1266. break;
  1267. /*
  1268. * Zoom and rotate
  1269. */
  1270. case '+':
  1271. app->resolution = zoom_in(app->resolution);
  1272. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1273. break;
  1274. case '-':
  1275. app->resolution = zoom_out(app->resolution);
  1276. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1277. break;
  1278. case 'W':
  1279. pdfapp_autozoom_horizontal(app);
  1280. break;
  1281. case 'H':
  1282. pdfapp_autozoom_vertical(app);
  1283. break;
  1284. case 'Z':
  1285. pdfapp_autozoom(app);
  1286. break;
  1287. case 'z':
  1288. if (app->numberlen > 0)
  1289. app->resolution = atoi(app->number);
  1290. else
  1291. app->resolution = app->default_resolution;
  1292. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1293. break;
  1294. case '[':
  1295. if (app->numberlen > 0)
  1296. app->rotate -= atoi(app->number);
  1297. else
  1298. app->rotate -= 90;
  1299. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1300. break;
  1301. case ']':
  1302. if (app->numberlen > 0)
  1303. app->rotate += atoi(app->number);
  1304. else
  1305. app->rotate += 90;
  1306. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1307. break;
  1308. /*
  1309. * Rendering and color management parameters.
  1310. */
  1311. case 'C':
  1312. app->tint ^= 1;
  1313. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1314. break;
  1315. case 'c':
  1316. app->grayscale ^= 1;
  1317. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1318. break;
  1319. case 'I':
  1320. app->invert ^= 1;
  1321. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1322. break;
  1323. case 'E':
  1324. app->useicc ^= 1;
  1325. if (app->useicc)
  1326. pdfapp_warn(app, "Using icc.");
  1327. else
  1328. pdfapp_warn(app, "Not using icc.");
  1329. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1330. break;
  1331. case 'e':
  1332. app->useseparations ^= 1;
  1333. if (app->useseparations)
  1334. pdfapp_warn(app, "Using separations.");
  1335. else
  1336. pdfapp_warn(app, "Not using separations.");
  1337. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1338. break;
  1339. case 'A':
  1340. if (app->numberlen > 0)
  1341. app->aalevel = atoi(app->number);
  1342. else
  1343. app->aalevel = (app->aalevel == 8 ? 0 : 8);
  1344. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1345. break;
  1346. /*
  1347. * Pan view, but don't need to repaint image
  1348. */
  1349. case 'f':
  1350. app->shrinkwrap = 0;
  1351. winfullscreen(app, !app->fullscreen);
  1352. app->fullscreen = !app->fullscreen;
  1353. break;
  1354. case 'w':
  1355. if (app->fullscreen)
  1356. {
  1357. winfullscreen(app, 0);
  1358. app->fullscreen = 0;
  1359. }
  1360. app->shrinkwrap = 1;
  1361. app->panx = app->pany = 0;
  1362. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1363. break;
  1364. case 'h':
  1365. if (modifiers & 8)
  1366. {
  1367. /* Alt pressed. Treat this as 't'. (i.e. ALT-Left) */
  1368. modifiers &= ~8;
  1369. c = 't';
  1370. goto key_rewritten;
  1371. }
  1372. app->panx += app->imgw / 10;
  1373. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1374. break;
  1375. case 'j':
  1376. {
  1377. if (app->imgh <= app->winh || app->pany <= app->winh - app->imgh)
  1378. {
  1379. panto = PAN_TO_TOP;
  1380. app->pageno++;
  1381. }
  1382. else
  1383. {
  1384. app->pany -= app->imgh / 10;
  1385. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1386. }
  1387. break;
  1388. }
  1389. case 'k':
  1390. {
  1391. if (app->imgh <= app->winh || app->pany == 0)
  1392. {
  1393. panto = PAN_TO_BOTTOM;
  1394. app->pageno--;
  1395. }
  1396. else
  1397. {
  1398. app->pany += app->imgh / 10;
  1399. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1400. }
  1401. break;
  1402. }
  1403. case 'l':
  1404. app->panx -= app->imgw / 10;
  1405. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1406. break;
  1407. /*
  1408. * Page navigation
  1409. */
  1410. case 'g':
  1411. if (app->numberlen > 0)
  1412. pdfapp_gotopage(app, atoi(app->number));
  1413. else
  1414. pdfapp_gotopage(app, 1);
  1415. break;
  1416. case 'G':
  1417. pdfapp_gotopage(app, app->pagecount);
  1418. break;
  1419. case 'm':
  1420. if (app->numberlen > 0)
  1421. {
  1422. int idx = atoi(app->number);
  1423. if (idx >= 0 && idx < (int)nelem(app->marks))
  1424. app->marks[idx] = app->pageno;
  1425. }
  1426. else
  1427. {
  1428. if (app->histlen + 1 == 256)
  1429. {
  1430. memmove(app->hist, app->hist + 1, sizeof(int) * 255);
  1431. app->histlen --;
  1432. }
  1433. app->hist[app->histlen++] = app->pageno;
  1434. }
  1435. break;
  1436. case 't':
  1437. if (app->numberlen > 0)
  1438. {
  1439. int idx = atoi(app->number);
  1440. if (idx >= 0 && idx < (int)nelem(app->marks))
  1441. if (app->marks[idx] > 0)
  1442. app->pageno = app->marks[idx];
  1443. }
  1444. else if (app->histlen > 0)
  1445. app->pageno = app->hist[--app->histlen];
  1446. break;
  1447. case 'p':
  1448. app->presentation_mode = !app->presentation_mode;
  1449. app->presentation_time_in_seconds = (app->numberlen > 0) ? atoi(app->number) : 5;
  1450. break;
  1451. /*
  1452. * Back and forth ...
  1453. */
  1454. case ',':
  1455. panto = DONT_PAN;
  1456. if (app->numberlen > 0)
  1457. app->pageno -= atoi(app->number);
  1458. else
  1459. app->pageno--;
  1460. break;
  1461. case '.':
  1462. panto = DONT_PAN;
  1463. if (app->numberlen > 0)
  1464. app->pageno += atoi(app->number);
  1465. else
  1466. app->pageno++;
  1467. break;
  1468. case 'b':
  1469. {
  1470. int number = 1;
  1471. if (app->numberlen > 0)
  1472. number = fz_maxi(atoi(app->number), number);
  1473. while (number--)
  1474. {
  1475. if (app->pany >= -app->imgh/20)
  1476. {
  1477. if (app->panx >= -app->imgw/20)
  1478. {
  1479. if (app->pageno - 1 > 0)
  1480. {
  1481. app->panx = INT_MIN;
  1482. app->pany = INT_MIN;
  1483. app->pageno--;
  1484. panto = DONT_PAN;
  1485. }
  1486. }
  1487. else
  1488. {
  1489. app->pany = -app->imgh;
  1490. app->panx += app->winw * 9 / 10;
  1491. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1492. }
  1493. }
  1494. else
  1495. {
  1496. app->pany += app->winh * 9 / 10;
  1497. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1498. }
  1499. }
  1500. }
  1501. break;
  1502. case ' ':
  1503. {
  1504. int number = 1;
  1505. if (app->numberlen > 0)
  1506. number = fz_maxi(atoi(app->number), number);
  1507. while (number--)
  1508. {
  1509. if (app->imgh + app->pany <= app->winh + app->imgh/20)
  1510. {
  1511. if (app->imgw + app->panx <= app->winw + app->imgw/20)
  1512. {
  1513. if (app->pageno + 1 <= app->pagecount)
  1514. {
  1515. app->panx = 0;
  1516. app->pany = 0;
  1517. app->pageno++;
  1518. panto = DONT_PAN;
  1519. }
  1520. }
  1521. else
  1522. {
  1523. app->pany = 0;
  1524. app->panx -= app->winw * 9 / 10;
  1525. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1526. }
  1527. }
  1528. else
  1529. {
  1530. app->pany -= app->winh * 9 / 10;
  1531. pdfapp_showpage(app, 0, 0, 1, 0, 0);
  1532. }
  1533. }
  1534. }
  1535. break;
  1536. /*
  1537. * Saving the file
  1538. */
  1539. case 'S':
  1540. pdfapp_save(app);
  1541. break;
  1542. /*
  1543. * Reloading the file...
  1544. */
  1545. case 'r':
  1546. panto = DONT_PAN;
  1547. oldpage = -1;
  1548. pdfapp_reloadfile(app);
  1549. break;
  1550. /*
  1551. * Searching
  1552. */
  1553. case '?':
  1554. app->issearching = 1;
  1555. app->searchdir = -1;
  1556. app->search[0] = 0;
  1557. app->hit_count = 0;
  1558. app->searchpage = -1;
  1559. winrepaintsearch(app);
  1560. break;
  1561. case '/':
  1562. app->issearching = 1;
  1563. app->searchdir = 1;
  1564. app->search[0] = 0;
  1565. app->hit_count = 0;
  1566. app->searchpage = -1;
  1567. winrepaintsearch(app);
  1568. break;
  1569. case 'n':
  1570. if (app->searchdir > 0)
  1571. pdfapp_search_in_direction(app, &panto, 1);
  1572. else
  1573. pdfapp_search_in_direction(app, &panto, -1);
  1574. loadpage = 0;
  1575. break;
  1576. case 'N':
  1577. if (app->searchdir > 0)
  1578. pdfapp_search_in_direction(app, &panto, -1);
  1579. else
  1580. pdfapp_search_in_direction(app, &panto, 1);
  1581. loadpage = 0;
  1582. break;
  1583. }
  1584. if (c < '0' || c > '9')
  1585. app->numberlen = 0;
  1586. if (app->pageno < 1)
  1587. app->pageno = 1;
  1588. if (app->pageno > app->pagecount)
  1589. app->pageno = app->pagecount;
  1590. if (app->pageno != oldpage)
  1591. {
  1592. switch (panto)
  1593. {
  1594. case PAN_TO_TOP:
  1595. app->pany = 0;
  1596. break;
  1597. case PAN_TO_BOTTOM:
  1598. app->pany = INT_MIN;
  1599. break;
  1600. case DONT_PAN:
  1601. break;
  1602. }
  1603. pdfapp_showpage(app, loadpage, 1, 1, 1, 0);
  1604. }
  1605. }
  1606. static void handlescroll(pdfapp_t *app, int modifiers, int dir)
  1607. {
  1608. app->ispanning = app->iscopying = 0;
  1609. if (modifiers & (1<<2))
  1610. {
  1611. /* zoom in/out if ctrl is pressed */
  1612. if (dir > 0)
  1613. app->resolution = zoom_in(app->resolution);
  1614. else
  1615. app->resolution = zoom_out(app->resolution);
  1616. if (app->resolution > MAXRES)
  1617. app->resolution = MAXRES;
  1618. if (app->resolution < MINRES)
  1619. app->resolution = MINRES;
  1620. pdfapp_showpage(app, 0, 1, 1, 0, 0);
  1621. }
  1622. else
  1623. {
  1624. /* scroll up/down, or left/right if
  1625. shift is pressed */
  1626. int xstep = 0;
  1627. int ystep = 0;
  1628. int pagestep = 0;
  1629. if (modifiers & (1<<0))
  1630. {
  1631. if (dir > 0 && app->panx >= 0)
  1632. pagestep = -1;
  1633. else if (dir < 0 && app->panx <= app->winw - app->imgw)
  1634. pagestep = 1;
  1635. else
  1636. xstep = 20 * dir;
  1637. }
  1638. else
  1639. {
  1640. if (dir > 0 && app->pany >= 0)
  1641. pagestep = -1;
  1642. else if (dir < 0 && app->pany <= app->winh - app->imgh)
  1643. pagestep = 1;
  1644. else
  1645. ystep = 20 * dir;
  1646. }
  1647. if (pagestep == 0)
  1648. pdfapp_panview(app, app->panx + xstep, app->pany + ystep);
  1649. else if (pagestep > 0 && app->pageno < app->pagecount)
  1650. {
  1651. app->pageno++;
  1652. app->pany = 0;
  1653. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1654. }
  1655. else if (pagestep < 0 && app->pageno > 1)
  1656. {
  1657. app->pageno--;
  1658. app->pany = INT_MIN;
  1659. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1660. }
  1661. }
  1662. }
  1663. void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int state)
  1664. {
  1665. fz_context *ctx = app->ctx;
  1666. fz_irect irect = { 0, 0, app->layout_w, app->layout_h };
  1667. fz_link *link;
  1668. fz_matrix ctm;
  1669. fz_point p;
  1670. int processed = 0;
  1671. if (app->image)
  1672. irect = fz_pixmap_bbox(app->ctx, app->image);
  1673. p.x = x - app->panx + irect.x0;
  1674. p.y = y - app->pany + irect.y0;
  1675. ctm = pdfapp_viewctm(app);
  1676. ctm = fz_invert_matrix(ctm);
  1677. p = fz_transform_point(p, ctm);
  1678. for (link = app->page_links; link; link = link->next)
  1679. {
  1680. if (p.x >= link->rect.x0 && p.x <= link->rect.x1)
  1681. if (p.y >= link->rect.y0 && p.y <= link->rect.y1)
  1682. break;
  1683. }
  1684. if (link)
  1685. {
  1686. wincursor(app, HAND);
  1687. if (btn == 1 && state == 1 && !processed)
  1688. {
  1689. if (fz_is_external_link(ctx, link->uri))
  1690. pdfapp_gotouri(app, link->uri);
  1691. else
  1692. {
  1693. fz_location loc = fz_resolve_link(ctx, app->doc, link->uri, NULL, NULL);
  1694. pdfapp_gotopage(app, fz_page_number_from_location(ctx, app->doc, loc)+1);
  1695. }
  1696. return;
  1697. }
  1698. }
  1699. else
  1700. {
  1701. wincursor(app, ARROW);
  1702. }
  1703. if (state == 1 && !processed)
  1704. {
  1705. if (btn == 1 && !app->iscopying)
  1706. {
  1707. app->ispanning = 1;
  1708. app->selx = x;
  1709. app->sely = y;
  1710. app->beyondy = 0;
  1711. }
  1712. if (btn == 3 && !app->ispanning)
  1713. {
  1714. app->iscopying = 1;
  1715. app->selx = x;
  1716. app->sely = y;
  1717. app->selr.x0 = x;
  1718. app->selr.x1 = x;
  1719. app->selr.y0 = y;
  1720. app->selr.y1 = y;
  1721. }
  1722. if (btn == 4 || btn == 5) /* scroll wheel */
  1723. {
  1724. handlescroll(app, modifiers, btn == 4 ? 1 : -1);
  1725. }
  1726. if (btn == 6 || btn == 7) /* scroll wheel (horizontal) */
  1727. {
  1728. /* scroll left/right or up/down if shift is pressed */
  1729. handlescroll(app, modifiers ^ (1<<0), btn == 6 ? 1 : -1);
  1730. }
  1731. if (app->presentation_mode)
  1732. {
  1733. if (btn == 1 && app->pageno < app->pagecount)
  1734. {
  1735. app->pageno++;
  1736. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1737. }
  1738. if (btn == 3 && app->pageno > 1)
  1739. {
  1740. app->pageno--;
  1741. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1742. }
  1743. }
  1744. }
  1745. else if (state == -1)
  1746. {
  1747. if (app->iscopying)
  1748. {
  1749. app->iscopying = 0;
  1750. app->selr.x0 = fz_mini(app->selx, x) - app->panx + irect.x0;
  1751. app->selr.x1 = fz_maxi(app->selx, x) - app->panx + irect.x0;
  1752. app->selr.y0 = fz_mini(app->sely, y) - app->pany + irect.y0;
  1753. app->selr.y1 = fz_maxi(app->sely, y) - app->pany + irect.y0;
  1754. winrepaint(app);
  1755. if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1)
  1756. windocopy(app);
  1757. }
  1758. app->ispanning = 0;
  1759. }
  1760. else if (app->ispanning)
  1761. {
  1762. int newx = app->panx + x - app->selx;
  1763. int newy = app->pany + y - app->sely;
  1764. int imgh = app->winh;
  1765. if (app->image)
  1766. imgh = fz_pixmap_height(app->ctx, app->image);
  1767. /* Scrolling beyond limits implies flipping pages */
  1768. /* Are we requested to scroll beyond limits? */
  1769. if (newy + imgh < app->winh || newy > 0)
  1770. {
  1771. /* Yes. We can assume that deltay != 0 */
  1772. int deltay = y - app->sely;
  1773. /* Check whether the panning has occurred in the
  1774. * direction that we are already crossing the
  1775. * limit it. If not, we can conclude that we
  1776. * have switched ends of the page and will thus
  1777. * start over counting.
  1778. */
  1779. if( app->beyondy == 0 || (app->beyondy ^ deltay) >= 0 )
  1780. {
  1781. /* Updating how far we are beyond and
  1782. * flipping pages if beyond threshold
  1783. */
  1784. app->beyondy += deltay;
  1785. if (app->beyondy > BEYOND_THRESHHOLD)
  1786. {
  1787. if( app->pageno > 1 )
  1788. {
  1789. app->pageno--;
  1790. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1791. if (app->image)
  1792. newy = -fz_pixmap_height(app->ctx, app->image);
  1793. }
  1794. app->beyondy = 0;
  1795. }
  1796. else if (app->beyondy < -BEYOND_THRESHHOLD)
  1797. {
  1798. if( app->pageno < app->pagecount )
  1799. {
  1800. app->pageno++;
  1801. pdfapp_showpage(app, 1, 1, 1, 0, 0);
  1802. newy = 0;
  1803. }
  1804. app->beyondy = 0;
  1805. }
  1806. }
  1807. else
  1808. app->beyondy = 0;
  1809. }
  1810. /* Although at this point we've already determined that
  1811. * or that no scrolling will be performed in
  1812. * y-direction, the x-direction has not yet been taken
  1813. * care off. Therefore
  1814. */
  1815. pdfapp_panview(app, newx, newy);
  1816. app->selx = x;
  1817. app->sely = y;
  1818. }
  1819. else if (app->iscopying)
  1820. {
  1821. app->selr.x0 = fz_mini(app->selx, x) - app->panx + irect.x0;
  1822. app->selr.x1 = fz_maxi(app->selx, x) - app->panx + irect.x0;
  1823. app->selr.y0 = fz_mini(app->sely, y) - app->pany + irect.y0;
  1824. app->selr.y1 = fz_maxi(app->sely, y) - app->pany + irect.y0;
  1825. winrepaint(app);
  1826. }
  1827. }
  1828. void pdfapp_oncopy(pdfapp_t *app, unsigned short *ucsbuf, int ucslen)
  1829. {
  1830. fz_matrix ctm;
  1831. fz_stext_page *page = app->page_text;
  1832. int p, need_newline;
  1833. fz_stext_block *block;
  1834. fz_stext_line *line;
  1835. fz_stext_char *ch;
  1836. fz_rect sel;
  1837. ctm = pdfapp_viewctm(app);
  1838. ctm = fz_invert_matrix(ctm);
  1839. sel = fz_transform_rect(app->selr, ctm);
  1840. p = 0;
  1841. need_newline = 0;
  1842. for (block = page->first_block; block; block = block->next)
  1843. {
  1844. if (block->type != FZ_STEXT_BLOCK_TEXT)
  1845. continue;
  1846. for (line = block->u.t.first_line; line; line = line->next)
  1847. {
  1848. int saw_text = 0;
  1849. for (ch = line->first_char; ch; ch = ch->next)
  1850. {
  1851. fz_rect bbox = fz_rect_from_quad(ch->quad);
  1852. int c = ch->c;
  1853. if (c < 32)
  1854. c = 0xFFFD;
  1855. if (bbox.x1 >= sel.x0 && bbox.x0 <= sel.x1 && bbox.y1 >= sel.y0 && bbox.y0 <= sel.y1)
  1856. {
  1857. saw_text = 1;
  1858. if (need_newline)
  1859. {
  1860. #ifdef _WIN32
  1861. if (p < ucslen - 1)
  1862. ucsbuf[p++] = '\r';
  1863. #endif
  1864. if (p < ucslen - 1)
  1865. ucsbuf[p++] = '\n';
  1866. need_newline = 0;
  1867. }
  1868. if (p < ucslen - 1)
  1869. ucsbuf[p++] = c;
  1870. }
  1871. }
  1872. if (saw_text)
  1873. need_newline = 1;
  1874. }
  1875. }
  1876. ucsbuf[p] = 0;
  1877. }
  1878. void pdfapp_postblit(pdfapp_t *app)
  1879. {
  1880. clock_t time;
  1881. float seconds;
  1882. int llama;
  1883. app->transitions_enabled = 1;
  1884. if (!app->in_transit)
  1885. return;
  1886. time = clock();
  1887. seconds = (float)(time - app->start_time) / CLOCKS_PER_SEC;
  1888. llama = seconds * 256 / app->transition.duration;
  1889. if (llama >= 256)
  1890. {
  1891. /* Completed. */
  1892. fz_drop_pixmap(app->ctx, app->image);
  1893. app->image = app->new_image;
  1894. app->new_image = NULL;
  1895. app->imgw = fz_pixmap_width(app->ctx, app->image);
  1896. app->imgh = fz_pixmap_height(app->ctx, app->image);
  1897. fz_drop_pixmap(app->ctx, app->old_image);
  1898. app->old_image = NULL;
  1899. if (app->duration != 0)
  1900. winadvancetimer(app, app->duration);
  1901. }
  1902. else
  1903. fz_generate_transition(app->ctx, app->image, app->old_image, app->new_image, llama, &app->transition);
  1904. winrepaint(app);
  1905. if (llama >= 256)
  1906. {
  1907. /* Completed. */
  1908. app->in_transit = 0;
  1909. }
  1910. }
  1911. void pdfapp_load_profile(pdfapp_t *app, char *profile_name)
  1912. {
  1913. fz_buffer *profile_data = NULL;
  1914. fz_var(profile_data);
  1915. fz_try(app->ctx)
  1916. {
  1917. profile_data = fz_read_file(app->ctx, profile_name);
  1918. #ifdef _WIN32
  1919. app->colorspace = fz_new_icc_colorspace(app->ctx, FZ_COLORSPACE_BGR, 0, NULL, profile_data);
  1920. #else
  1921. app->colorspace = fz_new_icc_colorspace(app->ctx, FZ_COLORSPACE_RGB, 0, NULL, profile_data);
  1922. #endif
  1923. }
  1924. fz_always(app->ctx)
  1925. {
  1926. fz_drop_buffer(app->ctx, profile_data);
  1927. }
  1928. fz_catch(app->ctx)
  1929. {
  1930. fz_report_error(app->ctx);
  1931. pdfapp_error(app, "cannot load color profile");
  1932. }
  1933. }