Skip to content

Commit

Permalink
Merge pull request #129 from liuggio/shields_spec
Browse files Browse the repository at this point in the history
little patch to be more compatible with shields specification
  • Loading branch information
liuggio committed Jun 24, 2014
2 parents fd6abca + c3810f4 commit 6fb2fc7
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before_script:
- COMPOSER_ROOT_VERSION=dev-master composer --prefer-source --dev --no-interaction install

script:
- bin/phpunit
- bin/phpunit -v

notifications:
email:
Expand Down
4 changes: 2 additions & 2 deletions src/PUGX/Badge/Image/Factory/SvgImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Badge\Image\Factory;

use PUGX\Badge\Image\Generator\SvgShieldGeneratorInterface;
Expand Down Expand Up @@ -138,4 +138,4 @@ private function generateShield($vendor, $value, $color)
{
return $this->shieldGenerator->generateShield($vendor, $value, $color);
}
}
}
16 changes: 8 additions & 8 deletions src/PUGX/Badge/Image/Generator/SvgShieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Badge\Image\Generator;

use PUGX\Badge\Image\Template\TemplateEngineInterface;
Expand All @@ -20,10 +20,11 @@
*/
class SvgShieldGenerator implements SvgShieldGeneratorInterface
{
const STRING_SIZE = 11;
const STRING_FONT = '/../Font/DejaVuSans.ttf';
const VENDOR_COLOR = '#555';
const SHIELD_PADDING = 17;
const STRING_SIZE = 11;
const STRING_FONT = '/../Font/DejaVuSans.ttf';
const VENDOR_COLOR = '#555';
const SHIELD_PADDING_EXTERNAL = 6;
const SHIELD_PADDING_INTERNAL = 4;

private static $colorScheme = array(
"brightgreen" => "#4c1",
Expand Down Expand Up @@ -93,9 +94,8 @@ public function generateShield($vendor, $value, $color)
private function stringWidth($text, $font = self::STRING_FONT, $size = self::STRING_SIZE)
{
$size = $this->convertToPt($size);
$box = imageftbbox($size, 0, __DIR__ . $font, $text);

return round($box[2] - $box[0] + self::SHIELD_PADDING, 1);
$box = imagettfbbox($size, 0, __DIR__ . $font, $text);
return round(abs($box[2] - $box[0]) + self::SHIELD_PADDING_EXTERNAL + self::SHIELD_PADDING_INTERNAL, 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Badge\Image\Generator;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/PUGX/Badge/Image/Template/TemplateEngineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Badge\Image\Template;

/**
Expand All @@ -25,4 +25,4 @@ interface TemplateEngineInterface
* @return string
*/
public function render($templatePath, array $parameters);
}
}
5 changes: 2 additions & 3 deletions src/PUGX/Badge/Image/Tests/Factory/ShieldIOFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PUGX\Badge\Image\Tests\Factory;

use Guzzle\Http\Message\Response;
use PUGX\Badge\Image\Factory\ShieldIOFactory;

class ShieldIOFactoryTest extends \PHPUnit_Framework_TestCase
Expand All @@ -19,9 +20,7 @@ public function setUp()

public function testShouldCreateDownloadsImage()
{
$response = $this->getMockBuilder('\Guzzle\Http\Message\Response')
->disableOriginalConstructor()
->getMock();
$response = Response::fromMessage("HTTP/1.1 Hello.\r\n\r\n");

$this->httpClient->expects($this->once())
->method('get')
Expand Down
34 changes: 28 additions & 6 deletions src/PUGX/Badge/Image/Tests/Generator/SvgShieldGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@
* file that was distributed with this source code.
*/

namespace PUGX\Badge\Image\Tests\Generator;
namespace PUGX\Badge\Image\Generator {
// travis fails the calculation of the box size... faked!
function imagettfbbox($size, $angle, $fontfile, $text) {
global $mockImagettfbbox;

if (isset($mockImagettfbbox) && $mockImagettfbbox != false) {
return $mockImagettfbbox[$text];
} else {
return call_user_func_array('\imagettfbbox', func_get_args());
}
}

}


namespace PUGX\Badge\Image\Tests\Generator {

use PUGX\Badge\Image\Generator\SvgShieldGenerator;
use PUGX\Badge\Image\Generator\SvgShieldGeneratorInterface;


/**
* Class SvgShieldGeneratorTest
*
Expand All @@ -29,6 +45,11 @@ class SvgShieldGeneratorTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
global $mockImagettfbbox;
$mockImagettfbbox = array(
'testVendor' => array(-2, 0, 61, 0, 61, -11, -2, -11),
'testValue' => array(-2, 0, 52, 0, 52, -11, -2, -11)
);
$this->templateEngine = $this->getMock('\PUGX\Badge\Image\Template\TemplateEngineInterface');
$this->shieldGenerator = new SvgShieldGenerator($this->templateEngine, 'test');
}
Expand All @@ -50,17 +71,18 @@ public function getShieldData()
return array(
array(
'testVendor', 'testValue', 'red', array(
'vendorWidth' => 80.0,
'valueWidth' => 71.0,
'totalWidth' => 151.0,
'vendorWidth' => 73.0,
'valueWidth' => 64.0,
'totalWidth' => 137.0,
'vendorColor' => '#555',
'valueColor' => '#e05d44',
'vendor' => 'testVendor',
'value' => 'testValue',
'vendorStartPosition' => 41.0,
'valueStartPosition' => 114.5
'vendorStartPosition' => 37.5,
'valueStartPosition' => 104.0
)
)
);
}
}
}
2 changes: 1 addition & 1 deletion src/PUGX/BadgeBundle/Resources/config/image_creator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
</service>

</services>
</container>
</container>
2 changes: 1 addition & 1 deletion src/PUGX/BadgeBundle/Service/TwigAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\BadgeBundle\Service;

use PUGX\Badge\Image\Template\TemplateEngineInterface;
Expand Down

0 comments on commit 6fb2fc7

Please sign in to comment.