Skip to content

Commit

Permalink
Merge pull request #3 from liuggio/new_flat_format
Browse files Browse the repository at this point in the history
added flat format
  • Loading branch information
liuggio committed Apr 20, 2015
2 parents f5e73a2 + 577f80c commit 10aaca8
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 29 deletions.
36 changes: 19 additions & 17 deletions features/bootstrap/fixtures/license-MIT-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions features/bootstrap/fixtures/license-MIT-blue_plastic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion features/ui_command_creating_image_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Feature: Generation of an image
As a visitor
I want to use the poser script

Scenario: Create the image running a script
Scenario: Create the image running a script with plastic format
When I run "poser license MIT blue -p /tmp/img.svg -f plastic"
Then it should pass
And the content of "/tmp/img.svg" should be equal to "bootstrap/fixtures/license-MIT-blue_plastic.svg"

Scenario: Create the image running a script with flat format
When I run "poser license MIT blue -p /tmp/img.svg"
Then it should pass
And the content of "/tmp/img.svg" should be equal to "bootstrap/fixtures/license-MIT-blue.svg"
9 changes: 7 additions & 2 deletions features/ui_command_echo_image.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Feature: Generation of an image by echo-ing the content
As a visitor
I want to use the poser script

Scenario: Echo the image running a script
Scenario: Echo the image running a script with plastic format
When I run "poser license MIT blue -f plastic"
Then it should pass
And the same output should be like the content of "bootstrap/fixtures/license-MIT-blue_plastic.svg"

Scenario: Echo the image running a script with flat format
When I run "poser license MIT blue"
Then it should pass
And the same output should be like the content of "bootstrap/fixtures/license-MIT-blue.svg"
And the same output should be like the content of "bootstrap/fixtures/license-MIT-blue.svg"
4 changes: 2 additions & 2 deletions spec/PUGX/Poser/PoserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use PUGX\Poser\Render\SvgRender;
use PUGX\Poser\Render\SvgFlatRender;

