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/>.
21 #include "raw_convert.h"
23 #include "exceptions.h"
24 #include "compose.hpp"
25 #include <boost/algorithm/string.hpp>
32 using namespace boost;
34 /** Construct a Fraction from a string of the form <numerator> <denominator>
37 Fraction::Fraction (string s)
40 split (b, s, is_any_of (" "));
42 boost::throw_exception (XMLError ("malformed fraction " + s + " in XML node"));
44 numerator = raw_convert<int> (b[0]);
45 denominator = raw_convert<int> (b[1]);
49 Fraction::as_string () const
51 return String::compose ("%1 %2", numerator, denominator);
55 dcp::operator== (Fraction const & a, Fraction const & b)
57 return (a.numerator == b.numerator && a.denominator == b.denominator);
61 dcp::operator!= (Fraction const & a, Fraction const & b)
63 return (a.numerator != b.numerator || a.denominator != b.denominator);
67 dcp::operator<< (ostream& s, Fraction const & f)
69 s << f.numerator << "/" << f.denominator;
73 /** Construct a Colour, initialising it to black. */
82 /** Construct a Colour from R, G and B. The values run between
85 Colour::Colour (int r_, int g_, int b_)
93 /** Construct a Colour from an ARGB hex string; the alpha value is ignored.
94 * @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
97 Colour::Colour (string argb_hex)
100 if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) != 4) {
101 boost::throw_exception (XMLError ("could not parse colour string"));
105 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
106 * hex value. The alpha value will always be FF (ie 255; maximum alpha).
109 Colour::to_argb_string () const
114 << setw(2) << setfill('0') << r
115 << setw(2) << setfill('0') << g
116 << setw(2) << setfill('0') << b;
123 /** operator== for Colours.
124 * @param a First colour to compare.
125 * @param b Second colour to compare.
128 dcp::operator== (Colour const & a, Colour const & b)
130 return (a.r == b.r && a.g == b.g && a.b == b.b);
133 /** operator!= for Colours.
134 * @param a First colour to compare.
135 * @param b Second colour to compare.
138 dcp::operator!= (Colour const & a, Colour const & b)
144 dcp::operator<< (ostream& s, Colour const & c)
146 s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
151 dcp::effect_to_string (Effect e)
162 boost::throw_exception (MiscError ("unknown effect type"));
166 dcp::string_to_effect (string s)
170 } else if (s == "border") {
172 } else if (s == "shadow") {
176 boost::throw_exception (DCPReadError ("unknown subtitle effect type"));
180 dcp::halign_to_string (HAlign h)
191 boost::throw_exception (MiscError ("unknown subtitle halign type"));
195 dcp::string_to_halign (string s)
199 } else if (s == "center") {
200 return HALIGN_CENTER;
201 } else if (s == "right") {
205 boost::throw_exception (DCPReadError ("unknown subtitle halign type"));
209 dcp::valign_to_string (VAlign v)
220 boost::throw_exception (MiscError ("unknown subtitle valign type"));
224 dcp::string_to_valign (string s)
228 } else if (s == "center") {
229 return VALIGN_CENTER;
230 } else if (s == "bottom") {
231 return VALIGN_BOTTOM;
234 boost::throw_exception (DCPReadError ("unknown subtitle valign type"));
238 dcp::direction_to_string (Direction v)
251 boost::throw_exception (MiscError ("unknown subtitle direction type"));
255 dcp::string_to_direction (string s)
257 if (s == "ltr" || s == "horizontal") {
258 return DIRECTION_LTR;
259 } else if (s == "rtl") {
260 return DIRECTION_RTL;
261 } else if (s == "ttb" || s == "vertical") {
262 return DIRECTION_TTB;
263 } else if (s == "btt") {
264 return DIRECTION_BTT;
267 boost::throw_exception (DCPReadError ("unknown subtitle direction type"));