#!/usr/bin/env ruby
# encoding: utf-8

 Myname = File.basename($0)
Version = '1.03'

Help=
<<'DOC'
= bigchars - print big characters, one per page

= Synopsis
bigchars [options] [string]	

== Options

-h			print this help and exit
-H, --help		show full documentation and exit
-V, --version		print version and exit
-f, --font=NAME		font used for printing the characters;
			default: Calibri
-s, --size=POINTSIZE	size in points; default: 500
-o, --out=FILENAME	the filename for PDF output;
			the default is bigchars.pdf,
			the extension is optional.

= Description
bigchars creates a PDF file containing one large character on each
page for each character in /string/. The default is to print all ASCII
symbols (33-126) in 500 point Calibri.
The default font is Calibri, for other fonts, scan your system for
|.ttf| and |.otf| files and use those, with or without extension.
        
= Dependencies
You need luatex to run this program

= Author
[Wybo Dekker](wybodekker@me.com)

= Copyright
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html)
DOC

require 'erb'
require 'optparse'
require 'ostruct'
require 'tempfile'
require 'colorize'

def die(m)
   STDERR.puts "#{Myname}: #{m}".red
   exit 1
end

def help
  system("echo \"#{Help}\" | less -P#{Myname}-#{Version.tr('.','·')}")
end

$o = OpenStruct.new(
   :font => 'Calibri',
   :size => 500,
   :out =>  'bigchars'
)

OptionParser.new(
   banner = <<~EOD,
      #{Myname} - print big characters, one per page\n
      Usage: #{Myname} [options] [string]\n
      By default, string is ASCII 33..126 concatenated.\n
      Options:
      EOD
   width = 23,
   indent = ''
) do |opts|

   opts.on('-h','print this help and exit') do
      puts opts.to_a.delete_if { |x| x =~ /—$/}
      exit
   end
   opts.on('-H','--help','show full documentation and exit') do
      help
      exit
   end
   opts.on('-V','--version','print version and exit') do
      puts Version
      exit
   end
   opts.on('-f','--font=NAME', String,
      "font used for printing the characters;",
      "default: Calibri") do |v|
       $o.font = v
   end
   opts.on('-s','--size=POINTSIZE', Integer,
      "size in points; default: 500") do |v|
       $o.size = v
   end
   opts.on('-o','--out=FILENAME', String,
      "the filename for PDF output;",
      "the default is bigchars.pdf,",
      "the extension is optional.") do |v|
       $o.out = v
   end
   opts.on('-I','—') do
     system("instscript #{Myname}") 
     exit
   end
   opts.parse!
end

tex = Tempfile.new
if ARGV[0]
   arr = ARGV[0].split('').map{|x| x[0]}
else
   arr = ('!'..'~').to_a
end

$o.out.sub(/.pdf/,'')

erb = <<~'EOF'
	\nopagenumbers\parindent0pt
	\pagewidth=210mm\pageheight=297mm
	\input luaotfload.sty
	\font\big=<%= $o.font %> at <%= $o.size %>pt\big
	% arr.each do |v|
	\char`<%= v.sub(/([%\\])/,'\\\\\1') %>\eject
	% end
	\bye
	EOF

open(tex,'w') do |f|
   f.print ERB.new(erb,trim_mode:'%').result(binding)
end
err = `luatex -jobname=#{$o.out} -interaction=batchmode #{tex.path}`
if $?.exitstatus > 0
  die "luatex error:\n%s" % err
end
File.delete("#{$o.out}.log") if File.exist?("#{$o.out}.log")
