Skip to content

Commit

Permalink
minor fixes wrt antialiasing
Browse files Browse the repository at this point in the history
Releases v0.1.1
  • Loading branch information
laufhannes committed Oct 9, 2018
1 parent cfac8d1 commit ed8dcb6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "runalyze/static-maps",
"description": "Library to create static images from various map tile providers.",
"version": "0.1.0",
"version": "0.1.1",
"license": "MIT",
"require": {
"php": ">=7.0",
Expand Down
15 changes: 14 additions & 1 deletion src/Drawer/AntialiasDrawer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class AntialiasDrawer
/** @var bool */
protected $IsNativeFunctionAvailable;

/** @var bool */
protected $UseNativeFunctionIfAvailable = false;

/** @var float */
protected $AntialiasThreshold;

Expand All @@ -27,9 +30,14 @@ public function __construct(float $antialiasThreshold = 0.0)
$this->AntialiasThreshold = $antialiasThreshold;
}

public function enableNativeAntialiasingIfAvailable(bool $flag)
{
$this->UseNativeFunctionIfAvailable = $flag;
}

public function drawLine($resource, $x1, $y1, $x2, $y2, int $r, int $g, int $b, float $alpha = 100.0, int $lineWidth = 1)
{
if ($this->IsNativeFunctionAvailable) {
if ($this->IsNativeFunctionAvailable && $this->UseNativeFunctionIfAvailable) {
$this->drawNativeLine($resource, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha, $lineWidth);
} else {
$this->drawAntialiasLine($resource, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha, $lineWidth);
Expand All @@ -38,6 +46,11 @@ public function drawLine($resource, $x1, $y1, $x2, $y2, int $r, int $g, int $b,

protected function drawNativeLine($resource, $x1, $y1, $x2, $y2, int $r, int $g, int $b, float $alpha = 100.0, int $lineWidth = 1)
{
$x1 = (int)round($x1);
$x2 = (int)round($x2);
$y1 = (int)round($y1);
$y2 = (int)round($y2);

imagesetthickness($resource, $lineWidth);
imagealphablending($resource, true);
imageantialias($resource, true);
Expand Down
11 changes: 9 additions & 2 deletions src/Feature/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Route implements FeatureInterface
/** @var bool */
protected $Antialiasing;

/** @var bool */
protected $AntialiasingNative = false;

/** @var float [px] */
protected $LineSimplificationTolerance = 0.0;

Expand All @@ -59,9 +62,10 @@ public function __construct(array $coordinates, $lineColor = '#000', int $lineWi
$this->AntialiasDrawer = new AntialiasDrawer();
}

public function enableAntialiasing(bool $flag)
public function enableAntialiasing(bool $flag = true, bool $nativeFlag = false)
{
$this->Antialiasing = $antialiasing;
$this->AntialiasingNative = $nativeFlag;
}

public function enableLineSimplification(float $pixelTolerance = 10)
Expand All @@ -83,6 +87,8 @@ protected function getLineColorArray($lineColor): array

public function render(ImageManager $imageManager, Image $image, ViewportInterface $viewport)
{
$this->AntialiasDrawer->enableNativeAntialiasingIfAvailable($this->AntialiasingNative);

foreach ($this->LineSegments as $segment) {
$numPoints = count($segment);
$lastPoint = 0;
Expand All @@ -106,7 +112,7 @@ public function render(ImageManager $imageManager, Image $image, ViewportInterfa

$this->truncateStartAndEndPointsBy(0.2, $x1, $y1, $x2, $y2);

if ($this->Antialiasing && !function_exists('imageantialias')) {
if ($this->Antialiasing) {
$this->AntialiasDrawer->drawLine(
$image->getCore(),
$x1,
Expand All @@ -120,6 +126,7 @@ public function render(ImageManager $imageManager, Image $image, ViewportInterfa
$this->LineWidth
);
} else {
imagesetthickness($image->getCore(), $this->LineWidth);
$image->line($x1, $y1, $x2, $y2, $this->LineCallback);
}

Expand Down

0 comments on commit ed8dcb6

Please sign in to comment.