Add halign to SubtitleString's operator<< and use it for dcpinfo.
[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 h_position,
39         HAlign h_align,
40         float v_position,
41         VAlign v_align,
42         string text,
43         Effect effect,
44         Colour effect_colour,
45         Time fade_up_time,
46         Time fade_down_time
47         )
48         : _font (font)
49         , _italic (italic)
50         , _colour (colour)
51         , _size (size)
52         , _aspect_adjust (aspect_adjust)
53         , _in (in)
54         , _out (out)
55         , _h_position (h_position)
56         , _h_align (h_align)
57         , _v_position (v_position)
58         , _v_align (v_align)
59         , _text (text)
60         , _effect (effect)
61         , _effect_colour (effect_colour)
62         , _fade_up_time (fade_up_time)
63         , _fade_down_time (fade_down_time)
64 {
65
66 }
67
68 int
69 SubtitleString::size_in_pixels (int screen_height) const
70 {
71         /* Size in the subtitle file is given in points as if the screen
72            height is 11 inches, so a 72pt font would be 1/11th of the screen
73            height.
74         */
75         
76         return _size * screen_height / (11 * 72);
77 }
78
79 bool
80 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
81 {
82         return (
83                 a.font() == b.font() &&
84                 a.italic() == b.italic() &&
85                 a.colour() == b.colour() &&
86                 a.size() == b.size() &&
87                 fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
88                 a.in() == b.in() &&
89                 a.out() == b.out() &&
90                 a.h_position() == b.h_position() &&
91                 a.h_align() == b.h_align() &&
92                 a.v_position() == b.v_position() &&
93                 a.v_align() == b.v_align() &&
94                 a.text() == b.text() &&
95                 a.effect() == b.effect() &&
96                 a.effect_colour() == b.effect_colour() &&
97                 a.fade_up_time() == b.fade_up_time() &&
98                 a.fade_down_time() == b.fade_down_time()
99                 );
100 }
101
102 ostream&
103 dcp::operator<< (ostream& s, SubtitleString const & sub)
104 {
105         s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
106           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
107           << "font " << sub.font().get_value_or ("[default]") << ", ";
108
109         if (sub.italic()) {
110                 s << "italic";
111         } else {
112                 s << "non-italic";
113         }
114         
115         s << ", size " << sub.size() << ", aspect " << sub.aspect_adjust() << ", colour " << sub.colour()
116           << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
117           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
118           << ", effect " << ((int) sub.effect()) << ", effect colour " << sub.effect_colour();
119
120         return s;
121 }