class PoserSpec extends ObjectBehavior
{
function let()
{
$render = new SvgRender();
$render = new SvgFlatRender();
$this->beConstructedWith(array($render));
}

Expand Down
69 changes: 69 additions & 0 deletions spec/PUGX/Poser/Render/SvgFlatRenderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace spec\PUGX\Poser\Render;

use PhpSpec\Exception\Exception;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use PUGX\Poser\Badge;
use PUGX\Poser\Calculator\GDTextSizeCalculator;
use PUGX\Poser\Calculator\TextSizeCalculatorInterface;

class SvgFlatRenderSpec extends ObjectBehavior
{
function let(TextSizeCalculatorInterface $calculator)
{
$calculator->calculateWidth(Argument::any())->willReturn(20);
$this->beConstructedWith($calculator);
}

function it_should_render_a_svg()
{
$badge = Badge::fromURI('version-stable-97CA00.svg');
$this->render($badge)->shouldBeAValidSVGImage();
}

public function getMatchers()
{
return array(
'beAValidSVGImage' => function($subject) {

$regex = '/^<svg.*width="((.|\n)*)<\/svg>$/';
$matches = array();

return preg_match($regex, (string) $subject, $matches, PREG_OFFSET_CAPTURE, 0);
}
);
}

function it_should_render_a_license_mit_exactly_like_this_svg()
{
$template = <<<EOF
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="40" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h20v20H0z"/>
<path fill="#007ec6" d="M20 0h31v20H20z"/>
<path fill="url(#b)" d="M0 0h40v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="25.5" y="15" fill="#010101" fill-opacity=".3">license</text>
<text x="25.5" y="14">license</text>
<text x="63.5" y="15" fill="#010101" fill-opacity=".3">MIT</text>
<text x="63.5" y="14">MIT</text>
</g>
</svg>
EOF;

$badge = Badge::fromURI('license-MIT-blue.svg');
$this->render($badge)->__toString()->shouldBeLike($template);
}


}
3 changes: 2 additions & 1 deletion src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function __construct($content, $format)
*/
public function __toString()
{
return $this->content;
return (string) $this->content;
}

/**
Expand All @@ -66,4 +66,5 @@ public function getFormat()
{
return $this->format;
}

}
162 changes: 162 additions & 0 deletions src/Render/SvgFlatRender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the badge-poser package.
*
* (c) PUGX <http://pugx.github.io/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\Poser\Render;

use PUGX\Poser\Badge;
use PUGX\Poser\Calculator\TextSizeCalculatorInterface;
use PUGX\Poser\Calculator\GDTextSizeCalculator;
use PUGX\Poser\Image;
/**
* Class SvgFlatGenerator
*
* @author Giulio De Donato <liuggio@gmail.com>
*/
class SvgFlatRender implements RenderInterface
{
const VENDOR_COLOR = '#555';
private $textSizeCalculator;
/*
private static $template = <<<EOF
<svg xmlns="http://www.w3.org/2000/svg" width="{{ totalWidth }}" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="{{ totalWidth }}" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path fill="#555" d="M0 0h{{ vendorWidth }}v20H0z"/>
<path fill="#007ec6" d="M{{ vendorWidth }} 0h31v20H{{ vendorWidth }}z"/>
<path fill="url(#b)" d="M0 0h{{ totalWidth }}v20H0z"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="25.5" y="15" fill="#010101" fill-opacity=".3">{{ vendor }}</text>
<text x="25.5" y="14">{{ vendor }}</text>
<text x="63.5" y="15" fill="#010101" fill-opacity=".3">{{ value }}</text>
<text x="63.5" y="14">{{ value }}</text>
</g>
</svg>
EOF;
quasi ok
ù private static $template = <<<EOF
<svg xmlns="http://www.w3.org/2000/svg" width="{{ totalWidth }}" height="20">
<linearGradient id="smooth" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="round">
<rect width="{{ totalWidth }}" height="20" rx="3" fill="{{ vendorColor }}"/>
</mask>
<g mask="url(#round)">
<rect width="{{ vendorWidth }}" height="20" fill="#fff"/>
<rect x="{{ vendorWidth }}" width="{{ valueWidth }}" height="20" fill="{{ valueColor }}"/>
<rect width="{{ totalWidth }}" height="20" fill="url(#smooth)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="{{ vendorStartPosition }}" y="15" fill="#010101" fill-opacity=".3">{{ vendor }}</text>
<text x="{{ vendorStartPosition }}" y="14">{{ vendor }}</text>
<text x="{{ valueStartPosition }}" y="15" fill="#010101" fill-opacity=".3">{{ value }}</text>
<text x="{{ valueStartPosition }}" y="14">{{ value }}</text>
</g>
</svg>
EOF;
*/

private static $template = <<<EOF
<svg xmlns="http://www.w3.org/2000/svg" width="{{ totalWidth }}" height="20">
<linearGradient id="b" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="{{ totalWidth }}" height="20" rx="3" fill="#fff"/>
</mask>
<g mask="url(#a)">
<rect width="{{ vendorWidth }}" height="20" fill="#555"/>
<rect x="{{ vendorWidth }}" width="{{ valueWidth }}" height="20" fill="{{ valueColor }}"/>
<rect width="{{ totalWidth }}" height="20" fill="url(#b)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="{{ vendorStartPosition }}" y="15" fill="#010101" fill-opacity=".3">{{ vendor }}</text>
<text x="{{ vendorStartPosition }}" y="14">{{ vendor }}</text>
<text x="{{ valueStartPosition }}" y="15" fill="#010101" fill-opacity=".3">{{ value }}</text>
<text x="{{ valueStartPosition }}" y="14">{{ value }}</text>
</g>
</svg>
EOF;

/**
* Constructor.
*
* @param TextSizeCalculatorInterface $textSizeCalculator
*/
public function __construct(TextSizeCalculatorInterface $textSizeCalculator = null)
{
$this->textSizeCalculator = $textSizeCalculator;

if (null === $this->textSizeCalculator) {
$this->textSizeCalculator = new GDTextSizeCalculator();
}
}

/**
* @param Badge $badge
*
* @return mixed
*/
public function render(Badge $badge)
{
$parameters = array();

$parameters['vendorWidth'] = $this->stringWidth($badge->getSubject());
$parameters['valueWidth'] = $this->stringWidth($badge->getStatus());
$parameters['totalWidth'] = $parameters['valueWidth'] + $parameters['vendorWidth'];
$parameters['vendorColor'] = self::VENDOR_COLOR;
$parameters['valueColor'] = $badge->getHexColor();
$parameters['vendor'] = $badge->getSubject();
$parameters['value'] = $badge->getStatus();
$parameters['vendorStartPosition'] = round($parameters['vendorWidth'] / 2, 1) + 1;
$parameters['valueStartPosition'] = $parameters['vendorWidth'] + round($parameters['valueWidth'] / 2, 1) - 1;

return $this->renderSvg(self::$template, $parameters, $badge->getFormat());
}

/**
* A list of all supported formats.
*
* @return array
*/
public function supportedFormats()
{
return array('flat', 'svg');
}

private function stringWidth($text)
{
return $this->textSizeCalculator->calculateWidth($text);
}

private function renderSvg($render, $parameters, $format)
{
foreach ($parameters as $key => $variable) {
$render = str_replace(sprintf('{{ %s }}', $key), $variable, $render);
}

return Image::createFromString($render, $format);
}
}
2 changes: 1 addition & 1 deletion src/Render/SvgRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function render(Badge $badge)
*/
public function supportedFormats()
{
return array('svg');
return array('plastic');
}

private function stringWidth($text)
Expand Down
Loading

0 comments on commit 10aaca8

Please sign in to comment.