blob: bbb029319c2a0b094bc4d9b248639858b500a387 [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
23namespace {
24
25int CharSet2CP(int charset) {
Dan Sinclairf51a02a2017-04-19 12:46:53 -040026 if (charset == FX_CHARSET_ShiftJIS)
27 return FX_CODEPAGE_ShiftJIS;
28 if (charset == FX_CHARSET_ChineseSimplified)
29 return FX_CODEPAGE_ChineseSimplified;
30 if (charset == FX_CHARSET_Hangul)
31 return FX_CODEPAGE_Hangul;
32 if (charset == FX_CHARSET_ChineseTraditional)
33 return FX_CODEPAGE_ChineseTraditional;
34 return FX_CODEPAGE_DefANSI;
dsinclairb9590102016-04-27 06:38:59 -070035}
36
37} // namespace
38
Tom Sepez940967d2017-05-18 12:32:20 -070039CFX_SystemHandler::CFX_SystemHandler(CPDFSDK_FormFillEnvironment* pFormFillEnv)
40 : m_pFormFillEnv(pFormFillEnv) {}
41
42CFX_SystemHandler::~CFX_SystemHandler() {}
43
Lei Zhang77f9bff2017-08-29 11:34:12 -070044void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget,
45 const CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070046 CPDFSDK_PageView* pPageView = widget->GetPageView();
47 UnderlyingPageType* pPage = widget->GetUnderlyingPage();
dsinclairb9590102016-04-27 06:38:59 -070048 if (!pPage || !pPageView)
49 return;
50
51 CFX_Matrix page2device;
52 pPageView->GetCurrentMatrix(page2device);
dsinclair8faac622016-09-15 12:41:50 -070053
Nicolas Penab21f1742017-06-29 12:02:06 -040054 CFX_Matrix device2page = page2device.GetInverse();
dsinclair8faac622016-09-15 12:41:50 -070055
Lei Zhang77f9bff2017-08-29 11:34:12 -070056 CFX_PointF left_top = device2page.Transform(CFX_PointF(rect.left, rect.top));
57 CFX_PointF right_bottom =
58 device2page.Transform(CFX_PointF(rect.right, rect.bottom));
Dan Sinclairafb44562017-02-09 13:07:43 -050059
Dan Sinclair1f403ce2017-02-21 12:56:24 -050060 CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y);
dsinclairb9590102016-04-27 06:38:59 -070061 rcPDF.Normalize();
Lei Zhang77f9bff2017-08-29 11:34:12 -070062 m_pFormFillEnv->Invalidate(pPage, rcPDF.GetOuterRect());
dsinclairb9590102016-04-27 06:38:59 -070063}
64
dsinclair8faac622016-09-15 12:41:50 -070065void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller,
dsinclairb9590102016-04-27 06:38:59 -070066 CFX_FloatRect& rect) {
dsinclair8faac622016-09-15 12:41:50 -070067 if (!pFormFiller)
dsinclairb9590102016-04-27 06:38:59 -070068 return;
69
Dan Sinclairf528eee2017-02-14 11:52:07 -050070 CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
71 CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
dsinclair8faac622016-09-15 12:41:50 -070072
73 CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
dsinclairb9590102016-04-27 06:38:59 -070074 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
75 ASSERT(pPage);
76
Dan Sinclair60fd9fc2017-02-21 17:21:08 -050077 m_pFormFillEnv->OutputSelectedRect(pPage,
78 CFX_FloatRect(ptA.x, ptA.y, ptB.x, ptB.y));
dsinclairb9590102016-04-27 06:38:59 -070079}
80
81bool CFX_SystemHandler::IsSelectionImplemented() const {
dsinclair8779fa82016-10-12 12:05:44 -070082 if (!m_pFormFillEnv)
dsinclair8faac622016-09-15 12:41:50 -070083 return false;
84
dsinclair8779fa82016-10-12 12:05:44 -070085 FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo();
dsinclair8faac622016-09-15 12:41:50 -070086 return pInfo && pInfo->FFI_OutputSelectedRect;
87}
88
89void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
dsinclair8779fa82016-10-12 12:05:44 -070090 m_pFormFillEnv->SetCursor(nCursorType);
dsinclairb9590102016-04-27 06:38:59 -070091}
92
Ryan Harrison275e2602017-09-18 14:23:18 -040093bool CFX_SystemHandler::FindNativeTrueTypeFont(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) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400106 if (font.Compare(sFontFaceName.AsStringView()))
dsinclairb9590102016-04-27 06:38:59 -0700107 return true;
108 }
npm065c3502016-10-06 12:29:09 -0700109 for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400110 if (fontPair.first.Compare(sFontFaceName.AsStringView()))
npm065c3502016-10-06 12:29:09 -0700111 return true;
112 }
dsinclairb9590102016-04-27 06:38:59 -0700113 return false;
114}
115
116CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
117 CPDF_Document* pDoc,
Ryan Harrison275e2602017-09-18 14:23:18 -0400118 ByteString sFontFaceName,
dsinclairb9590102016-04-27 06:38:59 -0700119 uint8_t nCharset) {
120 if (!pDoc)
121 return nullptr;
122
Dan Sinclair0bb13332017-03-30 16:12:02 -0400123 auto pFXFont = pdfium::MakeUnique<CFX_Font>();
tsepez4cf55152016-11-02 14:37:54 -0700124 pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, CharSet2CP(nCharset), false);
125 return pDoc->AddFont(pFXFont.get(), nCharset, false);
dsinclairb9590102016-04-27 06:38:59 -0700126}
127
128int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
129 TimerCallback lpTimerFunc) {
dsinclair8779fa82016-10-12 12:05:44 -0700130 return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc);
dsinclairb9590102016-04-27 06:38:59 -0700131}
132
133void CFX_SystemHandler::KillTimer(int32_t nID) {
dsinclair8779fa82016-10-12 12:05:44 -0700134 m_pFormFillEnv->KillTimer(nID);
dsinclairb9590102016-04-27 06:38:59 -0700135}