WIP: partially restore PlayerVideo transfer over network.
[dcpomatic.git] / src / lib / font.cc
index 309f3d1eb3a6eab77673d75b5d58802b9c376a6e..c817d361018ce9c9bfc51068a4846e4ce06c284e 100644 (file)
 
 #include "font.h"
 #include "dcpomatic_assert.h"
+#include "dcpomatic_socket.h"
+#include <dcp/data.h>
+#include <dcp/raw_convert.h>
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
 
 using std::string;
-
-static char const * names[] = {
-       "Normal",
-       "Italic",
-       "Bold"
-};
+using boost::shared_ptr;
+using dcp::raw_convert;
+using namespace dcpomatic;
 
 Font::Font (cxml::NodePtr node)
        : _id (node->string_child ("Id"))
 {
-       DCPOMATIC_ASSERT (FontFiles::VARIANTS == 3);
-
-       BOOST_FOREACH (cxml::NodePtr i, node->node_children ("File")) {
+       BOOST_FOREACH (cxml::NodePtr i, node->node_children("File")) {
                string variant = i->optional_string_attribute("Variant").get_value_or ("Normal");
-               for (int j = 0; j < FontFiles::VARIANTS; ++j) {
-                       if (variant == names[j]) {
-                               _files.set (static_cast<FontFiles::Variant>(j), i->content());
-                       }
+               if (variant == "Normal") {
+                       _file = i->content();
                }
        }
 }
 
 void
-Font::as_xml (xmlpp::Node* node)
+Font::as_xml (xmlpp::Node* node) const
 {
-       DCPOMATIC_ASSERT (FontFiles::VARIANTS == 3);
+       node->add_child("Id")->add_child_text (_id);
+       if (_file) {
+               node->add_child("File")->add_child_text(_file->string());
+       }
+}
 
+/** Add things to an XML node to describe this font for transfer across
+ *  a network to another machine.  The companion method send_binary() will be called
+ *  to send binary parts.
+ */
+void
+Font::transfer_xml (xmlpp::Node* node) const
+{
        node->add_child("Id")->add_child_text (_id);
-       for (int i = 0; i < FontFiles::VARIANTS; ++i) {
-               if (_files.get(static_cast<FontFiles::Variant>(i))) {
-                       xmlpp::Element* e = node->add_child ("File");
-                       e->set_attribute ("Variant", names[i]);
-                       e->add_child_text (_files.get(static_cast<FontFiles::Variant>(i)).get().string ());
-               }
+       if (_file) {
+               node->add_child("FileLength")->add_child_text(raw_convert<string>(boost::filesystem::file_size(*_file)));
        }
 }
 
+void
+Font::transfer_binary (shared_ptr<Socket> socket) const
+{
+       if (_file) {
+               dcp::Data data (*_file);
+               socket->write (data.data().get(), data.size());
+       }
+}
 
 bool
-operator== (Font const & a, Font const & b)
+dcpomatic::operator== (Font const & a, Font const & b)
 {
        if (a.id() != b.id()) {
                return false;
        }
 
-       for (int i = 0; i < FontFiles::VARIANTS; ++i) {
-               if (a.file(static_cast<FontFiles::Variant>(i)) != b.file(static_cast<FontFiles::Variant>(i))) {
-                       return false;
-               }
-       }
-
-       return true;
+       return a.file() == b.file();
 }
 
 bool
-operator!= (Font const & a, Font const & b)
+dcpomatic::operator!= (Font const & a, Font const & b)
 {
        return !(a == b);
 }