Getting text to work
parent
9c51b03783
commit
c5bc1fc103
|
|
@ -3,14 +3,16 @@ require 'squib'
|
||||||
|
|
||||||
data = {'name' => ['Thief', 'Grifter', 'Mastermind'],
|
data = {'name' => ['Thief', 'Grifter', 'Mastermind'],
|
||||||
'level' => [1,2,3]}
|
'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
|
Squib::Deck.new(width: 825, height: 1125, cards: 3) do
|
||||||
background color: [1.0,1.0,1.0]
|
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: 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
|
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['name'], x: 250, y: 55, font: 'Arial 54'
|
||||||
text str: data['level'], x: 65, y: 65
|
text str: data['level'], x: 65, y: 65, font: 'Arial 72'
|
||||||
|
text str: longtext, x: 100, y: 600, font: 'Arial 16'
|
||||||
|
|
||||||
save format: :png
|
save format: :png
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
module Squib
|
module Squib
|
||||||
class Deck
|
class Deck
|
||||||
|
def fontify (font)
|
||||||
|
font
|
||||||
|
end
|
||||||
|
|
||||||
def font(type: 'Arial', size: 12, **options)
|
def font(type: 'Arial', size: 12, **options)
|
||||||
end
|
end
|
||||||
|
|
@ -10,6 +13,7 @@ module Squib
|
||||||
def text(range: :all, str: '', font: :use_set, x: 0, y: 0, **options)
|
def text(range: :all, str: '', font: :use_set, x: 0, y: 0, **options)
|
||||||
range = rangeify(range)
|
range = rangeify(range)
|
||||||
str = [str] * @cards.size unless str.respond_to? :each
|
str = [str] * @cards.size unless str.respond_to? :each
|
||||||
|
font = fontify(font)
|
||||||
range.each do |i|
|
range.each do |i|
|
||||||
cards[i].text(str[i], font, x, y, options)
|
cards[i].text(str[i], font, x, y, options)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ module Squib
|
||||||
attr_accessor :cairo_surface, :cairo_context
|
attr_accessor :cairo_surface, :cairo_context
|
||||||
|
|
||||||
def initialize(width, height)
|
def initialize(width, height)
|
||||||
@width=width, @height=height
|
@width=width; @height=height
|
||||||
@cairo_surface = Cairo::ImageSurface.new(width,height)
|
@cairo_surface = Cairo::ImageSurface.new(width,height)
|
||||||
@cairo_context = Cairo::Context.new(@cairo_surface)
|
@cairo_context = Cairo::Context.new(@cairo_surface)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ module Squib
|
||||||
def save_pdf
|
def save_pdf
|
||||||
#paper is 8.5x11
|
#paper is 8.5x11
|
||||||
# PDF points are 1/72 of an inch
|
# PDF points are 1/72 of an inch
|
||||||
|
raise "Not implemented yet!"
|
||||||
width = 8.5 * 72
|
width = 8.5 * 72
|
||||||
height = 11 * 72
|
height = 11 * 72
|
||||||
cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height))
|
cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height))
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,27 @@
|
||||||
|
require 'pango'
|
||||||
|
|
||||||
module Squib
|
module Squib
|
||||||
class Card
|
class Card
|
||||||
|
|
||||||
def text(str, font, x, y, options)
|
def text(str, font, x, y, options)
|
||||||
cc = cairo_context
|
cc = cairo_context
|
||||||
cc.set_source_rgb(0.0,0.0,0.0)
|
cc.set_source_color(:black) #black
|
||||||
cc.select_font_face ("Helvetica");
|
cc.move_to(x,y)
|
||||||
cc.set_font_size(36)
|
layout = cc.create_pango_layout
|
||||||
cc.move_to(x,y + cc.text_extents(@str.to_s).height)
|
layout.text = str.to_s
|
||||||
cc.show_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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue