Class: HexaPDF::CLI::Fonts

Inherits:
Command
  • Object
show all
Defined in:
lib/hexapdf/cli/fonts.rb

Overview

Lists fonts from a PDF file.

See: HexaPDF::Type::Font

Instance Method Summary collapse

Methods included from Command::Extensions

#help, #help_banner

Constructor Details

#initializeFonts

:nodoc:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hexapdf/cli/fonts.rb', line 48

def initialize #:nodoc:
  super('fonts', takes_commands: false)
  short_desc("List fonts from a PDF file")
  long_desc(<<~EOF)
    Lists the fonts used in the PDF with additional information, sorted by page number.
  EOF
  options.on("--password PASSWORD", "-p", String,
             "The password for decryption. Use - for reading from standard input.") do |pwd|
    @password = (pwd == '-' ? read_password : pwd)
  end
  options.on("-i", "--pages PAGES", "The subset of pages that should be looked at") do |pages|
    @pages = pages
  end

  @password = nil
  @pages = nil
end

Instance Method Details

#execute(pdf) ⇒ Object

:nodoc:



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
# File 'lib/hexapdf/cli/fonts.rb', line 66

def execute(pdf) #:nodoc:
  with_document(pdf, password: @password) do |doc|
    printf("%5s %-40s %-12s %-10s %-3s %-3s %8s %9s\n",
           "page", "name", "type", "encoding", "emb", "sub", "size", "oid")
    puts("-" * 97)
    each_font(doc) do |pindex, font|
      font_type = case font[:Subtype]
                  when :Type1
                    if font.embedded? && font[:FontDescriptor][:FontFile3]
                      "Type 1C"
                    else
                      "Type 1"
                    end
                  when :Type3 then "Type 3"
                  when :TrueType then "TrueType"
                  when :Type0
                    if font.descendant_font[:Subtype] == :CIDFontType0
                      "CID CFF"
                    else
                      "CID TrueType"
                    end
                  else
                    "Unknown"
                  end
      encoding = font[:Encoding]
      encoding = if encoding.kind_of?(Symbol)
                   encoding.to_s.sub(/Encoding/, '')
                 elsif encoding.kind_of?(Dictionary) &&
                     [:Type1, :Type3, :TrueType].include?(font[:Subtype])
                   "Custom"
                 else
                   "Built-in"
                 end
      size = human_readable_file_size(font.embedded? ? font.font_file[:Length] : 0)
      embedded = (font.embedded? ? "yes" : "no")
      subset = (font[:BaseFont].match?(/\A[A-Z]{6}\+/) ? "yes" : "no")
      printf("%5s %-40s %-12s %-10s %-3s %-3s %8s %9s\n",
             pindex, font[:BaseFont], font_type, encoding,
             embedded, subset, size, "#{font.oid},#{font.gen}")
    end
  end
end