implement unichr caching. speedup = 25% at the cost of more memory usage

This commit is contained in:
Dennis Eichhorn 2023-10-25 01:56:36 +00:00
parent b222c9703f
commit 258d933dc7
2 changed files with 13 additions and 6 deletions

View File

@ -6256,11 +6256,11 @@ class TCPDF {
$charWidth = $charsWidth[$i];
if (($c != 160)
&& (($c == 173)
|| \preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
|| \preg_match($this->re_spaces, TCPDF_FONTS::$cache_unichr[$c] ?? (TCPDF_FONTS::$cache_unichr[$c] = TCPDF_FONTS::unichr($c, $this->isunicode)))
|| (($c == 45)
&& ($i > 0) && ($i < ($length - 1))
&& @\preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i - 1)], $this->isunicode))
&& @\preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
&& @\preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::$cache_unichr[$chars[($i - 1)]] ?? (TCPDF_FONTS::$cache_unichr[$chars[($i - 1)]] = TCPDF_FONTS::unichr($chars[($i - 1)], $this->isunicode)))
&& @\preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::$cache_unichr[$chars[($i + 1)]] ?? (TCPDF_FONTS::$cache_unichr[$chars[($i + 1)]] = TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode)))
)
)
) {
@ -6413,7 +6413,7 @@ class TCPDF {
$nb = \count($chars);
// replacement for SHY character (minus symbol)
$shy_replacement = 45;
$shy_replacement_char = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode);
$shy_replacement_char = TCPDF_FONTS::$cache_unichr[$shy_replacement] ?? (TCPDF_FONTS::$cache_unichr[$shy_replacement] = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode));
// widht for SHY replacement
$shy_replacement_width = $this->GetCharWidth($shy_replacement);
// page width

View File

@ -51,6 +51,7 @@ class TCPDF_FONTS {
* @protected
*/
protected static $cache_uniord = [];
public static $cache_unichr = [];
/**
* Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
@ -1724,7 +1725,13 @@ class TCPDF_FONTS {
* @public static
*/
public static function UTF8ArrayToUniArray($ta, $isunicode=true) {
return \array_map(['TCPDF_FONTS', 'unichr'], $ta);
$temp = [];
foreach ($ta as $t) {
$temp[] = self::$cache_unichr[$t] ?? (self::$cache_unichr[$t] = self::unichr($t, $isunicode));
}
//return \array_map(['TCPDF_FONTS', 'unichr'], $ta);
return $temp;
}
/**
@ -1745,7 +1752,7 @@ class TCPDF_FONTS {
}
$string = '';
for ($i = $start; $i < $end; ++$i) {
$string .= self::unichr($strarr[$i], $unicode);
$string .= self::$cache_unichr[$strarr[$i]] ?? (self::$cache_unichr[$strarr[$i]] = self::unichr($strarr[$i], $unicode));
}
return $string;
}