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.

PDFTextElementBridge.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.svg;
  18. import org.apache.batik.gvt.TextNode;
  19. import org.apache.batik.bridge.SVGTextElementBridge;
  20. import org.apache.batik.bridge.BridgeContext;
  21. import org.apache.batik.bridge.TextUtilities;
  22. import org.apache.batik.gvt.GraphicsNode;
  23. import org.apache.fop.apps.Document;
  24. import org.w3c.dom.Element;
  25. import org.w3c.dom.Node;
  26. /**
  27. * Bridge class for the <text> element.
  28. * This bridge will use the direct text painter if the text
  29. * for the element is simple.
  30. *
  31. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  32. */
  33. public class PDFTextElementBridge extends SVGTextElementBridge {
  34. private PDFTextPainter pdfTextPainter;
  35. /**
  36. * Constructs a new bridge for the &lt;text> element.
  37. * @param fi the font infomration
  38. */
  39. public PDFTextElementBridge(Document fi) {
  40. pdfTextPainter = new PDFTextPainter(fi);
  41. }
  42. /**
  43. * Create a text element bridge.
  44. * This set the text painter on the node if the text is simple.
  45. * @param ctx the bridge context
  46. * @param e the svg element
  47. * @return the text graphics node created by the super class
  48. */
  49. public GraphicsNode createGraphicsNode(BridgeContext ctx, Element e) {
  50. GraphicsNode node = super.createGraphicsNode(ctx, e);
  51. if (node != null && isSimple(ctx, e, node)) {
  52. ((TextNode)node).setTextPainter(getTextPainter());
  53. }
  54. return node;
  55. }
  56. private PDFTextPainter getTextPainter() {
  57. return pdfTextPainter;
  58. }
  59. /**
  60. * Check if text element contains simple text.
  61. * This checks the children of the text element to determine
  62. * if the text is simple. The text is simple if it can be rendered
  63. * with basic text drawing algorithms. This means there are no
  64. * alternate characters, the font is known and there are no effects
  65. * applied to the text.
  66. *
  67. * @param ctx the bridge context
  68. * @param element the svg text element
  69. * @param node the graphics node
  70. * @return true if this text is simple of false if it cannot be
  71. * easily rendered using normal drawString on the PDFGraphics2D
  72. */
  73. private boolean isSimple(BridgeContext ctx, Element element, GraphicsNode node) {
  74. // Font size, in user space units.
  75. float fs = TextUtilities.convertFontSize(element).floatValue();
  76. // PDF cannot display fonts over 36pt
  77. if (fs > 36) {
  78. return false;
  79. }
  80. Element nodeElement;
  81. for (Node n = element.getFirstChild();
  82. n != null;
  83. n = n.getNextSibling()) {
  84. switch (n.getNodeType()) {
  85. case Node.ELEMENT_NODE:
  86. nodeElement = (Element)n;
  87. if (n.getLocalName().equals(SVG_TSPAN_TAG)
  88. || n.getLocalName().equals(SVG_ALT_GLYPH_TAG)) {
  89. return false;
  90. } else if (n.getLocalName().equals(SVG_TEXT_PATH_TAG)) {
  91. return false;
  92. } else if (n.getLocalName().equals(SVG_TREF_TAG)) {
  93. return false;
  94. }
  95. break;
  96. case Node.TEXT_NODE:
  97. case Node.CDATA_SECTION_NODE:
  98. }
  99. }
  100. /*if (CSSUtilities.convertFilter(element, node, ctx) != null) {
  101. return false;
  102. }*/
  103. return true;
  104. }
  105. }