Module: Rabbit::Renderer::Engine::Cairo

Includes:
Kernel
Included in:
Display::ClutterEmbed, Display::DrawingArea, Display::DrawingAreaViewOnly, Offscreen, Printer, Widget::DrawingArea
Defined in:
lib/rabbit/renderer/engine/cairo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Kernel

#draw_circle, #draw_circle_by_radius, #draw_cone, #draw_cube, #draw_dodecahedron, #draw_flag, #draw_flag_layout, #draw_icosahedron, #draw_octahedron, #draw_rectangle_flag, #draw_slide, #draw_sphere, #draw_teapot, #draw_tetrahedron, #draw_torus, #draw_triangle_flag, #flag_size, #gl_call_list, #gl_compile, #gl_supported?, #make_color, #new_list_id, #z_far, #z_view

Instance Attribute Details

#background=(value) ⇒ Object (writeonly)

Sets the attribute background

Parameters:

  • value

    the value to set the attribute background to.


13
14
15
# File 'lib/rabbit/renderer/engine/cairo.rb', line 13

def background=(value)
  @background = value
end

#foreground=(value) ⇒ Object (writeonly)

Sets the attribute foreground

Parameters:

  • value

    the value to set the attribute foreground to.


13
14
15
# File 'lib/rabbit/renderer/engine/cairo.rb', line 13

def foreground=(value)
  @foreground = value
end

Instance Method Details

#alpha_available?Boolean

Returns:

  • (Boolean)

15
16
17
# File 'lib/rabbit/renderer/engine/cairo.rb', line 15

def alpha_available?
  true
end

#background_image=(pixbuf) ⇒ Object


19
20
21
22
23
24
25
26
# File 'lib/rabbit/renderer/engine/cairo.rb', line 19

def background_image=(pixbuf)
  surface = ::Cairo::ImageSurface.new(::Cairo::FORMAT_A1, 1, 1)
  context = ::Cairo::Context.new(surface)
  context.set_source_pixbuf(pixbuf)
  @background_image = context.source
  @background_image.extend = ::Cairo::EXTEND_REPEAT
  pixbuf
end

#create_pango_contextObject


296
297
298
299
300
# File 'lib/rabbit/renderer/engine/cairo.rb', line 296

def create_pango_context
  context = Pango::CairoFontMap.default.create_context
  set_font_resolution(context)
  context
end

#draw_arc(filled, x, y, w, h, a1, a2, color = nil, params = {}) ⇒ Object


141
142
143
144
145
# File 'lib/rabbit/renderer/engine/cairo.rb', line 141

def draw_arc(filled, x, y, w, h, a1, a2, color=nil, params={})
  r = w * 0.5
  draw_arc_by_radius(filled, x + w * 0.5, y + h * 0.5,
                     r, a1, a2, color, params)
end

#draw_arc_by_radius(filled, x, y, r, a1, a2, color = nil, params = {}) ⇒ Object


147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rabbit/renderer/engine/cairo.rb', line 147

def draw_arc_by_radius(filled, x, y, r, a1, a2, color=nil, params={})
  x, y = from_screen(x, y)
  a1, a2 = convert_angle(a1, a2)
  @context.save do
    set_source(color, params)
    set_line_options(params)
    args = [x, y, r, a1, a2]
    action, = cairo_action(filled, params)
    @context.move_to(x, y) unless action == :stroke
    @context.arc(*args)
    @context.close_path unless action == :stroke
    apply_cairo_action(filled, params)
  end
end

#draw_background(slide) ⇒ Object


93
94
95
96
97
98
99
100
101
# File 'lib/rabbit/renderer/engine/cairo.rb', line 93

def draw_background(slide)
  super
  if @background_image
    @context.save do
      @context.set_source(@background_image)
      @context.paint
    end
  end
end

#draw_layout(layout, x, y, color = nil, params = {}) ⇒ Object


180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rabbit/renderer/engine/cairo.rb', line 180

def draw_layout(layout, x, y, color=nil, params={})
  x, y = from_screen(x, y)
  @context.save do
    set_source(color, params)
    set_line_options(params)
    @context.move_to(x, y)
    if params[:stroke]
      @context.pango_layout_path(layout)
      apply_cairo_action(false, params)
    else
      @context.show_pango_layout(layout)
    end
  end
end

#draw_line(x1, y1, x2, y2, color = nil, params = {}) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rabbit/renderer/engine/cairo.rb', line 103

def draw_line(x1, y1, x2, y2, color=nil, params={})
  x1, y1 = from_screen(x1, y1)
  x2, y2 = from_screen(x2, y2)
  @context.save do
    set_source(color, params)
    set_line_options(params)
    @context.new_path
    @context.move_to(x1, y1)
    @context.line_to(x2, y2)
    apply_cairo_action(false, params)
  end
