1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\IComparable; |
7
|
|
|
|
8
|
|
|
class Font extends Supervisor implements IComparable |
9
|
|
|
{ |
10
|
|
|
/* Underline types */ |
11
|
|
|
const UNDERLINE_NONE = 'none'; |
12
|
|
|
const UNDERLINE_DOUBLE = 'double'; |
13
|
|
|
const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting'; |
14
|
|
|
const UNDERLINE_SINGLE = 'single'; |
15
|
|
|
const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Font Name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'Calibri'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Font Size. |
26
|
|
|
* |
27
|
|
|
* @var float |
28
|
|
|
*/ |
29
|
|
|
protected $size = 11; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Bold. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $bold = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Italic. |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $italic = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Superscript. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $superscript = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Subscript. |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $subscript = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Underline. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $underline = self::UNDERLINE_NONE; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Strikethrough. |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
protected $strikethrough = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Foreground color. |
75
|
|
|
* |
76
|
|
|
* @var Color |
77
|
|
|
*/ |
78
|
|
|
protected $color; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create a new Font. |
82
|
|
|
* |
83
|
|
|
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
84
|
|
|
* Leave this value at default unless you understand exactly what |
85
|
|
|
* its ramifications are |
86
|
|
|
* @param bool $isConditional Flag indicating if this is a conditional style or not |
87
|
|
|
* Leave this value at default unless you understand exactly what |
88
|
|
|
* its ramifications are |
89
|
|
|
*/ |
90
|
28 |
|
public function __construct($isSupervisor = false, $isConditional = false) |
91
|
|
|
{ |
92
|
|
|
// Supervisor? |
93
|
28 |
|
parent::__construct($isSupervisor); |
94
|
|
|
|
95
|
|
|
// Initialise values |
96
|
28 |
|
if ($isConditional) { |
97
|
|
|
$this->name = null; |
98
|
|
|
$this->size = null; |
99
|
|
|
$this->bold = null; |
100
|
|
|
$this->italic = null; |
101
|
|
|
$this->superscript = null; |
102
|
|
|
$this->subscript = null; |
103
|
|
|
$this->underline = null; |
104
|
|
|
$this->strikethrough = null; |
105
|
|
|
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); |
106
|
|
|
} else { |
107
|
28 |
|
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor); |
108
|
|
|
} |
109
|
|
|
// bind parent if we are a supervisor |
110
|
28 |
|
if ($isSupervisor) { |
111
|
28 |
|
$this->color->bindParent($this, 'color'); |
112
|
|
|
} |
113
|
28 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the shared style component for the currently active cell in currently active sheet. |
117
|
|
|
* Only used for style supervisor. |
118
|
|
|
* |
119
|
|
|
* @return Font |
120
|
|
|
*/ |
121
|
|
|
public function getSharedComponent() |
122
|
|
|
{ |
123
|
|
|
return $this->parent->getSharedComponent()->getFont(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Build style array from subcomponents. |
128
|
|
|
* |
129
|
|
|
* @param array $array |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
2 |
|
public function getStyleArray($array) |
134
|
|
|
{ |
135
|
2 |
|
return ['font' => $array]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Apply styles from array. |
140
|
|
|
* <code> |
141
|
|
|
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray( |
142
|
|
|
* array( |
143
|
|
|
* 'name' => 'Arial', |
144
|
|
|
* 'bold' => TRUE, |
145
|
|
|
* 'italic' => FALSE, |
146
|
|
|
* 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE, |
147
|
|
|
* 'strikethrough' => FALSE, |
148
|
|
|
* 'color' => array( |
149
|
|
|
* 'rgb' => '808080' |
150
|
|
|
* ) |
151
|
|
|
* ) |
152
|
|
|
* ); |
153
|
|
|
* </code>. |
154
|
|
|
* |
155
|
|
|
* @param array $pStyles Array containing style information |
156
|
|
|
* |
157
|
|
|
* @throws PhpSpreadsheetException |
158
|
|
|
* |
159
|
|
|
* @return Font |
160
|
|
|
*/ |
161
|
3 |
|
public function applyFromArray(array $pStyles) |
162
|
|
|
{ |
163
|
3 |
|
if ($this->isSupervisor) { |
164
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
165
|
|
|
} else { |
166
|
3 |
|
if (isset($pStyles['name'])) { |
167
|
3 |
|
$this->setName($pStyles['name']); |
168
|
|
|
} |
169
|
3 |
|
if (isset($pStyles['bold'])) { |
170
|
3 |
|
$this->setBold($pStyles['bold']); |
171
|
|
|
} |
172
|
3 |
|
if (isset($pStyles['italic'])) { |
173
|
2 |
|
$this->setItalic($pStyles['italic']); |
174
|
|
|
} |
175
|
3 |
|
if (isset($pStyles['superscript'])) { |
176
|
|
|
$this->setSuperscript($pStyles['superscript']); |
177
|
|
|
} |
178
|
3 |
|
if (isset($pStyles['subscript'])) { |
179
|
|
|
$this->setSubscript($pStyles['subscript']); |
180
|
|
|
} |
181
|
3 |
|
if (isset($pStyles['underline'])) { |
182
|
3 |
|
$this->setUnderline($pStyles['underline']); |
183
|
|
|
} |
184
|
3 |
|
if (isset($pStyles['strikethrough'])) { |
185
|
|
|
$this->setStrikethrough($pStyles['strikethrough']); |
186
|
|
|
} |
187
|
3 |
|
if (isset($pStyles['color'])) { |
188
|
3 |
|
$this->getColor()->applyFromArray($pStyles['color']); |
189
|
|
|
} |
190
|
3 |
|
if (isset($pStyles['size'])) { |
191
|
3 |
|
$this->setSize($pStyles['size']); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get Name. |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
3 |
|
public function getName() |
204
|
|
|
{ |
205
|
3 |
|
if ($this->isSupervisor) { |
206
|
|
|
return $this->getSharedComponent()->getName(); |
207
|
|
|
} |
208
|
|
|
|
209
|
3 |
|
return $this->name; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set Name. |
214
|
|
|
* |
215
|
|
|
* @param string $pValue |
216
|
|
|
* |
217
|
|
|
* @return Font |
218
|
|
|
*/ |
219
|
4 |
View Code Duplication |
public function setName($pValue) |
|
|
|
|
220
|
|
|
{ |
221
|
4 |
|
if ($pValue == '') { |
222
|
|
|
$pValue = 'Calibri'; |
223
|
|
|
} |
224
|
4 |
|
if ($this->isSupervisor) { |
225
|
2 |
|
$styleArray = $this->getStyleArray(['name' => $pValue]); |
226
|
2 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
227
|
|
|
} else { |
228
|
4 |
|
$this->name = $pValue; |
229
|
|
|
} |
230
|
|
|
|
231
|
4 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get Size. |
236
|
|
|
* |
237
|
|
|
* @return float |
238
|
|
|
*/ |
239
|
3 |
|
public function getSize() |
240
|
|
|
{ |
241
|
3 |
|
if ($this->isSupervisor) { |
242
|
|
|
return $this->getSharedComponent()->getSize(); |
243
|
|
|
} |
244
|
|
|
|
245
|
3 |
|
return $this->size; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Set Size. |
250
|
|
|
* |
251
|
|
|
* @param float $pValue |
252
|
|
|
* |
253
|
|
|
* @return Font |
254
|
|
|
*/ |
255
|
4 |
View Code Duplication |
public function setSize($pValue) |
|
|
|
|
256
|
|
|
{ |
257
|
4 |
|
if ($pValue == '') { |
258
|
|
|
$pValue = 10; |
259
|
|
|
} |
260
|
4 |
|
if ($this->isSupervisor) { |
261
|
2 |
|
$styleArray = $this->getStyleArray(['size' => $pValue]); |
262
|
2 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
263
|
|
|
} else { |
264
|
4 |
|
$this->size = $pValue; |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
4 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Get Bold. |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
3 |
|
public function getBold() |
276
|
|
|
{ |
277
|
3 |
|
if ($this->isSupervisor) { |
278
|
|
|
return $this->getSharedComponent()->getBold(); |
279
|
|
|
} |
280
|
|
|
|
281
|
3 |
|
return $this->bold; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Set Bold. |
286
|
|
|
* |
287
|
|
|
* @param bool $pValue |
288
|
|
|
* |
289
|
|
|
* @return Font |
290
|
|
|
*/ |
291
|
4 |
|
public function setBold($pValue) |
292
|
|
|
{ |
293
|
4 |
|
if ($pValue == '') { |
294
|
|
|
$pValue = false; |
295
|
|
|
} |
296
|
4 |
|
if ($this->isSupervisor) { |
297
|
2 |
|
$styleArray = $this->getStyleArray(['bold' => $pValue]); |
298
|
2 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
299
|
|
|
} else { |
300
|
4 |
|
$this->bold = $pValue; |
301
|
|
|
} |
302
|
|
|
|
303
|
4 |
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get Italic. |
308
|
|
|
* |
309
|
|
|
* @return bool |
310
|
|
|
*/ |
311
|
3 |
|
public function getItalic() |
312
|
|
|
{ |
313
|
3 |
|
if ($this->isSupervisor) { |
314
|
|
|
return $this->getSharedComponent()->getItalic(); |
315
|
|
|
} |
316
|
|
|
|
317
|
3 |
|
return $this->italic; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Set Italic. |
322
|
|
|
* |
323
|
|
|
* @param bool $pValue |
324
|
|
|
* |
325
|
|
|
* @return Font |
326
|
|
|
*/ |
327
|
4 |
|
public function setItalic($pValue) |
328
|
|
|
{ |
329
|
4 |
|
if ($pValue == '') { |
330
|
|
|
$pValue = false; |
331
|
|
|
} |
332
|
4 |
|
if ($this->isSupervisor) { |
333
|
1 |
|
$styleArray = $this->getStyleArray(['italic' => $pValue]); |
334
|
1 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
335
|
|
|
} else { |
336
|
4 |
|
$this->italic = $pValue; |
337
|
|
|
} |
338
|
|
|
|
339
|
4 |
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get Superscript. |
344
|
|
|
* |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
1 |
|
public function getSuperscript() |
348
|
|
|
{ |
349
|
1 |
|
if ($this->isSupervisor) { |
350
|
|
|
return $this->getSharedComponent()->getSuperscript(); |
351
|
|
|
} |
352
|
|
|
|
353
|
1 |
|
return $this->superscript; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Set Superscript. |
358
|
|
|
* |
359
|
|
|
* @param bool $pValue |
360
|
|
|
* |
361
|
|
|
* @return Font |
362
|
|
|
*/ |
363
|
|
|
public function setSuperscript($pValue) |
364
|
|
|
{ |
365
|
|
|
if ($pValue == '') { |
366
|
|
|
$pValue = false; |
367
|
|
|
} |
368
|
|
|
if ($this->isSupervisor) { |
369
|
|
|
$styleArray = $this->getStyleArray(['superscript' => $pValue]); |
370
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
371
|
|
|
} else { |
372
|
|
|
$this->superscript = $pValue; |
373
|
|
|
$this->subscript = !$pValue; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Get Subscript. |
381
|
|
|
* |
382
|
|
|
* @return bool |
383
|
|
|
*/ |
384
|
1 |
|
public function getSubscript() |
385
|
|
|
{ |
386
|
1 |
|
if ($this->isSupervisor) { |
387
|
|
|
return $this->getSharedComponent()->getSubscript(); |
388
|
|
|
} |
389
|
|
|
|
390
|
1 |
|
return $this->subscript; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Set Subscript. |
395
|
|
|
* |
396
|
|
|
* @param bool $pValue |
397
|
|
|
* |
398
|
|
|
* @return Font |
399
|
|
|
*/ |
400
|
|
|
public function setSubscript($pValue) |
401
|
|
|
{ |
402
|
|
|
if ($pValue == '') { |
403
|
|
|
$pValue = false; |
404
|
|
|
} |
405
|
|
|
if ($this->isSupervisor) { |
406
|
|
|
$styleArray = $this->getStyleArray(['subscript' => $pValue]); |
407
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
408
|
|
|
} else { |
409
|
|
|
$this->subscript = $pValue; |
410
|
|
|
$this->superscript = !$pValue; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $this; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Get Underline. |
418
|
|
|
* |
419
|
|
|
* @return string |
420
|
|
|
*/ |
421
|
3 |
|
public function getUnderline() |
422
|
|
|
{ |
423
|
3 |
|
if ($this->isSupervisor) { |
424
|
|
|
return $this->getSharedComponent()->getUnderline(); |
425
|
|
|
} |
426
|
|
|
|
427
|
3 |
|
return $this->underline; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Set Underline. |
432
|
|
|
* |
433
|
|
|
* @param string|bool $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type |
434
|
|
|
* If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE, |
435
|
|
|
* false equates to UNDERLINE_NONE |
436
|
|
|
* |
437
|
|
|
* @return Font |
438
|
|
|
*/ |
439
|
3 |
View Code Duplication |
public function setUnderline($pValue) |
|
|
|
|
440
|
|
|
{ |
441
|
3 |
|
if (is_bool($pValue)) { |
442
|
|
|
$pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE; |
443
|
3 |
|
} elseif ($pValue == '') { |
444
|
|
|
$pValue = self::UNDERLINE_NONE; |
445
|
|
|
} |
446
|
3 |
|
if ($this->isSupervisor) { |
447
|
2 |
|
$styleArray = $this->getStyleArray(['underline' => $pValue]); |
448
|
2 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
449
|
|
|
} else { |
450
|
3 |
|
$this->underline = $pValue; |
451
|
|
|
} |
452
|
|
|
|
453
|
3 |
|
return $this; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Get Strikethrough. |
458
|
|
|
* |
459
|
|
|
* @return bool |
460
|
|
|
*/ |
461
|
1 |
|
public function getStrikethrough() |
462
|
|
|
{ |
463
|
1 |
|
if ($this->isSupervisor) { |
464
|
|
|
return $this->getSharedComponent()->getStrikethrough(); |
465
|
|
|
} |
466
|
|
|
|
467
|
1 |
|
return $this->strikethrough; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Set Strikethrough. |
472
|
|
|
* |
473
|
|
|
* @param bool $pValue |
474
|
|
|
* |
475
|
|
|
* @return Font |
476
|
|
|
*/ |
477
|
|
|
public function setStrikethrough($pValue) |
478
|
|
|
{ |
479
|
|
|
if ($pValue == '') { |
480
|
|
|
$pValue = false; |
481
|
|
|
} |
482
|
|
|
if ($this->isSupervisor) { |
483
|
|
|
$styleArray = $this->getStyleArray(['strike' => $pValue]); |
484
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
485
|
|
|
} else { |
486
|
|
|
$this->strikethrough = $pValue; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
return $this; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Get Color. |
494
|
|
|
* |
495
|
|
|
* @return Color |
496
|
|
|
*/ |
497
|
5 |
|
public function getColor() |
498
|
|
|
{ |
499
|
5 |
|
return $this->color; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Set Color. |
504
|
|
|
* |
505
|
|
|
* @param Color $pValue |
506
|
|
|
* |
507
|
|
|
* @throws PhpSpreadsheetException |
508
|
|
|
* |
509
|
|
|
* @return Font |
510
|
|
|
*/ |
511
|
2 |
View Code Duplication |
public function setColor(Color $pValue) |
|
|
|
|
512
|
|
|
{ |
513
|
|
|
// make sure parameter is a real color and not a supervisor |
514
|
2 |
|
$color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue; |
515
|
|
|
|
516
|
2 |
|
if ($this->isSupervisor) { |
517
|
1 |
|
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
518
|
1 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
519
|
|
|
} else { |
520
|
1 |
|
$this->color = $color; |
521
|
|
|
} |
522
|
|
|
|
523
|
2 |
|
return $this; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Get hash code. |
528
|
|
|
* |
529
|
|
|
* @return string Hash code |
530
|
|
|
*/ |
531
|
10 |
|
public function getHashCode() |
532
|
|
|
{ |
533
|
10 |
|
if ($this->isSupervisor) { |
534
|
|
|
return $this->getSharedComponent()->getHashCode(); |
535
|
|
|
} |
536
|
|
|
|
537
|
10 |
|
return md5( |
538
|
10 |
|
$this->name . |
539
|
10 |
|
$this->size . |
540
|
10 |
|
($this->bold ? 't' : 'f') . |
541
|
10 |
|
($this->italic ? 't' : 'f') . |
542
|
10 |
|
($this->superscript ? 't' : 'f') . |
543
|
10 |
|
($this->subscript ? 't' : 'f') . |
544
|
10 |
|
$this->underline . |
545
|
10 |
|
($this->strikethrough ? 't' : 'f') . |
546
|
10 |
|
$this->color->getHashCode() . |
547
|
10 |
|
__CLASS__ |
548
|
|
|
); |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.