Merge branch '1.0' of git.carlh.net:git/libdcp into 1.0
[libdcp.git] / src / subtitle_string.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "subtitle_string.h"
21 #include "xml.h"
22 #include <cmath>
23
24 using std::string;
25 using std::ostream;
26 using boost::optional;
27 using namespace dcp;
28
29 /** @param v_position Vertical position as a fraction of the screen height (between 0 and 1) from v_align */
30 SubtitleString::SubtitleString (
31         optional<string> font,
32         bool italic,
33         Colour colour,
34         int size,
35         float aspect_adjust,
36         Time in,
37         Time out,
38         float v_position,
39         VAlign v_align,
40         string text,
41         Effect effect,
42         Colour effect_colour,
43         Time fade_up_time,
44         Time fade_down_time
45         )
46         : _font (font)
47         , _italic (italic)
48         , _colour (colour)
49         , _size (size)
50         , _aspect_adjust (aspect_adjust)
51         , _in (in)
52         , _out (out)
53         , _v_position (v_position)
54         , _v_align (v_align)
55         , _text (text)
56         , _effect (effect)
57         , _effect_colour (effect_colour)
58         , _fade_up_time (fade_up_time)
59         , _fade_down_time (fade_down_time)
60 {
61
62 }
63
64 int
65 SubtitleString::size_in_pixels (int screen_height) const
66 {
67         /* Size in the subtitle file is given in points as if the screen
68            height is 11 inches, so a 72pt font would be 1/11th of the screen
69            height.
70         */
71         
72         return _size * screen_height / (11 * 72);
73 }
74
75 bool
76 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
77 {
78         return (
79                 a.font() == b.font() &&
80                 a.italic() == b.italic() &&
81                 a.colour() == b.colour() &&
82                 a.size() == b.size() &&
83                 fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
84                 a.in() == b.in() &&
85                 a.out() == b.out() &&
86                 a.v_position() == b.v_position() &&
87                 a.v_align() == b.v_align() &&
88                 a.text() == b.text() &&
89                 a.effect() == b.effect() &&
90                 a.effect_colour() == b.effect_colour() &&
91                 a.fade_up_time() == b.fade_up_time() &&
92                 a.fade_down_time() == b.fade_down_time()
93                 );
94 }
95
96 ostream&
97 dcp::operator<< (ostream& s, SubtitleString const & sub)
98 {
99         s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
100           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
101           << "font " << sub.font().get_value_or ("[default]") << ", ";
102
103         if (sub.italic()) {
104                 s << "italic";
105         } else {
106                 s << "non-italic";
107         }
108         
109         s << ", size " << sub.size() << ", aspect " << sub.aspect_adjust() << ", colour " << sub.colour()
110           << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
111           << "effect " << ((int) sub.effect()) << ", effect colour " << sub.effect_colour();
112
113         return s;
114 }