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