Skip to content

Commit 101bf4a

Browse files
jakejackson1claude
andauthored
Draw SVG text with the alpha of an rgba() or cmyka() fill or stroke (#427) (#486)
* Draw SVG text with the alpha of an rgba() or cmyka() fill or stroke Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Add a snapshot of SVG text drawn in rgba() and cmyka() colours Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 1bc4855 commit 101bf4a

4 files changed

Lines changed: 172 additions & 65 deletions

File tree

‎src/Image/Svg.php‎

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,39 +2386,12 @@ function svgText()
23862386
$stroke_width = $current_style['stroke-width'];
23872387
$stroke_width /= $scale;
23882388

2389-
$opacitystr = '';
2390-
$fopacity = 1;
2391-
2392-
if (isset($current_style['fill-opacity'])) {
2393-
if ($current_style['fill-opacity'] == 0) {
2394-
$fopacity = 0;
2395-
} elseif ($current_style['fill-opacity'] > 1) {
2396-
$fopacity = 1;
2397-
} elseif ($current_style['fill-opacity'] > 0) {
2398-
$fopacity = $current_style['fill-opacity'];
2399-
} elseif ($current_style['fill-opacity'] < 0) {
2400-
$fopacity = 0;
2401-
}
2402-
}
2403-
2404-
$sopacity = 1;
2405-
if (isset($current_style['stroke-opacity'])) {
2406-
if ($current_style['stroke-opacity'] == 0) {
2407-
$sopacity = 0;
2408-
} elseif ($current_style['stroke-opacity'] > 1) {
2409-
$sopacity = 1;
2410-
} elseif ($current_style['stroke-opacity'] > 0) {
2411-
$sopacity = $current_style['stroke-opacity'];
2412-
} elseif ($current_style['stroke-opacity'] < 0) {
2413-
$sopacity = 0;
2414-
}
2415-
}
2416-
2417-
$opacitystr = $this->opacityState(['ca' => $fopacity, 'CA' => $sopacity]);
2418-
24192389
$fillstr = '';
24202390
if (isset($current_style['fill']) && $current_style['fill'] != 'none') {
24212391
$col = $this->colorConverter->convert($current_style['fill'], $this->mpdf->PDFAXwarnings);
2392+
if ($col) {
2393+
$current_style['fill-opacity'] = $this->withColorAlpha($col, $current_style['fill-opacity']);
2394+
}
24222395
$fillstr = $this->mpdf->SetFColor($col, true);
24232396
$render = "0"; // Fill (only)
24242397
$op = 'f';
@@ -2428,6 +2401,7 @@ function svgText()
24282401
if ($stroke_width > 0 && $current_style['stroke'] != 'none') {
24292402
$scol = $this->colorConverter->convert($current_style['stroke'], $this->mpdf->PDFAXwarnings);
24302403
if ($scol) {
2404+
$current_style['stroke-opacity'] = $this->withColorAlpha($scol, $current_style['stroke-opacity']);
24312405
$strokestr .= $this->mpdf->SetDColor($scol, true) . ' ';
24322406
}
24332407
$linewidth = $this->ConvertSVGSizePixels($stroke_width);
@@ -2447,6 +2421,8 @@ function svgText()
24472421
return '';
24482422
}
24492423

2424+
$opacitystr = $this->textOpacityState($current_style);
2425+
24502426
if ($op == 'fS') {
24512427
$op = 'B';
24522428
}
@@ -2558,42 +2534,13 @@ function svgText()
25582534
$current_style['font-family'] = $this->mpdf->SetFont($current_style['font-family'], $style, $size, false);
25592535
$this->mpdf->CurrentFont['fo'] = true;
25602536

2561-
$opacitystr = '';
2562-
// mPDF 6
2563-
$fopacity = 1;
2564-
2565-
if (isset($current_style['fill-opacity'])) {
2566-
if ($current_style['fill-opacity'] == 0) {
2567-
$fopacity = 0;
2568-
} elseif ($current_style['fill-opacity'] > 1) {
2569-
$fopacity = 1;
2570-
} elseif ($current_style['fill-opacity'] > 0) {
2571-
$fopacity = $current_style['fill-opacity'];
2572-
} elseif ($current_style['fill-opacity'] < 0) {
2573-
$fopacity = 0;
2574-
}
2575-
}
2576-
2577-
$sopacity = 1;
2578-
2579-
if (isset($current_style['stroke-opacity'])) {
2580-
if ($current_style['stroke-opacity'] == 0) {
2581-
$sopacity = 0;
2582-
} elseif ($current_style['stroke-opacity'] > 1) {
2583-
$sopacity = 1;
2584-
} elseif ($current_style['stroke-opacity'] > 0) {
2585-
$sopacity = $current_style['stroke-opacity'];
2586-
} elseif ($current_style['stroke-opacity'] < 0) {
2587-
$sopacity = 0;
2588-
}
2589-
}
2590-
2591-
$opacitystr = $this->opacityState(['ca' => $fopacity, 'CA' => $sopacity]);
2592-
25932537
$fillstr = '';
25942538

25952539
if (isset($current_style['fill']) && $current_style['fill'] != 'none') {
25962540
$col = $this->colorConverter->convert($current_style['fill'], $this->mpdf->PDFAXwarnings);
2541+
if ($col) {
2542+
$current_style['fill-opacity'] = $this->withColorAlpha($col, $current_style['fill-opacity']);
2543+
}
25972544
$fillstr = $this->mpdf->SetFColor($col, true);
25982545
$render = "0"; // Fill (only)
25992546
}
@@ -2604,6 +2551,7 @@ function svgText()
26042551
$scol = $this->colorConverter->convert($current_style['stroke'], $this->mpdf->PDFAXwarnings);
26052552

26062553
if ($scol) {
2554+
$current_style['stroke-opacity'] = $this->withColorAlpha($scol, $current_style['stroke-opacity']);
26072555
$strokestr .= $this->mpdf->SetDColor($scol, true) . ' ';
26082556
}
26092557

@@ -2624,6 +2572,8 @@ function svgText()
26242572
return '';
26252573
}
26262574

2575+
$opacitystr = $this->textOpacityState($current_style);
2576+
26272577
$x = $this->txt_data[0]; // mPDF 5.7.4
26282578
$y = $this->txt_data[1]; // mPDF 5.7.4
26292579
$txt = $this->txt_data[2];
@@ -2705,6 +2655,34 @@ function svgText()
27052655
return $path_cmd;
27062656
}
27072657

