krill 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 263d05f0b3e34007af5a65efed3386497244294e
4
- data.tar.gz: 5607a0d4c668dd9cda6d736e78c50986eb108c23
3
+ metadata.gz: bd3f51a4af9789839d2855c32fa16ec028774135
4
+ data.tar.gz: ae4d0f52fd062ba40368f6be9ed68daa34278d09
5
5
  SHA512:
6
- metadata.gz: 5dff75341afc874c3694154ccee79ff844c1dd27af1143486850bb256d1095a097d67092ad2252d21892c6e9787e7cfd5e54d0519d7a1841130979f7409bc571
7
- data.tar.gz: 72c902daa932736530e97c5ff755a4345dc93807241da37cc6c41492544cc55c4f927820f8d1a9293a3bf6060344dd28c9deec94464b8b93bbabd3835e89993a
6
+ metadata.gz: 19d495ab9f258a1f1b830a8634af24f6edcb0c77dbdd032bcc6551d2943ef1689e5ca51f0407e5972de2f751c96f6dd438fa41de7d9d2bc0d0667dc7d9c6b7cf
7
+ data.tar.gz: 83294c710da5637b8ed8dd28520383d11d6249dc0134b4fcad325e56214a2b73aabba62da833a69fbfa3958ce9b00eb163af4dab2c9ebd3bedf0fbcde9e0387f
@@ -1,6 +1,6 @@
1
1
  module Krill
2
2
  AFM = Struct.new(:filename) do
3
- attr_reader :name, :family, :ascender, :descender, :line_gap,:character_widths, :kernings
3
+ attr_reader :name, :family, :ascender, :descender, :line_gap, :character_widths, :kernings
4
4
 
5
5
  def self.open(filename)
6
6
  new filename
@@ -22,18 +22,16 @@ module Krill
22
22
  # NOTE: +string+ must be UTF8-encoded.
23
23
  def compute_width_of(string, kerning: true)
24
24
  if kerning
25
- kern(string).inject(0) do |s, r|
25
+ kern(string).inject(0.0) do |width, r|
26
26
  if r.is_a?(Numeric)
27
- s - r
27
+ width - r
28
28
  else
29
- r.inject(s) { |s2, u| s2 + font.character_widths.fetch(u, 0) }
29
+ r.inject(width) { |width2, char| width2 + width_of_char(char) }
30
30
  end
31
- end * size
31
+ end
32
32
  else
33
- string.chars.inject(0) do |sum, char|
34
- sum + font.character_widths.fetch(char, 0.0)
35
- end * size
36
- end
33
+ string.chars.inject(0.0) { |width, char| width + width_of_char(char) }
34
+ end * size
37
35
  end
38
36
 
39
37
 
@@ -74,6 +72,7 @@ module Krill
74
72
 
75
73
  private
76
74
 
75
+
77
76
  def normalized_height
78
77
  @normalized_height ||= font.ascender - font.descender + font.line_gap
79
78
  end
@@ -94,7 +93,7 @@ module Krill
94
93
  string.each_char do |char|
95
94
  if a.empty?
96
95
  a << [char]
97
- elsif (kern = font.kernings["#{a.last.last}#{char}"])
96
+ elsif (kern = kerning_for_chars(a.last.last, char))
98
97
  a << -kern << [char]
99
98
  else
100
99
  a.last << char
@@ -104,5 +103,18 @@ module Krill
104
103
  a
105
104
  end
106
105
 
106
+
107
+ protected
108
+
109
+
110
+ def width_of_char(char)
111
+ font.character_widths.fetch(char, 0.0)
112
+ end
113
+
114
+ def kerning_for_chars(char0, char1)
115
+ font.kernings["#{char0}#{char1}"]
116
+ end
117
+
118
+
107
119
  end
108
120
  end
@@ -0,0 +1,14 @@
1
+ module Krill
2
+ class Line
3
+ attr_reader :text, :width
4
+
5
+ def initialize(text, width)
6
+ @text = text
7
+ @width = width
8
+ end
9
+
10
+ alias :to_s :text
11
+ alias :to_str :text
12
+
13
+ end
14
+ end
@@ -35,7 +35,6 @@ module Krill
35
35
  while fragment = @arranger.next_string
36
36
  @fragment_output = ""
37
37
 
38
- fragment.lstrip! if first_fragment_on_this_line?(fragment)
39
38
  next if empty_line?(fragment)
40
39
 
41
40
  break unless apply_font_settings_and_add_fragment_to_line(fragment)
@@ -219,7 +218,7 @@ module Krill
219
218
  end
220
219
 
221
220
  def update_output_based_on_last_fragment(fragment, normalized_soft_hyphen = nil)
222
- remaining_text = fragment.slice(@fragment_output.length..fragment.length)
221
+ remaining_text = fragment.slice(@fragment_output.length..fragment.length).lstrip
223
222
  fail CannotFit if line_finished? && line_empty? && @fragment_output.empty? && !fragment.strip.empty?
224
223
  @arranger.update_last_string(@fragment_output, remaining_text, normalized_soft_hyphen)
225
224
  end
@@ -1,8 +1,10 @@
1
1
  require "krill/line_wrap"
2
+ require "krill/line"
2
3
  require "krill/arranger"
3
4
 
4
5
  module Krill
5
6
  class TextBox
7
+ attr_reader :printed_lines
6
8
 
7
9
  def initialize(formatted_text, options={})
8
10
  @original_array = formatted_text
@@ -189,9 +191,8 @@ module Krill
189
191
  accumulated_width += fragment_this_line.width
190
192
  end
191
193
 
192
- @printed_lines << printed_fragments.map do |s|
193
- s.force_encoding(::Encoding::UTF_8)
194
- end.join
194
+ text = printed_fragments.map { |s| s.force_encoding(::Encoding::UTF_8) }.join
195
+ @printed_lines << Krill::Line.new(text, accumulated_width)
195
196
  end
196
197
 
197
198
  def word_spacing_for_this_line
@@ -1,3 +1,3 @@
1
1
  module Krill
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -10,7 +10,11 @@ module Krill
10
10
  end
11
11
 
12
12
  def lines
13
- @text_box.instance_variable_get :@printed_lines
13
+ @text_box.printed_lines.map(&:to_s)
14
+ end
15
+
16
+ def width
17
+ @text_box.printed_lines.map(&:width).max
14
18
  end
15
19
 
16
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: krill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-22 00:00:00.000000000 Z
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,6 +102,7 @@ files:
102
102
  - lib/krill/arranger.rb
103
103
  - lib/krill/formatter.rb
104
104
  - lib/krill/fragment.rb
105
+ - lib/krill/line.rb
105
106
  - lib/krill/line_wrap.rb
106
107
  - lib/krill/text_box.rb
107
108
  - lib/krill/ttf.rb