Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/svgio/source/svgreader/svgtextnode.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 <svgtextnode.hxx>
21
#include <svgcharacternode.hxx>
22
#include <svgstyleattributes.hxx>
23
#include <svgtrefnode.hxx>
24
#include <svgtextpathnode.hxx>
25
#include <svgtspannode.hxx>
26
#include <osl/diagnose.h>
27
28
namespace svgio::svgreader
29
{
30
        SvgTextNode::SvgTextNode(
31
            SvgDocument& rDocument,
32
            SvgNode* pParent)
33
0
        :   SvgTspanNode(SVGToken::Text, rDocument, pParent)
34
0
        {
35
0
        }
36
37
        SvgTextNode::~SvgTextNode()
38
0
        {
39
0
        }
40
41
        void SvgTextNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
42
0
        {
43
            // call parent
44
0
            SvgTspanNode::parseAttribute(aSVGToken, aContent);
45
46
            // parse own
47
0
            switch(aSVGToken)
48
0
            {
49
0
                case SVGToken::Transform:
50
0
                {
51
0
                    const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
52
53
0
                    if(!aMatrix.isIdentity())
54
0
                    {
55
0
                        setTransform(aMatrix);
56
0
                    }
57
0
                    break;
58
0
                }
59
0
                default:
60
0
                {
61
0
                    break;
62
0
                }
63
0
            }
64
0
        }
65
66
        void SvgTextNode::addTextPrimitives(
67
            const SvgNode& rCandidate,
68
            drawinglayer::primitive2d::Primitive2DContainer& rTarget,
69
            drawinglayer::primitive2d::Primitive2DContainer&& rSource)
70
0
        {
71
0
            if(rSource.empty())
72
0
                return;
73
74
0
            const SvgStyleAttributes* pAttributes = rCandidate.getSvgStyleAttributes();
75
76
0
            if(pAttributes)
77
0
            {
78
                // add text with taking all Fill/Stroke attributes into account
79
0
                pAttributes->add_text(rTarget, std::move(rSource));
80
0
            }
81
0
            else
82
0
            {
83
                // should not happen, every subnode from SvgTextNode will at least
84
                // return the attributes from SvgTextNode. Nonetheless, add text
85
0
                rTarget.append(std::move(rSource));
86
0
            }
87
0
        }
88
89
        void SvgTextNode::DecomposeChild(const SvgNode& rCandidate, drawinglayer::primitive2d::Primitive2DContainer& rTarget, SvgTextPosition& rSvgTextPosition) const
90
0
        {
91
0
            switch(rCandidate.getType())
92
0
            {
93
0
                case SVGToken::Character:
94
0
                {
95
                    // direct SvgTextPathNode derivates, decompose them
96
0
                    const SvgCharacterNode& rSvgCharacterNode = static_cast< const SvgCharacterNode& >(rCandidate);
97
0
                    rSvgCharacterNode.decomposeText(rTarget, rSvgTextPosition);
98
0
                    break;
99
0
                }
100
0
                case SVGToken::TextPath:
101
0
                {
102
                    // direct TextPath decompose
103
0
                    const SvgTextPathNode& rSvgTextPathNode = static_cast< const SvgTextPathNode& >(rCandidate);
104
0
                    const auto& rChildren = rSvgTextPathNode.getChildren();
105
0
                    const sal_uInt32 nCount(rChildren.size());
106
107
0
                    if(nCount && rSvgTextPathNode.isValid())
108
0
                    {
109
                        // remember original TextStart to later detect hor/ver offsets
110
0
                        const basegfx::B2DPoint aTextStart(rSvgTextPosition.getPosition());
111
0
                        drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
112
113
                        // decompose to regular TextPrimitives
114
0
                        for(sal_uInt32 a(0); a < nCount; a++)
115
0
                        {
116
0
                            DecomposeChild(*rChildren[a], aNewTarget, rSvgTextPosition);
117
0
                        }
118
119
0
                        if(!aNewTarget.empty())
120
0
                        {
121
0
                            const drawinglayer::primitive2d::Primitive2DContainer aPathContent(aNewTarget);
122
0
                            aNewTarget.clear();
123
124
                            // dismantle TextPrimitives and map them on curve/path
125
0
                            rSvgTextPathNode.decomposePathNode(aPathContent, aNewTarget, aTextStart);
126
0
                        }
127
128
0
                        if(!aNewTarget.empty())
129
0
                        {
130
0
                            addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
131
0
                        }
132
0
                    }
133
134
0
                    break;
135
0
                }
136
0
                case SVGToken::Tspan:
137
0
                {
138
                    // Tspan may have children, call recursively
139
0
                    const SvgTspanNode& rSvgTspanNode = static_cast< const SvgTspanNode& >(rCandidate);
140
0
                    const auto& rChildren = rSvgTspanNode.getChildren();
141
0
                    const sal_uInt32 nCount(rChildren.size());
142
143
0
                    if(nCount)
144
0
                    {
145
0
                        SvgTextPosition aSvgTextPosition(&rSvgTextPosition, rSvgTspanNode);
146
0
                        drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
147
148
0
                        for(sal_uInt32 a(0); a < nCount; a++)
149
0
                        {
150
0
                            DecomposeChild(*rChildren[a], aNewTarget, aSvgTextPosition);
151
0
                        }
152
153
0
                        rSvgTextPosition.setPosition(aSvgTextPosition.getPosition());
154
155
0
                        if(!aNewTarget.empty())
156
0
                        {
157
0
                            addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
158
0
                        }
159
0
                    }
160
0
                    break;
161
0
                }
162
0
                case SVGToken::Tref:
163
0
                {
164
0
                    const SvgTrefNode& rSvgTrefNode = static_cast< const SvgTrefNode& >(rCandidate);
165
0
                    const SvgTextNode* pRefText = rSvgTrefNode.getReferencedSvgTextNode();
166
167
0
                    if(pRefText)
168
0
                    {
169
0
                        const auto& rChildren = pRefText->getChildren();
170
0
                        const sal_uInt32 nCount(rChildren.size());
171
0
                        drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
172
173
0
                        if(nCount)
174
0
                        {
175
0
                            for(sal_uInt32 a(0); a < nCount; a++)
176
0
                            {
177
0
                                const SvgNode& rChildCandidate = *rChildren[a];
178
0
                                const_cast< SvgNode& >(rChildCandidate).setAlternativeParent(this);
179
180
0
                                DecomposeChild(rChildCandidate, aNewTarget, rSvgTextPosition);
181
0
                                const_cast< SvgNode& >(rChildCandidate).setAlternativeParent();
182
0
                            }
183
184
0
                            if(!aNewTarget.empty())
185
0
                            {
186
0
                                addTextPrimitives(rCandidate, rTarget, std::move(aNewTarget));
187
0
                            }
188
0
                        }
189
0
                    }
190
191
0
                    break;
192
0
                }
193
0
                default:
194
0
                {
195
0
                    OSL_ENSURE(false, "Unexpected node in text token (!)");
196
0
                    break;
197
0
                }
198
0
            }
199
0
        }
200
201
        void SvgTextNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool /*bReferenced`*/) const
