fontist 1.0.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) 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 +54 -0
  10. data/bin/check_google +8 -0
  11. data/bin/convert_formulas +8 -0
  12. data/bin/fontist +8 -0
  13. data/bin/generate_otfinfo +8 -0
  14. data/bin/import_google +8 -0
  15. data/bin/stripttc +0 -0
  16. data/fontist.gemspec +15 -1
  17. data/lib/fontist.rb +22 -15
  18. data/lib/fontist/cli.rb +118 -0
  19. data/lib/fontist/errors.rb +5 -1
  20. data/lib/fontist/font.rb +153 -3
  21. data/lib/fontist/font_formula.rb +17 -6
  22. data/lib/fontist/fontist_font.rb +70 -0
  23. data/lib/fontist/formula.rb +11 -8
  24. data/lib/fontist/formula_template.rb +103 -0
  25. data/lib/fontist/formulas.rb +41 -0
  26. data/lib/fontist/import.rb +9 -0
  27. data/lib/fontist/import/convert_formulas.rb +65 -0
  28. data/lib/fontist/import/create_formula.rb +74 -0
  29. data/lib/fontist/import/extractors.rb +5 -0
  30. data/lib/fontist/import/extractors/cab_extractor.rb +37 -0
  31. data/lib/fontist/import/extractors/extractor.rb +19 -0
  32. data/lib/fontist/import/extractors/ole_extractor.rb +41 -0
  33. data/lib/fontist/import/extractors/seven_zip_extractor.rb +44 -0
  34. data/lib/fontist/import/extractors/zip_extractor.rb +31 -0
  35. data/lib/fontist/import/files/collection_file.rb +48 -0
  36. data/lib/fontist/import/formula_builder.rb +115 -0
  37. data/lib/fontist/import/formula_serializer.rb +133 -0
  38. data/lib/fontist/import/google.rb +16 -0
  39. data/lib/fontist/import/google/fonts_public.md +10 -0
  40. data/lib/fontist/import/google/fonts_public.pb.rb +71 -0
  41. data/lib/fontist/import/google/fonts_public.proto +46 -0
  42. data/lib/fontist/import/google/new_fonts_fetcher.rb +121 -0
  43. data/lib/fontist/import/google/skiplist.yml +6 -0
  44. data/lib/fontist/import/google_check.rb +34 -0
  45. data/lib/fontist/import/google_import.rb +180 -0
  46. data/lib/fontist/import/helpers/hash_helper.rb +13 -0
  47. data/lib/fontist/import/helpers/system_helper.rb +20 -0
  48. data/lib/fontist/import/otf/font_file.rb +89 -0
  49. data/lib/fontist/import/otf_parser.rb +25 -0
  50. data/lib/fontist/import/otf_style.rb +29 -0
  51. data/lib/fontist/import/otfinfo/otfinfo_requirement.rb +22 -0
  52. data/lib/fontist/import/otfinfo/template.erb +20 -0
  53. data/lib/fontist/import/otfinfo_generate.rb +45 -0
  54. data/lib/fontist/import/recursive_extraction.rb +101 -0
  55. data/lib/fontist/import/template_helper.rb +19 -0
  56. data/lib/fontist/import/text_helper.rb +30 -0
  57. data/lib/fontist/registry.rb +1 -0
  58. data/lib/fontist/system.yml +5 -1
  59. data/lib/fontist/system_font.rb +23 -20
  60. data/lib/fontist/utils.rb +6 -0
  61. data/lib/fontist/utils/cache.rb +69 -0
  62. data/lib/fontist/utils/downloader.rb +92 -0
  63. data/lib/fontist/utils/dsl.rb +8 -0
  64. data/lib/fontist/utils/dsl/font.rb +37 -0
  65. data/lib/fontist/utils/exe_extractor.rb +12 -19
  66. data/lib/fontist/utils/msi_extractor.rb +31 -0
  67. data/lib/fontist/utils/seven_zip_extractor.rb +41 -0
  68. data/lib/fontist/utils/system.rb +23 -0
  69. data/lib/fontist/utils/ui.rb +23 -0
  70. data/lib/fontist/utils/zip_extractor.rb +9 -4
  71. data/lib/fontist/version.rb +1 -1
  72. metadata +170 -50
  73. data/lib/fontist/downloader.rb +0 -70
  74. data/lib/fontist/formulas/andale_font.rb +0 -79
  75. data/lib/fontist/formulas/arial_black_font.rb +0 -78
  76. data/lib/fontist/formulas/cleartype_fonts.rb +0 -225
  77. data/lib/fontist/formulas/comic_font.rb +0 -77
  78. data/lib/fontist/formulas/courier_font.rb +0 -80
  79. data/lib/fontist/formulas/euphemia_font.rb +0 -85
  80. data/lib/fontist/formulas/georgia_font.rb +0 -79
  81. data/lib/fontist/formulas/impact_font.rb +0 -77
  82. data/lib/fontist/formulas/montserrat_font.rb +0 -132
  83. data/lib/fontist/formulas/ms_truetype_fonts.rb +0 -124
  84. data/lib/fontist/formulas/open_sans_fonts.rb +0 -263
  85. data/lib/fontist/formulas/overpass_font.rb +0 -73
  86. data/lib/fontist/formulas/source_fonts.rb +0 -109
  87. data/lib/fontist/formulas/stix_fonts.rb +0 -108
  88. data/lib/fontist/formulas/tahoma_font.rb +0 -147
  89. data/lib/fontist/formulas/webding_font.rb +0 -77
  90. data/spec/fontist/downloader_spec.rb +0 -35
  91. data/spec/fontist/font_formula_spec.rb +0 -67
  92. data/spec/fontist/font_spec.rb +0 -87
  93. data/spec/fontist/formula_spec.rb +0 -67
  94. data/spec/fontist/formulas/andale_font_spec.rb +0 -29
  95. data/spec/fontist/formulas/arial_black_font_spec.rb +0 -29
  96. data/spec/fontist/formulas/cleartype_fonts_spec.rb +0 -38
  97. data/spec/fontist/formulas/comic_font_spec.rb +0 -29
  98. data/spec/fontist/formulas/courier_font_spec.rb +0 -29
  99. data/spec/fontist/formulas/euphemia_font_spec.rb +0 -29
  100. data/spec/fontist/formulas/georgia_font_spec.rb +0 -29
  101. data/spec/fontist/formulas/impact_font_spec.rb +0 -29
  102. data/spec/fontist/formulas/montserrat_font_spec.rb +0 -29
  103. data/spec/fontist/formulas/ms_truetype_fonts_spec.rb +0 -29
  104. data/spec/fontist/formulas/open_sans_fonts_spec.rb +0 -29
  105. data/spec/fontist/formulas/overpass_font_spec.rb +0 -29
  106. data/spec/fontist/formulas/source_fonts_spec.rb +0 -31
  107. data/spec/fontist/formulas/stix_fonts_spec.rb +0 -29
  108. data/spec/fontist/formulas/tahoma_font_spec.rb +0 -29
  109. data/spec/fontist/formulas/webding_font_spec.rb +0 -29
  110. data/spec/fontist/registry_spec.rb +0 -47
  111. data/spec/fontist/system_font_spec.rb +0 -39
  112. data/spec/fontist_spec.rb +0 -5
  113. data/spec/spec_helper.rb +0 -22
  114. data/spec/support/fontist_helper.rb +0 -10
