|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "TxtWriter.h" |
|
20 #include <txtetext.h> |
|
21 #include "TXTPLAIN.H" |
|
22 |
|
23 static const TText KLineBreak = 0x0A;//Used by TSLBTransaltor class - 0x0A, 0x0D, {0x0D, 0x0A} |
|
24 //character aequences found in the input stream will be translated |
|
25 //to KLineBreak character. |
|
26 |
|
27 ////////////////////////////////////////////////////////////////////////////////////////////// |
|
28 ////////////////////////////////////////////////////////////////////////////////////////////// |
|
29 // TSLBTransaltor class |
|
30 |
|
31 /** |
|
32 The method processes the input characters and translates 0x0D, 0x0A and 0x0D 0x0A characters |
|
33 to line breaks. When the next output character is ready, the method calls WriteL() method of |
|
34 the controlled MTextWriter interface (iTextWriter data member) with the output character |
|
35 as an argument. |
|
36 Note: Identified line breaks will be translated to 0x0A character. 0x0A character code is very |
|
37 appropriate for use because all 0x0A, 0x0D or {0x0D, 0x0A} character sequences are filtered |
|
38 from the input stream and it is guaranteed that the output stream can't have any of them. |
|
39 Note: The output character stream should not contain 0x0D character. |
|
40 @param aChar Input character to be processed. |
|
41 */ |
|
42 void TSLBTransaltor::ProcessL(TText aChar) |
|
43 { |
|
44 if(aChar == 0x0A) |
|
45 { |
|
46 //Output a line break. It does not matter what is the previous character. |
|
47 //If it is 0x0D - line break should be the output (0x0D 0x0A). If it is something else - |
|
48 //(i\x0A) again the output is a line break. |
|
49 iTextWriter.WriteL(KLineBreak); |
|
50 } |
|
51 else |
|
52 { |
|
53 if(iPrevChar == 0x0D) |
|
54 { |
|
55 //Previous character is 0x0D and current character is not 0x0A - then we have to output |
|
56 //a line break - the previous character was stored one call before and now has to be |
|
57 //processed, if there is no 0x0A character. |
|
58 iTextWriter.WriteL(KLineBreak); |
|
59 } |
|
60 if(aChar != 0x0D) |
|
61 { |
|
62 //If current character is 0x0D, then it will be stored for further processing (in |
|
63 //case if the next character is 0x0A). If current character is not 0x0D and not |
|
64 //0x0A - then output it immediately. |
|
65 iTextWriter.WriteL(aChar); |
|
66 } |
|
67 } |
|
68 iPrevChar = aChar; |
|
69 } |
|
70 |
|
71 /** |
|
72 The method immediately sends to the output any characters, left for further processing. |
|
73 The method shoud be called by the TSLBTransaltor's client after the processing of all |
|
74 input characters. |
|
75 */ |
|
76 void TSLBTransaltor::FlushL() |
|
77 { |
|
78 if(iPrevChar == 0x0D) |
|
79 { |
|
80 //The last input character is 0x0D and there won't be any more characters, so there is |
|
81 //not a chanse to find a matching 0x0A character. Output it as a single line break. |
|
82 iTextWriter.WriteL(KLineBreak); |
|
83 } |
|
84 //iTextWriter has an internal state too. Flush it. |
|
85 iTextWriter.FlushL(); |
|
86 iPrevChar = 0; |
|
87 } |
|
88 |
|
89 ////////////////////////////////////////////////////////////////////////////////////////////// |
|
90 ////////////////////////////////////////////////////////////////////////////////////////////// |
|
91 // TParagraphTextWriter class |
|
92 |
|
93 /** |
|
94 This method should be called only from TSLBTransaltor implementation. It gets the characters |
|
95 processed by TSLBTransaltor instance as an input and writes them to the output (using MOutputChar |
|
96 interface - iOutputChar data member), translating line breaks to paragraph delimiters. |
|
97 @param aChar Input character to be processed. It can't be 0x0D, but it could be 0x0A - identified |
|
98 line break. |
|
99 */ |
|
100 void TParagraphTextWriter::WriteL(TText aChar) |
|
101 { |
|
102 __ASSERT_DEBUG(aChar != 0x0D, User::Invariant()); |
|
103 if(aChar == KLineBreak) |
|
104 { |
|
105 iOutputChar.OutputCharL(CEditableText::EParagraphDelimiter); |
|
106 } |
|
107 else |
|
108 { |
|
109 iOutputChar.OutputCharL(aChar); |
|
110 } |
|
111 } |
|
112 |
|
113 ////////////////////////////////////////////////////////////////////////////////////////////// |
|
114 ////////////////////////////////////////////////////////////////////////////////////////////// |
|
115 // TLineTextWriter class |
|
116 |
|
117 /** |
|
118 This method should be called only from TSLBTransaltor implementation. It gets the characters |
|
119 processed by TSLBTransaltor instance as an input and writes them to the output (using MOutputChar |
|
120 interface - iOutputChar data member), translating line breaks to paragraph delimiters or spaces. |
|
121 The translation rules are: |
|
122 - single line break - space; |
|
123 - double line break - paragraph delimiter; |
|
124 @param aChar Input character to be processed. It can't be 0x0D, but it could be 0x0A - identified |
|
125 line break. |
|
126 */ |
|
127 void TLineTextWriter::WriteL(TText aChar) |
|
128 { |
|
129 __ASSERT_DEBUG(aChar != 0x0D, User::Invariant()); |
|
130 TText prevChar = iPrevChar; |
|
131 iPrevChar = aChar; |
|
132 if(aChar != KLineBreak) |
|
133 { |
|
134 if(prevChar == KLineBreak) |
|
135 { |
|
136 //Current character is not a line break, but the previous one is. |
|
137 //Then, it is a single line break - output a space. |
|
138 iOutputChar.OutputCharL(' '); |
|
139 } |
|
140 //Current character is not a line break - then just output it. |
|
141 iOutputChar.OutputCharL(aChar); |
|
142 } |
|
143 else |
|
144 { |
|
145 if(prevChar == KLineBreak) |
|
146 { |
|
147 //Current character is a line break, previous character is a line break too. |
|
148 //Double line break - output a paragraph delimiter. |
|
149 //Both characters are consumed, so iPrevChar is set to 0. |
|
150 iPrevChar = 0; |
|
151 iOutputChar.OutputCharL(CEditableText::EParagraphDelimiter); |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 /** |
|
157 The method immediately sends to the output any characters, left for further processing. |
|
158 This method should be called only from TSLBTransaltor implementation. |
|
159 */ |
|
160 void TLineTextWriter::FlushL() |
|
161 { |
|
162 if(iPrevChar == KLineBreak) |
|
163 { |
|
164 //There is no more input characters and the last input charactes is a line break. |
|
165 //Then, treat it as a single line break and output a space. |
|
166 iOutputChar.OutputCharL(' '); |
|
167 } |
|
168 iPrevChar = 0; |
|
169 } |