Skip to content

Instantly share code, notes, and snippets.

@geoffyoungs
Created March 11, 2011 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffyoungs/866223 to your computer and use it in GitHub Desktop.
Save geoffyoungs/866223 to your computer and use it in GitHub Desktop.
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
def bling_text(cr, text, font, x, y)
cr.save do
cr.translate(x,y)
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.2,0.6,1.0], [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
## end of bling
end
end
def speech_bubble(cr, text, font, x, y, width, tag_pos=:bottom_left, tag_size=50)
cr.save do
cr.translate(x, y)
cr.set_line_join(:round)
layout = cr.create_pango_layout
layout.set_text(text)
layout.set_font_description(font)
border_radius = 8
layout.set_width(Pango::SCALE * (width - (2 * border_radius)))
ink, log = layout.get_pixel_extents
total_width = ink.width + (border_radius * 2)
straight_edge = ink.width #- (border_radius * 2)
straight_height = ink.height #- (border_radius * 2)
cr.new_path
cr.move_to(0 + border_radius, 0)
# top line filled in automatically
cr.arc(0 + border_radius + straight_edge, border_radius, border_radius, - Math::PI/2, 0)
# right line filled in automatically
cr.arc(0 + border_radius + straight_edge, border_radius + straight_height, border_radius, 0, Math::PI/2)
# base line filled in automatically
if tag_pos == :bottom_left
tag_base_width = total_width / 10
by = (2 * border_radius) + straight_height
x1,x2 = border_radius, border_radius + tag_base_width
cr.line_to(x2, by)
cr.curve_to(x2 - (tag_base_width/2), by + (tag_size / 2),
x2 - (0.2 * tag_base_width), by + (tag_size*0.9),
x2, by + tag_size)
cr.curve_to(x1 + (tag_base_width * 0.3), by + (tag_size*0.75),
x1, by + (tag_size / 2),
x1, by)
elsif tag_pos == :bottom_right
tag_base_width = total_width / 10
by = (2 * border_radius) + straight_height
x1,x2 = border_radius - tag_base_width + straight_edge, border_radius + straight_edge
cr.line_to(x2, by)
cr.curve_to(x2, by + (tag_size / 2), # + (tag_base_width/2)
x2 - (0.2 * tag_base_width), by + (tag_size*0.75),
x1, by + tag_size)
cr.curve_to(x1 + (tag_base_width * 0.3), by + (tag_size*0.9),
x1 + (tag_base_width/2), by + (tag_size / 2),
x1, by)
end
cr.arc(0 + border_radius, border_radius + straight_height, border_radius, Math::PI/2, Math::PI)
# left line filled in automatically
cr.arc(0 + border_radius, border_radius, border_radius, Math::PI, - Math::PI/2)
cr.set_source_rgb(1,1,1)
cr.fill_preserve
cr.set_source_rgb(0,0,0)
cr.stroke
cr.move_to(border_radius - ink.x, border_radius - ink.y)
cr.show_pango_layout(layout)
end
end
text = "Birthday"
font = Pango::FontDescription.new("Droid Sans Bold 56")
bling_text(cr, "Textual Bling", font, 0, 320)
speech_bubble(cr, "How dare you say such a hurtful crazy thing?!!!",
Pango::FontDescription.new("Marker Felt 18"), 20, 20, 270, :bottom_left, 20)
speech_bubble(cr, "I was only responding to your foolish talk...",
Pango::FontDescription.new("Marker Felt 18"), 300, 90, 250, :bottom_right, 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment