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
Instance methods
returns the bounding box of s, drawn at the drawer dot, as well as the advance
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.
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.
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
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)
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
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 renderx: 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)
Draw text vertically (top to bottom). Used for CJK vertical text layout. The dot position is the top-center of the first glyph.
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 renderx: Horizontal position (center of glyphs)y: Vertical position (top of first glyph baseline)
Example:
drawer.draw_vertical_text("HELLO", 100, 50)
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