class Cura::Component::Label

A component displaying text.

Public Class Methods

new(attributes={}) click to toggle source

Note that you can pass the following:

alignment: { horizontal: true, vertical: true }

instead of:

horizontal_alignment: true, vertical_alignment: true
Calls superclass method Cura::Attributes::HasAttributes::new
# File lib/cura/component/label.rb, line 16
def initialize(attributes={})
  @horizontal_alignment = :left
  @vertical_alignment = :top
  @bold = false
  @underline = false
  @text = ""

  super
end

Public Instance Methods

draw() click to toggle source
Calls superclass method Cura::Component::Base#draw
# File lib/cura/component/label.rb, line 132
def draw
  super
  return self unless @draw

  draw_text unless text.empty?
end
height() click to toggle source

Get the height of this label.

@return [Integer]

# File lib/cura/component/label.rb, line 40
def height
  return text_height if @height == :auto

  @height
end
lines() click to toggle source

Get the lines of this label.

@return [<String>]

# File lib/cura/component/label.rb, line 61
def lines
  @text.split("\n") # NOTE: Would use String#lines but it's output doesn't think a trailing newline character constitutes a line unless it is followed by another character. #split also removes the newline characters.
end
text_height() click to toggle source

Get the height of the text of this label.

@return [Integer]

# File lib/cura/component/label.rb, line 77
def text_height
  value = lines.length

  value == 0 ? 1 : value
end
text_width() click to toggle source

Get the width of the text of this label.

@return [Integer]

# File lib/cura/component/label.rb, line 68
def text_width
  return 0 if @text.empty?

  lines.collect(&:length).sort.last
end
width() click to toggle source

Get the width of this label.

@return [Integer]

# File lib/cura/component/label.rb, line 31
def width
  return text_width if @width == :auto

  @width
end

Protected Instance Methods

character_to_draw(character) click to toggle source

Helper method for subclasses

# File lib/cura/component/label.rb, line 147
def character_to_draw(character)
  character
end
convert_attributes(attributes={}) click to toggle source

TODO: Just use a alignment attribute and have a Cura::Alignment object?

# File lib/cura/component/label.rb, line 193
def convert_attributes(attributes={})
  attributes = super

  if attributes.key?(:alignment)
    alignment_attributes = attributes.delete(:alignment).to_h

    attributes[:horizontal_alignment] = alignment_attributes[:horizontal] if alignment_attributes.key?(:horizontal)
    attributes[:vertical_alignment] = alignment_attributes[:vertical] if alignment_attributes.key?(:vertical)
  end

  attributes
end
convert_horizontal_alignment_attribute(value) click to toggle source
# File lib/cura/component/label.rb, line 206
def convert_horizontal_alignment_attribute(value)
  value = value.to_sym
  raise ArgumentError, "must be :left, :center, or :right" unless [:left, :center, :right].include?(value)

  value
end
convert_vertical_alignment_attribute(value) click to toggle source
# File lib/cura/component/label.rb, line 213
def convert_vertical_alignment_attribute(value)
  value = value.to_sym
  raise ArgumentError, "must be :top, :center, or :bottom" unless [:top, :center, :bottom].include?(value)

  value
end
draw_text() click to toggle source

TODO: Should use instance vars

# File lib/cura/component/label.rb, line 152
def draw_text
  x_offset = x_offset_start = x_offset_from_alignment # + @offsets.left
  y_offset = y_offset_from_alignment # + @offsets.top
  absolute_x = self.absolute_x
  absolute_y = self.absolute_y

  text_to_draw.each_char do |character|
    if character == "\n" # TODO: If multiline? Also check if outside the bounds of the drawing area
      x_offset = x_offset_start

      y_offset += 1
    else
      unless x_offset > width || y_offset > height
        character = character_to_draw(character)
        draw_character(x_offset, y_offset, character, foreground, background, @bold, @underline)
      end

      x_offset += 1
    end
  end
end
text_to_draw() click to toggle source

Helper method for subclasses

# File lib/cura/component/label.rb, line 142
def text_to_draw
  @text
end
x_offset_from_alignment() click to toggle source
# File lib/cura/component/label.rb, line 174
def x_offset_from_alignment
  case horizontal_alignment
    when :left   then 0
    when :center then ((text_width - width).abs / 2).to_i
    when :right  then (text_width - width).abs
  end
end
y_offset_from_alignment() click to toggle source
# File lib/cura/component/label.rb, line 182
def y_offset_from_alignment
  case vertical_alignment
    when :top    then 0
    when :center then ((text_height - height).abs / 2).to_i
    when :bottom then (text_height - height).abs
  end
end