@@ -53,43 +53,8 @@ public function textToVipsImage(
53
53
FontInterface $ font ,
54
54
ColorInterface $ color = new Color (0 , 0 , 0 ),
55
55
): 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
-
91
56
return VipsImage::text (
92
- '<span foreground=" ' . $ color -> toHex ( ' # ' ) . '" > ' . htmlentities ($ text ) . '</span> ' ,
57
+ '<span ' . $ this -> pangoAttributes ( $ font , $ color ) . '> ' . htmlentities ($ text ) . '</span> ' ,
93
58
[
94
59
'fontfile ' => $ font ->filename (),
95
60
'font ' => TrueTypeFont::fromPath ($ font ->filename ())->familyName () . ' ' . $ font ->size (),
@@ -102,8 +67,28 @@ public function textToVipsImage(
102
67
'right ' => Align::HIGH ,
103
68
default => Align::LOW ,
104
69
},
105
- 'spacing ' => 0 // add value as pixel to each line
70
+ 'spacing ' => 0
106
71
]
107
72
);
108
73
}
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
+ }
109
94
}
0 commit comments