2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <boost/lexical_cast.hpp>
24 #include <boost/algorithm/string.hpp>
26 #include "exceptions.h"
27 #include "raw_convert.h"
30 using namespace libdcp;
31 using namespace boost;
33 Fraction::Fraction (string s)
36 split (b, s, is_any_of (" "));
38 boost::throw_exception (XMLError ("malformed fraction " + s + " in XML node"));
40 numerator = raw_convert<int> (b[0]);
41 denominator = raw_convert<int> (b[1]);
45 libdcp::operator== (Fraction const & a, Fraction const & b)
47 return (a.numerator == b.numerator && a.denominator == b.denominator);
51 libdcp::operator!= (Fraction const & a, Fraction const & b)
53 return (a.numerator != b.numerator || a.denominator != b.denominator);
64 Color::Color (int r_, int g_, int b_)
72 /** Construct a Color from an ARGB hex string; the alpha value is ignored.
73 * @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
76 Color::Color (string argb_hex)
79 if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) < 4) {
80 boost::throw_exception (XMLError ("could not parse colour string"));
84 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
85 * hex value. The alpha value will always be FF (ie 255; maximum alpha).
88 Color::to_argb_string () const
93 << setw(2) << setfill('0') << r
94 << setw(2) << setfill('0') << g
95 << setw(2) << setfill('0') << b;
102 /** operator== for Colors.
103 * @param a First color to compare.
104 * @param b Second color to compare.
107 libdcp::operator== (Color const & a, Color const & b)
109 return (a.r == b.r && a.g == b.g && a.b == b.b);
112 /** operator!= for Colors.
113 * @param a First color to compare.
114 * @param b Second color to compare.
117 libdcp::operator!= (Color const & a, Color const & b)
123 libdcp::operator<< (ostream& s, Color const & c)
125 s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
130 libdcp::effect_to_string (Effect e)
141 boost::throw_exception (MiscError ("unknown effect type"));
145 libdcp::string_to_effect (string s)
149 } else if (s == "border") {
151 } else if (s == "shadow") {
155 boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
159 libdcp::valign_to_string (VAlign v)
164 case VERTICAL_CENTER:
166 case VERTICAL_BOTTOM:
170 boost::throw_exception (MiscError ("unknown valign type"));
174 libdcp::string_to_valign (string s)
178 } else if (s == "center") {
179 return VERTICAL_CENTER;
180 } else if (s == "bottom") {
181 return VERTICAL_BOTTOM;
184 boost::throw_exception (DCPReadError ("unknown subtitle valign type"));
188 libdcp::halign_to_string (HAlign h)
191 case HORIZONTAL_LEFT:
193 case HORIZONTAL_CENTER:
195 case HORIZONTAL_RIGHT:
199 boost::throw_exception (MiscError ("unknown halign type"));
203 libdcp::string_to_halign (string s)
206 return HORIZONTAL_LEFT;
207 } else if (s == "center") {
208 return HORIZONTAL_CENTER;
209 } else if (s == "right") {
210 return HORIZONTAL_RIGHT;
213 boost::throw_exception (DCPReadError ("unknown subtitle halign type"));