File size: 6,818 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
// $Header: /cvsroot/html2ps/box.list-item.php,v 1.34 2006/09/07 18:38:12 Konstantin Exp $

class ListItemBox extends BlockBox {
  var $size;

  function &create(&$root, &$pipeline) {
    $box = new ListItemBox($root, $pipeline);
    $box->readCSS($pipeline->getCurrentCSSState());

    /**
     * Create text box containing item number
     */
    $css_state =& $pipeline->getCurrentCSSState();
    $css_state->pushState();
    $css_state->setProperty(CSS_COLOR, CSSColor::parse('transparent'));

    $list_style = $css_state->getProperty(CSS_LIST_STYLE);
    $box->str_number_box = TextBox::create(CSSListStyleType::format_number($list_style->type,
                                                                           $css_state->getProperty(CSS_HTML2PS_LIST_COUNTER)).". ", 
                                            'iso-8859-1', 
                                            $pipeline);
    $box->str_number_box->baseline = $box->str_number_box->default_baseline;

    $css_state->popState();

    /**
     * Create nested items
     */
    $box->create_content($root, $pipeline);

    return $box;
  }

  function readCSS(&$state) {
    parent::readCSS($state);
    
    $this->_readCSS($state,
                    array(CSS_LIST_STYLE));

    // Pseudo-CSS properties
    // '-list-counter'

    // increase counter value
    $value = $state->getProperty(CSS_HTML2PS_LIST_COUNTER) + 1;
    $state->setProperty(CSS_HTML2PS_LIST_COUNTER, $value);
    $state->setPropertyOnLevel(CSS_HTML2PS_LIST_COUNTER, CSS_PROPERTY_LEVEL_PARENT, $value);

    // open the marker image if specified
    $list_style = $this->getCSSProperty(CSS_LIST_STYLE);

    if (!$list_style->image->is_default()) {
      $this->marker_image = new ImgBox($list_style->image->_image);
      $state->pushDefaultState();
      $this->marker_image->readCSS($state);
      $state->popState();
      $this->marker_image->_setupSize();
    } else {
      $this->marker_image = null;
    };
  }

  function ListItemBox(&$root, &$pipeline) {
    // Call parent constructor
    $this->BlockBox($root);
  }

  function reflow(&$parent, &$context) {
    $list_style = $this->getCSSProperty(CSS_LIST_STYLE);

    // If list-style-position is inside, we'll need to move marker box inside the 
    // list-item box and offset all content by its size;
    if ($list_style->position === LSP_INSIDE) {
      // Add marker box width to text-indent value
      $this->_additional_text_indent = $this->get_marker_box_width();
    };

    // Procees with normal block box flow algorithm
    BlockBox::reflow($parent, $context);
  }

  function reflow_text(&$driver) {
    if (is_null($this->str_number_box->reflow_text($driver))) {
      return null;
    };

    return GenericContainerBox::reflow_text($driver);
  }
  
  function show(&$viewport) {
    // draw generic block box
    if (is_null(BlockBox::show($viewport))) {
      return null;
    };

    // Draw marker 
    /**
     * Determine the marker box base X coordinate 
     * If possible, the marker box should be drawn immediately to the left of the first word in this 
     * box; this means that marker should be tied to the first text box, not to the left 
     * edge of the list block box
     */
    $child = $this->get_first_data();
    if (is_null($child)) {
      $x = $this->get_left(); 

      $list_style = $this->getCSSProperty(CSS_LIST_STYLE);

      // If list-style-position is inside, we'll need to move marker box inside the 
      // list-item box and offset all content by its size;
      if ($list_style->position === LSP_INSIDE) {
        $x += $this->get_marker_box_width();
      };
    } else {
      $x = $child->get_left();
    };

    // Determine the base Y coordinate of marker box
    $element = $this->get_first_data();

    if ($element) {
      $y = $element->get_top() - $element->default_baseline;
    } else {
      $y = $this->get_top();
    }

    if (!is_null($this->marker_image)) {
      $this->mb_image($viewport, $x, $y);
    } else {
      $list_style = $this->getCSSProperty(CSS_LIST_STYLE);

      switch ($list_style->type) {
      case LST_NONE:
        // No marker at all
        break;
      case LST_DISC:
        $this->mb_disc($viewport, $x, $y);
        break;
      case LST_CIRCLE:
        $this->mb_circle($viewport, $x, $y);
        break;
      case LST_SQUARE:
        $this->mb_square($viewport, $x, $y);
        break;
      default:
        $this->mb_string($viewport, $x, $y);
        break;
      }
    };

    return true;
  }

  function get_marker_box_width() {
    $list_style = $this->getCSSProperty(CSS_LIST_STYLE);
    
    switch ($list_style->type) {
    case LST_NONE:
      // no marker box will be rendered at all
      return 0;
    case LST_DISC:
    case LST_CIRCLE:
    case LST_SQUARE:
      //  simple graphic marker
      $font = $this->getCSSProperty(CSS_FONT);
      return $font->size->getPoints();
    default:
      // string marker. Return the width of the marker text
      return $this->str_number_box->get_full_width();
    };
  }

  function mb_string(&$viewport, $x, $y) {
    $this->str_number_box->put_top($y + $this->str_number_box->default_baseline);
    $this->str_number_box->put_left($x - $this->str_number_box->get_full_width());

    $this->str_number_box->show($viewport);
  }

  function mb_disc(&$viewport, $x, $y) {
    $color = $this->getCSSProperty(CSS_COLOR);
    $color->apply($viewport);

    $font = $this->getCSSProperty(CSS_FONT);
    
    $viewport->circle( $x - $font->size->getPoints()*0.5, $y + $font->size->getPoints()*0.4*HEIGHT_KOEFF, $font->size->getPoints() * BULLET_SIZE_KOEFF);
    $viewport->fill();
  }
  
  function mb_circle(&$viewport, $x, $y) {
    $color = $this->getCSSProperty(CSS_COLOR);
    $color->apply($viewport);

    $viewport->setlinewidth(0.1);

    $font = $this->getCSSProperty(CSS_FONT);
    $viewport->circle( $x - $font->size->getPoints()*0.5, $y + $font->size->getPoints()*0.4*HEIGHT_KOEFF, $font->size->getPoints() * BULLET_SIZE_KOEFF);
    $viewport->stroke();
  }

  function mb_square(&$viewport, $x, $y) {
    $color = $this->getCSSProperty(CSS_COLOR);
    $color->apply($viewport);

    $font = $this->getCSSProperty(CSS_FONT);
    $viewport->rect($x - $font->size->getPoints()*0.512, $y + $font->size->getPoints()*0.3*HEIGHT_KOEFF, $font->size->getPoints() * 0.25, $font->size->getPoints() * 0.25);
    $viewport->fill();
  }

  function mb_image(&$viewport, $x, $y) {
    $font = $this->getCSSProperty(CSS_FONT);

    $imagebox =& $this->marker_image;
    $imagebox->moveto($x - $font->size->getPoints()*0.5 - $imagebox->get_width()/2, 
                      $y + $font->size->getPoints()*0.4*HEIGHT_KOEFF + $imagebox->get_height()/2);
    $imagebox->show($viewport);
  }

  function isBlockLevel() {
    return true;
  }
}

?>