Class AbstractCanvas

java.lang.Object
net.codecrete.qrbill.canvas.AbstractCanvas
All Implemented Interfaces:
Closeable, AutoCloseable, Canvas
Direct Known Subclasses:
Graphics2DCanvas, PDFCanvas, SVGCanvas

public abstract class AbstractCanvas extends Object implements Canvas
Abstract base class for simplified implementation of Canvas classes.

The class mainly implements text measurement and a helper for multi-line text.

  • Field Details

  • Constructor Details

    • AbstractCanvas

      protected AbstractCanvas()
      Creates a new instance.
  • Method Details

    • setupFontMetrics

      protected void setupFontMetrics(String fontFamilyList)
      Initializes the font metrics information for the specified font.

      The first font in the specified list of fonts is used.

      Parameters:
      fontFamilyList - list of font families
    • putTextLines

      public void putTextLines(String[] lines, double x, double y, int fontSize, double leading) throws IOException
      Description copied from interface: Canvas
      Adds several lines of text to the graphics.

      The text position refers to the left most point on the baseline of the first text line. Additional lines then follow below.

      Specified by:
      putTextLines in interface Canvas
      Parameters:
      lines - the text lines
      x - x position of the text's start (in mm)
      y - y position of the text's top (in mm)
      fontSize - the font size (in pt)
      leading - additional vertical space between text lines (in mm)
      Throws:
      IOException - thrown if the graphics cannot be generated
    • getAscender

      public double getAscender(int fontSize)
      Description copied from interface: Canvas
      Distance between baseline and top of the highest letter.
      Specified by:
      getAscender in interface Canvas
      Parameters:
      fontSize - the font size (in pt)
      Returns:
      the distance (in mm)
    • getDescender

      public double getDescender(int fontSize)
      Description copied from interface: Canvas
      Distance between baseline and bottom of letter extending the farthest below the baseline.
      Specified by:
      getDescender in interface Canvas
      Parameters:
      fontSize - the font size (in pt)
      Returns:
      the distance (in mm)
    • getLineHeight

      public double getLineHeight(int fontSize)
      Description copied from interface: Canvas
      Distance between the baselines of two consecutive text lines.
      Specified by:
      getLineHeight in interface Canvas
      Parameters:
      fontSize - the font size (in pt)
      Returns:
      the distance (in mm)
    • getTextWidth

      public double getTextWidth(CharSequence text, int fontSize, boolean isBold)
      Description copied from interface: Canvas
      Returns the width of the specified text for the specified font size
      Specified by:
      getTextWidth in interface Canvas
      Parameters:
      text - text
      fontSize - font size (in pt)
      isBold - indicates if the text is in bold or regular weight
      Returns:
      width (in mm)
    • splitLines

      public String[] splitLines(String text, double maxLength, int fontSize)
      Description copied from interface: Canvas
      Splits the text into lines.

      If a line would exceed the specified maximum length, line breaks are inserted. Newlines are treated as fixed line breaks.

      Specified by:
      splitLines in interface Canvas
      Parameters:
      text - the text
      maxLength - the maximum line length (in pt)
      fontSize - the font size (in pt)
      Returns:
      an array of text lines