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" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 12 | #include "core/fxge/cfx_fontmapper.h" |
| 13 | #include "core/fxge/cfx_fontmgr.h" |
| 14 | #include "core/fxge/cfx_gemodule.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 15 | #include "fpdfsdk/cpdfsdk_annot.h" |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 16 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 17 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 18 | #include "fpdfsdk/cpdfsdk_widget.h" |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
| 23 | int CharSet2CP(int charset) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 24 | if (charset == FXFONT_SHIFTJIS_CHARSET) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 25 | return 932; |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 26 | if (charset == FXFONT_GB2312_CHARSET) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 27 | return 936; |
npm | ea3c3be | 2016-09-19 07:24:33 -0700 | [diff] [blame] | 28 | if (charset == FXFONT_HANGUL_CHARSET) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 29 | return 949; |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 30 | if (charset == FXFONT_CHINESEBIG5_CHARSET) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 31 | return 950; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 37 | void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget, FX_RECT rect) { |
| 38 | CPDFSDK_PageView* pPageView = widget->GetPageView(); |
| 39 | UnderlyingPageType* pPage = widget->GetUnderlyingPage(); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 40 | if (!pPage || !pPageView) |
| 41 | return; |
| 42 | |
| 43 | CFX_Matrix page2device; |
| 44 | pPageView->GetCurrentMatrix(page2device); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 45 | |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 46 | CFX_Matrix device2page; |
| 47 | device2page.SetReverse(page2device); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 48 | |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 49 | 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 Sinclair | afb4456 | 2017-02-09 13:07:43 -0500 | [diff] [blame] | 53 | |
Dan Sinclair | 1f403ce | 2017-02-21 12:56:24 -0500 | [diff] [blame] | 54 | 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] | 55 | rcPDF.Normalize(); |
| 56 | |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 57 | m_pFormFillEnv->Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right, |
| 58 | rcPDF.bottom); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 59 | } |
| 60 | |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 61 | void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller, |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 62 | CFX_FloatRect& rect) { |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 63 | if (!pFormFiller) |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 64 | return; |
| 65 | |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 66 | CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom)); |
| 67 | CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top)); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 68 | |
| 69 | CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot(); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 70 | UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage(); |
| 71 | ASSERT(pPage); |
| 72 | |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 73 | m_pFormFillEnv->OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool CFX_SystemHandler::IsSelectionImplemented() const { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 77 | if (!m_pFormFillEnv) |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 78 | return false; |
| 79 | |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 80 | FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo(); |
dsinclair | 8faac62 | 2016-09-15 12:41:50 -0700 | [diff] [blame] | 81 | return pInfo && pInfo->FFI_OutputSelectedRect; |
| 82 | } |
| 83 | |
| 84 | void CFX_SystemHandler::SetCursor(int32_t nCursorType) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 85 | m_pFormFillEnv->SetCursor(nCursorType); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 86 | } |
| 87 | |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 88 | bool CFX_SystemHandler::FindNativeTrueTypeFont(CFX_ByteString sFontFaceName) { |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 89 | 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 | } |
npm | 065c350 | 2016-10-06 12:29:09 -0700 | [diff] [blame] | 104 | for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) { |
| 105 | if (fontPair.first.Compare(sFontFaceName.AsStringC())) |
| 106 | return true; |
| 107 | } |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF( |
| 112 | CPDF_Document* pDoc, |
| 113 | CFX_ByteString sFontFaceName, |
| 114 | uint8_t nCharset) { |
| 115 | if (!pDoc) |
| 116 | return nullptr; |
| 117 | |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 118 | std::unique_ptr<CFX_Font> pFXFont(new CFX_Font); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 119 | pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, CharSet2CP(nCharset), false); |
| 120 | return pDoc->AddFont(pFXFont.get(), nCharset, false); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int32_t CFX_SystemHandler::SetTimer(int32_t uElapse, |
| 124 | TimerCallback lpTimerFunc) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 125 | return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void CFX_SystemHandler::KillTimer(int32_t nID) { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 129 | m_pFormFillEnv->KillTimer(nID); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 132 | bool CFX_SystemHandler::IsSHIFTKeyDown(uint32_t nFlag) const { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 133 | return !!m_pFormFillEnv->IsSHIFTKeyDown(nFlag); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool CFX_SystemHandler::IsCTRLKeyDown(uint32_t nFlag) const { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 137 | return !!m_pFormFillEnv->IsCTRLKeyDown(nFlag); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | bool CFX_SystemHandler::IsALTKeyDown(uint32_t nFlag) const { |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 141 | return !!m_pFormFillEnv->IsALTKeyDown(nFlag); |
dsinclair | b959010 | 2016-04-27 06:38:59 -0700 | [diff] [blame] | 142 | } |