diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index 4873329..f8b655e 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -6,10 +6,11 @@ module Squib def draw_text_hint(x,y,layout, color) return if color.nil? && @deck.text_hint.nil? color ||= @deck.text_hint + # when w,h < 0, it was never set. extents[0] are ink extents 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.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) end @@ -50,6 +51,17 @@ module Squib layout 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) layout.width = options[:width] * Pango::SCALE unless options[:width].nil? layout.height = options[:height] * Pango::SCALE unless options[:height].nil? @@ -69,6 +81,7 @@ module Squib cc.set_source_color(color) cc.move_to(x,y) layout = cc.create_pango_layout + layout.font_description = Pango::FontDescription.new(font) layout.text = str.to_s layout.markup = str.to_s if options[:markup] layout = setwh(layout, options) unless options[:width].nil? && options[:height].nil? @@ -77,9 +90,10 @@ module Squib layout = ellipsize(layout, options) layout = align(layout, options) layout.justify = options[:justify] unless options[:justify].nil? - layout.font_description = Pango::FontDescription.new(font) - cc.update_pango_layout(layout) - cc.show_pango_layout(layout) + logical_extents = layout.extents[0] + cc.update_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]) end diff --git a/samples/save-pdf.rb b/samples/save-pdf.rb new file mode 100644 index 0000000..a1e6aa8 --- /dev/null +++ b/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!" \ No newline at end of file diff --git a/samples/text.rb b/samples/text-options.rb similarity index 95% rename from samples/text.rb rename to samples/text-options.rb index bc134c2..1cdd4da 100644 --- a/samples/text.rb +++ b/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, 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, x: 65, y: 400, width: 300, height: 200, - align: :center, - font: 'Arial 24' + align: :center, valign: :middle, + font: 'Arial 18' text str: "Ellipsization!\nThe ultimate question of life, the universe, and everything to life and everything is 42", hint: :green, font: 'Arial 22',