Font.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Class with Font related methods.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. /**
  11. * Class with Font related methods.
  12. *
  13. * @package PhpMyAdmin
  14. */
  15. class Font
  16. {
  17. /**
  18. * Get list with characters and the corresponding width modifiers.
  19. *
  20. * @return array with characters and corresponding width modifier
  21. * @access public
  22. */
  23. public function getCharLists(): array
  24. {
  25. // list of characters and their width modifiers
  26. $charLists = [];
  27. //ijl
  28. $charLists[] = [
  29. "chars" => [
  30. "i",
  31. "j",
  32. "l",
  33. ], "modifier" => 0.23,
  34. ];
  35. //f
  36. $charLists[] = [
  37. "chars" => ["f"],
  38. "modifier" => 0.27,
  39. ];
  40. //tI
  41. $charLists[] = [
  42. "chars" => [
  43. "t",
  44. "I",
  45. ], "modifier" => 0.28,
  46. ];
  47. //r
  48. $charLists[] = [
  49. "chars" => ["r"],
  50. "modifier" => 0.34,
  51. ];
  52. //1
  53. $charLists[] = [
  54. "chars" => ["1"],
  55. "modifier" => 0.49,
  56. ];
  57. //cksvxyzJ
  58. $charLists[] = [
  59. "chars" => [
  60. "c",
  61. "k",
  62. "s",
  63. "v",
  64. "x",
  65. "y",
  66. "z",
  67. "J",
  68. ],
  69. "modifier" => 0.5,
  70. ];
  71. //abdeghnopquL023456789
  72. $charLists[] = [
  73. "chars" => [
  74. "a",
  75. "b",
  76. "d",
  77. "e",
  78. "g",
  79. "h",
  80. "n",
  81. "o",
  82. "p",
  83. "q",
  84. "u",
  85. "L",
  86. "0",
  87. "2",
  88. "3",
  89. "4",
  90. "5",
  91. "6",
  92. "7",
  93. "8",
  94. "9",
  95. ],
  96. "modifier" => 0.56,
  97. ];
  98. //FTZ
  99. $charLists[] = [
  100. "chars" => [
  101. "F",
  102. "T",
  103. "Z",
  104. ], "modifier" => 0.61,
  105. ];
  106. //ABEKPSVXY
  107. $charLists[] = [
  108. "chars" => [
  109. "A",
  110. "B",
  111. "E",
  112. "K",
  113. "P",
  114. "S",
  115. "V",
  116. "X",
  117. "Y",
  118. ],
  119. "modifier" => 0.67,
  120. ];
  121. //wCDHNRU
  122. $charLists[] = [
  123. "chars" => [
  124. "w",
  125. "C",
  126. "D",
  127. "H",
  128. "N",
  129. "R",
  130. "U",
  131. ],
  132. "modifier" => 0.73,
  133. ];
  134. //GOQ
  135. $charLists[] = [
  136. "chars" => [
  137. "G",
  138. "O",
  139. "Q",
  140. ], "modifier" => 0.78,
  141. ];
  142. //mM
  143. $charLists[] = [
  144. "chars" => [
  145. "m",
  146. "M",
  147. ], "modifier" => 0.84,
  148. ];
  149. //W
  150. $charLists[] = [
  151. "chars" => ["W"],
  152. "modifier" => 0.95,
  153. ];
  154. //" "
  155. $charLists[] = [
  156. "chars" => [" "],
  157. "modifier" => 0.28,
  158. ];
  159. return $charLists;
  160. }
  161. /**
  162. * Get width of string/text
  163. *
  164. * The text element width is calculated depending on font name
  165. * and font size.
  166. *
  167. * @param string $text string of which the width will be calculated
  168. * @param string $font name of the font like Arial,sans-serif etc
  169. * @param integer $fontSize size of font
  170. * @param array|null $charLists list of characters and their width modifiers
  171. *
  172. * @return integer width of the text
  173. * @access public
  174. */
  175. public function getStringWidth(
  176. string $text,
  177. string $font,
  178. int $fontSize,
  179. ?array $charLists = null
  180. ): int {
  181. if (empty($charLists)
  182. || ! isset($charLists[0]["chars"]) || ! is_array($charLists[0]["chars"])
  183. || ! isset($charLists[0]["modifier"])
  184. ) {
  185. $charLists = $this->getCharLists();
  186. }
  187. /*
  188. * Start by counting the width, giving each character a modifying value
  189. */
  190. $count = 0;
  191. foreach ($charLists as $charList) {
  192. $count += ((mb_strlen($text)
  193. - mb_strlen(str_replace($charList["chars"], "", $text))
  194. ) * $charList["modifier"]);
  195. }
  196. $text = str_replace(" ", "", $text);//remove the " "'s
  197. //all other chars
  198. $count += (mb_strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3);
  199. $modifier = 1;
  200. $font = mb_strtolower($font);
  201. switch ($font) {
  202. /*
  203. * no modifier for arial and sans-serif
  204. */
  205. case 'arial':
  206. case 'sans-serif':
  207. break;
  208. /*
  209. * .92 modifier for time, serif, brushscriptstd, and californian fb
  210. */
  211. case 'times':
  212. case 'serif':
  213. case 'brushscriptstd':
  214. case 'californian fb':
  215. $modifier = .92;
  216. break;
  217. /*
  218. * 1.23 modifier for broadway
  219. */
  220. case 'broadway':
  221. $modifier = 1.23;
  222. break;
  223. }
  224. $textWidth = $count * $fontSize;
  225. return (int) ceil($textWidth * $modifier);
  226. }
  227. }