Skip to content

Novis\Image\Image::Align

How the lines of a text are placed next to each other.

Image::Align is declared in the class Novis\Image\Image.

You pass an Image::Align to Image::withText or Image::measureText as align. It matters when a text has more than one line, for example when maxWidth wraps it. Each line is placed inside a box as wide as the longest line.

  • Left starts every line at the left side of the box. This is the default.
  • Center puts every line in the middle of the box.
  • Right ends every line at the right side of the box.

align does not change the size of the box, so measureText returns the same size for all three.

The example uses a very small font that is stored in the fonts folder next to it.

Examples

Where the short line starts

<?nvs
// Shows where each `Image::Align` puts a short line under a longer one.
use Novis\Image\Color;
use Novis\Image\Image;
require 'fonts/box-font.nvs';
class Line
{
// Draws "Shop Hi" in a box 24 pixels wide, so "Hi" goes on a second line.
// Returns the first column of that line that is not white.
public static function start(Image::Align $align): int
{
var $raw = Image::create(24, 20, Color::hex('#ffffff'))
->withText('Shop Hi', BoxFont::load(), {
size: 10.0,
color: Color::hex('#000000'),
x: 0,
y: 0,
maxWidth: 24,
align: $align,
})
->raw();
// Row 15 is inside the second line. Each pixel is 4 bytes, red first.
int $row = 15 * 24;
int $x = 0;
while ($x < 24) {
tainted bytes $red = Core\Bytes::slice($raw->pixels, ($row + $x) * 4, 1);
if (Core\Encoding::toHex($red) != 'ff') {
return $x;
}
$x = $x + 1;
}
return 24;
}
}
// "Hi" is 12 pixels wide. This prints 0, 6 and 12.
array<Image::Align> $aligns = [Image::Align::Left, Image::Align::Center, Image::Align::Right];
foreach ($aligns as Image::Align $align) {
echo Line::start($align), "\n";
}
Output
0
6
12