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.8KB

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