3
# FILE NAME process_text_to_image.sh
6
# Gustav Hartvigsson 2024
7
# Distributed under the Cool Licence 1.1
7
# added --no-header for when you want to use your own pandoc header
12
# * added --no-header for when you want to use your own pandoc header
10
# Fixed --no-header... Seemed to have forgotten the "$" infront of the variable.
23
_NO_PANDOC_HEADER=false
15
# * Fixed --no-header...
16
# Seemed to have forgotten the "$" infront of the variable.
19
# * fixed up the if statments.
22
# * Added sanity check
23
# * General logic fixes.
24
# * Added --author argument
25
# * Fixed help message formating
33
__INVERT_COLOURS=false
34
__NO_PANDOC_HEADER=false
28
39
geometry: margin=0.5cm
30
41
mainfont: DejaVu Serif
51
__SCRIPT_ROOT=$(dirname $(readlink -f $0))
52
source $__SCRIPT_ROOT/useful.inc.sh
43
56
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
44
57
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
50
63
echo "---------------------"
53
66
echo " Print this help message"
55
echo "-i <file> --input <file>"
68
echo "-i <file> --input <file>"
56
69
echo " The file to use to convert to an image. "
58
echo "-o <file> --output <file>"
71
echo "-o <file> --output <file>"
59
72
echo " The image to output to. (Default=big_image.png)"
61
echo "-d <integer> --dpi <integer>"
74
echo "-d <integer> --dpi <integer>"
62
75
echo " Set the dpi of the intermediate image relative to an a5 paper."
63
76
echo " (Default=300)"
66
79
echo " Do not delete the TMP folder."
69
82
echo " Invert the colours of the final image."
71
echo "-t \"name\" --title \"name\""
84
echo "-t \"name\" --title \"name\""
72
85
echo " Set the title on the the title page."
87
echo "-a \"name\" --author \"name\""
88
echo " Set an author to the title page."
75
91
echo " Do not insert the pandoc header. (Default:"
76
echo "$_PANDOC_HEADER"
92
echo "$__PANDOC_HEADER"
79
95
echo "---------------------"
95
#ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
97
echo "IN_FILE\: $IN_FILE"
98
echo "OUT_FILE\: $OUT_FILE"
101
#echo "ESCAPED_CWD\: $ESCAPED_CWD"
103
if [ ! -e "$CWD/$IN_FILE" ]
107
function __sanity_check () {
108
# Check that we have the tools needed.
115
if [[ $__SANITY == false ]]; then
117
echo "Please install the missing tools."
124
# FIXME: Split the functionality out of the main function.
125
# FIXME: Use mkdtemp instead of the folder we are in.
129
echo "__IN_FILE\: $__IN_FILE"
130
echo "__OUT_FILE\: $__OUT_FILE"
134
if [[ ! -e "$__CWD/$__IN_FILE" ]] || [[ -z $__IN_FILE ]]
105
echo "!!!in file does not exist!!!"
136
echo "The provided <infile> does not exit."
111
141
# first we create a temp folder.
142
mkdir -p "$__CWD/tmp"
114
144
#next we want to copy our file into it.
115
cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
145
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
118
148
# Now we can start the work for this.
119
if [ $_NO_PANDOC_HEADER = false ]
149
if [[ $__NO_PANDOC_HEADER == false ]]
151
# FIXME: This is cursed.
121
152
# We add a special header to the file to make it pandoc know what to do.
123
printf '%s\n' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
126
printf '%s\n' "title: ${TITLE}" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
129
printf '%s' "$_PANDOC_HEADER" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
131
printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
154
# The header is built from the bottom up. The input text at the bottom, and
155
# the rest of the "elements" added above.
157
printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
158
if [[ ! -z $__TITLE ]]; then
159
printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
162
if [[ ! -z $__AUTHOR ]]; then
163
printf '%s\n' "author: ${__AUTHOR}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
166
printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
168
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
134
171
# Now we use pandoc to do to convert it to a PDF.
135
pandoc --pdf-engine=xelatex "$CWD/tmp/text.txt" -o "$CWD/tmp/text.pdf"
137
pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
172
echo "Generating PDF"
173
pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
175
pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
139
177
# Convert it to images
140
pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
178
echo "Converting to images"
179
pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
142
181
# convert make the colour space greyscale and the append to each other
143
convert -append -colorspace gray +matte -depth 8 "$CWD/tmp/page-*.png" "$CWD/tmp/big-page.png"
182
convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png"
147
186
# If we invert the final image this is where we do it.
148
if [ $INVERT_COLOURS = true ]
150
convert "$CWD/tmp/big-page.png" -channel RGB -negate "$CWD/tmp/big-page-inverted.png"
151
FINAL_IMAGE="$CWD/tmp/big-page-inverted.png"
153
FINAL_IMAGE="$CWD/tmp/big-page.png"
156
cp "$FINAL_IMAGE" "$CWD/$OUT_FILE.png"
187
if [[ $__INVERT_COLOURS == true ]]
189
echo "Inverting colours"
190
convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
191
FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
193
FINAL_IMAGE="$__CWD/tmp/big-page.png"
196
echo "Copying final image to $__CWD/$__OUT_FILE.png"
197
cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
200
# Cleanup of eveything.
202
if [[ $__PERSERVE_TMP == true ]]
204
echo "Note: Not cleaning up!"
213
function __parse_args () {
163
216
echo "Try --help or -h."
168
220
while [[ $# -gt 0 ]]
253
__INVERT_COLOURS=true
202
_NO_PANDOC_HEADER=true
257
__NO_PANDOC_HEADER=true
266
echo "Unkown argument \"${1}\"."
226
if [ $PERSERVE_TMP = true ]
228
echo "Not cleaning up!"