+
+bool
+libdcp::operator== (Subtitle const & a, Subtitle const & b)
+{
+ return (
+ a.font() == b.font() &&
+ a.italic() == b.italic() &&
+ a.color() == b.color() &&
+ a.size() == b.size() &&
+ a.in() == b.in() &&
+ a.out() == b.out() &&
+ a.v_position() == b.v_position() &&
+ a.v_align() == b.v_align() &&
+ a.text() == b.text() &&
+ a.effect() == b.effect() &&
+ a.effect_color() == b.effect_color() &&
+ a.fade_up_time() == b.fade_up_time() &&
+ a.fade_down_time() == b.fade_down_time()
+ );
+}
+
+ostream&
+libdcp::operator<< (ostream& s, Subtitle const & sub)
+{
+ s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
+ << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
+ << "font " << sub.font() << ", ";
+
+ if (sub.italic()) {
+ s << "italic";
+ } else {
+ s << "non-italic";
+ }
+
+ s << ", size " << sub.size() << ", color " << sub.color() << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align()) << ";\n"
+ << "effect " << ((int) sub.effect()) << ", effect color " << sub.effect_color();
+
+ return s;
+}
+
+void
+SubtitleAsset::add (shared_ptr<Subtitle> s)
+{
+ _subtitles.push_back (s);
+}
+
+void
+SubtitleAsset::write_to_cpl (ostream& s) const
+{
+ /* XXX: should EditRate, Duration and IntrinsicDuration be in here? */
+
+ s << " <MainSubtitle>\n"
+ << " <Id>urn:uuid:" << _uuid << "</Id>\n"
+ << " <AnnotationText>" << _file_name << "</AnnotationText>\n"
+ << " <EntryPoint>0</EntryPoint>\n"
+ << " </MainSubtitle>\n";
+}
+
+struct SubtitleSorter {
+ bool operator() (shared_ptr<Subtitle> a, shared_ptr<Subtitle> b) {
+ if (a->in() != b->in()) {
+ return a->in() < b->in();
+ }
+ return a->v_position() < b->v_position();
+ }
+};
+
+void
+SubtitleAsset::write_xml ()
+{
+ ofstream f (path().string().c_str());
+ write_xml (f);
+}
+
+void
+SubtitleAsset::write_xml (ostream& s)
+{
+ s << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ << "<DCSubtitle Version=\"1.0\">\n"
+ << " <SubtitleID>" << _uuid << "</SubtitleID>\n"
+ << " <MovieTitle>" << _movie_title << "</MovieTitle>\n"
+ << " <ReelNumber>" << _reel_number << "</ReelNumber>\n"
+ << " <Language>" << _language << "</Language>\n"
+ << " <LoadFont Id=\"theFontId\" URI=\"arial.ttf\"/>\n";
+
+ _subtitles.sort (SubtitleSorter ());
+
+ /* XXX: multiple fonts not supported */
+ /* XXX: script, underlined, weight not supported */
+
+ bool first = true;
+ bool italic = false;
+ Color color;
+ int size = 0;
+ Effect effect = NONE;
+ Color effect_color;
+ int spot_number = 1;
+ Time last_in;
+ Time last_out;
+ Time last_fade_up_time;
+ Time last_fade_down_time;
+
+ for (list<shared_ptr<Subtitle> >::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+
+ /* We will start a new <Font>...</Font> whenever some font property changes.
+ I suppose should really make an optimal hierarchy of <Font> tags, but
+ that seems hard.
+ */
+
+ bool const font_changed = first ||
+ italic != (*i)->italic() ||
+ color != (*i)->color() ||
+ size != (*i)->size() ||
+ effect != (*i)->effect() ||
+ effect_color != (*i)->effect_color();
+
+ stringstream a;
+ if (font_changed) {
+ italic = (*i)->italic ();
+ a << "Italic=\"" << (italic ? "yes" : "no") << "\" ";
+ color = (*i)->color ();
+ a << "Color=\"" << color.to_argb_string() << "\" ";
+ size = (*i)->size ();
+ a << "Size=\"" << size << "\" ";
+ effect = (*i)->effect ();
+ a << "Effect=\"" << effect_to_string(effect) << "\" ";
+ effect_color = (*i)->effect_color ();
+ a << "EffectColor=\"" << effect_color.to_argb_string() << "\" ";
+ a << "Script=\"normal\" Underlined=\"no\" Weight=\"normal\"";
+ }
+
+ if (first ||
+ (last_in != (*i)->in() ||
+ last_out != (*i)->out() ||
+ last_fade_up_time != (*i)->fade_up_time() ||
+ last_fade_down_time != (*i)->fade_down_time()
+ )) {
+
+ if (!first) {
+ s << " </Subtitle>\n";
+ }
+
+ if (font_changed) {
+ if (!first) {
+ s << " </Font>\n";
+ }
+ s << " <Font Id=\"theFontId\" " << a.str() << ">\n";
+ }
+
+ s << " <Subtitle "
+ << "SpotNumber=\"" << spot_number++ << "\" "
+ << "TimeIn=\"" << (*i)->in().to_string() << "\" "
+ << "TimeOut=\"" << (*i)->out().to_string() << "\" "
+ << "FadeUpTime=\"" << (*i)->fade_up_time().to_ticks() << "\" "
+ << "FadeDownTime=\"" << (*i)->fade_down_time().to_ticks() << "\""
+ << ">\n";
+
+ last_in = (*i)->in ();
+ last_out = (*i)->out ();
+ last_fade_up_time = (*i)->fade_up_time ();
+ last_fade_down_time = (*i)->fade_down_time ();
+ }
+
+ s << " <Text "
+ << "VAlign=\"" << valign_to_string ((*i)->v_align()) << "\" "
+ << "VPosition=\"" << (*i)->v_position() << "\""
+ << ">" << (*i)->text() << "</Text>\n";
+
+ first = false;
+ }
+
+ s << " </Subtitle>\n";
+ s << " </Font>\n";
+ s << "</DCSubtitle>\n";
+}