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

RtfFontManager.java 4.8KB

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