Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for text kerning (ImageMagick only) #624

Merged
merged 1 commit into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for text kerning (ImageMagick only)
  • Loading branch information
lupka committed Oct 2, 2016
commit 8608eb019182e1a4066e8cf9d26b77110feae071
28 changes: 28 additions & 0 deletions src/Intervention/Image/AbstractFont.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ abstract class AbstractFont
*/
public $valign;

/**
* Space between text characters
*
* @var float
*/
public $kerning = 0;

/**
* Path to TTF or GD library internal font file of the text
*
Expand Down Expand Up @@ -199,6 +206,27 @@ public function getValign()
return $this->valign;
}

/**
* Set text kerning
*
* @param string $kerning
* @return void
*/
public function kerning($kerning)
{
$this->kerning = $kerning;
}

/**
* Get kerning
*
* @return float
*/
public function getKerning()
{
return $this->kerning;
}

/**
* Set path to font file
*
Expand Down
14 changes: 14 additions & 0 deletions src/Intervention/Image/Gd/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,18 @@ public function applyToImage(Image $image, $posx = 0, $posy = 0)
imagestring($image->getCore(), $this->getInternalFont(), $posx, $posy, $this->text, $color->getInt());
}
}

/**
* Set text kerning
*
* @param string $kerning
* @return void
*/
public function kerning($kerning)
{
throw new \Intervention\Image\Exception\NotSupportedException(
"Kerning is not supported by GD driver."
);
}

}
1 change: 1 addition & 0 deletions src/Intervention/Image/Imagick/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function applyToImage(Image $image, $posx = 0, $posy = 0)

$draw->setFontSize($this->size);
$draw->setFillColor($color->getPixel());
$draw->setTextKerning($this->kerning);

// align horizontal
switch (strtolower($this->align)) {
Expand Down
11 changes: 9 additions & 2 deletions tests/AbstractFontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testConstructor()
$font = $this->getMockForAbstractClass('\Intervention\Image\AbstractFont', array('test'));
$this->assertEquals('test', $font->text);
}

public function testText()
{
$font = $this->getMockForAbstractClass('\Intervention\Image\AbstractFont');
Expand Down Expand Up @@ -55,6 +55,13 @@ public function testValign()
$this->assertEquals('top', $font->valign);
}

public function testKerning()
{
$font = $this->getMockForAbstractClass('\Intervention\Image\AbstractFont');
$font->kerning(10.5);
$this->assertEquals(10.5, $font->kerning);
}

public function testFile()
{
$font = $this->getMockForAbstractClass('\Intervention\Image\AbstractFont');
Expand All @@ -66,7 +73,7 @@ public function testCountLines()
{
$font = $this->getMockForAbstractClass('\Intervention\Image\AbstractFont');
$font->text('foo'.PHP_EOL.'bar'.PHP_EOL.'baz');
$this->assertEquals(3, $font->countLines());
$this->assertEquals(3, $font->countLines());
$font->text("foo\nbar\nbaz");
$this->assertEquals(3, $font->countLines());
$font->text('foo
Expand Down