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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 font;
  29. private int encoding;
  30. /**
  31. * Main constructor.
  32. * @param fontInfo the font catalog
  33. */
  34. public PDFTextUtil(FontInfo fontInfo) {
  35. super();
  36. this.fontInfo = fontInfo;
  37. }
  38. /** {@inheritDoc} */
  39. protected void initValues() {
  40. super.initValues();
  41. this.font = null;
  42. }
  43. /**
  44. * Returns the current font in use.
  45. * @return the current font or null if no font is currently active.
  46. */
  47. public Font getCurrentFont() {
  48. return this.font;
  49. }
  50. /**
  51. * Returns the current encoding.
  52. * @return the current encoding
  53. */
  54. public int getCurrentEncoding() {
  55. return this.encoding;
  56. }
  57. /**
  58. * Sets the current font.
  59. * @param f the new font to use
  60. */
  61. public void setCurrentFont(Font f) {
  62. this.font = f;
  63. }
  64. /**
  65. * Sets the current encoding.
  66. * @param encoding the new encoding
  67. */
  68. public void setCurrentEncoding(int encoding) {
  69. this.encoding = encoding;
  70. }
  71. /**
  72. * Determines whether the font with the given name is a multi-byte font.
  73. * @param name the name of the font
  74. * @return true if it's a multi-byte font
  75. */
  76. protected boolean isMultiByteFont(String name) {
  77. Typeface f = fontInfo.getFonts().get(name);
  78. return f.isMultiByte();
  79. }
  80. /**
  81. * Writes a "Tf" command, setting a new current font.
  82. * @param f the font to select
  83. */
  84. public void writeTf(Font f) {
  85. String fontName = f.getFontName();
  86. float fontSize = (float)f.getFontSize() / 1000f;
  87. boolean isMultiByte = isMultiByteFont(fontName);
  88. if (!isMultiByte && encoding != 0) {
  89. updateTf(fontName + "_" + Integer.toString(encoding), fontSize, isMultiByte);
  90. } else {
  91. updateTf(fontName, fontSize, isMultiByte);
  92. }
  93. }
  94. }