From c5bc1fc103916b96ffe9c30e38a2847416a5537d Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Wed, 16 Jul 2014 11:26:48 -0400 Subject: [PATCH] Getting text to work --- bin/squib-eg | 6 ++++-- lib/squib/api/text.rb | 4 ++++ lib/squib/card.rb | 2 +- lib/squib/graphics/save_doc.rb | 1 + lib/squib/graphics/text.rb | 26 ++++++++++++++++++++------ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/bin/squib-eg b/bin/squib-eg index 80f9388..4cfbb33 100755 --- a/bin/squib-eg +++ b/bin/squib-eg @@ -3,14 +3,16 @@ require 'squib' data = {'name' => ['Thief', 'Grifter', 'Mastermind'], 'level' => [1,2,3]} +longtext = "Hello, World! What do you know about tweetle beetles? well... \nWhen tweetle beetles fight, it's called a tweetle beetle battle. And when they battle in a puddle, it's a tweetle beetle puddle battle. AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle. AND... When beetles battle beetles in a puddle paddle battle and the beetle battle puddle is a puddle in a bottle... ..they call this a tweetle beetle bottle puddle paddle battle muddle. AND... When beetles fight these battles in a bottle with their paddles and the bottle's on a poodle and the poodle's eating noodles... ...they call this a muddle puddle tweetle poodle beetle noodle bottle paddle battle." Squib::Deck.new(width: 825, height: 1125, cards: 3) do background color: [1.0,1.0,1.0] rect x: 15, y: 15, width: 795, height: 1095, x_radius: 50, y_radius: 50 rect x: 30, y: 30, width: 150, height: 150, x_radius: 25, y_radius: 25 - text str: data['name'], x: 250, y: 55 - text str: data['level'], x: 65, y: 65 + text str: data['name'], x: 250, y: 55, font: 'Arial 54' + text str: data['level'], x: 65, y: 65, font: 'Arial 72' + text str: longtext, x: 100, y: 600, font: 'Arial 16' save format: :png end diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb index afcc5eb..a3b5912 100644 --- a/lib/squib/api/text.rb +++ b/lib/squib/api/text.rb @@ -1,5 +1,8 @@ module Squib class Deck + def fontify (font) + font + end def font(type: 'Arial', size: 12, **options) end @@ -10,6 +13,7 @@ module Squib def text(range: :all, str: '', font: :use_set, x: 0, y: 0, **options) range = rangeify(range) str = [str] * @cards.size unless str.respond_to? :each + font = fontify(font) range.each do |i| cards[i].text(str[i], font, x, y, options) end diff --git a/lib/squib/card.rb b/lib/squib/card.rb index 4dc1f1b..51d6cad 100644 --- a/lib/squib/card.rb +++ b/lib/squib/card.rb @@ -7,7 +7,7 @@ module Squib attr_accessor :cairo_surface, :cairo_context def initialize(width, height) - @width=width, @height=height + @width=width; @height=height @cairo_surface = Cairo::ImageSurface.new(width,height) @cairo_context = Cairo::Context.new(@cairo_surface) end diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index 0f34ede..8add2e5 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/lib/squib/graphics/save_doc.rb @@ -4,6 +4,7 @@ module Squib def save_pdf #paper is 8.5x11 # PDF points are 1/72 of an inch + raise "Not implemented yet!" width = 8.5 * 72 height = 11 * 72 cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height)) diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index 93ccfa3..2f38a82 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -1,13 +1,27 @@ +require 'pango' + module Squib class Card - + def text(str, font, x, y, options) cc = cairo_context - cc.set_source_rgb(0.0,0.0,0.0) - cc.select_font_face ("Helvetica"); - cc.set_font_size(36) - cc.move_to(x,y + cc.text_extents(@str.to_s).height) - cc.show_text(str.to_s) + cc.set_source_color(:black) #black + cc.move_to(x,y) + layout = cc.create_pango_layout + layout.text = str.to_s + w = options[:width] ; h = options[:height] + w ||= (@width - 2*x) ; h ||= (@height - 2*y) # default centers to x,y + w *= Pango::SCALE ; h *= Pango::SCALE + layout.width=w ; layout.height=h + layout.wrap = Pango::WRAP_WORD + layout.justify = true + layout.alignment = Pango::ALIGN_LEFT + layout.font_description = Pango::FontDescription.new(font) + #Center it vertically? + #puts "Height: #{layout.extents[1].height / Pango::SCALE}" + #puts "Height: #{layout.pixel_size[1]*layout.line_count}" + cc.update_pango_layout(layout) + cc.show_pango_layout(layout) end end