class Image

The Image class represents an image that is generated from a latex equation expression.

Attributes

filename[R]
height[R]
width[R]

Public Class Methods

generate_block(equ_data, equ_id, handler) click to toggle source
# File lib/asciidoctor-texnical/image.rb, line 22
def self.generate_block(equ_data, equ_id, handler)
  Image.new %($$#{equ_data}$$), equ_id, handler
end
generate_inline(equ_data, equ_id, handler) click to toggle source
# File lib/asciidoctor-texnical/image.rb, line 18
def self.generate_inline(equ_data, equ_id, handler)
  Image.new %($#{equ_data}$), equ_id, handler
end
new(input, equ_id, handler) click to toggle source
# File lib/asciidoctor-texnical/image.rb, line 26
def initialize(input, equ_id, handler)
  equ_id ||= %(stem-#{::Digest::MD5.hexdigest input})
  @filename = %(#{equ_id}.png)
  target_path = File.absolute_path handler.image_target_dir
  dir = Dir.mktmpdir
  tpth = File.join dir, @filename
  Dir.chdir(dir) do
    generate_png(input, tpth, handler.ppi)
    FileUtils.cp(tpth, target_path)
  end
end

Public Instance Methods

extract_dimensions_from_output(captured_stdout) click to toggle source
# File lib/asciidoctor-texnical/image.rb, line 47
def extract_dimensions_from_output(captured_stdout)
  @height = captured_stdout.scan(/height=-?(\d+)/)[0][0].to_i
  @width = captured_stdout.scan(/width=(\d+)/)[0][0].to_i
end
generate_png(input, pth, ppi) click to toggle source
# File lib/asciidoctor-texnical/image.rb, line 38
def generate_png(input, pth, ppi)
  File.open('eq.tex', 'w') do |file|
    file.write("#{HEADER}\\begin{document} #{input}\\end{document} ")
  end
  _ = Open3.capture2('latex -interaction=batchmode eq.tex')
  stdout, = Open3.capture2("dvipng -o #{pth} -T tight -z9 -D #{ppi} -q --height --width eq.dvi")
  extract_dimensions_from_output(stdout)
end