fontist 1.2.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) 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 +16 -1
  16. data/lib/fontist.rb +23 -13
  17. data/lib/fontist/cli.rb +162 -0
  18. data/lib/fontist/errors.rb +5 -0
  19. data/lib/fontist/font.rb +181 -8
  20. data/lib/fontist/font_formula.rb +12 -5
  21. data/lib/fontist/fontist_font.rb +70 -0
  22. data/lib/fontist/formula.rb +27 -9
  23. data/lib/fontist/formula_template.rb +108 -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 +74 -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 +47 -0
  35. data/lib/fontist/import/formula_builder.rb +162 -0
  36. data/lib/fontist/import/formula_serializer.rb +133 -0
  37. data/lib/fontist/import/google.rb +16 -0
  38. data/lib/fontist/import/google/fonts_public.md +10 -0
  39. data/lib/fontist/import/google/fonts_public.pb.rb +71 -0
  40. data/lib/fontist/import/google/fonts_public.proto +46 -0
  41. data/lib/fontist/import/google/new_fonts_fetcher.rb +121 -0
  42. data/lib/fontist/import/google/skiplist.yml +8 -0
  43. data/lib/fontist/import/google_check.rb +34 -0
  44. data/lib/fontist/import/google_import.rb +180 -0
  45. data/lib/fontist/import/helpers/hash_helper.rb +13 -0
  46. data/lib/fontist/import/helpers/system_helper.rb +23 -0
  47. data/lib/fontist/import/otf/font_file.rb +91 -0
  48. data/lib/fontist/import/otf_parser.rb +25 -0
  49. data/lib/fontist/import/otf_style.rb +29 -0
  50. data/lib/fontist/import/otfinfo/otfinfo_requirement.rb +22 -0
  51. data/lib/fontist/import/otfinfo/template.erb +20 -0
  52. data/lib/fontist/import/otfinfo_generate.rb +45 -0
  53. data/lib/fontist/import/recursive_extraction.rb +128 -0
  54. data/lib/fontist/import/template_helper.rb +19 -0
  55. data/lib/fontist/import/text_helper.rb +30 -0
  56. data/lib/fontist/manifest.rb +2 -0
  57. data/lib/fontist/manifest/common.rb +60 -0
  58. data/lib/fontist/manifest/install.rb +36 -0
  59. data/lib/fontist/manifest/locations.rb +13 -0
  60. data/lib/fontist/registry.rb +1 -0
  61. data/lib/fontist/system_font.rb +53 -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 +69 -8
  65. data/lib/fontist/utils/dsl.rb +4 -0
  66. data/lib/fontist/utils/dsl/font.rb +37 -0
  67. data/lib/fontist/utils/exe_extractor.rb +20 -18
  68. data/lib/fontist/utils/msi_extractor.rb +31 -0
  69. data/lib/fontist/utils/seven_zip_extractor.rb +41 -0
  70. data/lib/fontist/utils/system.rb +23 -0
  71. data/lib/fontist/utils/ui.rb +27 -0
  72. data/lib/fontist/utils/zip_extractor.rb +10 -4
  73. data/lib/fontist/version.rb +1 -1
  74. metadata +185 -48
  75. data/lib/fontist/formulas/andale_font.rb +0 -80
  76. data/lib/fontist/formulas/arial_black_font.rb +0 -79
  77. data/lib/fontist/formulas/cleartype_fonts.rb +0 -227
  78. data/lib/fontist/formulas/comic_font.rb +0 -78
  79. data/lib/fontist/formulas/courier_font.rb +0 -81
  80. data/lib/fontist/formulas/euphemia_font.rb +0 -85
  81. data/lib/fontist/formulas/georgia_font.rb +0 -80
  82. data/lib/fontist/formulas/impact_font.rb +0 -78
  83. data/lib/fontist/formulas/montserrat_font.rb +0 -132
  84. data/lib/fontist/formulas/ms_truetype_fonts.rb +0 -125
  85. data/lib/fontist/formulas/open_sans_fonts.rb +0 -263
  86. data/lib/fontist/formulas/overpass_font.rb +0 -73
  87. data/lib/fontist/formulas/source_fonts.rb +0 -109
  88. data/lib/fontist/formulas/stix_fonts.rb +0 -108
  89. data/lib/fontist/formulas/tahoma_font.rb +0 -147
  90. data/lib/fontist/formulas/webding_font.rb +0 -78
  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 -44
  112. data/spec/fontist/utils/downloader_spec.rb +0 -35
  113. data/spec/fontist_spec.rb +0 -5
  114. data/spec/spec_helper.rb +0 -22
  115. data/spec/support/fontist_helper.rb +0 -10
