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,5 @@
1
+ require_relative "extractors/extractor"
2
+ require_relative "extractors/zip_extractor"
3
+ require_relative "extractors/ole_extractor"
4
+ require_relative "extractors/seven_zip_extractor"
5
+ require_relative "extractors/cab_extractor"
@@ -0,0 +1,37 @@
1
+ module Fontist
2
+ module Import
3
+ module Extractors
4
+ class CabExtractor < Extractor
5
+ def extract
6
+ dir = Dir.mktmpdir
7
+ extract_exe(@archive, dir)
8
+ dir
9
+ end
10
+
11
+ def format
12
+ File.extname(@archive) == ".exe" ? "exe" : "cab"
13
+ end
14
+
15
+ private
16
+
17
+ def extract_exe(archive, dir)
18
+ opened = decompressor.search(archive)
19
+ file = opened.files
20
+
21
+ while file
22
+ path = File.join(dir, file.filename)
23
+ decompressor.extract(file, path)
24
+ file = file.next
25
+ end
26
+ end
27
+
28
+ def decompressor
29
+ @decompressor ||= begin
30
+ require "libmspack"
31
+ LibMsPack::CabDecompressor.new
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ module Fontist
2
+ module Import
3
+ module Extractors
4
+ class Extractor
5
+ def initialize(archive)
6
+ @archive = archive
7
+ end
8
+
9
+ def extract
10
+ raise NotImplementedError.new("You must implement this method")
11
+ end
12
+
13
+ def format
14
+ raise NotImplementedError.new("You must implement this method")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ module Fontist
2
+ module Import
3
+ module Extractors
4
+ class OleExtractor < Extractor
5
+ def extract
6
+ dir = Dir.mktmpdir
7
+ extract_ole(@archive, dir)
8
+ dir
9
+ end
10
+
11
+ def format
12
+ "msi"
13
+ end
14
+
15
+ private
16
+
17
+ def extract_ole(archive, dir)
18
+ ole = storage.open(archive)
19
+ file = the_largest_file(ole)
20
+
21
+ content = ole.file.read(file)
22
+ path = File.join(dir, "data.cab")
23
+ File.open(path, "wb") { |f| f.write(content) }
24
+ end
25
+
26
+ def storage
27
+ @storage ||= begin
28
+ require "ole/storage"
29
+ Ole::Storage
30
+ end
31
+ end
32
+
33
+ def the_largest_file(ole)
34
+ ole.dir.entries(".").max_by do |x|
35
+ ole.file.size(x)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ module Fontist
2
+ module Import
3
+ module Extractors
4
+ class SevenZipExtractor < Extractor
5
+ def extract
6
+ dir = Dir.mktmpdir
7
+ extract_7z(@archive, dir)
8
+ dir
9
+ end
10
+
11
+ def try
12
+ File.open(@archive, "rb") do |file|
13
+ reader.open(file)
14
+ end
15
+
16
+ true
17
+ rescue StandardError => e
18
+ return false if e.message.start_with?("Invalid file format")
19
+
20
+ raise
21
+ end
22
+
23
+ def format
24
+ "seven_zip"
25
+ end
26
+
27
+ private
28
+
29
+ def extract_7z(archive, dir)
30
+ File.open(archive, "rb") do |file|
31
+ reader.extract_all(file, dir)
32
+ end
33
+ end
34
+
35
+ def reader
36
+ @reader ||= begin
37
+ require "seven_zip_ruby"
38
+ SevenZipRuby::Reader
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ require "zip"
2
+
3
+ module Fontist
4
+ module Import
5
+ module Extractors
6
+ class ZipExtractor < Extractor
7
+ def extract
8
+ dir = Dir.mktmpdir
9
+ extract_zip(@archive, dir)
10
+ dir
11
+ end
12
+
13
+ def format
14
+ "zip"
15
+ end
16
+
17
+ private
18
+
19
+ def extract_zip(archive, dir)
20
+ Zip::File.open(archive) do |zip_file|
21
+ zip_file.each do |entry|
22
+ path = File.join(dir, entry.name)
23
+ FileUtils.mkdir_p(File.dirname(path))
24
+ entry.extract(path)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ require "extract_ttc"
2
+ require "fontist/import/helpers/system_helper"
3
+ require_relative "../otf/font_file"
4
+
5
+ module Fontist
6
+ module Import
7
+ module Files
8
+ class CollectionFile
9
+ attr_reader :fonts
10
+
11
+ def initialize(path)
12
+ @path = path
13
+ @fonts = read
14
+ end
15
+
16
+ def filename
17
+ File.basename(@path)
18
+ end
19
+
20
+ private
21
+
22
+ def read
23
+ switch_to_temp_dir do |tmp_dir|
24
+ extract_ttfs(tmp_dir).map do |path|
25
+ Otf::FontFile.new(path)
26
+ end
27
+ end
28
+ end
29
+
30
+ def switch_to_temp_dir
31
+ Dir.mktmpdir do |tmp_dir|
32
+ Dir.chdir(tmp_dir) do
33
+ yield tmp_dir
34
+ end
35
+ end
36
+ end
37
+
38
+ def extract_ttfs(tmp_dir)
39
+ filenames = ExtractTtc.extract(@path)
40
+ filenames.map do |filename|
41
+ File.join(tmp_dir, filename)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,162 @@
1
+ require_relative "text_helper"
2
+
3
+ module Fontist
4
+ module Import
5
+ class FormulaBuilder
6
+ FORMULA_ATTRIBUTES = %i[name description homepage resources
7
+ font_collections fonts extract copyright
8
+ license_url open_license].freeze
9
+
10
+ attr_accessor :archive,
11
+ :url,
12
+ :extractor,
13
+ :options,
14
+ :font_files,
15
+ :font_collection_files,
16
+ :license_text
17
+
18
+ def initialize
19
+ @options = {}
20
+ end
21
+
22
+ def formula
23
+ FORMULA_ATTRIBUTES.map { |name| [name, send(name)] }.to_h.compact
24
+ end
25
+
26
+ private
27
+
28
+ def name
29
+ return options[:name] if options[:name]
30
+
31
+ unique_names = both_fonts.map(&:family_name).uniq
32
+ TextHelper.longest_common_prefix(unique_names) ||
33
+ both_fonts.first.family_name
34
+ end
35
+
36
+ def both_fonts
37
+ @both_fonts ||= group_fonts
38
+ end
39
+
40
+ def group_fonts
41
+ files = (@font_files + @font_collection_files.map(&:fonts)).flatten
42
+ raise Errors::FontNotFoundError, "No font found" if files.empty?
43
+
44
+ files
45
+ end
46
+
47
+ def description
48
+ name
49
+ end
50
+
51
+ def homepage
52
+ both_fonts.first.homepage
53
+ end
54
+
55
+ def resources
56
+ filename = name.gsub(" ", "_") + "." + @extractor.extension
57
+
58
+ { filename => resource_options }
59
+ end
60
+
61
+ def resource_options
62
+ urls = []
63
+ sha = []
64
+ downloads do |url, path|
65
+ urls << url
66
+ sha << Digest::SHA256.file(path).to_s
67
+ end
68
+
69
+ sha = prepare_sha256(sha)
70
+
71
+ { urls: urls, sha256: sha }
72
+ end
73
+
74
+ def downloads
75
+ yield @url, @archive
76
+
77
+ mirrors.each do |url|
78
+ path = download(url)
79
+ next unless path
80
+
81
+ yield url, path
82
+ end
83
+ end
84
+
85
+ def mirrors
86
+ @options[:mirror] || []
87
+ end
88
+
89
+ def download(url)
90
+ Fontist::Utils::Downloader.download(url, progress_bar: true).path
91
+ rescue Errors::InvalidResourceError
92
+ Fontist.ui.error("WARN: a mirror is not found '#{url}'")
93
+ nil
94
+ end
95
+
96
+ def prepare_sha256(input)
97
+ output = input.uniq
98
+ return output.first if output.size == 1
99
+
100
+ checksums = output.join(", ")
101
+ Fontist.ui.error("WARN: SHA256 differs (#{checksums})")
102
+ output
103
+ end
104
+
105
+ def font_collections
106
+ return if @font_collection_files.empty?
107
+
108
+ collections = @font_collection_files.map do |file|
109
+ fonts = fonts_from_files(file.fonts, :to_collection_style)
110
+ { filename: file.filename, fonts: fonts }
111
+ end
112
+
113
+ collections.sort_by do |x|
114
+ x[:filename]
115
+ end
116
+ end
117
+
118
+ def fonts
119
+ return if @font_files.empty?
120
+
121
+ fonts_from_files(@font_files, :to_style)
122
+ end
123
+
124
+ def fonts_from_files(files, style_type = :to_style)
125
+ groups = files.group_by(&:family_name)
126
+
127
+ fonts = groups.map do |name, group|
128
+ { name: name,
129
+ styles: group.map(&style_type) }
130
+ end
131
+
132
+ fonts.sort_by do |x|
133
+ x[:name]
134
+ end
135
+ end
136
+
137
+ def extract
138
+ @extractor.operations
139
+ end
140
+
141
+ def copyright
142
+ both_fonts.first.copyright
143
+ end
144
+
145
+ def license_url
146
+ both_fonts.first.license_url
147
+ end
148
+
149
+ def open_license
150
+ unless @license_text
151
+ Fontist.ui.error("WARN: please add license manually")
152
+ return
153
+ end
154
+
155
+ Fontist.ui.error("WARN: ensure it's an open license, otherwise change \
156
+ to 'requires_license_agreement'")
157
+
158
+ TextHelper.cleanup(@license_text)
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,133 @@
1
+ module Fontist
2
+ module Import
3
+ class FormulaSerializer
4
+ def initialize(formula, code)
5
+ @formula = formula
6
+ @code = code
7
+ end
8
+
9
+ def call
10
+ to_h.compact
11
+ end
12
+
13
+ private
14
+
15
+ def to_h
16
+ info_attributes.merge(fonts_attributes).merge(license_attributes)
17
+ end
18
+
19
+ def info_attributes
20
+ { key: @formula.key,
21
+ name: formula_name(@formula),
22
+ description: @formula.description,
23
+ homepage: @formula.homepage }
24
+ end
25
+
26
+ def fonts_attributes
27
+ { display_progress_bar: formula_progress_bar(@formula),
28
+ resources: @formula.resources,
29
+ font_collections: font_collections(@formula.fonts),
30
+ fonts: standalone_fonts(@formula.fonts),
31
+ extract: extract_type(@code) }
32
+ end
33
+
34
+ def license_attributes
35
+ { copyright: @formula.copyright,
36
+ license_url: @formula.license_url,
37
+ requires_license_agreement: requires_license_agreement(@formula),
38
+ open_license: open_license(@formula) }
39
+ end
40
+
41
+ def formula_name(formula)
42
+ formula.class.name.match(/Formulas::(.+)Fonts?/)[1]
43
+ end
44
+
45
+ def formula_progress_bar(formula)
46
+ formula.options&.dig(:progress_bar)
47
+ end
48
+
49
+ def font_collections(fonts)
50
+ collections = collections(fonts)
51
+ return if collections.empty?
52
+
53
+ collections.map do |filename, coll_fonts|
54
+ { filename: filename,
55
+ fonts: collection_fonts(coll_fonts) }
56
+ end
57
+ end
58
+
59
+ def collections(fonts)
60
+ fonts.select { |f| collection_font?(f) }
61
+ .group_by { |f| f[:styles].first[:font] }
62
+ end
63
+
64
+ def collection_font?(font)
65
+ font[:styles].first[:font].end_with?(".ttc", ".TTC")
66
+ end
67
+
68
+ def collection_fonts(fonts)
69
+ fonts.map do |f|
70
+ { name: f[:name], styles: collection_font_styles(f[:styles]) }
71
+ end
72
+ end
73
+
74
+ def collection_font_styles(styles)
75
+ styles.map do |s|
76
+ collection_font_style(s)
77
+ end
78
+ end
79
+
80
+ def collection_font_style(style)
81
+ Hash.new.tap do |h|
82
+ h[:type] = style[:type]
83
+
84
+ if style[:collection]
85
+ h[:full_name] = collection_full_name(style[:collection])
86
+ end
87
+ end
88
+ end
89
+
90
+ def collection_full_name(collection_label)
91
+ if collection_label.is_a?(Hash)
92
+ collection_label.values_at(:name, :style).join(" ")
93
+ else
94
+ collection_label
95
+ end
96
+ end
97
+
98
+ def standalone_fonts(fonts)
99
+ standalone = fonts.reject do |f|
100
+ collection_font?(f)
101
+ end
102
+
103
+ standalone.empty? ? nil : standalone
104
+ end
105
+
106
+ # rubocop:disable Metrics/MethodLength, Metrics/LineLength
107
+ def extract_type(code)
108
+ case code
109
+ when /def extract.+exe_extract.+cab_extract.+ppviewer\.cab/m
110
+ [{ format: :exe }, { format: :cab, file: "ppviewer.cab" }]
111
+ when /def extract.+cab_extract/m
112
+ { format: :cab }
113
+ when /def extract.+(zip_extract|unzip).+fonts_sub_dir: "(.+?)"/m
114
+ dir = code.match(/def extract.+(zip_extract|unzip).+fonts_sub_dir: "(.+?)"/m)[2]
115
+ { format: :zip, options: { fonts_sub_dir: dir } }
116
+ when /def extract.+(zip_extract|unzip)/m
117
+ { format: :zip }
118
+ else
119
+ raise NotImplementedError, "Please implement an extract format"
120
+ end
121
+ end
122
+ # rubocop:enable Metrics/MethodLength, Metrics/LineLength
123
+
124
+ def requires_license_agreement(formula)
125
+ formula.license if formula.license && formula.license_required
126
+ end
127
+
128
+ def open_license(formula)
129
+ formula.license if formula.license && !formula.license_required
130
+ end
131
+ end
132
+ end
133
+ end