Method: Origami::ContentStream#draw_rectangle

Defined in:
lib/origami/graphics/xobject.rb

#draw_rectangle(x, y, width, height, attr = {}) ⇒ Object

Draw a rectangle at position (x,y) with defined width and height.



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
# File 'lib/origami/graphics/xobject.rb', line 149

def draw_rectangle(x, y, width, height, attr = {})
    load!

    stroke_color  = attr.fetch(:stroke_color, DEFAULT_STROKE_COLOR)
    fill_color    = attr.fetch(:fill_color, DEFAULT_FILL_COLOR)
    line_cap      = attr.fetch(:line_cap, DEFAULT_LINECAP)
    line_join     = attr.fetch(:line_join, DEFAULT_LINEJOIN)
    line_width    = attr.fetch(:line_width, DEFAULT_LINEWIDTH)
    dash_pattern  = attr.fetch(:dash, DEFAULT_DASHPATTERN)

    stroke        = attr[:stroke].nil? ? true : attr[:stroke]
    fill          = attr[:fill].nil? ? false : attr[:fill]

    stroke = true if fill == false and stroke == false

    set_fill_color(fill_color) if fill
    set_stroke_color(stroke_color) if stroke
    set_line_width(line_width)
    set_line_cap(line_cap)
    set_line_join(line_join)
    set_dash_pattern(dash_pattern)

    if @canvas.gs.text_state.is_in_text_object?
        @instructions << PDF::Instruction.new('ET').render(@canvas)
    end

    @instructions << PDF::Instruction.new('re', x,y,width,height).render(@canvas)

    @instructions << (i =
        if stroke and not fill
            PDF::Instruction.new('S')
        elsif fill and not stroke
            PDF::Instruction.new('f')
        elsif fill and stroke
            PDF::Instruction.new('B')
        end
    )

    i.render(@canvas)

    self
end