diff options
| author | Carl Hetherington <cth@carlh.net> | 2021-10-13 22:18:59 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2021-10-13 22:18:59 +0200 |
| commit | 8bc480da2db924349465dbe557f87bd6f05b7fdd (patch) | |
| tree | 9fcbab45685746b7a00b1045aca19f495f4f92df /src | |
| parent | 0ecf58d20eec5e9d10ee4b9f0440d232d875f94a (diff) | |
Basic implementation of <Space> tag in subtitles.v1.8.3
Diffstat (limited to 'src')
| -rw-r--r-- | src/subtitle_asset.cc | 27 | ||||
| -rw-r--r-- | src/subtitle_asset.h | 3 | ||||
| -rw-r--r-- | src/subtitle_asset_internal.cc | 12 | ||||
| -rw-r--r-- | src/subtitle_asset_internal.h | 9 | ||||
| -rw-r--r-- | src/subtitle_string.cc | 10 | ||||
| -rw-r--r-- | src/subtitle_string.h | 9 | ||||
| -rw-r--r-- | src/types.h | 6 |
7 files changed, 60 insertions, 16 deletions
diff --git a/src/subtitle_asset.cc b/src/subtitle_asset.cc index a8636368..2793772a 100644 --- a/src/subtitle_asset.cc +++ b/src/subtitle_asset.cc @@ -286,14 +286,28 @@ SubtitleAsset::parse_subtitles (xmlpp::Element const * node, vector<ParseState>& throw XMLError ("unexpected node " + node->get_name()); } + float space_before = 0; + for (auto i: node->get_children()) { auto const v = dynamic_cast<xmlpp::ContentNode const *>(i); if (v) { - maybe_add_subtitle (v->get_content(), state, standard); + maybe_add_subtitle (v->get_content(), state, space_before, standard); + space_before = 0; } auto const e = dynamic_cast<xmlpp::Element const *>(i); if (e) { - parse_subtitles (e, state, tcr, standard); + if (e->get_name() == "Space") { + if (node->get_name() != "Text") { + throw XMLError ("Space node found outside Text"); + } + auto size = optional_string_attribute(e, "Size").get_value_or("0.5"); + if (standard == dcp::Standard::INTEROP) { + boost::replace_all(size, "em", ""); + } + space_before += raw_convert<float>(size); + } else { + parse_subtitles (e, state, tcr, standard); + } } } @@ -302,7 +316,7 @@ SubtitleAsset::parse_subtitles (xmlpp::Element const * node, vector<ParseState>& void -SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse_state, Standard standard) +SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse_state, float space_before, Standard standard) { if (empty_or_white_space (text)) { return; @@ -398,7 +412,8 @@ SubtitleAsset::maybe_add_subtitle (string text, vector<ParseState> const & parse ps.effect.get_value_or (Effect::NONE), ps.effect_colour.get_value_or (dcp::Colour (0, 0, 0)), ps.fade_up_time.get_value_or(Time()), - ps.fade_down_time.get_value_or(Time()) + ps.fade_down_time.get_value_or(Time()), + space_before ) ); break; @@ -684,7 +699,7 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S fabs(last_v_position - is->v_position()) > ALIGN_EPSILON || last_direction != is->direction() ) { - text.reset (new order::Text (subtitle, is->h_align(), is->h_position(), is->v_align(), is->v_position(), is->direction())); + text = make_shared<order::Text>(subtitle, is->h_align(), is->h_position(), is->v_align(), is->v_position(), is->direction()); subtitle->children.push_back (text); last_h_align = is->h_align (); @@ -694,7 +709,7 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S last_direction = is->direction (); } - text->children.push_back (make_shared<order::String>(text, order::Font (is, standard), is->text())); + text->children.push_back (make_shared<order::String>(text, order::Font (is, standard), is->text(), is->space_before())); } auto ii = dynamic_pointer_cast<SubtitleImage>(i); diff --git a/src/subtitle_asset.h b/src/subtitle_asset.h index 8ec57fce..f51906e2 100644 --- a/src/subtitle_asset.h +++ b/src/subtitle_asset.h @@ -156,6 +156,7 @@ protected: IMAGE }; boost::optional<Type> type; + float space_before = 0; }; void parse_subtitles (xmlpp::Element const * node, std::vector<ParseState>& state, boost::optional<int> tcr, Standard standard); @@ -205,7 +206,7 @@ private: friend struct ::pull_fonts_test2; friend struct ::pull_fonts_test3; - void maybe_add_subtitle (std::string text, std::vector<ParseState> const & parse_state, Standard standard); + void maybe_add_subtitle (std::string text, std::vector<ParseState> const & parse_state, float space_before, Standard standard); static void pull_fonts (std::shared_ptr<order::Part> part); }; diff --git a/src/subtitle_asset_internal.cc b/src/subtitle_asset_internal.cc index bf73fcc3..7a10f472 100644 --- a/src/subtitle_asset_internal.cc +++ b/src/subtitle_asset_internal.cc @@ -134,9 +134,17 @@ order::Part::as_xml (xmlpp::Element* parent, Context &) const xmlpp::Element* -order::String::as_xml (xmlpp::Element* parent, Context &) const +order::String::as_xml (xmlpp::Element* parent, Context& context) const { - parent->add_child_text (text); + if (fabs(_space_before) > SPACE_BEFORE_EPSILON) { + auto space = parent->add_child("Space"); + auto size = raw_convert<string>(_space_before, 2); + if (context.standard == Standard::INTEROP) { + size += "em"; + } + space->set_attribute("Size", size); + } + parent->add_child_text (_text); return 0; } diff --git a/src/subtitle_asset_internal.h b/src/subtitle_asset_internal.h index f24ed58a..55880797 100644 --- a/src/subtitle_asset_internal.h +++ b/src/subtitle_asset_internal.h @@ -127,14 +127,17 @@ public: class String : public Part { public: - String (std::shared_ptr<Part> parent, Font font, std::string text_) + String (std::shared_ptr<Part> parent, Font font, std::string text, float space_before) : Part (parent, font) - , text (text_) + , _text (text) + , _space_before (space_before) {} virtual xmlpp::Element* as_xml (xmlpp::Element* parent, Context &) const override; - std::string text; +private: + std::string _text; + float _space_before; }; diff --git a/src/subtitle_string.cc b/src/subtitle_string.cc index acd35691..5e2c6251 100644 --- a/src/subtitle_string.cc +++ b/src/subtitle_string.cc @@ -69,7 +69,8 @@ SubtitleString::SubtitleString ( Effect effect, Colour effect_colour, Time fade_up_time, - Time fade_down_time + Time fade_down_time, + float space_before ) : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time) , _font (font) @@ -83,6 +84,7 @@ SubtitleString::SubtitleString ( , _text (text) , _effect (effect) , _effect_colour (effect_colour) + , _space_before (space_before) { _aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f); } @@ -122,7 +124,8 @@ dcp::operator== (SubtitleString const & a, SubtitleString const & b) a.effect() == b.effect() && a.effect_colour() == b.effect_colour() && a.fade_up_time() == b.fade_up_time() && - a.fade_down_time() == b.fade_down_time() + a.fade_down_time() == b.fade_down_time() && + fabs (a.space_before() - b.space_before()) < SPACE_BEFORE_EPSILON ); } @@ -163,7 +166,8 @@ dcp::operator<< (ostream& s, SubtitleString const & sub) << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align()) << ", direction " << ((int) sub.direction()) << ", effect " << ((int) sub.effect()) - << ", effect colour (" << sub.effect_colour().r << ", " << sub.effect_colour().g << ", " << sub.effect_colour().b << ")"; + << ", effect colour (" << sub.effect_colour().r << ", " << sub.effect_colour().g << ", " << sub.effect_colour().b << ")" + << ", space before " << sub.space_before(); return s; } diff --git a/src/subtitle_string.h b/src/subtitle_string.h index bf9c87d9..f6d0974b 100644 --- a/src/subtitle_string.h +++ b/src/subtitle_string.h @@ -76,6 +76,7 @@ public: * @param effect_colour Colour of the effect * @param fade_up_time Time to fade the text in * @param fade_down_time Time to fade the text out + * @param space_before Space to add before this string, in ems (could be negative to remove space). */ SubtitleString ( boost::optional<std::string> font, @@ -96,7 +97,8 @@ public: Effect effect, Colour effect_colour, Time fade_up_time, - Time fade_down_time + Time fade_down_time, + float space_before ); /** @return font ID */ @@ -142,6 +144,10 @@ public: int size_in_pixels (int screen_height) const; + float space_before () const { + return _space_before; + } + /** @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. @@ -202,6 +208,7 @@ private: std::string _text; Effect _effect; Colour _effect_colour; + float _space_before; }; bool operator== (SubtitleString const & a, SubtitleString const & b); diff --git a/src/types.h b/src/types.h index caa28dd5..be9ba1b3 100644 --- a/src/types.h +++ b/src/types.h @@ -349,6 +349,12 @@ constexpr float ASPECT_ADJUST_EPSILON = 1e-3; constexpr float ALIGN_EPSILON = 1e-3; +/** Maximum absolute difference between dcp::SubtitleString space_before values that + * are considered equal. + */ +constexpr float SPACE_BEFORE_EPSILON = 1e-3; + + enum class Marker { FFOC, ///< first frame of composition LFOC, ///< last frame of composition |
