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
-
#char_spacing ⇒ Object
readonly
The spacing between characters, in pixels (can be a decimal number).
-
#chars ⇒ Object
readonly
A string containing the characters supported by this font.
-
#height ⇒ Object
readonly
The height of this font in pixels.
-
#space_width ⇒ Object
readonly
The width of the white space character in 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, char_spacing = 0, 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, char_spacing = 0, 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.
- char_spacing
-
The spacing between non-white-space characters when writing text with this font. It can be a decimal number, useful when scaling the text up.
- 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/minigl/text.rb', line 43 def initialize(img_path, chars, widths, height, space_width, char_spacing = 0, 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 @char_spacing = char_spacing 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
#char_spacing ⇒ Object (readonly)
The spacing between characters, in pixels (can be a decimal number).
24 25 26 |
# File 'lib/minigl/text.rb', line 24 def char_spacing @char_spacing end |
#chars ⇒ Object (readonly)
A string containing the characters supported by this font.
15 16 17 |
# File 'lib/minigl/text.rb', line 15 def chars @chars end |
#height ⇒ Object (readonly)
The height of this font in pixels.
18 19 20 |
# File 'lib/minigl/text.rb', line 18 def height @height end |
#space_width ⇒ Object (readonly)
The width of the white space character in this font, in pixels.
21 22 23 |
# File 'lib/minigl/text.rb', line 21 def space_width @space_width 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.
114 115 116 |
# File 'lib/minigl/text.rb', line 114 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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/minigl/text.rb', line 91 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 + @char_spacing)).round 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
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/minigl/text.rb', line 74 def markup_width(text) w = 0 text.chars.each_with_index do |c, i| if c == ' ' w += @space_width else idx = @chars.index(c) w += idx ? @images[idx].width : 0 w += @char_spacing if i < text.chars.size - 1 end end w end |