diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /oox/source/vml | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | oox/source/vml/vmldrawing.cxx | 345 | ||||
-rw-r--r-- | oox/source/vml/vmldrawingfragment.cxx | 82 | ||||
-rw-r--r-- | oox/source/vml/vmlformatting.cxx | 986 | ||||
-rw-r--r-- | oox/source/vml/vmlinputstream.cxx | 395 | ||||
-rw-r--r-- | oox/source/vml/vmlshape.cxx | 1454 | ||||
-rw-r--r-- | oox/source/vml/vmlshapecontainer.cxx | 138 | ||||
-rw-r--r-- | oox/source/vml/vmlshapecontext.cxx | 632 | ||||
-rw-r--r-- | oox/source/vml/vmltextbox.cxx | 204 | ||||
-rw-r--r-- | oox/source/vml/vmltextboxcontext.cxx | 288 |
9 files changed, 4524 insertions, 0 deletions
diff --git a/oox/source/vml/vmldrawing.cxx b/oox/source/vml/vmldrawing.cxx new file mode 100644 index 000000000..cc24d1f10 --- /dev/null +++ b/oox/source/vml/vmldrawing.cxx @@ -0,0 +1,345 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/vml/vmldrawing.hxx> + +#include <algorithm> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/drawing/XControlShape.hpp> +#include <com/sun/star/drawing/XDrawPage.hpp> +#include <com/sun/star/drawing/XShapes.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/text/HoriOrientation.hpp> +#include <com/sun/star/text/RelOrientation.hpp> +#include <com/sun/star/text/VertOrientation.hpp> +#include <osl/diagnose.h> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> +#include <oox/core/xmlfilterbase.hxx> +#include <oox/helper/containerhelper.hxx> +#include <oox/ole/axcontrol.hxx> +#include <oox/vml/vmlshape.hxx> +#include <oox/vml/vmlshapecontainer.hxx> +#include <tools/diagnose_ex.h> +#include <tools/gen.hxx> + +namespace oox::vml { + +using namespace ::com::sun::star; +using namespace ::com::sun::star::awt; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::drawing; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::text; +using namespace ::com::sun::star::uno; + +using ::oox::core::XmlFilterBase; + +namespace { + +/** Returns the textual representation of a numeric VML shape identifier. */ +OUString lclGetShapeId( sal_Int32 nShapeId ) +{ + // identifier consists of a literal NUL character, a lowercase 's', and the id + sal_Unicode const aStr[2] = { '\0', 's' }; + return OUString( aStr, 2 ) + OUString::number( nShapeId ); +} + +/** Returns the numeric VML shape identifier from its textual representation. */ +sal_Int32 lclGetShapeId( const OUString& rShapeId ) +{ + // identifier consists of a literal NUL character, a lowercase 's', and the id + return ((rShapeId.getLength() >= 3) && (rShapeId[ 0 ] == '\0') && (rShapeId[ 1 ] == 's')) ? rShapeId.copy( 2 ).toInt32() : -1; +} + +} // namespace + +OleObjectInfo::OleObjectInfo( bool bDmlShape ) : + mbAutoLoad( false ), + mbDmlShape( bDmlShape ) +{ +} + +void OleObjectInfo::setShapeId( sal_Int32 nShapeId ) +{ + maShapeId = lclGetShapeId( nShapeId ); +} + +ControlInfo::ControlInfo() + : mbTextContentShape(false) +{ +} + +void ControlInfo::setShapeId( sal_Int32 nShapeId ) +{ + maShapeId = lclGetShapeId(nShapeId); +} + +Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) : + mrFilter( rFilter ), + mxDrawPage( rxDrawPage ), + mxShapes( new ShapeContainer( *this ) ), + meType( eType ) +{ + OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" ); +} + +Drawing::~Drawing() +{ +} + +::oox::ole::EmbeddedForm& Drawing::getControlForm() const +{ + if (!mxCtrlForm) + mxCtrlForm.reset( new ::oox::ole::EmbeddedForm( + mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) ); + return *mxCtrlForm; +} + +void Drawing::registerBlockId( sal_Int32 nBlockId ) +{ + OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" ); + if( nBlockId > 0 ) + { + // lower_bound() returns iterator pointing to element equal to nBlockId, if existing + BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId ); + if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) ) + maBlockIds.insert( aIt, nBlockId ); + } +} + +void Drawing::registerOleObject( const OleObjectInfo& rOleObject ) +{ + OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" ); + OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" ); + maOleObjects.emplace( rOleObject.maShapeId, rOleObject ); +} + +void Drawing::registerControl( const ControlInfo& rControl ) +{ + OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" ); + OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" ); + OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" ); + maControls.emplace( rControl.maShapeId, rControl ); +} + +void Drawing::finalizeFragmentImport() +{ + mxShapes->finalizeFragmentImport(); +} + +void Drawing::convertAndInsert() const +{ + Reference< XShapes > xShapes( mxDrawPage ); + mxShapes->convertAndInsert( xShapes ); + + // Group together form control radio buttons that are in the same groupBox + std::map<OUString, tools::Rectangle> GroupBoxMap; + std::map<Reference< XPropertySet >, tools::Rectangle> RadioButtonMap; + for ( sal_Int32 i = 0; i < xShapes->getCount(); ++i ) + { + try + { + Reference< XControlShape > xCtrlShape( xShapes->getByIndex(i), UNO_QUERY ); + if (!xCtrlShape.is()) + continue; + Reference< XControlModel > xCtrlModel( xCtrlShape->getControl(), UNO_SET_THROW ); + Reference< XServiceInfo > xModelSI (xCtrlModel, UNO_QUERY_THROW ); + Reference< XPropertySet > aProps( xCtrlModel, UNO_QUERY_THROW ); + + OUString sName; + aProps->getPropertyValue("Name") >>= sName; + const ::Point aPoint( xCtrlShape->getPosition().X, xCtrlShape->getPosition().Y ); + const ::Size aSize( xCtrlShape->getSize().Width, xCtrlShape->getSize().Height ); + const tools::Rectangle aRect( aPoint, aSize ); + if ( !sName.isEmpty() + && xModelSI->supportsService("com.sun.star.awt.UnoControlGroupBoxModel") ) + { + GroupBoxMap[sName] = aRect; + } + else if ( xModelSI->supportsService("com.sun.star.awt.UnoControlRadioButtonModel") ) + { + OUString sGroupName; + aProps->getPropertyValue("GroupName") >>= sGroupName; + // only Form Controls are affected by Group Boxes - see drawingfragment.cxx + if ( sGroupName == "autoGroup_formControl" ) + RadioButtonMap[aProps] = aRect; + } + } + catch (uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("oox.vml"); + } + } + for ( const auto& BoxItr : GroupBoxMap ) + { + const uno::Any aGroup( OUString("autoGroup_").concat(BoxItr.first) ); + for ( auto RadioItr = RadioButtonMap.begin(); RadioItr != RadioButtonMap.end(); ) + { + if ( BoxItr.second.IsInside(RadioItr->second) ) + { + RadioItr->first->setPropertyValue("GroupName", aGroup ); + // If conflict, first created GroupBox wins + RadioButtonMap.erase( RadioItr++ ); + } + else + RadioItr++; + } + } + +} + +sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const +{ + sal_Int32 nShapeId = lclGetShapeId( rShapeId ); + if( nShapeId <= 0 ) return -1; + + /* Shapes in a drawing are counted per registered shape identifier blocks + as stored in the o:idmap element. The contents of this element have + been stored in our member maBlockIds. Each block represents 1024 shape + identifiers, starting with identifier 1 for the block #0. This means, + block #0 represents the identifiers 1-1024, block #1 represents the + identifiers 1025-2048, and so on. The local shape index has to be + calculated according to all blocks registered for this drawing. + + Example: + Registered for this drawing are blocks #1 and #3 (shape identifiers + 1025-2048 and 3073-4096). + Shape identifier 1025 -> local shape index 1. + Shape identifier 1026 -> local shape index 2. + ... + Shape identifier 2048 -> local shape index 1024. + Shape identifier 3073 -> local shape index 1025. + ... + Shape identifier 4096 -> local shape index 2048. + */ + + // get block id from shape id and find its index in the list of used blocks + sal_Int32 nBlockId = (nShapeId - 1) / 1024; + BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId ); + sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() ); + + // block id not found in set -> register it now (value of nIndex remains valid) + if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) ) + maBlockIds.insert( aIt, nBlockId ); + + // get one-based offset of shape id in its block + sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1; + + // calculate the local shape index + return 1024 * nIndex + nBlockOffset; +} + +const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const +{ + return ContainerHelper::getMapElement( maOleObjects, rShapeId ); +} + +const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const +{ + return ContainerHelper::getMapElement( maControls, rShapeId ); +} + +Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService, + const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" ); + OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" ); + Reference< XShape > xShape; + if( !rService.isEmpty() && rxShapes.is() ) try + { + Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW ); + xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW ); + if ( rService != "com.sun.star.text.TextFrame" ) + { + // insert shape into passed shape collection (maybe drawpage or group shape) + rxShapes->add( xShape ); + xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) ); + } + else + { + Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW ); + xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) ); + xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) ); + xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) ); + xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) ); + xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) ); + xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) ); + } + xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) ); + } + catch( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "oox", "Drawing::createAndInsertXShape - error during shape object creation" ); + } + OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instantiate shape object" ); + return xShape; +} + +Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl, + const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const +{ + Reference< XShape > xShape; + try + { + // create control model and insert it into the form of the draw page + Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW ); + + // create the control shape + xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect ); + + // set the control model at the shape + Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel ); + } + catch (Exception const&) + { + TOOLS_WARN_EXCEPTION("oox", "exception inserting Shape"); + } + return xShape; +} + +bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const +{ + return true; +} + +OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const +{ + return OUString(); +} + +bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const +{ + return false; +} + +Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/, + const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const +{ + return Reference< XShape >(); +} + +void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/, + const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ ) +{ +} + +} // namespace oox::vml + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmldrawingfragment.cxx b/oox/source/vml/vmldrawingfragment.cxx new file mode 100644 index 000000000..cb7936baf --- /dev/null +++ b/oox/source/vml/vmldrawingfragment.cxx @@ -0,0 +1,82 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/vml/vmldrawingfragment.hxx> + +#include <oox/core/xmlfilterbase.hxx> +#include <oox/token/namespaces.hxx> +#include <oox/token/tokens.hxx> +#include <oox/vml/vmldrawing.hxx> +#include <oox/vml/vmlinputstream.hxx> +#include <oox/vml/vmlshapecontext.hxx> + +namespace oox::vml { + +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::uno; +using namespace ::oox::core; + +DrawingFragment::DrawingFragment( XmlFilterBase& rFilter, const OUString& rFragmentPath, Drawing& rDrawing ) : + FragmentHandler2( rFilter, rFragmentPath, false ), // do not trim whitespace, has been preprocessed by the input stream + mrDrawing( rDrawing ) +{ +} + +Reference< XInputStream > DrawingFragment::openFragmentStream() const +{ + // #i104719# create an input stream that preprocesses the VML data + return new InputStream( getFilter().getComponentContext(), FragmentHandler2::openFragmentStream() ); +} + +ContextHandlerRef DrawingFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + switch( mrDrawing.getType() ) + { + // DOCX filter handles plain shape elements with this fragment handler + case VMLDRAWING_WORD: + if ( getNamespace( nElement ) == NMSP_vml + || nElement == W_TOKEN(control) ) // Control shape also defined as a vml shape + return ShapeContextBase::createShapeContext( *this, mrDrawing.getShapes(), nElement, rAttribs ); + break; + + // XLSX and PPTX filters load the entire VML fragment + case VMLDRAWING_EXCEL: + case VMLDRAWING_POWERPOINT: + switch( getCurrentElement() ) + { + case XML_ROOT_CONTEXT: + if( nElement == XML_xml ) return this; + break; + case XML_xml: + return ShapeContextBase::createShapeContext( *this, mrDrawing.getShapes(), nElement, rAttribs ); + } + break; + } + return nullptr; +} + +void DrawingFragment::finalizeImport() +{ + // resolve shape template references for all shapes + mrDrawing.finalizeFragmentImport(); +} + +} // namespace oox::vml + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmlformatting.cxx b/oox/source/vml/vmlformatting.cxx new file mode 100644 index 000000000..d89914e8c --- /dev/null +++ b/oox/source/vml/vmlformatting.cxx @@ -0,0 +1,986 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cstdlib> + +#include <oox/vml/vmlformatting.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/drawing/XShape.hpp> +#include <com/sun/star/drawing/EnhancedCustomShapeTextPathMode.hpp> +#include <com/sun/star/table/ShadowFormat.hpp> +#include <com/sun/star/text/XTextRange.hpp> +#include <rtl/strbuf.hxx> +#include <sal/log.hxx> +#include <osl/diagnose.h> +#include <oox/drawingml/color.hxx> +#include <oox/drawingml/drawingmltypes.hxx> +#include <drawingml/fillproperties.hxx> +#include <drawingml/lineproperties.hxx> +#include <oox/drawingml/shapepropertymap.hxx> +#include <oox/helper/attributelist.hxx> +#include <oox/helper/graphichelper.hxx> +#include <oox/token/properties.hxx> +#include <oox/token/tokens.hxx> +#include <svx/svdtrans.hxx> +#include <comphelper/propertysequence.hxx> +#include <vcl/virdev.hxx> + +namespace oox::vml { + +using namespace ::com::sun::star; +using namespace ::com::sun::star::geometry; + +using ::oox::drawingml::Color; +using ::oox::drawingml::FillProperties; +using ::oox::drawingml::LineArrowProperties; +using ::oox::drawingml::LineProperties; +using ::oox::drawingml::ShapePropertyMap; +using ::com::sun::star::awt::Point; +using ::com::sun::star::drawing::PolygonFlags; +using ::com::sun::star::drawing::PolygonFlags_NORMAL; +using ::com::sun::star::drawing::PolygonFlags_CONTROL; + +namespace { + +bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue ) +{ + // extract the double value and find start position of unit characters + rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok; + orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos ); + return eConvStatus == rtl_math_ConversionStatus_Ok; +} + +} // namespace + +bool ConversionHelper::separatePair( OUString& orValue1, OUString& orValue2, + const OUString& rValue, sal_Unicode cSep ) +{ + sal_Int32 nSepPos = rValue.indexOf( cSep ); + if( nSepPos >= 0 ) + { + orValue1 = rValue.copy( 0, nSepPos ).trim(); + orValue2 = rValue.copy( nSepPos + 1 ).trim(); + } + else + { + orValue1 = rValue.trim(); + } + return !orValue1.isEmpty() && !orValue2.isEmpty(); +} + +bool ConversionHelper::decodeBool( const OUString& rValue ) +{ + sal_Int32 nToken = AttributeConversion::decodeToken( rValue ); + // anything else than 't' or 'true' is considered to be false, as specified + return (nToken == XML_t) || (nToken == XML_true); +} + +double ConversionHelper::decodePercent( const OUString& rValue, double fDefValue ) +{ + if( rValue.isEmpty() ) + return fDefValue; + + double fValue = 0.0; + sal_Int32 nEndPos = 0; + if( !lclExtractDouble( fValue, nEndPos, rValue ) ) + return fDefValue; + + if( nEndPos == rValue.getLength() ) + return fValue; + + if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == '%') ) + return fValue / 100.0; + + if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == 'f') ) + return fValue / 65536.0; + + OSL_FAIL( "ConversionHelper::decodePercent - unknown measure unit" ); + return fDefValue; +} + +sal_Int32 ConversionHelper::decodeRotation( const OUString& rValue ) +{ + if( rValue.isEmpty() ) + return 0; + + double fValue = 0.0; + double fRotation = 0.0; + sal_Int32 nEndPos = 0; + if( !lclExtractDouble(fValue, nEndPos, rValue) ) + return 0; + + if( nEndPos == rValue.getLength() ) + fRotation = fValue; + else if( (nEndPos + 2 == rValue.getLength()) && (rValue[nEndPos] == 'f') && (rValue[nEndPos+1] == 'd') ) + fRotation = fValue / 65536.0; + else + { + OSL_FAIL("ConversionHelper::decodeRotation - unknown measure unit"); + return 0; + } + + return NormAngle36000(fRotation * -100); +} + +sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper, + const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel ) +{ + // default for missing values is 0 + if( rValue.isEmpty() ) + return 0; + + // TODO: according to spec, value may contain "auto" + if ( rValue == "auto" ) + { + OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" ); + return nRefValue; + } + + // extract the double value and find start position of unit characters + double fValue = 0.0; + sal_Int32 nEndPos = 0; + if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) ) + return 0; + + // process trailing unit, convert to EMU + OUString aUnit; + if( (0 < nEndPos) && (nEndPos < rValue.getLength()) ) + aUnit = rValue.copy( nEndPos ); + else if( bDefaultAsPixel ) + aUnit = "px"; + // else default is EMU + + if( aUnit.getLength() == 2 ) + { + sal_Unicode cChar1 = aUnit[ 0 ]; + sal_Unicode cChar2 = aUnit[ 1 ]; + if( (cChar1 == 'i') && (cChar2 == 'n') ) // 1 inch = 914,400 EMU + fValue *= 914400.0; + else if( (cChar1 == 'c') && (cChar2 == 'm') ) // 1 cm = 360,000 EMU + fValue *= 360000.0; + else if( (cChar1 == 'm') && (cChar2 == 'm') ) // 1 mm = 36,000 EMU + fValue *= 36000.0; + else if( (cChar1 == 'p') && (cChar2 == 't') ) // 1 point = 1/72 inch = 12,700 EMU + fValue *= 12700.0; + else if( (cChar1 == 'p') && (cChar2 == 'c') ) // 1 pica = 1/6 inch = 152,400 EMU + fValue *= 152400.0; + else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device + fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu( + bPixelX ? + rGraphicHelper.convertScreenPixelXToHmm( fValue ) : + rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) ); + } + else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') ) + { + fValue *= nRefValue / 100.0; + } + else if( bDefaultAsPixel || !aUnit.isEmpty() ) // default as EMU and no unit -> do nothing + { + OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - unknown measure unit" ); + fValue = nRefValue; + } + return static_cast< sal_Int64 >( fValue + 0.5 ); +} + +sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper, + const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel ) +{ + return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) ); +} + +Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper, + const OptValue< OUString >& roVmlColor, const OptValue< double >& roVmlOpacity, + ::Color nDefaultRgb, ::Color nPrimaryRgb ) +{ + Color aDmlColor; + + // convert opacity + const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT; + double fOpacity = roVmlOpacity.get( 1.0 ); + sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE ); + if( nOpacity < DML_FULL_OPAQUE ) + aDmlColor.addTransformation( XML_alpha, nOpacity ); + + // color attribute not present - set passed default color + if( !roVmlColor.has() ) + { + aDmlColor.setSrgbClr( nDefaultRgb ); + return aDmlColor; + } + + // separate leading color name or RGB value from following palette index + OUString aColorName, aColorIndex; + separatePair( aColorName, aColorIndex, roVmlColor.get(), ' ' ); + + // RGB colors in the format '#RRGGBB' + if( (aColorName.getLength() == 7) && (aColorName[ 0 ] == '#') ) + { + aDmlColor.setSrgbClr( aColorName.copy( 1 ).toUInt32( 16 ) ); + return aDmlColor; + } + + // RGB colors in the format '#RGB' + if( (aColorName.getLength() == 4) && (aColorName[ 0 ] == '#') ) + { + sal_Int32 nR = aColorName.copy( 1, 1 ).toUInt32( 16 ) * 0x11; + sal_Int32 nG = aColorName.copy( 2, 1 ).toUInt32( 16 ) * 0x11; + sal_Int32 nB = aColorName.copy( 3, 1 ).toUInt32( 16 ) * 0x11; + aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB ); + return aDmlColor; + } + + /* Predefined color names or system color names (resolve to RGB to detect + valid color name). */ + sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName ); + ::Color nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT ); + if( nRgbValue == API_RGB_TRANSPARENT ) + nRgbValue = rGraphicHelper.getSystemColor( nColorToken ); + if( nRgbValue != API_RGB_TRANSPARENT ) + { + aDmlColor.setSrgbClr( nRgbValue ); + return aDmlColor; + } + + // try palette colors enclosed in brackets + if( (aColorIndex.getLength() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.getLength() - 1 ] == ']') ) + { + aDmlColor.setPaletteClr( aColorIndex.copy( 1, aColorIndex.getLength() - 2 ).toInt32() ); + return aDmlColor; + } + + // try fill gradient modificator 'fill <modifier>(<amount>)' + if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) ) + { + sal_Int32 nOpenParen = aColorIndex.indexOf( '(' ); + sal_Int32 nCloseParen = aColorIndex.indexOf( ')' ); + if( (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.getLength()) ) + { + sal_Int32 nModToken = XML_TOKEN_INVALID; + switch( AttributeConversion::decodeToken( aColorIndex.copy( 0, nOpenParen ) ) ) + { + case XML_darken: nModToken = XML_shade;break; + case XML_lighten: nModToken = XML_tint; + } + sal_Int32 nValue = aColorIndex.copy( nOpenParen + 1, nCloseParen - nOpenParen - 1 ).toInt32(); + if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) ) + { + /* Simulate this modifier color by a color with related transformation. + The modifier amount has to be converted from the range [0;255] to + percentage [0;100000] used by DrawingML. */ + aDmlColor.setSrgbClr( nPrimaryRgb ); + aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) ); + return aDmlColor; + } + } + } + + OSL_FAIL( OStringBuffer( "lclGetColor - invalid VML color name '" ). + append( OUStringToOString( roVmlColor.get(), RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() ); + aDmlColor.setSrgbClr( nDefaultRgb ); + return aDmlColor; +} + +void ConversionHelper::decodeVmlPath( ::std::vector< ::std::vector< Point > >& rPointLists, ::std::vector< ::std::vector< PolygonFlags > >& rFlagLists, const OUString& rPath ) +{ + ::std::vector< sal_Int32 > aCoordList; + Point aCurrentPoint; + sal_Int32 nTokenStart = 0; + sal_Int32 nTokenLen = 0; + sal_Int32 nParamCount = 0; + bool bCommand = false; + enum VML_State { START, MOVE_REL, MOVE_ABS, BEZIER_REL, BEZIER_ABS, + LINE_REL, LINE_ABS, CLOSE, END, UNSUPPORTED }; + VML_State state = START; + + rPointLists.emplace_back( ); + rFlagLists.emplace_back( ); + + for ( sal_Int32 i = 0; i < rPath.getLength(); i++ ) + { + // Keep track of current integer token + if ( ( rPath[ i ] >= '0' && rPath[ i ] <= '9' ) || rPath[ i ] == '-' ) + nTokenLen++; + else if ( rPath[ i ] != ' ' ) + { + // Store coordinate from current token + if ( state != START && state != UNSUPPORTED ) + { + if ( nTokenLen > 0 ) + aCoordList.push_back( rPath.copy( nTokenStart, nTokenLen ).toInt32() ); + else + aCoordList.push_back( 0 ); + nTokenLen = 0; + } + + if (rPath[ i ] == ',' ) + { + nParamCount--; + } + + // Upon finding the next command code, deal with stored + // coordinates for previous command and reset parameters counter if needed. + // See http://www.w3.org/TR/NOTE-VML#_Toc416858382 for params count reference + if ( rPath[ i ] != ',' || nParamCount == 0 ) + { + switch ( state ) + { + case MOVE_REL: + aCoordList.resize(2, 0); // 2* params -> param count reset + if ( !rPointLists.empty() && !rPointLists.back().empty() ) + { + rPointLists.emplace_back( ); + rFlagLists.emplace_back( ); + } + rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] ); + rFlagLists.back().push_back( PolygonFlags_NORMAL ); + aCurrentPoint = rPointLists.back().back(); + nParamCount = 2; + break; + + case MOVE_ABS: + aCoordList.resize(2, 0); // 2 params -> no param count reset + if ( !rPointLists.empty() && !rPointLists.back().empty() ) + { + rPointLists.emplace_back( ); + rFlagLists.emplace_back( ); + } + rPointLists.back().emplace_back( (aCoordList[ 0 ]), aCoordList[ 1 ] ); + rFlagLists.back().push_back( PolygonFlags_NORMAL ); + aCurrentPoint = rPointLists.back().back(); + break; + + case BEZIER_REL: + aCoordList.resize(6, 0); // 6* params -> param count reset + rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ], + aCurrentPoint.Y + aCoordList[ 1 ] ); + rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 2 ], + aCurrentPoint.Y + aCoordList[ 3 ] ); + rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 4 ], + aCurrentPoint.Y + aCoordList[ 5 ] ); + rFlagLists.back().push_back( PolygonFlags_CONTROL ); + rFlagLists.back().push_back( PolygonFlags_CONTROL ); + rFlagLists.back().push_back( PolygonFlags_NORMAL ); + aCurrentPoint = rPointLists.back().back(); + nParamCount = 6; + break; + + case BEZIER_ABS: + aCoordList.resize(6, 0); // 6* params -> param count reset + rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] ); + rPointLists.back().emplace_back( aCoordList[ 2 ], aCoordList[ 3 ] ); + rPointLists.back().emplace_back( aCoordList[ 4 ], aCoordList[ 5 ] ); + rFlagLists.back().push_back( PolygonFlags_CONTROL ); + rFlagLists.back().push_back( PolygonFlags_CONTROL ); + rFlagLists.back().push_back( PolygonFlags_NORMAL ); + aCurrentPoint = rPointLists.back().back(); + nParamCount = 6; + break; + + case LINE_REL: + aCoordList.resize(2, 0); // 2* params -> param count reset + rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ], + aCurrentPoint.Y + aCoordList[ 1 ] ); + rFlagLists.back().push_back( PolygonFlags_NORMAL ); + aCurrentPoint = rPointLists.back().back(); + nParamCount = 2; + break; + + case LINE_ABS: + aCoordList.resize(2, 0); // 2* params -> param count reset + rPointLists.back().emplace_back( aCoordList[ 0 ], (aCoordList.size() > 1 ? aCoordList[ 1 ] : 0) ); + rFlagLists.back().push_back( PolygonFlags_NORMAL ); + aCurrentPoint = rPointLists.back().back(); + nParamCount = 2; + break; + + case CLOSE: // 0 param + SAL_WARN_IF(rPointLists.back().empty() || rFlagLists.back().empty(), "oox", "empty pointlists at close"); + if (!rPointLists.back().empty() && !rFlagLists.back().empty()) + { + rPointLists.back().push_back( rPointLists.back()[ 0 ] ); + rFlagLists.back().push_back( rFlagLists.back()[ 0 ] ); + aCurrentPoint = rPointLists.back().back(); + } + break; + + case END: // 0 param + rPointLists.emplace_back( ); + rFlagLists.emplace_back( ); + break; + + case START: + case UNSUPPORTED: + break; + } + + aCoordList.clear(); + } + + // Allow two-char commands to peek ahead to the next character + sal_Unicode nextChar = '\0'; + if (i+1 < rPath.getLength()) + nextChar = rPath[i+1]; + + // Move to relevant state upon finding a command + bCommand = true; + switch ( rPath[ i ] ) + { + // Single-character commands + case 't': // rmoveto + state = MOVE_REL; nParamCount = 2; break; + case 'm': // moveto + state = MOVE_ABS; nParamCount = 2; break; + case 'v': // rcurveto + state = BEZIER_REL; nParamCount = 6; break; + case 'c': // curveto + state = BEZIER_ABS; nParamCount = 6; break; + case 'r': // rlineto + state = LINE_REL; nParamCount = 2; break; + case 'l': // lineto + state = LINE_ABS; nParamCount = 2; break; + case 'x': // close + state = CLOSE; break; + case 'e': // end + state = END; break; + + // Two-character commands + case 'n': + { + switch ( nextChar ) + { + case 'f': // nf - nofill + case 's': // ns - nostroke + state = UNSUPPORTED; i++; break; + } + break; + } + case 'a': // Elliptical curves + { + switch ( nextChar ) + { + case 'e': // ae - angleellipseto + case 'l': // al - angleellipse + state = UNSUPPORTED; i++; break; + case 't': // at - arcto + case 'r': // ar - arc + state = UNSUPPORTED; i++; break; + } + break; + } + case 'w': // Clockwise elliptical arcs + { + switch ( nextChar ) + { + case 'a': // wa - clockwisearcto + case 'r': // wr - clockwisearc + state = UNSUPPORTED; i++; break; + } + break; + } + case 'q': + { + switch ( nextChar ) + { + case 'x': // qx - ellipticalquadrantx + case 'y': // qy - ellipticalquadranty + state = UNSUPPORTED; i++; break; + case 'b': // qb - quadraticbezier + state = UNSUPPORTED; i++; break; + } + break; + } + case 'h': // behaviour extensions + { + switch ( nextChar ) + { + case 'a': // ha - AutoLine + case 'b': // hb - AutoCurve + case 'c': // hc - CornerLine + case 'd': // hd - CornerCurve + case 'e': // he - SmoothLine + case 'f': // hf - SmoothCurve + case 'g': // hg - SymmetricLine + case 'h': // hh - SymmetricCurve + case 'i': // hi - Freeform + state = UNSUPPORTED; i++; break; + } + break; + } + default: + bCommand = false; + break; + } + + if (bCommand) nTokenLen = 0; + nTokenStart = i+1; + } + } +} + +namespace { + +sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const OptValue< OUString >& roValue, sal_Int64 nDefValue ) +{ + return roValue.has() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.get(), 0, false, false ) : nDefValue; +} + +void lclGetDmlLineDash( OptValue< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const OptValue< OUString >& roDashStyle ) +{ + if( !roDashStyle.has() ) + return; + + const OUString& rDashStyle = roDashStyle.get(); + switch( AttributeConversion::decodeToken( rDashStyle ) ) + { + case XML_solid: oroPresetDash = XML_solid; return; + case XML_shortdot: oroPresetDash = XML_sysDot; return; + case XML_shortdash: oroPresetDash = XML_sysDash; return; + case XML_shortdashdot: oroPresetDash = XML_sysDashDot; return; + case XML_shortdashdotdot: oroPresetDash = XML_sysDashDotDot; return; + case XML_dot: oroPresetDash = XML_dot; return; + case XML_dash: oroPresetDash = XML_dash; return; + case XML_dashdot: oroPresetDash = XML_dashDot; return; + case XML_longdash: oroPresetDash = XML_lgDash; return; + case XML_longdashdot: oroPresetDash = XML_lgDashDot; return; + case XML_longdashdotdot: oroPresetDash = XML_lgDashDotDot; return; + + // try to convert user-defined dash style + default: + { + ::std::vector< sal_Int32 > aValues; + sal_Int32 nIndex = 0; + while( nIndex >= 0 ) + aValues.push_back( rDashStyle.getToken( 0, ' ', nIndex ).toInt32() ); + size_t nPairs = aValues.size() / 2; // ignore last value if size is odd + for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx ) + orCustomDash.emplace_back( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] ); + } + } +} + +sal_Int32 lclGetDmlArrowType( const OptValue< sal_Int32 >& roArrowType ) +{ + if( roArrowType.has() ) switch( roArrowType.get() ) + { + case XML_none: return XML_none; + case XML_block: return XML_triangle; + case XML_classic: return XML_stealth; + case XML_diamond: return XML_diamond; + case XML_oval: return XML_oval; + case XML_open: return XML_arrow; + } + return XML_none; +} + +sal_Int32 lclGetDmlArrowWidth( const OptValue< sal_Int32 >& roArrowWidth ) +{ + if( roArrowWidth.has() ) switch( roArrowWidth.get() ) + { + case XML_narrow: return XML_sm; + case XML_medium: return XML_med; + case XML_wide: return XML_lg; + } + return XML_med; +} + +sal_Int32 lclGetDmlArrowLength( const OptValue< sal_Int32 >& roArrowLength ) +{ + if( roArrowLength.has() ) switch( roArrowLength.get() ) + { + case XML_short: return XML_sm; + case XML_medium: return XML_med; + case XML_long: return XML_lg; + } + return XML_med; +} + +void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow ) +{ + orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType ); + orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth ); + orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength ); +} + +sal_Int32 lclGetDmlLineCompound( const OptValue< sal_Int32 >& roLineStyle ) +{ + if( roLineStyle.has() ) switch( roLineStyle.get() ) + { + case XML_single: return XML_sng; + case XML_thinThin: return XML_dbl; + case XML_thinThick: return XML_thinThick; + case XML_thickThin: return XML_thickThin; + case XML_thickBetweenThin: return XML_tri; + } + return XML_sng; +} + +sal_Int32 lclGetDmlLineCap( const OptValue< sal_Int32 >& roEndCap ) +{ + if( roEndCap.has() ) switch( roEndCap.get() ) + { + case XML_flat: return XML_flat; + case XML_square: return XML_sq; + case XML_round: return XML_rnd; + } + return XML_flat; // different defaults in VML (flat) and DrawingML (square) +} + +sal_Int32 lclGetDmlLineJoint( const OptValue< sal_Int32 >& roJoinStyle ) +{ + if( roJoinStyle.has() ) switch( roJoinStyle.get() ) + { + case XML_round: return XML_round; + case XML_bevel: return XML_bevel; + case XML_miter: return XML_miter; + } + return XML_round; +} + +} // namespace + +void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource ) +{ + moArrowType.assignIfUsed( rSource.moArrowType ); + moArrowWidth.assignIfUsed( rSource.moArrowWidth ); + moArrowLength.assignIfUsed( rSource.moArrowLength ); +} + +void StrokeModel::assignUsed( const StrokeModel& rSource ) +{ + moStroked.assignIfUsed( rSource.moStroked ); + maStartArrow.assignUsed( rSource.maStartArrow ); + maEndArrow.assignUsed( rSource.maEndArrow ); + moColor.assignIfUsed( rSource.moColor ); + moOpacity.assignIfUsed( rSource.moOpacity ); + moWeight.assignIfUsed( rSource.moWeight ); + moDashStyle.assignIfUsed( rSource.moDashStyle ); + moLineStyle.assignIfUsed( rSource.moLineStyle ); + moEndCap.assignIfUsed( rSource.moEndCap ); + moJoinStyle.assignIfUsed( rSource.moJoinStyle ); +} + +void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const +{ + /* Convert VML line formatting to DrawingML line formatting and let the + DrawingML code do the hard work. */ + LineProperties aLineProps; + + if( moStroked.get( true ) ) + { + aLineProps.maLineFill.moFillType = XML_solidFill; + lclConvertArrow( aLineProps.maStartArrow, maStartArrow ); + lclConvertArrow( aLineProps.maEndArrow, maEndArrow ); + aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK ); + aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 ); + lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle ); + aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle ); + aLineProps.moLineCap = lclGetDmlLineCap( moEndCap ); + aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle ); + } + else + { + aLineProps.maLineFill.moFillType = XML_noFill; + } + + aLineProps.pushToPropMap( rPropMap, rGraphicHelper ); +} + +void FillModel::assignUsed( const FillModel& rSource ) +{ + moFilled.assignIfUsed( rSource.moFilled ); + moColor.assignIfUsed( rSource.moColor ); + moOpacity.assignIfUsed( rSource.moOpacity ); + moColor2.assignIfUsed( rSource.moColor2 ); + moOpacity2.assignIfUsed( rSource.moOpacity2 ); + moType.assignIfUsed( rSource.moType ); + moAngle.assignIfUsed( rSource.moAngle ); + moFocus.assignIfUsed( rSource.moFocus ); + moFocusPos.assignIfUsed( rSource.moFocusPos ); + moFocusSize.assignIfUsed( rSource.moFocusSize ); + moBitmapPath.assignIfUsed( rSource.moBitmapPath ); + moRotate.assignIfUsed( rSource.moRotate ); +} + +static void lcl_setGradientStop( std::multimap< double, Color >& rMap, const double fKey, const Color& rValue ) { + auto aElement = rMap.find( fKey ); + + if (aElement != rMap.end()) + aElement->second = rValue; + else + rMap.emplace( fKey, rValue ); +} + +void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const +{ + /* Convert VML fill formatting to DrawingML fill formatting and let the + DrawingML code do the hard work. */ + FillProperties aFillProps; + + if( moFilled.get( true ) ) + { + sal_Int32 nFillType = moType.get( XML_solid ); + switch( nFillType ) + { + case XML_gradient: + case XML_gradientRadial: + { + aFillProps.moFillType = XML_gradFill; + aFillProps.maGradientProps.moRotateWithShape = moRotate.get( false ); + double fFocus = moFocus.get( 0.0 ); + + // prepare colors + Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE ); + Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) ); + + // type XML_gradient is linear or axial gradient + if( nFillType == XML_gradient ) + { + // normalize angle to range [0;360) degrees + sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.get( 0 ), 0, 360 ); + + // focus of -50% or 50% is axial gradient + if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) ) + { + /* According to spec, focus of 50% is outer-to-inner, + and -50% is inner-to-outer (color to color2). + BUT: For angles >= 180 deg., the behaviour is + reversed... that's not spec'ed of course. So, + [0;180) deg. and 50%, or [180;360) deg. and -50% is + outer-to-inner in fact. */ + bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180); + // simulate axial gradient by 3-step DrawingML gradient + const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2; + const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1; + + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, rOuterColor); + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, rOuterColor); + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.5, rInnerColor ); + } + else // focus of -100%, 0%, and 100% is linear gradient + { + /* According to spec, focus of -100% or 100% swaps the + start and stop colors, effectively reversing the + gradient. BUT: For angles >= 180 deg., the + behaviour is reversed. This means that in this case + a focus of 0% swaps the gradient. */ + if( fFocus < -0.5 || fFocus > 0.5 ) + nVmlAngle = (nVmlAngle + 180) % 360; + // set the start and stop colors + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, aColor1 ); + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, aColor2 ); + } + + // VML counts counterclockwise from bottom, DrawingML clockwise from left + sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360; + aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE; + } + else // XML_gradientRadial is rectangular gradient + { + aFillProps.maGradientProps.moGradientPath = XML_rect; + // convert VML focus position and size to DrawingML fill-to-rect + DoublePair aFocusPos = moFocusPos.get( DoublePair( 0.0, 0.0 ) ); + DoublePair aFocusSize = moFocusSize.get( DoublePair( 0.0, 0.0 ) ); + double fLeft = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 ); + double fTop = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 ); + double fRight = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 ); + double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 ); + aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D( + static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ), + static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ), + static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ), + static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) ); + + // set the start and stop colors (focus of 0% means outer-to-inner) + bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5); + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, bOuterToInner ? aColor2 : aColor1 ); + lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, bOuterToInner ? aColor1 : aColor2 ); + } + } + break; + + case XML_pattern: + case XML_tile: + case XML_frame: + { + if( moBitmapPath.has() && !moBitmapPath.get().isEmpty() ) + { + aFillProps.maBlipProps.mxFillGraphic = rGraphicHelper.importEmbeddedGraphic(moBitmapPath.get()); + if (aFillProps.maBlipProps.mxFillGraphic.is()) + { + aFillProps.moFillType = XML_blipFill; + aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile; + break; // do not break if bitmap is missing, but run to XML_solid instead + } + } + } + [[fallthrough]]; // to XML_solid in case of missing bitmap path intended! + + case XML_solid: + default: + { + aFillProps.moFillType = XML_solidFill; + // fill color (default is white) + aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE ); + } + } + } + else + { + aFillProps.moFillType = XML_noFill; + } + + aFillProps.pushToPropMap( rPropMap, rGraphicHelper ); +} + +ShadowModel::ShadowModel() + : mbHasShadow(false) +{ +} + +void ShadowModel::pushToPropMap(ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper) const +{ + if (!mbHasShadow || (moShadowOn.has() && !moShadowOn.get())) + return; + + drawingml::Color aColor = ConversionHelper::decodeColor(rGraphicHelper, moColor, moOpacity, API_RGB_GRAY); + // nOffset* is in mm100, default value is 35 twips, see DffPropertyReader::ApplyAttributes() in msfilter. + sal_Int32 nOffsetX = 62, nOffsetY = 62; + if (moOffset.has()) + { + OUString aOffsetX, aOffsetY; + ConversionHelper::separatePair(aOffsetX, aOffsetY, moOffset.get(), ','); + if (!aOffsetX.isEmpty()) + nOffsetX = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetX, 0, false, false ); + if (!aOffsetY.isEmpty()) + nOffsetY = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetY, 0, false, false ); + } + + table::ShadowFormat aFormat; + aFormat.Color = sal_Int32(aColor.getColor(rGraphicHelper)); + aFormat.Location = nOffsetX < 0 + ? nOffsetY < 0 ? table::ShadowLocation_TOP_LEFT : table::ShadowLocation_BOTTOM_LEFT + : nOffsetY < 0 ? table::ShadowLocation_TOP_RIGHT : table::ShadowLocation_BOTTOM_RIGHT; + // The width of the shadow is the average of the x and y values, see SwWW8ImplReader::MatchSdrItemsIntoFlySet(). + aFormat.ShadowWidth = ((std::abs(nOffsetX) + std::abs(nOffsetY)) / 2); + rPropMap.setProperty(PROP_ShadowFormat, aFormat); +} + +TextpathModel::TextpathModel() +{ +} + +static beans::PropertyValue lcl_createTextpathProps() +{ + uno::Sequence<beans::PropertyValue> aTextpathPropSeq( comphelper::InitPropertySequence({ + { "TextPath", uno::Any(true) }, + { "TextPathMode", uno::Any(drawing::EnhancedCustomShapeTextPathMode_SHAPE) }, + { "ScaleX", uno::Any(false) }, + { "SameLetterHeights", uno::Any(false) } + })); + + beans::PropertyValue aRet; + aRet.Name = "TextPath"; + aRet.Value <<= aTextpathPropSeq; + return aRet; +} + +void TextpathModel::pushToPropMap(ShapePropertyMap& rPropMap, const uno::Reference<drawing::XShape>& xShape, const GraphicHelper& rGraphicHelper) const +{ + OUString sFont = ""; + + if (moString.has()) + { + uno::Reference<text::XTextRange> xTextRange(xShape, uno::UNO_QUERY); + xTextRange->setString(moString.get()); + + uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aGeomPropSeq = xPropertySet->getPropertyValue("CustomShapeGeometry").get< uno::Sequence<beans::PropertyValue> >(); + bool bFound = false; + for (beans::PropertyValue& rProp : aGeomPropSeq) + { + if (rProp.Name == "TextPath") + { + bFound = true; + rProp = lcl_createTextpathProps(); + } + } + if (!bFound) + { + sal_Int32 nSize = aGeomPropSeq.getLength(); + aGeomPropSeq.realloc(nSize+1); + aGeomPropSeq[nSize] = lcl_createTextpathProps(); + } + rPropMap.setAnyProperty(PROP_CustomShapeGeometry, uno::makeAny(aGeomPropSeq)); + } + if (moStyle.has()) + { + OUString aStyle = moStyle.get(OUString()); + + sal_Int32 nIndex = 0; + while( nIndex >= 0 ) + { + OUString aName, aValue; + if (ConversionHelper::separatePair(aName, aValue, aStyle.getToken(0, ';', nIndex), ':')) + { + if (aName == "font-family") + { + // remove " (first, and last character) + if (aValue.getLength() > 2) + aValue = aValue.copy(1, aValue.getLength() - 2); + + uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY); + xPropertySet->setPropertyValue("CharFontName", uno::makeAny(aValue)); + sFont = aValue; + } + else if (aName == "font-size") + { + oox::OptValue<OUString> aOptString(aValue); + float nSize = drawingml::convertEmuToPoints(lclGetEmu(rGraphicHelper, aOptString, 1)); + + uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY); + xPropertySet->setPropertyValue("CharHeight", uno::makeAny(nSize)); + } + } + } + } + if (moTrim.has() && moTrim.get()) + return; + + OUString sText = moString.get(); + ScopedVclPtrInstance<VirtualDevice> pDevice; + vcl::Font aFont = pDevice->GetFont(); + aFont.SetFamilyName(sFont); + aFont.SetFontSize(Size(0, 96)); + pDevice->SetFont(aFont); + + auto nTextWidth = pDevice->GetTextWidth(sText); + if (nTextWidth) + { + sal_Int32 nNewHeight = (static_cast<double>(pDevice->GetTextHeight()) / nTextWidth) * xShape->getSize().Width; + xShape->setSize(awt::Size(xShape->getSize().Width, nNewHeight)); + } +} + +} // namespace oox + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmlinputstream.cxx b/oox/source/vml/vmlinputstream.cxx new file mode 100644 index 000000000..9a9515685 --- /dev/null +++ b/oox/source/vml/vmlinputstream.cxx @@ -0,0 +1,395 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/vml/vmlinputstream.hxx> + +#include <com/sun/star/io/IOException.hpp> +#include <com/sun/star/io/XTextInputStream2.hpp> +#include <map> +#include <string.h> +#include <rtl/strbuf.hxx> +#include <osl/diagnose.h> +#include <oox/helper/textinputstream.hxx> + +namespace oox::vml { + +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::uno; + +namespace { + +const char* lclFindCharacter( const char* pcBeg, const char* pcEnd, char cChar ) +{ + sal_Int32 nIndex = rtl_str_indexOfChar_WithLength( pcBeg, static_cast< sal_Int32 >( pcEnd - pcBeg ), cChar ); + return (nIndex < 0) ? pcEnd : (pcBeg + nIndex); +} + +bool lclIsWhiteSpace( char cChar ) +{ + return cChar <= 32; +} + +const char* lclFindWhiteSpace( const char* pcBeg, const char* pcEnd ) +{ + for( ; pcBeg < pcEnd; ++pcBeg ) + if( lclIsWhiteSpace( *pcBeg ) ) + return pcBeg; + return pcEnd; +} + +const char* lclFindNonWhiteSpace( const char* pcBeg, const char* pcEnd ) +{ + for( ; pcBeg < pcEnd; ++pcBeg ) + if( !lclIsWhiteSpace( *pcBeg ) ) + return pcBeg; + return pcEnd; +} + +const char* lclTrimWhiteSpaceFromEnd( const char* pcBeg, const char* pcEnd ) +{ + while( (pcBeg < pcEnd) && lclIsWhiteSpace( pcEnd[ -1 ] ) ) + --pcEnd; + return pcEnd; +} + +void lclAppendToBuffer( OStringBuffer& rBuffer, const char* pcBeg, const char* pcEnd ) +{ + rBuffer.append( pcBeg, static_cast< sal_Int32 >( pcEnd - pcBeg ) ); +} + +void lclProcessAttribs( OStringBuffer& rBuffer, const char* pcBeg, const char* pcEnd ) +{ + /* Map attribute names to char-pointer of all attributes. This map is used + to find multiple occurrences of attributes with the same name. The + mapped pointers are used as map key in the next map below. */ + typedef ::std::map< OString, const char* > AttributeNameMap; + AttributeNameMap aAttributeNames; + + /* Map the char-pointers of all attributes to the full attribute definition + string. This preserves the original order of the used attributes. */ + typedef ::std::map< const char*, OString > AttributeDataMap; + AttributeDataMap aAttributes; + + bool bOk = true; + const char* pcNameBeg = pcBeg; + while( bOk && (pcNameBeg < pcEnd) ) + { + // pcNameBeg points to begin of attribute name, find equality sign + const char* pcEqualSign = lclFindCharacter( pcNameBeg, pcEnd, '=' ); + bOk = (pcEqualSign < pcEnd); + if (bOk) + { + // find end of attribute name (ignore whitespace between name and equality sign) + const char* pcNameEnd = lclTrimWhiteSpaceFromEnd( pcNameBeg, pcEqualSign ); + bOk = (pcNameBeg < pcNameEnd); + if( bOk ) + { + // find begin of attribute value (must be single or double quote) + const char* pcValueBeg = lclFindNonWhiteSpace( pcEqualSign + 1, pcEnd ); + bOk = (pcValueBeg < pcEnd) && ((*pcValueBeg == '\'') || (*pcValueBeg == '"')); + if( bOk ) + { + // find end of attribute value (matching quote character) + const char* pcValueEnd = lclFindCharacter( pcValueBeg + 1, pcEnd, *pcValueBeg ); + bOk = (pcValueEnd < pcEnd); + if( bOk ) + { + ++pcValueEnd; + OString aAttribName( pcNameBeg, static_cast< sal_Int32 >( pcNameEnd - pcNameBeg ) ); + OString aAttribData( pcNameBeg, static_cast< sal_Int32 >( pcValueEnd - pcNameBeg ) ); + // search for an existing attribute with the same name + AttributeNameMap::iterator aIt = aAttributeNames.find( aAttribName ); + // remove its definition from the data map + if( aIt != aAttributeNames.end() ) + aAttributes.erase( aIt->second ); + // insert the attribute into both maps + aAttributeNames[ aAttribName ] = pcNameBeg; + aAttributes[ pcNameBeg ] = aAttribData; + // continue with next attribute (skip whitespace after this attribute) + pcNameBeg = pcValueEnd; + if( pcNameBeg < pcEnd ) + { + bOk = lclIsWhiteSpace( *pcNameBeg ); + if( bOk ) + pcNameBeg = lclFindNonWhiteSpace( pcNameBeg + 1, pcEnd ); + } + } + } + } + } + } + + // if no error has occurred, build the resulting attribute list + if( bOk ) + for (auto const& attrib : aAttributes) + rBuffer.append( ' ' ).append( attrib.second ); + // on error, just append the complete passed string + else + lclAppendToBuffer( rBuffer, pcBeg, pcEnd ); +} + +void lclProcessElement( OStringBuffer& rBuffer, const OString& rElement ) +{ + // check that passed string starts and ends with the brackets of an XML element + sal_Int32 nElementLen = rElement.getLength(); + if( nElementLen == 0 ) + return; + + const char* pcOpen = rElement.getStr(); + const char* pcClose = pcOpen + nElementLen - 1; + + // no complete element found + if( (pcOpen >= pcClose) || (*pcOpen != '<') || (*pcClose != '>') ) + { + // just append all passed characters + rBuffer.append( rElement ); + } + + // skip parser instructions: '<![...]>' + else if( (nElementLen >= 5) && (pcOpen[ 1 ] == '!') && (pcOpen[ 2 ] == '[') && (pcClose[ -1 ] == ']') ) + { + // do nothing + } + + // just append any xml prolog (text directive) or processing instructions: <?...?> + else if( (nElementLen >= 4) && (pcOpen[ 1 ] == '?') && (pcClose[ -1 ] == '?') ) + { + rBuffer.append( rElement ); + } + + // replace '<br>' element with newline + else if( (nElementLen >= 4) && (pcOpen[ 1 ] == 'b') && (pcOpen[ 2 ] == 'r') && (lclFindNonWhiteSpace( pcOpen + 3, pcClose ) == pcClose) ) + { + rBuffer.append( '\n' ); + } + + // check start elements and simple elements for repeated attributes + else if( pcOpen[ 1 ] != '/' ) + { + // find positions of text content inside brackets, exclude '/' in '<simpleelement/>' + const char* pcContentBeg = pcOpen + 1; + bool bIsEmptyElement = pcClose[ -1 ] == '/'; + const char* pcContentEnd = bIsEmptyElement ? (pcClose - 1) : pcClose; + // append opening bracket and element name to buffer + const char* pcWhiteSpace = lclFindWhiteSpace( pcContentBeg, pcContentEnd ); + lclAppendToBuffer( rBuffer, pcOpen, pcWhiteSpace ); + // find begin of attributes, and process all attributes + const char* pcAttribBeg = lclFindNonWhiteSpace( pcWhiteSpace, pcContentEnd ); + if( pcAttribBeg < pcContentEnd ) + lclProcessAttribs( rBuffer, pcAttribBeg, pcContentEnd ); + // close the element + if( bIsEmptyElement ) + rBuffer.append( '/' ); + rBuffer.append( '>' ); + } + + // append end elements without further processing + else + { + rBuffer.append( rElement ); + } +} + +bool lclProcessCharacters( OStringBuffer& rBuffer, const OString& rChars ) +{ + /* MSO has a very weird way to store and handle whitespaces. The stream + may contain lots of spaces, tabs, and newlines which have to be handled + as single space character. This will be done in this function. + + If the element text contains a literal line break, it will be stored as + <br> tag (without matching </br> element). This input stream wrapper + will replace this element with a literal LF character (see below). + + A single space character for its own is stored as is. Example: The + element + <font> </font> + represents a single space character. The XML parser will ignore this + space character completely without issuing a 'characters' event. The + VML import filter implementation has to react on this case manually. + + A single space character following another character is stored + literally and must not be stripped away here. Example: The element + <font>abc </font> + contains the three letters a, b, and c, followed by a space character. + + Consecutive space characters, or a leading single space character, are + stored in a <span> element. If there are N space characters (N > 1), + then the <span> element contains exactly (N-1) NBSP (non-breaking + space) characters, followed by a regular space character. Examples: + The element + <font><span style='mso-spacerun:yes'>\xA0\xA0\xA0 </span></font> + represents 4 consecutive space characters. Has to be handled by the + implementation. The element + <font><span style='mso-spacerun:yes'> abc</span></font> + represents a space characters followed by the letters a, b, c. These + strings have to be handled by the VML import filter implementation. + */ + + // passed string ends with the leading opening bracket of an XML element + const char* pcBeg = rChars.getStr(); + const char* pcEnd = pcBeg + rChars.getLength(); + bool bHasBracket = (pcBeg < pcEnd) && (pcEnd[ -1 ] == '<'); + if( bHasBracket ) --pcEnd; + + // skip leading whitespace + const char* pcContentsBeg = lclFindNonWhiteSpace( pcBeg, pcEnd ); + while( pcContentsBeg < pcEnd ) + { + const char* pcWhitespaceBeg = lclFindWhiteSpace( pcContentsBeg + 1, pcEnd ); + lclAppendToBuffer( rBuffer, pcContentsBeg, pcWhitespaceBeg ); + if( pcWhitespaceBeg < pcEnd ) + rBuffer.append( ' ' ); + pcContentsBeg = lclFindNonWhiteSpace( pcWhitespaceBeg, pcEnd ); + } + + return bHasBracket; +} + +} // namespace + +static const OString gaOpeningCData( "<![CDATA[" ); +static const OString gaClosingCData( "]]>" ); + +InputStream::InputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm ) : + // use single-byte ISO-8859-1 encoding which maps all byte characters to the first 256 Unicode characters + mxTextStrm( TextInputStream::createXTextInputStream( rxContext, rxInStrm, RTL_TEXTENCODING_ISO_8859_1 ) ), + maOpeningBracket( 1 ), + maClosingBracket( 1 ), + mnBufferPos( 0 ) +{ + if (!mxTextStrm.is()) + throw IOException(); + maOpeningBracket[ 0 ] = '<'; + maClosingBracket[ 0 ] = '>'; +} + +InputStream::~InputStream() +{ +} + +sal_Int32 SAL_CALL InputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead ) +{ + if( nBytesToRead < 0 ) + throw IOException(); + + rData.realloc( nBytesToRead ); + sal_Int8* pcDest = rData.getArray(); + sal_Int32 nRet = 0; + while( (nBytesToRead > 0) && !mxTextStrm->isEOF() ) + { + updateBuffer(); + sal_Int32 nReadSize = ::std::min( nBytesToRead, maBuffer.getLength() - mnBufferPos ); + if( nReadSize > 0 ) + { + memcpy( pcDest + nRet, maBuffer.getStr() + mnBufferPos, static_cast< size_t >( nReadSize ) ); + mnBufferPos += nReadSize; + nBytesToRead -= nReadSize; + nRet += nReadSize; + } + } + if( nRet < rData.getLength() ) + rData.realloc( nRet ); + return nRet; +} + +sal_Int32 SAL_CALL InputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead ) +{ + return readBytes( rData, nMaxBytesToRead ); +} + +void SAL_CALL InputStream::skipBytes( sal_Int32 nBytesToSkip ) +{ + if( nBytesToSkip < 0 ) + throw IOException(); + + while( (nBytesToSkip > 0) && !mxTextStrm->isEOF() ) + { + updateBuffer(); + sal_Int32 nSkipSize = ::std::min( nBytesToSkip, maBuffer.getLength() - mnBufferPos ); + mnBufferPos += nSkipSize; + nBytesToSkip -= nSkipSize; + } +} + +sal_Int32 SAL_CALL InputStream::available() +{ + updateBuffer(); + return maBuffer.getLength() - mnBufferPos; +} + +void SAL_CALL InputStream::closeInput() +{ + mxTextStrm->closeInput(); +} + +// private -------------------------------------------------------------------- + +void InputStream::updateBuffer() +{ + while( (mnBufferPos >= maBuffer.getLength()) && !mxTextStrm->isEOF() ) + { + // collect new contents in a string buffer + OStringBuffer aBuffer; + + // read and process characters until the opening bracket of the next XML element + OString aChars = readToElementBegin(); + bool bHasOpeningBracket = lclProcessCharacters( aBuffer, aChars ); + + // read and process characters until (and including) closing bracket (an XML element) + OSL_ENSURE( bHasOpeningBracket || mxTextStrm->isEOF(), "InputStream::updateBuffer - missing opening bracket of XML element" ); + if( bHasOpeningBracket && !mxTextStrm->isEOF() ) + { + // read the element text (add the leading opening bracket manually) + OString aElement = "<" + readToElementEnd(); + // check for CDATA part, starting with '<![CDATA[' + if( aElement.match( gaOpeningCData ) ) + { + // search the end tag ']]>' + while( ((aElement.getLength() < gaClosingCData.getLength()) || !aElement.endsWith( gaClosingCData )) && !mxTextStrm->isEOF() ) + aElement += readToElementEnd(); + // copy the entire CDATA part + aBuffer.append( aElement ); + } + else + { + // no CDATA part - process the contents of the element + lclProcessElement( aBuffer, aElement ); + } + } + + maBuffer = aBuffer.makeStringAndClear(); + mnBufferPos = 0; + } +} + +OString InputStream::readToElementBegin() +{ + return OUStringToOString( mxTextStrm->readString( maOpeningBracket, false ), RTL_TEXTENCODING_ISO_8859_1 ); +} + +OString InputStream::readToElementEnd() +{ + OString aText = OUStringToOString( mxTextStrm->readString( maClosingBracket, false ), RTL_TEXTENCODING_ISO_8859_1 ); + OSL_ENSURE( aText.endsWith(">"), "InputStream::readToElementEnd - missing closing bracket of XML element" ); + return aText; +} + +} // namespace oox::vml + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmlshape.cxx b/oox/source/vml/vmlshape.cxx new file mode 100644 index 000000000..a7a0c46d6 --- /dev/null +++ b/oox/source/vml/vmlshape.cxx @@ -0,0 +1,1454 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <algorithm> +#include <cassert> + +#include <optional> + +#include <o3tl/safeint.hxx> +#include <oox/vml/vmlshape.hxx> +#include <vcl/wmfexternal.hxx> + +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/drawing/EnhancedCustomShapeAdjustmentValue.hpp> +#include <com/sun/star/drawing/FillStyle.hpp> +#include <com/sun/star/drawing/PointSequenceSequence.hpp> +#include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp> +#include <com/sun/star/drawing/TextVerticalAdjust.hpp> +#include <com/sun/star/drawing/XEnhancedCustomShapeDefaulter.hpp> +#include <com/sun/star/drawing/XShapes.hpp> +#include <com/sun/star/drawing/XControlShape.hpp> +#include <com/sun/star/graphic/XGraphic.hpp> +#include <com/sun/star/table/BorderLine2.hpp> +#include <com/sun/star/text/HoriOrientation.hpp> +#include <com/sun/star/text/RelOrientation.hpp> +#include <com/sun/star/text/SizeType.hpp> +#include <com/sun/star/text/VertOrientation.hpp> +#include <com/sun/star/text/WrapTextMode.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/text/TextContentAnchorType.hpp> +#include <com/sun/star/text/GraphicCrop.hpp> +#include <com/sun/star/security/DocumentDigitalSignatures.hpp> +#include <com/sun/star/security/XDocumentDigitalSignatures.hpp> +#include <com/sun/star/text/WritingMode2.hpp> +#include <sal/log.hxx> +#include <oox/drawingml/shapepropertymap.hxx> +#include <oox/helper/graphichelper.hxx> +#include <oox/helper/propertyset.hxx> +#include <oox/ole/axcontrol.hxx> +#include <oox/ole/axcontrolfragment.hxx> +#include <oox/ole/oleobjecthelper.hxx> +#include <oox/token/properties.hxx> +#include <oox/token/tokens.hxx> +#include <oox/vml/vmldrawing.hxx> +#include <oox/vml/vmlshapecontainer.hxx> +#include <oox/vml/vmltextbox.hxx> +#include <oox/core/xmlfilterbase.hxx> +#include <oox/helper/containerhelper.hxx> +#include <svx/EnhancedCustomShapeTypeNames.hxx> +#include <svx/unoapi.hxx> +#include <svx/sdtagitm.hxx> +#include <svx/svdobj.hxx> +#include <comphelper/sequence.hxx> +#include <comphelper/processfactory.hxx> +#include <comphelper/propertyvalue.hxx> +#include <comphelper/storagehelper.hxx> + +using ::com::sun::star::beans::XPropertySet; +using ::com::sun::star::uno::Any; + +using namespace ::com::sun::star; +using namespace ::com::sun::star::text; + +namespace oox::vml { + +using namespace ::com::sun::star::drawing; +using namespace ::com::sun::star::graphic; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; + +using ::oox::core::XmlFilterBase; + +namespace { + +const sal_Int32 VML_SHAPETYPE_PICTUREFRAME = 75; +const sal_Int32 VML_SHAPETYPE_HOSTCONTROL = 201; + +awt::Point lclGetAbsPoint( const awt::Point& rRelPoint, const awt::Rectangle& rShapeRect, const awt::Rectangle& rCoordSys ) +{ + double fWidthRatio = static_cast< double >( rShapeRect.Width ) / rCoordSys.Width; + double fHeightRatio = static_cast< double >( rShapeRect.Height ) / rCoordSys.Height; + awt::Point aAbsPoint; + aAbsPoint.X = static_cast< sal_Int32 >( rShapeRect.X + fWidthRatio * (rRelPoint.X - rCoordSys.X) + 0.5 ); + aAbsPoint.Y = static_cast< sal_Int32 >( rShapeRect.Y + fHeightRatio * (rRelPoint.Y - rCoordSys.Y) + 0.5 ); + return aAbsPoint; +} + +awt::Rectangle lclGetAbsRect( const awt::Rectangle& rRelRect, const awt::Rectangle& rShapeRect, const awt::Rectangle& rCoordSys ) +{ + double fWidthRatio = static_cast< double >( rShapeRect.Width ) / rCoordSys.Width; + double fHeightRatio = static_cast< double >( rShapeRect.Height ) / rCoordSys.Height; + awt::Rectangle aAbsRect; + aAbsRect.X = static_cast< sal_Int32 >( rShapeRect.X + fWidthRatio * (rRelRect.X - rCoordSys.X) + 0.5 ); + aAbsRect.Y = static_cast< sal_Int32 >( rShapeRect.Y + fHeightRatio * (rRelRect.Y - rCoordSys.Y) + 0.5 ); + aAbsRect.Width = static_cast< sal_Int32 >( fWidthRatio * rRelRect.Width + 0.5 ); + aAbsRect.Height = static_cast< sal_Int32 >( fHeightRatio * rRelRect.Height + 0.5 ); + return aAbsRect; +} + +/// Count the crop value based on a crop fraction and a reference size. +sal_Int32 lclConvertCrop(const OUString& rCrop, sal_uInt32 nSize) +{ + if (rCrop.endsWith("f")) + { + // Numeric value is specified in 1/65536-ths. + sal_uInt32 nCrop = rCrop.copy(0, rCrop.getLength() - 1).toUInt32(); + return (nCrop * nSize) / 65536; + } + + return 0; +} + +} // namespace + +ShapeTypeModel::ShapeTypeModel(): + mbAutoHeight( false ), + mbVisible( true ) +{ +} + +void ShapeTypeModel::assignUsed( const ShapeTypeModel& rSource ) +{ + moShapeType.assignIfUsed( rSource.moShapeType ); + moCoordPos.assignIfUsed( rSource.moCoordPos ); + moCoordSize.assignIfUsed( rSource.moCoordSize ); + /* The style properties position, left, top, width, height, margin-left, + margin-top are not derived from shape template to shape. */ + maStrokeModel.assignUsed( rSource.maStrokeModel ); + maFillModel.assignUsed( rSource.maFillModel ); + moGraphicPath.assignIfUsed( rSource.moGraphicPath ); + moGraphicTitle.assignIfUsed( rSource.moGraphicTitle ); +} + +ShapeType::ShapeType( Drawing& rDrawing ) : + mrDrawing( rDrawing ) +{ +} + +ShapeType::~ShapeType() +{ +} + +sal_Int32 ShapeType::getShapeType() const +{ + return maTypeModel.moShapeType.get( 0 ); +} + +OUString ShapeType::getGraphicPath() const +{ + return maTypeModel.moGraphicPath.get( OUString() ); +} + +awt::Rectangle ShapeType::getCoordSystem() const +{ + Int32Pair aCoordPos = maTypeModel.moCoordPos.get( Int32Pair( 0, 0 ) ); + Int32Pair aCoordSize = maTypeModel.moCoordSize.get( Int32Pair( 1000, 1000 ) ); + if( aCoordSize.first == 0 ) + aCoordSize.first = 1; + if( aCoordSize.second == 0 ) + aCoordSize.second = 1; + return awt::Rectangle( aCoordPos.first, aCoordPos.second, aCoordSize.first, aCoordSize.second ); +} + +awt::Rectangle ShapeType::getRectangle( const ShapeParentAnchor* pParentAnchor ) const +{ + return pParentAnchor ? + lclGetAbsRect( getRelRectangle(), pParentAnchor->maShapeRect, pParentAnchor->maCoordSys ) : + getAbsRectangle(); +} + +awt::Rectangle ShapeType::getAbsRectangle() const +{ + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + + sal_Int32 nWidth = ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maTypeModel.maWidth, 0, true, true ); + if ( nWidth == 0 ) + nWidth = 1; + + sal_Int32 nHeight = ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maTypeModel.maHeight, 0, false, true ); + if ( nHeight == 0 ) + nHeight = 1; + + sal_Int32 nLeft; + if (o3tl::checked_add<sal_Int32>(ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maTypeModel.maLeft, 0, true, true), + ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maTypeModel.maMarginLeft, 0, true, true), + nLeft)) + { + SAL_WARN("oox", "overflow in addition"); + nLeft = 0; + } + if (nLeft == 0 && maTypeModel.maPosition == "absolute") + nLeft = 1; + + return awt::Rectangle( + nLeft, + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maTypeModel.maTop, 0, false, true ) + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maTypeModel.maMarginTop, 0, false, true ), + nWidth, nHeight ); +} + +awt::Rectangle ShapeType::getRelRectangle() const +{ + sal_Int32 nWidth = maTypeModel.maWidth.toInt32(); + if ( nWidth == 0 ) + nWidth = 1; + + sal_Int32 nHeight = maTypeModel.maHeight.toInt32(); + if ( nHeight == 0 ) + nHeight = 1; + + return awt::Rectangle( + maTypeModel.maLeft.toInt32(), + maTypeModel.maTop.toInt32(), + nWidth, nHeight ); +} + +ClientData::ClientData() : + mnObjType( XML_TOKEN_INVALID ), + mnTextHAlign( XML_Left ), + mnTextVAlign( XML_Top ), + mnCol( -1 ), + mnRow( -1 ), + mnChecked( VML_CLIENTDATA_UNCHECKED ), + mnDropStyle( XML_Combo ), + mnDropLines( 1 ), + mnVal( 0 ), + mnMin( 0 ), + mnMax( 0 ), + mnInc( 0 ), + mnPage( 0 ), + mnSelType( XML_Single ), + mnVTEdit( VML_CLIENTDATA_TEXT ), + mbPrintObject( true ), + mbVisible( false ), + mbDde( false ), + mbNo3D( false ), + mbNo3D2( false ), + mbMultiLine( false ), + mbVScroll( false ), + mbSecretEdit( false ) +{ +} + +ShapeModel::ShapeModel() + : mbIsSignatureLine(false) + , mbSignatureLineShowSignDate(true) + , mbSignatureLineCanAddComment(false) +{ +} + +ShapeModel::~ShapeModel() +{ +} + +TextBox& ShapeModel::createTextBox(ShapeTypeModel& rModel) +{ + mxTextBox.reset( new TextBox(rModel) ); + return *mxTextBox; +} + +ClientData& ShapeModel::createClientData() +{ + mxClientData.reset( new ClientData ); + return *mxClientData; +} + +ShapeBase::ShapeBase( Drawing& rDrawing ) : + ShapeType( rDrawing ) +{ +} + +void ShapeBase::finalizeFragmentImport() +{ + if( maShapeModel.maType.getLength() <= 1 ) + return; + + OUString aType = maShapeModel.maType; + if (aType[ 0 ] == '#') + aType = aType.copy(1); + if( const ShapeType* pShapeType = mrDrawing.getShapes().getShapeTypeById( aType ) ) + { + // Make sure that the stroke props from maTypeModel have priority over the stroke props from + // the shape type. + StrokeModel aMergedStrokeModel; + aMergedStrokeModel.assignUsed(pShapeType->getTypeModel().maStrokeModel); + aMergedStrokeModel.assignUsed(maTypeModel.maStrokeModel); + maTypeModel.assignUsed( pShapeType->getTypeModel() ); + maTypeModel.maStrokeModel = aMergedStrokeModel; + } + else { + // Temporary fix, shapetype not found if referenced from different substream + // FIXME: extend scope of ShapeContainer to store all shapetypes from the document + const OUString sShapeTypePrefix = "shapetype_"; + if (aType.startsWith(sShapeTypePrefix)) { + maTypeModel.moShapeType = aType.copy(sShapeTypePrefix.getLength()).toInt32(); + } + } +} + +OUString ShapeBase::getShapeName() const +{ + if( !maTypeModel.maShapeName.isEmpty() ) + return maTypeModel.maShapeName; + + OUString aBaseName = mrDrawing.getShapeBaseName( *this ); + if( !aBaseName.isEmpty() ) + { + sal_Int32 nShapeIdx = mrDrawing.getLocalShapeIndex( getShapeId() ); + if( nShapeIdx > 0 ) + return aBaseName + OUStringChar(' ') + OUString::number( nShapeIdx ); + } + + return OUString(); +} + +const ShapeType* ShapeBase::getChildTypeById( const OUString& ) const +{ + return nullptr; +} + +const ShapeBase* ShapeBase::getChildById( const OUString& ) const +{ + return nullptr; +} + +Reference< XShape > ShapeBase::convertAndInsert( const Reference< XShapes >& rxShapes, const ShapeParentAnchor* pParentAnchor ) const +{ + Reference< XShape > xShape; + if( mrDrawing.isShapeSupported( *this ) ) + { + /* Calculate shape rectangle. Applications may do something special + according to some imported shape client data (e.g. Excel cell anchor). */ + awt::Rectangle aShapeRect = calcShapeRectangle( pParentAnchor ); + + if( ((aShapeRect.Width > 0) || (aShapeRect.Height > 0)) && rxShapes.is() ) + { + xShape = implConvertAndInsert( rxShapes, aShapeRect ); + if( xShape.is() ) + { + // set imported or generated shape name (not supported by form controls) + PropertySet aShapeProp( xShape ); + if( aShapeProp.hasProperty( PROP_Name ) ) + aShapeProp.setProperty( PROP_Name, getShapeName() ); + uno::Reference< lang::XServiceInfo > xSInfo( xShape, uno::UNO_QUERY_THROW ); + + OUString sLinkChainName = getTypeModel().maLegacyId; + sal_Int32 id = 0; + sal_Int32 idPos = sLinkChainName.indexOf("_x"); + sal_Int32 seq = 0; + if (idPos >= 0) + { + sal_Int32 seqPos = sLinkChainName.indexOf("_s",idPos); + if (idPos < seqPos) + { + auto idPosEnd = idPos+2; + id = sLinkChainName.copy(idPosEnd, seqPos - idPosEnd).toInt32(); + seq = sLinkChainName.copy(seqPos+2).toInt32(); + } + } + + OUString s_mso_next_textbox; + if( getTextBox() ) + s_mso_next_textbox = getTextBox()->msNextTextbox; + if( s_mso_next_textbox.startsWith("#") ) + s_mso_next_textbox = s_mso_next_textbox.copy(1); + + if (xSInfo->supportsService("com.sun.star.text.TextFrame")) + { + uno::Reference<beans::XPropertySet> propertySet (xShape, uno::UNO_QUERY); + uno::Any aAny = propertySet->getPropertyValue("FrameInteropGrabBag"); + auto aGrabBag = comphelper::sequenceToContainer< std::vector<beans::PropertyValue> >(aAny.get< uno::Sequence<beans::PropertyValue> >()); + + aGrabBag.push_back(comphelper::makePropertyValue("VML-Z-ORDER", maTypeModel.maZIndex.toInt32())); + + if( !s_mso_next_textbox.isEmpty() ) + aGrabBag.push_back(comphelper::makePropertyValue("mso-next-textbox", s_mso_next_textbox)); + + if( !sLinkChainName.isEmpty() ) + { + aGrabBag.push_back(comphelper::makePropertyValue("TxbxHasLink", true)); + aGrabBag.push_back(comphelper::makePropertyValue("Txbx-Id", id)); + aGrabBag.push_back(comphelper::makePropertyValue("Txbx-Seq", seq)); + aGrabBag.push_back(comphelper::makePropertyValue("LinkChainName", sLinkChainName)); + } + + if(!maTypeModel.maRotation.isEmpty()) + aGrabBag.push_back(comphelper::makePropertyValue("mso-rotation-angle", ConversionHelper::decodeRotation(maTypeModel.maRotation))); + propertySet->setPropertyValue("FrameInteropGrabBag", uno::makeAny(comphelper::containerToSequence(aGrabBag))); + sal_Int32 backColorTransparency = 0; + propertySet->getPropertyValue("BackColorTransparency") + >>= backColorTransparency; + if (propertySet->getPropertyValue("FillStyle") == FillStyle_NONE && + backColorTransparency == 100) + { + // If there is no fill, the Word default is 100% transparency. + propertySet->setPropertyValue("FillTransparence", makeAny(sal_Int16(100))); + } + } + else + { + if( maTypeModel.maZIndex.toInt32() ) + { + uno::Sequence<beans::PropertyValue> aGrabBag; + uno::Reference<beans::XPropertySet> propertySet (xShape, uno::UNO_QUERY); + propertySet->getPropertyValue("InteropGrabBag") >>= aGrabBag; + sal_Int32 length; + + length = aGrabBag.getLength(); + aGrabBag.realloc( length+1 ); + aGrabBag[length].Name = "VML-Z-ORDER"; + aGrabBag[length].Value <<= maTypeModel.maZIndex.toInt32(); + + if( !s_mso_next_textbox.isEmpty() ) + { + length = aGrabBag.getLength(); + aGrabBag.realloc( length+1 ); + aGrabBag[length].Name = "mso-next-textbox"; + aGrabBag[length].Value <<= s_mso_next_textbox; + } + + if( !sLinkChainName.isEmpty() ) + { + length = aGrabBag.getLength(); + aGrabBag.realloc( length+4 ); + aGrabBag[length].Name = "TxbxHasLink"; + aGrabBag[length].Value <<= true; + aGrabBag[length+1].Name = "Txbx-Id"; + aGrabBag[length+1].Value <<= id; + aGrabBag[length+2].Name = "Txbx-Seq"; + aGrabBag[length+2].Value <<= seq; + aGrabBag[length+3].Name = "LinkChainName"; + aGrabBag[length+3].Value <<= sLinkChainName; + } + propertySet->setPropertyValue( "InteropGrabBag", uno::makeAny(aGrabBag) ); + } + } + Reference< XControlShape > xControlShape( xShape, uno::UNO_QUERY ); + if ( xControlShape.is() && !getTypeModel().mbVisible ) + { + PropertySet aControlShapeProp( xControlShape->getControl() ); + aControlShapeProp.setProperty( PROP_EnableVisible, uno::makeAny( false ) ); + } + /* Notify the drawing that a new shape has been inserted. For + convenience, pass the rectangle that contains position and + size of the shape. */ + bool bGroupChild = pParentAnchor != nullptr; + mrDrawing.notifyXShapeInserted( xShape, aShapeRect, *this, bGroupChild ); + } + } + else + SAL_WARN("oox", "not converting shape, as calculated rectangle is empty"); + } + return xShape; +} + +void ShapeBase::convertFormatting( const Reference< XShape >& rxShape ) const +{ + if( !rxShape.is() ) + return; + + /* Calculate shape rectangle. Applications may do something special + according to some imported shape client data (e.g. Excel cell anchor). */ + awt::Rectangle aShapeRect = calcShapeRectangle( nullptr ); + + // convert the shape, if the calculated rectangle is not empty + if( (aShapeRect.Width > 0) || (aShapeRect.Height > 0) ) + { + rxShape->setPosition( awt::Point( aShapeRect.X, aShapeRect.Y ) ); + rxShape->setSize( awt::Size( aShapeRect.Width, aShapeRect.Height ) ); + convertShapeProperties( rxShape ); + } +} + +void ShapeBase::setContainer(ShapeContainer* pContainer) { mpContainer = pContainer; } + +ShapeContainer* ShapeBase::getContainer() const { return mpContainer; } + +// protected ------------------------------------------------------------------ + +awt::Rectangle ShapeBase::calcShapeRectangle( const ShapeParentAnchor* pParentAnchor ) const +{ + /* Calculate shape rectangle. Applications may do something special + according to some imported shape client data (e.g. Excel cell anchor). */ + awt::Rectangle aShapeRect; + const ClientData* pClientData = getClientData(); + if( !pClientData || !mrDrawing.convertClientAnchor( aShapeRect, pClientData->maAnchor ) ) + aShapeRect = getRectangle( pParentAnchor ); + return aShapeRect; +} + +void ShapeBase::convertShapeProperties( const Reference< XShape >& rxShape ) const +{ + ::oox::drawingml::ShapePropertyMap aPropMap( mrDrawing.getFilter().getModelObjectHelper() ); + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + maTypeModel.maStrokeModel.pushToPropMap( aPropMap, rGraphicHelper ); + maTypeModel.maFillModel.pushToPropMap( aPropMap, rGraphicHelper ); + + uno::Reference<lang::XServiceInfo> xSInfo(rxShape, uno::UNO_QUERY_THROW); + if (xSInfo->supportsService("com.sun.star.text.TextFrame")) + { + // Any other service supporting the ShadowFormat property? + maTypeModel.maShadowModel.pushToPropMap(aPropMap, rGraphicHelper); + // TextFrames have BackColor, not FillColor + if (aPropMap.hasProperty(PROP_FillColor)) + { + aPropMap.setAnyProperty(PROP_BackColor, aPropMap.getProperty(PROP_FillColor)); + aPropMap.erase(PROP_FillColor); + } + // TextFrames have BackColorTransparency, not FillTransparence + if (aPropMap.hasProperty(PROP_FillTransparence)) + { + aPropMap.setAnyProperty(PROP_BackColorTransparency, aPropMap.getProperty(PROP_FillTransparence)); + aPropMap.erase(PROP_FillTransparence); + } + // And no LineColor property; individual borders can have colors and widths + std::optional<sal_Int32> oLineWidth; + if (maTypeModel.maStrokeModel.moWeight.has()) + oLineWidth = ConversionHelper::decodeMeasureToHmm( + rGraphicHelper, maTypeModel.maStrokeModel.moWeight.get(), 0, false, false); + if (aPropMap.hasProperty(PROP_LineColor)) + { + uno::Reference<beans::XPropertySet> xPropertySet(rxShape, uno::UNO_QUERY); + static const sal_Int32 aBorders[] = { + PROP_TopBorder, PROP_LeftBorder, PROP_BottomBorder, PROP_RightBorder + }; + for (sal_Int32 nBorder : aBorders) + { + table::BorderLine2 aBorderLine = xPropertySet->getPropertyValue(PropertyMap::getPropertyName(nBorder)).get<table::BorderLine2>(); + aBorderLine.Color = aPropMap.getProperty(PROP_LineColor).get<sal_Int32>(); + if (oLineWidth) + aBorderLine.LineWidth = *oLineWidth; + aPropMap.setProperty(nBorder, aBorderLine); + } + aPropMap.erase(PROP_LineColor); + } + } + else if (xSInfo->supportsService("com.sun.star.drawing.CustomShape")) + maTypeModel.maTextpathModel.pushToPropMap(aPropMap, rxShape, rGraphicHelper); + + PropertySet( rxShape ).setProperties( aPropMap ); +} + +SimpleShape::SimpleShape( Drawing& rDrawing, const OUString& rService ) : + ShapeBase( rDrawing ), + maService( rService ) +{ +} + +static void lcl_setSurround(PropertySet& rPropSet, const ShapeTypeModel& rTypeModel, const GraphicHelper& rGraphicHelper) +{ + OUString aWrapType = rTypeModel.moWrapType.get(); + + // Extreme negative top margin? Then the shape will end up at the top of the page, it's pointless to perform any kind of wrapping. + sal_Int32 nMarginTop = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, rTypeModel.maMarginTop, 0, false, true); + if (nMarginTop < -35277) // Less than 1000 points. + aWrapType.clear(); + + css::text::WrapTextMode nSurround = css::text::WrapTextMode_THROUGH; + if ( aWrapType == "square" || aWrapType == "tight" || + aWrapType == "through" ) + { + nSurround = css::text::WrapTextMode_PARALLEL; + if ( rTypeModel.moWrapSide.get() == "left" ) + nSurround = css::text::WrapTextMode_LEFT; + else if ( rTypeModel.moWrapSide.get() == "right" ) + nSurround = css::text::WrapTextMode_RIGHT; + } + else if ( aWrapType == "topAndBottom" ) + nSurround = css::text::WrapTextMode_NONE; + + rPropSet.setProperty(PROP_Surround, static_cast<sal_Int32>(nSurround)); + rPropSet.setProperty(PROP_SurroundContour, aWrapType == "tight"); +} + +static void lcl_SetAnchorType(PropertySet& rPropSet, const ShapeTypeModel& rTypeModel, const GraphicHelper& rGraphicHelper) +{ + if ( rTypeModel.maPosition == "absolute" ) + { + // Word supports as-character (inline) and at-character only, absolute can't be inline. + rPropSet.setProperty(PROP_AnchorType, text::TextContentAnchorType_AT_CHARACTER); + // anchor is set after insertion, so reset to NONE + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::NONE)); + + if ( rTypeModel.maPositionVerticalRelative == "page" ) + { + rPropSet.setProperty(PROP_VertOrientRelation, text::RelOrientation::PAGE_FRAME); + } + else if ( rTypeModel.maPositionVerticalRelative == "margin" ) + { + rPropSet.setProperty(PROP_VertOrientRelation, text::RelOrientation::PAGE_PRINT_AREA); + } + else + { + rPropSet.setProperty(PROP_VertOrientRelation, text::RelOrientation::FRAME); + } + } + else if( rTypeModel.maPosition == "relative" ) + { // I'm not very sure this is correct either. + rPropSet.setProperty(PROP_AnchorType, text::TextContentAnchorType_AT_PARAGRAPH); + // anchor is set after insertion, so reset to NONE + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::NONE)); + } + else // static (is the default) means anchored inline + { + rPropSet.setProperty(PROP_AnchorType, text::TextContentAnchorType_AS_CHARACTER); + // Use top orientation, this one seems similar to what MSO uses as inline + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::TOP)); + } + + if ( rTypeModel.maPositionHorizontal == "center" ) + rPropSet.setAnyProperty(PROP_HoriOrient, makeAny(text::HoriOrientation::CENTER)); + else if ( rTypeModel.maPositionHorizontal == "left" ) + rPropSet.setAnyProperty(PROP_HoriOrient, makeAny(text::HoriOrientation::LEFT)); + else if ( rTypeModel.maPositionHorizontal == "right" ) + rPropSet.setAnyProperty(PROP_HoriOrient, makeAny(text::HoriOrientation::RIGHT)); + else if ( rTypeModel.maPositionHorizontal == "inside" ) + { + rPropSet.setAnyProperty(PROP_HoriOrient, makeAny(text::HoriOrientation::LEFT)); + rPropSet.setAnyProperty(PROP_PageToggle, makeAny(true)); + } + else if ( rTypeModel.maPositionHorizontal == "outside" ) + { + rPropSet.setAnyProperty(PROP_HoriOrient, makeAny(text::HoriOrientation::RIGHT)); + rPropSet.setAnyProperty(PROP_PageToggle, makeAny(true)); + } + + if ( rTypeModel.maPositionHorizontalRelative == "page" ) + rPropSet.setAnyProperty(PROP_HoriOrientRelation, makeAny(text::RelOrientation::PAGE_FRAME)); + else if ( rTypeModel.maPositionHorizontalRelative == "margin" ) + rPropSet.setProperty(PROP_HoriOrientRelation, text::RelOrientation::PAGE_PRINT_AREA); + else if (rTypeModel.maPositionHorizontalRelative == "right-margin-area" || + rTypeModel.maPositionHorizontalRelative == "inner-margin-area") + rPropSet.setProperty(PROP_HoriOrientRelation, text::RelOrientation::PAGE_RIGHT); + else if (rTypeModel.maPositionHorizontalRelative == "left-margin-area" || + rTypeModel.maPositionHorizontalRelative == "outer-margin-area") + rPropSet.setProperty(PROP_HoriOrientRelation, text::RelOrientation::PAGE_LEFT); + else if ( rTypeModel.maPositionHorizontalRelative == "text" ) + rPropSet.setProperty(PROP_HoriOrientRelation, text::RelOrientation::FRAME); + + if ( rTypeModel.maPositionVertical == "center" ) + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::CENTER)); + else if ( rTypeModel.maPositionVertical == "top" ) + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::TOP)); + else if ( rTypeModel.maPositionVertical == "bottom" ) + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::BOTTOM)); + else if ( rTypeModel.maPositionVertical == "inside" ) + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::LINE_TOP)); + else if ( rTypeModel.maPositionVertical == "outside" ) + rPropSet.setAnyProperty(PROP_VertOrient, makeAny(text::VertOrientation::LINE_BOTTOM)); + + lcl_setSurround( rPropSet, rTypeModel, rGraphicHelper ); +} + +Reference< XShape > SimpleShape::implConvertAndInsert( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + awt::Rectangle aShapeRect(rShapeRect); + std::optional<sal_Int32> oRotation; + bool bFlipX = false, bFlipY = false; + if (!maTypeModel.maRotation.isEmpty()) + oRotation = ConversionHelper::decodeRotation(maTypeModel.maRotation); + if (!maTypeModel.maFlip.isEmpty()) + { + if (maTypeModel.maFlip.startsWith("x")) + { + bFlipX = true; + } + if (maTypeModel.maFlip.endsWith("y")) + { + bFlipY = true; + } + } + + Reference< XShape > xShape = mrDrawing.createAndInsertXShape( maService, rxShapes, aShapeRect ); + SdrObject* pShape = GetSdrObjectFromXShape( xShape ); + if( pShape && getShapeType() >= 0 ) + { + OUString aShapeType = EnhancedCustomShapeTypeNames::Get( static_cast< MSO_SPT >(getShapeType()) ); + //The resize autoshape to fit text attr of FontWork/Word-Art should always be false + //for the fallback geometry. + if(aShapeType.startsWith("fontwork")) + { + pShape->SetMergedItem(makeSdrTextAutoGrowHeightItem(false)); + pShape->SetMergedItem(makeSdrTextAutoGrowWidthItem(false)); + } + } + convertShapeProperties( xShape ); + + // Handle left/right/top/bottom wrap distance. + // Default value of mso-wrap-distance-left/right is supposed to be 0 (see + // 19.1.2.19 of the VML spec), but Word implements a non-zero value. + // [MS-ODRAW] says the below default value in 2.3.4.9. + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + OUString aWrapDistanceLeft = OUString::number(0x0001BE7C); + if (!maTypeModel.maWrapDistanceLeft.isEmpty()) + aWrapDistanceLeft = maTypeModel.maWrapDistanceLeft; + sal_Int32 nWrapDistanceLeft = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aWrapDistanceLeft, 0, true, false); + PropertySet(xShape).setAnyProperty(PROP_LeftMargin, uno::makeAny(nWrapDistanceLeft)); + OUString aWrapDistanceRight = OUString::number(0x0001BE7C); + if (!maTypeModel.maWrapDistanceRight.isEmpty()) + aWrapDistanceRight = maTypeModel.maWrapDistanceRight; + sal_Int32 nWrapDistanceRight = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aWrapDistanceRight, 0, true, false); + PropertySet(xShape).setAnyProperty(PROP_RightMargin, uno::makeAny(nWrapDistanceRight)); + sal_Int32 nWrapDistanceTop = 0; + if (!maTypeModel.maWrapDistanceTop.isEmpty()) + nWrapDistanceTop = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maTypeModel.maWrapDistanceTop, 0, false, true); + PropertySet(xShape).setAnyProperty(PROP_TopMargin, uno::makeAny(nWrapDistanceTop)); + sal_Int32 nWrapDistanceBottom = 0; + if (!maTypeModel.maWrapDistanceBottom.isEmpty()) + nWrapDistanceBottom = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maTypeModel.maWrapDistanceBottom, 0, false, true); + PropertySet(xShape).setAnyProperty(PROP_BottomMargin, uno::makeAny(nWrapDistanceBottom)); + + if ( maService == "com.sun.star.text.TextFrame" ) + { + PropertySet( xShape ).setAnyProperty( PROP_FrameIsAutomaticHeight, makeAny( maTypeModel.mbAutoHeight ) ); + PropertySet( xShape ).setAnyProperty( PROP_SizeType, makeAny( maTypeModel.mbAutoHeight ? SizeType::MIN : SizeType::FIX ) ); + if( getTextBox()->borderDistanceSet ) + { + PropertySet( xShape ).setAnyProperty( PROP_LeftBorderDistance, makeAny( sal_Int32( getTextBox()->borderDistanceLeft ))); + PropertySet( xShape ).setAnyProperty( PROP_TopBorderDistance, makeAny( sal_Int32( getTextBox()->borderDistanceTop ))); + PropertySet( xShape ).setAnyProperty( PROP_RightBorderDistance, makeAny( sal_Int32( getTextBox()->borderDistanceRight ))); + PropertySet( xShape ).setAnyProperty( PROP_BottomBorderDistance, makeAny( sal_Int32( getTextBox()->borderDistanceBottom ))); + } + + sal_Int16 nWritingMode = text::WritingMode2::LR_TB; + if (getTextBox()->maLayoutFlow == "vertical" && maTypeModel.maLayoutFlowAlt.isEmpty()) + { + nWritingMode = text::WritingMode2::TB_RL; + } + else if (maTypeModel.maLayoutFlowAlt == "bottom-to-top") + { + nWritingMode = text::WritingMode2::BT_LR; + } + if (nWritingMode != text::WritingMode2::LR_TB) + { + PropertySet(xShape).setAnyProperty(PROP_WritingMode, uno::makeAny(nWritingMode)); + } + } + else + { + // FIXME Setting the relative width/height only for everything but text frames as + // TextFrames already have relative width/height feature... but currently not working + // in the way we need. + + // Set the relative width / height if any + if ( !maTypeModel.maWidthPercent.isEmpty( ) ) + { + // Only page-relative width is supported ATM + if ( maTypeModel.maWidthRelative.isEmpty() || maTypeModel.maWidthRelative == "page" ) + { + sal_Int16 nWidth = maTypeModel.maWidthPercent.toInt32() / 10; + // Only apply if nWidth != 0 + if ( nWidth ) + PropertySet( xShape ).setAnyProperty(PROP_RelativeWidth, makeAny( nWidth ) ); + } + } + if ( !maTypeModel.maHeightPercent.isEmpty( ) ) + { + // Only page-relative height is supported ATM + if ( maTypeModel.maHeightRelative.isEmpty() || maTypeModel.maHeightRelative == "page" ) + { + sal_Int16 nHeight = maTypeModel.maHeightPercent.toInt32() / 10; + // Only apply if nHeight != 0 + if ( nHeight ) + PropertySet( xShape ).setAnyProperty(PROP_RelativeHeight, makeAny( nHeight ) ); + } + } + + // drawinglayer default is center, MSO default is top. + drawing::TextVerticalAdjust eTextVerticalAdjust = drawing::TextVerticalAdjust_TOP; + if (maTypeModel.maVTextAnchor == "middle") + eTextVerticalAdjust = drawing::TextVerticalAdjust_CENTER; + else if (maTypeModel.maVTextAnchor == "bottom") + eTextVerticalAdjust = drawing::TextVerticalAdjust_BOTTOM; + PropertySet(xShape).setAnyProperty(PROP_TextVerticalAdjust, makeAny(eTextVerticalAdjust)); + + PropertySet(xShape).setAnyProperty(PROP_TextAutoGrowHeight, + makeAny(maTypeModel.mbAutoHeight)); + + if (getTextBox()) + { + getTextBox()->convert(xShape); + if (getTextBox()->borderDistanceSet) + { + awt::Size aSize = xShape->getSize(); + PropertySet(xShape).setAnyProperty(PROP_TextLeftDistance, makeAny(sal_Int32(getTextBox()->borderDistanceLeft))); + PropertySet(xShape).setAnyProperty(PROP_TextUpperDistance, makeAny(sal_Int32(getTextBox()->borderDistanceTop))); + PropertySet(xShape).setAnyProperty(PROP_TextRightDistance, makeAny(sal_Int32(getTextBox()->borderDistanceRight))); + PropertySet(xShape).setAnyProperty(PROP_TextLowerDistance, makeAny(sal_Int32(getTextBox()->borderDistanceBottom))); + xShape->setSize(aSize); + } + } + } + + // Import Legacy Fragments (if any) + if( xShape.is() && !maShapeModel.maLegacyDiagramPath.isEmpty() ) + { + Reference< XInputStream > xInStrm( mrDrawing.getFilter().openInputStream( maShapeModel.maLegacyDiagramPath ), UNO_SET_THROW ); + if( xInStrm.is() ) + PropertySet( xShape ).setProperty( PROP_LegacyFragment, xInStrm ); + } + + PropertySet aPropertySet(xShape); + if (xShape.is()) + { + if (oRotation) + { + aPropertySet.setAnyProperty(PROP_RotateAngle, makeAny(*oRotation)); + uno::Reference<lang::XServiceInfo> xServiceInfo(rxShapes, uno::UNO_QUERY); + if (!xServiceInfo->supportsService("com.sun.star.drawing.GroupShape")) + { + // If rotation is used, simple setPosition() is not enough. + aPropertySet.setAnyProperty(PROP_HoriOrientPosition, makeAny(aShapeRect.X)); + aPropertySet.setAnyProperty(PROP_VertOrientPosition, makeAny(aShapeRect.Y)); + } + } + + // custom shape geometry attributes + std::vector<css::beans::PropertyValue> aPropVec; + + // When flip has 'x' or 'y', the associated ShapeRect will be changed but direction change doesn't occur. + // It might occur internally in SdrObject of "sw" module, not here. + // The associated properties "PROP_MirroredX" and "PROP_MirroredY" have to be set here so that direction change will occur internally. + if (bFlipX) + aPropVec.push_back(comphelper::makePropertyValue("MirroredX", true)); + if (bFlipY) + aPropVec.push_back(comphelper::makePropertyValue("MirroredY", true)); + + if (!maTypeModel.maAdjustments.isEmpty()) + { + std::vector<drawing::EnhancedCustomShapeAdjustmentValue> aAdjustmentValues; + sal_Int32 nIndex = 0; + do + { + OUString aToken = maTypeModel.maAdjustments.getToken(0, ',', nIndex); + drawing::EnhancedCustomShapeAdjustmentValue aAdjustmentValue; + if (aToken.isEmpty()) + aAdjustmentValue.State = css::beans::PropertyState::PropertyState_DEFAULT_VALUE; + else + aAdjustmentValue.Value <<= aToken.toInt32(); + aAdjustmentValues.push_back(aAdjustmentValue); + } while (nIndex >= 0); + + css::beans::PropertyValue aProp; + aProp.Name = "AdjustmentValues"; + aProp.Value <<= comphelper::containerToSequence(aAdjustmentValues); + aPropVec.push_back(aProp); + } + + if (!aPropVec.empty()) + aPropertySet.setAnyProperty(PROP_CustomShapeGeometry, makeAny(comphelper::containerToSequence(aPropVec))); + } + + lcl_SetAnchorType(aPropertySet, maTypeModel, rGraphicHelper ); + + return xShape; +} + +Reference< XShape > SimpleShape::createEmbeddedPictureObject( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, OUString const & rGraphicPath ) const +{ + Reference<XGraphic> xGraphic = mrDrawing.getFilter().getGraphicHelper().importEmbeddedGraphic(rGraphicPath); + return SimpleShape::createPictureObject(rxShapes, rShapeRect, xGraphic); +} + +Reference< XShape > SimpleShape::createPictureObject(const Reference< XShapes >& rxShapes, + const awt::Rectangle& rShapeRect, + uno::Reference<graphic::XGraphic> const & rxGraphic) const +{ + Reference< XShape > xShape = mrDrawing.createAndInsertXShape( "com.sun.star.drawing.GraphicObjectShape", rxShapes, rShapeRect ); + if( xShape.is() ) + { + PropertySet aPropSet(xShape); + if (rxGraphic.is()) + { + aPropSet.setProperty(PROP_Graphic, rxGraphic); + } + uno::Reference< lang::XServiceInfo > xServiceInfo(rxShapes, uno::UNO_QUERY); + // If the shape has an absolute position, set the properties accordingly, unless we're inside a group shape. + if ( maTypeModel.maPosition == "absolute" && !xServiceInfo->supportsService("com.sun.star.drawing.GroupShape")) + { + aPropSet.setProperty(PROP_HoriOrientPosition, rShapeRect.X); + aPropSet.setProperty(PROP_VertOrientPosition, rShapeRect.Y); + aPropSet.setProperty(PROP_Opaque, false); + } + // fdo#70457: preserve rotation information + if ( !maTypeModel.maRotation.isEmpty() ) + aPropSet.setAnyProperty(PROP_RotateAngle, makeAny(ConversionHelper::decodeRotation(maTypeModel.maRotation))); + + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + lcl_SetAnchorType(aPropSet, maTypeModel, rGraphicHelper); + + if (maTypeModel.moCropBottom.has() || maTypeModel.moCropLeft.has() || maTypeModel.moCropRight.has() || maTypeModel.moCropTop.has()) + { + text::GraphicCrop aGraphicCrop; + awt::Size aOriginalSize = rGraphicHelper.getOriginalSize(rxGraphic); + + if (maTypeModel.moCropBottom.has()) + aGraphicCrop.Bottom = lclConvertCrop(maTypeModel.moCropBottom.get(), aOriginalSize.Height); + if (maTypeModel.moCropLeft.has()) + aGraphicCrop.Left = lclConvertCrop(maTypeModel.moCropLeft.get(), aOriginalSize.Width); + if (maTypeModel.moCropRight.has()) + aGraphicCrop.Right = lclConvertCrop(maTypeModel.moCropRight.get(), aOriginalSize.Width); + if (maTypeModel.moCropTop.has()) + aGraphicCrop.Top = lclConvertCrop(maTypeModel.moCropTop.get(), aOriginalSize.Height); + + aPropSet.setProperty(PROP_GraphicCrop, aGraphicCrop); + } + } + return xShape; +} + +RectangleShape::RectangleShape( Drawing& rDrawing ) : + SimpleShape( rDrawing, "com.sun.star.drawing.RectangleShape" ) +{ +} + +Reference<XShape> RectangleShape::implConvertAndInsert(const Reference<XShapes>& rxShapes, const awt::Rectangle& rShapeRect) const +{ + OUString aGraphicPath = getGraphicPath(); + + // try to create a picture object + if(!aGraphicPath.isEmpty()) + return SimpleShape::createEmbeddedPictureObject(rxShapes, rShapeRect, aGraphicPath); + + // default: try to create a rectangle shape + Reference<XShape> xShape = SimpleShape::implConvertAndInsert(rxShapes, rShapeRect); + OUString sArcsize = maTypeModel.maArcsize; + if ( !sArcsize.isEmpty( ) ) + { + sal_Unicode cLastChar = sArcsize[sArcsize.getLength() - 1]; + sal_Int32 nValue = sArcsize.copy( 0, sArcsize.getLength() - 1 ).toInt32( ); + // Get the smallest half-side + double size = std::min( rShapeRect.Height, rShapeRect.Width ) / 2.0; + sal_Int32 nRadius = 0; + if ( cLastChar == 'f' ) + nRadius = size * nValue / 65536; + else if ( cLastChar == '%' ) + nRadius = size * nValue / 100; + PropertySet( xShape ).setAnyProperty( PROP_CornerRadius, makeAny( nRadius ) ); + } + return xShape; +} + +EllipseShape::EllipseShape( Drawing& rDrawing ) : + SimpleShape( rDrawing, "com.sun.star.drawing.EllipseShape" ) +{ +} + +PolyLineShape::PolyLineShape( Drawing& rDrawing ) : + SimpleShape( rDrawing, "com.sun.star.drawing.PolyLineShape" ) +{ +} + +Reference< XShape > PolyLineShape::implConvertAndInsert( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + Reference< XShape > xShape = SimpleShape::implConvertAndInsert( rxShapes, rShapeRect ); + // polygon path + awt::Rectangle aCoordSys = getCoordSystem(); + if( !maShapeModel.maPoints.empty() && (aCoordSys.Width > 0) && (aCoordSys.Height > 0) ) + { + ::std::vector< awt::Point > aAbsPoints; + for (auto const& point : maShapeModel.maPoints) + aAbsPoints.push_back( lclGetAbsPoint( point, rShapeRect, aCoordSys ) ); + PointSequenceSequence aPointSeq( 1 ); + aPointSeq[ 0 ] = ContainerHelper::vectorToSequence( aAbsPoints ); + PropertySet aPropSet( xShape ); + aPropSet.setProperty( PROP_PolyPolygon, aPointSeq ); + } + return xShape; +} + +LineShape::LineShape(Drawing& rDrawing) + : SimpleShape(rDrawing, "com.sun.star.drawing.LineShape") +{ +} + +awt::Rectangle LineShape::getAbsRectangle() const +{ + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + awt::Rectangle aShapeRect; + sal_Int32 nIndex = 0; + + aShapeRect.X = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maShapeModel.maFrom.getToken(0, ',', nIndex), 0, true, true); + aShapeRect.Y = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maShapeModel.maFrom.getToken(0, ',', nIndex), 0, false, true); + nIndex = 0; + aShapeRect.Width = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maShapeModel.maTo.getToken(0, ',', nIndex), 0, true, true) - aShapeRect.X; + aShapeRect.Height = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, maShapeModel.maTo.getToken(0, ',', nIndex), 0, false, true) - aShapeRect.Y; + return aShapeRect; +} + +awt::Rectangle LineShape::getRelRectangle() const +{ + awt::Rectangle aShapeRect; + sal_Int32 nIndex = 0; + + aShapeRect.X = maShapeModel.maFrom.getToken(0, ',', nIndex).toInt32(); + aShapeRect.Y = maShapeModel.maFrom.getToken(0, ',', nIndex).toInt32(); + nIndex = 0; + aShapeRect.Width = maShapeModel.maTo.getToken(0, ',', nIndex).toInt32() - aShapeRect.X; + aShapeRect.Height = maShapeModel.maTo.getToken(0, ',', nIndex).toInt32() - aShapeRect.Y; + return aShapeRect; +} + +BezierShape::BezierShape(Drawing& rDrawing) + : SimpleShape(rDrawing, "com.sun.star.drawing.OpenBezierShape") +{ +} + +Reference< XShape > BezierShape::implConvertAndInsert( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + // If we have an 'x' in the last part of the path it means it is closed... + sal_Int32 nPos = maShapeModel.maVmlPath.lastIndexOf(','); + if ( nPos != -1 && maShapeModel.maVmlPath.indexOf('x', nPos) != -1 ) + { + const_cast<BezierShape*>( this )->setService( "com.sun.star.drawing.ClosedBezierShape" ); + } + + awt::Rectangle aCoordSys = getCoordSystem(); + PolyPolygonBezierCoords aBezierCoords; + + if( (aCoordSys.Width > 0) && (aCoordSys.Height > 0) ) + { + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + + // Bezier paths may consist of one or more sub-paths + typedef ::std::vector< ::std::vector< PolygonFlags > > FlagsList; + std::vector< ::std::vector< awt::Point > > aCoordLists; + FlagsList aFlagLists; + sal_Int32 nIndex = 0; + + // Curve defined by to, from, control1 and control2 attributes + if ( maShapeModel.maVmlPath.isEmpty() ) + { + aCoordLists.emplace_back( ); + aFlagLists.emplace_back( ); + + // Start point + aCoordLists[ 0 ].emplace_back( + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maFrom.getToken( 0, ',', nIndex ), 0, true, true ), + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maFrom.getToken( 0, ',', nIndex ), 0, false, true ) ); + // Control point 1 + aCoordLists[ 0 ].emplace_back( + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maControl1.getToken( 0, ',', nIndex ), 0, true, true ), + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maControl1.getToken( 0, ',', nIndex ), 0, false, true ) ); + // Control point 2 + aCoordLists[ 0 ].emplace_back( + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maControl2.getToken( 0, ',', nIndex ), 0, true, true ), + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maControl2.getToken( 0, ',', nIndex ), 0, false, true ) ); + // End point + aCoordLists[ 0 ].emplace_back( + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maTo.getToken( 0, ',', nIndex ), 0, true, true ), + ConversionHelper::decodeMeasureToHmm( rGraphicHelper, maShapeModel.maTo.getToken( 0, ',', nIndex ), 0, false, true ) ); + + // First and last points are normals, points 2 and 4 are controls + aFlagLists[ 0 ].resize( aCoordLists[ 0 ].size(), PolygonFlags_CONTROL ); + aFlagLists[ 0 ][ 0 ] = PolygonFlags_NORMAL; + aFlagLists[ 0 ].back() = PolygonFlags_NORMAL; + } + // Curve defined by path attribute + else + { + // Parse VML path string and convert to absolute coordinates + ConversionHelper::decodeVmlPath( aCoordLists, aFlagLists, maShapeModel.maVmlPath ); + + for (auto & coordList : aCoordLists) + for (auto & point : coordList) + { + point = lclGetAbsPoint( point, rShapeRect, aCoordSys ); + } + } + + aBezierCoords.Coordinates.realloc( aCoordLists.size() ); + for ( size_t i = 0; i < aCoordLists.size(); i++ ) + aBezierCoords.Coordinates[i] = ContainerHelper::vectorToSequence( aCoordLists[i] ); + + aBezierCoords.Flags.realloc( aFlagLists.size() ); + for ( size_t i = 0; i < aFlagLists.size(); i++ ) + aBezierCoords.Flags[i] = ContainerHelper::vectorToSequence( aFlagLists[i] ); + + if( !aCoordLists.front().empty() && !aCoordLists.back().empty() + && aCoordLists.front().front().X == aCoordLists.back().back().X + && aCoordLists.front().front().Y == aCoordLists.back().back().Y ) + { // HACK: If the shape is in fact closed, which can be found out only when the path is known, + // force to closed bezier shape (otherwise e.g. fill won't work). + const_cast< BezierShape* >( this )->setService( "com.sun.star.drawing.ClosedBezierShape" ); + } + } + + Reference< XShape > xShape = SimpleShape::implConvertAndInsert( rxShapes, rShapeRect ); + + if( aBezierCoords.Coordinates.hasElements()) + { + PropertySet aPropSet( xShape ); + aPropSet.setProperty( PROP_PolyPolygonBezier, aBezierCoords ); + } + + // tdf#105875 handle rotation + // Note: must rotate before flip! + if (!maTypeModel.maRotation.isEmpty()) + { + if (SdrObject* pShape = GetSdrObjectFromXShape(xShape)) + { + // -1 is required because the direction of MSO rotation is the opposite of ours + // 100 is required because in this part of the code the angle is in a hundredth of + // degrees. + auto nAngle = -1 * 100.0 * maTypeModel.maRotation.toDouble(); + double nHRad = nAngle * F_PI18000; + pShape->NbcRotate(pShape->GetSnapRect().Center(), nAngle, sin(nHRad), cos(nHRad)); + } + } + + // Handle horizontal and vertical flip. + if (!maTypeModel.maFlip.isEmpty()) + { + if (SdrObject* pShape = GetSdrObjectFromXShape(xShape)) + { + if (maTypeModel.maFlip.startsWith("x")) + { + Point aCenter(pShape->GetSnapRect().Center()); + Point aPoint2(aCenter); + aPoint2.setY(aPoint2.getY() + 1); + pShape->NbcMirror(aCenter, aPoint2); + } + if (maTypeModel.maFlip.endsWith("y")) + { + Point aCenter(pShape->GetSnapRect().Center()); + Point aPoint2(aCenter); + aPoint2.setX(aPoint2.getX() + 1); + pShape->NbcMirror(aCenter, aPoint2); + } + } + } + + return xShape; +} + +CustomShape::CustomShape( Drawing& rDrawing ) : + SimpleShape( rDrawing, "com.sun.star.drawing.CustomShape" ) +{ +} + +Reference< XShape > CustomShape::implConvertAndInsert( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + // try to create a custom shape + Reference< XShape > xShape = SimpleShape::implConvertAndInsert( rxShapes, rShapeRect ); + if( xShape.is() ) try + { + // create the custom shape geometry + Reference< XEnhancedCustomShapeDefaulter > xDefaulter( xShape, UNO_QUERY_THROW ); + xDefaulter->createCustomShapeDefaults( OUString::number( getShapeType() ) ); + // convert common properties + convertShapeProperties( xShape ); + } + catch( Exception& ) + { + } + return xShape; +} + +ComplexShape::ComplexShape( Drawing& rDrawing ) : + CustomShape( rDrawing ) +{ +} + +Reference< XShape > ComplexShape::implConvertAndInsert( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + XmlFilterBase& rFilter = mrDrawing.getFilter(); + sal_Int32 nShapeType = getShapeType(); + OUString aGraphicPath = getGraphicPath(); + + // try to find registered OLE object info + if( const OleObjectInfo* pOleObjectInfo = mrDrawing.getOleObjectInfo( maTypeModel.maShapeId ) ) + { + SAL_WARN_IF( + nShapeType != VML_SHAPETYPE_PICTUREFRAME, "oox", + "ComplexShape::implConvertAndInsert - unexpected shape type"); + + // if OLE object is embedded into a DrawingML shape (PPTX), do not create it here + if( pOleObjectInfo->mbDmlShape ) + return Reference< XShape >(); + + PropertyMap aOleProps; + awt::Size aOleSize( rShapeRect.Width, rShapeRect.Height ); + if( rFilter.getOleObjectHelper().importOleObject( aOleProps, *pOleObjectInfo, aOleSize ) ) + { + Reference< XShape > xShape = mrDrawing.createAndInsertXShape( "com.sun.star.drawing.OLE2Shape", rxShapes, rShapeRect ); + if( xShape.is() ) + { + // set the replacement graphic + if( !aGraphicPath.isEmpty() ) + { + WmfExternal aExtHeader; + aExtHeader.mapMode = 8; + aExtHeader.xExt = rShapeRect.Width; + aExtHeader.yExt = rShapeRect.Height; + + Reference< XGraphic > xGraphic = rFilter.getGraphicHelper().importEmbeddedGraphic(aGraphicPath, &aExtHeader); + if (xGraphic.is()) + aOleProps.setProperty( PROP_Graphic, xGraphic); + } + + PropertySet aPropSet( xShape ); + aPropSet.setProperties( aOleProps ); + + return xShape; + } + } + } + + // try to find registered form control info + const ControlInfo* pControlInfo = mrDrawing.getControlInfo( maTypeModel.maShapeId ); + if( pControlInfo && !pControlInfo->maFragmentPath.isEmpty() ) + { + if( !pControlInfo->maName.isEmpty() ) + { + // load the control properties from fragment + ::oox::ole::EmbeddedControl aControl(pControlInfo->maName); + if( rFilter.importFragment( new ::oox::ole::AxControlFragment( rFilter, pControlInfo->maFragmentPath, aControl ) ) ) + { + // create and return the control shape (including control model) + sal_Int32 nCtrlIndex = -1; + Reference< XShape > xShape = mrDrawing.createAndInsertXControlShape( aControl, rxShapes, rShapeRect, nCtrlIndex ); + + if (pControlInfo->mbTextContentShape) + { + PropertySet aPropertySet(xShape); + lcl_SetAnchorType(aPropertySet, maTypeModel, mrDrawing.getFilter().getGraphicHelper()); + } + // on error, proceed and try to create picture from replacement image + if( xShape.is() ) + return xShape; + } + } + } + + // host application wants to create the shape (do not try failed OLE controls again) + if( (nShapeType == VML_SHAPETYPE_HOSTCONTROL) && !pControlInfo ) + { + OSL_ENSURE( getClientData(), "ComplexShape::implConvertAndInsert - missing client data" ); + Reference< XShape > xShape = mrDrawing.createAndInsertClientXShape( *this, rxShapes, rShapeRect ); + if( xShape.is() ) + return xShape; + } + + + if( getShapeModel().mbIsSignatureLine ) + { + uno::Reference<graphic::XGraphic> xGraphic; + bool bIsSigned(false); + try + { + // Get the document signatures + Reference<security::XDocumentDigitalSignatures> xSignatures( + security::DocumentDigitalSignatures::createDefault( + comphelper::getProcessComponentContext())); + + uno::Reference<embed::XStorage> xStorage + = comphelper::OStorageHelper::GetStorageOfFormatFromURL( + ZIP_STORAGE_FORMAT_STRING, mrDrawing.getFilter().getFileUrl(), + embed::ElementModes::READ); + SAL_WARN_IF(!xStorage.is(), "oox.vml", "No xStorage!"); + + const uno::Sequence<security::DocumentSignatureInformation> xSignatureInfo + = xSignatures->verifyScriptingContentSignatures(xStorage, + uno::Reference<io::XInputStream>()); + + // Try to find matching signature line image - if none exists that is fine, + // then the signature line is not digitally signed. + auto pSignInfo = std::find_if(xSignatureInfo.begin(), xSignatureInfo.end(), + [this](const security::DocumentSignatureInformation& rSigInfo) { + return rSigInfo.SignatureLineId == getShapeModel().maSignatureId; }); + if (pSignInfo != xSignatureInfo.end()) + { + bIsSigned = true; + if (pSignInfo->SignatureIsValid) + { + // Signature is valid, use the 'valid' image + SAL_WARN_IF(!pSignInfo->ValidSignatureLineImage.is(), "oox.vml", + "No ValidSignatureLineImage!"); + xGraphic = pSignInfo->ValidSignatureLineImage; + } + else + { + // Signature is invalid, use the 'invalid' image + SAL_WARN_IF(!pSignInfo->InvalidSignatureLineImage.is(), "oox.vml", + "No InvalidSignatureLineImage!"); + xGraphic = pSignInfo->InvalidSignatureLineImage; + } + } + } + catch (css::uno::Exception&) + { + // DocumentDigitalSignatures service not available. + // We continue by rendering the "unsigned" shape instead. + } + + Reference< XShape > xShape; + if (xGraphic.is()) + { + // If available, use the signed image from the signature + xShape = SimpleShape::createPictureObject(rxShapes, rShapeRect, xGraphic); + } + else + { + // Create shape with the fallback "unsigned" image + xShape = SimpleShape::createEmbeddedPictureObject(rxShapes, rShapeRect, aGraphicPath); + } + + // Store signature line properties + uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY); + xPropertySet->setPropertyValue("IsSignatureLine", uno::makeAny(true)); + xPropertySet->setPropertyValue("SignatureLineId", + uno::makeAny(getShapeModel().maSignatureId)); + xPropertySet->setPropertyValue( + "SignatureLineSuggestedSignerName", + uno::makeAny(getShapeModel().maSignatureLineSuggestedSignerName)); + xPropertySet->setPropertyValue( + "SignatureLineSuggestedSignerTitle", + uno::makeAny(getShapeModel().maSignatureLineSuggestedSignerTitle)); + xPropertySet->setPropertyValue( + "SignatureLineSuggestedSignerEmail", + uno::makeAny(getShapeModel().maSignatureLineSuggestedSignerEmail)); + xPropertySet->setPropertyValue( + "SignatureLineSigningInstructions", + uno::makeAny(getShapeModel().maSignatureLineSigningInstructions)); + xPropertySet->setPropertyValue( + "SignatureLineShowSignDate", + uno::makeAny(getShapeModel().mbSignatureLineShowSignDate)); + xPropertySet->setPropertyValue( + "SignatureLineCanAddComment", + uno::makeAny(getShapeModel().mbSignatureLineCanAddComment)); + xPropertySet->setPropertyValue("SignatureLineIsSigned", uno::makeAny(bIsSigned)); + + if (!aGraphicPath.isEmpty()) + { + xGraphic = rFilter.getGraphicHelper().importEmbeddedGraphic(aGraphicPath); + xPropertySet->setPropertyValue("SignatureLineUnsignedImage", uno::makeAny(xGraphic)); + } + return xShape; + } + + // try to create a picture object + if( !aGraphicPath.isEmpty() ) + { + Reference<XShape> xShape = SimpleShape::createEmbeddedPictureObject(rxShapes, rShapeRect, aGraphicPath); + // AS_CHARACTER shape: vertical orientation default is bottom, MSO default is top. + if ( maTypeModel.maPosition != "absolute" && maTypeModel.maPosition != "relative" ) + PropertySet( xShape ).setAnyProperty( PROP_VertOrient, makeAny(text::VertOrientation::TOP)); + + // Apply stroke props from the type model. + oox::drawingml::ShapePropertyMap aPropMap(mrDrawing.getFilter().getModelObjectHelper()); + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + maTypeModel.maStrokeModel.pushToPropMap(aPropMap, rGraphicHelper); + PropertySet(xShape).setProperties(aPropMap); + + return xShape; + } + + // default: try to create a custom shape + return CustomShape::implConvertAndInsert( rxShapes, rShapeRect ); +} + +GroupShape::GroupShape( Drawing& rDrawing ) : + ShapeBase( rDrawing ), + mxChildren( new ShapeContainer( rDrawing ) ) +{ +} + +GroupShape::~GroupShape() +{ +} + +void GroupShape::finalizeFragmentImport() +{ + // basic shape processing + ShapeBase::finalizeFragmentImport(); + // finalize all child shapes + mxChildren->finalizeFragmentImport(); +} + +const ShapeType* GroupShape::getChildTypeById( const OUString& rShapeId ) const +{ + return mxChildren->getShapeTypeById( rShapeId ); +} + +const ShapeBase* GroupShape::getChildById( const OUString& rShapeId ) const +{ + return mxChildren->getShapeById( rShapeId ); +} + +Reference< XShape > GroupShape::implConvertAndInsert( const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const +{ + Reference< XShape > xGroupShape; + // check that this shape contains children and a valid coordinate system + ShapeParentAnchor aParentAnchor; + aParentAnchor.maShapeRect = rShapeRect; + aParentAnchor.maCoordSys = getCoordSystem(); + if( !mxChildren->empty() && (aParentAnchor.maCoordSys.Width > 0) && (aParentAnchor.maCoordSys.Height > 0) ) try + { + xGroupShape = mrDrawing.createAndInsertXShape( "com.sun.star.drawing.GroupShape", rxShapes, rShapeRect ); + Reference< XShapes > xChildShapes( xGroupShape, UNO_QUERY_THROW ); + mxChildren->convertAndInsert( xChildShapes, &aParentAnchor ); + if( !xChildShapes->hasElements() ) + { + SAL_WARN("oox", "no child shape has been created - deleting the group shape"); + rxShapes->remove( xGroupShape ); + xGroupShape.clear(); + } + } + catch( Exception& ) + { + } + + uno::Reference<beans::XPropertySet> xPropertySet; + if (!maTypeModel.maEditAs.isEmpty()) + xPropertySet = uno::Reference<beans::XPropertySet>(xGroupShape, uno::UNO_QUERY); + if (xPropertySet.is()) + { + uno::Sequence<beans::PropertyValue> aGrabBag; + xPropertySet->getPropertyValue("InteropGrabBag") >>= aGrabBag; + beans::PropertyValue aPair; + aPair.Name = "mso-edit-as"; + aPair.Value <<= maTypeModel.maEditAs; + sal_Int32 nLength = aGrabBag.getLength(); + aGrabBag.realloc(nLength + 1); + aGrabBag[nLength] = aPair; + xPropertySet->setPropertyValue("InteropGrabBag", uno::makeAny(aGrabBag)); + } + // Make sure group shapes are inline as well, unless there is an explicit different style. + PropertySet aPropertySet(xGroupShape); + const GraphicHelper& rGraphicHelper = mrDrawing.getFilter().getGraphicHelper(); + lcl_SetAnchorType(aPropertySet, maTypeModel, rGraphicHelper); + if (!maTypeModel.maRotation.isEmpty()) + aPropertySet.setAnyProperty(PROP_RotateAngle, makeAny(ConversionHelper::decodeRotation(maTypeModel.maRotation))); + return xGroupShape; +} + +} // namespace oox + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmlshapecontainer.cxx b/oox/source/vml/vmlshapecontainer.cxx new file mode 100644 index 000000000..d9aa7d9aa --- /dev/null +++ b/oox/source/vml/vmlshapecontainer.cxx @@ -0,0 +1,138 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/vml/vmlshapecontainer.hxx> + +#include <oox/vml/vmldrawing.hxx> +#include <oox/vml/vmlshape.hxx> + +#include <osl/diagnose.h> + +namespace oox::vml { + +using namespace ::com::sun::star::awt; +using namespace ::com::sun::star::drawing; +using namespace ::com::sun::star::uno; + +namespace { + +template< typename ShapeType > +void lclMapShapesById( RefMap< OUString, ShapeType >& orMap, const RefVector< ShapeType >& rVector ) +{ + for (auto const& elem : rVector) + { + const OUString& rShapeId = elem->getShapeId(); + OSL_ENSURE( !rShapeId.isEmpty(), "lclMapShapesById - missing shape identifier" ); + if( !rShapeId.isEmpty() ) + { + OSL_ENSURE( orMap.find( rShapeId ) == orMap.end(), "lclMapShapesById - shape identifier already used " ); + orMap[ rShapeId ] = elem; + } + } +} + +} // namespace + +ShapeContainer::ShapeContainer( Drawing& rDrawing ) : + mrDrawing( rDrawing ) +{ +} + +ShapeContainer::~ShapeContainer() +{ +} + +std::shared_ptr<ShapeType> ShapeContainer::createShapeType() +{ + auto xShape = std::make_shared<ShapeType>( mrDrawing ); + maTypes.push_back( xShape ); + return xShape; +} + +void ShapeContainer::finalizeFragmentImport() +{ + // map all shape templates by shape identifier + lclMapShapesById( maTypesById, maTypes ); + // map all shapes by shape identifier + lclMapShapesById( maShapesById, maShapes ); + /* process all shapes (map all children templates/shapes in group shapes, + resolve template references in all shapes) */ + maShapes.forEachMem( &ShapeBase::finalizeFragmentImport ); +} + +const ShapeType* ShapeContainer::getShapeTypeById( const OUString& rShapeId ) const +{ + if (maTypesById.empty() && !maTypes.empty()) + { + lclMapShapesById(const_cast<ShapeTypeMap&>(maTypesById), maTypes); + } + + // search in own shape template list + if( const ShapeType* pType = maTypesById.get( rShapeId ).get() ) + return pType; + // search deep in child shapes + for (auto const& shape : maShapes) + if( const ShapeType* pType = shape->getChildTypeById( rShapeId ) ) + return pType; + return nullptr; +} + +const ShapeBase* ShapeContainer::getShapeById( const OUString& rShapeId ) const +{ + // search in own shape list + if( const ShapeBase* pShape = maShapesById.get( rShapeId ).get() ) + return pShape; + // search deep in child shapes + for (auto const& shape : maShapes) + if( const ShapeBase* pShape = shape->getChildById( rShapeId ) ) + return pShape; + return nullptr; +} + +std::shared_ptr< ShapeBase > ShapeContainer::takeLastShape() +{ + OSL_ENSURE( mrDrawing.getType() == VMLDRAWING_WORD, "ShapeContainer::takeLastShape - illegal call, Word filter only" ); + assert( !markStack.empty()); + if( markStack.top() >= maShapes.size()) + return std::shared_ptr< ShapeBase >(); + std::shared_ptr< ShapeBase > ret = maShapes.back(); + maShapes.pop_back(); + return ret; +} + +void ShapeContainer::pushMark() +{ + markStack.push( maShapes.size()); +} + +void ShapeContainer::popMark() +{ + assert( !markStack.empty()); + markStack.pop(); +} + +void ShapeContainer::convertAndInsert( const Reference< XShapes >& rxShapes, const ShapeParentAnchor* pParentAnchor ) const +{ + for (auto const& shape : maShapes) + shape->convertAndInsert( rxShapes, pParentAnchor ); +} + +} // namespace oox::vml + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmlshapecontext.cxx b/oox/source/vml/vmlshapecontext.cxx new file mode 100644 index 000000000..178d7c21f --- /dev/null +++ b/oox/source/vml/vmlshapecontext.cxx @@ -0,0 +1,632 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/vml/vmlshapecontext.hxx> + +#include <oox/core/xmlfilterbase.hxx> +#include <oox/helper/attributelist.hxx> +#include <oox/helper/helper.hxx> +#include <oox/token/namespaces.hxx> +#include <oox/token/tokens.hxx> +#include <oox/vml/vmldrawing.hxx> +#include <oox/vml/vmlshape.hxx> +#include <oox/vml/vmlshapecontainer.hxx> +#include <oox/vml/vmltextboxcontext.hxx> + +#include <osl/diagnose.h> +#include <filter/msfilter/escherex.hxx> + +namespace oox::vml { + +using namespace ::com::sun::star; + +using ::oox::core::ContextHandler2; +using ::oox::core::ContextHandler2Helper; +using ::oox::core::ContextHandlerRef; + +namespace { + +/** Returns the boolean value from the specified VML attribute (if present). + */ +OptValue< bool > lclDecodeBool( const AttributeList& rAttribs, sal_Int32 nToken ) +{ + OptValue< OUString > oValue = rAttribs.getString( nToken ); + if( oValue.has() ) return OptValue< bool >( ConversionHelper::decodeBool( oValue.get() ) ); + return OptValue< bool >(); +} + +/** Returns the percentage value from the specified VML attribute (if present). + The value will be normalized (1.0 is returned for 100%). + */ +OptValue< double > lclDecodePercent( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue ) +{ + OptValue< OUString > oValue = rAttribs.getString( nToken ); + if( oValue.has() ) return OptValue< double >( ConversionHelper::decodePercent( oValue.get(), fDefValue ) ); + return OptValue< double >(); +} + +/** #119750# Special method for opacity; it *should* be a percentage value, but there are cases + where a value relative to 0xffff (65536) is used, ending with an 'f' + */ +OptValue< double > lclDecodeOpacity( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue ) +{ + OptValue< OUString > oValue = rAttribs.getString( nToken ); + double fRetval(fDefValue); + + if( oValue.has() ) + { + const OUString& aString(oValue.get()); + const sal_Int32 nLength(aString.getLength()); + + if(nLength > 0) + { + if(aString.endsWith("f")) + { + fRetval = std::max(0.0, std::min(1.0, aString.toDouble() / 65536.0)); + } + else + { + fRetval = ConversionHelper::decodePercent( aString, fDefValue ); + } + } + } + + return OptValue< double >(fRetval); +} + +/** Returns the integer value pair from the specified VML attribute (if present). + */ +OptValue< Int32Pair > lclDecodeInt32Pair( const AttributeList& rAttribs, sal_Int32 nToken ) +{ + OptValue< OUString > oValue = rAttribs.getString( nToken ); + OptValue< Int32Pair > oRetValue; + if( oValue.has() ) + { + OUString aValue1, aValue2; + ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' ); + oRetValue = Int32Pair( aValue1.toInt32(), aValue2.toInt32() ); + } + return oRetValue; +} + +/** Returns the percentage pair from the specified VML attribute (if present). + */ +OptValue< DoublePair > lclDecodePercentPair( const AttributeList& rAttribs, sal_Int32 nToken ) +{ + OptValue< OUString > oValue = rAttribs.getString( nToken ); + OptValue< DoublePair > oRetValue; + if( oValue.has() ) + { + OUString aValue1, aValue2; + ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' ); + oRetValue = DoublePair( + ConversionHelper::decodePercent( aValue1, 0.0 ), + ConversionHelper::decodePercent( aValue2, 0.0 ) ); + } + return oRetValue; +} + +/** Returns the boolean value from the passed string of an attribute in the x: + namespace (VML for spreadsheets). Supported values: f, t, False, True. + @param bDefaultForEmpty Default value for the empty string. + */ +bool lclDecodeVmlxBool( const OUString& rValue, bool bDefaultForEmpty ) +{ + if( rValue.isEmpty() ) return bDefaultForEmpty; + sal_Int32 nToken = AttributeConversion::decodeToken( rValue ); + // anything else than 't' or 'True' is considered to be false, as specified + return (nToken == XML_t) || (nToken == XML_True); +} + +} // namespace + +ShapeLayoutContext::ShapeLayoutContext( ContextHandler2Helper const & rParent, Drawing& rDrawing ) : + ContextHandler2( rParent ), + mrDrawing( rDrawing ) +{ +} + +ContextHandlerRef ShapeLayoutContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + switch( nElement ) + { + case O_TOKEN( idmap ): + { + OUString aBlockIds = rAttribs.getString( XML_data, OUString() ); + sal_Int32 nIndex = 0; + while( nIndex >= 0 ) + { + OUString aToken = aBlockIds.getToken( 0, ' ', nIndex ).trim(); + if( !aToken.isEmpty() ) + mrDrawing.registerBlockId( aToken.toInt32() ); + } + } + break; + } + return nullptr; +} + +ClientDataContext::ClientDataContext( ContextHandler2Helper const & rParent, + ClientData& rClientData, const AttributeList& rAttribs ) : + ContextHandler2( rParent ), + mrClientData( rClientData ) +{ + mrClientData.mnObjType = rAttribs.getToken( XML_ObjectType, XML_TOKEN_INVALID ); +} + +ContextHandlerRef ClientDataContext::onCreateContext( sal_Int32 /*nElement*/, const AttributeList& /*rAttribs*/ ) +{ + if( isRootElement() ) + { + maElementText.clear(); + return this; + } + return nullptr; +} + +void ClientDataContext::onCharacters( const OUString& rChars ) +{ + /* Empty but existing elements have special meaning, e.g. 'true'. Collect + existing text and convert it in onEndElement(). */ + maElementText = rChars; +} + +void ClientDataContext::onEndElement() +{ + switch( getCurrentElement() ) + { + case VMLX_TOKEN( Anchor ): mrClientData.maAnchor = maElementText; break; + case VMLX_TOKEN( FmlaMacro ): mrClientData.maFmlaMacro = maElementText; break; + case VMLX_TOKEN( FmlaPict ): mrClientData.maFmlaPict = maElementText; break; + case VMLX_TOKEN( FmlaLink ): mrClientData.maFmlaLink = maElementText; break; + case VMLX_TOKEN( FmlaRange ): mrClientData.maFmlaRange = maElementText; break; + case VMLX_TOKEN( FmlaGroup ): mrClientData.maFmlaGroup = maElementText; break; + case VMLX_TOKEN( TextHAlign ): mrClientData.mnTextHAlign = AttributeConversion::decodeToken( maElementText ); break; + case VMLX_TOKEN( TextVAlign ): mrClientData.mnTextVAlign = AttributeConversion::decodeToken( maElementText ); break; + case VMLX_TOKEN( Column ): mrClientData.mnCol = maElementText.toInt32(); break; + case VMLX_TOKEN( Row ): mrClientData.mnRow = maElementText.toInt32(); break; + case VMLX_TOKEN( Checked ): mrClientData.mnChecked = maElementText.toInt32(); break; + case VMLX_TOKEN( DropStyle ): mrClientData.mnDropStyle = AttributeConversion::decodeToken( maElementText ); break; + case VMLX_TOKEN( DropLines ): mrClientData.mnDropLines = maElementText.toInt32(); break; + case VMLX_TOKEN( Val ): mrClientData.mnVal = maElementText.toInt32(); break; + case VMLX_TOKEN( Min ): mrClientData.mnMin = maElementText.toInt32(); break; + case VMLX_TOKEN( Max ): mrClientData.mnMax = maElementText.toInt32(); break; + case VMLX_TOKEN( Inc ): mrClientData.mnInc = maElementText.toInt32(); break; + case VMLX_TOKEN( Page ): mrClientData.mnPage = maElementText.toInt32(); break; + case VMLX_TOKEN( SelType ): mrClientData.mnSelType = AttributeConversion::decodeToken( maElementText ); break; + case VMLX_TOKEN( VTEdit ): mrClientData.mnVTEdit = maElementText.toInt32(); break; + case VMLX_TOKEN( PrintObject ): mrClientData.mbPrintObject = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( Visible ): mrClientData.mbVisible = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( DDE ): mrClientData.mbDde = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( NoThreeD ): mrClientData.mbNo3D = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( NoThreeD2 ): mrClientData.mbNo3D2 = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( MultiLine ): mrClientData.mbMultiLine = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( VScroll ): mrClientData.mbVScroll = lclDecodeVmlxBool( maElementText, true ); break; + case VMLX_TOKEN( SecretEdit ): mrClientData.mbSecretEdit = lclDecodeVmlxBool( maElementText, true ); break; + } +} + +ShapeContextBase::ShapeContextBase( ContextHandler2Helper const & rParent ) : + ContextHandler2( rParent ) +{ +} + +ContextHandlerRef ShapeContextBase::createShapeContext( ContextHandler2Helper const & rParent, + ShapeContainer& rShapes, sal_Int32 nElement, const AttributeList& rAttribs ) +{ + switch( nElement ) + { + case O_TOKEN( shapelayout ): + return new ShapeLayoutContext( rParent, rShapes.getDrawing() ); + + case VML_TOKEN( shapetype ): + return new ShapeTypeContext( rParent, rShapes.createShapeType(), rAttribs ); + case VML_TOKEN( group ): + return new GroupShapeContext( rParent, rShapes.createShape< GroupShape >(), rAttribs ); + case VML_TOKEN( shape ): + if (rAttribs.hasAttribute(XML_path) && + // tdf#122563 skip in the case of empty path + !rAttribs.getString(XML_path, "").isEmpty()) + return new ShapeContext( rParent, rShapes.createShape< BezierShape >(), rAttribs ); + else + return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs ); + case VML_TOKEN( rect ): + return new RectangleShapeContext( rParent, rAttribs, rShapes.createShape< RectangleShape >() ); + case VML_TOKEN( roundrect ): + return new ShapeContext( rParent, rShapes.createShape< RectangleShape >(), rAttribs ); + case VML_TOKEN( oval ): + return new ShapeContext( rParent, rShapes.createShape< EllipseShape >(), rAttribs ); + case VML_TOKEN( polyline ): + return new ShapeContext( rParent, rShapes.createShape< PolyLineShape >(), rAttribs ); + case VML_TOKEN( line ): + return new ShapeContext( rParent, rShapes.createShape< LineShape >(), rAttribs ); + case VML_TOKEN( curve ): + return new ShapeContext( rParent, rShapes.createShape< BezierShape >(), rAttribs ); + + // TODO: + case VML_TOKEN( arc ): + case VML_TOKEN( diagram ): + case VML_TOKEN( image ): + return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs ); + + case W_TOKEN(control): + return new ControlShapeContext( rParent, rShapes, rAttribs ); + } + return nullptr; +} + +ShapeTypeContext::ShapeTypeContext(ContextHandler2Helper const & rParent, + std::shared_ptr<ShapeType> const& pShapeType, + const AttributeList& rAttribs) + : ShapeContextBase(rParent) + , m_pShapeType(pShapeType) // tdf#112311 keep it alive + , mrTypeModel( pShapeType->getTypeModel() ) +{ + // shape identifier and shape name + bool bHasOspid = rAttribs.hasAttribute( O_TOKEN( spid ) ); + mrTypeModel.maShapeId = rAttribs.getXString( bHasOspid ? O_TOKEN( spid ) : XML_id, OUString() ); + mrTypeModel.maLegacyId = rAttribs.getString( XML_id, OUString() ); + OSL_ENSURE( !mrTypeModel.maShapeId.isEmpty(), "ShapeTypeContext::ShapeTypeContext - missing shape identifier" ); + // builtin shape type identifier + mrTypeModel.moShapeType = rAttribs.getInteger( O_TOKEN( spt ) ); + // if the o:spid attribute exists, the id attribute contains the user-defined shape name + if( bHasOspid ) + { + mrTypeModel.maShapeName = rAttribs.getXString( XML_id, OUString() ); + // get ShapeType and ShapeId from name for compatibility + static const OUString sShapeTypePrefix = "shapetype_"; + if( mrTypeModel.maShapeName.startsWith( sShapeTypePrefix ) ) + { + mrTypeModel.maShapeId = mrTypeModel.maShapeName; + mrTypeModel.moShapeType = mrTypeModel.maShapeName.copy(sShapeTypePrefix.getLength()).toInt32(); + } + } + + // coordinate system position/size, CSS style + mrTypeModel.moCoordPos = lclDecodeInt32Pair( rAttribs, XML_coordorigin ); + mrTypeModel.moCoordSize = lclDecodeInt32Pair( rAttribs, XML_coordsize ); + setStyle( rAttribs.getString( XML_style, OUString() ) ); + if( lclDecodeBool( rAttribs, O_TOKEN( hr )).get( false )) + { // MSO's handling of o:hr width is nowhere near what the spec says: + // - o:hrpct is not in % but in 0.1% + // - if o:hrpct is not given, 100% width is assumed + // - given width is used only if explicit o:hrpct="0" is given + OUString hrpct = rAttribs.getString( O_TOKEN( hrpct ), "1000" ); + if( hrpct != "0" ) + mrTypeModel.maWidthPercent = OUString::number( hrpct.toInt32() ); + mrTypeModel.maWrapDistanceLeft = "0"; + mrTypeModel.maWrapDistanceRight = "0"; + mrTypeModel.maPositionHorizontal = rAttribs.getString( O_TOKEN( hralign ), "left" ); + mrTypeModel.moWrapType = "topAndBottom"; + } + + // stroke settings (may be overridden by v:stroke element later) + mrTypeModel.maStrokeModel.moStroked = lclDecodeBool( rAttribs, XML_stroked ); + mrTypeModel.maStrokeModel.moColor = rAttribs.getString( XML_strokecolor ); + mrTypeModel.maStrokeModel.moWeight = rAttribs.getString( XML_strokeweight ); + + // fill settings (may be overridden by v:fill element later) + mrTypeModel.maFillModel.moFilled = lclDecodeBool( rAttribs, XML_filled ); + mrTypeModel.maFillModel.moColor = rAttribs.getString( XML_fillcolor ); + + // For roundrect we may have an arcsize attribute to read + mrTypeModel.maArcsize = rAttribs.getString(XML_arcsize, OUString()); + // editas + mrTypeModel.maEditAs = rAttribs.getString(XML_editas, OUString()); + + mrTypeModel.maAdjustments = rAttribs.getString(XML_adj, OUString()); +} + +ContextHandlerRef ShapeTypeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + if( isRootElement() ) switch( nElement ) + { + case VML_TOKEN( stroke ): + mrTypeModel.maStrokeModel.moStroked.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) ); + mrTypeModel.maStrokeModel.maStartArrow.moArrowType = rAttribs.getToken( XML_startarrow ); + mrTypeModel.maStrokeModel.maStartArrow.moArrowWidth = rAttribs.getToken( XML_startarrowwidth ); + mrTypeModel.maStrokeModel.maStartArrow.moArrowLength = rAttribs.getToken( XML_startarrowlength ); + mrTypeModel.maStrokeModel.maEndArrow.moArrowType = rAttribs.getToken( XML_endarrow ); + mrTypeModel.maStrokeModel.maEndArrow.moArrowWidth = rAttribs.getToken( XML_endarrowwidth ); + mrTypeModel.maStrokeModel.maEndArrow.moArrowLength = rAttribs.getToken( XML_endarrowlength ); + mrTypeModel.maStrokeModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) ); + mrTypeModel.maStrokeModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 ); + mrTypeModel.maStrokeModel.moWeight.assignIfUsed( rAttribs.getString( XML_weight ) ); + mrTypeModel.maStrokeModel.moDashStyle = rAttribs.getString( XML_dashstyle ); + mrTypeModel.maStrokeModel.moLineStyle = rAttribs.getToken( XML_linestyle ); + mrTypeModel.maStrokeModel.moEndCap = rAttribs.getToken( XML_endcap ); + mrTypeModel.maStrokeModel.moJoinStyle = rAttribs.getToken( XML_joinstyle ); + break; + case VML_TOKEN( fill ): + { + // in DOCX shapes use r:id for the relationship id + // in XLSX they use o:relid + bool bHasORelId = rAttribs.hasAttribute( O_TOKEN(relid) ); + mrTypeModel.maFillModel.moFilled.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) ); + mrTypeModel.maFillModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) ); + mrTypeModel.maFillModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 ); + mrTypeModel.maFillModel.moColor2 = rAttribs.getString( XML_color2 ); + mrTypeModel.maFillModel.moOpacity2 = lclDecodeOpacity( rAttribs, XML_opacity2, 1.0 ); + mrTypeModel.maFillModel.moType = rAttribs.getToken( XML_type ); + mrTypeModel.maFillModel.moAngle = rAttribs.getInteger( XML_angle ); + mrTypeModel.maFillModel.moFocus = lclDecodePercent( rAttribs, XML_focus, 0.0 ); + mrTypeModel.maFillModel.moFocusPos = lclDecodePercentPair( rAttribs, XML_focusposition ); + mrTypeModel.maFillModel.moFocusSize = lclDecodePercentPair( rAttribs, XML_focussize ); + mrTypeModel.maFillModel.moBitmapPath = decodeFragmentPath( rAttribs, bHasORelId ? O_TOKEN(relid) : R_TOKEN(id) ); + mrTypeModel.maFillModel.moRotate = lclDecodeBool( rAttribs, XML_rotate ); + break; + } + case VML_TOKEN( imagedata ): + { + // shapes in docx use r:id for the relationship id + // in xlsx it they use o:relid + bool bHasORelId = rAttribs.hasAttribute( O_TOKEN( relid ) ); + mrTypeModel.moGraphicPath = decodeFragmentPath( rAttribs, bHasORelId ? O_TOKEN( relid ) : R_TOKEN( id ) ); + mrTypeModel.moGraphicTitle = rAttribs.getString( O_TOKEN( title ) ); + + // Get crop attributes. + mrTypeModel.moCropBottom = rAttribs.getString(XML_cropbottom); + mrTypeModel.moCropLeft = rAttribs.getString(XML_cropleft); + mrTypeModel.moCropRight = rAttribs.getString(XML_cropright); + mrTypeModel.moCropTop = rAttribs.getString(XML_croptop); + } + break; + case NMSP_vmlWord | XML_wrap: + mrTypeModel.moWrapAnchorX = rAttribs.getString(XML_anchorx); + mrTypeModel.moWrapAnchorY = rAttribs.getString(XML_anchory); + mrTypeModel.moWrapType = rAttribs.getString(XML_type); + mrTypeModel.moWrapSide = rAttribs.getString(XML_side); + break; + case VML_TOKEN( shadow ): + { + mrTypeModel.maShadowModel.mbHasShadow = true; + mrTypeModel.maShadowModel.moShadowOn = lclDecodeBool(rAttribs, XML_on).get(false); + mrTypeModel.maShadowModel.moColor.assignIfUsed(rAttribs.getString(XML_color)); + mrTypeModel.maShadowModel.moOffset.assignIfUsed(rAttribs.getString(XML_offset)); + mrTypeModel.maShadowModel.moOpacity = lclDecodePercent(rAttribs, XML_opacity, 1.0); + } + break; + case VML_TOKEN( textpath ): + mrTypeModel.maTextpathModel.moString.assignIfUsed(rAttribs.getString(XML_string)); + mrTypeModel.maTextpathModel.moStyle.assignIfUsed(rAttribs.getString(XML_style)); + mrTypeModel.maTextpathModel.moTrim.assignIfUsed(lclDecodeBool(rAttribs, XML_trim)); + break; + } + return nullptr; +} + +OptValue< OUString > ShapeTypeContext::decodeFragmentPath( const AttributeList& rAttribs, sal_Int32 nToken ) const +{ + OptValue< OUString > oFragmentPath; + OptValue< OUString > oRelId = rAttribs.getString( nToken ); + if( oRelId.has() ) + oFragmentPath = getFragmentPathFromRelId( oRelId.get() ); + return oFragmentPath; +} + +void ShapeTypeContext::setStyle( const OUString& rStyle ) +{ + sal_Int32 nIndex = 0; + while( nIndex >= 0 ) + { + OUString aName, aValue; + if( ConversionHelper::separatePair( aName, aValue, rStyle.getToken( 0, ';', nIndex ), ':' ) ) + { + if( aName == "position" ) mrTypeModel.maPosition = aValue; + else if( aName == "z-index" ) mrTypeModel.maZIndex = aValue; + else if( aName == "left" ) mrTypeModel.maLeft = aValue; + else if( aName == "top" ) mrTypeModel.maTop = aValue; + else if( aName == "width" ) mrTypeModel.maWidth = aValue; + else if( aName == "height" ) mrTypeModel.maHeight = aValue; + else if( aName == "margin-left" ) mrTypeModel.maMarginLeft = aValue; + else if( aName == "margin-top" ) mrTypeModel.maMarginTop = aValue; + else if( aName == "mso-position-vertical-relative" ) mrTypeModel.maPositionVerticalRelative = aValue; + else if( aName == "mso-position-horizontal-relative" ) mrTypeModel.maPositionHorizontalRelative = aValue; + else if( aName == "mso-position-horizontal" ) mrTypeModel.maPositionHorizontal = aValue; + else if( aName == "mso-position-vertical" ) mrTypeModel.maPositionVertical = aValue; + else if( aName == "mso-width-percent" ) mrTypeModel.maWidthPercent = aValue; + else if( aName == "mso-width-relative" ) mrTypeModel.maWidthRelative = aValue; + else if( aName == "mso-height-percent" ) mrTypeModel.maHeightPercent = aValue; + else if( aName == "mso-height-relative" ) mrTypeModel.maHeightRelative = aValue; + else if( aName == "mso-fit-shape-to-text" ) mrTypeModel.mbAutoHeight = true; + else if( aName == "rotation" ) mrTypeModel.maRotation = aValue; + else if( aName == "flip" ) mrTypeModel.maFlip = aValue; + else if( aName == "visibility" ) + mrTypeModel.mbVisible = aValue != "hidden"; + else if( aName == "mso-wrap-style" ) mrTypeModel.maWrapStyle = aValue; + else if ( aName == "v-text-anchor" ) mrTypeModel.maVTextAnchor = aValue; + else if ( aName == "mso-wrap-distance-left" ) mrTypeModel.maWrapDistanceLeft = aValue; + else if ( aName == "mso-wrap-distance-right" ) mrTypeModel.maWrapDistanceRight = aValue; + else if ( aName == "mso-wrap-distance-top" ) mrTypeModel.maWrapDistanceTop = aValue; + else if ( aName == "mso-wrap-distance-bottom" ) mrTypeModel.maWrapDistanceBottom = aValue; + } + } +} + +ShapeContext::ShapeContext(ContextHandler2Helper const& rParent, + const std::shared_ptr<ShapeBase>& pShape, const AttributeList& rAttribs) + : ShapeTypeContext(rParent, pShape, rAttribs) + , mrShape(*pShape) + , mrShapeModel(pShape->getShapeModel()) +{ + // collect shape specific attributes + mrShapeModel.maType = rAttribs.getXString( XML_type, OUString() ); + // polyline path + setPoints( rAttribs.getString( XML_points, OUString() ) ); + // line start and end positions + setFrom(rAttribs.getString(XML_from, OUString())); + setTo(rAttribs.getString(XML_to, OUString())); + setControl1(rAttribs.getString(XML_control1, OUString())); + setControl2(rAttribs.getString(XML_control2, OUString())); + setVmlPath(rAttribs.getString(XML_path, OUString())); +} + +ContextHandlerRef ShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + // Excel specific shape client data + if( isRootElement() ) switch( nElement ) + { + case VML_TOKEN( textbox ): + { + // Calculate the shape type: map both <rect> and <v:shape> with a textbox shape type to + // a TextShape. + sal_Int32 nShapeType = 0; + if (ShapeContainer* pShapeContainer = mrShape.getContainer()) + { + OUString aType = mrShapeModel.maType; + if (!aType.isEmpty() && aType[0] == '#') + { + aType = aType.copy(1); + } + if (const ShapeType* pShapeType = pShapeContainer->getShapeTypeById(aType)) + { + nShapeType = pShapeType->getTypeModel().moShapeType.get(); + } + } + + if (getParentElement() != VML_TOKEN( group )) + { + // Custom shape in Writer with a textbox are transformed into a frame + dynamic_cast<SimpleShape&>( mrShape ).setService( + "com.sun.star.text.TextFrame"); + } + else if (getCurrentElement() == VML_TOKEN(rect) || nShapeType == ESCHER_ShpInst_TextBox) + // Transform only rectangles into a TextShape inside a groupshape. + dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.drawing.TextShape"); + return new TextBoxContext( *this, mrShapeModel.createTextBox(mrShape.getTypeModel()), rAttribs, + mrShape.getDrawing().getFilter().getGraphicHelper()); + } + case VMLX_TOKEN( ClientData ): + return new ClientDataContext( *this, mrShapeModel.createClientData(), rAttribs ); + case VMLPPT_TOKEN( textdata ): + // Force RectangleShape, this is ugly :( + // and is there because of the lines above which change it to TextFrame + dynamic_cast< SimpleShape& >( mrShape ).setService( + "com.sun.star.drawing.RectangleShape"); + mrShapeModel.maLegacyDiagramPath = getFragmentPathFromRelId(rAttribs.getString(XML_id, OUString())); + break; + case O_TOKEN( signatureline ): + mrShapeModel.mbIsSignatureLine = true; + mrShapeModel.maSignatureId = rAttribs.getString(XML_id, OUString()); + mrShapeModel.maSignatureLineSuggestedSignerName + = rAttribs.getString(O_TOKEN(suggestedsigner), OUString()); + mrShapeModel.maSignatureLineSuggestedSignerTitle + = rAttribs.getString(O_TOKEN(suggestedsigner2), OUString()); + mrShapeModel.maSignatureLineSuggestedSignerEmail + = rAttribs.getString(O_TOKEN(suggestedsigneremail), OUString()); + mrShapeModel.maSignatureLineSigningInstructions + = rAttribs.getString(O_TOKEN(signinginstructions), OUString()); + mrShapeModel.mbSignatureLineShowSignDate = ConversionHelper::decodeBool( + rAttribs.getString(XML_showsigndate, "t")); // default is true + mrShapeModel.mbSignatureLineCanAddComment = ConversionHelper::decodeBool( + rAttribs.getString(XML_allowcomments, "f")); // default is false + break; + case O_TOKEN( lock ): + // TODO + break; + } + // handle remaining stuff in base class + return ShapeTypeContext::onCreateContext( nElement, rAttribs ); +} + +void ShapeContext::setPoints( const OUString& rPoints ) +{ + mrShapeModel.maPoints.clear(); + sal_Int32 nIndex = 0; + + while( nIndex >= 0 ) + { + sal_Int32 nX = rPoints.getToken( 0, ',', nIndex ).toInt32(); + sal_Int32 nY = rPoints.getToken( 0, ',', nIndex ).toInt32(); + mrShapeModel.maPoints.emplace_back( nX, nY ); + } +} + +void ShapeContext::setFrom( const OUString& rPoints ) +{ + if (!rPoints.isEmpty()) + mrShapeModel.maFrom = rPoints; +} + +void ShapeContext::setTo( const OUString& rPoints ) +{ + if (!rPoints.isEmpty()) + mrShapeModel.maTo = rPoints; +} + +void ShapeContext::setControl1( const OUString& rPoints ) +{ + if (!rPoints.isEmpty()) + mrShapeModel.maControl1 = rPoints; +} + +void ShapeContext::setControl2( const OUString& rPoints ) +{ + if (!rPoints.isEmpty()) + mrShapeModel.maControl2 = rPoints; +} +void ShapeContext::setVmlPath( const OUString& rPath ) +{ + if (!rPath.isEmpty()) + mrShapeModel.maVmlPath = rPath; +} + +GroupShapeContext::GroupShapeContext(ContextHandler2Helper const& rParent, + const std::shared_ptr<GroupShape>& pShape, + const AttributeList& rAttribs) + : ShapeContext(rParent, pShape, rAttribs) + , mrShapes(pShape->getChildren()) +{ +} + +ContextHandlerRef GroupShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + // try to create a context of an embedded shape + ContextHandlerRef xContext = createShapeContext( *this, mrShapes, nElement, rAttribs ); + // handle remaining stuff of this shape in base class + return xContext ? xContext : ShapeContext::onCreateContext( nElement, rAttribs ); +} + +RectangleShapeContext::RectangleShapeContext(ContextHandler2Helper const& rParent, + const AttributeList& rAttribs, + const std::shared_ptr<RectangleShape>& pShape) + : ShapeContext(rParent, pShape, rAttribs) +{ +} + +ContextHandlerRef RectangleShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + // The parent class's context is fine + return ShapeContext::onCreateContext( nElement, rAttribs ); +} + +ControlShapeContext::ControlShapeContext( ::oox::core::ContextHandler2Helper const & rParent, ShapeContainer& rShapes, const AttributeList& rAttribs ) + : ShapeContextBase (rParent) +{ + ::oox::vml::ControlInfo aInfo; + aInfo.maShapeId = rAttribs.getXString( W_TOKEN( shapeid ), OUString() ); + aInfo.maFragmentPath = getFragmentPathFromRelId(rAttribs.getString( R_TOKEN(id), OUString() )); + aInfo.maName = rAttribs.getString( W_TOKEN( name ), OUString() ); + aInfo.mbTextContentShape = true; + rShapes.getDrawing().registerControl(aInfo); +} + +} // namespace oox + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmltextbox.cxx b/oox/source/vml/vmltextbox.cxx new file mode 100644 index 000000000..ed11aa4ec --- /dev/null +++ b/oox/source/vml/vmltextbox.cxx @@ -0,0 +1,204 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/vml/vmltextbox.hxx> + +#include <rtl/ustrbuf.hxx> +#include <tools/diagnose_ex.h> +#include <com/sun/star/awt/FontWeight.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/drawing/TextHorizontalAdjust.hpp> +#include <com/sun/star/drawing/XShape.hpp> +#include <com/sun/star/text/XTextAppend.hpp> +#include <com/sun/star/text/WritingMode.hpp> +#include <com/sun/star/style/ParagraphAdjust.hpp> +#include <comphelper/sequence.hxx> +#include <comphelper/sequenceashashmap.hxx> + +namespace oox::vml { + +using namespace com::sun::star; + +TextFontModel::TextFontModel() +{ +} + +TextPortionModel::TextPortionModel( const TextParagraphModel& rParagraph, const TextFontModel& rFont, const OUString& rText ) : + maParagraph( rParagraph ), + maFont( rFont ), + maText( rText ) +{ +} + +TextBox::TextBox(ShapeTypeModel& rTypeModel) + : mrTypeModel(rTypeModel) + , borderDistanceSet( false ) + , borderDistanceLeft(0) + , borderDistanceTop(0) + , borderDistanceRight(0) + , borderDistanceBottom(0) +{ +} + +void TextBox::appendPortion( const TextParagraphModel& rParagraph, const TextFontModel& rFont, const OUString& rText ) +{ + maPortions.emplace_back( rParagraph, rFont, rText ); +} + +const TextFontModel* TextBox::getFirstFont() const +{ + return maPortions.empty() ? nullptr : &maPortions.front().maFont; +} + +OUString TextBox::getText() const +{ + OUStringBuffer aBuffer; + for (auto const& portion : maPortions) + aBuffer.append( portion.maText ); + return aBuffer.makeStringAndClear(); +} + +void TextBox::convert(const uno::Reference<drawing::XShape>& xShape) const +{ + uno::Reference<text::XTextAppend> xTextAppend(xShape, uno::UNO_QUERY); + OUString sParaStyle; + bool bAmbiguousStyle = true; + + for (auto const& portion : maPortions) + { + beans::PropertyValue aPropertyValue; + std::vector<beans::PropertyValue> aPropVec; + const TextParagraphModel& rParagraph = portion.maParagraph; + const TextFontModel& rFont = portion.maFont; + if (rFont.moName.has()) + { + aPropertyValue.Name = "CharFontName"; + aPropertyValue.Value <<= rFont.moName.get(); + aPropVec.push_back(aPropertyValue); + + aPropertyValue.Name = "CharFontNameAsian"; + aPropertyValue.Value <<= rFont.moNameAsian.get(); + aPropVec.push_back(aPropertyValue); + + aPropertyValue.Name = "CharFontNameComplex"; + aPropertyValue.Value <<= rFont.moNameComplex.get(); + aPropVec.push_back(aPropertyValue); + } + if (rFont.mobBold.has()) + { + aPropertyValue.Name = "CharWeight"; + aPropertyValue.Value <<= rFont.mobBold.get() ? awt::FontWeight::BOLD : awt::FontWeight::NORMAL; + aPropVec.push_back(aPropertyValue); + } + if (rFont.monSize.has()) + { + aPropertyValue.Name = "CharHeight"; + aPropertyValue.Value <<= double(rFont.monSize.get()) / 2.; + aPropVec.push_back(aPropertyValue); + } + if (rFont.monSpacing.has()) + { + aPropertyValue.Name = "CharKerning"; + // Value is not converted to mm100: SvxKerningItem::PutValue() gets + // called with nMemberId = 0, so no mm100 -> twips conversion will + // be done there. + aPropertyValue.Value <<= sal_Int16(rFont.monSpacing.get()); + aPropVec.push_back(aPropertyValue); + } + if (rParagraph.moParaAdjust.has()) + { + style::ParagraphAdjust eAdjust = style::ParagraphAdjust_LEFT; + if (rParagraph.moParaAdjust.get() == "center") + eAdjust = style::ParagraphAdjust_CENTER; + else if (rParagraph.moParaAdjust.get() == "right") + eAdjust = style::ParagraphAdjust_RIGHT; + + aPropertyValue.Name = "ParaAdjust"; + aPropertyValue.Value <<= eAdjust; + aPropVec.push_back(aPropertyValue); + } + + // All paragraphs should be either undefined (default) or the same style, + // because it will only be applied to the entire shape, and not per-paragraph. + if (sParaStyle.isEmpty() ) + { + if ( rParagraph.moParaStyleName.has() ) + sParaStyle = rParagraph.moParaStyleName.get(); + if ( bAmbiguousStyle ) + bAmbiguousStyle = false; // both empty parastyle and ambiguous can only be true at the first paragraph + else + bAmbiguousStyle = rParagraph.moParaStyleName.has(); // ambiguous if both default and specified style used. + } + else if ( !bAmbiguousStyle ) + { + if ( !rParagraph.moParaStyleName.has() ) + bAmbiguousStyle = true; // ambiguous if both specified and default style used. + else if ( rParagraph.moParaStyleName.get() != sParaStyle ) + bAmbiguousStyle = true; // ambiguous if two different styles specified. + } + if (rFont.moColor.has()) + { + aPropertyValue.Name = "CharColor"; + aPropertyValue.Value <<= rFont.moColor.get().toUInt32(16); + aPropVec.push_back(aPropertyValue); + } + xTextAppend->appendTextPortion(portion.maText, comphelper::containerToSequence(aPropVec)); + } + + try + { + // Track the style in a grabBag for use later when style details are known. + comphelper::SequenceAsHashMap aGrabBag; + uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY_THROW); + aGrabBag.update( xPropertySet->getPropertyValue("CharInteropGrabBag") ); + aGrabBag["mso-pStyle"] <<= sParaStyle; + xPropertySet->setPropertyValue("CharInteropGrabBag", uno::makeAny(aGrabBag.getAsConstPropertyValueList())); + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION( "oox.vml","convert() grabbag exception" ); + } + + // Remove the last character of the shape text, if it would be a newline. + uno::Reference< text::XTextCursor > xCursor = xTextAppend->createTextCursor(); + xCursor->gotoEnd(false); + xCursor->goLeft(1, true); + if (xCursor->getString() == "\n") + xCursor->setString(""); + + if ( maLayoutFlow != "vertical" ) + return; + + uno::Reference<beans::XPropertySet> xProperties(xShape, uno::UNO_QUERY); + + // VML has the text horizontally aligned to left (all the time), + // v-text-anchor for vertical alignment, and vertical mode to swap the + // two. drawinglayer supports both horizontal and vertical alignment, + // but no vertical mode: we use T->B, R->L instead. + // As a result, we need to set horizontal adjustment here to 'right', + // that will result in vertical 'top' after writing mode is applied, + // which matches the VML behavior. + xProperties->setPropertyValue("TextHorizontalAdjust", uno::makeAny(drawing::TextHorizontalAdjust_RIGHT)); + + xProperties->setPropertyValue( "TextWritingMode", uno::makeAny( text::WritingMode_TB_RL ) ); +} + +} // namespace oox::vml + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/vml/vmltextboxcontext.cxx b/oox/source/vml/vmltextboxcontext.cxx new file mode 100644 index 000000000..d878c6dcd --- /dev/null +++ b/oox/source/vml/vmltextboxcontext.cxx @@ -0,0 +1,288 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <oox/helper/attributelist.hxx> +#include <oox/vml/vmlformatting.hxx> +#include <oox/vml/vmltextboxcontext.hxx> +#include <oox/vml/vmlshape.hxx> +#include <oox/token/namespaces.hxx> +#include <oox/token/tokens.hxx> +#include <osl/diagnose.h> +#include <sal/log.hxx> + +namespace oox::vml { + +using ::oox::core::ContextHandler2; +using ::oox::core::ContextHandler2Helper; +using ::oox::core::ContextHandlerRef; + +TextPortionContext::TextPortionContext( ContextHandler2Helper const & rParent, + TextBox& rTextBox, TextParagraphModel const & rParagraph, const TextFontModel& rParentFont, + sal_Int32 nElement, const AttributeList& rAttribs ) : + ContextHandler2( rParent ), + mrTextBox( rTextBox ), + maParagraph( rParagraph ), + maFont( rParentFont ), + mnInitialPortions( rTextBox.getPortionCount() ) +{ + switch( nElement ) + { + case XML_font: + maFont.moName = rAttribs.getXString( XML_face ); + maFont.moColor = rAttribs.getXString( XML_color ); + maFont.monSize = rAttribs.getInteger( XML_size ); + break; + case XML_u: + OSL_ENSURE( !maFont.monUnderline, "TextPortionContext::TextPortionContext - nested <u> elements" ); + maFont.monUnderline = (rAttribs.getToken( XML_class, XML_TOKEN_INVALID ) == XML_font4) ? XML_double : XML_single; + break; + case XML_sub: + case XML_sup: + OSL_ENSURE( !maFont.monEscapement, "TextPortionContext::TextPortionContext - nested <sub> or <sup> elements" ); + maFont.monEscapement = nElement; + break; + case XML_b: + OSL_ENSURE( !maFont.mobBold, "TextPortionContext::TextPortionContext - nested <b> elements" ); + maFont.mobBold = true; + break; + case XML_i: + OSL_ENSURE( !maFont.mobItalic, "TextPortionContext::TextPortionContext - nested <i> elements" ); + maFont.mobItalic = true; + break; + case XML_s: + OSL_ENSURE( !maFont.mobStrikeout, "TextPortionContext::TextPortionContext - nested <s> elements" ); + maFont.mobStrikeout = true; + break; + case OOX_TOKEN(dml, blip): + { + OptValue<OUString> oRelId = rAttribs.getString(R_TOKEN(embed)); + if (oRelId.has()) + mrTextBox.mrTypeModel.moGraphicPath = getFragmentPathFromRelId(oRelId.get()); + } + break; + case VML_TOKEN(imagedata): + { + OptValue<OUString> oRelId = rAttribs.getString(R_TOKEN(id)); + if (oRelId.has()) + mrTextBox.mrTypeModel.moGraphicPath = getFragmentPathFromRelId(oRelId.get()); + } + break; + case XML_span: + case W_TOKEN(r): + break; + default: + OSL_ENSURE( false, "TextPortionContext::TextPortionContext - unknown element" ); + } +} + +ContextHandlerRef TextPortionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + OSL_ENSURE( nElement != XML_font, "TextPortionContext::onCreateContext - nested <font> elements" ); + if (getNamespace(getCurrentElement()) == NMSP_doc) + return this; + return new TextPortionContext( *this, mrTextBox, maParagraph, maFont, nElement, rAttribs ); +} + +void TextPortionContext::onCharacters( const OUString& rChars ) +{ + if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != W_TOKEN(t)) + return; + + switch( getCurrentElement() ) + { + case XML_span: + // replace all NBSP characters with SP + mrTextBox.appendPortion( maParagraph, maFont, rChars.replace( 0xA0, ' ' ) ); + break; + default: + mrTextBox.appendPortion( maParagraph, maFont, rChars ); + } +} + +void TextPortionContext::onStartElement(const AttributeList& rAttribs) +{ + switch (getCurrentElement()) + { + case W_TOKEN(b): + maFont.mobBold = true; + break; + case W_TOKEN(sz): + maFont.monSize = rAttribs.getInteger( W_TOKEN(val) ); + break; + case W_TOKEN(br): + mrTextBox.appendPortion( maParagraph, maFont, "\n" ); + break; + case W_TOKEN(color): + maFont.moColor = rAttribs.getString( W_TOKEN(val) ); + break; + case W_TOKEN(spacing): + maFont.monSpacing = rAttribs.getInteger(W_TOKEN(val)); + break; + case W_TOKEN(r): + case W_TOKEN(rPr): + case W_TOKEN(t): + break; + case W_TOKEN(rFonts): + // See https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.runfonts(v=office.14).aspx + maFont.moName = rAttribs.getString(W_TOKEN(ascii)); + maFont.moNameAsian = rAttribs.getString(W_TOKEN(eastAsia)); + maFont.moNameComplex = rAttribs.getString(W_TOKEN(cs)); + break; + default: + SAL_INFO("oox", "unhandled: 0x" << std::hex<< getCurrentElement()); + break; + } +} + +void TextPortionContext::onEndElement() +{ + if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != W_TOKEN(t)) + return; + + /* A child element without own child elements may contain a single space + character, for example: + + <div> + <font><i>abc</i></font> + <font> </font> + <font><b>def</b></font> + </div> + + represents the italic text 'abc', an unformatted space character, and + the bold text 'def'. Unfortunately, the XML parser skips the space + character without issuing a 'characters' event. The class member + 'mnInitialPortions' contains the number of text portions existing when + this context has been constructed. If no text has been added in the + meantime, the space character has to be added manually. + */ + if( mrTextBox.getPortionCount() == mnInitialPortions ) + mrTextBox.appendPortion( maParagraph, maFont, OUString( ' ' ) ); +} + +TextBoxContext::TextBoxContext( ContextHandler2Helper const & rParent, TextBox& rTextBox, const AttributeList& rAttribs, + const GraphicHelper& graphicHelper ) : + ContextHandler2( rParent ), + mrTextBox( rTextBox ) +{ + if( rAttribs.getString( XML_insetmode ).get() != "auto" ) + { + OUString inset = rAttribs.getString( XML_inset ).get(); + OUString value; + OUString remainingStr; + + ConversionHelper::separatePair( value, remainingStr, inset, ',' ); + rTextBox.borderDistanceLeft = ConversionHelper::decodeMeasureToHmm( graphicHelper, + value.isEmpty() ? "0.1in" : value, 0, false, false ); + + inset = remainingStr; + ConversionHelper::separatePair( value, remainingStr, inset, ',' ); + rTextBox.borderDistanceTop = ConversionHelper::decodeMeasureToHmm( graphicHelper, + value.isEmpty() ? "0.05in" : value, 0, false, false ); + + inset = remainingStr; + ConversionHelper::separatePair( value, remainingStr, inset, ',' ); + rTextBox.borderDistanceRight = ConversionHelper::decodeMeasureToHmm( graphicHelper, + value.isEmpty() ? "0.1in" : value, 0, false, false ); + + inset = remainingStr; + ConversionHelper::separatePair( value, remainingStr, inset, ',' ); + rTextBox.borderDistanceBottom = ConversionHelper::decodeMeasureToHmm( graphicHelper, + value.isEmpty() ? "0.05in" : value, 0, false, false ); + + rTextBox.borderDistanceSet = true; + } + + OUString sStyle = rAttribs.getString( XML_style, OUString() ); + sal_Int32 nIndex = 0; + while( nIndex >= 0 ) + { + OUString aName, aValue; + if( ConversionHelper::separatePair( aName, aValue, sStyle.getToken( 0, ';', nIndex ), ':' ) ) + { + if( aName == "layout-flow" ) rTextBox.maLayoutFlow = aValue; + else if (aName == "mso-fit-shape-to-text") + rTextBox.mrTypeModel.mbAutoHeight = true; + else if (aName == "mso-layout-flow-alt") + rTextBox.mrTypeModel.maLayoutFlowAlt = aValue; + else if (aName == "mso-next-textbox") + rTextBox.msNextTextbox = aValue; + else + SAL_WARN("oox", "unhandled style property: " << aName); + } + } +} + +ContextHandlerRef TextBoxContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) +{ + switch( getCurrentElement() ) + { + case VML_TOKEN( textbox ): + if( nElement == XML_div ) return this; + else if (nElement == W_TOKEN(txbxContent)) return this; + break; + case XML_div: + if( nElement == XML_font ) return new TextPortionContext( *this, mrTextBox, maParagraph, TextFontModel(), nElement, rAttribs ); + break; + case W_TOKEN(txbxContent): + if (nElement == W_TOKEN(p)) return this; + break; + case W_TOKEN(p): + case W_TOKEN(sdtContent): + case W_TOKEN(smartTag): + if (nElement == W_TOKEN(r)) + return new TextPortionContext( *this, mrTextBox, maParagraph, TextFontModel(), nElement, rAttribs ); + else + return this; + break; + case W_TOKEN(pPr): + case W_TOKEN(sdt): + return this; + break; + default: + SAL_INFO("oox", "unhandled 0x" << std::hex << getCurrentElement()); + break; + } + return nullptr; +} + +void TextBoxContext::onStartElement(const AttributeList& rAttribs) +{ + switch (getCurrentElement()) + { + case W_TOKEN(jc): + maParagraph.moParaAdjust = rAttribs.getString( W_TOKEN(val) ); + break; + case W_TOKEN(pStyle): + maParagraph.moParaStyleName = rAttribs.getString( W_TOKEN(val) ); + break; + } +} + +void TextBoxContext::onEndElement() +{ + if (getCurrentElement() == W_TOKEN(p)) + { + mrTextBox.appendPortion( maParagraph, TextFontModel(), "\n" ); + maParagraph = TextParagraphModel(); + } +} + +} // namespace oox::vml + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |