This is an automated email from the ASF dual-hosted git repository. jimjag pushed a commit to branch odf-devel in repository https://gitbox.apache.org/repos/asf/openoffice.git
commit 6d366c8df75a896a12793944b91c9e0b3093f5c3 Author: Jim Jagielski <[email protected]> AuthorDate: Thu Oct 29 08:46:32 2026 -0400 xmloff: write the ODF 1.2 table template and table-cell fills in strict mode, read both --- main/xmloff/source/table/XMLTableExport.cxx | 84 +++++++++++++++++++++++++++-- main/xmloff/source/table/XMLTableImport.cxx | 43 ++++++++++++++- main/xmloff/source/table/table.hxx | 1 + 3 files changed, 122 insertions(+), 6 deletions(-) diff --git a/main/xmloff/source/table/XMLTableExport.cxx b/main/xmloff/source/table/XMLTableExport.cxx index 6c84942b6c..2f9fb02ba5 100644 --- a/main/xmloff/source/table/XMLTableExport.cxx +++ b/main/xmloff/source/table/XMLTableExport.cxx @@ -41,6 +41,7 @@ #include <com/sun/star/table/XMergeableCell.hpp> #include <com/sun/star/style/XStyle.hpp> #include <com/sun/star/beans/XPropertySetInfo.hpp> +#include <com/sun/star/drawing/FillStyle.hpp> #include "xmloff/table/XMLTableExport.hxx" #include "xmloff/xmlnmspe.hxx" @@ -65,6 +66,7 @@ using namespace ::xmloff::token; #define _MAP(name,prefix,token,type,context) { name, sizeof(name)-1, prefix, token, type, context, SvtSaveOptions::ODFVER_010 } #define CMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_COLUMN,context) #define RMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_ROW,context) +#define CELLMAP(name,prefix,token,type,context) _MAP(name,prefix,token,type|XML_TYPE_PROP_TABLE_CELL,context) #define MAP_END { 0L, 0, 0, XML_EMPTY, 0, 0, SvtSaveOptions::ODFVER_010 } // -------------------------------------------------------------------- @@ -98,6 +100,62 @@ const XMLPropertyMapEntry* getRowPropertiesMap() // -------------------------------------------------------------------- +const XMLPropertyMapEntry* getCellPropertiesMap() +{ + static const XMLPropertyMapEntry aXMLCellProperties[] = + { + CELLMAP( "FillColor", XML_NAMESPACE_FO, XML_BACKGROUND_COLOR, XML_TYPE_COLOR, 0 ), + MAP_END + }; + + return &aXMLCellProperties[0]; +} + +// -------------------------------------------------------------------- + +// ODF 1.2 strict: a table cell style has no style:graphic-properties; only a solid fill survives, as fo:background-color +class XMLTableCellExportPropertyMapper : public SvXMLExportPropertyMapper +{ +public: + XMLTableCellExportPropertyMapper( const UniReference< XMLPropertySetMapper >& rMapper ) : SvXMLExportPropertyMapper( rMapper ) {} + +protected: + virtual void ContextFilter( std::vector< XMLPropertyState >& rProperties, Reference< XPropertySet > rPropSet ) const; +}; + +void XMLTableCellExportPropertyMapper::ContextFilter( std::vector< XMLPropertyState >& rProperties, Reference< XPropertySet > rPropSet ) const +{ + const bool bStrict = SvtSaveOptions().GetODFDefaultVersion() == SvtSaveOptions::ODFVER_012; + bool bSolid = false; + if( bStrict ) try + { + ::com::sun::star::drawing::FillStyle eFillStyle = ::com::sun::star::drawing::FillStyle_NONE; + bSolid = ( rPropSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("FillStyle") ) ) >>= eFillStyle ) && eFillStyle == ::com::sun::star::drawing::FillStyle_SOLID; + } + catch( Exception& ) + { + } + + for( std::vector< XMLPropertyState >::iterator aIter( rProperties.begin() ); aIter != rProperties.end(); ++aIter ) + { + if( aIter->mnIndex == -1 ) + continue; + + const sal_uInt32 nPropType = maPropMapper->GetEntryFlags( aIter->mnIndex ) & XML_TYPE_PROP_MASK; + if( nPropType == XML_TYPE_PROP_TABLE_CELL ) + { + if( !bSolid ) + aIter->mnIndex = -1; + } + else if( bStrict && nPropType == XML_TYPE_PROP_GRAPHIC ) + aIter->mnIndex = -1; + } + + SvXMLExportPropertyMapper::ContextFilter( rProperties, rPropSet ); +} + +// -------------------------------------------------------------------- + class StringStatisticHelper : public std::map< OUString, sal_Int32 > { public: @@ -169,6 +227,7 @@ XMLTableExport::XMLTableExport(SvXMLExport& rExp, const rtl::Reference< SvXMLExp mxCellExportPropertySetMapper = xExportPropertyMapper; mxCellExportPropertySetMapper->ChainExportMapper(XMLTextParagraphExport::CreateParaExtPropMapper(rExp)); + mxCellExportPropertySetMapper->ChainExportMapper(new XMLTableCellExportPropertyMapper( new XMLPropertySetMapper( getCellPropertiesMap(), xFactoryRef.get() ) )); mxRowExportPropertySetMapper = new SvXMLExportPropertyMapper( new XMLPropertySetMapper( getRowPropertiesMap(), xFactoryRef.get() ) ); mxColumnExportPropertySetMapper = new SvXMLExportPropertyMapper( new XMLPropertySetMapper( getColumnPropertiesMap(), xFactoryRef.get() ) ); @@ -552,6 +611,11 @@ void XMLTableExport::exportTableTemplates() if( !mbExportTables ) return; + // ODF 1.2 strict: table:name, table:style-name and the part order of the schema (body before even-rows) + const bool bStrict = mrExport.getDefaultVersion() == SvtSaveOptions::ODFVER_012; + const TableStyleElement* pMap = getTableStyleMap(); + static const sal_Int32 aStrictPartOrder[] = { 0, 1, 2, 3, 8, 4, 5, 6, 7 }; + try { Reference< XStyleFamiliesSupplier > xFamiliesSupp( mrExport.GetModel(), UNO_QUERY_THROW ); @@ -567,19 +631,30 @@ void XMLTableExport::exportTableTemplates() Reference< XNameAccess > xStyleNames( xTableStyle, UNO_QUERY_THROW ); - mrExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xTableStyle->getName() ) ); + if( bStrict ) + { + mrExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NAME, GetExport().EncodeStyleName( xTableStyle->getName() ) ); + mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "first-row-start-column", "row" ); + mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "first-row-end-column", "row" ); + mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "last-row-start-column", "row" ); + mrExport.AddAttributeASCII(XML_NAMESPACE_TABLE, "last-row-end-column", "row" ); + } + else + mrExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xTableStyle->getName() ) ); SvXMLElementExport tableTemplate( mrExport, XML_NAMESPACE_TABLE, XML_TABLE_TEMPLATE, sal_True, sal_True ); const TableStyleElement* pElements = getTableStyleMap(); + sal_Int32 nPart = 0; while( pElements->meElement != XML_TOKEN_END ) { try { - Reference< XStyle > xStyle( xStyleNames->getByName( pElements->msStyleName ), UNO_QUERY ); + const TableStyleElement* pPart = bStrict ? pMap + aStrictPartOrder[nPart] : pElements; + Reference< XStyle > xStyle( xStyleNames->getByName( pPart->msStyleName ), UNO_QUERY ); if( xStyle.is() ) { - mrExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xStyle->getName() ) ); - SvXMLElementExport element( mrExport, XML_NAMESPACE_TABLE, pElements->meElement, sal_True, sal_True ); + mrExport.AddAttribute(bStrict ? XML_NAMESPACE_TABLE : XML_NAMESPACE_TEXT, XML_STYLE_NAME, GetExport().EncodeStyleName( xStyle->getName() ) ); + SvXMLElementExport element( mrExport, XML_NAMESPACE_TABLE, pPart->meElement, sal_True, sal_True ); } } catch( Exception& ) @@ -588,6 +663,7 @@ void XMLTableExport::exportTableTemplates() } pElements++; + nPart++; } } catch( Exception& ) diff --git a/main/xmloff/source/table/XMLTableImport.cxx b/main/xmloff/source/table/XMLTableImport.cxx index bcc5aea782..970c222f42 100644 --- a/main/xmloff/source/table/XMLTableImport.cxx +++ b/main/xmloff/source/table/XMLTableImport.cxx @@ -32,6 +32,7 @@ #include <com/sun/star/text/XText.hpp> #include <com/sun/star/container/XNameContainer.hpp> #include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/drawing/FillStyle.hpp> #include "xmloff/table/XMLTableImport.hxx" #include "xmloff/xmltkmap.hxx" @@ -203,6 +204,42 @@ SvXMLImportContext * XMLProxyContext::CreateChildContext( sal_uInt16 nPrefix, co return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList ); } +// -------------------------------------------------------------------- + +// fo:background-color in style:table-cell-properties is a solid fill; chained before the text mapper, whose finished() ends the chain +class XMLTableCellImportPropertyMapper : public SvXMLImportPropertyMapper +{ +public: + XMLTableCellImportPropertyMapper( const UniReference< XMLPropertySetMapper >& rMapper, SvXMLImport& rImport ) : SvXMLImportPropertyMapper( rMapper, rImport ) {} + + virtual void finished( ::std::vector< XMLPropertyState >& rProperties, sal_Int32 nStartIndex, sal_Int32 nEndIndex ) const; +}; + +void XMLTableCellImportPropertyMapper::finished( ::std::vector< XMLPropertyState >& rProperties, sal_Int32 nStartIndex, sal_Int32 nEndIndex ) const +{ + bool bHasBackground = false; + bool bHasFillStyle = false; + for( ::std::vector< XMLPropertyState >::const_iterator aIter( rProperties.begin() ); aIter != rProperties.end(); ++aIter ) + { + if( aIter->mnIndex == -1 ) + continue; + + if( ( maPropMapper->GetEntryFlags( aIter->mnIndex ) & XML_TYPE_PROP_MASK ) == XML_TYPE_PROP_TABLE_CELL ) + bHasBackground = true; + else if( maPropMapper->GetEntryAPIName( aIter->mnIndex ).equalsAscii( "FillStyle" ) ) + bHasFillStyle = true; + } + + if( bHasBackground && !bHasFillStyle ) + { + const sal_Int32 nFillStyle = maPropMapper->FindEntryIndex( "FillStyle", XML_NAMESPACE_DRAW, GetXMLToken( XML_FILL ) ); + if( nFillStyle != -1 ) + rProperties.push_back( XMLPropertyState( nFillStyle, Any( ::com::sun::star::drawing::FillStyle_SOLID ) ) ); + } + + SvXMLImportPropertyMapper::finished( rProperties, nStartIndex, nEndIndex ); +} + // -------------------------------------------------------------------- // class XMLTableImport // -------------------------------------------------------------------- @@ -211,6 +248,7 @@ XMLTableImport::XMLTableImport( SvXMLImport& rImport, const rtl::Reference< XMLP : mrImport( rImport ) { mxCellImportPropertySetMapper = new SvXMLImportPropertyMapper( xCellPropertySetMapper.get(), rImport ); + mxCellImportPropertySetMapper->ChainImportMapper(new XMLTableCellImportPropertyMapper( new XMLPropertySetMapper( getCellPropertiesMap(), xFactoryRef.get() ), rImport )); mxCellImportPropertySetMapper->ChainImportMapper(XMLTextImportHelper::CreateParaExtPropMapper(rImport)); @@ -773,7 +811,8 @@ void XMLTableTemplateContext::StartElement( const Reference< XAttributeList >& x { OUString sAttrName; sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &sAttrName ); - if( (nAttrPrefix == XML_NAMESPACE_TEXT ) && IsXMLToken( sAttrName, XML_STYLE_NAME ) ) + if( ( (nAttrPrefix == XML_NAMESPACE_TEXT ) && IsXMLToken( sAttrName, XML_STYLE_NAME ) ) || + ( (nAttrPrefix == XML_NAMESPACE_TABLE ) && IsXMLToken( sAttrName, XML_NAME ) ) ) { msTemplateStyleName = xAttrList->getValueByIndex( i ); break; @@ -807,7 +846,7 @@ SvXMLImportContext * XMLTableTemplateContext::CreateChildContext( sal_uInt16 nPr { OUString sAttrName; sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &sAttrName ); - if( (nAttrPrefix == XML_NAMESPACE_TEXT) && IsXMLToken( sAttrName, XML_STYLE_NAME ) ) + if( ( (nAttrPrefix == XML_NAMESPACE_TEXT) || (nAttrPrefix == XML_NAMESPACE_TABLE) ) && IsXMLToken( sAttrName, XML_STYLE_NAME ) ) { maTableTemplate[pElements->msStyleName] = xAttrList->getValueByIndex( i ); break; diff --git a/main/xmloff/source/table/table.hxx b/main/xmloff/source/table/table.hxx index f9bd2dd62e..581efb5d74 100644 --- a/main/xmloff/source/table/table.hxx +++ b/main/xmloff/source/table/table.hxx @@ -37,5 +37,6 @@ struct TableStyleElement extern const TableStyleElement* getTableStyleMap(); extern const XMLPropertyMapEntry* getColumnPropertiesMap(); extern const XMLPropertyMapEntry* getRowPropertiesMap(); +extern const XMLPropertyMapEntry* getCellPropertiesMap(); #endif
