LCOV - code coverage report
Current view: top level - oox/source/vml - vmltextboxcontext.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 98 113 86.7 %
Date: 2015-06-13 12:38:46 Functions: 9 9 100.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             : #include "oox/vml/vmlformatting.hxx"
      21             : #include "oox/vml/vmltextboxcontext.hxx"
      22             : #include "oox/vml/vmlshape.hxx"
      23             : #include <com/sun/star/drawing/XShape.hpp>
      24             : #include <osl/diagnose.h>
      25             : 
      26             : namespace oox {
      27             : namespace vml {
      28             : 
      29             : using ::oox::core::ContextHandler2;
      30             : using ::oox::core::ContextHandler2Helper;
      31             : using ::oox::core::ContextHandlerRef;
      32             : 
      33         268 : TextPortionContext::TextPortionContext( ContextHandler2Helper& rParent,
      34             :         TextBox& rTextBox, TextParagraphModel& rParagraph, const TextFontModel& rParentFont,
      35             :         sal_Int32 nElement, const AttributeList& rAttribs ) :
      36             :     ContextHandler2( rParent ),
      37             :     mrTextBox( rTextBox ),
      38             :     maParagraph( rParagraph ),
      39             :     maFont( rParentFont ),
      40         268 :     mnInitialPortions( rTextBox.getPortionCount() )
      41             : {
      42         268 :     switch( nElement )
      43             :     {
      44             :         case XML_font:
      45           1 :             maFont.moName = rAttribs.getXString( XML_face );
      46           1 :             maFont.moColor = rAttribs.getXString( XML_color );
      47           1 :             maFont.monSize = rAttribs.getInteger( XML_size );
      48           1 :         break;
      49             :         case XML_u:
      50             :             OSL_ENSURE( !maFont.monUnderline, "TextPortionContext::TextPortionContext - nested <u> elements" );
      51           0 :             maFont.monUnderline = (rAttribs.getToken( XML_class, XML_TOKEN_INVALID ) == XML_font4) ? XML_double : XML_single;
      52           0 :         break;
      53             :         case XML_sub:
      54             :         case XML_sup:
      55             :             OSL_ENSURE( !maFont.monEscapement, "TextPortionContext::TextPortionContext - nested <sub> or <sup> elements" );
      56           0 :             maFont.monEscapement = nElement;
      57           0 :         break;
      58             :         case XML_b:
      59             :             OSL_ENSURE( !maFont.mobBold, "TextPortionContext::TextPortionContext - nested <b> elements" );
      60           0 :             maFont.mobBold = true;
      61           0 :         break;
      62             :         case XML_i:
      63             :             OSL_ENSURE( !maFont.mobItalic, "TextPortionContext::TextPortionContext - nested <i> elements" );
      64           0 :             maFont.mobItalic = true;
      65           0 :         break;
      66             :         case XML_s:
      67             :             OSL_ENSURE( !maFont.mobStrikeout, "TextPortionContext::TextPortionContext - nested <s> elements" );
      68           0 :             maFont.mobStrikeout = true;
      69           0 :         break;
      70             :         case OOX_TOKEN(dml, blip):
      71             :             {
      72           2 :                 OptValue<OUString> oRelId = rAttribs.getString(R_TOKEN(embed));
      73           2 :                 if (oRelId.has())
      74           2 :                     mrTextBox.mrTypeModel.moGraphicPath = getFragmentPathFromRelId(oRelId.get());
      75             :             }
      76           2 :         break;
      77             :         case VML_TOKEN(imagedata):
      78             :             {
      79           1 :                 OptValue<OUString> oRelId = rAttribs.getString(R_TOKEN(id));
      80           1 :                 if (oRelId.has())
      81           1 :                     mrTextBox.mrTypeModel.moGraphicPath = getFragmentPathFromRelId(oRelId.get());
      82             :             }
      83           1 :         break;
      84             :         case XML_span:
      85             :         case W_TOKEN(r):
      86         208 :         break;
      87             :         default:
      88             :             OSL_ENSURE( false, "TextPortionContext::TextPortionContext - unknown element" );
      89             :     }
      90         268 : }
      91             : 
      92        1243 : ContextHandlerRef TextPortionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
      93             : {
      94             :     OSL_ENSURE( nElement != XML_font, "TextPortionContext::onCreateContext - nested <font> elements" );
      95        1243 :     if (getNamespace(getCurrentElement()) == NMSP_doc)
      96        1184 :         return this;
      97          59 :     return new TextPortionContext( *this, mrTextBox, maParagraph, maFont, nElement, rAttribs );
      98             : }
      99             : 
     100         456 : void TextPortionContext::onCharacters( const OUString& rChars )
     101             : {
     102         456 :     if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != W_TOKEN(t))
     103         621 :         return;
     104             : 
     105         291 :     switch( getCurrentElement() )
     106             :     {
     107             :         case XML_span:
     108             :             // replace all NBSP characters with SP
     109           0 :             mrTextBox.appendPortion( maParagraph, maFont, rChars.replace( 0xA0, ' ' ) );
     110           0 :         break;
     111             :         default:
     112         291 :             mrTextBox.appendPortion( maParagraph, maFont, rChars );
     113             :     }
     114             : }
     115             : 
     116        1452 : void TextPortionContext::onStartElement(const AttributeList& rAttribs)
     117             : {
     118        1452 :     switch (getCurrentElement())
     119             :     {
     120             :         case W_TOKEN(b):
     121          23 :             maFont.mobBold = true;
     122          23 :         break;
     123             :         case W_TOKEN(sz):
     124         197 :             maFont.monSize = rAttribs.getInteger( W_TOKEN(val) );
     125         197 :         break;
     126             :         case W_TOKEN(br):
     127           1 :             mrTextBox.appendPortion( maParagraph, maFont, "\n" );
     128           1 :         break;
     129             :         case W_TOKEN(color):
     130         131 :             maFont.moColor = rAttribs.getString( W_TOKEN(val) );
     131         131 :         break;
     132             :         case W_TOKEN(spacing):
     133           2 :             maFont.monSpacing = rAttribs.getInteger(W_TOKEN(val));
     134           2 :         break;
     135             :         case W_TOKEN(r):
     136             :         case W_TOKEN(rPr):
     137             :         case W_TOKEN(t):
     138         618 :         break;
     139             :         default:
     140             :             SAL_INFO("oox", "unhandled: 0x" << std::hex<< getCurrentElement());
     141         480 :         break;
     142             :     }
     143        1452 : }
     144             : 
     145        1452 : void TextPortionContext::onEndElement()
     146             : {
     147        1452 :     if (getNamespace(getCurrentElement()) == NMSP_doc && getCurrentElement() != W_TOKEN(t))
     148        2637 :         return;
     149             : 
     150             :     /*  A child element without own child elements may contain a single space
     151             :         character, for example:
     152             : 
     153             :           <div>
     154             :             <font><i>abc</i></font>
     155             :             <font> </font>
     156             :             <font><b>def</b></font>
     157             :           </div>
     158             : 
     159             :         represents the italic text 'abc', an unformatted space character, and
     160             :         the bold text 'def'. Unfortunately, the XML parser skips the space
     161             :         character without issuing a 'characters' event. The class member
     162             :         'mnInitialPortions' contains the number of text portions existing when
     163             :         this context has been constructed. If no text has been added in the
     164             :         meantime, the space character has to be added manually.
     165             :      */
     166         267 :     if( mrTextBox.getPortionCount() == mnInitialPortions )
     167          35 :         mrTextBox.appendPortion( maParagraph, maFont, OUString( ' ' ) );
     168             : }
     169             : 
     170         262 : TextBoxContext::TextBoxContext( ContextHandler2Helper& rParent, TextBox& rTextBox, const AttributeList& rAttribs,
     171             :     const GraphicHelper& graphicHelper ) :
     172             :     ContextHandler2( rParent ),
     173         262 :     mrTextBox( rTextBox )
     174             : {
     175         262 :     if( rAttribs.getString( XML_insetmode ).get() != "auto" )
     176             :     {
     177         262 :         OUString inset = rAttribs.getString( XML_inset ).get();
     178         524 :         OUString value;
     179         524 :         OUString remainingStr;
     180             : 
     181         262 :         ConversionHelper::separatePair( value, remainingStr, inset, ',' );
     182             :         rTextBox.borderDistanceLeft = ConversionHelper::decodeMeasureToHmm( graphicHelper,
     183         262 :             value.isEmpty() ? "0.1in" : value, 0, false, false );
     184             : 
     185         262 :         inset = remainingStr;
     186         262 :         ConversionHelper::separatePair( value, remainingStr, inset, ',' );
     187             :         rTextBox.borderDistanceTop = ConversionHelper::decodeMeasureToHmm( graphicHelper,
     188         262 :             value.isEmpty() ? "0.05in" : value, 0, false, false );
     189             : 
     190         262 :         inset = remainingStr;
     191         262 :         ConversionHelper::separatePair( value, remainingStr, inset, ',' );
     192             :         rTextBox.borderDistanceRight = ConversionHelper::decodeMeasureToHmm( graphicHelper,
     193         262 :             value.isEmpty() ? "0.1in" : value, 0, false, false );
     194             : 
     195         262 :         inset = remainingStr;
     196         262 :         ConversionHelper::separatePair( value, remainingStr, inset, ',' );
     197             :         rTextBox.borderDistanceBottom = ConversionHelper::decodeMeasureToHmm( graphicHelper,
     198         262 :             value.isEmpty() ? "0.05in" : value, 0, false, false );
     199             : 
     200         524 :         rTextBox.borderDistanceSet = true;
     201             :     }
     202             : 
     203         262 :     OUString sStyle = rAttribs.getString( XML_style, OUString() );
     204         262 :     sal_Int32 nIndex = 0;
     205         807 :     while( nIndex >= 0 )
     206             :     {
     207         566 :         OUString aName, aValue;
     208         283 :         if( ConversionHelper::separatePair( aName, aValue, sStyle.getToken( 0, ';', nIndex ), ':' ) )
     209             :         {
     210         209 :             if( aName == "layout-flow" )      rTextBox.maLayoutFlow = aValue;
     211         204 :             else if (aName == "mso-fit-shape-to-text")
     212         148 :                 rTextBox.mrTypeModel.mbAutoHeight = true;
     213          56 :             else if (aName == "mso-layout-flow-alt")
     214           3 :                 rTextBox.mrTypeModel.maLayoutFlowAlt = aValue;
     215             :             else
     216             :                 SAL_WARN("oox", "unhandled style property: " << aName);
     217             :         }
     218         545 :     }
     219         262 : }
     220             : 
     221        1056 : ContextHandlerRef TextBoxContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
     222             : {
     223        1056 :     switch( getCurrentElement() )
     224             :     {
     225             :         case VML_TOKEN( textbox ):
     226         187 :             if( nElement == XML_div ) return this;
     227         185 :             else if (nElement == W_TOKEN(txbxContent)) return this;
     228           0 :         break;
     229             :         case XML_div:
     230           1 :             if( nElement == XML_font ) return new TextPortionContext( *this, mrTextBox, maParagraph, TextFontModel(), nElement, rAttribs );
     231           0 :         break;
     232             :         case W_TOKEN(txbxContent):
     233         198 :             if (nElement == W_TOKEN(p)) return this;
     234           0 :         break;
     235             :         case W_TOKEN(p):
     236             :         case W_TOKEN(sdtContent):
     237             :         case W_TOKEN(smartTag):
     238         305 :             if (nElement == W_TOKEN(r))
     239         208 :                 return new TextPortionContext( *this, mrTextBox, maParagraph, TextFontModel(), nElement, rAttribs );
     240             :             else
     241          97 :                 return this;
     242             :         break;
     243             :         case W_TOKEN(pPr):
     244             :         case W_TOKEN(sdt):
     245         140 :             return this;
     246             :         break;
     247             :         default:
     248             :             SAL_INFO("oox", "unhandled 0x" << std::hex << getCurrentElement());
     249         225 :         break;
     250             :     }
     251         225 :     return 0;
     252             : }
     253             : 
     254         884 : void TextBoxContext::onStartElement(const AttributeList& rAttribs)
     255             : {
     256         884 :     switch (getCurrentElement())
     257             :     {
     258             :         case W_TOKEN(jc):
     259          52 :             maParagraph.moParaAdjust = rAttribs.getString( W_TOKEN(val) );
     260          52 :         break;
     261             :     }
     262         884 : }
     263             : 
     264         884 : void TextBoxContext::onEndElement()
     265             : {
     266         884 :     if (getCurrentElement() == W_TOKEN(p))
     267             :     {
     268         198 :         mrTextBox.appendPortion( maParagraph, TextFontModel(), "\n" );
     269         198 :         maParagraph = TextParagraphModel();
     270             :     }
     271         884 : }
     272             : 
     273             : } // namespace vml
     274             : } // namespace oox
     275             : 
     276             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11