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.



27
28
29
30
31
32
33
34
35
# File 'lib/ruby_client/Font.rb', line 27

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.



46
47
48
# File 'lib/ruby_client/Font.rb', line 46

def _name
  @_name
end

#_resourceObject

Returns the value of attribute _resource.



46
47
48
# File 'lib/ruby_client/Font.rb', line 46

def _resource
  @_resource
end

#embedObject

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



51
52
53
# File 'lib/ruby_client/Font.rb', line 51

def embed
  @embed
end

#resource_nameObject

Gets or sets a name for the font resource.



61
62
63
# File 'lib/ruby_client/Font.rb', line 61

def resource_name
  @resource_name
end

#subsetObject

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



56
57
58
# File 'lib/ruby_client/Font.rb', line 56

def subset
  @subset
end

Class Method Details

.courierObject

Gets the Courier core font with Latin 1 encoding.



146
147
148
149
150
151
# File 'lib/ruby_client/Font.rb', line 146

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.



156
157
158
159
160
161
# File 'lib/ruby_client/Font.rb', line 156

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.



176
177
178
179
180
181
# File 'lib/ruby_client/Font.rb', line 176

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.



166
167
168
169
170
171
# File 'lib/ruby_client/Font.rb', line 166

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



37
38
39
40
41
42
43
44
# File 'lib/ruby_client/Font.rb', line 37

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.



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

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



214
215
216
217
# File 'lib/ruby_client/Font.rb', line 214

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/ruby_client/Font.rb', line 261

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



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

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..



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

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.



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ruby_client/Font.rb', line 235

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.



106
107
108
109
110
111
# File 'lib/ruby_client/Font.rb', line 106

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.



116
117
118
119
120
121
# File 'lib/ruby_client/Font.rb', line 116

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.



136
137
138
139
140
141
# File 'lib/ruby_client/Font.rb', line 136

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.



126
127
128
129
130
131
# File 'lib/ruby_client/Font.rb', line 126

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

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

.load_fontsObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/ruby_client/Font.rb', line 278

def self.load_fonts
  return unless @load_required
  loaded_any = false

  @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



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

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.



186
187
188
189
190
191
# File 'lib/ruby_client/Font.rb', line 186

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.



76
77
78
79
80
81
# File 'lib/ruby_client/Font.rb', line 76

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.



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

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.



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

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.



66
67
68
69
70
71
# File 'lib/ruby_client/Font.rb', line 66

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.



196
197
198
199
200
201
# File 'lib/ruby_client/Font.rb', line 196

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



325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/ruby_client/Font.rb', line 325

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