[cairo-commit] rcairo/samples text-on-path.rb,NONE,1.1

Kouhei Sutou commit at pdx.freedesktop.org
Fri Dec 15 00:45:23 PST 2006


Committed by: kou

Update of /cvs/cairo/rcairo/samples
In directory kemper:/tmp/cvs-serv15191/samples

Added Files:
	text-on-path.rb 
Log Message:
* packages/cairo/lib/cairo/context/path.rb: ported Pango's
  cairotwisted example as library.
* samples/text-on-path.rb: ported Pango's cairotwisted example by
  using the library.


--- NEW FILE: text-on-path.rb ---
#!/usr/bin/env ruby

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

require 'cairo'
require 'pango'

def render_background(cr)
  cr.set_source_rgba(1.0, 1.0, 1.0)
  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_rgba(1, 0, 0, 1)
  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

  cr
end

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

output



More information about the cairo-commit mailing list