PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/docx4j/fonts/Mapper.java

http://github.com/plutext/docx4j
Java | 194 lines | 56 code | 31 blank | 107 comment | 8 complexity | d25ef3bd2f46fb05f918dda9200826c5 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2007-2008, Plutext Pty Ltd.
  3. *
  4. * This file is part of docx4j.
  5. docx4j is licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package org.docx4j.fonts;
  16. import java.util.Collections;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.apache.log4j.Logger;
  20. /**
  21. * Maps font names used in the document to
  22. * fonts physically available
  23. * on the system.
  24. *
  25. * So, a mapper per document.
  26. * If fonts are added to the document
  27. * (ie fonts introduced into use)
  28. * then the mapper should be updated
  29. * to include a mapping for the
  30. * new font.
  31. *
  32. * There are 2 implementations:
  33. *
  34. * - IndentityPlusMapper, which is best
  35. * where most of the fonts used in the
  36. * document are physically present
  37. * on the system
  38. *
  39. * - BestMatchingMapper, useful on
  40. * Linux and OSX systems on which
  41. * Microsoft fonts have not been
  42. * installed.
  43. *
  44. * Whichever one you use, you can
  45. * add/remove mappings programmatically
  46. * to customise to your needs.
  47. *
  48. * @author jharrop
  49. *
  50. */
  51. public abstract class Mapper {
  52. protected static Logger log = Logger.getLogger(Mapper.class);
  53. public Mapper() {
  54. super();
  55. }
  56. protected final static Map<String, PhysicalFont> fontMappings;
  57. public Map<String, PhysicalFont> getFontMappings() {
  58. return fontMappings;
  59. }
  60. public final static String FONT_FALLBACK = "Times New Roman";
  61. static {
  62. fontMappings = Collections.synchronizedMap(new HashMap<String, PhysicalFont>());
  63. }
  64. /**
  65. * Populate the fontMappings object. We make an entry for each
  66. * of the documentFontNames.
  67. *
  68. * @param documentFontNames - the fonts used in the document
  69. * @param wmlFonts - the content model for the fonts part
  70. * @throws Exception
  71. */
  72. public abstract void populateFontMappings(Map documentFontNames,
  73. org.docx4j.wml.Fonts wmlFonts ) throws Exception;
  74. // For Xalan
  75. public static String getSubstituteFontXsltExtension(Mapper s, String documentStyleId, String bolditalic, boolean fontFamilyStack) {
  76. return s.getSubstituteFontXsltExtension(documentStyleId, bolditalic, fontFamilyStack);
  77. }
  78. public String getSubstituteFontXsltExtension(String documentStyleId,
  79. String bolditalic, boolean fontFamilyStack) {
  80. log.debug("Trying to insert HTML font-family value for " + documentStyleId);
  81. if (documentStyleId==null) {
  82. log.error("passed null documentStyleId");
  83. return "nullInputToExtension";
  84. }
  85. PhysicalFont physicalFont = (PhysicalFont)fontMappings.get((documentStyleId));
  86. if (physicalFont==null) {
  87. log.error("No mapping for: " + documentStyleId);
  88. return Mapper.FONT_FALLBACK;
  89. } else {
  90. // iTextFontResolver wants a font family name
  91. // Until such time as we get this from FOP,
  92. // use the following heuristic..
  93. String fontFamily = physicalFont.getName();
  94. if (fontFamily.startsWith("Britannic")) { // special case
  95. return fontFamily;
  96. }
  97. if (fontFamily.endsWith(" Demibold" ) ) {
  98. fontFamily = fontFamily.substring(0, fontFamily.length() - 9);
  99. }
  100. if (fontFamily.endsWith(" Oblique" ) ) {
  101. fontFamily = fontFamily.substring(0, fontFamily.length() - 8);
  102. }
  103. if (fontFamily.endsWith(" Italic" ) ) {
  104. fontFamily = fontFamily.substring(0, fontFamily.length() - 7);
  105. }
  106. if (fontFamily.endsWith(" Bold" ) ) {
  107. fontFamily = fontFamily.substring(0, fontFamily.length() - 5);
  108. }
  109. // NB, in that order, it handles " Bold Italic" and "Bold Oblique" as well.
  110. log.debug("Mapping " + documentStyleId + " to " + physicalFont.getName());
  111. /* On my Windows box, the following are passed
  112. * to ITextFontResolver, but still not found in its
  113. * _fontFamilies map:
  114. *
  115. * DejaVu Sans ExtraLight
  116. * Lucida Sans Demibold
  117. * Lucida Sans Regular
  118. * Lucida Bright Demibold
  119. * Lucida Sans Demibold Roman
  120. * Lucida Fax Regular
  121. * Lucida Fax Demibold
  122. */
  123. return fontFamily;
  124. }
  125. // log.info(documentStyleId + " -> " + physicalFont.getName() );
  126. //
  127. // if (fontFamilyStack) {
  128. //
  129. // // TODO - if this is an HTML document intended
  130. // // for viewing in a web browser, we need to add a
  131. // // font-family cascade (since the true type font
  132. // // specified for PDF purposes won't necessarily be
  133. // // present on web browser's system).
  134. //
  135. // // The easiest way to do it might be to just
  136. // // see whether the substitute font is serif or
  137. // // not, and add cascade entries accordingly.
  138. //
  139. // // If we matched it via FontSubstitutions.xml,
  140. // // maybe that file contains an HTML match as well?
  141. //
  142. // // Either way, this stuff should be worked out in
  143. // // populateFontMappings, and added to the
  144. // // FontMapping objects.
  145. //
  146. // return physicalFont.getName();
  147. // } else {
  148. // return physicalFont.getName();
  149. // }
  150. /*
  151. * We want to return eg "Times New Roman"
  152. * or "Arial Unicode MS" here, ie _with spaces_, since that is
  153. * what xhtmlrender's org.xhtmlrenderer.pdf.ITextFontResolver sets up.
  154. *
  155. *
  156. */
  157. }
  158. }