Class AbstractCanvas

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, Canvas
    Direct Known Subclasses:
    PDFCanvas, PNGCanvas, SVGCanvas

    public abstract class AbstractCanvas
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      protected static double MM_TO_PT  
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractCanvas()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double getAscender​(int fontSize)
      Distance between baseline and top of highest letter.
      double getDescender​(int fontSize)
      Distance between baseline and bottom of letter extending the farest below the baseline.
      double getLineHeight​(int fontSize)
      Distance between the baselines of two consecutive text lines.
      double getTextWidth​(java.lang.CharSequence text, int fontSize, boolean isBold)
      Returns the width of the specified text for the specified font size
      void putTextLines​(java.lang.String[] lines, double x, double y, int fontSize, double leading)
      Adds several lines of text to the graphics.
      protected void setupFontMetrics​(java.lang.String fontFamilyList)  
      java.lang.String[] splitLines​(java.lang.String text, double maxLength, int fontSize)
      Splits the text into lines.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.io.Closeable

        close
    • Constructor Detail

      • AbstractCanvas

        public AbstractCanvas()
    • Method Detail

      • setupFontMetrics

        protected void setupFontMetrics​(java.lang.String fontFamilyList)
      • putTextLines

        public void putTextLines​(java.lang.String[] lines,
                                 double x,
                                 double y,
                                 int fontSize,
                                 double leading)
                          throws java.io.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:
        java.io.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 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 farest 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​(java.lang.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 java.lang.String[] splitLines​(java.lang.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