fontist 1.7.0 → 1.7.1

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
  SHA256:
3
- metadata.gz: 4832b87f5d5535536b6f2f2e2761bf8ff8dbd82770703f93e201e2c7e81f496d
4
- data.tar.gz: 32e720d4a33cb78a88bcdd8bf3b80bc24abec544d3dd755a459ae2a1a4a78d6b
3
+ metadata.gz: e024a76941056e06491654749b2a467dd1df4212752a3a98e8a9ee58755af281
4
+ data.tar.gz: d7caaade9c91008f4e0b91b157847d2de4dd26203d88c3558583549fec3bdb5a
5
5
  SHA512:
6
- metadata.gz: 6567b976d958bc852244e5080753eb1fc2e44f8231fe08f3cded4076d510c553565160cf83c108e2e6c309cce042393acf7e19ab34e5a851f4c879a7f9c642ae
7
- data.tar.gz: 2bab58316fdc830954b93daa716deaac90912ee4906510fad27efba2c0323af8cdb9bade71d5a5f257461063576863b44e1bfc0b72f7cdde1839689acb497f95
6
+ metadata.gz: 8d68a69167a89f6aa143f972da95f5bb06d2831e7e243d6c3ef338d47cc924b5b59c9f0404fad95dbd09b5ed870a555fae6a22e043a7a2eaa564b89036ddebfc
7
+ data.tar.gz: 3497ef80333b61a6a3df80f2493b3442779de017965cffbcffa13fbeb11a056de4f4741121100c8c7b34bd118e8af1aa1deaccad92423d8b68aa4b3b33c05c5a
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_runtime_dependency "ruby-ole", "~> 1.0"
35
35
  spec.add_runtime_dependency "thor", "~> 1.0.1"
36
36
  spec.add_runtime_dependency "git", "~> 1.0"
37
+ spec.add_runtime_dependency "ttfunk", "~> 1.0"
37
38
 
38
39
  spec.add_development_dependency "extract_ttc", "~> 0.1"
39
40
  spec.add_development_dependency "pry"
@@ -55,4 +55,8 @@ module Fontist
55
55
  def self.system_file_path
56
56
  Fontist.lib_path.join("fontist", "system.yml")
57
57
  end
58
+
59
+ def self.system_index_path
60
+ Fontist.fontist_path.join("system_index.yml")
61
+ end
58
62
  end
@@ -19,8 +19,8 @@ module Fontist
19
19
  new(font_name: name).find_fonts
20
20
  end
21
21
 
22
- def self.find_styles_with_fonts(font, style)
23
- new(font_name: font, style_name: style).find_styles_with_fonts
22
+ def self.find_styles(font, style)
23
+ new(font_name: font, style_name: style).find_styles
24
24
  end
25
25
 
26
26
  def all
@@ -37,16 +37,12 @@ module Fontist
37
37
  fonts.empty? ? nil : fonts
38
38
  end
39
39
 
40
- def find_styles_with_fonts
40
+ def find_styles
41
41
  formulas.values.flat_map do |formula|
42
42
  formula.fonts.flat_map do |f|
43
- selected = f.styles.select do |s|
43
+ f.styles.select do |s|
44
44
  f.name.casecmp?(font_name) && s.type.casecmp?(style_name)
45
45
  end
46
-
47
- selected.map do |s|
48
- { font: f, style: s }
49
- end
50
46
  end
51
47
  end
52
48
  end
@@ -53,7 +53,7 @@ module Fontist
53
53
  end
54
54
 
55
55
  def file_paths(font, style)
56
- Fontist::SystemFont.find_with_style(font, style).transform_keys(&:to_s)
56
+ Fontist::SystemFont.find_with_name(font, style).transform_keys(&:to_s)
57
57
  end
58
58
  end
59
59
  end
@@ -1,3 +1,5 @@
1
+ require_relative "system_index"
2
+
1
3
  module Fontist
2
4
  class SystemFont
3
5
  def initialize(font:, style: nil, sources: nil)
@@ -10,8 +12,8 @@ module Fontist
10
12
  new(font: font, sources: sources).find
11
13
  end
12
14
 
13
- def self.find_with_style(font, style)
14
- new(font: font, style: style).find_with_style
15
+ def self.find_with_name(font, style)
16
+ new(font: font, style: style).find_with_name
15
17
  end
16
18
 
17
19
  def find
@@ -21,11 +23,12 @@ module Fontist
21
23
  paths.empty? ? nil : paths
22
24
  end
23
25
 
24
- def find_with_style
25
- styles = Formula.find_styles_with_fonts(font, style)
26
+ def find_with_name
27
+ styles = find_styles
28
+ return { full_name: nil, paths: [] } unless styles
26
29
 
27
- { full_name: style_full_name(styles),
28
- paths: style_paths(styles) }
30
+ { full_name: styles.first[:full_name],
31
+ paths: styles.map { |x| x[:path] } }
29
32
  end
30
33
 
31
34
  private
@@ -95,24 +98,37 @@ module Fontist
95
98
  @default_sources ||= YAML.load(system_path_file)["system"][user_os.to_s]
96
99
  end
97
100
 
98
- def style_full_name(styles)
99
- return if styles.empty?
101
+ def find_styles
102
+ find_by_index || find_by_formulas
103
+ end
100
104
 
