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.

PDFTextPainter.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Color;
  20. import java.awt.Graphics2D;
  21. import java.awt.Paint;
  22. import java.awt.Shape;
  23. import java.awt.Stroke;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.geom.Point2D;
  26. import org.apache.batik.gvt.text.TextPaintInfo;
  27. import org.apache.fop.fonts.FontInfo;
  28. /**
  29. * Renders the attributed character iterator of a {@link org.apache.batik.gvt.TextNode}.
  30. * This class draws the text directly into the PDFGraphics2D so that
  31. * the text is not drawn using shapes which makes the PDF files larger.
  32. * If the text is simple enough to draw then it sets the font and calls
  33. * drawString. If the text is complex or the cannot be translated
  34. * into a simple drawString the StrokingTextPainter is used instead.
  35. *
  36. * @version $Id$
  37. */
  38. class PDFTextPainter extends NativeTextPainter {
  39. private PDFGraphics2D pdf;
  40. private PDFTextUtil textUtil;
  41. private double prevVisibleGlyphWidth;
  42. private boolean repositionNextGlyph;
  43. /**
  44. * Create a new PDF text painter with the given font information.
  45. *
  46. * @param fi the font info
  47. * @param fontFamilyResolver the Font Family Resolver
  48. */
  49. public PDFTextPainter(FontInfo fi) {
  50. super(fi);
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. protected boolean isSupported(Graphics2D g2d) {
  55. return g2d instanceof PDFGraphics2D;
  56. }
  57. @Override
  58. protected void preparePainting(Graphics2D g2d) {
  59. pdf = (PDFGraphics2D) g2d;
  60. }
  61. @Override
  62. protected void saveGraphicsState() {
  63. pdf.saveGraphicsState();
  64. }
  65. @Override
  66. protected void restoreGraphicsState() {
  67. pdf.restoreGraphicsState();
  68. }
  69. @Override
  70. protected void setInitialTransform(AffineTransform transform) {
  71. createTextUtil();
  72. textUtil.concatMatrix(transform);
  73. }
  74. private void createTextUtil() {
  75. textUtil = new PDFTextUtil(pdf.fontInfo) {
  76. protected void write(String code) {
  77. pdf.currentStream.write(code);
  78. }
  79. protected void write(StringBuffer code) {
  80. pdf.currentStream.append(code);
  81. }
  82. };
  83. }
  84. @Override
  85. protected void clip(Shape clip) {
  86. pdf.writeClip(clip);
  87. }
  88. @Override
  89. protected void beginTextObject() {
  90. applyColorAndPaint(tpi);
  91. textUtil.beginTextObject();
  92. boolean stroke = (tpi.strokePaint != null) && (tpi.strokeStroke != null);
  93. textUtil.setTextRenderingMode(tpi.fillPaint != null, stroke, false);
  94. }
  95. @Override
  96. protected void endTextObject() {
  97. textUtil.writeTJ();
  98. textUtil.endTextObject();
  99. }
  100. private void applyColorAndPaint(TextPaintInfo tpi) {
  101. Paint fillPaint = tpi.fillPaint;
  102. Paint strokePaint = tpi.strokePaint;
  103. Stroke stroke = tpi.strokeStroke;
  104. int fillAlpha = PDFGraphics2D.OPAQUE;
  105. if (fillPaint instanceof Color) {
  106. Color col = (Color) fillPaint;
  107. pdf.applyColor(col, true);
  108. fillAlpha = col.getAlpha();
  109. }
  110. if (strokePaint instanceof Color) {
  111. Color col = (Color) strokePaint;
  112. pdf.applyColor(col, false);
  113. }
  114. pdf.applyPaint(fillPaint, true);
  115. pdf.applyStroke(stroke);
  116. if (strokePaint != null) {
  117. pdf.applyPaint(strokePaint, false);
  118. }
  119. pdf.applyAlpha(fillAlpha, PDFGraphics2D.OPAQUE);
  120. }
  121. @Override
  122. protected void positionGlyph(Point2D prevPos, Point2D glyphPos, boolean reposition) {
  123. // TODO Glyph transforms could be refined so not every char has to be painted
  124. // with its own TJ command (stretch/squeeze case could be optimized)
  125. repositionNextGlyph = (prevPos == null
  126. || prevPos.getY() != glyphPos.getY()
  127. || reposition);
  128. if (!repositionNextGlyph) {
  129. double xdiff = glyphPos.getX() - prevPos.getX();
  130. //Width of previous character
  131. double cw = prevVisibleGlyphWidth;
  132. double effxdiff = (1000 * xdiff) - cw;
  133. if (effxdiff != 0) {
  134. double adjust = (-effxdiff / font.getFontSize());
  135. textUtil.adjustGlyphTJ(adjust * 1000);
  136. }
  137. }
  138. }
  139. @Override
  140. protected void writeGlyph(char glyph, AffineTransform transform) {
  141. prevVisibleGlyphWidth = font.getWidth(glyph);
  142. boolean encodingChanging = false; // used for single byte
  143. if (!textUtil.isMultiByteFont(font.getFontName())) {
  144. int encoding = glyph / 256;
  145. glyph = (char) (glyph % 256);
  146. if (textUtil.getCurrentEncoding() != encoding) {
  147. textUtil.setCurrentEncoding(encoding);
  148. encodingChanging = true;
  149. }
  150. }
  151. if (repositionNextGlyph || encodingChanging) {
  152. textUtil.writeTJ();
  153. if (font != textUtil.getCurrentFont() || encodingChanging) {
  154. textUtil.setCurrentFont(font);
  155. textUtil.writeTf(font);
  156. }
  157. textUtil.writeTextMatrix(transform);
  158. }
  159. textUtil.writeTJMappedChar(glyph);
  160. }
  161. }