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.

AbstractFOPTextPainter.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.svg;
  19. import java.awt.Color;
  20. import java.awt.Graphics2D;
  21. import java.awt.Paint;
  22. import java.awt.Shape;
  23. import java.awt.geom.Point2D;
  24. import java.awt.geom.Rectangle2D;
  25. import java.io.IOException;
  26. import java.text.AttributedCharacterIterator;
  27. import java.text.CharacterIterator;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.batik.gvt.TextNode;
  31. import org.apache.batik.gvt.TextPainter;
  32. import org.apache.batik.gvt.renderer.StrokingTextPainter;
  33. import org.apache.batik.gvt.text.GVTAttributedCharacterIterator;
  34. import org.apache.batik.gvt.text.Mark;
  35. import org.apache.batik.gvt.text.TextPaintInfo;
  36. import org.apache.fop.afp.AFPGraphics2D;
  37. import org.apache.fop.fonts.Font;
  38. /**
  39. * Renders the attributed character iterator of a {@link TextNode}.
  40. * This class draws the text directly into the Graphics2D so that
  41. * the text is not drawn using shapes.
  42. * If the text is simple enough to draw then it sets the font and calls
  43. * drawString. If the text is complex or the cannot be translated
  44. * into a simple drawString the StrokingTextPainter is used instead.
  45. */
  46. public abstract class AbstractFOPTextPainter implements TextPainter {
  47. /** the logger for this class */
  48. protected Log log = LogFactory.getLog(AbstractFOPTextPainter.class);
  49. private final FOPTextHandler nativeTextHandler;
  50. /**
  51. * Use the stroking text painter to get the bounds and shape.
  52. * Also used as a fallback to draw the string with strokes.
  53. */
  54. private final TextPainter proxyTextPainter;
  55. /**
  56. * Create a new PS text painter with the given font information.
  57. * @param nativeTextHandler the NativeTextHandler instance used for text painting
  58. */
  59. public AbstractFOPTextPainter(FOPTextHandler nativeTextHandler, TextPainter proxyTextPainter) {
  60. this.nativeTextHandler = nativeTextHandler;
  61. this.proxyTextPainter = proxyTextPainter;
  62. }
  63. /**
  64. * Paints the specified attributed character iterator using the
  65. * specified Graphics2D and context and font context.
  66. *
  67. * @param node the TextNode to paint
  68. * @param g2d the Graphics2D to use
  69. */
  70. public void paint(TextNode node, Graphics2D g2d) {
  71. if (isSupportedGraphics2D(g2d)) {
  72. new TextRunPainter().paintTextRuns(node.getTextRuns(), g2d, node.getLocation());
  73. }
  74. proxyTextPainter.paint(node, g2d);
  75. }
  76. /**
  77. * Checks whether the Graphics2D is compatible with this text painter. Batik may
  78. * pass in a Graphics2D instance that paints on a special buffer image, for example
  79. * for filtering operations. In that case, the text painter should be bypassed.
  80. * @param g2d the Graphics2D instance to check
  81. * @return true if the Graphics2D is supported
  82. */
  83. protected abstract boolean isSupportedGraphics2D(Graphics2D g2d);
  84. private class TextRunPainter {
  85. private Point2D currentLocation;
  86. public void paintTextRuns(Iterable<StrokingTextPainter.TextRun> textRuns, Graphics2D g2d,
  87. Point2D nodeLocation) {
  88. currentLocation = new Point2D.Double(nodeLocation.getX(), nodeLocation.getY());
  89. for (StrokingTextPainter.TextRun run : textRuns) {
  90. paintTextRun(run, g2d);
  91. }
  92. }
  93. private void paintTextRun(StrokingTextPainter.TextRun run, Graphics2D g2d) {
  94. AttributedCharacterIterator aci = run.getACI();
  95. aci.first();
  96. updateLocationFromACI(aci, currentLocation);
  97. // font
  98. Font font = getFont(aci);
  99. if (font != null) {
  100. nativeTextHandler.setOverrideFont(font);
  101. }
  102. // color
  103. TextPaintInfo tpi = (TextPaintInfo) aci.getAttribute(
  104. GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);
  105. if (tpi == null) {
  106. return;
  107. }
  108. Paint foreground = tpi.fillPaint;
  109. if (foreground instanceof Color) {
  110. Color col = (Color) foreground;
  111. g2d.setColor(col);
  112. }
  113. g2d.setPaint(foreground);
  114. // text anchor
  115. TextNode.Anchor anchor = (TextNode.Anchor) aci.getAttribute(
  116. GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE);
  117. // text
  118. String txt = getText(aci);
  119. double advance = font == null ? run.getLayout().getAdvance2D().getX() : getStringWidth(txt, font);
  120. double tx = 0;
  121. if (anchor != null) {
  122. switch (anchor.getType()) {
  123. case TextNode.Anchor.ANCHOR_MIDDLE:
  124. tx = -advance / 2;
  125. break;
  126. case TextNode.Anchor.ANCHOR_END:
  127. tx = -advance;
  128. break;
  129. default: //nop
  130. }
  131. }
  132. // draw string
  133. Point2D outputLocation = g2d.getTransform().transform(currentLocation, null);
  134. double x = outputLocation.getX();
  135. double y = outputLocation.getY();
  136. try {
  137. try {
  138. //TODO draw underline and overline if set
  139. nativeTextHandler.drawString(g2d, txt, (float) (x + tx), (float) y);
  140. //TODO draw strikethrough if set
  141. } catch (IOException ioe) {
  142. if (g2d instanceof AFPGraphics2D) {
  143. ((AFPGraphics2D) g2d).handleIOException(ioe);
  144. }
  145. }
  146. } finally {
  147. nativeTextHandler.setOverrideFont(null);
  148. }
  149. currentLocation.setLocation(currentLocation.getX() + advance, currentLocation.getY());
  150. }
  151. private void updateLocationFromACI(AttributedCharacterIterator aci, Point2D loc) {
  152. //Adjust position of span
  153. Float xpos = (Float) aci.getAttribute(
  154. GVTAttributedCharacterIterator.TextAttribute.X);
  155. Float ypos = (Float) aci.getAttribute(
  156. GVTAttributedCharacterIterator.TextAttribute.Y);
  157. Float dxpos = (Float) aci.getAttribute(
  158. GVTAttributedCharacterIterator.TextAttribute.DX);
  159. Float dypos = (Float) aci.getAttribute(
  160. GVTAttributedCharacterIterator.TextAttribute.DY);
  161. if (xpos != null) {
  162. loc.setLocation(xpos.doubleValue(), loc.getY());
  163. }
  164. if (ypos != null) {
  165. loc.setLocation(loc.getX(), ypos.doubleValue());
  166. }
  167. if (dxpos != null) {
  168. loc.setLocation(loc.getX() + dxpos.doubleValue(), loc.getY());
  169. }
  170. if (dypos != null) {
  171. loc.setLocation(loc.getX(), loc.getY() + dypos.doubleValue());
  172. }
  173. }
  174. }
  175. /**
  176. * Extract the raw text from an ACI.
  177. * @param aci ACI to inspect
  178. * @return the extracted text
  179. */
  180. protected String getText(AttributedCharacterIterator aci) {
  181. StringBuffer sb = new StringBuffer(aci.getEndIndex() - aci.getBeginIndex());
  182. for (char c = aci.first(); c != CharacterIterator.DONE; c = aci.next()) {
  183. sb.append(c);
  184. }
  185. return sb.toString();
  186. }
  187. private Font getFont(AttributedCharacterIterator aci) {
  188. Font[] fonts = ACIUtils.findFontsForBatikACI(aci, nativeTextHandler.getFontInfo());
  189. return fonts == null ? null : fonts[0];
  190. }
  191. private float getStringWidth(String str, Font font) {
  192. float wordWidth = 0;
  193. float whitespaceWidth = font.getWidth(font.mapChar(' '));
  194. for (int i = 0; i < str.length(); i++) {
  195. float charWidth;
  196. char c = str.charAt(i);
  197. if (!((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))) {
  198. charWidth = font.getWidth(font.mapChar(c));
  199. if (charWidth <= 0) {
  200. charWidth = whitespaceWidth;
  201. }
  202. } else {
  203. charWidth = whitespaceWidth;
  204. }
  205. wordWidth += charWidth;
  206. }
  207. return wordWidth / 1000f;
  208. }
  209. /**
  210. * Get the outline shape of the text characters.
  211. * This uses the StrokingTextPainter to get the outline
  212. * shape since in theory it should be the same.
  213. *
  214. * @param node the text node
  215. * @return the outline shape of the text characters
  216. */
  217. public Shape getOutline(TextNode node) {
  218. return proxyTextPainter.getOutline(node);
  219. }
  220. /**
  221. * Get the bounds.
  222. * This uses the StrokingTextPainter to get the bounds
  223. * since in theory it should be the same.
  224. *
  225. * @param node the text node
  226. * @return the bounds of the text
  227. */
  228. public Rectangle2D getBounds2D(TextNode node) {
  229. /* (todo) getBounds2D() is too slow
  230. * because it uses the StrokingTextPainter. We should implement this
  231. * method ourselves. */
  232. return proxyTextPainter.getBounds2D(node);
  233. }
  234. /**
  235. * Get the geometry bounds.
  236. * This uses the StrokingTextPainter to get the bounds
  237. * since in theory it should be the same.
  238. *
  239. * @param node the text node
  240. * @return the bounds of the text
  241. */
  242. public Rectangle2D getGeometryBounds(TextNode node) {
  243. return proxyTextPainter.getGeometryBounds(node);
  244. }
  245. // Methods that have no purpose for PS
  246. /**
  247. * Get the mark.
  248. * This does nothing since the output is AFP and not interactive.
  249. *
  250. * @param node the text node
  251. * @param pos the position
  252. * @param all select all
  253. * @return null
  254. */
  255. public Mark getMark(TextNode node, int pos, boolean all) {
  256. return null;
  257. }
  258. /**
  259. * Select at.
  260. * This does nothing since the output is AFP and not interactive.
  261. *
  262. * @param x the x position
  263. * @param y the y position
  264. * @param node the text node
  265. * @return null
  266. */
  267. public Mark selectAt(double x, double y, TextNode node) {
  268. return null;
  269. }
  270. /**
  271. * Select to.
  272. * This does nothing since the output is AFP and not interactive.
  273. *
  274. * @param x the x position
  275. * @param y the y position
  276. * @param beginMark the start mark
  277. * @return null
  278. */
  279. public Mark selectTo(double x, double y, Mark beginMark) {
  280. return null;
  281. }
  282. /**
  283. * Selec first.
  284. * This does nothing since the output is AFP and not interactive.
  285. *
  286. * @param node the text node
  287. * @return null
  288. */
  289. public Mark selectFirst(TextNode node) {
  290. return null;
  291. }
  292. /**
  293. * Select last.
  294. * This does nothing since the output is AFP and not interactive.
  295. *
  296. * @param node the text node
  297. * @return null
  298. */
  299. public Mark selectLast(TextNode node) {
  300. return null;
  301. }
  302. /**
  303. * Get selected.
  304. * This does nothing since the output is AFP and not interactive.
  305. *
  306. * @param start the start mark
  307. * @param finish the finish mark
  308. * @return null
  309. */
  310. public int[] getSelected(Mark start, Mark finish) {
  311. return null;
  312. }
  313. /**
  314. * Get the highlighted shape.
  315. * This does nothing since the output is AFP and not interactive.
  316. *
  317. * @param beginMark the start mark
  318. * @param endMark the end mark
  319. * @return null
  320. */
  321. public Shape getHighlightShape(Mark beginMark, Mark endMark) {
  322. return null;
  323. }
  324. }