@@ -22,19 +22,22 @@ This file is part of the iText (R) project.
2222 */
2323package com .itextpdf .layout .font ;
2424
25+ import com .itextpdf .commons .utils .FileUtil ;
2526import com .itextpdf .io .font .FontCache ;
2627import com .itextpdf .io .font .FontProgram ;
2728import com .itextpdf .io .font .FontProgramFactory ;
2829import com .itextpdf .io .font .PdfEncodings ;
2930import com .itextpdf .io .font .Type1Font ;
3031import com .itextpdf .io .font .constants .StandardFonts ;
31- import com .itextpdf .commons .utils .FileUtil ;
3232import com .itextpdf .kernel .exceptions .PdfException ;
3333import com .itextpdf .kernel .font .PdfFont ;
3434import com .itextpdf .kernel .font .PdfFontFactory ;
3535import com .itextpdf .kernel .font .PdfFontFactory .EmbeddingStrategy ;
3636import com .itextpdf .kernel .pdf .PdfDocument ;
3737import com .itextpdf .layout .exceptions .LayoutExceptionMessageConstant ;
38+ import com .itextpdf .layout .font .selectorstrategy .FirstMatchFontSelectorStrategy .FirstMathFontSelectorStrategyFactory ;
39+ import com .itextpdf .layout .font .selectorstrategy .IFontSelectorStrategy ;
40+ import com .itextpdf .layout .font .selectorstrategy .IFontSelectorStrategyFactory ;
3841
3942import java .io .IOException ;
4043import java .util .ArrayList ;
@@ -53,7 +56,7 @@ This file is part of the iText (R) project.
5356 * <p>
5457 * It is allowed to use only one {@link FontProvider} per document. If additional fonts per element needed,
5558 * another instance of {@link FontSet} can be used. For more details see {@link com.itextpdf.layout.properties.Property#FONT_SET},
56- * {@link #getPdfFont(FontInfo, FontSet)}, {@link #getStrategy(String, List, FontCharacteristics, FontSet)}.
59+ * {@link #getPdfFont(FontInfo, FontSet)}, {@link #createFontSelectorStrategy( List, FontCharacteristics, FontSet)}.
5760 * <p>
5861 * Note, FontProvider does not close created {@link FontProgram}s, because of possible conflicts with {@link FontCache}.
5962 */
@@ -69,6 +72,8 @@ public class FontProvider {
6972 protected final String defaultFontFamily ;
7073 protected final Map <FontInfo , PdfFont > pdfFonts ;
7174
75+ private IFontSelectorStrategyFactory fontSelectorStrategyFactory ;
76+
7277 /**
7378 * Creates a new instance of FontProvider.
7479 *
@@ -105,6 +110,7 @@ public FontProvider(FontSet fontSet, String defaultFontFamily) {
105110 pdfFonts = new HashMap <>();
106111 fontSelectorCache = new FontSelectorCache (this .fontSet );
107112 this .defaultFontFamily = defaultFontFamily ;
113+ this .fontSelectorStrategyFactory = new FirstMathFontSelectorStrategyFactory ();
108114 }
109115
110116 /**
@@ -364,7 +370,9 @@ public boolean getDefaultEmbeddingFlag() {
364370 * font selector strategy instance and will not be otherwise preserved in font provider.
365371 *
366372 * @return {@link FontSelectorStrategy} instance.
373+ * @deprecated use {@link #createFontSelectorStrategy(List, FontCharacteristics, FontSet)}
367374 */
375+ @ Deprecated
368376 public FontSelectorStrategy getStrategy (String text , List <String > fontFamilies , FontCharacteristics fc , FontSet additionalFonts ) {
369377 return new ComplexFontSelectorStrategy (text , getFontSelector (fontFamilies , fc , additionalFonts ), this , additionalFonts );
370378 }
@@ -379,7 +387,9 @@ public FontSelectorStrategy getStrategy(String text, List<String> fontFamilies,
379387 * @param fc instance of {@link FontCharacteristics} to create {@link FontSelector} for sequences of glyphs.
380388 *
381389 * @return {@link FontSelectorStrategy} instance.
390+ * @deprecated use {@link #createFontSelectorStrategy(List, FontCharacteristics, FontSet)}
382391 */
392+ @ Deprecated
383393 public FontSelectorStrategy getStrategy (String text , List <String > fontFamilies , FontCharacteristics fc ) {
384394 return getStrategy (text , fontFamilies , fc , null );
385395 }
@@ -393,11 +403,44 @@ public FontSelectorStrategy getStrategy(String text, List<String> fontFamilies,
393403 * @param fontFamilies target font families to create {@link FontSelector} for sequences of glyphs.
394404 *
395405 * @return {@link FontSelectorStrategy} instance.
406+ * @deprecated use {@link #createFontSelectorStrategy(List, FontCharacteristics, FontSet)}
396407 */
408+ @ Deprecated
397409 public FontSelectorStrategy getStrategy (String text , List <String > fontFamilies ) {
398410 return getStrategy (text , fontFamilies , null );
399411 }
400412
413+ /**
414+ * Sets factory which will be used in {@link #createFontSelectorStrategy(List, FontCharacteristics, FontSet)}
415+ * method.
416+ *
417+ * @param factory the factory which will be used to create font selector strategies
418+ */
419+ public void setFontSelectorStrategyFactory (IFontSelectorStrategyFactory factory ) {
420+ this .fontSelectorStrategyFactory = factory ;
421+ }
422+
423+ /**
424+ * Creates the {@link IFontSelectorStrategy} to split text into sequences of glyphs, already tied
425+ * to the fonts which contain them. The fonts can be taken from the added fonts to the font provider and
426+ * are chosen based on font-families list and desired font characteristics.
427+ *
428+ * @param fontFamilies target font families to create {@link FontSelector} for sequences of glyphs.
429+ * @param fc instance of {@link FontCharacteristics} to create {@link FontSelector} for sequences of glyphs.
430+ * @param additionalFonts set which provides fonts additionally to the fonts added to font provider.
431+ * Combined set of font provider fonts and additional fonts is used when choosing
432+ * a single font for a sequence of glyphs. Additional fonts will only be used for the given
433+ * font selector strategy instance and will not be otherwise preserved in font provider.
434+ *
435+ * @return {@link IFontSelectorStrategy} instance
436+ */
437+ public IFontSelectorStrategy createFontSelectorStrategy (List <String > fontFamilies ,
438+ FontCharacteristics fc , FontSet additionalFonts ) {
439+
440+ final FontSelector fontSelector = getFontSelector (fontFamilies , fc , additionalFonts );
441+ return fontSelectorStrategyFactory .createFontSelectorStrategy (this , fontSelector , additionalFonts );
442+ }
443+
401444 /**
402445 * Create {@link FontSelector} or get from cache.
403446 *
0 commit comments