Add AspectAdjust to subtitles.
authorCarl Hetherington <cth@carlh.net>
Wed, 3 Jun 2015 15:09:24 +0000 (16:09 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 3 Jun 2015 15:09:24 +0000 (16:09 +0100)
run/tests
src/font_node.cc
src/font_node.h
src/interop_subtitle_content.cc
src/subtitle_content.cc
src/subtitle_string.cc
src/subtitle_string.h
src/types.h
test/read_subtitle_test.cc
test/write_subtitle_test.cc

index 243eee961b02337ddd4acfded1ef94b445245fd5..e8357d735f7352575fd19c827a1ead84095c09e6 100755 (executable)
--- a/run/tests
+++ b/run/tests
@@ -10,7 +10,7 @@ private=../libdcp1-test-private
 work=build/test
 dcpinfo=build/tools/dcpinfo
 
-export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:build/src:build/asdcplib/src
+export LD_LIBRARY_PATH=build/src:build/asdcplib/src:$LD_LIBRARY_PATH
 
 for c in xmlsec1 xmldiff; do
   hash $c 2>/dev/null || { echo >&2 "$c required but not found; aborting"; exit 1; }
index 257a6b512f942289e18447c6ffa0dcb284b3593c..963eb0d81849f33481afe3fb012bfb50f0698d3f 100644 (file)
@@ -37,6 +37,7 @@ FontNode::FontNode (cxml::ConstNodePtr node, int tcr)
        
        id = node->optional_string_attribute ("Id");
        size = node->optional_number_attribute<int64_t> ("Size").get_value_or (0);
+       aspect_adjust = node->optional_number_attribute<float> ("AspectAdjust");
        italic = node->optional_bool_attribute ("Italic");
        optional<string> c = node->optional_string_attribute ("Color");
        if (c) {
@@ -80,6 +81,9 @@ FontNode::FontNode (std::list<boost::shared_ptr<FontNode> > const & font_nodes)
                if ((*i)->size != 0) {
                        size = (*i)->size;
                }
+               if ((*i)->aspect_adjust) {
+                       aspect_adjust = (*i)->aspect_adjust.get ();
+               }
                if ((*i)->italic) {
                        italic = (*i)->italic.get ();
                }
index 97efde739fff293f2e018af24fdc8003e2474512..2149e883392dd575d06a8d17866cf1ac38090913 100644 (file)
@@ -46,6 +46,7 @@ public:
        std::string text;
        boost::optional<std::string> id;
        int size;
+       boost::optional<float> aspect_adjust;
        boost::optional<bool> italic;
        boost::optional<Colour> colour;
        boost::optional<Effect> effect;
index 305c3666c1d071920519309f1327cf8e93c2e7cb..4829f077637bdaeb569da22f1749aee7e3578b13 100644 (file)
@@ -23,6 +23,7 @@
 #include "raw_convert.h"
 #include "font_node.h"
 #include <boost/foreach.hpp>
+#include <cmath>
 
 using std::list;
 using std::string;
@@ -93,6 +94,7 @@ InteropSubtitleContent::xml_as_string () const
        bool italic = false;
        Colour colour;
        int size = 0;
+       float aspect_adjust = 1.0;
        Effect effect = NONE;
        Colour effect_colour;
        int spot_number = 1;
@@ -112,11 +114,12 @@ InteropSubtitleContent::xml_as_string () const
                */
 
                bool const font_changed =
-                       font         != i->font()         ||
-                       italic       != i->italic()       ||
+                       font          != i->font()          ||
+                       italic        != i->italic()        ||
                        colour        != i->colour()        ||
-                       size         != i->size()         ||
-                       effect       != i->effect()       ||
+                       size          != i->size()          ||
+                       fabs (aspect_adjust - i->aspect_adjust()) > ASPECT_ADJUST_EPSILON ||
+                       effect        != i->effect()        ||
                        effect_colour != i->effect_colour();
 
                if (font_changed) {
@@ -124,6 +127,7 @@ InteropSubtitleContent::xml_as_string () const
                        italic = i->italic ();
                        colour = i->colour ();
                        size = i->size ();
+                       aspect_adjust = i->aspect_adjust ();
                        effect = i->effect ();
                        effect_colour = i->effect_colour ();
                }
@@ -136,6 +140,9 @@ InteropSubtitleContent::xml_as_string () const
                        font_element->set_attribute ("Italic", italic ? "yes" : "no");
                        font_element->set_attribute ("Color", colour.to_argb_string());
                        font_element->set_attribute ("Size", raw_convert<string> (size));
+                       if (fabs (aspect_adjust - 1.0) > ASPECT_ADJUST_EPSILON) {
+                               font_element->set_attribute ("AspectAdjust", raw_convert<string> (aspect_adjust));
+                       }
                        font_element->set_attribute ("Effect", effect_to_string (effect));
                        font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
                        font_element->set_attribute ("Script", "normal");
index f8321273c46789926d7a06009462ea94bf25adc1..1977861e17892798f6eabd27c4e774d59b6d7ba3 100644 (file)
@@ -131,16 +131,17 @@ SubtitleContent::maybe_add_subtitle (string text, ParseState const & parse_state
        _subtitles.push_back (
                SubtitleString (
                        effective_font.id,
-                       effective_font.italic.get(),
-                       effective_font.colour.get(),
+                       effective_font.italic.get_value_or (false),
+                       effective_font.colour.get_value_or (dcp::Colour (255, 255, 255)),
                        effective_font.size,
+                       effective_font.aspect_adjust.get_value_or (1.0),
                        effective_subtitle.in,
                        effective_subtitle.out,
                        effective_text.v_position,
                        effective_text.v_align,
                        text,
-                       effective_font.effect ? effective_font.effect.get() : NONE,
-                       effective_font.effect_colour.get(),
+                       effective_font.effect.get_value_or (NONE),
+                       effective_font.effect_colour.get_value_or (dcp::Colour (0, 0, 0)),
                        effective_subtitle.fade_up_time,
                        effective_subtitle.fade_down_time
                        )
index 151318730ff2255302aa3b4604ccf3d495c6bdab..c43583819f654165cb8ad3c54738010c4dde4788 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 #include "subtitle_string.h"
 #include "xml.h"
+#include <cmath>
 
 using std::string;
 using std::ostream;
@@ -31,6 +32,7 @@ SubtitleString::SubtitleString (
        bool italic,
        Colour colour,
        int size,
+       float aspect_adjust,
        Time in,
        Time out,
        float v_position,
@@ -45,6 +47,7 @@ SubtitleString::SubtitleString (
        , _italic (italic)
        , _colour (colour)
        , _size (size)
+       , _aspect_adjust (aspect_adjust)
        , _in (in)
        , _out (out)
        , _v_position (v_position)
@@ -77,6 +80,7 @@ dcp::operator== (SubtitleString const & a, SubtitleString const & b)
                a.italic() == b.italic() &&
                a.colour() == b.colour() &&
                a.size() == b.size() &&
+               fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
                a.in() == b.in() &&
                a.out() == b.out() &&
                a.v_position() == b.v_position() &&
@@ -102,7 +106,8 @@ dcp::operator<< (ostream& s, SubtitleString const & sub)
                s << "non-italic";
        }
        
-       s << ", size " << sub.size() << ", colour " << sub.colour() << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
+       s << ", size " << sub.size() << ", aspect " << sub.aspect_adjust() << ", colour " << sub.colour()
+         << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
          << "effect " << ((int) sub.effect()) << ", effect colour " << sub.effect_colour();
 
        return s;
index 966ae8cb129b2f82fe1fae371034d37012783d89..ba975778ae1eced62b197af1cd2b56403d811f83 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -42,6 +42,7 @@ public:
                bool italic,
                Colour colour,
                int size,
+               float aspect_adjust,
                Time in,
                Time out,
                float v_position,
@@ -108,9 +109,17 @@ public:
        int size () const {
                return _size;
        }
-       
+
        int size_in_pixels (int screen_height) const;
 
+       /** @return Aspect ratio `adjustment' of the font size.
+        *  Values greater than 1 widen each character, values less than 1 narrow each character,
+        *  and the value must be between 0.25 and 4.
+        */
+       float aspect_adjust () const {
+               return _aspect_adjust;
+       }
+
        /** @param p New vertical position as a proportion of the screen height
         *  from the top (between 0 and 1)
         */
@@ -133,6 +142,7 @@ private:
         *  would be 1/11th of the screen height.
         */ 
        int _size;
+       float _aspect_adjust;
        Time _in;
        Time _out;
        /** Vertical position as a proportion of the screen height from the _v_align
index 3c74c45c596b8f6503a6066091424f20b706a5a0..52b831f7b864016c25c01327d6f161da70bf8915 100644 (file)
@@ -214,6 +214,10 @@ extern std::ostream & operator<< (std::ostream & s, Colour const & c);
 
 typedef boost::function<void (NoteType, std::string)> NoteHandler;
 
+/** Maximum absolute difference between dcp::SubtitleString::aspect_adjust values that
+ *  are considered equal.
+ */
+#define ASPECT_ADJUST_EPSILON (1e-3)
 
 }
 
index eed8488e87ae8e4b152352da49c4d34fd699e7b3..6ee59612a4db22794850bb47a33f740f7e98d435 100644 (file)
@@ -39,6 +39,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test1)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   39,
+                                  1.0,
                                   dcp::Time (0, 0, 5, 198, 250),
                                   dcp::Time (0, 0, 7, 115, 250),
                                   0.15,
@@ -57,6 +58,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test1)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   39,
+                                  1.0,
                                   dcp::Time (0, 0, 7, 177, 250),
                                   dcp::Time (0, 0, 11, 31, 250),
                                   0.21,
@@ -72,6 +74,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test1)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   39,
+                                  1.0,
                                   dcp::Time (0, 0, 7, 177, 250),
                                   dcp::Time (0, 0, 11, 31, 250),
                                   0.15,
@@ -90,6 +93,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test1)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   39,
+                                  1.0,
                                   dcp::Time (0, 0, 11, 94, 250),
                                   dcp::Time (0, 0, 13, 63, 250),
                                   0.15,
@@ -108,6 +112,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test1)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   39,
+                                  1.0,
                                   dcp::Time (0, 0, 13, 104, 250),
                                   dcp::Time (0, 0, 15, 177, 250),
                                   0.15,
@@ -132,6 +137,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 0, 41, 62, 250),
                                   dcp::Time (0, 0, 43, 52, 250),
                                   0.89,
@@ -147,6 +153,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 0, 41, 62, 250),
                                   dcp::Time (0, 0, 43, 52, 250),
                                   0.95,
@@ -165,6 +172,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 0, 50, 42, 250),
                                   dcp::Time (0, 0, 52, 21, 250),
                                   0.89,
@@ -180,6 +188,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 0, 50, 42, 250),
                                   dcp::Time (0, 0, 52, 21, 250),
                                   0.95,
@@ -198,6 +207,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 2, 208, 250),
                                   dcp::Time (0, 1, 4, 10, 250),
                                   0.89,
@@ -213,6 +223,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 2, 208, 250),
                                   dcp::Time (0, 1, 4, 10, 250),
                                   0.95,
@@ -231,6 +242,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 15, 42, 250),
                                   dcp::Time (0, 1, 16, 42, 250),
                                   0.89,
@@ -246,6 +258,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 15, 42, 250),
                                   dcp::Time (0, 1, 16, 42, 250),
                                   0.95,
@@ -264,6 +277,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 27, 115, 250),
                                   dcp::Time (0, 1, 28, 208, 250),
                                   0.89,
@@ -279,6 +293,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 27, 115, 250),
                                   dcp::Time (0, 1, 28, 208, 250),
                                   0.95,
@@ -297,6 +312,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 42, 229, 250),
                                   dcp::Time (0, 1, 45, 62, 250),
                                   0.89,
@@ -312,6 +328,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 42, 229, 250),
                                   dcp::Time (0, 1, 45, 62, 250),
                                   0.95,
@@ -330,6 +347,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 45, 146, 250),
                                   dcp::Time (0, 1, 47, 94, 250),
                                   0.89,
@@ -345,6 +363,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 45, 146, 250),
                                   dcp::Time (0, 1, 47, 94, 250),
                                   0.95,
@@ -363,6 +382,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 47, 146, 250),
                                   dcp::Time (0, 1, 48, 167, 250),
                                   0.89,
@@ -378,6 +398,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   false,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 1, 47, 146, 250),
                                   dcp::Time (0, 1, 48, 167, 250),
                                   0.95,
@@ -396,6 +417,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 2, 5, 208, 250),
                                   dcp::Time (0, 2, 7, 31, 250),
                                   0.89,
@@ -411,6 +433,7 @@ BOOST_AUTO_TEST_CASE (read_subtitle_test2)
                                   true,
                                   dcp::Colour (255, 255, 255),
                                   42,
+                                  1.0,
                                   dcp::Time (0, 2, 5, 208, 250),
                                   dcp::Time (0, 2, 7, 31, 250),
                                   0.95,
index b5daa2b1774f1befd8d02f9d3fdff7c0cf7a460f..a017547bae3187e6e57f59b0b2bd3c2a5c56e4e0 100644 (file)
@@ -37,6 +37,7 @@ BOOST_AUTO_TEST_CASE (write_subtitle_test)
                        false,
                        dcp::Colour (255, 255, 255),
                        48,
+                       1.0,
                        dcp::Time (0, 4,  9, 22, 24),
                        dcp::Time (0, 4, 11, 22, 24),
                        0.8,
@@ -55,6 +56,7 @@ BOOST_AUTO_TEST_CASE (write_subtitle_test)
                        true,
                        dcp::Colour (128, 0, 64),
                        91,
+                       1.0,
                        dcp::Time (5, 41,  0, 21, 24),
                        dcp::Time (6, 12, 15, 21, 24),
                        0.4,