Font.class.php 4.3 KB

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