comparison mupdf-source/platform/java/example/PageCanvas.java @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2022 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
23 package example;
24
25 import com.artifex.mupdf.fitz.*;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.awt.image.*;
30
31 public class PageCanvas extends Canvas
32 {
33 protected float pixelScale;
34 protected BufferedImage image;
35 protected Rect[] links;
36 protected Quad[][] hits;
37
38 public PageCanvas(float pixelScale) {
39 this.pixelScale = pixelScale;
40 }
41
42 public void setPage(BufferedImage image, Rect[] links, Quad[][] hits) {
43 this.image = image;
44 this.links = links;
45 this.hits = hits;
46 repaint();
47 }
48
49 public Dimension getPreferredSize() {
50 if (image == null)
51 return new Dimension(600, 700);
52
53 return new Dimension(image.getWidth(), image.getHeight());
54 }
55
56 public void paint(Graphics g) {
57 if (image == null)
58 return;
59
60 float imageScale = 1 / pixelScale;
61 final Graphics2D g2d = (Graphics2D)g.create(0, 0, image.getWidth(), image.getHeight());
62 g2d.scale(imageScale, imageScale);
63 g2d.drawImage(image, 0, 0, null);
64
65 if (hits != null) {
66 g2d.setColor(new Color(1, 0, 0, 0.4f));
67
68 for (Quad[] hit : hits)
69 for (Quad q : hit) {
70 int[] x = new int[]{(int)q.ul_x, (int)q.ur_x, (int)q.lr_x, (int)q.ll_x };
71 int[] y = new int[]{(int)q.ul_y, (int)q.ur_y, (int)q.lr_y, (int)q.ll_y };
72 g2d.fillPolygon(x, y, 4);
73 }
74 }
75
76 if (links != null) {
77 g2d.setColor(new Color(0, 0, 1, 0.1f));
78 for (Rect link : links)
79 g2d.fillRect((int)link.x0, (int)link.y0, (int)(link.x1-link.x0), (int)(link.y1-link.y0));
80 }
81
82 g2d.dispose();
83 }
84
85 public Dimension getMinimumSize() { return getPreferredSize(); }
86 public Dimension getMaximumSize() { return getPreferredSize(); }
87 public void update(Graphics g) { paint(g); }
88 }