blob: 99aff8c6f800f8e0d778ea81f121c54cc7d8b26c [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"
Dan Sinclairdd17a142017-09-27 15:34:54 -040013#include "core/fxge/cfx_font.h"
dsinclair74a34fc2016-09-29 16:41:42 -070014#include "core/fxge/cfx_fontmapper.h"
15#include "core/fxge/cfx_fontmgr.h"
16#include "core/fxge/cfx_gemodule.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/cpdfsdk_annot.h"
dsinclair735606d2016-10-05 15:47:02 -070018#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/cpdfsdk_pageview.h"
20#include "fpdfsdk/cpdfsdk_widget.h"
dsinclairb9590102016-04-27 06:38:59 -070021#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclairb9590102016-04-27 06:38:59 -070022
Tom Sepez940967d2017-05-18 12:32:20 -070023CFX_SystemHandler::CFX_SystemHandler(CPDFSDK_FormFillEnvironment* pFormFillEnv)
24 : m_pFormFillEnv(pFormFillEnv) {}
25
26CFX_SystemHandler::~CFX_SystemHandler() {}
27
Lei Zhang77f9bff2017-08-29 11:34:12 -070028void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget,
29 const CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070030 CPDFSDK_PageView* pPageView = widget->GetPageView();
Tom Sepez101535f2018-06-12 13:36:05 +000031 IPDF_Page* pPage = widget->GetPage();
dsinclairb9590102016-04-27 06:38:59 -070032 if (!pPage || !pPageView)
33 return;
34
35 CFX_Matrix page2device;
36 pPageView->GetCurrentMatrix(page2device);
dsinclair8faac622016-09-15 12:41:50 -070037
Nicolas Penab21f1742017-06-29 12:02:06 -040038 CFX_Matrix device2page = page2device.GetInverse();
dsinclair8faac622016-09-15 12:41:50 -070039
Lei Zhang77f9bff2017-08-29 11:34:12 -070040 CFX_PointF left_top = device2page.Transform(CFX_PointF(rect.left, rect.top));
41 CFX_PointF right_bottom =
42 device2page.Transform(CFX_PointF(rect.right, rect.bottom));
Dan Sinclairafb44562017-02-09 13:07:43 -050043
Dan Sinclair1f403ce2017-02-21 12:56:24 -050044 CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y);
dsinclairb9590102016-04-27 06:38:59 -070045 rcPDF.Normalize();
Lei Zhang77f9bff2017-08-29 11:34:12 -070046 m_pFormFillEnv->Invalidate(pPage, rcPDF.GetOuterRect());
dsinclairb9590102016-04-27 06:38:59 -070047}
48
dsinclair8faac622016-09-15 12:41:50 -070049void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller,
dsinclairb9590102016-04-27 06:38:59 -070050 CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070051 if (!pFormFiller)
dsinclairb9590102016-04-27 06:38:59 -070052 return;
53
Dan Sinclairf528eee2017-02-14 11:52:07 -050054 CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
55 CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
dsinclair8faac622016-09-15 12:41:50 -070056
57 CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
Tom Sepez101535f2018-06-12 13:36:05 +000058 IPDF_Page* pPage = pAnnot->GetPage();
dsinclairb9590102016-04-27 06:38:59 -070059 ASSERT(pPage);
60
Dan Sinclair60fd9fc2017-02-21 17:21:08 -050061 m_pFormFillEnv->OutputSelectedRect(pPage,
62 CFX_FloatRect(ptA.x, ptA.y, ptB.x, ptB.y));
dsinclairb9590102016-04-27 06:38:59 -070063}
64
65bool CFX_SystemHandler::IsSelectionImplemented() const {
dsinclair8779fa82016-10-12 12:05:44 -070066 if (!m_pFormFillEnv)
dsinclair8faac622016-09-15 12:41:50 -070067 return false;
68
dsinclair8779fa82016-10-12 12:05:44 -070069 FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo();
dsinclair8faac622016-09-15 12:41:50 -070070 return pInfo && pInfo->FFI_OutputSelectedRect;
71}
72
73void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
dsinclair8779fa82016-10-12 12:05:44 -070074 m_pFormFillEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070075}
76
Ryan Harrison275e2602017-09-18 14:23:18 -040077bool CFX_SystemHandler::FindNativeTrueTypeFont(ByteString sFontFaceName) {
dsinclairb9590102016-04-27 06:38:59 -070078 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
79 if (!pFontMgr)
80 return false;
81
82 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
83 if (!pFontMapper)
84 return false;
85
Ryan Harrison1118a662018-05-31 19:26:52 +000086 pFontMapper->LoadInstalledFonts();
dsinclairb9590102016-04-27 06:38:59 -070087
88 for (const auto& font : pFontMapper->m_InstalledTTFonts) {
Ryan Harrison275e2602017-09-18 14:23:18 -040089 if (font.Compare(sFontFaceName.AsStringView()))
dsinclairb9590102016-04-27 06:38:59 -070090 return true;
91 }
npm065c3502016-10-06 12:29:09 -070092 for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
Ryan Harrison275e2602017-09-18 14:23:18 -040093 if (fontPair.first.Compare(sFontFaceName.AsStringView()))
npm065c3502016-10-06 12:29:09 -070094 return true;
95 }
dsinclairb9590102016-04-27 06:38:59 -070096 return false;
97}
98
99CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
100 CPDF_Document* pDoc,
Ryan Harrison275e2602017-09-18 14:23:18 -0400101 ByteString sFontFaceName,
dsinclairb9590102016-04-27 06:38:59 -0700102 uint8_t nCharset) {
103 if (!pDoc)
104 return nullptr;
105
Dan Sinclair0bb13332017-03-30 16:12:02 -0400106 auto pFXFont = pdfium::MakeUnique<CFX_Font>();
Artem Strygin656eb842018-05-31 14:08:11 +0000107 pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0,
108 FX_GetCodePageFromCharset(nCharset), false);
tsepez4cf55152016-11-02 14:37:54 -0700109 return pDoc->AddFont(pFXFont.get(), nCharset, false);
dsinclairb9590102016-04-27 06:38:59 -0700110}
111
112int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
113 TimerCallback lpTimerFunc) {
dsinclair8779fa82016-10-12 12:05:44 -0700114 return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700115}
116
117void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair8779fa82016-10-12 12:05:44 -0700118 m_pFormFillEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700119}