Package net.codecrete.qrbill.canvas
Class AbstractCanvas
java.lang.Object
net.codecrete.qrbill.canvas.AbstractCanvas
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Canvas
- Direct Known Subclasses:
Graphics2DCanvas
,PDFCanvas
,SVGCanvas
Abstract base class for simplified implementation of
Canvas
classes.
The class mainly implements text measurement and a helper for multi-line text.
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.codecrete.qrbill.canvas.Canvas
Canvas.LineStyle
-
Field Summary
Modifier and TypeFieldDescriptionprotected FontMetrics
Font metrics information.protected static final double
Factor for converting mm to points (1/72in) -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble
getAscender
(int fontSize) Distance between baseline and top of the highest letter.double
getDescender
(int fontSize) Distance between baseline and bottom of letter extending the farthest below the baseline.double
getLineHeight
(int fontSize) Distance between the baselines of two consecutive text lines.double
getTextWidth
(CharSequence text, int fontSize, boolean isBold) Returns the width of the specified text for the specified font sizevoid
putTextLines
(String[] lines, double x, double y, int fontSize, double leading) Adds several lines of text to the graphics.protected void
setupFontMetrics
(String fontFamilyList) Initializes the font metrics information for the specified font.String[]
splitLines
(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 net.codecrete.qrbill.canvas.Canvas
addRectangle, closeSubpath, cubicCurveTo, fillPath, lineTo, moveTo, putText, setTransformation, startPath, strokePath
-
Field Details
-
MM_TO_PT
protected static final double MM_TO_PTFactor for converting mm to points (1/72in)- See Also:
-
fontMetrics
Font metrics information.Available once
setupFontMetrics(String)
has been called.
-
-
Constructor Details
-
AbstractCanvas
protected AbstractCanvas()Creates a new instance.
-
-
Method Details
-
setupFontMetrics
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 interfaceCanvas
- Parameters:
lines
- the text linesx
- 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 interfaceCanvas
- 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 interfaceCanvas
- 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 interfaceCanvas
- Parameters:
fontSize
- the font size (in pt)- Returns:
- the distance (in mm)
-
getTextWidth
Description copied from interface:Canvas
Returns the width of the specified text for the specified font size- Specified by:
getTextWidth
in interfaceCanvas
- Parameters:
text
- textfontSize
- font size (in pt)isBold
- indicates if the text is in bold or regular weight- Returns:
- width (in mm)
-
splitLines
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 interfaceCanvas
- Parameters:
text
- the textmaxLength
- the maximum line length (in pt)fontSize
- the font size (in pt)- Returns:
- an array of text lines
-