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