Add a TeX driver
[pspp] / tests / output / tex-glyphs.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2020 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "libpspp/hmap.h"
20 #include "tex-rendering.h"
21 #include "tex-glyphs.h"
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <stdlib.h>
26
27 static void
28 tex_render (FILE *fp, const char *str)
29 {
30   fputs (str, fp);
31   fputc ('\n', fp);
32 }
33
34 static long macro_insertion_point = 0;
35
36 static void
37 tex_preamble (FILE *fp, const char *str)
38 {
39   long where = ftell (fp);
40   fseek (fp, macro_insertion_point, SEEK_SET);
41   tex_render (fp, str);
42   fputc ('\n', fp);
43   macro_insertion_point = ftell (fp);
44   fseek (fp, where, SEEK_SET);
45 }
46
47
48 int
49 main (int argc, char **argv)
50 {
51   if (argc < 2)
52     {
53       fprintf (stderr, "Usage: tex-glyphs <file>\n");
54       return 1;
55     }
56
57   FILE *fp = fopen (argv[1], "w");
58   if (!fp)
59     {
60       perror ("Cannot open output file");
61       return 1;
62     }
63
64   struct hmap macros;
65   hmap_init (&macros);
66
67   fseek (fp, 4096, SEEK_SET);
68
69   tex_render (fp, "\\raggedbottom");
70
71   tex_render (fp, "\\halign{{\\tt #}\\qquad&{\\font\\xx=cmr7 \\xx #}\\hfil&\\quad{\\rm #}");
72   tex_render (fp, "\\hfil&\\quad{\\sl #}");
73   tex_render (fp, "\\hfil&\\quad{\\it #}");
74   tex_render (fp, "\\hfil&\\quad{\\bf #}");
75   tex_render (fp, "\\hfil&\\quad{\\tt #}\\cr");
76
77   for (const struct glyph_block *gb = defined_blocks; gb->start; ++gb)
78   {
79     ucs4_t x = gb->start->code_point;
80     for (const struct glyph *g = gb->start; x < gb->n_glyphs + gb->start->code_point; ++g)
81       {
82         assert (g->code_point == x++);
83         fprintf (fp, "U+%04X&%s&M%sM", g->code_point, g->name,
84                  code_point_to_tex (g->code_point, &macros));
85         fprintf (fp, "&M%sM",
86                  code_point_to_tex (g->code_point, &macros));
87         fprintf (fp, "&M%sM",
88                  code_point_to_tex (g->code_point, &macros));
89         fprintf (fp, "&M%sM",
90                  code_point_to_tex (g->code_point, &macros));
91         fprintf (fp, "&M%sM\\cr\n",
92                  code_point_to_tex (g->code_point, &macros));
93       }
94   }
95
96   {
97     struct tex_macro *m;
98     struct tex_macro *next;
99     HMAP_FOR_EACH_SAFE (m, next, struct tex_macro, node, &macros)
100       {
101         tex_preamble (fp, tex_macro[m->index]);
102         free (m);
103       }
104   }
105   hmap_destroy (&macros);
106
107   tex_render (fp, "}");
108   tex_render (fp, "\\bye");
109
110   fclose (fp);
111   return 0;
112 }