Remove unused CFFL_Notify.
[pdfium.git] / fpdfsdk / src / formfiller / FFL_CBA_Fontmap.cpp
index 59617ac..3a8853c 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../include/formfiller/FormFiller.h"\r
-#include "../../include/formfiller/FFL_CBA_Fontmap.h"\r
-\r
-CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot, IFX_SystemHandler* pSystemHandler) :\r
-       CPWL_FontMap(pSystemHandler),\r
-       m_pDocument(NULL),\r
-       m_pAnnotDict(NULL),\r
-       m_pDefaultFont(NULL),\r
-       m_sAPType("N")\r
-{\r
-       ASSERT(pAnnot != NULL);\r
-\r
-       CPDF_Page* pPage = pAnnot->GetPDFPage();\r
-\r
-       m_pDocument = pPage->m_pDocument;\r
-       m_pAnnotDict = pAnnot->GetPDFAnnot()->m_pAnnotDict;\r
-}\r
-\r
-CBA_FontMap::CBA_FontMap(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict, \r
-                                                IFX_SystemHandler* pSystemHandler) :\r
-       CPWL_FontMap(pSystemHandler),\r
-       m_pDocument(pDocument),\r
-       m_pAnnotDict(pAnnotDict),\r
-       m_pDefaultFont(NULL),\r
-       m_sAPType("N")\r
-{\r
-}\r
-\r
-CBA_FontMap::~CBA_FontMap()\r
-{\r
-}\r
-\r
-void CBA_FontMap::Reset()\r
-{\r
-       Empty();\r
-       m_pDefaultFont = NULL;\r
-       m_sDefaultFontName = "";\r
-}\r
-\r
-void CBA_FontMap::Initial(FX_LPCSTR fontname)\r
-{\r
-       FX_INT32 nCharset = DEFAULT_CHARSET;\r
-\r
-       if (!m_pDefaultFont)\r
-       {\r
-               m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);       \r
-               if (m_pDefaultFont)\r
-               {\r
-                       if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())\r
-                               nCharset = pSubstFont->m_Charset;\r
-                       else\r
-                       {\r
-                               if (m_sDefaultFontName == "Wingdings" || m_sDefaultFontName == "Wingdings2" ||\r
-                                       m_sDefaultFontName == "Wingdings3" || m_sDefaultFontName == "Webdings")\r
-                                               nCharset = SYMBOL_CHARSET;\r
-                               else\r
-                                       nCharset = ANSI_CHARSET;\r
-                       }\r
-                       AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);\r
-                       AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);\r
-               }\r
-       }\r
-\r
-       if (nCharset != ANSI_CHARSET)\r
-               CPWL_FontMap::Initial(fontname);\r
-}\r
-\r
-void CBA_FontMap::SetDefaultFont(CPDF_Font * pFont, const CFX_ByteString & sFontName)\r
-{\r
-       ASSERT(pFont != NULL);\r
-\r
-       if (m_pDefaultFont) return;\r
-\r
-       m_pDefaultFont = pFont;\r
-       m_sDefaultFontName = sFontName;\r
-\r
-//     if (m_sDefaultFontName.IsEmpty())\r
-//             m_sDefaultFontName = pFont->GetFontTypeName();\r
-\r
-       FX_INT32 nCharset = DEFAULT_CHARSET;\r
-       if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())\r
-               nCharset = pSubstFont->m_Charset;\r
-       AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);\r
-}\r
-\r
-CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, FX_INT32 nCharset)\r
-{\r
-       ASSERT(m_pAnnotDict != NULL);\r
-\r
-       if (m_pAnnotDict->GetString("Subtype") == "Widget")\r
-       {\r
-               CPDF_Document* pDocument = GetDocument();\r
-               ASSERT(pDocument != NULL);\r
-\r
-               CPDF_Dictionary * pRootDict = pDocument->GetRoot();\r
-               if (!pRootDict) return NULL;\r
-\r
-               CPDF_Dictionary* pAcroFormDict = pRootDict->GetDict("AcroForm");\r
-               if (!pAcroFormDict) return NULL;\r
-\r
-               CPDF_Dictionary * pDRDict = pAcroFormDict->GetDict("DR");\r
-               if (!pDRDict) return NULL;\r
-\r
-               return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);\r
-       }\r
-\r
-       return NULL;\r
-}\r
-\r
-CPDF_Document* CBA_FontMap::GetDocument()\r
-{\r
-       return m_pDocument;\r
-}\r
-\r
-CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict, CFX_ByteString& sFontAlias, \r
-                                                                                                       FX_INT32 nCharset)\r
-{\r
-       if (!pResDict) return NULL;\r
-\r
-       CPDF_Document* pDocument = GetDocument();\r
-       ASSERT(pDocument != NULL);\r
-\r
-       CPDF_Dictionary* pFonts = pResDict->GetDict("Font");\r
-       if (pFonts == NULL) return NULL;\r
-\r
-       CPDF_Font* pFind = NULL;\r
-\r
-       FX_POSITION pos = pFonts->GetStartPos();\r
-       while (pos)\r
-       {\r
-               CPDF_Object* pObj = NULL;\r
-               CFX_ByteString csKey;\r
-               pObj = pFonts->GetNextElement(pos, csKey);\r
-               if (pObj == NULL) continue;\r
-\r
-               CPDF_Object* pDirect = pObj->GetDirect();\r
-               if (pDirect == NULL || pDirect->GetType() != PDFOBJ_DICTIONARY) continue;\r
-\r
-               CPDF_Dictionary* pElement = (CPDF_Dictionary*)pDirect;\r
-               if (pElement->GetString("Type") != "Font") continue;\r
-\r
-               CPDF_Font* pFont = pDocument->LoadFont(pElement);\r
-               if (pFont == NULL) continue;\r
-               const CFX_SubstFont* pSubst = pFont->GetSubstFont();\r
-               if (pSubst == NULL) continue;\r
-               if (pSubst->m_Charset == nCharset)\r
-               {\r
-                       sFontAlias = csKey;\r
-                       pFind = pFont;\r
-               }\r
-       }\r
-       return pFind;\r
-}\r
-\r
-void CBA_FontMap::AddedFont(CPDF_Font* pFont, const CFX_ByteString& sFontAlias)\r
-{\r
-       AddFontToAnnotDict(pFont, sFontAlias);\r
-}\r
-\r
-void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, const CFX_ByteString& sAlias)\r
-{\r
-       if (!pFont)     return;\r
-\r
-       ASSERT(m_pAnnotDict != NULL);\r
-       ASSERT(m_pDocument != NULL);\r
-\r
-       CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDict("AP");\r
-\r
-       if (pAPDict == NULL) \r
-       {\r
-               pAPDict = FX_NEW CPDF_Dictionary;\r
-               m_pAnnotDict->SetAt("AP", pAPDict);\r
-       }\r
-\r
-       //to avoid checkbox and radiobutton\r
-       CPDF_Object* pObject = pAPDict->GetElement(m_sAPType);\r
-       if (pObject && pObject->GetType() == PDFOBJ_DICTIONARY)\r
-               return;\r
-\r
-       CPDF_Stream* pStream = pAPDict->GetStream(m_sAPType);\r
-       if (pStream == NULL) \r
-       {\r
-               pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);\r
-               FX_INT32 objnum = m_pDocument->AddIndirectObject(pStream);\r
-               pAPDict->SetAtReference(m_sAPType, m_pDocument, objnum);\r
-       }\r
-\r
-       CPDF_Dictionary * pStreamDict = pStream->GetDict();\r
-\r
-       if (!pStreamDict)\r
-       {\r
-               pStreamDict = FX_NEW CPDF_Dictionary;\r
-               pStream->InitStream(NULL, 0, pStreamDict);\r
-       }\r
-\r
-       if (pStreamDict)\r
-       {\r
-               CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");\r
-               if (!pStreamResList)\r
-               {\r
-                       pStreamResList = FX_NEW CPDF_Dictionary();\r
-                       pStreamDict->SetAt("Resources", pStreamResList);\r
-               }\r
-\r
-               if (pStreamResList) \r
-               {\r
-                       CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDict("Font");\r
-                       if (!pStreamResFontList) \r
-                       {\r
-                               pStreamResFontList = FX_NEW CPDF_Dictionary;\r
-                               FX_INT32 objnum = m_pDocument->AddIndirectObject(pStreamResFontList);\r
-                               pStreamResList->SetAtReference("Font", m_pDocument, objnum);\r
-                       }\r
-                       if (!pStreamResFontList->KeyExist(sAlias))\r
-                               pStreamResFontList->SetAtReference(sAlias, m_pDocument, pFont->GetFontDict());\r
-               }\r
-       }\r
-}\r
-\r
-CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString &sAlias)\r
-{\r
-       ASSERT(m_pAnnotDict != NULL);\r
-       ASSERT(m_pDocument != NULL);\r
-\r
-       CPDF_Dictionary* pAcroFormDict = NULL;\r
-\r
-       FX_BOOL bWidget = (m_pAnnotDict->GetString("Subtype") == "Widget");\r
-\r
-       if (bWidget)\r
-       {\r
-               if (CPDF_Dictionary * pRootDict = m_pDocument->GetRoot())\r
-                       pAcroFormDict = pRootDict->GetDict("AcroForm");\r
-       }\r
-       \r
-       CFX_ByteString sDA;\r
-       \r
-       sDA = FPDF_GetFieldAttr(m_pAnnotDict, "DA")->GetString();\r
-\r
-       if (bWidget)\r
-       {\r
-               if (sDA.IsEmpty())\r
-               {\r
-                       sDA = FPDF_GetFieldAttr(pAcroFormDict, "DA")->GetString();      \r
-               }\r
-       }\r
-       \r
-       CPDF_Dictionary * pFontDict = NULL;     \r
-\r
-       if (!sDA.IsEmpty())\r
-       {\r
-               CPDF_SimpleParser syntax(sDA);\r
-               syntax.FindTagParam("Tf", 2);\r
-               CFX_ByteString sFontName = syntax.GetWord();\r
-               sAlias = PDF_NameDecode(sFontName).Mid(1);\r
-\r
-               if (CPDF_Dictionary * pDRDict = m_pAnnotDict->GetDict("DR"))\r
-                       if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDict("Font"))\r
-                               pFontDict = pDRFontDict->GetDict(sAlias);\r
-\r
-               if (!pFontDict)\r
-                       if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDict("AP"))\r
-                               if (CPDF_Dictionary* pNormalDict = pAPDict->GetDict("N"))\r
-                                       if (CPDF_Dictionary* pNormalResDict = pNormalDict->GetDict("Resources"))\r
-                                               if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDict("Font"))\r
-                                                       pFontDict = pResFontDict->GetDict(sAlias);\r
-\r
-               if (bWidget)\r
-               {                       \r
-                       if (!pFontDict)\r
-                       {\r
-                               if (pAcroFormDict)\r
-                               {\r
-                                       if (CPDF_Dictionary * pDRDict = pAcroFormDict->GetDict("DR"))\r
-                                               if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDict("Font"))\r
-                                                       pFontDict = pDRFontDict->GetDict(sAlias);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       if (pFontDict)\r
-               return m_pDocument->LoadFont(pFontDict);\r
-       else\r
-               return NULL;\r
-}\r
-\r
-void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType)\r
-{\r
-       m_sAPType = sAPType;\r
-\r
-       Reset();\r
-       Initial();\r
-}\r
-\r
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../include/formfiller/FormFiller.h"
+#include "../../include/formfiller/FFL_CBA_Fontmap.h"
+
+CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot,
+                         IFX_SystemHandler* pSystemHandler)
+    : CPWL_FontMap(pSystemHandler),
+      m_pDocument(NULL),
+      m_pAnnotDict(NULL),
+      m_pDefaultFont(NULL),
+      m_sAPType("N") {
+  ASSERT(pAnnot != NULL);
+
+  CPDF_Page* pPage = pAnnot->GetPDFPage();
+
+  m_pDocument = pPage->m_pDocument;
+  m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict();
+}
+
+CBA_FontMap::CBA_FontMap(CPDF_Document* pDocument,
+                         CPDF_Dictionary* pAnnotDict,
+                         IFX_SystemHandler* pSystemHandler)
+    : CPWL_FontMap(pSystemHandler),
+      m_pDocument(pDocument),
+      m_pAnnotDict(pAnnotDict),
+      m_pDefaultFont(NULL),
+      m_sAPType("N") {}
+
+CBA_FontMap::~CBA_FontMap() {}
+
+void CBA_FontMap::Reset() {
+  Empty();
+  m_pDefaultFont = NULL;
+  m_sDefaultFontName = "";
+}
+
+void CBA_FontMap::Initial(const FX_CHAR* fontname) {
+  int32_t nCharset = DEFAULT_CHARSET;
+
+  if (!m_pDefaultFont) {
+    m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);
+    if (m_pDefaultFont) {
+      if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
+        nCharset = pSubstFont->m_Charset;
+      else {
+        if (m_sDefaultFontName == "Wingdings" ||
+            m_sDefaultFontName == "Wingdings2" ||
+            m_sDefaultFontName == "Wingdings3" ||
+            m_sDefaultFontName == "Webdings")
+          nCharset = SYMBOL_CHARSET;
+        else
+          nCharset = ANSI_CHARSET;
+      }
+      AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
+      AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);
+    }
+  }
+
+  if (nCharset != ANSI_CHARSET)
+    CPWL_FontMap::Initial(fontname);
+}
+
+void CBA_FontMap::SetDefaultFont(CPDF_Font* pFont,
+                                 const CFX_ByteString& sFontName) {
+  ASSERT(pFont != NULL);
+
+  if (m_pDefaultFont)
+    return;
+
+  m_pDefaultFont = pFont;
+  m_sDefaultFontName = sFontName;
+
+  //   if (m_sDefaultFontName.IsEmpty())
+  //           m_sDefaultFontName = pFont->GetFontTypeName();
+
+  int32_t nCharset = DEFAULT_CHARSET;
+  if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
+    nCharset = pSubstFont->m_Charset;
+  AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
+}
+
+CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
+                                            int32_t nCharset) {
+  ASSERT(m_pAnnotDict != NULL);
+
+  if (m_pAnnotDict->GetString("Subtype") == "Widget") {
+    CPDF_Document* pDocument = GetDocument();
+    ASSERT(pDocument != NULL);
+
+    CPDF_Dictionary* pRootDict = pDocument->GetRoot();
+    if (!pRootDict)
+      return NULL;
+
+    CPDF_Dictionary* pAcroFormDict = pRootDict->GetDict("AcroForm");
+    if (!pAcroFormDict)
+      return NULL;
+
+    CPDF_Dictionary* pDRDict = pAcroFormDict->GetDict("DR");
+    if (!pDRDict)
+      return NULL;
+
+    return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
+  }
+
+  return NULL;
+}
+
+CPDF_Document* CBA_FontMap::GetDocument() {
+  return m_pDocument;
+}
+
+CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict,
+                                               CFX_ByteString& sFontAlias,
+                                               int32_t nCharset) {
+  if (!pResDict)
+    return NULL;
+
+  CPDF_Document* pDocument = GetDocument();
+  ASSERT(pDocument != NULL);
+
+  CPDF_Dictionary* pFonts = pResDict->GetDict("Font");
+  if (pFonts == NULL)
+    return NULL;
+
+  CPDF_Font* pFind = NULL;
+
+  FX_POSITION pos = pFonts->GetStartPos();
+  while (pos) {
+    CPDF_Object* pObj = NULL;
+    CFX_ByteString csKey;
+    pObj = pFonts->GetNextElement(pos, csKey);
+    if (pObj == NULL)
+      continue;
+
+    CPDF_Object* pDirect = pObj->GetDirect();
+    if (pDirect == NULL || pDirect->GetType() != PDFOBJ_DICTIONARY)
+      continue;
+
+    CPDF_Dictionary* pElement = (CPDF_Dictionary*)pDirect;
+    if (pElement->GetString("Type") != "Font")
+      continue;
+
+    CPDF_Font* pFont = pDocument->LoadFont(pElement);
+    if (pFont == NULL)
+      continue;
+    const CFX_SubstFont* pSubst = pFont->GetSubstFont();
+    if (pSubst == NULL)
+      continue;
+    if (pSubst->m_Charset == nCharset) {
+      sFontAlias = csKey;
+      pFind = pFont;
+    }
+  }
+  return pFind;
+}
+
+void CBA_FontMap::AddedFont(CPDF_Font* pFont,
+                            const CFX_ByteString& sFontAlias) {
+  AddFontToAnnotDict(pFont, sFontAlias);
+}
+
+void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont,
+                                     const CFX_ByteString& sAlias) {
+  if (!pFont)
+    return;
+
+  ASSERT(m_pAnnotDict != NULL);
+  ASSERT(m_pDocument != NULL);
+
+  CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDict("AP");
+
+  if (pAPDict == NULL) {
+    pAPDict = new CPDF_Dictionary;
+    m_pAnnotDict->SetAt("AP", pAPDict);
+  }
+
+  // to avoid checkbox and radiobutton
+  CPDF_Object* pObject = pAPDict->GetElement(m_sAPType);
+  if (pObject && pObject->GetType() == PDFOBJ_DICTIONARY)
+    return;
+
+  CPDF_Stream* pStream = pAPDict->GetStream(m_sAPType);
+  if (pStream == NULL) {
+    pStream = new CPDF_Stream(NULL, 0, NULL);
+    int32_t objnum = m_pDocument->AddIndirectObject(pStream);
+    pAPDict->SetAtReference(m_sAPType, m_pDocument, objnum);
+  }
+
+  CPDF_Dictionary* pStreamDict = pStream->GetDict();
+
+  if (!pStreamDict) {
+    pStreamDict = new CPDF_Dictionary;
+    pStream->InitStream(NULL, 0, pStreamDict);
+  }
+
+  if (pStreamDict) {
+    CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");
+    if (!pStreamResList) {
+      pStreamResList = new CPDF_Dictionary();
+      pStreamDict->SetAt("Resources", pStreamResList);
+    }
+
+    if (pStreamResList) {
+      CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDict("Font");
+      if (!pStreamResFontList) {
+        pStreamResFontList = new CPDF_Dictionary;
+        int32_t objnum = m_pDocument->AddIndirectObject(pStreamResFontList);
+        pStreamResList->SetAtReference("Font", m_pDocument, objnum);
+      }
+      if (!pStreamResFontList->KeyExist(sAlias))
+        pStreamResFontList->SetAtReference(sAlias, m_pDocument,
+                                           pFont->GetFontDict());
+    }
+  }
+}
+
+CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString& sAlias) {
+  ASSERT(m_pAnnotDict != NULL);
+  ASSERT(m_pDocument != NULL);
+
+  CPDF_Dictionary* pAcroFormDict = NULL;
+
+  FX_BOOL bWidget = (m_pAnnotDict->GetString("Subtype") == "Widget");
+
+  if (bWidget) {
+    if (CPDF_Dictionary* pRootDict = m_pDocument->GetRoot())
+      pAcroFormDict = pRootDict->GetDict("AcroForm");
+  }
+
+  CFX_ByteString sDA;
+  CPDF_Object* pObj;
+  if ((pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA")))
+    sDA = pObj->GetString();
+
+  if (bWidget) {
+    if (sDA.IsEmpty()) {
+      pObj = FPDF_GetFieldAttr(pAcroFormDict, "DA");
+      sDA = pObj ? pObj->GetString() : CFX_ByteString();
+    }
+  }
+
+  CPDF_Dictionary* pFontDict = NULL;
+
+  if (!sDA.IsEmpty()) {
+    CPDF_SimpleParser syntax(sDA);
+    syntax.FindTagParam("Tf", 2);
+    CFX_ByteString sFontName = syntax.GetWord();
+    sAlias = PDF_NameDecode(sFontName).Mid(1);
+
+    if (CPDF_Dictionary* pDRDict = m_pAnnotDict->GetDict("DR"))
+      if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDict("Font"))
+        pFontDict = pDRFontDict->GetDict(sAlias);
+
+    if (!pFontDict)
+      if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDict("AP"))
+        if (CPDF_Dictionary* pNormalDict = pAPDict->GetDict("N"))
+          if (CPDF_Dictionary* pNormalResDict =
+                  pNormalDict->GetDict("Resources"))
+            if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDict("Font"))
+              pFontDict = pResFontDict->GetDict(sAlias);
+
+    if (bWidget) {
+      if (!pFontDict) {
+        if (pAcroFormDict) {
+          if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDict("DR"))
+            if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDict("Font"))
+              pFontDict = pDRFontDict->GetDict(sAlias);
+        }
+      }
+    }
+  }
+
+  return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr;
+}
+
+void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType) {
+  m_sAPType = sAPType;
+
+  Reset();
+  Initial();
+}