os/textandloc/textrendering/texthandling/stext/TxtWriter.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/textrendering/texthandling/stext/TxtWriter.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,182 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "TxtWriter.h"
    1.23 +#include <txtetext.h>
    1.24 +#include "TXTPLAIN.H"
    1.25 +#include "OstTraceDefinitions.h"
    1.26 +#ifdef OST_TRACE_COMPILER_IN_USE
    1.27 +#include "TxtWriterTraces.h"
    1.28 +#endif
    1.29 +
    1.30 +
    1.31 +static const TText KLineBreak = 0x0A;//Used by TSLBTransaltor class - 0x0A, 0x0D, {0x0D, 0x0A}
    1.32 +                              //character aequences found in the input stream will be translated 
    1.33 +                              //to KLineBreak character.
    1.34 +
    1.35 +//////////////////////////////////////////////////////////////////////////////////////////////
    1.36 +//////////////////////////////////////////////////////////////////////////////////////////////
    1.37 +// TSLBTransaltor class
    1.38 +
    1.39 +/**
    1.40 +The method processes the input characters and translates 0x0D, 0x0A and 0x0D 0x0A characters
    1.41 +to line breaks. When the next output character is ready, the method calls WriteL() method of
    1.42 +the controlled MTextWriter interface (iTextWriter data member) with the output character 
    1.43 +as an argument. 
    1.44 +Note: Identified line breaks will be translated to 0x0A character. 0x0A character code is very
    1.45 +      appropriate for use because all 0x0A, 0x0D or {0x0D, 0x0A} character sequences are filtered
    1.46 +	  from the input stream and it is guaranteed that the output stream can't have any of them.
    1.47 +Note: The output character stream should not contain 0x0D character.
    1.48 +@param aChar Input character to be processed.
    1.49 +*/
    1.50 +void TSLBTransaltor::ProcessL(TText aChar)
    1.51 +	{
    1.52 +	if(aChar == 0x0A)
    1.53 +		{
    1.54 +		//Output a line break. It does not matter what is the previous character.
    1.55 +		//If it is 0x0D - line break should be the output (0x0D 0x0A). If it is something else - 
    1.56 +		//(i\x0A) again the output is a line break.
    1.57 +		iTextWriter.WriteL(KLineBreak);
    1.58 +		}
    1.59 +	else
    1.60 +		{
    1.61 +		if(iPrevChar == 0x0D)
    1.62 +			{
    1.63 +			//Previous character is 0x0D and current character is not 0x0A - then we have to output
    1.64 +			//a line break - the previous character was stored one call before and now has to be 
    1.65 +			//processed, if there is no 0x0A character.
    1.66 +			iTextWriter.WriteL(KLineBreak);
    1.67 +			}
    1.68 +		if(aChar != 0x0D)
    1.69 +			{
    1.70 +			//If current character is 0x0D, then it will be stored for further processing (in 
    1.71 +			//case if the next character is 0x0A). If current character is not 0x0D and not 
    1.72 +			//0x0A - then output it immediately.
    1.73 +			iTextWriter.WriteL(aChar);
    1.74 +			}
    1.75 +		}
    1.76 +	iPrevChar = aChar;
    1.77 +	}
    1.78 +
    1.79 +/**
    1.80 +The method immediately sends to the output any characters, left for further processing. 
    1.81 +The method shoud be called by the TSLBTransaltor's client after the processing of all 
    1.82 +input characters.
    1.83 +*/
    1.84 +void TSLBTransaltor::FlushL()
    1.85 +	{
    1.86 +	if(iPrevChar == 0x0D)
    1.87 +		{
    1.88 +		//The last input character is 0x0D and there won't be any more characters, so there is
    1.89 +		//not a chanse to find a matching 0x0A character. Output it as a single line break.
    1.90 +		iTextWriter.WriteL(KLineBreak);
    1.91 +		}
    1.92 +	//iTextWriter has an internal state too. Flush it.
    1.93 +	iTextWriter.FlushL();
    1.94 +	iPrevChar = 0;
    1.95 +	}
    1.96 +
    1.97 +//////////////////////////////////////////////////////////////////////////////////////////////
    1.98 +//////////////////////////////////////////////////////////////////////////////////////////////
    1.99 +// TParagraphTextWriter class
   1.100 +
   1.101 +/**
   1.102 +This method should be called only from TSLBTransaltor implementation. It gets the characters 
   1.103 +processed by TSLBTransaltor instance as an input and writes them to the output (using MOutputChar
   1.104 +interface - iOutputChar data member), translating line breaks to paragraph delimiters.
   1.105 +@param aChar Input character to be processed. It can't be 0x0D, but it could be 0x0A - identified
   1.106 +             line break.
   1.107 +*/
   1.108 +void TParagraphTextWriter::WriteL(TText aChar)
   1.109 +	{
   1.110 +	if (aChar == 0x0D)
   1.111 +	    {
   1.112 +	    OstTrace0( TRACE_DUMP, TPARAGRAPHTEXTWRITER_WRITEL, "Invariant" );
   1.113 +	    }
   1.114 +	__ASSERT_DEBUG(aChar != 0x0D, User::Invariant());
   1.115 +	if(aChar == KLineBreak)
   1.116 +		{
   1.117 +		iOutputChar.OutputCharL(CEditableText::EParagraphDelimiter);
   1.118 +		}
   1.119 +	else
   1.120 +		{
   1.121 +		iOutputChar.OutputCharL(aChar);
   1.122 +		}
   1.123 +	}
   1.124 +
   1.125 +//////////////////////////////////////////////////////////////////////////////////////////////
   1.126 +//////////////////////////////////////////////////////////////////////////////////////////////
   1.127 +// TLineTextWriter class
   1.128 +
   1.129 +/**
   1.130 +This method should be called only from TSLBTransaltor implementation. It gets the characters 
   1.131 +processed by TSLBTransaltor instance as an input and writes them to the output (using MOutputChar
   1.132 +interface - iOutputChar data member), translating line breaks to paragraph delimiters or spaces.
   1.133 +The translation rules are:
   1.134 + - single line break - space;
   1.135 + - double line break - paragraph delimiter;
   1.136 +@param aChar Input character to be processed. It can't be 0x0D, but it could be 0x0A - identified
   1.137 +             line break.
   1.138 +*/
   1.139 +void TLineTextWriter::WriteL(TText aChar)
   1.140 +	{
   1.141 +	if (aChar == 0x0D)
   1.142 +	    {
   1.143 +	    OstTrace0( TRACE_DUMP, TLINETEXTWRITER_WRITEL, "Invariant" );
   1.144 +	    }
   1.145 +	__ASSERT_DEBUG(aChar != 0x0D, User::Invariant());
   1.146 +	TText prevChar = iPrevChar;
   1.147 +	iPrevChar = aChar;
   1.148 +	if(aChar != KLineBreak)
   1.149 +		{
   1.150 +		if(prevChar == KLineBreak)
   1.151 +			{
   1.152 +			//Current character is not a line break, but the previous one is.
   1.153 +			//Then, it is a single line break - output a space.
   1.154 +			iOutputChar.OutputCharL(' ');
   1.155 +			}
   1.156 +		//Current character is not a line break - then just output it.
   1.157 +		iOutputChar.OutputCharL(aChar);
   1.158 +		}
   1.159 +	else
   1.160 +		{
   1.161 +		if(prevChar == KLineBreak)
   1.162 +			{
   1.163 +			//Current character is a line break, previous character is a line break too.
   1.164 +			//Double line break - output a paragraph delimiter.
   1.165 +			//Both characters are consumed, so iPrevChar is set to 0.
   1.166 +			iPrevChar = 0;
   1.167 +			iOutputChar.OutputCharL(CEditableText::EParagraphDelimiter);
   1.168 +			}
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +/**
   1.173 +The method immediately sends to the output any characters, left for further processing. 
   1.174 +This method should be called only from TSLBTransaltor implementation.
   1.175 +*/
   1.176 +void TLineTextWriter::FlushL()
   1.177 +	{
   1.178 +	if(iPrevChar == KLineBreak)
   1.179 +		{
   1.180 +		//There is no more input characters and the last input charactes is a line break.
   1.181 +		//Then, treat it as a single line break and output a space.
   1.182 +		iOutputChar.OutputCharL(' ');
   1.183 +		}
   1.184 +	iPrevChar = 0;
   1.185 +	}