blob: 784ed4ebd96b544b4f7020e5cf2c82fd4e92e6c4 [file] [log] [blame]
dsinclairb9590102016-04-27 06:38:59 -07001// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fpdfsdk/cfx_systemhandler.h"
8
thestig907a5222016-06-21 14:38:27 -07009#include <memory>
10
dsinclairf34518b2016-09-13 12:03:48 -070011#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
npmf73893a2016-07-27 13:54:25 -070012#include "core/fxge/include/cfx_fontmapper.h"
13#include "core/fxge/include/cfx_fontmgr.h"
npm26b86e62016-08-04 17:22:14 -070014#include "core/fxge/include/cfx_gemodule.h"
dsinclairb9590102016-04-27 06:38:59 -070015#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclairf34518b2016-09-13 12:03:48 -070016#include "fpdfsdk/include/cpdfsdk_annot.h"
17#include "fpdfsdk/include/cpdfsdk_document.h"
dsinclair79db6092016-09-14 07:27:21 -070018#include "fpdfsdk/include/cpdfsdk_environment.h"
dsinclairf34518b2016-09-13 12:03:48 -070019#include "fpdfsdk/include/cpdfsdk_pageview.h"
dsinclairb9590102016-04-27 06:38:59 -070020
21namespace {
22
23int CharSet2CP(int charset) {
thestig907a5222016-06-21 14:38:27 -070024 if (charset == FXFONT_SHIFTJIS_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070025 return 932;
thestig907a5222016-06-21 14:38:27 -070026 if (charset == FXFONT_GB2312_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070027 return 936;
thestig907a5222016-06-21 14:38:27 -070028 if (charset == FXFONT_HANGEUL_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070029 return 949;
thestig907a5222016-06-21 14:38:27 -070030 if (charset == FXFONT_CHINESEBIG5_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070031 return 950;
32 return 0;
33}
34
35} // namespace
36
37void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
dsinclair1f248902016-09-14 10:38:17 -070038 m_pEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070039}
40
41void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
42 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
43 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
44 UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage();
45 if (!pPage || !pPageView)
46 return;
47
48 CFX_Matrix page2device;
49 pPageView->GetCurrentMatrix(page2device);
50 CFX_Matrix device2page;
51 device2page.SetReverse(page2device);
52 FX_FLOAT left, top, right, bottom;
53 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
54 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
55 bottom);
56 CFX_FloatRect rcPDF(left, bottom, right, top);
57 rcPDF.Normalize();
58
dsinclair1f248902016-09-14 10:38:17 -070059 m_pEnv->Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right, rcPDF.bottom);
dsinclairb9590102016-04-27 06:38:59 -070060}
61
62void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller,
63 CFX_FloatRect& rect) {
64 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
65 if (!pFFL)
66 return;
67
68 CFX_FloatPoint leftbottom = CFX_FloatPoint(rect.left, rect.bottom);
69 CFX_FloatPoint righttop = CFX_FloatPoint(rect.right, rect.top);
70 CFX_FloatPoint ptA = pFFL->PWLtoFFL(leftbottom);
71 CFX_FloatPoint ptB = pFFL->PWLtoFFL(righttop);
72 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
73 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
74 ASSERT(pPage);
75
dsinclair1f248902016-09-14 10:38:17 -070076 m_pEnv->OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
dsinclairb9590102016-04-27 06:38:59 -070077}
78
79bool CFX_SystemHandler::IsSelectionImplemented() const {
80 if (m_pEnv) {
81 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
82 if (pInfo && pInfo->FFI_OutputSelectedRect)
83 return true;
84 }
85 return false;
86}
87
thestig907a5222016-06-21 14:38:27 -070088bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070089 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
90 if (!pFontMgr)
91 return false;
92
93 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
94 if (!pFontMapper)
95 return false;
96
97 if (pFontMapper->m_InstalledTTFonts.empty())
98 pFontMapper->LoadInstalledFonts();
99
100 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
101 if (font.Compare(sFontFaceName.AsStringC()))
102 return true;
103 }
104
105 return false;
106}
107
108CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
109 CPDF_Document* pDoc,
110 CFX_ByteString sFontFaceName,
111 uint8_t nCharset) {
112 if (!pDoc)
113 return nullptr;
114
thestig907a5222016-06-21 14:38:27 -0700115 std::unique_ptr<CFX_Font> pFXFont(new CFX_Font);
dsinclairb9590102016-04-27 06:38:59 -0700116 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset), FALSE);
thestig907a5222016-06-21 14:38:27 -0700117 return pDoc->AddFont(pFXFont.get(), nCharset, FALSE);
dsinclairb9590102016-04-27 06:38:59 -0700118}
119
120int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
121 TimerCallback lpTimerFunc) {
dsinclair1f248902016-09-14 10:38:17 -0700122 return m_pEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700123}
124
125void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair1f248902016-09-14 10:38:17 -0700126 m_pEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700127}
128
129FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
dsinclair1f248902016-09-14 10:38:17 -0700130 return m_pEnv->GetLocalTime();
dsinclairb9590102016-04-27 06:38:59 -0700131}
132
133bool CFX_SystemHandler::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair1f248902016-09-14 10:38:17 -0700134 return !!m_pEnv->IsSHIFTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700135}
136
137bool CFX_SystemHandler::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair1f248902016-09-14 10:38:17 -0700138 return !!m_pEnv->IsCTRLKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700139}
140
141bool CFX_SystemHandler::IsALTKeyDown(uint32_t nFlag) const {
dsinclair1f248902016-09-14 10:38:17 -0700142 return !!m_pEnv->IsALTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700143}