Class: MiniGL::ImageFont
- Inherits:
-
Object
- Object
- MiniGL::ImageFont
- Defined in:
- lib/minigl/text.rb
Overview
This class represents a font and exposes most of the methods from Gosu::Font
, but allows the font to be created from an image, allowing for better customization and also using the retro
option. Moreover, this class can be passed to the TextHelper
constructor as if it was a Gosu::Font
.
The image used to load the font must meet these criteria:
-
The characters should be laid out in lines of the same height in pixels.
-
The full image must have a height that is a multiple of that line height.
-
The characters should occupy the maximum available space in each line, i.e., if a character fits in the current line it must not be placed in the next one. In the last line there can be any amount of free space at the end.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
The height of this font in pixels.
Instance Method Summary collapse
-
#draw_markup(text, x, y, z, scale_x, scale_y, color) ⇒ Object
(also: #draw_text)
See
Gosu::Font#draw_markup
for details. -
#draw_markup_rel(text, x, y, z, rel_x, rel_y, scale_x, scale_y, color) ⇒ Object
(also: #draw_text_rel)
See
Gosu::Font#draw_markup_rel
for details. -
#initialize(img_path, chars, widths, height, space_width, global = true, ext = '.png', retro = nil) ⇒ ImageFont
constructor
Creates an
ImageFont
. -
#markup_width(text) ⇒ Object
(also: #text_width)
Returns the width, in pixels, of a given string written by this font.
Constructor Details
#initialize(img_path, chars, widths, height, space_width, global = true, ext = '.png', retro = nil) ⇒ ImageFont
Creates an ImageFont
.
Parameters:
- img_path
-
Identifier of an image fitting the description in the class documentation, as used in
Res.img
. - chars
-
A string containing all characters that will be present in the image, in the same order as they appear in the image. Do not include white space.
- widths
-
An integer representing the width of the chars in pixels, if this is a fixed width font, or an array containing the width of each char, in the same order as they appear in the
chars
string. - height
-
The height of the lines in the image (see description above).
- space_width
-
The width of the white space character in this font.
- global
-
Parameter that will be passed to
Res.img
when loading the image. - ext
-
Parameter that will be passed to
Res.img
when loading the image. - retro
-
Parameter that will be passed to
Res.img
when loading the image.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/minigl/text.rb', line 32 def initialize(img_path, chars, widths, height, space_width, global = true, ext = '.png', retro = nil) retro = Res.retro_images if retro.nil? img = Res.img(img_path, global, false, ext, retro) @chars = chars @images = [] @height = height @space_width = space_width wa = widths.is_a?(Array) if wa && widths.length != chars.length raise 'Wrong widths array size!' end x = y = 0 (0...chars.length).each do |i| @images.push(img.subimage(x, y, wa ? widths[i] : widths, height)) new_x = x + (wa ? widths[i] : widths) if i < chars.length - 1 && new_x + (wa ? widths[i+1] : widths) > img.width x = 0 y += height else x = new_x end end end |
Instance Attribute Details
#height ⇒ Object (readonly)
The height of this font in pixels.
15 16 17 |
# File 'lib/minigl/text.rb', line 15 def height @height end |
Instance Method Details
#draw_markup(text, x, y, z, scale_x, scale_y, color) ⇒ Object Also known as: draw_text
See Gosu::Font#draw_markup
for details. Note: Markup is not supported, this method is named this way to match Gosu::Font
‘s signature.
92 93 94 |
# File 'lib/minigl/text.rb', line 92 def draw_markup(text, x, y, z, scale_x, scale_y, color) draw_markup_rel(text, x, y, z, 0, 0, scale_x, scale_y, color) end |
#draw_markup_rel(text, x, y, z, rel_x, rel_y, scale_x, scale_y, color) ⇒ Object Also known as: draw_text_rel
See Gosu::Font#draw_markup_rel
for details. Note: Markup is not supported, this method is named this way to match Gosu::Font
‘s signature.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/minigl/text.rb', line 69 def draw_markup_rel(text, x, y, z, rel_x, rel_y, scale_x, scale_y, color) text = text.to_s unless text.is_a?(String) if rel_x != 0 x -= scale_x * markup_width(text) * rel_x end if rel_y != 0 y -= scale_y * @height * rel_y end text.each_char do |c| if c == ' ' x += scale_x * @space_width next end i = @chars.index(c) next if i.nil? @images[i].draw(x, y, z, scale_x, scale_y, color) x += scale_x * @images[i].width end end |
#markup_width(text) ⇒ Object Also known as: text_width
Returns the width, in pixels, of a given string written by this font. Note: Markup is not supported, this method is named this way to match Gosu::Font
‘s signature.
Parameters:
- text
-
The string to be measured
62 63 64 |
# File 'lib/minigl/text.rb', line 62 def markup_width(text) text.chars.reduce(0) { |w, c| if c == ' '; w += @space_width; else; i = @chars.index(c); w += i ? @images[i].width : 0; end } end |