summaryrefslogtreecommitdiff
path: root/platform/java/example
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-03-30 17:49:04 +0200
committerTor Andersson <tor.andersson@artifex.com>2016-03-31 13:00:41 +0200
commitd68576c3785572c1f5d41f83015b8fe6bbcbe9e8 (patch)
tree431a86edfac640864ba7f406611e8fe9929908cd /platform/java/example
parent32cdb2246eeb9e8109a712ec2a5dd2938e30e9b6 (diff)
downloadmupdf-d68576c3785572c1f5d41f83015b8fe6bbcbe9e8.tar.xz
Reorganize java and android source.
platform/java and platform/android are reorganized: platform/java The new JNI Java classes, mupdf_native.{c,h}, Makefile and Makejar. platform/java/example The example desktop viewer classes. platform/android/viewer The original demo viewer. ndk-build is used to build libmupdf_java.so, making reference to mupdf_native.{c,h} in platform/java.
Diffstat (limited to 'platform/java/example')
-rw-r--r--platform/java/example/PageCanvas.java59
-rw-r--r--platform/java/example/TraceDevice.java132
-rw-r--r--platform/java/example/Viewer.java203
3 files changed, 394 insertions, 0 deletions
diff --git a/platform/java/example/PageCanvas.java b/platform/java/example/PageCanvas.java
new file mode 100644
index 00000000..9de1abdb
--- /dev/null
+++ b/platform/java/example/PageCanvas.java
@@ -0,0 +1,59 @@
+package example;
+
+import com.artifex.mupdf.fitz.*;
+import java.awt.*;
+import java.awt.image.*;
+
+public class PageCanvas extends java.awt.Canvas
+{
+ protected Page page;
+ protected BufferedImage image;
+
+ public static BufferedImage imageFromPixmap(Pixmap pixmap) {
+ int w = pixmap.getWidth();
+ int h = pixmap.getHeight();
+ BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
+ image.setRGB(0, 0, w, h, pixmap.getPixels(), 0, w);
+ return image;
+ }
+
+ public static BufferedImage imageFromPageWithDevice(Page page, Matrix ctm) {
+ Rect bbox = page.getBounds();
+ Pixmap pixmap = new Pixmap(ColorSpace.DeviceBGR, bbox);
+ pixmap.clear(255);
+ DrawDevice dev = new DrawDevice(pixmap);
+ page.run(dev, new Matrix());
+ dev.destroy();
+ BufferedImage image = imageFromPixmap(pixmap);
+ pixmap.destroy();
+ return image;
+ }
+
+ public static BufferedImage imageFromPage(Page page, Matrix ctm) {
+ Pixmap pixmap = page.toPixmap(ctm, ColorSpace.DeviceBGR);
+ BufferedImage image = imageFromPixmap(pixmap);
+ pixmap.destroy();
+ return image;
+ }
+
+ public PageCanvas(Page page_) {
+ this.page = page_;
+ image = imageFromPage(page, new Matrix());
+ }
+
+ public Dimension getPreferredSize() {
+ return new Dimension(image.getWidth(), image.getHeight());
+ }
+
+ public Dimension getMinimumSize() {
+ return getPreferredSize();
+ }
+
+ public Dimension getMaximumSize() {
+ return getPreferredSize();
+ }
+
+ public void paint(Graphics g) {
+ g.drawImage(image, 0, 0, null);
+ }
+}
diff --git a/platform/java/example/TraceDevice.java b/platform/java/example/TraceDevice.java
new file mode 100644
index 00000000..49c97532
--- /dev/null
+++ b/platform/java/example/TraceDevice.java
@@ -0,0 +1,132 @@
+package example;
+
+import com.artifex.mupdf.fitz.*;
+
+public class TraceDevice extends Device implements PathWalker, TextWalker
+{
+ public String traceColor(ColorSpace cs, float color[], float alpha) {
+ String s = cs + " [";
+ int i;
+ for (i = 0; i < color.length; ++i) {
+ if (i > 0) s += " ";
+ s += color[i];
+ }
+ return s + "] " + alpha;
+ }
+ public String traceStroke(StrokeState stroke) {
+ return "c=" + stroke.getStartCap() + "," + stroke.getDashCap() + "," + stroke.getEndCap() +
+ " j=" + stroke.getLineJoin() +
+ " m=" + stroke.getMiterLimit() +
+ " l=" + stroke.getLineWidth();
+ }
+
+ public void moveTo(float x, float y) {
+ System.out.println("moveto " + x + " " + y);
+ }
+ public void lineTo(float x, float y) {
+ System.out.println("lineto " + x + " " + y);
+ }
+ public void curveTo(float cx1, float cy1, float cx2, float cy2, float ex, float ey) {
+ System.out.println("curveto " + cx1 + " " + cy1 + " " + cx2 + " " + cy2 + " " + ex + " " + ey);
+ }
+ public void closePath() {
+ System.out.println("closepath");
+ }
+
+ public void showGlyph(Font font, Matrix trm, int glyph, int unicode, boolean wmode) {
+ System.out.println("glyph '" + (char)unicode + "' " + glyph + "\t" + font + " " + trm);
+ }
+
+ public void tracePath(Path path) {
+ path.walk(this);
+ }
+ public void traceText(Text text) {
+ text.walk(this);
+ }
+
+ public void fillPath(Path path, boolean evenOdd, Matrix ctm, ColorSpace cs, float color[], float alpha) {
+ System.out.println("fillPath " + evenOdd + " " + ctm + " " + traceColor(cs, color, alpha));
+ tracePath(path);
+ }
+ public void strokePath(Path path, StrokeState stroke, Matrix ctm, ColorSpace cs, float color[], float alpha) {
+ System.out.println("strokePath " + traceStroke(stroke) + " " + ctm + " " + traceColor(cs, color, alpha));
+ tracePath(path);
+ }
+ public void clipPath(Path path, Rect rect, boolean evenOdd, Matrix ctm) {
+ System.out.println("clipPath " + evenOdd + " " + ctm);
+ tracePath(path);
+ }
+ public void clipStrokePath(Path path, Rect rect, StrokeState stroke, Matrix ctm) {
+ System.out.println("clipStrokePath " + traceStroke(stroke) + " " + ctm);
+ tracePath(path);
+ }
+
+ public void fillText(Text text, Matrix ctm, ColorSpace cs, float color[], float alpha) {
+ System.out.println("fillText " + ctm + " " + traceColor(cs, color, alpha));
+ traceText(text);
+ }
+ public void strokeText(Text text, StrokeState stroke, Matrix ctm, ColorSpace cs, float color[], float alpha) {
+ System.out.println("strokeText " + ctm + " " + traceStroke(stroke) + " " + traceColor(cs, color, alpha));
+ traceText(text);
+ }
+ public void clipText(Text text, Matrix ctm) {
+ System.out.println("clipText " + ctm);
+ traceText(text);
+ }
+ public void clipStrokeText(Text text, StrokeState stroke, Matrix ctm) {
+ System.out.println("clipStrokeText " + ctm + " " + traceStroke(stroke));
+ traceText(text);
+ }
+ public void ignoreText(Text text, Matrix ctm) {
+ System.out.println("ignoreText " + ctm);
+ traceText(text);
+ }
+ public void fillShade(Shade shade, Matrix ctm, float alpha) {
+ System.out.println("fillShade " + ctm + " " + alpha);
+ }
+ public void fillImage(Image img, Matrix ctm, float alpha) {
+ System.out.println("fillImage " + ctm + " " + alpha);
+ }
+ public void fillImageMask(Image img, Matrix ctm, ColorSpace cs, float color[], float alpha) {
+ System.out.println("fillImageMask " + ctm + " " + traceColor(cs, color, alpha));
+ }
+ public void clipImageMask(Image img, Rect rect, Matrix ctm) {
+ System.out.println("clipImageMask " + ctm + " " + rect);
+ }
+ public void popClip() {
+ System.out.println("popClip");
+ }
+
+ public void beginMask(Rect rect, boolean luminosity, ColorSpace cs, float bc[]) {
+ System.out.println("beginMask r=" + rect +
+ " l=" + luminosity +
+ " " + traceColor(cs, bc, 1));
+ }
+ public void endMask() {
+ System.out.println("endMask");
+ }
+ public void beginGroup(Rect rect, boolean isolated, boolean knockout, int blendmode, float alpha) {
+ System.out.println("beginGroup r=" + rect +
+ " i=" + isolated +
+ " k=" + knockout +
+ " bm=" + blendmode +
+ " a=" + alpha);
+ }
+ public void endGroup() {
+ System.out.println("endGroup");
+ }
+ public int beginTile(Rect area, Rect view, float xstep, float ystep, Matrix ctm, int id) {
+ System.out.println("beginTile");
+ return 0;
+ }
+ public void endTile() {
+ System.out.println("endTile");
+ }
+
+ public static void main(String[] args) {
+ Document doc = new Document("pdfref17.pdf");
+ Page page = doc.loadPage(1144);
+ TraceDevice dev = new TraceDevice();
+ page.run(dev, new Matrix());
+ }
+}
diff --git a/platform/java/example/Viewer.java b/platform/java/example/Viewer.java
new file mode 100644
index 00000000..91b0fa1d
--- /dev/null
+++ b/platform/java/example/Viewer.java
@@ -0,0 +1,203 @@
+package example;
+
+import com.artifex.mupdf.fitz.*;
+
+import java.io.File;
+
+import java.awt.Frame;
+import java.awt.Label;
+import java.awt.Button;
+import java.awt.Panel;
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowListener;
+import java.awt.event.WindowEvent;
+import java.awt.event.ActionEvent;
+
+import javax.swing.JFileChooser;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.JOptionPane;
+
+public class Viewer extends Frame implements WindowListener, ActionListener
+{
+ protected Document doc;
+ protected Panel toolbar;
+ protected PageCanvas pageCanvas;
+ protected Label pageLabel;
+ protected Button firstButton, prevButton, nextButton, lastButton;
+ protected int pageCount;
+ protected int pageNumber;
+
+ public Viewer(Document doc_) {
+ super("MuPDF");
+
+ this.doc = doc_;
+
+ pageCount = doc.countPages();
+ pageNumber = 0;
+
+ setSize(600, 900);
+ setTitle("MuPDF: " + doc.getMetaData(Document.META_INFO_TITLE));
+
+ toolbar = new Panel();
+ toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
+ firstButton = new Button("|<");
+ firstButton.addActionListener(this);
+ prevButton = new Button("<");
+ prevButton.addActionListener(this);
+ nextButton = new Button(">");
+ nextButton.addActionListener(this);
+ lastButton = new Button(">|");
+ lastButton.addActionListener(this);
+ pageLabel = new Label();
+
+ toolbar.add(firstButton);
+ toolbar.add(prevButton);
+ toolbar.add(nextButton);
+ toolbar.add(lastButton);
+ toolbar.add(pageLabel);
+
+ add(toolbar, BorderLayout.NORTH);
+
+ addWindowListener(this);
+
+ stuff();
+ }
+
+ public void stuff() {
+ pageLabel.setText("Page " + (pageNumber + 1) + " / " + pageCount);
+ if (pageCanvas != null)
+ remove(pageCanvas);
+ pageCanvas = new PageCanvas(doc.loadPage(pageNumber));
+ add(pageCanvas, BorderLayout.CENTER);
+ validate();
+ }
+
+ public void actionPerformed(ActionEvent event) {
+ Object source = event.getSource();
+ int oldPageNumber = pageNumber;
+
+ if (source == firstButton)
+ pageNumber = 0;
+ if (source == lastButton)
+ pageNumber = pageCount - 1;
+ if (source == prevButton) {
+ pageNumber = pageNumber - 1;
+ if (pageNumber < 0)
+ pageNumber = 0;
+ }
+ if (source == nextButton) {
+ pageNumber = pageNumber + 1;
+ if (pageNumber >= pageCount)
+ pageNumber = pageCount - 1;
+ }
+
+ if (pageNumber != oldPageNumber)
+ stuff();
+ }
+
+ public void windowClosing(WindowEvent event) {
+ System.exit(0);
+ }
+
+ public void windowActivated(WindowEvent event) { }
+ public void windowDeactivated(WindowEvent event) { }
+ public void windowIconified(WindowEvent event) { }
+ public void windowDeiconified(WindowEvent event) { }
+ public void windowOpened(WindowEvent event) { }
+ public void windowClosed(WindowEvent event) { }
+
+ public static void main(String[] args)
+ {
+ JFileChooser fileChooser = new JFileChooser();
+ fileChooser.setDialogTitle("Choose a file to open");
+ fileChooser.setFileFilter(new FileFilter()
+ {
+ public String getDescription()
+ {
+ return "Supported files (*.pdf, *,xps, *.jpg, *.jpeg, *.png, *.epub, *.cbz, *.cbr)";
+ }
+
+ public boolean accept(File f)
+ {
+ if (f.isDirectory())
+ return true;
+
+ String filename = f.getName().toLowerCase();
+ if (filename.endsWith(".pdf"))
+ return true;
+ if (filename.endsWith(".xps"))
+ return true;
+ if (filename.endsWith(".jpg"))
+ return true;
+ if (filename.endsWith(".jpeg"))
+ return true;
+ if (filename.endsWith(".png"))
+ return true;
+ if (filename.endsWith(".epub"))
+ return true;
+ if (filename.endsWith(".cbz"))
+ return true;
+ if (filename.endsWith(".cbr"))
+ return true;
+
+ return false;
+ }
+ });
+
+ while (true)
+ {
+ try
+ {
+ // get a file to open
+ int result = fileChooser.showOpenDialog(null);
+ if (result == JFileChooser.APPROVE_OPTION)
+ {
+ // user selects a file
+ File selectedFile = fileChooser.getSelectedFile();
+ if (selectedFile != null)
+ {
+ Document doc = new Document(selectedFile.getAbsolutePath());
+ if (doc != null)
+ {
+ Viewer app = new Viewer(doc);
+ if (app != null)
+ {
+ app.setVisible(true);
+ return;
+ }
+ else
+ {
+ infoBox("Cannot create Viewer for "+selectedFile.getAbsolutePath(),"Error");
+ }
+ }
+ else
+ {
+ infoBox("Cannot open "+selectedFile.getAbsolutePath(),"Error");
+ }
+ }
+ else
+ {
+ infoBox("Selected file not found.","Error");
+ }
+ }
+ else
+ {
+ infoBox("File selection cancelled.","Error");
+ return;
+ }
+
+ }
+ catch (Exception e)
+ {
+ infoBox("Exception: "+e.getMessage(),"Error");
+ }
+ }
+ }
+
+ private static void infoBox(String infoMessage, String titleBar)
+ {
+ JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE);
+ }
+}