Module: Pdfrb::Font::Encoding

Defined in:
lib/pdfrb/font/encoding.rb,
lib/pdfrb/font/encoding/pdf_doc_encoding.rb,
lib/pdfrb/font/encoding/standard_encoding.rb,
lib/pdfrb/font/encoding/win_ansi_encoding.rb,
lib/pdfrb/font/encoding/mac_roman_encoding.rb,
lib/pdfrb/font/encoding/zapf_dingbats_encoding.rb

Defined Under Namespace

Modules: MacRomanEncoding, PDFDocEncoding, StandardEncoding, WinAnsiEncoding, ZapfDingbatsEncoding

Class Method Summary collapse

Class Method Details

.build_reverse_table(name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pdfrb/font/encoding.rb', line 50

def build_reverse_table(name)
  table = table_for(name)
  return {} unless table

  if table.is_a?(::Array)
    table.each_with_index.with_object({}) do |(cp, byte), h|
      h[cp] = byte if cp
    end
  else
    table.each_with_object({}) { |(byte, cp), h| h[cp] = byte }
  end
end

.decode(encoding_name, bytes) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/pdfrb/font/encoding.rb', line 13

def decode(encoding_name, bytes)
  table = table_for(encoding_name)
  return bytes unless table

  bytes.each_byte.with_object(+"") do |b, buf|
    cp = table.is_a?(::Array) ? table[b] : table[b.to_s.to_sym]
    buf << (cp ? [cp].pack("U") : "?")
  end.encode("UTF-8")
end

.encode(encoding_name, text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdfrb/font/encoding.rb', line 23

def encode(encoding_name, text)
  table = table_for(encoding_name)
  return text.to_s.b unless table

  reverse = reverse_table_for(encoding_name)
  text.to_s.each_char.with_object(+"") do |ch, buf|
    cp = ch.ord
    byte = reverse[cp] || (cp < 0x80 ? cp : nil)
    buf << [byte || 0x3F].pack("C")
  end.b
end

.reverse_table_for(name) ⇒ Object



45
46
47
48
# File 'lib/pdfrb/font/encoding.rb', line 45

def reverse_table_for(name)
  @reverse_tables ||= {}
  @reverse_tables[name] ||= build_reverse_table(name)
end

.table_for(name) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/pdfrb/font/encoding.rb', line 35

def table_for(name)
  case name.to_sym
  when :WinAnsiEncoding then WinAnsiEncoding::TABLE
  when :MacRomanEncoding then MacRomanEncoding::TABLE
  when :StandardEncoding then StandardEncoding::TABLE
  when :PDFDocEncoding then PDFDocEncoding::TABLE
  when :ZapfDingbatsEncoding then ZapfDingbatsEncoding::TABLE
  end
end