From c411eb943bb51e16ff4fb5a6ffb06e277ca6a982 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 25 Jul 2017 09:39:30 -0400 Subject: Move fpdfsdk/pdfwindow to fpdfsdk/pwl This makes it clearer what the directory contains. Change-Id: I34fc38dd30b8e0f6e057052ea33c8b5a10f1b9c3 Reviewed-on: https://pdfium-review.googlesource.com/8791 Commit-Queue: dsinclair Reviewed-by: Henrique Nakashima Reviewed-by: Tom Sepez --- fpdfsdk/pwl/cpwl_icon.cpp | 131 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 fpdfsdk/pwl/cpwl_icon.cpp (limited to 'fpdfsdk/pwl/cpwl_icon.cpp') diff --git a/fpdfsdk/pwl/cpwl_icon.cpp b/fpdfsdk/pwl/cpwl_icon.cpp new file mode 100644 index 0000000000..8a2e31da6a --- /dev/null +++ b/fpdfsdk/pwl/cpwl_icon.cpp @@ -0,0 +1,131 @@ +// 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 "fpdfsdk/pwl/cpwl_icon.h" + +#include +#include + +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_stream.h" +#include "fpdfsdk/pwl/cpwl_wnd.h" + +CPWL_Icon::CPWL_Icon() : m_pPDFStream(nullptr), m_pIconFit(nullptr) {} + +CPWL_Icon::~CPWL_Icon() {} + +std::pair CPWL_Icon::GetImageSize() { + if (!m_pPDFStream) + return {0.0f, 0.0f}; + + CPDF_Dictionary* pDict = m_pPDFStream->GetDict(); + if (!pDict) + return {0.0f, 0.0f}; + + CFX_FloatRect rect = pDict->GetRectFor("BBox"); + return {rect.right - rect.left, rect.top - rect.bottom}; +} + +CFX_Matrix CPWL_Icon::GetImageMatrix() { + if (!m_pPDFStream) + return CFX_Matrix(); + if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) + return pDict->GetMatrixFor("Matrix"); + return CFX_Matrix(); +} + +CFX_ByteString CPWL_Icon::GetImageAlias() { + if (!m_pPDFStream) + return CFX_ByteString(); + if (CPDF_Dictionary* pDict = m_pPDFStream->GetDict()) + return pDict->GetStringFor("Name"); + return CFX_ByteString(); +} + +std::pair CPWL_Icon::GetIconPosition() { + if (!m_pIconFit) + return {0.0f, 0.0f}; + + CPDF_Array* pA = + m_pIconFit->GetDict() ? m_pIconFit->GetDict()->GetArrayFor("A") : nullptr; + if (!pA) + return {0.0f, 0.0f}; + + size_t dwCount = pA->GetCount(); + return {dwCount > 0 ? pA->GetNumberAt(0) : 0.0f, + dwCount > 1 ? pA->GetNumberAt(1) : 0.0f}; +} + +std::pair CPWL_Icon::GetScale() { + float fHScale = 1.0f; + float fVScale = 1.0f; + + if (!m_pPDFStream) + return {fHScale, fVScale}; + + CFX_FloatRect rcPlate = GetClientRect(); + float fPlateWidth = rcPlate.right - rcPlate.left; + float fPlateHeight = rcPlate.top - rcPlate.bottom; + + float fImageWidth; + float fImageHeight; + std::tie(fImageWidth, fImageHeight) = GetImageSize(); + + int32_t nScaleMethod = m_pIconFit ? m_pIconFit->GetScaleMethod() : 0; + + switch (nScaleMethod) { + default: + case 0: + fHScale = fPlateWidth / std::max(fImageWidth, 1.0f); + fVScale = fPlateHeight / std::max(fImageHeight, 1.0f); + break; + case 1: + if (fPlateWidth < fImageWidth) + fHScale = fPlateWidth / std::max(fImageWidth, 1.0f); + if (fPlateHeight < fImageHeight) + fVScale = fPlateHeight / std::max(fImageHeight, 1.0f); + break; + case 2: + if (fPlateWidth > fImageWidth) + fHScale = fPlateWidth / std::max(fImageWidth, 1.0f); + if (fPlateHeight > fImageHeight) + fVScale = fPlateHeight / std::max(fImageHeight, 1.0f); + break; + case 3: + break; + } + + float fMinScale; + if (m_pIconFit && m_pIconFit->IsProportionalScale()) { + fMinScale = std::min(fHScale, fVScale); + fHScale = fMinScale; + fVScale = fMinScale; + } + return {fHScale, fVScale}; +} + +std::pair CPWL_Icon::GetImageOffset() { + float fLeft; + float fBottom; + std::tie(fLeft, fBottom) = GetIconPosition(); + + float fImageWidth; + float fImageHeight; + std::tie(fImageWidth, fImageHeight) = GetImageSize(); + + float fHScale, fVScale; + std::tie(fHScale, fVScale) = GetScale(); + + float fImageFactWidth = fImageWidth * fHScale; + float fImageFactHeight = fImageHeight * fVScale; + + CFX_FloatRect rcPlate = GetClientRect(); + float fPlateWidth = rcPlate.right - rcPlate.left; + float fPlateHeight = rcPlate.top - rcPlate.bottom; + + return {(fPlateWidth - fImageFactWidth) * fLeft, + (fPlateHeight - fImageFactHeight) * fBottom}; +} -- cgit v1.2.3