This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
## 0.2.2
|
||||
|
||||
* The gem was missing
|
||||
|
||||
## 0.2.1
|
||||
|
||||
* Fixed rdoc tasks, replaced Test::Unit with Minitest (Thanks to @yob, @strzibny for changes)
|
||||
* Removed jeweler dependency
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
Copyright (c) 2009 Jan Krutisch
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,20 @@
|
||||
= afm
|
||||
|
||||
A very simple library to read Adobe Font Metrics files (afm).
|
||||
|
||||
Currently simply parses the file and saves it in a few attributes.
|
||||
|
||||
|
||||
== Note on Patches/Pull Requests
|
||||
|
||||
* Fork the project.
|
||||
* Make your feature addition or bug fix.
|
||||
* Add tests for it. This is important so I don't break it in a
|
||||
future version unintentionally.
|
||||
* Commit, do not mess with rakefile, version, or history.
|
||||
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
||||
* Send me a pull request. Bonus points for topic branches.
|
||||
|
||||
== Copyright
|
||||
|
||||
Copyright (c) 2010 Jan Krutisch. See LICENSE for details.
|
||||
@@ -0,0 +1,23 @@
|
||||
require 'rubygems'
|
||||
require 'bundler/setup'
|
||||
require 'rake'
|
||||
|
||||
require 'rake/testtask'
|
||||
Rake::TestTask.new(:test) do |test|
|
||||
test.libs << 'lib' << 'test'
|
||||
test.pattern = 'test/**/test_*.rb'
|
||||
test.verbose = true
|
||||
end
|
||||
|
||||
task :default => :test
|
||||
|
||||
require 'rdoc/task'
|
||||
|
||||
RDoc::Task.new do |rdoc|
|
||||
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
||||
|
||||
rdoc.rdoc_dir = 'rdoc'
|
||||
rdoc.title = "afm #{version}"
|
||||
rdoc.rdoc_files.include('README*')
|
||||
rdoc.rdoc_files.include('lib/**/*.rb')
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
0.2.2
|
||||
@@ -0,0 +1,105 @@
|
||||
module AFM
|
||||
|
||||
ISO_LATIN1_ENCODING = %w(
|
||||
.notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
|
||||
.notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
|
||||
.notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
|
||||
.notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef space
|
||||
exclam quotedbl numbersign dollar percent ampersand quoteright
|
||||
parenleft parenright asterisk plus comma minus period slash zero one
|
||||
two three four five six seven eight nine colon semicolon less equal
|
||||
greater question at A B C D E F G H I J K L M N O P Q R S
|
||||
T U V W X Y Z bracketleft backslash bracketright asciicircum
|
||||
underscore quoteleft a b c d e f g h i j k l m n o p q r s
|
||||
t u v w x y z braceleft bar braceright asciitilde .notdef .notdef
|
||||
.notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
|
||||
.notdef .notdef .notdef .notdef .notdef .notdef .notdef dotlessi grave
|
||||
acute circumflex tilde macron breve dotaccent dieresis .notdef ring
|
||||
cedilla .notdef hungarumlaut ogonek caron space exclamdown cent
|
||||
sterling currency yen brokenbar section dieresis copyright ordfeminine
|
||||
guillemotleft logicalnot hyphen registered macron degree plusminus
|
||||
twosuperior threesuperior acute mu paragraph periodcentered cedilla
|
||||
onesuperior ordmasculine guillemotright onequarter onehalf threequarters
|
||||
questiondown Agrave Aacute Acircumflex Atilde Adieresis Aring AE
|
||||
Ccedilla Egrave Eacute Ecircumflex Edieresis Igrave Iacute Icircumflex
|
||||
Idieresis Eth Ntilde Ograve Oacute Ocircumflex Otilde Odieresis
|
||||
multiply Oslash Ugrave Uacute Ucircumflex Udieresis Yacute Thorn
|
||||
germandbls agrave aacute acircumflex atilde adieresis aring ae
|
||||
ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex
|
||||
idieresis eth ntilde ograve oacute ocircumflex otilde odieresis divide
|
||||
oslash ugrave uacute ucircumflex udieresis yacute thorn ydieresis
|
||||
)
|
||||
|
||||
|
||||
class Font
|
||||
attr_reader :metadata, :char_metrics, :char_metrics_by_code, :kern_pairs
|
||||
|
||||
# Loading a Font Metrics file by absolute path (no automatic font path resolution)
|
||||
def initialize(filename)
|
||||
@metadata = {}
|
||||
@char_metrics = {}
|
||||
@char_metrics_by_code = {}
|
||||
@kern_pairs = []
|
||||
File.open(filename) do |file|
|
||||
mode = :meta
|
||||
file.each_line do |line|
|
||||
case(line)
|
||||
when /^StartFontMetrics/ ; mode = :meta
|
||||
when /^StartCharMetrics/ ; mode = :char_metrics
|
||||
when /^EndCharMetrics/ ; mode = :meta
|
||||
when /^StartKernData/ ; mode = :kern_data
|
||||
when /^StartKernPairs/ ; mode = :kern_pairs
|
||||
when /^EndKernPairs/ ; mode = :kern_data
|
||||
when /^EndKernData/ ; mode = :meta
|
||||
else
|
||||
case(mode)
|
||||
when :meta
|
||||
if match = line.match(/^([\w]+) (.*)$/)
|
||||
@metadata[match[1]] = match[2]
|
||||
end
|
||||
when :char_metrics
|
||||
metrics = {}
|
||||
metrics[:charcode] = match[1].to_i if match = line.match(/C (-?\d+) *?;/)
|
||||
metrics[:wx] = match[1].to_i if match = line.match(/WX (-?\d+) *?;/)
|
||||
metrics[:name] = match[1] if match = line.match(/N ([.\w]+) *?;/)
|
||||
if match = line.match(/B (-?\d+) (-?\d+) (-?\d+) (-?\d+) *?;/)
|
||||
metrics[:boundingbox] = [match[1].to_i, match[2].to_i, match[3].to_i, match[4].to_i]
|
||||
end
|
||||
@char_metrics[metrics[:name]] = metrics if metrics[:name]
|
||||
@char_metrics_by_code[metrics[:charcode]] = metrics if metrics[:charcode] && metrics[:charcode] > 0
|
||||
when :kern_pairs
|
||||
if match = line.match(/^KPX ([.\w]+) ([.\w]+) (-?\d+)$/)
|
||||
@kern_pairs << [match[1], match[2], match[3].to_i]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# alias for new()
|
||||
def self.from_file(file)
|
||||
self.new(file)
|
||||
end
|
||||
|
||||
#
|
||||
# Get metadata by key
|
||||
def [](key)
|
||||
@metadata[key]
|
||||
end
|
||||
#
|
||||
# Get metrics for character. Takes an integer (charcode) or
|
||||
# a one-char string. currently works only for Latin1 strings,
|
||||
# since we only have a chartable for the Latin1 charset so far.
|
||||
# (shamelessly stolen from AFM.pm by Gisle Aas)
|
||||
def metrics_for(char)
|
||||
glyph = if (char.kind_of?(Integer))
|
||||
ISO_LATIN1_ENCODING[char]
|
||||
else
|
||||
ISO_LATIN1_ENCODING[char.unpack("C*").first]
|
||||
end
|
||||
@char_metrics[glyph]
|
||||
end
|
||||
end
|
||||
end
|
||||
+1783
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,4 @@
|
||||
require 'rubygems'
|
||||
require 'bundler/setup'
|
||||
require 'minitest/autorun'
|
||||
require 'afm'
|
||||
@@ -0,0 +1,32 @@
|
||||
require 'helper'
|
||||
|
||||
class TestAfm < Minitest::Test
|
||||
|
||||
def setup
|
||||
@font = AFM::Font.new(File.join(File.dirname(__FILE__), 'fixtures', 'Vera.afm'))
|
||||
end
|
||||
|
||||
def test_should_set_metadata
|
||||
assert_equal "BitstreamVeraSans-Roman", @font.metadata['FontName']
|
||||
assert_equal "BitstreamVeraSans-Roman", @font['FontName']
|
||||
end
|
||||
|
||||
def test_should_set_char_metrics
|
||||
assert_equal 400, @font.char_metrics['exclam'][:wx]
|
||||
assert_equal [85, -131, 310, 758], @font.char_metrics['parenleft'][:boundingbox]
|
||||
end
|
||||
|
||||
def test_should_set_char_metrics_by_code
|
||||
assert_equal 400, @font.char_metrics_by_code[33][:wx]
|
||||
assert_equal [85, -131, 310, 758], @font.char_metrics_by_code[40][:boundingbox]
|
||||
end
|
||||
|
||||
def test_should_get_char_metrics_by_char
|
||||
assert_equal 400, @font.metrics_for("!")[:wx]
|
||||
end
|
||||
|
||||
def test_open_font_with_alternative_method
|
||||
assert !AFM::Font.from_file(File.join(File.dirname(__FILE__), 'fixtures', 'Vera.afm')).nil?
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user