Browse Source

Getting text to work

dev
Andy Meneely 12 years ago
parent
commit
c5bc1fc103
  1. 6
      bin/squib-eg
  2. 4
      lib/squib/api/text.rb
  3. 2
      lib/squib/card.rb
  4. 1
      lib/squib/graphics/save_doc.rb
  5. 26
      lib/squib/graphics/text.rb

6
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

4
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

2
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

1
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))

26
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

Loading…
Cancel
Save