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 "compose.hpp"
24 #include <boost/algorithm/string.hpp>
31 using namespace boost;
33 /** Construct a Fraction from a string of the form <numerator> <denominator>
36 Fraction::Fraction (string s)
39 split (b, s, is_any_of (" "));
41 boost::throw_exception (XMLError ("malformed fraction " + s + " in XML node"));
43 numerator = raw_convert<int> (b[0]);
44 denominator = raw_convert<int> (b[1]);
48 Fraction::as_string () const
50 return String::compose ("%1 %2", numerator, denominator);
54 dcp::operator== (Fraction const & a, Fraction const & b)
56 return (a.numerator == b.numerator && a.denominator == b.denominator);
60 dcp::operator!= (Fraction const & a, Fraction const & b)
62 return (a.numerator != b.numerator || a.denominator != b.denominator);
66 dcp::operator<< (ostream& s, Fraction const & f)
68 s << f.numerator << "/" << f.denominator;
72 /** Construct a Colour, initialising it to black. */
81 /** Construct a Colour from R, G and B. The values run between
84 Colour::Colour (int r_, int g_, int b_)
92 /** Construct a Colour from an ARGB hex string; the alpha value is ignored.
93 * @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
96 Colour::Colour (string argb_hex)
99 if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) != 4) {
100 boost::throw_exception (XMLError ("could not parse colour string"));
104 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
105 * hex value. The alpha value will always be FF (ie 255; maximum alpha).
108 Colour::to_argb_string () const
113 << setw(2) << setfill('0') << r
114 << setw(2) << setfill('0') << g
115 << setw(2) << setfill('0') << b;
122 /** operator== for Colours.
123 * @param a First colour to compare.
124 * @param b Second colour to compare.
127 dcp::operator== (Colour const & a, Colour const & b)
129 return (a.r == b.r && a.g == b.g && a.b == b.b);
132 /** operator!= for Colours.
133 * @param a First colour to compare.
134 * @param b Second colour to compare.
137 dcp::operator!= (Colour const & a, Colour const & b)
143 dcp::operator<< (ostream& s, Colour const & c)
145 s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
150 dcp::effect_to_string (Effect e)
161 boost::throw_exception (MiscError ("unknown effect type"));
165 dcp::string_to_effect (string s)
169 } else if (s == "border") {
171 } else if (s == "shadow") {
175 boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
179 dcp::halign_to_string (HAlign h)
190 boost::throw_exception (MiscError ("unknown subtitle halign type"));
194 dcp::string_to_halign (string s)
198 } else if (s == "center") {
199 return HALIGN_CENTER;
200 } else if (s == "right") {
204 boost::throw_exception (DCPReadError ("unknown subtitle halign type"));
208 dcp::valign_to_string (VAlign v)
219 boost::throw_exception (MiscError ("unknown subtitle valign type"));
223 dcp::string_to_valign (string s)
227 } else if (s == "center") {
228 return VALIGN_CENTER;
229 } else if (s == "bottom") {
230 return VALIGN_BOTTOM;
233 boost::throw_exception (DCPReadError ("unknown subtitle valign type"));
237 dcp::direction_to_string (Direction v)
250 boost::throw_exception (MiscError ("unknown subtitle direction type"));
254 dcp::string_to_direction (string s)
256 if (s == "ltr" || s == "horizontal") {
257 return DIRECTION_LTR;
258 } else if (s == "rtl") {
259 return DIRECTION_RTL;
260 } else if (s == "ttb" || s == "vertical") {
261 return DIRECTION_TTB;
262 } else if (s == "btt") {
263 return DIRECTION_BTT;
266 boost::throw_exception (DCPReadError ("unknown subtitle direction type"));