Move fontconfig-related code out to a class.
[dcpomatic.git] / src / lib / render_text.cc
index bcf04147a1af14496ed187e5824bbf867a70da93..f1fce62b07dbac20c47a0169967634379ffe1b70 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 "warnings.h"
 #include <dcp/raw_convert.h>
-#include <fontconfig/fontconfig.h>
+#include <dcp/warnings.h>
 #include <cairomm/cairomm.h>
-DCPOMATIC_DISABLE_WARNINGS
+LIBDCP_DISABLE_WARNINGS
 #include <pangomm.h>
-DCPOMATIC_ENABLE_WARNINGS
+LIBDCP_ENABLE_WARNINGS
 #include <pango/pangocairo.h>
 #include <boost/algorithm/string.hpp>
 #include <iostream>
@@ -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() + "\"";
@@ -177,10 +173,6 @@ create_surface (shared_ptr<Image> image)
 static string
 setup_font (StringText const& subtitle, list<shared_ptr<Font>> const& fonts)
 {
-       if (!fc_config) {
-               fc_config = FcInitLoadConfig ();
-       }
-
        auto font_file = default_font_file ();
 
        for (auto i: fonts) {
@@ -189,48 +181,7 @@ setup_font (StringText const& subtitle, list<shared_ptr<Font>> const& fonts)
                }
        }
 
-       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));
-       }
-
-       FcConfigSetCurrent (fc_config);
-       return font_name;
+       return FontConfig::instance()->make_font_available(font_file);
 }
 
 
@@ -244,19 +195,30 @@ calculate_fade_factor (StringText const& first, DCPTime time, int frame_rate)
        */
        auto const fade_in_start = DCPTime::from_seconds(first.in().as_seconds()).round(frame_rate);
        auto const fade_in_end = fade_in_start + DCPTime::from_seconds (first.fade_up_time().as_seconds ());
-       auto const fade_out_end =  DCPTime::from_seconds (first.out().as_seconds()).round(frame_rate);
-       auto const fade_out_start = fade_out_end - DCPTime::from_seconds (first.fade_down_time().as_seconds ());
 
        if (fade_in_start <= time && time <= fade_in_end && fade_in_start != fade_in_end) {
                fade_factor *= DCPTime(time - fade_in_start).seconds() / DCPTime(fade_in_end - fade_in_start).seconds();
        }
-       if (fade_out_start <= time && time <= fade_out_end && fade_out_start != fade_out_end) {
-               fade_factor *= 1 - DCPTime(time - fade_out_start).seconds() / DCPTime(fade_out_end - fade_out_start).seconds();
-       }
-       if (time < fade_in_start || time > fade_out_end) {
+
+       if (time < fade_in_start) {
                fade_factor = 0;
        }
 
+       /* first.out() may be zero if we don't know when this subtitle will finish.  We can only think about
+        * fading out if we _do_ know when it will finish.
+        */
+       if (first.out() != dcp::Time()) {
+               auto const fade_out_end = DCPTime::from_seconds (first.out().as_seconds()).round(frame_rate);
+               auto const fade_out_start = fade_out_end - DCPTime::from_seconds(first.fade_down_time().as_seconds());
+
+               if (fade_out_start <= time && time <= fade_out_end && fade_out_start != fade_out_end) {
+                       fade_factor *= 1 - DCPTime(time - fade_out_start).seconds() / DCPTime(fade_out_end - fade_out_start).seconds();
+               }
+               if (time > fade_out_end) {
+                       fade_factor = 0;
+               }
+       }
+
        return fade_factor;
 }