groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Groff] unicode support, part 8: comments and tweaks for glyph API


From: Bruno Haible
Subject: [Groff] unicode support, part 8: comments and tweaks for glyph API
Date: Wed, 15 Feb 2006 13:24:50 +0100
User-agent: KMail/1.5

Hi,

Before freezing the glyph API, let me add more naming fixes and comments.

Also, move some code from the name_to_glyph function to inside the
character_indexer class. (This code is needed because
character_indexer::ascii_char_index doesn't store the "charNNN" glyphs in the
by-name table: a detail of the character_indexer class that shouldn't matter
outside of this class.)


2006-02-15  Bruno Haible  <address@hidden>

        * src/libs/libgroff/nametoindex.cpp (character_indexer): Rename
        methods and fields from *_index to *_glyph.
        (character_indexer::named_char_glyph): Test for "charNNN" name here...
        (name_to_glyph): ... not here.

diff -r -c3 groff-20060213.orig/src/libs/libgroff/nametoindex.cpp 
groff-20060213/src/libs/libgroff/nametoindex.cpp
--- groff-20060213.orig/src/libs/libgroff/nametoindex.cpp       2006-02-15 
03:21:39.000000000 +0100
+++ groff-20060213/src/libs/libgroff/nametoindex.cpp    2006-02-15 
03:39:26.000000000 +0100
@@ -39,26 +39,33 @@
   friend class glyph;
 };
 
+// PTABLE(charinfo) is a hash table mapping `const char *' to 'charinfo *'.
 declare_ptable(charinfo)
 implement_ptable(charinfo)
 
+// ITABLE(charinfo) is a hash table mapping `int >= 0' to 'charinfo *'.
 declare_itable(charinfo)
 implement_itable(charinfo)
 
+// This class is as a registry storing all named and numbered glyphs known
+// so far, and assigns a unique index to each glyph.
 class character_indexer {
 public:
   character_indexer();
   ~character_indexer();
-  glyph ascii_char_index(unsigned char);
-  glyph named_char_index(const char *);
-  glyph numbered_char_index(int);
+  // --------------------- Lookup or creation of a glyph.
+  glyph ascii_char_glyph(unsigned char);
+  glyph named_char_glyph(const char *);
+  glyph numbered_char_glyph(int);
 private:
+  int next_index;              // Number of glyphs already allocated.
+  PTABLE(charinfo) table;      // Table mapping name to glyph.
+  glyph ascii_glyph[256];      // Shorthand table for looking up "charNNN"
+                               // glyphs.
+  ITABLE(charinfo) ntable;     // Table mapping number to glyph.
   enum { NSMALL = 256 };
-  int next_index;
-  glyph ascii_index[256];
-  glyph small_number_index[NSMALL];
-  PTABLE(charinfo) table;
-  ITABLE(charinfo) ntable;
+  glyph small_number_glyph[NSMALL]; // Shorthand table for looking up
+                               // numbered glyphs with small numbers.
 };
 
 character_indexer::character_indexer()
@@ -66,18 +73,18 @@
 {
   int i;
   for (i = 0; i < 256; i++)
-    ascii_index[i] = UNDEFINED_GLYPH;
+    ascii_glyph[i] = UNDEFINED_GLYPH;
   for (i = 0; i < NSMALL; i++)
-    small_number_index[i] = UNDEFINED_GLYPH;
+    small_number_glyph[i] = UNDEFINED_GLYPH;
 }
 
 character_indexer::~character_indexer()
 {
 }
 
-glyph character_indexer::ascii_char_index(unsigned char c)
+glyph character_indexer::ascii_char_glyph(unsigned char c)
 {
-  if (ascii_index[c] == UNDEFINED_GLYPH) {
+  if (ascii_glyph[c] == UNDEFINED_GLYPH) {
     char buf[4+3+1];
     memcpy(buf, "char", 4);
     strcpy(buf + 4, i_to_a(c));
@@ -85,22 +92,42 @@
     ci->index = next_index++;
     ci->number = -1;
     ci->name = strsave(buf);
-    ascii_index[c] = glyph(ci);
+    ascii_glyph[c] = glyph(ci);
   }
-  return ascii_index[c];
+  return ascii_glyph[c];
 }
 
-glyph character_indexer::numbered_char_index(int n)
+inline glyph character_indexer::named_char_glyph(const char *s)
+{
+  /* Glyphs with name "charNNN" are only stored in ascii_glyph[], not
+     in the table.  Therefore treat them specially here.  */
+  if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
+    char *val;
+    long n = strtol(s + 4, &val, 10);
+    if (val != s + 4 && *val == '\0' && n >= 0 && n < 256)
+      return ascii_char_glyph((unsigned char)n);
+  }
+  charinfo *ci = table.lookupassoc(&s);
+  if (ci == NULL) {
+    ci = new charinfo[1];
+    ci->index = next_index++;
+    ci->number = -1;
+    ci->name = table.define(s, ci);
+  }
+  return glyph(ci);
+}
+
+inline glyph character_indexer::numbered_char_glyph(int n)
 {
   if (n >= 0 && n < NSMALL) {
-    if (small_number_index[n] == UNDEFINED_GLYPH) {
+    if (small_number_glyph[n] == UNDEFINED_GLYPH) {
       charinfo *ci = new charinfo;
       ci->index = next_index++;
       ci->number = n;
       ci->name = NULL;
-      small_number_index[n] = glyph(ci);
+      small_number_glyph[n] = glyph(ci);
     }
-    return small_number_index[n];
+    return small_number_glyph[n];
   }
   charinfo *ci = ntable.lookup(n);
   if (ci == NULL) {
@@ -113,38 +140,20 @@
   return glyph(ci);
 }
 
-glyph character_indexer::named_char_index(const char *s)
-{
-  charinfo *ci = table.lookupassoc(&s);
-  if (ci == NULL) {
-    ci = new charinfo[1];
-    ci->index = next_index++;
-    ci->number = -1;
-    ci->name = table.define(s, ci);
-  }
-  return glyph(ci);
-}
-
 static character_indexer indexer;
 
 glyph number_to_glyph(int n)
 {
-  return indexer.numbered_char_index(n);
+  return indexer.numbered_char_glyph(n);
 }
 
 glyph name_to_glyph(const char *s)
 {
   assert(s != 0 && s[0] != '\0' && s[0] != ' ');
   if (s[1] == '\0')
-    return indexer.ascii_char_index(s[0]);
-  /* char128 and \200 are synonyms */
-  if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
-    char *val;
-    long n = strtol(s + 4, &val, 10);
-    if (val != s + 4 && *val == '\0' && n >= 0 && n < 256)
-      return indexer.ascii_char_index((unsigned char)n);
-  }
-  return indexer.named_char_index(s);
+    /* \200 and char128 are synonyms */
+    return indexer.ascii_char_glyph(s[0]);
+  return indexer.named_char_glyph(s);
 }
 
 const char *glyph::glyph_name()





reply via email to

[Prev in Thread] Current Thread [Next in Thread]