tipo 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Hugo Bastien
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Tipo
2
+
3
+ [![Code Climate](https://codeclimate.com/github/hugobast/tipo.png)](https://codeclimate.com/github/hugobast/tipo) [![Build Status](https://travis-ci.org/hugobast/tipo.png?branch=master)](https://travis-ci.org/hugobast/tipo)
4
+
5
+ This is very early work but Tipo is meant to be a pure ruby OpenType and TrueType font format reader. Support for more tables and information to come.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'tipo'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tipo
20
+
21
+ ## Usage
22
+
23
+ Very narrow interface:
24
+
25
+ ```ruby
26
+ info = Tipo.info 'path/to/font.{otf|ttf}'
27
+ info.naming.font # => "Font Name"
28
+ info.naming.style # => "Italic"
29
+
30
+ info.substitution.feature_list.map do |feature|
31
+ feature.tag
32
+ end.uniq # => ['aalt', 'dlig', 'liga' ... ]
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ require "active_support/inflector"
2
+
3
+ require "tipo/version"
4
+ require "tipo/table"
5
+ require "tipo/header"
6
+ require "tipo/font"
7
+ require "tipo/table"
8
+
9
+ module Tipo
10
+ class Info
11
+ attr_reader :font, :header
12
+
13
+ def initialize file
14
+ @font = Font.new file
15
+ @header = Header.new font
16
+ end
17
+
18
+ def method_missing table, *args, &block
19
+ table = "tipo/table/#{table}".camelize.constantize
20
+ table.new @header, @font
21
+ end
22
+ end
23
+
24
+ def self.info file
25
+ Info.new file
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'forwardable'
2
+
3
+ module Tipo
4
+ class Font
5
+ extend Forwardable
6
+
7
+ def_delegators :@file, :seek, :tell
8
+
9
+ def initialize file
10
+ @file = File.open file, 'rb:ASCII-8BIT'
11
+ end
12
+
13
+ def encoding
14
+ @file.read(5).encoding
15
+ end
16
+
17
+ def chunk options
18
+ @file.seek options[:seek] if options[:seek]
19
+ @file.read options.fetch :bytes
20
+ end
21
+
22
+ def unpack options
23
+ tmpl = options.delete :tmpl
24
+ chunk(options).unpack tmpl
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,26 @@
1
+ module Tipo
2
+ class Header
3
+ # full template 'a4n4'
4
+ OffsetTable = Struct.new :version, :tables_count
5
+
6
+ # full template 'a4N3'
7
+ TableRecord = Struct.new :tag, :checksum, :offset
8
+
9
+ attr_reader :font
10
+
11
+ def initialize font
12
+ @font = font
13
+ end
14
+
15
+ def offset_table
16
+ OffsetTable.new *(font.unpack seek: 0, bytes: 12, tmpl: 'a4n')
17
+ end
18
+
19
+ def table_records
20
+ font.seek 13
21
+ offset_table.tables_count.times.map do
22
+ TableRecord.new *(font.unpack bytes: 16, tmpl: 'a4N2')
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ require 'tipo/table/base'
2
+ require 'tipo/table/substitution'
3
+ require 'tipo/table/naming'
4
+
@@ -0,0 +1,28 @@
1
+ module Tipo
2
+ module Table
3
+ class Base
4
+ attr_reader :font_header, :font
5
+ attr_accessor :name
6
+
7
+ def initialize font_header, font
8
+ @font_header = font_header
9
+ @font = font
10
+ end
11
+
12
+ def offset
13
+ seek_to_table @name
14
+ end
15
+
16
+ private
17
+
18
+ def seek_to_table tag
19
+ font.seek find_table(tag).offset
20
+ font.tell
21
+ end
22
+
23
+ def find_table tag
24
+ font_header.table_records.select { |t| t.tag == tag }.first
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ module Tipo
2
+ module Table
3
+ class Naming < Base
4
+ # full template 'n3'
5
+ Header = Struct.new :format, :count, :storage_offset
6
+
7
+ # full template 'n6'
8
+ NameRecord = Struct.new(
9
+ :platform, :encoding, :language, :name, :length, :offset)
10
+
11
+ def initialize header, font
12
+ @name = 'name'
13
+ super
14
+ end
15
+
16
+ def header
17
+ Header.new *(font.unpack seek: offset, bytes: 6, tmpl: 'n3')
18
+ end
19
+
20
+ def storage_offset
21
+ header.storage_offset + offset
22
+ end
23
+
24
+ def naming_list
25
+ header.count.times.map do
26
+ NameRecord.new *(font.unpack bytes: 12, tmpl: 'n6')
27
+ end
28
+ end
29
+
30
+ def record_at id
31
+ naming_list.select do |n|
32
+ n.name == id && n.platform == 1
33
+ end.first
34
+ end
35
+
36
+ def family_name
37
+ stringify_record record_at 1
38
+ end
39
+
40
+ def name
41
+ stringify_record record_at 4
42
+ end
43
+
44
+ def style
45
+ stringify_record record_at 2
46
+ end
47
+
48
+ private
49
+
50
+ def stringify_record record
51
+ # To handle both 16bit and 8bit strings
52
+ # first convert to a ord array and build the string
53
+ #
54
+ # TODO: Verify platform and encoding ids and provide
55
+ # the proper template for each
56
+ #
57
+ text = font.unpack(
58
+ seek: storage_offset + record.offset,
59
+ bytes: record.length,
60
+ tmpl: 'c*')
61
+ text.pack 'U*'
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,40 @@
1
+ module Tipo
2
+ module Table
3
+ class Substitution < Base
4
+ # full template 'n5'
5
+ Header = Struct.new(
6
+ :version_left, :version_right,
7
+ :script_offset, :feature_offset, :lookup_offset) do
8
+ def version
9
+ "#{version_left}.#{version_right}"
10
+ end
11
+ end
12
+
13
+ # full template 'a4n'
14
+ FeatureRecord = Struct.new :tag, :feature_offset
15
+
16
+ def initialize header, font
17
+ @name = "GSUB"
18
+ super
19
+ end
20
+
21
+ def header
22
+ Header.new *(font.unpack seek: offset, bytes: 10, tmpl: 'n5')
23
+ end
24
+
25
+ def feature_list_offset
26
+ header.feature_offset + offset
27
+ end
28
+
29
+ def feature_list_count
30
+ font.unpack(seek: feature_list_offset, bytes: 2, tmpl: 'n').first
31
+ end
32
+
33
+ def feature_list
34
+ feature_list_count.times.map do
35
+ FeatureRecord.new *(font.unpack bytes: 6, tmpl: 'a4n')
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Tipo
2
+ VERSION = "0.0.2"
3
+ end
Binary file
@@ -0,0 +1,28 @@
1
+ require 'tipo'
2
+
3
+ include Tipo
4
+
5
+ describe Font do
6
+ let(:font) { Font.new 'spec/fixtures/hobo.otf' }
7
+ it "reads a file as ascii-8bit" do
8
+ font.encoding.should eq Encoding::ASCII_8BIT
9
+ end
10
+
11
+ it "delegates seek and tell to the file" do
12
+ font.seek 3
13
+ font.tell.should eq 3
14
+ end
15
+
16
+ it "gets a chunk of data" do
17
+ font.chunk(bytes: 4).should eq "OTTO"
18
+ end
19
+
20
+ it "gets chunks in sucessions" do
21
+ font.chunk(bytes: 2).should eq "OT"
22
+ font.chunk(bytes: 2).should eq "TO"
23
+ end
24
+
25
+ it "can unpack a chunk with a template" do
26
+ font.unpack(seek: 0, bytes: 12, tmpl: 'a4n4').should eq ["OTTO", 13, 128, 3, 80]
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ require 'tipo'
2
+
3
+ include Tipo
4
+
5
+ describe Header do
6
+ context "contains the information located in the header" do
7
+ let(:font) { Font.new 'spec/fixtures/hobo.otf' }
8
+ let(:header) { Header.new font }
9
+
10
+ it "reads the offset table" do
11
+ header.offset_table.version.should eq "OTTO"
12
+ end
13
+
14
+ it "reads the table record entries" do
15
+ header.table_records.first.tag.should eq "BASE"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ require 'tipo'
2
+
3
+ include Tipo
4
+ include Table
5
+
6
+ describe Base do
7
+ let(:font) { Font.new 'spec/fixtures/hobo.otf' }
8
+ let(:header) { Header.new font }
9
+ let(:table) { Base.new header, font }
10
+
11
+ context "navigating" do
12
+ it "has a name" do
13
+ table.name = "GPOS"
14
+ table.name.should eq "GPOS"
15
+ end
16
+
17
+ it "has a start position" do
18
+ table.name = "GSUB"
19
+ table.offset.should eq 23376
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,38 @@
1
+ require 'tipo'
2
+
3
+ include Tipo
4
+ include Table
5
+
6
+ describe Naming do
7
+ let(:font) { Font.new 'spec/fixtures/hobo.otf' }
8
+ let(:header) { Header.new font }
9
+ let(:name) { Naming.new header, font }
10
+
11
+ context "sorta header information" do
12
+ it "has a format" do
13
+ name.header.format.should eq 0
14
+ end
15
+
16
+ it "has a count" do
17
+ name.header.count.should eq 24
18
+ end
19
+
20
+ it "has a string storage offset" do
21
+ name.header.storage_offset.should eq 294
22
+ end
23
+ end
24
+
25
+ context "name records" do
26
+ it "has a family name" do
27
+ name.family_name.should eq "Hobo Std"
28
+ end
29
+
30
+ it "has a style" do
31
+ name.style.should eq "Regular"
32
+ end
33
+
34
+ it "has a name" do
35
+ name.name.should eq "Hobo Std Medium"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ require 'tipo'
2
+
3
+ include Tipo
4
+ include Table
5
+
6
+ describe Substitution do
7
+ let(:font) { Font.new 'spec/fixtures/hobo.otf' }
8
+ let(:header) { Header.new font }
9
+ let(:gsub) { Substitution.new header, font }
10
+
11
+ context "information" do
12
+ it "has a header" do
13
+ gsub.header.version.should eq "1.0"
14
+ end
15
+
16
+ it "has a feature list count" do
17
+ gsub.feature_list_count.should eq 10
18
+ end
19
+
20
+ it "has a list of features" do
21
+ gsub.feature_list.map do |feature|
22
+ feature.tag
23
+ end.uniq.should eq ["aalt", "frac", "liga", "ordn", "sups"]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require 'tipo'
2
+
3
+ include Tipo
4
+
5
+ describe Info do
6
+ it "takes a file" do
7
+ info = Info.new 'spec/fixtures/hobo.otf'
8
+ info.font.class.should eq Font
9
+ end
10
+
11
+ it "return a naming table" do
12
+ info = Info.new 'spec/fixtures/hobo.otf'
13
+ info.naming.class.should eq Table::Naming
14
+ info.substitution.class.should eq Table::Substitution
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tipo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tipo"
8
+ spec.version = Tipo::VERSION
9
+ spec.authors = ["Hugo Bastien"]
10
+ spec.email = ["hugobast@gmail.com"]
11
+ spec.description = %q{Pure Ruby OpenType font file reader}
12
+ spec.summary = %q{Incomplete implementation: Reads header tables, GSUB table and name table}
13
+ spec.homepage = "https://github.com/hugobast/tipo"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tipo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hugo Bastien
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Pure Ruby OpenType font file reader
79
+ email:
80
+ - hugobast@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/tipo.rb
92
+ - lib/tipo/font.rb
93
+ - lib/tipo/header.rb
94
+ - lib/tipo/table.rb
95
+ - lib/tipo/table/base.rb
96
+ - lib/tipo/table/naming.rb
97
+ - lib/tipo/table/substitution.rb
98
+ - lib/tipo/version.rb
99
+ - spec/fixtures/hobo.otf
100
+ - spec/tipo/font_spec.rb
101
+ - spec/tipo/header_spec.rb
102
+ - spec/tipo/table/base_spec.rb
103
+ - spec/tipo/table/naming_spec.rb
104
+ - spec/tipo/table/substitution_spec.rb
105
+ - spec/tipo_spec.rb
106
+ - tipo.gemspec
107
+ homepage: https://github.com/hugobast/tipo
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.23
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: ! 'Incomplete implementation: Reads header tables, GSUB table and name table'
132
+ test_files:
133
+ - spec/fixtures/hobo.otf
134
+ - spec/tipo/font_spec.rb
135
+ - spec/tipo/header_spec.rb
136
+ - spec/tipo/table/base_spec.rb
137
+ - spec/tipo/table/naming_spec.rb
138
+ - spec/tipo/table/substitution_spec.rb
139
+ - spec/tipo_spec.rb