+ vector<ReelWriter>::iterator* reel = nullptr;
+
+ switch (type) {
+ case TextType::OPEN_SUBTITLE:
+ reel = &_subtitle_reel;
+ _have_subtitles = true;
+ break;
+ case TextType::CLOSED_CAPTION:
+ DCPOMATIC_ASSERT (track);
+ DCPOMATIC_ASSERT (_caption_reels.find(*track) != _caption_reels.end());
+ reel = &_caption_reels[*track];
+ _have_closed_captions.insert (*track);
+ break;
+ default:
+ DCPOMATIC_ASSERT (false);
+ }
+
+ DCPOMATIC_ASSERT (*reel != _reels.end());
+ while ((*reel)->period().to <= period.from) {
+ ++(*reel);
+ DCPOMATIC_ASSERT (*reel != _reels.end());
+ write_hanging_text (**reel);
+ }
+
+ auto back_off = [this](DCPTimePeriod period) {
+ period.to -= DCPTime::from_frames(2, film()->video_frame_rate());
+ return period;
+ };
+
+ if (period.to > (*reel)->period().to) {
+ /* This text goes off the end of the reel. Store parts of it that should go into
+ * other reels.
+ */
+ for (auto i = std::next(*reel); i != _reels.end(); ++i) {
+ auto overlap = i->period().overlap(period);
+ if (overlap) {
+ _hanging_texts.push_back (HangingText{text, type, track, back_off(*overlap)});
+ }
+ }
+ /* Back off from the reel boundary by a couple of frames to avoid tripping checks
+ * for subtitles being too close together.
+ */
+ period.to = (*reel)->period().to;
+ period = back_off(period);