blob: b6dc19d48f29e87274d60f61de1da0baaa61dca7 [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
dsinclair488b7ad2016-10-04 11:55:50 -070011#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair74a34fc2016-09-29 16:41:42 -070012#include "core/fxge/cfx_fontmapper.h"
13#include "core/fxge/cfx_fontmgr.h"
14#include "core/fxge/cfx_gemodule.h"
dsinclair114e46a2016-09-29 17:18:21 -070015#include "fpdfsdk/cpdfsdk_annot.h"
dsinclair735606d2016-10-05 15:47:02 -070016#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/cpdfsdk_pageview.h"
18#include "fpdfsdk/cpdfsdk_widget.h"
dsinclairb9590102016-04-27 06:38:59 -070019#include "fpdfsdk/formfiller/cffl_formfiller.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;
npmea3c3be2016-09-19 07:24:33 -070028 if (charset == FXFONT_HANGUL_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
dsinclair8faac622016-09-15 12:41:50 -070037void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget, FX_RECT rect) {
38 CPDFSDK_PageView* pPageView = widget->GetPageView();
39 UnderlyingPageType* pPage = widget->GetUnderlyingPage();
dsinclairb9590102016-04-27 06:38:59 -070040 if (!pPage || !pPageView)
41 return;
42
43 CFX_Matrix page2device;
44 pPageView->GetCurrentMatrix(page2device);
dsinclair8faac622016-09-15 12:41:50 -070045
dsinclairb9590102016-04-27 06:38:59 -070046 CFX_Matrix device2page;
47 device2page.SetReverse(page2device);
dsinclair8faac622016-09-15 12:41:50 -070048
Dan Sinclair1f403ce2017-02-21 12:56:24 -050049 CFX_PointF left_top = device2page.Transform(CFX_PointF(
50 static_cast<FX_FLOAT>(rect.left), static_cast<FX_FLOAT>(rect.top)));
51 CFX_PointF right_bottom = device2page.Transform(CFX_PointF(
52 static_cast<FX_FLOAT>(rect.right), static_cast<FX_FLOAT>(rect.bottom)));
Dan Sinclairafb44562017-02-09 13:07:43 -050053
Dan Sinclair1f403ce2017-02-21 12:56:24 -050054 CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y);
dsinclairb9590102016-04-27 06:38:59 -070055 rcPDF.Normalize();
Dan Sinclair6eec1c42017-02-21 17:20:43 -050056 m_pFormFillEnv->Invalidate(pPage, rcPDF.ToFxRect());
dsinclairb9590102016-04-27 06:38:59 -070057}
58
dsinclair8faac622016-09-15 12:41:50 -070059void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller,
dsinclairb9590102016-04-27 06:38:59 -070060 CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070061 if (!pFormFiller)
dsinclairb9590102016-04-27 06:38:59 -070062 return;
63
Dan Sinclairf528eee2017-02-14 11:52:07 -050064 CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
65 CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
dsinclair8faac622016-09-15 12:41:50 -070066
67 CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
dsinclairb9590102016-04-27 06:38:59 -070068 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
69 ASSERT(pPage);
70
Dan Sinclair60fd9fc2017-02-21 17:21:08 -050071 m_pFormFillEnv->OutputSelectedRect(pPage,
72 CFX_FloatRect(ptA.x, ptA.y, ptB.x, ptB.y));
dsinclairb9590102016-04-27 06:38:59 -070073}
74
75bool CFX_SystemHandler::IsSelectionImplemented() const {
dsinclair8779fa82016-10-12 12:05:44 -070076 if (!m_pFormFillEnv)
dsinclair8faac622016-09-15 12:41:50 -070077 return false;
78
dsinclair8779fa82016-10-12 12:05:44 -070079 FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo();
dsinclair8faac622016-09-15 12:41:50 -070080 return pInfo && pInfo->FFI_OutputSelectedRect;
81}
82
83void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
dsinclair8779fa82016-10-12 12:05:44 -070084 m_pFormFillEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070085}
86
thestig907a5222016-06-21 14:38:27 -070087bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070088 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
89 if (!pFontMgr)
90 return false;
91
92 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
93 if (!pFontMapper)
94 return false;
95
96 if (pFontMapper->m_InstalledTTFonts.empty())
97 pFontMapper->LoadInstalledFonts();
98
99 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
100 if (font.Compare(sFontFaceName.AsStringC()))
101 return true;
102 }
npm065c3502016-10-06 12:29:09 -0700103 for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
104 if (fontPair.first.Compare(sFontFaceName.AsStringC()))
105 return true;
106 }
dsinclairb9590102016-04-27 06:38:59 -0700107 return false;
108}
109
110CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
111 CPDF_Document* pDoc,
112 CFX_ByteString sFontFaceName,
113 uint8_t nCharset) {
114 if (!pDoc)
115 return nullptr;
116
thestig907a5222016-06-21 14:38:27 -0700117 std::unique_ptr<CFX_Font> pFXFont(new CFX_Font);
tsepez4cf55152016-11-02 14:37:54 -0700118 pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, CharSet2CP(nCharset), false);
119 return pDoc->AddFont(pFXFont.get(), nCharset, false);
dsinclairb9590102016-04-27 06:38:59 -0700120}
121
122int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
123 TimerCallback lpTimerFunc) {
dsinclair8779fa82016-10-12 12:05:44 -0700124 return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700125}
126
127void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair8779fa82016-10-12 12:05:44 -0700128 m_pFormFillEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700129}
130
dsinclairb9590102016-04-27 06:38:59 -0700131bool CFX_SystemHandler::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair8779fa82016-10-12 12:05:44 -0700132 return !!m_pFormFillEnv->IsSHIFTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700133}
134
135bool CFX_SystemHandler::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair8779fa82016-10-12 12:05:44 -0700136 return !!m_pFormFillEnv->IsCTRLKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700137}
138
139bool CFX_SystemHandler::IsALTKeyDown(uint32_t nFlag) const {
dsinclair8779fa82016-10-12 12:05:44 -0700140 return !!m_pFormFillEnv->IsALTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700141}