Initial work on subtitle writing.
[libdcp.git] / src / types.cc
1 #include <vector>
2 #include <cstdio>
3 #include <iomanip>
4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string.hpp>
6 #include "types.h"
7 #include "exceptions.h"
8
9 using namespace std;
10 using namespace libdcp;
11 using namespace boost;
12
13 Fraction::Fraction (string s)
14 {
15         vector<string> b;
16         split (b, s, is_any_of (" "));
17         if (b.size() != 2) {
18                 throw XMLError ("malformed fraction " + s + " in XML node");
19         }
20         numerator = lexical_cast<int> (b[0]);
21         denominator = lexical_cast<int> (b[1]);
22 }
23
24 bool
25 libdcp::operator== (Fraction const & a, Fraction const & b)
26 {
27         return (a.numerator == b.numerator && a.denominator == b.denominator);
28 }
29
30 bool
31 libdcp::operator!= (Fraction const & a, Fraction const & b)
32 {
33         return (a.numerator != b.numerator || a.denominator != b.denominator);
34 }
35
36 Color::Color ()
37         : r (0)
38         , g (0)
39         , b (0)
40 {
41
42 }
43
44 Color::Color (int r_, int g_, int b_)
45         : r (r_)
46         , g (g_)
47         , b (b_)
48 {
49
50 }
51
52 Color::Color (string argb_hex)
53 {
54         int alpha;
55         if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) < 4) {
56                 throw XMLError ("could not parse colour string");
57         }
58 }
59
60 string
61 Color::to_argb_string () const
62 {
63         stringstream s;
64         s << "FF";
65         s << hex
66           << setw(2) << setfill('0') << r
67           << setw(2) << setfill('0') << g
68           << setw(2) << setfill('0') << b;
69
70         string t = s.str();
71         to_upper (t);
72         return t;
73 }
74
75 bool
76 libdcp::operator== (Color const & a, Color const & b)
77 {
78         return (a.r == b.r && a.g == b.g && a.b == b.b);
79 }
80
81 bool
82 libdcp::operator!= (Color const & a, Color const & b)
83 {
84         return !(a == b);
85 }
86
87 ostream &
88 libdcp::operator<< (ostream& s, Color const & c)
89 {
90         s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
91         return s;
92 }
93
94 string
95 libdcp::effect_to_string (Effect e)
96 {
97         switch (e) {
98         case NONE:
99                 return "none";
100         case BORDER:
101                 return "border";
102         case SHADOW:
103                 return "shadow";
104         }
105
106         throw MiscError ("unknown effect type");
107 }
108
109 Effect
110 libdcp::string_to_effect (string s)
111 {
112         if (s == "none") {
113                 return NONE;
114         } else if (s == "border") {
115                 return BORDER;
116         } else if (s == "shadow") {
117                 return SHADOW;
118         }
119
120         throw DCPReadError ("unknown subtitle effect type");
121 }
122
123 string
124 libdcp::valign_to_string (VAlign v)
125 {
126         switch (v) {
127         case TOP:
128                 return "top";
129         case CENTER:
130                 return "center";
131         case BOTTOM:
132                 return "bottom";
133         }
134
135         throw MiscError ("unknown valign type");
136 }
137
138 VAlign
139 libdcp::string_to_valign (string s)
140 {
141         if (s == "top") {
142                 return TOP;
143         } else if (s == "center") {
144                 return CENTER;
145         } else if (s == "bottom") {
146                 return BOTTOM;
147         }
148         
149         throw DCPReadError ("unknown subtitle valign type");
150 }
151
152