2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2006,
4 Free Software Foundation, Inc.
5 Written by James Clark (jjc@jclark.com)
7 This file is part of groff.
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 // Every glyphinfo is actually a charinfo.
34 class charinfo : glyph {
36 const char *name; // The glyph name, or NULL.
37 friend class character_indexer;
40 // PTABLE(charinfo) is a hash table mapping `const char *' to `charinfo *'.
41 declare_ptable(charinfo)
42 implement_ptable(charinfo)
44 // ITABLE(charinfo) is a hash table mapping `int >= 0' to `charinfo *'.
45 declare_itable(charinfo)
46 implement_itable(charinfo)
48 // This class is as a registry storing all named and numbered glyphs known
49 // so far, and assigns a unique index to each glyph.
50 class character_indexer {
54 // --------------------- Lookup or creation of a glyph.
55 glyph *ascii_char_glyph(unsigned char);
56 glyph *named_char_glyph(const char *);
57 glyph *numbered_char_glyph(int);
59 int next_index; // Number of glyphs already allocated.
60 PTABLE(charinfo) table; // Table mapping name to glyph.
61 glyph *ascii_glyph[256]; // Shorthand table for looking up "charNNN"
63 ITABLE(charinfo) ntable; // Table mapping number to glyph.
64 enum { NSMALL = 256 };
65 glyph *small_number_glyph[NSMALL]; // Shorthand table for looking up
66 // numbered glyphs with small numbers.
69 character_indexer::character_indexer()
73 for (i = 0; i < 256; i++)
74 ascii_glyph[i] = UNDEFINED_GLYPH;
75 for (i = 0; i < NSMALL; i++)
76 small_number_glyph[i] = UNDEFINED_GLYPH;
79 character_indexer::~character_indexer()
83 glyph *character_indexer::ascii_char_glyph(unsigned char c)
85 if (ascii_glyph[c] == UNDEFINED_GLYPH) {
87 memcpy(buf, "char", 4);
88 strcpy(buf + 4, i_to_a(c));
89 charinfo *ci = new charinfo;
90 ci->index = next_index++;
92 ci->name = strsave(buf);
95 return ascii_glyph[c];
98 inline glyph *character_indexer::named_char_glyph(const char *s)
100 // Glyphs with name `charNNN' are only stored in ascii_glyph[], not
101 // in the table. Therefore treat them specially here.
102 if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
104 long n = strtol(s + 4, &val, 10);
105 if (val != s + 4 && *val == '\0' && n >= 0 && n < 256)
106 return ascii_char_glyph((unsigned char)n);
108 charinfo *ci = table.lookupassoc(&s);
110 ci = new charinfo[1];
111 ci->index = next_index++;
113 ci->name = table.define(s, ci);
118 inline glyph *character_indexer::numbered_char_glyph(int n)
120 if (n >= 0 && n < NSMALL) {
121 if (small_number_glyph[n] == UNDEFINED_GLYPH) {
122 charinfo *ci = new charinfo;
123 ci->index = next_index++;
126 small_number_glyph[n] = ci;
128 return small_number_glyph[n];
130 charinfo *ci = ntable.lookup(n);
132 ci = new charinfo[1];
133 ci->index = next_index++;
136 ntable.define(n, ci);
141 static character_indexer indexer;
143 glyph *number_to_glyph(int n)
145 return indexer.numbered_char_glyph(n);
148 // troff overrides this function with its own version.
150 glyph *name_to_glyph(const char *s)
152 assert(s != 0 && s[0] != '\0' && s[0] != ' ');
154 // \200 and char128 are synonyms
155 return indexer.ascii_char_glyph(s[0]);
156 return indexer.named_char_glyph(s);
159 const char *glyph_to_name(glyph *g)
161 charinfo *ci = (charinfo *)g; // Every glyph is actually a charinfo.