krill 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ require "ttfunk"
2
+
3
+ module Krill
4
+ class TTF
5
+ attr_reader :ascender, :descender, :line_gap
6
+
7
+ def self.open(filename)
8
+ new TTFunk::File.open(filename)
9
+ end
10
+
11
+ def initialize(ttf)
12
+ @ttf = ttf
13
+ @scale_factor = 1.0 / ttf.header.units_per_em
14
+ @ascender = @ttf.ascent * scale_factor
15
+ @descender = @ttf.descent * scale_factor
16
+ @line_gap = @ttf.line_gap * scale_factor
17
+ end
18
+
19
+ def name
20
+ @name ||= ttf.name.font_name.reject { |family| family =~ /\x00/ }.first
21
+ end
22
+
23
+ def family
24
+ @family ||= ttf.name.font_family.reject { |family| family =~ /\x00/ }.first
25
+ end
26
+
27
+ def bold?
28
+ name["Bold"].present?
29
+ end
30
+
31
+ def italic?
32
+ name["Italic"].present?
33
+ end
34
+
35
+ def character_widths
36
+ @character_widths ||= hmtx.widths.each_with_index
37
+ .map { |width, index| [ cmap.key(index), width ] }
38
+ .reject { |(codepoint, width)| codepoint.nil? || width.nil? || width.zero? }
39
+ .map { |(codepoint, width)|
40
+ char = codepoint.chr(Encoding::UTF_8)
41
+
42
+ # Some TTF fonts have nonzero widths for \n (UTF-8 / ASCII code: 10).
43
+ # Patch around this as we'll never be drawing a newline with a width.
44
+ width = 0.0 if codepoint == 10
45
+
46
+ [ char, width * scale_factor ] }
47
+ .to_h
48
+ end
49
+
50
+ def kernings
51
+ @kernings ||= kern_pairs_table
52
+ .map { |(a, b), kerning| [ cmap.key(a), cmap.key(b), kerning ] }
53
+ .reject { |(a, b, kerning)| a.nil? || b.nil? || kerning.zero? }
54
+ .map { |(a, b, kerning)|
55
+ a = a.chr(Encoding::UTF_8)
56
+ b = b.chr(Encoding::UTF_8)
57
+ [ "#{a}#{b}", kerning * scale_factor ] }
58
+ .to_h
59
+ end
60
+
61
+ private
62
+ attr_reader :ttf, :scale_factor
63
+
64
+ def cmap
65
+ @cmap ||= ttf.cmap.unicode.first&.code_map or fail("no unicode cmap for font")
66
+ end
67
+
68
+ def kern_pairs_table
69
+ return {} unless ttf.kerning.exists? && ttf.kerning.tables.any?
70
+ ttf.kerning.tables.first.pairs
71
+ end
72
+
73
+ def hmtx
74
+ ttf.horizontal_metrics
75
+ end
76
+
77
+ end
78
+ end
@@ -1,3 +1,3 @@
1
1
  module Krill
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,17 @@
1
+ module Krill
2
+ class WrappedText
3
+
4
+ def initialize(text_box)
5
+ @text_box = text_box
6
+ end
7
+
8
+ def height
9
+ @text_box.height
10
+ end
11
+
12
+ def lines
13
+ @text_box.instance_variable_get :@printed_lines
14
+ end
15
+
16
+ end
17
+ 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.1.0
4
+ version: 0.2.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: 2017-12-20 00:00:00.000000000 Z
11
+ date: 2018-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,19 +39,47 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '3.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ttfunk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description:
56
84
  email:
57
85
  - bob.lailfamily@gmail.com
@@ -60,17 +88,25 @@ extensions: []
60
88
  extra_rdoc_files: []
61
89
  files:
62
90
  - ".gitignore"
91
+ - ".rspec"
63
92
  - ".travis.yml"
64
93
  - Gemfile
65
94
  - LICENSE
66
- - LICENSE.txt
67
95
  - README.md
68
96
  - Rakefile
69
97
  - bin/console
70
98
  - bin/setup
71
99
  - krill.gemspec
72
100
  - lib/krill.rb
101
+ - lib/krill/afm.rb
102
+ - lib/krill/arranger.rb
103
+ - lib/krill/formatter.rb
104
+ - lib/krill/fragment.rb
105
+ - lib/krill/line_wrap.rb
106
+ - lib/krill/text_box.rb
107
+ - lib/krill/ttf.rb
73
108
  - lib/krill/version.rb
109
+ - lib/krill/wrapped_text.rb
74
110
  homepage: https://github.com/cph/krill
75
111
  licenses:
76
112
  - GPL-3.0
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2017 Bob Lail
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.