fontist 0.4.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/macosx.yml +1 -1
  3. data/.github/workflows/ubuntu.yml +1 -1
  4. data/.gitignore +4 -2
  5. data/README.md +87 -25
  6. data/fontist.gemspec +1 -0
  7. data/lib/fontist.rb +30 -13
  8. data/lib/fontist/errors.rb +3 -1
  9. data/lib/fontist/font.rb +113 -0
  10. data/lib/fontist/font_formula.rb +123 -0
  11. data/lib/fontist/formula.rb +90 -0
  12. data/lib/fontist/formulas.rb +10 -5
  13. data/lib/fontist/formulas/andale_font.rb +80 -0
  14. data/lib/fontist/formulas/arial_black_font.rb +79 -0
  15. data/lib/fontist/formulas/cleartype_fonts.rb +227 -0
  16. data/lib/fontist/formulas/comic_font.rb +78 -0
  17. data/lib/fontist/formulas/courier_font.rb +68 -12
  18. data/lib/fontist/formulas/euphemia_font.rb +85 -0
  19. data/lib/fontist/formulas/georgia_font.rb +80 -0
  20. data/lib/fontist/formulas/impact_font.rb +78 -0
  21. data/lib/fontist/formulas/montserrat_font.rb +132 -0
  22. data/lib/fontist/formulas/ms_truetype_fonts.rb +125 -0
  23. data/lib/fontist/formulas/open_sans_fonts.rb +263 -0
  24. data/lib/fontist/formulas/overpass_font.rb +73 -0
  25. data/lib/fontist/formulas/source_fonts.rb +109 -0
  26. data/lib/fontist/formulas/stix_fonts.rb +108 -0
  27. data/lib/fontist/formulas/tahoma_font.rb +147 -0
  28. data/lib/fontist/formulas/webding_font.rb +78 -0
  29. data/lib/fontist/registry.rb +43 -0
  30. data/lib/fontist/{data/source.yml → system.yml} +5 -8
  31. data/lib/fontist/system_font.rb +19 -14
  32. data/lib/fontist/utils.rb +10 -0
  33. data/lib/fontist/utils/downloader.rb +79 -0
  34. data/lib/fontist/utils/dsl.rb +77 -0
  35. data/lib/fontist/utils/exe_extractor.rb +72 -0
  36. data/lib/fontist/utils/ui.rb +15 -0
  37. data/lib/fontist/utils/zip_extractor.rb +38 -0
  38. data/lib/fontist/version.rb +1 -1
  39. data/spec/fontist/font_formula_spec.rb +67 -0
  40. data/spec/fontist/font_spec.rb +113 -0
  41. data/spec/fontist/formula_spec.rb +67 -0
  42. data/spec/fontist/formulas/andale_font_spec.rb +29 -0
  43. data/spec/fontist/formulas/arial_black_font_spec.rb +29 -0
  44. data/spec/fontist/formulas/cleartype_fonts_spec.rb +38 -0
  45. data/spec/fontist/formulas/comic_font_spec.rb +29 -0
  46. data/spec/fontist/formulas/courier_font_spec.rb +18 -19
  47. data/spec/fontist/formulas/euphemia_font_spec.rb +29 -0
  48. data/spec/fontist/formulas/georgia_font_spec.rb +29 -0
  49. data/spec/fontist/formulas/impact_font_spec.rb +29 -0
  50. data/spec/fontist/formulas/montserrat_font_spec.rb +29 -0
  51. data/spec/fontist/formulas/ms_truetype_fonts_spec.rb +29 -0
  52. data/spec/fontist/formulas/open_sans_fonts_spec.rb +29 -0
  53. data/spec/fontist/formulas/overpass_font_spec.rb +29 -0
  54. data/spec/fontist/formulas/source_fonts_spec.rb +31 -0
  55. data/spec/fontist/formulas/stix_fonts_spec.rb +29 -0
  56. data/spec/fontist/formulas/tahoma_font_spec.rb +29 -0
  57. data/spec/fontist/formulas/webding_font_spec.rb +29 -0
  58. data/spec/fontist/registry_spec.rb +47 -0
  59. data/spec/fontist/system_font_spec.rb +12 -7
  60. data/spec/fontist/{downloader_spec.rb → utils/downloader_spec.rb} +6 -5
  61. data/spec/spec_helper.rb +4 -2
  62. data/spec/support/fontist_helper.rb +4 -2
  63. metadata +62 -27
  64. data/lib/fontist/data/formulas/courier.yml +0 -26
  65. data/lib/fontist/data/formulas/ms_system.yml +0 -68
  66. data/lib/fontist/data/formulas/ms_vista.yml +0 -118
  67. data/lib/fontist/data/formulas/source_font.yml +0 -163
  68. data/lib/fontist/downloader.rb +0 -70
  69. data/lib/fontist/finder.rb +0 -45
  70. data/lib/fontist/formula_finder.rb +0 -94
  71. data/lib/fontist/formulas/base.rb +0 -72
  72. data/lib/fontist/formulas/helpers/exe_extractor.rb +0 -45
  73. data/lib/fontist/formulas/helpers/zip_extractor.rb +0 -36
  74. data/lib/fontist/formulas/ms_system.rb +0 -29
  75. data/lib/fontist/formulas/ms_vista.rb +0 -42
  76. data/lib/fontist/formulas/source_font.rb +0 -25
  77. data/lib/fontist/installer.rb +0 -42
  78. data/lib/fontist/source.rb +0 -58
  79. data/spec/fontist/finder_spec.rb +0 -41
  80. data/spec/fontist/formula_finder_spec.rb +0 -54
  81. data/spec/fontist/formulas/ms_system_spec.rb +0 -31
  82. data/spec/fontist/formulas/ms_vista_spec.rb +0 -27
  83. data/spec/fontist/formulas/source_font_spec.rb +0 -18
  84. data/spec/fontist/installer_spec.rb +0 -48
  85. data/spec/fontist/source_spec.rb +0 -24
@@ -1,45 +0,0 @@
1
- module Fontist
2
- class Finder
3
- def initialize(name)
4
- @name = name
5
- end
6
-
7
- def self.find(name)
8
- new(name).find
9
- end
10
-
11
- def find
12
- find_system_font || downloadable_font || raise_invalid_error
13
- end
14
-
15
- private
16
-
17
- attr_reader :name
18
-
19
- def find_system_font
20
- Fontist::SystemFont.find(name)
21
- end
22
-
23
- def remote_source
24
- Fontist::FormulaFinder.find(name)
25
- end
26
-
27
- def downloadable_font
28
- unless remote_source.nil?
29
- raise(
30
- Fontist::Errors::MissingFontError,
31
- "Fonts are missing, please run" \
32
- "Fontist::Installer.download(name, confirmation: 'yes') to " \
33
- "download these fonts"
34
- )
35
- end
36
- end
37
-
38
- def raise_invalid_error
39
- raise(
40
- Fontist::Errors::NonSupportedFontError,
41
- "Could not find the #{name} font in any of the supported downlodable"
42
- )
43
- end
44
- end
45
- end
@@ -1,94 +0,0 @@
1
- module Fontist
2
- class FormulaFinder
3
- def initialize(font_name)
4
- @font_name = font_name
5
- end
6
-
7
- def self.find(font_name)
8
- new(font_name).find
9
- end
10
-
11
- def self.find_fonts(name)
12
- new(name).find_fonts
13
- end
14
-
15
- def find
16
- formulas = find_formula
17
- build_formulas_array(formulas)
18
- end
19
-
20
- def find_fonts
21
- matched_fonts = find_formula.map do |key, _value|
22
- formulas[key].fonts.select do |font|
23
- font.name == font_name
24
- end
25
- end
26
-
27
- matched_fonts.empty? ? nil : matched_fonts.flatten
28
- end
29
-
30
- private
31
-
32
- attr_reader :font_name
33
-
34
- def build_formulas_array(formulas)
35
- unless formulas.empty?
36
- Array.new.tap do |formula_array|
37
- formulas.each do |key|
38
- formula_array.push(
39
- key: key.to_s,
40
- installer: formula_installers[key]
41
- )
42
- end
43
- end
44
- end
45
- end
46
-
47
- def find_formula
48
- find_by_font_name || find_by_font || []
49
- end
50
-
51
- def formulas
52
- @formulas ||= Fontist::Source.formulas.to_h
53
- end
54
-
55
- def formula_installers
56
- {
57
- msvista: Fontist::Formulas::MsVista,
58
- ms_system: Fontist::Formulas::MsSystem,
59
- courier: Fontist::Formulas::CourierFont,
60
- source_front: Fontist::Formulas::SourceFont,
61
- }
62
- end
63
-
64
- def find_by_font_name
65
- formula_names = formulas.select do |key, value|
66
- !value.fonts.map(&:name).grep(/#{font_name}/i).empty?
67
- end.keys
68
-
69
- formula_names.empty? ? nil : formula_names
70
- end
71
-
72
- # Note
73
- #
74
- # These interface recursively look into every single font styles,
75
- # so ideally try to avoid using it when possible, and that's why
76
- # we've added it as last option in formula finder.
77
- #
78
- def find_by_font
79
- formula_names = formulas.select do |key, value|
80
- match_in_font_styles?(value.fonts)
81
- end.keys
82
-
83
- formula_names.empty? ? nil : formula_names
84
- end
85
-
86
- def match_in_font_styles?(fonts)
87
- styles = fonts.select do |font|
88
- !font.styles.map(&:font).grep(/#{font_name}/i).empty?
89
- end
90
-
91
- styles.empty? ? false : true
92
- end
93
- end
94
- end
@@ -1,72 +0,0 @@
1
- require "fontist/downloader"
2
- require "fontist/formulas/helpers/exe_extractor"
3
- require "fontist/formulas/helpers/zip_extractor"
4
-
5
- module Fontist
6
- module Formulas
7
- class Base
8
- def initialize(font_name, confirmation:, **options)
9
- @matched_fonts = []
10
- @font_name = font_name
11
- @confirmation = confirmation || "no"
12
- @force_download = options.fetch(:force_download, false)
13
- @fonts_path = options.fetch(:fonts_path, Fontist.fonts_path)
14
-
15
- check_user_license_agreement
16
- end
17
-
18
- def self.fetch_font(font_name, confirmation: nil, **options)
19
- new(font_name, options.merge(confirmation: confirmation)).fetch
20
- end
21
-
22
- def fetch
23
- extract_fonts(map_names_to_fonts(font_name))
24
-
25
- matched_fonts_uniq = matched_fonts.flatten.uniq
26
- matched_fonts_uniq.empty? ? nil : matched_fonts_uniq
27
- end
28
-
29
- private
30
-
31
- attr_reader(
32
- :font_name,
33
- :fonts_path,
34
- :confirmation,
35
- :matched_fonts,
36
- :force_download,
37
- )
38
-
39
- def check_user_license_agreement
40
- end
41
-
42
- def resources(name, &block)
43
- source = formulas[name]
44
- block_given? ? yield(source) : source
45
- end
46
-
47
- def formulas
48
- @formulas ||= Fontist::Source.formulas
49
- end
50
-
51
- def match_fonts(fonts_dir, font_name)
52
- font = fonts_dir.grep(/#{font_name}/i)
53
- @matched_fonts.push(font) if font
54
-
55
- font
56
- end
57
-
58
- def map_names_to_fonts(font_name)
59
- fonts = FormulaFinder.find_fonts(font_name)
60
- fonts = fonts.map { |font| font.styles.map(&:font) }.flatten if fonts
61
-
62
- fonts || []
63
- end
64
-
65
- def download_file(source)
66
- Fontist::Downloader.download(
67
- source.urls.first, sha: source.sha, file_size: source.file_size,
68
- )
69
- end
70
- end
71
- end
72
- end
@@ -1,45 +0,0 @@
1
- module Fontist
2
- module Formulas
3
- module Helpers
4
- module ExeExtractor
5
- def cab_extract(exe_file, download: true, font_ext: /.tt|.ttc/i)
6
- exe_file = download_file(exe_file).path if download
7
- cab_file = decompressor.search(exe_file)
8
- cabbed_fonts = grep_fonts(cab_file.files, font_ext) || []
9
- fonts_paths = extract_cabbed_fonts_to_assets(cabbed_fonts)
10
-
11
- yield(fonts_paths) if block_given?
12
- end
13
-
14
- private
15
-
16
- def decompressor
17
- @decompressor ||= (
18
- require "libmspack"
19
- LibMsPack::CabDecompressor.new
20
- )
21
- end
22
-
23
- def grep_fonts(file, font_ext)
24
- Array.new.tap do |fonts|
25
- while file
26
- fonts.push(file) if file.filename.match(font_ext)
27
- file = file.next
28
- end
29
- end
30
- end
31
-
32
- def extract_cabbed_fonts_to_assets(cabbed_fonts)
33
- Array.new.tap do |fonts|
34
- cabbed_fonts.each do |font|
35
- font_path = fonts_path.join(font.filename).to_s
36
- decompressor.extract(font, font_path)
37
-
38
- fonts.push(font_path)
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,36 +0,0 @@
1
- module Fontist
2
- module Formulas
3
- module Helpers
4
- module ZipExtractor
5
- def zip_extract(resource, download: true, fonts_sub_dir: "/")
6
- zip_file = download_file(resource) if download
7
- zip_file ||= resource.urls.first
8
-
9
- fonts_paths = unzip_fonts(zip_file, fonts_sub_dir)
10
- block_given? ? yield(fonts_paths) : fonts_paths
11
- end
12
-
13
- private
14
-
15
- def unzip_fonts(file, fonts_sub_dir = "/")
16
- Zip.on_exists_proc = true
17
-
18
- Array.new.tap do |fonts|
19
- Zip::File.open(file) do |zip_file|
20
- zip_file.glob("#{fonts_sub_dir}*.{ttf,ttc}").each do |entry|
21
- filename = entry.name.gsub("#{fonts_sub_dir}", "")
22
-
23
- if filename
24
- font_path = fonts_path.join(filename)
25
- fonts.push(font_path.to_s)
26
-
27
- entry.extract(font_path)
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
34
- end
35
- end
36
- end
@@ -1,29 +0,0 @@
1
- module Fontist
2
- module Formulas
3
- class MsSystem < Base
4
- include Formulas::Helpers::ExeExtractor
5
-
6
- private
7
-
8
- def data_node
9
- @data_node ||= "ms_system"
10
- end
11
-
12
- def check_user_license_agreement
13
- unless resources(data_node).agreement === confirmation
14
- raise(Fontist::Errors::LicensingError)
15
- end
16
- end
17
-
18
- def extract_fonts(font_names)
19
- resources(data_node) do |resource|
20
- cab_extract(resource) do |fonts_dir|
21
- font_names.each do |font_name|
22
- match_fonts(fonts_dir, font_name)
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,42 +0,0 @@
1
- module Fontist
2
- module Formulas
3
- class MsVista < Base
4
- include Formulas::Helpers::ExeExtractor
5
-
6
- private
7
-
8
- def data_node
9
- @data_node ||= "msvista"
10
- end
11
-
12
- def check_user_license_agreement
13
- unless resources(data_node).agreement === confirmation
14
- raise(Fontist::Errors::LicensingError)
15
- end
16
- end
17
-
18
- def extract_fonts(font_names)
19
- resources(data_node) do |resource|
20
- exe_extract(resource) do |cab_file|
21
- cab_extract(cab_file, download: false) do |fonts_dir|
22
- font_names.each do |font_name|
23
- match_fonts(fonts_dir, font_name)
24
- end
25
- end
26
- end
27
- end
28
- end
29
-
30
- def exe_extract(source)
31
- cab_files = decompressor.search(download_file(source).path)
32
- decompressor.extract(cab_files.files.next, ppviewer_cab)
33
-
34
- yield(ppviewer_cab) if block_given?
35
- end
36
-
37
- def ppviewer_cab
38
- @ppviewer_cab ||= Fontist.assets_path.join("ppviewer.cab").to_s
39
- end
40
- end
41
- end
42
- end
@@ -1,25 +0,0 @@
1
- require "zip"
2
-
3
- module Fontist
4
- module Formulas
5
- class SourceFont < Formulas::Base
6
- include Formulas::Helpers::ZipExtractor
7
-
8
- private
9
-
10
- def data_node
11
- @data_node ||= "source_font"
12
- end
13
-
14
- def extract_fonts(font_names)
15
- resources(data_node) do |resource|
16
- zip_extract(resource, fonts_sub_dir: "fonts/") do |fonts_paths|
17
- font_names.each do |font_name|
18
- match_fonts(fonts_paths, font_name)
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,42 +0,0 @@
1
- module Fontist
2
- class Installer
3
- def initialize(font_name:, confirmation:, **options)
4
- @font_name = font_name
5
- @confirmation = confirmation.downcase
6
- @options = options
7
- end
8
-
9
- def download
10
- find_system_font || download_font || raise(
11
- Fontist::Errors::NonSupportedFontError
12
- )
13
- end
14
-
15
- def self.download(font_name, confirmation:)
16
- new(font_name: font_name, confirmation: confirmation).download
17
- end
18
-
19
- private
20
-
21
- attr_reader :font_name, :confirmation, :options
22
-
23
- def find_system_font
24
- Fontist::SystemFont.find(font_name)
25
- end
26
-
27
- def font_formulas
28
- Fontist::FormulaFinder.find(font_name)
29
- end
30
-
31
- def download_font
32
- if font_formulas
33
- font_formulas.map do |formula|
34
- formula[:installer].fetch_font(
35
- font_name,
36
- confirmation: confirmation,
37
- )
38
- end.flatten
39
- end
40
- end
41
- end
42
- end
@@ -1,58 +0,0 @@
1
- require "yaml"
2
- require "json"
3
- require "ostruct"
4
-
5
- module Fontist
6
- class Source
7
- def self.all
8
- new.all
9
- end
10
-
11
- def self.formulas
12
- new.formulas
13
- end
14
-
15
- def all
16
- source_data
17
- end
18
-
19
- def formulas
20
- formulas_data
21
- end
22
-
23
- private
24
-
25
- def source_data
26
- @source_data ||= parse_to_object(yaml_data)
27
- end
28
-
29
- def parse_to_object(data)
30
- JSON.parse(data.to_json, object_class: OpenStruct)
31
- end
32
-
33
- def formulas_data
34
- @formulas_data ||= parse_to_object(load_formulas)
35
- end
36
-
37
- def load_formulas
38
- Hash.new.tap do |formulas|
39
- source_data.remote.formulas.map do |formula_file|
40
- formula_data = yaml_data(formula_file)
41
-
42
- if formula_data
43
- key = formula_data.keys.first
44
- formulas[key] = formula_data[key]
45
- end
46
- end
47
- end
48
- end
49
-
50
- def yaml_file(file)
51
- Fontist.data_path.join(file)
52
- end
53
-
54
- def yaml_data(file_name = "source.yml")
55
- YAML.load(File.open(yaml_file(file_name)))
56
- end
57
- end
58
- end