cheapredwine 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0db300cc3cd1f51a506262396b30650345609de
4
- data.tar.gz: d48412833b31a5bfbfceaca49fbdc39cd62e3d45
3
+ metadata.gz: 507b55232c7e11bb44fecba447aafb123c916a54
4
+ data.tar.gz: 71a1610253c44b2e813f5a35794403642e9741c9
5
5
  SHA512:
6
- metadata.gz: 8d1f414e15102ad639243999c88d0b4db26949225fc94512e916bb88750dc1e13f18d153498197faaf8cf7b383e51e22b550e1d503e932c893282afae6856258
7
- data.tar.gz: 8814e6f52930c0dd488c612a77a952cd67b260fb1cf41bd1375c005caddbbf86aeecc628e2b053e889b8b2c2e3e5f7722efd645a2f93057b8b3ce26f24e4fe7e
6
+ metadata.gz: 644ce9189ef137b71c6d4e09143a42f1113bcd35c1262b707e404b9bc117c139b0d3e266f2d0f5e3bf2a0124a734a81572374f8eaf0e8e6f2a54f99d743b5ef9
7
+ data.tar.gz: b4bc656a2f792e3b02bffbb0d5d87eab712aca3a02c9f604bd02f80dcd256207cdfe8fb5b1e042c325d5bc6ab5ae9592a00a21fefcdebd366ecfbdadbd9d7530
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml CHANGED
@@ -2,9 +2,3 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
-
6
- before_script:
7
- - echo 'yes' | sudo add-apt-repository ppa:olivier-berten/fonts
8
- - sudo apt-get update
9
- - sudo apt-get install libcairo
10
- - sudo apt-get install harfbuzz
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
- # CheapRedWine
1
+ # Cheapredwine
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/github/hugobast/cheapredwine.png)](https://codeclimate.com/github/hugobast/cheapredwine)
4
+ [![Build Status](https://travis-ci.org/hugobast/cheapredwine.png?branch=master)](https://travis-ci.org/hugobast/cheapredwine)
4
5
 
5
-
6
-
7
- I needed a way to test fonts on the web. I based this library on myfonts.com. CheapRedWine makes use to Harfbuzz' hb-view utility to generate images from fonts with a slew of different parameters. It also uses fontTools' ttx utility for font introspection.
6
+ Makes text images. Introspecs (in a limited way thus far) a font file to get to it's metadata. Supports OTF/TTF font features the likes of ligatures, old style numbers, stylistic alternatives, etc. Cheapredwine uses of Harfbuzz' hb-view utility to generate images.
8
7
 
9
8
  ## Installation
10
9
 
@@ -14,7 +13,7 @@ Make sure the dependencies are satisfied
14
13
 
15
14
  ## Dependencies
16
15
 
17
- It assumes the following is installed and in the case where it applies, accessible from the $PATH env variable.
16
+ It assumes the following is installed and in the case where it applies; accessible from the $PATH env variable.
18
17
 
19
18
  * [cairo](http://www.cairographics.org/releases/)
20
19
  * [harfbuzz](http://www.freedesktop.org/software/harfbuzz/release/)
@@ -23,28 +22,24 @@ It assumes the following is installed and in the case where it applies, accessib
23
22
 
24
23
  #### Getting a font object from a simple ttf or otf file:
25
24
 
26
- font = CheapRedWine.new font_file
27
- font.name # => "Font Name"
28
- font.family # => "Font Family"
25
+ font = Cheapredwine.new font_file
26
+ font.font_name # => "Font Name"
27
+ font.family_name # => "Font Family"
29
28
  font.style # => "Bold Italic"
30
29
  font.features # => ["liga", "onum", "dlig", … "salt"]
31
30
 
32
31
  #### Generating images with text for the font:
33
32
 
34
- image = font.image "some text", options
35
- # ... do something with image ...
36
- image.close
37
-
38
- **Caveat:** `image` is an IO object that can then be use to write to disk. You are responsible to close that object.
39
-
40
- ##### Options
41
-
42
- options = {
43
- margin: 5, # margin around the text
44
- size: 40, # size of the text eq to px
45
- color: red, # the text color
46
- features: [liga, salt] # list of otf features to be applied
47
- }
33
+ image = font.image do
34
+ append_text "Hello" # A regular piece of text
35
+ append_text "123456", with: ["onum"] # A piece of text with 'onum' enable
36
+ turn_on_feature "dlig" # Turns a feature on for the whole image
37
+ turn_off_feature "liga" # Turns off a feature for the whole image
38
+ set_margin 0
39
+ set_font_size 32
40
+ set_background "#123456"
41
+ set_foreground "#654321"
42
+ end
48
43
 
49
44
  ## Contributing
50
45
 
@@ -56,4 +51,4 @@ It assumes the following is installed and in the case where it applies, accessib
56
51
 
57
52
  ## TODO
58
53
 
59
- 1. Replace hb-view by something more appropriate (C Extension).
54
+ 1. Replace hb-view by something more appropriate (C Extension or pure Ruby implementation).
data/cheapredwine.gemspec CHANGED
@@ -5,11 +5,11 @@ require 'cheapredwine/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "cheapredwine"
8
- gem.version = CheapRedWine::VERSION
8
+ gem.version = Cheapredwine::VERSION
9
9
  gem.authors = ["Hugo Bastien"]
10
10
  gem.email = ["hugo@hbastien.com"]
11
- gem.description = %q{I needed a way to test fonts on the web. I based this library on myfonts.com. CheapRedWine makes use to Harfbuzz' hb-view utility to generate images from fonts with a slew of different parameters. It also uses fontTools' ttx utility for font introspection.}
12
- gem.summary = %q{I needed a way to test fonts on the web. I based this library on myfonts.com. CheapRedWine makes use to Harfbuzz' hb-view utility to generate images from fonts with a slew of different parameters. It also uses fontTools' ttx utility for font introspection.}
11
+ gem.description = %q{I needed a way to test fonts on the web. I based this library on myfonts.com. Cheapredwine uses to Harfbuzz' hb-view utility to generate images from fonts with a slew of different parameters. It also uses fontTools' ttx utility for font introspection.}
12
+ gem.summary = %q{I needed a way to test fonts on the web. I based this library on myfonts.com. Cheapredwine uses to Harfbuzz' hb-view utility to generate images from fonts with a slew of different parameters. It also uses fontTools' ttx utility for font introspection.}
13
13
  gem.homepage = "https://github.com/hugobast/cheapredwine"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
data/lib/cheapredwine.rb CHANGED
@@ -1,55 +1,34 @@
1
1
  require "cheapredwine/version"
2
- require "cheapredwine/info"
3
- require "cheapredwine/image"
2
+ require "cheapredwine/command"
4
3
 
5
- class CheapRedWine
4
+ class Cheapredwine
6
5
  extend Forwardable
7
- attr_accessor :font
8
- def_delegators :@font, :file, :name, :family, :style, :features
9
-
10
- def initialize(file)
11
- info = Info.new file
12
- @font = Font.new do |font|
13
- font.name = info.font_name
14
- font.family = info.family_name
15
- font.features = info.features
16
- font.style = info.style
17
- font.path = file
18
- font
19
- end
20
- end
21
6
 
22
- def image(text, options = {})
23
- features = merge_features(font.features, options.fetch(:features) { [] })
24
-
25
- params = options.merge({
26
- features: features,
27
- font: font.file,
28
- text: text
29
- })
30
-
31
- params = Image::Params.new(params)
32
- Image::Writer.new(params).exec
7
+ attr_accessor :file
8
+ def_delegators :@attrs, :font_name, :family_name, :style, :features
9
+
10
+ def initialize file
11
+ @attrs = Parser.new file
12
+ @file = File.new file
33
13
  end
34
14
 
35
- private
15
+ def image &block
16
+ create(&block).run!
17
+ end
36
18
 
37
- def merge_features(all_features, selected_features)
38
- all_features.map do |feature|
39
- prefix = selected_features.include?(feature) ? '+' : '-'
40
- prefix + feature
41
- end
19
+ def create &block
20
+ builder.instance_eval &block
21
+ builder.font_file file.path
22
+ runner
42
23
  end
43
24
 
44
- class Font
45
- attr_accessor :name, :family, :features, :path, :style
25
+ private
46
26
 
47
- def initialize
48
- yield(self) if block_given?
49
- end
27
+ def builder
28
+ @builder ||= Command::Builder.new
29
+ end
50
30
 
51
- def file
52
- File.new(path)
53
- end
31
+ def runner
32
+ @runner ||= Command::Runner.new builder
54
33
  end
55
34
  end
@@ -0,0 +1,2 @@
1
+ require 'cheapredwine/command/builder'
2
+ require 'cheapredwine/command/runner'
@@ -0,0 +1,69 @@
1
+ require 'cheapredwine/command/nodes'
2
+
3
+ class Cheapredwine
4
+ module Command
5
+ class Builder
6
+ attr_reader :features, :nodes
7
+
8
+ def initialize
9
+ @features = ["-liga"]
10
+ @nodes = Nodes.new
11
+ yield self if block_given?
12
+ end
13
+
14
+ def append_text text, options = {}
15
+ nodes.add text, options.fetch(:with, [])
16
+ end
17
+
18
+ def set_margin size
19
+ @margin = size
20
+ end
21
+
22
+ def set_font_size size
23
+ @font_size = size
24
+ end
25
+
26
+ def font_file file
27
+ @font_file = file
28
+ end
29
+
30
+ def set_foreground color
31
+ @foreground = color
32
+ end
33
+
34
+ def set_background color
35
+ @background = color
36
+ end
37
+
38
+ def turn_on_feature feature
39
+ features.push "+#{feature}"
40
+ end
41
+
42
+ def turn_off_feature feature
43
+ features.push "-#{feature}"
44
+ end
45
+
46
+ def to_params
47
+ {
48
+ features: "#{all_features.join(",")}",
49
+ text: "#{nodes.concat}",
50
+ font_file: @font_file,
51
+ font_size: @font_size,
52
+ foreground: @foreground,
53
+ background: @background,
54
+ margin: @margin
55
+ }.reject { |key, val| val.nil? }
56
+ end
57
+
58
+ private
59
+
60
+ def all_features
61
+ features + all_features_from_text
62
+ end
63
+
64
+ def all_features_from_text
65
+ nodes.all.map { |node| node.features }.flatten
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,19 @@
1
+ class Cheapredwine
2
+ module Command
3
+ class Node
4
+ attr_reader :text, :starts_at
5
+
6
+ def initialize text, features, starts_at
7
+ @text = text
8
+ @features = features
9
+ @starts_at = starts_at
10
+ end
11
+
12
+ def features
13
+ @features.map do |feature|
14
+ "#{feature}[#{starts_at}:#{starts_at + text.length}]"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'cheapredwine/command/node'
2
+
3
+ class Cheapredwine
4
+ module Command
5
+ class Nodes
6
+ attr_reader :nodes
7
+
8
+ def initialize
9
+ @nodes = []
10
+ end
11
+
12
+ def add text, features
13
+ nodes << Node.new(text, features, length)
14
+ end
15
+
16
+ def all
17
+ nodes
18
+ end
19
+
20
+ def concat
21
+ nodes.map { |node| node.text }.join
22
+ end
23
+
24
+ def length
25
+ concat.length
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ class Cheapredwine
2
+ module Command
3
+ class Runner
4
+ attr_reader :builder
5
+
6
+ def initialize builder
7
+ @builder = builder
8
+ end
9
+
10
+ def run!
11
+ IO.popen params.unshift "hb-view"
12
+ end
13
+
14
+ def params
15
+ builder.to_params.map { |k, v| parameterize_pair k, v }
16
+ end
17
+
18
+ private
19
+
20
+ def parameterize_pair key, val
21
+ "--#{parameterize key}=#{val}"
22
+ end
23
+
24
+ def parameterize key
25
+ key.to_s.gsub(/_/, "-")
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,9 +1,9 @@
1
1
  require 'tipo'
2
2
 
3
- class CheapRedWine
4
- class Info
5
- def initialize file
6
- @info = Tipo.info file
3
+ class Cheapredwine
4
+ class Parser
5
+ def initialize file, parser = Tipo
6
+ @info = parser.info file
7
7
  end
8
8
 
9
9
  def font_name
@@ -1,3 +1,3 @@
1
- class CheapRedWine
2
- VERSION = "0.3.1"
1
+ class Cheapredwine
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'cheapredwine/command/builder'
3
+
4
+ describe Cheapredwine::Command::Builder do
5
+ let(:builder) { Cheapredwine::Command::Builder.new }
6
+
7
+ it "sets liga to off by default" do
8
+ expect(builder.features).to eq ["-liga"]
9
+ end
10
+
11
+ it "transforms it's data to params" do
12
+ builder.append_text "hello"
13
+ builder.append_text " world"
14
+
15
+ expect(builder.to_params).to eq features: "-liga", text: "hello world"
16
+ end
17
+
18
+ it "transforms it's data to params along with features" do
19
+ builder.append_text "ffi", with: ["liga"]
20
+ builder.append_text "stffi"
21
+ builder.append_text "st", with: ["dlig"]
22
+
23
+ expect(builder.to_params).to eq features: "-liga,liga[0:3],dlig[8:10]", text: "ffistffist"
24
+ end
25
+
26
+ context "with features" do
27
+ let(:builder) { Cheapredwine::Command::Builder.new }
28
+
29
+ it "can turn on a features" do
30
+ builder.turn_on_feature "liga"
31
+ expect(builder.features).to eq ["-liga", "+liga"]
32
+ end
33
+
34
+ it "can turn off a feature" do
35
+ builder.turn_off_feature "onum"
36
+ expect(builder.features).to eq ["-liga", "-onum"]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ require 'cheapredwine/command/runner'
2
+ require 'cheapredwine/command/builder'
3
+
4
+ describe Cheapredwine::Command::Runner do
5
+ let(:builder) {
6
+ Cheapredwine::Command::Builder.new do |builder|
7
+ builder.append_text "Lorem ipsum dolor"
8
+ builder.turn_on_feature "aalt"
9
+ builder.set_font_size 15
10
+ builder.font_file "/path/to/font.otf"
11
+ end
12
+ }
13
+
14
+ let(:runner) { Cheapredwine::Command::Runner.new builder }
15
+
16
+ it "assembles params from a builder" do
17
+ expect(runner.params).to eq [
18
+ "--features=-liga,+aalt",
19
+ "--text=Lorem ipsum dolor",
20
+ "--font-file=/path/to/font.otf",
21
+ "--font-size=15"
22
+ ]
23
+ end
24
+
25
+ it "executes the command" do
26
+ expect(IO).to receive(:popen).with([
27
+ "hb-view",
28
+ "--features=-liga,+aalt",
29
+ "--text=Lorem ipsum dolor",
30
+ "--font-file=/path/to/font.otf",
31
+ "--font-size=15"
32
+ ])
33
+
34
+ runner.run!
35
+ end
36
+ end