Add a TeX driver
[pspp] / src / output / tex-rendering.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 "tex-rendering.h"
20 #include "tex-glyphs.h"
21
22 #include "libpspp/hmap.h"
23 #include "libpspp/hash-functions.h"
24
25 #include "gl/mbiter.h"
26 #include "gl/mbchar.h"
27 #include "gl/unistr.h"
28 #include "gl/xalloc.h"
29
30 /* Return a string containing TeX code which can  be used to typeset
31    Unicode code point CP.  As a side effect, insert any needed macro indeces
32    into the hash table MACROS.  */
33 const char *
34 code_point_to_tex (ucs4_t cp, struct hmap *macros)
35 {
36   const char *what = 0;
37
38   for (const struct glyph_block *gb = defined_blocks;  gb->start; gb++)
39   {
40     if (cp < gb->start->code_point)
41       break;
42
43     if (cp < gb->start->code_point + gb->n_glyphs)
44       {
45         what = gb->start[cp - gb->start->code_point].tex_rendering;
46         enum tex_ancilliary macro = gb->start[cp - gb->start->code_point].macro;
47         if (macro != TEX_NONE)
48           {
49             struct tex_macro *a_macro = NULL;
50             HMAP_FOR_EACH_WITH_HASH (a_macro, struct tex_macro, node, hash_int (0, macro), macros)
51               {
52                 if (a_macro->index == macro)
53                   break;
54               }
55
56             if (a_macro == NULL)
57               {
58                 a_macro = XMALLOC (struct tex_macro);
59                 a_macro->index = macro;
60                 hmap_insert (macros, &a_macro->node, hash_int (0, macro));
61               }
62           }
63         break;
64       }
65   }
66
67   if (!what)
68     fprintf (stderr, "Unsupported code point U+%04X\n", cp);
69   return what ? what : unsupported_glyph;
70 }
71
72 /* Convert the first character of the utf8 string S, into a TeX fragment.
73    LEN must be the length of S (in bytes).   After this function returns, S
74    will have been incremented by the length of the first character in S,
75    and LEN will have been decremented by the same amount.   */
76 const char *
77 u8_to_tex_fragments (const char **s, size_t *len, struct hmap *macros)
78 {
79   const uint8_t *u = (const uint8_t *) *s;
80   size_t clen = u8_mblen (u, *len);
81
82   ucs4_t puc;
83   u8_mbtouc (&puc, u, clen);
84
85   *len -= clen;
86   *s += clen;
87
88   return code_point_to_tex (puc, macros);
89 }