Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/drawinglayer/source/primitive2d/texteffectprimitive2d.cxx
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <primitive2d/texteffectprimitive2d.hxx>
21
#include <drawinglayer/geometry/viewinformation2d.hxx>
22
#include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
23
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
24
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
25
#include <basegfx/matrix/b2dhommatrixtools.hxx>
26
27
namespace drawinglayer::primitive2d
28
{
29
const double fDiscreteSize(1.1);
30
31
Primitive2DReference TextEffectPrimitive2D::create2DDecomposition(
32
    const geometry::ViewInformation2D& rViewInformation) const
33
0
{
34
    // get the distance of one discrete units from target display. Use between 1.0 and sqrt(2) to
35
    // have good results on rotated objects, too
36
0
    const basegfx::B2DVector aDistance(rViewInformation.getInverseObjectToViewTransformation()
37
0
                                       * basegfx::B2DVector(fDiscreteSize, fDiscreteSize));
38
0
    const basegfx::B2DVector aDiagonalDistance(aDistance * (1.0 / 1.44));
39
40
0
    Primitive2DContainer aContainer;
41
0
    switch (getTextEffectStyle2D())
42
0
    {
43
0
        case TextEffectStyle2D::ReliefEmbossed:
44
0
        case TextEffectStyle2D::ReliefEmbossedDefault:
45
0
        case TextEffectStyle2D::ReliefEngravedDefault:
46
0
        {
47
            // prepare transform of sub-group back to (0,0) and align to X-Axis
48
0
            basegfx::B2DHomMatrix aBackTransform(basegfx::utils::createTranslateB2DHomMatrix(
49
0
                -getRotationCenter().getX(), -getRotationCenter().getY()));
50
0
            aBackTransform.rotate(-getDirection());
51
52
            // prepare transform of sub-group back to its position and rotation
53
0
            basegfx::B2DHomMatrix aForwardTransform(
54
0
                basegfx::utils::createRotateB2DHomMatrix(getDirection()));
55
0
            aForwardTransform.translate(getRotationCenter().getX(), getRotationCenter().getY());
56
57
            // create transformation for one discrete unit
58
0
            const bool bEmbossed(TextEffectStyle2D::ReliefEmbossed == getTextEffectStyle2D()
59
0
                                 || TextEffectStyle2D::ReliefEmbossedDefault
60
0
                                        == getTextEffectStyle2D());
61
0
            const bool bDefaultTextColor(
62
0
                TextEffectStyle2D::ReliefEmbossedDefault == getTextEffectStyle2D()
63
0
                || TextEffectStyle2D::ReliefEngravedDefault == getTextEffectStyle2D());
64
0
            basegfx::B2DHomMatrix aTransform(aBackTransform);
65
66
0
            if (bEmbossed)
67
0
            {
68
                // to bottom-right
69
0
                aTransform.translate(aDiagonalDistance.getX(), aDiagonalDistance.getY());
70
0
            }
71
0
            else
72
0
            {
73
                // to top-left
74
0
                aTransform.translate(-aDiagonalDistance.getX(), -aDiagonalDistance.getY());
75
0
            }
76
77
0
            aTransform *= aForwardTransform;
78
79
0
            if (bDefaultTextColor)
80
0
            {
81
                // emboss/engrave in black, original forced to white
82
0
                const basegfx::BColorModifierSharedPtr aBColorModifierToGray
83
0
                    = std::make_shared<basegfx::BColorModifier_replace>(basegfx::BColor(0.0));
84
0
                const Primitive2DReference xModifiedColor(new ModifiedColorPrimitive2D(
85
0
                    Primitive2DContainer(getTextContent()), aBColorModifierToGray));
86
87
0
                aContainer.push_back(
88
0
                    new TransformPrimitive2D(aTransform, Primitive2DContainer{ xModifiedColor }));
89
90
                // add original, too
91
0
                const basegfx::BColorModifierSharedPtr aBColorModifierToWhite
92
0
                    = std::make_shared<basegfx::BColorModifier_replace>(basegfx::BColor(1.0));
93
94
0
                aContainer.push_back(new ModifiedColorPrimitive2D(
95
0
                    Primitive2DContainer(getTextContent()), aBColorModifierToWhite));
96
0
            }
97
0
            else
98
0
            {
99
                // emboss/engrave in gray, keep original's color
100
0
                const basegfx::BColorModifierSharedPtr aBColorModifierToGray
101
0
                    = std::make_shared<basegfx::BColorModifier_replace>(
102
0
                        basegfx::BColor(0.75)); // 192
103
0
                const Primitive2DReference xModifiedColor(new ModifiedColorPrimitive2D(
104
0
                    Primitive2DContainer(getTextContent()), aBColorModifierToGray));
105
106
0
                aContainer.push_back(
107
0
                    new TransformPrimitive2D(aTransform, Primitive2DContainer{ xModifiedColor }));
108
109
                // add original, too
110
0
                aContainer.push_back(new GroupPrimitive2D(Primitive2DContainer(getTextContent())));
111
0
            }
112
113
0
            break;
114
0
        }
115
0
        case TextEffectStyle2D::Outline:
116
0
        {
117
            // create transform primitives in all directions
118
0
            basegfx::B2DHomMatrix aTransform;
119
120
0
            aTransform.set(0, 2, aDistance.getX());
121
0
            aTransform.set(1, 2, 0.0);
122
0
            aContainer.push_back(
123
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
124
125
0
            aTransform.set(0, 2, aDiagonalDistance.getX());
126
0
            aTransform.set(1, 2, aDiagonalDistance.getY());
127
0
            aContainer.push_back(
128
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
129
130
0
            aTransform.set(0, 2, 0.0);
131
0
            aTransform.set(1, 2, aDistance.getY());
132
0
            aContainer.push_back(
133
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
134
135
0
            aTransform.set(0, 2, -aDiagonalDistance.getX());
136
0
            aTransform.set(1, 2, aDiagonalDistance.getY());
137
0
            aContainer.push_back(
138
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
139
140
0
            aTransform.set(0, 2, -aDistance.getX());
141
0
            aTransform.set(1, 2, 0.0);
142
0
            aContainer.push_back(
143
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
144
145
0
            aTransform.set(0, 2, -aDiagonalDistance.getX());
146
0
            aTransform.set(1, 2, -aDiagonalDistance.getY());
147
0
            aContainer.push_back(
148
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
149
150
0
            aTransform.set(0, 2, 0.0);
151
0
            aTransform.set(1, 2, -aDistance.getY());
152
0
            aContainer.push_back(
153
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
154
155
0
            aTransform.set(0, 2, aDiagonalDistance.getX());
156
0
            aTransform.set(1, 2, -aDiagonalDistance.getY());
157
0
            aContainer.push_back(
158
0
                new TransformPrimitive2D(aTransform, Primitive2DContainer(getTextContent())));
159
160
            // at last, place original over it, but force to white
161
0
            const basegfx::BColorModifierSharedPtr aBColorModifierToWhite
162
0
                = std::make_shared<basegfx::BColorModifier_replace>(basegfx::BColor(1.0, 1.0, 1.0));
163
0
            aContainer.push_back(new ModifiedColorPrimitive2D(
164
0
                Primitive2DContainer(getTextContent()), aBColorModifierToWhite));
165
166
0
            break;
167
0
        }
168
0
    }
169
0
    return new GroupPrimitive2D(std::move(aContainer));
170
0
}
171
172
TextEffectPrimitive2D::TextEffectPrimitive2D(Primitive2DContainer&& rTextContent,
173
                                             const basegfx::B2DPoint& rRotationCenter,
174
                                             double fDirection,
175
                                             TextEffectStyle2D eTextEffectStyle2D)
176
0
    : maTextContent(std::move(rTextContent))
177
0
    , maRotationCenter(rRotationCenter)
178
0
    , mfDirection(fDirection)
179
0
    , meTextEffectStyle2D(eTextEffectStyle2D)
180
0
{
181
0
}
182
183
bool TextEffectPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
184
0
{
185
0
    if (BasePrimitive2D::operator==(rPrimitive))
186
0
    {
187
0
        const TextEffectPrimitive2D& rCompare
188
0
            = static_cast<const TextEffectPrimitive2D&>(rPrimitive);
189
190
0
        return (getTextContent() == rCompare.getTextContent()
191
0
                && getRotationCenter() == rCompare.getRotationCenter()
192
0
                && getDirection() == rCompare.getDirection()
193
0
                && getTextEffectStyle2D() == rCompare.getTextEffectStyle2D());
194
0
    }
195
196
0
    return false;
197
0
}
198
199
basegfx::B2DRange
200
TextEffectPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
201
0
{
202
    // get range of content and grow by used fDiscreteSize. That way it is not necessary to ask
203
    // the whole decomposition for its ranges (which may be expensive with outline mode which
204
    // then will ask 9 times at nearly the same content. This may even be refined here using the
205
    // TextEffectStyle information, e.g. for TEXTEFFECTSTYLE2D_RELIEF the grow needs only to
206
    // be in two directions
207
0
    basegfx::B2DRange aRetval(getTextContent().getB2DRange(rViewInformation));
208
0
    aRetval.grow(fDiscreteSize);
209
210
0
    return aRetval;
211
0
}
212
213
void TextEffectPrimitive2D::get2DDecomposition(
214
    Primitive2DDecompositionVisitor& rVisitor,
215
    const geometry::ViewInformation2D& rViewInformation) const
216
0
{
217
0
    if (hasBuffered2DDecomposition())
218
0
    {
219
0
        if (maLastObjectToViewTransformation != rViewInformation.getObjectToViewTransformation())
220
0
        {
221
            // conditions of last local decomposition have changed, delete
222
0
            const_cast<TextEffectPrimitive2D*>(this)->setBuffered2DDecomposition(nullptr);
223
0
        }
224
0
    }
225
226
0
    if (!hasBuffered2DDecomposition())
227
0
    {
228
        // remember ViewRange and ViewTransformation
229
0
        const_cast<TextEffectPrimitive2D*>(this)->maLastObjectToViewTransformation
230
0
            = rViewInformation.getObjectToViewTransformation();
231
0
    }
232
233
    // use parent implementation
234
0
    BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
235
0
}
236
237
// provide unique ID
238
sal_uInt32 TextEffectPrimitive2D::getPrimitive2DID() const
239
0
{
240
0
    return PRIMITIVE2D_ID_TEXTEFFECTPRIMITIVE2D;
241
0
}
242
243
} // end of namespace
244
245
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */