Change API for alternate representations of time / font size etc. Fix Arial recognition.
authorCarl Hetherington <cth@carlh.net>
Sun, 25 May 2014 20:25:33 +0000 (21:25 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 25 May 2014 20:25:33 +0000 (21:25 +0100)
src/dcp_reader.cc
src/stl_writer.cc
src/stl_writer.h
src/subtitle.cc
src/subtitle.h
src/writer.h
test/stl_writer_test.cc

index dd40c97a1745b10fd305e9174bfe29c4a3f3a3da..4c3384ee49b01fc45181461e4f65c09a7f02dae4 100644 (file)
@@ -236,11 +236,11 @@ DCPReader::font_id_to_name (string id) const
                return "";
        }
 
-       if ((*i)->uri == "arial.ttf") {
+       if ((*i)->uri == "arial.ttf" || (*i)->uri == "Arial.ttf") {
                return "Arial";
        }
 
-       return "";
+       return (*i)->uri;
 }
 
 DCPReader::DCPReader (istream& in)
index 22667325ba85bbfe6ee56e416e037837adbf4204..126ad76dcb4e77e27bd1b519e22b16ac6641feff 100644 (file)
@@ -26,8 +26,8 @@ using std::string;
 using boost::optional;
 using namespace sub;
 
-STLWriter::STLWriter (list<Subtitle> subtitles, ostream& out)
-       : Writer (subtitles)
+STLWriter::STLWriter (list<Subtitle> subtitles, int screen_height_in_points, float frames_per_second, ostream& out)
+       : Writer (subtitles, screen_height_in_points, frames_per_second)
 {
        optional<string> font;
        optional<int> font_size;
@@ -45,9 +45,9 @@ STLWriter::STLWriter (list<Subtitle> subtitles, ostream& out)
                        font = i->font;
                        started_new = true;
                }
-               if (!font_size || font_size.get() != i->font_size.points.get()) {
-                       out << "\n$FontSize = " << i->font_size.points.get();
-                       font_size = i->font_size.points.get();
+               if (!font_size || font_size.get() != i->font_size_points (screen_height_in_points)) {
+                       out << "\n$FontSize = " << i->font_size_points (screen_height_in_points);
+                       font_size = i->font_size_points (screen_height_in_points);
                        started_new = true;
                }
                string text;
@@ -66,16 +66,16 @@ STLWriter::STLWriter (list<Subtitle> subtitles, ostream& out)
 
                text += i->text;
 
-               if (from && from.get() == i->from.frame.get() && to && to.get() == i->to.frame.get() && !started_new) {
+               if (from && from.get() == i->from_frame(frames_per_second) && to && to.get() == i->to_frame(frames_per_second) && !started_new) {
                        for (int j = line; j < i->line; ++j) {
                                out << "|";
                        }
                        out << text;
                        line = i->line;
                } else {
-                       out << "\n" << i->from.frame.get().timecode() << "," << i->to.frame.get().timecode() << "," << text;
-                       from = i->from.frame.get();
-                       to = i->to.frame.get();
+                       out << "\n" << i->from_frame(frames_per_second).timecode() << "," << i->to_frame(frames_per_second).timecode() << "," << text;
+                       from = i->from_frame (frames_per_second);
+                       to = i->to_frame (frames_per_second);
                        line = 0;
                }
        }
index 712aaa520e1a16423b0060a5683747bb943fd912..7e37f16e750c46da58ac572cf54871b166584913 100644 (file)
@@ -25,7 +25,7 @@ namespace sub {
 class STLWriter : public Writer
 {
 public:
-       STLWriter (std::list<Subtitle> subtitles, std::ostream &);
+       STLWriter (std::list<Subtitle> subtitles, int screen_height_in_points, float frames_per_second, std::ostream &);
 };
 
 }
index 105bf31a4cc8ac1d1219035e7f51a3a2463957d3..6d3360fa7f93e055d71b26f32d998e3f82781a57 100644 (file)
@@ -37,37 +37,62 @@ sub::operator< (Subtitle const & a, Subtitle const & b)
        assert (false);
 }
 
-void
-sub::convert_font_sizes (list<Subtitle>& subs, int screen_height_in_points)
+FrameTime
+Subtitle::from_frame (float frames_per_second) const
 {
-       for (list<Subtitle>::iterator i = subs.begin(); i != subs.end(); ++i) {
-               if (i->font_size.proportional) {
-                       i->font_size.points = i->font_size.proportional.get() * screen_height_in_points;
-               } else {
-                       i->font_size.proportional = float (i->font_size.points.get()) / screen_height_in_points;
-               }
+       if (from.frame) {
+               return from.frame.get ();
        }
+
+       return metric_to_frame (from.metric.get(), frames_per_second);
+}
+
+FrameTime
+Subtitle::to_frame (float frames_per_second) const
+{
+       if (to.frame) {
+               return to.frame.get ();
+       }
+
+       return metric_to_frame (to.metric.get(), frames_per_second);
+}
+
+MetricTime
+Subtitle::from_metric (float frames_per_second) const
+{
+       if (from.metric) {
+               return from.metric.get ();
+       }
+
+       return frame_to_metric (from.frame.get(), frames_per_second);
 }
 
-/** Take a list of Subtitles and convert their times either from metric to frame, or vice-versa,
- *  depending on what the Subtitles currently have.
- *  @param sub Subtitles.
- *  @param frames_per_second Video frames-per-second value to use in the conversion.
- */
-void
-sub::convert_times (list<Subtitle>& subs, float frames_per_second)
+MetricTime
+Subtitle::to_metric (float frames_per_second) const
 {
-       for (list<Subtitle>::iterator i = subs.begin(); i != subs.end(); ++i) {
-               if (i->from.frame) {
-                       i->from.metric = frame_to_metric (i->from.frame.get(), frames_per_second);
-               } else {
-                       i->from.frame = metric_to_frame (i->from.metric.get(), frames_per_second);
-               }
-
-               if (i->to.frame) {
-                       i->to.metric = frame_to_metric (i->to.frame.get(), frames_per_second);
-               } else {
-                       i->to.frame = metric_to_frame (i->to.metric.get(), frames_per_second);
-               }
+       if (to.metric) {
+               return to.metric.get ();
        }
+
+       return frame_to_metric (to.frame.get(), frames_per_second);
+}
+
+float
+Subtitle::font_size_proportional (int screen_height_in_points) const
+{
+       if (font_size.proportional) {
+               return font_size.proportional.get ();
+       }
+
+       return float (font_size.points.get ()) / screen_height_in_points;
+}
+
+int
+Subtitle::font_size_points (int screen_height_in_points) const
+{
+       if (font_size.points) {
+               return font_size.points.get ();
+       }
+
+       return font_size.proportional.get() * screen_height_in_points;
 }
index 77290641d83ea92593a1bf410903fdd4901c7f4c..70a8eb6d0ed73f98ef8547353b3bfb867cbb2219 100644 (file)
@@ -53,6 +53,9 @@ public:
                boost::optional<int> points;
        } font_size;
 
+       float font_size_proportional (int screen_height_in_points) const;
+       int font_size_points (int screen_height_in_points) const;
+
        /** vertical position of the baseline of the text */
        struct {
                /** as a proportion of screen height offset from some reference point */
@@ -76,21 +79,24 @@ public:
                boost::optional<MetricTime> metric;
        } from;
 
+       FrameTime  from_frame  (float frames_per_second) const;
+       MetricTime from_metric (float frames_per_second) const;
+
        /** to time */
        struct {
                boost::optional<FrameTime> frame;
                boost::optional<MetricTime> metric;
        } to;
 
+       FrameTime  to_frame  (float frames_per_second) const;
+       MetricTime to_metric (float frames_per_second) const;
+       
        boost::optional<MetricTime> fade_up;
        boost::optional<MetricTime> fade_down;
 };
 
 bool operator< (Subtitle const & a, Subtitle const & b);       
 
-void convert_font_sizes (std::list<Subtitle> &, int screen_height_in_points);
-void convert_times (std::list<Subtitle> &, float frames_per_second);
-       
 }
 
 #endif
index 274c455951016ee195c81587ac09d7a176fc713d..057214e9f03eb2eb270576f53a9df8a1d390fbdc 100644 (file)
@@ -27,12 +27,16 @@ class Subtitle;
 class Writer
 {
 public:
-       Writer (std::list<Subtitle> subtitles)
+       Writer (std::list<Subtitle> subtitles, int screen_height_in_points, float frames_per_second)
                : _subs (subtitles)
+               , _screen_height_in_points (screen_height_in_points)
+               , _frames_per_second (frames_per_second)
        {}
 
 protected:
        std::list<Subtitle> _subs;
+       int _screen_height_in_points;
+       float _frames_per_second;
 };
 
 }
index a9b06e159224132be3fd05ada8bb6806d3290349..8f16f1f09b7331ea966789c9dc442c1504c441db 100644 (file)
@@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE (stl_writer_test)
        subs.push_back (make (".",                        false, false, false, 0, sub::FrameTime (0, 1,  1, 1), sub::FrameTime (0, 1,  2, 10)));
 
        ofstream f ("build/test/test.stl");
-       sub::STLWriter writer (subs, f);
+       sub::STLWriter writer (subs, 24, 72 * 11, f);
        f.close ();
 
        check_text ("test/ref/test.stl", "build/test/test.stl");