⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scalesax.cpp

📁 WDK 下的XPSDrv 中filter 例子之scaling
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*++

Copyright (c) 2005 Microsoft Corporation

All rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

File Name:

   scalesax.xpp

Abstract:

   Page Scaling SAX handler implementation. The class derives from the default SAX handler
   and implements only the necessary SAX APIs to process the mark-up. The
   handler is responsible for copying page mark-up to a writer, removing
   the fixed page opening and closing tags.

Known Issues:

    Need to check if all resources are required resources are being added to the
    resource copying class.

--*/

#include "precomp.h"
#include "debug.h"
#include "globals.h"
#include "xdstring.h"
#include "bstrops.h"
#include "scalesax.h"

/*++

Routine Name:

    CScaleSaxHandler::CScaleSaxHandler

Routine Description:

    CScaleSaxHandler class constructor

Arguments:

    pWriter - Pointer to the output stream interface for the XPS page markup.
    pPageScaling - Pointer to the Page Scaling Interface.

Return Value:

    None
    Throws CXDException(HRESULT) on an error

--*/
CScaleSaxHandler::CScaleSaxHandler(
    __in ISequentialStream* pWriter,
    __in CPageScaling*      pPageScaling
    ) :
    m_pWriter(pWriter),
    m_pPageScaling(pPageScaling),
    m_bOpenTag(FALSE)
{
    ASSERTMSG(m_pWriter != NULL, "NULL writer passed to page scaling SAX handler.\n");
    ASSERTMSG(m_pPageScaling != NULL, "NULL page scaling class passed to page scaling SAX handler.\n");

    HRESULT hr = S_OK;
    if (FAILED(hr = CHECK_POINTER(m_pWriter, E_POINTER)) ||
        FAILED(hr = CHECK_POINTER(m_pPageScaling, E_POINTER)))
    {
        throw CXDException(hr);
    }
}

/*++

Routine Name:

    CScaleSaxHandler::~CScaleSaxHandler

Routine Description:

    CScaleSaxHandler class destructor

Arguments:

    None

Return Value:

    None

--*/
CScaleSaxHandler::~CScaleSaxHandler()
{
    m_bstrOpenElement.Empty();
}

/*++

Routine Name:

    CScaleSaxHandler::startElement

Routine Description:

    Receives notification of the beginning of an XML element in the XPS page.
    The page scaling filter parses each element, applies any changes
    and writes out the resultant XPS markup.

Arguments:

    pwchQName   - The XML 1.0 qualified name (QName), with prefix, or an empty string
                  (if QNames are not available).
    cchQName    - The length of the QName.
    pAttributes - The attributes attached to the element.

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT STDMETHODCALLTYPE
CScaleSaxHandler::startElement(
    CONST wchar_t*,
    INT,
    CONST wchar_t*,
    INT,
    __in_ecount(cchQName) CONST wchar_t*  pwchQName,
    __in                  INT             cchQName,
    __in                  ISAXAttributes* pAttributes
    )
{
    HRESULT hr = S_OK;
    CStringXDW cstrOut;

    //
    // If this is the fixed page we need the width and height retrieved
    //
    BOOL bIsFixedPage = FALSE;

    try
    {
        CComBSTR bstrElement(cchQName, pwchQName);

        bIsFixedPage = (bstrElement == L"FixedPage");

        //
        // Check if we need to close an opened tag
        //
        if (m_bOpenTag)
        {
            cstrOut.Append(L">\n");
        }

        //
        // Store the opened element name so we can handle nested elements
        //
        m_bstrOpenElement = bstrElement;
    }
    catch (CXDException& e)
    {
        hr = e;
    }

    if (SUCCEEDED(hr))
    {
        //
        // Write out element
        //
        try
        {
            cstrOut.Append(L"<");
            cstrOut.Append(m_bstrOpenElement);
        }
        catch (CXDException& e)
        {
            hr = e;
        }

        //
        // We opened a tag
        //
        m_bOpenTag = TRUE;

        REAL widthPage(0.0f);
        REAL heightPage(0.0f);
        RectF bleedBox(0.0f, 0.0f, 0.0f, 0.0f);
        RectF contentBox(0.0f, 0.0f, 0.0f, 0.0f);
        
        BOOL bBleedBoxSet = FALSE;
        BOOL bContentBoxSet = FALSE;

        //
        // Find the number of attributes and enumerate over all of them
        //
        INT cAttributes = 0;
        if (SUCCEEDED(hr) &&
            SUCCEEDED(hr = pAttributes->getLength(&cAttributes)))
        {
            for (INT cIndex = 0; cIndex < cAttributes && SUCCEEDED(hr); cIndex++)
            {
                PCWSTR pszAttUri   = NULL;
                INT    cchAttUri   = 0;
                PCWSTR pszAttName  = NULL;
                INT    cchAttName  = 0;
                PCWSTR pszAttQName = NULL;
                INT    cchAttQName = 0;
                PCWSTR pszAttValue = NULL;
                INT    cchAttValue = 0;

                //
                // Get the attribute data ready to write out
                //
                if (SUCCEEDED(hr = pAttributes->getName(cIndex,
                                                        &pszAttUri,
                                                        &cchAttUri,
                                                        &pszAttName,
                                                        &cchAttName,
                                                        &pszAttQName,
                                                        &cchAttQName)))
                {
                    if (SUCCEEDED(pAttributes->getValue(cIndex, &pszAttValue, &cchAttValue)))
                    {
                        try
                        {
                            CComBSTR bstrAttName(cchAttQName, pszAttQName);
                            CComBSTR bstrAttValue(cchAttValue, pszAttValue);

                            //
                            // If this is the fixed page we want to  retrieve the
                            // dimensions of the fixed page - take the opportunity
                            // to do so now while we parse the attributes
                            //
                            if (bIsFixedPage)
                            {
                                if (bstrAttName == L"Width")
                                {
                                    widthPage = static_cast<REAL>(_wtof(bstrAttValue));
                                }
                                else if (bstrAttName == L"Height")
                                {
                                    heightPage = static_cast<REAL>(_wtof(bstrAttValue));
                                }
                                else if (bstrAttName == L"ContentBox")
                                {
                                    CStringXDW str( bstrAttValue );
                                    CStringXDW resToken;
                                    INT curPos= 0;
                                    bContentBoxSet = TRUE;

                                    resToken = str.Tokenize(L",",curPos);

                                    if (resToken != L"")
                                    {
                                        contentBox.X = static_cast<REAL>(_wtof(resToken));
                                    }

                                    resToken = str.Tokenize(L",",curPos);

                                    if (resToken != L"")
                                    {
                                        contentBox.Y = static_cast<REAL>(_wtof(resToken));
                                    }

                                    resToken = str.Tokenize(L",",curPos);

                                    if (resToken != L"")
                                    {
                                        contentBox.Width = static_cast<REAL>(_wtof(resToken));
                                    }

                                    resToken = str.Tokenize(L",",curPos);

                                    if (resToken != L"")
                                    {
                                        contentBox.Height = static_cast<REAL>(_wtof(resToken));
                                    }
                                }
                                else if (bstrAttName == L"BleedBox")
                                {
                                    CStringXDW str( bstrAttValue );
                                    CStringXDW resToken;
                                    INT curPos= 0;
                                    bBleedBoxSet = TRUE;

                                    resToken = str.Tokenize(L",",curPos);

                                    if (resToken != "")
                                    {
                                        bleedBox.X = static_cast<REAL>(_wtof(resToken));
                                    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -