3 * Class with Font related methods.
6 declare(strict_types
=1);
11 use function is_array
;
12 use function mb_strlen
;
13 use function mb_strtolower
;
14 use function preg_replace
;
15 use function str_replace
;
18 * Class with Font related methods.
23 * Get list with characters and the corresponding width modifiers.
25 * @return array with characters and corresponding width modifier
29 public function getCharLists(): array
31 // list of characters and their width modifiers
80 //abdeghnopquL023456789
176 * Get width of string/text
178 * The text element width is calculated depending on font name
181 * @param string $text string of which the width will be calculated
182 * @param string $font name of the font like Arial,sans-serif etc
183 * @param int $fontSize size of font
184 * @param array|null $charLists list of characters and their width modifiers
186 * @return int width of the text
190 public function getStringWidth(
194 ?
array $charLists = null
197 ! isset($charLists[0]['chars'], $charLists[0]['modifier']) ||
empty($charLists)
198 ||
! is_array($charLists[0]['chars'])
200 $charLists = $this->getCharLists();
204 * Start by counting the width, giving each character a modifying value
208 foreach ($charLists as $charList) {
209 $count +
= (mb_strlen($text)
210 - mb_strlen(str_replace($charList['chars'], '', $text))
211 ) * $charList['modifier'];
214 $text = str_replace(' ', '', $text);//remove the " "'s
216 $count +
= mb_strlen((string) preg_replace('/[a-z0-9]/i', '', $text)) * 0.3;
219 $font = mb_strtolower($font);
222 * no modifier for arial and sans-serif
228 * .92 modifier for time, serif, brushscriptstd, and californian fb
232 case 'brushscriptstd':
233 case 'californian fb':
237 * 1.23 modifier for broadway
244 $textWidth = $count * $fontSize;
246 return (int) ceil($textWidth * $modifier);