fontist 1.17.0 → 1.17.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: e1c609003084e7675dfd55c7488c57b9f216a7e9f6412e2dd707e589bdb5f11e
4
- data.tar.gz: ff531fc17364227dc60b47eaa64fe5547bc9c1d30e05c94364109d79d7276b5e
3
+ metadata.gz: 91726c53a66563f2b0a32417d45d246617b85586cc7c9573b24c5b83533c5444
4
+ data.tar.gz: de8e1aba5de04fb720c85a6c98e708dcbae944bf439c15f4dc29c3f27e4d0831
5
5
  SHA512:
6
- metadata.gz: 2bcb373787926878310fa113e4454ac09e203c82708bd2edb3df796ce1e92f7e038a5c54011aade53f7cbc2ac2f20c311e449d5d5304a0235b93bfb0433750de
7
- data.tar.gz: 6ad5b3863ed427316065477a3c873264ffa6d543bb86be696396e61d8f727533bc8bb50268ce8818b45bb15da387e6913b88f2c1a9abb7ed4a4944e0a8d57691
6
+ metadata.gz: d05de7120339d223fc716ad1f99a60c80ec54deb37bbe77692cff0295d3ccc4b7263ae5bc2ed2beae3dc82f18f0684d85b47f01ca4c5730e04ec4cb82357be2c
7
+ data.tar.gz: 381a74039161a1e9d5ea9b560afbc9c551083c67462002f9c590da83fe69283fd8d0506983554c19d9f0ded9fba33a3b71a5d7db11a82d3689ebd7b885a9cfc2
@@ -0,0 +1,46 @@
1
+ require "ttfunk"
2
+
3
+ module Fontist
4
+ class CollectionFile
5
+ include Enumerable
6
+
7
+ class << self
8
+ def from_path(path)
9
+ io = ::File.new(path, "rb")
10
+
11
+ yield new(build_collection(io))
12
+ ensure
13
+ io.close
14
+ end
15
+
16
+ private
17
+
18
+ def build_collection(io)
19
+ TTFunk::Collection.new(io)
20
+ rescue StandardError => e
21
+ raise Errors::FontFileError,
22
+ "Font file could not be parsed: #{e.inspect}."
23
+ end
24
+ end
25
+
26
+ def initialize(ttfunk_collection)
27
+ @collection = ttfunk_collection
28
+ end
29
+
30
+ def count
31
+ @collection.count
32
+ end
33
+
34
+ def each
35
+ count.times do |index|
36
+ yield self[index]
37
+ end
38
+
39
+ self
40
+ end
41
+
42
+ def [](index)
43
+ FontFile.from_collection_index(@collection, index)
44
+ end
45
+ end
46
+ end
@@ -18,6 +18,8 @@ module Fontist
18
18
 
19
19
  class FontIndexCorrupted < GeneralError; end
20
20
 
21
+ class FontFileError < GeneralError; end
22
+
21
23
  class FontistVersionError < GeneralError; end
22
24
 
23
25
  class FontNotFoundError < GeneralError; end
@@ -0,0 +1,109 @@
1
+ require "ttfunk"
2
+
3
+ module Fontist
4
+ class FontFile
5
+ PLATFORM_MACINTOSH = 1
6
+ PLATFORM_MICROSOFT = 3
7
+
8
+ ENCODING_MAC_ROMAN = 0
9
+ ENCODING_MS_UNICODE_BMP = 1
10
+
11
+ LANGUAGE_MAC_ENGLISH = 0
12
+ LANGUAGE_MS_ENGLISH_AMERICAN = 0x409
13
+
14
+ class << self
15
+ def from_path(path)
16
+ content = File.read(path, mode: "rb")
17
+
18
+ from_content(content)
19
+ end
20
+
21
+ def from_content(content)
22
+ new(build_font(content))
23
+ end
24
+
25
+ def from_collection_index(collection, index)
26
+ new(build_font_from_collection_index(collection, index))
27
+ end
28
+
29
+ private
30
+
31
+ def build_font(content)
32
+ TTFunk::File.new(content)
33
+ rescue StandardError => e
34
+ raise_font_file_error(e)
35
+ end
36
+
37
+ def build_font_from_collection_index(collection, index)
38
+ collection[index]
39
+ rescue StandardError => e
40
+ raise_font_file_error(e)
41
+ end
42
+
43
+ def raise_font_file_error(exception)
44
+ raise Errors::FontFileError,
45
+ "Font file could not be parsed: #{exception.inspect}."
46
+ end
47
+ end
48
+
49
+ def initialize(ttfunk_file)
50
+ @file = ttfunk_file
51
+ end
52
+
53
+ def full_name
54
+ english_name(main_name.font_name)
55
+ end
56
+
57
+ def family
58
+ english_name(main_name.font_family)
59
+ end
60
+
61
+ def subfamily
62
+ english_name(main_name.font_subfamily)
63
+ end
64
+
65
+ def preferred_family
66
+ return if main_name.preferred_family.empty?
67
+
68
+ english_name(main_name.preferred_family)
69
+ end
70
+
71
+ def preferred_subfamily
72
+ return if main_name.preferred_subfamily.empty?
73
+
74
+ english_name(main_name.preferred_subfamily)
75
+ end
76
+
77
+ private
78
+
79
+ def main_name
80
+ @main_name ||= @file.name
81
+ end
82
+
83
+ def english_name(name)
84
+ visible_characters(find_english(name))
85
+ end
86
+
87
+ def find_english(name)
88
+ name.find { |x| microsoft_english?(x) } ||
89
+ name.find { |x| mac_english?(x) } ||
90
+ name.last
91
+ end
92
+
93
+ def microsoft_english?(string)
94
+ string.platform_id == PLATFORM_MICROSOFT &&
95
+ string.encoding_id == ENCODING_MS_UNICODE_BMP &&
96
+ string.language_id == LANGUAGE_MS_ENGLISH_AMERICAN
97
+ end
98
+
99
+ def mac_english?(string)
100
+ string.platform_id == PLATFORM_MACINTOSH &&
101
+ string.encoding_id == ENCODING_MAC_ROMAN &&
102
+ string.language_id == LANGUAGE_MAC_ENGLISH
103
+ end
104
+
105
+ def visible_characters(text)
106
+ text.gsub(/[^[:print:]]/, "").to_s
107
+ end
108
+ end
109
+ end
@@ -1,4 +1,5 @@
1
- require "ttfunk"
1
+ require_relative "font_file"
2
+ require_relative "collection_file"
2
3
 
3
4
  module Fontist
4
5
  class SystemIndex
@@ -6,11 +7,11 @@ module Fontist
6
7
 
7
8
  class DefaultFamily
8
9
  def family_name(name)
9
- name.font_family
10
+ name.family
10
11
  end
11
12
 
12
13
  def type(name)
13
- name.font_subfamily
14
+ name.subfamily
14
15
  end
15
16
 
16
17
  def transform_override_keys(dict)
@@ -20,15 +21,11 @@ module Fontist
20
21
 
21
22
  class PreferredFamily
22
23
  def family_name(name)
23
- return name.font_family if name.preferred_family.empty?
24
-
25
- name.preferred_family
24
+ name.preferred_family || name.family
26
25
  end
27
26
 
28
27
  def type(name)
29
- return name.font_subfamily if name.preferred_subfamily.empty?
30
-
31
- name.preferred_subfamily
28
+ name.preferred_subfamily || name.subfamily
32
29
  end
33
30
 
34
31
  def transform_override_keys(dict)
@@ -37,15 +34,6 @@ module Fontist
37
34
  end
38
35
  end
39
36
 
40
- PLATFORM_MACINTOSH = 1
41
- PLATFORM_MICROSOFT = 3
42
-
43
- ENCODING_MAC_ROMAN = 0
44
- ENCODING_MS_UNICODE_BMP = 1
45
-
46
- LANGUAGE_MAC_ENGLISH = 0
47
- LANGUAGE_MS_ENGLISH_AMERICAN = 0x409
48
-
49
37
  ALLOWED_KEYS = %i[path full_name family_name type].freeze
50
38
 
51
39
  def self.system_index
@@ -174,22 +162,26 @@ module Fontist
174
162
  else
175
163
  raise Errors::UnknownFontTypeError.new(path)
176
164
  end
177
- rescue StandardError
165
+ rescue Errors::FontFileError => e
166
+ print_recognition_error(e, path)
167
+ end
168
+
169
+ def print_recognition_error(exception, path)
178
170
  Fontist.ui.error(<<~MSG.chomp)
179
- #{$!.message}
171
+ #{exception.inspect}
180
172
  Warning: File at #{path} not recognized as a font file.
181
173
  MSG
174
+ nil
182
175
  end
183
176
 
184
177
  def detect_file_font(path)
185
- content = File.read(path, mode: "rb")
186
- file = TTFunk::File.new(content)
178
+ file = FontFile.from_path(path)
187
179
 
188
180
  parse_font(file, path)
189
181
  end
190
182
 
191
183
  def detect_collection_fonts(path)
192
- TTFunk::Collection.open(path) do |collection|
184
+ CollectionFile.from_path(path) do |collection|
193
185
  collection.map do |file|
194
186
  parse_font(file, path)
195
187
  end
@@ -197,43 +189,16 @@ module Fontist
197
189
  end
198
190
 
199
191
  def parse_font(file, path)
200
- x = file.name
201
- family_name = english_name(@family.family_name(x))
192
+ family_name = @family.family_name(file)
202
193
 
203
194
  {
204
195
  path: path,
205
- full_name: english_name(x.font_name),
196
+ full_name: file.full_name,
206
197
  family_name: family_name,
207
- type: english_name(@family.type(x)),
198
+ type: @family.type(file),
208
199
  }.merge(override_font_props(path, family_name))
209
200
  end
210
201
 
211
- def english_name(name)
212
- visible_characters(find_english(name))
213
- end
214
-
215
- def find_english(name)
216
- name.find { |x| microsoft_english?(x) } ||
217
- name.find { |x| mac_english?(x) } ||
218
- name.last
219
- end
220
-
221
- def microsoft_english?(string)
222
- string.platform_id == PLATFORM_MICROSOFT &&
223
- string.encoding_id == ENCODING_MS_UNICODE_BMP &&
224
- string.language_id == LANGUAGE_MS_ENGLISH_AMERICAN
225
- end
226
-
227
- def mac_english?(string)
228
- string.platform_id == PLATFORM_MACINTOSH &&
229
- string.encoding_id == ENCODING_MAC_ROMAN &&
230
- string.language_id == LANGUAGE_MAC_ENGLISH
231
- end
232
-
233
- def visible_characters(text)
234
- text.gsub(/[^[:print:]]/, "").to_s
235
- end
236
-
237
202
  def override_font_props(path, font_name)
238
203
  override = Formula.find_by_font_file(path)
239
204
  &.style_override(font_name)&.to_h || {}
@@ -1,3 +1,3 @@
1
1
  module Fontist
2
- VERSION = "1.17.0".freeze
2
+ VERSION = "1.17.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fontist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.17.0
4
+ version: 1.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-05 00:00:00.000000000 Z
11
+ date: 2023-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down
@@ -305,8 +305,10 @@ files:
305
305
  - lib/fontist/cache_cli.rb
306
306
  - lib/fontist/cli.rb
307
307
  - lib/fontist/cli/class_options.rb
308
+ - lib/fontist/collection_file.rb
308
309
  - lib/fontist/errors.rb
309
310
  - lib/fontist/font.rb
311
+ - lib/fontist/font_file.rb
310
312
  - lib/fontist/font_installer.rb
311
313
  - lib/fontist/font_path.rb
312
314
  - lib/fontist/fontconfig.rb