@@ -0,0 +1,2 @@
1
+ require_relative "manifest/locations"
2
+ require_relative "manifest/install"
@@ -0,0 +1,60 @@
1
+ module Fontist
2
+ module Manifest
3
+ class Common
4
+ def initialize(manifest)
5
+ @manifest = manifest
6
+ end
7
+
8
+ def self.call(manifest)
9
+ new(manifest).call
10
+ end
11
+
12
+ def call
13
+ font_names.zip(font_paths).to_h
14
+ end
15
+
16
+ private
17
+
18
+ def font_names
19
+ fonts.keys
20
+ end
21
+
22
+ def fonts
23
+ @fonts ||= begin
24
+ unless File.exist?(@manifest)
25
+ raise Fontist::Errors::ManifestCouldNotBeFoundError
26
+ end
27
+
28
+ fonts = YAML.load_file(@manifest)
29
+ unless fonts.is_a?(Hash)
30
+ raise Fontist::Errors::ManifestCouldNotBeReadError
31
+ end
32
+
33
+ fonts
34
+ end
35
+ end
36
+
37
+ def font_paths
38
+ fonts.map do |font, styles|
39
+ styles_to_ary = [styles].flatten
40
+ style_paths_map(font, styles_to_ary)
41
+ end
42
+ end
43
+
44
+ def style_paths_map(font, names)
45
+ paths = style_paths(font, names)
46
+ names.zip(paths).to_h
47
+ end
48
+
49
+ def style_paths(font, names)
50
+ names.map do |style|
51
+ file_paths(font, style)
52
+ end
53
+ end
54
+
55
+ def file_paths(_font, _style)
56
+ raise NotImplementedError.new("Implement #file_paths in child class")
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "common"
2
+
3
+ module Fontist
4
+ module Manifest
5
+ class Install < Common
6
+ def initialize(manifest, confirmation: "no")
7
+ @manifest = manifest
8
+ @confirmation = confirmation
9
+ end
10
+
11
+ def self.call(manifest, confirmation: "no")
12
+ new(manifest, confirmation: confirmation).call
13
+ end
14
+
15
+ private
16
+
17
+ def file_paths(font, style)
18
+ paths = find_installed_font(font, style)
19
+ return paths unless paths.empty?
20
+
21
+ install_font(font)
22
+ find_installed_font(font, style)
23
+ end
24
+
25
+ def find_installed_font(font, style)
26
+ Fontist::SystemFont.find_with_style(font, style)
27
+ end
28
+
29
+ def install_font(font)
30
+ Fontist::Font.try_install(font, confirmation: @confirmation)
31
+ rescue Fontist::Errors::LicensingError
32
+ [] # try to install other fonts
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ require_relative "common"
2
+
3
+ module Fontist
4
+ module Manifest
5
+ class Locations < Common
6
+ private
7
+
8
+ def file_paths(font, style)
9
+ Fontist::SystemFont.find_with_style(font, style)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -32,6 +32,7 @@ module Fontist
32
32
  license: formula.instance.license,
33
33
  homepage: formula.instance.homepage ,
34
34
  description: formula.instance.description,
35
+ license_required: formula.instance.license_required,
35
36
  }
36
37
  end
37
38
 
@@ -1,7 +1,8 @@
1
1
  module Fontist
2
2
  class SystemFont
3
- def initialize(font:, sources: nil)
3
+ def initialize(font:, style: nil, sources: nil)
4
4
  @font = font
5
+ @style = style
5
6
  @user_sources = sources || []
6
7
  end
7
8
 
@@ -9,28 +10,59 @@ module Fontist
9
10
  new(font: font, sources: sources).find
10
11
  end
11
12
 
13
+ def self.find_with_style(font, style)
14
+ new(font: font, style: style).find_with_style
15
+ end
16
+
12
17
  def find
