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
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceFont metrics override handler. -
Method Summary
Modifier and TypeMethodDescriptionfloatcodePointWidth(FontMetrics metrics, int codePoint) Returns not rounded value for the character's advance.getMetrics(Font font, FontRenderContext context) booleanhasOverride(FontMetrics metrics) Tells whether character advances returned by the specifiedFontMetricsinstance are overridden using the previoussetOverride(FontMetrics, Overrider)call.voidRemoves all overrides set previously bysetOverride(FontMetrics, Overrider)invocations.voidsetOverride(FontMetrics metrics, FontMetricsAccessor.Overrider overrider) Allows to override advance values returned by the specifiedFontMetricsinstance.
-
Method Details
-
getMetrics
Returns aFontMetricsinstance for the givenFontandFontRenderContext. 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 publicFontMetricsAPI, one can only extract it from one of thegetStringBoundsmethods' output.- Parameters:
metrics- font metrics orbjectcodePoint- code point- Returns:
- advance of the specified code point
-
setOverride
Allows to override advance values returned by the specifiedFontMetricsinstance. 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
nullas theoverridervalue will remove any override, if it was set up previously.While this method operates on a specific
FontMetricsinstance, 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 cachedFontMetricsinstance 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 specifiedFontMetricsinstance 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.
-