Fix various SNAFUs with Font ID handling.
[libdcp.git] / src / types.cc
index c97fb7b402d3188857fea48d9b523c8585e13bd6..687e90f81fa21f3b9d56195e892bd63fbe4bd6fb 100644 (file)
@@ -20,6 +20,7 @@
 #include "raw_convert.h"
 #include "types.h"
 #include "exceptions.h"
+#include "compose.hpp"
 #include <boost/algorithm/string.hpp>
 #include <vector>
 #include <cstdio>
@@ -43,6 +44,12 @@ Fraction::Fraction (string s)
        denominator = raw_convert<int> (b[1]);
 }
 
+string
+Fraction::as_string () const
+{
+       return String::compose ("%1 %2", numerator, denominator);
+}
+
 bool
 dcp::operator== (Fraction const & a, Fraction const & b)
 {
@@ -55,6 +62,13 @@ dcp::operator!= (Fraction const & a, Fraction const & b)
        return (a.numerator != b.numerator || a.denominator != b.denominator);
 }
 
+ostream&
+dcp::operator<< (ostream& s, Fraction const & f)
+{
+       s << f.numerator << "/" << f.denominator;
+       return s;
+}
+
 /** Construct a Colour, initialising it to black. */
 Colour::Colour ()
        : r (0)
@@ -82,7 +96,7 @@ Colour::Colour (int r_, int g_, int b_)
 Colour::Colour (string argb_hex)
 {
        int alpha;
-       if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) < 4) {
+       if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) != 4) {
                boost::throw_exception (XMLError ("could not parse colour string"));
        }
 }
@@ -161,33 +175,60 @@ dcp::string_to_effect (string s)
        boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
 }
 
+string
+dcp::halign_to_string (HAlign h)
+{
+       switch (h) {
+       case HALIGN_LEFT:
+               return "left";
+       case HALIGN_CENTER:
+               return "center";
+       case HALIGN_RIGHT:
+               return "right";
+       }
+
+       boost::throw_exception (MiscError ("unknown subtitle halign type"));
+}
+
+HAlign
+dcp::string_to_halign (string s)
+{
+       if (s == "left") {
+               return HALIGN_LEFT;
+       } else if (s == "center") {
+               return HALIGN_CENTER;
+       } else if (s == "right") {
+               return HALIGN_RIGHT;
+       }
+
+       boost::throw_exception (DCPReadError ("unknown subtitle halign type"));
+}
+
 string
 dcp::valign_to_string (VAlign v)
 {
        switch (v) {
-       case TOP:
+       case VALIGN_TOP:
                return "top";
-       case CENTER:
+       case VALIGN_CENTER:
                return "center";
-       case BOTTOM:
+       case VALIGN_BOTTOM:
                return "bottom";
        }
 
-       boost::throw_exception (MiscError ("unknown valign type"));
+       boost::throw_exception (MiscError ("unknown subtitle valign type"));
 }
 
 VAlign
 dcp::string_to_valign (string s)
 {
        if (s == "top") {
-               return TOP;
+               return VALIGN_TOP;
        } else if (s == "center") {
-               return CENTER;
+               return VALIGN_CENTER;
        } else if (s == "bottom") {
-               return BOTTOM;
+               return VALIGN_BOTTOM;
        }
-       
+
        boost::throw_exception (DCPReadError ("unknown subtitle valign type"));
 }
-
-