Class VulkanTextMetrics

java.lang.Object
org.inspirenxe.strata.vulkan.VulkanTextMetrics
All Implemented Interfaces:
org.inspirenxe.strata.api.text.TextMetrics

public final class VulkanTextMetrics extends Object implements org.inspirenxe.strata.api.text.TextMetrics
Measures text the way the Vulkan canvas draws it, with a VulkanFont for ordinary text, another for monospaced text, and the families the application registers under a FontId.

A style's font picks a registered family, which wins over monospace; a family that is not registered, or no font at all, picks the monospaced font for a monospaced style when there is one and the ordinary font otherwise. A style's size scales the chosen font from its own size, and a size of 0 is the default size, the ordinary font's. Widths, line heights and ascents scale alike, and the canvas draws the glyphs at the scaled size rather than stretching them.

Registering, replacing or removing a family changes version(), so open screens lay out again. The metrics never close a registered font: close it after removing it, or after the views that draw with it.

Use it on the UI thread only.

  • Field Details

    • DEFAULT_SIZE

      public static final float DEFAULT_SIZE
      The size defaults() uses, in UI units, the same as the Swing adapter's default font.
      See Also:
  • Constructor Details

    • VulkanTextMetrics

      public VulkanTextMetrics(VulkanFont text)
      Creates metrics with one font for every style; monospaced spans use it too.
      Parameters:
      text - the font
    • VulkanTextMetrics

      public VulkanTextMetrics(VulkanFont text, VulkanFont monospace)
      Creates metrics with a font for monospaced spans.
      Parameters:
      text - the font for ordinary spans
      monospace - the font for spans whose style asks for monospace
  • Method Details

    • defaults

      public static VulkanTextMetrics defaults()
      Returns metrics for the operating system's sans-serif and monospaced fonts at 9.0f units, as FreeTypeFont.system(float) and FreeTypeFont.systemMonospace(float) find them. The fonts are read once per call; keep the result.
      Returns:
      the metrics, without a monospaced font if the system has none of the ones looked for
      Throws:
      IllegalStateException - if no sans-serif font is found; give the metrics a font file instead
    • register

      public Optional<VulkanFont> register(org.inspirenxe.strata.api.text.FontId id, VulkanFont font)
      Registers a family under an id, replacing any family registered under it before, so styles that name the id draw with it.
      Parameters:
      id - the id styles name
      font - the family
      Returns:
      the family registered under the id before, if any, which the caller closes when nothing draws with it
    • unregister

      public Optional<VulkanFont> unregister(org.inspirenxe.strata.api.text.FontId id)
      Removes the family registered under an id, so styles that name it draw with the default font again.
      Parameters:
      id - the id
      Returns:
      the family that was registered, if any, which the caller closes when nothing draws with it
    • registered

      public Optional<VulkanFont> registered(org.inspirenxe.strata.api.text.FontId id)
      Returns the family registered under an id.
      Parameters:
      id - the id
      Returns:
      the family, or empty if none is registered under it
    • defaultSize

      public float defaultSize()
      Returns the size text of size 0 measures and draws at: the ordinary font's own size.
      Returns:
      the size in UI units
    • font

      public VulkanFont font(org.inspirenxe.strata.api.text.TextStyle style)
      Returns the font a style is measured and drawn with, at the font's own size.
      Parameters:
      style - the style; its font and monospace choose between the fonts
      Returns:
      the font
    • hasMonospace

      public boolean hasMonospace()
      Returns whether these metrics have a monospaced font, which the platform reports as Capabilities.monospaceFont().
      Returns:
      true when a monospaced font was given
    • width

      public float width(org.inspirenxe.strata.api.text.TextSpan span)
      Specified by:
      width in interface org.inspirenxe.strata.api.text.TextMetrics
    • lineHeight

      public float lineHeight(org.inspirenxe.strata.api.text.TextStyle style)
      Specified by:
      lineHeight in interface org.inspirenxe.strata.api.text.TextMetrics
    • ascent

      public float ascent(org.inspirenxe.strata.api.text.TextStyle style)
      Specified by:
      ascent in interface org.inspirenxe.strata.api.text.TextMetrics
    • version

      public long version()
      Specified by:
      version in interface org.inspirenxe.strata.api.text.TextMetrics