Don't search for empty font names to avoid random fonts being chosen.
[dcpomatic.git] / src / lib / string_text_file_content.cc
index 69d2a67560e35a4f774a39f689ec01e96c8458a1..934144fa411d6d6fdd7ddfe1c4158501b2e5e948 100644 (file)
 */
 
 
-#include "string_text_file_content.h"
-#include "util.h"
-#include "string_text_file.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 "util.h"
 #include <dcp/raw_convert.h>
+#include <fontconfig/fontconfig.h>
 #include <libxml++/libxml++.h>
 #include <iostream>
 
@@ -34,6 +36,7 @@
 
 
 using std::cout;
+using std::list;
 using std::make_shared;
 using std::shared_ptr;
 using std::string;
@@ -49,11 +52,11 @@ StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
 }
 
 
-StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version)
+StringTextFileContent::StringTextFileContent (cxml::ConstNodePtr node, int version, list<string>& notes)
        : Content (node)
        , _length (node->number_child<ContentTime::Type>("Length"))
 {
-       text = TextContent::from_xml (this, node, version);
+       text = TextContent::from_xml (this, node, version, notes);
 }
 
 
@@ -61,14 +64,34 @@ void
 StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
 {
        Content::examine (film, job);
-       StringTextFile s (shared_from_this());
+       StringTextFile file (shared_from_this());
 
        /* Default to turning these subtitles on */
        only_text()->set_use (true);
 
+       std::set<string> names;
+       for (auto const& subtitle: file.subtitles()) {
+               for (auto const& line: subtitle.lines) {
+                       for (auto const& block: line.blocks) {
+                               names.insert(block.font.get_value_or(""));
+                       }
+               }
+       }
+
+       for (auto name: names) {
+               optional<boost::filesystem::path> path;
+               if (!name.empty()) {
+                       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);
-       _length = s.length ();
-       only_text()->add_font (make_shared<Font>(TEXT_FONT_ID));
+       _length = file.length();
 }