fontist 1.3.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) 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 +15 -1
  16. data/lib/fontist.rb +19 -13
  17. data/lib/fontist/cli.rb +168 -0
  18. data/lib/fontist/errors.rb +6 -0
  19. data/lib/fontist/font.rb +145 -7
  20. data/lib/fontist/font_formula.rb +42 -7
  21. data/lib/fontist/fontist_font.rb +70 -0
  22. data/lib/fontist/formula.rb +31 -9
  23. data/lib/fontist/formula_template.rb +122 -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 +59 -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 +52 -0
  35. data/lib/fontist/import/files/file_requirement.rb +17 -0
  36. data/lib/fontist/import/files/font_detector.rb +48 -0
  37. data/lib/fontist/import/formula_builder.rb +166 -0
  38. data/lib/fontist/import/formula_serializer.rb +133 -0
  39. data/lib/fontist/import/google.rb +16 -0
  40. data/lib/fontist/import/google/fonts_public.md +10 -0
  41. data/lib/fontist/import/google/fonts_public.pb.rb +71 -0
  42. data/lib/fontist/import/google/fonts_public.proto +46 -0
  43. data/lib/fontist/import/google/new_fonts_fetcher.rb +121 -0
  44. data/lib/fontist/import/google/skiplist.yml +8 -0
  45. data/lib/fontist/import/google_check.rb +34 -0
  46. data/lib/fontist/import/google_import.rb +180 -0
  47. data/lib/fontist/import/helpers/hash_helper.rb +13 -0
  48. data/lib/fontist/import/helpers/system_helper.rb +23 -0
  49. data/lib/fontist/import/otf/font_file.rb +105 -0
  50. data/lib/fontist/import/otf_parser.rb +25 -0
  51. data/lib/fontist/import/otf_style.rb +29 -0
  52. data/lib/fontist/import/otfinfo/otfinfo_requirement.rb +22 -0
  53. data/lib/fontist/import/otfinfo/template.erb +20 -0
  54. data/lib/fontist/import/otfinfo_generate.rb +45 -0
  55. data/lib/fontist/import/recursive_extraction.rb +185 -0
  56. data/lib/fontist/import/template_helper.rb +19 -0
  57. data/lib/fontist/import/text_helper.rb +30 -0
  58. data/lib/fontist/manifest.rb +2 -0
  59. data/lib/fontist/manifest/install.rb +32 -0
  60. data/lib/fontist/manifest/locations.rb +60 -0
  61. data/lib/fontist/system_font.rb +66 -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 +66 -9
  65. data/lib/fontist/utils/dsl.rb +8 -0
  66. data/lib/fontist/utils/dsl/collection_font.rb +36 -0
  67. data/lib/fontist/utils/dsl/font.rb +38 -0
  68. data/lib/fontist/utils/exe_extractor.rb +25 -22
  69. data/lib/fontist/utils/msi_extractor.rb +31 -0
  70. data/lib/fontist/utils/seven_zip_extractor.rb +41 -0
  71. data/lib/fontist/utils/system.rb +23 -0
  72. data/lib/fontist/utils/ui.rb +12 -0
  73. data/lib/fontist/utils/zip_extractor.rb +22 -8
  74. data/lib/fontist/version.rb +1 -1
  75. metadata +173 -49
  76. data/lib/fontist/formulas/andale_font.rb +0 -80
  77. data/lib/fontist/formulas/arial_black_font.rb +0 -79
  78. data/lib/fontist/formulas/cleartype_fonts.rb +0 -227
  79. data/lib/fontist/formulas/comic_font.rb +0 -78
  80. data/lib/fontist/formulas/courier_font.rb +0 -81
  81. data/lib/fontist/formulas/euphemia_font.rb +0 -85
  82. data/lib/fontist/formulas/georgia_font.rb +0 -80
  83. data/lib/fontist/formulas/impact_font.rb +0 -78
  84. data/lib/fontist/formulas/montserrat_font.rb +0 -132
  85. data/lib/fontist/formulas/ms_truetype_fonts.rb +0 -125
  86. data/lib/fontist/formulas/open_sans_fonts.rb +0 -263
  87. data/lib/fontist/formulas/overpass_font.rb +0 -73
  88. data/lib/fontist/formulas/source_fonts.rb +0 -109
  89. data/lib/fontist/formulas/stix_fonts.rb +0 -108
  90. data/lib/fontist/formulas/tahoma_font.rb +0 -147
  91. data/lib/fontist/formulas/webding_font.rb +0 -78
  92. data/spec/fontist/font_formula_spec.rb +0 -67
  93. data/spec/fontist/font_spec.rb +0 -113
  94. data/spec/fontist/formula_spec.rb +0 -67
  95. data/spec/fontist/formulas/andale_font_spec.rb +0 -29
  96. data/spec/fontist/formulas/arial_black_font_spec.rb +0 -29
  97. data/spec/fontist/formulas/cleartype_fonts_spec.rb +0 -38
  98. data/spec/fontist/formulas/comic_font_spec.rb +0 -29
  99. data/spec/fontist/formulas/courier_font_spec.rb +0 -29
  100. data/spec/fontist/formulas/euphemia_font_spec.rb +0 -29
  101. data/spec/fontist/formulas/georgia_font_spec.rb +0 -29
  102. data/spec/fontist/formulas/impact_font_spec.rb +0 -29
  103. data/spec/fontist/formulas/montserrat_font_spec.rb +0 -29
  104. data/spec/fontist/formulas/ms_truetype_fonts_spec.rb +0 -29
  105. data/spec/fontist/formulas/open_sans_fonts_spec.rb +0 -29
  106. data/spec/fontist/formulas/overpass_font_spec.rb +0 -29
  107. data/spec/fontist/formulas/source_fonts_spec.rb +0 -31
  108. data/spec/fontist/formulas/stix_fonts_spec.rb +0 -29
  109. data/spec/fontist/formulas/tahoma_font_spec.rb +0 -29
  110. data/spec/fontist/formulas/webding_font_spec.rb +0 -29
  111. data/spec/fontist/registry_spec.rb +0 -47
  112. data/spec/fontist/system_font_spec.rb +0 -44
  113. data/spec/fontist/utils/downloader_spec.rb +0 -35
  114. data/spec/fontist_spec.rb +0 -5
  115. data/spec/spec_helper.rb +0 -22
  116. data/spec/support/fontist_helper.rb +0 -10
@@ -0,0 +1,180 @@
1
+ require "erb"
2
+ require_relative "google"
3
+ require_relative "google/new_fonts_fetcher"
4
+ require_relative "google/fonts_public.pb"
5
+ require_relative "template_helper"
6
+ require_relative "text_helper"
7
+ require_relative "otf_parser"
8
+ require_relative "otf_style"
9
+
10
+ module Fontist
11
+ module Import
12
+ class GoogleImport
13
+ def call
14
+ fonts = new_fonts
15
+ create_formulas(fonts)
16
+ end
17
+
18
+ private
19
+
20
+ def new_fonts
21
+ Fontist::Import::Google::NewFontsFetcher.new(logging: true).call
22
+ end
23
+
24
+ def create_formulas(fonts)
25
+ puts "Creating formulas..."
26
+ fonts.each do |path|
27
+ create_formula(path)
28
+ end
29
+ end
30
+
31
+ def create_formula(path)
32
+ puts path
33
+ metadata = fetch_metadata(path)
34
+ font = build_font(metadata, path)
35
+ save_formula(font)
36
+ end
37
+
38
+ def fetch_metadata(path)
39
+ protobuf = File.read(File.join(path, "METADATA.pb"))
40
+ ::Google::Fonts::FamilyProto.parse_from_text(protobuf)
41
+ end
42
+
43
+ def build_font(metadata, path)
44
+ h = from_metadata(metadata)
45
+ .merge(from_otfinfo(path))
46
+ .merge(styles: styles_from_otfinfo(path, metadata.fonts))
47
+ .merge(from_license(path))
48
+
49
+ OpenStruct.new(h)
50
+ end
51
+
52
+ def from_metadata(metadata)
53
+ copyright = metadata.fonts.first.copyright
54
+
55
+ Hash.new.tap do |h|
56
+ h[:fullname] = metadata.name
57
+ h[:cleanname] = metadata.name.gsub(/ /, "")
58
+ h[:sha256] = sha256(metadata.name) unless variable_style?(metadata)
59
+ h[:copyright] = Fontist::Import::TextHelper.cleanup(copyright)
60
+ end
61
+ end
62
+
63
+ def variable_style?(metadata)
64
+ metadata.fonts.any? do |s|
65
+ s.filename.match?(/\[(.+,)?wght\]/)
66
+ end
67
+ end
68
+
69
+ def sha256(name)
70
+ file = Down.download("https://fonts.google.com/download?family=#{name}",
71
+ open_timeout: 10,
72
+ read_timeout: 10)
73
+
74
+ Digest::SHA256.file(file).to_s
75
+ end
76
+
77
+ def from_license(path)
78
+ file = Dir.glob(File.join(path, "{OFL.txt,UFL.txt,LICENSE.txt}")).first
79
+ print "warn, no license, " unless file
80
+ return { license: "" } unless file
81
+
82
+ { license: cleanup_text(File.read(file)) }
83
+ end
84
+
85
+ def cleanup_text(text)
86
+ text.rstrip
87
+ .gsub("\r\n", "\n")
88
+ .lines
89
+ .map(&:rstrip)
90
+ .drop_while(&:empty?)
91
+ .join("\n")
92
+ end
93
+
94
+ def from_otfinfo(path)
95
+ font_file = Dir.glob(File.join(path, "*.ttf")).first
96
+ otf = OtfParser.new(font_file).call
97
+
98
+ { homepage: otf["Vendor URL"],
99
+ license_url: otf["License URL"] }
100
+ end
101
+
102
+ def styles_from_otfinfo(path, fonts)
103
+ fonts.map do |f|
104
+ file_path = File.join(path, f.filename)
105
+ info = OtfParser.new(file_path).call
106
+ OtfStyle.new(info, file_path).call
107
+ end
108
+ end
109
+
110
+ def save_formula(font)
111
+ hash = formula_hash(font)
112
+ path = formula_path(font.fullname)
113
+ save_to_path(hash, path)
114
+ end
115
+
116
+ def formula_hash(font)
117
+ stringify_keys(name: font.cleanname.sub(/\S/, &:upcase),
118
+ description: font.fullname,
119
+ homepage: font.homepage,
120
+ resources: formula_resource(font),
121
+ fonts: [yaml_font(font)],
122
+ extract: { format: :zip },
123
+ copyright: font.copyright,
124
+ license_url: font.license_url,
125
+ open_license: font.license)
126
+ end
127
+
128
+ def stringify_keys(hash)
129
+ JSON.parse(hash.to_json)
130
+ end
131
+
132
+ def formula_resource(font)
133
+ encoded_name = ERB::Util.url_encode(font.fullname)
134
+ url = "https://fonts.google.com/download?family=#{encoded_name}"
135
+
136
+ options = {}
137
+ options[:urls] = [url]
138
+ options[:sha256] = font.sha256 if font.sha256
139
+
140
+ { "#{font.cleanname}.zip" => options }
141
+ end
142
+
143
+ def yaml_font(font)
144
+ { name: font.fullname,
145
+ styles: yaml_styles(font.styles) }
146
+ end
147
+
148
+ def yaml_styles(styles)
149
+ styles.map do |s|
150
+ yaml_style(s)
151
+ end
152
+ end
153
+
154
+ def yaml_style(style)
155
+ Hash.new.tap do |h|
156
+ h.merge!(family_name: style.family_name,
157
+ type: style.style,
158
+ full_name: style.full_name)
159
+ h.merge!(style.to_h.slice(:post_script_name,
160
+ :version,
161
+ :description,
162
+ :copyright).compact)
163
+ h.merge!(font: fix_variable_filename(style.filename))
164
+ end
165
+ end
166
+
167
+ def fix_variable_filename(filename)
168
+ filename.sub("[wght]", "-VariableFont_wght")
169
+ end
170
+
171
+ def formula_path(name)
172
+ Fontist::Import::Google.formula_path(name)
173
+ end
174
+
175
+ def save_to_path(hash, path)
176
+ File.write(path, YAML.dump(hash))
177
+ end
178
+ end
179
+ end
180
+ end
@@ -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,105 @@
1
+ require_relative "../otfinfo/otfinfo_requirement"
2
+ require_relative "../text_helper"
3
+ require_relative "../files/font_detector"
4
+
5
+ module Fontist
6
+ module Import
7
+ module Otf
8
+ class FontFile
9
+ REQUIREMENTS = {
10
+ otfinfo: Otfinfo::OtfinfoRequirement.new,
11
+ }.freeze
12
+
13
+ STYLE_ATTRIBUTES = %i[family_name type full_name post_script_name
14
+ version description copyright font
15
+ source_font].freeze
16
+
17
+ COLLECTION_ATTRIBUTES = STYLE_ATTRIBUTES.reject do |a|
18
+ %i[font source_font].include?(a)
19
+ end
20
+
21
+ attr_reader :path
22
+
23
+ def initialize(path)
24
+ @path = path
25
+ @info = read
26
+ @extension = detect_extension
27
+ end
28
+
29
+ def to_style
30
+ STYLE_ATTRIBUTES.map { |name| [name, send(name)] }.to_h.compact
31
+ end
32
+
33
+ def to_collection_style
34
+ COLLECTION_ATTRIBUTES.map { |name| [name, send(name)] }.to_h.compact
35
+ end
36
+
37
+ def family_name
38
+ info["Preferred family"] || info["Family"]
39
+ end
40
+
41
+ def type
42
+ info["Preferred subfamily"] || info["Subfamily"]
43
+ end
44
+
45
+ def full_name
46
+ info["Full name"]
47
+ end
48
+
49
+ def post_script_name
50
+ info["PostScript name"]
51
+ end
52
+
53
+ def version
54
+ return unless info["Version"]
55
+
56
+ info["Version"].gsub("Version ", "")
57
+ end
58
+
59
+ def description
60
+ info["Description"]
61
+ end
62
+
63
+ def font
64
+ File.basename(@path, ".*") + "." + @extension
65
+ end
66
+
67
+ def source_font
68
+ File.basename(@path) unless font == File.basename(@path)
69
+ end
70
+
71
+ def copyright
72
+ info["Copyright"]
73
+ end
74
+
75
+ def homepage
76
+ info["Vendor URL"]
77
+ end
78
+
79
+ def license_url
80
+ info["License URL"]
81
+ end
82
+
83
+ private
84
+
85
+ attr_reader :info
86
+
87
+ def read
88
+ text = REQUIREMENTS[:otfinfo].call(@path)
89
+
90
+ text
91
+ .encode("UTF-8", invalid: :replace, replace: "")
92
+ .split("\n")
93
+ .select { |x| x.include?(":") }
94
+ .map { |x| x.split(":", 2) }
95
+ .map { |x| x.map { |y| Fontist::Import::TextHelper.cleanup(y) } }
96
+ .to_h
97
+ end
98
+
99
+ def detect_extension
100
+ Files::FontDetector.standard_extension(@path)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ 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