blob: 48896b0934bfc15206708a5a000131bc0d0a9e4e [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
npmf73893a2016-07-27 13:54:25 -070011#include "core/fxge/include/cfx_fontmapper.h"
12#include "core/fxge/include/cfx_fontmgr.h"
thestig25fa42f2016-05-25 21:39:46 -070013#include "core/fxge/include/fx_ge.h"
dsinclairb9590102016-04-27 06:38:59 -070014#include "fpdfsdk/formfiller/cffl_formfiller.h"
15#include "fpdfsdk/include/fsdk_mgr.h"
16
17namespace {
18
19int CharSet2CP(int charset) {
thestig907a5222016-06-21 14:38:27 -070020 if (charset == FXFONT_SHIFTJIS_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070021 return 932;
thestig907a5222016-06-21 14:38:27 -070022 if (charset == FXFONT_GB2312_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070023 return 936;
thestig907a5222016-06-21 14:38:27 -070024 if (charset == FXFONT_HANGEUL_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070025 return 949;
thestig907a5222016-06-21 14:38:27 -070026 if (charset == FXFONT_CHINESEBIG5_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070027 return 950;
28 return 0;
29}
30
31} // namespace
32
33void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
34 m_pEnv->FFI_SetCursor(nCursorType);
35}
36
37void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
38 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
39 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
40 UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage();
41 if (!pPage || !pPageView)
42 return;
43
44 CFX_Matrix page2device;
45 pPageView->GetCurrentMatrix(page2device);
46 CFX_Matrix device2page;
47 device2page.SetReverse(page2device);
48 FX_FLOAT left, top, right, bottom;
49 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
50 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
51 bottom);
52 CFX_FloatRect rcPDF(left, bottom, right, top);
53 rcPDF.Normalize();
54
55 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
56 rcPDF.bottom);
57}
58
59void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller,
60 CFX_FloatRect& rect) {
61 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
62 if (!pFFL)
63 return;
64
65 CFX_FloatPoint leftbottom = CFX_FloatPoint(rect.left, rect.bottom);
66 CFX_FloatPoint righttop = CFX_FloatPoint(rect.right, rect.top);
67 CFX_FloatPoint ptA = pFFL->PWLtoFFL(leftbottom);
68 CFX_FloatPoint ptB = pFFL->PWLtoFFL(righttop);
69 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
70 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
71 ASSERT(pPage);
72
73 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
74}
75
76bool CFX_SystemHandler::IsSelectionImplemented() const {
77 if (m_pEnv) {
78 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
79 if (pInfo && pInfo->FFI_OutputSelectedRect)
80 return true;
81 }
82 return false;
83}
84
thestig907a5222016-06-21 14:38:27 -070085bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070086 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
87 if (!pFontMgr)
88 return false;
89
90 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
91 if (!pFontMapper)
92 return false;
93
94 if (pFontMapper->m_InstalledTTFonts.empty())
95 pFontMapper->LoadInstalledFonts();
96
97 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
98 if (font.Compare(sFontFaceName.AsStringC()))
99 return true;
100 }
101
102 return false;
103}
104
105CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
106 CPDF_Document* pDoc,
107 CFX_ByteString sFontFaceName,
108 uint8_t nCharset) {
109 if (!pDoc)
110 return nullptr;
111
thestig907a5222016-06-21 14:38:27 -0700112 std::unique_ptr<CFX_Font> pFXFont(new CFX_Font);
dsinclairb9590102016-04-27 06:38:59 -0700113 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset), FALSE);
thestig907a5222016-06-21 14:38:27 -0700114 return pDoc->AddFont(pFXFont.get(), nCharset, FALSE);
dsinclairb9590102016-04-27 06:38:59 -0700115}
116
117int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
118 TimerCallback lpTimerFunc) {
119 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
120}
121
122void CFX_SystemHandler::KillTimer(int32_t nID) {
123 m_pEnv->FFI_KillTimer(nID);
124}
125
126FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
127 return m_pEnv->FFI_GetLocalTime();
128}
129
130bool CFX_SystemHandler::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair224bc5a2016-04-28 11:04:51 -0700131 return !!m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700132}
133
134bool CFX_SystemHandler::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair224bc5a2016-04-28 11:04:51 -0700135 return !!m_pEnv->FFI_IsCTRLKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700136}
137
138bool CFX_SystemHandler::IsALTKeyDown(uint32_t nFlag) const {
dsinclair224bc5a2016-04-28 11:04:51 -0700139 return !!m_pEnv->FFI_IsALTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700140}