PageCanvas.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. package example;
  23. import com.artifex.mupdf.fitz.*;
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.awt.image.*;
  27. public class PageCanvas extends Canvas
  28. {
  29. protected float pixelScale;
  30. protected BufferedImage image;
  31. protected Rect[] links;
  32. protected Quad[][] hits;
  33. public PageCanvas(float pixelScale) {
  34. this.pixelScale = pixelScale;
  35. }
  36. public void setPage(BufferedImage image, Rect[] links, Quad[][] hits) {
  37. this.image = image;
  38. this.links = links;
  39. this.hits = hits;
  40. repaint();
  41. }
  42. public Dimension getPreferredSize() {
  43. if (image == null)
  44. return new Dimension(600, 700);
  45. return new Dimension(image.getWidth(), image.getHeight());
  46. }
  47. public void paint(Graphics g) {
  48. if (image == null)
  49. return;
  50. float imageScale = 1 / pixelScale;
  51. final Graphics2D g2d = (Graphics2D)g.create(0, 0, image.getWidth(), image.getHeight());
  52. g2d.scale(imageScale, imageScale);
  53. g2d.drawImage(image, 0, 0, null);
  54. if (hits != null) {
  55. g2d.setColor(new Color(1, 0, 0, 0.4f));
  56. for (Quad[] hit : hits)
  57. for (Quad q : hit) {
  58. int[] x = new int[]{(int)q.ul_x, (int)q.ur_x, (int)q.lr_x, (int)q.ll_x };
  59. int[] y = new int[]{(int)q.ul_y, (int)q.ur_y, (int)q.lr_y, (int)q.ll_y };
  60. g2d.fillPolygon(x, y, 4);
  61. }
  62. }
  63. if (links != null) {
  64. g2d.setColor(new Color(0, 0, 1, 0.1f));
  65. for (Rect link : links)
  66. g2d.fillRect((int)link.x0, (int)link.y0, (int)(link.x1-link.x0), (int)(link.y1-link.y0));
  67. }
  68. g2d.dispose();
  69. }
  70. public Dimension getMinimumSize() { return getPreferredSize(); }
  71. public Dimension getMaximumSize() { return getPreferredSize(); }
  72. public void update(Graphics g) { paint(g); }
  73. }