#!/bin/bash
INPUT=`pwd`/word
CURRENT=`pwd`
DIR=`mktemp -d`
cd $DIR
function make_book()
{
word=`cat $INPUT`
unset chars
for c in $word
do
chars[${#chars[@]}]=$c
done
count=${#chars[@]}
table_count=$(($count/8))
cat >book.tex <<'EOF'
\documentclass{article}
\usepackage{xeCJK}
\setCJKmainfont{AR PL KaitiM GB}
\usepackage{tikz}
\usepackage[a4paper,lmargin=0mm,top=10mm]{geometry}
\usepackage[export]{adjustbox}
\usepackage{array}
\pagenumbering{gobble}
% \grid for a single character
\newcommand\grid[1]{%
\fontsize{160}{160}
\begin{tikzpicture}[baseline=(char.base)]
\path[use as bounding box]
(0,0) rectangle (1em,1em);
\draw[help lines,step=0.5em]
(0,0) grid (1em,1em);
\draw[help lines,dashed]
(0,0) -- (1em,1em) (0,1em) -- (1em,0);
\node[inner sep=0pt,anchor=base west]
(char) at (0em,\gridraiseamount) {#1};
\end{tikzpicture}}
% \gridraiseamount is a font-specific value
\newcommand\gridraiseamount{0.12em}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{10pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{N}{@{}m{0pt}@{}}
% \Grid for a CJK string
\makeatletter
\newcommand\Grid[1]{%
\@tfor\z:=#1\do{\grid{\z}}}
\makeatother
\begin{document}
\xeCJKsetup{PunctStyle=plain}
EOF
char=0
for(( i=0; i<$table_count; i++))
do
echo "\begin{table}[ht]" >>book.tex
echo "\begin{tabular}{L{101mm}|L{104mm}N}" >>book.tex
echo "\hline" >>book.tex
echo "\\grid{${chars[$((char++))]}} & \\grid{${chars[$((char++))]}} & \\\\[6.5cm]" >>book.tex
echo "\hline" >>book.tex
echo "\\grid{${chars[$((char++))]}} & \\grid{${chars[$((char++))]}} & \\\\[6.5cm]" >>book.tex
echo "\hline" >>book.tex
echo "\\grid{${chars[$((char++))]}} & \\grid{${chars[$((char++))]}} & \\\\[6.5cm]" >>book.tex
echo "\hline" >>book.tex
echo "\\grid{${chars[$((char++))]}} & \\grid{${chars[$((char++))]}} & \\\\[6.5cm]" >>book.tex
echo "\hline" >>book.tex
echo "\end{tabular}" >>book.tex
echo "\end{table}" >>book.tex
done
echo '\end{document}' >>book.tex
xelatex book.tex > /dev/null 2>&1
cp -rf book.pdf $CURRENT/
cp -rf book.tex $CURRENT/
rm -rf $DIR
}
make_book
echo "book.pdf generated."