diff options
Diffstat (limited to 'platform')
-rw-r--r-- | platform/java/example/PageCanvas.java | 41 | ||||
-rw-r--r-- | platform/java/example/Viewer.java | 65 |
2 files changed, 98 insertions, 8 deletions
diff --git a/platform/java/example/PageCanvas.java b/platform/java/example/PageCanvas.java index ffe3efc2..bfeca21b 100644 --- a/platform/java/example/PageCanvas.java +++ b/platform/java/example/PageCanvas.java @@ -9,6 +9,9 @@ public class PageCanvas extends java.awt.Canvas protected Page page; protected BufferedImage image; + protected float mScale = 1.0f; + private float mRetinaScale = 1.0f; + public static BufferedImage imageFromPixmap(Pixmap pixmap) { int w = pixmap.getWidth(); int h = pixmap.getHeight(); @@ -23,7 +26,7 @@ public class PageCanvas extends java.awt.Canvas Pixmap pixmap = new Pixmap(ColorSpace.DeviceBGR, bbox); pixmap.clear(255); DrawDevice dev = new DrawDevice(pixmap); - page.run(dev, new Matrix()); + page.run(dev, ctm); dev.destroy(); BufferedImage image = imageFromPixmap(pixmap); pixmap.destroy(); @@ -37,9 +40,19 @@ public class PageCanvas extends java.awt.Canvas return image; } - public PageCanvas(Page page_) { + public PageCanvas(Page page_, float nativeScale) { + mRetinaScale = nativeScale; + mScale = mRetinaScale; this.page = page_; - image = imageFromPage(page, new Matrix()); + run(); + } + + private void run() + { + Matrix ctm = new Matrix(); + ctm.scale(mScale); + image = imageFromPage(page, ctm); + repaint(); } public Dimension getPreferredSize() { @@ -54,7 +67,25 @@ public class PageCanvas extends java.awt.Canvas return getPreferredSize(); } - public void paint(Graphics g) { - g.drawImage(image, 0, 0, null); + public void zoomIn() { + mScale += 0.25f; + run(); + } + + public void zoomOut() { + mScale -= 0.25f; + run(); } + + public void paint(Graphics g) + { + float scale = 1.0f; + scale = 1/mRetinaScale; + + final Graphics2D g2d = (Graphics2D)g.create(0, 0, image.getWidth(), image.getHeight()); + g2d.scale(scale, scale); + g2d.drawImage(image, 0, 0, null); + g2d.dispose(); + } + } diff --git a/platform/java/example/Viewer.java b/platform/java/example/Viewer.java index 91b0fa1d..310aeb15 100644 --- a/platform/java/example/Viewer.java +++ b/platform/java/example/Viewer.java @@ -14,10 +14,14 @@ import java.awt.event.ActionListener; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; +import java.awt.GraphicsEnvironment; +import java.awt.GraphicsDevice; +import java.awt.Toolkit; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; import javax.swing.JOptionPane; +import java.lang.reflect.Field; public class Viewer extends Frame implements WindowListener, ActionListener { @@ -25,19 +29,23 @@ public class Viewer extends Frame implements WindowListener, ActionListener protected Panel toolbar; protected PageCanvas pageCanvas; protected Label pageLabel; - protected Button firstButton, prevButton, nextButton, lastButton; + protected Button firstButton, prevButton, nextButton, lastButton, zoomInButton, zoomOutButton; protected int pageCount; protected int pageNumber; + private float retinaScale; + public Viewer(Document doc_) { super("MuPDF"); + retinaScale = getRetinaScale(); + this.doc = doc_; pageCount = doc.countPages(); pageNumber = 0; - setSize(600, 900); + setSize(1200, 900); setTitle("MuPDF: " + doc.getMetaData(Document.META_INFO_TITLE)); toolbar = new Panel(); @@ -50,12 +58,18 @@ public class Viewer extends Frame implements WindowListener, ActionListener nextButton.addActionListener(this); lastButton = new Button(">|"); lastButton.addActionListener(this); + zoomInButton = new Button("+"); + zoomInButton.addActionListener(this); + zoomOutButton = new Button("-"); + zoomOutButton.addActionListener(this); pageLabel = new Label(); toolbar.add(firstButton); toolbar.add(prevButton); toolbar.add(nextButton); toolbar.add(lastButton); + toolbar.add(zoomInButton); + toolbar.add(zoomOutButton); toolbar.add(pageLabel); add(toolbar, BorderLayout.NORTH); @@ -69,7 +83,7 @@ public class Viewer extends Frame implements WindowListener, ActionListener pageLabel.setText("Page " + (pageNumber + 1) + " / " + pageCount); if (pageCanvas != null) remove(pageCanvas); - pageCanvas = new PageCanvas(doc.loadPage(pageNumber)); + pageCanvas = new PageCanvas(doc.loadPage(pageNumber), retinaScale); add(pageCanvas, BorderLayout.CENTER); validate(); } @@ -80,19 +94,30 @@ public class Viewer extends Frame implements WindowListener, ActionListener 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 (source == zoomInButton) { + pageCanvas.zoomIn(); + } + + if (source == zoomOutButton) { + pageCanvas.zoomOut(); + } + if (pageNumber != oldPageNumber) stuff(); } @@ -200,4 +225,38 @@ public class Viewer extends Frame implements WindowListener, ActionListener { JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE); } + + public float getRetinaScale() + { + // first try Oracle's VM (we should also test for 1.7.0_40 or higher) + final String vendor = System.getProperty("java.vm.vendor"); + boolean isOracle = vendor != null && vendor.toLowerCase().contains("Oracle".toLowerCase()); + if (isOracle) + { + GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); + final GraphicsDevice device = env.getDefaultScreenDevice(); + try { + Field field = device.getClass().getDeclaredField("scale"); + if (field != null) { + field.setAccessible(true); + Object scale = field.get(device); + if (scale instanceof Integer && ((Integer)scale).intValue() == 2) { + return 2.0f; + } + } + } + catch (Exception ignore) { + } + return 1.0f; + } + + // try Apple VM + final Float scaleFactor = (Float)Toolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor"); + if (scaleFactor != null && scaleFactor.intValue() == 2) { + return 2.0f; + } + + return 1.0f; + } + } |