File: name.rb

package info (click to toggle)
ruby-ttfunk 1.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 916 kB
  • sloc: ruby: 1,719; makefile: 16
file content (125 lines) | stat: -rw-r--r-- 3,750 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'ttfunk/table'

module TTFunk
  class Table
    class Name < Table
      class String < ::String
        attr_reader :platform_id
        attr_reader :encoding_id
        attr_reader :language_id

        def initialize(text, platform_id, encoding_id, language_id)
          super(text)
          @platform_id = platform_id
          @encoding_id = encoding_id
          @language_id = language_id
        end

        def strip_extended
          stripped = gsub(/[\x00-\x19\x80-\xff]/n, "")
          stripped = "[not-postscript]" if stripped.empty?
          return stripped
        end
      end

      attr_reader :strings

      attr_reader :copyright
      attr_reader :font_family
      attr_reader :font_subfamily
      attr_reader :unique_subfamily
      attr_reader :font_name
      attr_reader :version
      attr_reader :trademark
      attr_reader :manufacturer
      attr_reader :designer
      attr_reader :description
      attr_reader :vendor_url
      attr_reader :designer_url
      attr_reader :license
      attr_reader :license_url
      attr_reader :preferred_family
      attr_reader :preferred_subfamily
      attr_reader :compatible_full
      attr_reader :sample_text

      @@subset_tag = "AAAAAA"

      def self.encode(names)
        tag = @@subset_tag.dup
        @@subset_tag.succ!

        postscript_name = Name::String.new("#{tag}+#{names.postscript_name}", 1, 0, 0)

        strings = names.strings.dup
        strings[6] = [postscript_name]
        str_count = strings.inject(0) { |sum, (id, list)| sum + list.length }

        table = [0, str_count, 6 + 12 * str_count].pack("n*")
        strtable = ""

        strings.each do |id, list|
          list.each do |string|
            table << [string.platform_id, string.encoding_id, string.language_id, id, string.length, strtable.length].pack("n*")
            strtable << string
          end
        end

        table << strtable
      end

      def postscript_name
        return @postscript_name if @postscript_name
        font_family.first || "unnamed"
      end

      private

        def parse!
          format, count, string_offset = read(6, "n*")

          entries = []
          count.times do
            platform, encoding, language, id, length, start_offset = read(12, "n*")
            entries << {
              :platform_id => platform,
              :encoding_id => encoding,
              :language_id => language,
              :name_id => id,
              :length => length,
              :offset => offset + string_offset + start_offset
            }
          end

          @strings = Hash.new { |h,k| h[k] = [] }

          count.times do |i|
            io.pos = entries[i][:offset]
            text = io.read(entries[i][:length])
            @strings[entries[i][:name_id]] << Name::String.new(text,
              entries[i][:platform_id], entries[i][:encoding_id], entries[i][:language_id])
          end

          @copyright = @strings[0]
          @font_family = @strings[1]
          @font_subfamily = @strings[2]
          @unique_subfamily = @strings[3]
          @font_name = @strings[4]
          @version = @strings[5]
          @postscript_name = @strings[6].first.strip_extended # should only be ONE postscript name
          @trademark = @strings[7]
          @manufacturer = @strings[8]
          @designer = @strings[9]
          @description = @strings[10]
          @vendor_url = @strings[11]
          @designer_url = @strings[12]
          @license = @strings[13]
          @license_url = @strings[14]
          @preferred_family = @strings[15]
          @preferred_subfamily = @strings[17]
          @compatible_full = @strings[18]
          @sample_text = @strings[19]
        end
    end
  end
end