blob: 8ccbd75fbf5ba534dbdd8fd6aaf29249e560521d [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();
56
dsinclair8779fa82016-10-12 12:05:44 -070057 m_pFormFillEnv->Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
58 rcPDF.bottom);
dsinclairb9590102016-04-27 06:38:59 -070059}
60
dsinclair8faac622016-09-15 12:41:50 -070061void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller,
dsinclairb9590102016-04-27 06:38:59 -070062 CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070063 if (!pFormFiller)
dsinclairb9590102016-04-27 06:38:59 -070064 return;
65
Dan Sinclairf528eee2017-02-14 11:52:07 -050066 CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
67 CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
dsinclair8faac622016-09-15 12:41:50 -070068
69 CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
dsinclairb9590102016-04-27 06:38:59 -070070 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
71 ASSERT(pPage);
72
dsinclair8779fa82016-10-12 12:05:44 -070073 m_pFormFillEnv->OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
dsinclairb9590102016-04-27 06:38:59 -070074}
75
76bool CFX_SystemHandler::IsSelectionImplemented() const {
dsinclair8779fa82016-10-12 12:05:44 -070077 if (!m_pFormFillEnv)
dsinclair8faac622016-09-15 12:41:50 -070078 return false;
79
dsinclair8779fa82016-10-12 12:05:44 -070080 FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo();
dsinclair8faac622016-09-15 12:41:50 -070081 return pInfo && pInfo->FFI_OutputSelectedRect;
82}
83
84void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
dsinclair8779fa82016-10-12 12:05:44 -070085 m_pFormFillEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070086}
87
thestig907a5222016-06-21 14:38:27 -070088bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070089 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
90 if (!pFontMgr)
91 return false;
92
93 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
94 if (!pFontMapper)
95 return false;
96
97 if (pFontMapper->m_InstalledTTFonts.empty())
98 pFontMapper->LoadInstalledFonts();
99
100 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
101 if (font.Compare(sFontFaceName.AsStringC()))
102 return true;
103 }
npm065c3502016-10-06 12:29:09 -0700104 for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
105 if (fontPair.first.Compare(sFontFaceName.AsStringC()))
106 return true;
107 }
dsinclairb9590102016-04-27 06:38:59 -0700108 return false;
109}
110
111CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
112 CPDF_Document* pDoc,
113 CFX_ByteString sFontFaceName,
114 uint8_t nCharset) {
115 if (!pDoc)
116 return nullptr;
117
thestig907a5222016-06-21 14:38:27 -0700118 std::unique_ptr<CFX_Font> pFXFont(new CFX_Font);
tsepez4cf55152016-11-02 14:37:54 -0700119 pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, CharSet2CP(nCharset), false);
120 return pDoc->AddFont(pFXFont.get(), nCharset, false);
dsinclairb9590102016-04-27 06:38:59 -0700121}
122
123int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
124 TimerCallback lpTimerFunc) {
dsinclair8779fa82016-10-12 12:05:44 -0700125 return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700126}
127
128void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair8779fa82016-10-12 12:05:44 -0700129 m_pFormFillEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700130}
131
dsinclairb9590102016-04-27 06:38:59 -0700132bool CFX_SystemHandler::IsSHIFTKeyDown(uint32_t nFlag) const {
dsinclair8779fa82016-10-12 12:05:44 -0700133 return !!m_pFormFillEnv->IsSHIFTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700134}
135
136bool CFX_SystemHandler::IsCTRLKeyDown(uint32_t nFlag) const {
dsinclair8779fa82016-10-12 12:05:44 -0700137 return !!m_pFormFillEnv->IsCTRLKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700138}
139
140bool CFX_SystemHandler::IsALTKeyDown(uint32_t nFlag) const {
dsinclair8779fa82016-10-12 12:05:44 -0700141 return !!m_pFormFillEnv->IsALTKeyDown(nFlag);
dsinclairb9590102016-04-27 06:38:59 -0700142}