fontist 3.0.24 → 3.0.25

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: 954fe79bde0fb84cf7b2fcc96b686820a7c2f4fd6ec822abd4067b1c0d8c288e
4
- data.tar.gz: ac7eb9f594dd5a9be155e2893c4a07bad197743e107a92f16786b45f47000224
3
+ metadata.gz: 7e161f1ace760cebffefd73f61835fef3abb2875972836c3ad7e82324a465d11
4
+ data.tar.gz: 85f425e3a050b0e9674e32b413d750bde0d40e4e34f3c261c6138dadc571e0a5
5
5
  SHA512:
6
- metadata.gz: 9616ce9f46708c5acb1dc4643688f8ab0a43eeb5eafc7ee8259e9d5a110e415c738514cc00739d937b41d05d1b64868256419f3303d32159aeb41f462f260207
7
- data.tar.gz: ba15c400a1fed88380cd28f8669c7272e6d52cf0dcf4df09f42166cc0b95cf55db36dcf5456bd3704981550308e087b2074f3aaaa9419567e75733ff96099bbf
6
+ metadata.gz: 282670c9e6c9ebc53dedbaa927e80259a489f44bc33392d388c977ad909209b721b354b7922a2190f0e7554221a63ca3c15a234a55b100e27bdce92cca1e496e
7
+ data.tar.gz: 46ca6895943eefe3a7dce37a6e4cf08241530b6dfacc9fef5233f259ea091ab9d94aac2d4bdd903cc468948d3783a6ceff66219fd04f3514576f88a4a62048ef
@@ -14,7 +14,7 @@ module Fontist
14
14
 
15
15
  def formulas
16
16
  @formulas ||= if fontist_font?
17
- Indexes::FilenameIndex.from_file.load_index_formulas(File.basename(@path)).flat_map(&:name)
17
+ Indexes::FilenameIndex.lookup_index.load_index_formulas(File.basename(@path)).flat_map(&:name)
18
18
  else
19
19
  []
20
20
  end
@@ -122,15 +122,15 @@ module Fontist
122
122
  end
123
123
 
124
124
  def find(font_name)
125
- Indexes::FontIndex.from_file.load_formulas(font_name).first
125
+ Indexes::FontIndex.lookup_index.load_formulas(font_name).first
126
126
  end
127
127
 
128
128
  def find_many(font_name)
129
- Indexes::FontIndex.from_file.load_formulas(font_name)
129
+ Indexes::FontIndex.lookup_index.load_formulas(font_name)
130
130
  end
131
131
 
132
132
  def find_fonts(font_name)
133
- formulas = Indexes::FontIndex.from_file.load_formulas(font_name)
133
+ formulas = Indexes::FontIndex.lookup_index.load_formulas(font_name)
134
134
 
135
135
  formulas.map do |formula|
136
136
  formula.all_fonts.select do |f|
@@ -140,7 +140,7 @@ module Fontist
140
140
  end
141
141
 
142
142
  def find_styles(font_name, style_name)
143
- formulas = Indexes::FontIndex.from_file.load_formulas(font_name)
143
+ formulas = Indexes::FontIndex.lookup_index.load_formulas(font_name)
144
144
 
145
145
  formulas.map do |formula|
146
146
  formula.all_fonts.map do |f|
@@ -173,7 +173,7 @@ module Fontist
173
173
  end
174
174
 
175
175
  def find_by_font_file(font_file)
176
- key = Indexes::FilenameIndex.from_file
176
+ key = Indexes::FilenameIndex.lookup_index
177
177
  .load_index_formulas(File.basename(font_file))
178
178
  .flat_map(&:name)
179
179
  .first
@@ -8,6 +8,14 @@ module Fontist
8
8
  DefaultFamilyFontIndex.from_file
9
9
  end
10
10
  end
11
+
12
+ def self.lookup_index
13
+ if Fontist.preferred_family?
14
+ PreferredFamilyFontIndex.lookup_index
15
+ else
16
+ DefaultFamilyFontIndex.lookup_index
17
+ end
18
+ end
11
19
  end
12
20
  end
13
21
  end
@@ -1,5 +1,9 @@
1
+ require "json"
2
+
1
3
  module Fontist
2
4
  module Indexes
5
+ autoload :LookupIndex, "#{__dir__}/lookup_index"
6
+
3
7
  # IndexMixin provides common functionality for font index classes.
