File: text-on-path.rb

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (59 lines) | stat: -rwxr-xr-x 1,226 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env ruby

top = File.expand_path(File.join(File.dirname(__FILE__), ".."))
$LOAD_PATH.unshift File.join(top, "ext", "cairo")
$LOAD_PATH.unshift File.join(top, "lib")

require 'cairo'
require 'pango'

def render_background(cr)
  cr.set_source_color(:white)
  cr.paint
end

def make_layout(cr, text)
  layout = cr.create_pango_layout
  layout.text = text
  layout.font_description = Pango::FontDescription.new("Serif 36")
  cr.update_pango_layout(layout)
  layout
end

def render(surface)
  text = "It was a dream... Oh Just a dream..."

  cr = Cairo::Context.new(surface)

  render_background(cr)

  cr.set_source_color(:red)
  cr.move_to(25, 350)
  cr.line_to(150, 375)
  cr.curve_to(275, 400, 450, 350, 450, 200)
  cr.curve_to(450, 0, 300, 150, 50, 50)
  cr.stroke_preserve
  path = cr.copy_path_flat

  cr.line_width = 1
  cr.new_path
  layout = make_layout(cr, text)
  cr.pango_layout_line_path(layout.get_line(0))
  cr.map_path_onto(path)

  cr.set_source_rgba(0.3, 0.3, 1.0, 0.3)
  cr.fill_preserve
  cr.set_source_rgba(0.1, 0.1, 0.1)
  cr.stroke

  cr.show_page
end

def output
  Cairo::ImageSurface.new(500, 500) do |surface|
    render(surface)
    surface.write_to_png("text-on-path.png")
  end
end

output