2658+
/**
2659+
* The operator that sets the fill and stroke opacities of a text run, each held between 0 and 1
2660+
*
2661+
* @param array $style the text style, its opacities already multiplied by the alpha of its colours
2662+
*
2663+
* @return string
2664+
*/
2665+
private function textOpacityState(array $style)
2666+
{
2667+
$alphas = [];
2668+
foreach (['ca' => 'fill-opacity', 'CA' => 'stroke-opacity'] as $key => $property) {
2669+
$alphas[$key] = 1;
2670+
if (isset($style[$property])) {
2671+
if ($style[$property] == 0) {
2672+
$alphas[$key] = 0;
2673+
} elseif ($style[$property] > 1) {
2674+
$alphas[$key] = 1;
2675+
} elseif ($style[$property] > 0) {
2676+
$alphas[$key] = $style[$property];
2677+
} elseif ($style[$property] < 0) {
2678+
$alphas[$key] = 0;
2679+
}
2680+
}
2681+
}
2682+
2683+
return $this->opacityState($alphas);
2684+
}
2685+
27082686
function svgDefineTxtStyle($critere_style)
27092687
{
27102688
// get copy of current/default txt style, and modify it with supplied attributes

‎tests/Mpdf/Image/SvgColorAlphaTest.php‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Mpdf\PageStreams;
77

88
/**
9-
* The alpha of an rgba() or cmyka() fill, stroke or stop-color in an SVG (#395)
9+
* The alpha of an rgba() or cmyka() fill, stroke or stop-color in an SVG, on shapes and text alike (#395, #427)
1010
*/
1111
class SvgColorAlphaTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase
1212
{
@@ -48,6 +48,42 @@ public function fillAndStrokeProvider()
4848
];
4949
}
5050

