summaryrefslogtreecommitdiff
path: root/platform/java/example/PageCanvas.java
blob: b6471f3d83c8590a569f9c3a7c30f93f52ef953e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;

	protected float mScale = 1.0f;
	private float mRetinaScale = 1.0f;

	private static boolean mShowAnnots = false;

	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) {
		//  make a blank pixmap
		Rect bbox = page.getBounds();
		bbox.transform(ctm);
		Pixmap pixmap = new Pixmap(ColorSpace.DeviceBGR, bbox, true);
		pixmap.clear(255);

		//  make a device
		DrawDevice dev = new DrawDevice(pixmap);

		//  run page contents (without annotations)
		page.runPageContents(dev, ctm, new Cookie());

		//  run annotations
		if (mShowAnnots)
		{
			Annotation annotations[] = page.getAnnotations();
			if (annotations != null && annotations.length>0)
			{
				for (Annotation annot : annotations) {
					annot.run(dev, ctm, new Cookie());
				}
			}
		}

		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, true);
		BufferedImage image = imageFromPixmap(pixmap);
		pixmap.destroy();
		return image;
	}

	public PageCanvas(Page page_, float nativeScale) {
		mRetinaScale = nativeScale;
		mScale = mRetinaScale;
		this.page = page_;
		run();
	}

	private void run()
	{
		Matrix ctm = new Matrix();
		ctm.scale(mScale);
		image = imageFromPageWithDevice(page, ctm);
		repaint();
	}

	public Dimension getPreferredSize() {
		return new Dimension(image.getWidth(), image.getHeight());
	}

	public Dimension getMinimumSize() {
		return getPreferredSize();
	}

	public Dimension getMaximumSize() {
		return getPreferredSize();
	}

	public void zoomIn() {
		if (mScale < 10) {
			mScale += 0.25f;
			run();
		}
	}

	public void zoomOut() {
		if (mScale > 0.25f) {
			mScale -= 0.25f;
			run();
		}
	}

	public void toggleAnnots() {
		mShowAnnots = !mShowAnnots;
		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();
	}

}