101
- s = styles.first
102
- s[:style]["full_name"] || s[:font]["name"]
105
+ def find_by_index
106
+ SystemIndex.new(font_paths).find(font, style)
103
107
  end
104
108
 
105
- def style_paths(styles)
106
- filenames = styles.map { |x| x[:style]["font"] }
107
- paths = lookup_using_filenames(filenames)
108
- return paths unless paths.empty?
109
+ def find_by_formulas
110
+ styles = find_styles_by_formulas(font, style)
111
+ return if styles.empty?
109
112
 
110
- grep_font_paths(font, style)
113
+ fonts = styles.uniq { |s| s["font"] }.flat_map do |s|
114
+ paths = search_font_paths(s["font"])
115
+ paths.map do |path|
116
+ { full_name: s["full_name"],
117
+ path: path }
118
+ end
119
+ end
120
+
121
+ fonts.empty? ? nil : fonts
111
122
  end
112
123
 
113
- def lookup_using_filenames(filenames)
114
- filenames.flat_map do |filename|
115
- search_font_paths(filename)
124
+ def find_styles_by_formulas(font, style)
125
+ if style
126
+ Formula.find_styles(font, style)
127
+ else
128
+ fonts = Formula.find_fonts(font)
129
+ return [] unless fonts
130
+
131
+ fonts.flat_map(&:styles)
116
132
  end
117
133
  end
118
134
 
@@ -0,0 +1,92 @@
1
+ require "ttfunk"
2
+
3
+ module Fontist
4
+ class SystemIndex
5
+ attr_reader :font_paths
6
+
7
+ def initialize(font_paths)
8
+ @font_paths = font_paths
9
+ end
10
+
11
+ def find(font, style)
12
+ fonts = system_index.select do |file|
13
+ file[:family_name].casecmp?(font) &&
14
+ (style.nil? || file[:type].casecmp?(style))
15
+ end
16
+
17
+ fonts.empty? ? nil : fonts
18
+ end
19
+
20
+ private
21
+
22
+ def system_index
23
+ @system_index ||= build_system_index
24
+ end
25
+
26
+ def build_system_index
27
+ previous_index = load_system_index
28
+ updated_index = detect_paths(font_paths, previous_index)
29
+ updated_index.tap do |index|
30
+ save_index(index)
31
+ end
32
+ end
33
+
34
+ def load_system_index
35
+ index = File.exist?(Fontist.system_index_path) ? YAML.load_file(Fontist.system_index_path) : []
36
+ index.group_by { |x| x[:path] }
37
+ end
38
+
39
+ def detect_paths(paths, indexed)
40
+ paths.flat_map do |path|
41
+ next indexed[path] if indexed[path]
42
+
43
+ detect_fonts(path)
44
+ end
45
+ end
46
+
47
+ def detect_fonts(path)
48
+ case File.extname(path).delete_prefix(".").downcase
49
+ when "ttf", "otf"
50
+ detect_file_font(path)
51
+ when "ttc"
52
+ detect_collection_fonts(path)
53
+ else
54
+ raise Errors::UnknownFontTypeError.new(path)
55
+ end
56
+ end
57
+
58
+ def detect_file_font(path)
59
+ file = TTFunk::File.open(path)
60
+ parse_font(file, path)
61
+ end
62
+
63
+ def detect_collection_fonts(path)
64
+ TTFunk::Collection.open(path) do |collection|
65
+ collection.map do |file|
66
+ parse_font(file, path)
67
+ end
68
+ end
69
+ end
70
+
71
+ def parse_font(file, path)
72
+ x = file.name
73
+
74
+ {
75
+ path: path,
76
+ full_name: parse_text(x.font_name.first),
77
+ family_name: parse_text(x.preferred_family.first || x.font_family.first),
78
+ type: parse_text(x.preferred_subfamily.first || x.font_subfamily.first),
79
+ }
80
+ end
81
+
82
+ def parse_text(text)
83
+ text.gsub(/[^[:print:]]/, "").to_s
84
+ end
85
+
86
+ def save_index(index)
87
+ dir = File.dirname(Fontist.system_index_path)
88
+ FileUtils.mkdir_p(dir) unless File.exist?(dir)
89
+ File.write(Fontist.system_index_path, YAML.dump(index))
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module Fontist
2
- VERSION = "1.7.0".freeze
2
+ VERSION = "1.7.1".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fontist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-11-28 00:00:00.000000000 Z
12
+ date: 2020-12-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: down
@@ -109,6 +109,20 @@ dependencies:
109
109
  - - "~>"
110
110
  - !ruby/object:Gem::Version
111
111
  version: '1.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: ttfunk
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '1.0'
119
+ type: :runtime
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '1.0'
112
126
  - !ruby/object:Gem::Dependency
113
127
  name: extract_ttc
114
128
  requirement: !ruby/object:Gem::Requirement
@@ -313,6 +327,7 @@ files:
313
327
  - lib/fontist/registry.rb
314
328
  - lib/fontist/system.yml
315
329
  - lib/fontist/system_font.rb
330
+ - lib/fontist/system_index.rb
316
331
  - lib/fontist/utils.rb
317
332
  - lib/fontist/utils/cache.rb
318
333
  - lib/fontist/utils/downloader.rb