LCOV - code coverage report
Current view: top level - sdext/source/pdfimport/tree - style.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 128 0.0 %
Date: 2014-04-14 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "style.hxx"
      22             : #include "genericelements.hxx"
      23             : #include "xmlemitter.hxx"
      24             : #include "pdfiprocessor.hxx"
      25             : #include <rtl/ustrbuf.hxx>
      26             : 
      27             : #include <algorithm>
      28             : 
      29             : using namespace pdfi;
      30             : 
      31             : 
      32           0 : StyleContainer::StyleContainer() :
      33           0 :     m_nNextId( 1 )
      34             : {
      35           0 : }
      36             : 
      37           0 : sal_Int32 StyleContainer::impl_getStyleId( const Style& rStyle, bool bSubStyle )
      38             : {
      39           0 :     sal_Int32 nRet = -1;
      40             : 
      41             :     // construct HashedStyle to find or insert
      42           0 :     HashedStyle aSearchStyle;
      43           0 :     aSearchStyle.Name                   = rStyle.Name;
      44           0 :     aSearchStyle.Properties             = rStyle.Properties;
      45           0 :     aSearchStyle.Contents               = rStyle.Contents;
      46           0 :     aSearchStyle.ContainedElement       = rStyle.ContainedElement;
      47           0 :     for( unsigned int n = 0; n < rStyle.SubStyles.size(); ++n )
      48           0 :         aSearchStyle.SubStyles.push_back( impl_getStyleId( *rStyle.SubStyles[n], true ) );
      49             : 
      50             :     boost::unordered_map< HashedStyle, sal_Int32, StyleHash >::iterator it =
      51           0 :         m_aStyleToId.find( aSearchStyle );
      52             : 
      53           0 :     if( it != m_aStyleToId.end() )
      54             :     {
      55           0 :         nRet = it->second;
      56           0 :         HashedStyle& rFound = m_aIdToStyle[ nRet ];
      57             :         // increase refcount on this style
      58           0 :         rFound.RefCount++;
      59           0 :         if( ! bSubStyle )
      60           0 :             rFound.IsSubStyle = false;
      61             :     }
      62             :     else
      63             :     {
      64           0 :         nRet = m_nNextId++;
      65             :         // create new style
      66           0 :         HashedStyle& rNew = m_aIdToStyle[ nRet ];
      67           0 :         rNew = aSearchStyle;
      68           0 :         rNew.RefCount           = 1;
      69           0 :         rNew.IsSubStyle         = bSubStyle;
      70             :         // fill the style hash to find the id
      71           0 :         m_aStyleToId[ rNew ] = nRet;
      72             :     }
      73           0 :     return nRet;
      74             : }
      75             : 
      76           0 : sal_Int32 StyleContainer::getStandardStyleId( const OString& rName )
      77             : {
      78           0 :     PropertyMap aProps;
      79           0 :     aProps[ "style:family" ] = OStringToOUString( rName, RTL_TEXTENCODING_UTF8 );
      80           0 :     aProps[ "style:name" ] = "standard";
      81             : 
      82           0 :     Style aStyle( "style:style", aProps );
      83           0 :     return getStyleId( aStyle );
      84             : }
      85             : 
      86           0 : const PropertyMap* StyleContainer::getProperties( sal_Int32 nStyleId ) const
      87             : {
      88             :     boost::unordered_map< sal_Int32, HashedStyle >::const_iterator it =
      89           0 :         m_aIdToStyle.find( nStyleId );
      90           0 :     return it != m_aIdToStyle.end() ? &(it->second.Properties) : NULL;
      91             : }
      92             : 
      93           0 : sal_Int32 StyleContainer::setProperties( sal_Int32 nStyleId, const PropertyMap& rNewProps )
      94             : {
      95           0 :     sal_Int32 nRet = -1;
      96             :     boost::unordered_map< sal_Int32, HashedStyle >::iterator it =
      97           0 :         m_aIdToStyle.find( nStyleId );
      98           0 :     if( it != m_aIdToStyle.end() )
      99             :     {
     100           0 :         if( it->second.RefCount == 1 )
     101             :         {
     102           0 :             nRet = it->first;
     103             :             // erase old hash to id mapping
     104           0 :             m_aStyleToId.erase( it->second );
     105             :             // change properties
     106           0 :             it->second.Properties = rNewProps;
     107             :             // fill in new hash to id mapping
     108           0 :             m_aStyleToId[ it->second ] = nRet;
     109             :         }
     110             :         else
     111             :         {
     112             :             // decrease refcound on old instance
     113           0 :             it->second.RefCount--;
     114             :             // acquire new HashedStyle
     115           0 :             HashedStyle aSearchStyle;
     116           0 :             aSearchStyle.Name                   = it->second.Name;
     117           0 :             aSearchStyle.Properties             = rNewProps;
     118           0 :             aSearchStyle.Contents               = it->second.Contents;
     119           0 :             aSearchStyle.ContainedElement       = it->second.ContainedElement;
     120           0 :             aSearchStyle.SubStyles              = it->second.SubStyles;
     121           0 :             aSearchStyle.IsSubStyle             = it->second.IsSubStyle;
     122             : 
     123             :             // find out whether this new style already exists
     124             :             boost::unordered_map< HashedStyle, sal_Int32, StyleHash >::iterator new_it =
     125           0 :                 m_aStyleToId.find( aSearchStyle );
     126           0 :             if( new_it != m_aStyleToId.end() )
     127             :             {
     128           0 :                 nRet = new_it->second;
     129           0 :                 m_aIdToStyle[ nRet ].RefCount++;
     130             :             }
     131             :             else
     132             :             {
     133           0 :                 nRet = m_nNextId++;
     134             :                 // create new style with new id
     135           0 :                 HashedStyle& rNew = m_aIdToStyle[ nRet ];
     136           0 :                 rNew = aSearchStyle;
     137           0 :                 rNew.RefCount = 1;
     138             :                 // fill style to id hash
     139           0 :                 m_aStyleToId[ aSearchStyle ] = nRet;
     140           0 :             }
     141             :         }
     142             :     }
     143           0 :     return nRet;
     144             : }
     145             : 
     146           0 : OUString StyleContainer::getStyleName( sal_Int32 nStyle ) const
     147             : {
     148           0 :     OUStringBuffer aRet( 64 );
     149             : 
     150             :     boost::unordered_map< sal_Int32, HashedStyle >::const_iterator style_it =
     151           0 :         m_aIdToStyle.find( nStyle );
     152           0 :     if( style_it != m_aIdToStyle.end() )
     153             :     {
     154           0 :         const HashedStyle& rStyle = style_it->second;
     155             : 
     156           0 :         PropertyMap::const_iterator name_it = rStyle.Properties.find( "style:name" );
     157           0 :         if( name_it != rStyle.Properties.end() )
     158           0 :             aRet.append( name_it->second );
     159             :         else
     160             :         {
     161           0 :             PropertyMap::const_iterator fam_it = rStyle.Properties.find( "style:family" );
     162           0 :             OUString aStyleName;
     163           0 :             if( fam_it != rStyle.Properties.end() )
     164             :             {
     165           0 :                 aStyleName = fam_it->second;
     166             :             }
     167             :             else
     168           0 :                 aStyleName = OStringToOUString( rStyle.Name, RTL_TEXTENCODING_ASCII_US );
     169           0 :             sal_Int32 nIndex = aStyleName.lastIndexOf( ':' );
     170           0 :             aRet.append( aStyleName.copy( nIndex+1 ) );
     171           0 :             aRet.append( nStyle );
     172             :         }
     173             :     }
     174             :     else
     175             :     {
     176           0 :         aRet.appendAscii( "invalid style id " );
     177           0 :         aRet.append( nStyle );
     178             :     }
     179             : 
     180           0 :     return aRet.makeStringAndClear();
     181             : }
     182             : 
     183           0 : void StyleContainer::impl_emitStyle( sal_Int32           nStyleId,
     184             :                                      EmitContext&        rContext,
     185             :                                      ElementTreeVisitor& rContainedElemVisitor )
     186             : {
     187           0 :     boost::unordered_map< sal_Int32, HashedStyle >::const_iterator it = m_aIdToStyle.find( nStyleId );
     188           0 :     if( it != m_aIdToStyle.end() )
     189             :     {
     190           0 :         const HashedStyle& rStyle = it->second;
     191           0 :             PropertyMap aProps( rStyle.Properties );
     192           0 :         if( !rStyle.IsSubStyle )
     193           0 :             aProps[ "style:name" ] = getStyleName( nStyleId );
     194           0 :         rContext.rEmitter.beginTag( rStyle.Name.getStr(), aProps );
     195             : 
     196           0 :         for( unsigned int n = 0; n < rStyle.SubStyles.size(); ++n )
     197           0 :             impl_emitStyle( rStyle.SubStyles[n], rContext, rContainedElemVisitor );
     198           0 :         if( !rStyle.Contents.isEmpty() )
     199           0 :             rContext.rEmitter.write( rStyle.Contents );
     200           0 :         if( rStyle.ContainedElement )
     201             :             rStyle.ContainedElement->visitedBy( rContainedElemVisitor,
     202           0 :                                                 std::list<Element*>::iterator() );
     203           0 :         rContext.rEmitter.endTag( rStyle.Name.getStr() );
     204             :     }
     205           0 : }
     206             : 
     207           0 : void StyleContainer::emit( EmitContext&        rContext,
     208             :                            ElementTreeVisitor& rContainedElemVisitor )
     209             : {
     210           0 :     std::vector< sal_Int32 > aMasterPageSection, aAutomaticStyleSection, aOfficeStyleSection;
     211           0 :     for( boost::unordered_map< sal_Int32, HashedStyle >::iterator it = m_aIdToStyle.begin();
     212           0 :          it != m_aIdToStyle.end(); ++it )
     213             :     {
     214           0 :         if( ! it->second.IsSubStyle )
     215             :         {
     216           0 :             if( it->second.Name.equals( "style:master-page" ) )
     217           0 :                 aMasterPageSection.push_back( it->first );
     218           0 :             else if( getStyleName( it->first ) == "standard" )
     219           0 :                 aOfficeStyleSection.push_back( it->first );
     220             :             else
     221           0 :                 aAutomaticStyleSection.push_back( it->first );
     222             :         }
     223             :     }
     224             : 
     225           0 :     if( ! aMasterPageSection.empty() )
     226           0 :         std::stable_sort( aMasterPageSection.begin(), aMasterPageSection.end(), StyleIdNameSort(&m_aIdToStyle) );
     227           0 :     if( ! aAutomaticStyleSection.empty() )
     228           0 :         std::stable_sort( aAutomaticStyleSection.begin(), aAutomaticStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
     229           0 :     if( ! aOfficeStyleSection.empty() )
     230           0 :         std::stable_sort( aOfficeStyleSection.begin(), aOfficeStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
     231             : 
     232           0 :     int n = 0, nElements = 0;
     233           0 :     rContext.rEmitter.beginTag( "office:styles", PropertyMap() );
     234           0 :     for( n = 0, nElements = aOfficeStyleSection.size(); n < nElements; n++ )
     235           0 :         impl_emitStyle( aOfficeStyleSection[n], rContext, rContainedElemVisitor );
     236           0 :     rContext.rEmitter.endTag( "office:styles" );
     237           0 :     rContext.rEmitter.beginTag( "office:automatic-styles", PropertyMap() );
     238           0 :     for( n = 0, nElements = aAutomaticStyleSection.size(); n < nElements; n++ )
     239           0 :         impl_emitStyle( aAutomaticStyleSection[n], rContext, rContainedElemVisitor );
     240           0 :     rContext.rEmitter.endTag( "office:automatic-styles" );
     241           0 :     rContext.rEmitter.beginTag( "office:master-styles", PropertyMap() );
     242           0 :     for( n = 0, nElements = aMasterPageSection.size(); n < nElements; n++ )
     243           0 :         impl_emitStyle( aMasterPageSection[n], rContext, rContainedElemVisitor );
     244           0 :     rContext.rEmitter.endTag( "office:master-styles" );
     245           0 : }
     246             : 
     247             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10