end

#draw_lines(points, color = nil, params = {}) ⇒ Object


162
163
164
# File 'lib/rabbit/renderer/engine/cairo.rb', line 162

def draw_lines(points, color=nil, params={})
  draw_polygon(false, points, color, params.merge({:opened => true}))
end

276
277
278
279
280
281
282
283
284
# File 'lib/rabbit/renderer/engine/cairo.rb', line 276

def draw_link(uri)
  if @context.respond_to?(:tag)
    @context.tag(::Cairo::Tag::LINK, "uri='#{uri}'") do
      yield
    end
  else
    yield
  end
end

#draw_pixbuf(pixbuf, x, y, params = {}) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rabbit/renderer/engine/cairo.rb', line 195

def draw_pixbuf(pixbuf, x, y, params={})
  x, y = from_screen(x, y)
  @context.save do
    apply_clip(x, y, pixbuf.width, pixbuf.height, params)
    @context.translate(x, y)
    set_source_pixbuf(pixbuf, params)
    @context.paint(params[:alpha])
  end

  _draw_reflected_pixbuf(pixbuf, x, y, params)
end

#draw_polygon(filled, points, color = nil, params = {}) ⇒ Object


166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rabbit/renderer/engine/cairo.rb', line 166

def draw_polygon(filled, points, color=nil, params={})
  return if points.empty?
  @context.save do
    set_source(color, params)
    set_line_options(params)
    @context.move_to(*from_screen(*points.first))
    points[1..-1].each do |x, y|
      @context.line_to(*from_screen(x, y))
    end
    @context.line_to(*from_screen(*points.first)) unless params[:opened]
    apply_cairo_action(filled, params)
  end
end

#draw_poppler_page(page, x, y, params = {}) ⇒ Object


263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rabbit/renderer/engine/cairo.rb', line 263

def draw_poppler_page(page, x, y, params={})
  x, y = from_screen(x, y)
  w, h = page.size
  width = (params[:width] || w).to_f
  height = (params[:height] || h).to_f
  @context.save do
    apply_clip(x, y, w, h, params)
    @context.translate(x, y)
    @context.scale(width / w, height / h)
    @context.render_poppler_page(page)
  end
end

#draw_rectangle(filled, x, y, w, h, color = nil, params = {}) ⇒ Object


116
117
118
119
120
121
122
123
124
# File 'lib/rabbit/renderer/engine/cairo.rb', line 116

def draw_rectangle(filled, x, y, w, h, color=nil, params={})
  x, y = from_screen(x, y)
  @context.save do
    set_source(color, params)
    set_line_options(params)
    @context.rectangle(x, y, w, h)
    apply_cairo_action(filled, params)
  end
end

#draw_rounded_rectangle(filled, x, y, w, h, radius, color = nil, params = {}) ⇒ Object


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rabbit/renderer/engine/cairo.rb', line 126

def draw_rounded_rectangle(filled, x, y, w, h, radius,
                           color=nil, params={})
  x, y = from_screen(x, y)
  x_radius = params[:x_radius] || radius
  y_radius = params[:y_radius] || radius

  @context.save do
    set_source(color, params)
    set_line_options(params)
    @context.new_path
    @context.rounded_rectangle(x, y, w, h, x_radius, y_radius)
    apply_cairo_action(filled, params)
  end
end

#draw_rsvg_handle(handle, x, y, params = {}) ⇒ Object


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rabbit/renderer/engine/cairo.rb', line 245

def draw_rsvg_handle(handle, x, y, params={})
  x, y = from_screen(x, y)
  dim = handle.dimensions
  w = dim.width
  h = dim.height
  width = (params[:width] || w).to_f
  height = (params[:height] || h).to_f
  @context.save do
    apply_clip(x, y, w, h, params)
    @context.translate(x, y)
    @context.scale(width / w, height / h)
    @context.render_rsvg_handle(handle)
  end

  _draw_reflected_rsvg_handle(handle, x, y, width, height,
                              params)
end

#finish_contextObject


45
46
47
48
49
# File 'lib/rabbit/renderer/engine/cairo.rb', line 45

def finish_context
  @contexts.pop
  @context.destroy if @context.respond_to?(:destroy)
  @context = @contexts.last
end

#finish_rendererObject


32
33
34
# File 'lib/rabbit/renderer/engine/cairo.rb', line 32

def finish_renderer
  finish_context
end

#init_context(context) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/rabbit/renderer/engine/cairo.rb', line 36

