blob: eb9cff28dfa0518fb1eeec58b3939d25829ea9a0 [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) {
38 m_pEnv->FFI_SetCursor(nCursorType);
39}
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
59 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
60 rcPDF.bottom);
61}
62
63void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller,
64 CFX_FloatRect& rect) {
65 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
66 if (!pFFL)
67 return;
68
69 CFX_FloatPoint leftbottom = CFX_FloatPoint(rect.left, rect.bottom);
70 CFX_FloatPoint righttop = CFX_FloatPoint(rect.right, rect.top);
71 CFX_FloatPoint ptA = pFFL->PWLtoFFL(leftbottom);
72 CFX_FloatPoint ptB = pFFL->PWLtoFFL(righttop);
73 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
74 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
75 ASSERT(pPage);
76
77 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
78}
79
80bool CFX_SystemHandler::IsSelectionImplemented() const {
81 if (m_pEnv) {
82 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
83 if (pInfo && pInfo->FFI_OutputSelectedRect)
84 return true;
85 }
86 return false;
87}
88
thestig907a5222016-06-21 14:38:27 -070089bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070090 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
91 if (!pFontMgr)
92 return false;
93
94 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
95 if (!pFontMapper)
96 return false;
97
98 if (pFontMapper->m_InstalledTTFonts.empty())
99 pFontMapper->LoadInstalledFonts();
100
101 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
102 if (font.Compare(sFontFaceName.AsStringC()))
103 return true;
104 }
105
106 return false;
107}
108
109CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
110 CPDF_Document* pDoc,
111 CFX_ByteString sFontFaceName,
112 uint8_t nCharset) {
113 if (!pDoc)
114 return nullptr;
115
thestig907a5222016-06-21 14:38:27 -0700116 std::unique_ptr<CFX_Font> pFXFont(new CFX_Font);
dsinclairb9590102016-04-27 06:38:59 -0700117 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset), FALSE);
thestig907a5222016-06-21 14:38:27 -0700118 return pDoc->AddFont(pFXFont.get(), nCharset, FALSE);
dsinclairb9590102016-04-27 06:38:59 -0700119}
120
121int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
122 TimerCallback lpTimerFunc) {
123 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
124}
125
126void CFX_SystemHandler::KillTimer(int32_t nID) {
127 m_pEnv->FFI_KillTimer(nID);
128}
129
130FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
131 return m_pEnv->FFI_GetLocalTime();
132}
133
134bool CFX_SystemHandler::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair224bc5a2016-04-28 11:04:51 -0700135 return !!m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700136}
137
138bool CFX_SystemHandler::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair224bc5a2016-04-28 11:04:51 -0700139 return !!m_pEnv->FFI_IsCTRLKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700140}
141
142bool CFX_SystemHandler::IsALTKeyDown(uint32_t nFlag) const {
dsinclair224bc5a2016-04-28 11:04:51 -0700143 return !!m_pEnv->FFI_IsALTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700144}