Move some functions up the file so we can use them.
[dcpomatic.git] / src / lib / render_text.cc
index d4d827a6b23c1b629408a007a4401bb9a523cf00..989bc7dfea7d33e83c575057d700c3d9f482fcc6 100644 (file)
@@ -34,9 +34,6 @@ DCPOMATIC_DISABLE_WARNINGS
 #include <pangomm.h>
 DCPOMATIC_ENABLE_WARNINGS
 #include <pango/pangocairo.h>
-#ifndef DCPOMATIC_HAVE_SHOW_IN_CAIRO_CONTEXT
-#include <pango/pangocairo.h>
-#endif
 #include <boost/algorithm/string.hpp>
 #include <iostream>
 
@@ -51,8 +48,6 @@ using std::min;
 using std::pair;
 using std::shared_ptr;
 using std::string;
-using boost::optional;
-using boost::algorithm::replace_all;
 using namespace dcpomatic;
 
 
@@ -60,6 +55,33 @@ static FcConfig* fc_config = nullptr;
 static list<pair<boost::filesystem::path, string>> fc_config_fonts;
 
 
+/** 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()
+{
+       auto c_font_map = pango_cairo_font_map_new ();
+       DCPOMATIC_ASSERT (c_font_map);
+       auto font_map = Glib::wrap (c_font_map);
+       auto c_context = pango_font_map_create_context (c_font_map);
+       DCPOMATIC_ASSERT (c_context);
+       auto context = Glib::wrap (c_context);
+       return Pango::Layout::create (context);
+}
+
+
+static void
+setup_layout (Glib::RefPtr<Pango::Layout> layout, string font_name, string markup)
+{
+       layout->set_alignment (Pango::ALIGN_LEFT);
+       Pango::FontDescription font (font_name);
+       layout->set_font_description (font);
+       layout->set_markup (markup);
+}
+
+
 string
 marked_up (list<StringText> subtitles, int target_height, float fade_factor)
 {
@@ -80,11 +102,7 @@ marked_up (list<StringText> subtitles, int target_height, float fade_factor)
                /* Between 1-65535 inclusive, apparently... */
                out += "alpha=\"" + dcp::raw_convert<string>(int(floor(fade_factor * 65534)) + 1) + "\" ";
                out += "color=\"#" + i.colour().to_rgb_string() + "\">";
-
-               string t = i.text();
-               replace_all(t, "&", "&amp;");
-               out += t;
-
+               out += i.text();
                out += "</span>";
        }
 
@@ -102,8 +120,10 @@ set_source_rgba (Cairo::RefPtr<Cairo::Context> context, dcp::Colour colour, floa
 static shared_ptr<Image>
 create_image (dcp::Size size)
 {
-       /* FFmpeg BGRA means first byte blue, second byte green, third byte red, fourth byte alpha */
-       auto image = make_shared<Image>(AV_PIX_FMT_BGRA, size, false);
+       /* FFmpeg BGRA means first byte blue, second byte green, third byte red, fourth byte alpha.
+        * This must be COMPACT as we're using it with Cairo::ImageSurface::create
+        */
+       auto image = make_shared<Image>(AV_PIX_FMT_BGRA, size, Image::Alignment::COMPACT);
        image->make_black ();
        return image;
 }
@@ -112,8 +132,12 @@ create_image (dcp::Size size)
 static Cairo::RefPtr<Cairo::ImageSurface>
 create_surface (shared_ptr<Image> image)
 {
-#ifdef DCPOMATIC_HAVE_FORMAT_STRIDE_FOR_WIDTH
-       auto surface = Cairo::ImageSurface::create (
+       /* XXX: I don't think it's guaranteed that format_stride_for_width will return a stride without any padding,
+        * so it's lucky that this works.
+        */
+       DCPOMATIC_ASSERT (image->alignment() == Image::Alignment::COMPACT);
+       DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_BGRA);
+       return Cairo::ImageSurface::create (
                image->data()[0],
                Cairo::FORMAT_ARGB32,
                image->size().width,
@@ -121,20 +145,6 @@ create_surface (shared_ptr<Image> image)
                /* Cairo ARGB32 means first byte blue, second byte green, third byte red, fourth byte alpha */
                Cairo::ImageSurface::format_stride_for_width (Cairo::FORMAT_ARGB32, image->size().width)
                );
-#else
-       /* Centos 5 does not have Cairo::ImageSurface::format_stride_for_width, so just use width * 4
-          which I hope is safe (if slow)
-       */
-       auto surface = Cairo::ImageSurface::create (
-               image->data()[0],
-               Cairo::FORMAT_ARGB32,
-               image->size().width,
-               image->size().height,
-               image->size().width * 4
-               );
-#endif
-
-       return surface;
 }
 
 
@@ -276,33 +286,6 @@ y_position (StringText const& first, int target_height, int layout_height)
 }
 
 
-static void
-setup_layout (Glib::RefPtr<Pango::Layout> layout, string font_name, string markup)
-{
-       layout->set_alignment (Pango::ALIGN_LEFT);
-       Pango::FontDescription font (font_name);
-       layout->set_font_description (font);
-       layout->set_markup (markup);
-}
-
-
-/** 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()
-{
-       auto c_font_map = pango_cairo_font_map_new ();
-       DCPOMATIC_ASSERT (c_font_map);
-       auto font_map = Glib::wrap (c_font_map);
-       auto c_context = pango_font_map_create_context (c_font_map);
-       DCPOMATIC_ASSERT (c_context);
-       auto 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.
  */