xref: /aoo42x/main/oox/source/vml/vmlformatting.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/vml/vmlformatting.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <rtl/strbuf.hxx>
27cdf0e10cSrcweir #include "oox/drawingml/color.hxx"
28cdf0e10cSrcweir #include "oox/drawingml/drawingmltypes.hxx"
29cdf0e10cSrcweir #include "oox/drawingml/fillproperties.hxx"
30cdf0e10cSrcweir #include "oox/drawingml/lineproperties.hxx"
31cdf0e10cSrcweir #include "oox/drawingml/shapepropertymap.hxx"
32cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
33cdf0e10cSrcweir #include "oox/helper/graphichelper.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace oox {
36cdf0e10cSrcweir namespace vml {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // ============================================================================
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star::geometry;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using ::oox::drawingml::Color;
43cdf0e10cSrcweir using ::oox::drawingml::FillProperties;
44cdf0e10cSrcweir using ::oox::drawingml::LineArrowProperties;
45cdf0e10cSrcweir using ::oox::drawingml::LineProperties;
46cdf0e10cSrcweir using ::oox::drawingml::ShapePropertyMap;
47cdf0e10cSrcweir using ::rtl::OStringBuffer;
48cdf0e10cSrcweir using ::rtl::OUString;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // ============================================================================
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace {
53cdf0e10cSrcweir 
lclExtractDouble(double & orfValue,sal_Int32 & ornEndPos,const OUString & rValue)54cdf0e10cSrcweir bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     // extract the double value and find start position of unit characters
57cdf0e10cSrcweir     rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok;
58cdf0e10cSrcweir     orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos );
59cdf0e10cSrcweir     return eConvStatus == rtl_math_ConversionStatus_Ok;
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir } // namespace
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // ----------------------------------------------------------------------------
65cdf0e10cSrcweir 
separatePair(OUString & orValue1,OUString & orValue2,const OUString & rValue,sal_Unicode cSep)66cdf0e10cSrcweir /*static*/ bool ConversionHelper::separatePair( OUString& orValue1, OUString& orValue2,
67cdf0e10cSrcweir         const OUString& rValue, sal_Unicode cSep )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     sal_Int32 nSepPos = rValue.indexOf( cSep );
70cdf0e10cSrcweir     if( nSepPos >= 0 )
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         orValue1 = rValue.copy( 0, nSepPos ).trim();
73cdf0e10cSrcweir         orValue2 = rValue.copy( nSepPos + 1 ).trim();
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir     else
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         orValue1 = rValue.trim();
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir     return (orValue1.getLength() > 0) && (orValue2.getLength() > 0);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
decodeBool(const OUString & rValue)82cdf0e10cSrcweir /*static*/ bool ConversionHelper::decodeBool( const OUString& rValue )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
85cdf0e10cSrcweir     // anything else than 't' or 'true' is considered to be false, as specified
86cdf0e10cSrcweir     return (nToken == XML_t) || (nToken == XML_true);
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
decodePercent(const OUString & rValue,double fDefValue)89cdf0e10cSrcweir /*static*/ double ConversionHelper::decodePercent( const OUString& rValue, double fDefValue )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     if( rValue.getLength() == 0 )
92cdf0e10cSrcweir         return fDefValue;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     double fValue = 0.0;
95cdf0e10cSrcweir     sal_Int32 nEndPos = 0;
96cdf0e10cSrcweir     if( !lclExtractDouble( fValue, nEndPos, rValue ) )
97cdf0e10cSrcweir         return fDefValue;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     if( nEndPos == rValue.getLength() )
100cdf0e10cSrcweir         return fValue;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == '%') )
103cdf0e10cSrcweir         return fValue / 100.0;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     OSL_ENSURE( false, "ConversionHelper::decodePercent - unknown measure unit" );
106cdf0e10cSrcweir     return fDefValue;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
decodeMeasureToEmu(const GraphicHelper & rGraphicHelper,const OUString & rValue,sal_Int32 nRefValue,bool bPixelX,bool bDefaultAsPixel)109cdf0e10cSrcweir /*static*/ sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper,
110cdf0e10cSrcweir         const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     // default for missing values is 0
113cdf0e10cSrcweir     if( rValue.getLength() == 0 )
114cdf0e10cSrcweir         return 0;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     // TODO: according to spec, value may contain "auto"
117cdf0e10cSrcweir     if( rValue.equalsAscii( "auto" ) )
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         OSL_ENSURE( false, "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" );
120cdf0e10cSrcweir         return nRefValue;
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     // extract the double value and find start position of unit characters
124cdf0e10cSrcweir     double fValue = 0.0;
125cdf0e10cSrcweir     sal_Int32 nEndPos = 0;
126cdf0e10cSrcweir     if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) )
127cdf0e10cSrcweir         return 0;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     // process trailing unit, convert to EMU
130cdf0e10cSrcweir     static const OUString saPx = CREATE_OUSTRING( "px" );
131cdf0e10cSrcweir     OUString aUnit;
132cdf0e10cSrcweir     if( (0 < nEndPos) && (nEndPos < rValue.getLength()) )
133cdf0e10cSrcweir         aUnit = rValue.copy( nEndPos );
134cdf0e10cSrcweir     else if( bDefaultAsPixel )
135cdf0e10cSrcweir         aUnit = saPx;
136cdf0e10cSrcweir     // else default is EMU
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     if( aUnit.getLength() == 2 )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         sal_Unicode cChar1 = aUnit[ 0 ];
141cdf0e10cSrcweir         sal_Unicode cChar2 = aUnit[ 1 ];
142cdf0e10cSrcweir         if( (cChar1 == 'i') && (cChar2 == 'n') )        // 1 inch = 914,400 EMU
143cdf0e10cSrcweir             fValue *= 914400.0;
144cdf0e10cSrcweir         else if( (cChar1 == 'c') && (cChar2 == 'm') )   // 1 cm = 360,000 EMU
145cdf0e10cSrcweir             fValue *= 360000.0;
146cdf0e10cSrcweir         else if( (cChar1 == 'm') && (cChar2 == 'm') )   // 1 mm = 36,000 EMU
147cdf0e10cSrcweir             fValue *= 36000.0;
148cdf0e10cSrcweir         else if( (cChar1 == 'p') && (cChar2 == 't') )   // 1 point = 1/72 inch = 12,700 MEU
149cdf0e10cSrcweir             fValue *= 12700.0;
150cdf0e10cSrcweir         else if( (cChar1 == 'p') && (cChar2 == 'c') )   // 1 pica = 1/6 inch = 152,400 EMU
151cdf0e10cSrcweir             fValue *= 152400.0;
152cdf0e10cSrcweir         else if( (cChar1 == 'p') && (cChar2 == 'x') )   // 1 pixel, dependent on output device
153cdf0e10cSrcweir             fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu(
154cdf0e10cSrcweir                 bPixelX ?
155cdf0e10cSrcweir                     rGraphicHelper.convertScreenPixelXToHmm( fValue ) :
156cdf0e10cSrcweir                     rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) );
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir     else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') )
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         fValue *= nRefValue / 100.0;
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir     else if( bDefaultAsPixel || (aUnit.getLength() > 0) )   // default as EMU and no unit -> do nothing
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         OSL_ENSURE( false, "ConversionHelper::decodeMeasureToEmu - unknown measure unit" );
165cdf0e10cSrcweir         fValue = nRefValue;
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     return static_cast< sal_Int64 >( fValue + 0.5 );
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
decodeMeasureToHmm(const GraphicHelper & rGraphicHelper,const OUString & rValue,sal_Int32 nRefValue,bool bPixelX,bool bDefaultAsPixel)170cdf0e10cSrcweir /*static*/ sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper,
171cdf0e10cSrcweir         const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) );
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
decodeColor(const GraphicHelper & rGraphicHelper,const OptValue<OUString> & roVmlColor,const OptValue<double> & roVmlOpacity,sal_Int32 nDefaultRgb,sal_Int32 nPrimaryRgb)176cdf0e10cSrcweir /*static*/ Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper,
177cdf0e10cSrcweir         const OptValue< OUString >& roVmlColor, const OptValue< double >& roVmlOpacity,
178cdf0e10cSrcweir         sal_Int32 nDefaultRgb, sal_Int32 nPrimaryRgb )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     Color aDmlColor;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     // convert opacity
183cdf0e10cSrcweir     const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT;
184cdf0e10cSrcweir     double fOpacity = roVmlOpacity.get( 1.0 );
185cdf0e10cSrcweir     sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE );
186cdf0e10cSrcweir     if( nOpacity < DML_FULL_OPAQUE )
187cdf0e10cSrcweir         aDmlColor.addTransformation( XML_alpha, nOpacity );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     // color attribute not present - set passed default color
190cdf0e10cSrcweir     if( !roVmlColor.has() )
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         aDmlColor.setSrgbClr( nDefaultRgb );
193cdf0e10cSrcweir         return aDmlColor;
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     // separate leading color name or RGB value from following palette index
197cdf0e10cSrcweir     OUString aColorName, aColorIndex;
198cdf0e10cSrcweir     separatePair( aColorName, aColorIndex, roVmlColor.get(), ' ' );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     // RGB colors in the format '#RRGGBB'
201cdf0e10cSrcweir     if( (aColorName.getLength() == 7) && (aColorName[ 0 ] == '#') )
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         aDmlColor.setSrgbClr( aColorName.copy( 1 ).toInt32( 16 ) );
204cdf0e10cSrcweir         return aDmlColor;
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir     // RGB colors in the format '#RGB'
208cdf0e10cSrcweir     if( (aColorName.getLength() == 4) && (aColorName[ 0 ] == '#') )
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         sal_Int32 nR = aColorName.copy( 1, 1 ).toInt32( 16 ) * 0x11;
211cdf0e10cSrcweir         sal_Int32 nG = aColorName.copy( 2, 1 ).toInt32( 16 ) * 0x11;
212cdf0e10cSrcweir         sal_Int32 nB = aColorName.copy( 3, 1 ).toInt32( 16 ) * 0x11;
213cdf0e10cSrcweir         aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB );
214cdf0e10cSrcweir         return aDmlColor;
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     /*  Predefined color names or system color names (resolve to RGB to detect
218cdf0e10cSrcweir         valid color name). */
219cdf0e10cSrcweir     sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName );
220cdf0e10cSrcweir     sal_Int32 nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
221cdf0e10cSrcweir     if( nRgbValue == API_RGB_TRANSPARENT )
222cdf0e10cSrcweir         nRgbValue = rGraphicHelper.getSystemColor( nColorToken, API_RGB_TRANSPARENT );
223cdf0e10cSrcweir     if( nRgbValue != API_RGB_TRANSPARENT )
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         aDmlColor.setSrgbClr( nRgbValue );
226cdf0e10cSrcweir         return aDmlColor;
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     // try palette colors enclosed in brackets
230cdf0e10cSrcweir     if( (aColorIndex.getLength() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.getLength() - 1 ] == ']') )
231cdf0e10cSrcweir     {
232cdf0e10cSrcweir         aDmlColor.setPaletteClr( aColorIndex.copy( 1, aColorIndex.getLength() - 2 ).toInt32() );
233cdf0e10cSrcweir         return aDmlColor;
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     // try fill gradient modificator 'fill <modifier>(<amount>)'
237cdf0e10cSrcweir     if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) )
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         sal_Int32 nOpenParen = aColorIndex.indexOf( '(' );
240cdf0e10cSrcweir         sal_Int32 nCloseParen = aColorIndex.indexOf( ')' );
241cdf0e10cSrcweir         if( (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.getLength()) )
242cdf0e10cSrcweir         {
243cdf0e10cSrcweir             sal_Int32 nModToken = XML_TOKEN_INVALID;
244cdf0e10cSrcweir             switch( AttributeConversion::decodeToken( aColorIndex.copy( 0, nOpenParen ) ) )
245cdf0e10cSrcweir             {
246cdf0e10cSrcweir                 case XML_darken:    nModToken = XML_shade;
247cdf0e10cSrcweir                 case XML_lighten:   nModToken = XML_tint;
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir             sal_Int32 nValue = aColorIndex.copy( nOpenParen + 1, nCloseParen - nOpenParen - 1 ).toInt32();
250cdf0e10cSrcweir             if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) )
251cdf0e10cSrcweir             {
252cdf0e10cSrcweir                 /*  Simulate this modifier color by a color with related transformation.
253cdf0e10cSrcweir                     The modifier amount has to be converted from the range [0;255] to
254cdf0e10cSrcweir                     percentage [0;100000] used by DrawingML. */
255cdf0e10cSrcweir                 aDmlColor.setSrgbClr( nPrimaryRgb );
256cdf0e10cSrcweir                 aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) );
257cdf0e10cSrcweir                 return aDmlColor;
258cdf0e10cSrcweir             }
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     OSL_ENSURE( false, OStringBuffer( "ConversionHelper::decodeColor - invalid VML color name '" ).
263cdf0e10cSrcweir         append( OUStringToOString( roVmlColor.get(), RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
264cdf0e10cSrcweir     aDmlColor.setSrgbClr( nDefaultRgb );
265cdf0e10cSrcweir     return aDmlColor;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir // ============================================================================
269cdf0e10cSrcweir 
270cdf0e10cSrcweir namespace {
271cdf0e10cSrcweir 
lclGetEmu(const GraphicHelper & rGraphicHelper,const OptValue<OUString> & roValue,sal_Int64 nDefValue)272cdf0e10cSrcweir sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const OptValue< OUString >& roValue, sal_Int64 nDefValue )
273cdf0e10cSrcweir {
274cdf0e10cSrcweir     return roValue.has() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.get(), 0, false, false ) : nDefValue;
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
lclGetDmlLineDash(OptValue<sal_Int32> & oroPresetDash,LineProperties::DashStopVector & orCustomDash,const OptValue<OUString> & roDashStyle)277cdf0e10cSrcweir void lclGetDmlLineDash( OptValue< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const OptValue< OUString >& roDashStyle )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir     if( roDashStyle.has() )
280cdf0e10cSrcweir     {
281cdf0e10cSrcweir         const OUString& rDashStyle = roDashStyle.get();
282cdf0e10cSrcweir         switch( AttributeConversion::decodeToken( rDashStyle ) )
283cdf0e10cSrcweir         {
284cdf0e10cSrcweir             case XML_solid:             oroPresetDash = XML_solid;          return;
285cdf0e10cSrcweir             case XML_shortdot:          oroPresetDash = XML_sysDot;         return;
286cdf0e10cSrcweir             case XML_shortdash:         oroPresetDash = XML_sysDash;        return;
287cdf0e10cSrcweir             case XML_shortdashdot:      oroPresetDash = XML_sysDashDot;     return;
288cdf0e10cSrcweir             case XML_shortdashdotdot:   oroPresetDash = XML_sysDashDotDot;  return;
289cdf0e10cSrcweir             case XML_dot:               oroPresetDash = XML_dot;            return;
290cdf0e10cSrcweir             case XML_dash:              oroPresetDash = XML_dash;           return;
291cdf0e10cSrcweir             case XML_dashdot:           oroPresetDash = XML_dashDot;        return;
292cdf0e10cSrcweir             case XML_longdash:          oroPresetDash = XML_lgDash;         return;
293cdf0e10cSrcweir             case XML_longdashdot:       oroPresetDash = XML_lgDashDot;      return;
294cdf0e10cSrcweir             case XML_longdashdotdot:    oroPresetDash = XML_lgDashDotDot;   return;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir             // try to convert user-defined dash style
297cdf0e10cSrcweir             default:
298cdf0e10cSrcweir             {
299cdf0e10cSrcweir                 ::std::vector< sal_Int32 > aValues;
300cdf0e10cSrcweir                 sal_Int32 nIndex = 0;
301cdf0e10cSrcweir                 while( nIndex >= 0 )
302cdf0e10cSrcweir                     aValues.push_back( rDashStyle.getToken( 0, ' ', nIndex ).toInt32() );
303cdf0e10cSrcweir                 size_t nPairs = aValues.size() / 2; // ignore last value if size is odd
304cdf0e10cSrcweir                 for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx )
305cdf0e10cSrcweir                     orCustomDash.push_back( LineProperties::DashStop( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] ) );
306cdf0e10cSrcweir             }
307cdf0e10cSrcweir         }
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir }
310cdf0e10cSrcweir 
lclGetDmlArrowType(const OptValue<sal_Int32> & roArrowType)311cdf0e10cSrcweir sal_Int32 lclGetDmlArrowType( const OptValue< sal_Int32 >& roArrowType )
312cdf0e10cSrcweir {
313cdf0e10cSrcweir     if( roArrowType.has() ) switch( roArrowType.get() )
314cdf0e10cSrcweir     {
315cdf0e10cSrcweir         case XML_none:      return XML_none;
316cdf0e10cSrcweir         case XML_block:     return XML_triangle;
317cdf0e10cSrcweir         case XML_classic:   return XML_stealth;
318cdf0e10cSrcweir         case XML_diamond:   return XML_diamond;
319cdf0e10cSrcweir         case XML_oval:      return XML_oval;
320cdf0e10cSrcweir         case XML_open:      return XML_arrow;
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir     return XML_none;
323cdf0e10cSrcweir }
324cdf0e10cSrcweir 
lclGetDmlArrowWidth(const OptValue<sal_Int32> & roArrowWidth)325cdf0e10cSrcweir sal_Int32 lclGetDmlArrowWidth( const OptValue< sal_Int32 >& roArrowWidth )
326cdf0e10cSrcweir {
327cdf0e10cSrcweir     if( roArrowWidth.has() ) switch( roArrowWidth.get() )
328cdf0e10cSrcweir     {
329cdf0e10cSrcweir         case XML_narrow:    return XML_sm;
330cdf0e10cSrcweir         case XML_medium:    return XML_med;
331cdf0e10cSrcweir         case XML_wide:      return XML_lg;
332cdf0e10cSrcweir     }
333cdf0e10cSrcweir     return XML_med;
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
lclGetDmlArrowLength(const OptValue<sal_Int32> & roArrowLength)336cdf0e10cSrcweir sal_Int32 lclGetDmlArrowLength( const OptValue< sal_Int32 >& roArrowLength )
337cdf0e10cSrcweir {
338cdf0e10cSrcweir     if( roArrowLength.has() ) switch( roArrowLength.get() )
339cdf0e10cSrcweir     {
340cdf0e10cSrcweir         case XML_short:     return XML_sm;
341cdf0e10cSrcweir         case XML_medium:    return XML_med;
342cdf0e10cSrcweir         case XML_long:      return XML_lg;
343cdf0e10cSrcweir     }
344cdf0e10cSrcweir     return XML_med;
345cdf0e10cSrcweir }
346cdf0e10cSrcweir 
lclConvertArrow(LineArrowProperties & orArrowProp,const StrokeArrowModel & rStrokeArrow)347cdf0e10cSrcweir void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow )
348cdf0e10cSrcweir {
349cdf0e10cSrcweir     orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType );
350cdf0e10cSrcweir     orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth );
351cdf0e10cSrcweir     orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength );
352cdf0e10cSrcweir }
353cdf0e10cSrcweir 
lclGetDmlLineCompound(const OptValue<sal_Int32> & roLineStyle)354cdf0e10cSrcweir sal_Int32 lclGetDmlLineCompound( const OptValue< sal_Int32 >& roLineStyle )
355cdf0e10cSrcweir {
356cdf0e10cSrcweir     if( roLineStyle.has() ) switch( roLineStyle.get() )
357cdf0e10cSrcweir     {
358cdf0e10cSrcweir         case XML_single:            return XML_sng;
359cdf0e10cSrcweir         case XML_thinThin:          return XML_dbl;
360cdf0e10cSrcweir         case XML_thinThick:         return XML_thinThick;
361cdf0e10cSrcweir         case XML_thickThin:         return XML_thickThin;
362cdf0e10cSrcweir         case XML_thickBetweenThin:  return XML_tri;
363cdf0e10cSrcweir     }
364cdf0e10cSrcweir     return XML_sng;
365cdf0e10cSrcweir }
366cdf0e10cSrcweir 
lclGetDmlLineCap(const OptValue<sal_Int32> & roEndCap)367cdf0e10cSrcweir sal_Int32 lclGetDmlLineCap( const OptValue< sal_Int32 >& roEndCap )
368cdf0e10cSrcweir {
369cdf0e10cSrcweir     if( roEndCap.has() ) switch( roEndCap.get() )
370cdf0e10cSrcweir     {
371cdf0e10cSrcweir         case XML_flat:      return XML_flat;
372cdf0e10cSrcweir         case XML_square:    return XML_sq;
373cdf0e10cSrcweir         case XML_round:     return XML_rnd;
374cdf0e10cSrcweir     }
375cdf0e10cSrcweir     return XML_flat;    // different defaults in VML (flat) and DrawingML (square)
376cdf0e10cSrcweir }
377cdf0e10cSrcweir 
lclGetDmlLineJoint(const OptValue<sal_Int32> & roJoinStyle)378cdf0e10cSrcweir sal_Int32 lclGetDmlLineJoint( const OptValue< sal_Int32 >& roJoinStyle )
379cdf0e10cSrcweir {
380cdf0e10cSrcweir     if( roJoinStyle.has() ) switch( roJoinStyle.get() )
381cdf0e10cSrcweir     {
382cdf0e10cSrcweir         case XML_round: return XML_round;
383cdf0e10cSrcweir         case XML_bevel: return XML_bevel;
384cdf0e10cSrcweir         case XML_miter: return XML_miter;
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir     return XML_round;
387cdf0e10cSrcweir }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir } // namespace
390cdf0e10cSrcweir 
391cdf0e10cSrcweir // ============================================================================
392cdf0e10cSrcweir 
assignUsed(const StrokeArrowModel & rSource)393cdf0e10cSrcweir void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource )
394cdf0e10cSrcweir {
395cdf0e10cSrcweir     moArrowType.assignIfUsed( rSource.moArrowType );
396cdf0e10cSrcweir     moArrowWidth.assignIfUsed( rSource.moArrowWidth );
397cdf0e10cSrcweir     moArrowLength.assignIfUsed( rSource.moArrowLength );
398cdf0e10cSrcweir }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir // ============================================================================
401cdf0e10cSrcweir 
assignUsed(const StrokeModel & rSource)402cdf0e10cSrcweir void StrokeModel::assignUsed( const StrokeModel& rSource )
403cdf0e10cSrcweir {
404cdf0e10cSrcweir     moStroked.assignIfUsed( rSource.moStroked );
405cdf0e10cSrcweir     maStartArrow.assignUsed( rSource.maStartArrow );
406cdf0e10cSrcweir     maEndArrow.assignUsed( rSource.maEndArrow );
407cdf0e10cSrcweir     moColor.assignIfUsed( rSource.moColor );
408cdf0e10cSrcweir     moOpacity.assignIfUsed( rSource.moOpacity );
409cdf0e10cSrcweir     moWeight.assignIfUsed( rSource.moWeight );
410cdf0e10cSrcweir     moDashStyle.assignIfUsed( rSource.moDashStyle );
411cdf0e10cSrcweir     moLineStyle.assignIfUsed( rSource.moLineStyle );
412cdf0e10cSrcweir     moEndCap.assignIfUsed( rSource.moEndCap );
413cdf0e10cSrcweir     moJoinStyle.assignIfUsed( rSource.moJoinStyle );
414cdf0e10cSrcweir }
415cdf0e10cSrcweir 
pushToPropMap(ShapePropertyMap & rPropMap,const GraphicHelper & rGraphicHelper) const416cdf0e10cSrcweir void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
417cdf0e10cSrcweir {
418cdf0e10cSrcweir     /*  Convert VML line formatting to DrawingML line formatting and let the
419cdf0e10cSrcweir         DrawingML code do the hard work. */
420cdf0e10cSrcweir     LineProperties aLineProps;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir     if( moStroked.get( true ) )
423cdf0e10cSrcweir     {
424cdf0e10cSrcweir         aLineProps.maLineFill.moFillType = XML_solidFill;
425cdf0e10cSrcweir         lclConvertArrow( aLineProps.maStartArrow, maStartArrow );
426cdf0e10cSrcweir         lclConvertArrow( aLineProps.maEndArrow, maEndArrow );
427cdf0e10cSrcweir         aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK );
428cdf0e10cSrcweir         aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 );
429cdf0e10cSrcweir         lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle );
430cdf0e10cSrcweir         aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle );
431cdf0e10cSrcweir         aLineProps.moLineCap = lclGetDmlLineCap( moEndCap );
432cdf0e10cSrcweir         aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle );
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir     else
435cdf0e10cSrcweir     {
436cdf0e10cSrcweir         aLineProps.maLineFill.moFillType = XML_noFill;
437cdf0e10cSrcweir     }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     aLineProps.pushToPropMap( rPropMap, rGraphicHelper );
440cdf0e10cSrcweir }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir // ============================================================================
443cdf0e10cSrcweir 
assignUsed(const FillModel & rSource)444cdf0e10cSrcweir void FillModel::assignUsed( const FillModel& rSource )
445cdf0e10cSrcweir {
446cdf0e10cSrcweir     moFilled.assignIfUsed( rSource.moFilled );
447cdf0e10cSrcweir     moColor.assignIfUsed( rSource.moColor );
448cdf0e10cSrcweir     moOpacity.assignIfUsed( rSource.moOpacity );
449cdf0e10cSrcweir     moColor2.assignIfUsed( rSource.moColor2 );
450cdf0e10cSrcweir     moOpacity2.assignIfUsed( rSource.moOpacity2 );
451cdf0e10cSrcweir     moType.assignIfUsed( rSource.moType );
452cdf0e10cSrcweir     moAngle.assignIfUsed( rSource.moAngle );
453cdf0e10cSrcweir     moFocus.assignIfUsed( rSource.moFocus );
454cdf0e10cSrcweir     moFocusPos.assignIfUsed( rSource.moFocusPos );
455cdf0e10cSrcweir     moFocusSize.assignIfUsed( rSource.moFocusSize );
456cdf0e10cSrcweir     moBitmapPath.assignIfUsed( rSource.moBitmapPath );
457cdf0e10cSrcweir     moRotate.assignIfUsed( rSource.moRotate );
458cdf0e10cSrcweir }
459cdf0e10cSrcweir 
pushToPropMap(ShapePropertyMap & rPropMap,const GraphicHelper & rGraphicHelper) const460cdf0e10cSrcweir void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
461cdf0e10cSrcweir {
462cdf0e10cSrcweir     /*  Convert VML fill formatting to DrawingML fill formatting and let the
463cdf0e10cSrcweir         DrawingML code do the hard work. */
464cdf0e10cSrcweir     FillProperties aFillProps;
465cdf0e10cSrcweir 
466cdf0e10cSrcweir     if( moFilled.get( true ) )
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir         sal_Int32 nFillType = moType.get( XML_solid );
469cdf0e10cSrcweir         switch( nFillType )
470cdf0e10cSrcweir         {
471cdf0e10cSrcweir             case XML_gradient:
472cdf0e10cSrcweir             case XML_gradientRadial:
473cdf0e10cSrcweir             {
474cdf0e10cSrcweir                 aFillProps.moFillType = XML_gradFill;
475cdf0e10cSrcweir                 aFillProps.maGradientProps.moRotateWithShape = moRotate.get( false );
476cdf0e10cSrcweir                 double fFocus = moFocus.get( 0.0 );
477cdf0e10cSrcweir 
478cdf0e10cSrcweir                 // prepare colors
479cdf0e10cSrcweir                 Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
480cdf0e10cSrcweir                 Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) );
481cdf0e10cSrcweir 
482cdf0e10cSrcweir                 // type XML_gradient is linear or axial gradient
483cdf0e10cSrcweir                 if( nFillType == XML_gradient )
484cdf0e10cSrcweir                 {
485cdf0e10cSrcweir                     // normalize angle to range [0;360) degrees
486cdf0e10cSrcweir                     sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.get( 0 ), 0, 360 );
487cdf0e10cSrcweir 
488cdf0e10cSrcweir                     // focus of -50% or 50% is axial gradient
489cdf0e10cSrcweir                     if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) )
490cdf0e10cSrcweir                     {
491cdf0e10cSrcweir                         /*  According to spec, focus of 50% is outer-to-inner,
492cdf0e10cSrcweir                             and -50% is inner-to-outer (color to color2).
493cdf0e10cSrcweir                             BUT: For angles >= 180 deg., the behaviour is
494cdf0e10cSrcweir                             reversed... that's not spec'ed of course. So,
495cdf0e10cSrcweir                             [0;180) deg. and 50%, or [180;360) deg. and -50% is
496cdf0e10cSrcweir                             outer-to-inner in fact. */
497cdf0e10cSrcweir                         bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180);
498cdf0e10cSrcweir                         // simulate axial gradient by 3-step DrawingML gradient
499cdf0e10cSrcweir                         const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2;
500cdf0e10cSrcweir                         const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1;
501cdf0e10cSrcweir                         aFillProps.maGradientProps.maGradientStops[ 0.0 ] = aFillProps.maGradientProps.maGradientStops[ 1.0 ] = rOuterColor;
502cdf0e10cSrcweir                         aFillProps.maGradientProps.maGradientStops[ 0.5 ] = rInnerColor;
503cdf0e10cSrcweir                     }
504cdf0e10cSrcweir                     else    // focus of -100%, 0%, and 100% is linear gradient
505cdf0e10cSrcweir                     {
506cdf0e10cSrcweir                         /*  According to spec, focus of -100% or 100% swaps the
507cdf0e10cSrcweir                             start and stop colors, effectively reversing the
508cdf0e10cSrcweir                             gradient. BUT: For angles >= 180 deg., the
509cdf0e10cSrcweir                             behaviour is reversed. This means that in this case
510cdf0e10cSrcweir                             a focus of 0% swaps the gradient. */
511cdf0e10cSrcweir                         if( ((fFocus < -0.75) || (fFocus > 0.75)) == (nVmlAngle < 180) )
512cdf0e10cSrcweir                             (nVmlAngle += 180) %= 360;
513cdf0e10cSrcweir                         // set the start and stop colors
514cdf0e10cSrcweir                         aFillProps.maGradientProps.maGradientStops[ 0.0 ] = aColor1;
515cdf0e10cSrcweir                         aFillProps.maGradientProps.maGradientStops[ 1.0 ] = aColor2;
516cdf0e10cSrcweir                     }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir                     // VML counts counterclockwise from bottom, DrawingML clockwise from left
519cdf0e10cSrcweir                     sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360;
520cdf0e10cSrcweir                     aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE;
521cdf0e10cSrcweir                 }
522cdf0e10cSrcweir                 else    // XML_gradientRadial is rectangular gradient
523cdf0e10cSrcweir                 {
524cdf0e10cSrcweir                     aFillProps.maGradientProps.moGradientPath = XML_rect;
525cdf0e10cSrcweir                     // convert VML focus position and size to DrawingML fill-to-rect
526cdf0e10cSrcweir                     DoublePair aFocusPos = moFocusPos.get( DoublePair( 0.0, 0.0 ) );
527cdf0e10cSrcweir                     DoublePair aFocusSize = moFocusSize.get( DoublePair( 0.0, 0.0 ) );
528cdf0e10cSrcweir                     double fLeft   = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 );
529cdf0e10cSrcweir                     double fTop    = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 );
530cdf0e10cSrcweir                     double fRight  = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 );
531cdf0e10cSrcweir                     double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 );
532cdf0e10cSrcweir                     aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D(
533cdf0e10cSrcweir                         static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ),
534cdf0e10cSrcweir                         static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ),
535cdf0e10cSrcweir                         static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ),
536cdf0e10cSrcweir                         static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) );
537cdf0e10cSrcweir 
538cdf0e10cSrcweir                     // set the start and stop colors (focus of 0% means outer-to-inner)
539cdf0e10cSrcweir                     bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5);
540cdf0e10cSrcweir                     aFillProps.maGradientProps.maGradientStops[ 0.0 ] = bOuterToInner ? aColor2 : aColor1;
541cdf0e10cSrcweir                     aFillProps.maGradientProps.maGradientStops[ 1.0 ] = bOuterToInner ? aColor1 : aColor2;
542cdf0e10cSrcweir                 }
543cdf0e10cSrcweir             }
544cdf0e10cSrcweir             break;
545cdf0e10cSrcweir 
546cdf0e10cSrcweir             case XML_pattern:
547cdf0e10cSrcweir             case XML_tile:
548cdf0e10cSrcweir             case XML_frame:
549cdf0e10cSrcweir             {
550cdf0e10cSrcweir                 if( moBitmapPath.has() && moBitmapPath.get().getLength() > 0 )
551cdf0e10cSrcweir                 {
552cdf0e10cSrcweir                     aFillProps.maBlipProps.mxGraphic = rGraphicHelper.importEmbeddedGraphic( moBitmapPath.get() );
553cdf0e10cSrcweir                     if( aFillProps.maBlipProps.mxGraphic.is() )
554cdf0e10cSrcweir                     {
555cdf0e10cSrcweir                         aFillProps.moFillType = XML_blipFill;
556cdf0e10cSrcweir                         aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile;
557cdf0e10cSrcweir                         break;  // do not break if bitmap is missing, but run to XML_solid instead
558cdf0e10cSrcweir                     }
559cdf0e10cSrcweir                 }
560cdf0e10cSrcweir             }
561cdf0e10cSrcweir             // run-through to XML_solid in case of missing bitmap path intended!
562cdf0e10cSrcweir 
563cdf0e10cSrcweir             case XML_solid:
564cdf0e10cSrcweir             default:
565cdf0e10cSrcweir             {
566cdf0e10cSrcweir                 aFillProps.moFillType = XML_solidFill;
567cdf0e10cSrcweir                 // fill color (default is white)
568cdf0e10cSrcweir                 aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
569cdf0e10cSrcweir             }
570cdf0e10cSrcweir         }
571cdf0e10cSrcweir     }
572cdf0e10cSrcweir     else
573cdf0e10cSrcweir     {
574cdf0e10cSrcweir         aFillProps.moFillType = XML_noFill;
575cdf0e10cSrcweir     }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir     aFillProps.pushToPropMap( rPropMap, rGraphicHelper );
578cdf0e10cSrcweir }
579cdf0e10cSrcweir 
580cdf0e10cSrcweir // ============================================================================
581cdf0e10cSrcweir 
582cdf0e10cSrcweir } // namespace vml
583cdf0e10cSrcweir } // namespace oox
584