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