class Krill::Fragment
Attributes
ascender[RW]
baseline[RW]
descender[RW]
font[R]
format_state[R]
formatter[R]
left[RW]
line_height[RW]
text[R]
width[W]
word_spacing[RW]
Public Class Methods
new(text, format_state)
click to toggle source
# 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
Public Instance Methods
bottom()
click to toggle source
# File lib/krill/fragment.rb, line 88 def bottom baseline - descender end
character_spacing()
click to toggle source
# File lib/krill/fragment.rb, line 39 def character_spacing formatter.character_spacing end
default_direction=(direction)
click to toggle source
# 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
direction()
click to toggle source
# File lib/krill/fragment.rb, line 60 def direction @format_state[:direction] end
height()
click to toggle source
# File lib/krill/fragment.rb, line 27 def height top - bottom end
include_trailing_white_space!()
click to toggle source
# 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
right()
click to toggle source
# File lib/krill/fragment.rb, line 80 def right left + width end
space_count()
click to toggle source
# File lib/krill/fragment.rb, line 76 def space_count @text.count(" ") end
strikethrough_points()
click to toggle source
# File lib/krill/fragment.rb, line 55 def strikethrough_points y = baseline + ascender * 0.3 [[left, y], [right, y]] end
subscript?()
click to toggle source
# File lib/krill/fragment.rb, line 35 def subscript? formatter.subscript? end
superscript?()
click to toggle source
# File lib/krill/fragment.rb, line 31 def superscript? formatter.superscript? end
top()
click to toggle source
# File lib/krill/fragment.rb, line 84 def top baseline + ascender end
underline_points()
click to toggle source
# File lib/krill/fragment.rb, line 50 def underline_points y = baseline - 1.25 [[left, y], [right, y]] end
width()
click to toggle source
# File lib/krill/fragment.rb, line 22 def width return @width if @word_spacing.zero? @width + @word_spacing * space_count end
y_offset()
click to toggle source
# File lib/krill/fragment.rb, line 43 def y_offset if subscript? then -descender elsif superscript? then 0.85 * ascender else 0 end end
Private Instance Methods
exclude_trailing_white_space?()
click to toggle source
# File lib/krill/fragment.rb, line 117 def exclude_trailing_white_space? @format_state[:exclude_trailing_white_space] end
normalized_soft_hyphen()
click to toggle source
# File lib/krill/fragment.rb, line 125 def normalized_soft_hyphen @format_state[:normalized_soft_hyphen] end
process_soft_hyphens(string)
click to toggle source
# File lib/krill/fragment.rb, line 129 def process_soft_hyphens(string) if string.encoding != normalized_soft_hyphen.encoding string.force_encoding(normalized_soft_hyphen.encoding) end string.gsub(normalized_soft_hyphen, "") end
process_text(text)
click to toggle source
# File lib/krill/fragment.rb, line 94 def process_text(text) string = strip_zero_width_spaces(text) if exclude_trailing_white_space? string = string.rstrip if soft_hyphens_need_processing?(string) string = process_soft_hyphens(string[0..-2]) + string[-1..-1] end else if soft_hyphens_need_processing?(string) string = process_soft_hyphens(string) end end case direction when :rtl string.reverse else string end end
soft_hyphens_need_processing?(string)
click to toggle source
# File lib/krill/fragment.rb, line 121 def soft_hyphens_need_processing?(string) string.length > 0 && normalized_soft_hyphen end
strip_zero_width_spaces(string)
click to toggle source
# File lib/krill/fragment.rb, line 137 def strip_zero_width_spaces(string) if string.encoding == ::Encoding::UTF_8 string.gsub(ZWSP, "") else string end end