Skip to content

Commit 8e0fb52

Browse files
committed
Add support for text style and weight with the CPdf adapter
1 parent 091c051 commit 8e0fb52

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/Svg/Surface/SurfaceCpdf.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* @package php-svg-lib
44
* @link http://github.com/PhenX/php-svg-lib
5-
* @author Fabien Ménager <[email protected]>
5+
* @author Fabien M�nager <[email protected]>
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
77
*/
88

@@ -15,7 +15,7 @@ class SurfaceCpdf implements SurfaceInterface
1515
{
1616
const DEBUG = false;
1717

18-
/** @var Cpdf */
18+
/** @var \CPdf\CPdf */
1919
private $canvas;
2020

2121
private $width;
@@ -33,7 +33,9 @@ public function __construct(Document $doc, $canvas = null)
3333
$h = $dimensions["height"];
3434

3535
if (!$canvas) {
36-
$canvas = new CPdf(array(0, 0, $w, $h));
36+
$canvas = new \CPdf\CPdf(array(0, 0, $w, $h));
37+
$refl = new \ReflectionClass($canvas);
38+
$canvas->fontcache = dirname($refl->getFileName())."/../../fonts/";
3739
}
3840

3941
// Flip PDF coordinate system so that the origin is in
@@ -356,7 +358,7 @@ public function measureText($text)
356358
{
357359
if (self::DEBUG) echo __FUNCTION__ . "\n";
358360
$style = $this->getStyle();
359-
$this->getFont($style->fontFamily, $style->fontStyle);
361+
$this->getFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
360362

361363
return $this->canvas->getTextWidth($this->getStyle()->fontSize, $text);
362364
}
@@ -420,10 +422,10 @@ public function setStyle(Style $style)
420422
$dashArray
421423
);
422424

423-
$this->getFont($style->fontFamily, $style->fontStyle);
425+
$this->getFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
424426
}
425427

426-
private function getFont($family, $style)
428+
private function getFont($family, $style, $weight)
427429
{
428430
$map = array(
429431
"serif" => "Times",
@@ -436,11 +438,51 @@ private function getFont($family, $style)
436438
"verdana" => "Helvetica",
437439
);
438440

441+
$styleMap = array(
442+
'Helvetica' => array(
443+
'b' => 'Helvetica-Bold',
444+
'i' => 'Helvetica-Oblique',
445+
'bi' => 'Helvetica-BoldOblique',
446+
'ib' => 'Helvetica-BoldOblique'
447+
),
448+
'Courier' => array(
449+
'b' => 'Courier-Bold',
450+
'i' => 'Courier-Oblique',
451+
'bi' => 'Courier-BoldOblique',
452+
'ib' => 'Courier-BoldOblique'
453+
),
454+
'Times' => array(
455+
'b' => 'Times-Bold',
456+
'i' => 'Times-Italic',
457+
'bi' => 'Times-BoldItalic',
458+
'ib' => 'Times-BoldItalic'
459+
),
460+
);
461+
439462
$family = strtolower($family);
463+
$style = strtolower($style);
464+
$weight = strtolower($weight);
465+
440466
if (isset($map[$family])) {
441467
$family = $map[$family];
442468
}
443469

470+
if (isset($styleMap[$family])) {
471+
$key = "";
472+
473+
if ($weight === "bold" || $weight === "bolder" || (is_numeric($weight) && $weight >= 600)) {
474+
$key .= "b";
475+
}
476+
477+
if ($style === "italic" || $style === "oblique") {
478+
$key .= "i";
479+
}
480+
481+
if ($key && isset($styleMap[$family][$key])) {
482+
$family = $styleMap[$family][$key];
483+
}
484+
}
485+
444486
$this->canvas->selectFont("$family.afm");
445487
}
446488
}

src/Svg/Tag/Text.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ public function end()
3636
$x = $this->x;
3737
$y = $this->y;
3838

39-
$width = $surface->measureText($this->text);
39+
switch ($surface->getStyle()->textAnchor) {
40+
case "middle":
41+
$width = $surface->measureText($this->text);
42+
$x -= $width / 2;
43+
break;
4044

41-
if ($surface->getStyle()->textAnchor == "middle") {
42-
$x -= $width / 2;
43-
}
44-
if ($surface->getStyle()->textAnchor == "end") {
45-
$x -= $width;
45+
case "end":
46+
$width = $surface->measureText($this->text);
47+
$x -= $width;
48+
break;
4649
}
4750

4851
$surface->fillText($this->getText(), $x, $y);

0 commit comments

Comments
 (0)