@@ -19,6 +19,18 @@ module Fontist
19
19
  new(name: name, confirmation: confirmation).install
20
20
  end
21
21
 
22
+ def self.uninstall(name)
23
+ new(name: name).uninstall
24
+ end
25
+
26
+ def self.status(name)
27
+ new(name: name).status
28
+ end
29
+
30
+ def self.list(name)
31
+ new(name: name).list
32
+ end
33
+
22
34
  def find
23
35
  find_system_font || downloadable_font || raise(
24
36
  Fontist::Errors::NonSupportedFontError
@@ -31,6 +43,26 @@ module Fontist
31
43
  )
32
44
  end
33
45
 
46
+ def uninstall
47
+ uninstall_font || downloadable_font || raise(
48
+ Fontist::Errors::NonSupportedFontError
49
+ )
50
+ end
51
+
52
+ def status
53
+ return installed_statuses unless @name
54
+
55
+ font_status || downloadable_font || raise(
56
+ Fontist::Errors::NonSupportedFontError
57
+ )
58
+ end
59
+
60
+ def list
61
+ return all_list unless @name
62
+
63
+ font_list || raise(Fontist::Errors::NonSupportedFontError)
64
+ end
65
+
34
66
  def all
35
67
  Fontist::Formula.all.to_h.map { |_name, formula| formula.fonts }.flatten
