Module: Shellfie::TextMetrics
- Defined in:
- lib/shellfie/text_metrics.rb
Class Method Summary collapse
- .cell_width(text) ⇒ Object
- .char_width(char) ⇒ Object
- .combining?(codepoint) ⇒ Boolean
- .drop_cells(text, cells_to_drop) ⇒ Object
- .pixel_width(text, font_size) ⇒ Object
- .split_cells(text, max_cells) ⇒ Object
- .take_cells(text, max_cells) ⇒ Object
- .wide?(codepoint) ⇒ Boolean
Class Method Details
.cell_width(text) ⇒ Object
7 8 9 |
# File 'lib/shellfie/text_metrics.rb', line 7 def cell_width(text) text.to_s.each_char.sum { |char| char_width(char) } end |
.char_width(char) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/shellfie/text_metrics.rb', line 66 def char_width(char) codepoint = char.ord return 0 if combining?(codepoint) return 0 if codepoint < 32 || codepoint == 0x7f return 2 if wide?(codepoint) 1 end |
.combining?(codepoint) ⇒ Boolean
75 76 77 78 79 80 81 |
# File 'lib/shellfie/text_metrics.rb', line 75 def combining?(codepoint) (0x0300..0x036f).cover?(codepoint) || (0x1ab0..0x1aff).cover?(codepoint) || (0x1dc0..0x1dff).cover?(codepoint) || (0x20d0..0x20ff).cover?(codepoint) || (0xfe20..0xfe2f).cover?(codepoint) end |
.drop_cells(text, cells_to_drop) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/shellfie/text_metrics.rb', line 30 def drop_cells(text, cells_to_drop) used_cells = 0 text.to_s.each_char.with_object(+"") do |char, result| width = char_width(char) if used_cells < cells_to_drop used_cells += width next end result << char end end |
.pixel_width(text, font_size) ⇒ Object
11 12 13 |
# File 'lib/shellfie/text_metrics.rb', line 11 def pixel_width(text, font_size) (cell_width(text) * font_size * 0.6).ceil end |
.split_cells(text, max_cells) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/shellfie/text_metrics.rb', line 43 def split_cells(text, max_cells) return [text.to_s] if max_cells <= 0 chunks = [] current = +"" used_cells = 0 text.to_s.each_char do |char| width = char_width(char) if used_cells.positive? && used_cells + width > max_cells chunks << current current = +"" used_cells = 0 end current << char used_cells += width end chunks << current unless current.empty? chunks end |
.take_cells(text, max_cells) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/shellfie/text_metrics.rb', line 15 def take_cells(text, max_cells) result = +"" used_cells = 0 text.to_s.each_char do |char| width = char_width(char) break if used_cells + width > max_cells result << char used_cells += width end result end |
.wide?(codepoint) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/shellfie/text_metrics.rb', line 83 def wide?(codepoint) (0x1100..0x115f).cover?(codepoint) || (0x2329..0x232a).cover?(codepoint) || (0x2e80..0xa4cf).cover?(codepoint) || (0xac00..0xd7a3).cover?(codepoint) || (0xf900..0xfaff).cover?(codepoint) || (0xfe10..0xfe19).cover?(codepoint) || (0xfe30..0xfe6f).cover?(codepoint) || (0xff00..0xff60).cover?(codepoint) || (0xffe0..0xffe6).cover?(codepoint) || (0x1f300..0x1faff).cover?(codepoint) end |