blob: 6281b1be91670c5c42156c578ec1549ab102262a [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"
Dan Sinclairf51a02a2017-04-19 12:46:53 -040012#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070013#include "core/fxge/cfx_fontmapper.h"
14#include "core/fxge/cfx_fontmgr.h"
15#include "core/fxge/cfx_gemodule.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/cpdfsdk_annot.h"
dsinclair735606d2016-10-05 15:47:02 -070017#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_pageview.h"
19#include "fpdfsdk/cpdfsdk_widget.h"
dsinclairb9590102016-04-27 06:38:59 -070020#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclairb9590102016-04-27 06:38:59 -070021
22namespace {
23
24int CharSet2CP(int charset) {
Dan Sinclairf51a02a2017-04-19 12:46:53 -040025 if (charset == FX_CHARSET_ShiftJIS)
26 return FX_CODEPAGE_ShiftJIS;
27 if (charset == FX_CHARSET_ChineseSimplified)
28 return FX_CODEPAGE_ChineseSimplified;
29 if (charset == FX_CHARSET_Hangul)
30 return FX_CODEPAGE_Hangul;
31 if (charset == FX_CHARSET_ChineseTraditional)
32 return FX_CODEPAGE_ChineseTraditional;
33 return FX_CODEPAGE_DefANSI;
dsinclairb9590102016-04-27 06:38:59 -070034}
35
36} // namespace
37
Tom Sepez940967d2017-05-18 12:32:20 -070038CFX_SystemHandler::CFX_SystemHandler(CPDFSDK_FormFillEnvironment* pFormFillEnv)
39 : m_pFormFillEnv(pFormFillEnv) {}
40
41CFX_SystemHandler::~CFX_SystemHandler() {}
42
dsinclair8faac622016-09-15 12:41:50 -070043void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget, FX_RECT rect) {
44 CPDFSDK_PageView* pPageView = widget->GetPageView();
45 UnderlyingPageType* pPage = widget->GetUnderlyingPage();
dsinclairb9590102016-04-27 06:38:59 -070046 if (!pPage || !pPageView)
47 return;
48
49 CFX_Matrix page2device;
50 pPageView->GetCurrentMatrix(page2device);
dsinclair8faac622016-09-15 12:41:50 -070051
Nicolas Penab21f1742017-06-29 12:02:06 -040052 CFX_Matrix device2page = page2device.GetInverse();
dsinclair8faac622016-09-15 12:41:50 -070053
Dan Sinclair05df0752017-03-14 14:43:42 -040054 CFX_PointF left_top = device2page.Transform(
55 CFX_PointF(static_cast<float>(rect.left), static_cast<float>(rect.top)));
Dan Sinclair1f403ce2017-02-21 12:56:24 -050056 CFX_PointF right_bottom = device2page.Transform(CFX_PointF(
Dan Sinclair05df0752017-03-14 14:43:42 -040057 static_cast<float>(rect.right), static_cast<float>(rect.bottom)));
Dan Sinclairafb44562017-02-09 13:07:43 -050058
Dan Sinclair1f403ce2017-02-21 12:56:24 -050059 CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y);
dsinclairb9590102016-04-27 06:38:59 -070060 rcPDF.Normalize();
Dan Sinclair6eec1c42017-02-21 17:20:43 -050061 m_pFormFillEnv->Invalidate(pPage, rcPDF.ToFxRect());
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
Dan Sinclairf528eee2017-02-14 11:52:07 -050069 CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
70 CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
dsinclair8faac622016-09-15 12:41:50 -070071
72 CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
dsinclairb9590102016-04-27 06:38:59 -070073 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
74 ASSERT(pPage);
75
Dan Sinclair60fd9fc2017-02-21 17:21:08 -050076 m_pFormFillEnv->OutputSelectedRect(pPage,
77 CFX_FloatRect(ptA.x, ptA.y, ptB.x, ptB.y));
dsinclairb9590102016-04-27 06:38:59 -070078}
79
80bool CFX_SystemHandler::IsSelectionImplemented() const {
dsinclair8779fa82016-10-12 12:05:44 -070081 if (!m_pFormFillEnv)
dsinclair8faac622016-09-15 12:41:50 -070082 return false;
83
dsinclair8779fa82016-10-12 12:05:44 -070084 FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo();
dsinclair8faac622016-09-15 12:41:50 -070085 return pInfo && pInfo->FFI_OutputSelectedRect;
86}
87
88void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
dsinclair8779fa82016-10-12 12:05:44 -070089 m_pFormFillEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070090}
91
thestig907a5222016-06-21 14:38:27 -070092bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070093 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
94 if (!pFontMgr)
95 return false;
96
97 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
98 if (!pFontMapper)
99 return false;
100
101 if (pFontMapper->m_InstalledTTFonts.empty())
102 pFontMapper->LoadInstalledFonts();
103
104 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
105 if (font.Compare(sFontFaceName.AsStringC()))
106 return true;
107 }
npm065c3502016-10-06 12:29:09 -0700108 for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
109 if (fontPair.first.Compare(sFontFaceName.AsStringC()))
110 return true;
111 }
dsinclairb9590102016-04-27 06:38:59 -0700112 return false;
113}
114
115CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
116 CPDF_Document* pDoc,
117 CFX_ByteString sFontFaceName,
118 uint8_t nCharset) {
119 if (!pDoc)
120 return nullptr;
121
Dan Sinclair0bb13332017-03-30 16:12:02 -0400122 auto pFXFont = pdfium::MakeUnique<CFX_Font>();
tsepez4cf55152016-11-02 14:37:54 -0700123 pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, CharSet2CP(nCharset), false);
124 return pDoc->AddFont(pFXFont.get(), nCharset, false);
dsinclairb9590102016-04-27 06:38:59 -0700125}
126
127int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
128 TimerCallback lpTimerFunc) {
dsinclair8779fa82016-10-12 12:05:44 -0700129 return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700130}
131
132void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair8779fa82016-10-12 12:05:44 -0700133 m_pFormFillEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700134}