-
-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathE034_color_gradient_mesh.php
More file actions
223 lines (181 loc) · 5.53 KB
/
E034_color_gradient_mesh.php
File metadata and controls
223 lines (181 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* E034_color_gradient_mesh.php
*
* @since 2017-05-08
* @category Library
* @package Pdf
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* This file is part of tc-lib-pdf software library.
*/
// NOTE: run make deps fonts in the project root to generate the dependencies and example fonts.
// autoloader when using Composer
require(__DIR__ . '/../vendor/autoload.php');
\define('OUTPUT_FILE', \realpath(__DIR__ . '/../target') . '/example.pdf');
// define fonts directory
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
// autoloader when using RPM or DEB package installation
//require ('/usr/share/php/Com/Tecnick/Pdf/autoload.php');
// main TCPDF object
$pdf = new \Com\Tecnick\Pdf\Tcpdf(
'mm', // string $unit = 'mm',
true, // bool $isunicode = true,
false, // bool $subsetfont = false,
true, // bool $compress = true,
'', // string $mode = '',
null, // ?ObjEncrypt $objEncrypt = null,
);
// ----------
$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 034');
$pdf->setTitle('Example');
$pdf->setKeywords('TCPDF tc-lib-pdf example');
$pdf->setPDFFilename('034_color_gradient_mesh.pdf');
$pdf->setViewerPreferences(['DisplayDocTitle' => true]);
$pdf->enableDefaultPageContent();
// ----------
// Insert fonts
$bfont1 = $pdf->font->insert($pdf->pon, 'helvetica', '', 12);
// test images directory
$imgdir = \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-image/test/images/');
// ----------
$page01 = $pdf->addPage();
$pdf->setBookmark('Color Gradients', '', 1);
$html = '<h1>Color Gradients and Meshes</h1>'
. '<p>This page demonstrates linear and radial gradients together with Coons patch meshes rendered by tc-lib-pdf.</p>';
$pdf->addHTMLCell($html, 15, 20, 180);
// Linear gradient
$lingrad = $pdf->graph->getLinearGradient(20, 80, 80, 80, 'red', 'blue', [0, 0, 1, 0]);
$pdf->page->addContent($lingrad);
// Radial Gradient
$radgrad = $pdf->graph->getRadialGradient(110, 80, 80, 80, 'white', 'black', [0.5, 0.5, 1, 1, 1.2]);
$pdf->page->addContent($radgrad);
// CoonsPatchMesh
$coonspatchmesh1 = $pdf->graph->getCoonsPatchMeshWithCoords(20, 180, 80, 80, 'yellow', 'blue', 'green', 'red');
$pdf->page->addContent($coonspatchmesh1);
// set the coordinates for the cubic Bézier points x1,y1 ... x12, y12 of the patch
$coords = [
0.00,
0.00,
0.33,
0.20,
//lower left
0.67,
0.00,
1.00,
0.00,
0.80,
0.33,
//lower right
0.80,
0.67,
1.00,
1.00,
0.67,
0.80,
//upper right
0.33,
1.00,
0.00,
1.00,
0.20,
0.67,
//upper left
0.00,
0.33,
]; //lower left
// paint a coons patch gradient with the above coordinates
$coonspatchmesh2 = $pdf->graph->getCoonsPatchMeshWithCoords(110, 180, 80, 80, 'yellow', 'blue', 'green', 'red', $coords, 0, 1);
$pdf->page->addContent($coonspatchmesh2);
// ----------
$page02 = $pdf->addPage();
$pdf->setBookmark('Color gradient mesh', '', 1);
// first patch: f = 0
$patch_array[0]['f'] = 0;
$patch_array[0]['points'] = [0.00, 0.00, 0.33, 0.00, 0.67, 0.00, 1.00, 0.00, 1.00, 0.33, 0.8, 0.67, 1.00, 1.00, 0.67, 0.8, 0.33, 1.80, 0.00, 1.00, 0.00, 0.67, 0.00, 0.33];
$patch_array[0]['colors'][0] = [
'red' => 1,
'green' => 1,
'blue' => 0,
'alpha' => 1,
];
$patch_array[0]['colors'][1] = [
'red' => 1,
'green' => 1,
'blue' => 1,
'alpha' => 1,
];
$patch_array[0]['colors'][2] = [
'red' => 0,
'green' => 1,
'blue' => 0,
'alpha' => 1,
];
$patch_array[0]['colors'][3] = [
'red' => 1,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
// second patch - above the other: f = 2
$patch_array[1]['f'] = 2;
$patch_array[1]['points'] = [0.00, 1.33, 0.00, 1.67, 0.00, 2.00, 0.33, 2.00, 0.67, 2.00, 1.00, 2.00, 1.00, 1.67, 1.5, 1.33];
$patch_array[1]['colors'][0] = [
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
$patch_array[1]['colors'][1] = [
'red' => 1,
'green' => 0,
'blue' => 1,
'alpha' => 1,
];
// third patch - right of the above: f = 3
$patch_array[2]['f'] = 3;
$patch_array[2]['points'] = [1.33, 0.80, 1.67, 1.50, 2.00, 1.00, 2.00, 1.33, 2.00, 1.67, 2.00, 2.00, 1.67, 2.00, 1.33, 2.00];
$patch_array[2]['colors'][0] = [
'red' => 0,
'green' => 1,
'blue' => 1,
'alpha' => 1,
];
$patch_array[2]['colors'][1] = [
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
// fourth patch - below the above, which means left(?) of the above: f = 1
$patch_array[3]['f'] = 1;
$patch_array[3]['points'] = [2.00, 0.67, 2.00, 0.33, 2.00, 0.00, 1.67, 0.00, 1.33, 0.00, 1.00, 0.00, 1.00, 0.33, 0.8, 0.67];
$patch_array[3]['colors'][0] = [
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
$patch_array[3]['colors'][1] = [
'red' => 0,
'green' => 0,
'blue' => 1,
'alpha' => 1,
];
$coonspatchmesh3 = $pdf->graph->getCoonsPatchMesh(0, 0, 210, 297, $patch_array, 0, 2);
$pdf->page->addContent($coonspatchmesh3);
// =============================================================
// ----------
// get PDF document as raw string
$rawpdf = $pdf->getOutPDFString();
// ----------
// Various output modes:
//$pdf->savePDF(\dirname(__DIR__).'/target', $rawpdf);
$pdf->renderPDF($rawpdf);
//$pdf->downloadPDF($rawpdf);
//echo $pdf->getMIMEAttachmentPDF($rawpdf);