Browse Source

Added valign, renaming text

dev
Andy Meneely 12 years ago
parent
commit
a6eed0ad3d
  1. 22
      lib/squib/graphics/text.rb
  2. 13
      samples/save-pdf.rb
  3. 6
      samples/text-options.rb

22
lib/squib/graphics/text.rb

@ -6,10 +6,11 @@ module Squib
def draw_text_hint(x,y,layout, color) def draw_text_hint(x,y,layout, color)
return if color.nil? && @deck.text_hint.nil? return if color.nil? && @deck.text_hint.nil?
color ||= @deck.text_hint color ||= @deck.text_hint
# when w,h < 0, it was never set. extents[0] are ink extents
w = layout.width / Pango::SCALE w = layout.width / Pango::SCALE
w = layout.extents[1].width / Pango::SCALE if w < 0 #i.e. was never set w = layout.extents[1].width / Pango::SCALE if w < 0
h = layout.height / Pango::SCALE h = layout.height / Pango::SCALE
h = layout.extents[1].height / Pango::SCALE if h < 0 #i.e. was never set h = layout.extents[1].height / Pango::SCALE if h < 0
draw_rectangle(x,y,w,h,0,0,color) draw_rectangle(x,y,w,h,0,0,color)
end end
@ -50,6 +51,17 @@ module Squib
layout layout
end end
def valign(cc, layout, extents, x, y, valign)
if layout.height > 0
case valign
when :middle
cc.move_to(x, y + (layout.height - extents.height) / (2 * Pango::SCALE))
when :bottom
cc.move_to(x, y + (layout.height - extents.height) / Pango::SCALE)
end
end
end
def setwh(layout, options) def setwh(layout, options)
layout.width = options[:width] * Pango::SCALE unless options[:width].nil? layout.width = options[:width] * Pango::SCALE unless options[:width].nil?
layout.height = options[:height] * Pango::SCALE unless options[:height].nil? layout.height = options[:height] * Pango::SCALE unless options[:height].nil?
@ -69,6 +81,7 @@ module Squib
cc.set_source_color(color) cc.set_source_color(color)
cc.move_to(x,y) cc.move_to(x,y)
layout = cc.create_pango_layout layout = cc.create_pango_layout
layout.font_description = Pango::FontDescription.new(font)
layout.text = str.to_s layout.text = str.to_s
layout.markup = str.to_s if options[:markup] layout.markup = str.to_s if options[:markup]
layout = setwh(layout, options) unless options[:width].nil? && options[:height].nil? layout = setwh(layout, options) unless options[:width].nil? && options[:height].nil?
@ -77,9 +90,10 @@ module Squib
layout = ellipsize(layout, options) layout = ellipsize(layout, options)
layout = align(layout, options) layout = align(layout, options)
layout.justify = options[:justify] unless options[:justify].nil? layout.justify = options[:justify] unless options[:justify].nil?
layout.font_description = Pango::FontDescription.new(font) logical_extents = layout.extents[0]
cc.update_pango_layout(layout) cc.update_pango_layout(layout)
cc.show_pango_layout(layout) valign(cc, layout, logical_extents, x,y, options[:valign])
cc.update_pango_layout(layout) ; cc.show_pango_layout(layout)
draw_text_hint(x,y,layout,options[:hint]) draw_text_hint(x,y,layout,options[:hint])
end end

13
samples/save-pdf.rb

@ -0,0 +1,13 @@
#!/usr/bin/env ruby
require 'squib'
Squib::Deck.new(width: 825, height: 1125, cards: 16) do
background color: :gray
rect x: 38, y: 38, width: 750, height: 1050, x_radius: 38, y_radius: 38
text str: (1..16).to_a, x: 220, y: 78, font: 'Arial 54'
save_pdf file: "sample.pdf", margin: 50, gap: 5, trim: 38
end
puts "Done!"

6
samples/text.rb → samples/text-options.rb

@ -19,12 +19,12 @@ Squib::Deck.new(width: 825, height: 1125, cards: 3) do
text str: "Font strings are expressive!", x:65, y: 300, text str: "Font strings are expressive!", x:65, y: 300,
font: 'Arial,Verdana weight=900 style=oblique 36' font: 'Arial,Verdana weight=900 style=oblique 36'
text str: "This text has fixed width, fixed height, center-aligned, and has a red hint", text str: "This text has fixed width, fixed height, center-aligned, middle-valigned, and has a red hint",
hint: :red, hint: :red,
x: 65, y: 400, x: 65, y: 400,
width: 300, height: 200, width: 300, height: 200,
align: :center, align: :center, valign: :middle,
font: 'Arial 24' font: 'Arial 18'
text str: "Ellipsization!\nThe ultimate question of life, the universe, and everything to life and everything is 42", text str: "Ellipsization!\nThe ultimate question of life, the universe, and everything to life and everything is 42",
hint: :green, font: 'Arial 22', hint: :green, font: 'Arial 22',
Loading…
Cancel
Save