diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-06-03 23:32:48 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-06-03 23:32:48 +0100 |
| commit | e1f53890da6e4be2555a9e17a52edcc03d53656b (patch) | |
| tree | ff148f4d8dc54b1a09fa21ca743fce261f01c021 /src | |
| parent | 47b52fb54f302d5faf93a19ae2fe28fa610f96ca (diff) | |
Basic HAlign / HPosition support.
Diffstat (limited to 'src')
| -rw-r--r-- | src/interop_subtitle_content.cc | 6 | ||||
| -rw-r--r-- | src/subtitle_content.cc | 4 | ||||
| -rw-r--r-- | src/subtitle_string.cc | 9 | ||||
| -rw-r--r-- | src/subtitle_string.h | 15 | ||||
| -rw-r--r-- | src/text_node.cc | 41 | ||||
| -rw-r--r-- | src/text_node.h | 8 | ||||
| -rw-r--r-- | src/types.cc | 45 | ||||
| -rw-r--r-- | src/types.h | 23 |
8 files changed, 124 insertions, 27 deletions
diff --git a/src/interop_subtitle_content.cc b/src/interop_subtitle_content.cc index 4829f077..a833e795 100644 --- a/src/interop_subtitle_content.cc +++ b/src/interop_subtitle_content.cc @@ -171,6 +171,12 @@ InteropSubtitleContent::xml_as_string () const } xmlpp::Element* text = subtitle_element->add_child ("Text"); + if (i->h_align() != HALIGN_CENTER) { + text->set_attribute ("HAlign", halign_to_string (i->h_align ())); + } + if (i->h_position() > ALIGN_EPSILON) { + text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6)); + } text->set_attribute ("VAlign", valign_to_string (i->v_align())); text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6)); text->add_child_text (i->text()); diff --git a/src/subtitle_content.cc b/src/subtitle_content.cc index 1977861e..fb01502d 100644 --- a/src/subtitle_content.cc +++ b/src/subtitle_content.cc @@ -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 @@ -137,6 +137,8 @@ SubtitleContent::maybe_add_subtitle (string text, ParseState const & parse_state effective_font.aspect_adjust.get_value_or (1.0), effective_subtitle.in, effective_subtitle.out, + effective_text.h_position, + effective_text.h_align, effective_text.v_position, effective_text.v_align, text, diff --git a/src/subtitle_string.cc b/src/subtitle_string.cc index c4358381..478bd4aa 100644 --- a/src/subtitle_string.cc +++ b/src/subtitle_string.cc @@ -35,6 +35,8 @@ SubtitleString::SubtitleString ( float aspect_adjust, Time in, Time out, + float h_position, + HAlign h_align, float v_position, VAlign v_align, string text, @@ -50,6 +52,8 @@ SubtitleString::SubtitleString ( , _aspect_adjust (aspect_adjust) , _in (in) , _out (out) + , _h_position (h_position) + , _h_align (h_align) , _v_position (v_position) , _v_align (v_align) , _text (text) @@ -83,6 +87,8 @@ dcp::operator== (SubtitleString const & a, SubtitleString const & b) fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON && a.in() == b.in() && a.out() == b.out() && + a.h_position() == b.h_position() && + a.h_align() == b.h_align() && a.v_position() == b.v_position() && a.v_align() == b.v_align() && a.text() == b.text() && @@ -107,7 +113,8 @@ dcp::operator<< (ostream& s, SubtitleString const & sub) } s << ", size " << sub.size() << ", aspect " << sub.aspect_adjust() << ", colour " << sub.colour() - << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n" + << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ",\n" + << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align()) << ";\n" << "effect " << ((int) sub.effect()) << ", effect colour " << sub.effect_colour(); return s; diff --git a/src/subtitle_string.h b/src/subtitle_string.h index cf787e64..bb768397 100644 --- a/src/subtitle_string.h +++ b/src/subtitle_string.h @@ -45,6 +45,8 @@ public: float aspect_adjust, Time in, Time out, + float h_position, + HAlign h_align, float v_position, VAlign v_align, std::string text, @@ -79,6 +81,14 @@ public: return _text; } + float h_position () const { + return _h_position; + } + + HAlign h_align () const { + return _h_align; + } + /** @return vertical position as a proportion of the screen height from the top * (between 0 and 1) */ @@ -149,6 +159,11 @@ private: float _aspect_adjust; Time _in; Time _out; + /** Horizontal position as a proportion of the screen width from the _h_align + * (between 0 and 1) + */ + float _h_position; + HAlign _h_align; /** Vertical position as a proportion of the screen height from the _v_align * (between 0 and 1) */ diff --git a/src/text_node.cc b/src/text_node.cc index 834b78b2..6332e1f5 100644 --- a/src/text_node.cc +++ b/src/text_node.cc @@ -38,22 +38,43 @@ using namespace dcp; * @param node Node to read. */ TextNode::TextNode (boost::shared_ptr<const cxml::Node> node, int tcr) - : v_align (CENTER) + : h_position (0) + , h_align (HALIGN_CENTER) + , v_position (0) + , v_align (VALIGN_CENTER) { text = node->content (); - optional<float> x = node->optional_number_attribute<float> ("VPosition"); - if (!x) { - x = node->number_attribute<float> ("Vposition"); + + optional<float> hp = node->optional_number_attribute<float> ("HPosition"); + if (!hp) { + hp = node->optional_number_attribute<float> ("Hposition"); + } + if (hp) { + h_position = hp.get () / 100; } - v_position = x.get () / 100; - optional<string> v = node->optional_string_attribute ("VAlign"); - if (!v) { - v = node->optional_string_attribute ("Valign"); + optional<string> ha = node->optional_string_attribute ("HAlign"); + if (!ha) { + ha = node->optional_string_attribute ("Halign"); + } + if (ha) { + h_align = string_to_halign (ha.get ()); } - if (v) { - v_align = string_to_valign (v.get ()); + optional<float> vp = node->optional_number_attribute<float> ("VPosition"); + if (!vp) { + vp = node->optional_number_attribute<float> ("Vposition"); + } + if (vp) { + v_position = vp.get () / 100; + } + + optional<string> va = node->optional_string_attribute ("VAlign"); + if (!va) { + va = node->optional_string_attribute ("Valign"); + } + if (va) { + v_align = string_to_valign (va.get ()); } list<cxml::NodePtr> f = node->node_children ("Font"); diff --git a/src/text_node.h b/src/text_node.h index 2e1b9ac4..7cd73bb9 100644 --- a/src/text_node.h +++ b/src/text_node.h @@ -42,12 +42,16 @@ class TextNode public: /** Construct a default text node */ TextNode () - : v_position (0) - , v_align (TOP) + : h_position (0) + , h_align (HALIGN_LEFT) + , v_position (0) + , v_align (VALIGN_TOP) {} TextNode (boost::shared_ptr<const cxml::Node> node, int tcr); + float h_position; + HAlign h_align; float v_position; VAlign v_align; std::string text; diff --git a/src/types.cc b/src/types.cc index c519a03d..e86e999c 100644 --- a/src/types.cc +++ b/src/types.cc @@ -162,32 +162,59 @@ dcp::string_to_effect (string s) } string +dcp::halign_to_string (HAlign h) +{ + switch (h) { + case HALIGN_LEFT: + return "left"; + case HALIGN_CENTER: + return "center"; + case HALIGN_RIGHT: + return "right"; + } + + boost::throw_exception (MiscError ("unknown subtitle halign type")); +} + +HAlign +dcp::string_to_halign (string s) +{ + if (s == "left") { + return HALIGN_LEFT; + } else if (s == "center") { + return HALIGN_CENTER; + } else if (s == "right") { + return HALIGN_RIGHT; + } + + boost::throw_exception (DCPReadError ("unknown subtitle halign type")); +} + +string dcp::valign_to_string (VAlign v) { switch (v) { - case TOP: + case VALIGN_TOP: return "top"; - case CENTER: + case VALIGN_CENTER: return "center"; - case BOTTOM: + case VALIGN_BOTTOM: return "bottom"; } - boost::throw_exception (MiscError ("unknown valign type")); + boost::throw_exception (MiscError ("unknown subtitle valign type")); } VAlign dcp::string_to_valign (string s) { if (s == "top") { - return TOP; + return VALIGN_TOP; } else if (s == "center") { - return CENTER; + return VALIGN_CENTER; } else if (s == "bottom") { - return BOTTOM; + return VALIGN_BOTTOM; } boost::throw_exception (DCPReadError ("unknown subtitle valign type")); } - - diff --git a/src/types.h b/src/types.h index 52b831f7..a8e63faf 100644 --- a/src/types.h +++ b/src/types.h @@ -94,11 +94,21 @@ enum Effect extern std::string effect_to_string (Effect e); extern Effect string_to_effect (std::string s); +enum HAlign +{ + HALIGN_LEFT, ///< horizontal position is distance from left of screen to left of subtitle + HALIGN_CENTER, ///< horizontal position is distance from centre of screen to centre of subtitle + HALIGN_RIGHT, ///< horizontal position is distance from right of screen to right of subtitle +}; + +extern std::string halign_to_string (HAlign a); +extern HAlign string_to_halign (std::string s); + enum VAlign { - TOP, - CENTER, - BOTTOM + VALIGN_TOP, ///< vertical position is distance from top of screen to top of subtitle + VALIGN_CENTER, ///< vertical position is distance from centre of screen to centre of subtitle + VALIGN_BOTTOM ///< vertical position is distance from bottom of screen to bottom of subtitle }; extern std::string valign_to_string (VAlign a); @@ -217,7 +227,12 @@ 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) +const float ASPECT_ADJUST_EPSILON = 1e-3; + +/** Maximum absolute difference between dcp::SubtitleString alignment values that + * are considered equal. + */ +const float ALIGN_EPSILON = 1e-3; } |
