border.cxx 7.76 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

10
#include <memory>
11 12 13 14 15 16
#include <cppunit/TestAssert.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <drawinglayer/primitive2d/borderlineprimitive2d.hxx>
17
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
18 19
#include <drawinglayer/processor2d/baseprocessor2d.hxx>
#include <drawinglayer/processor2d/processorfromoutputdevice.hxx>
Stephan Bergmann's avatar
Stephan Bergmann committed
20
#include <rtl/ref.hxx>
21
#include <test/bootstrapfixture.hxx>
22
#include <vcl/metaact.hxx>
23 24
#include <vcl/vclptr.hxx>
#include <vcl/virdev.hxx>
25
#include <editeng/borderline.hxx>
26
#include <svtools/borderhelper.hxx>
27 28 29 30 31 32

using namespace com::sun::star;

namespace
{

33
class DrawinglayerBorderTest : public test::BootstrapFixture
34 35 36
{
};

37
CPPUNIT_TEST_FIXTURE(DrawinglayerBorderTest, testDoubleDecompositionSolid)
38 39 40 41 42
{
    // Create a border line primitive that's similar to the one from the bugdoc:
    // 1.47 pixels is 0.03cm at 130% zoom and 96 DPI.
    basegfx::B2DPoint aStart(0, 20);
    basegfx::B2DPoint aEnd(100, 20);
43
    double const fLeftWidth = 1.47;
44 45 46 47 48 49
    double const fDistance = 1.47;
    double const fRightWidth = 1.47;
    double const fExtendLeftStart = 0;
    double const fExtendLeftEnd = 0;
    double const fExtendRightStart = 0;
    double const fExtendRightEnd = 0;
50 51
    basegfx::BColor aColorRight;
    basegfx::BColor aColorLeft;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    const std::vector<double> aDashing(svtools::GetLineDashing(SvxBorderLineStyle::DOUBLE, 10.0));
    const drawinglayer::attribute::StrokeAttribute aStrokeAttribute(aDashing);
    std::vector< drawinglayer::primitive2d::BorderLine > aBorderlines;

    aBorderlines.push_back(
        drawinglayer::primitive2d::BorderLine(
            drawinglayer::attribute::LineAttribute(
                aColorLeft,
                fLeftWidth),
            fExtendLeftStart,
            fExtendLeftStart,
            fExtendLeftEnd,
            fExtendLeftEnd));

    aBorderlines.push_back(
        drawinglayer::primitive2d::BorderLine(fDistance));

    aBorderlines.push_back(
        drawinglayer::primitive2d::BorderLine(
            drawinglayer::attribute::LineAttribute(
                aColorRight,
                fRightWidth),
            fExtendRightStart,
            fExtendRightStart,
            fExtendRightEnd,
            fExtendRightEnd));

79 80 81 82
    rtl::Reference<drawinglayer::primitive2d::BorderLinePrimitive2D> aBorder(
        new drawinglayer::primitive2d::BorderLinePrimitive2D(
            aStart,
            aEnd,
83 84
            aBorderlines,
            aStrokeAttribute));
85 86 87

    // Decompose it into polygons.
    drawinglayer::geometry::ViewInformation2D aView;
88 89
    drawinglayer::primitive2d::Primitive2DContainer aContainer;
    aBorder->get2DDecomposition(aContainer, aView);
90 91 92 93

    // Make sure it results in two borders as it's a double one.
    CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), aContainer.size());

94 95
    // Get the inside line, now a PolygonStrokePrimitive2D
    auto pInside = dynamic_cast<const drawinglayer::primitive2d::PolygonStrokePrimitive2D*>(aContainer[0].get());
96 97 98
    CPPUNIT_ASSERT(pInside);

    // Make sure the inside line's height is fLeftWidth.
99 100
    const double fLineWidthFromDecompose = pInside->getLineAttribute().getWidth();

101
    // This was 2.47, i.e. the width of the inner line was 1 unit (in the bugdoc's case: 1 pixel) wider than expected.
102
    CPPUNIT_ASSERT_DOUBLES_EQUAL(fLeftWidth, fLineWidthFromDecompose, basegfx::fTools::getSmallValue());
103 104
}