13
- paths = font_paths.grep(/#{font}/i)
18
+ paths = grep_font_paths(font)
14
19
  paths = lookup_using_font_name || [] if paths.empty?
15
20
 
16
21
  paths.empty? ? nil : paths
17
22
  end
18
23
 
24
+ def find_with_style
25
+ paths = lookup_using_font_and_style
26
+ return paths unless paths.empty?
27
+
28
+ grep_font_paths(font, style)
29
+ end
30
+
19
31
  private
20
32
 
21
- attr_reader :font, :user_sources
33
+ attr_reader :font, :style, :user_sources
22
34
 
23
35
  def normalize_default_paths
24
36
  @normalize_default_paths ||= default_sources["paths"].map do |path|
25
37
  require "etc"
26
38
  passwd = Etc.getpwuid
39
+ username = passwd ? passwd.name : Etc.getlogin
27
40
 
28
- passwd ? path.gsub("{username}", passwd.name) : path
41
+ username ? path.gsub("{username}", username) : path
29
42
  end
30
43
  end
31
44
 
45
+ def grep_font_paths(font, style = nil)
46
+ pattern = prepare_pattern(font, style)
47
+
48
+ paths = font_paths.map { |path| [File.basename(path), path] }.to_h
49
+ files = paths.keys
50
+ matched = files.grep(pattern)
51
+ paths.values_at(*matched).compact
52
+ end
53
+
54
+ def prepare_pattern(font, style = nil)
55
+ style = nil if style&.casecmp?("regular")
56
+
57
+ s = [font, style].compact.map { |x| Regexp.quote(x) }
58
+ .join(".*")
59
+ .gsub("\\ ", "\s?") # space independent
60
+
61
+ Regexp.new(s, Regexp::IGNORECASE)
62
+ end
63
+
32
64
  def font_paths
33
- Dir.glob((
65
+ @font_paths ||= Dir.glob((
34
66
  user_sources +
35
67
  normalize_default_paths +
36
68
  [fontist_fonts_path.join("**")]
@@ -46,23 +78,8 @@ module Fontist
46
78
  @fontist_fonts_path ||= Fontist.fonts_path
47
79
  end
48
80
 
49
-
50
81
  def user_os
51
- @user_os ||= (
52
- host_os = RbConfig::CONFIG["host_os"]
53
- case host_os
54
- when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
55
- :windows
56
- when /darwin|mac os/
57
- :macos
58
- when /linux/
59
- :linux
60
- when /solaris|bsd/
61
- :unix
62
- else
63
- raise Fontist::Error, "unknown os: #{host_os.inspect}"
64
- end
65
- )
82
+ Fontist::Utils::System.user_os
66
83
  end
67
84
 
68
85
  def map_name_to_valid_font_names
@@ -71,11 +88,25 @@ module Fontist
71
88
  end
72
89
 
73
90
  def system_path_file
74
- File.open(Fontist.lib_path.join("fontist", "system.yml"))
91
+ File.open(Fontist.system_file_path)
75
92
  end
76
93
 
77
94
  def default_sources
78
95
  @default_sources ||= YAML.load(system_path_file)["system"][user_os.to_s]
79
96
  end
97
+
98
+ def lookup_using_font_and_style
99
+ styles = Formula.find_styles(font, style)
100
+ filenames = styles.map(&:font)
101
+ filenames.flat_map do |filename|
102
+ search_font_paths(filename)
103
+ end
104
+ end
105
+
106
+ def search_font_paths(filename)
107
+ font_paths.select do |path|
108
+ File.basename(path) == filename
109
+ end
110
+ end
80
111
  end
81
112
  end
@@ -1,7 +1,12 @@
1
+ require "fontist/utils/ui"
2
+ require "fontist/utils/system"
1
3
  require "fontist/utils/dsl"
4
+ require "fontist/utils/dsl/font"
2
5
  require "fontist/utils/downloader"
3
6
  require "fontist/utils/zip_extractor"
4
7
  require "fontist/utils/exe_extractor"
8
+ require "fontist/utils/msi_extractor"
9
+ require "fontist/utils/seven_zip_extractor"
5
10
 
6
11
  module Fontist
7
12
  module Utils
@@ -0,0 +1,88 @@
1
+ module Fontist
2
+ module Utils
3
+ class Cache
4
+ def fetch(key, bar: nil)
5
+ map = load_cache
6
+ if cache_exist?(map[key])
7
+ print_bar(bar, map[key]) if bar
8
+
9
+ return downloaded_file(map[key])
10
+ end
11
+
12
+ generated_file = yield
13
+ path = save_cache(generated_file, key)
14
+
15
+ downloaded_file(path)
16
+ end
17
+
18
+ private
19
+
20
+ def cache_map_path
21
+ Fontist.downloads_path.join("map.yml")
22
+ end
23
+
24
+ def load_cache
25
+ cache_map_path.exist? ? YAML.load_file(cache_map_path) : {}
26
+ end
27
+
28
+ def downloaded_file(path)
29
+ File.new(downloaded_path(path))
30
+ end
31
+
32
+ def cache_exist?(path)
33
+ path && File.exist?(downloaded_path(path))
34
+ end
35
+
36
+ def downloaded_path(path)
37
+ Fontist.downloads_path.join(path)
38
+ end
39
+
40
+ def print_bar(bar, path)
41
+ File.size(downloaded_path(path)).tap do |size|
42
+ bar.total = size
43
+ bar.increment(size)
44
+ bar.finish("cache")
45
+ end
46
+ end
47
+
48
+ def save_cache(generated_file, key)
49
+ path = move_to_downloads(generated_file)
50
+
51
+ map = load_cache
52
+ map[key] = path
53
+ File.write(cache_map_path, YAML.dump(map))
54
+
55
+ path
56
+ end
57
+
58
+ def move_to_downloads(source)
59
+ create_downloads_directory
60
+ path = generate_file_path(source)
61
+ move(source, path)
62
+ relative_to_downloads(path)
63
+ end
64
+
65
+ def create_downloads_directory
66
+ unless Fontist.downloads_path.exist?
67
+ FileUtils.mkdir_p(Fontist.downloads_path)
68
+ end
69
+ end
70
+
71
+ def generate_file_path(source)
72
+ dir = Dir.mktmpdir(nil, Fontist.downloads_path)
73
+ filename = source.original_filename
74
+ File.join(dir, filename)
75
+ end
76
+
77
+ def move(source_file, target_path)
78
+ # Windows requires file descriptors to be closed before files are moved
79
+ source_file.close
80
+ FileUtils.mv(source_file.path, target_path)
81
+ end
82
+
83
+ def relative_to_downloads(path)
84
+ Pathname.new(path).relative_path_from(Fontist.downloads_path).to_s
85
+ end
86
+ end
87
+ end
88
+ end
@@ -1,16 +1,21 @@
1
+ require_relative "cache"
2
+
1
3
  module Fontist
2
4
  module Utils
3
5
  class Downloader
4
6
  def initialize(file, file_size: nil, sha: nil, progress_bar: nil)
5
7
  # TODO: If the first mirror fails, try the second one
6
8
  @file = file
7
- @progress_bar = progress_bar
8
9
  @sha = [sha].flatten.compact
9
10
  @file_size = (file_size || default_file_size).to_i
11
+ @progress_bar = set_progress_bar(progress_bar)
12
+ @cache = Cache.new
10
13
  end
11
14
 
12
15
  def download
13
- file = download_file
16
+ file = @cache.fetch(@file, bar: @progress_bar) do
17
+ download_file
18
+ end
14
19
 
15
20
  if !sha.empty? && !sha.include?(Digest::SHA256.file(file).to_s)
16
21
  raise(Fontist::Errors::TamperedFileError.new(
@@ -42,33 +47,89 @@ module Fontist
42
47
  options[:download_path] || Fontist.root_path.join("tmp")
43
48
  end
44
49
 
45
- def download_file
46
- bar = ProgressBar.new(file_size / byte_to_megabyte)
50
+ def set_progress_bar(progress_bar)
51
+ if ENV.fetch("TEST_ENV", "") === "CI" || progress_bar
52
+ ProgressBar.new(@file_size)
53
+ else
54
+ NullProgressBar.new
55
+ end
56
+ end
47
57
 
48
- Down.download(
58
+ def download_file
59
+ file = Down.download(
49
60
  @file,
50
61
  open_timeout: 10,
51
62
  read_timeout: 10,
63
+ content_length_proc: ->(content_length) {
64
+ @progress_bar.total = content_length if content_length
65
+ },
52
66
  progress_proc: -> (progress) {
53
- bar.increment(progress / byte_to_megabyte) if @progress_bar === true
67
+ @progress_bar.increment(progress)
54
68
  }
55
69
  )
56
70
 
71
+ @progress_bar.finish
72
+
73
+ file
57
74
  rescue Down::NotFound
58
75
  raise(Fontist::Errors::InvalidResourceError.new("Invalid URL: #{@file}"))
59
76
  end
60
77
  end
61
78
 
79
+ class NullProgressBar
80
+ def total=(_)
81
+ # do nothing
82
+ end
83
+
84
+ def increment(_)
85
+ # do nothing
86
+ end
87
+
88
+ def finish(_ = nil)
89
+ # do nothing
90
+ end
91
+ end
92
+
62
93
  class ProgressBar
63
94
  def initialize(total)
64
95
  @counter = 1
65
96
  @total = total
66
97
  end
67
98
 
99
+ def total=(total)
100
+ @total = total
101
+ end
102
+
68
103
  def increment(progress)
69
- complete = sprintf("%#.2f%%", ((@counter.to_f / @total.to_f) * 100))
70
- print "\r\e[0KDownloads: #{@counter}MB/#{@total}MB (#{complete})"
71
104
  @counter = progress
105
+ Fontist.ui.print "\r\e[0KDownloads: #{counter_mb}MB/#{total_mb}MB " \
106
+ "(#{completeness})"
107
+ end
108
+
109
+ def finish(message = nil)
110
+ if message
111
+ Fontist.ui.print " (#{message})\n"
112
+ else
113
+ Fontist.ui.print "\n"
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ def completeness
120
+ sprintf("%#.2f%%", (@counter.fdiv(@total) * 100)) # rubocop:disable Style/FormatStringToken, Metrics/LineLength
121
+ end
122
+
123
+ def counter_mb
124
+ @counter / byte_to_megabyte
125
+ end
126
+
127
+ def total_mb
128
+ @total / byte_to_megabyte
129
+ end
130
+
131
+ def byte_to_megabyte
132
+ @byte_to_megabyte ||= 1024 * 1024
72
133
  end
73
134
  end
74
135
  end