Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23 liveuser 1
<?php
2
 
3
//require 'class.style.php';
4
//require 'exceptions/class.styleexception.php';
5
 
6
include_once 'phpodt.php';
7
 
8
/**
9
 * A Class representing style properties for paragraphs.
10
 *
11
 * @author Issam RACHDI
12
 */
13
 
14
class ParagraphStyle extends Style {
15
 
16
	public function __construct($name) {
17
    if (empty($name)) {
18
      $name = 'paragraphstyle'.rand(100, 9999999);
19
    }
20
		parent::__construct($name);
21
		$this->styleElement->setAttribute('style:family', 'paragraph');
22
	}
23
 
24
	/**
25
	 * Specifies a fixed line height either as a length or a percentage that 
26
	 * relates to the highest character in a line. The value StyleConstants::NORMAL 
27
	 * activates the default line height calculation
28
	 *
29
	 * @param length|percentage|StyleConstants::NORMAL $lineHeight
30
	 */
31
	function setLineHeight($lineHeight) {
32
		if (!isNumeric($lineHeight) && !isPercentage($lineHeight) && isLengthValue($lineHeight)) {
33
			if ($lineHeight == StyleConstants::NORMAL) {
34
				$lineHeight = 'normal';
35
			} else {
36
				throw new StyleException('Invalid line-height value.');
37
			}
38
		}
39
		$element = $this->styleDocument->createElement('style:paragraph-properties');
40
		$element->setAttribute('fo:line-height', $lineHeight);
41
		$this->styleElement->appendChild($element);
42
	}
43
 
44
	/**
45
	 * Specifies a fixed distance between two lines.
46
	 *
47
	 * @param length $distance
48
	 */
49
	function setLineDistance($distance) {
50
		if (!isNumeric($distance) && isLengthValue($distance)) {
51
			throw new StyleException('Invalid line-spacing value.');
52
		}
53
		$element = $this->styleDocument->createElement('style:paragraph-properties');
54
		$element->setAttribute('style:line-spacing', $distance);
55
		$this->styleElement->appendChild($element);
56
	}
57
 
58
	/**
59
	 * Specifies how to align text in paragraphs
60
	 *
61
	 * @param integer $textAlign Valid values are StyleConstants::(START, END, LEFT, RIGHT, CENTER, JUSTIFY)
62
	 */
63
	function setTextAlign($textAlign) {
64
		switch ($textAlign) {
65
			case StyleConstants::START:
66
				$textAlign = 'start';break;
67
			case StyleConstants::END:
68
				$textAlign = 'end';break;
69
			case StyleConstants::LEFT:
70
				$textAlign = 'left';break;
71
			case StyleConstants::RIGHT:
72
				$textAlign = 'right';break;
73
			case StyleConstants::CENTER:
74
				$textAlign = 'center';break;
75
			case StyleConstants::JUSTIFY:
76
				$textAlign = 'justify';break;
77
			default:
78
				throw new StyleException('Invalid text-align value.');
79
		}
80
		$element = $this->styleDocument->createElement('style:paragraph-properties');
81
		$element->setAttribute('fo:text-align', $textAlign);
82
		$this->styleElement->appendChild($element);
83
	}
84
 
85
	/**
86
	 * Specifies whether the lines of a paragraph should be kept together on
87
	 * the same page or column (if the value is ALWAYS),
88
	 * or whether breaks are allowed within the paragraph (if the value is AUTO)
89
	 *
90
	 * @param integer $keepTogether Valid values are StyleConstants::(AUTO, ALWAYS)
91
	 */
92
	function setKeepTogether($keepTogether = StyleConstants::ALWAYS) {
93
		switch ($keepTogether) {
94
			case StyleConstants::ALWAYS:
95
				$keepTogether = 'always';break;
96
			case StyleConstants::AUTO:
97
				$keepTogether = 'auto';break;
98
			default:
99
				throw new StyleException('Invalid keep-together value.');
100
		}
101
		$element = $this->styleDocument->createElement('style:paragraph-properties');
102
		$element->setAttribute('fo:keep-together', $keepTogether);
103
		$this->styleElement->appendChild($element);
104
	}
105
 
106
	/**
107
	 * Specifies the minimum number of lines allowed at the top of a page to avoid paragraph widows.
108
	 *
109
	 * @param integer $widows Minimum number of lines
110
	 */
111
	function setWidows($widows) {
112
		if (!isNumeric($widows)) {
113
			throw new StyleException('Invalid widows value.');
114
		}
115
		$element = $this->styleDocument->createElement('style:paragraph-properties');
116
		$element->setAttribute('fo:widows', $widows);
117
		$this->styleElement->appendChild($element);
118
	}
119
 
120
	/**
121
	 * Specifies the minimum number of lines required at the bottom of a page to avoid paragraph orphans.
122
	 *
123
	 * @param integer $orphans Minimum number of lines
124
	 */
125
	function setOrphans($orphans) {
126
		if (!isNumeric($orphans)) {
127
			throw new StyleException('Invalid orphans value.');
128
		}
129
		$element = $this->styleDocument->createElement('style:paragraph-properties');
130
		$element->setAttribute('fo:orphans', $orphans);
131
		$this->styleElement->appendChild($element);
132
	}
133
 
134
	/**
135
	 * Specifies the left & right margin for a paragraph
136
	 *
137
	 * @param integer|string $leftMargin
138
	 * @param integer|string $rightMargin
139
	 */
140
	function setHorizontalMargins($leftMargin = 0, $rightMargin = 0) {
141
		if (!isNumeric($leftMargin) && !isLengthValue($leftMargin)) {
142
			throw new StyleException('Invalid left-margin value');
143
		}
144
		if (!isNumeric($rightMargin) && !isLengthValue($rightMargin)) {
145
			throw new StyleException('Invalid right-margin value');
146
		}
147
		$element = $this->styleDocument->createElement('style:paragraph-properties');
148
		$element->setAttribute('fo:margin-left', $leftMargin);
149
		$element->setAttribute('fo:margin-right', $rightMargin);
150
		$this->styleElement->appendChild($element);
151
	}
152
 
153
	/**
154
	 * Specifies the top & bottom margin for a paragraph
155
	 *
156
	 * @param integer|string $topMargin
157
	 * @param integer|string $bottomMargin
158
	 */
159
	function setVerticalMargin($topMargin, $bottomMargin) {
160
		if (!isNumeric($topMargin, true) && !isLengthValue($topMargin, true)) {
161
			throw new StyleException('Invalid top-margin value');
162
		}
163
		if (!isNumeric($bottomMargin) && !isLengthValue($bottomMargin)) {
164
			throw new StyleException('Invalid bottom-margin value');
165
		}
166
		$element = $this->styleDocument->createElement('style:paragraph-properties');
167
		$element->setAttribute('fo:margin-top', $topMargin);
168
		$element->setAttribute('fo:margin-bottom', $bottomMargin);
169
		$this->styleElement->appendChild($element);
170
	}
171
 
172
	/**
173
	 * Insert a page or column break before a paragraph
174
	 *
175
	 * @param integer $breakBefore Valid values are StyleConstants::(PAGE|COLUMN)
176
	 */
177
	function setBreakBefore($breakBefore) {
178
		switch ($breakBefore) {
179
			case StyleConstants::PAGE:
180
				$breakBefore = 'page';break;
181
			case StyleConstants::COLUMN:
182
				$breakBefore = 'column';break;
183
			default:
184
				throw new StyleException('Invalid break-before value.');
185
		}
186
		$element = $this->styleDocument->createElement('style:paragraph-properties');
187
		$element->setAttribute('fo:break-before', $breakBefore);
188
		$this->styleElement->appendChild($element);
189
	}
190
 
191
	/**
192
	 * Insert a page or column break after a paragraph
193
	 *
194
	 * @param integer $breakAfter Valid values are StyleConstants::(PAGE|COLUMN)
195
	 */
196
	function setBreakAfter($breakAfter) {
197
		switch ($breakAfter) {
198
			case StyleConstants::PAGE:
199
				$breakAfter = 'page';break;
200
			case StyleConstants::COLUMN:
201
				$breakAfter = 'column';break;
202
			default:
203
				throw new StyleException('Invalid break-after value.');
204
		}
205
		$element = $this->styleDocument->createElement('style:paragraph-properties');
206
		$element->setAttribute('fo:break-after', $breakAfter);
207
		$this->styleElement->appendChild($element);
208
	}
209
 
210
	/**
211
	 * Specifies the background color for the paragraph. The color format
212
	 * must be #XXYYZZ where XX is the red, YY the green and ZZ the blue, in hexadecimal.
213
	 *
214
	 * @param color $color
215
	 */
216
	function setBackgroundColor($color) {
217
		if ($color != StyleConstants::TRANSPARENT && !isColor($color)) {
218
			throw new StyleException('Invalid paragraph background color');
219
		}
220
		$element = $this->styleDocument->createElement('style:paragraph-properties');
221
		$element->setAttribute('fo:background-color', $color);
222
		$this->styleElement->appendChild($element);
223
	}
224
 
225
	/**
226
	 * Specifies a background image for a paragraph. Note that if you specify the position, the image
227
	 * will not be repeated
228
	 *
229
	 * @param string $image The image's path.
230
	 * @param integer $position Specifies where to position a background image in a paragraph.
231
	 * Valid values are StyleConstants::(LEFT|RIGHT|CENTER|TOP|BOTTOM)
232
	 */
233
	function setBackgroundImage($image, $repeat = StyleConstants::REPEAT,
234
								$position = -1) {
235
		$file = fopen($image, 'r');
236
		if (!$file) {
237
			throw new StyleException('Cannot open image');
238
		}
239
		switch($repeat) {
240
			case StyleConstants::REPEAT:
241
				$repeat = 'repeat';break;
242
			case StyleConstants::NO_REPEAT:
243
				$repeat = 'no-repeat';break;
244
			case StyleConstants::STRETCH:
245
				$repeat = 'stretch';break;
246
			default:
247
				throw new StyleException('Invalid repeat value');
248
		}
249
		switch($position) {
250
			case -1:
251
				break;
252
			case StyleConstants::LEFT:
253
				$position = 'left';break;
254
			case StyleConstants::RIGHT:
255
				$position = 'right';break;
256
			case StyleConstants::CENTER:
257
				$position = 'center';break;
258
			case StyleConstants::TOP:
259
				$position = 'top';break;
260
			case StyleConstants::BOTTOM:
261
				$position = 'left';break;
262
			default:
263
				throw new StyleException('Invalid background-position value');
264
		}
265
		$dataImg = fread($file, filesize($image));
266
		$dateImgB64 = base64_encode($dataImg);
267
		fclose($file);
268
		$pPropElement = $this->styleDocument->createElement('style:paragraph-properties');
269
		$binaryElement = $this->styleDocument->createElement('office:binary-data', $dateImgB64);
270
		$imageElement = $this->styleDocument->createElement('style:background-image');
271
		$imageElement->setAttribute('style:repeat', $repeat);
272
		if ($position != -1) {
273
			$imageElement->setAttribute('style:position', $position);
274
		}
275
		$imageElement->appendChild($binaryElement);
276
		$pPropElement->appendChild($imageElement);
277
		$this->styleElement->appendChild($pPropElement);
278
	}
279
 
280
	/**
281
	 * Specifies a positive or negative indent for the first line of a paragraph
282
	 *
283
	 * @param length $textIndent
284
	 */
285
	function setTextIndent($textIndent) {
286
		if (!isNumeric($textIndent) && !isLengthValue($textIndent)) {
287
			throw new StyleException('Invalid text-indent value');
288
		}
289
		$element = $this->styleDocument->createElement('style:paragraph-properties');
290
		$element->setAttribute('fo:text-indent', $textIndent);
291
		$this->styleElement->appendChild($element);
292
	}
293
 
294
	/**
295
	 * Specifies the border properties for paragraphs.
296
	 *
297
	 * @param color $borderColor Border color
298
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
299
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
300
	 * @param string $position Do not use this, it's for internal use only.
301
	 */
302
	function setBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
303
					   $borderWidth = StyleConstants::THIN, $position = '') {
304
		if (!isColor($borderColor)) {
305
			throw new StyleException('Invalid border-color value');
306
		}
307
 
308
		switch ($borderStyle) {
309
			case StyleConstants::SOLID:
310
				$borderStyle = 'solid';break;
311
			case StyleConstants::DOUBLE:
312
				$borderStyle = 'double';break;
313
			default:
314
				throw new StyleException('Invalid border-style value');
315
		}
316
		switch ($borderWidth) {
317
			case StyleConstants::THIN:
318
				$borderWidth = 'thin';break;
319
			case StyleConstants::THICK:
320
				$borderWidth = 'thick';break;
321
			case StyleConstants::MEDIUM:
322
				$borderWidth = 'medium';break;
323
			default:
324
				if (!isLengthValue($borderWidth, true)) {
325
					throw new StyleException('Invalid border-width value');
326
				}
327
		}
328
		if (!empty($position)) {
329
			if (!in_array($position, array('top', 'bottom', 'left', 'right'))) {
330
				$position = '';
331
			} else {
332
				$position = '-'.$position;
333
			}
334
		}
335
		$element = $this->styleDocument->createElement('style:paragraph-properties');
336
		$element->setAttribute('fo:border'.$position, "$borderWidth $borderStyle $borderColor");
337
		$this->styleElement->appendChild($element);
338
	}
