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

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