Clean up CPDF_AnnotList.
[pdfium.git] / core / src / fpdfapi / fpdf_render / fpdf_render.cpp
index fba698c..7b37b1b 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../../include/fpdfapi/fpdf_render.h"\r
-#include "../../../include/fpdfapi/fpdf_module.h"\r
-#include "../fpdf_page/pageint.h"\r
-#include "../../../include/fxge/fx_ge.h"\r
-#include "../../../include/fxcodec/fx_codec.h"\r
-#include "render_int.h"\r
-CPDF_DocRenderData::CPDF_DocRenderData(CPDF_Document* pPDFDoc)\r
-    : m_pPDFDoc(pPDFDoc)\r
-    , m_pFontCache(NULL)\r
-{\r
-}\r
-CPDF_DocRenderData::~CPDF_DocRenderData()\r
-{\r
-    Clear(TRUE);\r
-}\r
-void CPDF_DocRenderData::Clear(FX_BOOL bRelease)\r
-{\r
-    FX_POSITION pos;\r
-    {\r
-        pos = m_Type3FaceMap.GetStartPosition();\r
-        while (pos) {\r
-            CPDF_Font* pFont;\r
-            CPDF_CountedObject<CPDF_Type3Cache*>* cache;\r
-            m_Type3FaceMap.GetNextAssoc(pos, pFont, cache);\r
-            if (bRelease || cache->m_nCount < 2) {\r
-                delete cache->m_Obj;\r
-                delete cache;\r
-                m_Type3FaceMap.RemoveKey(pFont);\r
-            }\r
-        }\r
-    }\r
-#ifndef _FPDFAPI_MINI_\r
-    {\r
-        pos = m_TransferFuncMap.GetStartPosition();\r
-        while (pos) {\r
-            CPDF_Object* key;\r
-            CPDF_CountedObject<CPDF_TransferFunc*>* value;\r
-            m_TransferFuncMap.GetNextAssoc(pos, key, value);\r
-            if (bRelease || value->m_nCount < 2) {\r
-                delete value->m_Obj;\r
-                delete value;\r
-                m_TransferFuncMap.RemoveKey(key);\r
-            }\r
-        }\r
-    }\r
-#endif\r
-    if (m_pFontCache) {\r
-        if (bRelease) {\r
-            delete m_pFontCache;\r
-            m_pFontCache = NULL;\r
-        } else {\r
-            m_pFontCache->FreeCache(FALSE);\r
-        }\r
-    }\r
-}\r
-FX_BOOL CPDF_DocRenderData::Initialize()\r
-{\r
-    m_pFontCache = FX_NEW CFX_FontCache;\r
-    return TRUE;\r
-}\r
-CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont)\r
-{\r
-    CPDF_CountedObject<CPDF_Type3Cache*>* pCache;\r
-    if (!m_Type3FaceMap.Lookup(pFont, pCache)) {\r
-        CPDF_Type3Cache* pType3 = FX_NEW CPDF_Type3Cache(pFont);\r
-        pCache = FX_NEW CPDF_CountedObject<CPDF_Type3Cache*>;\r
-        pCache->m_Obj = pType3;\r
-        pCache->m_nCount = 1;\r
-        m_Type3FaceMap.SetAt(pFont, pCache);\r
-    }\r
-    pCache->m_nCount++;\r
-    return pCache->m_Obj;\r
-}\r
-void CPDF_DocRenderData::ReleaseCachedType3(CPDF_Type3Font* pFont)\r
-{\r
-    CPDF_CountedObject<CPDF_Type3Cache*>* pCache;\r
-    if (!m_Type3FaceMap.Lookup(pFont, pCache)) {\r
-        return;\r
-    }\r
-    pCache->m_nCount--;\r
-}\r
-class CPDF_RenderModule : public CPDF_RenderModuleDef\r
-{\r
-public:\r
-    virtual ~CPDF_RenderModule() {}\r
-    virtual FX_BOOL    Installed()\r
-    {\r
-        return TRUE;\r
-    }\r
-    virtual CPDF_DocRenderData*        CreateDocData(CPDF_Document* pDoc);\r
-    virtual void       DestroyDocData(CPDF_DocRenderData* p);\r
-    virtual void       ClearDocData(CPDF_DocRenderData* p);\r
-    virtual CPDF_DocRenderData* GetRenderData()\r
-    {\r
-        return &m_RenderData;\r
-    }\r
-    virtual CPDF_PageRenderCache*      CreatePageCache(CPDF_Page* pPage)\r
-    {\r
-        return FX_NEW CPDF_PageRenderCache(pPage);\r
-    }\r
-    virtual void       DestroyPageCache(CPDF_PageRenderCache* pCache);\r
-    virtual CPDF_RenderConfig* GetConfig()\r
-    {\r
-        return &m_RenderConfig;\r
-    }\r
-private:\r
-    CPDF_DocRenderData m_RenderData;\r
-    CPDF_RenderConfig  m_RenderConfig;\r
-};\r
-CPDF_DocRenderData*    CPDF_RenderModule::CreateDocData(CPDF_Document* pDoc)\r
-{\r
-    CPDF_DocRenderData* pData = FX_NEW CPDF_DocRenderData(pDoc);\r
-    pData->Initialize();\r
-    return pData;\r
-}\r
-void CPDF_RenderModule::DestroyDocData(CPDF_DocRenderData* pDocData)\r
-{\r
-    delete pDocData;\r
-}\r
-void CPDF_RenderModule::ClearDocData(CPDF_DocRenderData* p)\r
-{\r
-    if (p) {\r
-        p->Clear(FALSE);\r
-    }\r
-}\r
-void CPDF_RenderModule::DestroyPageCache(CPDF_PageRenderCache* pCache)\r
-{\r
-    delete pCache;\r
-}\r
-void CPDF_ModuleMgr::InitRenderModule()\r
-{\r
-    if (m_pRenderModule) {\r
-        delete m_pRenderModule;\r
-    }\r
-    m_pRenderModule = FX_NEW CPDF_RenderModule;\r
-}\r
-CPDF_RenderOptions::CPDF_RenderOptions()\r
-    : m_ColorMode(RENDER_COLOR_NORMAL)\r
-    , m_Flags(RENDER_CLEARTYPE)\r
-    , m_Interpolation(0)\r
-    , m_AddFlags(0)\r
-    , m_pOCContext(NULL)\r
-    , m_dwLimitCacheSize(1024 * 1024 * 100)\r
-    , m_HalftoneLimit(-1)\r
-{\r
-#if defined(_FPDFAPI_MINI_)\r
-    m_Flags |= RENDER_LIMITEDIMAGECACHE;\r
-#endif\r
-}\r
-FX_ARGB CPDF_RenderOptions::TranslateColor(FX_ARGB argb) const\r
-{\r
-    if (m_ColorMode == RENDER_COLOR_NORMAL) {\r
-        return argb;\r
-    }\r
-    if (m_ColorMode == RENDER_COLOR_ALPHA) {\r
-        return argb;\r
-    }\r
-    int a, r, g, b;\r
-    ArgbDecode(argb, a, r, g, b);\r
-    int gray = FXRGB2GRAY(r, g, b);\r
-    if (m_ColorMode == RENDER_COLOR_TWOCOLOR) {\r
-        int color = (r - gray) * (r - gray) + (g - gray) * (g - gray) + (b - gray) * (b - gray);\r
-        if (gray < 35 && color < 20) {\r
-            return ArgbEncode(a, m_ForeColor);\r
-        }\r
-        if (gray > 221 && color < 20) {\r
-            return ArgbEncode(a, m_BackColor);\r
-        }\r
-        return argb;\r
-    }\r
-    int fr = FXSYS_GetRValue(m_ForeColor);\r
-    int fg = FXSYS_GetGValue(m_ForeColor);\r
-    int fb = FXSYS_GetBValue(m_ForeColor);\r
-    int br = FXSYS_GetRValue(m_BackColor);\r
-    int bg = FXSYS_GetGValue(m_BackColor);\r
-    int bb = FXSYS_GetBValue(m_BackColor);\r
-    r = (br - fr) * gray / 255 + fr;\r
-    g = (bg - fg) * gray / 255 + fg;\r
-    b = (bb - fb) * gray / 255 + fb;\r
-    return ArgbEncode(a, r, g, b);\r
-}\r
-CPDF_RenderStatus::CPDF_RenderStatus()\r
-{\r
-    m_pContext = NULL;\r
-    m_bStopped = FALSE;\r
-    m_Level = 0;\r
-    m_pDevice = NULL;\r
-    m_pCurObj = NULL;\r
-    m_pStopObj = NULL;\r
-    m_HalftoneLimit = 0;\r
-    m_pObjectRenderer = NULL;\r
-    m_bPrint = FALSE;\r
-    m_Transparency = 0;\r
-    m_DitherBits = 0;\r
-    m_bDropObjects = FALSE;\r
-    m_bStdCS = FALSE;\r
-    m_GroupFamily = 0;\r
-    m_bLoadMask = FALSE;\r
-    m_pType3Char = NULL;\r
-    m_T3FillColor = 0;\r
-    m_pFormResource = NULL;\r
-    m_pPageResource = NULL;\r
-    m_curBlend = FXDIB_BLEND_NORMAL;\r
-}\r
-CPDF_RenderStatus::~CPDF_RenderStatus()\r
-{\r
-    if (m_pObjectRenderer) {\r
-        delete m_pObjectRenderer;\r
-    }\r
-}\r
-FX_BOOL CPDF_RenderStatus::Initialize(int level, CPDF_RenderContext* pContext, CFX_RenderDevice* pDevice,\r
-                                      const CFX_AffineMatrix* pDeviceMatrix, const CPDF_PageObject* pStopObj,\r
-                                      const CPDF_RenderStatus* pParentState, const CPDF_GraphicStates* pInitialStates,\r
-                                      const CPDF_RenderOptions* pOptions, int transparency, FX_BOOL bDropObjects,\r
-                                      CPDF_Dictionary* pFormResource, FX_BOOL bStdCS, CPDF_Type3Char* pType3Char,\r
-                                      FX_ARGB fill_color, FX_DWORD GroupFamily,\r
-                                      FX_BOOL bLoadMask)\r
-{\r
-    m_Level = level;\r
-    m_pContext = pContext;\r
-    m_pDevice = pDevice;\r
-    m_DitherBits = pDevice->GetDeviceCaps(FXDC_DITHER_BITS);\r
-    m_bPrint = m_pDevice->GetDeviceClass() != FXDC_DISPLAY;\r
-    if (pDeviceMatrix) {\r
-        m_DeviceMatrix = *pDeviceMatrix;\r
-    }\r
-    m_pStopObj = pStopObj;\r
-    if (pOptions) {\r
-        m_Options = *pOptions;\r
-    }\r
-    m_bDropObjects = bDropObjects;\r
-    m_bStdCS = bStdCS;\r
-    m_T3FillColor = fill_color;\r
-    m_pType3Char = pType3Char;\r
-    m_GroupFamily = GroupFamily;\r
-    m_bLoadMask = bLoadMask;\r
-    m_pFormResource = pFormResource;\r
-    m_pPageResource = m_pContext->m_pPageResources;\r
-    if (pInitialStates && !m_pType3Char) {\r
-        m_InitialStates.CopyStates(*pInitialStates);\r
-        if (pParentState) {\r
-            CPDF_ColorStateData* pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)m_InitialStates.m_ColorState;\r
-            CPDF_ColorStateData* pParentData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)pParentState->m_InitialStates.m_ColorState;\r
-            if (!pColorData || pColorData->m_FillColor.IsNull()) {\r
-                CPDF_ColorStateData* pData = m_InitialStates.m_ColorState.GetModify();\r
-                pData->m_FillRGB = pParentData->m_FillRGB;\r
-                pData->m_FillColor.Copy(&pParentData->m_FillColor);\r
-            }\r
-            if (!pColorData || pColorData->m_StrokeColor.IsNull()) {\r
-                CPDF_ColorStateData* pData = m_InitialStates.m_ColorState.GetModify();\r
-                pData->m_StrokeRGB = pParentData->m_FillRGB;\r
-                pData->m_StrokeColor.Copy(&pParentData->m_StrokeColor);\r
-            }\r
-        }\r
-    } else {\r
-        m_InitialStates.DefaultStates();\r
-    }\r
-#if defined(_FPDFAPI_MINI_)||defined(_FXCORE_LIMITED_CPU_)\r
-    m_HalftoneLimit = CPDF_ModuleMgr::Get()->GetRenderModule()->GetConfig()->m_HalftoneLimit;\r
-    if (pOptions && pOptions->m_HalftoneLimit >= 0) {\r
-        m_HalftoneLimit = pOptions->m_HalftoneLimit;\r
-    }\r
-#endif\r
-    m_pObjectRenderer = NULL;\r
-    m_Transparency = transparency;\r
-    return TRUE;\r
-}\r
-void CPDF_RenderStatus::RenderObjectList(const CPDF_PageObjects* pObjs, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    if (m_Level > 32) {\r
-        return;\r
-    }\r
-    CFX_FloatRect clip_rect = m_pDevice->GetClipBox();\r
-    CFX_AffineMatrix device2object;\r
-    device2object.SetReverse(*pObj2Device);\r
-    device2object.TransformRect(clip_rect);\r
-    int index = 0;\r
-    FX_POSITION pos = pObjs->GetFirstObjectPosition();\r
-    while(pos) {\r
-        index ++;\r
-        CPDF_PageObject* pCurObj = pObjs->GetNextObject(pos);\r
-        if (pCurObj == m_pStopObj) {\r
-            m_bStopped = TRUE;\r
-            return;\r
-        }\r
-        if (!pCurObj) {\r
-            continue;\r
-        }\r
-        if(pCurObj == NULL || pCurObj->m_Left > clip_rect.right || pCurObj->m_Right < clip_rect.left ||\r
-                pCurObj->m_Bottom > clip_rect.top || pCurObj->m_Top < clip_rect.bottom) {\r
-            continue;\r
-        }\r
-        RenderSingleObject(pCurObj, pObj2Device);\r
-        if (m_bStopped) {\r
-            return;\r
-        }\r
-    }\r
-}\r
-void CPDF_RenderStatus::RenderSingleObject(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    if (m_Level > 32) {\r
-        return;\r
-    }\r
-    m_pCurObj = pObj;\r
-    if (m_Options.m_pOCContext && pObj->m_ContentMark.NotNull())\r
-        if (!m_Options.m_pOCContext->CheckObjectVisible(pObj)) {\r
-            return;\r
-        }\r
-    ProcessClipPath(pObj->m_ClipPath, pObj2Device);\r
-    if (ProcessTransparency(pObj, pObj2Device)) {\r
-        return;\r
-    }\r
-    ProcessObjectNoClip(pObj, pObj2Device);\r
-}\r
-FX_BOOL CPDF_RenderStatus::ContinueSingleObject(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device, IFX_Pause* pPause)\r
-{\r
-    if (m_pObjectRenderer) {\r
-        if (m_pObjectRenderer->Continue(pPause)) {\r
-            return TRUE;\r
-        }\r
-        if (!m_pObjectRenderer->m_Result) {\r
-            DrawObjWithBackground(pObj, pObj2Device);\r
-        }\r
-#ifdef _FPDFAPI_MINI_\r
-        if (m_DitherBits) {\r
-            DitherObjectArea(pObj, pObj2Device);\r
-        }\r
-#endif\r
-        delete m_pObjectRenderer;\r
-        m_pObjectRenderer = NULL;\r
-        return FALSE;\r
-    }\r
-    m_pCurObj = pObj;\r
-    if (m_Options.m_pOCContext && pObj->m_ContentMark.NotNull())\r
-        if (!m_Options.m_pOCContext->CheckObjectVisible(pObj)) {\r
-            return FALSE;\r
-        }\r
-    ProcessClipPath(pObj->m_ClipPath, pObj2Device);\r
-    if (ProcessTransparency(pObj, pObj2Device)) {\r
-        return FALSE;\r
-    }\r
-    if (pObj->m_Type == PDFPAGE_IMAGE) {\r
-        m_pObjectRenderer = IPDF_ObjectRenderer::Create(pObj->m_Type);\r
-        if (!m_pObjectRenderer->Start(this, pObj, pObj2Device, FALSE)) {\r
-            if (!m_pObjectRenderer->m_Result) {\r
-                DrawObjWithBackground(pObj, pObj2Device);\r
-            }\r
-#ifdef _FPDFAPI_MINI_\r
-            if (m_DitherBits) {\r
-                DitherObjectArea(pObj, pObj2Device);\r
-            }\r
-#endif\r
-            delete m_pObjectRenderer;\r
-            m_pObjectRenderer = NULL;\r
-            return FALSE;\r
-        }\r
-        return ContinueSingleObject(pObj, pObj2Device, pPause);\r
-    }\r
-    ProcessObjectNoClip(pObj, pObj2Device);\r
-    return FALSE;\r
-}\r
-IPDF_ObjectRenderer* IPDF_ObjectRenderer::Create(int type)\r
-{\r
-    IPDF_ObjectRenderer* pRenderer = NULL;\r
-    if (type == PDFPAGE_IMAGE) {\r
-        pRenderer = FX_NEW CPDF_ImageRenderer;\r
-    }\r
-    return pRenderer;\r
-}\r
-FX_BOOL CPDF_RenderStatus::GetObjectClippedRect(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device, FX_BOOL bLogical, FX_RECT &rect) const\r
-{\r
-    rect = pObj->GetBBox(pObj2Device);\r
-    FX_RECT rtClip = m_pDevice->GetClipBox();\r
-    if (!bLogical) {\r
-        CFX_Matrix dCTM = m_pDevice->GetCTM();\r
-        FX_FLOAT a = FXSYS_fabs(dCTM.a);\r
-        FX_FLOAT d = FXSYS_fabs(dCTM.d);\r
-        if (a != 1.0f || d != 1.0f) {\r
-            rect.right = rect.left + (FX_INT32)FXSYS_ceil((FX_FLOAT)rect.Width() * a);\r
-            rect.bottom = rect.top + (FX_INT32)FXSYS_ceil((FX_FLOAT)rect.Height() * d);\r
-            rtClip.right = rtClip.left + (FX_INT32)FXSYS_ceil((FX_FLOAT)rtClip.Width() * a);\r
-            rtClip.bottom = rtClip.top + (FX_INT32)FXSYS_ceil((FX_FLOAT)rtClip.Height() * d);\r
-        }\r
-    }\r
-    rect.Intersect(rtClip);\r
-    return rect.IsEmpty();\r
-}\r
-void CPDF_RenderStatus::DitherObjectArea(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    CFX_DIBitmap* pBitmap = m_pDevice->GetBitmap();\r
-    if (pBitmap == NULL) {\r
-        return;\r
-    }\r
-    FX_RECT rect;\r
-    if (GetObjectClippedRect(pObj, pObj2Device, FALSE, rect)) {\r
-        return;\r
-    }\r
-    if (m_DitherBits == 2) {\r
-        static FX_ARGB pal[4] = {0, 85, 170, 255};\r
-        pBitmap->DitherFS(pal, 4, &rect);\r
-    } else if (m_DitherBits == 3) {\r
-        static FX_ARGB pal[8] = {0, 36, 73, 109, 146, 182, 219, 255};\r
-        pBitmap->DitherFS(pal, 8, &rect);\r
-    } else if (m_DitherBits == 4) {\r
-        static FX_ARGB pal[16] = {0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255};\r
-        pBitmap->DitherFS(pal, 16, &rect);\r
-    }\r
-}\r
-void CPDF_RenderStatus::ProcessObjectNoClip(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    FX_BOOL bRet = FALSE;\r
-    switch (pObj->m_Type) {\r
-        case PDFPAGE_TEXT:\r
-            bRet = ProcessText((CPDF_TextObject*)pObj, pObj2Device, NULL);\r
-            break;\r
-        case PDFPAGE_PATH:\r
-            bRet = ProcessPath((CPDF_PathObject*)pObj, pObj2Device);\r
-            break;\r
-        case PDFPAGE_IMAGE:\r
-            bRet = ProcessImage((CPDF_ImageObject*)pObj, pObj2Device);\r
-            break;\r
-        case PDFPAGE_SHADING:\r
-            bRet = ProcessShading((CPDF_ShadingObject*)pObj, pObj2Device);\r
-            break;\r
-        case PDFPAGE_FORM:\r
-            bRet = ProcessForm((CPDF_FormObject*)pObj, pObj2Device);\r
-            break;\r
-#if defined(_FPDFAPI_MINI_)\r
-        case PDFPAGE_INLINES:\r
-            bRet = ProcessInlines((CPDF_InlineImages*)pObj, pObj2Device);\r
-            break;\r
-#endif\r
-    }\r
-    if (!bRet) {\r
-        DrawObjWithBackground(pObj, pObj2Device);\r
-    }\r
-}\r
-FX_BOOL CPDF_RenderStatus::DrawObjWithBlend(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    FX_BOOL bRet = FALSE;\r
-    switch (pObj->m_Type) {\r
-        case PDFPAGE_PATH:\r
-            bRet = ProcessPath((CPDF_PathObject*)pObj, pObj2Device);\r
-            break;\r
-        case PDFPAGE_IMAGE:\r
-            bRet = ProcessImage((CPDF_ImageObject *)pObj, pObj2Device);\r
-            break;\r
-        case PDFPAGE_FORM:\r
-            bRet = ProcessForm((CPDF_FormObject*)pObj, pObj2Device);\r
-            break;\r
-    }\r
-    return bRet;\r
-}\r
-void CPDF_RenderStatus::GetScaledMatrix(CFX_Matrix &matrix) const\r
-{\r
-    CFX_Matrix dCTM = m_pDevice->GetCTM();\r
-    matrix.a *= FXSYS_fabs(dCTM.a);\r
-    matrix.d *= FXSYS_fabs(dCTM.d);\r
-}\r
-void CPDF_RenderStatus::DrawObjWithBackground(const CPDF_PageObject* pObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-#if !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)\r
-    FX_RECT rect;\r
-    if (GetObjectClippedRect(pObj, pObj2Device, FALSE, rect)) {\r
-        return;\r
-    }\r
-    int res = 300;\r
-    if (pObj->m_Type == PDFPAGE_IMAGE && m_pDevice->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {\r
-        res = 0;\r
-    }\r
-    CPDF_ScaledRenderBuffer buffer;\r
-    if (!buffer.Initialize(m_pContext, m_pDevice, &rect, pObj, &m_Options, res)) {\r
-        return;\r
-    }\r
-    CFX_AffineMatrix matrix = *pObj2Device;\r
-    matrix.Concat(*buffer.GetMatrix());\r
-    GetScaledMatrix(matrix);\r
-    CPDF_Dictionary* pFormResource = NULL;\r
-    if (pObj->m_Type == PDFPAGE_FORM) {\r
-        CPDF_FormObject* pFormObj = (CPDF_FormObject*)pObj;\r
-        if (pFormObj->m_pForm && pFormObj->m_pForm->m_pFormDict) {\r
-            pFormResource = pFormObj->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));\r
-        }\r
-    }\r
-    CPDF_RenderStatus status;\r
-    status.Initialize(m_Level + 1, m_pContext, buffer.GetDevice(), buffer.GetMatrix(), NULL, NULL, NULL, &m_Options, m_Transparency, m_bDropObjects, pFormResource);\r
-    status.RenderSingleObject(pObj, &matrix);\r
-    buffer.OutputToDevice();\r
-#endif\r
-}\r
-FX_BOOL CPDF_RenderStatus::ProcessForm(CPDF_FormObject* pFormObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    CPDF_Dictionary* pOC = pFormObj->m_pForm->m_pFormDict->GetDict(FX_BSTRC("OC"));\r
-    if (pOC && m_Options.m_pOCContext && !m_Options.m_pOCContext->CheckOCGVisible(pOC)) {\r
-        return TRUE;\r
-    }\r
-    CFX_AffineMatrix matrix = pFormObj->m_FormMatrix;\r
-    matrix.Concat(*pObj2Device);\r
-    CPDF_Dictionary* pResources = NULL;\r
-    if (pFormObj->m_pForm && pFormObj->m_pForm->m_pFormDict) {\r
-        pResources = pFormObj->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));\r
-    }\r
-    CPDF_RenderStatus status;\r
-    status.Initialize(m_Level + 1, m_pContext, m_pDevice, NULL, m_pStopObj,\r
-                      this, pFormObj, &m_Options, m_Transparency, m_bDropObjects, pResources, TRUE);\r
-    status.m_curBlend = m_curBlend;\r
-    m_pDevice->SaveState();\r
-    status.RenderObjectList(pFormObj->m_pForm, &matrix);\r
-    m_bStopped = status.m_bStopped;\r
-    m_pDevice->RestoreState();\r
-    return TRUE;\r
-}\r
-FX_BOOL IsAvailableMatrix(const CFX_AffineMatrix& matrix)\r
-{\r
-    if (matrix.a == 0 || matrix.d == 0) {\r
-        return matrix.b != 0 && matrix.c != 0;\r
-    }\r
-    if (matrix.b == 0 || matrix.c == 0) {\r
-        return matrix.a != 0 && matrix.d != 0;\r
-    }\r
-    return TRUE;\r
-}\r
-FX_BOOL CPDF_RenderStatus::ProcessPath(CPDF_PathObject* pPathObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    int FillType = pPathObj->m_FillType;\r
-    FX_BOOL bStroke = pPathObj->m_bStroke;\r
-    ProcessPathPattern(pPathObj, pObj2Device, FillType, bStroke);\r
-    if (FillType == 0 && !bStroke) {\r
-        return TRUE;\r
-    }\r
-    FX_DWORD fill_argb = 0;\r
-    if (FillType) {\r
-        fill_argb = GetFillArgb(pPathObj);\r
-    }\r
-    FX_DWORD stroke_argb = 0;\r
-    if (bStroke) {\r
-        stroke_argb = GetStrokeArgb(pPathObj);\r
-    }\r
-    CFX_AffineMatrix path_matrix = pPathObj->m_Matrix;\r
-    path_matrix.Concat(*pObj2Device);\r
-    if (!IsAvailableMatrix(path_matrix)) {\r
-        return TRUE;\r
-    }\r
-    if (FillType && (m_Options.m_Flags & RENDER_RECT_AA)) {\r
-        FillType |= FXFILL_RECT_AA;\r
-    }\r
-    if (m_Options.m_Flags & RENDER_FILL_FULLCOVER) {\r
-        FillType |= FXFILL_FULLCOVER;\r
-    }\r
-    if (m_Options.m_Flags & RENDER_NOPATHSMOOTH) {\r
-        FillType |= FXFILL_NOPATHSMOOTH;\r
-    }\r
-    if (bStroke) {\r
-        FillType |= FX_FILL_STROKE;\r
-    }\r
-#if !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)\r
-    const CPDF_GeneralStateData* pGeneralData = ((CPDF_PageObject*)pPathObj)->m_GeneralState;\r
-    if (pGeneralData && pGeneralData->m_StrokeAdjust) {\r
-        FillType |= FX_STROKE_ADJUST;\r
-    }\r
-#endif\r
-    if (m_pType3Char) {\r
-        FillType |= FX_FILL_TEXT_MODE;\r
-    }\r
-    CFX_GraphStateData graphState(*pPathObj->m_GraphState);\r
-    if (m_Options.m_Flags & RENDER_THINLINE) {\r
-        graphState.m_LineWidth = 0;\r
-    }\r
-    return m_pDevice->DrawPath(pPathObj->m_Path, &path_matrix, &graphState, fill_argb, stroke_argb, FillType, 0, NULL, m_curBlend);\r
-}\r
-CPDF_TransferFunc* CPDF_RenderStatus::GetTransferFunc(CPDF_Object* pObj) const\r
-{\r
-    ASSERT(pObj != NULL);\r
-    CPDF_DocRenderData* pDocCache = m_pContext->m_pDocument->GetRenderData();\r
-    if (!pDocCache) {\r
-        return NULL;\r
-    }\r
-    return pDocCache->GetTransferFunc(pObj);\r
-}\r
-FX_ARGB CPDF_RenderStatus::GetFillArgb(const CPDF_PageObject* pObj, FX_BOOL bType3) const\r
-{\r
-    CPDF_ColorStateData* pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)pObj->m_ColorState;\r
-    if (m_pType3Char && !bType3 && (!m_pType3Char->m_bColored || (m_pType3Char->m_bColored && (!pColorData || pColorData->m_FillColor.IsNull())))) {\r
-        return m_T3FillColor;\r
-    } else if (!pColorData || pColorData->m_FillColor.IsNull()) {\r
-        pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)m_InitialStates.m_ColorState;\r
-    }\r
-    FX_COLORREF rgb = pColorData->m_FillRGB;\r
-    if (rgb == (FX_DWORD) - 1) {\r
-        return 0;\r
-    }\r
-    const CPDF_GeneralStateData* pGeneralData = pObj->m_GeneralState;\r
-    int alpha;\r
-    if (pGeneralData) {\r
-        alpha = (FX_INT32)(pGeneralData->m_FillAlpha * 255);\r
-#ifndef _FPDFAPI_MINI_\r
-        if (pGeneralData->m_pTR) {\r
-            if (!pGeneralData->m_pTransferFunc) {\r
-                ((CPDF_GeneralStateData*)pGeneralData)->m_pTransferFunc = GetTransferFunc(pGeneralData->m_pTR);\r
-            }\r
-            if (pGeneralData->m_pTransferFunc) {\r
-                rgb = pGeneralData->m_pTransferFunc->TranslateColor(rgb);\r
-            }\r
-        }\r
-#endif\r
-    } else {\r
-        alpha = 255;\r
-    }\r
-    return m_Options.TranslateColor(ArgbEncode(alpha, rgb));\r
-}\r
-FX_ARGB CPDF_RenderStatus::GetStrokeArgb(const CPDF_PageObject* pObj) const\r
-{\r
-    CPDF_ColorStateData* pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)pObj->m_ColorState;\r
-    if (m_pType3Char && (!m_pType3Char->m_bColored || (m_pType3Char->m_bColored && (!pColorData || pColorData->m_StrokeColor.IsNull())))) {\r
-        return m_T3FillColor;\r
-    } else if (!pColorData || pColorData->m_StrokeColor.IsNull()) {\r
-        pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)m_InitialStates.m_ColorState;\r
-    }\r
-    FX_COLORREF rgb = pColorData->m_StrokeRGB;\r
-    if (rgb == (FX_DWORD) - 1) {\r
-        return 0;\r
-    }\r
-    const CPDF_GeneralStateData* pGeneralData = pObj->m_GeneralState;\r
-    int alpha;\r
-    if (pGeneralData) {\r
-        alpha = (FX_INT32)(pGeneralData->m_StrokeAlpha * 255);\r
-#ifndef _FPDFAPI_MINI_\r
-        if (pGeneralData->m_pTR) {\r
-            if (!pGeneralData->m_pTransferFunc) {\r
-                ((CPDF_GeneralStateData*)pGeneralData)->m_pTransferFunc = GetTransferFunc(pGeneralData->m_pTR);\r
-            }\r
-            if (pGeneralData->m_pTransferFunc) {\r
-                rgb = pGeneralData->m_pTransferFunc->TranslateColor(rgb);\r
-            }\r
-        }\r
-#endif\r
-    } else {\r
-        alpha = 255;\r
-    }\r
-    return m_Options.TranslateColor(ArgbEncode(alpha, rgb));\r
-}\r
-void CPDF_RenderStatus::ProcessClipPath(CPDF_ClipPath ClipPath, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    if (ClipPath.IsNull()) {\r
-        if (m_LastClipPath.IsNull()) {\r
-            return;\r
-        }\r
-        m_pDevice->RestoreState(TRUE);\r
-        m_LastClipPath.SetNull();\r
-        return;\r
-    }\r
-    if (m_LastClipPath == ClipPath) {\r
-        return;\r
-    }\r
-    m_LastClipPath = ClipPath;\r
-    m_pDevice->RestoreState(TRUE);\r
-    int nClipPath = ClipPath.GetPathCount();\r
-    int i;\r
-    for (i = 0; i < nClipPath; i++) {\r
-        const CFX_PathData* pPathData = ClipPath.GetPath(i);\r
-        if (pPathData == NULL) {\r
-            continue;\r
-        }\r
-        if (pPathData->GetPointCount() == 0) {\r
-            CFX_PathData EmptyPath;\r
-            EmptyPath.AppendRect(-1, -1, 0, 0);\r
-            int fill_mode = FXFILL_WINDING;\r
-            m_pDevice->SetClip_PathFill(&EmptyPath, NULL, fill_mode);\r
-        } else {\r
-            int ClipType = ClipPath.GetClipType(i);\r
-            m_pDevice->SetClip_PathFill(pPathData, pObj2Device, ClipType);\r
-        }\r
-    }\r
-    int textcount = ClipPath.GetTextCount();\r
-    if (textcount == 0) {\r
-        return;\r
-    }\r
-    if (m_pDevice->GetDeviceClass() == FXDC_DISPLAY && !(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_SOFT_CLIP)) {\r
-        return;\r
-    }\r
-    CFX_PathData* pTextClippingPath = NULL;\r
-    for (i = 0; i < textcount; i ++) {\r
-        CPDF_TextObject* pText = ClipPath.GetText(i);\r
-        if (pText == NULL) {\r
-            if (pTextClippingPath) {\r
-                int fill_mode = FXFILL_WINDING;\r
-                if (m_Options.m_Flags & RENDER_NOTEXTSMOOTH) {\r
-                    fill_mode |= FXFILL_NOPATHSMOOTH;\r
-                }\r
-                m_pDevice->SetClip_PathFill(pTextClippingPath, NULL, fill_mode);\r
-                delete pTextClippingPath;\r
-                pTextClippingPath = NULL;\r
-            }\r
-        } else {\r
-            if (pTextClippingPath == NULL) {\r
-                pTextClippingPath = FX_NEW CFX_PathData;\r
-            }\r
-            ProcessText(pText, pObj2Device, pTextClippingPath);\r
-        }\r
-    }\r
-    if (pTextClippingPath) {\r
-        delete pTextClippingPath;\r
-    }\r
-}\r
-void CPDF_RenderStatus::DrawClipPath(CPDF_ClipPath ClipPath, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    if (ClipPath.IsNull()) {\r
-        return;\r
-    }\r
-    int fill_mode = 0;\r
-    if (m_Options.m_Flags & RENDER_NOPATHSMOOTH) {\r
-        fill_mode |= FXFILL_NOPATHSMOOTH;\r
-    }\r
-    int nClipPath = ClipPath.GetPathCount();\r
-    int i;\r
-    for (i = 0; i < nClipPath; i++) {\r
-        const CFX_PathData* pPathData = ClipPath.GetPath(i);\r
-        if (pPathData == NULL) {\r
-            continue;\r
-        }\r
-        CFX_GraphStateData stroke_state;\r
-        if (m_Options.m_Flags & RENDER_THINLINE) {\r
-            stroke_state.m_LineWidth = 0;\r
-        }\r
-        m_pDevice->DrawPath(pPathData, pObj2Device, &stroke_state, 0, 0xffff0000, fill_mode);\r
-    }\r
-}\r
-FX_BOOL CPDF_RenderStatus::SelectClipPath(CPDF_PathObject* pPathObj, const CFX_AffineMatrix* pObj2Device, FX_BOOL bStroke)\r
-{\r
-    CFX_AffineMatrix path_matrix = pPathObj->m_Matrix;\r
-    path_matrix.Concat(*pObj2Device);\r
-    if (bStroke) {\r
-        CFX_GraphStateData graphState(*pPathObj->m_GraphState);\r
-        if (m_Options.m_Flags & RENDER_THINLINE) {\r
-            graphState.m_LineWidth = 0;\r
-        }\r
-        return m_pDevice->SetClip_PathStroke(pPathObj->m_Path, &path_matrix, &graphState);\r
-    }\r
-    int fill_mode = pPathObj->m_FillType;\r
-    if (m_Options.m_Flags & RENDER_NOPATHSMOOTH) {\r
-        fill_mode |= FXFILL_NOPATHSMOOTH;\r
-    }\r
-    return m_pDevice->SetClip_PathFill(pPathObj->m_Path, &path_matrix, fill_mode);\r
-}\r
-FX_BOOL CPDF_RenderStatus::ProcessTransparency(const CPDF_PageObject* pPageObj, const CFX_AffineMatrix* pObj2Device)\r
-{\r
-    const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;\r
-    int blend_type = pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;\r
-    if (blend_type == FXDIB_BLEND_UNSUPPORTED) {\r
-        return TRUE;\r
-    }\r
-    CPDF_Dictionary* pSMaskDict = pGeneralState ? (CPDF_Dictionary*)pGeneralState->m_pSoftMask : NULL;\r
-    if (pSMaskDict) {\r
-        if (pPageObj->m_Type == PDFPAGE_IMAGE &&\r
-                ((CPDF_ImageObject*)pPageObj)->m_pImage->GetDict()->KeyExist(FX_BSTRC("SMask"))) {\r
-            pSMaskDict = NULL;\r
-        }\r
-    }\r
-    CPDF_Dictionary* pFormResource = NULL;\r
-    FX_FLOAT group_alpha = 1.0f;\r
-    int Transparency = m_Transparency;\r
-    FX_BOOL bGroupTransparent = FALSE;\r
-    if (pPageObj->m_Type == PDFPAGE_FORM) {\r
-        CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;\r
-        const CPDF_GeneralStateData *pStateData = pFormObj->m_GeneralState.GetObject();\r
-        if (pStateData) {\r
-            group_alpha = pStateData->m_FillAlpha;\r
-        }\r
-        Transparency = pFormObj->m_pForm->m_Transparency;\r
-        bGroupTransparent = Transparency & PDFTRANS_ISOLATED ? TRUE : FALSE;\r
-        if (pFormObj->m_pForm->m_pFormDict) {\r
-            pFormResource = pFormObj->m_pForm->m_pFormDict->GetDict("Resources");\r
-        }\r
-    }\r
-    FX_BOOL bTextClip = FALSE;\r
-    if (pPageObj->m_ClipPath.NotNull() && pPageObj->m_ClipPath.GetTextCount() &&\r
-            m_pDevice->GetDeviceClass() == FXDC_DISPLAY && !(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_SOFT_CLIP)) {\r
-        bTextClip = TRUE;\r
-    }\r
-    if ((m_Options.m_Flags & RENDER_OVERPRINT) && pPageObj->m_Type == PDFPAGE_IMAGE && pGeneralState && pGeneralState->m_FillOP && pGeneralState->m_StrokeOP) {\r
-        CPDF_Document* pDocument = NULL;\r
-        CPDF_Page* pPage = NULL;\r
-        if (m_pContext->m_pPageCache) {\r
-            pPage = m_pContext->m_pPageCache->GetPage();\r
-            pDocument = pPage->m_pDocument;\r
-        } else {\r
-            pDocument = ((CPDF_ImageObject*)pPageObj)->m_pImage->GetDocument();\r
-        }\r
-        CPDF_Dictionary* pPageResources = pPage ? pPage->m_pPageResources : NULL;\r
-        CPDF_Object* pCSObj = ((CPDF_ImageObject*)pPageObj)->m_pImage->GetStream()->GetDict()->GetElementValue(FX_BSTRC("ColorSpace"));\r
-        CPDF_ColorSpace* pColorSpace = pDocument->LoadColorSpace(pCSObj, pPageResources);\r
-        if (pColorSpace) {\r
-            int format = pColorSpace->GetFamily();\r
-            if (format == PDFCS_DEVICECMYK || format == PDFCS_SEPARATION || format == PDFCS_DEVICEN) {\r
-                blend_type = FXDIB_BLEND_DARKEN;\r
-            }\r
-            pDocument->GetPageData()->ReleaseColorSpace(pCSObj);\r
-        }\r
-    }\r
-    if (pSMaskDict == NULL && group_alpha == 1.0f && blend_type == FXDIB_BLEND_NORMAL && !bTextClip && !bGroupTransparent) {\r
-        return FALSE;\r
-    }\r
-    FX_BOOL isolated = Transparency & PDFTRANS_ISOLATED;\r
-    if (m_bPrint) {\r
-        FX_BOOL bRet = FALSE;\r
-        int rendCaps = m_pDevice->GetRenderCaps();\r
-        if (!((Transparency & PDFTRANS_ISOLATED) || pSMaskDict || bTextClip) && (rendCaps & FXRC_BLEND_MODE)) {\r
-            int oldBlend = m_curBlend;\r
-            m_curBlend = blend_type;\r
-            bRet = DrawObjWithBlend(pPageObj, pObj2Device);\r
-            m_curBlend = oldBlend;\r
-        }\r
-        if (!bRet) {\r
-            DrawObjWithBackground(pPageObj, pObj2Device);\r
-        }\r
-        return TRUE;\r
-    }\r
-    FX_RECT rect = pPageObj->GetBBox(pObj2Device);\r
-    rect.Intersect(m_pDevice->GetClipBox());\r
-    if (rect.IsEmpty()) {\r
-        return TRUE;\r
-    }\r
-    CFX_Matrix deviceCTM = m_pDevice->GetCTM();\r
-    FX_FLOAT scaleX = FXSYS_fabs(deviceCTM.a);\r
-    FX_FLOAT scaleY = FXSYS_fabs(deviceCTM.d);\r
-    int width = FXSYS_round((FX_FLOAT)rect.Width() * scaleX);\r
-    int height = FXSYS_round((FX_FLOAT)rect.Height() * scaleY);\r
-    CFX_FxgeDevice bitmap_device;\r
-    CFX_DIBitmap* oriDevice = NULL;\r
-    if (!isolated && (m_pDevice->GetRenderCaps() & FXRC_GET_BITS)) {\r
-        oriDevice = FX_NEW CFX_DIBitmap;\r
-        if (!m_pDevice->CreateCompatibleBitmap(oriDevice, width, height)) {\r
-            return TRUE;\r
-        }\r
-        m_pDevice->GetDIBits(oriDevice, rect.left, rect.top);\r
-    }\r
-    if (!bitmap_device.Create(width, height, FXDIB_Argb, 0, oriDevice)) {\r
-        return TRUE;\r
-    }\r
-    CFX_DIBitmap* bitmap = bitmap_device.GetBitmap();\r
-    bitmap->Clear(0);\r
-    CFX_AffineMatrix new_matrix = *pObj2Device;\r
-    new_matrix.TranslateI(-rect.left, -rect.top);\r
-    new_matrix.Scale(scaleX, scaleY);\r
-    CFX_DIBitmap* pTextMask = NULL;\r
-    if (bTextClip) {\r
-        pTextMask = FX_NEW CFX_DIBitmap;\r
-        if (!pTextMask->Create(width, height, FXDIB_8bppMask)) {\r
-            delete pTextMask;\r
-            return TRUE;\r
-        }\r
-        pTextMask->Clear(0);\r
-        CFX_FxgeDevice text_device;\r
-        text_device.Attach(pTextMask);\r
-        for (FX_DWORD i = 0; i < pPageObj->m_ClipPath.GetTextCount(); i ++) {\r
-            CPDF_TextObject* textobj = pPageObj->m_ClipPath.GetText(i);\r
-            if (textobj == NULL) {\r
-                break;\r
-            }\r
-            CFX_AffineMatrix text_matrix;\r
-            textobj->GetTextMatrix(&text_matrix);\r
-            CPDF_TextRenderer::DrawTextPath(&text_device, textobj->m_nChars, textobj->m_pCharCodes, textobj->m_pCharPos,\r
-                                            textobj->m_TextState.GetFont(), textobj->m_TextState.GetFontSize(),\r
-                                            &text_matrix, &new_matrix, textobj->m_GraphState, (FX_ARGB) - 1, 0, NULL);\r
-        }\r
-    }\r
-    CPDF_RenderStatus bitmap_render;\r
-    bitmap_render.Initialize(m_Level + 1, m_pContext, &bitmap_device, NULL,\r
-                             m_pStopObj, NULL, NULL, &m_Options, 0, m_bDropObjects, pFormResource, TRUE);\r
-    bitmap_render.ProcessObjectNoClip(pPageObj, &new_matrix);\r
-    m_bStopped = bitmap_render.m_bStopped;\r
-    if (pSMaskDict) {\r
-        CFX_AffineMatrix smask_matrix;\r
-        FXSYS_memcpy32(&smask_matrix, pGeneralState->m_SMaskMatrix, sizeof smask_matrix);\r
-        smask_matrix.Concat(*pObj2Device);\r
-        CFX_DIBSource* pSMaskSource = LoadSMask(pSMaskDict, &rect, &smask_matrix);\r
-        if (pSMaskSource) {\r
-            bitmap->MultiplyAlpha(pSMaskSource);\r
-            delete pSMaskSource;\r
-        }\r
-    }\r
-    if (pTextMask) {\r
-        bitmap->MultiplyAlpha(pTextMask);\r
-        delete pTextMask;\r
-        pTextMask = NULL;\r
-    }\r
-    if (Transparency & PDFTRANS_GROUP && group_alpha != 1.0f) {\r
-        bitmap->MultiplyAlpha((FX_INT32)(group_alpha * 255));\r
-    }\r
-    Transparency = m_Transparency;\r
-    if (pPageObj->m_Type == PDFPAGE_FORM) {\r
-        Transparency |= PDFTRANS_GROUP;\r
-    }\r
-    CompositeDIBitmap(bitmap, rect.left, rect.top, 0, 255, blend_type, Transparency);\r
-    if (oriDevice) {\r
-        delete oriDevice;\r
-    }\r
-    return TRUE;\r
-}\r
-CFX_DIBitmap* CPDF_RenderStatus::GetBackdrop(const CPDF_PageObject* pObj, const FX_RECT& rect, int& left, int& top,\r
-        FX_BOOL bBackAlphaRequired)\r
-{\r
-    FX_RECT bbox = rect;\r
-    bbox.Intersect(m_pDevice->GetClipBox());\r
-    left = bbox.left;\r
-    top = bbox.top;\r
-    CFX_Matrix deviceCTM = m_pDevice->GetCTM();\r
-    FX_FLOAT scaleX = FXSYS_fabs(deviceCTM.a);\r
-    FX_FLOAT scaleY = FXSYS_fabs(deviceCTM.d);\r
-    int width = FXSYS_round(bbox.Width() * scaleX);\r
-    int height = FXSYS_round(bbox.Height() * scaleY);\r
-    CFX_DIBitmap* pBackdrop = FX_NEW CFX_DIBitmap;\r
-    if (bBackAlphaRequired && !m_bDropObjects) {\r
-        pBackdrop->Create(width, height, FXDIB_Argb);\r
-    } else {\r
-        m_pDevice->CreateCompatibleBitmap(pBackdrop, width, height);\r
-    }\r
-    if (pBackdrop->GetBuffer() == NULL) {\r
-        delete pBackdrop;\r
-        return NULL;\r
-    }\r
-    FX_BOOL bNeedDraw;\r
-    if (pBackdrop->HasAlpha()) {\r
-        bNeedDraw = !(m_pDevice->GetRenderCaps() & FXRC_ALPHA_OUTPUT);\r
-    } else {\r
-        bNeedDraw = !(m_pDevice->GetRenderCaps() & FXRC_GET_BITS);\r
-    }\r
-    if (!bNeedDraw) {\r
-        m_pDevice->GetDIBits(pBackdrop, left, top);\r
-        return pBackdrop;\r
-    }\r
-    CFX_AffineMatrix FinalMatrix = m_DeviceMatrix;\r
-    FinalMatrix.TranslateI(-left, -top);\r
-    FinalMatrix.Scale(scaleX, scaleY);\r
-    pBackdrop->Clear(pBackdrop->HasAlpha() ? 0 : 0xffffffff);\r
-    CFX_FxgeDevice device;\r
-    device.Attach(pBackdrop);\r
-    m_pContext->Render(&device, pObj, &m_Options, &FinalMatrix);\r
-    return pBackdrop;\r
-}\r
-void CPDF_RenderContext::GetBackground(CFX_DIBitmap* pBuffer, const CPDF_PageObject* pObj,\r
-                                       const CPDF_RenderOptions* pOptions, CFX_AffineMatrix* pFinalMatrix)\r
-{\r
-    CFX_FxgeDevice device;\r
-    device.Attach(pBuffer);\r
-    if (m_pBackgroundDraw) {\r
-        m_pBackgroundDraw->OnDrawBackground(&device, pFinalMatrix);\r
-    } else {\r
-        FX_RECT rect(0, 0, device.GetWidth(), device.GetHeight());\r
-        device.FillRect(&rect, 0xffffffff);\r
-    }\r
-    Render(&device, pObj, pOptions, pFinalMatrix);\r
-}\r
-CPDF_GraphicStates* CPDF_RenderStatus::CloneObjStates(const CPDF_GraphicStates* pSrcStates, FX_BOOL bStroke)\r
-{\r
-    if (!pSrcStates) {\r
-        return NULL;\r
-    }\r
-    CPDF_GraphicStates* pStates = FX_NEW CPDF_GraphicStates;\r
-    if (!pStates) {\r
-        return NULL;\r
-    }\r
-    pStates->CopyStates(*pSrcStates);\r
-    CPDF_Color* pObjColor = bStroke ? pSrcStates->m_ColorState.GetStrokeColor() :\r
-                            pSrcStates->m_ColorState.GetFillColor();\r
-    if (!pObjColor->IsNull()) {\r
-        CPDF_ColorStateData* pColorData = pStates->m_ColorState.GetModify();\r
-        pColorData->m_FillRGB = bStroke ? pSrcStates->m_ColorState.GetObject()->m_StrokeRGB :\r
-                                pSrcStates->m_ColorState.GetObject()->m_FillRGB;\r
-        pColorData->m_StrokeRGB = pColorData->m_FillRGB;\r
-    }\r
-    return pStates;\r
-}\r
-CPDF_RenderContext::CPDF_RenderContext()\r
-{\r
-}\r
-void CPDF_RenderContext::Create(CPDF_Document* pDoc, CPDF_PageRenderCache* pPageCache,\r
-                                CPDF_Dictionary* pPageResources, FX_BOOL bFirstLayer)\r
-{\r
-    m_pBackgroundDraw = NULL;\r
-    m_pDocument = pDoc;\r
-    m_pPageResources = pPageResources;\r
-    m_pPageCache = pPageCache;\r
-    m_bFirstLayer = bFirstLayer;\r
-}\r
-void CPDF_RenderContext::Create(CPDF_Page* pPage, FX_BOOL bFirstLayer)\r
-{\r
-    m_pBackgroundDraw = NULL;\r
-    m_pDocument = pPage->m_pDocument;\r
-    m_pPageResources = pPage->m_pPageResources;\r
-    m_pPageCache = pPage->GetRenderCache();\r
-    m_bFirstLayer = bFirstLayer;\r
-}\r
-CPDF_RenderContext::~CPDF_RenderContext()\r
-{\r
-}\r
-void CPDF_RenderContext::Clear()\r
-{\r
-    m_pDocument = NULL;\r
-    m_pPageResources = NULL;\r
-    m_pPageCache = NULL;\r
-    m_pBackgroundDraw = NULL;\r
-    m_bFirstLayer = TRUE;\r
-    m_ContentList.RemoveAll();\r
-}\r
-void CPDF_RenderContext::AppendObjectList(CPDF_PageObjects* pObjs, const CFX_AffineMatrix* pObject2Device)\r
-{\r
-    _PDF_RenderItem* pItem = m_ContentList.AddSpace();\r
-    pItem->m_pObjectList = pObjs;\r
-    if (pObject2Device) {\r
-        pItem->m_Matrix = *pObject2Device;\r
-    } else {\r
-        pItem->m_Matrix.SetIdentity();\r
-    }\r
-}\r
-void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice, const CPDF_RenderOptions* pOptions,\r
-                                const CFX_AffineMatrix* pLastMatrix)\r
-{\r
-    Render(pDevice, NULL, pOptions, pLastMatrix);\r
-}\r
-void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice, const CPDF_PageObject* pStopObj,\r
-                                const CPDF_RenderOptions* pOptions, const CFX_AffineMatrix* pLastMatrix)\r
-{\r
-    int count = m_ContentList.GetSize();\r
-    for (int j = 0; j < count; j ++) {\r
-        pDevice->SaveState();\r
-        _PDF_RenderItem* pItem = m_ContentList.GetDataPtr(j);\r
-        if (pLastMatrix) {\r
-            CFX_AffineMatrix FinalMatrix = pItem->m_Matrix;\r
-            FinalMatrix.Concat(*pLastMatrix);\r
-            CPDF_RenderStatus status;\r
-            status.Initialize(0, this, pDevice, pLastMatrix, pStopObj, NULL, NULL, pOptions,\r
-                              pItem->m_pObjectList->m_Transparency, FALSE, NULL);\r
-            status.RenderObjectList(pItem->m_pObjectList, &FinalMatrix);\r
-#if !defined(_FPDFAPI_MINI_)\r
-            if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) {\r
-                m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize);\r
-            }\r
-#endif\r
-            if (status.m_bStopped) {\r
-                pDevice->RestoreState();\r
-                break;\r
-            }\r
-        } else {\r
-            CPDF_RenderStatus status;\r
-            status.Initialize(0, this, pDevice, NULL, pStopObj, NULL, NULL, pOptions,\r
-                              pItem->m_pObjectList->m_Transparency, FALSE, NULL);\r
-            status.RenderObjectList(pItem->m_pObjectList, &pItem->m_Matrix);\r
-#if !defined(_FPDFAPI_MINI_)\r
-            if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) {\r
-                m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize);\r
-            }\r
-#endif\r
-            if (status.m_bStopped) {\r
-                pDevice->RestoreState();\r
-                break;\r
-            }\r
-        }\r
-        pDevice->RestoreState();\r
-    }\r
-}\r
-void CPDF_RenderContext::DrawObjectList(CFX_RenderDevice* pDevice, CPDF_PageObjects* pObjs,\r
-                                        const CFX_AffineMatrix* pObject2Device, const CPDF_RenderOptions* pOptions)\r
-{\r
-    AppendObjectList(pObjs, pObject2Device);\r
-    Render(pDevice, pOptions);\r
-}\r
-CPDF_ProgressiveRenderer::CPDF_ProgressiveRenderer()\r
-{\r
-    m_pRenderer = NULL;\r
-    m_pContext = NULL;\r
-    m_pDevice = NULL;\r
-    m_Status = Ready;\r
-}\r
-CPDF_ProgressiveRenderer::~CPDF_ProgressiveRenderer()\r
-{\r
-    Clear();\r
-}\r
-void CPDF_ProgressiveRenderer::Clear()\r
-{\r
-    if (m_pRenderer) {\r
-        delete m_pRenderer;\r
-        m_pDevice->RestoreState();\r
-        m_pRenderer = NULL;\r
-    }\r
-    m_Status = Ready;\r
-}\r
-void CPDF_ProgressiveRenderer::Start(CPDF_RenderContext* pContext, CFX_RenderDevice* pDevice,\r
-                                     const CPDF_RenderOptions* pOptions, IFX_Pause* pPause, FX_BOOL bDropObjects)\r
-{\r
-    if (m_Status != Ready) {\r
-        m_Status = Failed;\r
-        return;\r
-    }\r
-    m_pContext = pContext;\r
-    m_pDevice = pDevice;\r
-    m_pOptions = pOptions;\r
-    m_bDropObjects = bDropObjects;\r
-    if (pContext == NULL || pDevice == NULL) {\r
-        m_Status = Failed;\r
-        return;\r
-    }\r
-    m_Status = ToBeContinued;\r
-    m_ObjectPos = NULL;\r
-    m_LayerIndex = 0;\r
-    m_ObjectIndex = 0;\r
-    m_PrevLastPos = NULL;\r
-    Continue(pPause);\r
-}\r
-#ifdef _FPDFAPI_MINI_\r
-#define RENDER_STEP_LIMIT 20\r
-#else\r
-#define RENDER_STEP_LIMIT 100\r
-#endif\r
-void CPDF_ProgressiveRenderer::Continue(IFX_Pause* pPause)\r
-{\r
-    if (m_Status != ToBeContinued) {\r
-        return;\r
-    }\r
-    FX_DWORD nLayers = m_pContext->m_ContentList.GetSize();\r
-    for (; m_LayerIndex < nLayers; m_LayerIndex ++) {\r
-        _PDF_RenderItem* pItem = m_pContext->m_ContentList.GetDataPtr(m_LayerIndex);\r
-        FX_POSITION LastPos = pItem->m_pObjectList->GetLastObjectPosition();\r
-        if (m_ObjectPos == NULL) {\r
-            if (LastPos == m_PrevLastPos) {\r
-                if (!pItem->m_pObjectList->IsParsed()) {\r
-                    pItem->m_pObjectList->ContinueParse(pPause);\r
-                    if (!pItem->m_pObjectList->IsParsed()) {\r
-                        return;\r
-                    }\r
-                    LastPos = pItem->m_pObjectList->GetLastObjectPosition();\r
-                }\r
-            }\r
-            if (LastPos == m_PrevLastPos) {\r
-                if (m_pRenderer) {\r
-                    delete m_pRenderer;\r
-                    m_pRenderer = NULL;\r
-                    m_pDevice->RestoreState();\r
-                    m_ObjectPos = NULL;\r
-                    m_PrevLastPos = NULL;\r
-                }\r
-                continue;\r
-            }\r
-            if (m_PrevLastPos) {\r
-                m_ObjectPos = m_PrevLastPos;\r
-                pItem->m_pObjectList->GetNextObject(m_ObjectPos);\r
-            } else {\r
-                m_ObjectPos = pItem->m_pObjectList->GetFirstObjectPosition();\r
-            }\r
-            m_PrevLastPos = LastPos;\r
-        }\r
-        if (m_pRenderer == NULL) {\r
-            m_ObjectPos = pItem->m_pObjectList->GetFirstObjectPosition();\r
-            m_ObjectIndex = 0;\r
-            m_pRenderer = FX_NEW CPDF_RenderStatus();\r
-            m_pRenderer->Initialize(0, m_pContext, m_pDevice, NULL, NULL, NULL, NULL,\r
-                                    m_pOptions, pItem->m_pObjectList->m_Transparency, m_bDropObjects, NULL);\r
-            m_pDevice->SaveState();\r
-            m_ClipRect = m_pDevice->GetClipBox();\r
-            CFX_AffineMatrix device2object;\r
-            device2object.SetReverse(pItem->m_Matrix);\r
-            device2object.TransformRect(m_ClipRect);\r
-        }\r
-        int objs_to_go = CPDF_ModuleMgr::Get()->GetRenderModule()->GetConfig()->m_RenderStepLimit;\r
-        while (m_ObjectPos) {\r
-            CPDF_PageObject* pCurObj = pItem->m_pObjectList->GetObjectAt(m_ObjectPos);\r
-            if (pCurObj && pCurObj->m_Left <= m_ClipRect.right && pCurObj->m_Right >= m_ClipRect.left &&\r
-                    pCurObj->m_Bottom <= m_ClipRect.top && pCurObj->m_Top >= m_ClipRect.bottom) {\r
-                if (m_pRenderer->ContinueSingleObject(pCurObj, &pItem->m_Matrix, pPause)) {\r
-                    return;\r
-                }\r
-#if !defined(_FPDFAPI_MINI_)\r
-                if (pCurObj->m_Type == PDFPAGE_IMAGE && m_pRenderer->m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) {\r
-                    m_pContext->GetPageCache()->CacheOptimization(m_pRenderer->m_Options.m_dwLimitCacheSize);\r
-                }\r
-#endif\r
-                if (pCurObj->m_Type == PDFPAGE_FORM || pCurObj->m_Type == PDFPAGE_SHADING) {\r
-                    objs_to_go = 0;\r
-                } else {\r
-                    objs_to_go --;\r
-                }\r
-            }\r
-            m_ObjectIndex ++;\r
-            pItem->m_pObjectList->GetNextObject(m_ObjectPos);\r
-            if (objs_to_go == 0) {\r
-                if (pPause && pPause->NeedToPauseNow()) {\r
-                    return;\r
-                }\r
-                objs_to_go = CPDF_ModuleMgr::Get()->GetRenderModule()->GetConfig()->m_RenderStepLimit;\r
-            }\r
-        }\r
-        if (!pItem->m_pObjectList->IsParsed()) {\r
-            return;\r
-        }\r
-        delete m_pRenderer;\r
-        m_pRenderer = NULL;\r
-        m_pDevice->RestoreState();\r
-        m_ObjectPos = NULL;\r
-        m_PrevLastPos = NULL;\r
-        if (pPause && pPause->NeedToPauseNow()) {\r
-            m_LayerIndex++;\r
-            return;\r
-        }\r
-    }\r
-    m_Status = Done;\r
-}\r
-int CPDF_ProgressiveRenderer::EstimateProgress()\r
-{\r
-    if (!m_pContext) {\r
-        return 0;\r
-    }\r
-    FX_DWORD nLayers = m_pContext->m_ContentList.GetSize();\r
-    int nTotal = 0, nRendered = 0;\r
-    for (FX_DWORD layer = 0; layer < nLayers; layer ++) {\r
-        _PDF_RenderItem* pItem = m_pContext->m_ContentList.GetDataPtr(layer);\r
-        int nObjs = pItem->m_pObjectList->CountObjects();\r
-        if (layer == m_LayerIndex) {\r
-            nRendered += m_ObjectIndex;\r
-        } else if (layer < m_LayerIndex) {\r
-            nRendered += nObjs;\r
-        }\r
-        nTotal += nObjs;\r
-    }\r
-    if (nTotal == 0) {\r
-        return 0;\r
-    }\r
-    return 100 * nRendered / nTotal;\r
-}\r
-CPDF_TransferFunc* CPDF_DocRenderData::GetTransferFunc(CPDF_Object* pObj)\r
-{\r
-    if (pObj == NULL) {\r
-        return NULL;\r
-    }\r
-    CPDF_CountedObject<CPDF_TransferFunc*>* pTransferCounter;\r
-    if (!m_TransferFuncMap.Lookup(pObj, pTransferCounter)) {\r
-        CPDF_TransferFunc* pTransfer = NULL;\r
-        CPDF_Function* pFuncs[3] = {NULL, NULL, NULL};\r
-        FX_BOOL bUniTransfer = TRUE;\r
-        int i;\r
-        FX_BOOL bIdentity = TRUE;\r
-        if (pObj->GetType() == PDFOBJ_ARRAY) {\r
-            bUniTransfer = FALSE;\r
-            CPDF_Array* pArray = (CPDF_Array*)pObj;\r
-            if (pArray->GetCount() < 3) {\r
-                return NULL;\r
-            }\r
-            for (FX_DWORD i = 0; i < 3; i ++) {\r
-                pFuncs[2 - i] = CPDF_Function::Load(pArray->GetElementValue(i));\r
-                if (pFuncs[2 - i] == NULL) {\r
-                    return NULL;\r
-                }\r
-            }\r
-        } else {\r
-            pFuncs[0] = CPDF_Function::Load(pObj);\r
-            if (pFuncs[0] == NULL) {\r
-                return NULL;\r
-            }\r
-        }\r
-        pTransfer = FX_NEW CPDF_TransferFunc;\r
-        pTransfer->m_pPDFDoc = m_pPDFDoc;\r
-        pTransferCounter = FX_NEW CPDF_CountedObject<CPDF_TransferFunc*>;\r
-        pTransferCounter->m_nCount = 1;\r
-        pTransferCounter->m_Obj = pTransfer;\r
-        m_TransferFuncMap.SetAt(pObj, pTransferCounter);\r
-        static const int kMaxOutputs = 16;\r
-        FX_FLOAT output[kMaxOutputs];\r
-        FXSYS_memset32(output, 0, sizeof(output));\r
-        FX_FLOAT input;\r
-        int noutput;\r
-        for (int v = 0; v < 256; v ++) {\r
-            input = (FX_FLOAT)v / 255.0f;\r
-            if (bUniTransfer) {\r
-                if (pFuncs[0] && pFuncs[0]->CountOutputs() <= kMaxOutputs) {\r
-                    pFuncs[0]->Call(&input, 1, output, noutput);\r
-                }\r
-                int o = FXSYS_round(output[0] * 255);\r
-                if (o != v) {\r
-                    bIdentity = FALSE;\r
-                }\r
-                for (i = 0; i < 3; i ++) {\r
-                    pTransfer->m_Samples[i * 256 + v] = o;\r
-                }\r
-            } else\r
-                for (i = 0; i < 3; i ++) {\r
-                    if (pFuncs[i] && pFuncs[i]->CountOutputs() <= kMaxOutputs) {\r
-                        pFuncs[i]->Call(&input, 1, output, noutput);\r
-                        int o = FXSYS_round(output[0] * 255);\r
-                        if (o != v) {\r
-                            bIdentity = FALSE;\r
-                        }\r
-                        pTransfer->m_Samples[i * 256 + v] = o;\r
-                    } else {\r
-                        pTransfer->m_Samples[i * 256 + v] = v;\r
-                    }\r
-                }\r
-        }\r
-        for (i = 0; i < 3; i ++)\r
-            if (pFuncs[i]) {\r
-                delete pFuncs[i];\r
-            }\r
-        pTransfer->m_bIdentity = bIdentity;\r
-    }\r
-    pTransferCounter->m_nCount++;\r
-    return pTransferCounter->m_Obj;\r
-}\r
-void CPDF_DocRenderData::ReleaseTransferFunc(CPDF_Object* pObj)\r
-{\r
-    CPDF_CountedObject<CPDF_TransferFunc*>* pTransferCounter;\r
-    if (!m_TransferFuncMap.Lookup(pObj, pTransferCounter)) {\r
-        return;\r
-    }\r
-    pTransferCounter->m_nCount--;\r
-}\r
-CPDF_RenderConfig::CPDF_RenderConfig()\r
-{\r
-    m_HalftoneLimit = 0;\r
-#ifdef _FPDFAPI_MINI_\r
-    m_RenderStepLimit = 20;\r
-#else\r
-    m_RenderStepLimit = 100;\r
-#endif\r
-}\r
-CPDF_RenderConfig::~CPDF_RenderConfig()\r
-{\r
-}\r
-CPDF_DeviceBuffer::CPDF_DeviceBuffer()\r
-{\r
-    m_pBitmap = NULL;\r
-    m_pDevice = NULL;\r
-    m_pContext = NULL;\r
-    m_pObject = NULL;\r
-}\r
-CPDF_DeviceBuffer::~CPDF_DeviceBuffer()\r
-{\r
-    if (m_pBitmap) {\r
-        delete m_pBitmap;\r
-    }\r
-}\r
-FX_BOOL CPDF_DeviceBuffer::Initialize(CPDF_RenderContext* pContext, CFX_RenderDevice* pDevice, FX_RECT* pRect,\r
-                                      const CPDF_PageObject* pObj, int max_dpi)\r
-{\r
-    m_pDevice = pDevice;\r
-    m_pContext = pContext;\r
-    m_Rect = *pRect;\r
-    m_pObject = pObj;\r
-    m_Matrix.TranslateI(-pRect->left, -pRect->top);\r
-#if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_\r
-    int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);\r
-    int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);\r
-    if (horz_size && vert_size && max_dpi) {\r
-        int dpih = pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);\r
-        int dpiv = pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);\r
-        if (dpih > max_dpi) {\r
-            m_Matrix.Scale((FX_FLOAT)(max_dpi) / dpih, 1.0f);\r
-        }\r
-        if (dpiv > max_dpi) {\r
-            m_Matrix.Scale(1.0f, (FX_FLOAT)(max_dpi) / (FX_FLOAT)dpiv);\r
-        }\r
-    }\r
-#ifdef _FPDFAPI_MINI_\r
-    m_Matrix.Scale(0.5f, 0.5f);\r
-#endif\r
-#endif\r
-    CFX_Matrix ctm = m_pDevice->GetCTM();\r
-    FX_FLOAT fScaleX = FXSYS_fabs(ctm.a);\r
-    FX_FLOAT fScaleY = FXSYS_fabs(ctm.d);\r
-    m_Matrix.Concat(fScaleX, 0, 0, fScaleY, 0, 0);\r
-    CFX_FloatRect rect(*pRect);\r
-    m_Matrix.TransformRect(rect);\r
-    FX_RECT bitmap_rect = rect.GetOutterRect();\r
-    m_pBitmap = FX_NEW CFX_DIBitmap;\r
-    m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(), FXDIB_Argb);\r
-    return TRUE;\r
-}\r
-void CPDF_DeviceBuffer::OutputToDevice()\r
-{\r
-    if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {\r
-        if (m_Matrix.a == 1.0f && m_Matrix.d == 1.0f) {\r
-            m_pDevice->SetDIBits(m_pBitmap, m_Rect.left, m_Rect.top);\r
-        } else {\r
-            m_pDevice->StretchDIBits(m_pBitmap, m_Rect.left, m_Rect.top, m_Rect.Width(), m_Rect.Height());\r
-        }\r
-    } else {\r
-#if !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)\r
-        CFX_DIBitmap buffer;\r
-        m_pDevice->CreateCompatibleBitmap(&buffer, m_pBitmap->GetWidth(), m_pBitmap->GetHeight());\r
-        m_pContext->GetBackground(&buffer, m_pObject, NULL, &m_Matrix);\r
-        buffer.CompositeBitmap(0, 0, buffer.GetWidth(), buffer.GetHeight(), m_pBitmap, 0, 0);\r
-        m_pDevice->StretchDIBits(&buffer, m_Rect.left, m_Rect.top, m_Rect.Width(), m_Rect.Height());\r
-#endif\r
-    }\r
-}\r
-CPDF_ScaledRenderBuffer::CPDF_ScaledRenderBuffer()\r
-{\r
-    m_pBitmapDevice = NULL;\r
-}\r
-CPDF_ScaledRenderBuffer::~CPDF_ScaledRenderBuffer()\r
-{\r
-    if (m_pBitmapDevice) {\r
-        delete m_pBitmapDevice;\r
-    }\r
-}\r
-#ifndef _FPDFAPI_MINI_\r
-#define _FPDFAPI_IMAGESIZE_LIMIT_      (30 * 1024 * 1024)\r
-#else\r
-#define _FPDFAPI_IMAGESIZE_LIMIT_      (10 * 1024 * 1024)\r
-#endif\r
-FX_BOOL CPDF_ScaledRenderBuffer::Initialize(CPDF_RenderContext* pContext, CFX_RenderDevice* pDevice, FX_RECT* pRect,\r
-        const CPDF_PageObject* pObj, const CPDF_RenderOptions *pOptions, int max_dpi)\r
-{\r
-    FXSYS_assert(pRect != NULL);\r
-    m_pDevice = pDevice;\r
-    if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {\r
-        return TRUE;\r
-    }\r
-    m_pContext = pContext;\r
-    m_Rect = *pRect;\r
-    m_pObject = pObj;\r
-    m_Matrix.TranslateI(-pRect->left, -pRect->top);\r
-    int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);\r
-    int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);\r
-    if (horz_size && vert_size && max_dpi) {\r
-        int dpih = pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);\r
-        int dpiv = pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);\r
-        if (dpih > max_dpi) {\r
-            m_Matrix.Scale((FX_FLOAT)(max_dpi) / dpih, 1.0f);\r
-        }\r
-        if (dpiv > max_dpi) {\r
-            m_Matrix.Scale(1.0f, (FX_FLOAT)(max_dpi) / (FX_FLOAT)dpiv);\r
-        }\r
-    }\r
-    m_pBitmapDevice = FX_NEW CFX_FxgeDevice;\r
-    FXDIB_Format dibFormat = FXDIB_Rgb;\r
-    FX_INT32 bpp = 24;\r
-    if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_ALPHA_OUTPUT) {\r
-        dibFormat = FXDIB_Argb;\r
-        bpp = 32;\r
-    }\r
-    CFX_FloatRect rect;\r
-    FX_INT32 iWidth, iHeight, iPitch;\r
-    while (1) {\r
-        rect = *pRect;\r
-        m_Matrix.TransformRect(rect);\r
-        FX_RECT bitmap_rect = rect.GetOutterRect();\r
-        iWidth = bitmap_rect.Width();\r
-        iHeight = bitmap_rect.Height();\r
-        iPitch = (iWidth * bpp + 31) / 32 * 4;\r
-        if (iWidth * iHeight < 1) {\r
-            return FALSE;\r
-        }\r
-        if (iPitch * iHeight <= _FPDFAPI_IMAGESIZE_LIMIT_ &&\r
-                m_pBitmapDevice->Create(iWidth, iHeight, dibFormat)) {\r
-            break;\r
-        }\r
-        m_Matrix.Scale(0.5f, 0.5f);\r
-    }\r
-#if  !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)\r
-    m_pContext->GetBackground(m_pBitmapDevice->GetBitmap(), m_pObject, pOptions, &m_Matrix);\r
-#endif\r
-    return TRUE;\r
-}\r
-void CPDF_ScaledRenderBuffer::OutputToDevice()\r
-{\r
-    if (m_pBitmapDevice) {\r
-        m_pDevice->StretchDIBits(m_pBitmapDevice->GetBitmap(), m_Rect.left, m_Rect.top, m_Rect.Width(), m_Rect.Height());\r
-    }\r
-}\r
-FX_BOOL IPDF_OCContext::CheckObjectVisible(const CPDF_PageObject* pObj)\r
-{\r
-    const CPDF_ContentMarkData* pData = pObj->m_ContentMark;\r
-    int nItems = pData->CountItems();\r
-    for (int i = 0; i < nItems; i ++) {\r
-        CPDF_ContentMarkItem& item = pData->GetItem(i);\r
-        if (item.GetName() == FX_BSTRC("OC") && item.GetParamType() == CPDF_ContentMarkItem::PropertiesDict) {\r
-            CPDF_Dictionary* pOCG = (CPDF_Dictionary*)item.GetParam();\r
-            if (!CheckOCGVisible(pOCG)) {\r
-                return FALSE;\r
-            }\r
-        }\r
-    }\r
-    return TRUE;\r
-}\r
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fpdfapi/fpdf_module.h"
+#include "../../../include/fpdfapi/fpdf_render.h"
+#include "../../../include/fxge/fx_ge.h"
+#include "../fpdf_page/pageint.h"
+#include "render_int.h"
+
+CPDF_DocRenderData::CPDF_DocRenderData(CPDF_Document* pPDFDoc)
+    : m_pPDFDoc(pPDFDoc), m_pFontCache(new CFX_FontCache) {}
+
+CPDF_DocRenderData::~CPDF_DocRenderData() {
+  Clear(TRUE);
+}
+
+void CPDF_DocRenderData::Clear(FX_BOOL bRelease) {
+  for (auto it = m_Type3FaceMap.begin(); it != m_Type3FaceMap.end();) {
+    auto curr_it = it++;
+    CPDF_CountedObject<CPDF_Type3Cache>* cache = curr_it->second;
+    if (bRelease || cache->use_count() < 2) {
+      delete cache->get();
+      delete cache;
+      m_Type3FaceMap.erase(curr_it);
+    }
+  }
+
+  for (auto it = m_TransferFuncMap.begin(); it != m_TransferFuncMap.end();) {
+    auto curr_it = it++;
+    CPDF_CountedObject<CPDF_TransferFunc>* value = curr_it->second;
+    if (bRelease || value->use_count() < 2) {
+      delete value->get();
+      delete value;
+      m_TransferFuncMap.erase(curr_it);
+    }
+  }
+
+  if (m_pFontCache) {
+    if (bRelease) {
+      delete m_pFontCache;
+      m_pFontCache = NULL;
+    } else {
+      m_pFontCache->FreeCache(FALSE);
+    }
+  }
+}
+
+CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) {
+  CPDF_CountedObject<CPDF_Type3Cache>* pCache;
+  auto it = m_Type3FaceMap.find(pFont);
+  if (it == m_Type3FaceMap.end()) {
+    CPDF_Type3Cache* pType3 = new CPDF_Type3Cache(pFont);
+    pCache = new CPDF_CountedObject<CPDF_Type3Cache>(pType3);
+    m_Type3FaceMap[pFont] = pCache;
+  } else {
+    pCache = it->second;
+  }
+  return pCache->AddRef();
+}
+
+void CPDF_DocRenderData::ReleaseCachedType3(CPDF_Type3Font* pFont) {
+  auto it = m_Type3FaceMap.find(pFont);
+  if (it != m_Type3FaceMap.end())
+    it->second->RemoveRef();
+}
+
+class CPDF_RenderModule : public IPDF_RenderModule {
+ public:
+  CPDF_RenderModule() {}
+
+ private:
+  ~CPDF_RenderModule() override {}
+
+  CPDF_DocRenderData* CreateDocData(CPDF_Document* pDoc) override;
+  void DestroyDocData(CPDF_DocRenderData* p) override;
+  void ClearDocData(CPDF_DocRenderData* p) override;
+
+  CPDF_DocRenderData* GetRenderData() override { return &m_RenderData; }
+
+  CPDF_PageRenderCache* CreatePageCache(CPDF_Page* pPage) override {
+    return new CPDF_PageRenderCache(pPage);
+  }
+
+  void DestroyPageCache(CPDF_PageRenderCache* pCache) override;
+
+  CPDF_RenderConfig* GetConfig() override { return &m_RenderConfig; }
+
+  CPDF_DocRenderData m_RenderData;
+  CPDF_RenderConfig m_RenderConfig;
+};
+
+CPDF_DocRenderData* CPDF_RenderModule::CreateDocData(CPDF_Document* pDoc) {
+  return new CPDF_DocRenderData(pDoc);
+}
+void CPDF_RenderModule::DestroyDocData(CPDF_DocRenderData* pDocData) {
+  delete pDocData;
+}
+void CPDF_RenderModule::ClearDocData(CPDF_DocRenderData* p) {
+  if (p) {
+    p->Clear(FALSE);
+  }
+}
+void CPDF_RenderModule::DestroyPageCache(CPDF_PageRenderCache* pCache) {
+  delete pCache;
+}
+
+void CPDF_ModuleMgr::InitRenderModule() {
+  m_pRenderModule.reset(new CPDF_RenderModule);
+}
+
+CPDF_RenderOptions::CPDF_RenderOptions()
+    : m_ColorMode(RENDER_COLOR_NORMAL),
+      m_Flags(RENDER_CLEARTYPE),
+      m_Interpolation(0),
+      m_AddFlags(0),
+      m_pOCContext(NULL),
+      m_dwLimitCacheSize(1024 * 1024 * 100),
+      m_HalftoneLimit(-1) {}
+FX_ARGB CPDF_RenderOptions::TranslateColor(FX_ARGB argb) const {
+  if (m_ColorMode == RENDER_COLOR_NORMAL) {
+    return argb;
+  }
+  if (m_ColorMode == RENDER_COLOR_ALPHA) {
+    return argb;
+  }
+  int a, r, g, b;
+  ArgbDecode(argb, a, r, g, b);
+  int gray = FXRGB2GRAY(r, g, b);
+  if (m_ColorMode == RENDER_COLOR_TWOCOLOR) {
+    int color = (r - gray) * (r - gray) + (g - gray) * (g - gray) +
+                (b - gray) * (b - gray);
+    if (gray < 35 && color < 20) {
+      return ArgbEncode(a, m_ForeColor);
+    }
+    if (gray > 221 && color < 20) {
+      return ArgbEncode(a, m_BackColor);
+    }
+    return argb;
+  }
+  int fr = FXSYS_GetRValue(m_ForeColor);
+  int fg = FXSYS_GetGValue(m_ForeColor);
+  int fb = FXSYS_GetBValue(m_ForeColor);
+  int br = FXSYS_GetRValue(m_BackColor);
+  int bg = FXSYS_GetGValue(m_BackColor);
+  int bb = FXSYS_GetBValue(m_BackColor);
+  r = (br - fr) * gray / 255 + fr;
+  g = (bg - fg) * gray / 255 + fg;
+  b = (bb - fb) * gray / 255 + fb;
+  return ArgbEncode(a, r, g, b);
+}
+
+// static
+int CPDF_RenderStatus::s_CurrentRecursionDepth = 0;
+
+CPDF_RenderStatus::CPDF_RenderStatus() {
+  m_pContext = NULL;
+  m_bStopped = FALSE;
+  m_pDevice = NULL;
+  m_pCurObj = NULL;
+  m_pStopObj = NULL;
+  m_HalftoneLimit = 0;
+  m_pObjectRenderer = NULL;
+  m_bPrint = FALSE;
+  m_Transparency = 0;
+  m_DitherBits = 0;
+  m_bDropObjects = FALSE;
+  m_bStdCS = FALSE;
+  m_GroupFamily = 0;
+  m_bLoadMask = FALSE;
+  m_pType3Char = NULL;
+  m_T3FillColor = 0;
+  m_pFormResource = NULL;
+  m_pPageResource = NULL;
+  m_curBlend = FXDIB_BLEND_NORMAL;
+}
+
+CPDF_RenderStatus::~CPDF_RenderStatus() {
+  delete m_pObjectRenderer;
+}
+
+FX_BOOL CPDF_RenderStatus::Initialize(CPDF_RenderContext* pContext,
+                                      CFX_RenderDevice* pDevice,
+                                      const CFX_AffineMatrix* pDeviceMatrix,
+                                      const CPDF_PageObject* pStopObj,
+                                      const CPDF_RenderStatus* pParentState,
+                                      const CPDF_GraphicStates* pInitialStates,
+                                      const CPDF_RenderOptions* pOptions,
+                                      int transparency,
+                                      FX_BOOL bDropObjects,
+                                      CPDF_Dictionary* pFormResource,
+                                      FX_BOOL bStdCS,
+                                      CPDF_Type3Char* pType3Char,
+                                      FX_ARGB fill_color,
+                                      FX_DWORD GroupFamily,
+                                      FX_BOOL bLoadMask) {
+  m_pContext = pContext;
+  m_pDevice = pDevice;
+  m_DitherBits = pDevice->GetDeviceCaps(FXDC_DITHER_BITS);
+  m_bPrint = m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
+  if (pDeviceMatrix) {
+    m_DeviceMatrix = *pDeviceMatrix;
+  }
+  m_pStopObj = pStopObj;
+  if (pOptions) {
+    m_Options = *pOptions;
+  }
+  m_bDropObjects = bDropObjects;
+  m_bStdCS = bStdCS;
+  m_T3FillColor = fill_color;
+  m_pType3Char = pType3Char;
+  m_GroupFamily = GroupFamily;
+  m_bLoadMask = bLoadMask;
+  m_pFormResource = pFormResource;
+  m_pPageResource = m_pContext->m_pPageResources;
+  if (pInitialStates && !m_pType3Char) {
+    m_InitialStates.CopyStates(*pInitialStates);
+    if (pParentState) {
+      CPDF_ColorStateData* pColorData =
+          (CPDF_ColorStateData*)(const CPDF_ColorStateData*)
+              m_InitialStates.m_ColorState;
+      CPDF_ColorStateData* pParentData =
+          (CPDF_ColorStateData*)(const CPDF_ColorStateData*)
+              pParentState->m_InitialStates.m_ColorState;
+      if (!pColorData || pColorData->m_FillColor.IsNull()) {
+        CPDF_ColorStateData* pData = m_InitialStates.m_ColorState.GetModify();
+        pData->m_FillRGB = pParentData->m_FillRGB;
+        pData->m_FillColor.Copy(&pParentData->m_FillColor);
+      }
+      if (!pColorData || pColorData->m_StrokeColor.IsNull()) {
+        CPDF_ColorStateData* pData = m_InitialStates.m_ColorState.GetModify();
+        pData->m_StrokeRGB = pParentData->m_FillRGB;
+        pData->m_StrokeColor.Copy(&pParentData->m_StrokeColor);
+      }
+    }
+  } else {
+    m_InitialStates.DefaultStates();
+  }
+  m_pObjectRenderer = NULL;
+  m_Transparency = transparency;
+  return TRUE;
+}
+void CPDF_RenderStatus::RenderObjectList(const CPDF_PageObjects* pObjs,
+                                         const CFX_AffineMatrix* pObj2Device) {
+  CFX_FloatRect clip_rect = m_pDevice->GetClipBox();
+  CFX_AffineMatrix device2object;
+  device2object.SetReverse(*pObj2Device);
+  device2object.TransformRect(clip_rect);
+  int index = 0;
+  FX_POSITION pos = pObjs->GetFirstObjectPosition();
+  while (pos) {
+    index++;
+    CPDF_PageObject* pCurObj = pObjs->GetNextObject(pos);
+    if (pCurObj == m_pStopObj) {
+      m_bStopped = TRUE;
+      return;
+    }
+    if (!pCurObj) {
+      continue;
+    }
+    if (pCurObj == NULL || pCurObj->m_Left > clip_rect.right ||
+        pCurObj->m_Right < clip_rect.left ||
+        pCurObj->m_Bottom > clip_rect.top ||
+        pCurObj->m_Top < clip_rect.bottom) {
+      continue;
+    }
+    RenderSingleObject(pCurObj, pObj2Device);
+    if (m_bStopped) {
+      return;
+    }
+  }
+}
+void CPDF_RenderStatus::RenderSingleObject(
+    const CPDF_PageObject* pObj,
+    const CFX_AffineMatrix* pObj2Device) {
+  CFX_AutoRestorer<int> restorer(&s_CurrentRecursionDepth);
+  if (++s_CurrentRecursionDepth > kRenderMaxRecursionDepth) {
+    return;
+  }
+  m_pCurObj = pObj;
+  if (m_Options.m_pOCContext && pObj->m_ContentMark.NotNull()) {
+    if (!m_Options.m_pOCContext->CheckObjectVisible(pObj)) {
+      return;
+    }
+  }
+  ProcessClipPath(pObj->m_ClipPath, pObj2Device);
+  if (ProcessTransparency(pObj, pObj2Device)) {
+    return;
+  }
+  ProcessObjectNoClip(pObj, pObj2Device);
+}
+FX_BOOL CPDF_RenderStatus::ContinueSingleObject(
+    const CPDF_PageObject* pObj,
+    const CFX_AffineMatrix* pObj2Device,
+    IFX_Pause* pPause) {
+  if (m_pObjectRenderer) {
+    if (m_pObjectRenderer->Continue(pPause)) {
+      return TRUE;
+    }
+    if (!m_pObjectRenderer->m_Result) {
+      DrawObjWithBackground(pObj, pObj2Device);
+    }
+    delete m_pObjectRenderer;
+    m_pObjectRenderer = NULL;
+    return FALSE;
+  }
+  m_pCurObj = pObj;
+  if (m_Options.m_pOCContext && pObj->m_ContentMark.NotNull())
+    if (!m_Options.m_pOCContext->CheckObjectVisible(pObj)) {
+      return FALSE;
+    }
+  ProcessClipPath(pObj->m_ClipPath, pObj2Device);
+  if (ProcessTransparency(pObj, pObj2Device)) {
+    return FALSE;
+  }
+  if (pObj->m_Type == PDFPAGE_IMAGE) {
+    m_pObjectRenderer = IPDF_ObjectRenderer::Create(pObj->m_Type);
+    if (!m_pObjectRenderer->Start(this, pObj, pObj2Device, FALSE)) {
+      if (!m_pObjectRenderer->m_Result) {
+        DrawObjWithBackground(pObj, pObj2Device);
+      }
+      delete m_pObjectRenderer;
+      m_pObjectRenderer = NULL;
+      return FALSE;
+    }
+    return ContinueSingleObject(pObj, pObj2Device, pPause);
+  }
+  ProcessObjectNoClip(pObj, pObj2Device);
+  return FALSE;
+}
+IPDF_ObjectRenderer* IPDF_ObjectRenderer::Create(int type) {
+  if (type != PDFPAGE_IMAGE) {
+    return NULL;
+  }
+  return new CPDF_ImageRenderer;
+}
+FX_BOOL CPDF_RenderStatus::GetObjectClippedRect(
+    const CPDF_PageObject* pObj,
+    const CFX_AffineMatrix* pObj2Device,
+    FX_BOOL bLogical,
+    FX_RECT& rect) const {
+  rect = pObj->GetBBox(pObj2Device);
+  FX_RECT rtClip = m_pDevice->GetClipBox();
+  if (!bLogical) {
+    CFX_Matrix dCTM = m_pDevice->GetCTM();
+    FX_FLOAT a = FXSYS_fabs(dCTM.a);
+    FX_FLOAT d = FXSYS_fabs(dCTM.d);
+    if (a != 1.0f || d != 1.0f) {
+      rect.right = rect.left + (int32_t)FXSYS_ceil((FX_FLOAT)rect.Width() * a);
+      rect.bottom = rect.top + (int32_t)FXSYS_ceil((FX_FLOAT)rect.Height() * d);
+      rtClip.right =
+          rtClip.left + (int32_t)FXSYS_ceil((FX_FLOAT)rtClip.Width() * a);
+      rtClip.bottom =
+          rtClip.top + (int32_t)FXSYS_ceil((FX_FLOAT)rtClip.Height() * d);
+    }
+  }
+  rect.Intersect(rtClip);
+  return rect.IsEmpty();
+}
+void CPDF_RenderStatus::DitherObjectArea(const CPDF_PageObject* pObj,
+                                         const CFX_AffineMatrix* pObj2Device) {
+  CFX_DIBitmap* pBitmap = m_pDevice->GetBitmap();
+  if (pBitmap == NULL) {
+    return;
+  }
+  FX_RECT rect;
+  if (GetObjectClippedRect(pObj, pObj2Device, FALSE, rect)) {
+    return;
+  }
+  if (m_DitherBits == 2) {
+    static FX_ARGB pal[4] = {0, 85, 170, 255};
+    pBitmap->DitherFS(pal, 4, &rect);
+  } else if (m_DitherBits == 3) {
+    static FX_ARGB pal[8] = {0, 36, 73, 109, 146, 182, 219, 255};
+    pBitmap->DitherFS(pal, 8, &rect);
+  } else if (m_DitherBits == 4) {
+    static FX_ARGB pal[16] = {0,   17,  34,  51,  68,  85,  102, 119,
+                              136, 153, 170, 187, 204, 221, 238, 255};
+    pBitmap->DitherFS(pal, 16, &rect);
+  }
+}
+void CPDF_RenderStatus::ProcessObjectNoClip(
+    const CPDF_PageObject* pObj,
+    const CFX_AffineMatrix* pObj2Device) {
+  FX_BOOL bRet = FALSE;
+  switch (pObj->m_Type) {
+    case PDFPAGE_TEXT:
+      bRet = ProcessText((CPDF_TextObject*)pObj, pObj2Device, NULL);
+      break;
+    case PDFPAGE_PATH:
+      bRet = ProcessPath((CPDF_PathObject*)pObj, pObj2Device);
+      break;
+    case PDFPAGE_IMAGE:
+      bRet = ProcessImage((CPDF_ImageObject*)pObj, pObj2Device);
+      break;
+    case PDFPAGE_SHADING:
+      bRet = ProcessShading((CPDF_ShadingObject*)pObj, pObj2Device);
+      break;
+    case PDFPAGE_FORM:
+      bRet = ProcessForm((CPDF_FormObject*)pObj, pObj2Device);
+      break;
+  }
+  if (!bRet) {
+    DrawObjWithBackground(pObj, pObj2Device);
+  }
+}
+FX_BOOL CPDF_RenderStatus::DrawObjWithBlend(
+    const CPDF_PageObject* pObj,
+    const CFX_AffineMatrix* pObj2Device) {
+  FX_BOOL bRet = FALSE;
+  switch (pObj->m_Type) {
+    case PDFPAGE_PATH:
+      bRet = ProcessPath((CPDF_PathObject*)pObj, pObj2Device);
+      break;
+    case PDFPAGE_IMAGE:
+      bRet = ProcessImage((CPDF_ImageObject*)pObj, pObj2Device);
+      break;
+    case PDFPAGE_FORM:
+      bRet = ProcessForm((CPDF_FormObject*)pObj, pObj2Device);
+      break;
+  }
+  return bRet;
+}
+void CPDF_RenderStatus::GetScaledMatrix(CFX_Matrix& matrix) const {
+  CFX_Matrix dCTM = m_pDevice->GetCTM();
+  matrix.a *= FXSYS_fabs(dCTM.a);
+  matrix.d *= FXSYS_fabs(dCTM.d);
+}
+void CPDF_RenderStatus::DrawObjWithBackground(
+    const CPDF_PageObject* pObj,
+    const CFX_AffineMatrix* pObj2Device) {
+  FX_RECT rect;
+  if (GetObjectClippedRect(pObj, pObj2Device, FALSE, rect)) {
+    return;
+  }
+  int res = 300;
+  if (pObj->m_Type == PDFPAGE_IMAGE &&
+      m_pDevice->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
+    res = 0;
+  }
+  CPDF_ScaledRenderBuffer buffer;
+  if (!buffer.Initialize(m_pContext, m_pDevice, &rect, pObj, &m_Options, res)) {
+    return;
+  }
+  CFX_AffineMatrix matrix = *pObj2Device;
+  matrix.Concat(*buffer.GetMatrix());
+  GetScaledMatrix(matrix);
+  CPDF_Dictionary* pFormResource = NULL;
+  if (pObj->m_Type == PDFPAGE_FORM) {
+    CPDF_FormObject* pFormObj = (CPDF_FormObject*)pObj;
+    if (pFormObj->m_pForm && pFormObj->m_pForm->m_pFormDict) {
+      pFormResource =
+          pFormObj->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));
+    }
+  }
+  CPDF_RenderStatus status;
+  status.Initialize(m_pContext, buffer.GetDevice(), buffer.GetMatrix(), NULL,
+                    NULL, NULL, &m_Options, m_Transparency, m_bDropObjects,
+                    pFormResource);
+  status.RenderSingleObject(pObj, &matrix);
+  buffer.OutputToDevice();
+}
+FX_BOOL CPDF_RenderStatus::ProcessForm(CPDF_FormObject* pFormObj,
+                                       const CFX_AffineMatrix* pObj2Device) {
+  CPDF_Dictionary* pOC =
+      pFormObj->m_pForm->m_pFormDict->GetDict(FX_BSTRC("OC"));
+  if (pOC && m_Options.m_pOCContext &&
+      !m_Options.m_pOCContext->CheckOCGVisible(pOC)) {
+    return TRUE;
+  }
+  CFX_AffineMatrix matrix = pFormObj->m_FormMatrix;
+  matrix.Concat(*pObj2Device);
+  CPDF_Dictionary* pResources = NULL;
+  if (pFormObj->m_pForm && pFormObj->m_pForm->m_pFormDict) {
+    pResources = pFormObj->m_pForm->m_pFormDict->GetDict(FX_BSTRC("Resources"));
+  }
+  CPDF_RenderStatus status;
+  status.Initialize(m_pContext, m_pDevice, NULL, m_pStopObj, this, pFormObj,
+                    &m_Options, m_Transparency, m_bDropObjects, pResources,
+                    FALSE);
+  status.m_curBlend = m_curBlend;
+  m_pDevice->SaveState();
+  status.RenderObjectList(pFormObj->m_pForm, &matrix);
+  m_bStopped = status.m_bStopped;
+  m_pDevice->RestoreState();
+  return TRUE;
+}
+FX_BOOL IsAvailableMatrix(const CFX_AffineMatrix& matrix) {
+  if (matrix.a == 0 || matrix.d == 0) {
+    return matrix.b != 0 && matrix.c != 0;
+  }
+  if (matrix.b == 0 || matrix.c == 0) {
+    return matrix.a != 0 && matrix.d != 0;
+  }
+  return TRUE;
+}
+FX_BOOL CPDF_RenderStatus::ProcessPath(CPDF_PathObject* pPathObj,
+                                       const CFX_AffineMatrix* pObj2Device) {
+  int FillType = pPathObj->m_FillType;
+  FX_BOOL bStroke = pPathObj->m_bStroke;
+  ProcessPathPattern(pPathObj, pObj2Device, FillType, bStroke);
+  if (FillType == 0 && !bStroke) {
+    return TRUE;
+  }
+  FX_DWORD fill_argb = 0;
+  if (FillType) {
+    fill_argb = GetFillArgb(pPathObj);
+  }
+  FX_DWORD stroke_argb = 0;
+  if (bStroke) {
+    stroke_argb = GetStrokeArgb(pPathObj);
+  }
+  CFX_AffineMatrix path_matrix = pPathObj->m_Matrix;
+  path_matrix.Concat(*pObj2Device);
+  if (!IsAvailableMatrix(path_matrix)) {
+    return TRUE;
+  }
+  if (FillType && (m_Options.m_Flags & RENDER_RECT_AA)) {
+    FillType |= FXFILL_RECT_AA;
+  }
+  if (m_Options.m_Flags & RENDER_FILL_FULLCOVER) {
+    FillType |= FXFILL_FULLCOVER;
+  }
+  if (m_Options.m_Flags & RENDER_NOPATHSMOOTH) {
+    FillType |= FXFILL_NOPATHSMOOTH;
+  }
+  if (bStroke) {
+    FillType |= FX_FILL_STROKE;
+  }
+  const CPDF_GeneralStateData* pGeneralData =
+      ((CPDF_PageObject*)pPathObj)->m_GeneralState;
+  if (pGeneralData && pGeneralData->m_StrokeAdjust) {
+    FillType |= FX_STROKE_ADJUST;
+  }
+  if (m_pType3Char) {
+    FillType |= FX_FILL_TEXT_MODE;
+  }
+  CFX_GraphStateData graphState(*pPathObj->m_GraphState);
+  if (m_Options.m_Flags & RENDER_THINLINE) {
+    graphState.m_LineWidth = 0;
+  }
+  return m_pDevice->DrawPath(pPathObj->m_Path, &path_matrix, &graphState,
+                             fill_argb, stroke_argb, FillType, 0, NULL,
+                             m_curBlend);
+}
+CPDF_TransferFunc* CPDF_RenderStatus::GetTransferFunc(CPDF_Object* pObj) const {
+  ASSERT(pObj != NULL);
+  CPDF_DocRenderData* pDocCache = m_pContext->m_pDocument->GetRenderData();
+  if (!pDocCache) {
+    return NULL;
+  }
+  return pDocCache->GetTransferFunc(pObj);
+}
+FX_ARGB CPDF_RenderStatus::GetFillArgb(const CPDF_PageObject* pObj,
+                                       FX_BOOL bType3) const {
+  CPDF_ColorStateData* pColorData =
+      (CPDF_ColorStateData*)(const CPDF_ColorStateData*)pObj->m_ColorState;
+  if (m_pType3Char && !bType3 &&
+      (!m_pType3Char->m_bColored ||
+       (m_pType3Char->m_bColored &&
+        (!pColorData || pColorData->m_FillColor.IsNull())))) {
+    return m_T3FillColor;
+  }
+  if (!pColorData || pColorData->m_FillColor.IsNull()) {
+    pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)
+                     m_InitialStates.m_ColorState;
+  }
+  FX_COLORREF rgb = pColorData->m_FillRGB;
+  if (rgb == (FX_DWORD)-1) {
+    return 0;
+  }
+  const CPDF_GeneralStateData* pGeneralData = pObj->m_GeneralState;
+  int alpha;
+  if (pGeneralData) {
+    alpha = (int32_t)(pGeneralData->m_FillAlpha * 255);
+    if (pGeneralData->m_pTR) {
+      if (!pGeneralData->m_pTransferFunc) {
+        ((CPDF_GeneralStateData*)pGeneralData)->m_pTransferFunc =
+            GetTransferFunc(pGeneralData->m_pTR);
+      }
+      if (pGeneralData->m_pTransferFunc) {
+        rgb = pGeneralData->m_pTransferFunc->TranslateColor(rgb);
+      }
+    }
+  } else {
+    alpha = 255;
+  }
+  return m_Options.TranslateColor(ArgbEncode(alpha, rgb));
+}
+FX_ARGB CPDF_RenderStatus::GetStrokeArgb(const CPDF_PageObject* pObj) const {
+  CPDF_ColorStateData* pColorData =
+      (CPDF_ColorStateData*)(const CPDF_ColorStateData*)pObj->m_ColorState;
+  if (m_pType3Char && (!m_pType3Char->m_bColored ||
+                       (m_pType3Char->m_bColored &&
+                        (!pColorData || pColorData->m_StrokeColor.IsNull())))) {
+    return m_T3FillColor;
+  }
+  if (!pColorData || pColorData->m_StrokeColor.IsNull()) {
+    pColorData = (CPDF_ColorStateData*)(const CPDF_ColorStateData*)
+                     m_InitialStates.m_ColorState;
+  }
+  FX_COLORREF rgb = pColorData->m_StrokeRGB;
+  if (rgb == (FX_DWORD)-1) {
+    return 0;
+  }
+  const CPDF_GeneralStateData* pGeneralData = pObj->m_GeneralState;
+  int alpha;
+  if (pGeneralData) {
+    alpha = (int32_t)(pGeneralData->m_StrokeAlpha * 255);
+    if (pGeneralData->m_pTR) {
+      if (!pGeneralData->m_pTransferFunc) {
+        ((CPDF_GeneralStateData*)pGeneralData)->m_pTransferFunc =
+            GetTransferFunc(pGeneralData->m_pTR);
+      }
+      if (pGeneralData->m_pTransferFunc) {
+        rgb = pGeneralData->m_pTransferFunc->TranslateColor(rgb);
+      }
+    }
+  } else {
+    alpha = 255;
+  }
+  return m_Options.TranslateColor(ArgbEncode(alpha, rgb));
+}
+void CPDF_RenderStatus::ProcessClipPath(CPDF_ClipPath ClipPath,
+                                        const CFX_AffineMatrix* pObj2Device) {
+  if (ClipPath.IsNull()) {
+    if (m_LastClipPath.IsNull()) {
+      return;
+    }
+    m_pDevice->RestoreState(TRUE);
+    m_LastClipPath.SetNull();
+    return;
+  }
+  if (m_LastClipPath == ClipPath) {
+    return;
+  }
+  m_LastClipPath = ClipPath;
+  m_pDevice->RestoreState(TRUE);
+  int nClipPath = ClipPath.GetPathCount();
+  int i;
+  for (i = 0; i < nClipPath; i++) {
+    const CFX_PathData* pPathData = ClipPath.GetPath(i);
+    if (pPathData == NULL) {
+      continue;
+    }
+    if (pPathData->GetPointCount() == 0) {
+      CFX_PathData EmptyPath;
+      EmptyPath.AppendRect(-1, -1, 0, 0);
+      int fill_mode = FXFILL_WINDING;
+      m_pDevice->SetClip_PathFill(&EmptyPath, NULL, fill_mode);
+    } else {
+      int ClipType = ClipPath.GetClipType(i);
+      m_pDevice->SetClip_PathFill(pPathData, pObj2Device, ClipType);
+    }
+  }
+  int textcount = ClipPath.GetTextCount();
+  if (textcount == 0) {
+    return;
+  }
+  if (m_pDevice->GetDeviceClass() == FXDC_DISPLAY &&
+      !(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_SOFT_CLIP)) {
+    return;
+  }
+  CFX_PathData* pTextClippingPath = NULL;
+  for (i = 0; i < textcount; i++) {
+    CPDF_TextObject* pText = ClipPath.GetText(i);
+    if (pText == NULL) {
+      if (pTextClippingPath) {
+        int fill_mode = FXFILL_WINDING;
+        if (m_Options.m_Flags & RENDER_NOTEXTSMOOTH) {
+          fill_mode |= FXFILL_NOPATHSMOOTH;
+        }
+        m_pDevice->SetClip_PathFill(pTextClippingPath, NULL, fill_mode);
+        delete pTextClippingPath;
+        pTextClippingPath = NULL;
+      }
+    } else {
+      if (pTextClippingPath == NULL) {
+        pTextClippingPath = new CFX_PathData;
+      }
+      ProcessText(pText, pObj2Device, pTextClippingPath);
+    }
+  }
+  delete pTextClippingPath;
+}
+void CPDF_RenderStatus::DrawClipPath(CPDF_ClipPath ClipPath,
+                                     const CFX_AffineMatrix* pObj2Device) {
+  if (ClipPath.IsNull()) {
+    return;
+  }
+  int fill_mode = 0;
+  if (m_Options.m_Flags & RENDER_NOPATHSMOOTH) {
+    fill_mode |= FXFILL_NOPATHSMOOTH;
+  }
+  int nClipPath = ClipPath.GetPathCount();
+  int i;
+  for (i = 0; i < nClipPath; i++) {
+    const CFX_PathData* pPathData = ClipPath.GetPath(i);
+    if (pPathData == NULL) {
+      continue;
+    }
+    CFX_GraphStateData stroke_state;
+    if (m_Options.m_Flags & RENDER_THINLINE) {
+      stroke_state.m_LineWidth = 0;
+    }
+    m_pDevice->DrawPath(pPathData, pObj2Device, &stroke_state, 0, 0xffff0000,
+                        fill_mode);
+  }
+}
+FX_BOOL CPDF_RenderStatus::SelectClipPath(CPDF_PathObject* pPathObj,
+                                          const CFX_AffineMatrix* pObj2Device,
+                                          FX_BOOL bStroke) {
+  CFX_AffineMatrix path_matrix = pPathObj->m_Matrix;
+  path_matrix.Concat(*pObj2Device);
+  if (bStroke) {
+    CFX_GraphStateData graphState(*pPathObj->m_GraphState);
+    if (m_Options.m_Flags & RENDER_THINLINE) {
+      graphState.m_LineWidth = 0;
+    }
+    return m_pDevice->SetClip_PathStroke(pPathObj->m_Path, &path_matrix,
+                                         &graphState);
+  }
+  int fill_mode = pPathObj->m_FillType;
+  if (m_Options.m_Flags & RENDER_NOPATHSMOOTH) {
+    fill_mode |= FXFILL_NOPATHSMOOTH;
+  }
+  return m_pDevice->SetClip_PathFill(pPathObj->m_Path, &path_matrix, fill_mode);
+}
+FX_BOOL CPDF_RenderStatus::ProcessTransparency(
+    const CPDF_PageObject* pPageObj,
+    const CFX_AffineMatrix* pObj2Device) {
+  const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
+  int blend_type =
+      pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
+  if (blend_type == FXDIB_BLEND_UNSUPPORTED) {
+    return TRUE;
+  }
+  CPDF_Dictionary* pSMaskDict =
+      pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
+  if (pSMaskDict) {
+    if (pPageObj->m_Type == PDFPAGE_IMAGE &&
+        ((CPDF_ImageObject*)pPageObj)
+            ->m_pImage->GetDict()
+            ->KeyExist(FX_BSTRC("SMask"))) {
+      pSMaskDict = NULL;
+    }
+  }
+  CPDF_Dictionary* pFormResource = NULL;
+  FX_FLOAT group_alpha = 1.0f;
+  int Transparency = m_Transparency;
+  FX_BOOL bGroupTransparent = FALSE;
+  if (pPageObj->m_Type == PDFPAGE_FORM) {
+    CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
+    const CPDF_GeneralStateData* pStateData =
+        pFormObj->m_GeneralState.GetObject();
+    if (pStateData) {
+      group_alpha = pStateData->m_FillAlpha;
+    }
+    Transparency = pFormObj->m_pForm->m_Transparency;
+    bGroupTransparent = !!(Transparency & PDFTRANS_ISOLATED);
+    if (pFormObj->m_pForm->m_pFormDict) {
+      pFormResource = pFormObj->m_pForm->m_pFormDict->GetDict("Resources");
+    }
+  }
+  FX_BOOL bTextClip = FALSE;
+  if (pPageObj->m_ClipPath.NotNull() && pPageObj->m_ClipPath.GetTextCount() &&
+      m_pDevice->GetDeviceClass() == FXDC_DISPLAY &&
+      !(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_SOFT_CLIP)) {
+    bTextClip = TRUE;
+  }
+  if ((m_Options.m_Flags & RENDER_OVERPRINT) &&
+      pPageObj->m_Type == PDFPAGE_IMAGE && pGeneralState &&
+      pGeneralState->m_FillOP && pGeneralState->m_StrokeOP) {
+    CPDF_Document* pDocument = NULL;
+    CPDF_Page* pPage = NULL;
+    if (m_pContext->m_pPageCache) {
+      pPage = m_pContext->m_pPageCache->GetPage();
+      pDocument = pPage->m_pDocument;
+    } else {
+      pDocument = ((CPDF_ImageObject*)pPageObj)->m_pImage->GetDocument();
+    }
+    CPDF_Dictionary* pPageResources = pPage ? pPage->m_pPageResources : NULL;
+    CPDF_Object* pCSObj = ((CPDF_ImageObject*)pPageObj)
+                              ->m_pImage->GetStream()
+                              ->GetDict()
+                              ->GetElementValue(FX_BSTRC("ColorSpace"));
+    CPDF_ColorSpace* pColorSpace =
+        pDocument->LoadColorSpace(pCSObj, pPageResources);
+    if (pColorSpace) {
+      int format = pColorSpace->GetFamily();
+      if (format == PDFCS_DEVICECMYK || format == PDFCS_SEPARATION ||
+          format == PDFCS_DEVICEN) {
+        blend_type = FXDIB_BLEND_DARKEN;
+      }
+      pDocument->GetPageData()->ReleaseColorSpace(pCSObj);
+    }
+  }
+  if (pSMaskDict == NULL && group_alpha == 1.0f &&
+      blend_type == FXDIB_BLEND_NORMAL && !bTextClip && !bGroupTransparent) {
+    return FALSE;
+  }
+  FX_BOOL isolated = Transparency & PDFTRANS_ISOLATED;
+  if (m_bPrint) {
+    FX_BOOL bRet = FALSE;
+    int rendCaps = m_pDevice->GetRenderCaps();
+    if (!((Transparency & PDFTRANS_ISOLATED) || pSMaskDict || bTextClip) &&
+        (rendCaps & FXRC_BLEND_MODE)) {
+      int oldBlend = m_curBlend;
+      m_curBlend = blend_type;
+      bRet = DrawObjWithBlend(pPageObj, pObj2Device);
+      m_curBlend = oldBlend;
+    }
+    if (!bRet) {
+      DrawObjWithBackground(pPageObj, pObj2Device);
+    }
+    return TRUE;
+  }
+  FX_RECT rect = pPageObj->GetBBox(pObj2Device);
+  rect.Intersect(m_pDevice->GetClipBox());
+  if (rect.IsEmpty()) {
+    return TRUE;
+  }
+  CFX_Matrix deviceCTM = m_pDevice->GetCTM();
+  FX_FLOAT scaleX = FXSYS_fabs(deviceCTM.a);
+  FX_FLOAT scaleY = FXSYS_fabs(deviceCTM.d);
+  int width = FXSYS_round((FX_FLOAT)rect.Width() * scaleX);
+  int height = FXSYS_round((FX_FLOAT)rect.Height() * scaleY);
+  CFX_FxgeDevice bitmap_device;
+  CFX_DIBitmap* oriDevice = NULL;
+  if (!isolated && (m_pDevice->GetRenderCaps() & FXRC_GET_BITS)) {
+    oriDevice = new CFX_DIBitmap;
+    if (!m_pDevice->CreateCompatibleBitmap(oriDevice, width, height)) {
+      return TRUE;
+    }
+    m_pDevice->GetDIBits(oriDevice, rect.left, rect.top);
+  }
+  if (!bitmap_device.Create(width, height, FXDIB_Argb, 0, oriDevice)) {
+    return TRUE;
+  }
+  CFX_DIBitmap* bitmap = bitmap_device.GetBitmap();
+  bitmap->Clear(0);
+  CFX_AffineMatrix new_matrix = *pObj2Device;
+  new_matrix.TranslateI(-rect.left, -rect.top);
+  new_matrix.Scale(scaleX, scaleY);
+  CFX_DIBitmap* pTextMask = NULL;
+  if (bTextClip) {
+    pTextMask = new CFX_DIBitmap;
+    if (!pTextMask->Create(width, height, FXDIB_8bppMask)) {
+      delete pTextMask;
+      return TRUE;
+    }
+    pTextMask->Clear(0);
+    CFX_FxgeDevice text_device;
+    text_device.Attach(pTextMask);
+    for (FX_DWORD i = 0; i < pPageObj->m_ClipPath.GetTextCount(); i++) {
+      CPDF_TextObject* textobj = pPageObj->m_ClipPath.GetText(i);
+      if (textobj == NULL) {
+        break;
+      }
+      CFX_AffineMatrix text_matrix;
+      textobj->GetTextMatrix(&text_matrix);
+      CPDF_TextRenderer::DrawTextPath(
+          &text_device, textobj->m_nChars, textobj->m_pCharCodes,
+          textobj->m_pCharPos, textobj->m_TextState.GetFont(),
+          textobj->m_TextState.GetFontSize(), &text_matrix, &new_matrix,
+          textobj->m_GraphState, (FX_ARGB)-1, 0, NULL);
+    }
+  }
+  CPDF_RenderStatus bitmap_render;
+  bitmap_render.Initialize(m_pContext, &bitmap_device, NULL, m_pStopObj, NULL,
+                           NULL, &m_Options, 0, m_bDropObjects, pFormResource,
+                           TRUE);
+  bitmap_render.ProcessObjectNoClip(pPageObj, &new_matrix);
+  m_bStopped = bitmap_render.m_bStopped;
+  if (pSMaskDict) {
+    CFX_AffineMatrix smask_matrix;
+    FXSYS_memcpy(&smask_matrix, pGeneralState->m_SMaskMatrix,
+                 sizeof smask_matrix);
+    smask_matrix.Concat(*pObj2Device);
+    CFX_DIBSource* pSMaskSource = LoadSMask(pSMaskDict, &rect, &smask_matrix);
+    if (pSMaskSource) {
+      bitmap->MultiplyAlpha(pSMaskSource);
+      delete pSMaskSource;
+    }
+  }
+  if (pTextMask) {
+    bitmap->MultiplyAlpha(pTextMask);
+    delete pTextMask;
+    pTextMask = NULL;
+  }
+  if (Transparency & PDFTRANS_GROUP && group_alpha != 1.0f) {
+    bitmap->MultiplyAlpha((int32_t)(group_alpha * 255));
+  }
+  Transparency = m_Transparency;
+  if (pPageObj->m_Type == PDFPAGE_FORM) {
+    Transparency |= PDFTRANS_GROUP;
+  }
+  CompositeDIBitmap(bitmap, rect.left, rect.top, 0, 255, blend_type,
+                    Transparency);
+  delete oriDevice;
+  return TRUE;
+}
+CFX_DIBitmap* CPDF_RenderStatus::GetBackdrop(const CPDF_PageObject* pObj,
+                                             const FX_RECT& rect,
+                                             int& left,
+                                             int& top,
+                                             FX_BOOL bBackAlphaRequired) {
+  FX_RECT bbox = rect;
+  bbox.Intersect(m_pDevice->GetClipBox());
+  left = bbox.left;
+  top = bbox.top;
+  CFX_Matrix deviceCTM = m_pDevice->GetCTM();
+  FX_FLOAT scaleX = FXSYS_fabs(deviceCTM.a);
+  FX_FLOAT scaleY = FXSYS_fabs(deviceCTM.d);
+  int width = FXSYS_round(bbox.Width() * scaleX);
+  int height = FXSYS_round(bbox.Height() * scaleY);
+  CFX_DIBitmap* pBackdrop = new CFX_DIBitmap;
+  if (bBackAlphaRequired && !m_bDropObjects) {
+    pBackdrop->Create(width, height, FXDIB_Argb);
+  } else {
+    m_pDevice->CreateCompatibleBitmap(pBackdrop, width, height);
+  }
+  if (pBackdrop->GetBuffer() == NULL) {
+    delete pBackdrop;
+    return NULL;
+  }
+  FX_BOOL bNeedDraw;
+  if (pBackdrop->HasAlpha()) {
+    bNeedDraw = !(m_pDevice->GetRenderCaps() & FXRC_ALPHA_OUTPUT);
+  } else {
+    bNeedDraw = !(m_pDevice->GetRenderCaps() & FXRC_GET_BITS);
+  }
+  if (!bNeedDraw) {
+    m_pDevice->GetDIBits(pBackdrop, left, top);
+    return pBackdrop;
+  }
+  CFX_AffineMatrix FinalMatrix = m_DeviceMatrix;
+  FinalMatrix.TranslateI(-left, -top);
+  FinalMatrix.Scale(scaleX, scaleY);
+  pBackdrop->Clear(pBackdrop->HasAlpha() ? 0 : 0xffffffff);
+  CFX_FxgeDevice device;
+  device.Attach(pBackdrop);
+  m_pContext->Render(&device, pObj, &m_Options, &FinalMatrix);
+  return pBackdrop;
+}
+void CPDF_RenderContext::GetBackground(CFX_DIBitmap* pBuffer,
+                                       const CPDF_PageObject* pObj,
+                                       const CPDF_RenderOptions* pOptions,
+                                       CFX_AffineMatrix* pFinalMatrix) {
+  CFX_FxgeDevice device;
+  device.Attach(pBuffer);
+
+  FX_RECT rect(0, 0, device.GetWidth(), device.GetHeight());
+  device.FillRect(&rect, 0xffffffff);
+  Render(&device, pObj, pOptions, pFinalMatrix);
+}
+CPDF_GraphicStates* CPDF_RenderStatus::CloneObjStates(
+    const CPDF_GraphicStates* pSrcStates,
+    FX_BOOL bStroke) {
+  if (!pSrcStates) {
+    return NULL;
+  }
+  CPDF_GraphicStates* pStates = new CPDF_GraphicStates;
+  pStates->CopyStates(*pSrcStates);
+  CPDF_Color* pObjColor = bStroke ? pSrcStates->m_ColorState.GetStrokeColor()
+                                  : pSrcStates->m_ColorState.GetFillColor();
+  if (!pObjColor->IsNull()) {
+    CPDF_ColorStateData* pColorData = pStates->m_ColorState.GetModify();
+    pColorData->m_FillRGB =
+        bStroke ? pSrcStates->m_ColorState.GetObject()->m_StrokeRGB
+                : pSrcStates->m_ColorState.GetObject()->m_FillRGB;
+    pColorData->m_StrokeRGB = pColorData->m_FillRGB;
+  }
+  return pStates;
+}
+CPDF_RenderContext::CPDF_RenderContext() {}
+void CPDF_RenderContext::Create(CPDF_Document* pDoc,
+                                CPDF_PageRenderCache* pPageCache,
+                                CPDF_Dictionary* pPageResources,
+                                FX_BOOL bFirstLayer) {
+  m_pDocument = pDoc;
+  m_pPageResources = pPageResources;
+  m_pPageCache = pPageCache;
+  m_bFirstLayer = bFirstLayer;
+}
+void CPDF_RenderContext::Create(CPDF_Page* pPage, FX_BOOL bFirstLayer) {
+  m_pDocument = pPage->m_pDocument;
+  m_pPageResources = pPage->m_pPageResources;
+  m_pPageCache = pPage->GetRenderCache();
+  m_bFirstLayer = bFirstLayer;
+}
+CPDF_RenderContext::~CPDF_RenderContext() {}
+void CPDF_RenderContext::Clear() {
+  m_pDocument = NULL;
+  m_pPageResources = NULL;
+  m_pPageCache = NULL;
+  m_bFirstLayer = TRUE;
+  m_ContentList.RemoveAll();
+}
+void CPDF_RenderContext::AppendObjectList(
+    CPDF_PageObjects* pObjs,
+    const CFX_AffineMatrix* pObject2Device) {
+  _PDF_RenderItem* pItem = m_ContentList.AddSpace();
+  pItem->m_pObjectList = pObjs;
+  if (pObject2Device) {
+    pItem->m_Matrix = *pObject2Device;
+  } else {
+    pItem->m_Matrix.SetIdentity();
+  }
+}
+void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice,
+                                const CPDF_RenderOptions* pOptions,
+                                const CFX_AffineMatrix* pLastMatrix) {
+  Render(pDevice, NULL, pOptions, pLastMatrix);
+}
+void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice,
+                                const CPDF_PageObject* pStopObj,
+                                const CPDF_RenderOptions* pOptions,
+                                const CFX_AffineMatrix* pLastMatrix) {
+  int count = m_ContentList.GetSize();
+  for (int j = 0; j < count; j++) {
+    pDevice->SaveState();
+    _PDF_RenderItem* pItem = m_ContentList.GetDataPtr(j);
+    if (pLastMatrix) {
+      CFX_AffineMatrix FinalMatrix = pItem->m_Matrix;
+      FinalMatrix.Concat(*pLastMatrix);
+      CPDF_RenderStatus status;
+      status.Initialize(this, pDevice, pLastMatrix, pStopObj, NULL, NULL,
+                        pOptions, pItem->m_pObjectList->m_Transparency, FALSE,
+                        NULL);
+      status.RenderObjectList(pItem->m_pObjectList, &FinalMatrix);
+      if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) {
+        m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize);
+      }
+      if (status.m_bStopped) {
+        pDevice->RestoreState();
+        break;
+      }
+    } else {
+      CPDF_RenderStatus status;
+      status.Initialize(this, pDevice, NULL, pStopObj, NULL, NULL, pOptions,
+                        pItem->m_pObjectList->m_Transparency, FALSE, NULL);
+      status.RenderObjectList(pItem->m_pObjectList, &pItem->m_Matrix);
+      if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) {
+        m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize);
+      }
+      if (status.m_bStopped) {
+        pDevice->RestoreState();
+        break;
+      }
+    }
+    pDevice->RestoreState();
+  }
+}
+void CPDF_RenderContext::DrawObjectList(CFX_RenderDevice* pDevice,
+                                        CPDF_PageObjects* pObjs,
+                                        const CFX_AffineMatrix* pObject2Device,
+                                        const CPDF_RenderOptions* pOptions) {
+  AppendObjectList(pObjs, pObject2Device);
+  Render(pDevice, pOptions);
+}
+
+CPDF_ProgressiveRenderer::CPDF_ProgressiveRenderer(
+    CPDF_RenderContext* pContext,
+    CFX_RenderDevice* pDevice,
+    const CPDF_RenderOptions* pOptions)
+    : m_Status(Ready),
+      m_pContext(pContext),
+      m_pDevice(pDevice),
+      m_pOptions(pOptions),
+      m_LayerIndex(0),
+      m_ObjectIndex(0),
+      m_ObjectPos(nullptr),
+      m_PrevLastPos(nullptr) {
+}
+
+CPDF_ProgressiveRenderer::~CPDF_ProgressiveRenderer() {
+  if (m_pRenderStatus)
+    m_pDevice->RestoreState();
+}
+
+void CPDF_ProgressiveRenderer::Start(IFX_Pause* pPause) {
+  if (!m_pContext || !m_pDevice || m_Status != Ready) {
+    m_Status = Failed;
+    return;
+  }
+  m_Status = ToBeContinued;
+  Continue(pPause);
+}
+
+#define RENDER_STEP_LIMIT 100
+void CPDF_ProgressiveRenderer::Continue(IFX_Pause* pPause) {
+  if (m_Status != ToBeContinued) {
+    return;
+  }
+  FX_DWORD nLayers = m_pContext->m_ContentList.GetSize();
+  for (; m_LayerIndex < nLayers; m_LayerIndex++) {
+    _PDF_RenderItem* pItem = m_pContext->m_ContentList.GetDataPtr(m_LayerIndex);
+    FX_POSITION LastPos = pItem->m_pObjectList->GetLastObjectPosition();
+    if (m_ObjectPos == NULL) {
+      if (LastPos == m_PrevLastPos) {
+        if (!pItem->m_pObjectList->IsParsed()) {
+          pItem->m_pObjectList->ContinueParse(pPause);
+          if (!pItem->m_pObjectList->IsParsed()) {
+            return;
+          }
+          LastPos = pItem->m_pObjectList->GetLastObjectPosition();
+        }
+      }
+      if (LastPos == m_PrevLastPos) {
+        if (m_pRenderStatus) {
+          m_pRenderStatus.reset();
+          m_pDevice->RestoreState();
+          m_ObjectPos = NULL;
+          m_PrevLastPos = NULL;
+        }
+        continue;
+      }
+      if (m_PrevLastPos) {
+        m_ObjectPos = m_PrevLastPos;
+        pItem->m_pObjectList->GetNextObject(m_ObjectPos);
+      } else {
+        m_ObjectPos = pItem->m_pObjectList->GetFirstObjectPosition();
+      }
+      m_PrevLastPos = LastPos;
+    }
+    if (!m_pRenderStatus) {
+      m_ObjectPos = pItem->m_pObjectList->GetFirstObjectPosition();
+      m_ObjectIndex = 0;
+      m_pRenderStatus.reset(new CPDF_RenderStatus());
+      m_pRenderStatus->Initialize(
+          m_pContext, m_pDevice, NULL, NULL, NULL, NULL, m_pOptions,
+          pItem->m_pObjectList->m_Transparency, FALSE, NULL);
+      m_pDevice->SaveState();
+      m_ClipRect = m_pDevice->GetClipBox();
+      CFX_AffineMatrix device2object;
+      device2object.SetReverse(pItem->m_Matrix);
+      device2object.TransformRect(m_ClipRect);
+    }
+    int objs_to_go = CPDF_ModuleMgr::Get()
+                         ->GetRenderModule()
+                         ->GetConfig()
+                         ->m_RenderStepLimit;
+    while (m_ObjectPos) {
+      CPDF_PageObject* pCurObj = pItem->m_pObjectList->GetObjectAt(m_ObjectPos);
+      if (pCurObj && pCurObj->m_Left <= m_ClipRect.right &&
+          pCurObj->m_Right >= m_ClipRect.left &&
+          pCurObj->m_Bottom <= m_ClipRect.top &&
+          pCurObj->m_Top >= m_ClipRect.bottom) {
+        if (m_pRenderStatus->ContinueSingleObject(pCurObj, &pItem->m_Matrix,
+                                                  pPause)) {
+          return;
+        }
+        if (pCurObj->m_Type == PDFPAGE_IMAGE &&
+            m_pRenderStatus->m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE) {
+          m_pContext->GetPageCache()->CacheOptimization(
+              m_pRenderStatus->m_Options.m_dwLimitCacheSize);
+        }
+        if (pCurObj->m_Type == PDFPAGE_FORM ||
+            pCurObj->m_Type == PDFPAGE_SHADING) {
+          objs_to_go = 0;
+        } else {
+          objs_to_go--;
+        }
+      }
+      m_ObjectIndex++;
+      pItem->m_pObjectList->GetNextObject(m_ObjectPos);
+      if (objs_to_go == 0) {
+        if (pPause && pPause->NeedToPauseNow()) {
+          return;
+        }
+        objs_to_go = CPDF_ModuleMgr::Get()
+                         ->GetRenderModule()
+                         ->GetConfig()
+                         ->m_RenderStepLimit;
+      }
+    }
+    if (!pItem->m_pObjectList->IsParsed()) {
+      return;
+    }
+    m_pRenderStatus.reset();
+    m_pDevice->RestoreState();
+    m_ObjectPos = NULL;
+    m_PrevLastPos = NULL;
+    if (pPause && pPause->NeedToPauseNow()) {
+      m_LayerIndex++;
+      return;
+    }
+  }
+  m_Status = Done;
+}
+int CPDF_ProgressiveRenderer::EstimateProgress() {
+  if (!m_pContext) {
+    return 0;
+  }
+  FX_DWORD nLayers = m_pContext->m_ContentList.GetSize();
+  int nTotal = 0, nRendered = 0;
+  for (FX_DWORD layer = 0; layer < nLayers; layer++) {
+    _PDF_RenderItem* pItem = m_pContext->m_ContentList.GetDataPtr(layer);
+    int nObjs = pItem->m_pObjectList->CountObjects();
+    if (layer == m_LayerIndex) {
+      nRendered += m_ObjectIndex;
+    } else if (layer < m_LayerIndex) {
+      nRendered += nObjs;
+    }
+    nTotal += nObjs;
+  }
+  if (nTotal == 0) {
+    return 0;
+  }
+  return 100 * nRendered / nTotal;
+}
+CPDF_TransferFunc* CPDF_DocRenderData::GetTransferFunc(CPDF_Object* pObj) {
+  if (!pObj)
+    return nullptr;
+
+  auto it = m_TransferFuncMap.find(pObj);
+  if (it != m_TransferFuncMap.end()) {
+    CPDF_CountedObject<CPDF_TransferFunc>* pTransferCounter = it->second;
+    return pTransferCounter->AddRef();
+  }
+
+  CPDF_Function* pFuncs[3] = {nullptr, nullptr, nullptr};
+  FX_BOOL bUniTransfer = TRUE;
+  FX_BOOL bIdentity = TRUE;
+  if (CPDF_Array* pArray = pObj->AsArray()) {
+    bUniTransfer = FALSE;
+    if (pArray->GetCount() < 3)
+      return nullptr;
+
+    for (FX_DWORD i = 0; i < 3; ++i) {
+      pFuncs[2 - i] = CPDF_Function::Load(pArray->GetElementValue(i));
+      if (!pFuncs[2 - i]) {
+        return nullptr;
+      }
+    }
+  } else {
+    pFuncs[0] = CPDF_Function::Load(pObj);
+    if (!pFuncs[0]) {
+      return nullptr;
+    }
+  }
+  CPDF_TransferFunc* pTransfer = new CPDF_TransferFunc;
+  pTransfer->m_pPDFDoc = m_pPDFDoc;
+  CPDF_CountedObject<CPDF_TransferFunc>* pTransferCounter =
+      new CPDF_CountedObject<CPDF_TransferFunc>(pTransfer);
+  m_TransferFuncMap[pObj] = pTransferCounter;
+  static const int kMaxOutputs = 16;
+  FX_FLOAT output[kMaxOutputs];
+  FXSYS_memset(output, 0, sizeof(output));
+  FX_FLOAT input;
+  int noutput;
+  for (int v = 0; v < 256; ++v) {
+    input = (FX_FLOAT)v / 255.0f;
+    if (bUniTransfer) {
+      if (pFuncs[0] && pFuncs[0]->CountOutputs() <= kMaxOutputs)
+        pFuncs[0]->Call(&input, 1, output, noutput);
+      int o = FXSYS_round(output[0] * 255);
+      if (o != v)
+        bIdentity = FALSE;
+      for (int i = 0; i < 3; ++i) {
+        pTransfer->m_Samples[i * 256 + v] = o;
+      }
+    } else {
+      for (int i = 0; i < 3; ++i) {
+        if (pFuncs[i] && pFuncs[i]->CountOutputs() <= kMaxOutputs) {
+          pFuncs[i]->Call(&input, 1, output, noutput);
+          int o = FXSYS_round(output[0] * 255);
+          if (o != v)
+            bIdentity = FALSE;
+          pTransfer->m_Samples[i * 256 + v] = o;
+        } else {
+          pTransfer->m_Samples[i * 256 + v] = v;
+        }
+      }
+    }
+  }
+  for (int i = 0; i < 3; ++i)
+    delete pFuncs[i];
+
+  pTransfer->m_bIdentity = bIdentity;
+  return pTransferCounter->AddRef();
+}
+
+void CPDF_DocRenderData::ReleaseTransferFunc(CPDF_Object* pObj) {
+  auto it = m_TransferFuncMap.find(pObj);
+  if (it != m_TransferFuncMap.end())
+    it->second->RemoveRef();
+}
+CPDF_RenderConfig::CPDF_RenderConfig() {
+  m_HalftoneLimit = 0;
+  m_RenderStepLimit = 100;
+}
+CPDF_RenderConfig::~CPDF_RenderConfig() {}
+CPDF_DeviceBuffer::CPDF_DeviceBuffer() {
+  m_pBitmap = NULL;
+  m_pDevice = NULL;
+  m_pContext = NULL;
+  m_pObject = NULL;
+}
+CPDF_DeviceBuffer::~CPDF_DeviceBuffer() {
+  delete m_pBitmap;
+}
+FX_BOOL CPDF_DeviceBuffer::Initialize(CPDF_RenderContext* pContext,
+                                      CFX_RenderDevice* pDevice,
+                                      FX_RECT* pRect,
+                                      const CPDF_PageObject* pObj,
+                                      int max_dpi) {
+  m_pDevice = pDevice;
+  m_pContext = pContext;
+  m_Rect = *pRect;
+  m_pObject = pObj;
+  m_Matrix.TranslateI(-pRect->left, -pRect->top);
+#if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
+  int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);
+  int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);
+  if (horz_size && vert_size && max_dpi) {
+    int dpih =
+        pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);
+    int dpiv =
+        pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);
+    if (dpih > max_dpi) {
+      m_Matrix.Scale((FX_FLOAT)(max_dpi) / dpih, 1.0f);
+    }
+    if (dpiv > max_dpi) {
+      m_Matrix.Scale(1.0f, (FX_FLOAT)(max_dpi) / (FX_FLOAT)dpiv);
+    }
+  }
+#endif
+  CFX_Matrix ctm = m_pDevice->GetCTM();
+  FX_FLOAT fScaleX = FXSYS_fabs(ctm.a);
+  FX_FLOAT fScaleY = FXSYS_fabs(ctm.d);
+  m_Matrix.Concat(fScaleX, 0, 0, fScaleY, 0, 0);
+  CFX_FloatRect rect(*pRect);
+  m_Matrix.TransformRect(rect);
+  FX_RECT bitmap_rect = rect.GetOutterRect();
+  m_pBitmap = new CFX_DIBitmap;
+  m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(), FXDIB_Argb);
+  return TRUE;
+}
+void CPDF_DeviceBuffer::OutputToDevice() {
+  if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {
+    if (m_Matrix.a == 1.0f && m_Matrix.d == 1.0f) {
+      m_pDevice->SetDIBits(m_pBitmap, m_Rect.left, m_Rect.top);
+    } else {
+      m_pDevice->StretchDIBits(m_pBitmap, m_Rect.left, m_Rect.top,
+                               m_Rect.Width(), m_Rect.Height());
+    }
+  } else {
+    CFX_DIBitmap buffer;
+    m_pDevice->CreateCompatibleBitmap(&buffer, m_pBitmap->GetWidth(),
+                                      m_pBitmap->GetHeight());
+    m_pContext->GetBackground(&buffer, m_pObject, NULL, &m_Matrix);
+    buffer.CompositeBitmap(0, 0, buffer.GetWidth(), buffer.GetHeight(),
+                           m_pBitmap, 0, 0);
+    m_pDevice->StretchDIBits(&buffer, m_Rect.left, m_Rect.top, m_Rect.Width(),
+                             m_Rect.Height());
+  }
+}
+CPDF_ScaledRenderBuffer::CPDF_ScaledRenderBuffer() {
+  m_pBitmapDevice = NULL;
+}
+CPDF_ScaledRenderBuffer::~CPDF_ScaledRenderBuffer() {
+  delete m_pBitmapDevice;
+}
+#define _FPDFAPI_IMAGESIZE_LIMIT_ (30 * 1024 * 1024)
+FX_BOOL CPDF_ScaledRenderBuffer::Initialize(CPDF_RenderContext* pContext,
+                                            CFX_RenderDevice* pDevice,
+                                            FX_RECT* pRect,
+                                            const CPDF_PageObject* pObj,
+                                            const CPDF_RenderOptions* pOptions,
+                                            int max_dpi) {
+  FXSYS_assert(pRect != NULL);
+  m_pDevice = pDevice;
+  if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {
+    return TRUE;
+  }
+  m_pContext = pContext;
+  m_Rect = *pRect;
+  m_pObject = pObj;
+  m_Matrix.TranslateI(-pRect->left, -pRect->top);
+  int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);
+  int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);
+  if (horz_size && vert_size && max_dpi) {
+    int dpih =
+        pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);
+    int dpiv =
+        pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);
+    if (dpih > max_dpi) {
+      m_Matrix.Scale((FX_FLOAT)(max_dpi) / dpih, 1.0f);
+    }
+    if (dpiv > max_dpi) {
+      m_Matrix.Scale(1.0f, (FX_FLOAT)(max_dpi) / (FX_FLOAT)dpiv);
+    }
+  }
+  m_pBitmapDevice = new CFX_FxgeDevice;
+  FXDIB_Format dibFormat = FXDIB_Rgb;
+  int32_t bpp = 24;
+  if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_ALPHA_OUTPUT) {
+    dibFormat = FXDIB_Argb;
+    bpp = 32;
+  }
+  CFX_FloatRect rect;
+  int32_t iWidth, iHeight, iPitch;
+  while (1) {
+    rect = *pRect;
+    m_Matrix.TransformRect(rect);
+    FX_RECT bitmap_rect = rect.GetOutterRect();
+    iWidth = bitmap_rect.Width();
+    iHeight = bitmap_rect.Height();
+    iPitch = (iWidth * bpp + 31) / 32 * 4;
+    if (iWidth * iHeight < 1) {
+      return FALSE;
+    }
+    if (iPitch * iHeight <= _FPDFAPI_IMAGESIZE_LIMIT_ &&
+        m_pBitmapDevice->Create(iWidth, iHeight, dibFormat)) {
+      break;
+    }
+    m_Matrix.Scale(0.5f, 0.5f);
+  }
+  m_pContext->GetBackground(m_pBitmapDevice->GetBitmap(), m_pObject, pOptions,
+                            &m_Matrix);
+  return TRUE;
+}
+void CPDF_ScaledRenderBuffer::OutputToDevice() {
+  if (m_pBitmapDevice) {
+    m_pDevice->StretchDIBits(m_pBitmapDevice->GetBitmap(), m_Rect.left,
+                             m_Rect.top, m_Rect.Width(), m_Rect.Height());
+  }
+}
+FX_BOOL IPDF_OCContext::CheckObjectVisible(const CPDF_PageObject* pObj) {
+  const CPDF_ContentMarkData* pData = pObj->m_ContentMark;
+  int nItems = pData->CountItems();
+  for (int i = 0; i < nItems; i++) {
+    CPDF_ContentMarkItem& item = pData->GetItem(i);
+    if (item.GetName() == FX_BSTRC("OC") &&
+        item.GetParamType() == CPDF_ContentMarkItem::PropertiesDict) {
+      CPDF_Dictionary* pOCG =
+          ToDictionary(static_cast<CPDF_Object*>(item.GetParam()));
+      if (!CheckOCGVisible(pOCG)) {
+        return FALSE;
+      }
+    }
+  }
+  return TRUE;
+}