339
 
340
	/**
341
	 * Specifies the top border properties for paragraphs.
342
	 *
343
	 * @param color $borderColor Border color
344
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
345
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
346
	 */
347
	function setTopBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
348
					   $borderWidth = StyleConstants::THIN) {
349
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'top');
350
	}
351
 
352
	/**
353
	 * Specifies the bottom border properties for paragraphs.
354
	 *
355
	 * @param color $borderColor Border color
356
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
357
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
358
	 */
359
	function setBottomBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
360
					   $borderWidth = StyleConstants::THIN) {
361
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'bottom');
362
	}
363
 
364
	/**
365
	 * Specifies the left border properties for paragraphs.
366
	 *
367
	 * @param color $borderColor Border color
368
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
369
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
370
	 */
371
	function setLeftBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
372
					   $borderWidth = StyleConstants::THIN) {
373
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'left');
374
	}
375
 
376
	/**
377
	 * Specifies the right border properties for paragraphs.
378
	 *
379
	 * @param color $borderColor Border color
380
	 * @param int $borderStyle Valid values: StyleConstants::(SOLID|DOUBLE)
381
	 * @param int|length $borderWidth Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM)
382
	 */
383
	function setRightBorder($borderColor = '#000000', $borderStyle = StyleConstants::SOLID,
384
					   $borderWidth = StyleConstants::THIN) {
385
		$this->setBorder($borderColor, $borderStyle, $borderWidth, 'right');
386
	}
