dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 1 | // 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 | |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_document.h" |
Dan Sinclair | f51a02a | 2017-04-19 12:46:53 -0400 | [diff] [blame] | 12 | #include "core/fxcrt/fx_codepage.h" |
Dan Sinclair | dd17a14 | 2017-09-27 15:34:54 -0400 | [diff] [blame] | 13 | #include "core/fxge/cfx_font.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 14 | #include "core/fxge/cfx_fontmapper.h" |
| 15 | #include "core/fxge/cfx_fontmgr.h" |
| 16 | #include "core/fxge/cfx_gemodule.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 17 | #include "fpdfsdk/cpdfsdk_annot.h" |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 18 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 20 | #include "fpdfsdk/cpdfsdk_widget.h" |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 21 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 22 | |
Tom Sepez | 940967d | 2017-05-18 12:32:20 -0700 | [diff] [blame] | 23 | CFX_SystemHandler::CFX_SystemHandler(CPDFSDK_FormFillEnvironment* pFormFillEnv) |
| 24 | : m_pFormFillEnv(pFormFillEnv) {} |
| 25 | |
| 26 | CFX_SystemHandler::~CFX_SystemHandler() {} |
| 27 | |
Lei Zhang | 77f9bff | 2017-08-29 11:34:12 -0700 | [diff] [blame] | 28 | void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget, |
| 29 | const CFX_FloatRect& rect) { |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 30 | CPDFSDK_PageView* pPageView = widget->GetPageView(); |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame^] | 31 | IPDF_Page* pPage = widget->GetPage(); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 32 | if (!pPage || !pPageView) |
| 33 | return; |
| 34 | |
| 35 | CFX_Matrix page2device; |
| 36 | pPageView->GetCurrentMatrix(page2device); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 37 | |
Nicolas Pena | b21f174 | 2017-06-29 12:02:06 -0400 | [diff] [blame] | 38 | CFX_Matrix device2page = page2device.GetInverse(); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 39 | |
Lei Zhang | 77f9bff | 2017-08-29 11:34:12 -0700 | [diff] [blame] | 40 | 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 Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 43 | |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 44 | CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 45 | rcPDF.Normalize(); |
Lei Zhang | 77f9bff | 2017-08-29 11:34:12 -0700 | [diff] [blame] | 46 | m_pFormFillEnv->Invalidate(pPage, rcPDF.GetOuterRect()); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 47 | } |
| 48 | |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 49 | void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller, |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 50 | CFX_FloatRect& rect) { |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 51 | if (!pFormFiller) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 52 | return; |
| 53 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 54 | CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom)); |
| 55 | CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top)); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 56 | |
| 57 | CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot(); |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame^] | 58 | IPDF_Page* pPage = pAnnot->GetPage(); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 59 | ASSERT(pPage); |
| 60 | |
Dan Sinclair | 60fd9fc | 2017-02-21 17:21:08 -0500 | [diff] [blame] | 61 | m_pFormFillEnv->OutputSelectedRect(pPage, |
| 62 | CFX_FloatRect(ptA.x, ptA.y, ptB.x, ptB.y)); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool CFX_SystemHandler::IsSelectionImplemented() const { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 66 | if (!m_pFormFillEnv) |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 67 | return false; |
| 68 | |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 69 | FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo(); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 70 | return pInfo && pInfo->FFI_OutputSelectedRect; |
| 71 | } |
| 72 | |
| 73 | void CFX_SystemHandler::SetCursor(int32_t nCursorType) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 74 | m_pFormFillEnv->SetCursor(nCursorType); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 77 | bool CFX_SystemHandler::FindNativeTrueTypeFont(ByteString sFontFaceName) { |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 78 | 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 Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 86 | pFontMapper->LoadInstalledFonts(); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 87 | |
| 88 | for (const auto& font : pFontMapper->m_InstalledTTFonts) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 89 | if (font.Compare(sFontFaceName.AsStringView())) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 90 | return true; |
| 91 | } |
npm | 065c350 | 2016-10-06 12:29:09 -0700 | [diff] [blame] | 92 | for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 93 | if (fontPair.first.Compare(sFontFaceName.AsStringView())) |
npm | 065c350 | 2016-10-06 12:29:09 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
| 99 | CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF( |
| 100 | CPDF_Document* pDoc, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 101 | ByteString sFontFaceName, |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 102 | uint8_t nCharset) { |
| 103 | if (!pDoc) |
| 104 | return nullptr; |
| 105 | |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 106 | auto pFXFont = pdfium::MakeUnique<CFX_Font>(); |
Artem Strygin | 656eb84 | 2018-05-31 14:08:11 +0000 | [diff] [blame] | 107 | pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, |
| 108 | FX_GetCodePageFromCharset(nCharset), false); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 109 | return pDoc->AddFont(pFXFont.get(), nCharset, false); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int32_t CFX_SystemHandler::SetTimer(int32_t uElapse, |
| 113 | TimerCallback lpTimerFunc) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 114 | return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void CFX_SystemHandler::KillTimer(int32_t nID) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 118 | m_pFormFillEnv->KillTimer(nID); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 119 | } |