class

CrImage::Font::Drawer

Inherits Reference / Object

Text rendering engine for drawing text on images.

Drawer handles text rendering with support for kerning, effects, and decorations. Use draw_text for the simple high-level API with x, y coordinates and named parameters for effects.

Example:

# Create drawer
face = FreeType::TrueType.new_face(font, 48.0)
text_color = CrImage::Uniform.new(CrImage::Color::BLACK)
drawer = CrImage::Font::Drawer.new(image, text_color, face)

# Draw text with effects
drawer.draw_text("Hello", 50, 100)
drawer.draw_text("World", 50, 200, underline: true)
drawer.draw_text("Title", 50, 300, shadow: true, outline: true)

Note: Not thread-safe. Create separate Drawer instances for concurrent use.

Constructors

new(dest : CrImage::Image, src : CrImage::Image, face : CrImage::Font::Face, dot : CrImage::Math::Fixed::Point26_6 = Math::Fixed::Point26_6.zero)
Source

Instance methods

bounds(s : String) : Tuple(Math::Fixed::Rectangle26_6, Math::Fixed::Int26_6)

returns the bounding box of s, drawn at the drawer dot, as well as the advance

Source
dest
Source
dot

dot is the baseline location to draw the next glyph. The majority of the affected pixels will be above and to the right of the dot, but some may be below or to the left. For example, drawing a 'j' in an italic face may affect pixels below and to the left of the dot.

Source
dot=(dot : Math::Fixed::Point26_6)

dot is the baseline location to draw the next glyph. The majority of the affected pixels will be above and to the right of the dot, but some may be below or to the left. For example, drawing a 'j' in an italic face may affect pixels below and to the left of the dot.

Source
draw(s : String)
Source
draw_aligned(text : String, box : TextBox)

Draw text aligned within a bounding box. The text will be positioned according to the horizontal and vertical alignment settings of the TextBox.

Parameters:

  • text: The text to render
  • box: TextBox specifying the bounding rectangle and alignment
Source
draw_multiline(text : String, max_width : Int32 | Nil = nil, line_spacing : Float64 = 1.2)

Render multi-line text with word wrapping. This method draws text split across multiple lines.

Parameters:

  • text: The text to render (may contain newline characters)
  • max_width: Optional maximum width for word wrapping (in pixels)
  • line_spacing: Multiplier for line spacing (default 1.2)
Source
draw_styled(text : String, style : TextStyle)

Draw text with effects (shadow, outline, underline, strikethrough). Effects are rendered in the correct order: shadow first, then outline, then text fill, then decorations.

Parameters:

  • text: The text to render
  • style: TextStyle containing optional effects
Source
draw_text(s : String, x : Int32, y : Int32, underline : Bool = false, strikethrough : Bool = false, decoration_color : Color::Color | Nil = nil, shadow : Bool = false, shadow_offset_x : Int32 = 2, shadow_offset_y : Int32 = 2, shadow_blur : Int32 = 3, shadow_color : Color::Color | Nil = nil, outline : Bool = false, outline_thickness : Int32 = 2, outline_color : Color::Color | Nil = nil)

Draw text at specified coordinates with optional visual effects.

This is the primary method for rendering text with decorations and effects. The x, y coordinates specify the baseline position where text rendering begins.

Parameters:

  • s : The text string to render
  • x : Horizontal position (baseline start)
  • y : Vertical position (baseline)

Text Decorations:

  • underline : Add underline below text (default: false)
  • strikethrough : Add line through text (default: false)
  • decoration_color : Color for decorations (default: text color)

Shadow Effect:

  • shadow : Enable drop shadow (default: false)
  • shadow_offset_x : Shadow horizontal offset in pixels (default: 2)
  • shadow_offset_y : Shadow vertical offset in pixels (default: 2)
  • shadow_blur : Shadow blur radius (default: 3)
  • shadow_color : Shadow color (default: semi-transparent black)

Outline Effect:

  • outline : Enable text outline/stroke (default: false)
  • outline_thickness : Outline width in pixels (default: 2)
  • outline_color : Outline color (default: black)

Examples:

# Simple text
drawer.draw_text("Hello", 50, 100)

# Underlined text
drawer.draw_text("Important", 50, 200, underline: true)

# Text with red underline
drawer.draw_text("Error", 50, 300, underline: true, decoration_color: Color::RED)

# Text with shadow
drawer.draw_text("Title", 50, 400, shadow: true)

# Text with outline
drawer.draw_text("Bold", 50, 500, outline: true, outline_thickness: 3)

# Combined effects
drawer.draw_text("Fancy", 50, 600, shadow: true, outline: true, underline: true)
Source
draw_vertical(s : String)

Draw text vertically (top to bottom). Used for CJK vertical text layout. The dot position is the top-center of the first glyph.

Source
draw_vertical_text(s : String, x : Int32, y : Int32)

Draw text vertically at specified coordinates.

This renders text top-to-bottom, useful for CJK vertical layouts. The x, y coordinates specify the starting position for the first glyph.

Parameters:

  • s : The text string to render
  • x : Horizontal position (center of glyphs)
  • y : Vertical position (top of first glyph baseline)

Example:

drawer.draw_vertical_text("HELLO", 100, 50)
Source
face

face provides the glyph mask images.

Source
measure(s : String) : Math::Fixed::Int26_6

returns how far dot would advance by drawing s.

Source
measure_multiline(text : String, max_width : Int32 | Nil = nil, line_spacing : Float64 = 1.2) : TextLayout

Calculate text layout for multi-line text with optional word wrapping. Splits text on newlines and calculates line heights.

Parameters:

  • text: The text to layout (may contain newline characters)
  • max_width: Optional maximum width for word wrapping (in pixels)
  • line_spacing: Multiplier for line spacing (default 1.2)

Returns: TextLayout with line information

Source