Class: Typst::Png

Inherits:
Base
  • Object
show all
Defined in:
lib/typst.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#font_paths, #input, #root

Instance Method Summary collapse

Methods inherited from Base

from_s, from_zip

Constructor Details

#initialize(input, root: ".", font_paths: []) ⇒ Png

Returns a new instance of Png.



118
119
120
121
# File 'lib/typst.rb', line 118

def initialize(input, root: ".", font_paths: [])
  super(input, root: root, font_paths: font_paths)
  @pages = Typst::_to_png(self.input, self.root, self.font_paths, File.dirname(__FILE__), false, {}).collect{ |page| page.pack("C*").to_s }
end

Instance Attribute Details

#pagesObject

Returns the value of attribute pages.



116
117
118
# File 'lib/typst.rb', line 116

def pages
  @pages
end

Instance Method Details

#write(output) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/typst.rb', line 123

def write(output)
  if pages.size > 1
    pages.each_with_index do |page, i|
      if output.include?("{{n}}")
        file_name = output.gsub("{{n}}", (i+1).to_s)
      else
        file_name = File.basename(output, File.extname(output)) + "_" + i.to_s
        file_name = file_name + File.extname(output)
      end
      File.open(file_name, "w"){ |f| f.write(page) }
    end
  elsif pages.size == 1
    File.open(output, "w"){ |f| f.write(pages[0]) }
  else
  end
end