Created
March 11, 2011 13:45
-
-
Save geoffyoungs/865891 to your computer and use it in GitHub Desktop.
Simple text effects for cairo/pango rendered text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cairo' | |
require 'pango' | |
width, height = 600, 600 | |
surface = Cairo::ImageSurface.new(:argb32, width, height) | |
cr = Cairo::Context.new(surface) | |
cr.save do | |
cr.set_operator(Cairo::OPERATOR_CLEAR) | |
cr.set_source_rgba(1.0, 1.0, 1.0, 0) | |
cr.paint(1.0) | |
end | |
def grad_vl(y1,y2,col1=nil,col2=nil,&block) | |
pattern= Cairo::LinearPattern.new(0, y1, 0, y2) | |
{0=>col1,1=>col2}.each do |offset, col| | |
next unless col | |
case col.size | |
when 3 | |
pattern.add_color_stop_rgb(offset, *col) | |
when 4 | |
pattern.add_color_stop_rgba(offset, *col) | |
end | |
end | |
yield(pattern) if block_given? | |
pattern | |
end | |
text = "Birthday" | |
font = Pango::FontDescription.new("Droid Sans Bold 104") | |
layout = cr.create_pango_layout | |
layout.font_description = font | |
layout.set_text(text) | |
ink, log = layout.get_pixel_extents | |
cr.save do | |
cr.pseudo_blur(5) do | |
cr.set_source_rgba(0,0,0,0.3) | |
cr.show_pango_layout(layout) | |
end | |
end | |
cr.set_source(grad_vl(ink.y, ink.height, [0,0.4,0.8], [0,0.2,0.5])) | |
cr.show_pango_layout(layout) | |
x,y = ink.x+ink.width, ink.y+ink.height/2 | |
x2 = ink.x+(ink.width/2) | |
x3 = ink.x | |
ywave_factor = ink.height / 12.0 | |
xwave_factor = ink.width / 6.0 | |
cr.save do | |
#cr.rectangle(ink.x,ink.y,ink.width, ink.height/2.0) | |
cr.new_path | |
cr.move_to(ink.x, ink.y) | |
cr.line_to(ink.x+ink.width, ink.y) | |
cr.line_to(ink.x+ink.width, ink.y+ink.height/2) | |
cr.curve_to(x - xwave_factor, y-ywave_factor, x2 + xwave_factor, y-ywave_factor, x2, y) | |
cr.curve_to(x2 - xwave_factor, y+ywave_factor, x3 + xwave_factor, y+ywave_factor, x3, y) | |
#cr.line_to(ink.x, ink.y+ink.height/2) | |
cr.line_to(ink.x, ink.y) | |
cr.clip | |
cr.set_source(grad_vl(ink.y, ink.height, [1,1,1,0.1], [1,1,1,0.3])) | |
cr.show_pango_layout(layout) | |
end | |
if false | |
cr.set_source_rgba(1,0.7,0,0.5) | |
pts =[x,y,x3,y,x - xwave_factor, y-ywave_factor, x2 + xwave_factor, y-ywave_factor, x2 - xwave_factor, y+ywave_factor, x3 + xwave_factor, y+ywave_factor] | |
until pts.empty? | |
x = pts.shift | |
y = pts.shift | |
cr.rectangle(x-4,y-4,8,8) | |
cr.fill | |
end | |
end | |
surface.write_to_png('birthday-text.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment