fontist 1.3.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/check_google.yml +28 -0
  3. data/.github/workflows/macosx.yml +3 -0
  4. data/.github/workflows/ubuntu.yml +1 -1
  5. data/.github/workflows/windows.yml +1 -1
  6. data/.gitignore +6 -0
  7. data/.hound.yml +2 -0
  8. data/.rubocop.yml +22 -0
  9. data/README.md +224 -0
  10. data/bin/check_google +8 -0
  11. data/bin/convert_formulas +8 -0
  12. data/bin/fontist +7 -0
  13. data/bin/generate_otfinfo +8 -0
  14. data/bin/import_google +8 -0
  15. data/fontist.gemspec +15 -1
  16. data/lib/fontist.rb +19 -13
  17. data/lib/fontist/cli.rb +168 -0
  18. data/lib/fontist/errors.rb +6 -0
  19. data/lib/fontist/font.rb +145 -7
  20. data/lib/fontist/font_formula.rb +42 -7
  21. data/lib/fontist/fontist_font.rb +70 -0
  22. data/lib/fontist/formula.rb +31 -9
  23. data/lib/fontist/formula_template.rb +122 -0
  24. data/lib/fontist/formulas.rb +41 -0
  25. data/lib/fontist/import.rb +9 -0
  26. data/lib/fontist/import/convert_formulas.rb +65 -0
  27. data/lib/fontist/import/create_formula.rb +59 -0
  28. data/lib/fontist/import/extractors.rb +5 -0
  29. data/lib/fontist/import/extractors/cab_extractor.rb +37 -0
  30. data/lib/fontist/import/extractors/extractor.rb +19 -0
  31. data/lib/fontist/import/extractors/ole_extractor.rb +41 -0
  32. data/lib/fontist/import/extractors/seven_zip_extractor.rb +44 -0
  33. data/lib/fontist/import/extractors/zip_extractor.rb +31 -0
  34. data/lib/fontist/import/files/collection_file.rb +52 -0
  35. data/lib/fontist/import/files/file_requirement.rb +17 -0
  36. data/lib/fontist/import/files/font_detector.rb +48 -0
  37. data/lib/fontist/import/formula_builder.rb +166 -0
  38. data/lib/fontist/import/formula_serializer.rb +133 -0
  39. data/lib/fontist/import/google.rb +16 -0
  40. data/lib/fontist/import/google/fonts_public.md +10 -0
  41. data/lib/fontist/import/google/fonts_public.pb.rb +71 -0
  42. data/lib/fontist/import/google/fonts_public.proto +46 -0
  43. data/lib/fontist/import/google/new_fonts_fetcher.rb +121 -0
  44. data/lib/fontist/import/google/skiplist.yml +8 -0
  45. data/lib/fontist/import/google_check.rb +34 -0
  46. data/lib/fontist/import/google_import.rb +180 -0
  47. data/lib/fontist/import/helpers/hash_helper.rb +13 -0
  48. data/lib/fontist/import/helpers/system_helper.rb +23 -0
  49. data/lib/fontist/import/otf/font_file.rb +105 -0
  50. data/lib/fontist/import/otf_parser.rb +25 -0
  51. data/lib/fontist/import/otf_style.rb +29 -0
  52. data/lib/fontist/import/otfinfo/otfinfo_requirement.rb +22 -0
  53. data/lib/fontist/import/otfinfo/template.erb +20 -0
  54. data/lib/fontist/import/otfinfo_generate.rb +45 -0
  55. data/lib/fontist/import/recursive_extraction.rb +185 -0
  56. data/lib/fontist/import/template_helper.rb +19 -0
  57. data/lib/fontist/import/text_helper.rb +30 -0
  58. data/lib/fontist/manifest.rb +2 -0
  59. data/lib/fontist/manifest/install.rb +32 -0
  60. data/lib/fontist/manifest/locations.rb +60 -0
  61. data/lib/fontist/system_font.rb +66 -22
  62. data/lib/fontist/utils.rb +5 -0
  63. data/lib/fontist/utils/cache.rb +88 -0
  64. data/lib/fontist/utils/downloader.rb +66 -9
  65. data/lib/fontist/utils/dsl.rb +8 -0
  66. data/lib/fontist/utils/dsl/collection_font.rb +36 -0
  67. data/lib/fontist/utils/dsl/font.rb +38 -0
  68. data/lib/fontist/utils/exe_extractor.rb +25 -22
  69. data/lib/fontist/utils/msi_extractor.rb +31 -0
  70. data/lib/fontist/utils/seven_zip_extractor.rb +41 -0
  71. data/lib/fontist/utils/system.rb +23 -0
  72. data/lib/fontist/utils/ui.rb +12 -0
  73. data/lib/fontist/utils/zip_extractor.rb +22 -8
  74. data/lib/fontist/version.rb +1 -1
  75. metadata +173 -49
  76. data/lib/fontist/formulas/andale_font.rb +0 -80
  77. data/lib/fontist/formulas/arial_black_font.rb +0 -79
  78. data/lib/fontist/formulas/cleartype_fonts.rb +0 -227
  79. data/lib/fontist/formulas/comic_font.rb +0 -78
  80. data/lib/fontist/formulas/courier_font.rb +0 -81
  81. data/lib/fontist/formulas/euphemia_font.rb +0 -85
  82. data/lib/fontist/formulas/georgia_font.rb +0 -80
  83. data/lib/fontist/formulas/impact_font.rb +0 -78
  84. data/lib/fontist/formulas/montserrat_font.rb +0 -132
  85. data/lib/fontist/formulas/ms_truetype_fonts.rb +0 -125
  86. data/lib/fontist/formulas/open_sans_fonts.rb +0 -263
  87. data/lib/fontist/formulas/overpass_font.rb +0 -73
  88. data/lib/fontist/formulas/source_fonts.rb +0 -109
  89. data/lib/fontist/formulas/stix_fonts.rb +0 -108
  90. data/lib/fontist/formulas/tahoma_font.rb +0 -147
  91. data/lib/fontist/formulas/webding_font.rb +0 -78
  92. data/spec/fontist/font_formula_spec.rb +0 -67
  93. data/spec/fontist/font_spec.rb +0 -113
  94. data/spec/fontist/formula_spec.rb +0 -67
  95. data/spec/fontist/formulas/andale_font_spec.rb +0 -29
  96. data/spec/fontist/formulas/arial_black_font_spec.rb +0 -29
  97. data/spec/fontist/formulas/cleartype_fonts_spec.rb +0 -38
  98. data/spec/fontist/formulas/comic_font_spec.rb +0 -29
  99. data/spec/fontist/formulas/courier_font_spec.rb +0 -29
  100. data/spec/fontist/formulas/euphemia_font_spec.rb +0 -29
  101. data/spec/fontist/formulas/georgia_font_spec.rb +0 -29
  102. data/spec/fontist/formulas/impact_font_spec.rb +0 -29
  103. data/spec/fontist/formulas/montserrat_font_spec.rb +0 -29
  104. data/spec/fontist/formulas/ms_truetype_fonts_spec.rb +0 -29
  105. data/spec/fontist/formulas/open_sans_fonts_spec.rb +0 -29
  106. data/spec/fontist/formulas/overpass_font_spec.rb +0 -29
  107. data/spec/fontist/formulas/source_fonts_spec.rb +0 -31
  108. data/spec/fontist/formulas/stix_fonts_spec.rb +0 -29
  109. data/spec/fontist/formulas/tahoma_font_spec.rb +0 -29
  110. data/spec/fontist/formulas/webding_font_spec.rb +0 -29
  111. data/spec/fontist/registry_spec.rb +0 -47
  112. data/spec/fontist/system_font_spec.rb +0 -44
  113. data/spec/fontist/utils/downloader_spec.rb +0 -35
  114. data/spec/fontist_spec.rb +0 -5
  115. data/spec/spec_helper.rb +0 -22
  116. data/spec/support/fontist_helper.rb +0 -10
@@ -0,0 +1,168 @@
1
+ require "thor"
2
+
3
+ module Fontist
4
+ class CLI < Thor
5
+ STATUS_SUCCESS = 0
6
+ STATUS_ERROR = 1
7
+
8
+ def self.exit_on_failure?
9
+ false
10
+ end
11
+
12
+ desc "install FONT", "Install font by font or formula"
13
+ option :force, type: :boolean, aliases: :f,
14
+ desc: "Install even if it's already installed in system"
15
+ option :confirm_license, type: :boolean, desc: "Confirm license agreement"
16
+ def install(font)
17
+ Fontist::Font.install(
18
+ font,
19
+ force: options[:force],
20
+ confirmation: options[:confirm_license] ? "yes" : "no"
21
+ )
22
+ STATUS_SUCCESS
23
+ rescue Fontist::Errors::NonSupportedFontError
24
+ could_not_find_font(font)
25
+ end
26
+
27
+ desc "uninstall/remove FONT", "Uninstall font by font or formula"
28
+ def uninstall(font)
29
+ fonts_paths = Fontist::Font.uninstall(font)
30
+ Fontist.ui.success("These fonts are removed:")
31
+ Fontist.ui.success(fonts_paths.join("\n"))
32
+ STATUS_SUCCESS
33
+ rescue Fontist::Errors::MissingFontError => e
34
+ Fontist.ui.error(e.message)
35
+ STATUS_ERROR
36
+ rescue Fontist::Errors::NonSupportedFontError
37
+ could_not_find_font(font)
38
+ end
39
+ map remove: :uninstall
40
+
41
+ desc "status [FONT]", "Show status of FONT or all fonts in fontist"
42
+ def status(font = nil)
43
+ formulas = Fontist::Font.status(font)
44
+ return error("No font is installed.") if formulas.empty?
45
+
46
+ print_formulas(formulas)
47
+ success
48
+ rescue Fontist::Errors::MissingFontError => e
49
+ error(e.message)
50
+ rescue Fontist::Errors::NonSupportedFontError
51
+ could_not_find_font(font)
52
+ end
53
+
54
+ desc "list [FONT]", "List installation status of FONT or fonts in fontist"
55
+ def list(font = nil)
56
+ formulas = Fontist::Font.list(font)
57
+ print_list(formulas)
58
+ success
59
+ rescue Fontist::Errors::NonSupportedFontError
60
+ could_not_find_font(font)
61
+ end
62
+
63
+ desc "update", "Update formulas"
64
+ def update
65
+ Formulas.fetch_formulas
66
+ Fontist.ui.say("Formulas have been successfully updated")
67
+ STATUS_SUCCESS
68
+ end
69
+
70
+ desc "manifest-locations MANIFEST",
71
+ "Get locations of fonts from MANIFEST (yaml)"
72
+ def manifest_locations(manifest)
73
+ paths = Fontist::Manifest::Locations.call(manifest)
74
+ print_yaml(paths)
75
+ success
76
+ rescue Fontist::Errors::ManifestCouldNotBeFoundError
77
+ error("Manifest could not be found.")
78
+ rescue Fontist::Errors::ManifestCouldNotBeReadError
79
+ error("Manifest could not be read.")
80
+ end
81
+
82
+ desc "manifest-install MANIFEST", "Install fonts from MANIFEST (yaml)"
83
+ option :confirm_license, type: :boolean, desc: "Confirm license agreement"
84
+ def manifest_install(manifest)
85
+ paths = Fontist::Manifest::Install.call(
86
+ manifest,
87
+ confirmation: options[:confirm_license] ? "yes" : "no"
88
+ )
89
+
90
+ print_yaml(paths)
91
+ success
92
+ rescue Fontist::Errors::ManifestCouldNotBeFoundError
93
+ error("Manifest could not be found.")
94
+ rescue Fontist::Errors::ManifestCouldNotBeReadError
95
+ error("Manifest could not be read.")
96
+ end
97
+
98
+ desc "create-formula URL", "Create a new formula with fonts from URL"
99
+ option :name, desc: "Example: Times New Roman"
100
+ option :mirror, repeatable: true
101
+ option :subarchive, desc: "Subarchive to choose when there are several ones"
102
+ option :subdir, desc: "Subdirectory to take fonts from"
103
+ def create_formula(url)
104
+ require "fontist/import/create_formula"
105
+ name = Fontist::Import::CreateFormula.new(url, options).call
106
+ Fontist.ui.say("#{name} formula has been successfully created")
107
+ STATUS_SUCCESS
108
+ end
109
+
110
+ private
111
+
112
+ def success
113
+ STATUS_SUCCESS
114
+ end
115
+
116
+ def could_not_find_font(font)
117
+ error("Font '#{font}' not found locally nor available in the Fontist " \
118
+ "formula repository.\n" \
119
+ "Perhaps it is available at the latest Fontist formula " \
120
+ "repository.\n" \
121
+ "You can update the formula repository using the command " \
122
+ "`fontist update` and try again.")
123
+ end
124
+
125
+ def error(message)
126
+ Fontist.ui.error(message)
127
+ STATUS_ERROR
128
+ end
129
+
130
+ def print_yaml(object)
131
+ Fontist.ui.say(YAML.dump(object))
132
+ end
133
+
134
+ def print_formulas(formulas)
135
+ formulas.each do |formula, fonts|
136
+ Fontist.ui.success(formula.installer)
137
+
138
+ fonts.each do |font, styles|
139
+ Fontist.ui.success(" #{font.name}")
140
+
141
+ styles.each do |style, path|
142
+ Fontist.ui.success(" #{style.type} (#{path})")
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
149
+ def print_list(formulas)
150
+ formulas.each do |formula, fonts|
151
+ Fontist.ui.say(formula.installer)
152
+
153
+ fonts.each do |font, styles|
154
+ Fontist.ui.say(" #{font.name}")
155
+
156
+ styles.each do |style, installed|
157
+ if installed
158
+ Fontist.ui.success(" #{style.type} (installed)")
159
+ else
160
+ Fontist.ui.error(" #{style.type} (uninstalled)")
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
167
+ end
168
+ end
@@ -6,5 +6,11 @@ module Fontist
6
6
  class TamperedFileError < StandardError; end
7
7
  class InvalidResourceError < StandardError; end
8
8
  class TimeoutError < StandardError; end
9
+ class MissingAttributeError < StandardError; end
10
+ class UnknownFontTypeError < StandardError; end
11
+ class FontNotFoundError < StandardError; end
12
+ class BinaryCallError < StandardError; end
13
+ class ManifestCouldNotBeReadError < StandardError; end
14
+ class ManifestCouldNotBeFoundError < StandardError; end
9
15
  end
10
16
  end
@@ -3,6 +3,7 @@ module Fontist
3
3
  def initialize(options = {})
4
4
  @name = options.fetch(:name, nil)
5
5
  @confirmation = options.fetch(:confirmation, "no")
6
+ @force = options.fetch(:force, false)
6
7
 
7
8
  check_or_create_fontist_path!
8
9
  end
@@ -15,8 +16,24 @@ module Fontist
15
16
  new(name: name).find
16
17
  end
17
18
 
18
- def self.install(name, confirmation: "no")
19
- new(name: name, confirmation: confirmation).install
19
+ def self.install(name, confirmation: "no", force: false)
20
+ new(name: name, confirmation: confirmation, force: force).install
21
+ end
22
+
23
+ def self.try_install(name, confirmation: "no")
24
+ new(name: name, confirmation: confirmation).try_install
25
+ end
26
+
27
+ def self.uninstall(name)
28
+ new(name: name).uninstall
29
+ end
30
+
31
+ def self.status(name)
32
+ new(name: name).status
33
+ end
34
+
35
+ def self.list(name)
36
+ new(name: name).list
20
37
  end
