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.

PDFPainter.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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.pdf;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Paint;
  22. import java.awt.Point;
  23. import java.awt.Rectangle;
  24. import java.awt.geom.AffineTransform;
  25. import java.io.FileNotFoundException;
  26. import java.io.IOException;
  27. import java.util.HashSet;
  28. import java.util.Locale;
  29. import java.util.Set;
  30. import org.w3c.dom.Document;
  31. import org.apache.xmlgraphics.image.loader.ImageException;
  32. import org.apache.xmlgraphics.image.loader.ImageInfo;
  33. import org.apache.xmlgraphics.image.loader.ImageManager;
  34. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  35. import org.apache.fop.ResourceEventProducer;
  36. import org.apache.fop.fonts.CustomFont;
  37. import org.apache.fop.fonts.Font;
  38. import org.apache.fop.fonts.FontTriplet;
  39. import org.apache.fop.fonts.LazyFont;
  40. import org.apache.fop.fonts.SingleByteFont;
  41. import org.apache.fop.fonts.Typeface;
  42. import org.apache.fop.pdf.PDFArray;
  43. import org.apache.fop.pdf.PDFDictionary;
  44. import org.apache.fop.pdf.PDFName;
  45. import org.apache.fop.pdf.PDFNumber;
  46. import org.apache.fop.pdf.PDFStructElem;
  47. import org.apache.fop.pdf.PDFTextUtil;
  48. import org.apache.fop.pdf.PDFXObject;
  49. import org.apache.fop.render.RenderingContext;
  50. import org.apache.fop.render.intermediate.AbstractIFPainter;
  51. import org.apache.fop.render.intermediate.BorderPainter;
  52. import org.apache.fop.render.intermediate.GraphicsPainter;
  53. import org.apache.fop.render.intermediate.IFContext;
  54. import org.apache.fop.render.intermediate.IFException;
  55. import org.apache.fop.render.intermediate.IFState;
  56. import org.apache.fop.render.intermediate.IFUtil;
  57. import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
  58. import org.apache.fop.traits.BorderProps;
  59. import org.apache.fop.traits.Direction;
  60. import org.apache.fop.traits.RuleStyle;
  61. import org.apache.fop.util.CharUtilities;
  62. /**
  63. * IFPainter implementation that produces PDF.
  64. */
  65. public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
  66. /** The current content generator */
  67. protected PDFContentGenerator generator;
  68. private final GraphicsPainter graphicsPainter;
  69. private final BorderPainter borderPainter;
  70. private boolean accessEnabled;
  71. private MarkedContentInfo imageMCI;
  72. private PDFLogicalStructureHandler logicalStructureHandler;
  73. private final LanguageAvailabilityChecker languageAvailabilityChecker;
  74. private static class LanguageAvailabilityChecker {
  75. private final IFContext context;
  76. private final Set<String> reportedLocations = new HashSet<String>();
  77. LanguageAvailabilityChecker(IFContext context) {
  78. this.context = context;
  79. }
  80. private void checkLanguageAvailability(String text) {
  81. Locale locale = context.getLanguage();
  82. if (locale == null && containsLettersOrDigits(text)) {
  83. String location = context.getLocation();
  84. if (!reportedLocations.contains(location)) {
  85. PDFEventProducer.Provider.get(context.getUserAgent().getEventBroadcaster())
  86. .unknownLanguage(this, location);
  87. reportedLocations.add(location);
  88. }
  89. }
  90. }
  91. private boolean containsLettersOrDigits(String text) {
  92. for (int i = 0; i < text.length(); i++) {
  93. if (Character.isLetterOrDigit(text.charAt(i))) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. }
  100. /**
  101. * Default constructor.
  102. * @param documentHandler the parent document handler
  103. * @param logicalStructureHandler the logical structure handler
  104. */
  105. public PDFPainter(PDFDocumentHandler documentHandler,
  106. PDFLogicalStructureHandler logicalStructureHandler) {
  107. super(documentHandler);
  108. this.logicalStructureHandler = logicalStructureHandler;
  109. this.generator = documentHandler.getGenerator();
  110. this.graphicsPainter = new PDFGraphicsPainter(this.generator);
  111. this.borderPainter = new BorderPainter(this.graphicsPainter);
  112. this.state = IFState.create();
  113. accessEnabled = this.getUserAgent().isAccessibilityEnabled();
  114. languageAvailabilityChecker = accessEnabled
  115. ? new LanguageAvailabilityChecker(documentHandler.getContext())
  116. : null;
  117. }
  118. /** {@inheritDoc} */
  119. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  120. throws IFException {
  121. generator.saveGraphicsState();
  122. generator.concatenate(toPoints(transform));
  123. if (clipRect != null) {
  124. clipRect(clipRect);
  125. }
  126. }
  127. /** {@inheritDoc} */
  128. public void endViewport() throws IFException {
  129. generator.restoreGraphicsState();
  130. }
  131. /** {@inheritDoc} */
  132. public void startGroup(AffineTransform transform, String layer) throws IFException {
  133. generator.saveGraphicsState(layer);
  134. generator.concatenate(toPoints(transform));
  135. }
  136. /** {@inheritDoc} */
  137. public void endGroup() throws IFException {
  138. generator.restoreGraphicsState();
  139. }
  140. /** {@inheritDoc} */
  141. public void drawImage(String uri, Rectangle rect)
  142. throws IFException {
  143. PDFXObject xobject = getDocumentHandler().getPDFDocument().getXObject(uri);
  144. if (xobject != null) {
  145. if (accessEnabled) {
  146. PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
  147. prepareImageMCID(structElem);
  148. placeImageAccess(rect, xobject);
  149. } else {
  150. placeImage(rect, xobject);
  151. }
  152. } else {
  153. addStructTreeBBox(rect);
  154. drawImageUsingURI(uri, rect);
  155. if (!getDocumentHandler().getPDFDocument().isLinearizationEnabled()) {
  156. flushPDFDoc();
  157. }
  158. }
  159. }
  160. private void addStructTreeBBox(Rectangle rect) {
  161. if (accessEnabled && getDocumentHandler().getPDFDocument().getProfile().getPDFUAMode().isEnabled()) {
  162. PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
  163. if (structElem != null) { //structElem is null if the image is marked as an artifact
  164. PDFDictionary d = new PDFDictionary();
  165. int x = rect.x / 1000;
  166. int y = rect.y / 1000;
  167. int w = rect.width / 1000;
  168. int h = rect.height / 1000;
  169. d.put("BBox", new PDFArray(x, y, w, h));
  170. d.put("O", new PDFName("Layout"));
  171. structElem.put("A", d);
  172. }
  173. }
  174. }
  175. @Override
  176. protected void drawImageUsingURI(String uri, Rectangle rect) {
  177. ImageManager manager = getUserAgent().getImageManager();
  178. ImageInfo info = null;
  179. try {
  180. ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
  181. info = manager.getImageInfo(uri, sessionContext);
  182. if (accessEnabled) {
  183. PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
  184. String mimeType = info.getMimeType();
  185. if (!mimeType.equalsIgnoreCase("application/pdf")) {
  186. prepareImageMCID(structElem);
  187. }
  188. }
  189. drawImageUsingImageHandler(info, rect);
  190. } catch (ImageException ie) {
  191. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  192. getUserAgent().getEventBroadcaster());
  193. eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
  194. } catch (FileNotFoundException fe) {
  195. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  196. getUserAgent().getEventBroadcaster());
  197. eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
  198. } catch (IOException ioe) {
  199. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  200. getUserAgent().getEventBroadcaster());
  201. eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
  202. }
  203. }
  204. private void prepareImageMCID(PDFStructElem structElem) {
  205. imageMCI = logicalStructureHandler.addImageContentItem(structElem);
  206. if (structElem != null) {
  207. languageAvailabilityChecker.checkLanguageAvailability((String) structElem.get("Alt"));
  208. }
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. protected RenderingContext createRenderingContext() {
  213. PDFRenderingContext pdfContext = new PDFRenderingContext(
  214. getUserAgent(), generator, getDocumentHandler().getCurrentPage(), getFontInfo());
  215. pdfContext.setMarkedContentInfo(imageMCI);
  216. pdfContext.setPageNumbers(getDocumentHandler().getPageNumbers());
  217. pdfContext.setPdfLogicalStructureHandler(logicalStructureHandler);
  218. pdfContext.setCurrentSessionStructElem((PDFStructElem) getContext().getStructureTreeElement());
  219. return pdfContext;
  220. }
  221. /**
  222. * Places a previously registered image at a certain place on the page.
  223. * @param rect the rectangle for the image
  224. * @param xobj the image XObject
  225. */
  226. private void placeImage(Rectangle rect, PDFXObject xobj) {
  227. generator.saveGraphicsState();
  228. generator.add(format(rect.width) + " 0 0 "
  229. + format(-rect.height) + " "
  230. + format(rect.x) + " "
  231. + format(rect.y + rect.height)
  232. + " cm " + xobj.getName() + " Do\n");
  233. generator.restoreGraphicsState();
  234. }
  235. /**
  236. * Places a previously registered image at a certain place on the page - Accessibility version
  237. * @param rect the rectangle for the image
  238. * @param xobj the image XObject
  239. */
  240. private void placeImageAccess(Rectangle rect, PDFXObject xobj) {
  241. generator.saveGraphicsState(imageMCI.tag, imageMCI.mcid);
  242. generator.add(format(rect.width) + " 0 0 "
  243. + format(-rect.height) + " "
  244. + format(rect.x) + " "
  245. + format(rect.y + rect.height)
  246. + " cm " + xobj.getName() + " Do\n");
  247. generator.restoreGraphicsStateAccess();
  248. }
  249. /** {@inheritDoc} */
  250. public void drawImage(Document doc, Rectangle rect) throws IFException {
  251. if (accessEnabled) {
  252. PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
  253. prepareImageMCID(structElem);
  254. addStructTreeBBox(rect);
  255. }
  256. drawImageUsingDocument(doc, rect);
  257. if (!getDocumentHandler().getPDFDocument().isLinearizationEnabled()) {
  258. flushPDFDoc();
  259. }
  260. }
  261. private void flushPDFDoc() throws IFException {
  262. // output new data
  263. try {
  264. generator.flushPDFDoc();
  265. } catch (IOException ioe) {
  266. throw new IFException("I/O error flushing the PDF document", ioe);
  267. }
  268. }
  269. /**
  270. * Formats a integer value (normally coordinates in millipoints) to a String.
  271. * @param value the value (in millipoints)
  272. * @return the formatted value
  273. */
  274. protected static String format(int value) {
  275. return PDFNumber.doubleOut(value / 1000f);
  276. }
  277. /** {@inheritDoc} */
  278. public void clipRect(Rectangle rect) throws IFException {
  279. generator.endTextObject();
  280. generator.clipRect(rect);
  281. }
  282. /** {@inheritDoc} */
  283. public void clipBackground(Rectangle rect,
  284. BorderProps bpsBefore, BorderProps bpsAfter,
  285. BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
  286. try {
  287. borderPainter.clipBackground(rect,
  288. bpsBefore, bpsAfter, bpsStart, bpsEnd);
  289. } catch (IOException ioe) {
  290. throw new IFException("I/O error while clipping background", ioe);
  291. }
  292. }
  293. /** {@inheritDoc} */
  294. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  295. if (fill == null) {
  296. return;
  297. }
  298. if (rect.width != 0 && rect.height != 0) {
  299. generator.endTextObject();
  300. if (accessEnabled && getUserAgent().isPdfUAEnabled()) {
  301. generator.beginMarkedContentSequence(null, 0, null);
  302. }
  303. if (fill != null) {
  304. if (fill instanceof Color) {
  305. generator.updateColor((Color)fill, true, null);
  306. } else {
  307. throw new UnsupportedOperationException("Non-Color paints NYI");
  308. }
  309. }
  310. StringBuffer sb = new StringBuffer();
  311. sb.append(format(rect.x)).append(' ');
  312. sb.append(format(rect.y)).append(' ');
  313. sb.append(format(rect.width)).append(' ');
  314. sb.append(format(rect.height)).append(" re");
  315. if (fill != null) {
  316. sb.append(" f");
  317. }
  318. /* Removed from method signature as it is currently not used
  319. if (stroke != null) {
  320. sb.append(" S");
  321. }*/
  322. sb.append('\n');
  323. generator.add(sb.toString());
  324. if (accessEnabled && getUserAgent().isPdfUAEnabled()) {
  325. generator.endMarkedContentSequence();
  326. }
  327. }
  328. }
  329. /** {@inheritDoc} */
  330. @Override
  331. public void drawBorderRect(Rectangle rect, BorderProps top, BorderProps bottom,
  332. BorderProps left, BorderProps right, Color innerBackgroundColor) throws IFException {
  333. if (top != null || bottom != null || left != null || right != null) {
  334. generator.endTextObject();
  335. if (accessEnabled && getUserAgent().isPdfUAEnabled()) {
  336. generator.beginMarkedContentSequence(null, 0, null);
  337. }
  338. this.borderPainter.drawBorders(rect, top, bottom, left, right, innerBackgroundColor);
  339. if (accessEnabled && getUserAgent().isPdfUAEnabled()) {
  340. generator.endMarkedContentSequence();
  341. }
  342. }
  343. }
  344. /** {@inheritDoc} */
  345. @Override
  346. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  347. throws IFException {
  348. generator.endTextObject();
  349. if (accessEnabled && getUserAgent().isPdfUAEnabled()) {
  350. generator.beginMarkedContentSequence(null, 0, null);
  351. }
  352. try {
  353. this.graphicsPainter.drawLine(start, end, width, color, style);
  354. } catch (IOException ioe) {
  355. throw new IFException("Cannot draw line", ioe);
  356. }
  357. if (accessEnabled && getUserAgent().isPdfUAEnabled()) {
  358. generator.endMarkedContentSequence();
  359. }
  360. }
  361. private Typeface getTypeface(String fontName) {
  362. if (fontName == null) {
  363. throw new NullPointerException("fontName must not be null");
  364. }
  365. Typeface tf = getFontInfo().getFonts().get(fontName);
  366. if (tf instanceof LazyFont) {
  367. tf = ((LazyFont)tf).getRealFont();
  368. }
  369. return tf;
  370. }
  371. /** {@inheritDoc} */
  372. public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[][] dp,
  373. String text)
  374. throws IFException {
  375. if (accessEnabled) {
  376. PDFStructElem structElem = (PDFStructElem) getContext().getStructureTreeElement();
  377. languageAvailabilityChecker.checkLanguageAvailability(text);
  378. MarkedContentInfo mci = logicalStructureHandler.addTextContentItem(structElem);
  379. String actualText = getContext().isHyphenated() ? text.substring(0, text.length() - 1) : null;
  380. generator.endTextObject();
  381. generator.updateColor(state.getTextColor(), true, null);
  382. generator.beginTextObject(mci.tag, mci.mcid, actualText);
  383. } else {
  384. generator.updateColor(state.getTextColor(), true, null);
  385. generator.beginTextObject();
  386. }
  387. FontTriplet triplet = new FontTriplet(
  388. state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
  389. if ((dp == null) || IFUtil.isDPOnlyDX(dp)) {
  390. drawTextWithDX(x, y, text, triplet, letterSpacing,
  391. wordSpacing, IFUtil.convertDPToDX(dp));
  392. } else {
  393. drawTextWithDP(x, y, text, triplet, letterSpacing,
  394. wordSpacing, dp);
  395. }
  396. }
  397. public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[][] dp, String text, boolean nextIsSpace)
  398. throws IFException {
  399. if (accessEnabled && nextIsSpace) {
  400. text += ' ';
  401. }
  402. drawText(x, y, letterSpacing, wordSpacing, dp, text);
  403. }
  404. private void drawTextWithDX(int x, int y, String text, FontTriplet triplet,
  405. int letterSpacing, int wordSpacing, int[] dx) throws IFException {
  406. //TODO Ignored: state.getFontVariant()
  407. //TODO Opportunity for font caching if font state is more heavily used
  408. String fontKey = getFontKey(triplet);
  409. int sizeMillipoints = state.getFontSize();
  410. float fontSize = sizeMillipoints / 1000f;
  411. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  412. Typeface tf = getTypeface(fontKey);
  413. Font font = getFontInfo().getFontInstance(triplet, sizeMillipoints);
  414. String fontName = font.getFontName();
  415. PDFTextUtil textutil = generator.getTextUtil();
  416. textutil.updateTf(fontKey, fontSize, tf.isMultiByte(), tf.isCID());
  417. double shear = 0;
  418. boolean simulateStyle = tf instanceof CustomFont && ((CustomFont) tf).getSimulateStyle();
  419. if (simulateStyle) {
  420. if (triplet.getWeight() == 700) {
  421. generator.add("q\n");
  422. generator.add("2 Tr 0.31543 w\n");
  423. }
  424. if (triplet.getStyle().equals("italic")) {
  425. shear = 0.3333;
  426. }
  427. }
  428. generator.updateCharacterSpacing(letterSpacing / 1000f);
  429. textutil.writeTextMatrix(new AffineTransform(1, 0, shear, -1, x / 1000f, y / 1000f));
  430. int l = text.length();
  431. int dxl = (dx != null ? dx.length : 0);
  432. if (dx != null && dxl > 0 && dx[0] != 0) {
  433. textutil.adjustGlyphTJ(-dx[0] / fontSize);
  434. }
  435. for (int i = 0; i < l; i++) {
  436. int orgChar = text.charAt(i);
  437. int ch;
  438. // surrogate pairs have to be merged in a single code point
  439. if (CharUtilities.containsSurrogatePairAt(text, i)) {
  440. orgChar = Character.toCodePoint((char) orgChar, text.charAt(++i));
  441. }
  442. float glyphAdjust = 0;
  443. if (font.hasCodePoint(orgChar)) {
  444. ch = font.mapCodePoint(orgChar);
  445. ch = selectAndMapSingleByteFont(tf, fontName, fontSize, textutil, ch);
  446. if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
  447. glyphAdjust += wordSpacing;
  448. }
  449. } else {
  450. if (CharUtilities.isFixedWidthSpace(orgChar)) {
  451. //Fixed width space are rendered as spaces so copy/paste works in a reader
  452. ch = font.mapChar(CharUtilities.SPACE);
  453. int spaceDiff = font.getCharWidth(CharUtilities.SPACE) - font.getCharWidth(orgChar);
  454. glyphAdjust = -spaceDiff;
  455. } else {
  456. ch = font.mapCodePoint(orgChar);
  457. if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
  458. glyphAdjust += wordSpacing;
  459. }
  460. }
  461. ch = selectAndMapSingleByteFont(tf, fontName, fontSize, textutil, ch);
  462. }
  463. textutil.writeTJMappedCodePoint(ch);
  464. if (dx != null && i < dxl - 1) {
  465. glyphAdjust += dx[i + 1];
  466. }
  467. if (glyphAdjust != 0) {
  468. textutil.adjustGlyphTJ(-glyphAdjust / fontSize);
  469. }
  470. }
  471. textutil.writeTJ();
  472. if (simulateStyle && triplet.getWeight() == 700) {
  473. generator.add("Q\n");
  474. }
  475. }
  476. private static int[] paZero = new int[4];
  477. private void drawTextWithDP(int x, int y, String text, FontTriplet triplet,
  478. int letterSpacing, int wordSpacing, int[][] dp) {
  479. assert text != null;
  480. assert triplet != null;
  481. assert dp != null;
  482. String fk = getFontInfo().getInternalFontKey(triplet);
  483. Typeface tf = getTypeface(fk);
  484. if (tf.isMultiByte() || tf.isCID()) {
  485. int fs = state.getFontSize();
  486. float fsPoints = fs / 1000f;
  487. Font f = getFontInfo().getFontInstance(triplet, fs);
  488. PDFTextUtil tu = generator.getTextUtil();
  489. double xc = 0f;
  490. double yc = 0f;
  491. double xoLast = 0f;
  492. double yoLast = 0f;
  493. double wox = wordSpacing;
  494. // FOP-2810
  495. boolean simulateStyle = tf instanceof CustomFont && ((CustomFont) tf).getSimulateStyle();
  496. double shear = 0;
  497. if (simulateStyle) {
  498. if (triplet.getWeight() == 700) {
  499. generator.add("q\n");
  500. generator.add("2 Tr 0.31543 w\n");
  501. }
  502. if (triplet.getStyle().equals("italic")) {
  503. shear = 0.3333;
  504. }
  505. }
  506. tu.writeTextMatrix(new AffineTransform(1, 0, shear, -1, x / 1000f, y / 1000f));
  507. tu.updateTf(fk, fsPoints, tf.isMultiByte(), true);
  508. generator.updateCharacterSpacing(letterSpacing / 1000f);
  509. for (int i = 0, n = text.length(); i < n; i++) {
  510. char ch = text.charAt(i);
  511. int[] pa = ((i >= dp.length) || (dp[i] == null)) ? paZero : dp[i];
  512. double xo = xc + pa[0];
  513. double yo = yc + pa[1];
  514. double xa = f.getCharWidth(ch) + maybeWordOffsetX(wox, ch, null);
  515. double ya = 0;
  516. double xd = (xo - xoLast) / 1000f;
  517. double yd = (yo - yoLast) / 1000f;
  518. tu.writeTd(xd, yd);
  519. tu.writeTj(f.mapChar(ch), tf.isMultiByte(), true);
  520. xc += xa + pa[2];
  521. yc += ya + pa[3];
  522. xoLast = xo;
  523. yoLast = yo;
  524. }
  525. }
  526. }
  527. private double maybeWordOffsetX(double wox, char ch, Direction dir) {
  528. if ((wox != 0)
  529. && CharUtilities.isAdjustableSpace(ch)
  530. && ((dir == null) || dir.isHorizontal())) {
  531. return wox;
  532. } else {
  533. return 0;
  534. }
  535. }
  536. /*
  537. private double maybeWordOffsetY ( double woy, char ch, Direction dir ) {
  538. if ( ( woy != 0 )
  539. && CharUtilities.isAdjustableSpace ( ch ) && dir.isVertical()
  540. && ( ( dir != null ) && dir.isVertical() ) ) {
  541. return woy;
  542. } else {
  543. return 0;
  544. }
  545. }
  546. */
  547. private int selectAndMapSingleByteFont(Typeface tf, String fontName, float fontSize, PDFTextUtil textutil,
  548. int ch) {
  549. if ((tf instanceof SingleByteFont && ((SingleByteFont)tf).hasAdditionalEncodings()) || tf.isCID()) {
  550. int encoding = ch / 256;
  551. if (encoding == 0) {
  552. textutil.updateTf(fontName, fontSize, tf.isMultiByte(), tf.isCID());
  553. } else {
  554. textutil.updateTf(fontName + "_" + Integer.toString(encoding),
  555. fontSize, tf.isMultiByte(), tf.isCID());
  556. ch = (char)(ch % 256);
  557. }
  558. }
  559. return ch;
  560. }
  561. }