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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.xmlgraphics.java2d.ps.PSGraphics2D;
  24. import org.apache.xmlgraphics.java2d.ps.PSTextHandler;
  25. import org.apache.xmlgraphics.ps.PSGenerator;
  26. import org.apache.fop.fonts.Font;
  27. import org.apache.fop.fonts.FontInfo;
  28. import org.apache.fop.fonts.FontSetup;
  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 rootG2D;
  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.rootG2D = 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. boolean base14Kerning = false;
  62. FontSetup.setup(fontInfo, base14Kerning);
  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.rootG2D.getPSGenerator();
  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. /**
  85. * Draw a string to the PostScript document. The text is painted using
  86. * text operations.
  87. * {@inheritDoc}
  88. */
  89. public void drawString(Graphics2D g, String s, float x, float y) throws IOException {
  90. PSGraphics2D g2d = (PSGraphics2D)g;
  91. g2d.preparePainting();
  92. if (this.overrideFont == null) {
  93. java.awt.Font awtFont = g2d.getFont();
  94. this.font = createFont(awtFont);
  95. } else {
  96. this.font = this.overrideFont;
  97. this.overrideFont = null;
  98. }
  99. //Color and Font state
  100. g2d.establishColor(g2d.getColor());
  101. establishCurrentFont();
  102. PSGenerator gen = getPSGenerator();
  103. gen.saveGraphicsState();
  104. //Clip
  105. Shape imclip = g2d.getClip();
  106. g2d.writeClip(imclip);
  107. //Prepare correct transformation
  108. AffineTransform trans = g2d.getTransform();
  109. gen.concatMatrix(trans);
  110. gen.writeln(gen.formatDouble(x) + " "
  111. + gen.formatDouble(y) + " moveto ");
  112. gen.writeln("1 -1 scale");
  113. StringBuffer sb = new StringBuffer("(");
  114. escapeText(s, sb);
  115. sb.append(") t ");
  116. gen.writeln(sb.toString());
  117. gen.restoreGraphicsState();
  118. }
  119. private void escapeText(final String text, StringBuffer target) {
  120. final int l = text.length();
  121. for (int i = 0; i < l; i++) {
  122. final char ch = text.charAt(i);
  123. final char mch = this.font.mapChar(ch);
  124. PSGenerator.escapeChar(mch, target);
  125. }
  126. }
  127. private Font createFont(java.awt.Font f) {
  128. return fontInfo.getFontInstanceForAWTFont(f);
  129. }
  130. private void establishCurrentFont() throws IOException {
  131. if ((currentFontName != this.font.getFontName())
  132. || (currentFontSize != this.font.getFontSize())) {
  133. PSGenerator gen = getPSGenerator();
  134. gen.writeln("/" + this.font.getFontTriplet().getName() + " "
  135. + gen.formatDouble(font.getFontSize() / 1000f) + " F");
  136. currentFontName = this.font.getFontName();
  137. currentFontSize = this.font.getFontSize();
  138. }
  139. }
  140. /**
  141. * Sets the overriding font.
  142. * @param override Overriding Font to set
  143. */
  144. public void setOverrideFont(Font override) {
  145. this.overrideFont = override;
  146. }
  147. }