Completed
Branch development (b1b115)
by Johannes
10:28
created

Inline::render()   F

Complexity

Conditions 49
Paths > 20000

Size

Total Lines 183

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 183
rs 0
cc 49
nc 1359361
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package dompdf
4
 * @link    http://dompdf.github.com/
5
 * @author  Benj Carson <[email protected]>
6
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7
 */
8
namespace Dompdf\Renderer;
9
10
use Dompdf\Frame;
11
use Dompdf\Helpers;
12
13
/**
14
 * Renders inline frames
15
 *
16
 * @access  private
17
 * @package dompdf
18
 */
19
class Inline extends AbstractRenderer
20
{
21
22
    /**
23
     * @param Frame $frame
24
     */
25
    function render(Frame $frame)
26
    {
27
        $style = $frame->get_style();
28
29
        if (!$frame->get_first_child()) {
30
            return; // No children, no service
31
        }
32
33
        // Draw the left border if applicable
34
        $bp = $style->get_border_properties();
35
        $widths = array(
36
            (float)$style->length_in_pt($bp["top"]["width"]),
37
            (float)$style->length_in_pt($bp["right"]["width"]),
38
            (float)$style->length_in_pt($bp["bottom"]["width"]),
39
            (float)$style->length_in_pt($bp["left"]["width"])
40
        );
41
42
        // Draw the background & border behind each child.  To do this we need
43
        // to figure out just how much space each child takes:
44
        list($x, $y) = $frame->get_first_child()->get_position();
45
        $w = null;
46
        $h = 0;
47
        // $x += $widths[3];
48
        // $y += $widths[0];
49
50
        $this->_set_opacity($frame->get_opacity($style->opacity));
51
52
        $first_row = true;
53
54
        $DEBUGLAYOUTINLINE = $this->_dompdf->getOptions()->getDebugLayout() && $this->_dompdf->getOptions()->getDebugLayoutInline();
55
56
        foreach ($frame->get_children() as $child) {
57
            list($child_x, $child_y, $child_w, $child_h) = $child->get_padding_box();
58
59
            if (!is_null($w) && $child_x < $x + $w) {
60
                //This branch seems to be supposed to being called on the first part
61
                //of an inline html element, and the part after the if clause for the
62
                //parts after a line break.
63
                //But because $w initially mostly is 0, and gets updated only on the next
64
                //round, this seem to be never executed and the common close always.
65
66
                // The next child is on another line.  Draw the background &
67
                // borders on this line.
68
69
                // Background:
70
                if (($bg = $style->background_color) !== "transparent") {
71
                    $this->_canvas->filled_rectangle($x, $y, $w, $h, $bg);
72
                }
73
74
                if (($url = $style->background_image) && $url !== "none") {
75
                    $this->_background_image($url, $x, $y, $w, $h, $style);
76
                }
77
78
                // If this is the first row, draw the left border
79
                if ($first_row) {
80
                    if ($bp["left"]["style"] !== "none" && $bp["left"]["color"] !== "transparent" && $bp["left"]["width"] > 0) {
81
                        $method = "_border_" . $bp["left"]["style"];
82
                        $this->$method($x, $y, $h + $widths[0] + $widths[2], $bp["left"]["color"], $widths, "left");
83
                    }
84
                    $first_row = false;
85
                }
86
87
                // Draw the top & bottom borders
88
                if ($bp["top"]["style"] !== "none" && $bp["top"]["color"] !== "transparent" && $bp["top"]["width"] > 0) {
89
                    $method = "_border_" . $bp["top"]["style"];
90
                    $this->$method($x, $y, $w + $widths[1] + $widths[3], $bp["top"]["color"], $widths, "top");
91
                }
92
93
                if ($bp["bottom"]["style"] !== "none" && $bp["bottom"]["color"] !== "transparent" && $bp["bottom"]["width"] > 0) {
94
                    $method = "_border_" . $bp["bottom"]["style"];
95
                    $this->$method($x, $y + $h + $widths[0] + $widths[2], $w + $widths[1] + $widths[3], $bp["bottom"]["color"], $widths, "bottom");
96
                }
97
98
                // Handle anchors & links
99
                $link_node = null;
100
                if ($frame->get_node()->nodeName === "a") {
101
                    $link_node = $frame->get_node();
102
                } else if ($frame->get_parent()->get_node()->nodeName === "a") {
103
                    $link_node = $frame->get_parent()->get_node();
104
                }
105
106
                if ($link_node && $href = $link_node->getAttribute("href")) {
107
                    $href = Helpers::build_url($this->_dompdf->getProtocol(), $this->_dompdf->getBaseHost(), $this->_dompdf->getBasePath(), $href);
108
                    $this->_canvas->add_link($href, $x, $y, $w, $h);
109
                }
110
111
                $x = $child_x;
112
                $y = $child_y;
113
                $w = (float)$child_w;
114
                $h = (float)$child_h;
115
                continue;
116
            }
117
118
            if (is_null($w)) {
119
                $w = (float)$child_w;
120
            }else {
121
                $w += (float)$child_w;
122
            }
123
124
            $h = max($h, $child_h);
125
126
            if ($DEBUGLAYOUTINLINE) {
127
                $this->_debug_layout($child->get_border_box(), "blue");
128
                if ($this->_dompdf->getOptions()->getDebugLayoutPaddingBox()) {
129
                    $this->_debug_layout($child->get_padding_box(), "blue", array(0.5, 0.5));
130
                }
131
            }
132
        }
133
134
        // Handle the last child
135
        if (($bg = $style->background_color) !== "transparent") {
136
            $this->_canvas->filled_rectangle($x + $widths[3], $y + $widths[0], $w, $h, $bg);
137
        }
138
139
        //On continuation lines (after line break) of inline elements, the style got copied.
140
        //But a non repeatable background image should not be repeated on the next line.
141
        //But removing the background image above has never an effect, and removing it below
142
        //removes it always, even on the initial line.
143
        //Need to handle it elsewhere, e.g. on certain ...clone()... usages.
144
        // Repeat not given: default is Style::__construct
145
        // ... && (!($repeat = $style->background_repeat) || $repeat === "repeat" ...
146
        //different position? $this->_background_image($url, $x, $y, $w, $h, $style);
147
        if (($url = $style->background_image) && $url !== "none") {
148
            $this->_background_image($url, $x + $widths[3], $y + $widths[0], $w, $h, $style);
149
        }
150
151
        // Add the border widths
152
        $w += (float)$widths[1] + (float)$widths[3];
153
        $h += (float)$widths[0] + (float)$widths[2];
154
155
        // make sure the border and background start inside the left margin
156
        $left_margin = (float)$style->length_in_pt($style->margin_left);
157
        $x += $left_margin;
158
159
        // If this is the first row, draw the left border too
160
        if ($first_row && $bp["left"]["style"] !== "none" && $bp["left"]["color"] !== "transparent" && $widths[3] > 0) {
161
            $method = "_border_" . $bp["left"]["style"];
162
            $this->$method($x, $y, $h, $bp["left"]["color"], $widths, "left");
163
        }
164
165
        // Draw the top & bottom borders
166
        if ($bp["top"]["style"] !== "none" && $bp["top"]["color"] !== "transparent" && $widths[0] > 0) {
167
            $method = "_border_" . $bp["top"]["style"];
168
            $this->$method($x, $y, $w, $bp["top"]["color"], $widths, "top");
169
        }
170
171
        if ($bp["bottom"]["style"] !== "none" && $bp["bottom"]["color"] !== "transparent" && $widths[2] > 0) {
172
            $method = "_border_" . $bp["bottom"]["style"];
173
            $this->$method($x, $y + $h, $w, $bp["bottom"]["color"], $widths, "bottom");
174
        }
175
176
        //    Helpers::var_dump(get_class($frame->get_next_sibling()));
177
        //    $last_row = get_class($frame->get_next_sibling()) !== 'Inline';
178
        // Draw the right border if this is the last row
179
        if ($bp["right"]["style"] !== "none" && $bp["right"]["color"] !== "transparent" && $widths[1] > 0) {
180
            $method = "_border_" . $bp["right"]["style"];
181
            $this->$method($x + $w, $y, $h, $bp["right"]["color"], $widths, "right");
182
        }
183
184
        $id = $frame->get_node()->getAttribute("id");
185
        if (strlen($id) > 0)  {
186
            $this->_canvas->add_named_dest($id);
187
        }
188
189
        // Only two levels of links frames
190
        $link_node = null;
191
        if ($frame->get_node()->nodeName === "a") {
192
            $link_node = $frame->get_node();
193
194
            if (($name = $link_node->getAttribute("name"))) {
195
                $this->_canvas->add_named_dest($name);
196
            }
197
        }
198
199
        if ($frame->get_parent() && $frame->get_parent()->get_node()->nodeName === "a") {
200
            $link_node = $frame->get_parent()->get_node();
201
        }
202
203
        // Handle anchors & links
204
        if ($link_node) {
205
            if ($href = $link_node->getAttribute("href")) {
206
                $href = Helpers::build_url($this->_dompdf->getProtocol(), $this->_dompdf->getBaseHost(), $this->_dompdf->getBasePath(), $href);
207
                $this->_canvas->add_link($href, $x, $y, $w, $h);
208
            }
209
        }
210
    }
211
}
212