Skip to content

Commit 4be17cf

Browse files
committed
[PERFORMANCE] Improve FileSystemFontProvider.scanFonts() performance by adding 'only headers' mode to TTF parser:
* only read tables needed for FSFontInfo ('name', 'head', 'OS/2', 'CFF ', 'gcid') * 'CFF ' and 'head' table parsers finish as soon as it has all needed headers
1 parent 7646000 commit 4be17cf

File tree

9 files changed

+504
-112
lines changed

9 files changed

+504
-112
lines changed

fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.apache.commons.logging.Log;
3030
import org.apache.commons.logging.LogFactory;
31+
import org.apache.fontbox.ttf.FontHeaders;
3132
import org.apache.pdfbox.io.RandomAccessRead;
3233

3334

@@ -48,7 +49,8 @@ public class CFFParser
4849

4950
private String[] stringIndex = null;
5051
private ByteSource source;
51-
52+
private FontHeaders loadOnlyHeaders;
53+
5254
// for debugging only
5355
private String debugFontName;
5456

@@ -66,6 +68,11 @@ public interface ByteSource
6668
byte[] getBytes() throws IOException;
6769
}
6870

71+
public void setLoadOnlyHeaders(FontHeaders loadOnlyHeaders)
72+
{
73+
this.loadOnlyHeaders = loadOnlyHeaders;
74+
}
75+
6976
/**
7077
* Parse CFF font using byte array, also passing in a byte source for future use.
7178
*
@@ -91,17 +98,21 @@ public List<CFFFont> parse(byte[] bytes, ByteSource source) throws IOException
9198
public List<CFFFont> parse(RandomAccessRead randomAccessRead) throws IOException
9299
{
93100
// TODO do we need to store the source data of the font? It isn't used at all
94-
byte[] bytes = new byte[(int) randomAccessRead.length()];
101+
// definitely don't need 'source' in 'loadOnlyHeaders' mode
95102
randomAccessRead.seek(0);
96-
int remainingBytes = bytes.length;
97-
int amountRead;
98-
while ((amountRead = randomAccessRead.read(bytes, bytes.length - remainingBytes,
99-
remainingBytes)) > 0)
103+
if (loadOnlyHeaders == null)
100104
{
101-
remainingBytes -= amountRead;
105+
byte[] bytes = new byte[(int) randomAccessRead.length()];
106+
int remainingBytes = bytes.length;
107+
int amountRead;
108+
while ((amountRead = randomAccessRead.read(bytes, bytes.length - remainingBytes,
109+
remainingBytes)) > 0)
110+
{
111+
remainingBytes -= amountRead;
112+
}
113+
randomAccessRead.seek(0);
114+
this.source = new CFFBytesource(bytes);
102115
}
103-
randomAccessRead.seek(0);
104-
this.source = new CFFBytesource(bytes);
105116
return parse(new DataInputRandomAccessRead(randomAccessRead));
106117
}
107118

@@ -492,6 +503,15 @@ private CFFFont parseFont(DataInput input, String name, byte[] topDictIndex) thr
492503
cffCIDFont.setSupplement(rosEntry.getNumber(2).intValue());
493504

494505
font = cffCIDFont;
506+
if (loadOnlyHeaders != null)
507+
{
508+
loadOnlyHeaders.setOtfROS(
509+
cffCIDFont.getRegistry(),
510+
cffCIDFont.getOrdering(),
511+
cffCIDFont.getSupplement());
512+
// we just read (Registry, Ordering, Supplement) and don't need anything else
513+
return font;
514+
}
495515
}
496516
else
497517
{
@@ -501,6 +521,10 @@ private CFFFont parseFont(DataInput input, String name, byte[] topDictIndex) thr
501521
// name
502522
debugFontName = name;
503523
font.setName(name);
524+
if (loadOnlyHeaders != null)
525+
{
526+
return font; // not a 'CFFCIDFont' => cannot read properties needed by LoadOnlyHeaders anyway
527+
}
504528

505529
// top dict
506530
font.addValueToTopDict("version", getString(topDict, "version"));

fontbox/src/main/java/org/apache/fontbox/ttf/CFFTable.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import org.apache.fontbox.cff.CFFFont;
2222
import org.apache.fontbox.cff.CFFParser;
23+
import org.apache.pdfbox.io.RandomAccessRead;
2324

2425
/**
2526
* PostScript font program (compact font format).
@@ -48,9 +49,23 @@ public class CFFTable extends TTFTable
4849
@Override
4950
void read(TrueTypeFont ttf, TTFDataStream data) throws IOException
5051
{
51-
byte[] bytes = data.read((int)getLength());
52-
5352
CFFParser parser = new CFFParser();
53+
FontHeaders loadOnlyHeaders = ttf.getLoadOnlyHeaders();
54+
parser.setLoadOnlyHeaders(loadOnlyHeaders);
55+
if (loadOnlyHeaders != null)
56+
{
57+
try (RandomAccessRead subReader = data.createSubView(getLength()))
58+
{
59+
if (subReader != null)
60+
{
61+
cffFont = parser.parse(subReader).get(0);
62+
data.seek(getOffset() + getLength());
63+
initialized = true;
64+
return;
65+
}
66+
}
67+
}
68+
byte[] bytes = data.read((int)getLength());
5469
cffFont = parser.parse(bytes, new CFFBytesource(ttf)).get(0);
5570

5671
initialized = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
package org.apache.fontbox.ttf;
18+
19+
/**
20+
* To improve performance of {@code FileSystemFontProvider.scanFonts(...)},
21+
* this class is used both as a marker (to skip unused data) and as a storage for collected data.
22+
* <p>
23+
* Tables it needs:<ul>
24+
* <li>NamingTable.TAG
25+
* <li>HeaderTable.TAG
26+
* <li>OS2WindowsMetricsTable.TAG
27+
* <li>CFFTable.TAG (for OTF)
28+
* <li>"gcid" (for non-OTF)
29+
* </ul>
30+
*
31+
* @author Mykola Bohdiuk
32+
*/
33+
public final class FontHeaders
34+
{
35+
static final int BYTES_GCID = 142;
36+
37+
private String error;
38+
private String name;
39+
private Integer headerMacStyle;
40+
private OS2WindowsMetricsTable os2Windows;
41+
private String fontFamily;
42+
private String fontSubFamily;
43+
private byte[] nonOtfGcid142;
44+
//
45+
private boolean isOTFAndPostScript;
46+
private String otfRegistry;
47+
private String otfOrdering;
48+
private int otfSupplement;
49+
50+
public String getError()
51+
{
52+
return error;
53+
}
54+
55+
public String getName()
56+
{
57+
return name;
58+
}
59+
60+
/**
61+
* null == no HeaderTable, {@code ttf.getHeader().getMacStyle()}
62+
*/
63+
public Integer getHeaderMacStyle()
64+
{
65+
return headerMacStyle;
66+
}
67+
68+
public OS2WindowsMetricsTable getOS2Windows()
69+
{
70+
return os2Windows;
71+
}
72+
73+
// only when LOGGER(FileSystemFontProvider).isTraceEnabled() tracing: FontFamily, FontSubfamily
74+
public String getFontFamily()
75+
{
76+
return fontFamily;
77+
}
78+
79+
public String getFontSubFamily()
80+
{
81+
return fontSubFamily;
82+
}
83+
84+
public boolean isOpenTypePostScript()
85+
{
86+
return isOTFAndPostScript;
87+
}
88+
89+
public byte[] getNonOtfTableGCID142()
90+
{
91+
return nonOtfGcid142;
92+
}
93+
94+
public String getOtfRegistry()
95+
{
96+
return otfRegistry;
97+
}
98+
99+
public String getOtfOrdering()
100+
{
101+
return otfOrdering;
102+
}
103+
104+
public int getOtfSupplement()
105+
{
106+
return otfSupplement;
107+
}
108+
109+
void setError(String exception)
110+
{
111+
this.error = exception;
112+
}
113+
114+
void setName(String name)
115+
{
116+
this.name = name;
117+
}
118+
119+
void setHeaderMacStyle(Integer headerMacStyle)
120+
{
121+
this.headerMacStyle = headerMacStyle;
122+
}
123+
124+
void setOs2Windows(OS2WindowsMetricsTable os2Windows)
125+
{
126+
this.os2Windows = os2Windows;
127+
}
128+
129+
void setFontFamily(String fontFamily, String fontSubFamily)
130+
{
131+
this.fontFamily = fontFamily;
132+
this.fontSubFamily = fontSubFamily;
133+
}
134+
135+
void setNonOtfGcid142(byte[] nonOtfGcid142)
136+
{
137+
this.nonOtfGcid142 = nonOtfGcid142;
138+
}
139+
140+
void setIsOTFAndPostScript(boolean isOTFAndPostScript)
141+
{
142+
this.isOTFAndPostScript = isOTFAndPostScript;
143+
}
144+
145+
// public because CFFParser is in a different package
146+
public void setOtfROS(String otfRegistry, String otfOrdering, int otfSupplement)
147+
{
148+
this.otfRegistry = otfRegistry;
149+
this.otfOrdering = otfOrdering;
150+
this.otfSupplement = otfSupplement;
151+
}
152+
}