36
68
  end
@@ -62,17 +94,135 @@ module Fontist
62
94
  if formula
63
95
  raise(
64
96
  Fontist::Errors::MissingFontError,
65
- "Fonts are missing, please run " \
66
- "Fontist::Font.install('#{name}', confirmation: 'yes') to " \
67
- "download the font"
97
+ "#{name} fonts are missing, please run " \
98
+ "`fontist install '#{name}'` to " \
99
+ "download the font."
68
100
  )
69
101
  end
70
102
  end
71
103
 
72
104
  def download_font
73
105
  if formula
106
+ check_and_confirm_required_license(formula)
74
107
  font_installer(formula).fetch_font(name, confirmation: confirmation)
75
108
  end
76
109
  end
110
+
111
+ def check_and_confirm_required_license(formula)
112
+ if formula.license_required && !confirmation.casecmp("yes").zero?
113
+ @confirmation = show_license_and_ask_for_input(formula.license)
114
+
115
+ if !confirmation.casecmp("yes").zero?
116
+ raise Fontist::Errors::LicensingError.new(
117
+ "Fontist will not download these fonts unless you accept the terms."
118
+ )
119
+ end
120
+ end
121
+ end
122
+
123
+ def show_license_and_ask_for_input(license)
124
+ Fontist.ui.say(license_agrement_message(license))
125
+ Fontist.ui.ask(
126
+ "\nDo you accept all presented font licenses, and want Fontist " \
127
+ "to download these fonts for you? => TYPE 'Yes' or 'No':"
128
+ )
129
+ end
130
+
131
+ def license_agrement_message(license)
132
+ <<~MSG
133
+ FONT LICENSE ACCEPTANCE REQUIRED FOR "#{name}":
134
+
135
+ Fontist can install this font if you accept its licensing conditions.
136
+
137
+ FONT LICENSE BEGIN ("#{name}")
138
+ -----------------------------------------------------------------------
139
+ #{license}
140
+ -----------------------------------------------------------------------
141
+ FONT LICENSE END ("#{name}")
142
+ MSG
143
+ end
144
+
145
+ def uninstall_font
146
+ paths = find_fontist_font
147
+ return unless paths
148
+
149
+ paths.each do |path|
150
+ File.delete(path)
151
+ end
152
+
153
+ paths
154
+ end
155
+
156
+ def find_fontist_font
157
+ Fontist::FontistFont.find(name)
158
+ end
159
+
160
+ def installed_statuses
161
+ installed_styles(all_formulas)
162
+ end
163
+
164
+ def all_formulas
165
+ Fontist::Formula.all.to_h.values
166
+ end
167
+
168
+ def font_status
169
+ return unless formula
170
+
171
+ statuses = installed_styles([formula])
172
+ statuses.empty? ? nil : statuses
173
+ end
174
+
175
+ def installed_styles(formulas)
176
+ filter_blank(formulas) do |formula|
177
+ filter_blank(formula.fonts) do |font|
178
+ filter_blank(font.styles) do |style|
179
+ path(style)
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ def filter_blank(elements)
186
+ elements.map { |e| [e, yield(e)] }
187
+ .to_h
188
+ .reject { |_k, v| v.nil? || v.empty? }
189
+ end
190
+
191
+ def path(style)
192
+ font_paths.grep(/#{style.font}/i).first
193
+ end
194
+
195
+ def font_paths
196
+ @font_paths ||= Dir.glob(Fontist.fonts_path.join("**"))
197
+ end
198
+
199
+ def all_list
200
+ list_styles(all_formulas)
201
+ end
202
+
203
+ def font_list
204
+ return unless formula
205
+
206
+ list_styles([formula])
207
+ end
208
+
209
+ def list_styles(formulas)
210
+ map_to_hash(formulas) do |formula|
211
+ map_to_hash(formula.fonts) do |font|
212
+ map_to_hash(font.styles) do |style|
213
+ installed(style)
214
+ end
215
+ end
216
+ end
217
+ end
218
+
219
+ def map_to_hash(elements)
220
+ elements.map { |e| [e, yield(e)] }.to_h
221
+ end
222
+
223
+ def installed(style)
224
+ path(style) ? true : false
225
+ end
226
+
77
227
  end
78
228
  end
@@ -4,9 +4,11 @@ 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
9
- attr_accessor :key, :homepage, :description, :temp_resource
10
+ attr_accessor :license, :license_url, :license_required, :copyright
11
+ attr_accessor :key, :homepage, :description, :options, :temp_resource
10
12
 
11
13
  def font_list
12
14
  @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
 
@@ -105,12 +109,19 @@ module Fontist
105
109
  end
106
110
 
107
111
  def download_file(source)
108
- downloaded_file = Fontist::Downloader.download(
109
- source[:urls].sample, sha: source[:sha256], file_size: source[:file_size]
112
+ downloaded_file = Fontist::Utils::Downloader.download(
113
+ source[:urls].first,
114
+ sha: source[:sha256],
115
+ file_size: source[:file_size],
116
+ progress_bar: is_progress_bar_enabled
110
117
  )
111
118
 
112
119
  @downloaded = true
113
120
  downloaded_file
114
121
  end
122
+
123
+ def is_progress_bar_enabled
124
+ options.nil? ? false : options.fetch(:progress_bar, false)
125
+ end
115
126
  end
116
127
  end
@@ -0,0 +1,70 @@
1
+ module Fontist
2
+ class FontistFont
3
+ def initialize(font_name:)
4
+ @font_name = font_name
5
+
6
+ check_and_register_font_formulas
7
+ end
8
+
9
+ def self.find(name)
10
+ new(font_name: name).find
11
+ end
12
+
13
+ def find
14
+ return unless @font_name
15
+
16
+ filenames = fonts_filenames
17
+ return if filenames.empty?
18
+
19
+ paths = font_paths.select do |path|
20
+ filenames.any? { |f| File.basename(path).casecmp?(f) }
21
+ end
22
+
23
+ paths.empty? ? nil : paths
24
+ end
25
+
26
+ private
27
+
28
+ def fonts_filenames
29
+ fonts.map { |font| font.styles.map(&:font) }.flatten
30
+ end
31
+
32
+ def fonts
33
+ by_key || by_name || []
34
+ end
35
+
36
+ def by_key
37
+ _key, formula = formulas.detect do |key, _value|
38
+ key.to_s.casecmp?(@font_name)
39
+ end
40
+
41
+ return unless formula
42
+
43
+ formula.fonts
44
+ end
45
+
46
+ def by_name
47
+ _key, formula = formulas.detect do |_key, value|
48
+ value.fonts.map(&:name).map(&:downcase).include?(@font_name.downcase)
49
+ end
50
+
51
+ return unless formula
52
+
53
+ formula.fonts.select do |font|
54
+ font.name.casecmp?(@font_name)
55
+ end
56
+ end
57
+
58
+ def formulas
59
+ @formulas ||= Fontist::Registry.instance.formulas.to_h
60
+ end
61
+
62
+ def font_paths
63
+ Dir.glob(Fontist.fonts_path.join("**"))
64
+ end
65
+
66
+ def check_and_register_font_formulas
67
+ $check_and_register_font_formulas ||= Fontist::Formulas.register_formulas
68
+ end
69
+ end
70
+ end
@@ -28,7 +28,8 @@ module Fontist
28
28
 
29
29
  def find_fonts
30
30
  formulas = [find_formula].flatten
31
- match_fonts_by_name(formulas) unless formulas.empty?
31
+ fonts = take_fonts(formulas)
32
+ fonts.empty? ? nil : fonts
32
33
  end
33
34
 
34
35
  private
@@ -36,21 +37,23 @@ module Fontist
36
37
  attr_reader :font_name
37
38
 
38
39
  def find_formula
39
- find_by_font_name || find_by_font || []
40
+ find_by_key || find_by_font_name || find_by_font || []
40
41
  end
41
42
 
42
43
  def formulas
43
44
  @formulas ||= all.to_h
44
45
  end
45
46
 
46
- def match_fonts_by_name(formulas)
47
- matched_fonts = formulas.map do |formula|
48
- formula.fonts.select do |font|
49
- font.name.downcase == font_name.downcase
50
- end
47
+ def take_fonts(formulas)
48
+ formulas.map(&:fonts).flatten
49
+ end
50
+
51
+ def find_by_key
52
+ matched_formulas = formulas.select do |key, _value|
53
+ key.to_s.casecmp?(font_name)
51
54
  end
52
55
 
53
- matched_fonts.empty? ? nil : matched_fonts.flatten
56
+ matched_formulas.empty? ? nil : matched_formulas.values
54
57
  end
55
58
 
56
59
  def find_by_font_name
@@ -0,0 +1,103 @@
1
+ module Fontist
2
+ class FormulaTemplate
3
+ def self.create_formula_class(formula)
4
+ Class.new(FontFormula) do |klass|
5
+ cleanname = formula.fonts.first.name.gsub(/ /, "")
6
+ resource_name = formula.resources.to_h.keys.first
7
+ font_filename = formula.fonts.first.styles.first.font
8
+
9
+ key formula.key&.to_sym || formula.name.gsub(/ /, "_").downcase.to_sym
10
+ display_progress_bar formula.display_progress_bar if formula.display_progress_bar
11
+ desc formula.description
12
+ homepage formula.homepage
13
+
14
+ formula.resources.to_h.each do |filename, options|
15
+ resource filename do
16
+ urls options.urls
17
+ sha256 options.sha256
18
+ end
19
+ end
20
+
21
+ if formula.font_collections
22
+ formula.font_collections.each do |collection|
23
+ provides_font_collection do
24
+ filename collection.filename
25
+
26
+ collection.fonts.each do |font|
27
+ styles = font.styles.map { |s| [s.type, s.full_name] }.to_h
28
+ provides_font font.name, extract_styles_from_collection: styles
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ formula.fonts.each do |font|
35
+ provides_font(
36
+ font.name,
37
+ match_styles_from_file: font.styles.map do |style|
38
+ {
39
+ family_name: style.family_name,
40
+ style: style.type,
41
+ full_name: style.full_name,
42
+ post_script_name: style.post_script_name,
43
+ version: style.version,
44
+ description: style.description,
45
+ filename: style.font,
46
+ copyright: style.copyright,
47
+ }
48
+ end
49
+ )
50
+ end
51
+
52
+ klass.define_method :extract do
53
+ resource = resource(resource_name)
54
+
55
+ [formula.extract].flatten.each do |operation|
56
+ method = "#{operation.format}_extract"
57
+ resource = if operation.options
58
+ send(method, resource, **operation.options.to_h)
59
+ else
60
+ send(method, resource)
61
+ end
62
+ end
63
+
64
+ formula.fonts.each do |font|
65
+ match_fonts(resource, font.name)
66
+ end
67
+
68
+ if formula.font_collections
69
+ formula.font_collections.each do |collection|
70
+ collection.fonts.each do |font|
71
+ match_fonts(resource, font.name)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ klass.define_method :install do
78
+ case platform
79
+ when :macos
80
+ install_matched_fonts "$HOME/Library/Fonts/#{cleanname}"
81
+ when :linux
82
+ install_matched_fonts "/usr/share/fonts/truetype/#{cleanname.downcase}"
83
+ end
84
+ end
85
+
86
+ test do
87
+ case platform
88
+ when :macos
89
+ assert_predicate "$HOME/Library/Fonts/#{cleanname}/#{font_filename}", :exist?
90
+ when :linux
91
+ assert_predicate "/usr/share/fonts/truetype/#{cleanname.downcase}/#{font_filename}", :exist?
92
+ end
93
+ end
94
+
95
+ copyright formula.copyright
96
+ license_url formula.license_url
97
+
98
+ open_license formula.open_license if formula.open_license
99
+ requires_license_agreement formula.requires_license_agreement if formula.requires_license_agreement
100
+ end
101
+ end
102
+ end
103
+ end