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.
20 #include "raw_convert.h"
22 #include "exceptions.h"
23 #include <boost/algorithm/string.hpp>
30 using namespace boost;
32 /** Construct a Fraction from a string of the form <numerator> <denominator>
35 Fraction::Fraction (string s)
38 split (b, s, is_any_of (" "));
40 boost::throw_exception (XMLError ("malformed fraction " + s + " in XML node"));
42 numerator = raw_convert<int> (b[0]);
43 denominator = raw_convert<int> (b[1]);
47 dcp::operator== (Fraction const & a, Fraction const & b)
49 return (a.numerator == b.numerator && a.denominator == b.denominator);
53 dcp::operator!= (Fraction const & a, Fraction const & b)
55 return (a.numerator != b.numerator || a.denominator != b.denominator);
58 /** Construct a Colour, initialising it to black. */
67 /** Construct a Colour from R, G and B. The values run between
70 Colour::Colour (int r_, int g_, int b_)
78 /** Construct a Colour from an ARGB hex string; the alpha value is ignored.
79 * @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
82 Colour::Colour (string argb_hex)
85 if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) < 4) {
86 boost::throw_exception (XMLError ("could not parse colour string"));
90 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
91 * hex value. The alpha value will always be FF (ie 255; maximum alpha).
94 Colour::to_argb_string () const
99 << setw(2) << setfill('0') << r
100 << setw(2) << setfill('0') << g
101 << setw(2) << setfill('0') << b;
108 /** operator== for Colours.
109 * @param a First colour to compare.
110 * @param b Second colour to compare.
113 dcp::operator== (Colour const & a, Colour const & b)
115 return (a.r == b.r && a.g == b.g && a.b == b.b);
118 /** operator!= for Colours.
119 * @param a First colour to compare.
120 * @param b Second colour to compare.
123 dcp::operator!= (Colour const & a, Colour const & b)
129 dcp::operator<< (ostream& s, Colour const & c)
131 s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
136 dcp::effect_to_string (Effect e)
147 boost::throw_exception (MiscError ("unknown effect type"));
151 dcp::string_to_effect (string s)
155 } else if (s == "border") {
157 } else if (s == "shadow") {
161 boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
165 dcp::valign_to_string (VAlign v)
176 boost::throw_exception (MiscError ("unknown valign type"));
180 dcp::string_to_valign (string s)
184 } else if (s == "center") {
186 } else if (s == "bottom") {
190 boost::throw_exception (DCPReadError ("unknown subtitle valign type"));