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

Includes:
Kernel
Included in:
Display::ClutterEmbed, Pixmap::Cairo, Print::Cairo
Defined in:
lib/rabbit/renderer/engine/cairo.rb

Constant Summary collapse

@@rsvg_available =
nil
@@poppler_available =
nil

Instance Attribute Summary collapse

Class Method 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.



72
73
74
# File 'lib/rabbit/renderer/engine/cairo.rb', line 72

def background=(value)
  @background = value
end

#foreground=(value) ⇒ Object (writeonly)

Sets the attribute foreground

Parameters:

  • value

    the value to set the attribute foreground to.



72
73
74
# File 'lib/rabbit/renderer/engine/cairo.rb', line 72

def foreground=(value)
  @foreground = value
end

Class Method Details

.available_with_gdk?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rabbit/renderer/engine/cairo.rb', line 49

def available_with_gdk?
  Gdk.respond_to?(:cairo_available?) and Gdk.cairo_available?
end

.available_with_gdk_pixbuf?Boolean

Returns:

  • (Boolean)


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

def available_with_gdk_pixbuf?
  Gdk::Pixbuf.respond_to?(:cairo_available?) and
    Gdk::Pixbuf.cairo_available?
end

.available_with_pango?Boolean

Returns:

  • (Boolean)


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

def available_with_pango?
  Pango.respond_to?(:cairo_available?) and Pango.cairo_available?
end

.priorityObject



62
63
64
65
66
67
68
69
# File 'lib/rabbit/renderer/engine/cairo.rb', line 62

def priority
  if available_with_gdk? and available_with_gdk_pixbuf? and
      available_with_pango?
    100
  else
    -100
  end
end

Instance Method Details

#alpha_available?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rabbit/renderer/engine/cairo.rb', line 74

def alpha_available?
  true
end

#background_image=(pixbuf) ⇒ Object



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

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



326
327
328
329
330
# File 'lib/rabbit/renderer/engine/cairo.rb', line 326

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



203
204
205
206
207
# File 'lib/rabbit/renderer/engine/cairo.rb', line 203

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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rabbit/renderer/engine/cairo.rb', line 209

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



155
156
157
158
159
160
161
162
163
# File 'lib/rabbit/renderer/engine/cairo.rb', line 155

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



242
243
244
245
246
247
248
249
250
# File 'lib/rabbit/renderer/engine/cairo.rb', line 242

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)
    @context.show_pango_layout(layout)
  end
end

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



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

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



224
225
226
# File 'lib/rabbit/renderer/engine/cairo.rb', line 224

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

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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rabbit/renderer/engine/cairo.rb', line 252

def draw_pixbuf(pixbuf, x, y, params={})
  x, y = from_screen(x, y)

  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 draw_scaled_pixbuf and
      [width, height] != [pixbuf.width, pixbuf.height]
    pixbuf = pixbuf.scale(width, height)
  end
  @context.save do
    @context.translate(x, y)
    unless draw_scaled_pixbuf
      @context.scale(width / pixbuf.width, height / pixbuf.height)
    end
    @context.set_source_pixbuf(pixbuf, 0, 0)
    @context.paint(params[:alpha])
  end

  _draw_reflected_pixbuf(pixbuf, x, y, params[:reflect])
end

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



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rabbit/renderer/engine/cairo.rb', line 228

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



304
305
306
307
308
309
310
311
312
313
314
# File 'lib/rabbit/renderer/engine/cairo.rb', line 304

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
    @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



178
179
180
181
182
183
184
185
186
# File 'lib/rabbit/renderer/engine/cairo.rb', line 178

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rabbit/renderer/engine/cairo.rb', line 188

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



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

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

#finish_contextObject



107
108
109
110
111
# File 'lib/rabbit/renderer/engine/cairo.rb', line 107

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

#finish_rendererObject



94
95
96
# File 'lib/rabbit/renderer/engine/cairo.rb', line 94

def finish_renderer
  finish_context
end

#init_context(context) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/rabbit/renderer/engine/cairo.rb', line 98

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



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

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

#make_layout(text) ⇒ Object



316
317
318
319
320
321
322
323
324
# File 'lib/rabbit/renderer/engine/cairo.rb', line 316

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

#poppler_available?Boolean

Returns:

  • (Boolean)


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

def poppler_available?
  if @@poppler_available.nil?
    instance_methods = ::Cairo::Context.instance_methods
    available = instance_methods.include?("render_poppler_page")
    @@poppler_available = available
  end
  @@poppler_available
end

#prepare_renderer(drawable) ⇒ Object



87
88
# File 'lib/rabbit/renderer/engine/cairo.rb', line 87

def prepare_renderer(drawable)
end

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



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

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



151
152
153
# File 'lib/rabbit/renderer/engine/cairo.rb', line 151

def restore_context
  @context.restore
end

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



121
122
123
# File 'lib/rabbit/renderer/engine/cairo.rb', line 121

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

#rsvg_available?Boolean

Returns:

  • (Boolean)


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

def rsvg_available?
  if @@rsvg_available.nil?
    instance_methods = ::Cairo::Context.instance_methods
    @@rsvg_available = instance_methods.include?("render_rsvg_handle")
  end
  @@rsvg_available
end

#save_contextObject



146
147
148
149
# File 'lib/rabbit/renderer/engine/cairo.rb', line 146

def save_context
  @context.save
  super
end

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



125
126
127
# File 'lib/rabbit/renderer/engine/cairo.rb', line 125

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

#set_font_resolution(context) ⇒ Object



332
333
334
# File 'lib/rabbit/renderer/engine/cairo.rb', line 332

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

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



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

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

#to_gdk_rgb(color) ⇒ Object



113
114
115
# File 'lib/rabbit/renderer/engine/cairo.rb', line 113

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

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



117
118
119
# File 'lib/rabbit/renderer/engine/cairo.rb', line 117

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