Class: SilkLayout::Layout::InlineFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/silk_layout/layout/inline_formatter.rb

Defined Under Namespace

Classes: Fragment

Class Method Summary collapse

Class Method Details

.layout(inline_children, available_width, parent_x, parent_y) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/silk_layout/layout/inline_formatter.rb', line 13

def layout(inline_children, available_width, parent_x, parent_y)
  fragments = flatten(inline_children)
  lines = []
  current_fragments = []
  current_y = parent_y

  fragments.each do |fragment|
    if fragment.break_after
      line = flush_line(current_fragments, parent_x, current_y)
      if line
        lines << line
        current_y += line.height
      end
      current_fragments = []
      next
    end

    if fragment.whitespace? && current_fragments.empty?
      next
    end

    current_width = current_fragments.sum(&:width)
    overflow = !current_fragments.empty? && (current_width + fragment.width) > available_width

    if overflow
      line = flush_line(current_fragments, parent_x, current_y)
      if line
        lines << line
        current_y += line.height
      end
      current_fragments = []
    end

    next if fragment.whitespace? && current_fragments.empty?

    current_fragments << fragment
  end

  line = flush_line(current_fragments, parent_x, current_y)
  lines << line if line
  lines
end