387
 
388
	/**
389
	 * Specifies the spacing around a paragraph.
390
	 *
391
	 * @param length $padding
392
	 * @param string $position Do not use this, it's for internal use only.
393
	 */
394
	function setPadding($padding, $position = '') {
395
		if (!isLengthValue($padding, true) && !isNumeric($padding)) {
396
			throw new StyleException('Invalid padding value');
397
		}
398
		if (!empty($position)) {
399
			if (!in_array($position, array('top', 'bottom', 'left', 'right'))) {
400
				$position = '';
401
			} else {
402
				$position = '-'.$position;
403
			}
404
		}
405
		$element = $this->styleDocument->createElement('style:paragraph-properties');
406
		$element->setAttribute('fo:padding'.$position, $padding);
407
		$this->styleElement->appendChild($element);
408
	}
409
 
410
	/**
411
	 * Specifies the spacing on top of a paragraph.
412
	 *
413
	 * @param length $padding
414
	 */
415
	function setTopPadding($padding) {
416
		$this->setPadding($padding, 'top');
417
	}
418
 
419
	/**
420
	 * Specifies the spacing in the bottom of a paragraph.
421
	 *
422
	 * @param length $padding
423
	 */
424
	function setBottomPadding($padding) {
425
		$this->setPadding($padding, 'bottom');
426
	}
