Class: Cabird::PrettyTextStyle
- Inherits:
-
Object
- Object
- Cabird::PrettyTextStyle
- Defined in:
- lib/pretty_text_style.rb
Overview
This class represents a style used by the PrettyText
class to generate an image. This style includes font properties like the font-file and the font-size, colors and text transformation like upcase.
Possible style attributes are:
format
-
Can either be
:png
or:gif
, defaults to:png
. If:gif
is used set thebg_color
attribute to get proper antialiasing. color
-
The color of the rendered text. Use HTML/CSS style color definitions (eg.: ##fff, ##d2d2d2). Defaults to ##fff.
bg_color
-
If you choose
:gif
as format the text will be rendered against the givenbg_color
and the areas of the background which are not effected by the antialiasing are cut out. This way you get proper antialiasing, although the gif format does not support alpha channels. size
-
The size of the rendered font in points
font
-
The font file to use relative to the font root directory, which is fonts by default
upcase
-
This flag defines if the text is transformed to uppercase before rendering. This boolean defaults to false.
extra_width
-
Sometimes Imagemagick will calculate the wrong width for the image. You can add some extra space by setting this value. Defaults to 0. This might be obsolete due to an former error in the implementation.
You can set the atrributes via the constructor:
style = Cabird::PrettyTextStyle.new({:format => :png, :color => '#f00'})
or via the setter methods:
style.color = '#000'
Constant Summary collapse
- @@default =
{:format => :png, :color => '#000', :bg_color => '#fff', :size => 12, :font => nil, :upcase => false, :extra_width => 0, :xextra => 0, :xoffset => 0, :yextra => 0, :yoffset => 0, :kerning => 0}
Instance Method Summary collapse
-
#alter(attributes = Hash.new) ⇒ Object
Clones this
PrettyTextStyle
object and returns an altered version of the clone. -
#bg_color ⇒ Object
This method overrides the default getter method for the
bg_color
method. -
#generate_filename(str) ⇒ Object
This method generates a hashed filename of the style attributes and a given text.
-
#generate_hash(str = "") ⇒ Object
Generates a hash string of all attributes and a given string.
-
#initialize(attributes = nil) ⇒ PrettyTextStyle
constructor
Sets up a new
PrettyTextStyle
object. -
#process_text(text) ⇒ Object
Applies all text transformations to a given text, which includes only the
upcase
option at the moment.
Constructor Details
#initialize(attributes = nil) ⇒ PrettyTextStyle
Sets up a new PrettyTextStyle
object. All of the above mentioned attributes can be set here.
69 70 71 |
# File 'lib/pretty_text_style.rb', line 69 def initialize(attributes = nil) @attributes = attributes || Hash.new end |
Instance Method Details
#alter(attributes = Hash.new) ⇒ Object
Clones this PrettyTextStyle
object and returns an altered version of the clone. You can override attributes with the argument.
104 105 106 107 108 109 110 111 |
# File 'lib/pretty_text_style.rb', line 104 def alter(attributes = Hash.new) result = Hash.new @@default.keys.each do |key| result[key] = (attributes[key]) ? attributes[key] : @attributes[key] end return self.class.new(result) end |
#bg_color ⇒ Object
This method overrides the default getter method for the bg_color
method.
74 75 76 77 |
# File 'lib/pretty_text_style.rb', line 74 def bg_color return "transparent" if self.format == :png return @attributes[:bg_color] || @@default[:bg_color] end |
#generate_filename(str) ⇒ Object
This method generates a hashed filename of the style attributes and a given text
97 98 99 100 |
# File 'lib/pretty_text_style.rb', line 97 def generate_filename(str) fragments = self.generate_hash(str).scan(/(..)/).flatten return File.join(fragments) + "." + self.format.to_s end |
#generate_hash(str = "") ⇒ Object
Generates a hash string of all attributes and a given string. This method is used by the generate_filename
method to retrieve the filename for a combilnation of style attributes and a certain text.
88 89 90 91 92 93 94 |
# File 'lib/pretty_text_style.rb', line 88 def generate_hash(str = "") to_hash = str @@default.keys.each do |key| to_hash += self.send(key).to_s end return Digest::MD5.hexdigest(to_hash) end |
#process_text(text) ⇒ Object
Applies all text transformations to a given text, which includes only the upcase
option at the moment. Returns the transformed text.
81 82 83 |
# File 'lib/pretty_text_style.rb', line 81 def process_text(text) return (self.upcase == true) ? Unicode::upcase(text) : text end |