Interface Canvas

  • All Superinterfaces:
    java.lang.AutoCloseable, java.io.Closeable
    All Known Implementing Classes:
    AbstractCanvas, PDFCanvas, PNGCanvas, SVGCanvas

    public interface Canvas
    extends java.io.Closeable
    Common graphics interface for drawing

    The coordinate system is initialized by setupPage. It's origin is initially in the bottom left corner of the pages and extends in x-direction to the right and in y-direction to the top.

    The font family is specified at initialization and then used for the entire lifecycle.

    A canvas may only be used to generate a single page. After the result has been retrieved, the instance must not be used anymore.

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void addRectangle​(double x, double y, double width, double height)
      Adds a rectangle to the path
      void closeSubpath()
      Closes the current subpath
      void cubicCurveTo​(double x1, double y1, double x2, double y2, double x, double y)
      Adds a cubic Beziér curve to the open path going from the previous point to the speicifed position.
      void fillPath​(int color)
      Fills the current path and ends it
      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.
      byte[] getResult()
      Returns the generated graphics as a byte array
      double getTextWidth​(java.lang.CharSequence text, int fontSize, boolean isBold)
      Returns the width of the specified text for the specified font size
      void lineTo​(double x, double y)
      Adds a line segment to the open path from the previous point to the speicifed position.
      void moveTo​(double x, double y)
      Moves the current point of the open path to the specified position.
      void putText​(java.lang.String text, double x, double y, int fontSize, boolean isBold)
      Adds text to the graphics.
      void putTextLines​(java.lang.String[] lines, double x, double y, int fontSize, double leading)
      Adds several lines of text to the graphics.
      void setTransformation​(double translateX, double translateY, double rotate, double scaleX, double scaleY)
      Sets a translation, rotation and scaling for the subsequent operations
      void setupPage​(double width, double height, java.lang.String fontFamily)
      Sets up the page
      java.lang.String[] splitLines​(java.lang.String text, double maxLength, int fontSize)
      Splits the text into lines.
      void startPath()
      Starts a path that can be filled or stroked
      void strokePath​(double strokeWidth, int color)
      Strokes the current path and ends it
      • Methods inherited from interface java.io.Closeable

        close
    • Method Detail

      • setupPage

        void setupPage​(double width,
                       double height,
                       java.lang.String fontFamily)
                throws java.io.IOException
        Sets up the page

        The page (and graphics context) is not valid until this method has been called.

        The font family can be specified as a comma separated list, e.g. "Helvetica,Arial,sans". The first family will be used to calculate text widths. For formats that support it (e.g. SVG), the entire list will be used in the generated graphics file. Other format will just use the first one.

        Parameters:
        width - width of page (in mm)
        height - height of page (in mm)
        fontFamily - font family to use
        Throws:
        java.io.IOException - thrown if graphics cannot be generated
      • setTransformation

        void setTransformation​(double translateX,
                               double translateY,
                               double rotate,
                               double scaleX,
                               double scaleY)
                        throws java.io.IOException
        Sets a translation, rotation and scaling for the subsequent operations

        Before a new translation is applied, the coordinate system is reset to it's original state after page setup (see setupPage).

        The transformations are applied in the order translation, rotation, scaling.

        Parameters:
        translateX - translation in x direction (in mm)
        translateY - translation in y direction (in mm)
        rotate - rotation angle, in radians
        scaleX - scale factor in x direction (1.0 = no scaling)
        scaleY - scale factor in y direction (1.0 = no scaling)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • putText

        void putText​(java.lang.String text,
                     double x,
                     double y,
                     int fontSize,
                     boolean isBold)
              throws java.io.IOException
        Adds text to the graphics.

        The text position refers to the left most point on the text's baseline.

        Parameters:
        text - the text
        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)
        isBold - indicates if the text is in bold or regular weight
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • putTextLines

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

        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
      • startPath

        void startPath()
                throws java.io.IOException
        Starts a path that can be filled or stroked
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • moveTo

        void moveTo​(double x,
                    double y)
             throws java.io.IOException
        Moves the current point of the open path to the specified position.
        Parameters:
        x - x-coordinate of position
        y - y-coordinate of position
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • lineTo

        void lineTo​(double x,
                    double y)
             throws java.io.IOException
        Adds a line segment to the open path from the previous point to the speicifed position.
        Parameters:
        x - x-coordinate of position
        y - y-coordinate of position
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • cubicCurveTo

        void cubicCurveTo​(double x1,
                          double y1,
                          double x2,
                          double y2,
                          double x,
                          double y)
                   throws java.io.IOException
        Adds a cubic Beziér curve to the open path going from the previous point to the speicifed position. Two control points control the curve
        Parameters:
        x1 - x-coordinate of first control point
        y1 - y-coordinate of first control point
        x2 - x-coordinate of second control point
        y2 - y-coordinate of second control point
        x - x-coordinate of position
        y - y-coordinate of position
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • addRectangle

        void addRectangle​(double x,
                          double y,
                          double width,
                          double height)
                   throws java.io.IOException
        Adds a rectangle to the path
        Parameters:
        x - the rectangle's left position (in mm)
        y - the rectangle's top position (in mm)
        width - the rectangle's width (in mm)
        height - rectangle's height (in mm)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • closeSubpath

        void closeSubpath()
                   throws java.io.IOException
        Closes the current subpath
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • fillPath

        void fillPath​(int color)
               throws java.io.IOException
        Fills the current path and ends it
        Parameters:
        color - the fill color (expressed similar to HTML, e.g. 0xffffff for white)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • strokePath

        void strokePath​(double strokeWidth,
                        int color)
                 throws java.io.IOException
        Strokes the current path and ends it
        Parameters:
        strokeWidth - the stroke width (in pt)
        color - the stroke color (expressed similar to HTML, e.g. 0xffffff for white)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • getAscender

        double getAscender​(int fontSize)
        Distance between baseline and top of highest letter.
        Parameters:
        fontSize - the font size (in pt)
        Returns:
        the distance (in mm)
      • getDescender

        double getDescender​(int fontSize)
        Distance between baseline and bottom of letter extending the farest below the baseline.
        Parameters:
        fontSize - the font size (in pt)
        Returns:
        the distance (in mm)
      • getLineHeight

        double getLineHeight​(int fontSize)
        Distance between the baselines of two consecutive text lines.
        Parameters:
        fontSize - the font size (in pt)
        Returns:
        the distance (in mm)
      • getTextWidth

        double getTextWidth​(java.lang.CharSequence text,
                            int fontSize,
                            boolean isBold)
        Returns the width of the specified text for the specified font size
        Parameters:
        text - text
        fontSize - font size (in pt)
        isBold - indicates if the text is in bold or regular weight
        Returns:
        width (in mm)
      • splitLines

        java.lang.String[] splitLines​(java.lang.String text,
                                      double maxLength,
                                      int fontSize)
        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.

        Parameters:
        text - the text
        maxLength - the maximum line length (in pt)
        fontSize - the font size (in pt)
        Returns:
        an array of text lines
      • getResult

        byte[] getResult()
                  throws java.io.IOException
        Returns the generated graphics as a byte array

        After this method was called, the page is no longer valid.

        Returns:
        the byte array
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated