Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Font.php
blob8c5575c6c2b7b28f1581068fa060cbd6fa865925
1 <?php
2 /**
3 * Class with Font related methods.
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin;
10 use function ceil;
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;
17 /**
18 * Class with Font related methods.
20 class Font
22 /**
23 * Get list with characters and the corresponding width modifiers.
25 * @return (float|string[])[][] with characters and corresponding width modifier
27 public function getCharLists(): array
29 return [
30 //ijl
31 ['chars' => ['i', 'j', 'l'], 'modifier' => 0.23],
32 //f
33 ['chars' => ['f'], 'modifier' => 0.27],
34 //tI
35 ['chars' => ['t', 'I'], 'modifier' => 0.28],
36 //r
37 ['chars' => ['r'], 'modifier' => 0.34],
38 //1
39 ['chars' => ['1'], 'modifier' => 0.49],
40 //cksvxyzJ
41 ['chars' => ['c', 'k', 's', 'v', 'x', 'y', 'z', 'J'], 'modifier' => 0.5],
42 //abdeghnopquL023456789
44 'chars' => [
45 'a',
46 'b',
47 'd',
48 'e',
49 'g',
50 'h',
51 'n',
52 'o',
53 'p',
54 'q',
55 'u',
56 'L',
57 '0',
58 '2',
59 '3',
60 '4',
61 '5',
62 '6',
63 '7',
64 '8',
65 '9',
67 'modifier' => 0.56,
69 //FTZ
70 ['chars' => ['F', 'T', 'Z'], 'modifier' => 0.61],
71 //ABEKPSVXY
72 ['chars' => ['A', 'B', 'E', 'K', 'P', 'S', 'V', 'X', 'Y'], 'modifier' => 0.67],
73 //wCDHNRU
74 ['chars' => ['w', 'C', 'D', 'H', 'N', 'R', 'U'], 'modifier' => 0.73],
75 //GOQ
76 ['chars' => ['G', 'O', 'Q'], 'modifier' => 0.78],
77 //mM
78 ['chars' => ['m', 'M'], 'modifier' => 0.84],
79 //W
80 ['chars' => ['W'], 'modifier' => 0.95],
81 //" "
82 ['chars' => [' '], 'modifier' => 0.28],
86 /**
87 * Get width of string/text
89 * The text element width is calculated depending on font name
90 * and font size.
92 * @param string $text string of which the width will be calculated
93 * @param string $font name of the font like Arial,sans-serif etc
94 * @param int $fontSize size of font
95 * @param mixed[]|null $charLists list of characters and their width modifiers
97 * @return int width of the text
99 public function getStringWidth(
100 string $text,
101 string $font,
102 int $fontSize,
103 array|null $charLists = null,
104 ): int {
105 if (
106 ! isset($charLists[0]['chars'], $charLists[0]['modifier'])
107 || ! is_array($charLists[0]['chars'])
109 $charLists = $this->getCharLists();
112 // Start by counting the width, giving each character a modifying value
113 $count = 0;
115 foreach ($charLists as $charList) {
116 $count += (mb_strlen($text)
117 - mb_strlen(str_replace($charList['chars'], '', $text))
118 ) * $charList['modifier'];
121 $text = str_replace(' ', '', $text);//remove the " "'s
122 //all other chars
123 $count += mb_strlen((string) preg_replace('/[a-z0-9]/i', '', $text)) * 0.3;
125 $modifier = 1;
126 $font = mb_strtolower($font);
127 switch ($font) {
128 case 'arial':
129 case 'sans-serif':
130 // no modifier for arial and sans-serif
131 break;
132 case 'times':
133 case 'serif':
134 case 'brushscriptstd':
135 case 'californian fb':
136 // .92 modifier for time, serif, brushscriptstd, and californian fb
137 $modifier = .92;
138 break;
139 case 'broadway':
140 // 1.23 modifier for broadway
141 $modifier = 1.23;
142 break;
145 $textWidth = $count * $fontSize;
147 return (int) ceil($textWidth * $modifier);