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

Generated by: LCOV version 1.10