1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "tex-rendering.h"
20 #include "tex-glyphs.h"
29 #include <gl/xalloc.h>
32 tex_render (FILE *fp, const char *str)
40 /* Reads an entire file FP and returns it as a string.
41 Any single instance of newline will be mutated to a space.
42 However multiple consecutive newlines will be mutated to
45 read_whole_file (FILE *fp)
51 int consecutive_nl = 0;
52 while ((c = fgetc (fp)) >= 0)
56 result = xrealloc (result, len + BLOCK_SIZE);
57 memset (result + len, 0, BLOCK_SIZE);
62 if (consecutive_nl > 1)
63 result[bytes++] = '\n';
64 if (consecutive_nl == 1)
65 result[bytes++] = ' ';
79 static long macro_insertion_point = 0;
82 tex_preamble (FILE *fp, const char *str)
84 long where = ftell (fp);
85 fseek (fp, macro_insertion_point, SEEK_SET);
88 macro_insertion_point = ftell (fp);
89 fseek (fp, where, SEEK_SET);
94 main (int argc, char **argv)
98 while ((opt = getopt (argc, argv, "o:")) != -1)
103 outfile = argv[optind-1];
106 fprintf (stderr, "Usage: tex-strings -o <outfile> <infile1> <infile2> ... <infileN>\n");
113 fprintf (stderr, "Usage: tex-strings -o <outfile> <infile1> <infile2> ... <infileN>\n");
117 FILE *fpout = fopen (outfile, "w");
121 fprintf (stderr, "Cannot open output file %s: %s\n", outfile, strerror (err));
128 fseek (fpout, 4096, SEEK_SET);
130 for (int arg = optind; arg < argc; ++arg)
132 FILE *fpin = fopen (argv[arg], "r");
136 fprintf (stderr, "Cannot open input file %s: %s\n", argv[arg], strerror (err));
140 tex_render (fpout, "\\noindent");
142 char *str = read_whole_file (fpin);
144 size_t n = strlen (str);
145 const char *frag = 0;
148 frag = u8_to_tex_fragments (&s, &n, ¯os);
156 tex_render(fpout, "\\par\\vskip 1em");
161 struct tex_macro *next;
162 HMAP_FOR_EACH_SAFE (m, next, struct tex_macro, node, ¯os)
164 tex_preamble (fpout, tex_macro[m->index]);
168 hmap_destroy (¯os);
170 tex_render (fpout, "\\bye");