2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Class with Font related methods.
8 namespace PMA\libraries
;
11 * Class with Font related methods.
18 * Get list with characters and the corresponding width modifiers.
20 * @return array with characters and corresponding width modifier
23 public static function getCharLists()
25 // list of characters and their width modifiers
29 $charLists[] = array("chars" => array("i", "j", "l"), "modifier" => 0.23);
31 $charLists[] = array("chars" => array("f"), "modifier" => 0.27);
33 $charLists[] = array("chars" => array("t", "I"), "modifier" => 0.28);
35 $charLists[] = array("chars" => array("r"), "modifier" => 0.34);
37 $charLists[] = array("chars" => array("1"), "modifier" => 0.49);
40 "chars" => array("c", "k", "s", "v", "x", "y", "z", "J"),
43 //abdeghnopquL023456789
46 "a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L",
47 "0", "2", "3", "4", "5", "6", "7", "8", "9"
52 $charLists[] = array("chars" => array("F", "T", "Z"), "modifier" => 0.61);
55 "chars" => array("A", "B", "E", "K", "P", "S", "V", "X", "Y"),
60 "chars" => array("w", "C", "D", "H", "N", "R", "U"),
64 $charLists[] = array("chars" => array("G", "O", "Q"), "modifier" => 0.78);
66 $charLists[] = array("chars" => array("m", "M"), "modifier" => 0.84);
68 $charLists[] = array("chars" => array("W"), "modifier" => 0.95);
70 $charLists[] = array("chars" => array(" "), "modifier" => 0.28);
76 * Get width of string/text
78 * The text element width is calculated depending on font name
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
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"])
95 $charLists = self
::getCharLists();
99 * Start by counting the width, giving each character a modifying value
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
112 +
(mb_strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3);
115 $font = mb_strtolower($font);
118 * no modifier for arial and sans-serif
124 * .92 modifier for time, serif, brushscriptstd, and californian fb
128 case 'brushscriptstd':
129 case 'californian fb':
133 * 1.23 modifier for broadway
139 $textWidth = $count * $fontSize;
140 return (int)ceil($textWidth * $modifier);