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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.svg;
  19. import java.awt.Color;
  20. import java.awt.Graphics2D;
  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.AFPGraphics2D;
  25. import org.apache.fop.afp.AFPPaintingState;
  26. import org.apache.fop.afp.fonts.AFPFont;
  27. import org.apache.fop.afp.fonts.AFPFontAttributes;
  28. import org.apache.fop.afp.fonts.AFPPageFonts;
  29. import org.apache.fop.afp.modca.GraphicsObject;
  30. import org.apache.fop.fonts.Font;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.svg.FOPTextHandler;
  33. /**
  34. * Specialized TextHandler implementation that the AFPGraphics2D class delegates to to paint text
  35. * using AFP GOCA text operations.
  36. */
  37. public class AFPTextHandler implements FOPTextHandler {
  38. /** logging instance */
  39. private static Log log = LogFactory.getLog(AFPTextHandler.class);
  40. /** Overriding FontState */
  41. protected Font overrideFont = null;
  42. /** Font information */
  43. private final FontInfo fontInfo;
  44. /**
  45. * Main constructor.
  46. *
  47. * @param fontInfo the AFPGraphics2D instance
  48. */
  49. public AFPTextHandler(FontInfo fontInfo) {
  50. this.fontInfo = fontInfo;
  51. }
  52. /**
  53. * Return the font information associated with this object
  54. *
  55. * @return the FontInfo object
  56. */
  57. public FontInfo getFontInfo() {
  58. return fontInfo;
  59. }
  60. /**
  61. * Registers a page font
  62. *
  63. * @param internalFontName the internal font name
  64. * @param internalFontName the internal font name
  65. * @param fontSize the font size
  66. * @return a font reference
  67. */
  68. private int registerPageFont(AFPPageFonts pageFonts, String internalFontName, int fontSize) {
  69. AFPFont afpFont = (AFPFont)fontInfo.getFonts().get(internalFontName);
  70. // register if necessary
  71. AFPFontAttributes afpFontAttributes = pageFonts.registerFont(
  72. internalFontName,
  73. afpFont,
  74. fontSize
  75. );
  76. return afpFontAttributes.getFontReference();
  77. }
  78. /** {@inheritDoc} */
  79. public void drawString(String text, float x, float y) throws IOException {
  80. // TODO Remove me after removing the deprecated method in TextHandler.
  81. throw new UnsupportedOperationException("Deprecated method!");
  82. }
  83. /**
  84. * Add a text string to the current data object of the AFP datastream.
  85. * The text is painted using text operations.
  86. *
  87. * {@inheritDoc}
  88. */
  89. public void drawString(Graphics2D g, String str, float x, float y) throws IOException {
  90. if (log.isDebugEnabled()) {
  91. log.debug("drawString() str=" + str + ", x=" + x + ", y=" + y);
  92. }
  93. if (g instanceof AFPGraphics2D) {
  94. AFPGraphics2D g2d = (AFPGraphics2D)g;
  95. GraphicsObject graphicsObj = g2d.getGraphicsObject();
  96. Color color = g2d.getColor();
  97. // set the color
  98. AFPPaintingState paintingState = g2d.getPaintingState();
  99. if (paintingState.setColor(color)) {
  100. graphicsObj.setColor(color);
  101. }
  102. // set the character set
  103. int fontReference = 0;
  104. int fontSize;
  105. String internalFontName;
  106. AFPPageFonts pageFonts = paintingState.getPageFonts();
  107. if (overrideFont != null) {
  108. internalFontName = overrideFont.getFontName();
  109. fontSize = overrideFont.getFontSize();
  110. } else {
  111. java.awt.Font awtFont = g2d.getFont();
  112. Font fopFont = fontInfo.getFontInstanceForAWTFont(awtFont);
  113. internalFontName = fopFont.getFontName();
  114. fontSize = fopFont.getFontSize();
  115. }
  116. fontSize = (int)Math.round(
  117. g2d.convertToAbsoluteLength(fontSize));
  118. fontReference = registerPageFont(pageFonts, internalFontName, fontSize);
  119. graphicsObj.setCharacterSet(fontReference);
  120. // add the character string
  121. graphicsObj.addString(str, Math.round(x), Math.round(y));
  122. } else {
  123. //Inside Batik's SVG filter operations, you won't get an AFPGraphics2D
  124. g.drawString(str, x, y);
  125. }
  126. }
  127. /**
  128. * Sets the overriding font.
  129. *
  130. * @param overrideFont Overriding Font to set
  131. */
  132. public void setOverrideFont(Font overrideFont) {
  133. this.overrideFont = overrideFont;
  134. }
  135. }