| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "xfa/fxfa/xfa_fontmgr.h" | 7 #include "xfa/fxfa/xfa_fontmgr.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 1722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1733 }); | 1733 }); |
| 1734 if (pFontInfo < pEnd && pFontInfo->dwFontNameHash == dwCurFontNameHash) | 1734 if (pFontInfo < pEnd && pFontInfo->dwFontNameHash == dwCurFontNameHash) |
| 1735 return pFontInfo; | 1735 return pFontInfo; |
| 1736 return nullptr; | 1736 return nullptr; |
| 1737 } | 1737 } |
| 1738 | 1738 |
| 1739 } // namespace | 1739 } // namespace |
| 1740 | 1740 |
| 1741 CXFA_DefFontMgr::CXFA_DefFontMgr() {} | 1741 CXFA_DefFontMgr::CXFA_DefFontMgr() {} |
| 1742 | 1742 |
| 1743 CXFA_DefFontMgr::~CXFA_DefFontMgr() { | 1743 CXFA_DefFontMgr::~CXFA_DefFontMgr() {} |
| 1744 for (int32_t i = 0; i < m_CacheFonts.GetSize(); i++) | |
| 1745 m_CacheFonts[i]->Release(); | |
| 1746 } | |
| 1747 | 1744 |
| 1748 CFGAS_GEFont* CXFA_DefFontMgr::GetFont(CXFA_FFDoc* hDoc, | 1745 CFX_RetainPtr<CFGAS_GEFont> CXFA_DefFontMgr::GetFont( |
| 1749 const CFX_WideStringC& wsFontFamily, | 1746 CXFA_FFDoc* hDoc, |
| 1750 uint32_t dwFontStyles, | 1747 const CFX_WideStringC& wsFontFamily, |
| 1751 uint16_t wCodePage) { | 1748 uint32_t dwFontStyles, |
| 1749 uint16_t wCodePage) { |
| 1752 CFX_WideString wsFontName(wsFontFamily); | 1750 CFX_WideString wsFontName(wsFontFamily); |
| 1753 CFGAS_FontMgr* pFDEFontMgr = hDoc->GetApp()->GetFDEFontMgr(); | 1751 CFGAS_FontMgr* pFDEFontMgr = hDoc->GetApp()->GetFDEFontMgr(); |
| 1754 CFGAS_GEFont* pFont = | 1752 CFX_RetainPtr<CFGAS_GEFont> pFont = |
| 1755 pFDEFontMgr->LoadFont(wsFontName.c_str(), dwFontStyles, wCodePage); | 1753 pFDEFontMgr->LoadFont(wsFontName.c_str(), dwFontStyles, wCodePage); |
| 1756 if (!pFont) { | 1754 if (!pFont) { |
| 1757 const XFA_FONTINFO* pCurFont = | 1755 const XFA_FONTINFO* pCurFont = |
| 1758 XFA_GetFontINFOByFontName(wsFontName.AsStringC()); | 1756 XFA_GetFontINFOByFontName(wsFontName.AsStringC()); |
| 1759 if (pCurFont && pCurFont->pReplaceFont) { | 1757 if (pCurFont && pCurFont->pReplaceFont) { |
| 1760 uint32_t dwStyle = 0; | 1758 uint32_t dwStyle = 0; |
| 1761 if (dwFontStyles & FX_FONTSTYLE_Bold) { | 1759 if (dwFontStyles & FX_FONTSTYLE_Bold) { |
| 1762 dwStyle |= FX_FONTSTYLE_Bold; | 1760 dwStyle |= FX_FONTSTYLE_Bold; |
| 1763 } | 1761 } |
| 1764 if (dwFontStyles & FX_FONTSTYLE_Italic) { | 1762 if (dwFontStyles & FX_FONTSTYLE_Italic) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1778 if (pFont) | 1776 if (pFont) |
| 1779 break; | 1777 break; |
| 1780 | 1778 |
| 1781 iLength--; | 1779 iLength--; |
| 1782 pNameText++; | 1780 pNameText++; |
| 1783 pReplace = pNameText; | 1781 pReplace = pNameText; |
| 1784 } | 1782 } |
| 1785 } | 1783 } |
| 1786 } | 1784 } |
| 1787 if (pFont) | 1785 if (pFont) |
| 1788 m_CacheFonts.Add(pFont); | 1786 m_CacheFonts.push_back(pFont); |
| 1789 return pFont; | 1787 return pFont; |
| 1790 } | 1788 } |
| 1791 | 1789 |
| 1792 CFGAS_GEFont* CXFA_DefFontMgr::GetDefaultFont( | 1790 CFX_RetainPtr<CFGAS_GEFont> CXFA_DefFontMgr::GetDefaultFont( |
| 1793 CXFA_FFDoc* hDoc, | 1791 CXFA_FFDoc* hDoc, |
| 1794 const CFX_WideStringC& wsFontFamily, | 1792 const CFX_WideStringC& wsFontFamily, |
| 1795 uint32_t dwFontStyles, | 1793 uint32_t dwFontStyles, |
| 1796 uint16_t wCodePage) { | 1794 uint16_t wCodePage) { |
| 1797 CFGAS_FontMgr* pFDEFontMgr = hDoc->GetApp()->GetFDEFontMgr(); | 1795 CFGAS_FontMgr* pFDEFontMgr = hDoc->GetApp()->GetFDEFontMgr(); |
| 1798 CFGAS_GEFont* pFont = | 1796 CFX_RetainPtr<CFGAS_GEFont> pFont = |
| 1799 pFDEFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, wCodePage); | 1797 pFDEFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, wCodePage); |
| 1800 if (!pFont) { | 1798 if (!pFont) { |
| 1801 pFont = pFDEFontMgr->LoadFont(static_cast<const FX_WCHAR*>(nullptr), | 1799 pFont = pFDEFontMgr->LoadFont(static_cast<const FX_WCHAR*>(nullptr), |
| 1802 dwFontStyles, wCodePage); | 1800 dwFontStyles, wCodePage); |
| 1803 } | 1801 } |
| 1804 | |
| 1805 ASSERT(pFont); | |
| 1806 if (pFont) | 1802 if (pFont) |
| 1807 m_CacheFonts.Add(pFont); | 1803 m_CacheFonts.push_back(pFont); |
| 1808 return pFont; | 1804 return pFont; |
| 1809 } | 1805 } |
| 1810 | 1806 |
| 1811 CXFA_PDFFontMgr::CXFA_PDFFontMgr(CXFA_FFDoc* pDoc) : m_pDoc(pDoc) {} | 1807 CXFA_PDFFontMgr::CXFA_PDFFontMgr(CXFA_FFDoc* pDoc) : m_pDoc(pDoc) {} |
| 1812 | 1808 |
| 1813 CXFA_PDFFontMgr::~CXFA_PDFFontMgr() { | 1809 CXFA_PDFFontMgr::~CXFA_PDFFontMgr() {} |
| 1814 for (const auto& pair : m_FontMap) { | |
| 1815 if (pair.second) | |
| 1816 pair.second->Release(); | |
| 1817 } | |
| 1818 } | |
| 1819 | 1810 |
| 1820 CFGAS_GEFont* CXFA_PDFFontMgr::FindFont(const CFX_ByteString& strPsName, | 1811 CFX_RetainPtr<CFGAS_GEFont> CXFA_PDFFontMgr::FindFont( |
| 1821 bool bBold, | 1812 const CFX_ByteString& strPsName, |
| 1822 bool bItalic, | 1813 bool bBold, |
| 1823 CPDF_Font** pDstPDFFont, | 1814 bool bItalic, |
| 1824 bool bStrictMatch) { | 1815 CPDF_Font** pDstPDFFont, |
| 1816 bool bStrictMatch) { |
| 1825 CPDF_Document* pDoc = m_pDoc->GetPDFDoc(); | 1817 CPDF_Document* pDoc = m_pDoc->GetPDFDoc(); |
| 1826 if (!pDoc) { | 1818 if (!pDoc) |
| 1827 return nullptr; | 1819 return nullptr; |
| 1828 } | 1820 |
| 1829 CPDF_Dictionary* pFontSetDict = | 1821 CPDF_Dictionary* pFontSetDict = |
| 1830 pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR"); | 1822 pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR"); |
| 1831 if (!pFontSetDict) { | 1823 if (!pFontSetDict) |
| 1832 return nullptr; | 1824 return nullptr; |
| 1833 } | 1825 |
| 1834 pFontSetDict = pFontSetDict->GetDictFor("Font"); | 1826 pFontSetDict = pFontSetDict->GetDictFor("Font"); |
| 1835 if (!pFontSetDict) { | 1827 if (!pFontSetDict) |
| 1836 return nullptr; | 1828 return nullptr; |
| 1837 } | 1829 |
| 1838 CFX_ByteString name = strPsName; | 1830 CFX_ByteString name = strPsName; |
| 1839 name.Remove(' '); | 1831 name.Remove(' '); |
| 1840 CFGAS_FontMgr* pFDEFontMgr = m_pDoc->GetApp()->GetFDEFontMgr(); | 1832 CFGAS_FontMgr* pFDEFontMgr = m_pDoc->GetApp()->GetFDEFontMgr(); |
| 1841 for (const auto& it : *pFontSetDict) { | 1833 for (const auto& it : *pFontSetDict) { |
| 1842 const CFX_ByteString& key = it.first; | 1834 const CFX_ByteString& key = it.first; |
| 1843 CPDF_Object* pObj = it.second.get(); | 1835 CPDF_Object* pObj = it.second.get(); |
| 1844 if (!PsNameMatchDRFontName(name.AsStringC(), bBold, bItalic, key, | 1836 if (!PsNameMatchDRFontName(name.AsStringC(), bBold, bItalic, key, |
| 1845 bStrictMatch)) { | 1837 bStrictMatch)) { |
| 1846 continue; | 1838 continue; |
| 1847 } | 1839 } |
| 1848 CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect()); | 1840 CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect()); |
| 1849 if (!pFontDict || pFontDict->GetStringFor("Type") != "Font") { | 1841 if (!pFontDict || pFontDict->GetStringFor("Type") != "Font") { |
| 1850 return nullptr; | 1842 return nullptr; |
| 1851 } | 1843 } |
| 1852 CPDF_Font* pPDFFont = pDoc->LoadFont(pFontDict); | 1844 CPDF_Font* pPDFFont = pDoc->LoadFont(pFontDict); |
| 1853 if (!pPDFFont) { | 1845 if (!pPDFFont) { |
| 1854 return nullptr; | 1846 return nullptr; |
| 1855 } | 1847 } |
| 1856 if (!pPDFFont->IsEmbedded()) { | 1848 if (!pPDFFont->IsEmbedded()) { |
| 1857 *pDstPDFFont = pPDFFont; | 1849 *pDstPDFFont = pPDFFont; |
| 1858 return nullptr; | 1850 return nullptr; |
| 1859 } | 1851 } |
| 1860 return CFGAS_GEFont::LoadFont(&pPDFFont->m_Font, pFDEFontMgr); | 1852 return CFGAS_GEFont::LoadFont(&pPDFFont->m_Font, pFDEFontMgr); |
| 1861 } | 1853 } |
| 1862 return nullptr; | 1854 return nullptr; |
| 1863 } | 1855 } |
| 1864 | 1856 |
| 1865 CFGAS_GEFont* CXFA_PDFFontMgr::GetFont(const CFX_WideStringC& wsFontFamily, | 1857 CFX_RetainPtr<CFGAS_GEFont> CXFA_PDFFontMgr::GetFont( |
| 1866 uint32_t dwFontStyles, | 1858 const CFX_WideStringC& wsFontFamily, |
| 1867 CPDF_Font** pPDFFont, | 1859 uint32_t dwFontStyles, |
| 1868 bool bStrictMatch) { | 1860 CPDF_Font** pPDFFont, |
| 1861 bool bStrictMatch) { |
| 1869 uint32_t dwHashCode = FX_HashCode_GetW(wsFontFamily, false); | 1862 uint32_t dwHashCode = FX_HashCode_GetW(wsFontFamily, false); |
| 1870 CFX_ByteString strKey; | 1863 CFX_ByteString strKey; |
| 1871 strKey.Format("%u%u", dwHashCode, dwFontStyles); | 1864 strKey.Format("%u%u", dwHashCode, dwFontStyles); |
| 1872 auto it = m_FontMap.find(strKey); | 1865 auto it = m_FontMap.find(strKey); |
| 1873 if (it != m_FontMap.end()) | 1866 if (it != m_FontMap.end()) |
| 1874 return it->second; | 1867 return it->second; |
| 1875 CFX_ByteString bsPsName = | 1868 CFX_ByteString bsPsName = |
| 1876 CFX_ByteString::FromUnicode(CFX_WideString(wsFontFamily)); | 1869 CFX_ByteString::FromUnicode(CFX_WideString(wsFontFamily)); |
| 1877 bool bBold = (dwFontStyles & FX_FONTSTYLE_Bold) == FX_FONTSTYLE_Bold; | 1870 bool bBold = (dwFontStyles & FX_FONTSTYLE_Bold) == FX_FONTSTYLE_Bold; |
| 1878 bool bItalic = (dwFontStyles & FX_FONTSTYLE_Italic) == FX_FONTSTYLE_Italic; | 1871 bool bItalic = (dwFontStyles & FX_FONTSTYLE_Italic) == FX_FONTSTYLE_Italic; |
| 1879 CFX_ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic); | 1872 CFX_ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic); |
| 1880 CFGAS_GEFont* pFont = | 1873 CFX_RetainPtr<CFGAS_GEFont> pFont = |
| 1881 FindFont(strFontName, bBold, bItalic, pPDFFont, bStrictMatch); | 1874 FindFont(strFontName, bBold, bItalic, pPDFFont, bStrictMatch); |
| 1882 if (pFont) | 1875 if (pFont) |
| 1883 m_FontMap[strKey] = pFont; | 1876 m_FontMap[strKey] = pFont; |
| 1884 return pFont; | 1877 return pFont; |
| 1885 } | 1878 } |
| 1886 | 1879 |
| 1887 CFX_ByteString CXFA_PDFFontMgr::PsNameToFontName( | 1880 CFX_ByteString CXFA_PDFFontMgr::PsNameToFontName( |
| 1888 const CFX_ByteString& strPsName, | 1881 const CFX_ByteString& strPsName, |
| 1889 bool bBold, | 1882 bool bBold, |
| 1890 bool bItalic) { | 1883 bool bItalic) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1968 } break; | 1961 } break; |
| 1969 default: | 1962 default: |
| 1970 break; | 1963 break; |
| 1971 } | 1964 } |
| 1972 return bMatch; | 1965 return bMatch; |
| 1973 } | 1966 } |
| 1974 } | 1967 } |
| 1975 return true; | 1968 return true; |
| 1976 } | 1969 } |
| 1977 | 1970 |
| 1978 bool CXFA_PDFFontMgr::GetCharWidth(const CFGAS_GEFont* pFont, | 1971 bool CXFA_PDFFontMgr::GetCharWidth(const CFX_RetainPtr<CFGAS_GEFont>& pFont, |
| 1979 FX_WCHAR wUnicode, | 1972 FX_WCHAR wUnicode, |
| 1980 bool bCharCode, | 1973 bool bCharCode, |
| 1981 int32_t* pWidth) { | 1974 int32_t* pWidth) { |
| 1982 if (wUnicode != 0x20 || bCharCode) | 1975 if (wUnicode != 0x20 || bCharCode) |
| 1983 return false; | 1976 return false; |
| 1984 | 1977 |
| 1985 auto it = m_FDE2PDFFont.find(pFont); | 1978 auto it = m_FDE2PDFFont.find(pFont); |
| 1986 if (it == m_FDE2PDFFont.end()) | 1979 if (it == m_FDE2PDFFont.end()) |
| 1987 return false; | 1980 return false; |
| 1988 | 1981 |
| 1989 CPDF_Font* pPDFFont = it->second; | 1982 CPDF_Font* pPDFFont = it->second; |
| 1990 *pWidth = pPDFFont->GetCharWidthF(pPDFFont->CharCodeFromUnicode(wUnicode)); | 1983 *pWidth = pPDFFont->GetCharWidthF(pPDFFont->CharCodeFromUnicode(wUnicode)); |
| 1991 return true; | 1984 return true; |
| 1992 } | 1985 } |
| 1993 | 1986 |
| 1994 void CXFA_PDFFontMgr::SetFont(const CFGAS_GEFont* pFont, CPDF_Font* pPDFFont) { | 1987 void CXFA_PDFFontMgr::SetFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont, |
| 1988 CPDF_Font* pPDFFont) { |
| 1995 m_FDE2PDFFont[pFont] = pPDFFont; | 1989 m_FDE2PDFFont[pFont] = pPDFFont; |
| 1996 } | 1990 } |
| 1997 | 1991 |
| 1998 CXFA_FontMgr::CXFA_FontMgr() {} | 1992 CXFA_FontMgr::CXFA_FontMgr() {} |
| 1999 | 1993 |
| 2000 CXFA_FontMgr::~CXFA_FontMgr() {} | 1994 CXFA_FontMgr::~CXFA_FontMgr() {} |
| 2001 | 1995 |
| 2002 CFGAS_GEFont* CXFA_FontMgr::GetFont(CXFA_FFDoc* hDoc, | 1996 CFX_RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont( |
| 2003 const CFX_WideStringC& wsFontFamily, | 1997 CXFA_FFDoc* hDoc, |
| 2004 uint32_t dwFontStyles, | 1998 const CFX_WideStringC& wsFontFamily, |
| 2005 uint16_t wCodePage) { | 1999 uint32_t dwFontStyles, |
| 2000 uint16_t wCodePage) { |
| 2006 uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false); | 2001 uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false); |
| 2007 CFX_ByteString bsKey; | 2002 CFX_ByteString bsKey; |
| 2008 bsKey.Format("%u%u%u", dwHash, dwFontStyles, wCodePage); | 2003 bsKey.Format("%u%u%u", dwHash, dwFontStyles, wCodePage); |
| 2009 auto iter = m_FontMap.find(bsKey); | 2004 auto iter = m_FontMap.find(bsKey); |
| 2010 if (iter != m_FontMap.end()) | 2005 if (iter != m_FontMap.end()) |
| 2011 return iter->second; | 2006 return iter->second; |
| 2012 | 2007 |
| 2013 CFX_WideString wsEnglishName = XFA_LocalFontNameToEnglishName(wsFontFamily); | 2008 CFX_WideString wsEnglishName = XFA_LocalFontNameToEnglishName(wsFontFamily); |
| 2014 auto it = m_PDFFontMgrMap.find(hDoc); | 2009 auto it = m_PDFFontMgrMap.find(hDoc); |
| 2015 CXFA_PDFFontMgr* pMgr = | 2010 CXFA_PDFFontMgr* pMgr = |
| 2016 it != m_PDFFontMgrMap.end() ? it->second.get() : nullptr; | 2011 it != m_PDFFontMgrMap.end() ? it->second.get() : nullptr; |
| 2017 CPDF_Font* pPDFFont = nullptr; | 2012 CPDF_Font* pPDFFont = nullptr; |
| 2018 CFGAS_GEFont* pFont = nullptr; | 2013 CFX_RetainPtr<CFGAS_GEFont> pFont; |
| 2019 if (pMgr) { | 2014 if (pMgr) { |
| 2020 pFont = | 2015 pFont = |
| 2021 pMgr->GetFont(wsEnglishName.AsStringC(), dwFontStyles, &pPDFFont, true); | 2016 pMgr->GetFont(wsEnglishName.AsStringC(), dwFontStyles, &pPDFFont, true); |
| 2022 if (pFont) | 2017 if (pFont) |
| 2023 return pFont; | 2018 return pFont; |
| 2024 } | 2019 } |
| 2025 if (!pFont && m_pDefFontMgr) | 2020 if (!pFont && m_pDefFontMgr) |
| 2026 pFont = m_pDefFontMgr->GetFont(hDoc, wsFontFamily, dwFontStyles, wCodePage); | 2021 pFont = m_pDefFontMgr->GetFont(hDoc, wsFontFamily, dwFontStyles, wCodePage); |
| 2027 | 2022 |
| 2028 if (!pFont && pMgr) { | 2023 if (!pFont && pMgr) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 2051 m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique<CXFA_PDFFontMgr>(hDoc); | 2046 m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique<CXFA_PDFFontMgr>(hDoc); |
| 2052 } | 2047 } |
| 2053 | 2048 |
| 2054 void CXFA_FontMgr::ReleaseDocFonts(CXFA_FFDoc* hDoc) { | 2049 void CXFA_FontMgr::ReleaseDocFonts(CXFA_FFDoc* hDoc) { |
| 2055 m_PDFFontMgrMap.erase(hDoc); | 2050 m_PDFFontMgrMap.erase(hDoc); |
| 2056 } | 2051 } |
| 2057 | 2052 |
| 2058 void CXFA_FontMgr::SetDefFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr) { | 2053 void CXFA_FontMgr::SetDefFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr) { |
| 2059 m_pDefFontMgr = std::move(pFontMgr); | 2054 m_pDefFontMgr = std::move(pFontMgr); |
| 2060 } | 2055 } |
| OLD | NEW |