Home | History | Annotate | Download | only in fpdfsdk
      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 
      9 #include <memory>
     10 
     11 #include "core/fpdfapi/parser/cpdf_document.h"
     12 #include "core/fxcrt/fx_codepage.h"
     13 #include "core/fxge/cfx_font.h"
     14 #include "core/fxge/cfx_fontmapper.h"
     15 #include "core/fxge/cfx_fontmgr.h"
     16 #include "core/fxge/cfx_gemodule.h"
     17 #include "fpdfsdk/cpdfsdk_annot.h"
     18 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
     19 #include "fpdfsdk/cpdfsdk_pageview.h"
     20 #include "fpdfsdk/cpdfsdk_widget.h"
     21 #include "fpdfsdk/formfiller/cffl_formfiller.h"
     22 
     23 namespace {
     24 
     25 int CharSet2CP(int charset) {
     26   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;
     35 }
     36 
     37 }  // namespace
     38 
     39 CFX_SystemHandler::CFX_SystemHandler(CPDFSDK_FormFillEnvironment* pFormFillEnv)
     40     : m_pFormFillEnv(pFormFillEnv) {}
     41 
     42 CFX_SystemHandler::~CFX_SystemHandler() {}
     43 
     44 void CFX_SystemHandler::InvalidateRect(CPDFSDK_Widget* widget,
     45                                        const CFX_FloatRect& rect) {
     46   CPDFSDK_PageView* pPageView = widget->GetPageView();
     47   UnderlyingPageType* pPage = widget->GetUnderlyingPage();
     48   if (!pPage || !pPageView)
     49     return;
     50 
     51   CFX_Matrix page2device;
     52   pPageView->GetCurrentMatrix(page2device);
     53 
     54   CFX_Matrix device2page = page2device.GetInverse();
     55 
     56   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));
     59 
     60   CFX_FloatRect rcPDF(left_top.x, right_bottom.y, right_bottom.x, left_top.y);
     61   rcPDF.Normalize();
     62   m_pFormFillEnv->Invalidate(pPage, rcPDF.GetOuterRect());
     63 }
     64 
     65 void CFX_SystemHandler::OutputSelectedRect(CFFL_FormFiller* pFormFiller,
     66                                            CFX_FloatRect& rect) {
     67   if (!pFormFiller)
     68     return;
     69 
     70   CFX_PointF ptA = pFormFiller->PWLtoFFL(CFX_PointF(rect.left, rect.bottom));
     71   CFX_PointF ptB = pFormFiller->PWLtoFFL(CFX_PointF(rect.right, rect.top));
     72 
     73   CPDFSDK_Annot* pAnnot = pFormFiller->GetSDKAnnot();
     74   UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
     75   ASSERT(pPage);
     76 
     77   m_pFormFillEnv->OutputSelectedRect(pPage,
     78                                      CFX_FloatRect(ptA.x, ptA.y, ptB.x, ptB.y));
     79 }
     80 
     81 bool CFX_SystemHandler::IsSelectionImplemented() const {
     82   if (!m_pFormFillEnv)
     83     return false;
     84 
     85   FPDF_FORMFILLINFO* pInfo = m_pFormFillEnv->GetFormFillInfo();
     86   return pInfo && pInfo->FFI_OutputSelectedRect;
     87 }
     88 
     89 void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
     90   m_pFormFillEnv->SetCursor(nCursorType);
     91 }
     92 
     93 bool CFX_SystemHandler::FindNativeTrueTypeFont(ByteString sFontFaceName) {
     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.AsStringView()))
    107       return true;
    108   }
    109   for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
    110     if (fontPair.first.Compare(sFontFaceName.AsStringView()))
    111       return true;
    112   }
    113   return false;
    114 }
    115 
    116 CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
    117     CPDF_Document* pDoc,
    118     ByteString sFontFaceName,
    119     uint8_t nCharset) {
    120   if (!pDoc)
    121     return nullptr;
    122 
    123   auto pFXFont = pdfium::MakeUnique<CFX_Font>();
    124   pFXFont->LoadSubst(sFontFaceName, true, 0, 0, 0, CharSet2CP(nCharset), false);
    125   return pDoc->AddFont(pFXFont.get(), nCharset, false);
    126 }
    127 
    128 int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
    129                                     TimerCallback lpTimerFunc) {
    130   return m_pFormFillEnv->SetTimer(uElapse, lpTimerFunc);
    131 }
    132 
    133 void CFX_SystemHandler::KillTimer(int32_t nID) {
    134   m_pFormFillEnv->KillTimer(nID);
    135 }
    136