Class: DynamicPDFApi::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_client/Font.rb

Overview

Represents font.

Constant Summary collapse

@@times_roman =
nil
@@times_bold =
nil
@@times_italic =
nil
@@times_bold_italic =
nil
@@helvetica =
nil
@@helvetica_bold =
nil
@@helvetica_oblique =
nil
@@helvetica_boldOblique =
nil
@@courier =
nil
@@courier_bold =
nil
@@courier_oblique =
nil
@@courier_boldOblique =
nil
@@symbol =
nil
@@zapf_dingbats =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cloud_resource_name = nil) ⇒ Font

Initializes a new instance of the Font class using the font name that is present in the cloud resource manager.

Parameters:

  • cloud_resource_name (String) (defaults to: nil)

    The font name present in the cloud resource manager.



70
71
72
73
74
75
76
77
78
# File 'lib/ruby_client/Font.rb', line 70

def initialize(cloud_resource_name = nil)
  @data = {}
  @_resource = nil
  @embed = nil
  @subset = nil

  @resource_name = cloud_resource_name
  @_name = SecureRandom.uuid
end

Instance Attribute Details

#_nameObject

Returns the value of attribute _name.



89
90
91
# File 'lib/ruby_client/Font.rb', line 89

def _name
  @_name
end

#_resourceObject

Returns the value of attribute _resource.



89
90
91
# File 'lib/ruby_client/Font.rb', line 89

def _resource
  @_resource
end

#embedObject

Gets or sets a boolean indicating whether to embed the font.



94
95
96
# File 'lib/ruby_client/Font.rb', line 94

def embed
  @embed
end

#resource_nameObject

Gets or sets a name for the font resource.



104
105
106
# File 'lib/ruby_client/Font.rb', line 104

def resource_name
  @resource_name
end

#subsetObject

Gets or sets a boolean indicating whether to subset embed the font.



99
100
101
# File 'lib/ruby_client/Font.rb', line 99

def subset
  @subset
end

Class Method Details

.courierObject

Gets the Courier core font with Latin 1 encoding.



189
190
191
192
193
194
# File 'lib/ruby_client/Font.rb', line 189

def self.courier
  @@courier = Font.new if @@courier.nil?

  @@courier._name = "courier"
  @@courier
end

.courier_boldObject

Gets the Courier Bold core font with Latin 1 encoding.



199
200
201
202
203
204
# File 'lib/ruby_client/Font.rb', line 199

def self.courier_bold
  @@courier_bold = Font.new if @@courier_bold.nil?

  @@courier_bold._name = "courierBold"
  @@courier_bold
end

.courier_bold_obliqueObject

Gets the Courier Bold Oblique core font with Latin 1 encoding.



219
220
221
222
223
224
# File 'lib/ruby_client/Font.rb', line 219

def self.courier_bold_oblique
  @@courier_boldOblique = Font.new if @@courier_boldOblique.nil?

  @@courier_boldOblique._name = "courierBoldOblique"
  @@courier_boldOblique
end

.courier_obliqueObject

Gets the Courier Oblique core font with Latin 1 encoding.



209
210
211
212
213
214
# File 'lib/ruby_client/Font.rb', line 209

def self.courier_oblique
  @@courier_oblique = Font.new if @@courier_oblique.nil?

  @@courier_oblique._name = "courierOblique"
  @@courier_oblique
end

.create_font(font_resource = nil, resource_name = nil) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/ruby_client/Font.rb', line 80

def self.create_font(font_resource = nil, resource_name = nil)
  font = Font.new
  font._resource = font_resource
  font.resource_name = resource_name

  font._name = SecureRandom.uuid
  font
end

.from_file(file_path, resource_name = nil) ⇒ Object

Initializes a new instance of the Font class using the file path of the font and resource name.

Parameters:

  • file_path (String)

    The file path of the font file.

  • resource_name (String) (defaults to: nil)

    The resource name for the font.



252
253
254
255
# File 'lib/ruby_client/Font.rb', line 252

def self.from_file(file_path, resource_name = nil)
  resource = Resource.new(file_path, resource_name)
  Font.create_font(resource, resource.resource_name)
end

.from_stream(_stream, _resource_name = nil) ⇒ Object



257
258
259
260
# File 'lib/ruby_client/Font.rb', line 257

def self.from_stream(_stream, _resource_name = nil)
  resource = Resource.new(stream, resource_name)
  Font.create_font(resource, resource.resource_name)
end

.from_system(font_name, resource_name = nil) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/ruby_client/Font.rb', line 304

def self.from_system(font_name, resource_name = nil)
  return nil if font_name.nil? || font_name.strip.empty?

  font_name = font_name.gsub(/[- ]/, "")  # Removing the space and - from font_name

  self.load_fonts if @load_required

  @font_details.each do |detail|
    if detail._name.casecmp(font_name).zero?
      font_resource = FontResource.new(detail.file_path, resource_name)
      return Font.create_font(font_resource, font_resource.resource_name)
    end
  end

  nil
end

.get_google_font_text(name, weight, italic) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/ruby_client/Font.rb', line 262

def self.get_google_font_text(name, weight, italic)
  if italic == true
    name + ":" + weight.to_s + "italic"
  else
    name + ":" + weight.to_s
  end
end

.global(font_name) ⇒ Object

Gets the font from the global storage.

Parameters:

  • fontName (string)

    The name of the font to get from the global storage..



298
299
300
301
302
# File 'lib/ruby_client/Font.rb', line 298

def self.global(font_name)
  font = Font.new()
  font._name = font_name
  font
end

.google(font_name, bold = nil, italic = false) ⇒ Object

Gets the font from the google.

Parameters:

  • fontName (string)

    The name of the google font.

  • bold (bold) (defaults to: nil)

    If true font weight will be taken as 700 otherwise 400.

  • italic (italic) (defaults to: false)

    The italic property of the font.



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/ruby_client/Font.rb', line 278

def self.google(font_name, bold = nil, italic = false)
  font = Font.new()
  if bold == true
    font._name = Font.get_google_font_text(font_name, 700, italic)
  elsif (bold.is_a?(Integer) == true)
    font._name = Font.get_google_font_text(font_name, bold, italic);
  elsif bold == false
    font._name = Font.get_google_font_text(font_name, 400, italic);
  else
    font._name = font_name;
  end
  font
end

.helveticaObject

Gets the Helvetica core font with Latin 1 encoding.



149
150
151
152
153
154
# File 'lib/ruby_client/Font.rb', line 149

def self.helvetica
  @@helvetica = Font.new if @@helvetica.nil?

  @@helvetica._name = "helvetica"
  @@helvetica
end

.helvetica_boldObject

Gets the Helvetica Bold core font with Latin 1 encoding.



159
160
161
162
163
164
# File 'lib/ruby_client/Font.rb', line 159

def self.helvetica_bold
  @@helvetica_bold = Font.new if @@helvetica_bold.nil?

  @@helvetica_bold._name = "helveticaBold"
  @@helvetica_bold
end

.helvetica_bold_obliqueObject

Gets the Helvetica Bold Oblique core font with Latin 1 encoding.



179
180
181
182
183
184
# File 'lib/ruby_client/Font.rb', line 179

def self.helvetica_bold_oblique
  @@helvetica_boldOblique = Font.new if @@helvetica_boldOblique.nil?

  @@helvetica_boldOblique._name = "helveticaBoldOblique"
  @@helvetica_boldOblique
end

.helvetica_obliqueObject

Gets the Helvetica Oblique core font with Latin 1 encoding.



169
170
171
172
173
174
# File 'lib/ruby_client/Font.rb', line 169

def self.helvetica_oblique
  @@helvetica_oblique = Font.new if @@helvetica_oblique.nil?

  @@helvetica_oblique._name = "helveticaOblique"
  @@helvetica_oblique
end

.init(platform_override = nil) ⇒ Object



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
# File 'lib/ruby_client/Font.rb', line 13

def self.init(platform_override = nil)
  begin

    # -------- Windows --------
    if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
      windir = ENV["WINDIR"]
      if windir && !windir.empty?
        path = File.join(windir, "Fonts")
        @path_to_fonts_resource_directory = path if Dir.exist?(path)
      end

    # -------- macOS --------
    elsif RUBY_PLATFORM =~ /darwin/
      home = Dir.home rescue nil
      paths = [
        "/System/Library/Fonts",
        "/Library/Fonts",
        home ? File.join(home, "Library", "Fonts") : nil
      ]

      paths.each do |path|
        next unless path
        if Dir.exist?(path)
          @path_to_fonts_resource_directory = path
          break
        end
      end

    # -------- Linux --------
    elsif RUBY_PLATFORM =~ /linux/
      home = Dir.home rescue nil
      paths = [
        "/usr/share/fonts",
        "/usr/local/share/fonts",
        home ? File.join(home, ".fonts") : nil,
        home ? File.join(home, ".local", "share", "fonts") : nil
      ]

      paths.each do |path|
        next unless path
        if Dir.exist?(path)
          @path_to_fonts_resource_directory = path
          break
        end
      end
    end
  rescue e
    puts "Error in getting the font: #{e.message}"
  end
