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
@@ -1,9 +1,45 @@
1
+ require "git"
1
2
  require "fontist/utils"
2
3
  require "fontist/font_formula"
4
+ require "fontist/formula_template"
3
5
 
4
6
  module Fontist
5
7
  module Formulas
6
8
  def self.register_formulas
9
+ load_formulas
10
+ update_registry
11
+ end
12
+
13
+ def self.fetch_formulas
14
+ if Dir.exist?(Fontist.formulas_repo_path)
15
+ Git.open(Fontist.formulas_repo_path).pull
16
+ else
17
+ Git.clone(Fontist.formulas_repo_url,
18
+ Fontist.formulas_repo_path,
19
+ depth: 1)
20
+ end
21
+ end
22
+
23
+ def self.load_formulas
24
+ Dir[Fontist.formulas_path.join("**/*.yml").to_s].sort.each do |file|
25
+ create_formula_class(file)
26
+ end
27
+ end
28
+
29
+ def self.create_formula_class(file)
30
+ formula = parse_to_object(YAML.load_file(file))
31
+ class_name = name_to_classname(formula.name)
32
+ return if Formulas.const_defined?(class_name)
33
+
34
+ klass = FormulaTemplate.create_formula_class(formula)
35
+ Formulas.const_set(class_name, klass)
36
+ end
37
+
38
+ def self.parse_to_object(hash)
39
+ JSON.parse(hash.to_json, object_class: OpenStruct)
40
+ end
41
+
42
+ def self.update_registry
7
43
  Formulas.constants.select do |constant|
8
44
  if Formulas.const_get(constant).is_a?(Class)
9
45
  klass = "Fontist::Formulas::#{constant}"
@@ -11,5 +47,10 @@ module Fontist
11
47
  end
12
48
  end
13
49
  end
50
+
51
+ def self.name_to_classname(name)
52
+ cleanname = name.gsub(/ /, "").sub(/\S/, &:upcase)
53
+ "#{cleanname}Font"
54
+ end
14
55
  end
15
56
  end
@@ -0,0 +1,9 @@
1
+ module Fontist
2
+ module Import
3
+ class << self
4
+ def name_to_filename(name)
5
+ name.downcase.gsub(" ", "_") + ".yml"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,65 @@
1
+ require_relative "formula_serializer"
2
+
3
+ module Fontist
4
+ module Import
5
+ class ConvertFormulas
6
+ def call
7
+ find_formulas.each do |formula|
8
+ convert_to_yaml(formula)
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def find_formulas
15
+ require_formulas_files
16
+ formulas_instances
17
+ end
18
+
19
+ def require_formulas_files
20
+ path = Fontist.lib_path.join("fontist", "converted_formulas")
21
+ Dir[path.join("**/*.rb").to_s].sort.each do |file|
22
+ require file
23
+ end
24
+ end
25
+
26
+ def formulas_instances
27
+ classes = Formulas.constants.select do |constant|
28
+ Formulas.const_get(constant).is_a?(Class)
29
+ end
30
+
31
+ classes.map do |constant|
32
+ Object.const_get("Fontist::Formulas::#{constant}").instance
33
+ end
34
+ end
35
+
36
+ def convert_to_yaml(formula)
37
+ hash = formula_hash(formula)
38
+ write_yaml(formula, hash)
39
+ end
40
+
41
+ def formula_hash(formula)
42
+ code = File.read(formula_path(formula))
43
+ FormulaSerializer.new(formula, code).call
44
+ end
45
+
46
+ def formula_path(formula)
47
+ formula.method(:extract).source_location.first
48
+ end
49
+
50
+ def write_yaml(formula, hash)
51
+ File.write(yaml_formula_path(formula), YAML.dump(stringify_keys(hash)))
52
+ end
53
+
54
+ def yaml_formula_path(formula)
55
+ name = formula_path(formula).match(/formulas\/(.*)_fonts?.rb/)[1]
56
+
57
+ Fontist.formulas_path.join(name + ".yml")
58
+ end
59
+
60
+ def stringify_keys(hash)
61
+ JSON.parse(hash.to_json)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,74 @@
1
+ require "fontist/import"
2
+ require_relative "recursive_extraction"
3
+ require_relative "otf/font_file"
4
+ require_relative "files/collection_file"
5
+ require_relative "helpers/hash_helper"
6
+ require_relative "formula_builder"
7
+
8
+ module Fontist
9
+ module Import
10
+ class CreateFormula
11
+ FONT_PATTERN = /(\.ttf|\.otf)$/i.freeze
12
+ FONT_COLLECTION_PATTERN = /\.ttc$/i.freeze
13
+ LICENSE_PATTERN = /(OFL\.txt|UFL\.txt|LICENSE\.txt)$/i.freeze
14
+
15
+ def initialize(url, options = {})
16
+ @url = url
17
+ @options = options
18
+ end
19
+
20
+ def call
21
+ save(formula)
22
+ end
23
+
24
+ private
25
+
26
+ def formula
27
+ builder = FormulaBuilder.new
28
+ builder.url = @url
29
+ builder.archive = download(@url)
30
+ builder.extractor = extractor(builder.archive)
31
+ builder.options = @options
32
+ builder.font_files = font_files(builder.extractor)
33
+ builder.font_collection_files = font_collection_files(builder.extractor)
34
+ builder.license_text = license_texts(builder.extractor).first
35
+ builder.formula
36
+ end
37
+
38
+ def download(url)
39
+ return url if File.exist?(url)
40
+
41
+ Fontist::Utils::Downloader.download(url, progress_bar: true).path
42
+ end
43
+
44
+ def extractor(archive)
45
+ RecursiveExtraction.new(archive)
46
+ end
47
+
48
+ def font_files(extractor)
49
+ extractor.extract(FONT_PATTERN) do |path|
50
+ Otf::FontFile.new(path)
51
+ end
52
+ end
53
+
54
+ def font_collection_files(extractor)
55
+ extractor.extract(FONT_COLLECTION_PATTERN) do |path|
56
+ Files::CollectionFile.new(path)
57
+ end
58
+ end
59
+
60
+ def license_texts(extractor)
61
+ extractor.extract(LICENSE_PATTERN) do |path|
62
+ File.read(path)
63
+ end
64
+ end
65
+
66
+ def save(hash)
67
+ filename = Import.name_to_filename(hash[:name])
68
+ yaml = YAML.dump(Helpers::HashHelper.stringify_keys(hash))
69
+ File.write(filename, yaml)
70
+ filename
71
+ end
72
+ end
73
+ end
74
+ end
@@ -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,48 @@
1
+ require "fontist/import/helpers/system_helper"
2
+ require_relative "../otf/font_file"
3
+
4
+ module Fontist
5
+ module Import
6
+ module Files
7
+ class CollectionFile
8
+ STRIP_TTC_BINARY = Fontist.root_path.join("bin", "stripttc")
9
+
10
+ attr_reader :fonts
11
+
12
+ def initialize(path)
13
+ @path = path
14
+ @fonts = read
15
+ end
16
+
17
+ def filename
18
+ File.basename(@path)
19
+ end
20
+
21
+ private
22
+
23
+ def read
24
+ switch_to_temp_dir do |tmp_dir|
25
+ extract_ttfs(tmp_dir).map do |path|
26
+ Otf::FontFile.new(path)
27
+ end
28
+ end
29
+ end
30
+
31
+ def switch_to_temp_dir
32
+ Dir.mktmpdir do |tmp_dir|
33
+ Dir.chdir(tmp_dir) do
34
+ yield tmp_dir
35
+ end
36
+ end
37
+ end
38
+
39
+ def extract_ttfs(tmp_dir)
40
+ Helpers::SystemHelper.run("#{STRIP_TTC_BINARY} #{@path}")
41
+
42
+ basename = File.basename(@path, ".ttc")
43
+ Dir.glob(File.join(tmp_dir, "#{basename}_[0-9][0-9].ttf"))
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,115 @@
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 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
+ options = { urls: [@url] + (@options[:mirror] || []),
59
+ sha256: Digest::SHA256.file(@archive).to_s }
60
+
61
+ { filename => options }
62
+ end
63
+
64
+ def font_collections
65
+ return if @font_collection_files.empty?
66
+
67
+ collections = @font_collection_files.map do |file|
68
+ fonts = fonts_from_files(file.fonts, :to_collection_style)
69
+ { filename: file.filename, fonts: fonts }
70
+ end
71
+
72
+ collections.sort_by do |x|
73
+ x[:filename]
74
+ end
75
+ end
76
+
77
+ def fonts
78
+ return if @font_files.empty?
79
+
80
+ fonts_from_files(@font_files, :to_style)
81
+ end
82
+
83
+ def fonts_from_files(files, style_type = :to_style)
84
+ groups = files.group_by(&:family_name)
85
+
86
+ fonts = groups.map do |name, group|
87
+ { name: name,
88
+ styles: group.map(&style_type) }
89
+ end
90
+
91
+ fonts.sort_by do |x|
92
+ x[:name]
93
+ end
94
+ end
95
+
96
+ def extract
97
+ @extractor.operations
98
+ end
99
+
100
+ def copyright
101
+ both_fonts.first.copyright
102
+ end
103
+
104
+ def license_url
105
+ both_fonts.first.license_url
106
+ end
107
+
108
+ def open_license
109
+ return unless @license_text
110
+
111
+ TextHelper.cleanup(@license_text)
112
+ end
113
+ end
114
+ end
115
+ end