Class: Arrolio::TextLayout::BreakOpportunity

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/text_layout/break_opportunity.rb

Overview

A candidate line-break position within a list of InlineRuns.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_index:, char_offset:, width_before:, type:) ⇒ BreakOpportunity

Returns a new instance of BreakOpportunity.



9
10
11
12
13
14
15
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 9

def initialize(run_index:, char_offset:, width_before:, type:)
  @run_index = run_index
  @char_offset = char_offset
  @width_before = width_before.to_f
  @type = type
  freeze
end

Instance Attribute Details

#char_offsetObject (readonly)

Returns the value of attribute char_offset.



7
8
9
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 7

def char_offset
  @char_offset
end

#run_indexObject (readonly)

Returns the value of attribute run_index.



7
8
9
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 7

def run_index
  @run_index
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 7

def type
  @type
end

#width_beforeObject (readonly)

Returns the value of attribute width_before.



7
8
9
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 7

def width_before
  @width_before
end

Class Method Details

.each_in(runs, measurer:, font_size:) {|new(run_index: 0, char_offset: 0, width_before: 0.0, type: :start)| ... } ⇒ Object

Yields:

  • (new(run_index: 0, char_offset: 0, width_before: 0.0, type: :start))


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
84
85
86
87
88
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 40

def each_in(runs, measurer:, font_size:)
  return enum_for(:each_in, runs, measurer: measurer, font_size: font_size) unless block_given?

  yield new(run_index: 0, char_offset: 0, width_before: 0.0, type: :start)

  cumulative = 0.0
  runs.each_with_index do |run, run_index|
    text = run.text
    i = 0
    while i < text.length
      ch = text[i]
      if ch == "\n"
        yield new(run_index: run_index, char_offset: i,
                  width_before: cumulative, type: :forced)
        i += 1
        next
      end

      if ch == ' '
        cumulative += char_width(run, ' ', measurer, font_size)
        yield new(run_index: run_index, char_offset: i + 1,
                  width_before: cumulative, type: :soft)
        i += 1
        next
      end

      if ch == '-' || ch == '' || ch == '­'
        cumulative += char_width(run, ch, measurer, font_size)
        yield new(run_index: run_index, char_offset: i + 1,
                  width_before: cumulative, type: :soft)
        i += 1
        next
      end

      if ch == ''
        yield new(run_index: run_index, char_offset: i,
                  width_before: cumulative, type: :soft)
        i += 1
        next
      end

      cumulative += char_width(run, ch, measurer, font_size)
      i += 1
    end
  end

  yield new(run_index: runs.length, char_offset: 0,
            width_before: cumulative, type: :end)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



25
26
27
28
29
30
31
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 25

def ==(other)
  other.is_a?(self.class) &&
    run_index == other.run_index &&
    char_offset == other.char_offset &&
    width_before == other.width_before &&
    type == other.type
end

#forced?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 17

def forced?
  @type == :forced
end

#hashObject



35
36
37
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 35

def hash
  [self.class, run_index, char_offset, width_before, type].hash
end

#soft?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/arrolio/text_layout/break_opportunity.rb', line 21

def soft?
  @type == :soft
end