Module: JotPDF::Document

Defined in:
lib/jot_pdf/document.rb

Defined Under Namespace

Classes: DocumentWriter, FontManager, ImageContext, NonstandardFont, PageContext, StandardFont, TextContext

Class Method Summary collapse

Class Method Details

.generate_unicode_cmap(mapping) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/jot_pdf/document.rb', line 356

def self.generate_unicode_cmap(mapping)
  <<~CMAP
    /CIDInit /ProcSet findresource begin
    12 dict begin
    begincmap
    /CIDSystemInfo 3 dict dup begin
      /Registry (Adobe) def
      /Ordering (UCS) def
      /Supplement 0 def
    end def
    /CMapName /Adobe-Identity-UCS def
    /CMapType 2 def

    1 begincodespacerange
    <00> <FF>
    endcodespacerange

    #{mapping.length} beginbfchar
    #{mapping.map do |code, codepoint|
        format("<%<code>02X><%<codepoint>s>", code:, codepoint: codepoint.chr(::Encoding::UTF_16BE).unpack1("H*"))
      end.join("\n")}
    endbfchar

    endcmap
    CMapName currentdict /CMap defineresource pop
    end
    end
  CMAP
end

.write(io, &block) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/jot_pdf/document.rb', line 386

def self.write(io, &block)
  Core.write(io) do
    header

    alloc_obj => resources_obj
    alloc_obj => pages_obj
    writer = Document::DocumentWriter.new(self, resources_obj:, pages_obj:)
    writer.dsl(&block)

    # @type var font_file_objs: Hash[::String | Symbol, ObjectRef]
    # @type var widths_objs: Hash[::String | Symbol, ObjectRef]
    # @type var tounicode_objs: Hash[::String | Symbol, ObjectRef]
    font_file_objs = {}
    widths_objs = {}
    tounicode_objs = {}
    writer.font_manager.loaded_fonts.each do |n, f|
      # TODO: How can Steep use is_a? on _Font?
      # rubocop:disable Style/CaseEquality
      next unless NonstandardFont === f
      # rubocop:enable Style/CaseEquality

      subset_data = f.encode_subset
      obj do
        dict do
          entry("Length").of_num subset_data.bytesize
          entry("Length1").of_num subset_data.bytesize # always required for TrueType
        end
        stream do |stream|
          stream << subset_data
        end
      end => font_file_obj
      font_file_objs[n] = font_file_obj

      subset = TTFunk::File.new(subset_data)
      obj.of_array do
        (subset.os2.first_char_index..subset.os2.last_char_index).each do |code|
          gid = subset.cmap.tables.first[code]
          width_in_units = subset.horizontal_metrics.for(gid).advance_width
          num (Float(width_in_units) * 1000 / subset.header.units_per_em).to_i
        end
      end => widths_obj
      widths_objs[n] = widths_obj

      alloc_obj => length_obj
      stream_size = nil
      obj do
        dict { entry("Length").of_ref length_obj }
        stream do |w|
          w << generate_unicode_cmap(f.subset.to_unicode_map)
        end => stream_size
      end => tounicode_obj
      obj(length_obj).of_num stream_size
      tounicode_objs[n] = tounicode_obj
    end

    obj(resources_obj).of_dict do
      entry("XObject").of_dict do
        writer.images.each do |n, r|
          entry(n).of_ref r
        end
      end
      entry("ProcSet").of_array { name "PDF"; name "Text"; name "ImageB"; name "ImageC"; name "ImageI" }
      entry("Font").of_dict do
        writer.font_manager.loaded_fonts.each do |n, f|
          entry(n.to_s).of_dict do
            entry("Type") { name "Font" }
            case f
            when StandardFont
              entry("Subtype") { name "Type1" }
              entry("BaseFont") { name n.to_s }
            when NonstandardFont
              subset = TTFunk::File.new(f.encode_subset)
              # https://github.com/prawnpdf/prawn/blob/aaea7f6beda092ba48001414125a576dcf891362/lib/prawn/fonts/ttf.rb#L446-L447
              base_name = subset.name.postscript_name[0, 33].delete("\0")
              entry("Subtype").of_name "TrueType"
              entry("FirstChar").of_num subset.os2.first_char_index
              entry("LastChar").of_num subset.os2.last_char_index
              entry("ToUnicode").of_ref tounicode_objs[n]
              entry("BaseFont").of_name base_name
              entry("Widths").of_ref widths_objs[n]
              entry("FontDescriptor").of_dict do
                entry("Ascent").of_num subset.ascent
                entry("Descent").of_num subset.descent
                entry("CapHeight").of_num subset.os2.cap_height
                entry("StemV").of_num 0
                entry("ItalicAngle").of_num 0
                entry("Flags").of_num 0b100
                entry("FontBBox").of_array do
                  subset.bbox.each do |i|
                    num i
                  end
                end
                entry("FontName").of_name base_name
                entry("XHeight").of_num subset.os2.x_height
                entry("FontFile2").of_ref font_file_objs[n]
              end
            end
          end
        end
      end
    end => resources_obj

    obj(pages_obj).of_dict do
      entry("Type") { name "Pages" }
      entry("Kids").of_array do
        writer.pages.each do |page|
          ref page
        end
      end
      entry("Count") { num writer.pages.size }
    end
    obj.of_dict do
      entry("Type") { name "Catalog" }
      entry("Pages") { ref pages_obj }
    end => root_obj
    xref
    trailer do
      entry("Root") { ref root_obj }
    end
  end
end