2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Class with Font related methods.
8 if (! defined('PHPMYADMIN')) {
13 * Class with Font related methods.
20 * Get list with characters and the corresponding width modifiers.
22 * @param string $font name of the font like Arial,sans-serif etc
24 * @return array with characters and corresponding width modifier
27 public static function getCharLists($font)
29 // list of characters and their width modifiers
33 $charLists[] = array("chars" => array("i", "j", "l"), "modifier" => 0.23);
35 $charLists[] = array("chars" => array("f"), "modifier" => 0.27);
37 $charLists[] = array("chars" => array("t", "I"), "modifier" => 0.28);
39 $charLists[] = array("chars" => array("r"), "modifier" => 0.34);
41 $charLists[] = array("chars" => array("1"), "modifier" => 0.49);
44 "chars" => array("c", "k", "s", "v", "x", "y", "z", "J"),
47 //abdeghnopquL023456789
50 "a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L",
51 "0", "2", "3", "4", "5", "6", "7", "8", "9"
56 $charLists[] = array("chars" => array("F", "T", "Z"), "modifier" => 0.61);
59 "chars" => array("A", "B", "E", "K", "P", "S", "V", "X", "Y"),
64 "chars" => array("w", "C", "D", "H", "N", "R", "U"),
68 $charLists[] = array("chars" => array("G", "O", "Q"), "modifier" => 0.78);
70 $charLists[] = array("chars" => array("m", "M"), "modifier" => 0.84);
72 $charLists[] = array("chars" => array("W"), "modifier" => 0.95);
74 $charLists[] = array("chars" => array(" "), "modifier" => 0.28);
80 * Get width of string/text
82 * The text element width is calculated depending on font name
85 * @param string $text string of which the width will be calculated
86 * @param string $font name of the font like Arial,sans-serif etc
87 * @param integer $fontSize size of font
88 * @param array $charLists list of characters and their width modifiers
90 * @return integer width of the text
93 public static function getStringWidth($text, $font, $fontSize, $charLists = null)
95 if (empty($charLists) ||
!is_array($charLists)
96 ||
!isset($charLists[0]["chars"]) ||
!is_array($charLists[0]["chars"])
97 ||
!isset($charLists[0]["modifier"])
99 $charLists = self
::getCharLists($font);
103 * Start by counting the width, giving each character a modifying value
107 foreach ($charLists as $charList) {
108 $count +
= ((strlen($text)
109 - strlen(str_replace($charList["chars"], "", $text))
110 ) * $charList["modifier"]);
113 $text = str_replace(" ", "", $text);//remove the " "'s
115 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3);
118 $font = strtolower($font);
121 * no modifier for arial and sans-serif
127 * .92 modifer for time, serif, brushscriptstd, and californian fb
131 case 'brushscriptstd':
132 case 'californian fb':
136 * 1.23 modifier for broadway
142 $textWidth = $count*$fontSize;
143 return ceil($textWidth*$modifier);