Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/include/svx/EnhancedCustomShapeFunctionParser.hxx
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
#ifndef INCLUDED_SVX_ENHANCEDCUSTOMSHAPEFUNCTIONPARSER_HXX
21
#define INCLUDED_SVX_ENHANCEDCUSTOMSHAPEFUNCTIONPARSER_HXX
22
23
#include <config_options.h>
24
#include <sal/config.h>
25
#include <com/sun/star/drawing/EnhancedCustomShapeParameter.hpp>
26
#include <memory>
27
#include <ostream>
28
#include <string_view>
29
#include <vector>
30
31
#include <svx/svxdllapi.h>
32
33
struct EnhancedCustomShapeEquation
34
{
35
    sal_Int32   nOperation;
36
    sal_Int32   nPara[ 3 ];
37
38
    EnhancedCustomShapeEquation() :
39
0
        nOperation  ( 0 )
40
0
        {
41
0
            nPara[ 0 ] = nPara[ 1 ] = nPara[ 2 ] = 0;
42
0
        }
43
};
44
45
class EnhancedCustomShape2d;
46
47
namespace EnhancedCustomShape {
48
49
enum class ExpressionFunct
50
{
51
    Const,
52
53
    EnumPi,
54
    EnumLeft,
55
    EnumTop,
56
    EnumRight,
57
    EnumBottom,
58
    EnumXStretch,
59
    EnumYStretch,
60
    EnumHasStroke,
61
    EnumHasFill,
62
    EnumWidth,
63
    EnumHeight,
64
    EnumLogWidth,
65
    EnumLogHeight,
66
    EnumAdjustment,
67
    EnumEquation,
68
69
    UnaryAbs,
70
    UnarySqrt,
71
    UnarySin,
72
    UnaryCos,
73
    UnaryTan,
74
    UnaryAtan,
75
    UnaryNeg,
76
77
    BinaryPlus,
78
    BinaryMinus,
79
    BinaryMul,
80
    BinaryDiv,
81
    BinaryMin,
82
    BinaryMax,
83
    BinaryAtan2,
84
85
    TernaryIf
86
};
87
88
template< typename charT, typename traits >
89
inline std::basic_ostream<charT, traits> & operator <<(
90
    std::basic_ostream<charT, traits> & stream, const ExpressionFunct& eFunc )
91
0
{
92
0
    switch (eFunc)
93
0
    {
94
0
    case ExpressionFunct::Const : return stream << "const";
95
96
0
    case ExpressionFunct::EnumPi : return stream << "pi";
97
0
    case ExpressionFunct::EnumLeft : return stream << "left";
98
0
    case ExpressionFunct::EnumTop : return stream << "top";
99
0
    case ExpressionFunct::EnumRight : return stream << "right";
100
0
    case ExpressionFunct::EnumBottom : return stream << "bottom";
101
0
    case ExpressionFunct::EnumXStretch : return stream << "xstretch";
102
0
    case ExpressionFunct::EnumYStretch : return stream << "ystretch";
103
0
    case ExpressionFunct::EnumHasStroke : return stream << "hasstroke";
104
0
    case ExpressionFunct::EnumHasFill : return stream << "hasfill";
105
0
    case ExpressionFunct::EnumWidth : return stream << "width";
106
0
    case ExpressionFunct::EnumHeight : return stream << "height";
107
0
    case ExpressionFunct::EnumLogWidth : return stream << "logwidth";
108
0
    case ExpressionFunct::EnumLogHeight : return stream << "logheight";
109
0
    case ExpressionFunct::EnumAdjustment : return stream << "adjustment";
110
0
    case ExpressionFunct::EnumEquation : return stream << "equation";
111
112
0
    case ExpressionFunct::UnaryAbs : return stream << "abs";
113
0
    case ExpressionFunct::UnarySqrt : return stream << "sqrt";
114
0
    case ExpressionFunct::UnarySin : return stream << "sin";
115
0
    case ExpressionFunct::UnaryCos : return stream << "cos";
116
0
    case ExpressionFunct::UnaryTan : return stream << "tan";
117
0
    case ExpressionFunct::UnaryAtan : return stream << "atan";
118
0
    case ExpressionFunct::UnaryNeg : return stream << "neg";
119
120
0
    case ExpressionFunct::BinaryPlus : return stream << "plus";
121
0
    case ExpressionFunct::BinaryMinus : return stream << "minus";
122
0
    case ExpressionFunct::BinaryMul : return stream << "mul";
123
0
    case ExpressionFunct::BinaryDiv : return stream << "div";
124
0
    case ExpressionFunct::BinaryMin : return stream << "min";
125
0
    case ExpressionFunct::BinaryMax : return stream << "max";
126
0
    case ExpressionFunct::BinaryAtan2 : return stream << "atan2";
127
128
0
    case ExpressionFunct::TernaryIf : return stream << "if";
129
130
0
    default: return stream << "?(" << static_cast<int>(eFunc) << ")";
131
0
    }
132
0
}
133
134
0
#define EXPRESSION_FLAG_SUMANGLE_MODE 1
135
136
SVXCORE_DLLPUBLIC void FillEquationParameter( const css::drawing::EnhancedCustomShapeParameter&, const sal_Int32, EnhancedCustomShapeEquation& );
137
138
class SAL_DLLPUBLIC_RTTI ExpressionNode
139
{
140
public:
141
    virtual ~ExpressionNode();
142
143
    /** Predicate whether this node is constant.
144
145
        This predicate returns true, if this node is
146
        neither time- nor ViewInfo dependent. This allows
147
        for certain optimizations, i.e. not the full
148
        expression tree needs be represented by
149
        ExpressionNodes.
150
151
        @returns true, if the note is constant
152
    */
153
    virtual bool isConstant() const = 0;
154
155
    /** Operator to calculate function value.
156
157
        This method calculates the function value.
158
    */
159
    virtual double operator()() const = 0;
160
161
    /** Operator to retrieve the type of expression node
162
    */
163
    virtual ExpressionFunct getType() const = 0;
164
165
    /** Operator to retrieve the ms version of expression
166
    */
167
    virtual css::drawing::EnhancedCustomShapeParameter fillNode(
168
        std::vector< EnhancedCustomShapeEquation >& rEquations, ExpressionNode* pOptionalArg, sal_uInt32 nFlags ) = 0;
169
};
170
171
/** This exception is thrown, when the arithmetic expression
172
    parser failed to parse a string.
173
    */
174
struct ParseError
175
{
176
4.59k
    ParseError( const char* ) {}
177
};
178
179
class FunctionParser
180
{
181
public:
182
183
    /** Parse a string
184
185
        The following grammar is accepted by this method:
186
        <code>
187
188
        number_digit = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
189
190
        number = number number_digit | number_digit
191
192
        identifier = 'pi'|'left'|'top'|'right'|'bottom'|'xstretch'|'ystretch'|
193
                 'hasstroke'|'hasfill'|'width'|'height'|'logwidth'|'logheight'
194
195
        unary_function = 'abs'|'sqrt'|'sin'|'cos'|'tan'|'atan'
196
        binary_function = 'min'|'max'|'atan2'
197
        ternary_function = 'if'
198
199
        function_reference = '?' 'a-z,A-Z,0-9' ' '
200
        modifier_reference = '$' '0-9' ' '
201
202
        basic_expression =
203
            number |
204
            identifier |
205
            function_reference |
206
            unary_function '(' additive_expression ')' |
207
            binary_function '(' additive_expression ',' additive_expression ')' |
208
            ternary_function '(' additive_expression ',' additive_expression ',
209
                               ' additive_expression ')' | '(' additive_expression ')'
210
211
        unary_expression = '-' basic_expression
212
213
        multiplicative_expression =
214
                          basic_expression |
215
                          multiplicative_expression '*' basic_expression |
216
                          multiplicative_expression '/' basic_expression
217
218
        additive_expression =
219
                        multiplicative_expression |
220
                        additive_expression '+' multiplicative_expression |
221
                        additive_expression '-' multiplicative_expression
222
223
        </code>
224
225
        @param rFunction
226
        The string to parse
227
228
        @param rCustoShape
229
        The CustomShape is required for calculation of dynamic values such
230
        "hasstroke", function references and or modifier references ...
231
232
        @throws ParseError if an invalid expression is given.
233
234
        @return the generated function object.
235
       */
236
237
    UNLESS_MERGELIBS(SVXCORE_DLLPUBLIC) static std::shared_ptr<ExpressionNode> const & parseFunction( std::u16string_view rFunction, const EnhancedCustomShape2d& rCustoShape );
238
239
    // this is a singleton
240
    FunctionParser() = delete;
241
    FunctionParser(const FunctionParser&) = delete;
242
    FunctionParser& operator=( const FunctionParser& ) = delete;
243
};
244
245
}
246
247
#endif // INCLUDED_SVX_ENHANCEDCUSTOMSHAPEFUNCTIONPARSER_HXX
248
249
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */