Package com.jetbrains

Interface FontMetricsAccessor


@Service @Provided public interface FontMetricsAccessor
Provides convenience methods to access 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.
  • Method Details

    • getMetrics

      FontMetrics getMetrics(Font font, FontRenderContext context)
      Returns a FontMetrics instance for the given Font and FontRenderContext. This is supposed to be the same instance as returned by the public API methods (Graphics.getFontMetrics(), Graphics.getFontMetrics(Font) and Component.getFontMetrics(Font)) in the same context.
      Parameters:
      font - the specified font
      context - font rendering context
      Returns:
      font metrics of the specified font
    • codePointWidth

      float codePointWidth(FontMetrics metrics, int codePoint)
      Returns not rounded value for the character's advance. It's not accessible directly via public FontMetrics API, one can only extract it from one of the getStringBounds methods' output.
      Parameters:
      metrics - font metrics orbject
      codePoint - code point
      Returns:
      advance of the specified code point
    • setOverride

      void setOverride(FontMetrics metrics, FontMetricsAccessor.Overrider overrider)
      Allows to override advance values returned by the specified FontMetrics instance. It's not generally guaranteed the invocation of this method actually has the desired effect. One can verify whether it's the case using hasOverride(FontMetrics) method.

      A subsequent invocation of this method will override any previous invocations. Passing null as the overrider 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 cached FontMetrics 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 override
      overrider - override handler
    • hasOverride

      boolean hasOverride(FontMetrics metrics)
      Tells whether character advances returned by the specified FontMetrics instance are overridden using the previous setOverride(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 by setOverride(FontMetrics, Overrider) invocations.