Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AFPTextHandler.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.AFPResourceManager;
  27. import org.apache.fop.afp.fonts.AFPFont;
  28. import org.apache.fop.afp.fonts.AFPFontAttributes;
  29. import org.apache.fop.afp.fonts.AFPPageFonts;
  30. import org.apache.fop.afp.fonts.CharacterSet;
  31. import org.apache.fop.afp.modca.GraphicsObject;
  32. import org.apache.fop.fonts.Font;
  33. import org.apache.fop.fonts.FontInfo;
  34. import org.apache.fop.svg.FOPTextHandlerAdapter;
  35. /**
  36. * Specialized TextHandler implementation that the AFPGraphics2D class delegates to to paint text
  37. * using AFP GOCA text operations.
  38. */
  39. public class AFPTextHandler extends FOPTextHandlerAdapter {
  40. /** logging instance */
  41. private static Log log = LogFactory.getLog(AFPTextHandler.class);
  42. /** Overriding FontState */
  43. protected Font overrideFont = null;
  44. /** Font information */
  45. private final FontInfo fontInfo;
  46. /** the resource manager */
  47. private AFPResourceManager resourceManager;
  48. /**
  49. * Main constructor.
  50. *
  51. * @param fontInfo the AFPGraphics2D instance
  52. * @param resourceManager the AFPResourceManager instance
  53. */
  54. public AFPTextHandler(FontInfo fontInfo, AFPResourceManager resourceManager) {
  55. this.fontInfo = fontInfo;
  56. this.resourceManager = resourceManager;
  57. }
  58. /**
  59. * Return the font information associated with this object
  60. *
  61. * @return the FontInfo object
  62. */
  63. public FontInfo getFontInfo() {
  64. return fontInfo;
  65. }
  66. /**
  67. * Registers a page font
  68. *
  69. * @param internalFontName the internal font name
  70. * @param fontSize the font size
  71. * @return a font reference
  72. */
  73. private int registerPageFont(AFPPageFonts pageFonts, String internalFontName, int fontSize) {
  74. AFPFont afpFont = (AFPFont)fontInfo.getFonts().get(internalFontName);
  75. // register if necessary
  76. AFPFontAttributes afpFontAttributes = pageFonts.registerFont(
  77. internalFontName,
  78. afpFont,
  79. fontSize
  80. );
  81. if (afpFont.isEmbeddable()) {
  82. try {
  83. final CharacterSet charSet = afpFont.getCharacterSet(fontSize);
  84. this.resourceManager.embedFont(afpFont, charSet);
  85. } catch (IOException ioe) {
  86. throw new RuntimeException("Error while embedding font resources", ioe);
  87. }
  88. }
  89. return afpFontAttributes.getFontReference();
  90. }
  91. /**
  92. * Add a text string to the current data object of the AFP datastream.
  93. * The text is painted using text operations.
  94. *
  95. * {@inheritDoc}
  96. */
  97. @Override
  98. public void drawString(Graphics2D g, String str, float x, float y) {
  99. if (log.isDebugEnabled()) {
  100. log.debug("drawString() str=" + str + ", x=" + x + ", y=" + y);
  101. }
  102. if (g instanceof AFPGraphics2D) {
  103. AFPGraphics2D g2d = (AFPGraphics2D)g;
  104. GraphicsObject graphicsObj = g2d.getGraphicsObject();
  105. Color color = g2d.getColor();
  106. // set the color
  107. AFPPaintingState paintingState = g2d.getPaintingState();
  108. if (paintingState.setColor(color)) {
  109. graphicsObj.setColor(color);
  110. }
  111. // set the character set
  112. int fontReference = 0;
  113. int fontSize;
  114. String internalFontName;
  115. AFPPageFonts pageFonts = paintingState.getPageFonts();
  116. if (overrideFont != null) {
  117. internalFontName = overrideFont.getFontName();
  118. fontSize = overrideFont.getFontSize();
  119. if (log.isDebugEnabled()) {
  120. log.debug(" with overriding font: " + internalFontName + ", " + fontSize);
  121. }
  122. fontSize = (int) Math.round(g2d.convertToAbsoluteLength(fontSize));
  123. fontReference = registerPageFont(pageFonts, internalFontName, fontSize);
  124. // TODO: re-think above registerPageFont code...
  125. AFPFont afpFont = (AFPFont) fontInfo.getFonts().get(internalFontName);
  126. final CharacterSet charSet = afpFont.getCharacterSet(fontSize);
  127. // Work-around for InfoPrint's AFP which loses character set state
  128. // over Graphics Data
  129. // boundaries.
  130. graphicsObj.setCharacterSet(fontReference);
  131. // add the character string
  132. graphicsObj.addString(str, Math.round(x), Math.round(y), charSet);
  133. }
  134. } else {
  135. //Inside Batik's SVG filter operations, you won't get an AFPGraphics2D
  136. g.drawString(str, x, y);
  137. }
  138. }
  139. /**
  140. * Sets the overriding font.
  141. *
  142. * @param overrideFont Overriding Font to set
  143. */
  144. public void setOverrideFont(Font overrideFont) {
  145. this.overrideFont = overrideFont;
  146. }
  147. }