Skip to content

Commit

Permalink
Can set font for TextBlock using :family style
Browse files Browse the repository at this point in the history
  • Loading branch information
davorb committed Oct 27, 2012
1 parent 90a2d27 commit 644cd26
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
16 changes: 8 additions & 8 deletions lib/shoes/element_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def rgb(red, green, blue, alpha = Shoes::Color::OPAQUE)
def stroke(color)
@style[:stroke] = color
end

def nostroke
@style[:stroke] = nil
end
Expand All @@ -292,7 +292,7 @@ def strokewidth(width)
def fill(color)
@style[:fill] = color
end

def nofill
@style[:fill] = nil
end
Expand Down Expand Up @@ -325,7 +325,7 @@ def style(new_styles = {})
eval "Shoes::#{m.capitalize}.new(@current_slot, text, #{m.upcase}_FONT_SIZE, opts)"
end
end

def get_styles msg, styles=[], spoint=0
msg.each do |e|
if e.is_a? Shoes::Text
Expand All @@ -337,28 +337,28 @@ def get_styles msg, styles=[], spoint=0
end
styles
end

[:code, :del, :em, :ins, :strong, :sub, :sup].each do |m|
define_method m do |*str|
Shoes::Text.new m, str
end
end

[:bg, :fg].each do |m|
define_method m do |*str|
color = str.pop
Shoes::Text.new m, str, color
end
end

def mouse
[@app.mouse_button, @app.mouse_pos[0], @app.mouse_pos[1]]
end

def motion &blk
@app.mouse_motion << blk
end

def keypress &blk
opts = {:app => @app}
Shoes::Keypress.new opts, &blk
Expand Down
32 changes: 30 additions & 2 deletions lib/shoes/text_block.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'shoes/common_methods'

module Shoes
DEFAULT_TEXTBLOCK_FONT = "Arial"

class TextBlock
include Shoes::CommonMethods

Expand All @@ -9,12 +11,14 @@ class TextBlock

def initialize(parent, text, font_size, opts = {})
@parent = parent
@font = 'sans'
@font = DEFAULT_TEXTBLOCK_FONT
@font_size = font_size
@text = text
@left = opts[:left]
@top = opts[:top]

handle_opts opts

@gui = Shoes.configuration.backend_for(self, opts)
if text.split.length == 1
@width, @height = @gui.get_size
Expand Down Expand Up @@ -43,11 +47,35 @@ def positioning x, y, max
set_size @left.to_i, @left.to_i unless @fixed
super
end

def set_size left, top
@width = (left + @parent.width <= app.width) ? @parent.width : app.width - left
@height = @gui.get_height
end

private

def handle_opts(opts)
if opts.has_key? :family
parse_font_opt opts[:family]
end
end

def parse_font_opt(type)
size_regex = /\d+(?=px)?/
style_regex = /none|bold|normal|oblique|italic/i # TODO: add more

ffamily = type.gsub(style_regex,'').gsub(/\d+(px)?/,'').
split(',').map { |x| x.strip.gsub(/["]/,'') }

@font = ffamily.first unless (ffamily.size == 1 and
ffamily[0] == "") or ffamily.size == 0

fsize = type.scan(size_regex)
@font_size = fsize.first.to_i unless fsize.empty?

# TODO: Style options
end
end

class Banner < TextBlock; end
Expand Down
24 changes: 24 additions & 0 deletions spec/shoes/text_block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@
subject.text = "Goodbye Cruel World"
end
end

describe "font" do
it "sets the default font to Arial" do
subject.font.should eql "Arial"
end

it "should allow setting the font with :family" do
s = Shoes::TextBlock.new(parent, "Hello, world!", 99, { family: "Helvetica" })
s.font.should eql "Helvetica"
end

it "should allow setting the font size with :family" do
s = Shoes::TextBlock.new(parent, "Hello, world!", 99, { family: "Helvetica 33px" })
s.font.should eql "Helvetica"
s.font_size.should eql 33
end

it "should accept fonts surrounded with questionmarks when using :family" do
s = Shoes::TextBlock.new(parent, "Hello, world!", 99, { family: '"Comic Sans" 13px' })
s.font.should eql "Comic Sans"
s.font_size.should eql 13
end

end
end

0 comments on commit 644cd26

Please sign in to comment.