blob: 08d9743993691f6e4cf12df74a00d87f302f7311 [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"
dsinclair8faac622016-09-15 12:41:50 -070020#include "fpdfsdk/include/cpdfsdk_widget.h"
dsinclairb9590102016-04-27 06:38:59 -070021
22namespace {
23
24int CharSet2CP(int charset) {
thestig907a5222016-06-21 14:38:27 -070025 if (charset == FXFONT_SHIFTJIS_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070026 return 932;
thestig907a5222016-06-21 14:38:27 -070027 if (charset == FXFONT_GB2312_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070028 return 936;
thestig907a5222016-06-21 14:38:27 -070029 if (charset == FXFONT_HANGEUL_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070030 return 949;
thestig907a5222016-06-21 14:38:27 -070031 if (charset == FXFONT_CHINESEBIG5_CHARSET)
dsinclairb9590102016-04-27 06:38:59 -070032 return 950;
33 return 0;
34}
35
36} // namespace
37
dsinclair8faac622016-09-15 12:41:50 -070038void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget, FX_RECT rect) {
39 CPDFSDK_PageView* pPageView = widget->GetPageView();
40 UnderlyingPageType* pPage = widget->GetUnderlyingPage();
dsinclairb9590102016-04-27 06:38:59 -070041 if (!pPage || !pPageView)
42 return;
43
44 CFX_Matrix page2device;
45 pPageView->GetCurrentMatrix(page2device);
dsinclair8faac622016-09-15 12:41:50 -070046
dsinclairb9590102016-04-27 06:38:59 -070047 CFX_Matrix device2page;
48 device2page.SetReverse(page2device);
dsinclair8faac622016-09-15 12:41:50 -070049
50 FX_FLOAT left;
51 FX_FLOAT top;
52 FX_FLOAT right;
53 FX_FLOAT bottom;
54 device2page.Transform(static_cast<FX_FLOAT>(rect.left),
55 static_cast<FX_FLOAT>(rect.top), left, top);
56 device2page.Transform(static_cast<FX_FLOAT>(rect.right),
57 static_cast<FX_FLOAT>(rect.bottom), right, bottom);
dsinclairb9590102016-04-27 06:38:59 -070058 CFX_FloatRect rcPDF(left, bottom, right, top);
59 rcPDF.Normalize();
60
dsinclair1f248902016-09-14 10:38:17 -070061 m_pEnv->Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right, rcPDF.bottom);
dsinclairb9590102016-04-27 06:38:59 -070062}
63
dsinclair8faac622016-09-15 12:41:50 -070064void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller,
dsinclairb9590102016-04-27 06:38:59 -070065 CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070066 if (!pFormFiller)
dsinclairb9590102016-04-27 06:38:59 -070067 return;
68
69 CFX_FloatPoint leftbottom = CFX_FloatPoint(rect.left, rect.bottom);
70 CFX_FloatPoint righttop = CFX_FloatPoint(rect.right, rect.top);
dsinclair8faac622016-09-15 12:41:50 -070071 CFX_FloatPoint ptA = pFormFiller->PWLtoFFL(leftbottom);
72 CFX_FloatPoint ptB = pFormFiller->PWLtoFFL(righttop);
73
74 CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
dsinclairb9590102016-04-27 06:38:59 -070075 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
76 ASSERT(pPage);
77
dsinclair1f248902016-09-14 10:38:17 -070078 m_pEnv->OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
dsinclairb9590102016-04-27 06:38:59 -070079}
80
81bool CFX_SystemHandler::IsSelectionImplemented() const {
dsinclair8faac622016-09-15 12:41:50 -070082 if (!m_pEnv)
83 return false;
84
85 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
86 return pInfo && pInfo->FFI_OutputSelectedRect;
87}
88
89void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
90 m_pEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070091}
92
thestig907a5222016-06-21 14:38:27 -070093bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070094 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
95 if (!pFontMgr)
96 return false;
97
98 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
99 if (!pFontMapper)
100 return false;
101
102 if (pFontMapper->m_InstalledTTFonts.empty())
103 pFontMapper->LoadInstalledFonts();
104
105 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
106 if (font.Compare(sFontFaceName.AsStringC()))
107 return true;
108 }
dsinclairb9590102016-04-27 06:38:59 -0700109 return false;
110}
111
112CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
113 CPDF_Document* pDoc,
114 CFX_ByteString sFontFaceName,
115 uint8_t nCharset) {
116 if (!pDoc)
117 return nullptr;
118
thestig907a5222016-06-21 14:38:27 -0700119 std::unique_ptr<CFX_Font> pFXFont(new CFX_Font);
dsinclairb9590102016-04-27 06:38:59 -0700120 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset), FALSE);
thestig907a5222016-06-21 14:38:27 -0700121 return pDoc->AddFont(pFXFont.get(), nCharset, FALSE);
dsinclairb9590102016-04-27 06:38:59 -0700122}
123
124int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
125 TimerCallback lpTimerFunc) {
dsinclair1f248902016-09-14 10:38:17 -0700126 return m_pEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700127}
128
129void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair1f248902016-09-14 10:38:17 -0700130 m_pEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700131}
132
dsinclairb9590102016-04-27 06:38:59 -0700133bool 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}