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"
22 #include "libpspp/hmap.h"
23 #include "libpspp/hash-functions.h"
25 #include "gl/mbiter.h"
26 #include "gl/mbchar.h"
27 #include "gl/unistr.h"
28 #include "gl/xalloc.h"
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. */
34 code_point_to_tex (ucs4_t cp, struct hmap *macros)
38 for (const struct glyph_block *gb = defined_blocks; gb->start; gb++)
40 if (cp < gb->start->code_point)
43 if (cp < gb->start->code_point + gb->n_glyphs)
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)
49 struct tex_macro *a_macro = NULL;
50 HMAP_FOR_EACH_WITH_HASH (a_macro, struct tex_macro, node, hash_int (0, macro), macros)
52 if (a_macro->index == macro)
58 a_macro = XMALLOC (struct tex_macro);
59 a_macro->index = macro;
60 hmap_insert (macros, &a_macro->node, hash_int (0, macro));
68 fprintf (stderr, "Unsupported code point U+%04X\n", cp);
69 return what ? what : unsupported_glyph;
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. */
77 u8_to_tex_fragments (const char **s, size_t *len, struct hmap *macros)
79 const uint8_t *u = (const uint8_t *) *s;
80 size_t clen = u8_mblen (u, *len);
83 u8_mbtouc (&puc, u, clen);
88 return code_point_to_tex (puc, macros);