Class: MtgCardMaker::TextRenderingService

Inherits:
Object
  • Object
show all
Defined in:
lib/mtg_card_maker/text_rendering_service.rb

Overview

Minimal text rendering service with basic word wrapping for SVG. This service handles text layout, wrapping, and attribute generation for SVG text elements with configurable font sizes, colors, and spacing.

Examples:

service = MtgCardMaker::TextRenderingService.new(
  text: "Lightning Bolt deals 3 damage to any target.",
  x: 40, y: 545, font_size: 24, available_width: 550
)
lines = service.wrapped_text_lines

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ TextRenderingService

Initialize a new text rendering service

Parameters:

  • kwargs (Hash)

    the initialization parameters

Options Hash (**kwargs):

  • :text (String)

    the text to render (default: '')

  • :layer_config (LayerConfig)

    the layer configuration (default: LayerConfig.default)

  • :x (Integer)

    the x-coordinate (default: 0)

  • :y (Integer)

    the y-coordinate (default: 0)

  • :font_size (Integer)

    the font size (default: from layer_config)

  • :color (String)

    the text color (default: from layer_config)

  • :available_width (Integer)

    the available width (default: CARD_WIDTH)

  • :line_height (Integer)

    the line height (default: calculated)

  • :css_class (String)

    the CSS class (default: nil)

Since:

  • 0.1.0



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 56

def initialize(**kwargs)
  # Set defaults
  defaults = {
    text: '',
    layer_config: LayerConfig.default,
    x: 0,
    y: 0,
    font_size: nil,
    color: nil,
    available_width: nil,
    line_height: nil,
    css_class: nil
  }

  # Merge defaults with provided kwargs
  final_params = defaults.merge(kwargs)

  # Set instance variables
  final_params.each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end

  # Post-process dependent values
  @font_size ||= @layer_config.default_font_size
  @color ||= @layer_config.default_text_color
  @available_width ||= CARD_WIDTH
  @line_height ||= calculate_default_line_height
end

Instance Attribute Details

#available_widthInteger

Returns the available width for text wrapping.

Returns:

  • (Integer)

    the available width for text wrapping

Since:

  • 0.1.0



36
37
38
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 36

def available_width
  @available_width
end

#colorString

Returns the text color.

Returns:

  • (String)

    the text color

Since:

  • 0.1.0



33
34
35
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 33

def color
  @color
end

#css_classString?

Returns the CSS class for styling.

Returns:

  • (String, nil)

    the CSS class for styling

Since:

  • 0.1.0



42
43
44
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 42

def css_class
  @css_class
end

#font_sizeInteger

Returns the font size.

Returns:

  • (Integer)

    the font size

Since:

  • 0.1.0



30
31
32
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 30

def font_size
  @font_size
end

#layer_configLayerConfig

Returns the layer configuration.

Returns:

Since:

  • 0.1.0



21
22
23
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 21

def layer_config
  @layer_config
end

#line_heightInteger

Returns the line height.

Returns:

  • (Integer)

    the line height

Since:

  • 0.1.0



39
40
41
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 39

def line_height
  @line_height
end

#textString

Returns the text to render.

Returns:

  • (String)

    the text to render

Since:

  • 0.1.0



18
19
20
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 18

def text
  @text
end

#xInteger

Returns the x-coordinate for text positioning.

Returns:

  • (Integer)

    the x-coordinate for text positioning

Since:

  • 0.1.0



24
25
26
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 24

def x
  @x
end

#yInteger

Returns the y-coordinate for text positioning.

Returns:

  • (Integer)

    the y-coordinate for text positioning

Since:

  • 0.1.0



27
28
29
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 27

def y
  @y
end

Class Method Details

.wrapped_text_linesObject

Convenience method for backward compatibility

Since:

  • 0.1.0



155
156
157
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 155

def wrapped_text_lines(*, **)
  new(*, **).wrapped_text_lines
end

Instance Method Details

#wrapped_text_lines(text = nil) ⇒ Array<Array>

Returns an array of [line, attrs] for SVG text elements

Parameters:

  • text (String, nil) (defaults to: nil)

    optional text to override current text

Returns:

  • (Array<Array>)

    array of [line_text, attributes_hash] pairs

Since:

  • 0.1.0



89
90
91
92
# File 'lib/mtg_card_maker/text_rendering_service.rb', line 89

def wrapped_text_lines(text = nil)
  @text = text if text
  render_text_lines
end