diff --git a/doc/credits.rst b/doc/credits.rst index 85d74cf047..61c77587ea 100644 --- a/doc/credits.rst +++ b/doc/credits.rst @@ -354,6 +354,23 @@ Credits, in chronological order * Refactoring +* Mohamed Ashraf (Google Summer of Code 2013) + + * AJAX error reporting + +* Adam Kang (Google Summer of Code 2013) + + * Automated testing + +* Ayush Chaudhary (Google Summer of Code 2013) + + * Automated testing + +* Kasun Chathuranga (Google Summer of Code 2013) + + * Interface improvements + + And also to the following people who have contributed minor changes, enhancements, bugfixes or support for a new language since version 2.1.0: diff --git a/libraries/Font.class.php b/libraries/Font.class.php new file mode 100644 index 0000000000..b4c2024e09 --- /dev/null +++ b/libraries/Font.class.php @@ -0,0 +1,146 @@ + array("i", "j", "l"), "modifier" => 0.23); + //f + $charLists[] = array("chars" => array("f"), "modifier" => 0.27); + //tI + $charLists[] = array("chars" => array("t", "I"), "modifier" => 0.28); + //r + $charLists[] = array("chars" => array("r"), "modifier" => 0.34); + //1 + $charLists[] = array("chars" => array("1"), "modifier" => 0.49); + //cksvxyzJ + $charLists[] = array( + "chars" => array("c", "k", "s", "v", "x", "y", "z", "J"), + "modifier" => 0.5 + ); + //abdeghnopquL023456789 + $charLists[] = array( + "chars" => array( + "a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L", + "0", "2", "3", "4", "5", "6", "7", "8", "9" + ), + "modifier" => 0.56 + ); + //FTZ + $charLists[] = array("chars" => array("F", "T", "Z"), "modifier" => 0.61); + //ABEKPSVXY + $charLists[] = array( + "chars" => array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), + "modifier" => 0.67 + ); + //wCDHNRU + $charLists[] = array( + "chars" => array("w", "C", "D", "H", "N", "R", "U"), + "modifier" => 0.73 + ); + //GOQ + $charLists[] = array("chars" => array("G", "O", "Q"), "modifier" => 0.78); + //mM + $charLists[] = array("chars" => array("m", "M"), "modifier" => 0.84); + //W + $charLists[] = array("chars" => array("W"), "modifier" => 0.95); + //" " + $charLists[] = array("chars" => array(" "), "modifier" => 0.28); + + return $charLists; + } + + /** + * Get width of string/text + * + * The text element width is calculated depending on font name + * and font size. + * + * @param string $text string of which the width will be calculated + * @param string $font name of the font like Arial,sans-serif etc + * @param integer $fontSize size of font + * @param array $charLists list of characters and their width modifiers + * + * @return integer width of the text + * @access public + */ + public static function getStringWidth($text, $font, $fontSize, $charLists = null) + { + if (empty($charLists) || !is_array($charLists) + || !isset($charLists[0]["chars"]) || !is_array($charLists[0]["chars"]) + || !isset($charLists[0]["modifier"]) + ) { + $charLists = self::getCharLists($font); + } + + /* + * Start by counting the width, giving each character a modifying value + */ + $count = 0; + + foreach ($charLists as $charList) { + $count += ((strlen($text) + - strlen(str_replace($charList["chars"], "", $text)) + ) * $charList["modifier"]); + } + + $text = str_replace(" ", "", $text);//remove the " "'s + //all other chars + $count = $count + (strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); + + $modifier = 1; + $font = strtolower($font); + switch ($font) { + /* + * no modifier for arial and sans-serif + */ + case 'arial': + case 'sans-serif': + break; + /* + * .92 modifer for time, serif, brushscriptstd, and californian fb + */ + case 'times': + case 'serif': + case 'brushscriptstd': + case 'californian fb': + $modifier = .92; + break; + /* + * 1.23 modifier for broadway + */ + case 'broadway': + $modifier = 1.23; + break; + } + $textWidth = $count*$fontSize; + return ceil($textWidth*$modifier); + } +} +?> diff --git a/libraries/schema/Eps_Relation_Schema.class.php b/libraries/schema/Eps_Relation_Schema.class.php index 1a3a3536c9..3be3d9df94 100644 --- a/libraries/schema/Eps_Relation_Schema.class.php +++ b/libraries/schema/Eps_Relation_Schema.class.php @@ -9,6 +9,7 @@ if (! defined('PHPMYADMIN')) { } require_once 'Export_Relation_Schema.class.php'; +require_once 'libraries/Font.class.php'; /** * This Class is EPS Library and @@ -261,76 +262,6 @@ class PMA_EPS $this->show($text); } - /** - * get width of string/text - * - * EPS text width is calcualted depending on font name - * and font size. It is very important to know the width of text - * because rectangle is drawn around it. - * - * This is a bit hardcore method. I didn't found any other better than this. - * if someone found better than this. would love to hear that method - * - * @param string $text string that width will be calculated - * @param integer $font name of the font like Arial,sans-serif etc - * @param integer $fontSize size of font - * - * @return integer width of the text - * - * @access public - */ - function getStringWidth($text,$font,$fontSize) - { - /* - * Start by counting the width, giving each character a modifying value - */ - $count = 0; - $count = $count + ((strlen($text) - strlen(str_replace(array("i", "j", "l"), "", $text))) * 0.23);//ijl - $count = $count + ((strlen($text) - strlen(str_replace(array("f"), "", $text))) * 0.27);//f - $count = $count + ((strlen($text) - strlen(str_replace(array("t", "I"), "", $text))) * 0.28);//tI - $count = $count + ((strlen($text) - strlen(str_replace(array("r"), "", $text))) * 0.34);//r - $count = $count + ((strlen($text) - strlen(str_replace(array("1"), "", $text))) * 0.49);//1 - $count = $count + ((strlen($text) - strlen(str_replace(array("c", "k", "s", "v", "x", "y", "z", "J"), "", $text))) * 0.5);//cksvxyzJ - $count = $count + ((strlen($text) - strlen(str_replace(array("a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L", "0", "2", "3", "4", "5", "6", "7", "8", "9"), "", $text))) * 0.56);//abdeghnopquL023456789 - $count = $count + ((strlen($text) - strlen(str_replace(array("F", "T", "Z"), "", $text))) * 0.61);//FTZ - $count = $count + ((strlen($text) - strlen(str_replace(array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), "", $text))) * 0.67);//ABEKPSVXY - $count = $count + ((strlen($text) - strlen(str_replace(array("w", "C", "D", "H", "N", "R", "U"), "", $text))) * 0.73);//wCDHNRU - $count = $count + ((strlen($text) - strlen(str_replace(array("G", "O", "Q"), "", $text))) * 0.78);//GOQ - $count = $count + ((strlen($text) - strlen(str_replace(array("m", "M"), "", $text))) * 0.84);//mM - $count = $count + ((strlen($text) - strlen(str_replace("W", "", $text))) * .95);//W - $count = $count + ((strlen($text) - strlen(str_replace(" ", "", $text))) * .28);//" " - $text = str_replace(" ", "", $text);//remove the " "'s - $count = $count + (strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); //all other chrs - - $modifier = 1; - $font = strtolower($font); - switch ($font) { - /* - * no modifier for arial and sans-serif - */ - case 'arial': - case 'sans-serif': - break; - /* - * .92 modifer for time, serif, brushscriptstd, and californian fb - */ - case 'times': - case 'serif': - case 'brushscriptstd': - case 'californian fb': - $modifier = .92; - break; - /* - * 1.23 modifier for broadway - */ - case 'broadway': - $modifier = 1.23; - break; - } - $textWidth = $count*$fontSize; - return ceil($textWidth*$modifier); - } - /** * Ends EPS Document * @@ -529,20 +460,19 @@ class Table_Stats_Eps */ private function _setWidthTable($font,$fontSize) { - global $eps; - foreach ($this->fields as $field) { $this->width = max( $this->width, - $eps->getStringWidth($field, $font, $fontSize) + PMA_Font::getStringWidth($field, $font, $fontSize) ); } - $this->width += $eps->getStringWidth(' ', $font, $fontSize); + $this->width += PMA_Font::getStringWidth(' ', $font, $fontSize); /* * it is unknown what value must be added, because * table title is affected by the tabe width value */ - while ($this->width < $eps->getStringWidth($this->_getTitle(), $font, $fontSize)) { + while ($this->width + < PMA_Font::getStringWidth($this->_getTitle(), $font, $fontSize)) { $this->width += 7; } } diff --git a/libraries/schema/Svg_Relation_Schema.class.php b/libraries/schema/Svg_Relation_Schema.class.php index 5304ab239e..a68a722ba9 100644 --- a/libraries/schema/Svg_Relation_Schema.class.php +++ b/libraries/schema/Svg_Relation_Schema.class.php @@ -10,6 +10,7 @@ if (! defined('PHPMYADMIN')) { } require_once 'Export_Relation_Schema.class.php'; +require_once 'libraries/Font.class.php'; /** * This Class inherits the XMLwriter class and @@ -260,115 +261,6 @@ class PMA_SVG extends XMLWriter $this->writeAttribute('style', $styles); $this->endElement(); } - - /** - * get width of string/text - * - * Svg text element width is calculated depending on font name - * and font size. It is very important to know the width of text - * because rectangle is drawn around it. - * - * This is a bit hardcore method. I didn't found any other than this. - * - * @param string $text string that width will be calculated - * @param integer $font name of the font like Arial,sans-serif etc - * @param integer $fontSize size of font - * - * @return integer width of the text - * @access public - */ - function getStringWidth($text, $font, $fontSize) - { - // list of characters and their width modifiers - $charLists = array(); - - //ijl - $charLists[] = array("chars" => array("i", "j", "l"), "modifier" => 0.23); - //f - $charLists[] = array("chars" => array("f"), "modifier" => 0.27); - //tI - $charLists[] = array("chars" => array("t", "I"), "modifier" => 0.28); - //r - $charLists[] = array("chars" => array("r"), "modifier" => 0.34); - //1 - $charLists[] = array("chars" => array("1"), "modifier" => 0.49); - //cksvxyzJ - $charLists[] = array( - "chars" => array("c", "k", "s", "v", "x", "y", "z", "J"), - "modifier" => 0.5 - ); - //abdeghnopquL023456789 - $charLists[] = array( - "chars" => array( - "a", "b", "d", "e", "g", "h", "n", "o", "p", "q", "u", "L", - "0", "2", "3", "4", "5", "6", "7", "8", "9" - ), - "modifier" => 0.56 - ); - //FTZ - $charLists[] = array("chars" => array("F", "T", "Z"), "modifier" => 0.61); - //ABEKPSVXY - $charLists[] = array( - "chars" => array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), - "modifier" => 0.67 - ); - //wCDHNRU - $charLists[] = array( - "chars" => array("w", "C", "D", "H", "N", "R", "U"), - "modifier" => 0.73 - ); - //GOQ - $charLists[] = array("chars" => array("G", "O", "Q"), "modifier" => 0.78); - //mM - $charLists[] = array("chars" => array("m", "M"), "modifier" => 0.84); - //W - $charLists[] = array("chars" => array("W"), "modifier" => 0.95); - //" " - $charLists[] = array("chars" => array(" "), "modifier" => 0.28); - - /* - * Start by counting the width, giving each character a modifying value - */ - $count = 0; - - foreach ($charLists as $charList) { - $count += ((strlen($text) - - strlen(str_replace($charList["chars"], "", $text)) - ) * $charList["modifier"]); - } - - $text = str_replace(" ", "", $text);//remove the " "'s - //all other chars - $count = $count + (strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); - - $modifier = 1; - $font = strtolower($font); - switch ($font) { - /* - * no modifier for arial and sans-serif - */ - case 'arial': - case 'sans-serif': - break; - /* - * .92 modifer for time, serif, brushscriptstd, and californian fb - */ - case 'times': - case 'serif': - case 'brushscriptstd': - case 'californian fb': - $modifier = .92; - break; - /* - * 1.23 modifier for broadway - */ - case 'broadway': - $modifier = 1.23; - break; - } - $textWidth = $count*$fontSize; - return ceil($textWidth*$modifier); - } } /** @@ -544,22 +436,20 @@ class Table_Stats_Svg */ private function _setWidthTable($font,$fontSize) { - global $svg; - foreach ($this->fields as $field) { $this->width = max( $this->width, - $svg->getStringWidth($field, $font, $fontSize) + PMA_Font::getStringWidth($field, $font, $fontSize) ); } - $this->width += $svg->getStringWidth(' ', $font, $fontSize); + $this->width += PMA_Font::getStringWidth(' ', $font, $fontSize); /* * it is unknown what value must be added, because * table title is affected by the tabe width value */ while ($this->width - < $svg->getStringWidth($this->_getTitle(), $font, $fontSize) + < PMA_Font::getStringWidth($this->_getTitle(), $font, $fontSize) ) { $this->width += 7; } diff --git a/test/classes/PMA_Font_test.php b/test/classes/PMA_Font_test.php new file mode 100644 index 0000000000..4478cf4397 --- /dev/null +++ b/test/classes/PMA_Font_test.php @@ -0,0 +1,266 @@ +assertEquals( + 0, + PMA_Font::getStringWidth("", "arial", "10") + ); + + // empty string + $this->assertEquals( + 3, + PMA_Font::getStringWidth(" ", "arial", "10") + ); + + // string "a" + $this->assertEquals( + 6, + PMA_Font::getStringWidth("a", "arial", "10") + ); + + // string "aa" + $this->assertEquals( + 12, + PMA_Font::getStringWidth("aa", "arial", "10") + ); + + // string "i" + $this->assertEquals( + 3, + PMA_Font::getStringWidth("i", "arial", "10") + ); + + // string "f" + $this->assertEquals( + 3, + PMA_Font::getStringWidth("f", "arial", "10") + ); + + // string "t" + $this->assertEquals( + 3, + PMA_Font::getStringWidth("t", "arial", "10") + ); + + // string "if" + $this->assertEquals( + 5, + PMA_Font::getStringWidth("if", "arial", "10") + ); + + // string "it" + $this->assertEquals( + 6, + PMA_Font::getStringWidth("it", "arial", "10") + ); + + // string "r" + $this->assertEquals( + 4, + PMA_Font::getStringWidth("r", "arial", "10") + ); + + // string "1" + $this->assertEquals( + 5, + PMA_Font::getStringWidth("1", "arial", "10") + ); + + // string "c" + $this->assertEquals( + 5, + PMA_Font::getStringWidth("c", "arial", "10") + ); + + // string "F" + $this->assertEquals( + 7, + PMA_Font::getStringWidth("F", "arial", "10") + ); + + // string "A" + $this->assertEquals( + 7, + PMA_Font::getStringWidth("A", "arial", "10") + ); + + // string "w" + $this->assertEquals( + 8, + PMA_Font::getStringWidth("w", "arial", "10") + ); + + // string "G" + $this->assertEquals( + 8, + PMA_Font::getStringWidth("G", "arial", "10") + ); + + // string "m" + $this->assertEquals( + 9, + PMA_Font::getStringWidth("m", "arial", "10") + ); + + // string "W" + $this->assertEquals( + 10, + PMA_Font::getStringWidth("W", "arial", "10") + ); + + // string "$" + $this->assertEquals( + 3, + PMA_Font::getStringWidth("$", "arial", "10") + ); + } + + /** + * Test getStringWidth with different fonts. + * + * @return void + */ + function testGetStringWidthFont() + { + // string "phpMyAdmin", with Arial 10 + $this->assertEquals( + 59, + PMA_Font::getStringWidth("phpMyAdmin", "arial", "10") + ); + + // string "phpMyAdmin", with No font + $this->assertEquals( + 59, + PMA_Font::getStringWidth("phpMyAdmin", "", "10") + ); + + // string "phpMyAdmin", with Times 10 + $this->assertEquals( + 55, + PMA_Font::getStringWidth("phpMyAdmin", "times", "10") + ); + + // string "phpMyAdmin", with Broadway 10 + $this->assertEquals( + 73, + PMA_Font::getStringWidth("phpMyAdmin", "broadway", "10") + ); + + } + + /** + * Test getStringWidth with different font sizes. + * + * @return void + */ + function testGetStringWidthSize() + { + // string "phpMyAdmin", with font size 0 + $this->assertEquals( + 0, + PMA_Font::getStringWidth("phpMyAdmin", "arial", "0") + ); + + // string "phpMyAdmin", with Arial 10 + $this->assertEquals( + 59, + PMA_Font::getStringWidth("phpMyAdmin", "arial", "10") + ); + + // string "phpMyAdmin", with Arial 11 + $this->assertEquals( + 65, + PMA_Font::getStringWidth("phpMyAdmin", "arial", "11") + ); + + // string "phpMyAdmin", with Arial 20 + $this->assertEquals( + 118, + PMA_Font::getStringWidth("phpMyAdmin", "arial", "20") + ); + } + + /** + * Test getStringWidth with a custom charList. + * + * @return void + */ + function testGetStringWidthCharLists() + { + // string "a", with invalid charlist (= string) + $this->assertEquals( + 6, + PMA_Font::getStringWidth("a", "arial", "10", "list") + ); + + // string "a", with invalid charlist (= array without proper structure) + $this->assertEquals( + 6, + PMA_Font::getStringWidth("a", "arial", "10", array("list")) + ); + + // string "a", with invalid charlist (= array without proper structure : + // modifier is missing + $this->assertEquals( + 6, + PMA_Font::getStringWidth( + "a", "arial", "10", + array(array("chars" => "a")) + ) + ); + + // string "a", with invalid charlist (= array without proper structure : + // chars is missing + $this->assertEquals( + 6, + PMA_Font::getStringWidth( + "a", "arial", "10", + array(array("modifier" => 0.61)) + ) + ); + + // string "a", with invalid charlist (= array without proper structure : + // chars is not an array + $this->assertEquals( + 6, + PMA_Font::getStringWidth( + "a", "arial", "10", + array(array("chars" => "a", "modifier" => 0.61)) + ) + ); + + // string "a", with valid charlist + $this->assertEquals( + 7, + PMA_Font::getStringWidth( + "a", "arial", "10", + array(array("chars" => array("a"), "modifier" => 0.61)) + ) + ); + } +} +?> diff --git a/test/classes/schema/Svg_test.php b/test/classes/schema/Svg_test.php deleted file mode 100644 index 5f4014130e..0000000000 --- a/test/classes/schema/Svg_test.php +++ /dev/null @@ -1,235 +0,0 @@ -object = new PMA_SVG(); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - * - * @access protected - * @return void - */ - protected function tearDown() - { - unset($this->object); - } - - /** - * Test getStringWidth with different characters. - * - * @return void - */ - function testGetStringWidth() - { - // empty string - $this->assertEquals( - 0, - $this->object->getStringWidth("", "arial", "10") - ); - - // empty string - $this->assertEquals( - 3, - $this->object->getStringWidth(" ", "arial", "10") - ); - - // string "a" - $this->assertEquals( - 6, - $this->object->getStringWidth("a", "arial", "10") - ); - - // string "aa" - $this->assertEquals( - 12, - $this->object->getStringWidth("aa", "arial", "10") - ); - - // string "i" - $this->assertEquals( - 3, - $this->object->getStringWidth("i", "arial", "10") - ); - - // string "f" - $this->assertEquals( - 3, - $this->object->getStringWidth("f", "arial", "10") - ); - - // string "t" - $this->assertEquals( - 3, - $this->object->getStringWidth("t", "arial", "10") - ); - - // string "if" - $this->assertEquals( - 5, - $this->object->getStringWidth("if", "arial", "10") - ); - - // string "it" - $this->assertEquals( - 6, - $this->object->getStringWidth("it", "arial", "10") - ); - - // string "r" - $this->assertEquals( - 4, - $this->object->getStringWidth("r", "arial", "10") - ); - - // string "1" - $this->assertEquals( - 5, - $this->object->getStringWidth("1", "arial", "10") - ); - - // string "c" - $this->assertEquals( - 5, - $this->object->getStringWidth("c", "arial", "10") - ); - - // string "F" - $this->assertEquals( - 7, - $this->object->getStringWidth("F", "arial", "10") - ); - - // string "A" - $this->assertEquals( - 7, - $this->object->getStringWidth("A", "arial", "10") - ); - - // string "w" - $this->assertEquals( - 8, - $this->object->getStringWidth("w", "arial", "10") - ); - - // string "G" - $this->assertEquals( - 8, - $this->object->getStringWidth("G", "arial", "10") - ); - - // string "m" - $this->assertEquals( - 9, - $this->object->getStringWidth("m", "arial", "10") - ); - - // string "W" - $this->assertEquals( - 10, - $this->object->getStringWidth("W", "arial", "10") - ); - - // string "$" - $this->assertEquals( - 3, - $this->object->getStringWidth("$", "arial", "10") - ); - } - - /** - * Test getStringWidth with different fonts. - * - * @return void - */ - function testGetStringWidthFont() - { - // string "phpMyAdmin", with Arial 10 - $this->assertEquals( - 59, - $this->object->getStringWidth("phpMyAdmin", "arial", "10") - ); - - // string "phpMyAdmin", with No font - $this->assertEquals( - 59, - $this->object->getStringWidth("phpMyAdmin", "", "10") - ); - - // string "phpMyAdmin", with Times 10 - $this->assertEquals( - 55, - $this->object->getStringWidth("phpMyAdmin", "times", "10") - ); - - // string "phpMyAdmin", with Broadway 10 - $this->assertEquals( - 73, - $this->object->getStringWidth("phpMyAdmin", "broadway", "10") - ); - - } - - /** - * Test getStringWidth with different fonts. - * - * @return void - */ - function testGetStringWidthSize() - { - // string "phpMyAdmin", with font size 0 - $this->assertEquals( - 0, - $this->object->getStringWidth("phpMyAdmin", "arial", "0") - ); - - // string "phpMyAdmin", with Arial 10 - $this->assertEquals( - 59, - $this->object->getStringWidth("phpMyAdmin", "arial", "10") - ); - - // string "phpMyAdmin", with Arial 11 - $this->assertEquals( - 65, - $this->object->getStringWidth("phpMyAdmin", "arial", "11") - ); - - // string "phpMyAdmin", with Arial 20 - $this->assertEquals( - 118, - $this->object->getStringWidth("phpMyAdmin", "arial", "20") - ); - } -}