Class: JotPDF::Document::DocumentWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/jot_pdf/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core, resources_obj:, pages_obj:) ⇒ DocumentWriter

Returns a new instance of DocumentWriter.



303
304
305
306
307
308
309
310
# File 'lib/jot_pdf/document.rb', line 303

def initialize(core, resources_obj:, pages_obj:)
  @core = core
  @pages = []
  @pages_obj = pages_obj
  @resources_obj = resources_obj
  @images = {}
  @font_manager = FontManager.new(default_font: :Helvetica)
end

Instance Attribute Details

#font_managerObject (readonly)

Returns the value of attribute font_manager.



301
302
303
# File 'lib/jot_pdf/document.rb', line 301

def font_manager
  @font_manager
end

#imagesObject (readonly)

Returns the value of attribute images.



301
302
303
# File 'lib/jot_pdf/document.rb', line 301

def images
  @images
end

#pagesObject (readonly)

Returns the value of attribute pages.



301
302
303
# File 'lib/jot_pdf/document.rb', line 301

def pages
  @pages
end

Instance Method Details

#default_font(spec) ⇒ Object



312
313
314
# File 'lib/jot_pdf/document.rb', line 312

def default_font(spec)
  @font_manager.default_font = spec
end

#dsl(&block) ⇒ Object



351
352
353
# File 'lib/jot_pdf/document.rb', line 351

def dsl(&block)
  Docile.dsl_eval(self, &block)
end

#image(n, width:, height:, &block) ⇒ Object



320
321
322
323
324
# File 'lib/jot_pdf/document.rb', line 320

def image(n, width:, height:, &block)
  ic = ImageContext.new(@core, width:, height:)
  ic.dsl(&block)
  @images[n] = ic.image_obj
end

#load_font(path) ⇒ Object



316
317
318
# File 'lib/jot_pdf/document.rb', line 316

def load_font(path)
  @font_manager.load_font(path)
end

#page(width:, height:, &block) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/jot_pdf/document.rb', line 326

def page(width:, height:, &block)
  @core.dsl do
    alloc_obj => length_obj

    stream_size = nil
    obj do
      dict { entry("Length") { ref length_obj } }
      content_stream do
        PageContext.new(self, font_manager: @font_manager).dsl(&block)
      end => stream_size
    end => contents_obj

    obj(length_obj) { num stream_size }

    obj.of_dict do
      entry("Type") { name "Page" }
      entry("Parent") { ref @pages_obj }
      entry("MediaBox").of_array { num 0; num 0; num width; num height }
      entry("Resources") { ref @resources_obj }
      entry("Contents") { ref contents_obj }
    end => page_obj
    @pages << page_obj
  end
end