105
CPPUNIT_TEST_FIXTURE(DrawinglayerBorderTest, testDoublePixelProcessing)
106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
    // Create a pixel processor.
    ScopedVclPtrInstance<VirtualDevice> pDev;
    drawinglayer::geometry::ViewInformation2D aView;
    std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(drawinglayer::processor2d::createBaseProcessor2DFromOutputDevice(*pDev, aView));
    CPPUNIT_ASSERT(pProcessor);
    GDIMetaFile aMetaFile;
    // Start recording after the processor is created, so we can test the pixel processor.
    aMetaFile.Record(pDev);

    // Create a border line primitive that's similar to the one from the bugdoc:
    // 1.47 pixels is 0.03cm at 130% zoom and 96 DPI.
    basegfx::B2DPoint aStart(0, 20);
    basegfx::B2DPoint aEnd(100, 20);
120 121
    double const fLeftWidth = 1.47;
    double const fDistance = 1.47;
122
    double const fRightWidth = 1.47;
123 124 125 126
    double const fExtendLeftStart = 0;
    double const fExtendLeftEnd = 0;
    double const fExtendRightStart = 0;
    double const fExtendRightEnd = 0;
127 128
    basegfx::BColor aColorRight;
    basegfx::BColor aColorLeft;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    const std::vector<double> aDashing(svtools::GetLineDashing(SvxBorderLineStyle::DOUBLE, 10.0));
    const drawinglayer::attribute::StrokeAttribute aStrokeAttribute(aDashing);
    std::vector< drawinglayer::primitive2d::BorderLine > aBorderlines;

    aBorderlines.push_back(
        drawinglayer::primitive2d::BorderLine(
            drawinglayer::attribute::LineAttribute(
                aColorLeft,
                fLeftWidth),
            fExtendLeftStart,
            fExtendLeftStart,
            fExtendLeftEnd,
            fExtendLeftEnd));

    aBorderlines.push_back(
        drawinglayer::primitive2d::BorderLine(fDistance));

    aBorderlines.push_back(
        drawinglayer::primitive2d::BorderLine(
            drawinglayer::attribute::LineAttribute(
                aColorRight,
                fRightWidth),
            fExtendRightStart,
            fExtendRightStart,
            fExtendRightEnd,
            fExtendRightEnd));

    rtl::Reference<drawinglayer::primitive2d::BorderLinePrimitive2D> aBorder(
157 158 159
        new drawinglayer::primitive2d::BorderLinePrimitive2D(
            aStart,
            aEnd,
160 161
            aBorderlines,
            aStrokeAttribute));
162

163
    drawinglayer::primitive2d::Primitive2DContainer aPrimitives;
164
    aPrimitives.push_back(drawinglayer::primitive2d::Primitive2DReference(aBorder.get()));
165 166 167 168

    // Process the primitives.
    pProcessor->process(aPrimitives);

169 170 171
    // Double line now gets decomposed in Metafile to painting four lines
    // with width == 0 in a cross pattern due to real line width being between
    // 1.0 and 2.0. Count created lines
172 173
    aMetaFile.Stop();
    aMetaFile.WindStart();
174 175
    sal_uInt32 nPolyLineActionCount = 0;

176
    for (std::size_t nAction = 0; nAction < aMetaFile.GetActionSize(); ++nAction)
177 178
    {
        MetaAction* pAction = aMetaFile.GetAction(nAction);
179 180

        if (MetaActionType::POLYLINE == pAction->GetType())
181
        {
182 183
            auto pMPLAction = static_cast<MetaPolyLineAction*>(pAction);

184
            if (0 != pMPLAction->GetLineInfo().GetWidth() && LineStyle::Solid == pMPLAction->GetLineInfo().GetStyle())
185
            {
186
                nPolyLineActionCount++;
187 188 189
            }
        }
    }
190 191 192

    // Check if all eight (2x four) simple lines with width == 0 and
    // solid were created
193 194 195 196 197 198 199 200 201 202
    //
    // This has changed: Now, just the needed 'real' lines get created
    // which have a width of 1. This are two lines. The former multiple
    // lines were a combination of view-dependent force to a single-pixel
    // line width (0 == lineWidth -> hairline) and vcl rendering this
    // using a (insane) combination of single non-AAed lines. All the
    // system-dependent part of the BorderLine stuff is now done in
    // SdrFrameBorderPrimitive2D and svx.
    // Adapted this test - still useful, breaking it may be a hint :-)
    const sal_uInt32 nExpectedNumPolyLineActions = 2;
203 204

    CPPUNIT_ASSERT_EQUAL(nExpectedNumPolyLineActions, nPolyLineActionCount);
205 206
}

207 208 209 210 211
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */