Skip to content

Commit e9a7b08

Browse files
committed
Add test with PdfType3Font in FontProvider
DEVSIX-1468
1 parent 56cf7b2 commit e9a7b08

File tree

12 files changed

+178
-26
lines changed

12 files changed

+178
-26
lines changed

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public final class LogMessageConstant {
129129
public static final String TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH = "Table width is more than expected due to min width of cell(s).";
130130
public static final String TAG_STRUCTURE_CONTEXT_WILL_BE_REINITIALIZED_ON_SERIALIZATION = "Tag structure context is not null and will be reinitialized in the copy of document. The copy may lose some data";
131131
public static final String TAG_STRUCTURE_INIT_FAILED = "Tag structure initialization failed, tag structure is ignored, it might be corrupted.";
132+
public static final String TYPE3_FONT_CANNOT_BE_ADDED = "Type 3 font cannot be added to FontSet. Custom FontProvider class may be created for this purpose.";
132133
public static final String TYPE3_FONT_ISSUE_TAGGED_PDF = "Type 3 font issue. Font Descriptor is required for tagged PDF. FontName shall be specified.";
133134
public static final String TOUNICODE_CMAP_MORE_THAN_2_BYTES_NOT_SUPPORTED = "ToUnicode CMap more than 2 bytes not supported.";
134135
public static final String UNEXPECTED_BEHAVIOUR_DURING_TABLE_ROW_COLLAPSING = "Unexpected behaviour during table row collapsing. Calculated rowspan was less then 1.";

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,16 @@ protected void setFontFamily(String fontFamily) {
272272

273273
/**
274274
* Sets the PostScript name of the font.
275+
* <br />
276+
* If full name is null, it will be set as well.
275277
*
276278
* @param fontName the PostScript name of the font, shall not be null or empty.
277279
*/
278280
protected void setFontName(String fontName) {
279281
fontNames.setFontName(fontName);
282+
if (fontNames.getFullName() == null) {
283+
fontNames.setFullName(fontName);
284+
}
280285
}
281286

282287
protected void checkFilePath(String path) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public void setForceWidthsOutput(boolean forceWidthsOutput) {
328328

329329
protected void flushFontData(String fontName, PdfName subtype) {
330330
getPdfObject().put(PdfName.Subtype, subtype);
331-
if (fontName != null) {
331+
if (fontName != null && fontName.length() > 0) {
332332
getPdfObject().put(PdfName.BaseFont, new PdfName(fontName));
333333
}
334334
int firstChar;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public class PdfType1Font extends PdfSimpleFont<Type1Font> {
7878
super(fontDictionary);
7979
newFont = false;
8080
CMapToUnicode toUni = FontUtil.processToUnicode(fontDictionary.get(PdfName.ToUnicode));
81-
//if there is no FontDescriptor, it is most likely one of the Standard Font with StandardEncoding as base encoding.
82-
boolean fillStandardEncoding = !fontDictionary.containsKey(PdfName.FontDescriptor);
81+
// if there is no FontDescriptor, it is most likely one of the Standard Font with StandardEncoding as base encoding.
82+
// unused variable.
83+
// boolean fillStandardEncoding = !fontDictionary.containsKey(PdfName.FontDescriptor);
8384
fontEncoding = DocFontEncoding.createDocFontEncoding(fontDictionary.get(PdfName.Encoding), toUni);
8485
fontProgram = DocType1Font.createFontProgram(fontDictionary, fontEncoding, toUni);
8586

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public class PdfType3Font extends PdfSimpleFont<Type3Font> {
8585
* Creates a Type 3 font.
8686
*
8787
* @param colorized defines whether the glyph color is specified in the glyph descriptions in the font.
88-
* @deprecated Type 3 font should contain font name and font family in case tagged PDF.
8988
*/
90-
@Deprecated
9189
PdfType3Font(PdfDocument document, boolean colorized) {
9290
super();
9391
makeIndirect(document);
@@ -149,6 +147,7 @@ public class PdfType3Font extends PdfSimpleFont<Type3Font> {
149147
((Type3Font) getFontProgram()).addGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.getAsStream(glyphName), getDocument()));
150148
}
151149
}
150+
fillFontDescriptor(fontDictionary.getAsDictionary(PdfName.FontDescriptor));
152151
}
153152

154153
/**
@@ -237,8 +236,8 @@ public void setFontMatrix(double[] fontMatrix) {
237236
*
238237
* @return number of glyphs.
239238
*/
240-
public int getGlyphsCount() {
241-
return ((Type3Font) getFontProgram()).getGlyphsCount();
239+
public int getNumberOfGlyphs() {
240+
return ((Type3Font) getFontProgram()).getNumberOfGlyphs();
242241
}
243242

244243
/**
@@ -305,7 +304,7 @@ public boolean containsGlyph(int unicode) {
305304
@Override
306305
public void flush() {
307306
ensureUnderlyingObjectHasIndirectReference();
308-
if (((Type3Font) getFontProgram()).getGlyphsCount() < 1) {
307+
if (((Type3Font) getFontProgram()).getNumberOfGlyphs() < 1) {
309308
throw new PdfException("no.glyphs.defined.fo r.type3.font");
310309
}
311310

@@ -324,7 +323,13 @@ public void flush() {
324323
getPdfObject().put(PdfName.FontMatrix, new PdfArray(getFontMatrix()));
325324
getPdfObject().put(PdfName.FontBBox, new PdfArray(fontProgram.getFontMetrics().getBbox()));
326325

327-
super.flushFontData(fontProgram.getFontNames().getFontName(), PdfName.Type3);
326+
String fontName = fontProgram.getFontNames().getFontName();
327+
if (fontName != null && fontName.length() > 0) {
328+
getPdfObject().put(PdfName.BaseFont, new PdfName(fontName));
329+
}
330+
super.flushFontData(fontName, PdfName.Type3);
331+
//TODO improve
332+
getPdfObject().remove(PdfName.BaseFont);
328333
super.flush();
329334
}
330335

@@ -382,4 +387,34 @@ private int getFirstEmptyCode() {
382387
}
383388
return -1;
384389
}
390+
391+
392+
private void fillFontDescriptor(PdfDictionary fontDesc) {
393+
if (fontDesc == null) {
394+
return;
395+
}
396+
PdfNumber v = fontDesc.getAsNumber(PdfName.ItalicAngle);
397+
if (v != null) {
398+
setItalicAngle(v.intValue());
399+
}
400+
v = fontDesc.getAsNumber(PdfName.FontWeight);
401+
if (v != null) {
402+
setFontWeight(v.intValue());
403+
}
404+
405+
PdfName fontStretch = fontDesc.getAsName(PdfName.FontStretch);
406+
if (fontStretch != null) {
407+
setFontStretch(fontStretch.getValue());
408+
}
409+
410+
PdfName fontName = fontDesc.getAsName(PdfName.FontName);
411+
if (fontName != null) {
412+
setFontName(fontName.getValue());
413+
}
414+
415+
PdfString fontFamily = fontDesc.getAsString(PdfName.FontFamily);
416+
if (fontFamily != null) {
417+
setFontFamily(fontFamily.getValue());
418+
}
419+
}
385420
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ This file is part of the iText (R) project.
5353
import java.util.HashMap;
5454
import java.util.Map;
5555

56-
class Type3Font extends FontProgram {
56+
/**
57+
* FontProgram class for Type 3 font. Contains map of {@Link Type3Glyph}.
58+
* Type3Glyphs belong to a particular pdf document.
59+
* Note, an instance of Type3Font can not be reused for multiple pdf documents.
60+
*/
61+
public class Type3Font extends FontProgram {
5762

5863
private static final long serialVersionUID = 1027076515537536993L;
5964

@@ -62,10 +67,9 @@ class Type3Font extends FontProgram {
6267
private int flags = 0;
6368

6469
/**
65-
* Creates a Type 3 font programm.
70+
* Creates a Type 3 font program.
6671
*
6772
* @param colorized defines whether the glyph color is specified in the glyph descriptions in the font.
68-
* @deprecated Type 3 font should contain font name and font family in case tagged PDF.
6973
*/
7074
Type3Font(boolean colorized) {
7175
this.colorized = colorized;
@@ -96,16 +100,17 @@ public int getKerning(Glyph glyph1, Glyph glyph2) {
96100
return 0;
97101
}
98102

99-
public int getGlyphsCount() {
103+
public int getNumberOfGlyphs() {
100104
return type3Glyphs.size();
101105
}
102106

103107
/**
104108
* Sets the PostScript name of the font.
105-
*
109+
* <br />
110+
* If full name is null, it will be set as well.
106111
* @param fontName the PostScript name of the font, shall not be null or empty.
107112
*/
108-
@Override //This dummy override allows PdfType3Font to set font name.
113+
@Override //This dummy override allows PdfType3Font to set font name because of different modules.
109114
protected void setFontName(String fontName) {
110115
super.setFontName(fontName);
111116
}
@@ -115,7 +120,7 @@ protected void setFontName(String fontName) {
115120
*
116121
* @param fontFamily a preferred font family name.
117122
*/
118-
@Override //This dummy override allows PdfType3Font to set font family.
123+
@Override //This dummy override allows PdfType3Font to set font family because of different modules.
119124
protected void setFontFamily(String fontFamily) {
120125
super.setFontFamily(fontFamily);
121126
}
@@ -125,8 +130,8 @@ protected void setFontFamily(String fontFamily) {
125130
*
126131
* @param fontWeight integer form 100 to 900. See {@link FontWeights}.
127132
*/
128-
@Override //This dummy override allows PdfType3Font to set font weight.
129-
public void setFontWeight(int fontWeight) {
133+
@Override //This dummy override allows PdfType3Font to set font weight because of different modules.
134+
protected void setFontWeight(int fontWeight) {
130135
super.setFontWeight(fontWeight);
131136
}
132137

@@ -135,7 +140,7 @@ public void setFontWeight(int fontWeight) {
135140
*
136141
* @param fontWidth {@link FontStretches}.
137142
*/
138-
@Override //This dummy override allows PdfType3Font to set font stretch.
143+
@Override //This dummy override allows PdfType3Font to set font stretch because of different modules.
139144
protected void setFontStretch(String fontWidth) {
140145
super.setFontStretch(fontWidth);
141146
}
@@ -147,7 +152,7 @@ protected void setFontStretch(String fontWidth) {
147152
*
148153
* @param italicAngle in counter-clockwise degrees from the vertical
149154
*/
150-
@Override //This dummy override allows PdfType3Font to set the PostScript italicAngel.
155+
@Override //This dummy override allows PdfType3Font to set the PostScript italicAngel because of different modules.
151156
protected void setItalicAngle(int italicAngle) {
152157
super.setItalicAngle(italicAngle);
153158
}
@@ -156,7 +161,7 @@ protected void setItalicAngle(int italicAngle) {
156161
* Sets Font descriptor flags.
157162
* @see FontDescriptorFlags
158163
*
159-
* @param flags
164+
* @param flags {@link FontDescriptorFlags}.
160165
*/
161166
void setPdfFontFlags(int flags) {
162167
this.flags = flags;

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfFontTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ public void testUpdateType3FontBasedExistingFont() throws IOException, Interrupt
882882
page.flush();
883883
pdfDoc.close();
884884

885-
Assert.assertEquals(6, pdfType3Font.getGlyphsCount());
885+
Assert.assertEquals(6, pdfType3Font.getNumberOfGlyphs());
886886

887887
Assert.assertNull(new CompareTool().compareByContent(outputFileName, cmpOutputFileName, destinationFolder, "diff_"));
888888
}
@@ -925,7 +925,7 @@ public void testNewType3FontBasedExistingFont() throws IOException, InterruptedE
925925
page.flush();
926926
outputPdfDoc.close();
927927

928-
Assert.assertEquals(6, pdfType3Font.getGlyphsCount());
928+
Assert.assertEquals(6, pdfType3Font.getNumberOfGlyphs());
929929

930930
Assert.assertNull(new CompareTool().compareByContent(outputFileName, cmpOutputFileName, destinationFolder, "diff_"));
931931
}

layout/src/main/java/com/itextpdf/layout/font/FontInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ private FontInfo(String fontName, byte[] fontData, String encoding, FontProgramD
8585
this.hash = calculateHashCode(fontName, fontData, encoding);
8686
}
8787

88-
static FontInfo create(FontInfo fontInfo, String alias) {
88+
public static FontInfo create(FontInfo fontInfo, String alias) {
8989
return new FontInfo(fontInfo.fontName, fontInfo.fontData, fontInfo.encoding, fontInfo.descriptor, alias);
9090
}
9191

92-
static FontInfo create(FontProgram fontProgram, String encoding, String alias) {
92+
public static FontInfo create(FontProgram fontProgram, String encoding, String alias) {
9393
FontProgramDescriptor descriptor = FontProgramDescriptorFactory.fetchDescriptor(fontProgram);
9494
return new FontInfo(descriptor.getFontName(), null, encoding, descriptor, alias);
9595
}

layout/src/main/java/com/itextpdf/layout/font/FontProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ This file is part of the iText (R) project.
7676
public class FontProvider {
7777

7878
private final FontSet fontSet;
79-
private final Map<FontInfo, PdfFont> pdfFonts;
8079
private final FontSelectorCache fontSelectorCache;
80+
protected final Map<FontInfo, PdfFont> pdfFonts;
8181

8282
/**
8383
* Creates a new instance of FontProvider

layout/src/main/java/com/itextpdf/layout/font/FontSet.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.layout.font;
4444

45+
import com.itextpdf.io.LogMessageConstant;
4546
import com.itextpdf.io.font.FontProgram;
4647
import com.itextpdf.io.util.FileUtil;
48+
import com.itextpdf.kernel.font.Type3Font;
49+
import org.slf4j.Logger;
50+
import org.slf4j.LoggerFactory;
4751

4852
import java.util.ArrayList;
4953
import java.util.Collection;
@@ -138,6 +142,11 @@ public boolean addFont(FontProgram fontProgram, String encoding, String alias) {
138142
if (fontProgram == null) {
139143
return false;
140144
}
145+
if (fontProgram instanceof Type3Font) {
146+
Logger logger = LoggerFactory.getLogger(FontSet.class);
147+
logger.error(LogMessageConstant.TYPE3_FONT_CANNOT_BE_ADDED);
148+
return false;
149+
}
141150
FontInfo fi = FontInfo.create(fontProgram, encoding, alias);
142151
if (addFont(fi)) {
143152
fontPrograms.put(fi, fontProgram);

0 commit comments

Comments
 (0)