end

.load_fontsObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/ruby_client/Font.rb', line 321

def self.load_fonts
  return unless @load_required
  loaded_any = false

  init

  @lock.synchronize do
    if @path_to_fonts_resource_directory && !@path_to_fonts_resource_directory.empty?
      dir_Info = File.join(@path_to_fonts_resource_directory.gsub("\\", "/"), "*")

      Dir.glob(dir_Info).each do |file_path|
        next unless file_path.downcase.end_with?(".ttf", ".otf")

        File.open(file_path, "rb") do |reader|
          name_table = self.read_font_name_table(reader)

          if name_table && name_table._name && !name_table._name.empty?
            @font_details << FontInformation.new(name_table._name, file_path)
          end
        end
      end
    end
    @load_required = false
  end
end

.read_font_name_table(reader) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/ruby_client/Font.rb', line 347

def self.read_font_name_table(reader)
  name_table = nil
  begin
    reader.seek(4, IO::SEEK_SET)
    table_count = (reader.readbyte << 8) | reader.readbyte
    if table_count > 0
      reader.seek(12, IO::SEEK_SET)
      table_directory = reader.read(table_count * 16).b
      (0...table_directory.size).step(16) do |i|
        tag_bytes = table_directory[i, 4]
        tag = tag_bytes.unpack1("V")
        if tag == 1701667182 # "name"
          name_table = FullNameTable.new(reader, table_directory, i)
          break
        end
      end
    end
  rescue => e
    puts "Error in read_font_name_table: #{e.message}"
  end
  name_table
end

.symbolObject

Gets the Symbol core font.



229
230
231
232
233
234
# File 'lib/ruby_client/Font.rb', line 229

def self.symbol
  @@symbol = Font.new if @@symbol.nil?

  @@symbol._name = "symbol"
  @@symbol
end

.times_boldObject

Gets the Times Bold core font with Latin 1 encoding.



119
120
121
122
123
124
# File 'lib/ruby_client/Font.rb', line 119

def self.times_bold
  @@times_bold = Font.new if @@times_bold.nil?

  @@times_bold._name = "timesBold"
  @@times_bold
end

.times_bold_italicObject

Gets the Times Bold Italic core font with Latin 1 encoding.



139
140
141
142
143
144
# File 'lib/ruby_client/Font.rb', line 139

def self.times_bold_italic
  @@times_bold_italic = Font.new if @@times_bold_italic.nil?

  @@times_bold_italic._name = "timesBoldItalic"
  @@times_bold_italic
end

.times_italicObject

Gets the Times Italic core font with Latin 1 encoding.



129
130
131
132
133
134
# File 'lib/ruby_client/Font.rb', line 129

def self.times_italic
  @@times_italic = Font.new if @@times_italic.nil?

  @@times_italic._name = "timesItalic"
  @@times_italic
end

.times_romanObject

Gets the Times Roman core font with Latin 1 encoding.



109
110
111
112
113
114
# File 'lib/ruby_client/Font.rb', line 109

def self.times_roman
  @@times_roman = Font.new if @@times_roman.nil?

  @@times_roman._name = "timesRoman"
  @@times_roman
end

.zapf_dingbatsObject

Gets the Zapf Dingbats core font.



239
240
241
242
243
244
# File 'lib/ruby_client/Font.rb', line 239

def self.zapf_dingbats
  @@zapf_dingbats = Font.new if @@zapf_dingbats.nil?

  @@zapf_dingbats._name = "zapfDingbats"
  @@zapf_dingbats
end

Instance Method Details

#to_json(_options = {}) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/ruby_client/Font.rb', line 370

def to_json(_options = {})
  json_array = {}
  json_array["name"] = @_name unless @_name.nil?

  json_array["embed"] = @embed unless @embed.nil?

  json_array["subset"] = @subset unless @subset.nil?

  json_array["resourceName"] = @resource_name unless @resource_name.nil?

  JSON.pretty_generate(json_array)
end