Class FontConf
StyleConf configuration object.
The following properties with their respective purpose are available:
- Name
The name of the font, which is essentially the font family. This will ultimately translate to
Font.getFamily().
You may specify the font family name through thefamily(String)method. - Size
The size of the font in points, which will ultimately translate to
Font.getSize(). Use thesize(int)method to specify the size of the font. - Posture
The posture of the font, which is a value between 0 and 1.
A value of 0 means that the font is not italic, while a value of 1 means that the font is "fully" italic.
You can use theposture(float)method to specify the posture of the font. - Weight
The weight of the font (boldness, see
Font.BOLD), which is a value between 0 and 2.The weight of the font can be specified using the
weight(double)method. - Spacing (Tracking)
This property controls the tracking which is a floating point number with the default value of
0, meaning no additional tracking is added to the font.Useful constant values are the predefined
TextAttribute.TRACKING_TIGHTandTextAttribute.TRACKING_LOOSEvalues, which represent values of-0.04and0.04,The tracking value is multiplied by the font point size and passed through the font transform to determine an additional amount to add to the advance of each glyph cluster. Positive tracking values will inhibit formation of optional ligatures. Tracking values are typically between
-0.1and0.3; values outside this range are generally not desirable.You can use the
spacing(float)method to specify the tracking of the font. - Color
The color of the font, which translates to the text property
TextAttribute.FOREGROUND.You can use the
color(Color)orcolor(String)methods to specify the color of the font. - Background Color
The background color of the font which translates to the text property
TextAttribute.BACKGROUND. - Selection Color
The selection color of the font, which translates to
JTextComponent.setSelectionColor(Color).
Note that this property is only relevant for text components, most components do not support text selection. - Underlined
Whether or not the font is underlined. This will ultimately translate to
TextAttribute.UNDERLINE. - Strike
Whether or not the font is strike through. This will ultimately translate to
TextAttribute.STRIKETHROUGH. - Transform
The transform of the font, which is an
AffineTransforminstance. - Paint
The paint of the font, which is a
Paintinstance. Note that specifying a custom paint will override the effects of the color property as the color property is in essence merely a convenience property for a paint painting across the entire font area homogeneously using the specified color. - Background Paint
The background paint of the font, which is a
Paintinstance that is used to paint the background of the font. - Horizontal Alignment
The horizontal alignment of the font.
Note that this property is not relevant for all components, It will usually only be relevant forJLabel,AbstractButtonandJTextFieldtypes or maybe some custom components. Not all components support horizontal alignment. - Vertical Alignment
The vertical alignment of the font.
Note that this property is not relevant for all components, It will usually only be relevant forJLabel,AbstractButtonandJTextFieldtypes or maybe some custom components. Not all components support vertical alignment.
You can use the none() method to specify that no font should be used,
as the instance returned by that method is a font style with an empty string as its name,
and other properties set to their default values,
effectively making it a representation of the absence of a font style.
Also note that this class is immutable, which means that wither-like methods
will always return new instances of this class, leaving the original instance untouched.
This means that you can not modify a font style instance directly, but you can
easily create a modified copy of it by calling one of the wither-like methods.
-
Method Summary
Modifier and TypeMethodDescriptionalignment(UI.Alignment alignment) Returns a new font configuration with both horizontal and vertical alignment policies specified through a unified alignment object.backgroundColor(Color backgroundColor) Returns an updated font config with the specified background color.backgroundColor(String colorString) Returns an updated font config with the specified background color string used to define the background color.backgroundGradient(Configurator<GradientConf> configurator) Configures a gradient function basedPaintfor the background of the font appearance, using a configurator function that takes aGradientConfinstance and returns an updatedGradientConfinstance with the desired properties.backgroundNoise(Configurator<NoiseConf> configurator) backgroundPaint(@Nullable Paint backgroundPaint) Returns an updated font config with the specified background paint.Returns an updated font config with the specified color, which will be used for theTextAttribute.FOREGROUNDproperty of the resultingFontinstance.Returns an updated font config with the specified color string used to define the font color.booleanfamily()Returns the font family name for this font configuration.Returns an updated font config with the specified font family name.gradient(Configurator<GradientConf> configurator) Configures a gradient function basedPaintfor the font appearance, using a configurator function that takes aGradientConfinstance and returns an updatedGradientConfinstance with the desired properties.inthashCode()The horizontal alignment of a font configuration is an enum constant which express how text should be positioned within components that support text alignment in terms of left/right/center positioning along the x-axis.horizontalAlignment(UI.HorizontalAlignment horizontalAlignment) Returns an updated font config with the specified horizontal alignment policy, that expresses text to be placed in certain positions alongside the horizontal axis.booleanReturns whether the font should be rendered with an underline.noise(Configurator<NoiseConf> configurator) static FontConfnone()Returns a font style with all properties set to their no-op default values, effectively making it a representation of the absence of a font style.Returns an updated font config with the specified paint.posture()Returns the posture (italic) attribute of this font configuration.posture(float posture) Returns an updated font config with the specified posture, defining the tilt of the font.Returns the text selection color for this font configuration.selectionColor(Color selectionColor) Returns an updated font config with the specified selection color.selectionColor(String colorString) Returns an updated font config with the specified selection color string used to define the selection color.intsize()Returns the font size in points for this font configuration.size(int fontSize) Returns an updated font config with the specified font size, which will translate to aFontinstance with the specified size (seeFont.getSize()).floatspacing()Returns the character spacing (tracking) attribute of this font configuration.spacing(float spacing) Returns an updated font config with the specified spacing, defining the tracking of the font.strikeThrough(boolean strike) Returns an updated font config with the specified strike through property.style(UI.FontStyle fontStyle) Determines if the font should be plain, bold, italic or bold and italic based on the providedUI.FontStyleparameter, which may beUI.FontStyle.PLAIN,UI.FontStyle.BOLD,UI.FontStyle.ITALICorUI.FontStyle.BOLD_ITALIC.
Note that this will override any previous bold or italic settings.Creates a newFontinstance based on this font config, using the default system font as the base font.toString()transform(@Nullable AffineTransform transform) Returns an updated font config with the specified transform.underlined(boolean underlined) Returns an updated font config with the specified underlined property.The vertical alignment of a font configuration is an enum constant which express how text should be positioned within components that support text alignment in terms of top/bottom/center positioning along the y-axis.verticalAlignment(UI.VerticalAlignment verticalAlignment) Returns an updated font config with the specified vertical alignment policy, that * expresses text to be placed in certain positions alongside the certical axis.weight()Returns the weight (boldness) attribute of this font configuration.weight(double fontWeight) Returns an updated font config with the specified weight, defining the boldness of the font.withPropertiesFromFont(Font font)
-
Method Details
-
none
Returns a font style with all properties set to their no-op default values, effectively making it a representation of the absence of a font style.- Returns:
- A font style with all properties set to no-op default values.
-
family
Returns the font family name for this font configuration. This corresponds to theFont.getFamily()property inFontand determines the visual style category of the typeface.The font family represents a group of related typefaces that share common design characteristics but may vary in weight, style, or other attributes. Common examples include:
- "Arial" - A clean, sans-serif font
- "Times New Roman" - A classic serif font
- "Courier New" - A monospaced font
- "Verdana" - A sans-serif font designed for screen readability
From a user perspective, the font family defines the overall visual personality of the text.
- Returns:
- The font family name as a String. Returns an empty string if no specific family is defined, which typically means the system default font family should be used.
- See Also:
-
size
public int size()Returns the font size in points for this font configuration. This corresponds to theFont.getSize()property inFontand determines the visual height of the text characters.In typography, a "point" is a physical unit of measurement where 1 point equals 1/72 of an inch. However, in digital typography, the actual rendered size may vary based on screen resolution, display density (DPI), and the current UI scaling factor.
The perceived size may also be influenced by the specific font family, as some typefaces appear larger than others at the same point size due to differences in x-height and character proportions.
- Returns:
- The font size in points. Returns 0 if no specific size is defined, which typically means the system default font size should be used.
- See Also:
-
posture
Returns the posture (italic) attribute of this font configuration. This corresponds to theTextAttribute.POSTUREproperty and influences whether the font appears in regular upright or italic (slanted) form.The posture is represented as a floating-point value where:
- 0.0 - Regular upright posture (non-italic)
- 0.2 - Standard italic posture
- Values between 0.0 and 0.2 represent varying degrees of slant
From a user perspective, italic text serves several purposes:
- Emphasis - Drawing attention to specific words or phrases within a body of text
- Distinction - Differentiating certain types of content, such as book titles, foreign words, or technical terms
- Visual variety - Adding stylistic variation to break visual monotony
- Hierarchy - Creating subtle visual distinctions in complex layouts
It's important to note that true italic fonts are specifically designed with unique character shapes, while oblique fonts are simply slanted versions of the regular font. The visual quality depends on whether the font family includes dedicated italic variants.
- Returns:
- An
Optionalcontaining the posture value if defined, or empty if no specific posture is configured (system default should be used). - See Also:
-
weight
Returns the weight (boldness) attribute of this font configuration. This corresponds to theTextAttribute.WEIGHTproperty and determines the thickness of the character strokes.The weight is represented as a floating-point value where:
- 0.0 - Light weight (thin strokes)
- 1.0 - Regular/standard weight (equivalent to
Font.PLAIN) - 2.0 - Bold weight (equivalent to
Font.BOLD) - Values between these represent intermediate weights (semibold, etc.)
The actual availability of different weights depends on the font family. Some families only support regular and bold, while professional typefaces may offer multiple weights from thin to black.
- Returns:
- An
Optionalcontaining the weight value if defined, or empty if no specific weight is configured (system default should be used). - See Also:
-
spacing
public float spacing()Returns the character spacing (tracking) attribute of this font configuration. This corresponds to theTextAttribute.TRACKINGproperty and controls the uniform adjustment of space between characters.The spacing value is a multiplier that affects the space between all characters:
- 0.0 - Normal, font-default spacing
- Negative values (-0.04 to 0.0) - Tighter spacing, characters are closer together
- Positive values (0.0 to 0.04) - Looser spacing, characters are farther apart
Useful predefined constants include:
TextAttribute.TRACKING_TIGHT(-0.04)TextAttribute.TRACKING_LOOSE(0.04)
Typical values range from -0.1 to 0.3, with values outside this range generally producing undesirable results for continuous text. Tighter spacing can make text feel denser and more compact, while looser spacing can improve legibility, especially at small sizes or for users with visual impairments
- Returns:
- The character spacing value. Returns 0.0 if normal, font-default spacing should be used.
- See Also:
-
selectionColor
Returns the text selection color for this font configuration. This property is specifically relevant for text components that support user text selection, such asJTextField,JTextArea, andJTextPane.The selection color determines the background color applied to text when users highlight it with mouse dragging or keyboard navigation. This visual feedback is crucial for indicating which text is currently selected for operations like copy, cut, or formatting changes.
This property typically maps to component methods like
JTextComponent.setSelectionColor(Color). When not specified, components use their default selection colors, which are often derived from the current look and feel or system preferences. -
isUnderlined
public boolean isUnderlined()Returns whether the font should be rendered with an underline. This corresponds to theTextAttribute.UNDERLINEproperty and controls the presence of a horizontal line beneath the text baseline.The underline is a binary attribute - text is either underlined or not. When enabled, a continuous line is drawn below the text characters, typically positioned to avoid descending characters (like 'g', 'j', 'p', 'q', 'y').
It's important to use underlining judiciously, as excessive use can reduce readability and create visual noise. In modern UI design, underlining is primarily reserved for link identification.
- Returns:
trueif the font should be rendered with an underline,falseif no underline should be applied. Returns the default value (false) if no specific underline preference is configured.- See Also:
-
horizontalAlignment
The horizontal alignment of a font configuration is an enum constant which express how text should be positioned within components that support text alignment in terms of left/right/center positioning along the x-axis.Component Support Limitations:
Note that horizontal alignment is only applicable to specific Swing components that expose alignment methods or other means of styling, such as:JLabel.setHorizontalAlignment(int)AbstractButton.setHorizontalAlignment(int)JTextField.setHorizontalAlignment(int)(vertical alignment not supported)JTextPane.getStyledDocument()(vertical alignment not supported)
UIForAnySwing.withStyle(Styler)).- Returns:
- The horizontal alignment constant of this conf, which may be NONE, LEFT, CENTER, RIGHT... among others.
-
verticalAlignment
The vertical alignment of a font configuration is an enum constant which express how text should be positioned within components that support text alignment in terms of top/bottom/center positioning along the y-axis.Component Support Limitations:
This property will have no effect on components that don't support text alignment, except for when you use this property to render text through the SwingTree style API (see
Note that vertical alignment is only applicable to specific Swing components that expose alignment methods or other means of styling, such as:UIForAnySwing.withStyle(Styler)).- Returns:
- The vertical alignment constant of this conf, which may be NONE, TOP, CENTER, BOTTOM... among others.
-
family
Returns an updated font config with the specified font family name.- Parameters:
fontFamily- The font family name to use for theFont.getFamily()property.- Returns:
- A new font style with the specified font family name.
-
size
Returns an updated font config with the specified font size, which will translate to aFontinstance with the specified size (seeFont.getSize()).- Parameters:
fontSize- The font size to use for theFont.getSize()property.- Returns:
- A new font style with the specified font size.
-
posture
Returns an updated font config with the specified posture, defining the tilt of the font. AFontwith a higher posture value will be more italic. (seeFont.isItalic()).- Parameters:
posture- The posture to use for theFont.isItalic()property.- Returns:
- A new font style with the specified posture.
-
weight
Returns an updated font config with the specified weight, defining the boldness of the font. AFontwith a higher weight value will be bolder. (seeFont.isBold()).- Parameters:
fontWeight- The weight to use for theFont.isBold()property.- Returns:
- A new font style with the specified weight.
-
style
Determines if the font should be plain, bold, italic or bold and italic based on the providedUI.FontStyleparameter, which may beUI.FontStyle.PLAIN,UI.FontStyle.BOLD,UI.FontStyle.ITALICorUI.FontStyle.BOLD_ITALIC.
Note that this will override any previous bold or italic settings.- Parameters:
fontStyle- The font style to use for the font in theUI.FontStyleenum.- Returns:
- An updated font config with the specified font style.
-
spacing
Returns an updated font config with the specified spacing, defining the tracking of the font. The tracking value is multiplied by the font point size and passed through the font transform to determine an additional amount to add to the advance of each glyph cluster. Positive tracking values will inhibit formation of optional ligatures. Tracking values are typically between-0.1and0.3; values outside this range are generally not desirable.- Parameters:
spacing- The spacing to use for theTextAttribute.TRACKINGproperty.- Returns:
- A new font style with the specified spacing.
-
color
Returns an updated font config with the specified color, which will be used for theTextAttribute.FOREGROUNDproperty of the resultingFontinstance.- Parameters:
color- The color to use for theTextAttribute.FOREGROUNDproperty.- Returns:
- A new font style with the specified color.
-
color
Returns an updated font config with the specified color string used to define the font color. The color will be used for theTextAttribute.FOREGROUNDproperty of the resultingFontinstance.- Parameters:
colorString- The color string to use for theTextAttribute.FOREGROUNDproperty.- Returns:
- A new font style with the specified color.
-
backgroundColor
Returns an updated font config with the specified background color. The color value will be used for theTextAttribute.BACKGROUNDproperty of the resultingFontinstance.- Parameters:
backgroundColor- The background color to use for theTextAttribute.BACKGROUNDproperty.- Returns:
- A new font style with the specified background color.
-
backgroundColor
Returns an updated font config with the specified background color string used to define the background color. The background color will be used for theTextAttribute.BACKGROUNDproperty of the resultingFontinstance.- Parameters:
colorString- The color string to use for theTextAttribute.BACKGROUNDproperty.- Returns:
- A new font style with the specified background color.
-
selectionColor
Returns an updated font config with the specified selection color. The selection color will be used for the selection color of the font. Note that not all components support text selection, so this property may not have an effect on all components.- Parameters:
selectionColor- The selection color to use for the selection color of the font.- Returns:
- A new font style with the specified selection color.
-
selectionColor
Returns an updated font config with the specified selection color string used to define the selection color. The selection color will be used for the selection color of the font. Note that not all components support text selection, so this property may not have an effect on all components.- Parameters:
colorString- The color string to use for the selection color of the font.- Returns:
- A new font style with the specified selection color.
-
underlined
Returns an updated font config with the specified underlined property. This boolean will translate to theTextAttribute.UNDERLINEproperty of the resultingFontinstance.- Parameters:
underlined- Whether the font should be underlined.- Returns:
- A new font style with the specified underlined property.
-
strikeThrough
Returns an updated font config with the specified strike through property. This boolean will translate to theTextAttribute.STRIKETHROUGHproperty of the resultingFontinstance.- Parameters:
strike- Whether the font should be strike through.- Returns:
- A new font style with the specified strike through property.
-
transform
Returns an updated font config with the specified transform. This transform will be used for theTextAttribute.TRANSFORMproperty of the resultingFontinstance.- Parameters:
transform- The transform to use for theTextAttribute.TRANSFORMproperty.- Returns:
- A new font style with the specified transform.
-
paint
Returns an updated font config with the specified paint. This paint will be used for theTextAttribute.FOREGROUNDproperty of the resultingFontinstance. Note that specifying a custom paint will override the effects of the color property as the color property is in essence merely a convenience property for a paint painting across the entire font area homogeneously using the specified color.
Note that this will override the effects of thecolor(Color),color(String),noise(Configurator)orgradient(Configurator)methods as a font can only have one paint.- Parameters:
paint- The paint to use for theTextAttribute.FOREGROUNDproperty.- Returns:
- A new font style with the specified paint.
-
noise
Configures a noise function basedPaintfor the font appearance, using a configurator function that takes aNoiseConfinstance and returns an updatedNoiseConfinstance with the desired properties.
Keep in mind that this will override the effects of thecolor(Color),color(String),paint(Paint)orgradient(Configurator)methods as a font can only have one paint. -
gradient
Configures a gradient function basedPaintfor the font appearance, using a configurator function that takes aGradientConfinstance and returns an updatedGradientConfinstance with the desired properties.
Keep in mind that this will override the effects of thecolor(Color),color(String),paint(Paint)ornoise(Configurator)methods as a font can only have one paint.- Parameters:
configurator- The configurator function that takes aGradientConfinstance and returns an updatedGradientConfinstance with the desired properties.- Returns:
- A new font style with the specified gradient paint.
-
backgroundPaint
Returns an updated font config with the specified background paint. This paint will be used for theTextAttribute.BACKGROUNDproperty of the resultingFontinstance.- Parameters:
backgroundPaint- The background paint to use for theTextAttribute.BACKGROUNDproperty.- Returns:
- A new font style with the specified background paint.
-
backgroundNoise
Configures a noise function basedPaintfor the background of the font appearance, using a configurator function that takes aNoiseConfinstance and returns an updatedNoiseConfinstance with the desired properties.
Note that the background can only have one paint, so specifying a noise based paint will override the effects of thebackgroundPaint(Paint),backgroundGradient(Configurator), andbackgroundColor(String)methods. -
backgroundGradient
Configures a gradient function basedPaintfor the background of the font appearance, using a configurator function that takes aGradientConfinstance and returns an updatedGradientConfinstance with the desired properties.
The background of a font can only have one paint, so specifying a gradient based paint will override the effects of thebackgroundPaint(Paint),backgroundNoise(Configurator), andbackgroundColor(String)methods.- Parameters:
configurator- The configurator function that takes aGradientConfinstance and returns an updatedGradientConfinstance with the desired properties.- Returns:
- A new font style with the specified gradient background paint.
-
horizontalAlignment
Returns an updated font config with the specified horizontal alignment policy, that expresses text to be placed in certain positions alongside the horizontal axis. This property is not relevant for all components, as it can only be applied to methods likeJLabel.setHorizontalAlignment(int)AbstractButton.setHorizontalAlignment(int)andJTextField.setHorizontalAlignment(int), which not all components have. You can specify this horizontal text alignment property effectively through theComponentStyleDelegate.fontAlignment(UI.VerticalAlignment)or through theFontConfin aUI.Fontpassed to methods likeUIForAnySwing.withFont(UI.Font)orUIForAnySwing.withFont(Val).- Parameters:
horizontalAlignment- The horizontal alignment to use for the font.- Returns:
- A new font style with the specified horizontal alignment.
- Throws:
NullPointerException- if the supplied enum constant is null.
-
verticalAlignment
Returns an updated font config with the specified vertical alignment policy, that * expresses text to be placed in certain positions alongside the certical axis. This property is not relevant for all components, as it can only be applied to methods likeJLabel.setVerticalAlignment(int)andAbstractButton.setVerticalAlignment(int), which not all components have! You can specify this property effectively through theComponentStyleDelegate.fontAlignment(UI.VerticalAlignment)or through theFontConfin aUI.Fontpassed to methods likeUIForAnySwing.withFont(UI.Font)orUIForAnySwing.withFont(Val).- Parameters:
verticalAlignment- The vertical alignment to use for the font.- Returns:
- A new font style with the specified vertical alignment.
- Throws:
NullPointerException- if the supplied enum constant is null.
-
alignment
Returns a new font configuration with both horizontal and vertical alignment policies specified through a unified alignment object. This method provides a convenient way to set both alignment dimensions simultaneously while ensuring consistency between horizontal and vertical alignment settings.The alignment properties control how text is positioned within components that support text alignment. The horizontal alignment determines left/right/center positioning along the x-axis, while vertical alignment controls top/bottom/center positioning along the y-axis.
Component Support Limitations:
Note that alignment properties are only applicable to specific Swing components that expose alignment methods, such as:JLabel.setHorizontalAlignment(int)andJLabel.setVerticalAlignment(int)AbstractButton.setHorizontalAlignment(int)andAbstractButton.setVerticalAlignment(int)JTextField.setHorizontalAlignment(int)(vertical alignment not supported)
ComponentStyleDelegate.fontAlignment(UI.Alignment))Recommended Usage:
For effective alignment specification, use either:ComponentStyleDelegate.fontAlignment(UI.VerticalAlignment)for individual alignmentFontConfwithin aUI.Fontpassed to:
- Parameters:
alignment- The unified alignment object containing both horizontal and vertical alignment specifications. Must not benull.- Returns:
- A new
FontConfinstance with the specified horizontal and vertical alignment properties applied, preserving all other font attributes from the current configuration. - Throws:
NullPointerException- if the providedalignmentparameter isnull.NullPointerException- if the supplied enum constant is null.- See Also:
-
withPropertiesFromFont
- Parameters:
font- TheFontinstance to extract the properties from.- Returns:
- A new
FontConfinstance with the extracted properties. - Throws:
NullPointerException- if the provided font is null.
-
toAwtFont
Creates a newFontinstance based on this font config, using the default system font as the base font.- Returns:
- A new
Fontinstance based on this font config.
-
hashCode
public int hashCode() -
equals
-
toString
-