Blame view

export/PHPRtfLite-1.3.1/lib/PHPRtfLite/List.php 6.43 KB
f90e19c3   Andrea Petta   plugin update
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
  <?php
  /*
      PHPRtfLite
      Copyright 2007-2008 Denis Slaveckij <sinedas@gmail.com>
      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/>.
  */
  
  /**
   * Abstract class for rtf lists (numberings and enumerations).
   * @version     1.2
   * @author      Denis Slaveckij <sinedas@gmail.com>
   * @author      Steffen Zeidler <sigma_z@sigma-scripts.de>
   * @copyright   2007-2008 Denis Slaveckij, 2010-2012 Steffen Zeidler
   * @package     PHPRtfLite
   * @subpackage  PHPRtfLite_List
   */
  abstract class PHPRtfLite_List
  {
  
      /**
       * @var PHPRtfLite
       */
      protected $_rtf;
  
      /**
       * type of list
       * @var integer
       */
      protected $_type;
  
      /**
       * @var PHPRtfLite_Font
       */
      protected $_font;
  
      /**
       * @var PHPRtfLite_ParFormat
       */
      protected $_parFormat;
  
      /**
       * indent to list character in centimeter
       * @var float
       */
      protected $_listIndent = PHPRtfLite_Unit::UNIT_CM;
  
      /**
       * indent to text in centimeter
       * @var float
       */
      protected $_textIndent = PHPRtfLite_Unit::UNIT_CM;
  
      /**
       * @var array
       */
      protected $_items = array();
  
  
      /**
       * gets list character
       *
       * @param   integer $number
       * @return  string
       */
      abstract protected function getListCharacter($number);
  
  
      /**
       * constructor
       *
       * @param   PHPRtfLite              $rtf
       * @param   integer                 $type
       * @param   PHPRtfLite_Font         $font
       * @param   PHPRtfLite_ParFormat    $parFormat
       */
      public function __construct(PHPRtfLite $rtf, $type = null,
                                  PHPRtfLite_Font $font = null, PHPRtfLite_ParFormat $parFormat = null)
      {
          $this->_rtf         = $rtf;
          $this->_type        = $type;
          $this->_font        = $font;
          $this->_parFormat   = $parFormat;
      }
  
  
      /**
       * adds item to list
       *
       * @param   string                  $text
       * @param   PHPRtfLite_Font         $font
       * @param   PHPRtfLite_ParFormat    $parFormat
       * @param   boolean                 $convertTagsToRtf
       * @return  PHPRtfLite_List
       */
      public function addItem($text,
              PHPRtfLite_Font $font = null,
              PHPRtfLite_ParFormat $parFormat = null,
              $convertTagsToRtf = true)
      {
          if ($font === null) {
              $font = $this->_font;
          }
          if ($parFormat === null) {
              $parFormat = $this->_parFormat;
          }
  
          $element = new PHPRtfLite_Element($this->_rtf, $text, $font, $parFormat);
          if ($convertTagsToRtf) {
              $element->setConvertTagsToRtf();
          }
          $this->_items[] = $element;
  
          return $this;
      }
  
  
      /**
       * adds element to list
       *
       * @param   PHPRtfLite_Element  $element
       * @return  PHPRtfLite_List
       */
      public function addElement(PHPRtfLite_Element $element)
      {
          $this->_items[] = $element;
          return $this;
      }
  
  
      /**
       * adds list to list
       *
       * @param   PHPRtfLite_List  $element
       * @return  PHPRtfLite_List
       */
      public function addList(PHPRtfLite_List $list)
      {
          $this->_items[] = $list;
          $list->_textIndent += $this->_textIndent;
          return $this;
      }
  
  
      /**
       * sets list indent
       *
       * @param   float   $indent
       */
      public function setListIndent($indent)
      {
          $this->_listIndent = PHPRtfLite_Unit::getUnitInTwips($indent);
      }
  
  
      /**
       * sets text indent
       *
       * @param   float   $indent
       */
      public function setTextIndent($indent)
      {
          $this->_textIndent = PHPRtfLite_Unit::getUnitInTwips($indent);
      }
  
  
      /**
       * gets font
       *
       * @return PHPRtfLite_Font
       */
      public function getFont()
      {
          return $this->_font;
      }
  
  
      /**
       * gets par format
       *
       * @return PHPRtfLite_ParFormat
       */
      public function getParFormat()
      {
          return $this->_parFormat;
      }
  
  
      /**
       * default implementation for getting font index for list character
       *
       * @return string
       */
      protected function getListCharFontIndex()
      {
          return '0';
      }
  
  
      /**
       * renders list
       */
      public function render()
      {
          $stream = $this->_rtf->getWriter();
          $number = 0;
  
          foreach ($this->_items as $item) {
              // item is a list
              if ($item instanceof PHPRtfLite_List) {
                  if ($this instanceof PHPRtfLite_List_Numbering) {
                      $item->setPrefix($this->_prefix . $this->getNumber($number) . $this->_separator);
                      $item->setSuffix($this->_suffix);
                  }
              }
              // item is a element
              else {
                  $number++;
                  $listCharFontIndex  = $this->getListCharFontIndex();
                  $listCharacter      = $this->getListCharacter($number);
                  $listCharDefinition = '{\*\pn\pnlvlblt' . '\pnf' . $listCharFontIndex;
                  if ($this->_font) {
                      $listCharDefinition .= '\pnfs' . ($this->_font->getSize() * 2);
                      if (($color = $this->_font->getColor())) {
                          $listCharDefinition .= '\pncf' . $this->_rtf->getColorTable()->getColorIndex($color);
                      }
                  }
                  $listCharDefinition .= '\pnindent0{\pntxtb ' . $listCharacter . '}}';
  
                  $textIndent = $this->_listIndent + $this->_textIndent;
                  $stream->write('\nowidctlpar\fi-' . $this->_listIndent . '\li' . $textIndent . "\r\n");
                  $stream->write($listCharDefinition);
              }
  
              // renders item
              $item->render();
  
              if (false == ($item instanceof PHPRtfLite_List)) {
                  $stream->write('\par\pard' . "\r\n");
              }
          }
      }
  
  }