Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Font.php
blobdd4bd81042b57892a34e1024c0b1daf791a4cb52
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Class with Font related methods.
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 /**
11 * Class with Font related methods.
13 * @package PhpMyAdmin
15 class Font
17 /**
18 * Get list with characters and the corresponding width modifiers.
20 * @return array with characters and corresponding width modifier
21 * @access public
23 public static function getCharLists()
25 // list of characters and their width modifiers
26 $charLists = array();
28 //ijl
29 $charLists[] = array("chars" => array("i", "j", "l"), "modifier" => 0.23);
30 //f
31 $charLists[] = array("chars" => array("f"), "modifier" => 0.27);
32 //tI
33 $charLists[] = array("chars" => array("t", "I"), "modifier" => 0.28);
34 //r
35 $charLists[] = array("chars" => array("r"), "modifier" => 0.34);
36 //1
37 $charLists[] = array("chars" => array("1"), "modifier" => 0.49);
38 //cksvxyzJ
39 $charLists[] = array(
40 "chars" => array("c", "k", "s", "v", "x", "y", "z", "J"),
41 "modifier" => 0.5
43 //abdeghnopquL023456789
44 $charLists[] = array(
45 "chars" => array(
46 "a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L",
47 "0", "2", "3", "4", "5", "6", "7", "8", "9"
49 "modifier" => 0.56
51 //FTZ
52 $charLists[] = array("chars" => array("F", "T", "Z"), "modifier" => 0.61);
53 //ABEKPSVXY
54 $charLists[] = array(
55 "chars" => array("A", "B", "E", "K", "P", "S", "V", "X", "Y"),
56 "modifier" => 0.67
58 //wCDHNRU
59 $charLists[] = array(
60 "chars" => array("w", "C", "D", "H", "N", "R", "U"),
61 "modifier" => 0.73
63 //GOQ
64 $charLists[] = array("chars" => array("G", "O", "Q"), "modifier" => 0.78);
65 //mM
66 $charLists[] = array("chars" => array("m", "M"), "modifier" => 0.84);
67 //W
68 $charLists[] = array("chars" => array("W"), "modifier" => 0.95);
69 //" "
70 $charLists[] = array("chars" => array(" "), "modifier" => 0.28);
72 return $charLists;
75 /**
76 * Get width of string/text
78 * The text element width is calculated depending on font name
79 * and font size.
81 * @param string $text string of which the width will be calculated
82 * @param string $font name of the font like Arial,sans-serif etc
83 * @param integer $fontSize size of font
84 * @param array $charLists list of characters and their width modifiers
86 * @return integer width of the text
87 * @access public
89 public static function getStringWidth($text, $font, $fontSize, $charLists = null)
91 if (empty($charLists) || !is_array($charLists)
92 || !isset($charLists[0]["chars"]) || !is_array($charLists[0]["chars"])
93 || !isset($charLists[0]["modifier"])
94 ) {
95 $charLists = self::getCharLists();
99 * Start by counting the width, giving each character a modifying value
101 $count = 0;
103 foreach ($charLists as $charList) {
104 $count += ((mb_strlen($text)
105 - mb_strlen(str_replace($charList["chars"], "", $text))
106 ) * $charList["modifier"]);
109 $text = str_replace(" ", "", $text);//remove the " "'s
110 //all other chars
111 $count = $count
112 + (mb_strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3);
114 $modifier = 1;
115 $font = mb_strtolower($font);
116 switch ($font) {
118 * no modifier for arial and sans-serif
120 case 'arial':
121 case 'sans-serif':
122 break;
124 * .92 modifier for time, serif, brushscriptstd, and californian fb
126 case 'times':
127 case 'serif':
128 case 'brushscriptstd':
129 case 'californian fb':
130 $modifier = .92;
131 break;
133 * 1.23 modifier for broadway
135 case 'broadway':
136 $modifier = 1.23;
137 break;
139 $textWidth = $count * $fontSize;
140 return (int)ceil($textWidth * $modifier);