class MiniGL::ImageFont
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.
Attributes
The spacing between characters, in pixels (can be a decimal number).
A string containing the characters supported by this font.
The height of this font in pixels.
The width of the white space character in this font, in pixels.
Public Class Methods
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.
# 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
Public Instance Methods
See Gosu::Font#draw_markup
for details. Note: Markup is not supported, this method is named this way to match Gosu::Font
‘s signature.
# 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
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.
# 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
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
# 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