You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFANode.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.svg;
  19. import java.awt.Graphics2D;
  20. import java.awt.Shape;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.util.StringTokenizer;
  24. import org.apache.batik.gvt.CompositeGraphicsNode;
  25. /**
  26. * <p>A graphics node that represents an image described as a graphics node.</p>
  27. *
  28. * <p>This work was authored by Keiron Liddle (keiron@aftexsw.com).</p>
  29. */
  30. public class PDFANode extends CompositeGraphicsNode {
  31. private String destination;
  32. private AffineTransform transform;
  33. /**
  34. * Constructs a new empty {@link PDFANode}.
  35. */
  36. public PDFANode() {
  37. }
  38. /**
  39. * Set the destination String.
  40. * @param dest the target destination
  41. */
  42. public void setDestination(String dest) {
  43. destination = dest;
  44. }
  45. /**
  46. * Set the current transform of this node.
  47. * @param tf the transform
  48. */
  49. public void setTransform(AffineTransform tf) {
  50. transform = tf;
  51. }
  52. /**
  53. * Paints this node if visible.
  54. *
  55. * @param g2d the Graphics2D to use
  56. */
  57. public void paint(Graphics2D g2d) {
  58. if (isVisible) {
  59. super.paint(g2d);
  60. if (g2d instanceof PDFGraphics2D) {
  61. PDFGraphics2D pdfg = (PDFGraphics2D)g2d;
  62. int type = org.apache.fop.pdf.PDFLink.EXTERNAL;
  63. Shape outline = getOutline();
  64. if (destination.startsWith("#svgView(viewBox(")) {
  65. type = org.apache.fop.pdf.PDFLink.INTERNAL;
  66. String nums = destination.substring(17, destination.length() - 2);
  67. float x = 0;
  68. float y = 0;
  69. float width = 0;
  70. float height = 0;
  71. int count = 0;
  72. try {
  73. StringTokenizer st = new StringTokenizer(nums, ",");
  74. while (st.hasMoreTokens()) {
  75. String tok = st.nextToken();
  76. count++;
  77. switch(count) {
  78. case 1:
  79. x = Float.parseFloat(tok);
  80. break;
  81. case 2:
  82. y = Float.parseFloat(tok);
  83. break;
  84. case 3:
  85. width = Float.parseFloat(tok);
  86. break;
  87. case 4:
  88. height = Float.parseFloat(tok);
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. } catch (Exception e) {
  95. //TODO Move this to setDestination() and throw an IllegalArgumentException
  96. e.printStackTrace();
  97. }
  98. Rectangle2D destRect = new Rectangle2D.Float(x, y, width, height);
  99. destRect = transform.createTransformedShape(destRect).getBounds();
  100. // these numbers need conversion to current
  101. // svg position and scaled for the page
  102. x = (float)destRect.getX();
  103. y = (float)destRect.getY();
  104. width = (float)destRect.getWidth();
  105. height = (float)destRect.getHeight();
  106. destination = "" + x + " " + y + " "
  107. + (x + width) + " " + (y + height);
  108. }
  109. pdfg.addLink(getBounds(), transform, destination, type);
  110. }
  111. }
  112. }
  113. }