Remove in-place translations support.
[dcpomatic.git] / src / lib / string_text_file_content.cc
index 2698dbb6d733fad73da91e07cf1d4cbfed301cb0..d8c195be77af1fe53ada9e66830d776ad4810826 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014-2018 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#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>
 
+
 #include "i18n.h"
 
-using std::string;
+
 using std::cout;
-using boost::shared_ptr;
+using std::list;
+using std::make_shared;
+using std::shared_ptr;
+using std::string;
+using boost::optional;
 using dcp::raw_convert;
+using namespace dcpomatic;
+
 
-StringTextFileContent::StringTextFileContent (shared_ptr<const Film> film, boost::filesystem::path path)
-       : Content (film, path)
+StringTextFileContent::StringTextFileContent (boost::filesystem::path path)
+       : Content (path)
 {
-       text.push_back (shared_ptr<TextContent> (new TextContent (this, TEXT_OPEN_SUBTITLE)));
+       text.push_back (make_shared<TextContent>(this, TextType::OPEN_SUBTITLE, TextType::UNKNOWN));
 }
 
-StringTextFileContent::StringTextFileContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
-       : Content (film, node)
-       , _length (node->number_child<ContentTime::Type> ("Length"))
+
+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, notes);
+}
+
+
+static
+std::set<string>
+font_names(StringTextFile const& string_text_file)
 {
-       text = TextContent::from_xml (this, node, version);
+       std::set<string> names;
+
+       for (auto const& subtitle: string_text_file.subtitles()) {
+               for (auto const& line: subtitle.lines) {
+                       for (auto const& block: line.blocks) {
+                               names.insert(block.font.get_value_or(""));
+                       }
+               }
+       }
+
+       return names;
 }
 
+
 void
-StringTextFileContent::examine (boost::shared_ptr<Job> job)
+StringTextFileContent::examine (shared_ptr<const Film> film, shared_ptr<Job> job)
 {
-       Content::examine (job);
-       StringTextFile s (shared_from_this ());
+       Content::examine (film, job);
+       StringTextFile file (shared_from_this());
+
+       only_text()->clear_fonts();
 
        /* Default to turning these subtitles on */
        only_text()->set_use (true);
 
+       for (auto name: font_names(file)) {
+               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 (shared_ptr<Font> (new Font (TEXT_FONT_ID)));
+       _length = file.length();
 }
 
+
 string
 StringTextFileContent::summary () const
 {
        return path_summary() + " " + _("[subtitles]");
 }
 
+
 string
 StringTextFileContent::technical_summary () const
 {
        return Content::technical_summary() + " - " + _("Text subtitles");
+
 }
 
+
 void
 StringTextFileContent::as_xml (xmlpp::Node* node, bool with_paths) const
 {
-       node->add_child("Type")->add_child_text ("TextSubtitle");
+       node->add_child("Type")->add_child_text("TextSubtitle");
        Content::as_xml (node, with_paths);
 
        if (only_text()) {
-               only_text()->as_xml (node);
+               only_text()->as_xml(node);
        }
 
-       node->add_child("Length")->add_child_text (raw_convert<string> (_length.get ()));
+       node->add_child("Length")->add_child_text(raw_convert<string>(_length.get ()));
 }
 
+
 DCPTime
-StringTextFileContent::full_length () const
+StringTextFileContent::full_length (shared_ptr<const Film> film) const
 {
-       FrameRateChange const frc (active_video_frame_rate(), film()->video_frame_rate ());
+       FrameRateChange const frc (film, shared_from_this());
        return DCPTime (_length, frc);
 }
+
+
+DCPTime
+StringTextFileContent::approximate_length () const
+{
+       return DCPTime (_length, FrameRateChange());
+}
+
+
+string
+StringTextFileContent::identifier () const
+{
+       auto s = Content::identifier ();
+       s += "_" + only_text()->identifier();
+       return s;
+}
+
+
+/** In 5a820bb8fae34591be5ac6d19a73461b9dab532a there were some changes to subtitle font management.
+ *
+ *  With StringTextFileContent we used to write a <Font> tag to the metadata with the id "font".  Users
+ *  could then set a font file that content should use, and (with some luck) it would end up in the DCP
+ *  that way.
+ *
+ *  After the changes we write a <Font> tag for every different font "id" (i.e. name) found in the source
+ *  file (including a <Font> with id "" in the .srt case where there are no font names).
+ *
+ *  However, this meant that making DCPs from old projects would fail, as the new code would see a font name
+ *  in the source, then lookup a Font object for it from the Content, and fail in doing so (since the content
+ *  only contains a font called "font").
+ *
+ *  To put it another way: after the changes, the code expects that any font ID (i.e. name) used in some content
+ *  will have a <Font> in the metadata and so a Font object in the TextContent.  Without that, making DCPs fails.
+ *
+ *  To work around this problem, this check_font_ids() is called for all subtitle content written by DoM versions
+ *  before 2.16.14.  We find all the font IDs in the content and map them all to the "legacy" font name (if there
+ *  is one).  This is more-or-less a re-examine()-ation, except that we try to preserve any settings that
+ *  the user had previously set up.
+ *
+ *  See #2271.
+ */
+void
+StringTextFileContent::check_font_ids()
+{
+       StringTextFile file (shared_from_this());
+       auto names = font_names(file);
+
+       auto content = only_text();
+       optional<boost::filesystem::path> legacy_font_file;
+       if (auto legacy_font = content->get_font("font")) {
+               legacy_font_file = legacy_font->file();
+       }
+
+       for (auto name: names) {
+               if (!content->get_font(name)) {
+                       if (legacy_font_file) {
+                               content->add_font(make_shared<Font>(name, *legacy_font_file));
+                       } else {
+                               content->add_font(make_shared<Font>(name));
+                       }
+               }
+       }
+}
+