All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lessvoid.nifty.java2d.renderer.RenderFontJava2dImpl Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package de.lessvoid.nifty.java2d.renderer;

import de.lessvoid.nifty.spi.render.RenderFont;

import javax.annotation.Nonnull;
import java.awt.*;

public class RenderFontJava2dImpl implements RenderFont {

  private final Font font;

  private final RenderDeviceJava2dImpl renderDevice;

  Font getFont() {
    return font;
  }

  public RenderFontJava2dImpl(RenderDeviceJava2dImpl renderDevice, Font font) {
    this.renderDevice = renderDevice;
    this.font = font;
  }

  @Override
  public int getCharacterAdvance(
      char currentCharacter,
      char nextCharacter, float size) {
    // I don't know exactly what to do here.
    return getFont().getSize();
  }

  @Override
  public int getHeight() {
    Graphics graphics = renderDevice.getGraphics();

    if (graphics == null) {
      return 0;
    }

    FontMetrics fontMetrics = graphics.getFontMetrics(font);
    double height = fontMetrics.getStringBounds("A", graphics).getHeight();
    return (int) height;
  }

  @Override
  public int getWidth(@Nonnull String text) {
    return getWidth(text, 1.f);
  }

  @Override
  public int getWidth(@Nonnull String text, float size) {
    Graphics graphics = renderDevice.getGraphics();

    if (graphics == null) {
      return 0;
    }

    FontMetrics fontMetrics = graphics.getFontMetrics(font);
    double width = fontMetrics.getStringBounds(text, graphics).getWidth();
    return (int) width;
  }

  @Override
  public void dispose() {

  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy