Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NativeTextHandler.java 6.0KB

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