Element.php
6.43 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/*
PHPRtfLite
Copyright 2010-2012 Steffen Zeidler <sigma_z@sigma-scripts.de>
This file is part of PHPRtfLite.
PHPRtfLite is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PHPRtfLite is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PHPRtfLite. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* class for creating elements used in containers like sections, footers and headers.
* @version 1.2
* @author Steffen Zeidler <sigma_z@sigma-scripts.de>
* @copyright 2010-2012 Steffen Zeidler
* @package PHPRtfLite
* @subpackage PHPRtfLite_Element
*/
class PHPRtfLite_Element
{
/**
* @var string
*/
protected $_text = '';
/**
* @var boolean
*/
protected $_isRtfCode = false;
/**
* @var boolean
*/
protected $_convertTagsToRtf = false;
/**
* @var PHPRtfLite_Font
*/
protected $_font;
/**
* @var PHPRtfLite_ParFormat
*/
protected $_parFormat;
/**
* constructor
*
* @param PHPRtfLite $rtf
* @param string $text
* @param PHPRtfLite_Font $font
* @param PHPRtfLite_ParFormat $parFormat
*/
public function __construct(PHPRtfLite $rtf,
$text,
PHPRtfLite_Font $font = null,
PHPRtfLite_ParFormat $parFormat = null)
{
if ($font) {
$rtf->registerFont($font);
}
if ($parFormat) {
$rtf->registerParFormat($parFormat);
}
$this->_rtf = $rtf;
$this->_text = $text;
$this->_font = $font;
$this->_parFormat = $parFormat;
}
/**
* checks, if element is an empty paragraph
*
* @return boolean
*/
public function isEmptyParagraph()
{
return ($this->_parFormat && $this->_text == '\\par' && $this->_isRtfCode);
}
/**
* sets flag, that text tags shall be converted into rtf code
*/
public function setConvertTagsToRtf()
{
$this->_convertTagsToRtf = true;
}
/**
* sets rtf code
*/
public function setIsRtfCode()
{
$this->_isRtfCode = true;
}
/**
* returns true, if text is rtf code
*
* @return boolean
*/
public function isRtfCode()
{
return $this->_isRtfCode;
}
/**
* gets font
*
* @return PHPRtfLite_Font
*/
public function getFont()
{
return $this->_font;
}
/**
* gets par format
*
* @return PHPRtfLite_ParFormat
*/
public function getParFormat()
{
return $this->_parFormat;
}
/**
* converts text tags into rtf code
*
* @param string $text
* @param string $charset
* @return string
*/
public static function convertTagsToRtf($text, $charset)
{
$search = array(
// bold
'|<STRONG\s*>(.*?)</STRONG\s*>|smi',
'|<B\s*>(.*?)</B\s*>|smi',
// italic
'|<EM\s*>(.*?)</EM\s*>|smi',
'|<I\s*>(.*?)</I\s*>|smi',
// underline
'|<U\s*>(.*?)</U\s*>|smi',
// break
'|<BR\s*(/)?\s*>|smi',
'|<LINE\s*(/)?\s*>|smi',
// horizontal rule
'|<HR\s*(/)?\s*>|smi',
'|<CHDATE\s*(/)?\s*>|smi',
'|<CHDPL\s*(/)?\s*>|smi',
'|<CHDPA\s*(/)?\s*>|smi',
'|<CHTIME\s*(/)?\s*>|smi',
'|<CHPGN\s*(/)?\s*>|smi',
// tab
'|<TAB\s*(/)?\s*>|smi',
// bullet
'|<BULLET\s*(/)?\s*>|smi',
'|<PAGENUM\s*(/)?\s*>|smi',
'|<PAGETOTAL\s*(/)?\s*>|smi',
'|<SECTNUM\s*(/)?\s*>|smi',
'|<SUP\s*>(.*?)</SUP\s*>|smi',
'|<SUB\s*>(.*?)</SUB\s*>|smi',
// '|<PAGE\s*(/)?\s*>|smi',
// '|<SECT\s*(/)?\s*>|smi'
);
$replace = array(
// bold
'\b \1\b0 ',
'\b \1\b0 ',
// italic
'\i \1\i0 ',
'\i \1\i0 ',
// underline
'\ul \1\ul0 ',
// break
'\line ',
'\line ',
// horizontal rule
'{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}',
'\chdate ',
'\chdpl ',
'\chdpa ',
'\chtime ',
'\chpgn ',
// tab
'\tab ',
// bullet
'\bullet ',
'\chpgn ',
'{\field {\*\fldinst {NUMPAGES }}}',
'\sectnum ',
'{\super \1\super0}',
'{\sub \1\sub0}',
// '\page ',
// '\sect '
);
$text = preg_replace($search, $replace, $text);
$text = html_entity_decode($text, ENT_COMPAT, $charset);
return $text;
}
/**
* gets opening token
*
* @return string
*/
protected function getOpeningToken()
{
return '{';
}
/**
* gets closing token
*
* @return string
*/
protected function getClosingToken()
{
return '}';
}
/**
* renders the element
*/
public function render()
{
$stream = $this->_rtf->getWriter();
$text = $this->_text;
if (!$this->_isRtfCode) {
$charset = $this->_rtf->getCharset();
$text = PHPRtfLite::quoteRtfCode($text);
if ($this->_convertTagsToRtf) {
$text = self::convertTagsToRtf($text, $charset);
}
$text = PHPRtfLite_Utf8::getUnicodeEntities($text, $charset);
}
$stream->write($this->getOpeningToken());
if ($this->_font) {
$stream->write($this->_font->getContent());
}
if (!$this->isEmptyParagraph() || $this->_isRtfCode) {
$stream->write($text);
}
$stream->write($this->getClosingToken() . "\r\n");
}
}