您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PDFTextUtil.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // @SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS")
  27. public abstract class PDFTextUtil extends org.apache.fop.pdf.PDFTextUtil {
  28. private FontInfo fontInfo;
  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. * Returns the current font in use.
  46. * @return the current font or null if no font is currently active.
  47. */
  48. public Font getCurrentFont() {
  49. return this.font;
  50. }
  51. /**
  52. * Returns the current encoding.
  53. * @return the current encoding
  54. */
  55. public int getCurrentEncoding() {
  56. return this.encoding;
  57. }
  58. /**
  59. * Sets the current font.
  60. * @param f the new font to use
  61. */
  62. public void setCurrentFont(Font f) {
  63. this.font = f;
  64. }
  65. /**
  66. * Sets the current encoding.
  67. * @param encoding the new encoding
  68. */
  69. public void setCurrentEncoding(int encoding) {
  70. this.encoding = encoding;
  71. }
  72. /**
  73. * Determines whether the font with the given name is a multi-byte font.
  74. * @param name the name of the font
  75. * @return true if it's a multi-byte font
  76. */
  77. protected boolean isMultiByteFont(String name) {
  78. Typeface f = fontInfo.getFonts().get(name);
  79. return f.isMultiByte();
  80. }
  81. protected boolean isCIDFont(String name) {
  82. Typeface f = fontInfo.getFonts().get(name);
  83. return f.isCID();
  84. }
  85. /**
  86. * Writes a "Tf" command, setting a new current font.
  87. * @param f the font to select
  88. */
  89. public void writeTf(Font f) {
  90. String fontName = f.getFontName();
  91. float fontSize = (float)f.getFontSize() / 1000f;
  92. boolean isMultiByte = isMultiByteFont(fontName);
  93. boolean isCid = isCIDFont(fontName);
  94. if (!isMultiByte && encoding != 0) {
  95. updateTf(fontName + "_" + Integer.toString(encoding), fontSize, isMultiByte, isCid);
  96. } else {
  97. updateTf(fontName, fontSize, isMultiByte, isCid);
  98. }
  99. }
  100. }