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.

NativeTextHandler.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 1999-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.ps;
  18. import java.awt.Shape;
  19. import java.awt.geom.AffineTransform;
  20. import java.io.IOException;
  21. import org.apache.fop.fonts.Font;
  22. import org.apache.fop.fonts.FontInfo;
  23. import org.apache.fop.fonts.FontSetup;
  24. import org.apache.fop.fonts.FontTriplet;
  25. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  26. import org.apache.xmlgraphics.java2d.ps.TextHandler;
  27. import org.apache.xmlgraphics.ps.PSGenerator;
  28. /**
  29. * Specialized TextHandler implementation that the PSGraphics2D class delegates to to paint text
  30. * using PostScript text operations.
  31. */
  32. public class NativeTextHandler implements TextHandler {
  33. private PSGraphics2D g2d;
  34. /** FontInfo containing all available fonts */
  35. protected FontInfo fontInfo;
  36. /** Currently valid Font */
  37. protected Font font;
  38. /** Overriding FontState */
  39. protected Font overrideFont = null;
  40. /** the current (internal) font name */
  41. protected String currentFontName;
  42. /** the current font size in millipoints */
  43. protected int currentFontSize;
  44. /**
  45. * Main constructor.
  46. * @param g2d the PSGraphics2D instance this instances is used by
  47. * @param fontInfo the FontInfo object with all available fonts
  48. */
  49. public NativeTextHandler(PSGraphics2D g2d, FontInfo fontInfo) {
  50. this.g2d = g2d;
  51. if (fontInfo != null) {
  52. this.fontInfo = fontInfo;
  53. } else {
  54. setupFontInfo();
  55. }
  56. }
  57. private void setupFontInfo() {
  58. //Sets up a FontInfo with default fonts
  59. fontInfo = new FontInfo();
  60. FontSetup.setup(fontInfo, null, null);
  61. }
  62. /**
  63. * Return the font information associated with this object
  64. * @return the FontInfo object
  65. */
  66. public FontInfo getFontInfo() {
  67. return fontInfo;
  68. }
  69. private PSGenerator getPSGenerator() {
  70. return this.g2d.getPSGenerator();
  71. }
  72. /** @see org.apache.xmlgraphics.java2d.ps.TextHandler#writeSetup() */
  73. public void writeSetup() throws IOException {
  74. if (fontInfo != null) {
  75. PSFontUtils.writeFontDict(getPSGenerator(), fontInfo);
  76. }
  77. }
  78. /** @see org.apache.xmlgraphics.java2d.ps.TextHandler#writePageSetup() */
  79. public void writePageSetup() throws IOException {
  80. if (fontInfo != null) {
  81. getPSGenerator().writeln("FOPFonts begin");
  82. }
  83. }
  84. /**
  85. * Draw a string to the PostScript document. The text is painted using
  86. * text operations.
  87. * @see org.apache.xmlgraphics.java2d.ps.TextHandler#drawString(java.lang.String, float, float)
  88. */
  89. public void drawString(String s, float x, float y) throws IOException {
  90. g2d.preparePainting();
  91. if (this.overrideFont == null) {
  92. java.awt.Font awtFont = g2d.getFont();
  93. this.font = createFont(awtFont);
  94. } else {
  95. this.font = this.overrideFont;
  96. this.overrideFont = null;
  97. }
  98. //Color and Font state
  99. g2d.establishColor(g2d.getColor());
  100. establishCurrentFont();
  101. PSGenerator gen = getPSGenerator();
  102. gen.saveGraphicsState();
  103. //Clip
  104. Shape imclip = g2d.getClip();
  105. g2d.writeClip(imclip);
  106. //Prepare correct transformation
  107. AffineTransform trans = g2d.getTransform();
  108. gen.concatMatrix(trans);
  109. gen.writeln(gen.formatDouble(x) + " "
  110. + gen.formatDouble(y) + " moveto ");
  111. gen.writeln("1 -1 scale");
  112. StringBuffer sb = new StringBuffer("(");
  113. escapeText(s, sb);
  114. sb.append(") t ");
  115. gen.writeln(sb.toString());
  116. gen.restoreGraphicsState();
  117. }
  118. private void escapeText(final String text, StringBuffer target) {
  119. final int l = text.length();
  120. for (int i = 0; i < l; i++) {
  121. final char ch = text.charAt(i);
  122. final char mch = this.font.mapChar(ch);
  123. PSGenerator.escapeChar(mch, target);
  124. }
  125. }
  126. private Font createFont(java.awt.Font f) {
  127. String fontFamily = f.getFamily();
  128. if (fontFamily.equals("sanserif")) {
  129. fontFamily = "sans-serif";
  130. }
  131. int fontSize = 1000 * f.getSize();
  132. String style = f.isItalic() ? "italic" : "normal";
  133. int weight = f.isBold() ? Font.BOLD : Font.NORMAL;
  134. FontTriplet triplet = fontInfo.findAdjustWeight(fontFamily, style, weight);
  135. if (triplet == null) {
  136. triplet = fontInfo.findAdjustWeight("sans-serif", style, weight);
  137. }
  138. return fontInfo.getFontInstance(triplet, fontSize);
  139. }
  140. private void establishCurrentFont() throws IOException {
  141. if ((currentFontName != this.font.getFontName())
  142. || (currentFontSize != this.font.getFontSize())) {
  143. PSGenerator gen = getPSGenerator();
  144. gen.writeln(this.font.getFontName() + " "
  145. + gen.formatDouble(font.getFontSize() / 1000f) + " F");
  146. currentFontName = this.font.getFontName();
  147. currentFontSize = this.font.getFontSize();
  148. }
  149. }
  150. /**
  151. * Sets the overriding font.
  152. * @param override Overriding Font to set
  153. */
  154. public void setOverrideFont(Font override) {
  155. this.overrideFont = override;
  156. }
  157. }