~ian-mcintosh/luz/trunk

« back to all changes in this revision

Viewing changes to utils/cairo_canvas.rb

  • Committer: yella
  • Date: 2009-09-07 01:14:10 UTC
  • Revision ID: yella@nutwork-20090907011410-1fw81og60j70dnef
- move Cairo::Context addons to separate file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'cairo'
2
 
 
3
 
class Cairo::Context
4
 
        boolean_accessor :track_dirty_rects
5
 
        boolean_accessor :entire_canvas_dirty
6
 
        attr_reader :dirty_rects
7
 
 
8
 
        def add_dirty_rect(rect)
9
 
                @dirty_rects ||= []
10
 
                @dirty_rects << rect
11
 
        end
12
 
 
13
 
        def dirty_rects_clear
14
 
                r = @dirty_rects
15
 
                @dirty_rects = nil
16
 
                r || []
17
 
        end
18
 
 
19
 
        alias :fill_without_dirty_rects :fill
20
 
        def fill
21
 
                add_dirty_rect(fill_extents) if track_dirty_rects?
22
 
                fill_without_dirty_rects
23
 
        end
24
 
 
25
 
        alias :fill_preserve_without_dirty_rects :fill_preserve
26
 
        def fill_preserve
27
 
                add_dirty_rect(fill_extents) if track_dirty_rects?
28
 
                fill_preserve_without_dirty_rects
29
 
        end
30
 
 
31
 
        alias :stroke_without_dirty_rects :stroke
32
 
        def stroke
33
 
                add_dirty_rect(stroke_extents) if track_dirty_rects?
34
 
                stroke_without_dirty_rects
35
 
        end
36
 
 
37
 
        alias :stroke_preserve_without_dirty_rects :stroke_preserve
38
 
        def stroke_preserve
39
 
                add_dirty_rect(stroke_extents) if track_dirty_rects?
40
 
                stroke_preserve_without_dirty_rects
41
 
        end
42
 
 
43
 
        alias :paint_without_dirty_rects :paint
44
 
        def paint(amt=1.0)
45
 
                set_entire_canvas_dirty(true) if track_dirty_rects?
46
 
                paint_without_dirty_rects(amt)
47
 
        end
48
 
end
 
1
require 'cairo_context'
49
2
 
50
3
class CairoCanvas
51
4
        attr_reader :width, :height, :string_data
53
6
        def initialize(width, height)
54
7
                @width, @height = width, height
55
8
 
56
 
                @string_data = (' ' * (@width * @height * 4))           # TODO: ensure this is right for RUBY1.9, RUBY2.0 etc.
 
9
                @string_data = ("\0" * (@width * @height * 4))          # TODO: ensure this is right for RUBY1.9, RUBY2.0 etc.
57
10
                @cairo_surface = Cairo::ImageSurface.new(@string_data, Cairo::FORMAT_ARGB32, @width, @height, stride = (@width * 4))
58
11
 
59
12
                @cr = Cairo::Context.new(@cairo_surface)
65
18
        pipe :dirty_rects_clear, :cr
66
19
        pipe :entire_canvas_dirty?, :cr
67
20
        pipe :entire_canvas_dirty=, :cr
68
 
        
69
21
 
70
22
        def using
71
23
                yield @cr