Skip to content

Commit 40968cf

Browse files
committed
Add PdfDocument#findFont() for already added PdfFonts
DEVSIX-2068
1 parent afc2254 commit 40968cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+881
-18
lines changed

io/src/main/java/com/itextpdf/io/font/CMapEncoding.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This file is part of the iText (R) project.
5656
import java.io.Serializable;
5757
import java.util.Arrays;
5858
import java.util.List;
59+
import java.util.Objects;
5960

6061
public class CMapEncoding implements Serializable {
6162

@@ -160,6 +161,15 @@ public String getCmapName() {
160161
return cmap;
161162
}
162163

164+
/**
165+
* Checks whether the {@link CMapEncoding} was built with corresponding cmap name.
166+
*
167+
* @param cmap a CMAP
168+
* @return true, if the CMapEncoding was built with the cmap. Otherwise false.
169+
*/
170+
public boolean isBuiltWith(String cmap) {
171+
return Objects.equals(cmap, this.cmap);
172+
}
163173

164174
/**
165175
* @deprecated Will be removed in 7.2. Use {@link #getCmapBytes(int)} instead.

io/src/main/java/com/itextpdf/io/font/CidFont.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.io.util.IntHashtable;
5050

5151
import java.util.Map;
52+
import java.util.Objects;
5253
import java.util.Set;
5354
import java.util.StringTokenizer;
5455

5556
public class CidFont extends FontProgram {
5657

5758
private static final long serialVersionUID = 5444988003799502179L;
58-
59+
60+
private String fontName;
5961
private int pdfFontFlags;
6062
private Set<String> compatibleCmaps;
6163

6264
CidFont(String fontName, Set<String> cmaps) {
65+
this.fontName = fontName;
6366
compatibleCmaps = cmaps;
6467
fontNames = new FontNames();
6568
initializeCidFontNameAndStyle(fontName);
@@ -99,6 +102,11 @@ public boolean isFontSpecific() {
99102
return false;
100103
}
101104

105+
@Override
106+
public boolean isBuiltWith(String fontName) {
107+
return Objects.equals(this.fontName, fontName);
108+
}
109+
102110
private void initializeCidFontNameAndStyle(String fontName) {
103111
String nameBase = trimFontStyle(fontName);
104112
if (nameBase.length() < fontName.length()) {
@@ -120,11 +128,11 @@ private void initializeCidFontProperties(Map<String, Object> fontDesc) {
120128
pdfFontFlags = Integer.parseInt((String) fontDesc.get("Flags"));
121129
String fontBBox = (String) fontDesc.get("FontBBox");
122130
StringTokenizer tk = new StringTokenizer(fontBBox, " []\r\n\t\f");
123-
Integer llx = Integer.parseInt(tk.nextToken());
124-
Integer lly = Integer.parseInt(tk.nextToken());
125-
Integer urx = Integer.parseInt(tk.nextToken());
126-
Integer ury = Integer.parseInt(tk.nextToken());
127-
fontMetrics.updateBbox((int) llx, (int) lly, (int) urx, (int) ury);
131+
int llx = Integer.parseInt(tk.nextToken());
132+
int lly = Integer.parseInt(tk.nextToken());
133+
int urx = Integer.parseInt(tk.nextToken());
134+
int ury = Integer.parseInt(tk.nextToken());
135+
fontMetrics.updateBbox(llx, lly, urx, ury);
128136
registry = (String) fontDesc.get("Registry");
129137
String uniMap = getCompatibleUniMap(registry);
130138
if (uniMap != null) {

io/src/main/java/com/itextpdf/io/font/FontEncoding.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import com.itextpdf.io.util.TextUtil;
4949

5050
import java.io.Serializable;
51+
import java.util.Objects;
5152
import java.util.StringTokenizer;
5253

5354
public class FontEncoding implements Serializable {
@@ -157,6 +158,7 @@ public boolean addSymbol(int code, int unicode) {
157158

158159
/**
159160
* Gets unicode value for corresponding font's char code.
161+
*
160162
* @param index font's char code
161163
* @return -1, if the char code unsupported or valid unicode.
162164
*/
@@ -191,7 +193,7 @@ public byte[] convertToBytes(String text) {
191193
byte[] bytes = new byte[text.length()];
192194
for (int i = 0; i < text.length(); i++) {
193195
if (unicodeToCode.containsKey(text.charAt(i))) {
194-
bytes[ptr++] = (byte)convertToByte(text.charAt(i));
196+
bytes[ptr++] = (byte) convertToByte(text.charAt(i));
195197
}
196198
}
197199
return ArrayUtil.shortenArray(bytes, ptr);
@@ -230,6 +232,16 @@ public boolean canDecode(int code) {
230232
return codeToUnicode[code] > -1;
231233
}
232234

235+
/**
236+
* Checks whether the {@link FontEncoding} was built with corresponding encoding.
237+
*
238+
* @param encoding an encoding
239+
* @return true, if the FontEncoding was built with the encoding. Otherwise false.
240+
*/
241+
public boolean isBuiltWith(String encoding) {
242+
return Objects.equals(normalizeEncoding(encoding), baseEncoding);
243+
}
244+
233245
protected void fillCustomEncoding() {
234246
differences = new String[256];
235247
StringTokenizer tok = new StringTokenizer(baseEncoding.substring(1), " ,\t\n\r\f");

io/src/main/java/com/itextpdf/io/font/FontProgram.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ public int getKerning(int first, int second) {
167167
*/
168168
public abstract int getKerning(Glyph first, Glyph second);
169169

170+
/**
171+
* Checks whether the {@link FontProgram} was built with corresponding fontName.
172+
* Default value is false unless overridden.
173+
*
174+
* @param fontName a font name or path to a font program
175+
* @return true, if the FontProgram was built with the fontProgram. Otherwise false.
176+
*/
177+
public boolean isBuiltWith(String fontName) {
178+
return false;
179+
}
180+
170181
protected void setRegistry(String registry) {
171182
this.registry = registry;
172183
}

io/src/main/java/com/itextpdf/io/font/TrueTypeFont.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ This file is part of the iText (R) project.
5757
import com.itextpdf.io.util.MessageFormatUtil;
5858
import java.util.LinkedHashMap;
5959
import java.util.Map;
60+
import java.util.Objects;
6061
import java.util.Set;
6162

6263
public class TrueTypeFont extends FontProgram {
@@ -358,6 +359,11 @@ public String[] getCodePagesSupported() {
358359
return ret;
359360
}
360361

362+
@Override
363+
public boolean isBuiltWith(String fontProgram) {
364+
return Objects.equals(fontParser.fileName, fontProgram);
365+
}
366+
361367
public void close() throws java.io.IOException {
362368
if (fontParser != null) {
363369
fontParser.close();

io/src/main/java/com/itextpdf/io/font/Type1Font.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This file is part of the iText (R) project.
5454

5555
import java.util.HashMap;
5656
import java.util.Map;
57+
import java.util.Objects;
5758
import java.util.StringTokenizer;
5859

5960
public class Type1Font extends FontProgram {
@@ -238,6 +239,10 @@ public int[] getFontStreamLengths() {
238239
return fontStreamLengths;
239240
}
240241

242+
public boolean isBuiltWith(String fontProgram) {
243+
return Objects.equals(fontParser.getAfmPath(), fontProgram);
244+
}
245+
241246
protected void process() throws java.io.IOException {
242247
RandomAccessFileOrArray raf = fontParser.getMetricsFile();
243248
String line;

kernel/src/main/java/com/itextpdf/kernel/font/DocTrueTypeFont.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ public PdfName getSubtype() {
156156
return subtype;
157157
}
158158

159+
/**
160+
* Returns false, because we cannot rely on an actual font subset and font name.
161+
*
162+
* @param fontName a font name or path to a font program
163+
* @return return false.
164+
*/
165+
@Override
166+
public boolean isBuiltWith(String fontName) {
167+
return false;
168+
}
169+
159170
public int getMissingWidth() {
160171
return missingWidth;
161172
}

kernel/src/main/java/com/itextpdf/kernel/font/DocType1Font.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,32 @@ static Type1Font createFontProgram(PdfDictionary fontDictionary, FontEncoding fo
121121
return fontProgram;
122122
}
123123

124+
@Override
124125
public PdfStream getFontFile() {
125126
return fontFile;
126127
}
127128

129+
@Override
128130
public PdfName getFontFileName() {
129131
return fontFileName;
130132
}
131133

134+
@Override
132135
public PdfName getSubtype() {
133136
return subtype;
134137
}
135138

139+
/**
140+
* Returns false, because we cannot rely on an actual font subset and font name.
141+
*
142+
* @param fontName a font name or path to a font program
143+
* @return return false.
144+
*/
145+
@Override
146+
public boolean isBuiltWith(String fontName) {
147+
return false;
148+
}
149+
136150
public int getMissingWidth() {
137151
return missingWidth;
138152
}

kernel/src/main/java/com/itextpdf/kernel/font/PdfFont.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ public boolean containsGlyph(int unicode) {
135135
* Append all supported glyphs and return number of processed chars.
136136
* Composite font supports surrogate pairs.
137137
*
138-
* @param text String to convert to glyphs.
139-
* @param from from index of the text.
140-
* @param to to index of the text.
138+
* @param text String to convert to glyphs.
139+
* @param from from index of the text.
140+
* @param to to index of the text.
141141
* @param glyphs array for a new glyphs, shall not be null.
142142
* @return number of processed chars from text.
143143
*/
@@ -147,8 +147,8 @@ public boolean containsGlyph(int unicode) {
147147
* Append any single glyph, even notdef.
148148
* Returns number of processed chars: 2 in case surrogate pair, otherwise 1.
149149
*
150-
* @param text String to convert to glyphs.
151-
* @param from from index of the text.
150+
* @param text String to convert to glyphs.
151+
* @param from from index of the text.
152152
* @param glyphs array for a new glyph, shall not be null.
153153
* @return number of processed chars: 2 in case surrogate pair, otherwise 1
154154
*/
@@ -170,6 +170,7 @@ public boolean containsGlyph(int unicode) {
170170

171171
/**
172172
* Decodes a given {@link PdfString} containing encoded string (e.g. from content stream) into a {@link GlyphLine}
173+
*
173174
* @param content the encoded string
174175
* @return the {@link GlyphLine} containing the glyphs encoded by the passed string
175176
*/
@@ -436,6 +437,22 @@ public List<String> splitString(String text, float fontSize, float maxWidth) {
436437
return resultString;
437438
}
438439

440+
/**
441+
* Checks whether the {@link PdfFont} was built with corresponding fontProgram and encoding or CMAP.
442+
* Default value is false unless overridden.
443+
*
444+
* @param fontProgram a font name or path to a font program
445+
* @param encoding an encoding or CMAP
446+
* @return true, if the PdfFont was built with the fontProgram and encoding. Otherwise false.
447+
* @see PdfDocument#findFont(String, String)
448+
* @see FontProgram#isBuiltWith(String)
449+
* @see com.itextpdf.io.font.FontEncoding#isBuiltWith(String)
450+
* @see com.itextpdf.io.font.CMapEncoding#isBuiltWith(String)
451+
*/
452+
public boolean isBuiltWith(String fontProgram, String encoding) {
453+
return false;
454+
}
455+
439456
/**
440457
* To manually flush a {@code PdfObject} behind this wrapper, you have to ensure
441458
* that this object is added to the document, i.e. it has an indirect reference.

kernel/src/main/java/com/itextpdf/kernel/font/PdfFontFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ public static PdfFont createFont(PdfDictionary fontDictionary) {
121121
}
122122
}
123123

124+
public static PdfFont createFont(String fontProgram, String encoding, PdfDocument cacheTo) throws IOException {
125+
PdfFont pdfFont;
126+
if (cacheTo != null) {
127+
pdfFont = cacheTo.findFont(fontProgram, encoding);
128+
if (pdfFont != null) {
129+
return pdfFont;
130+
}
131+
}
132+
133+
pdfFont = createFont(fontProgram, encoding);
134+
if (cacheTo != null) pdfFont.makeIndirect(cacheTo);
135+
136+
return pdfFont;
137+
}
138+
124139
/**
125140
* Creates a {@link PdfFont} instance by the path of the font program file
126141
*

0 commit comments

Comments
 (0)