Font.php 4.3 KB

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