summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/subtitle_asset.cc100
-rw-r--r--src/subtitle_asset.h16
-rw-r--r--src/util.cc12
-rw-r--r--src/util.h1
-rw-r--r--src/xml.cc21
5 files changed, 95 insertions, 55 deletions
diff --git a/src/subtitle_asset.cc b/src/subtitle_asset.cc
index ac222638..b7586207 100644
--- a/src/subtitle_asset.cc
+++ b/src/subtitle_asset.cc
@@ -19,6 +19,7 @@
#include <boost/lexical_cast.hpp>
#include "subtitle_asset.h"
+#include "util.h"
using namespace std;
using namespace boost;
@@ -42,71 +43,93 @@ SubtitleAsset::SubtitleAsset (string directory, string xml)
in a sane way.
*/
- list<shared_ptr<FontNode> > current_font_nodes;
- list<shared_ptr<SubtitleNode> > current_subtitle_nodes;
- current_subtitle_nodes.push_back (shared_ptr<SubtitleNode> ());
- examine_font_nodes (font_nodes, current_font_nodes, current_subtitle_nodes);
+ ParseState parse_state;
+ examine_font_nodes (font_nodes, parse_state);
}
void
SubtitleAsset::examine_font_nodes (
list<shared_ptr<FontNode> > const & font_nodes,
- list<shared_ptr<FontNode> >& current_font_nodes,
- list<shared_ptr<SubtitleNode> >& current_subtitle_nodes
+ ParseState& parse_state
)
{
for (list<shared_ptr<FontNode> >::const_iterator i = font_nodes.begin(); i != font_nodes.end(); ++i) {
-
- current_font_nodes.push_back (*i);
+
+ parse_state.font_nodes.push_back (*i);
+ maybe_add_subtitle ((*i)->text, parse_state);
for (list<shared_ptr<SubtitleNode> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
- current_subtitle_nodes.push_back (*j);
- examine_text_nodes (*j, (*j)->text_nodes, current_font_nodes);
- examine_font_nodes ((*j)->font_nodes, current_font_nodes, current_subtitle_nodes);
- current_subtitle_nodes.pop_back ();
+ parse_state.subtitle_nodes.push_back (*j);
+ examine_text_nodes ((*j)->text_nodes, parse_state);
+ examine_font_nodes ((*j)->font_nodes, parse_state);
+ parse_state.subtitle_nodes.pop_back ();
}
- examine_font_nodes ((*i)->font_nodes, current_font_nodes, current_subtitle_nodes);
- examine_text_nodes (current_subtitle_nodes.back (), (*i)->text_nodes, current_font_nodes);
+ examine_font_nodes ((*i)->font_nodes, parse_state);
+ examine_text_nodes ((*i)->text_nodes, parse_state);
- current_font_nodes.pop_back ();
+ parse_state.font_nodes.pop_back ();
}
}
void
SubtitleAsset::examine_text_nodes (
- shared_ptr<SubtitleNode> subtitle_node,
list<shared_ptr<TextNode> > const & text_nodes,
- list<shared_ptr<FontNode> >& current_font_nodes
+ ParseState& parse_state
)
{
for (list<shared_ptr<TextNode> >::const_iterator i = text_nodes.begin(); i != text_nodes.end(); ++i) {
- FontNode effective (current_font_nodes);
- _subtitles.push_back (
- shared_ptr<Subtitle> (
- new Subtitle (
- font_id_to_name (effective.id),
- effective.italic.get(),
- effective.color.get(),
- effective.size,
- subtitle_node->in,
- subtitle_node->out,
- (*i)->v_position,
- (*i)->v_align,
- (*i)->text,
- effective.effect.get(),
- effective.effect_color.get(),
- subtitle_node->fade_up_time,
- subtitle_node->fade_down_time
- )
- )
- );
+ parse_state.text_nodes.push_back (*i);
+ maybe_add_subtitle ((*i)->text, parse_state);
+ examine_font_nodes ((*i)->font_nodes, parse_state);
+ parse_state.text_nodes.pop_back ();
}
}
+void
+SubtitleAsset::maybe_add_subtitle (string text, ParseState const & parse_state)
+{
+ if (empty_or_white_space (text)) {
+ return;
+ }
+
+ if (parse_state.text_nodes.empty() || parse_state.subtitle_nodes.empty ()) {
+ return;
+ }
+
+ assert (!parse_state.text_nodes.empty ());
+ assert (!parse_state.subtitle_nodes.empty ());
+
+ FontNode effective_font (parse_state.font_nodes);
+ TextNode effective_text (*parse_state.text_nodes.back ());
+ SubtitleNode effective_subtitle (*parse_state.subtitle_nodes.back ());
+
+ _subtitles.push_back (
+ shared_ptr<Subtitle> (
+ new Subtitle (
+ font_id_to_name (effective_font.id),
+ effective_font.italic.get(),
+ effective_font.color.get(),
+ effective_font.size,
+ effective_subtitle.in,
+ effective_subtitle.out,
+ effective_text.v_position,
+ effective_text.v_align,
+ text,
+ effective_font.effect.get(),
+ effective_font.effect_color.get(),
+ effective_subtitle.fade_up_time,
+ effective_subtitle.fade_down_time
+ )
+ )
+ );
+}
+
FontNode::FontNode (xmlpp::Node const * node)
: XMLNode (node)
{
+ text = content ();
+
id = optional_string_attribute ("Id");
size = optional_int64_attribute ("Size");
italic = optional_bool_attribute ("Italic");
@@ -209,8 +232,9 @@ TextNode::TextNode (xmlpp::Node const * node)
} else if (v == "bottom") {
v_align = BOTTOM;
}
-}
+ font_nodes = sub_nodes<FontNode> ("Font");
+}
list<shared_ptr<Subtitle> >
SubtitleAsset::subtitles_at (Time t) const
diff --git a/src/subtitle_asset.h b/src/subtitle_asset.h
index efcab3a3..4d62bfb4 100644
--- a/src/subtitle_asset.h
+++ b/src/subtitle_asset.h
@@ -35,6 +35,7 @@ public:
float v_position;
VAlign v_align;
std::string text;
+ std::list<boost::shared_ptr<FontNode> > font_nodes;
};
class SubtitleNode : public XMLNode
@@ -61,6 +62,7 @@ public:
FontNode (xmlpp::Node const * node);
FontNode (std::list<boost::shared_ptr<FontNode> > const & font_nodes);
+ std::string text;
std::string id;
int size;
boost::optional<bool> italic;
@@ -198,16 +200,22 @@ public:
private:
std::string font_id_to_name (std::string id) const;
+ struct ParseState {
+ std::list<boost::shared_ptr<FontNode> > font_nodes;
+ std::list<boost::shared_ptr<TextNode> > text_nodes;
+ std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
+ };
+
+ void maybe_add_subtitle (std::string text, ParseState const & parse_state);
+
void examine_font_nodes (
std::list<boost::shared_ptr<FontNode> > const & font_nodes,
- std::list<boost::shared_ptr<FontNode> >& current_font_nodes,
- std::list<boost::shared_ptr<SubtitleNode> >& current_subtitle_nodes
+ ParseState& parse_state
);
void examine_text_nodes (
- boost::shared_ptr<SubtitleNode> subtitle_node,
std::list<boost::shared_ptr<TextNode> > const & text_nodes,
- std::list<boost::shared_ptr<FontNode> >& current_font_nodes
+ ParseState& parse_state
);
std::string _subtitle_id;
diff --git a/src/util.cc b/src/util.cc
index fd76e556..913b8b50 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -252,3 +252,15 @@ libdcp::xyz_to_rgb (opj_image_t* xyz_frame)
return argb_frame;
}
+
+bool
+libdcp::empty_or_white_space (string s)
+{
+ for (size_t i = 0; i < s.length(); ++i) {
+ if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
+ return false;
+ }
+ }
+
+ return true;
+}
diff --git a/src/util.h b/src/util.h
index 9485c450..fd0ea5e9 100644
--- a/src/util.h
+++ b/src/util.h
@@ -48,6 +48,7 @@ extern std::string content_kind_to_string (ContentKind kind);
extern ContentKind content_kind_from_string (std::string kind);
extern bool starts_with (std::string big, std::string little);
extern bool ends_with (std::string big, std::string little);
+extern bool empty_or_white_space (std::string s);
extern opj_image_t* decompress_j2k (uint8_t* data, int64_t size, int reduce);
extern boost::shared_ptr<ARGBFrame> xyz_to_rgb (opj_image_t* xyz_frame);
diff --git a/src/xml.cc b/src/xml.cc
index 7ca3cc1a..4982e9fb 100644
--- a/src/xml.cc
+++ b/src/xml.cc
@@ -226,22 +226,17 @@ XMLNode::done ()
string
XMLNode::content ()
{
+ string content;
+
xmlpp::Node::NodeList c = _node->get_children ();
-
- if (c.size() > 1) {
- throw XMLError ("unexpected content in XML node");
+ for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
+ xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
+ if (v) {
+ content += v->get_content ();
+ }
}
- if (c.empty ()) {
- return "";
- }
-
- xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (c.front());
- if (!v) {
- throw XMLError ("missing content in XML node");
- }
-
- return v->get_content ();
+ return content;
}
XMLFile::XMLFile (string file, string root_name)