4
8
  #
5
9
  # == Performance Optimization (Tech Debt)
@@ -72,9 +76,38 @@ module Fontist
72
76
  # to force a fresh parse on next access
73
77
  Fontist::FromFileCache.clear
74
78
 
75
- # Delete the index file to force rebuild on next access
76
- # This is important for tests to ensure clean state
79
+ # Delete the index file and its lookup sidecar to force a
80
+ # rebuild on next access. This is important for tests to
81
+ # ensure clean state
77
82
  FileUtils.rm_f(path)
83
+ FileUtils.rm_f("#{path}.json")
84
+ end
85
+
86
+ # Read-only lookups over the sidecar JSON beside the index
87
+ # file, which skips binding every entry into model objects. The
88
+ # sidecar is (re)written whenever the index file is written,
89
+ # and regenerated here when it is missing or stale.
90
+ def lookup_index
91
+ entries_hash = sidecar_entries || rebuild_sidecar_entries
92
+ normalizer = new.method(:normalize_key)
93
+ LookupIndex.new(entries_hash, normalizer: normalizer)
94
+ end
95
+
96
+ def sidecar_entries
97
+ sidecar_path = "#{path}.json"
98
+ return unless File.exist?(sidecar_path)
99
+ return if File.exist?(path) &&
100
+ File.mtime(sidecar_path) < File.mtime(path)
101
+
102
+ JSON.parse(File.read(sidecar_path))["entries"]
103
+ rescue JSON::ParserError, TypeError
104
+ nil
105
+ end
106
+
107
+ def rebuild_sidecar_entries
108
+ entries_hash = from_file(path).entries_hash
109
+ File.write("#{path}.json", JSON.dump("entries" => entries_hash))
110
+ entries_hash
78
111
  end
79
112
  end
80
113
 
@@ -158,6 +191,19 @@ module Fontist
158
191
 
159
192
  FileUtils.mkdir_p(File.dirname(file_path))
160
193
  File.write(file_path, to_yaml)
194
+ write_lookup_sidecar(file_path)
195
+ end
196
+
197
+ def write_lookup_sidecar(file_path)
198
+ File.write("#{file_path}.json",
199
+ JSON.dump("entries" => entries_hash))
200
+ end
201
+
202
+ # key -> formula paths, in the sidecar's JSON-friendly shape
203
+ def entries_hash
204
+ Array(entries).to_h do |entry|
205
+ [normalize_key(entry.key), entry.formula_path]
206
+ end
161
207
  end
162
208
 
163
209
  private
@@ -0,0 +1,34 @@
1
+ module Fontist
2
+ module Indexes
3
+ # Read-only lookups over the JSON sidecar written beside an index
4
+ # file. Binding every index entry into model objects dominates the
5
+ # cost of loading an index; the sidecar holds the key-to-path map
6
+ # as plain data, and FormulaKeyToPath entries are materialized only
7
+ # for the requested key.
8
+ class LookupIndex
9
+ def initialize(entries_hash, normalizer:)
10
+ @entries_hash = entries_hash
11
+ @normalizer = normalizer
12
+ end
13
+
14
+ def load_index_formulas(key)
15
+ [
16
+ FormulaKeyToPath.new(
17
+ key: key,
18
+ formula_path: paths_for(@normalizer.call(key)),
19
+ ),
20
+ ]
21
+ end
22
+
23
+ def load_formulas(key)
24
+ load_index_formulas(key).flat_map(&:to_full)
25
+ end
26
+
27
+ private
28
+
29
+ def paths_for(normalized_key)
30
+ @entries_hash[normalized_key] || []
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Fontist
2
- VERSION = "3.0.24".freeze
2
+ VERSION = "3.0.25".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: 3.0.24
4
+ version: 3.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -452,6 +452,7 @@ files:
452
452
  - lib/fontist/indexes/incremental_index_updater.rb
453
453
  - lib/fontist/indexes/incremental_scanner.rb
454
454
  - lib/fontist/indexes/index_mixin.rb
455
+ - lib/fontist/indexes/lookup_index.rb
455
456
  - lib/fontist/indexes/preferred_family_font_index.rb
456
457
  - lib/fontist/indexes/system_index.rb
457
458
  - lib/fontist/indexes/user_index.rb