1414import java .io .File ;
1515import java .io .InputStream ;
1616import java .util .ArrayList ;
17+ import java .util .EnumSet ;
1718import java .util .HashMap ;
1819import java .util .List ;
1920import java .util .Map ;
@@ -453,61 +454,98 @@ public final TFinalClass useFastMode() {
453454 }
454455
455456 /**
456- * Like {@link #useFont(FSSupplier, String, Integer, FontStyle, boolean)}, but
457- * allows to supply a font file. If the font file is a .ttc file it is handled
458- * as TrueTypeCollection (PDF only). If you have the font in file form you should use this
459- * API.
457+ * <p>Allows the user to provide a font file for use by the main
458+ * document only (not SVGs). See:
459+ * {@link #useFont(File, String, Integer, FontStyle, boolean, Set)}</p>
460+ *
461+ * <p>For gotchas related to font handling please see:
462+ * <a href="https://github.com/danfickle/openhtmltopdf/wiki/Fonts">Wiki: Fonts</a></p>
460463 */
461- public TFinalClass useFont (File fontFile , String fontFamily , Integer fontWeight , FontStyle fontStyle ,
464+ public TFinalClass useFont (
465+ File fontFile ,
466+ String fontFamily ,
467+ Integer fontWeight ,
468+ FontStyle fontStyle ,
462469 boolean subset ) {
463- state ._fonts .add (new AddedFont (null , fontFile , fontWeight , fontFamily , subset , fontStyle ));
470+ state ._fonts .add (new AddedFont (null , fontFile , fontWeight , fontFamily , subset , fontStyle , EnumSet .of (FSFontUseCase .DOCUMENT )));
471+ return (TFinalClass ) this ;
472+ }
473+
474+ /**
475+ * <p>Allows the user to provide a font file for use any or all of
476+ * the use cases listed in {@link FSFontUseCase} such as main
477+ * document, SVGs, etc.</p>
478+ *
479+ * <p>For gotchas related to font handling please see:
480+ * <a href="https://github.com/danfickle/openhtmltopdf/wiki/Fonts">Wiki: Fonts</a></p>
481+ *
482+ * @param fontFile A file system font file in true-type format. Beware of using resources as
483+ * they will not be separate files in the final jar.
484+ * @param fontFamily Font family name. If using a font in Java2D, SVG or MathML this should match
485+ * <code>Font.createFont(Font.TRUETYPE_FONT, fontFile).getFamily()</code>.
486+ * @param fontWeight Font boldness, usually 400 for regular fonts and 700 for bold fonts.
487+ * @param fontStyle Normal, italic or oblique.
488+ * @param subset For PDF use whether the font is subset, usually true unless the font is
489+ * being used by form controls.
490+ * @param fontUsedFor Which components use the font such as main document, SVG, etc. Example:
491+ * <code>EnumSet.of(FSFontUseCase.DOCUMENT, FSFontUseCase.SVG)</code>
492+ * @return this for method chaining
493+ */
494+ public TFinalClass useFont (
495+ File fontFile ,
496+ String fontFamily ,
497+ Integer fontWeight ,
498+ FontStyle fontStyle ,
499+ boolean subset ,
500+ Set <FSFontUseCase > fontUsedFor ) {
501+ state ._fonts .add (new AddedFont (null , fontFile , fontWeight , fontFamily , subset , fontStyle , fontUsedFor ));
464502 return (TFinalClass ) this ;
465503 }
466504
467505 /**
468506 * Simpler overload for
469507 * {@link #useFont(File, String, Integer, FontStyle, boolean)}
470508 *
471- * @param fontFile
472- * @param fontFamily
473509 * @return this for method chaining
474510 */
475511 public TFinalClass useFont (File fontFile , String fontFamily ) {
476512 return this .useFont (fontFile , fontFamily , 400 , FontStyle .NORMAL , true );
477513 }
478-
514+
479515 /**
480- * Add a font programmatically. If the font is NOT subset, it will be downloaded
516+ * <p> Add a font programmatically. If the font is NOT subset, it will be downloaded
481517 * when the renderer is run, otherwise, assuming a font-metrics cache has been configured,
482518 * the font will only be downloaded if required. Therefore, the user could add many fonts,
483- * confident that only those that are needed will be downloaded and processed.
519+ * confident that only those that are needed will be downloaded and processed.</p>
484520 *
485- * The InputStream returned by the supplier will be closed by the caller. Fonts
486- * should generally be subset (Java2D rendered ignores this argument),
487- * except when used in form controls. FSSupplier is a lambda compatible interface.
521+ * <p> The InputStream returned by the supplier will be closed by the caller. Fonts
522+ * should generally be subset (Java2D renderer ignores this argument),
523+ * except when used in form controls. FSSupplier is a lambda compatible interface.</p>
488524 *
489- * Fonts can also be added using a font-face at-rule in the CSS.
525+ * <p>Fonts can also be added using a font-face at-rule in the CSS (not
526+ * recommended for Java2D usage).</p>
527+ *
528+ * <p><strong>IMPORTANT:</strong> This method will add fonts for use by the main document
529+ * only. It is not recommended for use with Java2D.
530+ * To add fonts for use by Java2D, SVG, etc see:
531+ * {@link #useFont(File, String, Integer, FontStyle, boolean, Set)}</p>
532+ *
533+ * <p>For gotchas related to font handling please see:
534+ * <a href="https://github.com/danfickle/openhtmltopdf/wiki/Fonts">Wiki: Fonts</a></p>
490535 *
491- * @param supplier
492- * @param fontFamily
493- * @param fontWeight
494- * @param fontStyle
495- * @param subset
496- * @return
536+ * @return this for method chaining
497537 */
498538 public TFinalClass useFont (FSSupplier <InputStream > supplier , String fontFamily , Integer fontWeight ,
499539 FontStyle fontStyle , boolean subset ) {
500- state ._fonts .add (new AddedFont (supplier , null , fontWeight , fontFamily , subset , fontStyle ));
540+ state ._fonts .add (new AddedFont (supplier , null , fontWeight , fontFamily , subset , fontStyle , EnumSet . of ( FSFontUseCase . DOCUMENT ) ));
501541 return (TFinalClass ) this ;
502542 }
503543
504544 /**
505545 * Simpler overload for
506546 * {@link #useFont(FSSupplier, String, Integer, FontStyle, boolean)}
507547 *
508- * @param supplier
509- * @param fontFamily
510- * @return
548+ * @return this for method chaining
511549 */
512550 public TFinalClass useFont (FSSupplier <InputStream > supplier , String fontFamily ) {
513551 return this .useFont (supplier , fontFamily , 400 , FontStyle .NORMAL , true );
@@ -533,4 +571,14 @@ public enum PageSizeUnits {
533571 public enum FontStyle {
534572 NORMAL , ITALIC , OBLIQUE
535573 }
574+
575+ /**
576+ * Use cases for fonts.
577+ */
578+ public enum FSFontUseCase {
579+ /** Main document (PDF or Java2D) */
580+ DOCUMENT ,
581+ SVG ,
582+ MATHML
583+ }
536584}
0 commit comments