summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-08-21 01:13:24 +0100
committerCarl Hetherington <cth@carlh.net>2012-08-21 01:13:24 +0100
commit8409171ec7d749a2426cca57cf6edd6714dc497d (patch)
treee20fd664c31b0a52e7fc60bdfd60f38639accd06 /src
parent4d91615b49a3246654baaf7a08f10f303b79b85a (diff)
De-hackify subtitle handling a bit.
Diffstat (limited to 'src')
-rw-r--r--src/subtitle_asset.cc116
-rw-r--r--src/subtitle_asset.h108
-rw-r--r--src/xml.cc6
-rw-r--r--src/xml.h1
4 files changed, 170 insertions, 61 deletions
diff --git a/src/subtitle_asset.cc b/src/subtitle_asset.cc
index 22098213..5238e56a 100644
--- a/src/subtitle_asset.cc
+++ b/src/subtitle_asset.cc
@@ -34,51 +34,121 @@ SubtitleAsset::SubtitleAsset (string directory, string xml)
ignore_node ("LoadFont");
- _fonts = sub_nodes<Font> ("Font");
+ _font_nodes = sub_nodes<FontNode> ("Font");
+ list<shared_ptr<LoadFontNode> > load_font_nodes = sub_nodes<LoadFontNode> ("LoadFont");
+
+ /* Now make Subtitle objects to represent the raw XML nodes
+ in a sane way.
+ */
+
+ for (list<shared_ptr<FontNode> >::iterator i = _font_nodes.begin(); i != _font_nodes.end(); ++i) {
+ for (list<shared_ptr<SubtitleNode> >::iterator j = (*i)->subtitle_nodes.begin(); j != (*i)->subtitle_nodes.end(); ++j) {
+ for (list<shared_ptr<TextNode> >::iterator k = (*j)->text_nodes.begin(); k != (*j)->text_nodes.end(); ++k) {
+ _subtitles.push_back (
+ shared_ptr<Subtitle> (
+ new Subtitle (
+ font_id_to_name ((*i)->id, load_font_nodes),
+ (*i)->size,
+ (*j)->in,
+ (*j)->out,
+ (*k)->v_position,
+ (*k)->text
+ )
+ )
+ );
+ }
+ }
+ }
+}
+
+FontNode::FontNode (xmlpp::Node const * node)
+ : XMLNode (node)
+{
+ id = string_attribute ("Id");
+ size = int64_attribute ("Size");
+ subtitle_nodes = sub_nodes<SubtitleNode> ("Subtitle");
}
-Font::Font (xmlpp::Node const * node)
+LoadFontNode::LoadFontNode (xmlpp::Node const * node)
: XMLNode (node)
{
- _subtitles = sub_nodes<Subtitle> ("Subtitle");
+ id = string_attribute ("Id");
+ uri = string_attribute ("URI");
}
+
-Subtitle::Subtitle (xmlpp::Node const * node)
+SubtitleNode::SubtitleNode (xmlpp::Node const * node)
: XMLNode (node)
{
- _in = time_attribute ("TimeIn");
- _out = time_attribute ("TimeOut");
- _texts = sub_nodes<Text> ("Text");
+ in = time_attribute ("TimeIn");
+ out = time_attribute ("TimeOut");
+ text_nodes = sub_nodes<TextNode> ("Text");
}
-Text::Text (xmlpp::Node const * node)
+TextNode::TextNode (xmlpp::Node const * node)
: XMLNode (node)
{
- _text = content ();
- _v_position = float_attribute ("VPosition");
+ text = content ();
+ v_position = float_attribute ("VPosition");
}
-list<shared_ptr<Text> >
+list<shared_ptr<Subtitle> >
SubtitleAsset::subtitles_at (Time t) const
{
- for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
- list<shared_ptr<Text> > s = (*i)->subtitles_at (t);
- if (!s.empty ()) {
- return s;
+ list<shared_ptr<Subtitle> > s;
+ for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+ if ((*i)->in() <= t && t <= (*i)->out ()) {
+ s.push_back (*i);
}
}
- return list<shared_ptr<Text> > ();
+ return s;
}
-list<shared_ptr<Text> >
-Font::subtitles_at (Time t) const
+std::string
+SubtitleAsset::font_id_to_name (string id, list<shared_ptr<LoadFontNode> > const & load_font_nodes) const
{
- for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
- if ((*i)->in() <= t && t <= (*i)->out()) {
- return (*i)->texts ();
- }
+ list<shared_ptr<LoadFontNode> >::const_iterator i = load_font_nodes.begin();
+ while (i != load_font_nodes.end() && (*i)->id != id) {
+ ++i;
+ }
+
+ if (i == load_font_nodes.end ()) {
+ return "";
+ }
+
+ if ((*i)->uri == "arial.ttf") {
+ return "Arial";
}
- return list<shared_ptr<Text> > ();
+ return "";
+}
+
+Subtitle::Subtitle (
+ std::string font,
+ int size,
+ Time in,
+ Time out,
+ float v_position,
+ std::string text
+ )
+ : _font (font)
+ , _size (size)
+ , _in (in)
+ , _out (out)
+ , _v_position (v_position)
+ , _text (text)
+{
+
+}
+
+int
+Subtitle::size_in_pixels (int screen_height) const
+{
+ /* Size in the subtitle file is given in points as if the screen
+ height is 11 inches, so a 72pt font would be 1/11th of the screen
+ height.
+ */
+
+ return _size * screen_height / (11 * 72);
}
diff --git a/src/subtitle_asset.h b/src/subtitle_asset.h
index 6d584a0a..71ae1401 100644
--- a/src/subtitle_asset.h
+++ b/src/subtitle_asset.h
@@ -24,30 +24,64 @@
namespace libdcp
{
-class Text : public XMLNode
+class TextNode : public XMLNode
{
public:
- Text () {}
- Text (xmlpp::Node const * node);
+ TextNode () {}
+ TextNode (xmlpp::Node const * node);
- float v_position () const {
- return _v_position;
- }
+ float v_position;
+ std::string text;
+};
- std::string text () const {
- return _text;
- }
+class SubtitleNode : public XMLNode
+{
+public:
+ SubtitleNode () {}
+ SubtitleNode (xmlpp::Node const * node);
-private:
- float _v_position;
- std::string _text;
+ Time in;
+ Time out;
+ std::list<boost::shared_ptr<TextNode> > text_nodes;
+};
+
+class FontNode : public XMLNode
+{
+public:
+ FontNode () {}
+ FontNode (xmlpp::Node const * node);
+
+ std::string id;
+ int size;
+
+ std::list<boost::shared_ptr<SubtitleNode> > subtitle_nodes;
+};
+
+class LoadFontNode : public XMLNode
+{
+public:
+ LoadFontNode () {}
+ LoadFontNode (xmlpp::Node const * node);
+
+ std::string id;
+ std::string uri;
};
-class Subtitle : public XMLNode
+class Subtitle
{
public:
- Subtitle () {}
- Subtitle (xmlpp::Node const * node);
+ Subtitle (
+ std::string font,
+ int size,
+ Time in,
+ Time out,
+ float v_position,
+ std::string text
+ );
+
+ std::string font () const {
+ return _font;
+ }
Time in () const {
return _in;
@@ -57,32 +91,25 @@ public:
return _out;
}
- std::list<boost::shared_ptr<Text> > const & texts () const {
- return _texts;
+ std::string text () const {
+ return _text;
+ }
+
+ float v_position () const {
+ return _v_position;
}
+ int size_in_pixels (int screen_height) const;
+
private:
- std::list<boost::shared_ptr<Text> > _texts;
+ std::string _font;
+ int _size;
Time _in;
Time _out;
+ float _v_position;
+ std::string _text;
};
-class Font : public XMLNode
-{
-public:
- Font () {}
- Font (xmlpp::Node const * node);
-
- std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
- return _subtitles;
- }
-
- std::list<boost::shared_ptr<Text> > subtitles_at (Time t) const;
-
-private:
- std::list<boost::shared_ptr<Subtitle> > _subtitles;
-};
-
class SubtitleAsset : public Asset, public XMLFile
{
public:
@@ -90,6 +117,7 @@ public:
void write_to_cpl (std::ostream&) const {}
virtual std::list<std::string> equals (boost::shared_ptr<const Asset>, EqualityOptions) const {
+ /* XXX */
return std::list<std::string> ();
}
@@ -97,18 +125,22 @@ public:
return _language;
}
- std::list<boost::shared_ptr<Text> > subtitles_at (Time t) const;
+ std::list<boost::shared_ptr<Subtitle> > subtitles_at (Time t) const;
- std::list<boost::shared_ptr<Font> > const & fonts () const {
- return _fonts;
+ std::list<boost::shared_ptr<FontNode> > font_nodes () const {
+ return _font_nodes;
}
private:
+ std::string font_id_to_name (std::string id, std::list<boost::shared_ptr<LoadFontNode> > const & load_font_nodes) const;
+
std::string _subtitle_id;
std::string _movie_title;
int64_t _reel_number;
std::string _language;
- std::list<boost::shared_ptr<Font> > _fonts;
+ std::list<boost::shared_ptr<FontNode> > _font_nodes;
+
+ std::list<boost::shared_ptr<Subtitle> > _subtitles;
};
}
diff --git a/src/xml.cc b/src/xml.cc
index 21912bdf..0f0d2d47 100644
--- a/src/xml.cc
+++ b/src/xml.cc
@@ -157,6 +157,12 @@ XMLNode::float_attribute (string name)
return lexical_cast<float> (string_attribute (name));
}
+int64_t
+XMLNode::int64_attribute (string name)
+{
+ return lexical_cast<int64_t> (string_attribute (name));
+}
+
void
XMLNode::done ()
{
diff --git a/src/xml.h b/src/xml.h
index 85589a0f..d7f6ad36 100644
--- a/src/xml.h
+++ b/src/xml.h
@@ -37,6 +37,7 @@ protected:
Time time_attribute (std::string);
float float_attribute (std::string);
std::string string_attribute (std::string);
+ int64_t int64_attribute (std::string);
std::string content ();