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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.GeneralPath;
  26. import java.awt.geom.Point2D;
  27. import java.io.IOException;
  28. import org.apache.batik.gvt.text.TextPaintInfo;
  29. import org.apache.fop.fonts.Font;
  30. import org.apache.fop.fonts.FontInfo;
  31. import org.apache.fop.svg.font.FOPGVTFont;
  32. import org.apache.fop.svg.font.FOPGVTGlyphVector;
  33. /**
  34. * Renders the attributed character iterator of a {@link org.apache.batik.bridge.TextNode}.
  35. * This class draws the text directly into the PDFGraphics2D so that
  36. * the text is not drawn using shapes which makes the PDF files larger.
  37. * If the text is simple enough to draw then it sets the font and calls
  38. * drawString. If the text is complex or the cannot be translated
  39. * into a simple drawString the StrokingTextPainter is used instead.
  40. *
  41. * @version $Id$
  42. */
  43. class PDFTextPainter extends NativeTextPainter {
  44. private PDFGraphics2D pdf;
  45. private PDFTextUtil textUtil;
  46. private double prevVisibleGlyphWidth;
  47. private boolean repositionNextGlyph;
  48. /**
  49. * Create a new PDF text painter with the given font information.
  50. *
  51. * @param fi the font info
  52. */
  53. public PDFTextPainter(FontInfo fi) {
  54. super(fi);
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. protected boolean isSupported(Graphics2D g2d) {
  59. return g2d instanceof PDFGraphics2D;
  60. }
  61. @Override
  62. protected void preparePainting(Graphics2D g2d) {
  63. pdf = (PDFGraphics2D) g2d;
  64. }
  65. @Override
  66. protected void saveGraphicsState() {
  67. pdf.saveGraphicsState();
  68. }
  69. @Override
  70. protected void restoreGraphicsState() {
  71. pdf.restoreGraphicsState();
  72. }
  73. @Override
  74. protected void setInitialTransform(AffineTransform transform) {
  75. createTextUtil();
  76. textUtil.concatMatrix(transform);
  77. }
  78. private void createTextUtil() {
  79. textUtil = new PDFTextUtil(pdf.fontInfo) {
  80. protected void write(String code) {
  81. pdf.currentStream.write(code);
  82. }
  83. protected void write(StringBuffer code) {
  84. pdf.currentStream.append(code);
  85. }
  86. };
  87. }
  88. @Override
  89. protected void clip(Shape clip) {
  90. pdf.writeClip(clip);
  91. }
  92. private static int[] paZero = new int[4];
  93. protected void writeGlyphs(FOPGVTGlyphVector gv, GeneralPath debugShapes) throws IOException {
  94. if (gv.getGlyphPositionAdjustments() == null) {
  95. super.writeGlyphs(gv, debugShapes);
  96. } else {
  97. FOPGVTFont gvtFont = (FOPGVTFont) gv.getFont();
  98. String fk = gvtFont.getFontKey();
  99. Font f = gvtFont.getFont();
  100. Point2D initialPos = gv.getGlyphPosition(0);
  101. if (f.isMultiByte()) {
  102. int fs = f.getFontSize();
  103. float fsPoints = fs / 1000f;
  104. double xc = 0f;
  105. double yc = 0f;
  106. double xoLast = 0f;
  107. double yoLast = 0f;
  108. textUtil.writeTextMatrix(new AffineTransform(1, 0, 0, -1, initialPos.getX(), initialPos.getY()));
  109. textUtil.updateTf(fk, fsPoints, true, false);
  110. int[][] dp = gv.getGlyphPositionAdjustments();
  111. for (int i = 0, n = gv.getNumGlyphs(); i < n; i++) {
  112. int gc = gv.getGlyphCode(i);
  113. int[] pa = ((i > dp.length) || (dp[i] == null)) ? paZero : dp[i];
  114. double xo = xc + pa[0];
  115. double yo = yc + pa[1];
  116. double xa = f.getWidth(gc);
  117. double ya = 0;
  118. double xd = (xo - xoLast) / 1000f;
  119. double yd = (yo - yoLast) / 1000f;
  120. textUtil.writeTd(xd, yd);
  121. textUtil.writeTj((char) gc, true, false);
  122. xc += xa + pa[2];
  123. yc += ya + pa[3];
  124. xoLast = xo;
  125. yoLast = yo;
  126. }
  127. }
  128. }
  129. }
  130. @Override
  131. protected void beginTextObject() {
  132. applyColorAndPaint(tpi);
  133. textUtil.beginTextObject();
  134. boolean stroke = (tpi.strokePaint != null) && (tpi.strokeStroke != null);
  135. textUtil.setTextRenderingMode(tpi.fillPaint != null, stroke, false);
  136. }
  137. @Override
  138. protected void endTextObject() {
  139. textUtil.writeTJ();
  140. textUtil.endTextObject();
  141. }
  142. private void applyColorAndPaint(TextPaintInfo tpi) {
  143. Paint fillPaint = tpi.fillPaint;
  144. Paint strokePaint = tpi.strokePaint;
  145. Stroke stroke = tpi.strokeStroke;
  146. int fillAlpha = PDFGraphics2D.OPAQUE;
  147. if (fillPaint instanceof Color) {
  148. Color col = (Color) fillPaint;
  149. pdf.applyColor(col, true);
  150. fillAlpha = col.getAlpha();
  151. }
  152. if (strokePaint instanceof Color) {
  153. Color col = (Color) strokePaint;
  154. pdf.applyColor(col, false);
  155. }
  156. pdf.applyPaint(fillPaint, true);
  157. pdf.applyStroke(stroke);
  158. if (strokePaint != null) {
  159. pdf.applyPaint(strokePaint, false);
  160. }
  161. pdf.applyAlpha(fillAlpha, PDFGraphics2D.OPAQUE);
  162. }
  163. @Override
  164. protected void positionGlyph(Point2D prevPos, Point2D glyphPos, boolean reposition) {
  165. // TODO Glyph transforms could be refined so not every char has to be painted
  166. // with its own TJ command (stretch/squeeze case could be optimized)
  167. repositionNextGlyph = (prevPos == null
  168. || prevPos.getY() != glyphPos.getY()
  169. || reposition);
  170. if (!repositionNextGlyph) {
  171. double xdiff = glyphPos.getX() - prevPos.getX();
  172. //Width of previous character
  173. double cw = prevVisibleGlyphWidth;
  174. double effxdiff = (1000 * xdiff) - cw;
  175. if (effxdiff != 0) {
  176. double adjust = (-effxdiff / font.getFontSize());
  177. textUtil.adjustGlyphTJ(adjust * 1000);
  178. }
  179. }
  180. }
  181. @Override
  182. protected void writeGlyph(char glyph, AffineTransform transform) {
  183. prevVisibleGlyphWidth = font.getWidth(glyph);
  184. boolean encodingChanging = false; // used for single byte
  185. if (!textUtil.isMultiByteFont(font.getFontName())) {
  186. int encoding = glyph / 256;
  187. glyph = (char) (glyph % 256);
  188. if (textUtil.getCurrentEncoding() != encoding) {
  189. textUtil.setCurrentEncoding(encoding);
  190. encodingChanging = true;
  191. }
  192. }
  193. if (repositionNextGlyph || encodingChanging) {
  194. textUtil.writeTJ();
  195. if (font != textUtil.getCurrentFont() || encodingChanging) {
  196. textUtil.setCurrentFont(font);
  197. textUtil.writeTf(font);
  198. }
  199. textUtil.writeTextMatrix(transform);
  200. }
  201. textUtil.writeTJMappedChar(glyph);
  202. }
  203. }