51+
/**
52+
* Text takes the alpha of its fill and stroke colours, multiplied by any fill-opacity or stroke-opacity, whether
53+
* it is drawn in a document font or an SVG font
54+
*
55+
* @dataProvider textProvider
56+
*
57+
* @param string $attributes
58+
* @param float $fill
59+
* @param float $stroke
60+
* @param string $font
61+
*/
62+
public function testTextTakesTheColourAlpha($attributes, $fill, $stroke, $font = '')
63+
{
64+
$states = $this->states($this->draw($font . '<text x="10" y="30" font-size="20" ' . $attributes . '>Alpha</text>'));
65+
66+
$this->assertContains(['ca' => $fill, 'CA' => $stroke, 'BM' => '/Normal'], $states);
67+
}
68+
69+
/**
70+
* Text attributes, the fill and stroke alphas they draw with, and any SVG font the text is set in
71+
*
72+
* @return array
73+
*/
74+
public function textProvider()
75+
{
76+
$font = '<defs><font horiz-adv-x="500"><font-face font-family="Boxes" units-per-em="1000" />'
77+
. '<missing-glyph horiz-adv-x="500" d="M0 0 L400 0 L400 700 L0 700 Z" /></font></defs>';
78+
79+
return [
80+
'rgba fill' => ['fill="rgba(255,0,0,0.3)"', 0.3, 1],
81+
'cmyka fill, rgba stroke' => ['fill="cmyka(100,0,0,0,0.45)" stroke="rgba(0,0,255,0.7)" stroke-width="1"', 0.45, 0.7],
82+
'with opacity' => ['fill="rgba(255,0,0,0.5)" fill-opacity="0.5" stroke="cmyka(0,0,100,0,0.8)" stroke-opacity="0.5" stroke-width="1"', 0.25, 0.4],
83+
'SVG font' => ['font-family="Boxes" fill="rgba(255,0,0,0.3)" stroke="cmyka(0,100,0,0,0.6)" stroke-width="1"', 0.3, 0.6, $font],
84+
];
85+
}
86+
5187
/**
5288
* A gradient stop takes the alpha of its rgba() stop-color, multiplied by any stop-opacity
5389
*/
@@ -106,7 +142,7 @@ public function noTransparencyProvider()
106142
}
107143

