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