fontist 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) 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 +14 -1
  17. data/lib/fontist.rb +18 -13
  18. data/lib/fontist/cli.rb +118 -0
  19. data/lib/fontist/errors.rb +3 -0
  20. data/lib/fontist/font.rb +117 -2
  21. data/lib/fontist/font_formula.rb +7 -3
  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/system_font.rb +12 -18
  58. data/lib/fontist/utils.rb +4 -0
  59. data/lib/fontist/utils/cache.rb +69 -0
  60. data/lib/fontist/utils/downloader.rb +15 -2
  61. data/lib/fontist/utils/dsl.rb +4 -0
  62. data/lib/fontist/utils/dsl/font.rb +37 -0
  63. data/lib/fontist/utils/exe_extractor.rb +12 -19
  64. data/lib/fontist/utils/msi_extractor.rb +31 -0
  65. data/lib/fontist/utils/seven_zip_extractor.rb +41 -0
  66. data/lib/fontist/utils/system.rb +23 -0
  67. data/lib/fontist/utils/ui.rb +8 -0
  68. data/lib/fontist/utils/zip_extractor.rb +9 -4
  69. data/lib/fontist/version.rb +1 -1
  70. metadata +154 -49
  71. data/lib/fontist/formulas/andale_font.rb +0 -80
  72. data/lib/fontist/formulas/arial_black_font.rb +0 -79
  73. data/lib/fontist/formulas/cleartype_fonts.rb +0 -227
  74. data/lib/fontist/formulas/comic_font.rb +0 -78
  75. data/lib/fontist/formulas/courier_font.rb +0 -81
  76. data/lib/fontist/formulas/euphemia_font.rb +0 -85
  77. data/lib/fontist/formulas/georgia_font.rb +0 -80
  78. data/lib/fontist/formulas/impact_font.rb +0 -78
  79. data/lib/fontist/formulas/montserrat_font.rb +0 -132
  80. data/lib/fontist/formulas/ms_truetype_fonts.rb +0 -125
  81. data/lib/fontist/formulas/open_sans_fonts.rb +0 -263
  82. data/lib/fontist/formulas/overpass_font.rb +0 -73
  83. data/lib/fontist/formulas/source_fonts.rb +0 -109
  84. data/lib/fontist/formulas/stix_fonts.rb +0 -108
  85. data/lib/fontist/formulas/tahoma_font.rb +0 -147
  86. data/lib/fontist/formulas/webding_font.rb +0 -78
  87. data/spec/fontist/font_formula_spec.rb +0 -67
  88. data/spec/fontist/font_spec.rb +0 -113
  89. data/spec/fontist/formula_spec.rb +0 -67
  90. data/spec/fontist/formulas/andale_font_spec.rb +0 -29
  91. data/spec/fontist/formulas/arial_black_font_spec.rb +0 -29
  92. data/spec/fontist/formulas/cleartype_fonts_spec.rb +0 -38
  93. data/spec/fontist/formulas/comic_font_spec.rb +0 -29
  94. data/spec/fontist/formulas/courier_font_spec.rb +0 -29
  95. data/spec/fontist/formulas/euphemia_font_spec.rb +0 -29
  96. data/spec/fontist/formulas/georgia_font_spec.rb +0 -29
  97. data/spec/fontist/formulas/impact_font_spec.rb +0 -29
  98. data/spec/fontist/formulas/montserrat_font_spec.rb +0 -29
  99. data/spec/fontist/formulas/ms_truetype_fonts_spec.rb +0 -29
  100. data/spec/fontist/formulas/open_sans_fonts_spec.rb +0 -29
  101. data/spec/fontist/formulas/overpass_font_spec.rb +0 -29
  102. data/spec/fontist/formulas/source_fonts_spec.rb +0 -31
  103. data/spec/fontist/formulas/stix_fonts_spec.rb +0 -29
  104. data/spec/fontist/formulas/tahoma_font_spec.rb +0 -29
  105. data/spec/fontist/formulas/webding_font_spec.rb +0 -29
  106. data/spec/fontist/registry_spec.rb +0 -47
  107. data/spec/fontist/system_font_spec.rb +0 -44
  108. data/spec/fontist/utils/downloader_spec.rb +0 -35
  109. data/spec/fontist_spec.rb +0 -5
  110. data/spec/spec_helper.rb +0 -22
  111. 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,20 @@
1
+ module Fontist
2
+ module Import
3
+ module Helpers
4
+ module SystemHelper
5
+ class << self
6
+ def run(command)
7
+ puts "Run `#{command}`" unless ENV.fetch("TEST_ENV", "") === "CI"
8
+ result = `#{command}`
9
+ unless $CHILD_STATUS.to_i.zero?
10
+ raise Errors::BinaryCallError,
11
+ "Failed to run #{command}, status: #{$CHILD_STATUS}"
12
+ end
13
+
14
+ result
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,89 @@
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.split("\n")
81
+ .select { |x| x.include?(":") }
82
+ .map { |x| x.split(":", 2) }
83
+ .map { |x| x.map { |y| Fontist::Import::TextHelper.cleanup(y) } }
84
+ .to_h
85
+ end
86
+ end
87
+ end
88
+ end
89
+ 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,101 @@
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)
11
+ @archive = archive
12
+ @operations = []
13
+ end
14
+
15
+ def extension
16
+ File.extname(filename(@archive)).sub(/^\./, "")
17
+ end
18
+
19
+ def extract(pattern)
20
+ Array.new.tap do |results|
21
+ Find.find(extracted_path) do |path| # rubocop:disable Style/CollectionMethods, Metrics/LineLength
22
+ results << yield(path) if path.match(pattern)
23
+ end
24
+ end
25
+ end
26
+
27
+ def operations
28
+ ensure_extracted
29
+ @operations.size == 1 ? @operations.first : @operations
30
+ end
31
+
32
+ private
33
+
34
+ def filename(file)
35
+ if file.respond_to?(:original_filename)
36
+ file.original_filename
37
+ else
38
+ File.basename(file)
39
+ end
40
+ end
41
+
42
+ def ensure_extracted
43
+ extracted_path
44
+ end
45
+
46
+ def extracted_path
47
+ @extracted_path ||= extract_recursively(@archive)
48
+ end
49
+
50
+ def extract_recursively(archive)
51
+ path = operate_on_archive(archive)
52
+ return path if fonts_exist?(path)
53
+
54
+ next_archive = find_archive(path)
55
+ extract_recursively(next_archive)
56
+ end
57
+
58
+ def operate_on_archive(archive)
59
+ extractor = choose_extractor(archive)
60
+ save_operation(extractor)
61
+ extractor.extract
62
+ end
63
+
64
+ # rubocop:disable Metrics/MethodLength
65
+ def choose_extractor(archive)
66
+ case filename(archive)
67
+ when /\.msi$/i
68
+ Extractors::OleExtractor.new(archive)
69
+ when /\.cab$/i
70
+ Extractors::CabExtractor.new(archive)
71
+ when /\.exe$/i
72
+ extractor = Extractors::SevenZipExtractor.new(archive)
73
+ extractor.try ? extractor : Extractors::CabExtractor.new(archive)
74
+ else
75
+ Extractors::ZipExtractor.new(archive)
76
+ end
77
+ end
78
+ # rubocop:enable Metrics/MethodLength
79
+
80
+ def save_operation(extractor)
81
+ @operations << { format: extractor.format }
82
+ end
83
+
84
+ def fonts_exist?(path)
85
+ fonts = Dir.glob(File.join(path, BOTH_FONTS_PATTERN))
86
+ !fonts.empty?
87
+ end
88
+
89
+ def find_archive(path)
90
+ Dir.children(path)
91
+ .map { |file_name| File.join(path, file_name) }
92
+ .max_by { |file_path| [file_type(file_path), File.size(file_path)] }
93
+ end
94
+
95
+ def file_type(file_path)
96
+ extension = File.extname(file_path).delete(".")
97
+ ARCHIVE_EXTENSIONS.include?(extension) ? 1 : 0
98
+ end
99
+ end
100
+ end
101
+ 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
@@ -10,7 +10,7 @@ module Fontist
10
10
  end
11
11
 
12
12
  def find
13
- paths = font_paths.grep(/#{font}/i)
13
+ paths = grep_font_paths(font)
14
14
  paths = lookup_using_font_name || [] if paths.empty?
15
15
 
16
16
  paths.empty? ? nil : paths
@@ -24,11 +24,19 @@ module Fontist
24
24
  @normalize_default_paths ||= default_sources["paths"].map do |path|
25
25
  require "etc"
26
26
  passwd = Etc.getpwuid
27
+ username = passwd ? passwd.name : Etc.getlogin
27
28
 
28
- passwd ? path.gsub("{username}", passwd.name) : path
29
+ username ? path.gsub("{username}", username) : path
29
30
  end
30
31
  end
31
32
 
33
+ def grep_font_paths(font)
34
+ paths = font_paths.map { |path| [File.basename(path), path] }.to_h
35
+ files = paths.keys
36
+ matched = files.grep(/#{font}/i)
37
+ paths.values_at(*matched).compact
38
+ end
39
+
32
40
  def font_paths
33
41
  Dir.glob((
34
42
  user_sources +
@@ -48,21 +56,7 @@ module Fontist
48
56
 
49
57
 
50
58
  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
- )
59
+ Fontist::Utils::System.user_os
66
60
  end
67
61
 
68
62
  def map_name_to_valid_font_names
@@ -71,7 +65,7 @@ module Fontist
71
65
  end
72
66
 
73
67
  def system_path_file
74
- File.open(Fontist.lib_path.join("fontist", "system.yml"))
68
+ File.open(Fontist.system_file_path)
75
69
  end
76
70
 
77
71
  def default_sources