108144
/**
109-
* A fill, a stroke and a gradient stop at half opacity. An alpha of 0.5 is stored as the byte "2", a digit, which
145+
* A fill, a stroke, a gradient stop and text at half opacity. An alpha of 0.5 is stored as the byte "2", a digit, which
110146
* the old reading divided and passed to ord() as a longer string
111147
*
112148
* @return string
@@ -115,7 +151,8 @@ private function translucentShapes()
115151
{
116152
return '<rect width="80" height="40" fill="rgba(255,0,0,0.5)" stroke="cmyka(0,100,0,0,0.5)" />'
117153
. '<linearGradient id="g"><stop offset="0" stop-color="rgba(255,0,0,0.5)" /><stop offset="1" stop-color="blue" /></linearGradient>'
118-
. '<rect width="80" height="40" fill="url(#g)" />';
154+
. '<rect width="80" height="40" fill="url(#g)" />'
155+
. '<text x="10" y="30" fill="rgba(255,0,0,0.5)" stroke="cmyka(0,100,0,0,0.5)" stroke-width="1">Alpha</text>';
119156
}
120157

121158
/**
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Snapshots;
4+
5+
/**
6+
* @group snapshot
7+
*/
8+
class SvgTextColorAlphaSnapshotTest extends Snapshot
9+
{
10+
/**
11+
* @return string A unique identifier / name for the snapshot
12+
*/
13+
public function getId()
14+
{
15+
return 'svg-text-color-alpha';
16+
}
17+
18+
/**
19+
* Stripes for the text to be drawn over, so what shows through it can be seen
20+
*
21+
* @return string
22+
*/
23+
private function stripes()
24+
{
25+
return '<rect x="0" y="0" width="400" height="60" fill="#ffffff" />'
26+
. '<rect x="0" y="0" width="100" height="60" fill="#14386b" />'
27+
. '<rect x="200" y="0" width="100" height="60" fill="#f0d878" />';
28+
}
29+
30+
/**
31+
* An SVG of text drawn over the stripes
32+
*
33+
* @param string $attributes The attributes on the text
34+
* @param string $defs Anything the text needs defined first, such as an SVG font
35+
*
36+
* @return string
37+
*/
38+
private function sample($attributes, $defs = '')
39+
{
40+
return '<p><svg width="400" height="60" viewBox="0 0 400 60">' . $defs . $this->stripes()
41+
. '<text x="10" y="46" font-size="48" ' . $attributes . '>Translucent</text>'
42+
. '</svg></p>';
43+
}
44+
45+
/**
46+
* Generate a PDF document by initializing the Mpdf object on $this->mpdf and
47+
* loading it with content
48+
*
49+
* @return void
50+
* @internal Don't call any $this->mpdf->Output*() method
51+
*/
52+
public function generatePdf()
53+
{
54+
$font = '<defs><font horiz-adv-x="500"><font-face font-family="Boxes" units-per-em="1000" />'
55+
. '<missing-glyph horiz-adv-x="500" d="M50 0 L450 0 L450 700 L50 700 Z" /></font></defs>';
56+
57+
ob_start();
58+
?>
59+
<style>
60+
p.caption { margin: 5mm 0 2mm 0; color: #606060; }
61+
</style>
62+
63+
<h1>mPDF</h1>
64+
<h2>SVG text in an rgba() or cmyka() colour</h2>
65+
66+
<p class="caption">The alpha of a fill or stroke colour is drawn on text as it is on shapes. Each line is set
67+
over blue, white and yellow stripes, which should show through the letters.</p>
68+
69+
<p class="caption">fill="#d02020" - opaque, for comparison</p>
70+
<?= $this->sample('fill="#d02020"') ?>
71+
72+
<p class="caption">fill="rgba(208,32,32,0.4)"</p>
73+
<?= $this->sample('fill="rgba(208,32,32,0.4)"') ?>
74+
75+
<p class="caption">fill="cmyka(0,85,85,0,0.4)" stroke="rgba(0,0,0,0.6)"</p>
76+
<?= $this->sample('fill="cmyka(0,85,85,0,0.4)" stroke="rgba(0,0,0,0.6)" stroke-width="2"') ?>
77+
78+
<p class="caption">fill="rgba(208,32,32,0.8)" fill-opacity="0.5" - the two multiplied, 0.4</p>
79+
<?= $this->sample('fill="rgba(208,32,32,0.8)" fill-opacity="0.5"') ?>
80+
81+
<p class="caption">An SVG font, fill="rgba(208,32,32,0.4)" stroke="cmyka(0,0,0,100,0.6)"</p>
82+
<?= $this->sample('font-family="Boxes" fill="rgba(208,32,32,0.4)" stroke="cmyka(0,0,0,100,0.6)" stroke-width="2"', $font) ?>
83+
84+
<p class="caption">A rect with fill="rgba(208,32,32,0.4)", which the text above should match</p>
85+
<p><svg width="400" height="60" viewBox="0 0 400 60"><?= $this->stripes() ?><rect x="10" y="10" width="380" height="40" fill="rgba(208,32,32,0.4)" /></svg></p>
86+
<?php
87+
$html = ob_get_clean();
88+
89+
$this->mpdf = $this->createMpdf();
90+
$this->mpdf->WriteHTML($html);
91+
}
92+
}
35.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)