1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.xslf.usermodel;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.poi.common.usermodel.fonts.FontCharset;
import org.apache.poi.common.usermodel.fonts.FontFacet;
import org.apache.poi.common.usermodel.fonts.FontFamily;
import org.apache.poi.common.usermodel.fonts.FontHeader;
import org.apache.poi.common.usermodel.fonts.FontInfo;
import org.apache.poi.common.usermodel.fonts.FontPitch;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.IOUtils;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
import org.openxmlformats.schemas.presentationml.x2006.main.CTEmbeddedFontDataId;
import org.openxmlformats.schemas.presentationml.x2006.main.CTEmbeddedFontList;
import org.openxmlformats.schemas.presentationml.x2006.main.CTEmbeddedFontListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation;
@SuppressWarnings("WeakerAccess")
public class XSLFFontInfo implements FontInfo {
final XMLSlideShow ppt;
final String typeface;
final CTEmbeddedFontListEntry fontListEntry;
public XSLFFontInfo(XMLSlideShow ppt, String typeface) {
this.ppt = ppt;
this.typeface = typeface;
final CTPresentation pres = ppt.getCTPresentation();
CTEmbeddedFontList fontList = pres.isSetEmbeddedFontLst()
? pres.getEmbeddedFontLst() : pres.addNewEmbeddedFontLst();
for (CTEmbeddedFontListEntry fe : fontList.getEmbeddedFontArray()) {
if (typeface.equalsIgnoreCase(fe.getFont().getTypeface())) {
fontListEntry = fe;
return;
}
}
fontListEntry = fontList.addNewEmbeddedFont();
fontListEntry.addNewFont().setTypeface(typeface);
}
public XSLFFontInfo(XMLSlideShow ppt, CTEmbeddedFontListEntry fontListEntry) {
this.ppt = ppt;
this.typeface = fontListEntry.getFont().getTypeface();
this.fontListEntry = fontListEntry;
}
@Override
public String getTypeface() {
return getFont().getTypeface();
}
@Override
public void setTypeface(String typeface) {
getFont().setTypeface(typeface);
}
@Override
public FontCharset getCharset() {
return FontCharset.valueOf(getFont().getCharset());
}
@Override
public void setCharset(FontCharset charset) {
getFont().setCharset((byte)charset.getNativeId());
}
@Override
public FontFamily getFamily() {
return FontFamily.valueOfPitchFamily(getFont().getPitchFamily());
}
@Override
public void setFamily(FontFamily family) {
byte pitchAndFamily = getFont().getPitchFamily();
FontPitch pitch = FontPitch.valueOfPitchFamily(pitchAndFamily);
getFont().setPitchFamily(FontPitch.getNativeId(pitch, family));
}
@Override
public FontPitch getPitch() {
return FontPitch.valueOfPitchFamily(getFont().getPitchFamily());
}
@Override
public void setPitch(FontPitch pitch) {
byte pitchAndFamily = getFont().getPitchFamily();
FontFamily family = FontFamily.valueOfPitchFamily(pitchAndFamily);
getFont().setPitchFamily(FontPitch.getNativeId(pitch, family));
}
@Override
public byte[] getPanose() {
return getFont().getPanose();
}
@Override
public List<FontFacet> getFacets() {
List<FontFacet> facetList = new ArrayList<>();
if (fontListEntry.isSetRegular()) {
facetList.add(new XSLFFontFacet((fontListEntry.getRegular())));
}
if (fontListEntry.isSetItalic()) {
facetList.add(new XSLFFontFacet((fontListEntry.getItalic())));
}
if (fontListEntry.isSetBold()) {
facetList.add(new XSLFFontFacet((fontListEntry.getBold())));
}
if (fontListEntry.isSetBoldItalic()) {
facetList.add(new XSLFFontFacet((fontListEntry.getBoldItalic())));
}
return facetList;
}
public FontFacet addFacet(InputStream fontData) throws IOException {
FontHeader header = new FontHeader();
InputStream is = header.bufferInit(fontData);
final CTPresentation pres = ppt.getCTPresentation();
pres.setEmbedTrueTypeFonts(true);
pres.setSaveSubsetFonts(true);
final CTEmbeddedFontDataId dataId;
final int style =
(header.getWeight() > 400 ? Font.BOLD : Font.PLAIN) |
(header.isItalic() ? Font.ITALIC : Font.PLAIN);
switch (style) {
case Font.PLAIN:
dataId = fontListEntry.isSetRegular()
? fontListEntry.getRegular() : fontListEntry.addNewRegular();
break;
case Font.BOLD:
dataId = fontListEntry.isSetBold()
? fontListEntry.getBold() : fontListEntry.addNewBold();
break;
case Font.ITALIC:
dataId = fontListEntry.isSetItalic()
? fontListEntry.getItalic() : fontListEntry.addNewItalic();
break;
default:
dataId = fontListEntry.isSetBoldItalic()
? fontListEntry.getBoldItalic() : fontListEntry.addNewBoldItalic();
break;
}
XSLFFontFacet facet = new XSLFFontFacet(dataId);
facet.setFontData(is);
return facet;
}
private final class XSLFFontFacet implements FontFacet {
private final CTEmbeddedFontDataId fontEntry;
private final FontHeader header = new FontHeader();
private XSLFFontFacet(CTEmbeddedFontDataId fontEntry) {
this.fontEntry = fontEntry;
}
@Override
public int getWeight() {
init();
return header.getWeight();
}
@Override
public boolean isItalic() {
init();
return header.isItalic();
}
@Override
public XSLFFontData getFontData() {
return ppt.getRelationPartById(fontEntry.getId()).getDocumentPart();
}
void setFontData(InputStream is) throws IOException {
final XSLFRelation fntRel = XSLFRelation.FONT;
final String relId = fontEntry.getId();
final XSLFFontData fntData;
if (relId == null || relId.isEmpty()) {
final int fntDataIdx;
try {
fntDataIdx = ppt.getPackage().getUnusedPartIndex(fntRel.getDefaultFileName());
} catch (InvalidFormatException e) {
throw new RuntimeException(e);
}
POIXMLDocumentPart.RelationPart rp = ppt.createRelationship(fntRel, XSLFFactory.getInstance(), fntDataIdx, false);
fntData = rp.getDocumentPart();
fontEntry.setId(rp.getRelationship().getId());
} else {
fntData = (XSLFFontData)ppt.getRelationById(relId);
}
assert (fntData != null);
try (OutputStream os = fntData.getOutputStream()) {
IOUtils.copy(is, os);
}
}
private void init() {
if (header.getFamilyName() == null) {
try (InputStream is = getFontData().getInputStream()) {
byte[] buf = IOUtils.toByteArray(is, 1000);
header.init(buf, 0, buf.length);
} catch (IOException e) {
// TODO: better exception class
throw new RuntimeException(e);
}
}
}
}
private CTTextFont getFont() {
return fontListEntry.getFont();
}
/**
* Adds or updates a (MTX-) font
* @param ppt the slideshow which will contain the font
* @param fontStream the (MTX) font data as stream
* @return a font data object
* @throws IOException if the font data can't be stored
*
* @since POI 4.1.0
*/
public static XSLFFontInfo addFontToSlideShow(XMLSlideShow ppt, InputStream fontStream)
throws IOException {
FontHeader header = new FontHeader();
InputStream is = header.bufferInit(fontStream);
XSLFFontInfo fontInfo = new XSLFFontInfo(ppt, header.getFamilyName());
fontInfo.addFacet(is);
return fontInfo;
}
/**
* Return all registered fonts
* @param ppt the slideshow containing the fonts
* @return the list of registered fonts
*/
public static List<XSLFFontInfo> getFonts(XMLSlideShow ppt) {
final CTPresentation pres = ppt.getCTPresentation();
//noinspection deprecation
return pres.isSetEmbeddedFontLst()
? Stream.of(pres.getEmbeddedFontLst().getEmbeddedFontArray())
.map(fe -> new XSLFFontInfo(ppt, fe)).collect(Collectors.toList())
: Collections.emptyList();
}
}
|