Skip to content

Commit 6df7562

Browse files
committed
Implement line spacing
1 parent 0a6af5c commit 6df7562

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

src/FontProcessor.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,8 @@ public function textToVipsImage(
5353
FontInterface $font,
5454
ColorInterface $color = new Color(0, 0, 0),
5555
): VipsImage {
56-
// TODO: implement line spacing
57-
58-
// @font size 24:
59-
// ---------------
60-
// 1 -> -15
61-
// 1.25 -> -10
62-
// 2 -> 7
63-
// 3 -> 18
64-
65-
// @font size 80:
66-
// ---------------
67-
// 1 -> -35
68-
// 1.25 -> -30
69-
// 2 -> 35
70-
// 3 -> 110
71-
72-
// @font size 100:
73-
// ---------------
74-
// 1 -> -45
75-
// 1.25 -> -35
76-
// 2 -> -10
77-
// 3 -> 55
78-
79-
// @font size 120:
80-
// ---------------
81-
// 1 -> -55
82-
// 1.25 -> -30
83-
// 2 -> 35
84-
// 3 -> 110
85-
86-
// leading 168
87-
88-
// 1 point (computer) 1.3333333333 pixel (X)
89-
// typicall like font size times 1.2.
90-
9156
return VipsImage::text(
92-
'<span foreground="' . $color->toHex('#') . '">' . htmlentities($text) . '</span>',
57+
'<span ' . $this->pangoAttributes($font, $color) . '>' . htmlentities($text) . '</span>',
9358
[
9459
'fontfile' => $font->filename(),
9560
'font' => TrueTypeFont::fromPath($font->filename())->familyName() . ' ' . $font->size(),
@@ -102,8 +67,28 @@ public function textToVipsImage(
10267
'right' => Align::HIGH,
10368
default => Align::LOW,
10469
},
105-
'spacing' => 0 // add value as pixel to each line
70+
'spacing' => 0
10671
]
10772
);
10873
}
74+
75+
/**
76+
* Return a pango markup attribute string based on the given font and color values
77+
*
78+
* @param FontInterface $font
79+
* @param ColorInterface $color
80+
* @return string
81+
*/
82+
private function pangoAttributes(FontInterface $font, ColorInterface $color): string
83+
{
84+
$pango_attributes = [
85+
'line_height' => (string) $font->lineHeight() / 1.62,
86+
'foreground' => $color->toHex('#'),
87+
];
88+
89+
// format pango attributes
90+
return join(' ', array_map(function ($value, $key): string {
91+
return $key . '="' . $value . '"';
92+
}, $pango_attributes, array_keys($pango_attributes)));
93+
}
10994
}

0 commit comments

Comments
 (0)