LCOV - code coverage report
Current view: top level - sdext/source/pdfimport/tree - style.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 93 128 72.7 %
Date: 2014-04-11 Functions: 6 8 75.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           2 : StyleContainer::StyleContainer() :
      33           2 :     m_nNextId( 1 )
      34             : {
      35           2 : }
      36             : 
      37         345 : sal_Int32 StyleContainer::impl_getStyleId( const Style& rStyle, bool bSubStyle )
      38             : {
      39         345 :     sal_Int32 nRet = -1;
      40             : 
      41             :     // construct HashedStyle to find or insert
      42         345 :     HashedStyle aSearchStyle;
      43         345 :     aSearchStyle.Name                   = rStyle.Name;
      44         345 :     aSearchStyle.Properties             = rStyle.Properties;
      45         345 :     aSearchStyle.Contents               = rStyle.Contents;
      46         345 :     aSearchStyle.ContainedElement       = rStyle.ContainedElement;
      47         503 :     for( unsigned int n = 0; n < rStyle.SubStyles.size(); ++n )
      48         158 :         aSearchStyle.SubStyles.push_back( impl_getStyleId( *rStyle.SubStyles[n], true ) );
      49             : 
      50             :     boost::unordered_map< HashedStyle, sal_Int32, StyleHash >::iterator it =
      51         345 :         m_aStyleToId.find( aSearchStyle );
      52             : 
      53         345 :     if( it != m_aStyleToId.end() )
      54             :     {
      55         298 :         nRet = it->second;
      56         298 :         HashedStyle& rFound = m_aIdToStyle[ nRet ];
      57             :         // increase refcount on this style
      58         298 :         rFound.RefCount++;
      59         298 :         if( ! bSubStyle )
      60         161 :             rFound.IsSubStyle = false;
      61             :     }
      62             :     else
      63             :     {
      64          47 :         nRet = m_nNextId++;
      65             :         // create new style
      66          47 :         HashedStyle& rNew = m_aIdToStyle[ nRet ];
      67          47 :         rNew = aSearchStyle;
      68          47 :         rNew.RefCount           = 1;
      69          47 :         rNew.IsSubStyle         = bSubStyle;
      70             :         // fill the style hash to find the id
      71          47 :         m_aStyleToId[ rNew ] = nRet;
      72             :     }
      73         345 :     return nRet;
      74             : }
      75             : 
      76          26 : sal_Int32 StyleContainer::getStandardStyleId( const OString& rName )
      77             : {
      78          26 :     PropertyMap aProps;
      79          26 :     aProps[ "style:family" ] = OStringToOUString( rName, RTL_TEXTENCODING_UTF8 );
      80          26 :     aProps[ "style:name" ] = "standard";
      81             : 
      82          52 :     Style aStyle( "style:style", aProps );
      83          52 :     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         226 : OUString StyleContainer::getStyleName( sal_Int32 nStyle ) const
     147             : {
     148         226 :     OUStringBuffer aRet( 64 );
     149             : 
     150             :     boost::unordered_map< sal_Int32, HashedStyle >::const_iterator style_it =
     151         226 :         m_aIdToStyle.find( nStyle );
     152         226 :     if( style_it != m_aIdToStyle.end() )
     153             :     {
     154         226 :         const HashedStyle& rStyle = style_it->second;
     155             : 
     156         226 :         PropertyMap::const_iterator name_it = rStyle.Properties.find( "style:name" );
     157         226 :         if( name_it != rStyle.Properties.end() )
     158           4 :             aRet.append( name_it->second );
     159             :         else
     160             :         {
     161         222 :             PropertyMap::const_iterator fam_it = rStyle.Properties.find( "style:family" );
     162         222 :             OUString aStyleName;
     163         222 :             if( fam_it != rStyle.Properties.end() )
     164             :             {
     165         212 :                 aStyleName = fam_it->second;
     166             :             }
     167             :             else
     168          10 :                 aStyleName = OStringToOUString( rStyle.Name, RTL_TEXTENCODING_ASCII_US );
     169         222 :             sal_Int32 nIndex = aStyleName.lastIndexOf( ':' );
     170         222 :             aRet.append( aStyleName.copy( nIndex+1 ) );
     171         222 :             aRet.append( nStyle );
     172             :         }
     173             :     }
     174             :     else
     175             :     {
     176           0 :         aRet.appendAscii( "invalid style id " );
     177           0 :         aRet.append( nStyle );
     178             :     }
     179             : 
     180         226 :     return aRet.makeStringAndClear();
     181             : }
     182             : 
     183          47 : void StyleContainer::impl_emitStyle( sal_Int32           nStyleId,
     184             :                                      EmitContext&        rContext,
     185             :                                      ElementTreeVisitor& rContainedElemVisitor )
     186             : {
     187          47 :     boost::unordered_map< sal_Int32, HashedStyle >::const_iterator it = m_aIdToStyle.find( nStyleId );
     188          47 :     if( it != m_aIdToStyle.end() )
     189             :     {
     190          47 :         const HashedStyle& rStyle = it->second;
     191          47 :             PropertyMap aProps( rStyle.Properties );
     192          47 :         if( !rStyle.IsSubStyle )
     193          26 :             aProps[ "style:name" ] = getStyleName( nStyleId );
     194          47 :         rContext.rEmitter.beginTag( rStyle.Name.getStr(), aProps );
     195             : 
     196          68 :         for( unsigned int n = 0; n < rStyle.SubStyles.size(); ++n )
     197          21 :             impl_emitStyle( rStyle.SubStyles[n], rContext, rContainedElemVisitor );
     198          47 :         if( !rStyle.Contents.isEmpty() )
     199           0 :             rContext.rEmitter.write( rStyle.Contents );
     200          47 :         if( rStyle.ContainedElement )
     201             :             rStyle.ContainedElement->visitedBy( rContainedElemVisitor,
     202           0 :                                                 std::list<Element*>::iterator() );
     203          47 :         rContext.rEmitter.endTag( rStyle.Name.getStr() );
     204             :     }
     205          47 : }
     206             : 
     207           2 : void StyleContainer::emit( EmitContext&        rContext,
     208             :                            ElementTreeVisitor& rContainedElemVisitor )
     209             : {
     210           4 :     std::vector< sal_Int32 > aMasterPageSection, aAutomaticStyleSection, aOfficeStyleSection;
     211         147 :     for( boost::unordered_map< sal_Int32, HashedStyle >::iterator it = m_aIdToStyle.begin();
     212          98 :          it != m_aIdToStyle.end(); ++it )
     213             :     {
     214          47 :         if( ! it->second.IsSubStyle )
     215             :         {
     216          26 :             if( it->second.Name.equals( "style:master-page" ) )
     217           2 :                 aMasterPageSection.push_back( it->first );
     218          24 :             else if( getStyleName( it->first ) == "standard" )
     219           2 :                 aOfficeStyleSection.push_back( it->first );
     220             :             else
     221          22 :                 aAutomaticStyleSection.push_back( it->first );
     222             :         }
     223             :     }
     224             : 
     225           2 :     if( ! aMasterPageSection.empty() )
     226           2 :         std::stable_sort( aMasterPageSection.begin(), aMasterPageSection.end(), StyleIdNameSort(&m_aIdToStyle) );
     227           2 :     if( ! aAutomaticStyleSection.empty() )
     228           2 :         std::stable_sort( aAutomaticStyleSection.begin(), aAutomaticStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
     229           2 :     if( ! aOfficeStyleSection.empty() )
     230           1 :         std::stable_sort( aOfficeStyleSection.begin(), aOfficeStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
     231             : 
     232           2 :     int n = 0, nElements = 0;
     233           2 :     rContext.rEmitter.beginTag( "office:styles", PropertyMap() );
     234           4 :     for( n = 0, nElements = aOfficeStyleSection.size(); n < nElements; n++ )
     235           2 :         impl_emitStyle( aOfficeStyleSection[n], rContext, rContainedElemVisitor );
     236           2 :     rContext.rEmitter.endTag( "office:styles" );
     237           2 :     rContext.rEmitter.beginTag( "office:automatic-styles", PropertyMap() );
     238          24 :     for( n = 0, nElements = aAutomaticStyleSection.size(); n < nElements; n++ )
     239          22 :         impl_emitStyle( aAutomaticStyleSection[n], rContext, rContainedElemVisitor );
     240           2 :     rContext.rEmitter.endTag( "office:automatic-styles" );
     241           2 :     rContext.rEmitter.beginTag( "office:master-styles", PropertyMap() );
     242           4 :     for( n = 0, nElements = aMasterPageSection.size(); n < nElements; n++ )
     243           2 :         impl_emitStyle( aMasterPageSection[n], rContext, rContainedElemVisitor );
     244           4 :     rContext.rEmitter.endTag( "office:master-styles" );
     245           2 : }
     246             : 
     247             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10