SetaPDF_Core_Text_RichTextBlock Class representing a rich-text block which can be drawn onto a canvas object
File: /SetaPDF v2/Core/Text/RichTextBlock.php
A rich-text block allows you to use a subset of HTML and CSS to style the text. Following HTML tags are interpreted as you know from HTML:
-
<b>
/<strong>
for bold text -
<i>
/<em>
for italic text -
<u>
for underline text -
<sup>
for superscript -
<sub>
for subscript -
<br>
/</br>
for a line-break
You can use any other HTML tag such as <span>
or <div>
, too. Internally these tags are only parsed
for their style
attibute.
You can use the style
attribute to define CSS styles for an element to any tag.
Following CSS styles are supported:
-
font-family: (string)
-
font-size: (float|integer)(pt|%)
-
color: #RRGGBB
hexadecimal notation -
line-height: (float|integer)(%)
unitless or percentual value
By default, the instance uses the standard font Helvetica
and
loads the different styles automatically by a predefined font-loader callable.
To use individual fonts and make use of font subsets you have to pass your own font-loader which is a callable with following signature:
function ( SetaPDF_Core_Document $document, string $fontFamily, # The font family as defined in the font-family property. string $fontStyle # An empty string = normal, 'B' = bold, 'I' = italic, 'BI' = bold+italic ): ?SetaPDF_Core_Font_FontInterface`
The font instances created in this callable are bound to the document instance, and you need to take care of caching the instances appropriately to avoid an unneeded overhead. You also should make sure that the callable shares the font instances through different rich-text block instances.
A font-loader for e.g. DejaVuSans could look like:
$loadedFonts = []; $fontLoader = function (SetaPDF_Core_Document $document, $fontFamily, $fontStyle) use (&$loadedFonts) { $cacheKey = $document->getInstanceIdent() . '_' . $fontFamily . '_' . $fontStyle; $font = null; if (!array_key_exists($cacheKey, $loadedFonts)) { if ($fontFamily === 'DejaVuSans' && $fontStyle === '') { $font = new SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'B') { $font = new SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans-Bold.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'I') { $font = new SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans-Oblique.ttf'); } elseif ($fontFamily === 'DejaVuSans' && $fontStyle === 'BI') { $font = new SetaPDF_Core_Font_Type0_Subset($document, 'path/to/DejaVuSans-BoldOblique.ttf'); } $loadedFonts[$cacheKey] = $font; } return $loadedFonts[$cacheKey]; };
and can be registered by the registerFontLoader()
method:
$richTextBlock->registerFontLoader($fontLoader);
In the same step you should set Frutiger as the default font-family:
$richTextBlock->setDefaultFontFamily('DejaVuSans');
Class hierarchy
Summary
Methods
- __construct()
- calculateItemWidths()
- calculateLineHeights()
- calculateLines()
- draw()
- drawBorderAndBackground()
- drawText()
- fixWordsWithNbsp()
- getAlign()
- getBackgroundColor()
- getBorderColor()
- getBorderWidth()
- getDefaultFontFamily()
- getDefaultFontSize()
- getDefaultLineHeight()
- getDefaultTextColor()
- getHeight()
- getPaddingBottom()
- getPaddingLeft()
- getPaddingRight()
- getPaddingTop()
- getTextWidth()
- getWidth()
- loadFont()
- parseChilds()
- parseNode()
- parseText()
- parseTextBlocks()
- registerDefaultFontLoader()
- registerFontLoader()
- setAlign()
- setBackgroundColor()
- setBorderColor()
- setBorderWidth()
- setDefaultFontFamily()
- setDefaultFontSize()
- setDefaultLineHeight()
- setDefaultTextColor()
- setPadding()
- setPaddingBottom()
- setPaddingLeft()
- setPaddingRight()
- setPaddingTop()
- setStrict()
- setText()
- setTextWidth()
Properties
$bodyNode
$calculatedLines
$defaultTextColor
$dynamicWidth
Used for caching if no width is given.
$fontLoader
A callback with the signature function (SetaPDF_Core_Document $document, string $fontFamily, string $fontStyle): ?SetaPDF_Core_Font_FontInterface
$locale
$width
Methods
__construct()
Parameters
- $document : SetaPDF_Core_Document
calculateLines()
Exceptions
Throws SetaPDF_Core_Exception
draw()
Draws the text block onto a canvas.
Parameters
- $canvas : SetaPDF_Core_Canvas
- $x : int|float
The lower left x-value of the text block
- $y : int|float
The lower left y-value of the text block
Exceptions
Throws SetaPDF_Core_Exception
Throws SetaPDF_Core_Font_Exception
Throws SetaPDF_Core_Type_Exception
drawBorderAndBackground()
Draws the border and background onto the canvas.
Parameters
- $canvas : SetaPDF_Core_Canvas
- $x : int|float
The lower left x-value of the text block
- $y : int|float
The lower left y-value of the text block
Exceptions
Throws SetaPDF_Core_Exception
drawText()
Parameters
- $canvas : SetaPDF_Core_Canvas
- $x : float|int
- $y : float|int
Exceptions
Throws SetaPDF_Core_Exception
Throws SetaPDF_Core_Font_Exception
Throws SetaPDF_Core_Type_Exception
fixWordsWithNbsp()
Split up words which contain spaces created by . Primarily needed for proper justify alignment as spaces are also stretched.
Parameters
- $lines : array
getBackgroundColor()
Get the background color object.
getBorderColor()
Get the border color object.
If no border color is defined a greyscale black color will be returned.
getHeight()
Returns the height of the text block including padding and border.
Exceptions
Throws SetaPDF_Core_Exception
getTextWidth()
getWidth()
Returns the width of the rich-text including padding and border.
Exceptions
Throws SetaPDF_Core_Exception
loadFont()
Parameters
- $fontFamily : string
- $fontStyle : string
Exceptions
Throws SetaPDF_Core_Exception
parseChilds()
Parameters
- $node : DOMNode
- $fontFamily : string
- $fontSize : int|float
- $color : SetaPDF_Core_DataStructure_Color
- $fontStyle : string
- $lineHeight : int|float
- $fontDecoration : array
Exceptions
Throws SetaPDF_Core_Exception
parseNode()
Parameters
- $node : DOMNode
- $fontFamily : string
- $fontSize : int|float
- $color : SetaPDF_Core_DataStructure_Color
- $fontStyle : string
- $lineHeight : int|float
- $fontDecoration : array
Exceptions
Throws SetaPDF_Core_Exception
parseText()
registerDefaultFontLoader()
Registers the default font-loader which handles the standard font Helvetica.
Parameters
- $loadedFonts : array
Memorized loaded fonts. If you're using multiple RichTextBlocks these should share the same $loadedFonts array.
registerFontLoader()
Register a font loader.
Please note that you MUST cache the results of this callback per document.
Parameters
- $resolveFont : callable
A callable with the following signature:
function (SetaPDF_Core_Document $document, string $fontFamily, string $fontStyle): ?SetaPDF_Core_Font_FontInterface
See
- registerDefaultFontLoader
setBackgroundColor()
Set the background color.
Parameters
- $color : SetaPDF_Core_DataStructure_Color|int|float|string|array|SetaPDF_Core_Type_Array
See
setBorderColor()
Set the border color.
Parameters
- $color : SetaPDF_Core_DataStructure_Color|int|float|string|array|SetaPDF_Core_Type_Array
See
setDefaultTextColor()
Parameters
- $color : int|float|string|array|SetaPDF_Core_Type_Array|SetaPDF_Core_DataStructure_Color
setStrict()
If true an error will be thrown if a tag or style isn't supported.
Parameters
- $strict : bool
setText()
Set the rich-text.
The text is cleaned-up and passed to a body node in a raw HTML template which is then loaded by
DOMDocument::loadHTML() method.
Entity loading, network access and error handling is disabled by using LIBXML_NONET | LIBXML_NOERROR
as
the $options
parameter. For PHP < 8 also libxml_disable_entity_loader()
is used to disable entity loading.
Parameters
- $text : string
An UTF-8 string
- $locale : string
INTL locale used for the break behaviour
setTextWidth()
Set the width of the rich-text. Padding is not included in this width.
Parameters
- $width : null|int|float