libdcp
subtitle_string.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012-2021 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 
40 #include "subtitle_string.h"
41 #include "xml.h"
42 #include <cmath>
43 
44 
45 using std::max;
46 using std::min;
47 using std::ostream;
48 using std::string;
49 using boost::optional;
50 using namespace dcp;
51 
52 
54  optional<string> font,
55  bool italic,
56  bool bold,
57  bool underline,
58  Colour colour,
59  int size,
60  float aspect_adjust,
61  Time in,
62  Time out,
63  float h_position,
64  HAlign h_align,
65  float v_position,
66  VAlign v_align,
67  Direction direction,
68  string text,
69  Effect effect,
70  Colour effect_colour,
71  Time fade_up_time,
72  Time fade_down_time,
73  float space_before
74  )
75  : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
76  , _font (font)
77  , _italic (italic)
78  , _bold (bold)
79  , _underline (underline)
80  , _colour (colour)
81  , _size (size)
82  , _aspect_adjust (aspect_adjust)
83  , _direction (direction)
84  , _text (text)
85  , _effect (effect)
86  , _effect_colour (effect_colour)
87  , _space_before (space_before)
88 {
89  _aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f);
90 }
91 
92 
93 int
94 SubtitleString::size_in_pixels (int screen_height) const
95 {
96  /* Size in the subtitle file is given in points as if the screen
97  height is 11 inches, so a 72pt font would be 1/11th of the screen
98  height.
99  */
100 
101  return _size * screen_height / (11 * 72);
102 }
103 
104 
105 bool
106 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
107 {
108  return (
109  a.font() == b.font() &&
110  a.italic() == b.italic() &&
111  a.bold() == b.bold() &&
112  a.underline() == b.underline() &&
113  a.colour() == b.colour() &&
114  a.size() == b.size() &&
116  a.in() == b.in() &&
117  a.out() == b.out() &&
118  a.h_position() == b.h_position() &&
119  a.h_align() == b.h_align() &&
120  a.v_position() == b.v_position() &&
121  a.v_align() == b.v_align() &&
122  a.direction() == b.direction() &&
123  a.text() == b.text() &&
124  a.effect() == b.effect() &&
125  a.effect_colour() == b.effect_colour() &&
126  a.fade_up_time() == b.fade_up_time() &&
127  a.fade_down_time() == b.fade_down_time() &&
128  fabs (a.space_before() - b.space_before()) < SPACE_BEFORE_EPSILON
129  );
130 }
131 
132 
133 bool
134 dcp::operator!= (SubtitleString const & a, SubtitleString const & b)
135 {
136  return !(a == b);
137 }
138 
139 
140 ostream&
141 dcp::operator<< (ostream& s, SubtitleString const & sub)
142 {
143  s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
144  << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
145  << "font " << sub.font().get_value_or ("[default]") << ", ";
146 
147  if (sub.italic()) {
148  s << "italic, ";
149  } else {
150  s << "non-italic, ";
151  }
152 
153  if (sub.bold()) {
154  s << "bold, ";
155  } else {
156  s << "normal, ";
157  }
158 
159  if (sub.underline()) {
160  s << "underlined, ";
161  }
162 
163  s << "size " << sub.size() << ", aspect " << sub.aspect_adjust()
164  << ", colour (" << sub.colour().r << ", " << sub.colour().g << ", " << sub.colour().b << ")"
165  << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
166  << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
167  << ", direction " << ((int) sub.direction())
168  << ", effect " << ((int) sub.effect())
169  << ", effect colour (" << sub.effect_colour().r << ", " << sub.effect_colour().g << ", " << sub.effect_colour().b << ")"
170  << ", space before " << sub.space_before();
171 
172  return s;
173 }
An RGB colour.
Definition: types.h:301
int g
green component, from 0 to 255
Definition: types.h:318
int b
blue component, from 0 to 255
Definition: types.h:319
int r
red component, from 0 to 255
Definition: types.h:317
A single line of subtitle text with all the associated attributes.
SubtitleString(boost::optional< std::string > font, bool italic, bool bold, bool underline, Colour colour, int size, float aspect_adjust, Time in, Time out, float h_position, HAlign h_align, float v_position, VAlign v_align, Direction direction, std::string text, Effect effect, Colour effect_colour, Time fade_up_time, Time fade_down_time, float space_before)
float aspect_adjust() const
boost::optional< std::string > font() const
float v_position() const
Definition: subtitle.h:77
Time in() const
Definition: subtitle.h:56
Time out() const
Definition: subtitle.h:61
A representation of time within a DCP.
Definition: dcp_time.h:73
Namespace for everything in libdcp.
Definition: array_data.h:50
Direction
Definition: types.h:191
HAlign
Definition: types.h:166
constexpr float SPACE_BEFORE_EPSILON
Definition: types.h:355
VAlign
Definition: types.h:178
constexpr float ASPECT_ADJUST_EPSILON
Definition: types.h:343
SubtitleString class.
Helpers for XML reading with libcxml.