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,13 @@
1
+ module Fontist
2
+ module Import
3
+ module Helpers
4
+ module HashHelper
5
+ class << self
6
+ def stringify_keys(hash)
7
+ JSON.parse(hash.to_json)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module Fontist
2
+ module Import
3
+ module Helpers
4
+ module SystemHelper
5
+ class << self
6
+ def run(command)
7
+ unless ENV.fetch("TEST_ENV", "") === "CI"
8
+ Fontist.ui.say("Run `#{command}`")
9
+ end
10
+
11
+ result = `#{command}`
12
+ unless $CHILD_STATUS.to_i.zero?
13
+ raise Errors::BinaryCallError,
14
+ "Failed to run #{command}, status: #{$CHILD_STATUS}"
15
+ end
16
+
17
+ result
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,91 @@
1
+ require_relative "../otfinfo/otfinfo_requirement"
2
+ require_relative "../text_helper"
3
+
4
+ module Fontist
5
+ module Import
6
+ module Otf
7
+ class FontFile
8
+ REQUIREMENTS = {
9
+ otfinfo: Otfinfo::OtfinfoRequirement.new,
10
+ }.freeze
11
+
12
+ STYLE_ATTRIBUTES = %i[family_name type full_name post_script_name
13
+ version description copyright font].freeze
14
+ COLLECTION_ATTRIBUTES = STYLE_ATTRIBUTES.reject { |a| a == :font }
15
+
16
+ attr_reader :path
17
+
18
+ def initialize(path)
19
+ @path = path
20
+ @info = read
21
+ end
22
+
23
+ def to_style
24
+ STYLE_ATTRIBUTES.map { |name| [name, send(name)] }.to_h.compact
25
+ end
26
+
27
+ def to_collection_style
28
+ COLLECTION_ATTRIBUTES.map { |name| [name, send(name)] }.to_h.compact
29
+ end
30
+
31
+ def family_name
32
+ info["Preferred family"] || info["Family"]
33
+ end
34
+
35
+ def type
36
+ info["Preferred subfamily"] || info["Subfamily"]
37
+ end
38
+
39
+ def full_name
40
+ info["Full name"]
41
+ end
42
+
43
+ def post_script_name
44
+ info["PostScript name"]
45
+ end
46
+
47
+ def version
48
+ return unless info["Version"]
49
+
50
+ info["Version"].gsub("Version ", "")
51
+ end
52
+
53
+ def description
54
+ info["Description"]
55
+ end
56
+
57
+ def font
58
+ File.basename(@path)
59
+ end
60
+
61
+ def copyright
62
+ info["Copyright"]
63
+ end
64
+
65
+ def homepage
66
+ info["Vendor URL"]
67
+ end
68
+
69
+ def license_url
70
+ info["License URL"]
71
+ end
72
+
73
+ private
74
+
75
+ attr_reader :info
76
+
77
+ def read
78
+ text = REQUIREMENTS[:otfinfo].call(@path)
79
+
80
+ text
81
+ .encode("UTF-8", invalid: :replace, replace: "")
82
+ .split("\n")
83
+ .select { |x| x.include?(":") }
84
+ .map { |x| x.split(":", 2) }
85
+ .map { |x| x.map { |y| Fontist::Import::TextHelper.cleanup(y) } }
86
+ .to_h
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "otfinfo/otfinfo_requirement"
2
+ require_relative "text_helper"
3
+
4
+ module Fontist
5
+ module Import
6
+ class OtfParser
7
+ REQUIREMENTS = {
8
+ otfinfo: Fontist::Import::Otfinfo::OtfinfoRequirement.new,
9
+ }.freeze
10
+
11
+ def initialize(path)
12
+ @path = path
13
+ end
14
+
15
+ def call
16
+ text = REQUIREMENTS[:otfinfo].call(@path)
17
+ text.split("\n")
18
+ .select { |x| x.include?(":") }
19
+ .map { |x| x.split(":", 2) }
20
+ .map { |x| x.map { |y| Fontist::Import::TextHelper.cleanup(y) } }
21
+ .to_h
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ module Fontist
2
+ module Import
3
+ class OtfStyle
4
+ def initialize(info, path)
5
+ @info = info
6
+ @path = path
7
+ end
8
+
9
+ def call
10
+ style = { family_name: @info["Preferred family"] || @info["Family"],
11
+ style: @info["Preferred subfamily"] || @info["Subfamily"],
12
+ full_name: @info["Full name"],
13
+ post_script_name: @info["PostScript name"],
14
+ version: version(@info["Version"]),
15
+ description: @info["Description"],
16
+ filename: File.basename(@path),
17
+ copyright: @info["Copyright"] }
18
+
19
+ OpenStruct.new(style)
20
+ end
21
+
22
+ private
23
+
24
+ def version(text)
25
+ Fontist::Import::Google.style_version(text)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require "fontist/import/helpers/system_helper"
2
+
3
+ module Fontist
4
+ module Import
5
+ module Otfinfo
6
+ class OtfinfoRequirement
7
+ def initialize
8
+ otfinfo_path = `which otfinfo`
9
+ if otfinfo_path.empty?
10
+ abort "otfinfo is not available. (Or is PATH not setup properly?)"\
11
+ " You must install otfinfo."\
12
+ " On macOS it can be installed via `brew install lcdf-typetools`."
13
+ end
14
+ end
15
+
16
+ def call(path)
17
+ Helpers::SystemHelper.run("otfinfo --info '#{path}'")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ <%- resource.each do |s| -%>
2
+ {
3
+ family_name: "<%= s.family_name %>",
4
+ style: "<%= s.style %>",
5
+ full_name: "<%= s.full_name %>",
6
+ <%- if s.post_script_name -%>
7
+ post_script_name: "<%= s.post_script_name %>",
8
+ <%- end -%>
9
+ <%- if s.version -%>
10
+ version: "<%= esc(s.version) %>",
11
+ <%- end -%>
12
+ <%- if s.description -%>
13
+ description: "<%= esc(s.description) %>",
14
+ <%- end -%>
15
+ filename: "<%= s.filename %>",
16
+ <%- if s.copyright -%>
17
+ copyright: "<%= esc(s.copyright) %>",
18
+ <%- end -%>
19
+ },
20
+ <%- end -%>
@@ -0,0 +1,45 @@
1
+ require "erb"
2
+ require_relative "template_helper"
3
+ require_relative "text_helper"
4
+ require_relative "otf_parser"
5
+ require_relative "otf_style"
6
+
7
+ module Fontist
8
+ module Import
9
+ class OtfinfoGenerate
10
+ TEMPLATE_PATH = File.expand_path("otfinfo/template.erb", __dir__)
11
+
12
+ def initialize(font)
13
+ @font = font
14
+ end
15
+
16
+ def call
17
+ paths = font_paths(@font)
18
+ puts paths
19
+ styles = generate_styles(paths)
20
+ puts render(styles)
21
+ end
22
+
23
+ private
24
+
25
+ def font_paths(font)
26
+ formula = Fontist::Formula.find(font)
27
+ font_formula = Object.const_get(formula.installer)
28
+ font_formula.fetch_font(nil, confirmation: "yes")
29
+ end
30
+
31
+ def generate_styles(paths)
32
+ paths.map do |path|
33
+ info = OtfParser.new(path).call
34
+ OtfStyle.new(info, path).call
35
+ end
36
+ end
37
+
38
+ def render(styles)
39
+ template = File.read(TEMPLATE_PATH)
40
+ renderer = ERB.new(template, trim_mode: "-")
41
+ renderer.result(Fontist::Import::TemplateHelper.bind(styles))
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,128 @@
1
+ require "find"
2
+ require_relative "extractors"
3
+
4
+ module Fontist
5
+ module Import
6
+ class RecursiveExtraction
7
+ BOTH_FONTS_PATTERN = "**/*.{ttf,otf,ttc}*".freeze
8
+ ARCHIVE_EXTENSIONS = %w[zip msi exe cab].freeze
9
+
10
+ def initialize(archive, subarchive: nil)
11
+ @archive = archive
12
+ @subarchive = subarchive
13
+ @operations = []
14
+ end
15
+
16
+ def extension
17
+ File.extname(filename(@archive)).sub(/^\./, "")
18
+ end
19
+
20
+ def extract(pattern)
21
+ Array.new.tap do |results|
22
+ Find.find(extracted_path) do |path| # rubocop:disable Style/CollectionMethods, Metrics/LineLength
23
+ results << yield(path) if path.match(pattern)
24
+ end
25
+ end
26
+ end
27
+
28
+ def operations
29
+ ensure_extracted
30
+ @operations.size == 1 ? @operations.first : @operations
31
+ end
32
+
33
+ private
34
+
35
+ def filename(file)
36
+ if file.respond_to?(:original_filename)
37
+ file.original_filename
38
+ else
39
+ File.basename(file)
40
+ end
41
+ end
42
+
43
+ def ensure_extracted
44
+ extracted_path
45
+ end
46
+
47
+ def extracted_path
48
+ @extracted_path ||= extract_recursively(@archive)
49
+ end
50
+
51
+ def extract_recursively(archive)
52
+ path = operate_on_archive(archive)
53
+ return path if fonts_exist?(path)
54
+
55
+ next_archive = find_archive(path)
56
+ extract_recursively(next_archive)
57
+ end
58
+
59
+ def operate_on_archive(archive)
60
+ extractor = choose_extractor(archive)
61
+ Fontist.ui.say("Extracting #{archive} with #{extractor.class.name}")
62
+
63
+ save_operation(extractor)
64
+ extractor.extract
65
+ end
66
+
67
+ # rubocop:disable Metrics/MethodLength
68
+ def choose_extractor(archive)
69
+ case filename(archive)
70
+ when /\.msi$/i
71
+ Extractors::OleExtractor.new(archive)
72
+ when /\.cab$/i
73
+ Extractors::CabExtractor.new(archive)
74
+ when /\.exe$/i
75
+ extractor = Extractors::SevenZipExtractor.new(archive)
76
+ extractor.try ? extractor : Extractors::CabExtractor.new(archive)
77
+ else
78
+ Extractors::ZipExtractor.new(archive)
79
+ end
80
+ end
81
+ # rubocop:enable Metrics/MethodLength
82
+
83
+ def save_operation(extractor)
84
+ @operations << { format: extractor.format }
85
+ end
86
+
87
+ def fonts_exist?(path)
88
+ fonts = Dir.glob(File.join(path, BOTH_FONTS_PATTERN))
89
+ !fonts.empty?
90
+ end
91
+
92
+ def find_archive(path)
93
+ paths = Dir.children(path).map { |file| File.join(path, file) }
94
+ by_subarchive(paths) || by_size(paths)
95
+ end
96
+
97
+ def by_subarchive(paths)
98
+ return unless @subarchive
99
+
100
+ path_found = paths.detect do |path|
101
+ @subarchive == File.basename(path)
102
+ end
103
+
104
+ return unless path_found
105
+
106
+ save_operation_subarchive(path_found)
107
+
108
+ path_found
109
+ end
110
+
111
+ def save_operation_subarchive(path)
112
+ @operations.last[:options] ||= {}
113
+ @operations.last[:options][:subarchive] = File.basename(path)
114
+ end
115
+
116
+ def by_size(paths)
117
+ paths.max_by do |path|
118
+ [file_type(path), File.size(path)]
119
+ end
120
+ end
121
+
122
+ def file_type(file_path)
123
+ extension = File.extname(file_path).delete(".")
124
+ ARCHIVE_EXTENSIONS.include?(extension) ? 1 : 0
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,19 @@
1
+ module Fontist
2
+ module Import
3
+ module TemplateHelper
4
+ class << self
5
+ def bind(resource, name = nil)
6
+ b = binding
7
+ b.local_variable_set(name, resource) if name
8
+ b
9
+ end
10
+
11
+ def escape_double_quotes(text)
12
+ text.gsub('"', '\"')
13
+ end
14
+
15
+ alias esc escape_double_quotes
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ module Fontist
2
+ module Import
3
+ module TextHelper
4
+ class << self
5
+ def cleanup(text)
6
+ return unless text
7
+
8
+ text.gsub("\r\n", "\n")
9
+ .gsub("\r", "\n")
10
+ .strip
11
+ .lines
12
+ .map(&:rstrip)
13
+ .drop_while(&:empty?)
14
+ .join("\n")
15
+ end
16
+
17
+ def longest_common_prefix(strs)
18
+ return if strs.empty?
19
+
20
+ min, max = strs.minmax
21
+ idx = min.size.times { |i| break i if min[i] != max[i] }
22
+ prefix = min[0...idx].strip
23
+ return if prefix.empty?
24
+
25
+ prefix
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end