427
 
428
	/**
429
	 * Specifies the spacing in the left side of a paragraph.
430
	 *
431
	 * @param length $padding
432
	 */
433
	function setLeftPadding($padding) {
434
		$this->setPadding($padding, 'left');
435
	}
436
 
437
	/**
438
	 * Specifies the spacing in the right side of a paragraph.
439
	 *
440
	 * @param length $padding
441
	 */
442
	function setRightPadding($padding) {
443
		$this->setPadding($padding, 'right');
444
	}
445
 
446
	/**
447
	 * Keeps the current paragraph and the next paragraph together on a page or in a column after a break is inserted.
448
	 */
449
	function setKeepWithNext() {
450
		$element = $this->styleDocument->createElement('style:paragraph-properties');
451
		$element->setAttribute('fo:keep-with-next', 'always');
452
		$this->styleElement->appendChild($element);
453
	}
454
 
455
//	function setLineNumebring() {
456
//		$element = $this->styleDocument->createElement('style:paragraph-properties');
457
//		$element->setAttribute('text:number-lines', 'true');
458
//		$this->styleElement->appendChild($element);
459
//	}
460
 
461
	/**
462
	 * Specifies the vertical position of a character. By default characters are aligned according to their baseline.
463
	 *
464
	 * @param integer $align Valid values: StyleConstants::(TOP|BOTTOM|MIDDLE|BASELINE|AUTO)
465
	 */
