1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _ENHANCEDCUSTOMSHAPEFUNCTIONPARSER_HXX
25 #define _ENHANCEDCUSTOMSHAPEFUNCTIONPARSER_HXX
26 
27 #include <sal/config.h>
28 #include <boost/shared_ptr.hpp>
29 #include "EnhancedCustomShapeFunctionParser.hxx"
30 #include <com/sun/star/drawing/EnhancedCustomShapeParameter.hpp>
31 #include <com/sun/star/drawing/EnhancedCustomShapeParameterType.hpp>
32 #include <vector>
33 
34 #include <svx/svxdllapi.h>
35 
36 struct EnhancedCustomShapeEquation
37 {
38 	sal_Int32	nOperation;
39 	sal_Int32	nPara[ 3 ];
40 
EnhancedCustomShapeEquationEnhancedCustomShapeEquation41 	EnhancedCustomShapeEquation() :
42 		nOperation	( 0 )
43 		{
44 			nPara[ 0 ] = nPara[ 1 ] = nPara[ 2 ] = 0;
45 		}
46 };
47 
48 class EnhancedCustomShape2d;
49 
50 namespace EnhancedCustomShape {
51 
52 enum ExpressionFunct
53 {
54 	FUNC_CONST,
55 
56 	ENUM_FUNC_PI,
57 	ENUM_FUNC_LEFT,
58 	ENUM_FUNC_TOP,
59 	ENUM_FUNC_RIGHT,
60 	ENUM_FUNC_BOTTOM,
61 	ENUM_FUNC_XSTRETCH,
62 	ENUM_FUNC_YSTRETCH,
63 	ENUM_FUNC_HASSTROKE,
64 	ENUM_FUNC_HASFILL,
65 	ENUM_FUNC_WIDTH,
66 	ENUM_FUNC_HEIGHT,
67 	ENUM_FUNC_LOGWIDTH,
68 	ENUM_FUNC_LOGHEIGHT,
69 	ENUM_FUNC_ADJUSTMENT,
70 	ENUM_FUNC_EQUATION,
71 
72 	UNARY_FUNC_ABS,
73 	UNARY_FUNC_SQRT,
74 	UNARY_FUNC_SIN,
75 	UNARY_FUNC_COS,
76 	UNARY_FUNC_TAN,
77 	UNARY_FUNC_ATAN,
78 	UNARY_FUNC_NEG,
79 
80 	BINARY_FUNC_PLUS,
81 	BINARY_FUNC_MINUS,
82 	BINARY_FUNC_MUL,
83 	BINARY_FUNC_DIV,
84 	BINARY_FUNC_MIN,
85 	BINARY_FUNC_MAX,
86 	BINARY_FUNC_ATAN2,
87 
88 	TERNARY_FUNC_IF
89 };
90 
91 #define EXPRESSION_FLAG_SUMANGLE_MODE 1
92 
93 SVX_DLLPUBLIC void FillEquationParameter( const com::sun::star::drawing::EnhancedCustomShapeParameter&, const sal_Int32, EnhancedCustomShapeEquation& );
94 
95 class ExpressionNode
96 {
97 public:
98     virtual ~ExpressionNode();
99 
100     /** Predicate whether this node is constant.
101 
102         This predicate returns true, if this node is
103         neither time- nor ViewInfo dependent. This allows
104         for certain obtimizations, i.e. not the full
105         expression tree needs be represented by
106         ExpressionNodes.
107 
108         @returns true, if the note is constant
109     */
110     virtual bool isConstant() const = 0;
111 
112     /** Operator to calculate function value.
113 
114         This method calculates the function value.
115 	*/
116     virtual double operator()() const = 0;
117 
118 	/** Operator to retrieve the type of expression node
119 	*/
120 	virtual ExpressionFunct getType() const = 0;
121 
122 	/** Operator to retrieve the ms version of expression
123 	*/
124 	virtual com::sun::star::drawing::EnhancedCustomShapeParameter fillNode(
125 		std::vector< EnhancedCustomShapeEquation >& rEquations, ExpressionNode* pOptionalArg, sal_uInt32 nFlags ) = 0;
126 };
127 typedef ::boost::shared_ptr< ExpressionNode > ExpressionNodeSharedPtr;
128 
129 /** This exception is thrown, when the arithmetic expression
130     parser failed to parse a string.
131     */
132 struct ParseError
133 {
ParseErrorEnhancedCustomShape::ParseError134     ParseError() {}
ParseErrorEnhancedCustomShape::ParseError135     ParseError( const char* ) {}
136 };
137 
138 class FunctionParser
139 {
140 public:
141 
142     /** Parse a string
143 
144         The following grammar is accepted by this method:
145         <code>
146 
147 		number_digit = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
148 
149 		number = number number_digit | number_digit
150 
151 		identifier = 'pi'|'left'|'top'|'right'|'bottom'|'xstretch'|'ystretch'|
152 				 'hasstroke'|'hasfill'|'width'|'height'|'logwidth'|'logheight'
153 
154 		unary_function = 'abs'|'sqrt'|'sin'|'cos'|'tan'|'atan'
155 		binary_function = 'min'|'max'|'atan2'
156 		ternary_function = 'if'
157 
158 		function_reference = '?' 'a-z,A-Z,0-9' ' '
159 		modifier_reference = '$' '0-9' ' '
160 
161 		basic_expression =
162 			number |
163 			identifier |
164 			function_reference |
165 			unary_function '(' additive_expression ')' |
166 			binary_function '(' additive_expression ',' additive_expression ')' |
167 			ternary_function '(' additive_expression ',' additive_expression ',
168 					           ' additive_expression ')' | '(' additive_expression ')'
169 
170 		unary_expression = '-' basic_expression
171 
172 		multiplicative_expression =
173 						  basic_expression |
174 						  multiplicative_expression '*' basic_expression |
175 						  multiplicative_expression '/' basic_expression
176 
177 		additive_expression =
178 						multiplicative_expression |
179 						additive_expression '+' multiplicative_expression |
180 						additive_expression '-' multiplicative_expression
181 
182         </code>
183 
184         @param rFunction
185         The string to parse
186 
187         @param rCustoShape
188         The CustomShape is required for calculation of dynamic values such
189 		"hasstroke", function references and or modifier references ...
190 
191         @throws ParseError if an invalid expression is given.
192 
193         @return the generated function object.
194        */
195 
196     SVX_DLLPUBLIC static ExpressionNodeSharedPtr parseFunction( const ::rtl::OUString& rFunction, const EnhancedCustomShape2d& rCustoShape );
197 
198 private:
199     // disabled constructor/destructor, since this is
200     // supposed to be a singleton
201     FunctionParser();
202 
203     // default: disabled copy/assignment
204     FunctionParser(const FunctionParser&);
205     FunctionParser& operator=( const FunctionParser& );
206 };
207 
208 }
209 
210 #endif /* _ENHANCEDCUSTOMSHAPEFUNCTIONPARSER_HXX */
211