Basic load base font when specified in a FontDescriptor. basic load of TrueType font (FontFile2)

Review URL: https://codereview.chromium.org/18059003

git-svn-id: http://skia.googlecode.com/svn/trunk@9788 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/PdfViewer/SkPdfBasics.h b/experimental/PdfViewer/SkPdfBasics.h
index 27e21f1..5b37820 100644
--- a/experimental/PdfViewer/SkPdfBasics.h
+++ b/experimental/PdfViewer/SkPdfBasics.h
@@ -11,7 +11,7 @@
 #include <cstdio>
 #include <stack>
 
-//#define PDF_TRACE
+#define PDF_TRACE
 //#define PDF_TRACE_DIFF_IN_PNG
 //#define PDF_DEBUG_NO_CLIPING
 //#define PDF_DEBUG_NO_PAGE_CLIPING
diff --git a/experimental/PdfViewer/SkPdfFont.cpp b/experimental/PdfViewer/SkPdfFont.cpp
index 4fac286..42c8056 100644
--- a/experimental/PdfViewer/SkPdfFont.cpp
+++ b/experimental/PdfViewer/SkPdfFont.cpp
@@ -1,6 +1,9 @@
 #include "SkPdfFont.h"
 #include "SkPdfParser.h"
 
+#include "SkStream.h"
+#include "SkTypeface.h"
+
 std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts() {
     static std::map<std::string, SkPdfStandardFontEntry> gPdfStandardFonts;
 
@@ -145,18 +148,73 @@
     return typeface;
 }
 
+static SkPdfFont* fontFromFontDescriptor(SkPdfFontDescriptorDictionary* fd) {
+    // Only one, at most be available
+    if (fd->has_FontFile()) {
+
+    } else if (fd->has_FontFile2()) {
+        SkPdfStream* pdfStream = fd->FontFile2();
+
+        if (!pdfStream->podofo()->GetStream()) {
+            // TODO(edisonn): report warning to be used in testing.
+            return NULL;
+        }
+
+        char* uncompressedStream = NULL;
+        pdf_long uncompressedStreamLength = 0;
+
+        // TODO(edisonn): get rid of try/catch exceptions! We should not throw on user data!
+        try {
+            pdfStream->podofo()->GetStream()->GetFilteredCopy(&uncompressedStream, &uncompressedStreamLength);
+        } catch (PdfError& e) {
+            // TODO(edisonn): report warning to be used in testing.
+            return NULL;
+        }
+        SkMemoryStream* skStream = new SkMemoryStream(uncompressedStream, uncompressedStreamLength);
+        SkTypeface* face = SkTypeface::CreateFromStream(skStream);
+
+        if (face == NULL) {
+            // TODO(edisonn): report warning to be used in testing.
+            return NULL;
+        }
+
+        face->ref();
+
+        return new SkPdfStandardFont(face);
+    } if (fd->has_FontFile3()) {
+
+    } else {
+
+    }
+
+    return NULL;
+}
+
 SkPdfFont* SkPdfFontFromName(SkPdfObject* obj, const char* fontName) {
     SkTypeface* typeface = SkTypefaceFromPdfStandardFont(fontName, false, false);
     if (typeface != NULL) {
         return new SkPdfStandardFont(typeface);
     }
-//        SkPdfObject* font = obtainFont(pdfContext, fontName);
-//        if (!font->asFontDictionary()) {
-//            return NULL;
-//        }
-//        SkPdfFont::fontFromPdfDictionary(font->asDictionary());
-//    }
-    // TODO(edisonn): deal with inherited fonts, load from parent objects
+
+    // TODO(edisonn): perf - make a map
+    for (int i = 0 ; i < obj->doc()->GetObjects().GetSize(); i++) {
+        PdfVecObjects& objects = (PdfVecObjects&)obj->doc()->GetObjects();
+        const PdfObject* podofoFont = objects[i];
+        SkPdfFontDescriptorDictionary* fd = NULL;
+        if (mapFontDescriptorDictionary(*obj->doc(), *podofoFont, &fd)) {
+            if (fd->has_FontName() && fd->FontName() == fontName) {
+                SkPdfFont* font = fontFromFontDescriptor(fd);
+                if (font) {
+                    return font;
+                } else {
+                    // failed to load font descriptor
+                    break;
+                }
+            }
+        }
+    }
+
+    // TODO(edisonn): warning/report issue
     return SkPdfFont::Default();
 }
 
diff --git a/experimental/PdfViewer/autogen/SkPdfPodofoMapper_autogen.cpp b/experimental/PdfViewer/autogen/SkPdfPodofoMapper_autogen.cpp
index def2046..e89a635 100644
--- a/experimental/PdfViewer/autogen/SkPdfPodofoMapper_autogen.cpp
+++ b/experimental/PdfViewer/autogen/SkPdfPodofoMapper_autogen.cpp
@@ -3476,6 +3476,10 @@
 }
 
 bool isFontDescriptorDictionary(const PdfMemDocument& podofoDoc, const PdfObject& podofoObj) {
+  std::string Type;
+  if (!NameFromDictionary(&podofoDoc, podofoObj.GetDictionary(), "Type", "", &Type)) return false;
+  if ((Type != "FontDescriptor")) return false;
+
   return true;
 }
 
diff --git a/experimental/PdfViewer/autogen/pdfspec_autogen.py b/experimental/PdfViewer/autogen/pdfspec_autogen.py
index f43c848..c5a419d 100644
--- a/experimental/PdfViewer/autogen/pdfspec_autogen.py
+++ b/experimental/PdfViewer/autogen/pdfspec_autogen.py
@@ -2050,6 +2050,7 @@
           .name('Type')\
           .type('name')\
           .comment('(Required) The type of PDF object that this dictionary describes; must be\nFontDescriptor for a font descriptor.')\
+          .must([datatypes.PdfName('FontDescriptor')])\
           .done().done()\
       .required('NULL')\
           .field('FontName')\
diff --git a/experimental/PdfViewer/spec2def.py b/experimental/PdfViewer/spec2def.py
index dc78223..bbb5830 100644
--- a/experimental/PdfViewer/spec2def.py
+++ b/experimental/PdfViewer/spec2def.py
@@ -108,7 +108,7 @@
 'TABLE 5.13': ['CIDFontDictionary', 'Entries in a CIDFont dictionary', 'FontDictionary', {'Subtype': '[datatypes.PdfName(\'CIDFontType0\'), datatypes.PdfName(\'CIDFontType2\')]'}],
 'TABLE 5.16': ['CMapDictionary', 'Additional entries in a CMap dictionary'],
 'TABLE 5.17': ['Type0FontDictionary', 'Entries in a Type 0 font dictionary', 'FontDictionary', {'Subtype': '[datatypes.PdfName(\'Type0\')]'}],
-'TABLE 5.18': ['FontDescriptorDictionary', 'Entries common to all font descriptors'],
+'TABLE 5.18': ['FontDescriptorDictionary', 'Entries common to all font descriptors', '', {'Type': '[datatypes.PdfName(\'FontDescriptor\')]'}],
 'TABLE 5.20': ['CIDFontDescriptorDictionary', 'Additional font descriptor entries for CIDFonts'],
 'TABLE 5.23': ['EmbeddedFontStreamDictionary', 'Additional entries in an embedded font stream dictionary'],
 'TABLE 6.3': ['Type1HalftoneDictionary', 'Entries in a type 1 halftone dictionary'],