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.

PDFTextUtil.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 org.apache.fop.fonts.Font;
  20. import org.apache.fop.fonts.FontInfo;
  21. import org.apache.fop.fonts.Typeface;
  22. /**
  23. * Utility class for generating PDF text objects. It needs to be subclassed to add writing
  24. * functionality (see {@link #write(String)}).
  25. */
  26. public abstract class PDFTextUtil extends org.apache.fop.pdf.PDFTextUtil {
  27. private FontInfo fontInfo;
  28. private Font[] fonts;
  29. private Font font;
  30. private int encoding;
  31. /**
  32. * Main constructor.
  33. * @param fontInfo the font catalog
  34. */
  35. public PDFTextUtil(FontInfo fontInfo) {
  36. super();
  37. this.fontInfo = fontInfo;
  38. }
  39. /** {@inheritDoc} */
  40. protected void initValues() {
  41. super.initValues();
  42. this.font = null;
  43. }
  44. /**
  45. * Sets the current fonts for the text object. For every character, the suitable font will
  46. * be selected.
  47. * @param fonts the new fonts
  48. */
  49. public void setFonts(Font[] fonts) {
  50. this.fonts = fonts;
  51. }
  52. /**
  53. * Sets the current font for the text object.
  54. * @param font the new font
  55. */
  56. public void setFont(Font font) {
  57. setFonts(new Font[] {font});
  58. }
  59. /**
  60. * Returns the current font in use.
  61. * @return the current font or null if no font is currently active.
  62. */
  63. public Font getCurrentFont() {
  64. return this.font;
  65. }
  66. /**
  67. * Returns the current encoding.
  68. * @return the current encoding
  69. */
  70. public int getCurrentEncoding() {
  71. return this.encoding;
  72. }
  73. /**
  74. * Sets the current font.
  75. * @param f the new font to use
  76. */
  77. public void setCurrentFont(Font f) {
  78. this.font = f;
  79. }
  80. /**
  81. * Sets the current encoding.
  82. * @param encoding the new encoding
  83. */
  84. public void setCurrentEncoding(int encoding) {
  85. this.encoding = encoding;
  86. }
  87. /**
  88. * Determines whether the font with the given name is a multi-byte font.
  89. * @param name the name of the font
  90. * @return true if it's a multi-byte font
  91. */
  92. protected boolean isMultiByteFont(String name) {
  93. Typeface f = (Typeface)fontInfo.getFonts().get(name);
  94. return f.isMultiByte();
  95. }
  96. /**
  97. * Writes a "Tf" command, setting a new current font.
  98. * @param f the font to select
  99. */
  100. public void writeTf(Font f) {
  101. String fontName = f.getFontName();
  102. float fontSize = (float)f.getFontSize() / 1000f;
  103. boolean isMultiByte = isMultiByteFont(fontName);
  104. if (!isMultiByte && encoding != 0) {
  105. updateTf(fontName + "_" + Integer.toString(encoding), fontSize, isMultiByte);
  106. } else {
  107. updateTf(fontName, fontSize, isMultiByte);
  108. }
  109. }
  110. /**
  111. * Selects a font from the font list suitable to display the given character.
  112. * @param ch the character
  113. * @return the recommended Font to use
  114. */
  115. public Font selectFontForChar(char ch) {
  116. for (int i = 0, c = fonts.length; i < c; i++) {
  117. if (fonts[i].hasChar(ch)) {
  118. return fonts[i];
  119. }
  120. }
  121. return fonts[0]; //TODO Maybe fall back to painting with shapes
  122. }
  123. /**
  124. * Writes a char to the "TJ-Buffer".
  125. * @param ch the unmapped character
  126. */
  127. public void writeTJChar(char ch) {
  128. char mappedChar = font.mapChar(ch);
  129. writeTJMappedChar(mappedChar);
  130. }
  131. }