[API] Add hb_direction_from/to_string()
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 27 Apr 2011 18:48:19 +0000 (14:48 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Wed, 27 Apr 2011 18:48:19 +0000 (14:48 -0400)
And hb-view --direction argument.

TODO
src/hb-common.cc
src/hb-common.h
src/hb-view.cc
test/test-common.c

diff --git a/TODO b/TODO
index 92269d0..941a8c8 100644 (file)
--- a/TODO
+++ b/TODO
@@ -50,7 +50,6 @@ API to add (maybe after 1.0):
 hb-view enhancements:
 ====================
 
-- Add --output
 - Add --format
 - Add --width, --height, --auto-size, --align, etc?
 - Port to GOption, --help
index 5872678..1a6f518 100644 (file)
@@ -51,6 +51,42 @@ hb_tag_from_string (const char *s)
 }
 
 
+/* hb_direction_t */
+
+const char direction_strings[][4] = {
+  "ltr",
+  "rtl",
+  "ttb",
+  "btt"
+};
+
+hb_direction_t
+hb_direction_from_string (const char *str)
+{
+  if (unlikely (!str || !*str))
+    return HB_DIRECTION_INVALID;
+
+  /* Lets match loosely: just match the first letter, such that
+   * all of "ltr", "left-to-right", etc work!
+   */
+  char c = TOLOWER (str[0]);
+  for (unsigned int i = 0; i < ARRAY_LENGTH (direction_strings); i++)
+    if (c == direction_strings[i][0])
+      return (hb_direction_t) i;
+
+  return HB_DIRECTION_INVALID;
+}
+
+const char *
+hb_direction_to_string (hb_direction_t direction)
+{
+  if (likely ((unsigned int) direction < ARRAY_LENGTH (direction_strings)))
+    return direction_strings[direction];
+
+  return "invalid";
+}
+
+
 /* hb_language_t */
 
 struct _hb_language_t {
index c496dcf..e6f5f2b 100644 (file)
@@ -92,6 +92,12 @@ typedef enum _hb_direction_t {
   HB_DIRECTION_BTT
 } hb_direction_t;
 
+hb_direction_t
+hb_direction_from_string (const char *str);
+
+const char *
+hb_direction_to_string (hb_direction_t direction);
+
 #define HB_DIRECTION_IS_HORIZONTAL(dir)        ((((unsigned int) (dir)) & ~1U) == 0)
 #define HB_DIRECTION_IS_VERTICAL(dir)  ((((unsigned int) (dir)) & ~1U) == 2)
 #define HB_DIRECTION_IS_FORWARD(dir)   ((((unsigned int) (dir)) & ~2U) == 0)
index fdf0b80..7d936dd 100644 (file)
@@ -57,8 +57,9 @@ static const char *back = "#ffffff";
 static const char *text = NULL;
 static const char *font_file = NULL;
 static const char *out_file = "/dev/stdout";
-static const char *language = NULL;
+static const char *direction = NULL;
 static const char *script = NULL;
+static const char *language = NULL;
 static hb_feature_t *features = NULL;
 static unsigned int num_features;
 static hb_bool_t debug = FALSE;
@@ -100,6 +101,7 @@ parse_opts (int argc, char **argv)
       static struct option long_options[] = {
        {"background", 1, 0, 'B'},
        {"debug", 0, &debug, TRUE},
+       {"direction", 1, 0, 'd'},
        {"features", 1, 0, 'f'},
        {"font-size", 1, 0, 's'},
        {"foreground", 1, 0, 'F'},
@@ -152,12 +154,15 @@ parse_opts (int argc, char **argv)
        case 't':
          text = optarg;
          break;
-       case 'L':
-         language = optarg;
+       case 'd':
+         direction = optarg;
          break;
        case 'S':
          script = optarg;
          break;
+       case 'L':
+         language = optarg;
+         break;
        case 'o':
          out_file = optarg;
          break;
@@ -350,6 +355,8 @@ _hb_cr_text_glyphs (cairo_t *cr,
 
   hb_buffer = hb_buffer_create (0);
 
+  if (direction)
+    hb_buffer_set_direction (hb_buffer, hb_direction_from_string (direction));
   if (script)
     hb_buffer_set_script (hb_buffer, hb_script_from_string (script));
   if (language)
index 96607d2..d232740 100644 (file)
@@ -77,6 +77,15 @@ test_types_direction (void)
   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_RTL), ==, HB_DIRECTION_LTR);
   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_TTB), ==, HB_DIRECTION_BTT);
   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_BTT), ==, HB_DIRECTION_TTB);
+
+  g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string (NULL));
+  g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string (""));
+  g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("x"));
+  g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("r"));
+  g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("rtl"));
+  g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("RtL"));
+  g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("right-to-left"));
+  g_assert_cmpint (HB_DIRECTION_TTB, ==, hb_direction_from_string ("ttb"));
 }
 
 static void
@@ -108,16 +117,26 @@ test_types_script (void)
 
   hb_tag_t x123 = HB_TAG_CHAR4 ("x123");
 
-  g_assert_cmpint ((hb_tag_t) HB_SCRIPT_INVALID, ==, HB_TAG_NONE);
+  g_assert_cmpint (HB_SCRIPT_INVALID, ==, (hb_script_t) HB_TAG_NONE);
   g_assert_cmphex (HB_SCRIPT_ARABIC, !=, HB_SCRIPT_LATIN);
 
+  g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string (NULL));
+  g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string (""));
+  g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x"));
+
+  g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("arab"));
+  g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("Arab"));
+  g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("ARAB"));
+
   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (arab));
   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (Arab));
   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (ARAB));
 
   /* Arbitrary tags that look like may be valid ISO 15924 should be preserved. */
+  g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_string ("wWyZ"));
   g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_iso15924_tag (wWyZ));
   /* Otherwise, UNKNOWN should be returned. */
+  g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x123"));
   g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_iso15924_tag (x123));
 
   g_assert_cmphex (hb_script_to_iso15924_tag (HB_SCRIPT_ARABIC), ==, Arab);