202
0
        {
203
            // text has a group of child nodes, allowed are SVGToken::Character, SVGToken::Tspan,
204
            // SVGToken::Tref and SVGToken::TextPath. These increase a given current text position
205
0
            const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
206
207
0
            if(!pStyle || getChildren().empty())
208
0
                return;
209
210
0
            const double fOpacity(pStyle->getOpacity().getNumber());
211
212
0
            if(fOpacity <= 0.0)
213
0
                return;
214
215
0
            SvgTextPosition aSvgTextPosition(nullptr, *this);
216
0
            drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
217
0
            const auto& rChildren = getChildren();
218
0
            const sal_uInt32 nCount(rChildren.size());
219
220
0
            for(sal_uInt32 a(0); a < nCount; a++)
221
0
            {
222
0
                const SvgNode& rCandidate = *rChildren[a];
223
224
0
                DecomposeChild(rCandidate, aNewTarget, aSvgTextPosition);
225
0
            }
226
227
0
            if(!aNewTarget.empty())
228
0
            {
229
0
                drawinglayer::primitive2d::Primitive2DContainer aNewTarget2;
230
231
0
                addTextPrimitives(*this, aNewTarget2, std::move(aNewTarget));
232
0
                aNewTarget = std::move(aNewTarget2);
233
0
            }
234
235
0
            if(!aNewTarget.empty())
236
0
            {
237
0
                pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform());
238
0
            }
239
0
        }
240
241
} // end of namespace svgio::svgreader
242
243
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */