fontist 0.2.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) 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 -3
  5. data/README.md +91 -5
  6. data/fontist.gemspec +1 -0
  7. data/lib/fontist.rb +26 -8
  8. data/lib/fontist/errors.rb +3 -0
  9. data/lib/fontist/font.rb +78 -0
  10. data/lib/fontist/font_formula.rb +123 -0
  11. data/lib/fontist/formula.rb +90 -0
  12. data/lib/fontist/formulas.rb +15 -0
  13. data/lib/fontist/formulas/andale_font.rb +79 -0
  14. data/lib/fontist/formulas/arial_black_font.rb +78 -0
  15. data/lib/fontist/formulas/cleartype_fonts.rb +226 -0
  16. data/lib/fontist/formulas/comic_font.rb +77 -0
  17. data/lib/fontist/formulas/courier_font.rb +80 -0
  18. data/lib/fontist/formulas/euphemia_font.rb +85 -0
  19. data/lib/fontist/formulas/georgia_font.rb +79 -0
  20. data/lib/fontist/formulas/impact_font.rb +77 -0
  21. data/lib/fontist/formulas/montserrat_font.rb +132 -0
  22. data/lib/fontist/formulas/ms_truetype_fonts.rb +124 -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 +146 -0
  28. data/lib/fontist/formulas/webding_font.rb +77 -0
  29. data/lib/fontist/registry.rb +42 -0
  30. data/lib/fontist/system.yml +17 -0
  31. data/lib/fontist/system_font.rb +19 -12
  32. data/lib/fontist/utils.rb +9 -0
  33. data/lib/fontist/utils/downloader.rb +74 -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/zip_extractor.rb +38 -0
  37. data/lib/fontist/version.rb +1 -1
  38. data/spec/fontist/font_formula_spec.rb +67 -0
  39. data/spec/fontist/font_spec.rb +87 -0
  40. data/spec/fontist/formula_spec.rb +67 -0
  41. data/spec/fontist/formulas/andale_font_spec.rb +29 -0
  42. data/spec/fontist/formulas/arial_black_font_spec.rb +29 -0
  43. data/spec/fontist/formulas/cleartype_fonts_spec.rb +38 -0
  44. data/spec/fontist/formulas/comic_font_spec.rb +29 -0
  45. data/spec/fontist/formulas/courier_font_spec.rb +29 -0
  46. data/spec/fontist/formulas/euphemia_font_spec.rb +29 -0
  47. data/spec/fontist/formulas/georgia_font_spec.rb +29 -0
  48. data/spec/fontist/formulas/impact_font_spec.rb +29 -0
  49. data/spec/fontist/formulas/montserrat_font_spec.rb +29 -0
  50. data/spec/fontist/formulas/ms_truetype_fonts_spec.rb +29 -0
  51. data/spec/fontist/formulas/open_sans_fonts_spec.rb +29 -0
  52. data/spec/fontist/formulas/overpass_font_spec.rb +29 -0
  53. data/spec/fontist/formulas/source_fonts_spec.rb +31 -0
  54. data/spec/fontist/formulas/stix_fonts_spec.rb +29 -0
  55. data/spec/fontist/formulas/tahoma_font_spec.rb +29 -0
  56. data/spec/fontist/formulas/webding_font_spec.rb +29 -0
  57. data/spec/fontist/registry_spec.rb +47 -0
  58. data/spec/fontist/system_font_spec.rb +16 -0
  59. data/spec/fontist/{downloader_spec.rb → utils/downloader_spec.rb} +8 -8
  60. data/spec/spec_helper.rb +7 -2
  61. data/spec/support/fontist_helper.rb +10 -0
  62. metadata +65 -14
  63. data/assets/source.yml +0 -54
  64. data/lib/fontist/downloader.rb +0 -69
  65. data/lib/fontist/finder.rb +0 -47
  66. data/lib/fontist/installer.rb +0 -44
  67. data/lib/fontist/ms_vista_font.rb +0 -83
  68. data/lib/fontist/source.rb +0 -31
  69. data/spec/fontist/finder_spec.rb +0 -41
  70. data/spec/fontist/installer_spec.rb +0 -48
  71. data/spec/fontist/ms_vista_font_spec.rb +0 -27
  72. data/spec/fontist/source_spec.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e446487ef01cae5da6b38d363e22e6a4210880f88a4741762a234598db88940
4
- data.tar.gz: 8d622e34e2a80ae41018238d99b7be5781dbd785d8505d8a69e63aa48a70706f
3
+ metadata.gz: 0eabd37a359d5d16263d7aacc3e3c4ed215619eb257d20bbe9c1505786a4ba70
4
+ data.tar.gz: 491a3dbdf9b4d2a2b1d74b3f07bb11d65e238bcf87f316d1d48b9707739d8202
5
5
  SHA512:
6
- metadata.gz: 106f4f4c64991ad9301eb2315e4b98bab19973d393a3daed060ca0d2014c4d8236ebb7de4cf6dceee7e9e4fed849111238d3d2231c83e5c3aeb6f8c3a385a4eb
7
- data.tar.gz: 5fb219fb32f6d8616924abaabe4451fba80e0be2f2986ac0b7aa574511884dec670a33635c3edad1eec817f6126f0017b995a7aa876f8711a87be8570efc6c48
6
+ metadata.gz: a9921980bec05a202f13786302e859f2f7660492ff8db9339c02a7088989d6bb0478139cf1846c05e9967272914c3a87822e827a658c5046ca1611f7ed0588b0
7
+ data.tar.gz: 2ac883da0ac5488ae0d58fa81da7473e40dfeb610a91bdc1286059c1edad816f99e892ec19a5e4ee57df835af8a27ba511c6359062cf8dba15b9d0fb07ba1619
@@ -27,4 +27,4 @@ jobs:
27
27
  run: bin/setup
28
28
 
29
29
  - name: Run tests
30
- run: bin/rspec
30
+ run: TEST_ENV=CI bin/rspec
@@ -27,4 +27,4 @@ jobs:
27
27
  run: bin/setup
28
28
 
29
29
  - name: Run tests
30
- run: bin/rspec
30
+ run: TEST_ENV=CI bin/rspec
data/.gitignore CHANGED
@@ -7,9 +7,10 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  Gemfile.lock
10
- /assets/*
11
- !/assets/source.yml
12
- !/assets/fonts/.keep
10
+
11
+ # fonts
12
+ /spec/fixtures/fonts/*
13
+ /spec/fixtures/fonts/DejaVuSerif.ttf
13
14
 
14
15
  # rspec failure tracking
15
16
  .rspec_status
data/README.md CHANGED
@@ -28,16 +28,102 @@ gem install fontist
28
28
 
29
29
  ## Usage
30
30
 
31
- ### Find a font
31
+ ### Font
32
32
 
33
- The fontist library allows us to easily locate or download a any of the supported
34
- fonts and then it returns the complete path for the path. To find any font in a
35
- user's system we can use the following interface.
33
+ The `Fontist::Font` is your go to place to deal with any font using fontist. This
34
+ interface will allow you to find a font or install a font. Lets start with how
35
+ can we find a font in your system.
36
+
37
+ #### Finding a font
38
+
39
+ The `Fontist::Fontist.find` interface can be used a find a font in your system.
40
+ It will look into the operating system specific font directories, and also the
41
+ fontist specific `~/.fontist` directory.
42
+
43
+ ```ruby
44
+ Fontist::Font.find(name)
45
+ ```
46
+
47
+ If fontist find a font then it will return the paths, but if not found then it
48
+ will could raise an unsupported font error or maybe an installation instruction
49
+ for that specific font.
50
+
51
+ #### Install a font
52
+
53
+ The `Fontist::Font.install` interface can be used to install any supported font.
54
+ This interface first checks if you already have that font installed or not and
55
+ if you do then it will return the paths.
56
+
57
+ If you don't but supported by fontist, then it will download the font and copy
58
+ it to `~/.fontist` directory and also return the paths.
36
59
 
37
60
  ```ruby
38
- Fontist::Finder.find("CALIBRI.TTF")
61
+ Fontist::Font.install(name, confirmation: "no")
39
62
  ```
40
63
 
64
+ If there are some issue with the provided font, like not supported or some other
65
+ issue then it will raise those errors.
66
+
67
+ #### List all fonts
68
+
69
+ The `Fontist::Font` interface exposes an interface to list all supported fonts,
70
+ this might be useful if want to know the name of the font or the available
71
+ styles. You can do that by using:
72
+
73
+ ```ruby
74
+ Fontist::Font.all
75
+ ```
76
+
77
+ The return values are ` OpenStruct` object, so you can easily do any other
78
+ operation you would do in any ruby object.
79
+
80
+ ### Formula
81
+
82
+ The `fontist` gem internally usages the `Fontist::Formula` interface to find a
83
+ registered formula or fonts supported by any formula. Unless, you need to do
84
+ anything with that you shouldn't need to work with this interface directly. But
85
+ if you do then these are the public interface it offers.
86
+
87
+ #### Find a formula
88
+
89
+ The `Fontist::Formula.find` interface allows you to find any of the registered
90
+ formula. This interface takes a font name as an argument and it looks through
91
+ each of the registered formula that offers this font installation. Usages:
92
+
93
+ ```ruby
94
+ Fontist::Formula.find("Calibri")
95
+ ```
96
+
97
+ The above method will find which formula offers this font and then it will
98
+ return a installable formula that can be used to check licences or install that
99
+ fonts in your system.
100
+
101
+ #### Find formula fonts
102
+
103
+ Normally, each font name can be associated with multiple styles or collection, for
104
+ example the `Calibri` font might contains a `regular`, `bola` or `italic` styles
105
+ fonts and if you want a interface that can return the complete list then this is
106
+ your friend. You can use it as following:
107
+
108
+ ```ruby
109
+ Fontist::Formula.find_fonts("Calibri")
110
+ ```
111
+
112
+
113
+ #### List all formulas
114
+
115
+ The `Fontist::Formula` interface exposes an interface to list all registered
116
+ font formula. This might be useful if want to know the name of the formula or
117
+ what type fonts can be installed using that formula. Usages:
118
+
119
+ ```ruby
120
+ Fontist::Formula.all
121
+ ```
122
+
123
+ The return values are ` OpenStruct` object, so you can easily do any other
124
+ operation you would do in any ruby object.
125
+
126
+
41
127
  ## Development
42
128
 
43
129
  We are following Sandi Metz's Rules for this gem, you can read the
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_runtime_dependency "down", "~> 5.0"
25
25
  spec.add_runtime_dependency "libmspack", "~> 0.1.0"
26
+ spec.add_runtime_dependency "rubyzip", "~> 2.3.0"
26
27
 
27
28
  spec.add_development_dependency "pry"
28
29
  spec.add_development_dependency "bundler", "~> 2.0"
@@ -1,11 +1,17 @@
1
+ require "down"
2
+ require "digest"
3
+ require "json"
4
+ require "yaml"
5
+ require "singleton"
6
+
1
7
  require "fontist/errors"
2
8
  require "fontist/version"
3
9
 
4
- require "fontist/finder"
5
- require "fontist/source"
6
- require "fontist/installer"
10
+ require "fontist/font"
11
+ require "fontist/registry"
12
+ require "fontist/formulas"
13
+ require "fontist/formula"
7
14
  require "fontist/system_font"
8
- require "fontist/ms_vista_font"
9
15
 
10
16
  module Fontist
11
17
  def self.lib_path
@@ -16,10 +22,6 @@ module Fontist
16
22
  Pathname.new(File.dirname(__dir__))
17
23
  end
18
24
 
19
- def self.assets_path
20
- Fontist.root_path.join("assets")
21
- end
22
-
23
25
  def self.fontist_path
24
26
  Pathname.new(Dir.home).join(".fontist")
25
27
  end
@@ -27,4 +29,20 @@ module Fontist
27
29
  def self.fonts_path
28
30
  Fontist.fontist_path.join("fonts")
29
31
  end
32
+
33
+ def self.formulas_path
34
+ Fontist.lib_path.join("fontist", "formulas")
35
+ end
30
36
  end
37
+
38
+ # Loading formulas
39
+ #
40
+ # The formula loading behavior is dynamic, so what we are actualy
41
+ # doing here is looking for formulas in the `./fontist/formulas` directory
42
+ # then require thos as we go.
43
+ #
44
+ # There is a caviat, since the `Dir` method depends on absoulate path
45
+ # so moving this loading up or somewhere else might not always ensure
46
+ # the fontist related path helpers.
47
+ #
48
+ Dir[Fontist.formulas_path.join("**.rb").to_s].sort.each { |file| require file }
@@ -3,5 +3,8 @@ module Fontist
3
3
  class LicensingError < StandardError; end
4
4
  class MissingFontError < StandardError; end
5
5
  class NonSupportedFontError < StandardError; end
6
+ class TemparedFileError < StandardError; end
7
+ class InvalidResourceError < StandardError; end
8
+ class TimeoutError < StandardError; end
6
9
  end
7
10
  end
@@ -0,0 +1,78 @@
1
+ module Fontist
2
+ class Font
3
+ def initialize(options = {})
4
+ @name = options.fetch(:name, nil)
5
+ @confirmation = options.fetch(:confirmation, "no")
6
+
7
+ check_or_create_fontist_path!
8
+ end
9
+
10
+ def self.all
11
+ new.all
12
+ end
13
+
14
+ def self.find(name)
15
+ new(name: name).find
16
+ end
17
+
18
+ def self.install(name, confirmation: "no")
19
+ new(name: name, confirmation: confirmation).install
20
+ end
21
+
22
+ def find
23
+ find_system_font || downloadable_font || raise(
24
+ Fontist::Errors::NonSupportedFontError
25
+ )
26
+ end
27
+
28
+ def install
29
+ find_system_font || download_font || raise(
30
+ Fontist::Errors::NonSupportedFontError
31
+ )
32
+ end
33
+
34
+ def all
35
+ Fontist::Formula.all.to_h.map { |_name, formula| formula.fonts }.flatten
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :name, :confirmation
41
+
42
+ def find_system_font
43
+ Fontist::SystemFont.find(name)
44
+ end
45
+
46
+ def check_or_create_fontist_path!
47
+ unless Fontist.fonts_path.exist?
48
+ require "fileutils"
49
+ FileUtils.mkdir_p(Fontist.fonts_path)
50
+ end
51
+ end
52
+
53
+ def font_installer(formula)
54
+ Object.const_get(formula.installer)
55
+ end
56
+
57
+ def formula
58
+ @formula ||= Fontist::Formula.find(name)
59
+ end
60
+
61
+ def downloadable_font
62
+ if formula
63
+ raise(
64
+ Fontist::Errors::MissingFontError,
65
+ "Fonts are missing, please run " \
66
+ "Fontist::Font.install('#{name}', confirmation: 'yes') to " \
67
+ "download the font"
68
+ )
69
+ end
70
+ end
71
+
72
+ def download_font
73
+ if formula
74
+ font_installer(formula).fetch_font(name, confirmation: confirmation)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,123 @@
1
+ module Fontist
2
+ class FontFormula
3
+ include Singleton
4
+ extend Fontist::Utils::Dsl
5
+ include Fontist::Utils::ZipExtractor
6
+ include Fontist::Utils::ExeExtractor
7
+
8
+ attr_accessor :license, :license_url, :license_required
9
+ attr_accessor :key, :homepage, :description, :options, :temp_resource
10
+
11
+ def font_list
12
+ @font_list ||= []
13
+ end
14
+
15
+ def resources
16
+ @resources ||= {}
17
+ end
18
+
19
+ def fonts
20
+ @fonts ||= font_list.uniq
21
+ end
22
+
23
+ def extract_font_styles(options)
24
+ extract_from_file(options) ||
25
+ extract_from_collection(options) || default_font
26
+ end
27
+
28
+ def reinitialize
29
+ @downloaded = false
30
+ @matched_fonts = []
31
+ end
32
+
33
+ def self.fetch_font(name, confirmation:)
34
+ if instance.license_required && confirmation.downcase != "yes"
35
+ raise(Fontist::Errors::LicensingError)
36
+ end
37
+
38
+ instance.reinitialize
39
+ instance.install_font(name, confirmation)
40
+ end
41
+
42
+ def install_font(name, confirmation)
43
+ run_in_temp_dir { extract }
44
+ matched_fonts_uniq = matched_fonts.flatten.uniq
45
+ matched_fonts_uniq.empty? ? nil : matched_fonts_uniq
46
+ end
47
+
48
+ private
49
+
50
+ attr_reader :downloaded, :matched_fonts
51
+
52
+ def resource(name, &block)
53
+ source = resources[name]
54
+ block_given? ? yield(source) : source
55
+ end
56
+
57
+ def fonts_path
58
+ @fonts_path ||= Fontist.fonts_path
59
+ end
60
+
61
+ def default_font
62
+ [{ type: "Regular", font: temp_resource[:filename] }]
63
+ end
64
+
65
+ def run_in_temp_dir(&block)
66
+ Dir.mktmpdir(nil, Dir.tmpdir) do |dir|
67
+ @temp_dir = Pathname.new(dir)
68
+
69
+ yield
70
+ @temp_dir = nil
71
+ end
72
+ end
73
+
74
+ def extract_from_file(options)
75
+ styles = options.fetch(:match_styles_from_file, [])
76
+
77
+ unless styles.empty?
78
+ styles.map { |type, file | { type: type, font: file } }
79
+ end
80
+ end
81
+
82
+ def match_fonts(fonts_dir, font_name)
83
+ fonts = map_names_to_fonts(font_name).join("|")
84
+ font = fonts_dir.grep(/#{fonts}/i)
85
+ @matched_fonts.push(font) if font
86
+
87
+ font
88
+ end
89
+
90
+ def extract_from_collection(options)
91
+ styles = options.fetch(:extract_styles_from_collection, [])
92
+
93
+ unless styles.empty?
94
+ styles.map do |type, file|
95
+ { type: type, collection: file, font: temp_resource[:filename] }
96
+ end
97
+ end
98
+ end
99
+
100
+ def map_names_to_fonts(font_name)
101
+ fonts = Fontist::Formula.find_fonts(font_name)
102
+ fonts = fonts.map { |font| font.styles.map(&:font) }.flatten if fonts
103
+
104
+ fonts || []
105
+ end
106
+
107
+ def download_file(source)
108
+ downloaded_file = Fontist::Utils::Downloader.download(
109
+ source[:urls].sample,
110
+ sha: source[:sha256],
111
+ file_size: source[:file_size],
112
+ progress_bar: is_progress_bar_enabled
113
+ )
114
+
115
+ @downloaded = true
116
+ downloaded_file
117
+ end
118
+
119
+ def is_progress_bar_enabled
120
+ options.nil? ? false : options.fetch(:progress_bar, false)
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,90 @@
1
+ module Fontist
2
+ class Formula
3
+ def initialize(options = {})
4
+ @font_name = options.fetch(:font_name, nil)
5
+
6
+ check_and_register_font_formulas
7
+ end
8
+
9
+ def self.all
10
+ new.all
11
+ end
12
+
13
+ def self.find(font_name)
14
+ new(font_name: font_name).find
15
+ end
16
+
17
+ def self.find_fonts(name)
18
+ new(font_name: name).find_fonts
19
+ end
20
+
21
+ def all
22
+ @all ||= Fontist::Registry.instance.formulas
23
+ end
24
+
25
+ def find
26
+ [find_formula].flatten.first
27
+ end
28
+
29
+ def find_fonts
30
+ formulas = [find_formula].flatten
31
+ match_fonts_by_name(formulas) unless formulas.empty?
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :font_name
37
+
38
+ def find_formula
39
+ find_by_font_name || find_by_font || []
40
+ end
41
+
42
+ def formulas
43
+ @formulas ||= all.to_h
44
+ end
45
+
46
+ def match_fonts_by_name(formulas)
47
+ matched_fonts = formulas.map do |formula|
48
+ formula.fonts.select do |font|
49
+ font.name.downcase == font_name.downcase
50
+ end
51
+ end
52
+
53
+ matched_fonts.empty? ? nil : matched_fonts.flatten
54
+ end
55
+
56
+ def find_by_font_name
57
+ matched_formulas = formulas.select do |key, value|
58
+ !value.fonts.map(&:name).grep(/#{font_name}/i).empty?
59
+ end
60
+
61
+ matched_formulas.empty? ? nil : matched_formulas.values
62
+ end
63
+
64
+ # Note
65
+ #
66
+ # These interface recursively look into every single font styles,
67
+ # so ideally try to avoid using it when possible, and that's why
68
+ # we've added it as last option in formula finder.
69
+ #
70
+ def find_by_font
71
+ matched_formulas = formulas.select do |key, value|
72
+ match_in_font_styles?(value[:fonts])
73
+ end
74
+
75
+ matched_formulas.empty? ? nil : matched_formulas.values
76
+ end
77
+
78
+ def match_in_font_styles?(fonts)
79
+ styles = fonts.select do |font|
80
+ !font.styles.map(&:font).grep(/#{font_name}/i).empty?
81
+ end
82
+
83
+ styles.empty? ? false : true
84
+ end
85
+
86
+ def check_and_register_font_formulas
87
+ $check_and_register_font_formulas ||= Fontist::Formulas.register_formulas
88
+ end
89
+ end
90
+ end