ReadFileIntoString() modified to return OK when the file is empty
[asdcplib.git] / src / ST2052_TextParser.cpp
index b00ebe6a9214df189a920baa8fd479055f03209e..305f46ff836b882cda0b3ca7f67400a27b4fe34e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2013-2015, John Hurst
+Copyright (c) 2013-2016, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -29,7 +29,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     \brief   AS-DCP library, PCM essence reader and writer implementation
 */
 
-
 #include "AS_02_internal.h"
 #include "KM_xml.h"
 #include <openssl/sha.h>
@@ -45,22 +44,32 @@ const char* c_tt_namespace_name = "http://www.smpte-ra.org/schemas/2052-1/2010/s
 //------------------------------------------------------------------------------------------
 
 //
+int const NS_ID_LENGTH = 16;
+
 //
-static byte_t s_id_prefix[16] = {
+static byte_t s_png_id_prefix[NS_ID_LENGTH] = {
   // RFC 4122 type 5
-  // 2067-2 5.4.5
+  // 2067-2 5.4.5 / RFC4122 Appendix C
   0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
   0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
 };
 
 //
-void
-gen_png_name_id(const std::string& image_name, UUID& asset_id)
+static byte_t s_font_id_prefix[NS_ID_LENGTH] = {
+  // RFC 4122 type 5
+  // 2067-2 5.4.6
+  0xb6, 0xcc, 0x57, 0xa0, 0x87, 0xe7, 0x4e, 0x75,
+  0xb1, 0xc3, 0x33, 0x59, 0xf3, 0xae, 0x88, 0x17
+};
+
+//
+static Kumu::UUID
+create_4122_type5_id(const std::string& subject_name, const byte_t* ns_id)
 {
   SHA_CTX ctx;
   SHA1_Init(&ctx);
-  SHA1_Update(&ctx, s_id_prefix, 16);
-  SHA1_Update(&ctx, (byte_t*)image_name.c_str(), image_name.length());
+  SHA1_Update(&ctx, ns_id, NS_ID_LENGTH);
+  SHA1_Update(&ctx, (byte_t*)subject_name.c_str(), subject_name.size());
 
   const ui32_t sha_len = 20;
   byte_t bin_buf[sha_len];
@@ -73,9 +82,42 @@ gen_png_name_id(const std::string& image_name, UUID& asset_id)
   buf[6] |= 0x50; // set UUID version 'digest'
   buf[8] &= 0x3f; // clear bits 6&7
   buf[8] |= 0x80; // set bit 7
-  asset_id.Set(buf);
+  return Kumu::UUID(buf);
 }
 
+//
+Kumu::UUID
+AS_02::TimedText::CreatePNGNameId(const std::string& image_name)
+{
+  return create_4122_type5_id(image_name, s_png_id_prefix);
+}
+
+//
+Kumu::UUID
+AS_02::TimedText::CreateFontNameId(const std::string& font_name)
+{
+  return create_4122_type5_id(font_name, s_font_id_prefix);
+}
+
+//
+static Kumu::Mutex sg_default_font_family_list_lock;
+static std::set<std::string> sg_default_font_family_list;
+
+static void
+setup_default_font_family_list()
+{
+  AutoMutex l(sg_default_font_family_list_lock);
+  sg_default_font_family_list.insert("default");
+  sg_default_font_family_list.insert("monospace");
+  sg_default_font_family_list.insert("sansSerif");
+  sg_default_font_family_list.insert("serif");
+  sg_default_font_family_list.insert("monospaceSansSerif");
+  sg_default_font_family_list.insert("monospaceSerif");
+  sg_default_font_family_list.insert("proportionalSansSerif");
+  sg_default_font_family_list.insert("proportionalSerif");
+}
+
+
 //------------------------------------------------------------------------------------------
 
 
@@ -101,7 +143,9 @@ AS_02::TimedText::Type5UUIDFilenameResolver::OpenRead(const std::string& dirname
       abs_dirname = ".";
     }
 
-  if ( KM_SUCCESS(dir_reader.Open(abs_dirname.c_str())) )
+  Result_t result = dir_reader.Open(abs_dirname);
+
+  if ( KM_SUCCESS(result) )
     {
       while ( KM_SUCCESS(dir_reader.GetNext(next_item, ft)) )
         {
@@ -111,26 +155,35 @@ AS_02::TimedText::Type5UUIDFilenameResolver::OpenRead(const std::string& dirname
          if ( ft == DET_FILE )
            {
              FileReader reader;
-             Result_t result = reader.OpenRead(tmp_path);
+             Result_t read_result = reader.OpenRead(tmp_path);
 
-             if ( KM_SUCCESS(result) )
+             if ( KM_SUCCESS(read_result) )
                {
-                 result = reader.Read(read_buffer, 16);
+                 read_result = reader.Read(read_buffer, 16);
                }
 
-             if ( KM_SUCCESS(result) )
+             if ( KM_SUCCESS(read_result) )
                {
                  // is it PNG?
                  if ( memcmp(read_buffer, PNGMagic, sizeof(PNGMagic)) == 0 )
                    {
-                     UUID asset_id;
-                     gen_png_name_id(next_item, asset_id);
+                     UUID asset_id = CreatePNGNameId(PathBasename(next_item));
+                     m_ResourceMap.insert(ResourceMap::value_type(asset_id, next_item));
+                   }
+                 // is it a font?
+                 else if ( memcmp(read_buffer, OpenTypeMagic, sizeof(OpenTypeMagic)) == 0
+                           || memcmp(read_buffer, TrueTypeMagic, sizeof(TrueTypeMagic)) == 0 )
+                   {
+                     std::string font_root_name = PathSetExtension(next_item, "");
+                     UUID asset_id = CreateFontNameId(PathBasename(font_root_name));
                      m_ResourceMap.insert(ResourceMap::value_type(asset_id, next_item));
                    }
                }
            }
        }
     }
+
+  return result;
 }
 
 //
@@ -144,6 +197,7 @@ AS_02::TimedText::Type5UUIDFilenameResolver::ResolveRID(const byte_t* uuid, ASDC
 
   if ( i == m_ResourceMap.end() )
     {
+      DefaultLogSink().Debug("Missing timed-text resource \"%s\"\n", tmp_id.EncodeHex(buf, 64));
       return RESULT_NOT_FOUND;
     }
 
@@ -235,93 +289,123 @@ AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead(const std::string&
   return OpenRead();
 }
 
-//
-template <class VisitorType>
-bool
-apply_visitor(const XMLElement& element, VisitorType& visitor)
-{
-  const ElementList& l = element.GetChildren();
-  ElementList::const_iterator i;
 
-  for ( i = l.begin(); i != l.end(); ++i )
-    {
-      if ( ! visitor.Element(**i) )
-       {
-         return false;
-       }
 
-      if ( ! apply_visitor(**i, visitor) )
-       {
-         return false;
-       }
-    }
-
-  return true;
-}
-
-//
-class AttributeVisitor
-{
-  std::string attr_name;
-
-public:
-  AttributeVisitor(const std::string& n) : attr_name(n) {}
-  std::set<std::string> value_list;
-
-  bool Element(const XMLElement& e)
-  {
-    const AttributeList& l = e.GetAttributes();
-    AttributeList::const_iterator i;
-    for ( i = l.begin(); i != l.end(); ++i )
-      {
-       if ( i->name == attr_name )
-         {
-           value_list.insert(i->value);
-         }
-      }
-
-    return true;
-  }
-};
+std::string const IMSC1_imageProfile = "http://www.w3.org/ns/ttml/profile/imsc1/image";
+std::string const IMSC1_textProfile = "http://www.w3.org/ns/ttml/profile/imsc1/text";
 
 //
 Result_t
 AS_02::TimedText::ST2052_TextParser::h__TextParser::OpenRead()
 {
+  setup_default_font_family_list();
+
   if ( ! m_Root.ParseString(m_XMLDoc.c_str()) )
     {
+      DefaultLogSink(). Error("ST 2052-1 document is not well-formed.\n");
       return RESULT_FORMAT;
     }
 
   m_TDesc.EncodingName = "UTF-8"; // the XML parser demands UTF-8
   m_TDesc.ResourceList.clear();
   m_TDesc.ContainerDuration = 0;
-  const XMLNamespace* ns = m_Root.Namespace();
+  std::set<std::string>::const_iterator i;
 
-  if ( ns == 0 )
+  // Attempt to set the profile from <conformsToStandard>
+  if ( m_TDesc.NamespaceName.empty() )
     {
-      DefaultLogSink(). Warn("Document has no namespace name, assuming %s\n", c_tt_namespace_name);
-      m_TDesc.NamespaceName = c_tt_namespace_name;
+      ElementVisitor conforms_visitor("conformsToStandard");
+      apply_visitor(m_Root, conforms_visitor);
+
+      for ( i = conforms_visitor.value_list.begin(); i != conforms_visitor.value_list.end(); ++i )
+       {
+         if ( *i == IMSC1_imageProfile || *i == IMSC1_textProfile )
+           {
+             m_TDesc.NamespaceName = *i;
+             break;
+           }
+       }
     }
-  else
+
+  // Attempt to set the profile from the use of attribute "profile"
+  if ( m_TDesc.NamespaceName.empty() )
     {
-      m_TDesc.NamespaceName = ns->Name();
+      AttributeVisitor profile_visitor("profile");
+      apply_visitor(m_Root, profile_visitor);
+
+      for ( i = profile_visitor.value_list.begin(); i != profile_visitor.value_list.end(); ++i )
+       {
+         if ( *i == IMSC1_imageProfile || *i == IMSC1_textProfile )
+           {
+             m_TDesc.NamespaceName = *i;
+             break;
+           }
+       }
     }
 
+  // Find image resources for later packaging as GS partitions.
+  // Attempt to set the profile; infer from use of images.
   AttributeVisitor png_visitor("backgroundImage");
   apply_visitor(m_Root, png_visitor);
-  std::set<std::string>::const_iterator i;
 
   for ( i = png_visitor.value_list.begin(); i != png_visitor.value_list.end(); ++i )
     {
-      UUID asset_id;
-      gen_png_name_id(*i, asset_id);
-      TimedTextResourceDescriptor TmpResource;
-      memcpy(TmpResource.ResourceID, asset_id.Value(), UUIDlen);
-      TmpResource.Type = ASDCP::TimedText::MT_PNG;
-      m_TDesc.ResourceList.push_back(TmpResource);
-      m_ResourceTypes.insert(ResourceTypeMap_t::value_type(UUID(TmpResource.ResourceID), ASDCP::TimedText::MT_PNG));
+      UUID asset_id = CreatePNGNameId(PathBasename(*i));
+      TimedTextResourceDescriptor png_resource;
+      memcpy(png_resource.ResourceID, asset_id.Value(), UUIDlen);
+      png_resource.Type = ASDCP::TimedText::MT_PNG;
+      m_TDesc.ResourceList.push_back(png_resource);
+      m_ResourceTypes.insert(ResourceTypeMap_t::value_type(UUID(png_resource.ResourceID),
+                                                          ASDCP::TimedText::MT_PNG));
+
+      if ( m_TDesc.NamespaceName.empty() )
+       {
+         m_TDesc.NamespaceName = IMSC1_imageProfile;
+       }
+    }
+
+  // If images are present and profile is "text" make sure to say something.
+  if ( ! m_ResourceTypes.empty() && m_TDesc.NamespaceName == IMSC1_textProfile )
+    {
+      DefaultLogSink().Warn("Unexpected IMSC-1 text profile; document contains images.\n ");
+    }
+  
+  // If all else fails set the profile to "text".
+  if ( m_TDesc.NamespaceName.empty() )
+    {
+      DefaultLogSink().Warn("Using default IMSC-1 text profile.\n ");
+      m_TDesc.NamespaceName = IMSC1_textProfile;
+    }
+
+  // Find font resources for later packaging as GS partitions.
+  AttributeVisitor font_visitor("fontFamily");
+  apply_visitor(m_Root, font_visitor);
+  char buf[64];
+
+  for ( i = font_visitor.value_list.begin(); i != font_visitor.value_list.end(); ++i )
+    {
+      UUID font_id = CreateFontNameId(PathBasename(*i));
+
+      if ( PathIsFile(font_id.EncodeHex(buf, 64))
+          || PathIsFile(*i+".ttf")
+          || PathIsFile(*i+".otf") )
+       {
+         TimedTextResourceDescriptor font_resource;
+         memcpy(font_resource.ResourceID, font_id.Value(), UUIDlen);
+         font_resource.Type = ASDCP::TimedText::MT_OPENTYPE;
+         m_TDesc.ResourceList.push_back(font_resource);
+         m_ResourceTypes.insert(ResourceTypeMap_t::value_type(UUID(font_resource.ResourceID),
+                                                              ASDCP::TimedText::MT_OPENTYPE));
+       }
+      else
+       {
+         AutoMutex l(sg_default_font_family_list_lock);
+         if ( sg_default_font_family_list.find(*i) == sg_default_font_family_list.end() )
+           {
+             DefaultLogSink(). Error("Unable to locate external font resource \"%s\".\n", i->c_str());
+             return RESULT_FORMAT;
+           }
+       }
     }
 
   return RESULT_OK;