[API] Add hb_direction_from/to_string()

And hb-view --direction argument.
diff --git a/TODO b/TODO
index 92269d0..941a8c8 100644
--- a/TODO
+++ b/TODO
@@ -50,7 +50,6 @@
 hb-view enhancements:
 ====================
 
-- Add --output
 - Add --format
 - Add --width, --height, --auto-size, --align, etc?
 - Port to GOption, --help
diff --git a/src/hb-common.cc b/src/hb-common.cc
index 5872678..1a6f518 100644
--- a/src/hb-common.cc
+++ b/src/hb-common.cc
@@ -51,6 +51,42 @@
 }
 
 
+/* 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 {
diff --git a/src/hb-common.h b/src/hb-common.h
index c496dcf..e6f5f2b 100644
--- a/src/hb-common.h
+++ b/src/hb-common.h
@@ -92,6 +92,12 @@
   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)
diff --git a/src/hb-view.cc b/src/hb-view.cc
index fdf0b80..7d936dd 100644
--- a/src/hb-view.cc
+++ b/src/hb-view.cc
@@ -57,8 +57,9 @@
 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 @@
       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 @@
 	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_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)
diff --git a/test/test-common.c b/test/test-common.c
index 96607d2..d232740 100644
--- a/test/test-common.c
+++ b/test/test-common.c
@@ -77,6 +77,15 @@
   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 @@
 
   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);