Bump libcxml for comment-in-node-data fix.
[libdcp.git] / src / subtitle_string.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "subtitle_string.h"
35 #include "xml.h"
36 #include <cmath>
37
38 using std::string;
39 using std::ostream;
40 using boost::optional;
41 using namespace dcp;
42
43 /** @param v_position Vertical position as a fraction of the screen height (between 0 and 1) from v_align */
44 SubtitleString::SubtitleString (
45         optional<string> font,
46         bool italic,
47         bool bold,
48         bool underline,
49         Colour colour,
50         int size,
51         float aspect_adjust,
52         Time in,
53         Time out,
54         float h_position,
55         HAlign h_align,
56         float v_position,
57         VAlign v_align,
58         Direction direction,
59         string text,
60         Effect effect,
61         Colour effect_colour,
62         Time fade_up_time,
63         Time fade_down_time
64         )
65         : _font (font)
66         , _italic (italic)
67         , _bold (bold)
68         , _underline (underline)
69         , _colour (colour)
70         , _size (size)
71         , _aspect_adjust (aspect_adjust)
72         , _in (in)
73         , _out (out)
74         , _h_position (h_position)
75         , _h_align (h_align)
76         , _v_position (v_position)
77         , _v_align (v_align)
78         , _direction (direction)
79         , _text (text)
80         , _effect (effect)
81         , _effect_colour (effect_colour)
82         , _fade_up_time (fade_up_time)
83         , _fade_down_time (fade_down_time)
84 {
85
86 }
87
88 int
89 SubtitleString::size_in_pixels (int screen_height) const
90 {
91         /* Size in the subtitle file is given in points as if the screen
92            height is 11 inches, so a 72pt font would be 1/11th of the screen
93            height.
94         */
95
96         return _size * screen_height / (11 * 72);
97 }
98
99 bool
100 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
101 {
102         return (
103                 a.font() == b.font() &&
104                 a.italic() == b.italic() &&
105                 a.bold() == b.bold() &&
106                 a.underline() == b.underline() &&
107                 a.colour() == b.colour() &&
108                 a.size() == b.size() &&
109                 fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
110                 a.in() == b.in() &&
111                 a.out() == b.out() &&
112                 a.h_position() == b.h_position() &&
113                 a.h_align() == b.h_align() &&
114                 a.v_position() == b.v_position() &&
115                 a.v_align() == b.v_align() &&
116                 a.direction() == b.direction() &&
117                 a.text() == b.text() &&
118                 a.effect() == b.effect() &&
119                 a.effect_colour() == b.effect_colour() &&
120                 a.fade_up_time() == b.fade_up_time() &&
121                 a.fade_down_time() == b.fade_down_time()
122                 );
123 }
124
125 ostream&
126 dcp::operator<< (ostream& s, SubtitleString const & sub)
127 {
128         s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
129           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
130           << "font " << sub.font().get_value_or ("[default]") << ", ";
131
132         if (sub.italic()) {
133                 s << "italic, ";
134         } else {
135                 s << "non-italic, ";
136         }
137
138         if (sub.bold()) {
139                 s << "bold, ";
140         } else {
141                 s << "normal, ";
142         }
143
144         if (sub.underline()) {
145                 s << "underlined, ";
146         }
147
148         s << "size " << sub.size() << ", aspect " << sub.aspect_adjust() << ", colour " << sub.colour()
149           << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
150           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
151           << ", direction " << ((int) sub.direction())
152           << ", effect " << ((int) sub.effect()) << ", effect colour " << sub.effect_colour();
153
154         return s;
155 }