Skip to content

Commit 0aeca31

Browse files
committed
Improve font handling
1 parent 9af64c2 commit 0aeca31

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

src/Svg/Style.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function getStyleMap()
6363

6464
'font-family' => array('fontFamily', self::TYPE_NAME),
6565
'font-size' => array('fontSize', self::TYPE_NUMBER),
66-
'font-weight' => array('fontWeight', self::TYPE_NUMBER),
66+
'font-weight' => array('fontWeight', self::TYPE_NAME),
6767
'font-style' => array('fontStyle', self::TYPE_NAME),
6868
'text-anchor' => array('textAnchor', self::TYPE_NAME),
6969
);

src/Svg/Surface/SurfaceCpdf.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(Document $doc, $canvas = null)
3535
if (!$canvas) {
3636
$canvas = new \CPdf\CPdf(array(0, 0, $w, $h));
3737
$refl = new \ReflectionClass($canvas);
38-
$canvas->fontcache = dirname($refl->getFileName())."/../../fonts/";
38+
$canvas->fontcache = realpath(dirname($refl->getFileName()) . "/../../fonts/")."/";
3939
}
4040

4141
// Flip PDF coordinate system so that the origin is in
@@ -143,7 +143,7 @@ public function fillText($text, $x, $y, $maxWidth = null)
143143
public function strokeText($text, $x, $y, $maxWidth = null)
144144
{
145145
if (self::DEBUG) echo __FUNCTION__ . "\n";
146-
// TODO: Implement drawImage() method.
146+
$this->canvas->addText($x, $y, $this->style->fontSize, $text);
147147
}
148148

149149
public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null)
@@ -358,7 +358,7 @@ public function measureText($text)
358358
{
359359
if (self::DEBUG) echo __FUNCTION__ . "\n";
360360
$style = $this->getStyle();
361-
$this->getFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
361+
$this->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
362362

363363
return $this->canvas->getTextWidth($this->getStyle()->fontSize, $text);
364364
}
@@ -422,11 +422,12 @@ public function setStyle(Style $style)
422422
$dashArray
423423
);
424424

425-
$this->getFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
425+
$this->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
426426
}
427427

428-
private function getFont($family, $style, $weight)
428+
public function setFont($family, $style, $weight)
429429
{
430+
var_dump("$family.$style.$weight");
430431
$map = array(
431432
"serif" => "Times",
432433
"sans-serif" => "Helvetica",
@@ -443,19 +444,17 @@ private function getFont($family, $style, $weight)
443444
'b' => 'Helvetica-Bold',
444445
'i' => 'Helvetica-Oblique',
445446
'bi' => 'Helvetica-BoldOblique',
446-
'ib' => 'Helvetica-BoldOblique'
447447
),
448448
'Courier' => array(
449449
'b' => 'Courier-Bold',
450450
'i' => 'Courier-Oblique',
451451
'bi' => 'Courier-BoldOblique',
452-
'ib' => 'Courier-BoldOblique'
453452
),
454453
'Times' => array(
454+
'' => 'Times-Roman',
455455
'b' => 'Times-Bold',
456456
'i' => 'Times-Italic',
457457
'bi' => 'Times-BoldItalic',
458-
'ib' => 'Times-BoldItalic'
459458
),
460459
);
461460

@@ -468,7 +467,7 @@ private function getFont($family, $style, $weight)
468467
}
469468

470469
if (isset($styleMap[$family])) {
471-
$key = "";
470+
$key = "b";
472471

473472
if ($weight === "bold" || $weight === "bolder" || (is_numeric($weight) && $weight >= 600)) {
474473
$key .= "b";
@@ -478,7 +477,7 @@ private function getFont($family, $style, $weight)
478477
$key .= "i";
479478
}
480479

481-
if ($key && isset($styleMap[$family][$key])) {
480+
if (isset($styleMap[$family][$key])) {
482481
$family = $styleMap[$family][$key];
483482
}
484483
}

src/Svg/Surface/SurfaceGmagick.php

Lines changed: 6 additions & 1 deletion
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

@@ -300,4 +300,9 @@ private function getFont($family, $style)
300300

301301
return $this->canvas->load_font($family, "unicode", "fontstyle=$style");
302302
}
303+
304+
public function setFont($family, $style, $weight)
305+
{
306+
// TODO: Implement setFont() method.
307+
}
303308
}

src/Svg/Surface/SurfaceInterface.php

Lines changed: 3 additions & 1 deletion
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

@@ -85,4 +85,6 @@ public function setStyle(Style $style);
8585
* @return Style
8686
*/
8787
public function getStyle();
88+
89+
public function setFont($family, $style, $weight);
8890
}

src/Svg/Surface/SurfacePDFLib.php

Lines changed: 6 additions & 1 deletion
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

@@ -414,4 +414,9 @@ private function getFont($family, $style)
414414

415415
return $this->canvas->load_font($family, "unicode", "fontstyle=$style");
416416
}
417+
418+
public function setFont($family, $style, $weight)
419+
{
420+
// TODO: Implement setFont() method.
421+
}
417422
}

src/Svg/Tag/Text.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function end()
3535
$surface = $this->document->getSurface();
3636
$x = $this->x;
3737
$y = $this->y;
38+
$style = $surface->getStyle();
39+
$surface->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight);
3840

39-
switch ($surface->getStyle()->textAnchor) {
41+
switch ($style->textAnchor) {
4042
case "middle":
4143
$width = $surface->measureText($this->text);
4244
$x -= $width / 2;

0 commit comments

Comments
 (0)