fontbox/src/main/java/org/apache/fontbox/ttf/HeaderTable.java

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public class HeaderTable extends TTFTable
7474
@Override
7575
void read(TrueTypeFont ttf, TTFDataStream data) throws IOException
7676
{
77+
FontHeaders outHeaders = ttf.getLoadOnlyHeaders();
78+
if (outHeaders != null)
79+
{
80+
// 44 == 4 + 4 + 4 + 4 + 2 + 2 + 2*8 + 4*2
81+
data.seek(data.getCurrentPosition() + 44);
82+
macStyle = data.readUnsignedShort();
83+
outHeaders.setHeaderMacStyle(macStyle);
84+
initialized = true;
85+
return;
86+
}
87+
7788
version = data.read32Fixed();
7889
fontRevision = data.read32Fixed();
7990
checkSumAdjustment = data.readUnsignedInt();

fontbox/src/main/java/org/apache/fontbox/ttf/NamingTable.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ void read(TrueTypeFont ttf, TTFDataStream data) throws IOException
6262
int numberOfNameRecords = data.readUnsignedShort();
6363
int offsetToStartOfStringStorage = data.readUnsignedShort();
6464
nameRecords = new ArrayList<>(numberOfNameRecords);
65+
FontHeaders onlyHeaders = ttf.getLoadOnlyHeaders();
6566
for (int i=0; i< numberOfNameRecords; i++)
6667
{
6768
NameRecord nr = new NameRecord();
6869
nr.initData(ttf, data);
69-
nameRecords.add(nr);
70+
if (onlyHeaders == null || isUsefulForOnlyHeaders(nr))
71+
{
72+
nameRecords.add(nr);
73+
}
7074
}
7175

7276
for (NameRecord nr : nameRecords)
@@ -86,7 +90,7 @@ void read(TrueTypeFont ttf, TTFDataStream data) throws IOException
8690

8791
lookupTable = new HashMap<>(nameRecords.size());
8892
fillLookupTable();
89-
readInterestingStrings();
93+
readInterestingStrings(onlyHeaders);
9094

9195
initialized = true;
9296
}
@@ -138,7 +142,7 @@ private void fillLookupTable()
138142
}
139143
}
140144

