Support for conversions of things that have multiple representations.
[libsub.git] / src / subtitle.cc
1 /*
2     Copyright (C) 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.h"
21 #include "convert_time.h"
22
23 using std::list;
24 using namespace sub;
25
26 bool
27 sub::operator< (Subtitle const & a, Subtitle const & b)
28 {
29         if (a.from.frame && b.from.frame) {
30                 return a.from.frame.get() < b.from.frame.get();
31         }
32
33         if (a.from.metric && b.from.metric) {
34                 return a.from.metric.get() < b.from.metric.get();
35         }
36
37         assert (false);
38 }
39
40 void
41 sub::convert_font_sizes (list<Subtitle>& subs, int screen_height_in_points)
42 {
43         for (list<Subtitle>::iterator i = subs.begin(); i != subs.end(); ++i) {
44                 if (i->font_size.proportional) {
45                         i->font_size.points = i->font_size.proportional.get() * screen_height_in_points;
46                 } else {
47                         i->font_size.proportional = float (i->font_size.points.get()) / screen_height_in_points;
48                 }
49         }
50 }
51
52 void
53 sub::convert_times (list<Subtitle>& subs, float frames_per_second)
54 {
55         for (list<Subtitle>::iterator i = subs.begin(); i != subs.end(); ++i) {
56                 if (i->from.frame) {
57                         i->from.metric = frame_to_metric (i->from.frame.get(), frames_per_second);
58                 } else {
59                         i->from.frame = metric_to_frame (i->from.metric.get(), frames_per_second);
60                 }
61
62                 if (i->to.frame) {
63                         i->to.metric = frame_to_metric (i->to.frame.get(), frames_per_second);
64                 } else {
65                         i->to.frame = metric_to_frame (i->to.metric.get(), frames_per_second);
66                 }
67         }
68 }