2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
34 #include "raw_convert.h"
36 #include "exceptions.h"
37 #include "compose.hpp"
38 #include <boost/algorithm/string.hpp>
45 using namespace boost;
47 /** Construct a Fraction from a string of the form <numerator> <denominator>
50 Fraction::Fraction (string s)
53 split (b, s, is_any_of (" "));
55 boost::throw_exception (XMLError ("malformed fraction " + s + " in XML node"));
57 numerator = raw_convert<int> (b[0]);
58 denominator = raw_convert<int> (b[1]);
62 Fraction::as_string () const
64 return String::compose ("%1 %2", numerator, denominator);
68 dcp::operator== (Fraction const & a, Fraction const & b)
70 return (a.numerator == b.numerator && a.denominator == b.denominator);
74 dcp::operator!= (Fraction const & a, Fraction const & b)
76 return (a.numerator != b.numerator || a.denominator != b.denominator);
80 dcp::operator<< (ostream& s, Fraction const & f)
82 s << f.numerator << "/" << f.denominator;
86 /** Construct a Colour, initialising it to black. */
95 /** Construct a Colour from R, G and B. The values run between
98 Colour::Colour (int r_, int g_, int b_)
106 /** Construct a Colour from an ARGB hex string; the alpha value is ignored.
107 * @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
110 Colour::Colour (string argb_hex)
113 if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) != 4) {
114 boost::throw_exception (XMLError ("could not parse colour string"));
118 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
119 * hex value. The alpha value will always be FF (ie 255; maximum alpha).
122 Colour::to_argb_string () const
124 locked_stringstream s;
127 << setw(2) << setfill('0') << r
128 << setw(2) << setfill('0') << g
129 << setw(2) << setfill('0') << b;
136 /** operator== for Colours.
137 * @param a First colour to compare.
138 * @param b Second colour to compare.
141 dcp::operator== (Colour const & a, Colour const & b)
143 return (a.r == b.r && a.g == b.g && a.b == b.b);
146 /** operator!= for Colours.
147 * @param a First colour to compare.
148 * @param b Second colour to compare.
151 dcp::operator!= (Colour const & a, Colour const & b)
157 dcp::operator<< (ostream& s, Colour const & c)
159 s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
164 dcp::effect_to_string (Effect e)
175 boost::throw_exception (MiscError ("unknown effect type"));
179 dcp::string_to_effect (string s)
183 } else if (s == "border") {
185 } else if (s == "shadow") {
189 boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
193 dcp::halign_to_string (HAlign h)
204 boost::throw_exception (MiscError ("unknown subtitle halign type"));
208 dcp::string_to_halign (string s)
212 } else if (s == "center") {
213 return HALIGN_CENTER;
214 } else if (s == "right") {
218 boost::throw_exception (DCPReadError ("unknown subtitle halign type"));
222 dcp::valign_to_string (VAlign v)
233 boost::throw_exception (MiscError ("unknown subtitle valign type"));
237 dcp::string_to_valign (string s)
241 } else if (s == "center") {
242 return VALIGN_CENTER;
243 } else if (s == "bottom") {
244 return VALIGN_BOTTOM;
247 boost::throw_exception (DCPReadError ("unknown subtitle valign type"));
251 dcp::direction_to_string (Direction v)
264 boost::throw_exception (MiscError ("unknown subtitle direction type"));
268 dcp::string_to_direction (string s)
270 if (s == "ltr" || s == "horizontal") {
271 return DIRECTION_LTR;
272 } else if (s == "rtl") {
273 return DIRECTION_RTL;
274 } else if (s == "ttb" || s == "vertical") {
275 return DIRECTION_TTB;
276 } else if (s == "btt") {
277 return DIRECTION_BTT;
280 boost::throw_exception (DCPReadError ("unknown subtitle direction type"));