freetype 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 753294da701bc4144a7be8a3dead8f738b124cf2
4
+ data.tar.gz: 36af82be6476778d84a738dbfedd136ece627ae6
5
+ SHA512:
6
+ metadata.gz: 1ea7b0f90ef2cfec35d4c2e5964334cc36bae5b1589d893a2552039dd604f0c439362d3e9e196898a9486ea101ee9b5a13fb454d45d75eb25673c28f098c3a8a
7
+ data.tar.gz: 973d1397fc88fe29d576dfdbff4f02b72884386558a271ae0c04505f44a571bb09a0f82acd9d6570ecf6f94a80a0f0310bf5f185b8616ab85ad0bdd0aeb1fb6d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.7
5
+ - 2.2.3
6
+ - 2.3.0-preview1
7
+ notifications:
8
+ email: false
9
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in freetype.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ksss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,121 @@
1
+ # FreeType
2
+
3
+ FreeType is freetype wrapper using by ffi
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'freetype'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install freetype
20
+
21
+ ## Usage
22
+
23
+ ### Low Level API
24
+
25
+ ```ruby
26
+ require 'freetype/c'
27
+
28
+ include FreeType::C
29
+
30
+ library = FFI::MemoryPointer.new(:pointer)
31
+ err = FT_Init_FreeType(library)
32
+ raise FreeType::Error.find(err) unless err == 0
33
+
34
+ face = FFI::MemoryPointer.new(:pointer)
35
+ err = FT_New_Face(library.get_pointer(0), 'font.otf', 0, face)
36
+ raise FreeType::Error.find(err) unless err == 0
37
+
38
+ err = FT_Set_Char_Size(face.get_pointer(0), 0, 32 * 32, 300, 300)
39
+ raise FreeType::Error.find(err) unless err == 0
40
+
41
+ err = FT_Load_Char(face.get_pointer(0), 'a'.ord, FT_LOAD_DEFAULT)
42
+ raise FreeType::Error.find(err) unless err == 0
43
+
44
+ face_rec = FT_FaceRec.new face.get_pointer(0)
45
+ outline = face_rec[:glyph][:outline]
46
+
47
+ points = outline[:n_points].times.map do |i|
48
+ FT_Vector.new(outline[:points] + i * FT_Vector.size)
49
+ end
50
+
51
+ tags = outline[:tags].get_array_of_char(0, outline[:n_points])
52
+
53
+ points.zip(tags).each do |(point, tag)|
54
+ p point[:x] #=> 10
55
+ p point[:y] #=> 24
56
+ p tag #=> -1
57
+ end
58
+
59
+ v = FT_Vector.new
60
+ err = FT_Get_Kerning(
61
+ face.get_pointer(0),
62
+ FT_Get_Char_Index(face.get_pointer(0), 'A'.ord),
63
+ FT_Get_Char_Index(face.get_pointer(0), 'W'.ord),
64
+ :FT_KERNING_UNFITTED,
65
+ v
66
+ )
67
+ p v[:x] #=> -10
68
+ p v[:y] #=> 0
69
+
70
+ err = FT_Done_Face(face.get_pointer(0))
71
+ raise FreeType::Error.find(err) unless err == 0
72
+
73
+ err = FT_Done_Library(library.get_pointer(0))
74
+ raise FreeType::Error.find(err) unless err == 0
75
+ ```
76
+
77
+ ### High Level API
78
+
79
+ ```ruby
80
+ require 'freetype/api'
81
+
82
+ include FreeType::API
83
+
84
+ Library.open do |lib|
85
+ lib.face_open('font.ttf') do |face|
86
+ face.set_char_size(0, 32 * 32, 300, 300)
87
+ outline = face.outline('a')
88
+ p outline.points #=> [#<FreeType::API::Outline tag=-1 x= 10 y=24>, ...]
89
+ p face.kerning_unfitted('A', 'W') #=> #<FreeType::API::Vector x=-10 y=0>
90
+ end
91
+ end
92
+ ```
93
+
94
+ ### Use All
95
+
96
+ ```ruby
97
+ require 'freetype'
98
+
99
+ FreeType::API::Library.open do |lib|
100
+ face = FFI::MemoryPointer.new(:pointer)
101
+ FreeType::C::FT_New_Face(lib.pointer, 'font.otf', 0, face)
102
+ end
103
+ ```
104
+
105
+ ## Development
106
+
107
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
108
+
109
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/freetype. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
118
+
119
+ ## Reference
120
+
121
+ FreeType: http://www.freetype.org/
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :test do
4
+ sh 'rgot -v'
5
+ end
6
+
7
+ task default: [:test]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'freetype/c'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
Binary file
@@ -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 'freetype/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'freetype'
8
+ spec.version = FreeType::VERSION
9
+ spec.authors = ['ksss']
10
+ spec.email = ['co000ri@gmail.com']
11
+
12
+ spec.summary = 'FreeType binding by ffi'
13
+ spec.description = 'FreeType binding by ffi'
14
+ spec.homepage = 'https://github.com/ksss/freetype'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'ffi'
23
+ spec.add_development_dependency 'bundler', '~> 1.10'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rgot'
26
+ end
@@ -0,0 +1,5 @@
1
+ module FreeType
2
+ require 'freetype/c'
3
+ require 'freetype/api'
4
+ require 'freetype/version'
5
+ end
@@ -0,0 +1,296 @@
1
+ require 'freetype/c'
2
+ require 'freetype/error'
3
+
4
+ module FreeType
5
+ # high level API for freetype wrapping by FFI
6
+ module API
7
+ module IOInterface
8
+ def open(*args)
9
+ i = new(*args)
10
+ if block_given?
11
+ begin
12
+ yield i
13
+ ensure
14
+ i.close
15
+ end
16
+ else
17
+ i
18
+ end
19
+ end
20
+ end
21
+
22
+ class Library
23
+ extend IOInterface
24
+ include C
25
+
26
+ def initialize
27
+ @library = ::FFI::MemoryPointer.new(:pointer)
28
+ err = FT_Init_FreeType(@library)
29
+ raise FreeType::Error.find(err) unless err == 0
30
+ end
31
+
32
+ def face_open(font)
33
+ Face.open(pointer, font) do |f|
34
+ yield f
35
+ end
36
+ end
37
+
38
+ def pointer
39
+ @library.get_pointer(0)
40
+ end
41
+
42
+ def version
43
+ amajor = ::FFI::MemoryPointer.new(:int)
44
+ aminor = ::FFI::MemoryPointer.new(:int)
45
+ apatch = ::FFI::MemoryPointer.new(:int)
46
+ FT_Library_Version(@library.get_pointer(0), amajor, aminor, apatch)
47
+ "#{amajor.get_int(0)}.#{aminor.get_int(0)}.#{apatch.get_int(0)}"
48
+ end
49
+
50
+ def close
51
+ err = FT_Done_Library(@library.get_pointer(0))
52
+ raise FreeType::Error.find(err) unless err == 0
53
+ @library.free
54
+ end
55
+ end
56
+
57
+ class Face
58
+ extend IOInterface
59
+ include C
60
+
61
+ attr_reader :font_path
62
+ def initialize(library, font_path)
63
+ @library = library
64
+ @font_path = font_path
65
+ @outline = nil
66
+ f = ::FFI::MemoryPointer.new(:pointer)
67
+ err = FT_New_Face(@library, @font_path, 0, f)
68
+ raise FreeType::Error.find(err) unless err == 0
69
+ @face = FT_FaceRec.new(f.get_pointer(0))
70
+ end
71
+
72
+ def raw
73
+ @face
74
+ end
75
+
76
+ def select_charmap(enc_code)
77
+ err = FT_Select_Charmap(@face, enc_code)
78
+ raise FreeType::Error.find(err) unless err == 0
79
+ end
80
+
81
+ def set_char_size(char_width, char_height, horz_resolution, vert_resolution)
82
+ err = FT_Set_Char_Size(@face, char_width, char_height, horz_resolution, vert_resolution)
83
+ raise FreeType::Error.find(err) unless err == 0
84
+ end
85
+
86
+ # TODO: Should be use FT_Get_Glyph
87
+ def notdef
88
+ glyph("\x00".freeze)
89
+ end
90
+
91
+ # TODO
92
+ # Should be use FT_Get_Glyph and FT_Done_Glyph
93
+ # Because return value will be change after call FT_Load_Char
94
+ def glyph(char)
95
+ load_char(char)
96
+ Glyph.new(@face[:glyph])
97
+ end
98
+
99
+ def char_index(char)
100
+ FT_Get_Char_Index(@face, char.ord)
101
+ end
102
+
103
+ def line_height
104
+ @face[:size][:metrics][:height]
105
+ end
106
+
107
+ def bbox
108
+ bbox = @face[:bbox]
109
+ BBox.new(bbox[:xMin], bbox[:xMax], bbox[:yMin], bbox[:yMax])
110
+ end
111
+
112
+ def kerning(before_char, after_char)
113
+ get_kerning(before_char, after_char, :FT_KERNING_DEFAULT)
114
+ end
115
+ alias_method :kerning_default, :kerning
116
+
117
+ def kerning_unfitted(before_char, after_char)
118
+ get_kerning(before_char, after_char, :FT_KERNING_UNFITTED)
119
+ end
120
+
121
+ def kerning_unscaled(before_char, after_char)
122
+ get_kerning(before_char, after_char, :FT_KERNING_UNSCALED)
123
+ end
124
+
125
+ def close
126
+ err = FT_Done_Face(@face)
127
+ raise FreeType::Error.find(err) unless err == 0
128
+ end
129
+
130
+ private
131
+
132
+ def get_kerning(before_char, after_char, kerning_mode)
133
+ if before_char.nil? || before_char == ''.freeze || after_char.nil? || after_char == ''.freeze
134
+ return Vector.new(0, 0)
135
+ end
136
+
137
+ v = FT_Vector.new
138
+ err = FT_Get_Kerning(
139
+ @face,
140
+ char_index(before_char),
141
+ char_index(after_char),
142
+ kerning_mode,
143
+ v,
144
+ )
145
+ raise FreeType::Error.find(err) unless err == 0
146
+
147
+ Vector.new(v[:x], v[:y])
148
+ end
149
+
150
+ def load_char(char)
151
+ err = FT_Load_Char(@face, char.ord, FreeType::C::FT_LOAD_DEFAULT)
152
+ unless err == 0
153
+ e = FreeType::Error.find(err)
154
+ if FreeType::Error::Invalid_Size_Handle === e
155
+ warn 'should be call FT_Set_Char_Size before FT_Load_Char'
156
+ end
157
+ raise e
158
+ end
159
+ end
160
+ end
161
+
162
+ class Glyph
163
+ def initialize(glyph)
164
+ @glyph = glyph
165
+ end
166
+
167
+ def raw
168
+ @glyph
169
+ end
170
+
171
+ def metrics
172
+ @glyph[:metrics]
173
+ end
174
+
175
+ def outline
176
+ Outline.new(@glyph[:outline])
177
+ end
178
+
179
+ def space_width
180
+ @glyph[:metrics][:horiAdvance]
181
+ end
182
+ end
183
+
184
+ class Outline
185
+ include C
186
+
187
+ def initialize(outline)
188
+ @outline = outline
189
+ end
190
+
191
+ def raw
192
+ @outline
193
+ end
194
+
195
+ def points
196
+ @points ||= begin
197
+ points = @outline[:n_points].times.map do |i|
198
+ FT_Vector.new(@outline[:points] + i * FT_Vector.size)
199
+ end
200
+ points.zip(tags).map do |(point, tag)|
201
+ Point.new(tag, point[:x], point[:y])
202
+ end
203
+ end
204
+ end
205
+
206
+ def contours
207
+ return [] if @outline[:n_contours] == 0
208
+ @outline[:contours].get_array_of_short(0, @outline[:n_contours])
209
+ end
210
+
211
+ def tags
212
+ return [] if @outline[:n_points] == 0
213
+ @outline[:tags].get_array_of_char(0, @outline[:n_points])
214
+ end
215
+
216
+ def to_svg_path
217
+ end_ptd_of_counts = contours
218
+ contours = []
219
+ contour = []
220
+ points.each.with_index do |point, index|
221
+ contour << point
222
+ if index == end_ptd_of_counts.first
223
+ end_ptd_of_counts.shift
224
+ contours << contour
225
+ contour = []
226
+ end
227
+ end
228
+
229
+ path = []
230
+ contours.each do |contour|
231
+ first_pt = contour.first
232
+ last_pt = contour.last
233
+ curve_pt = nil
234
+ start = 0
235
+ if first_pt.on_curve?
236
+ curve_pt = nil
237
+ start = 1
238
+ else
239
+ if last_pt.on_curve?
240
+ first_pt = last_pt
241
+ else
242
+ first_pt = Point.new(0, (first_pt.x + last_pt.x) / 2, (first_pt.y + last_pt.y) / 2)
243
+ end
244
+ curve_pt = first_pt
245
+ end
246
+ path << ['M', first_pt.x, -first_pt.y]
247
+
248
+ prev_pt = nil
249
+ (start...contour.length).each do |j|
250
+ pt = contour[j]
251
+ prev_pt = if j == 0
252
+ first_pt
253
+ else
254
+ contour[j - 1]
255
+ end
256
+
257
+ if prev_pt.on_curve? && pt.on_curve?
258
+ path << ['L', pt.x, -pt.y]
259
+ elsif prev_pt.on_curve? && !pt.on_curve?
260
+ curve_pt = pt
261
+ elsif !prev_pt.on_curve? && !pt.on_curve?
262
+ path << ['Q', prev_pt.x, -prev_pt.y, (prev_pt.x + pt.x) / 2, -((prev_pt.y + pt.y) / 2)]
263
+ curve_pt = pt
264
+ elsif !prev_pt.on_curve? && pt.on_curve?
265
+ path << ['Q', curve_pt.x, -curve_pt.y, pt.x, -pt.y]
266
+ curve_pt = nil
267
+ else
268
+ raise
269
+ end
270
+ end
271
+
272
+ next unless first_pt != last_pt
273
+ if curve_pt
274
+ path << ['Q', curve_pt.x, -curve_pt.y, first_pt.x, -first_pt.y]
275
+ else
276
+ path << ['L', first_pt.x, -first_pt.y]
277
+ end
278
+ end
279
+ path << ['z'] if 0 < path.length
280
+
281
+ path.map { |(command, *args)|
282
+ "#{command}#{args.join(' ')}"
283
+ }.join('')
284
+ end
285
+ end
286
+
287
+ Point = Struct.new(:tag, :x, :y) do
288
+ def on_curve?
289
+ tag & 0x01 != 0
290
+ end
291
+ end
292
+
293
+ Vector = Struct.new(:x, :y)
294
+ BBox = Struct.new(:x_min, :x_max, :y_min, :y_max)
295
+ end
296
+ end