def init_context(context)
  @context = context
  @contexts ||= []
  @contexts.push(@context)
  set_line_width(1)
  @context.line_cap = ::Cairo::LINE_CAP_ROUND
  @context.line_join = ::Cairo::LINE_JOIN_ROUND
end

#init_renderer(drawable) ⇒ Object


28
29
30
# File 'lib/rabbit/renderer/engine/cairo.rb', line 28

def init_renderer(drawable)
  init_context(drawable.create_cairo_context)
end

#make_layout(text) ⇒ Object


286
287
288
289
290
291
292
293
294
# File 'lib/rabbit/renderer/engine/cairo.rb', line 286

def make_layout(text)
  attributes, text = Pango.parse_markup(text)
  layout = @context.create_pango_layout
  layout.text = text
  layout.attributes = attributes
  set_font_resolution(layout.context)
  @context.update_pango_layout(layout)
  layout
end

#reflect_context(base, params = {}) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rabbit/renderer/engine/cairo.rb', line 67

def reflect_context(base, params={})
  case base
  when :y
    matrix = make_matrix(-1, 0, 0,
                          0, 1, 0)
  else
    matrix = make_matrix(1,  0, 0,
                         0, -1, 0)
  end
  @context.transform(matrix)
end

#restore_contextObject


89
90
91
# File 'lib/rabbit/renderer/engine/cairo.rb', line 89

def restore_context
  @context.restore
end

#rotate_context(angle, params = {}) ⇒ Object


59
60
61
# File 'lib/rabbit/renderer/engine/cairo.rb', line 59

def rotate_context(angle, params={})
  @context.rotate(convert_angle(angle, 0)[0])
end

#save_contextObject


84
85
86
87
# File 'lib/rabbit/renderer/engine/cairo.rb', line 84

def save_context
  @context.save
  super
end

#scale_context(x, y, params = {}) ⇒ Object


63
64
65
# File 'lib/rabbit/renderer/engine/cairo.rb', line 63

def scale_context(x, y, params={})
  @context.scale(x, y)
end

#set_font_resolution(context) ⇒ Object


302
303
304
# File 'lib/rabbit/renderer/engine/cairo.rb', line 302

def set_font_resolution(context)
  context.resolution = @canvas.font_resolution
end

#set_source_pixbuf(pixbuf, params) ⇒ Object


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
# File 'lib/rabbit/renderer/engine/cairo.rb', line 207

def set_source_pixbuf(pixbuf, params)
  draw_scaled_pixbuf = params[:draw_scaled_pixbuf]
  draw_scaled_pixbuf = @draw_scaled_image if draw_scaled_pixbuf.nil?
  width = (params[:width] || pixbuf.width).to_f
  height = (params[:height] || pixbuf.height).to_f

  if [width, height] == [pixbuf.width, pixbuf.height]
    @context.set_source_pixbuf(pixbuf, 0, 0)
    return
  end

  case draw_scaled_pixbuf
  when true
    scaled_pixbuf = pixbuf.scale(width, height)
    @context.set_source_pixbuf(scaled_pixbuf, 0, 0)
  when false
    @context.set_source_pixbuf(pixbuf, 0, 0)
    matrix = ::Cairo::Matrix.scale(pixbuf.width / width,
                                   pixbuf.height / height)
    @context.source.matrix = matrix
  else
    scales = [4, 3, 2]
    scales.each do |scale|
      if width * scale < pixbuf.width and height * scale < pixbuf.height
        scaled_pixbuf = pixbuf.scale(width * scale, height * scale)
        @context.set_source_pixbuf(scaled_pixbuf)
        matrix = ::Cairo::Matrix.scale(scale, scale)
        @context.source.matrix = matrix
        return
      end
    end
    @context.set_source_pixbuf(pixbuf, 0, 0)
    matrix = ::Cairo::Matrix.scale(pixbuf.width / width,
                                   pixbuf.height / height)
    @context.source.matrix = matrix
  end
end

#shear_context(x, y, params = {}) ⇒ Object


79
80
81
82
# File 'lib/rabbit/renderer/engine/cairo.rb', line 79

def shear_context(x, y, params={})
  @context.transform(make_matrix(1, x, 0,
                                 y, 1, 0))
end

#to_gdk_rgb(color) ⇒ Object


51
52
53
# File 'lib/rabbit/renderer/engine/cairo.rb', line 51

def to_gdk_rgb(color)
  make_color(color).to_gdk_rgb
end

#translate_context(x, y, params = {}) ⇒ Object


55
56
57
# File 'lib/rabbit/renderer/engine/cairo.rb', line 55

def translate_context(x, y, params={})
  @context.translate(x, y)
end