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