Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / classes / Font.php
blob9a98a87b2fceb6ec7baa70971aed226c94196776
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 array with characters and corresponding width modifier
27 * @access public
29 public function getCharLists(): array
31 // list of characters and their width modifiers
32 $charLists = [];
34 //ijl
35 $charLists[] = [
36 'chars' => [
37 'i',
38 'j',
39 'l',
41 'modifier' => 0.23,
43 //f
44 $charLists[] = [
45 'chars' => ['f'],
46 'modifier' => 0.27,
48 //tI
49 $charLists[] = [
50 'chars' => [
51 't',
52 'I',
54 'modifier' => 0.28,
56 //r
57 $charLists[] = [
58 'chars' => ['r'],
59 'modifier' => 0.34,
61 //1
62 $charLists[] = [
63 'chars' => ['1'],
64 'modifier' => 0.49,
66 //cksvxyzJ
67 $charLists[] = [
68 'chars' => [
69 'c',
70 'k',
71 's',
72 'v',
73 'x',
74 'y',
75 'z',
76 'J',
78 'modifier' => 0.5,
80 //abdeghnopquL023456789
81 $charLists[] = [
82 'chars' => [
83 'a',
84 'b',
85 'd',
86 'e',
87 'g',
88 'h',
89 'n',
90 'o',
91 'p',
92 'q',
93 'u',
94 'L',
95 '0',
96 '2',
97 '3',
98 '4',
99 '5',
100 '6',
101 '7',
102 '8',
103 '9',
105 'modifier' => 0.56,
107 //FTZ
108 $charLists[] = [
109 'chars' => [
110 'F',
111 'T',
112 'Z',
114 'modifier' => 0.61,
116 //ABEKPSVXY
117 $charLists[] = [
118 'chars' => [
119 'A',
120 'B',
121 'E',
122 'K',
123 'P',
124 'S',
125 'V',
126 'X',
127 'Y',
129 'modifier' => 0.67,
131 //wCDHNRU
132 $charLists[] = [
133 'chars' => [
134 'w',
135 'C',
136 'D',
137 'H',
138 'N',
139 'R',
140 'U',
142 'modifier' => 0.73,
144 //GOQ
145 $charLists[] = [
146 'chars' => [
147 'G',
148 'O',
149 'Q',
151 'modifier' => 0.78,
153 //mM
154 $charLists[] = [
155 'chars' => [
156 'm',
157 'M',
159 'modifier' => 0.84,
162 $charLists[] = [
163 'chars' => ['W'],
164 'modifier' => 0.95,
166 //" "
167 $charLists[] = [
168 'chars' => [' '],
169 'modifier' => 0.28,
172 return $charLists;
176 * Get width of string/text
178 * The text element width is calculated depending on font name
179 * and font size.
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
188 * @access public
190 public function getStringWidth(
191 string $text,
192 string $font,
193 int $fontSize,
194 ?array $charLists = null
195 ): int {
196 if (
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
206 $count = 0;
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
215 //all other chars
216 $count += mb_strlen((string) preg_replace('/[a-z0-9]/i', '', $text)) * 0.3;
218 $modifier = 1;
219 $font = mb_strtolower($font);
220 switch ($font) {
222 * no modifier for arial and sans-serif
224 case 'arial':
225 case 'sans-serif':
226 break;
228 * .92 modifier for time, serif, brushscriptstd, and californian fb
230 case 'times':
231 case 'serif':
232 case 'brushscriptstd':
233 case 'californian fb':
234 $modifier = .92;
235 break;
237 * 1.23 modifier for broadway
239 case 'broadway':
240 $modifier = 1.23;
241 break;
244 $textWidth = $count * $fontSize;
246 return (int) ceil($textWidth * $modifier);