Novis\Image\Image::measureText
Novis\Image\Image::measureText(
string $text,
Font $font,
{ size: float, maxWidth?: uint, align?: Align } $options,
): SizeReturns the size in pixels that a text needs, without drawing it.
Image::measureText takes the text, a Font and the options size, maxWidth and align. withText takes the same options. The result has a width and a height in pixels. With the same text and options, the text that withText draws is exactly this size.
size is the font size in pixels. A line feed starts a new line. With maxWidth, lines also break at spaces, and each extra line adds to the height. A word wider than maxWidth is not broken, so the result can be wider than maxWidth. Text in any script is measured the way it is drawn.
Text with nothing to draw throws a LogicError. A font file with broken tables throws a ParseError.
Good to know: use it to make an image exactly as big as its text, or to choose a font size that fits a space.
The examples below make an image the size of a label, and count the lines of a wrapped text. The last one finds the biggest font size for a banner.
Parameters
$textstring$fontFont$options{ size: float, maxWidth?: uint, align?: Align }
Return value
Returns Size.
Examples
Size a label
<?nvs// Shows how to make an image exactly as big as its text, with a margin around it.use Novis\Image\Color;use Novis\Image\Image;
require 'fonts/box-font.nvs';
var $font = BoxFont::load();// This prints 24 and 10.var $size = Image::measureText('Shop', $font, { size: 10.0 });echo $size->width, "\n", $size->height, "\n";
// The image is 4 pixels bigger on each side. The text starts at 4, 4.tainted bytes $png = Image::create($size->width + 8, $size->height + 8, Color::hex('#ffffff')) ->withText('Shop', $font, { size: 10.0, color: Color::hex('#000000'), x: 4, y: 4 }) ->encode();var $info = Image::info($png);// This prints 32 and 18.echo $info->width, "\n", $info->height, "\n";24103218Count wrapped lines
<?nvs// Shows how to find how many lines a text needs in a box of a fixed width.use Novis\Image\Image;
require 'fonts/box-font.nvs';
var $font = BoxFont::load();// One line of this font at size 10.0 is 10 pixels high.uint $line = Image::measureText('Shop', $font, { size: 10.0 })->height;
// With `maxWidth`, lines break at spaces. This prints 1, 3 and 5.array<uint> $widths = [200, 60, 30];foreach ($widths as uint $width) { var $box = Image::measureText('New shoes in the shop', $font, { size: 10.0, maxWidth: $width }); echo $box->height / $line, "\n";}135Fit a title
<?nvs// Shows how to find the biggest font size at which a title fits in a banner.use Novis\Image\Image;
require 'fonts/box-font.nvs';
class Title{ // Starts at size 40.0 and makes the text smaller until it fits in `$width` pixels. public static function fit(string $title, uint $width): float { var $font = BoxFont::load(); float $size = 40.0; while ($size > 4.0 && Image::measureText($title, $font, { size: $size })->width > $width) { $size = $size - 2.0; } return $size; }}
// A banner is 120 pixels wide. This prints 40, 18 and 6.echo Title::fit('Shop', 120), "\n";echo Title::fit('Summer sale', 120), "\n";echo Title::fit('Free shipping on all orders', 120), "\n";40186