2
2
/**
3
3
* @package php-svg-lib
4
4
* @link http://github.com/PhenX/php-svg-lib
5
- * @author Fabien Ménager <[email protected] >
5
+ * @author Fabien M�nager <[email protected] >
6
6
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7
7
*/
8
8
@@ -15,7 +15,7 @@ class SurfaceCpdf implements SurfaceInterface
15
15
{
16
16
const DEBUG = false ;
17
17
18
- /** @var Cpdf */
18
+ /** @var \CPdf\CPdf */
19
19
private $ canvas ;
20
20
21
21
private $ width ;
@@ -33,7 +33,9 @@ public function __construct(Document $doc, $canvas = null)
33
33
$ h = $ dimensions ["height " ];
34
34
35
35
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/ " ;
37
39
}
38
40
39
41
// Flip PDF coordinate system so that the origin is in
@@ -356,7 +358,7 @@ public function measureText($text)
356
358
{
357
359
if (self ::DEBUG ) echo __FUNCTION__ . "\n" ;
358
360
$ style = $ this ->getStyle ();
359
- $ this ->getFont ($ style ->fontFamily , $ style ->fontStyle );
361
+ $ this ->getFont ($ style ->fontFamily , $ style ->fontStyle , $ style -> fontWeight );
360
362
361
363
return $ this ->canvas ->getTextWidth ($ this ->getStyle ()->fontSize , $ text );
362
364
}
@@ -420,10 +422,10 @@ public function setStyle(Style $style)
420
422
$ dashArray
421
423
);
422
424
423
- $ this ->getFont ($ style ->fontFamily , $ style ->fontStyle );
425
+ $ this ->getFont ($ style ->fontFamily , $ style ->fontStyle , $ style -> fontWeight );
424
426
}
425
427
426
- private function getFont ($ family , $ style )
428
+ private function getFont ($ family , $ style, $ weight )
427
429
{
428
430
$ map = array (
429
431
"serif " => "Times " ,
@@ -436,11 +438,51 @@ private function getFont($family, $style)
436
438
"verdana " => "Helvetica " ,
437
439
);
438
440
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
+
439
462
$ family = strtolower ($ family );
463
+ $ style = strtolower ($ style );
464
+ $ weight = strtolower ($ weight );
465
+
440
466
if (isset ($ map [$ family ])) {
441
467
$ family = $ map [$ family ];
442
468
}
443
469
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
+
444
486
$ this ->canvas ->selectFont ("$ family.afm " );
445
487
}
446
488
}
0 commit comments