Skip to content

Commit

Permalink
reworked how TeX is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
TwP committed Mar 1, 2008
1 parent 16bd407 commit 9979613
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 65 deletions.
6 changes: 0 additions & 6 deletions lib/webby/helpers/graphviz_helper.rb
Expand Up @@ -86,12 +86,6 @@ def graphviz( *args, &block )
return
end

# ensure the requested graphviz command actually exists
%x[#{cmd} -V 2>&1]
unless 0 == $?.exitstatus
raise NameError, "'#{cmd}' not found on the path"
end

# pull the name of the graph|digraph out of the DOT script
name = text.match(%r/\A\s*(?:strict\s+)?(?:di)?graph\s+([A-Za-z_][A-Za-z0-9_]*)\s+\{/o)[1]

Expand Down
88 changes: 29 additions & 59 deletions lib/webby/helpers/tex_img_helper.rb
Expand Up @@ -2,7 +2,6 @@

require Webby.libpath(*%w[webby stelan mktemp])
require 'fileutils'
require 'tempfile'

module Webby::Helpers
module TexImgHelper
Expand Down Expand Up @@ -36,8 +35,6 @@ class Error < StandardError; end
# TeX color spec, or #aabbcc) [default is black]
# :resolution : the desired resolution in dpi (HxV)
# [default is 150x150]
# :antialias : if false, disables anti-aliasing in the resulting image
# [default is true]
#
# the following options are passed as-is to the generated <img /> tag
# :style : CSS styles to apply to the <img />
Expand All @@ -60,21 +57,15 @@ def tex2img( *args, &block )
return
end

# create a temporary file for holding any error messages
# from the graphviz program
err = Tempfile.new('graphviz_err')
err.close
path = opts.getopt(:path)
type = opts.getopt(:type, 'png')
bg = opts.getopt(:bg, 'white')
fg = opts.getopt(:fg, 'black')
res = opts.getopt(:resolution, '150x150')

path = opts.getopt(:path)
type = opts.getopt(:type, 'png')
bg = opts.getopt(:bg, 'white')
fg = opts.getopt(:fg, 'black')
res = opts.getopt(:resolution, '150x150')
aa = opts.getopt(:antialias, true)

# fix the color escaping
fg = TexImgHelper.tex_color(fg)
bg = TexImgHelper.tex_color(bg)
# fix color escaping
fg = fg =~ %r/^[a-zA-Z]+$/ ? fg : "\"#{fg}\""
bg = bg =~ %r/^[a-zA-Z]+$/ ? bg : "\"#{bg}\""

# generate the image filename based on the path, graph name, and type
# of image to generate
Expand All @@ -88,15 +79,21 @@ def tex2img( *args, &block )
FileUtils.mkpath(::File.join(out_dir, path)) unless path.nil?

tex = <<-TEX
\\documentclass[12pt]{article}
\\usepackage[usenames,dvipsnames]{color}
\\usepackage[dvips]{graphicx}
\\nonstopmode
\\documentclass{article}
\\usepackage[T1]{fontenc}
\\usepackage{amsmath,amsfonts,amssymb,wasysym,latexsym,marvosym,txfonts}
\\usepackage[pdftex]{color}
\\pagestyle{empty}
\\pagecolor#{bg}
\\begin{document}
{\\color#{fg}
\\fontsize{12}{24}
\\selectfont
\\color{white}
\\pagecolor{black}
\\[
#{text}
}\\end{document}
\\]
\\end{document}
TEX
tex.gsub!(%r/\n\s+/, "\n").strip!

Expand All @@ -109,9 +106,14 @@ def tex2img( *args, &block )
File.open('out.tex', 'w') {|fd| fd.puts tex}
dev_null = test(?e, "/dev/null") ? "/dev/null" : "NUL:"

%x[latex -interaction=batchmode out.tex &> #{dev_null}]
%x[dvips -o out.eps -E out.dvi &> #{dev_null}]
%x[convert +adjoin #{aa ? '-antialias' : '+antialias'} -density #{res} out.eps #{out_file} &> #{dev_null}]
%x[pdflatex -interaction=batchmode out.tex &> #{dev_null}]

convert = "\\( -density #{res} out.pdf -trim +repage \\) "
convert << "\\( +clone -fuzz 100% -fill #{fg} -opaque black \\) "
convert << "+swap -compose copy-opacity -composite "
convert << "\\( +clone -fuzz 100% -fill #{bg} -opaque white +matte \\) "
convert << "+swap -compose over -composite #{out_file}"
%x[convert #{convert} &> #{dev_null}]
ensure
Dir.chdir(pwd)
FileUtils.rm_rf(tmpdir) if test(?e, tmpdir)
Expand All @@ -133,42 +135,10 @@ def tex2img( *args, &block )

buffer[pos..-1] = out
return
ensure
end

# call-seq:
# TexImgHelper.tex_color( string ) => string
#
# Taken the given color _string_ and convert it to a TeX color spec. The
# input string can be either a RGB Hex value, a TeX color spec, or a color
# name.
#
# tex_color( '#666666' ) #=> [rgb]{0.4,0.4,0.4}
# tex_color( 'Tan' ) #=> {Tan}
# tex_color( '[rgb]{1,0,0}' ) #=> [rgb]{1,0,0}
#
# This is an example of an invalid Hex RGB color -- they must contain six
# hexidecimal characters to be valid.
#
# tex_color( '#333' ) #=> {#333}
#
def self.tex_color( color )
case color
when %r/^#([A-Fa-f0-9]{6})/o
hex = $1
rgb = []
hex.scan(%r/../) {|n| rgb << Float(n.to_i(16))/255.0}
"[rgb]{#{rgb.join(',')}}"
when %r/^[\{\[]/o
"{#{color}}"
else
color
end
end

end # module TexImgHelper

%x[latex --version 2>&1]
%x[pdflatex --version 2>&1]
if 0 == $?.exitstatus
%x[convert --version 2>&1]
if 0 == $?.exitstatus
Expand Down

0 comments on commit 9979613

Please sign in to comment.