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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.gvt.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. * @param fontFamilyResolver the Font Family Resolver
  53. */
  54. public PDFTextPainter(FontInfo fi) {
  55. super(fi);
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. protected boolean isSupported(Graphics2D g2d) {
  60. return g2d instanceof PDFGraphics2D;
  61. }
  62. @Override
  63. protected void preparePainting(Graphics2D g2d) {
  64. pdf = (PDFGraphics2D) g2d;
  65. }
  66. @Override
  67. protected void saveGraphicsState() {
  68. pdf.saveGraphicsState();
  69. }
  70. @Override
  71. protected void restoreGraphicsState() {
  72. pdf.restoreGraphicsState();
  73. }
  74. @Override
  75. protected void setInitialTransform(AffineTransform transform) {
  76. createTextUtil();
  77. textUtil.concatMatrix(transform);
  78. }
  79. private void createTextUtil() {
  80. textUtil = new PDFTextUtil(pdf.fontInfo) {
  81. protected void write(String code) {
  82. pdf.currentStream.write(code);
  83. }
  84. protected void write(StringBuffer code) {
  85. pdf.currentStream.append(code);
  86. }
  87. };
  88. }
  89. @Override
  90. protected void clip(Shape clip) {
  91. pdf.writeClip(clip);
  92. }
  93. private static int[] paZero = new int[4];
  94. protected void writeGlyphs(FOPGVTGlyphVector gv, GeneralPath debugShapes) throws IOException {
  95. if (gv.getGlyphPositionAdjustments() == null) {
  96. super.writeGlyphs(gv, debugShapes);
  97. } else {
  98. FOPGVTFont gvtFont = (FOPGVTFont) gv.getFont();
  99. String fk = gvtFont.getFontKey();
  100. Font f = gvtFont.getFont();
  101. Point2D initialPos = gv.getGlyphPosition(0);
  102. if (f.isMultiByte()) {
  103. int fs = f.getFontSize();
  104. float fsPoints = fs / 1000f;
  105. double xc = 0f;
  106. double yc = 0f;
  107. double xoLast = 0f;
  108. double yoLast = 0f;
  109. textUtil.writeTextMatrix(new AffineTransform(1, 0, 0, -1, initialPos.getX(), initialPos.getY()));
  110. textUtil.updateTf(fk, fsPoints, true, false);
  111. int[][] dp = gv.getGlyphPositionAdjustments();
  112. for (int i = 0, n = gv.getNumGlyphs(); i < n; i++) {
  113. int gc = gv.getGlyphCode(i);
  114. int[] pa = ((i > dp.length) || (dp[i] == null)) ? paZero : dp[i];
  115. double xo = xc + pa[0];
  116. double yo = yc + pa[1];
  117. double xa = f.getWidth(gc);
  118. double ya = 0;
  119. double xd = (xo - xoLast) / 1000f;
  120. double yd = (yo - yoLast) / 1000f;
  121. textUtil.writeTd(xd, yd);
  122. textUtil.writeTj((char) gc, true, false);
  123. xc += xa + pa[2];
  124. yc += ya + pa[3];
  125. xoLast = xo;
  126. yoLast = yo;
  127. }
  128. }
  129. }
  130. }
  131. @Override
  132. protected void beginTextObject() {
  133. applyColorAndPaint(tpi);
  134. textUtil.beginTextObject();
  135. boolean stroke = (tpi.strokePaint != null) && (tpi.strokeStroke != null);
  136. textUtil.setTextRenderingMode(tpi.fillPaint != null, stroke, false);
  137. }
  138. @Override
  139. protected void endTextObject() {
  140. textUtil.writeTJ();
  141. textUtil.endTextObject();
  142. }
  143. private void applyColorAndPaint(TextPaintInfo tpi) {
  144. Paint fillPaint = tpi.fillPaint;
  145. Paint strokePaint = tpi.strokePaint;
  146. Stroke stroke = tpi.strokeStroke;
  147. int fillAlpha = PDFGraphics2D.OPAQUE;
  148. if (fillPaint instanceof Color) {
  149. Color col = (Color) fillPaint;
  150. pdf.applyColor(col, true);
  151. fillAlpha = col.getAlpha();
  152. }
  153. if (strokePaint instanceof Color) {
  154. Color col = (Color) strokePaint;
  155. pdf.applyColor(col, false);
  156. }
  157. pdf.applyPaint(fillPaint, true);
  158. pdf.applyStroke(stroke);
  159. if (strokePaint != null) {
  160. pdf.applyPaint(strokePaint, false);
  161. }
  162. pdf.applyAlpha(fillAlpha, PDFGraphics2D.OPAQUE);
  163. }
  164. @Override
  165. protected void positionGlyph(Point2D prevPos, Point2D glyphPos, boolean reposition) {
  166. // TODO Glyph transforms could be refined so not every char has to be painted
  167. // with its own TJ command (stretch/squeeze case could be optimized)
  168. repositionNextGlyph = (prevPos == null
  169. || prevPos.getY() != glyphPos.getY()
  170. || reposition);
  171. if (!repositionNextGlyph) {
  172. double xdiff = glyphPos.getX() - prevPos.getX();
  173. //Width of previous character
  174. double cw = prevVisibleGlyphWidth;
  175. double effxdiff = (1000 * xdiff) - cw;
  176. if (effxdiff != 0) {
  177. double adjust = (-effxdiff / font.getFontSize());
  178. textUtil.adjustGlyphTJ(adjust * 1000);
  179. }
  180. }
  181. }
  182. @Override
  183. protected void writeGlyph(char glyph, AffineTransform transform) {
  184. prevVisibleGlyphWidth = font.getWidth(glyph);
  185. boolean encodingChanging = false; // used for single byte
  186. if (!textUtil.isMultiByteFont(font.getFontName())) {
  187. int encoding = glyph / 256;
  188. glyph = (char) (glyph % 256);
  189. if (textUtil.getCurrentEncoding() != encoding) {
  190. textUtil.setCurrentEncoding(encoding);
  191. encodingChanging = true;
  192. }
  193. }
  194. if (repositionNextGlyph || encodingChanging) {
  195. textUtil.writeTJ();
  196. if (font != textUtil.getCurrentFont() || encodingChanging) {
  197. textUtil.setCurrentFont(font);
  198. textUtil.writeTf(font);
  199. }
  200. textUtil.writeTextMatrix(transform);
  201. }
  202. textUtil.writeTJMappedChar(glyph);
  203. }
  204. }