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.

FontCollection.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. 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.apache.poi.hslf.record;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.ArrayList;
  20. import java.util.LinkedHashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.function.Predicate;
  24. import java.util.function.Supplier;
  25. import org.apache.poi.common.usermodel.fonts.FontCharset;
  26. import org.apache.poi.common.usermodel.fonts.FontHeader;
  27. import org.apache.poi.common.usermodel.fonts.FontInfo;
  28. import org.apache.poi.common.usermodel.fonts.FontPitch;
  29. import org.apache.poi.hslf.usermodel.HSLFFontInfo;
  30. import org.apache.poi.hslf.usermodel.HSLFFontInfoPredefined;
  31. import org.apache.poi.util.IOUtils;
  32. import org.apache.poi.util.POILogger;
  33. /**
  34. * {@code FontCollection} ia a container that holds information
  35. * about all the fonts in the presentation.
  36. */
  37. @SuppressWarnings("WeakerAccess")
  38. public final class FontCollection extends RecordContainer {
  39. private final Map<Integer,HSLFFontInfo> fonts = new LinkedHashMap<>();
  40. private byte[] _header;
  41. /* package */ FontCollection(byte[] source, int start, int len) {
  42. _header = new byte[8];
  43. System.arraycopy(source,start,_header,0,8);
  44. _children = Record.findChildRecords(source,start+8,len-8);
  45. for (org.apache.poi.hslf.record.Record r : _children){
  46. if(r instanceof FontEntityAtom) {
  47. HSLFFontInfo fi = new HSLFFontInfo((FontEntityAtom) r);
  48. fonts.put(fi.getIndex(), fi);
  49. } else if (r instanceof FontEmbeddedData) {
  50. FontEmbeddedData fed = (FontEmbeddedData)r;
  51. FontHeader fontHeader = fed.getFontHeader();
  52. HSLFFontInfo fi = addFont(fontHeader);
  53. fi.addFacet(fed);
  54. } else {
  55. logger.log(POILogger.WARN, "Warning: FontCollection child wasn't a FontEntityAtom, was " + r.getClass().getSimpleName());
  56. }
  57. }
  58. }
  59. /**
  60. * Return the type, which is 2005
  61. */
  62. @Override
  63. public long getRecordType() {
  64. return RecordTypes.FontCollection.typeID;
  65. }
  66. /**
  67. * Write the contents of the record back, so it can be written
  68. * to disk
  69. */
  70. @Override
  71. public void writeOut(OutputStream out) throws IOException {
  72. writeOut(_header[0],_header[1],getRecordType(),_children,out);
  73. }
  74. /**
  75. * Add font with the given FontInfo configuration to the font collection.
  76. * The returned FontInfo contains the HSLF specific details and the collection
  77. * uniquely contains fonts based on their typeface, i.e. calling the method with FontInfo
  78. * objects having the same name results in the same HSLFFontInfo reference.
  79. *
  80. * @param fontInfo the FontInfo configuration, can be a instance of {@link HSLFFontInfo},
  81. * {@link HSLFFontInfoPredefined} or a custom implementation
  82. * @return the register HSLFFontInfo object
  83. */
  84. public HSLFFontInfo addFont(FontInfo fontInfo) {
  85. HSLFFontInfo fi = getFontInfo(fontInfo.getTypeface(), fontInfo.getCharset());
  86. if (fi != null) {
  87. return fi;
  88. }
  89. fi = new HSLFFontInfo(fontInfo);
  90. fi.setIndex(fonts.size());
  91. fonts.put(fi.getIndex(), fi);
  92. FontEntityAtom fnt = fi.createRecord();
  93. // Append new child to the end
  94. appendChildRecord(fnt);
  95. // the added font is the last in the list
  96. return fi;
  97. }
  98. public HSLFFontInfo addFont(InputStream fontData) throws IOException {
  99. FontHeader fontHeader = new FontHeader();
  100. InputStream is = fontHeader.bufferInit(fontData);
  101. HSLFFontInfo fi = addFont(fontHeader);
  102. // always overwrite the font info properties when a font data given
  103. // as the font info properties are assigned generically when only a typeface is given
  104. FontEntityAtom fea = fi.getFontEntityAtom();
  105. assert (fea != null);
  106. fea.setCharSet(fontHeader.getCharsetByte());
  107. fea.setPitchAndFamily(FontPitch.getNativeId(fontHeader.getPitch(),fontHeader.getFamily()));
  108. // always activate subsetting
  109. fea.setFontFlags(1);
  110. // true type font and no font substitution
  111. fea.setFontType(12);
  112. Record after = fea;
  113. final int insertIdx = getFacetIndex(fontHeader.isItalic(), fontHeader.isBold());
  114. FontEmbeddedData newChild = null;
  115. for (FontEmbeddedData fed : fi.getFacets()) {
  116. FontHeader fh = fed.getFontHeader();
  117. final int curIdx = getFacetIndex(fh.isItalic(), fh.isBold());
  118. if (curIdx == insertIdx) {
  119. newChild = fed;
  120. break;
  121. } else if (curIdx > insertIdx) {
  122. // the new facet needs to be inserted before the current facet
  123. break;
  124. }
  125. after = fed;
  126. }
  127. if (newChild == null) {
  128. newChild = new FontEmbeddedData();
  129. addChildAfter(newChild, after);
  130. fi.addFacet(newChild);
  131. }
  132. newChild.setFontData(IOUtils.toByteArray(is));
  133. return fi;
  134. }
  135. private static int getFacetIndex(boolean isItalic, boolean isBold) {
  136. return (isItalic ? 2 : 0) | (isBold ? 1 : 0);
  137. }
  138. /**
  139. * Lookup a FontInfo object by its typeface
  140. *
  141. * @param typeface the full font name
  142. *
  143. * @return the HSLFFontInfo for the given name or {@code null} if not found
  144. */
  145. public HSLFFontInfo getFontInfo(String typeface) {
  146. return getFontInfo(typeface, null);
  147. }
  148. /**
  149. * Lookup a FontInfo object by its typeface
  150. *
  151. * @param typeface the full font name
  152. * @param charset the charset
  153. *
  154. * @return the HSLFFontInfo for the given name or {@code null} if not found
  155. */
  156. public HSLFFontInfo getFontInfo(String typeface, FontCharset charset) {
  157. return fonts.values().stream().filter(findFont(typeface, charset)).findFirst().orElse(null);
  158. }
  159. private static Predicate<HSLFFontInfo> findFont(String typeface, FontCharset charset) {
  160. return (fi) -> typeface.equals(fi.getTypeface()) && (charset == null || charset.equals(fi.getCharset()));
  161. }
  162. /**
  163. * Lookup a FontInfo object by its internal font index
  164. *
  165. * @param index the internal font index
  166. *
  167. * @return the HSLFFontInfo for the given index or {@code null} if not found
  168. */
  169. public HSLFFontInfo getFontInfo(int index) {
  170. for (HSLFFontInfo fi : fonts.values()) {
  171. if (fi.getIndex() == index) {
  172. return fi;
  173. }
  174. }
  175. return null;
  176. }
  177. /**
  178. * @return the number of registered fonts
  179. */
  180. public int getNumberOfFonts() {
  181. return fonts.size();
  182. }
  183. public List<HSLFFontInfo> getFonts() {
  184. return new ArrayList<>(fonts.values());
  185. }
  186. @Override
  187. public Map<String, Supplier<?>> getGenericProperties() {
  188. return null;
  189. }
  190. }