141-
private void readInterestingStrings()
145+
private void readInterestingStrings(FontHeaders onlyHeaders)
142146
{
143147
// extract strings of interest
144148
fontFamily = getEnglishName(NameRecord.NAME_FONT_FAMILY_NAME);
@@ -160,6 +164,27 @@ private void readInterestingStrings()
160164
{
161165
psName = psName.trim();
162166
}
167+
168+
if (onlyHeaders != null)
169+
{
170+
onlyHeaders.setName(psName);
171+
onlyHeaders.setFontFamily(fontFamily, fontSubFamily);
172+
}
173+
}
174+
175+
private static boolean isUsefulForOnlyHeaders(NameRecord nr)
176+
{
177+
int nameId = nr.getNameId();
178+
// see "psName =" and "getEnglishName()"
179+
if (nameId == NameRecord.NAME_POSTSCRIPT_NAME
180+
|| nameId == NameRecord.NAME_FONT_FAMILY_NAME
181+
|| nameId == NameRecord.NAME_FONT_SUB_FAMILY_NAME)
182+
{
183+
int languageId = nr.getLanguageId();
184+
return languageId == NameRecord.LANGUAGE_UNICODE
185+
|| languageId == NameRecord.LANGUAGE_WINDOWS_EN_US;
186+
}
187+
return false;
163188
}
164189

165190
/**

0 commit comments

Comments
 (0)