+/** Create a Pango layout using a dummy context which we can use to calculate the size
+ * of the text we will render. Then we can transfer the layout over to the real context
+ * for the actual render.
+ */
+static Glib::RefPtr<Pango::Layout>
+create_layout()
+{
+ PangoFontMap* c_font_map = pango_cairo_font_map_new ();
+ Glib::RefPtr<Pango::FontMap> font_map = Glib::wrap (c_font_map);
+ PangoContext* c_context = pango_font_map_create_context (c_font_map);
+ Glib::RefPtr<Pango::Context> context = Glib::wrap (c_context);
+ return Pango::Layout::create (context);
+}
+
+
+/** @param subtitles A list of subtitles that are all on the same line,
+ * at the same time and with the same fade in/out.
+ */
+static PositionImage
+render_line (list<StringText> subtitles, list<shared_ptr<Font> > fonts, dcp::Size target, DCPTime time, int frame_rate)
+{
+ /* XXX: this method can only handle italic / bold changes mid-line,
+ nothing else yet.
+ */
+
+ DCPOMATIC_ASSERT (!subtitles.empty ());
+ StringText const& first = subtitles.front ();
+
+ string const font_name = setup_font (first, fonts);
+ float const fade_factor = calculate_fade_factor (first, time, frame_rate);
+ string const markup = marked_up (subtitles, target.height, fade_factor);
+ Glib::RefPtr<Pango::Layout> layout = create_layout ();
+ setup_layout (layout, font_name, markup);
+ dcp::Size size;
+ layout->get_pixel_size (size.width, size.height);
+
+ /* Calculate x and y scale factors. These are only used to stretch
+ the font away from its normal aspect ratio.
+ */
+ float x_scale = 1;
+ float y_scale = 1;
+ if (fabs (first.aspect_adjust() - 1.0) > dcp::ASPECT_ADJUST_EPSILON) {
+ if (first.aspect_adjust() < 1) {
+ x_scale = max (0.25f, first.aspect_adjust ());
+ y_scale = 1;
+ } else {
+ x_scale = 1;
+ y_scale = 1 / min (4.0f, first.aspect_adjust ());
+ }
+ }
+
+ size.width *= x_scale;
+ size.height *= y_scale;