Skip to content

Commit 3f8f41f

Browse files
Fix TextRenderer #resolveFirstPdfFont, refactor Property#FONT_SET usage documentation
DEVSIX-3431
1 parent 09b077c commit 3f8f41f

File tree

12 files changed

+86
-49
lines changed

12 files changed

+86
-49
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public class ComplexFontSelectorStrategy extends FontSelectorStrategy {
6060
private FontSelector selector;
6161

6262

63-
public ComplexFontSelectorStrategy(String text, FontSelector selector, FontProvider provider, FontSet tempFonts) {
64-
super(text, provider, tempFonts);
63+
public ComplexFontSelectorStrategy(String text, FontSelector selector, FontProvider provider, FontSet additionalFonts) {
64+
super(text, provider, additionalFonts);
6565
this.font = null;
6666
this.selector = selector;
6767
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ This file is part of the iText (R) project.
6969
* In the former case the {@link FontSelectorCache} is reused and in the latter it's reinitialised.
7070
* FontProvider the only end point for creating {@link PdfFont}.
7171
* <p>
72-
* It is allowed to use only one {@link FontProvider} per document. If temporary fonts per element needed,
73-
* additional {@link FontSet} can be used. For more details see {@link com.itextpdf.layout.property.Property#FONT_SET},
72+
* It is allowed to use only one {@link FontProvider} per document. If additional fonts per element needed,
73+
* another instance of {@link FontSet} can be used. For more details see {@link com.itextpdf.layout.property.Property#FONT_SET},
7474
* {@link #getPdfFont(FontInfo, FontSet)}, {@link #getStrategy(String, List, FontCharacteristics, FontSet)}.
7575
* <p>
7676
* Note, FontProvider does not close created {@link FontProgram}s, because of possible conflicts with {@link FontCache}.
@@ -240,8 +240,8 @@ public boolean getDefaultEmbeddingFlag() {
240240
return true;
241241
}
242242

243-
public FontSelectorStrategy getStrategy(String text, List<String> fontFamilies, FontCharacteristics fc, FontSet additonalFonts) {
244-
return new ComplexFontSelectorStrategy(text, getFontSelector(fontFamilies, fc, additonalFonts), this, additonalFonts);
243+
public FontSelectorStrategy getStrategy(String text, List<String> fontFamilies, FontCharacteristics fc, FontSet additionalFonts) {
244+
return new ComplexFontSelectorStrategy(text, getFontSelector(fontFamilies, fc, additionalFonts), this, additionalFonts);
245245
}
246246

247247
public FontSelectorStrategy getStrategy(String text, List<String> fontFamilies, FontCharacteristics fc) {
@@ -276,17 +276,17 @@ public final FontSelector getFontSelector(List<String> fontFamilies, FontCharact
276276
*
277277
* @param fontFamilies target font families
278278
* @param fc instance of {@link FontCharacteristics}.
279-
* @param tempFonts set of temporary fonts.
279+
* @param additionalFonts set of additional fonts.
280280
* @return an instance of {@link FontSelector}.
281281
* @see #createFontSelector(Collection, List, FontCharacteristics) }
282282
*/
283283
public final FontSelector getFontSelector(List<String> fontFamilies, FontCharacteristics fc,
284-
FontSet tempFonts) {
284+
FontSet additionalFonts) {
285285
FontSelectorKey key = new FontSelectorKey(fontFamilies, fc);
286-
FontSelector fontSelector = fontSelectorCache.get(key, tempFonts);
286+
FontSelector fontSelector = fontSelectorCache.get(key, additionalFonts);
287287
if (fontSelector == null) {
288-
fontSelector = createFontSelector(fontSet.getFonts(tempFonts), fontFamilies, fc);
289-
fontSelectorCache.put(key, fontSelector, tempFonts);
288+
fontSelector = createFontSelector(fontSet.getFonts(additionalFonts), fontFamilies, fc);
289+
fontSelectorCache.put(key, fontSelector, additionalFonts);
290290
}
291291
return fontSelector;
292292
}
@@ -322,16 +322,16 @@ public PdfFont getPdfFont(FontInfo fontInfo) {
322322
* Get from cache or create a new instance of {@link PdfFont}.
323323
*
324324
* @param fontInfo font info, to create {@link FontProgram} and {@link PdfFont}.
325-
* @param tempFonts Set of temporary fonts.
325+
* @param additionalFonts set of additional fonts to consider.
326326
* @return cached or new instance of {@link PdfFont}.
327327
*/
328-
public PdfFont getPdfFont(FontInfo fontInfo, FontSet tempFonts) {
328+
public PdfFont getPdfFont(FontInfo fontInfo, FontSet additionalFonts) {
329329
if (pdfFonts.containsKey(fontInfo)) {
330330
return pdfFonts.get(fontInfo);
331331
} else {
332332
FontProgram fontProgram = null;
333-
if (tempFonts != null) {
334-
fontProgram = tempFonts.getFontProgram(fontInfo);
333+
if (additionalFonts != null) {
334+
fontProgram = additionalFonts.getFontProgram(fontInfo);
335335
}
336336
if (fontProgram == null) {
337337
fontProgram = fontSet.getFontProgram(fontInfo);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ FontSelector get(FontSelectorKey key) {
6666
}
6767
}
6868

69-
FontSelector get(FontSelectorKey key, FontSet fontSet) {
70-
if (fontSet == null) {
69+
FontSelector get(FontSelectorKey key, FontSet additionalFonts) {
70+
if (additionalFonts == null) {
7171
return get(key);
7272
} else {
73-
FontSetSelectors selectors = caches.get(fontSet.getId());
73+
FontSetSelectors selectors = caches.get(additionalFonts.getId());
7474
if (selectors == null) {
75-
caches.put(fontSet.getId(), selectors = new FontSetSelectors());
75+
caches.put(additionalFonts.getId(), selectors = new FontSetSelectors());
7676
}
77-
if (update(selectors, fontSet)) {
77+
if (update(selectors, additionalFonts)) {
7878
return null;
7979
} else {
8080
return selectors.map.get(key);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public abstract class FontSelectorStrategy {
5656
protected String text;
5757
protected int index;
5858
protected final FontProvider provider;
59-
protected final FontSet tempFonts;
59+
protected final FontSet additionalFonts;
6060

61-
protected FontSelectorStrategy(String text, FontProvider provider, FontSet tempFonts) {
61+
protected FontSelectorStrategy(String text, FontProvider provider, FontSet additionalFonts) {
6262
this.text = text;
6363
this.index = 0;
6464
this.provider = provider;
65-
this.tempFonts = tempFonts;
65+
this.additionalFonts = additionalFonts;
6666
}
6767

6868
public boolean endOfText() {
@@ -81,6 +81,6 @@ public boolean endOfText() {
8181
* @see FontProvider#getPdfFont(FontInfo, FontSet)
8282
*/
8383
protected PdfFont getPdfFont(FontInfo fontInfo) {
84-
return provider.getPdfFont(fontInfo, tempFonts);
84+
return provider.getPdfFont(fontInfo, additionalFonts);
8585
}
8686
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,11 @@ public Collection<FontInfo> getFonts() {
424424
* <p>
425425
* Note, the collection is unmodifiable.
426426
*
427-
* @param tempFonts set of temporary fonts
427+
* @param additionalFonts set of temporary fonts
428428
* @return set of all available and temporary fonts
429429
*/
430-
public Collection<FontInfo> getFonts(FontSet tempFonts) {
431-
return new FontSetCollection(fonts, tempFonts != null ? tempFonts.fonts : null);
430+
public Collection<FontInfo> getFonts(FontSet additionalFonts) {
431+
return new FontSetCollection(fonts, additionalFonts != null ? additionalFonts.fonts : null);
432432
}
433433

434434
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ This file is part of the iText (R) project.
4949
class FontSetCollection extends AbstractCollection<FontInfo> {
5050

5151
private final Collection<FontInfo> primary;
52-
private final Collection<FontInfo> temporary;
52+
private final Collection<FontInfo> additional;
5353

54-
FontSetCollection(Collection<FontInfo> primary, Collection<FontInfo> temporary) {
54+
FontSetCollection(Collection<FontInfo> primary, Collection<FontInfo> additional) {
5555
this.primary = primary;
56-
this.temporary = temporary;
56+
this.additional = additional;
5757
}
5858

5959
public int size() {
60-
return primary.size() + (temporary != null ? temporary.size() : 0);
60+
return primary.size() + (additional != null ? additional.size() : 0);
6161
}
6262

6363
public Iterator<FontInfo> iterator() {
@@ -67,8 +67,8 @@ public Iterator<FontInfo> iterator() {
6767

6868
public boolean hasNext() {
6969
boolean hasNext = i.hasNext();
70-
if (!hasNext && isPrimary && temporary != null) {
71-
i = temporary.iterator();
70+
if (!hasNext && isPrimary && additional != null) {
71+
i = additional.iterator();
7272
isPrimary = false;
7373
return i.hasNext();
7474
} else {

layout/src/main/java/com/itextpdf/layout/property/ListSymbolPosition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ public enum ListSymbolPosition {
4646
DEFAULT,
4747
INSIDE,
4848
OUTSIDE
49-
}
49+
}

layout/src/main/java/com/itextpdf/layout/renderer/AbstractRenderer.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ This file is part of the iText (R) project.
7272
import com.itextpdf.layout.font.FontCharacteristics;
7373
import com.itextpdf.layout.font.FontFamilySplitter;
7474
import com.itextpdf.layout.font.FontProvider;
75+
import com.itextpdf.layout.font.FontSelector;
7576
import com.itextpdf.layout.font.FontSet;
7677
import com.itextpdf.layout.layout.LayoutArea;
7778
import com.itextpdf.layout.layout.LayoutContext;
@@ -2150,16 +2151,22 @@ FontCharacteristics createFontCharacteristics() {
21502151
return fc;
21512152
}
21522153

2153-
// This method is intended to get first valid PdfFont in this renderer, based of font property.
2154-
// It is usually done for counting some layout characteristics like ascender or descender.
2155-
// NOTE: It neither change Font Property of renderer, nor is guarantied to contain all glyphs used in renderer.
2154+
/**
2155+
* Gets any valid {@link PdfFont} for this renderer, based on {@link Property#FONT}, {@link Property#FONT_PROVIDER} and
2156+
* {@link Property#FONT_SET} properties.
2157+
* This method will not change font property of renderer. Also it is not guarantied that returned font will contain
2158+
* all glyphs used in renderer or its children.
2159+
* <p>
2160+
* This method is usually needed for evaluating some layout characteristics like ascender or descender.
2161+
* @return a valid {@link PdfFont} instance based on renderer {@link Property#FONT} property.
2162+
*/
21562163
PdfFont resolveFirstPdfFont() {
21572164
Object font = this.<Object>getProperty(Property.FONT);
21582165
if (font instanceof PdfFont) {
21592166
return (PdfFont) font;
21602167
} else if (font instanceof String || font instanceof String[]) {
21612168
if (font instanceof String) {
2162-
// TODO remove this if-clause before 7.2
2169+
// TODO DEVSIX-3814 remove this if-clause before 7.2
21632170
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
21642171
logger.warn(LogMessageConstant.FONT_PROPERTY_OF_STRING_TYPE_IS_DEPRECATED_USE_STRINGS_ARRAY_INSTEAD);
21652172
List<String> splitFontFamily = FontFamilySplitter.splitFontFamily((String) font);
@@ -2174,19 +2181,23 @@ PdfFont resolveFirstPdfFont() {
21742181
throw new IllegalStateException(PdfException.FontProviderNotSetFontFamilyNotResolved);
21752182
}
21762183
FontCharacteristics fc = createFontCharacteristics();
2177-
return provider.getPdfFont(provider.getFontSelector(Arrays.asList((String[]) font), fc, fontSet).bestMatch(), fontSet);
2184+
return resolveFirstPdfFont((String[]) font, provider, fc, fontSet);
21782185
} else {
21792186
throw new IllegalStateException("String[] or PdfFont expected as value of FONT property");
21802187
}
21812188
}
21822189

2183-
// This method is intended to get first valid PdfFont described in font string,
2184-
// with specific FontCharacteristics with the help of specified font provider.
2185-
// This method is intended to be called from previous method that deals with Font Property.
2186-
// NOTE: It neither change Font Property of renderer, nor is guarantied to contain all glyphs used in renderer.
2187-
// TODO this mechanism does not take text into account
2188-
PdfFont resolveFirstPdfFont(String[] font, FontProvider provider, FontCharacteristics fc) {
2189-
return provider.getPdfFont(provider.getFontSelector(Arrays.asList(font), fc).bestMatch());
2190+
/**
2191+
* Get first valid {@link PdfFont} for this renderer, based on given font-families, font provider and font characteristics.
2192+
* This method will not change font property of renderer. Also it is not guarantied that returned font will contain
2193+
* all glyphs used in renderer or its children.
2194+
* <p>
2195+
* This method is usually needed for evaluating some layout characteristics like ascender or descender.
2196+
* @return a valid {@link PdfFont} instance based on renderer {@link Property#FONT} property.
2197+
*/
2198+
PdfFont resolveFirstPdfFont(String[] font, FontProvider provider, FontCharacteristics fc, FontSet additionalFonts) {
2199+
FontSelector fontSelector = provider.getFontSelector(Arrays.asList(font), fc, additionalFonts);
2200+
return provider.getPdfFont(fontSelector.bestMatch(), additionalFonts);
21902201
}
21912202

21922203
static Border[] getBorders(IRenderer renderer) {

layout/src/main/java/com/itextpdf/layout/renderer/LineHeightHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.kernel.font.PdfFont;
2828
import com.itextpdf.layout.property.LineHeight;
2929
import com.itextpdf.layout.property.Property;
30+
import com.itextpdf.layout.property.RenderingMode;
3031

3132
class LineHeightHelper {
3233
private static float DEFAULT_LINE_HEIGHT_COEFF = 1.15f;

layout/src/main/java/com/itextpdf/layout/renderer/TextRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,8 @@ static void updateRangeBasedOnRemovedCharacters(ArrayList<Integer> removedIds, i
12791279
}
12801280

12811281
@Override
1282-
PdfFont resolveFirstPdfFont(String[] font, FontProvider provider, FontCharacteristics fc) {
1283-
FontSelectorStrategy strategy = provider.getStrategy(strToBeConverted, Arrays.asList(font), fc);
1282+
PdfFont resolveFirstPdfFont(String[] font, FontProvider provider, FontCharacteristics fc, FontSet additionalFonts) {
1283+
FontSelectorStrategy strategy = provider.getStrategy(strToBeConverted, Arrays.asList(font), fc, additionalFonts);
12841284
List<Glyph> resolvedGlyphs;
12851285
PdfFont currentFont;
12861286
//try to find first font that can render at least one glyph.
@@ -1293,7 +1293,7 @@ PdfFont resolveFirstPdfFont(String[] font, FontProvider provider, FontCharacteri
12931293
}
12941294
}
12951295
}
1296-
return super.resolveFirstPdfFont(font, provider, fc);
1296+
return super.resolveFirstPdfFont(font, provider, fc, additionalFonts);
12971297
}
12981298

12991299
private static int numberOfElementsLessThan(ArrayList<Integer> numbers, int n) {

0 commit comments

Comments
 (0)