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

RtfFontManager.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.io.IOException;
  26. import java.util.Hashtable;
  27. import java.util.Vector;
  28. /**
  29. * <p>RTF font table.</p>
  30. *
  31. * <p>This work was authored by Andreas Putz (a.putz@skynamics.com).</p>
  32. */
  33. public final class RtfFontManager {
  34. //////////////////////////////////////////////////
  35. // @@ Members
  36. //////////////////////////////////////////////////
  37. /** Singelton instance */
  38. private static RtfFontManager instance = new RtfFontManager();
  39. /** Index table for the fonts */
  40. private Hashtable fontIndex;
  41. /** Used fonts to this vector */
  42. private Vector fontTable;
  43. //////////////////////////////////////////////////
  44. // @@ Construction
  45. //////////////////////////////////////////////////
  46. /**
  47. * Constructor.
  48. */
  49. private RtfFontManager() {
  50. fontTable = new Vector();
  51. fontIndex = new Hashtable();
  52. init();
  53. }
  54. /**
  55. * Singelton.
  56. *
  57. * @return The instance of RtfFontManager
  58. */
  59. public static RtfFontManager getInstance() {
  60. return instance;
  61. }
  62. //////////////////////////////////////////////////
  63. // @@ Initializing
  64. //////////////////////////////////////////////////
  65. /**
  66. * Initialize the font table.
  67. */
  68. private void init() {
  69. // getFontNumber ("Helvetica");
  70. //Chanded by R.Marra default font Arial
  71. getFontNumber("Arial");
  72. getFontNumber("Symbol"); // used by RtfListItem.java
  73. getFontNumber("Times New Roman");
  74. /*
  75. {\\f0\\fswiss Helv;}
  76. // f1 is used by RtfList and RtfListItem for bullets
  77. {\\f1\\froman\\fcharset2 Symbol;}
  78. {\\f2\\froman\\fprq2 Times New Roman;}
  79. {\\f3\\froman Times New Roman;}
  80. */
  81. }
  82. //////////////////////////////////////////////////
  83. // @@ Public methods
  84. //////////////////////////////////////////////////
  85. /**
  86. * Gets the number of font in the font table
  87. *
  88. * @param family Font family name ('Helvetica')
  89. *
  90. * @return The number of the font in the table
  91. */
  92. public int getFontNumber(String family) {
  93. Object o = fontIndex.get(getFontKey(family));
  94. int retVal;
  95. if (o == null) {
  96. addFont(family);
  97. retVal = fontTable.size() - 1;
  98. } else {
  99. retVal = (Integer) o;
  100. }
  101. return retVal;
  102. }
  103. /**
  104. * Writes the font table in the header.
  105. *
  106. * @param header The header container to write in
  107. *
  108. * @throws IOException On error
  109. */
  110. public void writeFonts(RtfHeader header) throws IOException {
  111. if (fontTable == null || fontTable.size() == 0) {
  112. return;
  113. }
  114. header.newLine();
  115. header.writeGroupMark(true);
  116. header.writeControlWord("fonttbl");
  117. int len = fontTable.size();
  118. for (int i = 0; i < len; i++) {
  119. header.writeGroupMark(true);
  120. header.newLine();
  121. header.write("\\f" + i);
  122. header.write(" " + (String) fontTable.elementAt(i));
  123. header.write(";");
  124. header.writeGroupMark(false);
  125. }
  126. header.newLine();
  127. header.writeGroupMark(false);
  128. }
  129. //////////////////////////////////////////////////
  130. // @@ Private methods
  131. //////////////////////////////////////////////////
  132. private String getFontKey(String family) {
  133. return family.toLowerCase();
  134. }
  135. /**
  136. * Adds a font to the table.
  137. *
  138. * @param family Identifier of font
  139. */
  140. private void addFont(String family) {
  141. fontIndex.put(getFontKey(family), fontTable.size());
  142. fontTable.addElement(family);
  143. }
  144. }