FontMetrics
instances, and obtain character advances from them without
rounding. Also provides an (unsafe) way to override character advances in those instances with arbitrary specified
values.-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Font metrics override handler. -
Method Summary
Modifier and TypeMethodDescriptionfloat
codePointWidth
(FontMetrics metrics, int codePoint) Returns not rounded value for the character's advance.getMetrics
(Font font, FontRenderContext context) boolean
hasOverride
(FontMetrics metrics) Tells whether character advances returned by the specifiedFontMetrics
instance are overridden using the previoussetOverride(FontMetrics, Overrider)
call.void
Removes all overrides set previously bysetOverride(FontMetrics, Overrider)
invocations.void
setOverride
(FontMetrics metrics, FontMetricsAccessor.Overrider overrider) Allows to override advance values returned by the specifiedFontMetrics
instance.
-
Method Details
-
getMetrics
Returns aFontMetrics
instance for the givenFont
andFontRenderContext
. This is supposed to be the same instance as returned by the public API methods (Graphics.getFontMetrics()
,Graphics.getFontMetrics(Font)
andComponent.getFontMetrics(Font)
) in the same context.- Parameters:
font
- the specified fontcontext
- font rendering context- Returns:
- font metrics of the specified font
-
codePointWidth
Returns not rounded value for the character's advance. It's not accessible directly via publicFontMetrics
API, one can only extract it from one of thegetStringBounds
methods' output.- Parameters:
metrics
- font metrics orbjectcodePoint
- code point- Returns:
- advance of the specified code point
-
setOverride
Allows to override advance values returned by the specifiedFontMetrics
instance. It's not generally guaranteed the invocation of this method actually has the desired effect. One can verify whether it's the case usinghasOverride(FontMetrics)
method.A subsequent invocation of this method will override any previous invocations. Passing
null
as theoverrider
value will remove any override, if it was set up previously.While this method operates on a specific
FontMetrics
instance, it's expected that overriding will have effect regardless of the method font metrics are accessed, for all future character advance requests. This is feasible, as JDK implementation generally uses the same cachedFontMetrics
instance in identical contexts.The method doesn't provides any concurrency guarantees, i.e. the override isn't guaranteed to be immediately visible for font metrics readers in other threads.
WARNING. This method can break the consistency of a UI application, as previously calculated/returned advance values can already be used/cached by some UI components. It's the calling code's responsibility to remediate such consequences (e.g. re-validating all components which use the relevant font might be required).
- Parameters:
metrics
- font metrics to overrideoverrider
- override handler
-
hasOverride
Tells whether character advances returned by the specifiedFontMetrics
instance are overridden using the previoussetOverride(FontMetrics, Overrider)
call.- Parameters:
metrics
- font metrics being checked- Returns:
- true if given font metrics is overridden
-
removeAllOverrides
void removeAllOverrides()Removes all overrides set previously bysetOverride(FontMetrics, Overrider)
invocations.
-