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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.render.ps;
  19. import java.awt.Shape;
  20. import java.awt.geom.AffineTransform;
  21. import java.io.IOException;
  22. import org.apache.fop.fonts.Font;
  23. import org.apache.fop.fonts.FontInfo;
  24. import org.apache.fop.fonts.FontSetup;
  25. import org.apache.fop.fonts.FontTriplet;
  26. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  27. import org.apache.xmlgraphics.java2d.ps.PSTextHandler;
  28. import org.apache.xmlgraphics.ps.PSGenerator;
  29. /**
  30. * Specialized TextHandler implementation that the PSGraphics2D class delegates to to paint text
  31. * using PostScript text operations.
  32. */
  33. public class NativeTextHandler implements PSTextHandler {
  34. private PSGraphics2D g2d;
  35. /** FontInfo containing all available fonts */
  36. protected FontInfo fontInfo;
  37. /** Currently valid Font */
  38. protected Font font;
  39. /** Overriding FontState */
  40. protected Font overrideFont = null;
  41. /** the current (internal) font name */
  42. protected String currentFontName;
  43. /** the current font size in millipoints */
  44. protected int currentFontSize;
  45. /**
  46. * Main constructor.
  47. * @param g2d the PSGraphics2D instance this instances is used by
  48. * @param fontInfo the FontInfo object with all available fonts
  49. */
  50. public NativeTextHandler(PSGraphics2D g2d, FontInfo fontInfo) {
  51. this.g2d = g2d;
  52. if (fontInfo != null) {
  53. this.fontInfo = fontInfo;
  54. } else {
  55. setupFontInfo();
  56. }
  57. }
  58. private void setupFontInfo() {
  59. //Sets up a FontInfo with default fonts
  60. fontInfo = new FontInfo();
  61. FontSetup.setup(fontInfo);
  62. }
  63. /**
  64. * Return the font information associated with this object
  65. * @return the FontInfo object
  66. */
  67. public FontInfo getFontInfo() {
  68. return fontInfo;
  69. }
  70. private PSGenerator getPSGenerator() {
  71. return this.g2d.getPSGenerator();
  72. }
  73. /** {@inheritDoc} */
  74. public void writeSetup() throws IOException {
  75. if (fontInfo != null) {
  76. PSFontUtils.writeFontDict(getPSGenerator(), fontInfo);
  77. }
  78. }
  79. /** {@inheritDoc} */
  80. public void writePageSetup() throws IOException {
  81. //nop
  82. }
  83. /**
  84. * Draw a string to the PostScript document. The text is painted using
  85. * text operations.
  86. * {@inheritDoc}
  87. */
  88. public void drawString(String s, float x, float y) throws IOException {
  89. g2d.preparePainting();
  90. if (this.overrideFont == null) {
  91. java.awt.Font awtFont = g2d.getFont();
  92. this.font = createFont(awtFont);
  93. } else {
  94. this.font = this.overrideFont;
  95. this.overrideFont = null;
  96. }
  97. //Color and Font state
  98. g2d.establishColor(g2d.getColor());
  99. establishCurrentFont();
  100. PSGenerator gen = getPSGenerator();
  101. gen.saveGraphicsState();
  102. //Clip
  103. Shape imclip = g2d.getClip();
  104. g2d.writeClip(imclip);
  105. //Prepare correct transformation
  106. AffineTransform trans = g2d.getTransform();
  107. gen.concatMatrix(trans);
  108. gen.writeln(gen.formatDouble(x) + " "
  109. + gen.formatDouble(y) + " moveto ");
  110. gen.writeln("1 -1 scale");
  111. StringBuffer sb = new StringBuffer("(");
  112. escapeText(s, sb);
  113. sb.append(") t ");
  114. gen.writeln(sb.toString());
  115. gen.restoreGraphicsState();
  116. }
  117. private void escapeText(final String text, StringBuffer target) {
  118. final int l = text.length();
  119. for (int i = 0; i < l; i++) {
  120. final char ch = text.charAt(i);
  121. final char mch = this.font.mapChar(ch);
  122. PSGenerator.escapeChar(mch, target);
  123. }
  124. }
  125. private Font createFont(java.awt.Font f) {
  126. String fontFamily = f.getFamily();
  127. if (fontFamily.equals("sanserif")) {
  128. fontFamily = "sans-serif";
  129. }
  130. int fontSize = 1000 * f.getSize();
  131. String style = f.isItalic() ? "italic" : "normal";
  132. int weight = f.isBold() ? Font.WEIGHT_BOLD : Font.WEIGHT_NORMAL;
  133. FontTriplet triplet = fontInfo.findAdjustWeight(fontFamily, style, weight);
  134. if (triplet == null) {
  135. triplet = fontInfo.findAdjustWeight("sans-serif", style, weight);
  136. }
  137. return fontInfo.getFontInstance(triplet, fontSize);
  138. }
  139. private void establishCurrentFont() throws IOException {
  140. if ((currentFontName != this.font.getFontName())
  141. || (currentFontSize != this.font.getFontSize())) {
  142. PSGenerator gen = getPSGenerator();
  143. gen.writeln(this.font.getFontName() + " "
  144. + gen.formatDouble(font.getFontSize() / 1000f) + " F");
  145. currentFontName = this.font.getFontName();
  146. currentFontSize = this.font.getFontSize();
  147. }
  148. }
  149. /**
  150. * Sets the overriding font.
  151. * @param override Overriding Font to set
  152. */
  153. public void setOverrideFont(Font override) {
  154. this.overrideFont = override;
  155. }
  156. }