+ auto sorted = _subtitles;
+ std::stable_sort(sorted.begin(), sorted.end(), SubtitleSorter());
+
+ /* Gather our subtitles into a hierarchy of Subtitle/Text/String objects, writing
+ font information into the bottom level (String) objects.
+ */
+
+ auto root = make_shared<order::Part>(shared_ptr<order::Part>());
+ shared_ptr<order::Subtitle> subtitle;
+ shared_ptr<order::Text> text;
+
+ Time last_in;
+ Time last_out;
+ Time last_fade_up_time;
+ Time last_fade_down_time;
+ HAlign last_h_align;
+ float last_h_position;
+ VAlign last_v_align;
+ float last_v_position;
+ Direction last_direction;
+
+ for (auto i: sorted) {
+ if (!subtitle ||
+ (last_in != i->in() ||
+ last_out != i->out() ||
+ last_fade_up_time != i->fade_up_time() ||
+ last_fade_down_time != i->fade_down_time())
+ ) {
+
+ subtitle = make_shared<order::Subtitle>(root, i->in(), i->out(), i->fade_up_time(), i->fade_down_time());
+ root->children.push_back (subtitle);
+
+ last_in = i->in ();
+ last_out = i->out ();
+ last_fade_up_time = i->fade_up_time ();
+ last_fade_down_time = i->fade_down_time ();
+ text.reset ();
+ }
+
+ auto is = dynamic_pointer_cast<SubtitleString>(i);
+ if (is) {
+ if (!text ||
+ last_h_align != is->h_align() ||
+ fabs(last_h_position - is->h_position()) > ALIGN_EPSILON ||
+ last_v_align != is->v_align() ||
+ 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()));
+ subtitle->children.push_back (text);
+
+ last_h_align = is->h_align ();
+ last_h_position = is->h_position ();
+ last_v_align = is->v_align ();
+ last_v_position = is->v_position ();
+ last_direction = is->direction ();
+ }
+
+ text->children.push_back (make_shared<order::String>(text, order::Font (is, standard), is->text()));
+ }