Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: core/src/fpdfapi/fpdf_font/fpdf_font.cpp

Issue 1408063016: Fix a leak in CPDF_Type3Font::LoadChar(). (Closed) Base URL: https://pdfium.googlesource.com/pdfium@parser_b268
Patch Set: remove dead code, m_bPageRequired is always false Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/include/fpdfapi/fpdf_resource.h ('k') | core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "../../../include/fpdfapi/fpdf_module.h" 7 #include "../../../include/fpdfapi/fpdf_module.h"
8 #include "../../../include/fpdfapi/fpdf_page.h" 8 #include "../../../include/fpdfapi/fpdf_page.h"
9 #include "../../../include/fpdfapi/fpdf_pageobj.h" 9 #include "../../../include/fpdfapi/fpdf_pageobj.h"
10 #include "../../../include/fpdfapi/fpdf_resource.h" 10 #include "../../../include/fpdfapi/fpdf_resource.h"
(...skipping 1625 matching lines...) Expand 10 before | Expand all | Expand 10 after
1636 } 1636 }
1637 } 1637 }
1638 if (bGotOne) { 1638 if (bGotOne) {
1639 return; 1639 return;
1640 } 1640 }
1641 } 1641 }
1642 for (int charcode = 0; charcode < 256; charcode++) { 1642 for (int charcode = 0; charcode < 256; charcode++) {
1643 m_GlyphIndex[charcode] = charcode; 1643 m_GlyphIndex[charcode] = charcode;
1644 } 1644 }
1645 } 1645 }
1646 CPDF_Type3Font::CPDF_Type3Font() : CPDF_SimpleFont(PDFFONT_TYPE3) { 1646
1647 m_pPageResources = NULL; 1647 CPDF_Type3Font::CPDF_Type3Font()
1648 FXSYS_memset(m_CharWidthL, 0, sizeof m_CharWidthL); 1648 : CPDF_SimpleFont(PDFFONT_TYPE3),
1649 m_pCharProcs(nullptr),
1650 m_pPageResources(nullptr),
1651 m_pFontResources(nullptr) {
1652 FXSYS_memset(m_CharWidthL, 0, sizeof(m_CharWidthL));
1649 } 1653 }
1654
1650 CPDF_Type3Font::~CPDF_Type3Font() { 1655 CPDF_Type3Font::~CPDF_Type3Font() {
1651 FX_POSITION pos = m_CacheMap.GetStartPosition(); 1656 for (auto it : m_CacheMap)
1652 while (pos) { 1657 delete it.second;
1653 void* key;
1654 void* value;
1655 m_CacheMap.GetNextAssoc(pos, key, value);
1656 delete (CPDF_Type3Char*)value;
1657 }
1658 m_CacheMap.RemoveAll();
1659 pos = m_DeletedMap.GetStartPosition();
1660 while (pos) {
1661 void* key;
1662 void* value;
1663 m_DeletedMap.GetNextAssoc(pos, key, value);
1664 delete (CPDF_Type3Char*)key;
1665 }
1666 } 1658 }
1659
1667 FX_BOOL CPDF_Type3Font::_Load() { 1660 FX_BOOL CPDF_Type3Font::_Load() {
1668 m_pFontResources = m_pFontDict->GetDict(FX_BSTRC("Resources")); 1661 m_pFontResources = m_pFontDict->GetDict(FX_BSTRC("Resources"));
1669 CPDF_Array* pMatrix = m_pFontDict->GetArray(FX_BSTRC("FontMatrix")); 1662 CPDF_Array* pMatrix = m_pFontDict->GetArray(FX_BSTRC("FontMatrix"));
1670 FX_FLOAT xscale = 1.0f, yscale = 1.0f; 1663 FX_FLOAT xscale = 1.0f, yscale = 1.0f;
1671 if (pMatrix) { 1664 if (pMatrix) {
1672 m_FontMatrix = pMatrix->GetMatrix(); 1665 m_FontMatrix = pMatrix->GetMatrix();
1673 xscale = m_FontMatrix.a; 1666 xscale = m_FontMatrix.a;
1674 yscale = m_FontMatrix.d; 1667 yscale = m_FontMatrix.d;
1675 } 1668 }
1676 CPDF_Array* pBBox = m_pFontDict->GetArray(FX_BSTRC("FontBBox")); 1669 CPDF_Array* pBBox = m_pFontDict->GetArray(FX_BSTRC("FontBBox"));
(...skipping 30 matching lines...) Expand all
1707 m_Encoding.m_Unicodes[i] = i; 1700 m_Encoding.m_Unicodes[i] = i;
1708 } 1701 }
1709 } 1702 }
1710 } 1703 }
1711 } 1704 }
1712 return TRUE; 1705 return TRUE;
1713 } 1706 }
1714 void CPDF_Type3Font::CheckType3FontMetrics() { 1707 void CPDF_Type3Font::CheckType3FontMetrics() {
1715 CheckFontMetrics(); 1708 CheckFontMetrics();
1716 } 1709 }
1710
1717 CPDF_Type3Char* CPDF_Type3Font::LoadChar(FX_DWORD charcode, int level) { 1711 CPDF_Type3Char* CPDF_Type3Font::LoadChar(FX_DWORD charcode, int level) {
1718 if (level >= _FPDF_MAX_TYPE3_FORM_LEVEL_) 1712 if (level >= _FPDF_MAX_TYPE3_FORM_LEVEL_)
1719 return nullptr; 1713 return nullptr;
1720 1714
1721 CPDF_Type3Char* pChar = nullptr; 1715 auto it = m_CacheMap.find(charcode);
1722 if (m_CacheMap.Lookup((void*)(uintptr_t)charcode, (void*&)pChar)) { 1716 if (it != m_CacheMap.end())
1723 if (pChar->m_bPageRequired && m_pPageResources) { 1717 return it->second;
1724 delete pChar; 1718
1725 m_CacheMap.RemoveKey((void*)(uintptr_t)charcode);
1726 return LoadChar(charcode, level + 1);
1727 }
1728 return pChar;
1729 }
1730 const FX_CHAR* name = 1719 const FX_CHAR* name =
1731 GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode); 1720 GetAdobeCharName(m_BaseEncoding, m_pCharNames, charcode);
1732 if (!name) 1721 if (!name)
1733 return nullptr; 1722 return nullptr;
1734 1723
1735 CPDF_Stream* pStream = 1724 CPDF_Stream* pStream =
1736 ToStream(m_pCharProcs ? m_pCharProcs->GetElementValue(name) : nullptr); 1725 ToStream(m_pCharProcs ? m_pCharProcs->GetElementValue(name) : nullptr);
1737 if (!pStream) 1726 if (!pStream)
1738 return nullptr; 1727 return nullptr;
1739 1728
1740 pChar = new CPDF_Type3Char; 1729 nonstd::unique_ptr<CPDF_Type3Char> pNewChar(new CPDF_Type3Char(new CPDF_Form(
1741 pChar->m_pForm = new CPDF_Form(
1742 m_pDocument, m_pFontResources ? m_pFontResources : m_pPageResources, 1730 m_pDocument, m_pFontResources ? m_pFontResources : m_pPageResources,
1743 pStream, nullptr); 1731 pStream, nullptr)));
1744 pChar->m_pForm->ParseContent(nullptr, nullptr, pChar, nullptr, level + 1); 1732
1733 // This can trigger recursion into this method. The content of |m_CacheMap|
1734 // can change as a result. Thus after it returns, check the cache again for
1735 // a cache hit.
1736 pNewChar->m_pForm->ParseContent(nullptr, nullptr, pNewChar.get(), nullptr,
1737 level + 1);
1738 it = m_CacheMap.find(charcode);
1739 if (it != m_CacheMap.end())
1740 return it->second;
1741
1745 FX_FLOAT scale = m_FontMatrix.GetXUnit(); 1742 FX_FLOAT scale = m_FontMatrix.GetXUnit();
1746 pChar->m_Width = (int32_t)(pChar->m_Width * scale + 0.5f); 1743 pNewChar->m_Width = (int32_t)(pNewChar->m_Width * scale + 0.5f);
1747 FX_RECT& rcBBox = pChar->m_BBox; 1744 FX_RECT& rcBBox = pNewChar->m_BBox;
1748 CFX_FloatRect char_rect( 1745 CFX_FloatRect char_rect(
1749 (FX_FLOAT)rcBBox.left / 1000.0f, (FX_FLOAT)rcBBox.bottom / 1000.0f, 1746 (FX_FLOAT)rcBBox.left / 1000.0f, (FX_FLOAT)rcBBox.bottom / 1000.0f,
1750 (FX_FLOAT)rcBBox.right / 1000.0f, (FX_FLOAT)rcBBox.top / 1000.0f); 1747 (FX_FLOAT)rcBBox.right / 1000.0f, (FX_FLOAT)rcBBox.top / 1000.0f);
1751 if (rcBBox.right <= rcBBox.left || rcBBox.bottom >= rcBBox.top) { 1748 if (rcBBox.right <= rcBBox.left || rcBBox.bottom >= rcBBox.top)
1752 char_rect = pChar->m_pForm->CalcBoundingBox(); 1749 char_rect = pNewChar->m_pForm->CalcBoundingBox();
1753 } 1750
1754 char_rect.Transform(&m_FontMatrix); 1751 char_rect.Transform(&m_FontMatrix);
1755 rcBBox.left = FXSYS_round(char_rect.left * 1000); 1752 rcBBox.left = FXSYS_round(char_rect.left * 1000);
1756 rcBBox.right = FXSYS_round(char_rect.right * 1000); 1753 rcBBox.right = FXSYS_round(char_rect.right * 1000);
1757 rcBBox.top = FXSYS_round(char_rect.top * 1000); 1754 rcBBox.top = FXSYS_round(char_rect.top * 1000);
1758 rcBBox.bottom = FXSYS_round(char_rect.bottom * 1000); 1755 rcBBox.bottom = FXSYS_round(char_rect.bottom * 1000);
1759 m_CacheMap.SetAt((void*)(uintptr_t)charcode, pChar); 1756
1760 if (pChar->m_pForm->CountObjects() == 0) { 1757 FXSYS_assert(m_CacheMap.find(charcode) == m_CacheMap.end());
1761 delete pChar->m_pForm; 1758 CPDF_Type3Char* pCachedChar = pNewChar.release();
1762 pChar->m_pForm = nullptr; 1759 m_CacheMap[charcode] = pCachedChar;
1760 if (pCachedChar->m_pForm->CountObjects() == 0) {
1761 delete pCachedChar->m_pForm;
1762 pCachedChar->m_pForm = nullptr;
1763 } 1763 }
1764 return pChar; 1764 return pCachedChar;
1765 } 1765 }
1766
1766 int CPDF_Type3Font::GetCharWidthF(FX_DWORD charcode, int level) { 1767 int CPDF_Type3Font::GetCharWidthF(FX_DWORD charcode, int level) {
1767 if (charcode > 0xff) { 1768 if (charcode >= FX_ArraySize(m_CharWidthL))
1768 charcode = 0; 1769 charcode = 0;
1769 } 1770
1770 if (m_CharWidthL[charcode]) { 1771 if (m_CharWidthL[charcode])
1771 return m_CharWidthL[charcode]; 1772 return m_CharWidthL[charcode];
1772 } 1773
1773 CPDF_Type3Char* pChar = LoadChar(charcode, level); 1774 const CPDF_Type3Char* pChar = LoadChar(charcode, level);
1774 if (pChar == NULL) { 1775 return pChar ? pChar->m_Width : 0;
1775 return 0;
1776 }
1777 return pChar->m_Width;
1778 } 1776 }
1777
1779 void CPDF_Type3Font::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level) { 1778 void CPDF_Type3Font::GetCharBBox(FX_DWORD charcode, FX_RECT& rect, int level) {
1780 CPDF_Type3Char* pChar = LoadChar(charcode, level); 1779 const CPDF_Type3Char* pChar = LoadChar(charcode, level);
1781 if (pChar == NULL) { 1780 if (!pChar) {
1782 rect.left = rect.right = rect.top = rect.bottom = 0; 1781 rect.left = 0;
1782 rect.right = 0;
1783 rect.top = 0;
1784 rect.bottom = 0;
1783 return; 1785 return;
1784 } 1786 }
1785 rect = pChar->m_BBox; 1787 rect = pChar->m_BBox;
1786 } 1788 }
1787 CPDF_Type3Char::CPDF_Type3Char() { 1789
1788 m_pForm = NULL; 1790 CPDF_Type3Char::CPDF_Type3Char(CPDF_Form* pForm)
1789 m_pBitmap = NULL; 1791 : m_pForm(pForm), m_pBitmap(nullptr), m_bColored(FALSE) {}
1790 m_bPageRequired = FALSE; 1792
1791 m_bColored = FALSE;
1792 }
1793 CPDF_Type3Char::~CPDF_Type3Char() { 1793 CPDF_Type3Char::~CPDF_Type3Char() {
1794 delete m_pForm; 1794 delete m_pForm;
1795 delete m_pBitmap; 1795 delete m_pBitmap;
1796 } 1796 }
OLDNEW
« no previous file with comments | « core/include/fpdfapi/fpdf_resource.h ('k') | core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698