21
38
 
22
39
  def find
@@ -26,11 +43,35 @@ module Fontist
26
43
  end
27
44
 
28
45
  def install
29
- find_system_font || download_font || raise(
46
+ (find_system_font unless @force) || download_font || raise(
47
+ Fontist::Errors::NonSupportedFontError
48
+ )
49
+ end
50
+
51
+ def try_install
52
+ download_font
53
+ end
54
+
55
+ def uninstall
56
+ uninstall_font || downloadable_font || raise(
57
+ Fontist::Errors::NonSupportedFontError
58
+ )
59
+ end
60
+
61
+ def status
62
+ return installed_statuses unless @name
63
+
64
+ font_status || downloadable_font || raise(
30
65
  Fontist::Errors::NonSupportedFontError
31
66
  )
32
67
  end
33
68
 
69
+ def list
70
+ return all_list unless @name
71
+
72
+ font_list || raise(Fontist::Errors::NonSupportedFontError)
73
+ end
74
+
34
75
  def all
35
76
  Fontist::Formula.all.to_h.map { |_name, formula| formula.fonts }.flatten
36
77
  end
@@ -40,7 +81,16 @@ module Fontist
40
81
  attr_reader :name, :confirmation
41
82
 
42
83
  def find_system_font
43
- Fontist::SystemFont.find(name)
84
+ paths = Fontist::SystemFont.find(name)
85
+ unless paths
86
+ Fontist.ui.say(%(Font "#{name}" not found locally.))
87
+ return
88
+ end
89
+
90
+ Fontist.ui.say("Fonts found at:")
91
+ paths.each do |path|
92
+ Fontist.ui.say("- #{path}")
93
+ end
44
94
  end
45
95
 
46
96
  def check_or_create_fontist_path!
@@ -62,8 +112,8 @@ module Fontist
62
112
  if formula
63
113
  raise(
64
114
  Fontist::Errors::MissingFontError,
65
- "#{name}" "Fonts are missing, please run " \
66
- "Fontist::Font.install('#{name}', confirmation: 'yes') to " \
115
+ "#{name} fonts are missing, please run " \
116
+ "`fontist install '#{name}'` to " \
67
117
  "download the font."
68
118
  )
69
119
  end
@@ -72,7 +122,13 @@ module Fontist
72
122
  def download_font
73
123
  if formula
74
124
  check_and_confirm_required_license(formula)
75
- font_installer(formula).fetch_font(name, confirmation: confirmation)
125
+ paths = font_installer(formula).fetch_font(name,
126
+ confirmation: confirmation)
127
+
128
+ Fontist.ui.say("Fonts installed at:")
129
+ paths.each do |path|
130
+ Fontist.ui.say("- #{path}")
131
+ end
76
132
  end
77
133
  end
78
134
 
@@ -109,5 +165,87 @@ module Fontist
109
165
  FONT LICENSE END ("#{name}")
110
166
  MSG
111
167
  end
168
+
169
+ def uninstall_font
170
+ paths = find_fontist_font
171
+ return unless paths
172
+
173
+ paths.each do |path|
174
+ File.delete(path)
175
+ end
176
+
177
+ paths
178
+ end
179
+
180
+ def find_fontist_font
181
+ Fontist::FontistFont.find(name)
182
+ end
183
+
184
+ def installed_statuses
185
+ installed_styles(all_formulas)
186
+ end
187
+
188
+ def all_formulas
189
+ Fontist::Formula.all.to_h.values
190
+ end
191
+
192
+ def font_status
193
+ return unless formula
194
+
195
+ statuses = installed_styles([formula])
196
+ statuses.empty? ? nil : statuses
197
+ end
198
+
199
+ def installed_styles(formulas)
200
+ filter_blank(formulas) do |formula|
201
+ filter_blank(formula.fonts) do |font|
202
+ filter_blank(font.styles) do |style|
203
+ path(style)
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ def filter_blank(elements)
210
+ elements.map { |e| [e, yield(e)] }
211
+ .to_h
212
+ .reject { |_k, v| v.nil? || v.empty? }
213
+ end
214
+
215
+ def path(style)
216
+ font_paths.grep(/#{style.font}/i).first
217
+ end
218
+
219
+ def font_paths
220
+ @font_paths ||= Dir.glob(Fontist.fonts_path.join("**"))
221
+ end
222
+
223
+ def all_list
224
+ list_styles(all_formulas)
225
+ end
226
+
227
+ def font_list
228
+ return unless formula
229
+
230
+ list_styles([formula])
231
+ end
232
+
233
+ def list_styles(formulas)
234
+ map_to_hash(formulas) do |formula|
235
+ map_to_hash(formula.fonts) do |font|
236
+ map_to_hash(font.styles) do |style|
237
+ installed(style)
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ def map_to_hash(elements)
244
+ elements.map { |e| [e, yield(e)] }.to_h
245
+ end
246
+
247
+ def installed(style)
248
+ path(style) ? true : false
249
+ end
112
250
  end
113
251
  end
@@ -4,8 +4,10 @@ module Fontist
4
4
  extend Fontist::Utils::Dsl
5
5
  include Fontist::Utils::ZipExtractor
6
6
  include Fontist::Utils::ExeExtractor
7
+ include Fontist::Utils::MsiExtractor
8
+ include Fontist::Utils::SevenZipExtractor
7
9
 
8
- attr_accessor :license, :license_url, :license_required
10
+ attr_accessor :license, :license_url, :license_required, :copyright
9
11
  attr_accessor :key, :homepage, :description, :options, :temp_resource
10
12
 
11
13
  def font_list
@@ -55,7 +57,7 @@ module Fontist
55
57
  end
56
58
 
57
59
  def fonts_path
58
- @fonts_path ||= Fontist.fonts_path
60
+ @fonts_path = Fontist.fonts_path
59
61
  end
60
62
 
61
63
  def default_font
@@ -75,7 +77,9 @@ module Fontist
75
77
  styles = options.fetch(:match_styles_from_file, [])
76
78
 
77
79
  unless styles.empty?
78
- styles.map { |type, file | { type: type, font: file } }
80
+ styles.map do |attributes|
81
+ Fontist::Utils::Dsl::Font.new(attributes).attributes
82
+ end
79
83
  end
80
84
  end
81
85
 
@@ -91,8 +95,10 @@ module Fontist
91
95
  styles = options.fetch(:extract_styles_from_collection, [])
92
96
 
93
97
  unless styles.empty?
94
- styles.map do |type, file|
95
- { type: type, collection: file, font: temp_resource[:filename] }
98
+ styles.map do |attributes|
99
+ filenames = temp_resource.slice(:filename, :source_filename)
100
+ Fontist::Utils::Dsl::CollectionFont.new(attributes.merge(filenames))
101
+ .attributes
96
102
  end
97
103
  end
98
104
  end
@@ -105,8 +111,11 @@ module Fontist
105
111
  end
106
112
 
107
113
  def download_file(source)
114
+ url = source[:urls].first
115
+ Fontist.ui.say(%(Downloading font "#{key}" from #{url}))
116
+
108
117
  downloaded_file = Fontist::Utils::Downloader.download(
109
- source[:urls].first,
118
+ url,
110
119
  sha: source[:sha256],
111
120
  file_size: source[:file_size],
112
121
  progress_bar: is_progress_bar_enabled
@@ -117,7 +126,33 @@ module Fontist
117
126
  end
118
127
 
119
128
  def is_progress_bar_enabled
120
- options.nil? ? false : options.fetch(:progress_bar, false)
129
+ options.nil? ? true : options.fetch(:progress_bar, true)
130
+ end
131
+
132
+ def font_file?(filename)
133
+ source_files.include?(filename)
134
+ end
135
+
136
+ def source_files
137
+ @source_files ||= fonts.flat_map do |font|
138
+ font[:styles].map do |style|
139
+ style[:source_font] || style[:font]
140
+ end
141
+ end
142
+ end
143
+
144
+ def target_filename(source_filename)
145
+ target_filenames[source_filename]
146
+ end
147
+
148
+ def target_filenames
149
+ @target_filenames ||= fonts.flat_map do |font|
150
+ font[:styles].map do |style|
151
+ source = style[:source_font] || style[:font]
152
+ target = style[:font]
153
+ [source, target]
154
+ end
155
+ end.to_h
121
156
  end
122
157
  end
123
158
  end