TextElement: Fixed measurement, implemented font prop

This commit is contained in:
Chris 2017-10-31 02:35:38 +01:00
parent a410ce8b19
commit c8abdce24a

View File

@ -9,7 +9,7 @@ class TextElement extends Element
private static $fontCache = []; private static $fontCache = [];
private static function findFont($font) private static function findFont($font, $style='regular')
{ {
if (count(self::$fontCache)==0) { if (count(self::$fontCache)==0) {
self::updateFonts(); self::updateFonts();
@ -18,7 +18,14 @@ class TextElement extends Element
$font = strtolower($font); $font = strtolower($font);
if (array_key_exists($font, self::$fontCache)) { if (array_key_exists($font, self::$fontCache)) {
return self::$fontCache[$font]; $info = self::$fontCache[$font];
if (!$style) {
return $info;
}
if (array_key_exists($style, $info)) {
return $info[$style];
}
return false;
} }
return false; return false;
} }
@ -34,7 +41,14 @@ class TextElement extends Element
$seg = explode(":", $line); $seg = explode(":", $line);
$file = $seg[0]; $file = $seg[0];
$name = strtolower(trim(preg_replace("/[^A-Za-z0-9 ]/", '', $seg[1]))); $name = strtolower(trim(preg_replace("/[^A-Za-z0-9 ]/", '', $seg[1])));
self::$fontCache[$name] = $file; $style = strtolower(str_replace("style=","",$seg[2]));
$style = explode(",",$style);
$style = reset($style);
if (!array_key_exists($name, self::$fontCache)) {
self::$fontCache[$name] = [];
}
self::$fontCache[$name][$style] = $file;
} }
} }
@ -47,7 +61,9 @@ class TextElement extends Element
$value = $this->getProp("value"); $value = $this->getProp("value");
} }
$font = self::findFont("liberation sans"); $fontName = $this->getProp("font")?:"liberation sans";
$font = self::findFont($fontName);
if (!$font) { if (!$font) {
return; return;
} }
@ -56,7 +72,7 @@ class TextElement extends Element
$size = $this->getProp("font-size", 8); $size = $this->getProp("font-size", 8);
$dims = imagettfbbox($size, 0, $font, $value); $dims = imagettfbbox($size, 0, $font, "AaBbXxYyZz0129:;.");
$textAscent = abs($dims[7]); $textAscent = abs($dims[7]);
$textDescent = abs($dims[1]); $textDescent = abs($dims[1]);