Class: Krill::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/krill/fragment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, format_state) ⇒ Fragment

Returns a new instance of Fragment.



11
12
13
14
15
16
17
18
19
20
# File 'lib/krill/fragment.rb', line 11

def initialize(text, format_state)
  @format_state = format_state
  @font = format_state.fetch(:font)
  @word_spacing = 0

  # keep the original value of "text", so we can reinitialize @text if formatting parameters
  #   like text direction are changed
  @original_text = text
  @text = process_text(@original_text)
end

Instance Attribute Details

#ascenderObject

Returns the value of attribute ascender.



5
6
7
# File 'lib/krill/fragment.rb', line 5

def ascender
  @ascender
end

#baselineObject

Returns the value of attribute baseline.



6
7
8
# File 'lib/krill/fragment.rb', line 6

def baseline
  @baseline
end

#descenderObject

Returns the value of attribute descender.



5
6
7
# File 'lib/krill/fragment.rb', line 5

def descender
  @descender
end

#fontObject (readonly) Also known as: formatter

Returns the value of attribute font.



8
9
10
# File 'lib/krill/fragment.rb', line 8

def font
  @font
end

#format_stateObject (readonly)

Returns the value of attribute format_state.



3
4
5
# File 'lib/krill/fragment.rb', line 3

def format_state
  @format_state
end

#leftObject

Returns the value of attribute left.



6
7
8
# File 'lib/krill/fragment.rb', line 6

def left
  @left
end

#line_heightObject

Returns the value of attribute line_height.



5
6
7
# File 'lib/krill/fragment.rb', line 5

def line_height
  @line_height
end

#textObject (readonly)

Returns the value of attribute text.



3
4
5
# File 'lib/krill/fragment.rb', line 3

def text
  @text
end

#widthObject



22
23
24
25
# File 'lib/krill/fragment.rb', line 22

def width
  return @width if @word_spacing.zero?
  @width + @word_spacing * space_count
end

#word_spacingObject

Returns the value of attribute word_spacing.



6
7
8
# File 'lib/krill/fragment.rb', line 6

def word_spacing
  @word_spacing
end

Instance Method Details

#bottomObject



88
89
90
# File 'lib/krill/fragment.rb', line 88

def bottom
  baseline - descender
end

#character_spacingObject



39
40
41
# File 'lib/krill/fragment.rb', line 39

def character_spacing
  formatter.character_spacing
end

#default_direction=(direction) ⇒ Object



64
65
66
67
68
69
# File 'lib/krill/fragment.rb', line 64

def default_direction=(direction)
  unless @format_state[:direction]
    @format_state[:direction] = direction
    @text = process_text(@original_text)
  end
end

#directionObject



60
61
62
# File 'lib/krill/fragment.rb', line 60

def direction
  @format_state[:direction]
end

#heightObject



27
28
29
# File 'lib/krill/fragment.rb', line 27

def height
  top - bottom
end

#include_trailing_white_space!Object



71
72
73
74
# File 'lib/krill/fragment.rb', line 71

def include_trailing_white_space!
  @format_state.delete(:exclude_trailing_white_space)
  @text = process_text(@original_text)
end

#rightObject



80
81
82
# File 'lib/krill/fragment.rb', line 80

def right
  left + width
end

#space_countObject



76
77
78
# File 'lib/krill/fragment.rb', line 76

def space_count
  @text.count(" ")
end

#strikethrough_pointsObject



55
56
57
58
# File 'lib/krill/fragment.rb', line 55

def strikethrough_points
  y = baseline + ascender * 0.3
  [[left, y], [right, y]]
end

#subscript?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/krill/fragment.rb', line 35

def subscript?
  formatter.subscript?
end

#superscript?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/krill/fragment.rb', line 31

def superscript?
  formatter.superscript?
end

#topObject



84
85
86
# File 'lib/krill/fragment.rb', line 84

def top
  baseline + ascender
end

#underline_pointsObject



50
51
52
53
# File 'lib/krill/fragment.rb', line 50

def underline_points
  y = baseline - 1.25
  [[left, y], [right, y]]
end

#y_offsetObject



43
44
45
46
47
48
# File 'lib/krill/fragment.rb', line 43

def y_offset
  if subscript? then -descender
  elsif superscript? then 0.85 * ascender
  else 0
  end
end