Class: TTFunk::Subset::Unicode8Bit

Inherits:
Base
  • Object
show all
Defined in:
lib/ttfunk/subset/unicode_8bit.rb

Overview

An 8-bit Unicode-based subset. It can include any Unicode character but limits number of characters so that the could be encoded by a single byte.

Constant Summary

Constants inherited from Base

Base::MICROSOFT_PLATFORM_ID, Base::MS_SYMBOL_ENCODING_ID

Instance Attribute Summary

Attributes inherited from Base

#original

Instance Method Summary collapse

Methods inherited from Base

#collect_glyphs, #encode, #encoder_klass, #glyphs, #microsoft_symbol?, #new_to_old_glyph, #old_to_new_glyph, #unicode_cmap

Constructor Details

#initialize(original) ⇒ Unicode8Bit

Returns a new instance of Unicode8Bit.

Parameters:



12
13
14
15
16
17
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 12

def initialize(original)
  super
  @subset = { 0x20 => 0x20 }
  @unicodes = { 0x20 => 0x20 }
  @next = 0x21 # apparently, PDF's don't like to use chars between 0-31
end

Instance Method Details

#covers?(character) ⇒ Boolean

Can this subset include the character?

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Boolean)


49
50
51
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 49

def covers?(character)
  @unicodes.key?(character) || @next < 256
end

#from_unicode(character) ⇒ Integer

Get character code for Unicode codepoint.

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Integer)


65
66
67
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 65

def from_unicode(character)
  @unicodes[character]
end

#includes?(character) ⇒ Boolean

Does this subset actually has the character?

Parameters:

  • character (Integer)

    Unicode codepoint

Returns:

  • (Boolean)


57
58
59
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 57

def includes?(character)
  @unicodes.key?(character)
end

#new_cmap_tableTTFunk::Table::Cmap

Get ‘cmap` table for this subset.

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 72

def new_cmap_table
  @new_cmap_table ||=
    begin
      mapping =
        @subset.each_with_object({}) do |(code, unicode), map|
          map[code] = unicode_cmap[unicode]
          map
        end

      # since we're mapping a subset of the unicode glyphs into an
      # arbitrary 256-character space, the actual encoding we're
      # using is irrelevant. We choose MacRoman because it's a 256-character
      # encoding that happens to be well-supported in both TTF and
      # PDF formats.
      TTFunk::Table::Cmap.encode(mapping, :mac_roman)
    end
end

#original_glyph_idsArray<Integer>

Get the list of Glyph IDs from the original font that are in this subset.

Returns:

  • (Array<Integer>)


94
95
96
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 94

def original_glyph_ids
  ([0] + @unicodes.keys.map { |unicode| unicode_cmap[unicode] }).uniq.sort
end

#to_unicode_mapHash{Integer => Integer}

Get a mapping from this subset to Unicode.

Returns:

  • (Hash{Integer => Integer})


29
30
31
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 29

def to_unicode_map
  @subset.dup
end

#unicode?true

Is this a Unicode-based subset?

Returns:

  • (true)


22
23
24
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 22

def unicode?
  true
end

#use(character) ⇒ void

This method returns an undefined value.

Add a character to subset.

Parameters:

  • character (Integer)

    Unicode codepoint



37
38
39
40
41
42
43
# File 'lib/ttfunk/subset/unicode_8bit.rb', line 37

def use(character)
  unless @unicodes.key?(character)
    @subset[@next] = character
    @unicodes[character] = @next
    @next += 1
  end
end