Class: SilkLayout::Layout::TextBox

Inherits:
InlineBox show all
Defined in:
lib/silk_layout/layout/inline.rb

Instance Attribute Summary collapse

Attributes inherited from Box

#background_color, #border, #border_color, #display, #explicit_height, #explicit_width, #flex, #has_border, #height, #margin, #node, #padding, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Box

#add_child, #border_box_height, #border_box_width, #border_box_x, #border_box_y

Constructor Details

#initialize(text, style) ⇒ TextBox

Returns a new instance of TextBox.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/silk_layout/layout/inline.rb', line 19

def initialize(text, style)
  super(nil)
  @text = text

  style ||= {}

  @font_size = px(style["font-size"]) || 16
  @font_family = style["font-family"] || "Helvetica"
  @font_weight = style["font-weight"] || "normal"
  @font_style = style["font-style"] || "normal"
  @color = style["color"] || "black"

  metrics = SilkLayout::Render::FontLibrary.metrics(
    @font_family,
    font_weight: @font_weight,
    font_style: @font_style
  )

  @font_name = metrics[:font_name]
  @ascender = metrics[:ascender] * @font_size / 1000.0
  @descender = metrics[:descender] * @font_size / 1000.0
  @line_height = parse_line_height(style["line-height"], @font_size)
end

Instance Attribute Details

#ascenderObject (readonly)

Returns the value of attribute ascender.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def ascender
  @ascender
end

#colorObject (readonly)

Returns the value of attribute color.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def color
  @color
end

#descenderObject (readonly)

Returns the value of attribute descender.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def descender
  @descender
end

#font_familyObject (readonly)

Returns the value of attribute font_family.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def font_family
  @font_family
end

#font_nameObject (readonly)

Returns the value of attribute font_name.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def font_name
  @font_name
end

#font_sizeObject (readonly)

Returns the value of attribute font_size.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def font_size
  @font_size
end

#font_styleObject (readonly)

Returns the value of attribute font_style.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def font_style
  @font_style
end

#font_weightObject (readonly)

Returns the value of attribute font_weight.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def font_weight
  @font_weight
end

#line_heightObject (readonly)

Returns the value of attribute line_height.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def line_height
  @line_height
end

#textObject (readonly)

Returns the value of attribute text.



8
9
10
# File 'lib/silk_layout/layout/inline.rb', line 8

def text
  @text
end

Instance Method Details

#childrenObject



43
44
45
# File 'lib/silk_layout/layout/inline.rb', line 43

def children
  []
end

#clone_with_text(text) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/silk_layout/layout/inline.rb', line 47

def clone_with_text(text)
  self.class.new(text, {
    "font-size" => "#{@font_size}px",
    "font-family" => @font_family,
    "font-weight" => @font_weight,
    "font-style" => @font_style,
    "line-height" => "#{@line_height}px",
    "color" => @color
  })
end