1 package com.artifex.mupdf.fitz; 2 3 public class Document 4 { 5 static { Context.init()6 Context.init(); 7 } 8 9 public static final String META_FORMAT = "format"; 10 public static final String META_ENCRYPTION = "encryption"; 11 public static final String META_INFO_AUTHOR = "info:Author"; 12 public static final String META_INFO_TITLE = "info:Title"; 13 14 protected long pointer; 15 finalize()16 protected native void finalize(); 17 destroy()18 public void destroy() { 19 finalize(); 20 } 21 Document(long p)22 protected Document(long p) { 23 pointer = p; 24 } 25 openNativeWithPath(String filename, String accelerator)26 protected native static Document openNativeWithPath(String filename, String accelerator); openNativeWithBuffer(String magic, byte[] buffer, byte[] accelerator)27 protected native static Document openNativeWithBuffer(String magic, byte[] buffer, byte[] accelerator); openNativeWithStream(String magic, SeekableInputStream stream, SeekableInputStream accelerator)28 protected native static Document openNativeWithStream(String magic, SeekableInputStream stream, SeekableInputStream accelerator); openNativeWithPathAndStream(String filename, SeekableInputStream accelerator)29 protected native static Document openNativeWithPathAndStream(String filename, SeekableInputStream accelerator); 30 openDocument(String filename)31 public static Document openDocument(String filename) { 32 return openNativeWithPath(filename, null); 33 } 34 openDocument(String filename, String accelerator)35 public static Document openDocument(String filename, String accelerator) { 36 return openNativeWithPath(filename, accelerator); 37 } 38 openDocument(String filename, SeekableInputStream accelerator)39 public static Document openDocument(String filename, SeekableInputStream accelerator) { 40 return openNativeWithPathAndStream(filename, accelerator); 41 } 42 openDocument(byte[] buffer, String magic)43 public static Document openDocument(byte[] buffer, String magic) { 44 return openNativeWithBuffer(magic, buffer, null); 45 } 46 openDocument(byte[] buffer, String magic, byte[] accelerator)47 public static Document openDocument(byte[] buffer, String magic, byte[] accelerator) { 48 return openNativeWithBuffer(magic, buffer, accelerator); 49 } 50 openDocument(SeekableInputStream stream, String magic)51 public static Document openDocument(SeekableInputStream stream, String magic) { 52 return openNativeWithStream(magic, stream, null); 53 } 54 openDocument(SeekableInputStream stream, String magic, SeekableInputStream accelerator)55 public static Document openDocument(SeekableInputStream stream, String magic, SeekableInputStream accelerator) { 56 return openNativeWithStream(magic, stream, accelerator); 57 } 58 recognize(String magic)59 public static native boolean recognize(String magic); 60 saveAccelerator(String filename)61 public native void saveAccelerator(String filename); outputAccelerator(SeekableOutputStream stream)62 public native void outputAccelerator(SeekableOutputStream stream); 63 needsPassword()64 public native boolean needsPassword(); authenticatePassword(String password)65 public native boolean authenticatePassword(String password); 66 countChapters()67 public native int countChapters(); countPages(int chapter)68 public native int countPages(int chapter); loadPage(int chapter, int number)69 public native Page loadPage(int chapter, int number); 70 countPages()71 public int countPages() { 72 int np = 0; 73 int nc = countChapters(); 74 for (int i = 0; i < nc; ++i) 75 np += countPages(i); 76 return np; 77 } 78 loadPage(Location loc)79 public Page loadPage(Location loc) { 80 return loadPage(loc.chapter, loc.page); 81 } 82 loadPage(int number)83 public Page loadPage(int number) { 84 int start = 0; 85 int nc = countChapters(); 86 for (int i = 0; i < nc; ++i) { 87 int np = countPages(i); 88 if (number < start + np) 89 return loadPage(i, number - start); 90 start += np; 91 } 92 throw new IllegalArgumentException("page number out of range"); 93 } 94 lastPage()95 public Location lastPage() { 96 int nc = countChapters(); 97 int np = countPages(nc-1); 98 return new Location(nc-1, np-1); 99 } 100 nextPage(Location loc)101 public Location nextPage(Location loc) { 102 int np = countPages(loc.chapter); 103 if (loc.page + 1 == np) { 104 int nc = countChapters(); 105 if (loc.chapter + 1 < nc) 106 return new Location(loc.chapter + 1, 0); 107 } else { 108 return new Location(loc.chapter, loc.page + 1); 109 } 110 return loc; 111 } 112 previousPage(Location loc)113 public Location previousPage(Location loc) { 114 if (loc.page == 0) { 115 if (loc.chapter > 0) { 116 int np = countPages(loc.chapter - 1); 117 return new Location(loc.chapter - 1, np - 1); 118 } 119 } else { 120 return new Location(loc.chapter, loc.page - 1); 121 } 122 return loc; 123 } 124 clampLocation(Location input)125 public Location clampLocation(Location input) { 126 int c = input.chapter; 127 int p = input.page; 128 int nc = countChapters(); 129 if (c < 0) c = 0; 130 if (c >= nc) c = nc - 1; 131 int np = countPages(c); 132 if (p < 0) p = 0; 133 if (p >= np) p = np - 1; 134 if (input.chapter == c && input.page == p) 135 return input; 136 return new Location(c, p); 137 } 138 locationFromPageNumber(int number)139 public Location locationFromPageNumber(int number) { 140 int i, start = 0, np = 0, nc = countChapters(); 141 if (number < 0) 142 number = 0; 143 for (i = 0; i < nc; ++i) 144 { 145 np = countPages(i); 146 if (number < start + np) 147 return new Location(i, number - start); 148 start += np; 149 } 150 return new Location(i - 1, np - 1); 151 } 152 pageNumberFromLocation(Location loc)153 public int pageNumberFromLocation(Location loc) { 154 int nc = countChapters(); 155 int start = 0; 156 if (loc == null) 157 return -1; 158 for (int i = 0; i < nc; ++i) { 159 if (i == loc.chapter) 160 return start + loc.page; 161 start += countPages(i); 162 } 163 return -1; 164 } 165 search(int chapter, int page, String needle)166 public native Quad[] search(int chapter, int page, String needle); 167 resolveLink(String uri)168 public native Location resolveLink(String uri); resolveLink(Outline link)169 public Location resolveLink(Outline link) { 170 return resolveLink(link.uri); 171 } resolveLink(Link link)172 public Location resolveLink(Link link) { 173 return resolveLink(link.uri); 174 } 175 loadOutline()176 public native Outline[] loadOutline(); getMetaData(String key)177 public native String getMetaData(String key); isReflowable()178 public native boolean isReflowable(); layout(float width, float height, float em)179 public native void layout(float width, float height, float em); 180 findBookmark(long mark)181 public native Location findBookmark(long mark); makeBookmark(int chapter, int page)182 public native long makeBookmark(int chapter, int page); makeBookmark(Location loc)183 public long makeBookmark(Location loc) { 184 return makeBookmark(loc.chapter, loc.page); 185 } 186 187 public static final int PERMISSION_PRINT = (int) 'p'; 188 public static final int PERMISSION_COPY = (int) 'c'; 189 public static final int PERMISSION_EDIT = (int) 'e'; 190 public static final int PERMISSION_ANNOTATE = (int) 'n'; 191 hasPermission(int permission)192 public native boolean hasPermission(int permission); 193 isUnencryptedPDF()194 public native boolean isUnencryptedPDF(); 195 isPDF()196 public boolean isPDF() { 197 return false; 198 } 199 } 200