Rearrange subtitle font management.
[dcpomatic.git] / src / lib / render_text.cc
index 0c14b00c4e7c3881a7b45b72ffa4b39bc40c420c..284e4fa88924ea97b027e5154a99ea159f88f76a 100644 (file)
 #include "cross.h"
 #include "dcpomatic_assert.h"
 #include "font.h"
+#include "font_config.h"
 #include "image.h"
 #include "render_text.h"
 #include "types.h"
 #include "util.h"
 #include <dcp/raw_convert.h>
 #include <dcp/warnings.h>
-#include <fontconfig/fontconfig.h>
 #include <cairomm/cairomm.h>
 LIBDCP_DISABLE_WARNINGS
 #include <pangomm.h>
@@ -51,10 +51,6 @@ using std::string;
 using namespace dcpomatic;
 
 
-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.
@@ -99,7 +95,7 @@ marked_up (list<StringText> subtitles, int target_height, float fade_factor, str
                if (subtitle.underline()) {
                        span += "underline=\"single\" ";
                }
-               span += "size=\"" + dcp::raw_convert<string>(subtitle.size_in_pixels(target_height) * pixels_to_1024ths_point) + "\" ";
+               span += "size=\"" + dcp::raw_convert<string>(lrintf(subtitle.size_in_pixels(target_height) * pixels_to_1024ths_point)) + "\" ";
                /* Between 1-65535 inclusive, apparently... */
                span += "alpha=\"" + dcp::raw_convert<string>(int(floor(fade_factor * 65534)) + 1) + "\" ";
                span += "color=\"#" + subtitle.colour().to_rgb_string() + "\"";
@@ -175,62 +171,15 @@ create_surface (shared_ptr<Image> image)
 
 
 static string
-setup_font (StringText const& subtitle, list<shared_ptr<Font>> const& fonts)
+setup_font (StringText const& subtitle)
 {
-       if (!fc_config) {
-               fc_config = FcInitLoadConfig ();
-       }
-
        auto font_file = default_font_file ();
 
-       for (auto i: fonts) {
-               if (i->id() == subtitle.font() && i->file()) {
-                       font_file = i->file().get();
-               }
-       }
-
-       auto existing = fc_config_fonts.cbegin ();
-       while (existing != fc_config_fonts.end() && existing->first != font_file) {
-               ++existing;
-       }
-
-       string font_name;
-       if (existing != fc_config_fonts.end ()) {
-               font_name = existing->second;
-       } else {
-               /* Make this font available to DCP-o-matic */
-               FcConfigAppFontAddFile (fc_config, reinterpret_cast<FcChar8 const *>(font_file.string().c_str()));
-               auto pattern = FcPatternBuild (
-                       0, FC_FILE, FcTypeString, font_file.string().c_str(), static_cast<char *>(0)
-                       );
-               auto object_set = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, static_cast<char *> (0));
-               auto font_set = FcFontList (fc_config, pattern, object_set);
-               if (font_set) {
-                       for (int i = 0; i < font_set->nfont; ++i) {
-                               FcPattern* font = font_set->fonts[i];
-                               FcChar8* file;
-                               FcChar8* family;
-                               FcChar8* style;
-                               if (
-                                       FcPatternGetString (font, FC_FILE, 0, &file) == FcResultMatch &&
-                                       FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
-                                       FcPatternGetString (font, FC_STYLE, 0, &style) == FcResultMatch
-                                       ) {
-                                       font_name = reinterpret_cast<char const *> (family);
-                               }
-                       }
-
-                       FcFontSetDestroy (font_set);
-               }
-
-               FcObjectSetDestroy (object_set);
-               FcPatternDestroy (pattern);
-
-               fc_config_fonts.push_back (make_pair(font_file, font_name));
+       if (subtitle.font && subtitle.font->file()) {
+               font_file = *subtitle.font->file();
        }
 
-       FcConfigSetCurrent (fc_config);
-       return font_name;
+       return FontConfig::instance()->make_font_available(font_file);
 }
 
 
@@ -327,7 +276,7 @@ y_position (StringText const& first, int target_height, int layout_height)
  *  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)
+render_line (list<StringText> subtitles, dcp::Size target, DCPTime time, int frame_rate)
 {
        /* XXX: this method can only handle italic / bold changes mid-line,
           nothing else yet.
@@ -336,7 +285,7 @@ render_line (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size
        DCPOMATIC_ASSERT (!subtitles.empty ());
        auto const& first = subtitles.front ();
 
-       auto const font_name = setup_font (first, fonts);
+       auto const font_name = setup_font (first);
        auto const fade_factor = calculate_fade_factor (first, time, frame_rate);
        auto const markup = marked_up (subtitles, target.height, fade_factor, font_name);
        auto layout = create_layout ();
@@ -424,21 +373,21 @@ render_line (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size
  *  @param frame_rate DCP frame rate.
  */
 list<PositionImage>
-render_text (list<StringText> subtitles, list<shared_ptr<Font>> fonts, dcp::Size target, DCPTime time, int frame_rate)
+render_text (list<StringText> subtitles, dcp::Size target, DCPTime time, int frame_rate)
 {
        list<StringText> pending;
        list<PositionImage> images;
 
        for (auto const& i: subtitles) {
                if (!pending.empty() && (i.v_align() != pending.back().v_align() || fabs(i.v_position() - pending.back().v_position()) > 1e-4)) {
-                       images.push_back (render_line (pending, fonts, target, time, frame_rate));
+                       images.push_back(render_line(pending, target, time, frame_rate));
                        pending.clear ();
                }
                pending.push_back (i);
        }
 
        if (!pending.empty()) {
-               images.push_back (render_line (pending, fonts, target, time, frame_rate));
+               images.push_back(render_line(pending,  target, time, frame_rate));
        }
 
        return images;