Fix warnings in android build, fix font rendering issue, fix issue 357588: wrong...
[pdfium.git] / core / src / fpdfdoc / doc_annot.cpp
index da30485..227578a 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/fpdfdoc/fpdf_doc.h"\r
-#include "../../include/fpdfapi/fpdf_pageobj.h"\r
-CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)\r
-{\r
-    ASSERT(pPage != NULL);\r
-    m_pPageDict = pPage->m_pFormDict;\r
-    if (m_pPageDict == NULL) {\r
-        return;\r
-    }\r
-    m_pDocument = pPage->m_pDocument;\r
-    CPDF_Array* pAnnots = m_pPageDict->GetArray("Annots");\r
-    if (pAnnots == NULL) {\r
-        return;\r
-    }\r
-    CPDF_Dictionary* pRoot = m_pDocument->GetRoot();\r
-    CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");\r
-    FX_BOOL bRegenerateAP = pAcroForm && pAcroForm->GetBoolean("NeedAppearances");\r
-    for (FX_DWORD i = 0; i < pAnnots->GetCount(); i ++) {\r
-        CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);\r
-        if (pDict == NULL || pDict->GetType() != PDFOBJ_DICTIONARY) {\r
-            continue;\r
-        }\r
-        FX_DWORD dwObjNum = pDict->GetObjNum();\r
-        if (dwObjNum == 0) {\r
-            dwObjNum = m_pDocument->AddIndirectObject(pDict);\r
-            CPDF_Reference* pAction = CPDF_Reference::Create(m_pDocument, dwObjNum);\r
-            if (pAction == NULL) {\r
-                break;\r
-            }\r
-            pAnnots->InsertAt(i, pAction);\r
-            pAnnots->RemoveAt(i + 1);\r
-            pDict = pAnnots->GetDict(i);\r
-        }\r
-        CPDF_Annot* pAnnot = FX_NEW CPDF_Annot(pDict);\r
-        if (pAnnot == NULL) {\r
-            break;\r
-        }\r
-        pAnnot->m_pList = this;\r
-        m_AnnotList.Add(pAnnot);\r
-        if (bRegenerateAP && pDict->GetConstString(FX_BSTRC("Subtype")) == FX_BSTRC("Widget"))\r
-            if (CPDF_InterForm::UpdatingAPEnabled()) {\r
-                FPDF_GenerateAP(m_pDocument, pDict);\r
-            }\r
-    }\r
-}\r
-CPDF_AnnotList::~CPDF_AnnotList()\r
-{\r
-    int i = 0;\r
-    for (i = 0; i < m_AnnotList.GetSize(); i ++) {\r
-        delete (CPDF_Annot*)m_AnnotList[i];\r
-    }\r
-    for (i = 0; i < m_Borders.GetSize(); ++i) {\r
-        delete (CPDF_PageObjects*)m_Borders[i];\r
-    }\r
-}\r
-void CPDF_AnnotList::DisplayPass(const CPDF_Page* pPage, CFX_RenderDevice* pDevice,\r
-                                 CPDF_RenderContext* pContext, FX_BOOL bPrinting, CFX_AffineMatrix* pMatrix,\r
-                                 FX_BOOL bWidgetPass, CPDF_RenderOptions* pOptions, FX_RECT* clip_rect)\r
-{\r
-    for (int i = 0; i < m_AnnotList.GetSize(); i ++) {\r
-        CPDF_Annot* pAnnot = (CPDF_Annot*)m_AnnotList[i];\r
-        FX_BOOL bWidget = pAnnot->GetSubType() == "Widget";\r
-        if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget)) {\r
-            continue;\r
-        }\r
-        FX_DWORD annot_flags = pAnnot->GetFlags();\r
-        if (annot_flags & ANNOTFLAG_HIDDEN) {\r
-            continue;\r
-        }\r
-        if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) {\r
-            continue;\r
-        }\r
-        if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) {\r
-            continue;\r
-        }\r
-        if (pOptions != NULL) {\r
-            IPDF_OCContext* pOCContext = pOptions->m_pOCContext;\r
-            CPDF_Dictionary* pAnnotDict = pAnnot->m_pAnnotDict;\r
-            if (pOCContext != NULL && pAnnotDict != NULL &&\r
-                    !pOCContext->CheckOCGVisible(pAnnotDict->GetDict(FX_BSTRC("OC")))) {\r
-                continue;\r
-            }\r
-        }\r
-        CPDF_Rect annot_rect_f;\r
-        pAnnot->GetRect(annot_rect_f);\r
-        CFX_Matrix matrix;\r
-        matrix = *pMatrix;\r
-        if (clip_rect) {\r
-            annot_rect_f.Transform(&matrix);\r
-            FX_RECT annot_rect = annot_rect_f.GetOutterRect();\r
-            annot_rect.Intersect(*clip_rect);\r
-            if (annot_rect.IsEmpty()) {\r
-                continue;\r
-            }\r
-        }\r
-        if (pContext) {\r
-            pAnnot->DrawInContext(pPage, pContext, &matrix, CPDF_Annot::Normal);\r
-        } else if (!pAnnot->DrawAppearance(pPage, pDevice, &matrix, CPDF_Annot::Normal, pOptions)) {\r
-            pAnnot->DrawBorder(pDevice, &matrix, pOptions);\r
-        }\r
-    }\r
-}\r
-void CPDF_AnnotList::DisplayAnnots(const CPDF_Page* pPage, CFX_RenderDevice* pDevice,\r
-                                   CFX_AffineMatrix* pUser2Device,\r
-                                   FX_BOOL bShowWidget, CPDF_RenderOptions* pOptions)\r
-{\r
-    FX_RECT clip_rect;\r
-    if (pDevice) {\r
-        clip_rect = pDevice->GetClipBox();\r
-    }\r
-    FX_BOOL bPrinting = pDevice->GetDeviceClass() == FXDC_PRINTER || (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW));\r
-    DisplayAnnots(pPage, pDevice, NULL, bPrinting, pUser2Device, bShowWidget ? 3 : 1, pOptions, &clip_rect);\r
-}\r
-void CPDF_AnnotList::DisplayAnnots(const CPDF_Page* pPage, CFX_RenderDevice* pDevice, CPDF_RenderContext* pContext,\r
-                                   FX_BOOL bPrinting, CFX_AffineMatrix* pUser2Device, FX_DWORD dwAnnotFlags,\r
-                                   CPDF_RenderOptions* pOptions, FX_RECT* pClipRect)\r
-{\r
-    if (dwAnnotFlags & 0x01) {\r
-        DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, FALSE, pOptions, pClipRect);\r
-    }\r
-    if (dwAnnotFlags & 0x02) {\r
-        DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, TRUE, pOptions, pClipRect);\r
-    }\r
-}\r
-int CPDF_AnnotList::GetIndex(CPDF_Annot* pAnnot)\r
-{\r
-    for (int i = 0; i < m_AnnotList.GetSize(); i ++)\r
-        if (m_AnnotList[i] == (FX_LPVOID)pAnnot) {\r
-            return i;\r
-        }\r
-    return -1;\r
-}\r
-CPDF_Annot::CPDF_Annot(CPDF_Dictionary* pDict)\r
-{\r
-    m_pList = NULL;\r
-    m_pAnnotDict = pDict;\r
-}\r
-CPDF_Annot::~CPDF_Annot()\r
-{\r
-    ClearCachedAP();\r
-}\r
-CPDF_Reference* CPDF_Annot::NewAnnotRef()\r
-{\r
-    if (m_pAnnotDict->GetObjNum() == 0) {\r
-        m_pList->m_pDocument->AddIndirectObject(m_pAnnotDict);\r
-    }\r
-    return CPDF_Reference::Create(m_pList->m_pDocument, m_pAnnotDict->GetObjNum());\r
-}\r
-void CPDF_Annot::ClearCachedAP()\r
-{\r
-    FX_POSITION pos = m_APMap.GetStartPosition();\r
-    while (pos) {\r
-        void* pForm;\r
-        void* pObjects;\r
-        m_APMap.GetNextAssoc(pos, pForm, pObjects);\r
-        delete (CPDF_PageObjects*)pObjects;\r
-    }\r
-    m_APMap.RemoveAll();\r
-}\r
-CFX_ByteString CPDF_Annot::GetSubType() const\r
-{\r
-    return m_pAnnotDict->GetConstString(FX_BSTRC("Subtype"));\r
-}\r
-void CPDF_Annot::GetRect(CPDF_Rect& rect) const\r
-{\r
-    if (m_pAnnotDict == NULL) {\r
-        return;\r
-    }\r
-    rect = m_pAnnotDict->GetRect("Rect");\r
-    rect.Normalize();\r
-}\r
-CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode mode)\r
-{\r
-    CPDF_Dictionary* pAP = pAnnotDict->GetDict("AP");\r
-    if (pAP == NULL) {\r
-        return NULL;\r
-    }\r
-    const FX_CHAR* ap_entry = "N";\r
-    if (mode == CPDF_Annot::Down) {\r
-        ap_entry = "D";\r
-    } else if (mode == CPDF_Annot::Rollover) {\r
-        ap_entry = "R";\r
-    }\r
-    if (!pAP->KeyExist(ap_entry)) {\r
-        ap_entry = "N";\r
-    }\r
-    CPDF_Object* psub = pAP->GetElementValue(ap_entry);\r
-    if (psub == NULL) {\r
-        return NULL;\r
-    }\r
-    CPDF_Stream* pStream = NULL;\r
-    if (psub->GetType() == PDFOBJ_STREAM) {\r
-        pStream = (CPDF_Stream*)psub;\r
-    } else if (psub->GetType() == PDFOBJ_DICTIONARY) {\r
-        CFX_ByteString as = pAnnotDict->GetString("AS");\r
-        if (as.IsEmpty()) {\r
-            CFX_ByteString value = pAnnotDict->GetString(FX_BSTRC("V"));\r
-            if (value.IsEmpty()) {\r
-                value = pAnnotDict->GetDict(FX_BSTRC("Parent"))->GetString(FX_BSTRC("V"));\r
-            }\r
-            if (value.IsEmpty() || !((CPDF_Dictionary*)psub)->KeyExist(value)) {\r
-                as = FX_BSTRC("Off");\r
-            } else {\r
-                as = value;\r
-            }\r
-        }\r
-        pStream = ((CPDF_Dictionary*)psub)->GetStream(as);\r
-    }\r
-    return pStream;\r
-}\r
-CPDF_Form* CPDF_Annot::GetAPForm(const CPDF_Page* pPage, AppearanceMode mode)\r
-{\r
-    CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(m_pAnnotDict, mode);\r
-    if (pStream == NULL) {\r
-        return NULL;\r
-    }\r
-    CPDF_Form* pForm;\r
-    if (m_APMap.Lookup(pStream, (void*&)pForm)) {\r
-        return pForm;\r
-    }\r
-    pForm = FX_NEW CPDF_Form(m_pList->m_pDocument, pPage->m_pResources, pStream);\r
-    if (pForm == NULL) {\r
-        return NULL;\r
-    }\r
-    pForm->ParseContent(NULL, NULL, NULL, NULL);\r
-    m_APMap.SetAt(pStream, pForm);\r
-    return pForm;\r
-}\r
-static CPDF_Form* FPDFDOC_Annot_GetMatrix(const CPDF_Page* pPage, CPDF_Annot* pAnnot, CPDF_Annot::AppearanceMode mode, const CFX_AffineMatrix* pUser2Device, CFX_Matrix &matrix)\r
-{\r
-    CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode);\r
-    if (!pForm) {\r
-        return NULL;\r
-    }\r
-    CFX_FloatRect form_bbox = pForm->m_pFormDict->GetRect(FX_BSTRC("BBox"));\r
-    CFX_Matrix form_matrix = pForm->m_pFormDict->GetMatrix(FX_BSTRC("Matrix"));\r
-    form_matrix.TransformRect(form_bbox);\r
-    CPDF_Rect arect;\r
-    pAnnot->GetRect(arect);\r
-    matrix.MatchRect(arect, form_bbox);\r
-    matrix.Concat(*pUser2Device);\r
-    return pForm;\r
-}\r
-FX_BOOL CPDF_Annot::DrawAppearance(const CPDF_Page* pPage, CFX_RenderDevice* pDevice, const CFX_AffineMatrix* pUser2Device,\r
-                                   AppearanceMode mode, const CPDF_RenderOptions* pOptions)\r
-{\r
-    CFX_Matrix matrix;\r
-    CPDF_Form* pForm = FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix);\r
-    if (!pForm) {\r
-        return FALSE;\r
-    }\r
-    CPDF_RenderContext context;\r
-    context.Create((CPDF_Page*)pPage);\r
-    context.DrawObjectList(pDevice, pForm, &matrix, pOptions);\r
-    return TRUE;\r
-}\r
-FX_BOOL CPDF_Annot::DrawInContext(const CPDF_Page* pPage, const CPDF_RenderContext* pContext, const CFX_AffineMatrix* pUser2Device, AppearanceMode mode)\r
-{\r
-    CFX_Matrix matrix;\r
-    CPDF_Form* pForm = FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix);\r
-    if (!pForm) {\r
-        return FALSE;\r
-    }\r
-    ((CPDF_RenderContext*)pContext)->AppendObjectList(pForm, &matrix);\r
-    return TRUE;\r
-}\r
-CPDF_PageObject* CPDF_Annot::GetBorder(FX_BOOL bPrint, const CPDF_RenderOptions* pOptions)\r
-{\r
-    if (GetSubType() == "Popup") {\r
-        return NULL;\r
-    }\r
-    FX_DWORD annot_flags = GetFlags();\r
-    if (annot_flags & ANNOTFLAG_HIDDEN) {\r
-        return NULL;\r
-    }\r
-    FX_BOOL bPrinting = bPrint || (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW));\r
-    if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) {\r
-        return NULL;\r
-    }\r
-    if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) {\r
-        return NULL;\r
-    }\r
-    CPDF_Dictionary* pBS = m_pAnnotDict->GetDict("BS");\r
-    char style_char;\r
-    FX_FLOAT width;\r
-    CPDF_Array* pDashArray = NULL;\r
-    if (pBS == NULL) {\r
-        CPDF_Array* pBorderArray = m_pAnnotDict->GetArray("Border");\r
-        style_char = 'S';\r
-        if (pBorderArray) {\r
-            width = pBorderArray->GetNumber(2);\r
-            if (pBorderArray->GetCount() == 4) {\r
-                pDashArray = pBorderArray->GetArray(3);\r
-                if (pDashArray == NULL) {\r
-                    return NULL;\r
-                }\r
-                style_char = 'D';\r
-            }\r
-        } else {\r
-            width = 1;\r
-        }\r
-    } else {\r
-        CFX_ByteString style = pBS->GetString("S");\r
-        pDashArray = pBS->GetArray("D");\r
-        style_char = style[1];\r
-        width = pBS->GetNumber("W");\r
-    }\r
-    if (width <= 0) {\r
-        return NULL;\r
-    }\r
-    CPDF_Array* pColor = m_pAnnotDict->GetArray("C");\r
-    FX_DWORD argb = 0xff000000;\r
-    if (pColor != NULL) {\r
-        int R = (FX_INT32)(pColor->GetNumber(0) * 255);\r
-        int G = (FX_INT32)(pColor->GetNumber(1) * 255);\r
-        int B = (FX_INT32)(pColor->GetNumber(2) * 255);\r
-        argb = ArgbEncode(0xff, R, G, B);\r
-    }\r
-    CPDF_PathObject *pPathObject = FX_NEW CPDF_PathObject();\r
-    if (!pPathObject) {\r
-        return NULL;\r
-    }\r
-    CPDF_GraphStateData *pGraphState = pPathObject->m_GraphState.GetModify();\r
-    if (!pGraphState) {\r
-        pPathObject->Release();\r
-        return NULL;\r
-    }\r
-    pGraphState->m_LineWidth = width;\r
-    CPDF_ColorStateData *pColorData = pPathObject->m_ColorState.GetModify();\r
-    if (!pColorData) {\r
-        pPathObject->Release();\r
-        return NULL;\r
-    }\r
-    pColorData->m_StrokeRGB = argb;\r
-    pPathObject->m_bStroke = TRUE;\r
-    pPathObject->m_FillType = 0;\r
-    if (style_char == 'D') {\r
-        if (pDashArray) {\r
-            FX_DWORD dash_count = pDashArray->GetCount();\r
-            if (dash_count % 2) {\r
-                dash_count ++;\r
-            }\r
-            pGraphState->m_DashArray = FX_Alloc(FX_FLOAT, dash_count);\r
-            if (pGraphState->m_DashArray == NULL) {\r
-                pPathObject->Release();\r
-                return NULL;\r
-            }\r
-            pGraphState->m_DashCount = dash_count;\r
-            FX_DWORD i;\r
-            for (i = 0; i < pDashArray->GetCount(); i ++) {\r
-                pGraphState->m_DashArray[i] = pDashArray->GetNumber(i);\r
-            }\r
-            if (i < dash_count) {\r
-                pGraphState->m_DashArray[i] = pGraphState->m_DashArray[i - 1];\r
-            }\r
-        } else {\r
-            pGraphState->m_DashArray = FX_Alloc(FX_FLOAT, 2);\r
-            if (pGraphState->m_DashArray == NULL) {\r
-                pPathObject->Release();\r
-                return NULL;\r
-            }\r
-            pGraphState->m_DashCount = 2;\r
-            pGraphState->m_DashArray[0] = pGraphState->m_DashArray[1] = 3 * 1.0f;\r
-        }\r
-    }\r
-    CFX_FloatRect rect;\r
-    GetRect(rect);\r
-    width /= 2;\r
-    CPDF_PathData *pPathData = pPathObject->m_Path.GetModify();\r
-    if (pPathData) {\r
-        pPathData->AppendRect(rect.left + width, rect.bottom + width, rect.right - width, rect.top - width);\r
-    }\r
-    pPathObject->CalcBoundingBox();\r
-    return pPathObject;\r
-}\r
-void CPDF_Annot::DrawBorder(CFX_RenderDevice* pDevice, const CFX_AffineMatrix* pUser2Device, const CPDF_RenderOptions* pOptions)\r
-{\r
-    if (GetSubType() == "Popup") {\r
-        return;\r
-    }\r
-    FX_DWORD annot_flags = GetFlags();\r
-    if (annot_flags & ANNOTFLAG_HIDDEN) {\r
-        return;\r
-    }\r
-    FX_BOOL bPrinting = pDevice->GetDeviceClass() == FXDC_PRINTER || (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW));\r
-    if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) {\r
-        return;\r
-    }\r
-    if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) {\r
-        return;\r
-    }\r
-    CPDF_Dictionary* pBS = m_pAnnotDict->GetDict("BS");\r
-    char style_char;\r
-    FX_FLOAT width;\r
-    CPDF_Array* pDashArray = NULL;\r
-    if (pBS == NULL) {\r
-        CPDF_Array* pBorderArray = m_pAnnotDict->GetArray("Border");\r
-        style_char = 'S';\r
-        if (pBorderArray) {\r
-            width = pBorderArray->GetNumber(2);\r
-            if (pBorderArray->GetCount() == 4) {\r
-                pDashArray = pBorderArray->GetArray(3);\r
-                if (pDashArray == NULL) {\r
-                    return;\r
-                }\r
-                int nLen = pDashArray->GetCount();\r
-                int i = 0;\r
-                for (; i < nLen; ++i) {\r
-                    CPDF_Object*pObj = pDashArray->GetElementValue(i);\r
-                    if (pObj && pObj->GetInteger()) {\r
-                        break;\r
-                    }\r
-                }\r
-                if (i == nLen) {\r
-                    return;\r
-                }\r
-                style_char = 'D';\r
-            }\r
-        } else {\r
-            width = 1;\r
-        }\r
-    } else {\r
-        CFX_ByteString style = pBS->GetString("S");\r
-        pDashArray = pBS->GetArray("D");\r
-        style_char = style[1];\r
-        width = pBS->GetNumber("W");\r
-    }\r
-    if (width <= 0) {\r
-        return;\r
-    }\r
-    CPDF_Array* pColor = m_pAnnotDict->GetArray("C");\r
-    FX_DWORD argb = 0xff000000;\r
-    if (pColor != NULL) {\r
-        int R = (FX_INT32)(pColor->GetNumber(0) * 255);\r
-        int G = (FX_INT32)(pColor->GetNumber(1) * 255);\r
-        int B = (FX_INT32)(pColor->GetNumber(2) * 255);\r
-        argb = ArgbEncode(0xff, R, G, B);\r
-    }\r
-    CPDF_GraphStateData graph_state;\r
-    graph_state.m_LineWidth = width;\r
-    if (style_char == 'D') {\r
-        if (pDashArray) {\r
-            FX_DWORD dash_count = pDashArray->GetCount();\r
-            if (dash_count % 2) {\r
-                dash_count ++;\r
-            }\r
-            graph_state.m_DashArray = FX_Alloc(FX_FLOAT, dash_count);\r
-            if (graph_state.m_DashArray == NULL) {\r
-                return ;\r
-            }\r
-            graph_state.m_DashCount = dash_count;\r
-            FX_DWORD i;\r
-            for (i = 0; i < pDashArray->GetCount(); i ++) {\r
-                graph_state.m_DashArray[i] = pDashArray->GetNumber(i);\r
-            }\r
-            if (i < dash_count) {\r
-                graph_state.m_DashArray[i] = graph_state.m_DashArray[i - 1];\r
-            }\r
-        } else {\r
-            graph_state.m_DashArray = FX_Alloc(FX_FLOAT, 2);\r
-            if (graph_state.m_DashArray == NULL) {\r
-                return ;\r
-            }\r
-            graph_state.m_DashCount = 2;\r
-            graph_state.m_DashArray[0] = graph_state.m_DashArray[1] = 3 * 1.0f;\r
-        }\r
-    }\r
-    CFX_FloatRect rect;\r
-    GetRect(rect);\r
-    CPDF_PathData path;\r
-    width /= 2;\r
-    path.AppendRect(rect.left + width, rect.bottom + width, rect.right - width, rect.top - width);\r
-    int fill_type = 0;\r
-    if (pOptions && (pOptions->m_Flags & RENDER_NOPATHSMOOTH)) {\r
-        fill_type |= FXFILL_NOPATHSMOOTH;\r
-    }\r
-    pDevice->DrawPath(&path, pUser2Device, &graph_state, argb, argb, fill_type);\r
-}\r
-int CPDF_Annot::CountIRTNotes()\r
-{\r
-    int count = 0;\r
-    for (int i = 0; i < m_pList->Count(); i ++) {\r
-        CPDF_Annot* pAnnot = m_pList->GetAt(i);\r
-        if (pAnnot == NULL) {\r
-            continue;\r
-        }\r
-        CPDF_Dictionary* pIRT = pAnnot->m_pAnnotDict->GetDict("IRT");\r
-        if (pIRT != m_pAnnotDict) {\r
-            continue;\r
-        }\r
-        count ++;\r
-    }\r
-    return count;\r
-}\r
-CPDF_Annot* CPDF_Annot::GetIRTNote(int index)\r
-{\r
-    int count = 0;\r
-    for (int i = 0; i < m_pList->Count(); i ++) {\r
-        CPDF_Annot* pAnnot = m_pList->GetAt(i);\r
-        if (pAnnot == NULL) {\r
-            continue;\r
-        }\r
-        CPDF_Dictionary* pIRT = pAnnot->m_pAnnotDict->GetDict("IRT");\r
-        if (pIRT != m_pAnnotDict) {\r
-            continue;\r
-        }\r
-        if (count == index) {\r
-            return pAnnot;\r
-        }\r
-        count ++;\r
-    }\r
-    return NULL;\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/fpdfdoc/fpdf_doc.h"
+#include "../../include/fpdfapi/fpdf_pageobj.h"
+CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
+{
+    ASSERT(pPage != NULL);
+    m_pPageDict = pPage->m_pFormDict;
+    if (m_pPageDict == NULL) {
+        return;
+    }
+    m_pDocument = pPage->m_pDocument;
+    CPDF_Array* pAnnots = m_pPageDict->GetArray("Annots");
+    if (pAnnots == NULL) {
+        return;
+    }
+    CPDF_Dictionary* pRoot = m_pDocument->GetRoot();
+    CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
+    FX_BOOL bRegenerateAP = pAcroForm && pAcroForm->GetBoolean("NeedAppearances");
+    for (FX_DWORD i = 0; i < pAnnots->GetCount(); i ++) {
+        CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
+        if (pDict == NULL || pDict->GetType() != PDFOBJ_DICTIONARY) {
+            continue;
+        }
+        FX_DWORD dwObjNum = pDict->GetObjNum();
+        if (dwObjNum == 0) {
+            dwObjNum = m_pDocument->AddIndirectObject(pDict);
+            CPDF_Reference* pAction = CPDF_Reference::Create(m_pDocument, dwObjNum);
+            if (pAction == NULL) {
+                break;
+            }
+            pAnnots->InsertAt(i, pAction);
+            pAnnots->RemoveAt(i + 1);
+            pDict = pAnnots->GetDict(i);
+        }
+        CPDF_Annot* pAnnot = FX_NEW CPDF_Annot(pDict);
+        if (pAnnot == NULL) {
+            break;
+        }
+        pAnnot->m_pList = this;
+        m_AnnotList.Add(pAnnot);
+        if (bRegenerateAP && pDict->GetConstString(FX_BSTRC("Subtype")) == FX_BSTRC("Widget"))
+            if (CPDF_InterForm::UpdatingAPEnabled()) {
+                FPDF_GenerateAP(m_pDocument, pDict);
+            }
+    }
+}
+CPDF_AnnotList::~CPDF_AnnotList()
+{
+    int i = 0;
+    for (i = 0; i < m_AnnotList.GetSize(); i ++) {
+        delete (CPDF_Annot*)m_AnnotList[i];
+    }
+    for (i = 0; i < m_Borders.GetSize(); ++i) {
+        delete (CPDF_PageObjects*)m_Borders[i];
+    }
+}
+void CPDF_AnnotList::DisplayPass(const CPDF_Page* pPage, CFX_RenderDevice* pDevice,
+                                 CPDF_RenderContext* pContext, FX_BOOL bPrinting, CFX_AffineMatrix* pMatrix,
+                                 FX_BOOL bWidgetPass, CPDF_RenderOptions* pOptions, FX_RECT* clip_rect)
+{
+    for (int i = 0; i < m_AnnotList.GetSize(); i ++) {
+        CPDF_Annot* pAnnot = (CPDF_Annot*)m_AnnotList[i];
+        FX_BOOL bWidget = pAnnot->GetSubType() == "Widget";
+        if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget)) {
+            continue;
+        }
+        FX_DWORD annot_flags = pAnnot->GetFlags();
+        if (annot_flags & ANNOTFLAG_HIDDEN) {
+            continue;
+        }
+        if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) {
+            continue;
+        }
+        if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) {
+            continue;
+        }
+        if (pOptions != NULL) {
+            IPDF_OCContext* pOCContext = pOptions->m_pOCContext;
+            CPDF_Dictionary* pAnnotDict = pAnnot->m_pAnnotDict;
+            if (pOCContext != NULL && pAnnotDict != NULL &&
+                    !pOCContext->CheckOCGVisible(pAnnotDict->GetDict(FX_BSTRC("OC")))) {
+                continue;
+            }
+        }
+        CPDF_Rect annot_rect_f;
+        pAnnot->GetRect(annot_rect_f);
+        CFX_Matrix matrix;
+        matrix = *pMatrix;
+        if (clip_rect) {
+            annot_rect_f.Transform(&matrix);
+            FX_RECT annot_rect = annot_rect_f.GetOutterRect();
+            annot_rect.Intersect(*clip_rect);
+            if (annot_rect.IsEmpty()) {
+                continue;
+            }
+        }
+        if (pContext) {
+            pAnnot->DrawInContext(pPage, pContext, &matrix, CPDF_Annot::Normal);
+        } else if (!pAnnot->DrawAppearance(pPage, pDevice, &matrix, CPDF_Annot::Normal, pOptions)) {
+            pAnnot->DrawBorder(pDevice, &matrix, pOptions);
+        }
+    }
+}
+void CPDF_AnnotList::DisplayAnnots(const CPDF_Page* pPage, CFX_RenderDevice* pDevice,
+                                   CFX_AffineMatrix* pUser2Device,
+                                   FX_BOOL bShowWidget, CPDF_RenderOptions* pOptions)
+{
+    FX_RECT clip_rect;
+    if (pDevice) {
+        clip_rect = pDevice->GetClipBox();
+    }
+    FX_BOOL bPrinting = pDevice->GetDeviceClass() == FXDC_PRINTER || (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW));
+    DisplayAnnots(pPage, pDevice, NULL, bPrinting, pUser2Device, bShowWidget ? 3 : 1, pOptions, &clip_rect);
+}
+void CPDF_AnnotList::DisplayAnnots(const CPDF_Page* pPage, CFX_RenderDevice* pDevice, CPDF_RenderContext* pContext,
+                                   FX_BOOL bPrinting, CFX_AffineMatrix* pUser2Device, FX_DWORD dwAnnotFlags,
+                                   CPDF_RenderOptions* pOptions, FX_RECT* pClipRect)
+{
+    if (dwAnnotFlags & 0x01) {
+        DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, FALSE, pOptions, pClipRect);
+    }
+    if (dwAnnotFlags & 0x02) {
+        DisplayPass(pPage, pDevice, pContext, bPrinting, pUser2Device, TRUE, pOptions, pClipRect);
+    }
+}
+int CPDF_AnnotList::GetIndex(CPDF_Annot* pAnnot)
+{
+    for (int i = 0; i < m_AnnotList.GetSize(); i ++)
+        if (m_AnnotList[i] == (FX_LPVOID)pAnnot) {
+            return i;
+        }
+    return -1;
+}
+CPDF_Annot::CPDF_Annot(CPDF_Dictionary* pDict)
+{
+    m_pList = NULL;
+    m_pAnnotDict = pDict;
+}
+CPDF_Annot::~CPDF_Annot()
+{
+    ClearCachedAP();
+}
+CPDF_Reference* CPDF_Annot::NewAnnotRef()
+{
+    if (m_pAnnotDict->GetObjNum() == 0) {
+        m_pList->m_pDocument->AddIndirectObject(m_pAnnotDict);
+    }
+    return CPDF_Reference::Create(m_pList->m_pDocument, m_pAnnotDict->GetObjNum());
+}
+void CPDF_Annot::ClearCachedAP()
+{
+    FX_POSITION pos = m_APMap.GetStartPosition();
+    while (pos) {
+        void* pForm;
+        void* pObjects;
+        m_APMap.GetNextAssoc(pos, pForm, pObjects);
+        delete (CPDF_PageObjects*)pObjects;
+    }
+    m_APMap.RemoveAll();
+}
+CFX_ByteString CPDF_Annot::GetSubType() const
+{
+    return m_pAnnotDict->GetConstString(FX_BSTRC("Subtype"));
+}
+void CPDF_Annot::GetRect(CPDF_Rect& rect) const
+{
+    if (m_pAnnotDict == NULL) {
+        return;
+    }
+    rect = m_pAnnotDict->GetRect("Rect");
+    rect.Normalize();
+}
+CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict, CPDF_Annot::AppearanceMode mode)
+{
+    CPDF_Dictionary* pAP = pAnnotDict->GetDict("AP");
+    if (pAP == NULL) {
+        return NULL;
+    }
+    const FX_CHAR* ap_entry = "N";
+    if (mode == CPDF_Annot::Down) {
+        ap_entry = "D";
+    } else if (mode == CPDF_Annot::Rollover) {
+        ap_entry = "R";
+    }
+    if (!pAP->KeyExist(ap_entry)) {
+        ap_entry = "N";
+    }
+    CPDF_Object* psub = pAP->GetElementValue(ap_entry);
+    if (psub == NULL) {
+        return NULL;
+    }
+    CPDF_Stream* pStream = NULL;
+    if (psub->GetType() == PDFOBJ_STREAM) {
+        pStream = (CPDF_Stream*)psub;
+    } else if (psub->GetType() == PDFOBJ_DICTIONARY) {
+        CFX_ByteString as = pAnnotDict->GetString("AS");
+        if (as.IsEmpty()) {
+            CFX_ByteString value = pAnnotDict->GetString(FX_BSTRC("V"));
+            if (value.IsEmpty()) {
+                value = pAnnotDict->GetDict(FX_BSTRC("Parent"))->GetString(FX_BSTRC("V"));
+            }
+            if (value.IsEmpty() || !((CPDF_Dictionary*)psub)->KeyExist(value)) {
+                as = FX_BSTRC("Off");
+            } else {
+                as = value;
+            }
+        }
+        pStream = ((CPDF_Dictionary*)psub)->GetStream(as);
+    }
+    return pStream;
+}
+CPDF_Form* CPDF_Annot::GetAPForm(const CPDF_Page* pPage, AppearanceMode mode)
+{
+    CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(m_pAnnotDict, mode);
+    if (pStream == NULL) {
+        return NULL;
+    }
+    CPDF_Form* pForm;
+    if (m_APMap.Lookup(pStream, (void*&)pForm)) {
+        return pForm;
+    }
+    pForm = FX_NEW CPDF_Form(m_pList->m_pDocument, pPage->m_pResources, pStream);
+    if (pForm == NULL) {
+        return NULL;
+    }
+    pForm->ParseContent(NULL, NULL, NULL, NULL);
+    m_APMap.SetAt(pStream, pForm);
+    return pForm;
+}
+static CPDF_Form* FPDFDOC_Annot_GetMatrix(const CPDF_Page* pPage, CPDF_Annot* pAnnot, CPDF_Annot::AppearanceMode mode, const CFX_AffineMatrix* pUser2Device, CFX_Matrix &matrix)
+{
+    CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode);
+    if (!pForm) {
+        return NULL;
+    }
+    CFX_FloatRect form_bbox = pForm->m_pFormDict->GetRect(FX_BSTRC("BBox"));
+    CFX_Matrix form_matrix = pForm->m_pFormDict->GetMatrix(FX_BSTRC("Matrix"));
+    form_matrix.TransformRect(form_bbox);
+    CPDF_Rect arect;
+    pAnnot->GetRect(arect);
+    matrix.MatchRect(arect, form_bbox);
+    matrix.Concat(*pUser2Device);
+    return pForm;
+}
+FX_BOOL CPDF_Annot::DrawAppearance(const CPDF_Page* pPage, CFX_RenderDevice* pDevice, const CFX_AffineMatrix* pUser2Device,
+                                   AppearanceMode mode, const CPDF_RenderOptions* pOptions)
+{
+    CFX_Matrix matrix;
+    CPDF_Form* pForm = FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix);
+    if (!pForm) {
+        return FALSE;
+    }
+    CPDF_RenderContext context;
+    context.Create((CPDF_Page*)pPage);
+    context.DrawObjectList(pDevice, pForm, &matrix, pOptions);
+    return TRUE;
+}
+FX_BOOL CPDF_Annot::DrawInContext(const CPDF_Page* pPage, const CPDF_RenderContext* pContext, const CFX_AffineMatrix* pUser2Device, AppearanceMode mode)
+{
+    CFX_Matrix matrix;
+    CPDF_Form* pForm = FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix);
+    if (!pForm) {
+        return FALSE;
+    }
+    ((CPDF_RenderContext*)pContext)->AppendObjectList(pForm, &matrix);
+    return TRUE;
+}
+CPDF_PageObject* CPDF_Annot::GetBorder(FX_BOOL bPrint, const CPDF_RenderOptions* pOptions)
+{
+    if (GetSubType() == "Popup") {
+        return NULL;
+    }
+    FX_DWORD annot_flags = GetFlags();
+    if (annot_flags & ANNOTFLAG_HIDDEN) {
+        return NULL;
+    }
+    FX_BOOL bPrinting = bPrint || (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW));
+    if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) {
+        return NULL;
+    }
+    if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) {
+        return NULL;
+    }
+    CPDF_Dictionary* pBS = m_pAnnotDict->GetDict("BS");
+    char style_char;
+    FX_FLOAT width;
+    CPDF_Array* pDashArray = NULL;
+    if (pBS == NULL) {
+        CPDF_Array* pBorderArray = m_pAnnotDict->GetArray("Border");
+        style_char = 'S';
+        if (pBorderArray) {
+            width = pBorderArray->GetNumber(2);
+            if (pBorderArray->GetCount() == 4) {
+                pDashArray = pBorderArray->GetArray(3);
+                if (pDashArray == NULL) {
+                    return NULL;
+                }
+                style_char = 'D';
+            }
+        } else {
+            width = 1;
+        }
+    } else {
+        CFX_ByteString style = pBS->GetString("S");
+        pDashArray = pBS->GetArray("D");
+        style_char = style[1];
+        width = pBS->GetNumber("W");
+    }
+    if (width <= 0) {
+        return NULL;
+    }
+    CPDF_Array* pColor = m_pAnnotDict->GetArray("C");
+    FX_DWORD argb = 0xff000000;
+    if (pColor != NULL) {
+        int R = (FX_INT32)(pColor->GetNumber(0) * 255);
+        int G = (FX_INT32)(pColor->GetNumber(1) * 255);
+        int B = (FX_INT32)(pColor->GetNumber(2) * 255);
+        argb = ArgbEncode(0xff, R, G, B);
+    }
+    CPDF_PathObject *pPathObject = FX_NEW CPDF_PathObject();
+    if (!pPathObject) {
+        return NULL;
+    }
+    CPDF_GraphStateData *pGraphState = pPathObject->m_GraphState.GetModify();
+    if (!pGraphState) {
+        pPathObject->Release();
+        return NULL;
+    }
+    pGraphState->m_LineWidth = width;
+    CPDF_ColorStateData *pColorData = pPathObject->m_ColorState.GetModify();
+    if (!pColorData) {
+        pPathObject->Release();
+        return NULL;
+    }
+    pColorData->m_StrokeRGB = argb;
+    pPathObject->m_bStroke = TRUE;
+    pPathObject->m_FillType = 0;
+    if (style_char == 'D') {
+        if (pDashArray) {
+            FX_DWORD dash_count = pDashArray->GetCount();
+            if (dash_count % 2) {
+                dash_count ++;
+            }
+            pGraphState->m_DashArray = FX_Alloc(FX_FLOAT, dash_count);
+            if (pGraphState->m_DashArray == NULL) {
+                pPathObject->Release();
+                return NULL;
+            }
+            pGraphState->m_DashCount = dash_count;
+            FX_DWORD i;
+            for (i = 0; i < pDashArray->GetCount(); i ++) {
+                pGraphState->m_DashArray[i] = pDashArray->GetNumber(i);
+            }
+            if (i < dash_count) {
+                pGraphState->m_DashArray[i] = pGraphState->m_DashArray[i - 1];
+            }
+        } else {
+            pGraphState->m_DashArray = FX_Alloc(FX_FLOAT, 2);
+            if (pGraphState->m_DashArray == NULL) {
+                pPathObject->Release();
+                return NULL;
+            }
+            pGraphState->m_DashCount = 2;
+            pGraphState->m_DashArray[0] = pGraphState->m_DashArray[1] = 3 * 1.0f;
+        }
+    }
+    CFX_FloatRect rect;
+    GetRect(rect);
+    width /= 2;
+    CPDF_PathData *pPathData = pPathObject->m_Path.GetModify();
+    if (pPathData) {
+        pPathData->AppendRect(rect.left + width, rect.bottom + width, rect.right - width, rect.top - width);
+    }
+    pPathObject->CalcBoundingBox();
+    return pPathObject;
+}
+void CPDF_Annot::DrawBorder(CFX_RenderDevice* pDevice, const CFX_AffineMatrix* pUser2Device, const CPDF_RenderOptions* pOptions)
+{
+    if (GetSubType() == "Popup") {
+        return;
+    }
+    FX_DWORD annot_flags = GetFlags();
+    if (annot_flags & ANNOTFLAG_HIDDEN) {
+        return;
+    }
+    FX_BOOL bPrinting = pDevice->GetDeviceClass() == FXDC_PRINTER || (pOptions && (pOptions->m_Flags & RENDER_PRINTPREVIEW));
+    if (bPrinting && (annot_flags & ANNOTFLAG_PRINT) == 0) {
+        return;
+    }
+    if (!bPrinting && (annot_flags & ANNOTFLAG_NOVIEW)) {
+        return;
+    }
+    CPDF_Dictionary* pBS = m_pAnnotDict->GetDict("BS");
+    char style_char;
+    FX_FLOAT width;
+    CPDF_Array* pDashArray = NULL;
+    if (pBS == NULL) {
+        CPDF_Array* pBorderArray = m_pAnnotDict->GetArray("Border");
+        style_char = 'S';
+        if (pBorderArray) {
+            width = pBorderArray->GetNumber(2);
+            if (pBorderArray->GetCount() == 4) {
+                pDashArray = pBorderArray->GetArray(3);
+                if (pDashArray == NULL) {
+                    return;
+                }
+                int nLen = pDashArray->GetCount();
+                int i = 0;
+                for (; i < nLen; ++i) {
+                    CPDF_Object*pObj = pDashArray->GetElementValue(i);
+                    if (pObj && pObj->GetInteger()) {
+                        break;
+                    }
+                }
+                if (i == nLen) {
+                    return;
+                }
+                style_char = 'D';
+            }
+        } else {
+            width = 1;
+        }
+    } else {
+        CFX_ByteString style = pBS->GetString("S");
+        pDashArray = pBS->GetArray("D");
+        style_char = style[1];
+        width = pBS->GetNumber("W");
+    }
+    if (width <= 0) {
+        return;
+    }
+    CPDF_Array* pColor = m_pAnnotDict->GetArray("C");
+    FX_DWORD argb = 0xff000000;
+    if (pColor != NULL) {
+        int R = (FX_INT32)(pColor->GetNumber(0) * 255);
+        int G = (FX_INT32)(pColor->GetNumber(1) * 255);
+        int B = (FX_INT32)(pColor->GetNumber(2) * 255);
+        argb = ArgbEncode(0xff, R, G, B);
+    }
+    CPDF_GraphStateData graph_state;
+    graph_state.m_LineWidth = width;
+    if (style_char == 'D') {
+        if (pDashArray) {
+            FX_DWORD dash_count = pDashArray->GetCount();
+            if (dash_count % 2) {
+                dash_count ++;
+            }
+            graph_state.m_DashArray = FX_Alloc(FX_FLOAT, dash_count);
+            if (graph_state.m_DashArray == NULL) {
+                return ;
+            }
+            graph_state.m_DashCount = dash_count;
+            FX_DWORD i;
+            for (i = 0; i < pDashArray->GetCount(); i ++) {
+                graph_state.m_DashArray[i] = pDashArray->GetNumber(i);
+            }
+            if (i < dash_count) {
+                graph_state.m_DashArray[i] = graph_state.m_DashArray[i - 1];
+            }
+        } else {
+            graph_state.m_DashArray = FX_Alloc(FX_FLOAT, 2);
+            if (graph_state.m_DashArray == NULL) {
+                return ;
+            }
+            graph_state.m_DashCount = 2;
+            graph_state.m_DashArray[0] = graph_state.m_DashArray[1] = 3 * 1.0f;
+        }
+    }
+    CFX_FloatRect rect;
+    GetRect(rect);
+    CPDF_PathData path;
+    width /= 2;
+    path.AppendRect(rect.left + width, rect.bottom + width, rect.right - width, rect.top - width);
+    int fill_type = 0;
+    if (pOptions && (pOptions->m_Flags & RENDER_NOPATHSMOOTH)) {
+        fill_type |= FXFILL_NOPATHSMOOTH;
+    }
+    pDevice->DrawPath(&path, pUser2Device, &graph_state, argb, argb, fill_type);
+}
+int CPDF_Annot::CountIRTNotes()
+{
+    int count = 0;
+    for (int i = 0; i < m_pList->Count(); i ++) {
+        CPDF_Annot* pAnnot = m_pList->GetAt(i);
+        if (pAnnot == NULL) {
+            continue;
+        }
+        CPDF_Dictionary* pIRT = pAnnot->m_pAnnotDict->GetDict("IRT");
+        if (pIRT != m_pAnnotDict) {
+            continue;
+        }
+        count ++;
+    }
+    return count;
+}
+CPDF_Annot* CPDF_Annot::GetIRTNote(int index)
+{
+    int count = 0;
+    for (int i = 0; i < m_pList->Count(); i ++) {
+        CPDF_Annot* pAnnot = m_pList->GetAt(i);
+        if (pAnnot == NULL) {
+            continue;
+        }
+        CPDF_Dictionary* pIRT = pAnnot->m_pAnnotDict->GetDict("IRT");
+        if (pIRT != m_pAnnotDict) {
+            continue;
+        }
+        if (count == index) {
+            return pAnnot;
+        }
+        count ++;
+    }
+    return NULL;
+}