From 2694601eaa8230667a2fed44f36b7cf066b095b8 Mon Sep 17 00:00:00 2001 From: Werner LEMBERG Date: Sat, 11 Feb 2006 17:54:28 +0000 Subject: [PATCH] New accessor method glyph_t::glyph_name(). * src/include/ptable.h (declare_ptable): Add a return value to the 'define' method, and declare a 'lookupassoc' method. (implement_ptable): Return the stored key in 'define'. Implement lookupassoc. * src/include/font.h (glyph_t): Add 'name' field. Add an argument to the constructor. (glyph_t::glyph_name): New method. * src/libs/libgroff/nametoindex.cpp (character_indexer): Change return type of methods and field member type to glyph_t. (character_indexer::character_indexer): Update. (character_indexer::ascii_char_index): Allocate a name for the glyph. Return a glyph_t with name. (character_indexer::numbered_char_index): Return a glyph_t without a name. (character_indexer::named_char_index): Return a glyph_t with a name. (font::number_to_index, font::name_to_index): Update. * src/roff/troff/input.cpp (charinfo::charinfo): Use the symbol as the glyph's name. --- ChangeLog | 22 ++++++++++++++++++ src/include/font.h | 24 ++++++++++++++++---- src/include/ptable.h | 34 +++++++++++++++++++++++---- src/libs/libgroff/nametoindex.cpp | 48 +++++++++++++++++++++++---------------- src/roff/troff/input.cpp | 2 +- 5 files changed, 100 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9dd4491d..11cb8eb6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,27 @@ 2006-02-11 Bruno Haible + New accessor method glyph_t::glyph_name(). + * src/include/ptable.h (declare_ptable): Add a return value to the + 'define' method, and declare a 'lookupassoc' method. + (implement_ptable): Return the stored key in 'define'. Implement + lookupassoc. + * src/include/font.h (glyph_t): Add 'name' field. Add an argument to + the constructor. + (glyph_t::glyph_name): New method. + * src/libs/libgroff/nametoindex.cpp (character_indexer): Change + return type of methods and field member type to glyph_t. + (character_indexer::character_indexer): Update. + (character_indexer::ascii_char_index): Allocate a name for the glyph. + Return a glyph_t with name. + (character_indexer::numbered_char_index): Return a glyph_t without a + name. + (character_indexer::named_char_index): Return a glyph_t with a name. + (font::number_to_index, font::name_to_index): Update. + * src/roff/troff/input.cpp (charinfo::charinfo): Use the symbol as + the glyph's name. + +2006-02-11 Bruno Haible + * src/devices/grotty/tty.cpp (output_character_t): New type. (tty_printer::make_bold, tty_printer::add_char, tty_printer::put_char): Change argument type to output_character_t. diff --git a/src/include/font.h b/src/include/font.h index 43b95c93..05918114 100644 --- a/src/include/font.h +++ b/src/include/font.h @@ -29,33 +29,42 @@ typedef void (*FONT_COMMAND_HANDLER)(const char *, // command // A glyph is represented by a font-independent glyph_t object. // The functions font::name_to_index and font::number_to_index return such // an object. +// There are two types of glyphs: +// - those with a name, and among these in particular: +// "charNNN" denoting a single 'char' in the input character set, +// "uXXXX" denoting a Unicode character, +// - those with a number, referring to the the font-dependent glyph with +// the given number. struct glyph_t { private: int index; // A font-independent integer value. + const char *name; // Glyph name, statically allocated. friend class font; + friend class character_indexer; friend class charinfo; - glyph_t(int); // Glyph with given index. + glyph_t(int, const char *); // Glyph with given index and name. public: glyph_t(); // Uninitialized glyph. static glyph_t undefined_glyph(); // Undefined glyph. int glyph_index(); + const char *glyph_name(); int operator==(const glyph_t&) const; int operator!=(const glyph_t&) const; }; -inline glyph_t::glyph_t(int idx) -: index (idx) +inline glyph_t::glyph_t(int idx, const char *nm) +: index (idx), name (nm) { } inline glyph_t::glyph_t() -: index (0xdeadbeef) +: index (0xdeadbeef), name (NULL) { } inline glyph_t glyph_t::undefined_glyph() { - return glyph_t(-1); + return glyph_t(-1, NULL); } #define UNDEFINED_GLYPH glyph_t::undefined_glyph() @@ -64,6 +73,11 @@ inline int glyph_t::glyph_index() return index; } +inline const char *glyph_t::glyph_name() +{ + return name; +} + inline int glyph_t::operator==(const glyph_t &other) const { return index == other.index; diff --git a/src/include/ptable.h b/src/include/ptable.h index fadb158d..b0470e54 100644 --- a/src/include/ptable.h +++ b/src/include/ptable.h @@ -88,11 +88,20 @@ public: \ PTABLE(T)(); /* Create an empty table. */ \ ~PTABLE(T)(); /* Delete a table, including its \ values. */ \ - void define(const char *, T *); /* Define the value (arg2) for a key \ - (arg1). */ \ + const char *define(const char *, T *);/* Define the value (arg2) for a key \ + (arg1). Return the copy in the \ + table of the key (arg1), or \ + possibly NULL if the value (arg2) \ + is NULL. */ \ T *lookup(const char *); /* Return a pointer to the value of \ the given key, if found in the \ table, or NULL otherwise. */ \ + T *lookupassoc(const char **); /* Return a pointer to the value of \ + the given key, passed by reference,\ + and replace the key argument with \ + the copy found in the table, if \ + the key is found in the table. \ + Return NULL otherwise. */ \ friend class PTABLE_ITERATOR(T); \ }; @@ -125,7 +134,7 @@ PTABLE(T)::~PTABLE(T)() \ a_delete v; \ } \ \ -void PTABLE(T)::define(const char *key, T *val) \ +const char *PTABLE(T)::define(const char *key, T *val) \ { \ assert(key != 0); \ unsigned long h = hash_string(key); \ @@ -136,10 +145,10 @@ void PTABLE(T)::define(const char *key, T *val) \ if (strcmp(v[n].key, key) == 0) { \ a_delete v[n].val; \ v[n].val = val; \ - return; \ + return v[n].key; \ } \ if (val == 0) \ - return; \ + return 0; \ if (used*FULL_DEN >= size*FULL_NUM) { \ PASSOC(T) *oldv = v; \ unsigned old_size = size; \ @@ -170,6 +179,7 @@ void PTABLE(T)::define(const char *key, T *val) \ v[n].key = temp; \ v[n].val = val; \ used++; \ + return temp; \ } \ \ T *PTABLE(T)::lookup(const char *key) \ @@ -183,6 +193,20 @@ T *PTABLE(T)::lookup(const char *key) \ return 0; \ } \ \ +T *PTABLE(T)::lookupassoc(const char **keyptr) \ +{ \ + const char *key = *keyptr; \ + assert(key != 0); \ + for (unsigned n = unsigned(hash_string(key) % size); \ + v[n].key != 0; \ + n = (n == 0 ? size - 1 : n - 1)) \ + if (strcmp(v[n].key, key) == 0) { \ + *keyptr = v[n].key; \ + return v[n].val; \ + } \ + return 0; \ +} \ + \ PTABLE_ITERATOR(T)::PTABLE_ITERATOR(T)(PTABLE(T) *t) \ : p(t), i(0) \ { \ diff --git a/src/libs/libgroff/nametoindex.cpp b/src/libs/libgroff/nametoindex.cpp index feca1587..81c02688 100644 --- a/src/libs/libgroff/nametoindex.cpp +++ b/src/libs/libgroff/nametoindex.cpp @@ -36,14 +36,14 @@ class character_indexer { public: character_indexer(); ~character_indexer(); - int ascii_char_index(unsigned char); - int named_char_index(const char *); - int numbered_char_index(int); + glyph_t ascii_char_index(unsigned char); + glyph_t named_char_index(const char *); + glyph_t numbered_char_index(int); private: enum { NSMALL = 256 }; int next_index; - int ascii_index[256]; - int small_number_index[NSMALL]; + glyph_t ascii_index[256]; + glyph_t small_number_index[NSMALL]; PTABLE(int) table; }; @@ -52,45 +52,55 @@ character_indexer::character_indexer() { int i; for (i = 0; i < 256; i++) - ascii_index[i] = -1; + ascii_index[i] = glyph_t(-1, NULL); for (i = 0; i < NSMALL; i++) - small_number_index[i] = -1; + small_number_index[i] = glyph_t(-1, NULL); } character_indexer::~character_indexer() { } -int character_indexer::ascii_char_index(unsigned char c) +glyph_t character_indexer::ascii_char_index(unsigned char c) { - if (ascii_index[c] < 0) - ascii_index[c] = next_index++; + if (ascii_index[c].index < 0) { + char buf[4+3+1]; + memcpy(buf, "char", 4); + strcpy(buf + 4, i_to_a(c)); + ascii_index[c] = glyph_t(next_index++, strsave(buf)); + } return ascii_index[c]; } -int character_indexer::numbered_char_index(int n) +glyph_t character_indexer::numbered_char_index(int n) { if (n >= 0 && n < NSMALL) { - if (small_number_index[n] < 0) - small_number_index[n] = next_index++; + if (small_number_index[n].index < 0) + small_number_index[n] = glyph_t(next_index++, NULL); return small_number_index[n]; } // Not the most efficient possible implementation. - char buf[INT_DIGITS + 3]; + char buf[1 + 1 + INT_DIGITS + 1]; buf[0] = ' '; strcpy(buf + 1, i_to_a(n)); - return named_char_index(buf); + int *np = table.lookup(buf); + if (!np) { + np = new int[1]; + *np = next_index++; + table.define(buf, np); + } + return glyph_t(*np, NULL); } -int character_indexer::named_char_index(const char *s) +glyph_t character_indexer::named_char_index(const char *s) { - int *np = table.lookup(s); + int *np = table.lookupassoc(&s); if (!np) { np = new int[1]; *np = next_index++; - table.define(s, np); + s = table.define(s, np); } - return *np; + return glyph_t(*np, s); } static character_indexer indexer; diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp index dd733397..4afccebb 100644 --- a/src/roff/troff/input.cpp +++ b/src/roff/troff/input.cpp @@ -8094,7 +8094,7 @@ charinfo::charinfo(symbol s) not_found(0), transparent_translate(1), translate_input(0), mode(CHAR_NORMAL), nm(s) { - index = glyph_t(next_index++); + index = glyph_t(next_index++, s.contents()); } void charinfo::set_hyphenation_code(unsigned char c) -- 2.11.4.GIT