File: cairo-pattern-fill.rb

package info (click to toggle)
ruby-gnome2 0.15.0-1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 7,692 kB
  • ctags: 8,558
  • sloc: ansic: 69,912; ruby: 19,511; makefile: 97; xml: 35; sql: 13
file content (88 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (c) 2005 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
# $Id: cairo-pattern-fill.rb,v 1.1 2005/10/12 05:38:30 ktou Exp $
=begin
= cairo/Pattern Fill

This demo shows how to use GDK and cairo to fill by pattern.
From http://cairographics.org/samples/pattern_fill.html.
=end
require 'common'

module Demo
  class CairoPatternFill < CairoWindow
    X_FUZZ = 0.08
    Y_FUZZ = 0.08
    
    X_INNER_RADIUS = 0.3
    Y_INNER_RADIUS = 0.2
    
    X_OUTER_RADIUS = 0.45
    Y_OUTER_RADIUS = 0.35
    
    SPIKES = 10

    TEXT = "Ruby!"

    def initialize
      super('cairo pattern fill')
    end

    def draw(cr)
      srand(45)
      
      cr.set_line_width(0.01)

      x = y = nil
      0.step(SPIKES * 2 - 1, 2) do |i|
        x = x_position(i, X_INNER_RADIUS)
        y = y_position(i, Y_INNER_RADIUS)
        
        if (i == 0)
          cr.move_to(x, y)
        else
          cr.line_to(x, y)
        end
            
        i += 1
            
        x = x_position(i, X_OUTER_RADIUS)
        y = y_position(i, Y_OUTER_RADIUS)
        
        cr.line_to(x, y)
      end

      cr.close_path
      cr.stroke
          
      cr.select_font_face("Sans",
                          Cairo::FONT_SLANT_NORMAL,
                          Cairo::FONT_WEIGHT_BOLD)

      cr.move_to(x - 0.5, y)
      cr.set_font_size(0.1)
      cr.text_path(TEXT)
      cr.set_source_rgba(1, 0, 0.5)
      cr.fill
          
      cr.set_font_size(0.2)
      extents = cr.text_extents(TEXT)
      x = 0.5 - (extents.width / 2 + extents.x_bearing)
      y = 0.5 - (extents.height / 2 + extents.y_bearing)

      cr.move_to(x, y)
      cr.text_path(TEXT)
      cr.set_source_rgba(1, 0, 0)
      cr.stroke
    end

    def x_position(i, radius)
      0.5 + Math.cos(Math::PI * i / SPIKES) * radius + rand * X_FUZZ
    end

    def y_position(i, radius)
      0.5 + Math.sin(Math::PI * i / SPIKES) * radius + rand * Y_FUZZ      
    end
  end
end