466
	function setVerticalAlignment($align) {
467
		switch ($align) {
468
			case StyleConstants::TOP:
469
				$align = 'top';break;
470
			case StyleConstants::BOTTOM:
471
				$align = 'bottom';break;
472
			case StyleConstants::MIDDLE:
473
				$align = 'middle';break;
474
			case StyleConstants::BASELINE:
475
				$align = 'baseline';break;
476
			case StyleConstants::AUTO:
477
				$align = 'auto';break;
478
			default:
479
				throw new StyleException('Invalid vertical-align value');
480
		}
481
		$element = $this->styleDocument->createElement('style:paragraph-properties');
482
		$element->setAttribute('style:vertical-align', $align);
483
		$this->styleElement->appendChild($element);
484
	}
485
 
486
	/**
487
	 * Specifies the writing mode of a paragraph.
488
	 * @param integer $writingMode Valid values: StyleConstants::(LR_TB|RL_TB|TB_RL|TB_LR|RL|TB|PAGE)
489
	 */
490
	function setWritingMode($writingMode) {
491
		switch ($writingMode) {
492
			case StyleConstants::LR_TB:
493
				$writingMode = 'lr-tb';break;
494
			case StyleConstants::RL_TB:
495
				$writingMode = 'rl-tb';break;
496
			case StyleConstants::TB_RL:
497
				$writingMode = 'tb-rl';break;
498
			case StyleConstants::TB_LR:
499
				$writingMode = 'tb-lr';break;
500
			case StyleConstants::RL:
501
				$writingMode = 'rl';break;
502
			case StyleConstants::TB:
503
				$writingMode = 'tb';break;
504
			case StyleConstants::PAGE:
505
				$writingMode = 'page';break;
506
			default:
507
				throw new StyleException('Invalid writing-mode value');
508
		}
509
		$element = $this->styleDocument->createElement('style:paragraph-properties');
510
		$element->setAttribute('style:writing-mode', $writingMode);
511
		$this->styleElement->appendChild($element);
512
	}
513
}
514
?>