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.

AFPTextHandler.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.afp;
  19. import java.awt.Color;
  20. import java.awt.geom.AffineTransform;
  21. import java.io.IOException;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.afp.fonts.AFPFont;
  25. import org.apache.fop.afp.fonts.AFPFontAttributes;
  26. import org.apache.fop.afp.modca.GraphicsObject;
  27. import org.apache.fop.fonts.Font;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.xmlgraphics.java2d.TextHandler;
  30. /**
  31. * Specialized TextHandler implementation that the AFPGraphics2D class delegates to to paint text
  32. * using AFP GOCA text operations.
  33. */
  34. public class AFPTextHandler implements TextHandler {
  35. /** logging instance */
  36. private static Log log = LogFactory.getLog(AFPTextHandler.class);
  37. private static final int X = 0;
  38. private static final int Y = 1;
  39. private AFPGraphics2D g2d = null;
  40. /** Overriding FontState */
  41. protected Font overrideFont = null;
  42. /**
  43. * Main constructor.
  44. * @param g2d the AFPGraphics2D instance
  45. */
  46. public AFPTextHandler(AFPGraphics2D g2d) {
  47. this.g2d = g2d;
  48. }
  49. /**
  50. * Return the font information associated with this object
  51. *
  52. * @return the FontInfo object
  53. */
  54. public FontInfo getFontInfo() {
  55. return g2d.getFontInfo();
  56. }
  57. /**
  58. * Add a text string to the current data object of the AFP datastream.
  59. * The text is painted using text operations.
  60. *
  61. * {@inheritDoc}
  62. */
  63. public void drawString(String str, float x, float y) throws IOException {
  64. log.debug("drawString() str=" + str + ", x=" + x + ", y=" + y);
  65. GraphicsObject graphicsObj = g2d.getGraphicsObject();
  66. Color color = g2d.getColor();
  67. AFPPaintingState paintingState = g2d.getPaintingState();
  68. if (paintingState.setColor(color)) {
  69. graphicsObj.setColor(color);
  70. }
  71. if (overrideFont != null) {
  72. FontInfo fontInfo = getFontInfo();
  73. AFPPageFonts pageFonts = paintingState.getPageFonts();
  74. String internalFontName = overrideFont.getFontName();
  75. int fontSize = overrideFont.getFontSize();
  76. if (paintingState.setFontName(internalFontName) || paintingState.setFontSize(fontSize)) {
  77. AFPFont font = (AFPFont)fontInfo.getFonts().get(internalFontName);
  78. AFPFontAttributes afpFontAttributes = pageFonts.registerFont(
  79. internalFontName,
  80. font,
  81. fontSize
  82. );
  83. int fontReference = afpFontAttributes.getFontReference();
  84. graphicsObj.setCharacterSet(fontReference);
  85. }
  86. }
  87. // calculate x, y plotting coordinates from graphics context
  88. AffineTransform at = g2d.getTransform();
  89. float[] srcPts = new float[] { x, y };
  90. float[] dstPts = new float[srcPts.length];
  91. at.transform(srcPts, 0, dstPts, 0, 1);
  92. graphicsObj.addString(str, Math.round(dstPts[X]), Math.round(dstPts[Y]));
  93. }
  94. /**
  95. * Sets the overriding font.
  96. *
  97. * @param overrideFont Overriding Font to set
  98. */
  99. public void setOverrideFont(Font overrideFont) {
  100. this.overrideFont = overrideFont;
  101. }
  102. }