Ask fontconfig to scan the system for fonts requested by text subtitle files (#2264).
authorCarl Hetherington <cth@carlh.net>
Thu, 2 Jun 2022 10:20:41 +0000 (12:20 +0200)
committerCarl Hetherington <cth@carlh.net>
Tue, 7 Jun 2022 15:01:06 +0000 (17:01 +0200)
src/lib/font_config.cc
src/lib/font_config.h
src/lib/string_text_file_content.cc

index c7361bc04823e7547a74b5117c5dfdbd732bd252..5c9eebb3fb752f7d2cc4fbc104235d9f71d6bbc7 100644 (file)
@@ -22,6 +22,7 @@
 #include "dcpomatic_assert.h"
 #include "font_config.h"
 #include <fontconfig/fontconfig.h>
 #include "dcpomatic_assert.h"
 #include "font_config.h"
 #include <fontconfig/fontconfig.h>
+#include <boost/filesystem.hpp>
 #include <boost/optional.hpp>
 
 
 #include <boost/optional.hpp>
 
 
@@ -85,6 +86,33 @@ FontConfig::make_font_available(boost::filesystem::path font_file)
 }
 
 
 }
 
 
+optional<boost::filesystem::path>
+FontConfig::system_font_with_name(string name)
+{
+       optional<boost::filesystem::path> path;
+
+       auto pattern = FcNameParse(reinterpret_cast<FcChar8 const*>(name.c_str()));
+       auto object_set = FcObjectSetBuild(FC_FILE, nullptr);
+       auto font_set = FcFontList(_config, pattern, object_set);
+       if (font_set) {
+               for (int i = 0; i < font_set->nfont; ++i) {
+                       auto font = font_set->fonts[i];
+                       FcChar8* file;
+                       if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
+                               path = boost::filesystem::path(reinterpret_cast<char*>(file));
+                               break;
+                       }
+               }
+               FcFontSetDestroy(font_set);
+       }
+
+       FcObjectSetDestroy(object_set);
+       FcPatternDestroy(pattern);
+
+       return path;
+}
+
+
 FontConfig *
 FontConfig::instance()
 {
 FontConfig *
 FontConfig::instance()
 {
index 7527d9127fea21c9bc651dd44c2745186d27da98..6dadbae8ad88ed416e0bc6724aeb6c7a30abe6f4 100644 (file)
@@ -32,6 +32,7 @@ public:
        static FontConfig* instance();
 
        std::string make_font_available(boost::filesystem::path font_file);
        static FontConfig* instance();
 
        std::string make_font_available(boost::filesystem::path font_file);
+       boost::optional<boost::filesystem::path> system_font_with_name(std::string name);
 
 private:
        FontConfig();
 
 private:
        FontConfig();
index 3750b0a5026a70db90b54dfdfe67ea2465fe6a62..eea9362bbc384369b4fc05e0d00f4040724d500b 100644 (file)
 */
 
 
 */
 
 
-#include "string_text_file_content.h"
-#include "util.h"
-#include "string_text_file.h"
 #include "film.h"
 #include "font.h"
 #include "film.h"
 #include "font.h"
+#include "font_config.h"
+#include "string_text_file.h"
+#include "string_text_file_content.h"
 #include "text_content.h"
 #include "text_content.h"
+#include "util.h"
 #include <dcp/raw_convert.h>
 #include <dcp/raw_convert.h>
+#include <fontconfig/fontconfig.h>
 #include <libxml++/libxml++.h>
 #include <iostream>
 
 #include <libxml++/libxml++.h>
 #include <iostream>
 
@@ -76,10 +78,12 @@ StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job
        }
 
        for (auto name: names) {
        }
 
        for (auto name: names) {
-               /* Make a font for each family name that somebody might later
-                * ask about.
-                */
-               only_text()->add_font(make_shared<Font>(name));
+               auto path = FontConfig::instance()->system_font_with_name(name);
+               if (path) {
+                       only_text()->add_font(make_shared<Font>(name, *path));
+               } else {
+                       only_text()->add_font(make_shared<Font>(name));
+               }
        }
 
        boost::mutex::scoped_lock lm (_mutex);
        }
 
        boost::mutex::scoped_lock lm (_mutex);