Class: Cabird::PrettyText
- Inherits:
-
Object
- Object
- Cabird::PrettyText
- Defined in:
- lib/pretty_text.rb
Overview
This class represents a rendered text. It provides methods to get the path and the geometry to the generated image. Some parameters of the rendering process can be set using the global Hash PRETTY_TEXT
. These are the configuration keys for PRETTY_TEXT
:
:image_path
-
The public path where the plugin stores the rendered images relative to the public path. Defaults to ‘images/pretty_text’
:scale_threshold
-
Some fonts might look a little bit ugly at smaller sizes.
PrettyText
tries to fix that by rendering small fonts 5 times bigger and scaling the result down to the original size. You can set a threshold for the font size in pts. All fonts smaller than this threshold will be rendered using this scale hack. :scale_filter
-
The ImageMagick filter which is used for resizing. Defaults to
Magick::GaussianFilter
. :scale_support
-
The support argument used by the ImageMagick
resize
method. Defaults to 0.5.
Constant Summary collapse
- SCALE_FACTOR =
5
- @@config =
Hash.new
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Class Method Summary collapse
-
.create(text, style = nil) ⇒ Object
This class method is used to set up a new
PrettyText
object.
Instance Method Summary collapse
-
#height ⇒ Object
Returns the height of the image.
-
#initialize(text, public_path) ⇒ PrettyText
constructor
The constructor is used by the
create
factory method. -
#path ⇒ Object
Returns the relative path of the image starting at the public directory.
-
#to_s ⇒ Object
Returns the string the image is created from.
-
#width ⇒ Object
Return the width of the image.
Constructor Details
#initialize(text, public_path) ⇒ PrettyText
The constructor is used by the create
factory method. You should not use it, but use the create
method instead.
89 90 91 92 |
# File 'lib/pretty_text.rb', line 89 def initialize(text, public_path) @content = text @public_path = public_path end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
29 30 31 |
# File 'lib/pretty_text.rb', line 29 def content @content end |
Class Method Details
.create(text, style = nil) ⇒ Object
This class method is used to set up a new PrettyText
object. If the requested image is not yet created, it generates the image and returns the approprate PrettyText
object.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pretty_text.rb', line 34 def self.create(text, style = nil) text = text.to_s if (style.class.to_s != "Cabird::PrettyTextStyle") style = Cabird::PrettyTextStyle.new(style) end # set up paths public_image_path = @@config[:image_path] || File.join('system' , 'pretty_text') public_path = File.join(public_image_path, style.generate_filename(text)) absolute_path = File.join(RAILS_ROOT, 'public', public_path) # render small fonts bigger and scale down later size = style.size scale_hack = size <= (@@config[:scale_threshold] || 12) size *= SCALE_FACTOR if scale_hack if (!File.exist?(absolute_path)) # create directory FileUtils.mkpath(File.dirname(absolute_path)) unless File.exists?(File.dirname(absolute_path)) gc = Magick::Draw.new gc.font = File.join(RAILS_ROOT, 'fonts', style.font) if style.font gc.pointsize = size #gc.kerning(style.kerning) gc.gravity = Magick::NorthWestGravity gc.fill = style.color text_to_render = style.process_text(text) gc.text(style.xoffset, style.yoffset, text_to_render) metrics = gc.get_multiline_type_metrics(text_to_render) image = Magick::Image.new(metrics.width + style.xextra + style.xoffset, metrics.height + style.yextra + style.yoffset) do self.background_color = style.bg_color end gc.draw(image) # scale down to original size if scale_hack image.resize!(metrics.width / SCALE_FACTOR, metrics.height / SCALE_FACTOR, @@config[:scale_filter] || Magick::GaussianFilter, @@config[:scale_support] || 0.5) end # handle gif transparency if (style.format == :gif) image = image.matte_replace(0, 0) end image.write(absolute_path) end return self.new(text, public_path) end |
Instance Method Details
#height ⇒ Object
Returns the height of the image
106 107 108 109 |
# File 'lib/pretty_text.rb', line 106 def height get_geometry if @height.nil? return @height end |
#path ⇒ Object
Returns the relative path of the image starting at the public directory
95 96 97 |
# File 'lib/pretty_text.rb', line 95 def path return File.join('', @public_path) end |
#to_s ⇒ Object
Returns the string the image is created from
112 113 114 |
# File 'lib/pretty_text.rb', line 112 def to_s return @text end |
#width ⇒ Object
Return the width of the image
100 101 102 103 |
# File 'lib/pretty_text.rb', line 100 def width get_geometry if @width.nil? return @width end |