diff options
Diffstat (limited to 'fpdfsdk/fpdf_flatten.cpp')
-rw-r--r-- | fpdfsdk/fpdf_flatten.cpp | 70 |
1 files changed, 26 insertions, 44 deletions
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index 914008c1a3..e530553f5a 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -29,23 +29,19 @@ enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; namespace { -bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { - if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) +bool IsValidRect(const CFX_FloatRect& rect, const CFX_FloatRect& rcPage) { + constexpr float kMinSize = 0.000001f; + if (rect.IsEmpty() || rect.Width() < kMinSize || rect.Height() < kMinSize) return false; - if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f && - rect.bottom == 0.0f) - return false; - - if (!rcPage.IsEmpty()) { - if (rect.left - rcPage.left < -10.000001f || - rect.right - rcPage.right > 10.000001f || - rect.top - rcPage.top > 10.000001f || - rect.bottom - rcPage.bottom < -10.000001f) - return false; - } + if (rcPage.IsEmpty()) + return true; - return true; + constexpr float kMinBorderSize = 10.000001f; + return rect.left - rcPage.left >= -kMinBorderSize && + rect.right - rcPage.right <= kMinBorderSize && + rect.top - rcPage.top <= kMinBorderSize && + rect.bottom - rcPage.bottom >= -kMinBorderSize; } void GetContentsRect(CPDF_Document* pDoc, @@ -60,7 +56,7 @@ void GetContentsRect(CPDF_Document* pDoc, rc.right = pPageObject->m_Right; rc.bottom = pPageObject->m_Bottom; rc.top = pPageObject->m_Top; - if (IsValiableRect(rc, pDict->GetRectFor("MediaBox"))) + if (IsValidRect(rc, pDict->GetRectFor("MediaBox"))) pRectArray->push_back(rc); } } @@ -77,7 +73,7 @@ void ParserStream(CPDF_Dictionary* pPageDic, else if (pStream->KeyExist("BBox")) rect = pStream->GetRectFor("BBox"); - if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox"))) + if (IsValidRect(rect, pPageDic->GetRectFor("MediaBox"))) pRectArray->push_back(rect); pObjectArray->push_back(pStream); @@ -96,9 +92,8 @@ int ParserAnnots(CPDF_Document* pSourceDoc, if (!pAnnots) return FLATTEN_NOTHINGTODO; - uint32_t dwSize = pAnnots->GetCount(); - for (int i = 0; i < (int)dwSize; i++) { - CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i)); + for (const auto& pAnnot : *pAnnots) { + CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnot->GetDirect()); if (!pAnnotDic) continue; @@ -110,15 +105,13 @@ int ParserAnnots(CPDF_Document* pSourceDoc, if (nAnnotFlag & ANNOTFLAG_HIDDEN) continue; - if (nUsage == FLAT_NORMALDISPLAY) { - if (nAnnotFlag & ANNOTFLAG_INVISIBLE) - continue; - + bool bParseStream; + if (nUsage == FLAT_NORMALDISPLAY) + bParseStream = !(nAnnotFlag & ANNOTFLAG_INVISIBLE); + else + bParseStream = !!(nAnnotFlag & ANNOTFLAG_PRINT); + if (bParseStream) ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); - } else { - if (nAnnotFlag & ANNOTFLAG_PRINT) - ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); - } } return FLATTEN_SUCCESS; } @@ -126,10 +119,10 @@ int ParserAnnots(CPDF_Document* pSourceDoc, float GetMinMaxValue(const std::vector<CFX_FloatRect>& array, FPDF_TYPE type, FPDF_VALUE value) { - size_t nRects = array.size(); - if (nRects <= 0) + if (array.empty()) return 0.0f; + size_t nRects = array.size(); std::vector<float> pArray(nRects); switch (value) { case LEFT: @@ -149,7 +142,7 @@ float GetMinMaxValue(const std::vector<CFX_FloatRect>& array, pArray[i] = array[i].bottom; break; default: - // Not reachable. + NOTREACHED(); return 0.0f; } @@ -330,7 +323,6 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject"); pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form"); pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1); - pNewOXbjectDic->SetNewFor<CPDF_Name>("Name", "FRM"); CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); pNewOXbjectDic->SetRectFor("BBox", rcBBox); } @@ -357,9 +349,8 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { if (!sAnnotState.IsEmpty()) { pAPStream = pAPDic->GetStreamFor(sAnnotState); } else { - auto it = pAPDic->begin(); - if (it != pAPDic->end()) { - CPDF_Object* pFirstObj = it->second.get(); + if (pAPDic->GetCount() > 0) { + CPDF_Object* pFirstObj = pAPDic->begin()->second.get(); if (pFirstObj) { if (pFirstObj->IsReference()) pFirstObj = pFirstObj->GetDirect(); @@ -409,17 +400,8 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pAcc->LoadAllData(); CFX_ByteString sStream(pAcc->GetData(), pAcc->GetSize()); CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix"); - if (matrix.IsIdentity()) { - matrix.a = 1.0f; - matrix.b = 0.0f; - matrix.c = 0.0f; - matrix.d = 1.0f; - matrix.e = 0.0f; - matrix.f = 0.0f; - } - - CFX_ByteString sTemp; CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); + CFX_ByteString sTemp; sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, sFormName.c_str()); sStream += sTemp; |