xref: /trunk/main/oox/inc/oox/xls/richstring.hxx (revision c0670b14)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_XLS_RICHSTRING_HXX
25 #define OOX_XLS_RICHSTRING_HXX
26 
27 #include "oox/helper/refvector.hxx"
28 #include "oox/xls/stylesbuffer.hxx"
29 
30 namespace com { namespace sun { namespace star {
31     namespace text { class XText; }
32 } } }
33 
34 namespace oox {
35 namespace xls {
36 
37 // ============================================================================
38 
39 /** Flags used to specify import/export mode of strings. */
40 typedef sal_Int32 BiffStringFlags;
41 
42 const BiffStringFlags BIFF_STR_DEFAULT      = 0x0000;   /// Default string settings.
43 const BiffStringFlags BIFF_STR_FORCEUNICODE = 0x0001;   /// Always use UCS-2 characters (default: try to compress). BIFF8 export only.
44 const BiffStringFlags BIFF_STR_8BITLENGTH   = 0x0002;   /// 8-bit string length field (default: 16-bit).
45 const BiffStringFlags BIFF_STR_SMARTFLAGS   = 0x0004;   /// Omit flags on empty string (default: read/write always). BIFF8 only.
46 const BiffStringFlags BIFF_STR_KEEPFONTS    = 0x0008;   /// Keep old fonts when reading unformatted string (default: clear fonts). Import only.
47 const BiffStringFlags BIFF_STR_EXTRAFONTS   = 0x0010;   /// Read trailing rich-string font array (default: nothing). BIFF2-BIFF5 import only.
48 
49 // ============================================================================
50 
51 /** Contains text data and font attributes for a part of a rich formatted string. */
52 class RichStringPortion : public WorkbookHelper
53 {
54 public:
55     explicit            RichStringPortion( const WorkbookHelper& rHelper );
56 
57     /** Sets text data for this portion. */
58     void                setText( const ::rtl::OUString& rText );
59     /** Creates and returns a new font formatting object. */
60     FontRef             createFont();
61     /** Links this portion to a font object from the global font list. */
62     void                setFontId( sal_Int32 nFontId );
63 
64     /** Final processing after import of all strings. */
65     void                finalizeImport();
66 
67     /** Returns the text data of this portion. */
getText() const68     inline const ::rtl::OUString& getText() const { return maText; }
69     /** Returns true, if the portion fontains font formatting. */
hasFont() const70     inline bool         hasFont() const { return mxFont.get() != 0; }
71 
72     /** Converts the portion and replaces or appends to the passed XText. */
73     void                convert(
74                             const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& rxText,
75                             const Font* pFont, bool bReplace );
76 
77 private:
78     ::rtl::OUString     maText;         /// Portion text.
79     FontRef             mxFont;         /// Embedded portion font, may be empty.
80     sal_Int32           mnFontId;       /// Link to global font list.
81     bool                mbConverted;    /// Without repeatly convert
82 };
83 
84 typedef ::boost::shared_ptr< RichStringPortion > RichStringPortionRef;
85 
86 // ----------------------------------------------------------------------------
87 
88 enum BiffFontPortionMode
89 {
90     BIFF_FONTPORTION_8BIT,              /// Font portion with 8-bit values.
91     BIFF_FONTPORTION_16BIT,             /// Font portion with 16-bit values.
92     BIFF_FONTPORTION_OBJ                /// Font portion in OBJ or TXO record.
93 };
94 
95 // ----------------------------------------------------------------------------
96 
97 /** Represents a position in a rich-string containing current font identifier.
98 
99     This object stores the position of a formatted character in a rich-string
100     and the identifier of a font from the global font list used to format this
101     and the following characters. Used in binary filters only.
102  */
103 struct FontPortionModel
104 {
105     sal_Int32           mnPos;          /// First character in the string.
106     sal_Int32           mnFontId;       /// Font identifier for the next characters.
107 
FontPortionModeloox::xls::FontPortionModel108     explicit inline     FontPortionModel() : mnPos( 0 ), mnFontId( -1 ) {}
FontPortionModeloox::xls::FontPortionModel109     explicit inline     FontPortionModel( sal_Int32 nPos, sal_Int32 nFontId ) :
110                             mnPos( nPos ), mnFontId( nFontId ) {}
111 
112     void                read( SequenceInputStream& rStrm );
113     void                read( BiffInputStream& rStrm, BiffFontPortionMode eMode );
114 };
115 
116 // ----------------------------------------------------------------------------
117 
118 /** A vector with all font portions in a rich-string. */
119 class FontPortionModelList : public ::std::vector< FontPortionModel >
120 {
121 public:
FontPortionModelList()122     inline explicit     FontPortionModelList() {}
123 
124     /** Appends a rich-string font identifier. */
125     void                appendPortion( const FontPortionModel& rPortion );
126     /** Reads count and font identifiers from the passed stream. */
127     void                importPortions( SequenceInputStream& rStrm );
128     /** Reads nCount font identifiers from the passed stream. */
129     void                importPortions( BiffInputStream& rStrm, sal_uInt16 nCount, BiffFontPortionMode eMode );
130     /** Reads count and font identifiers from the passed stream. */
131     void                importPortions( BiffInputStream& rStrm, bool b16Bit );
132 };
133 
134 // ============================================================================
135 
136 struct PhoneticDataModel
137 {
138     sal_Int32           mnFontId;       /// Font identifier for text formatting.
139     sal_Int32           mnType;         /// Phonetic text type.
140     sal_Int32           mnAlignment;    /// Phonetic portion alignment.
141 
142     explicit            PhoneticDataModel();
143 
144     /** Sets the passed data from binary import. */
145     void                setBiffData( sal_Int32 nType, sal_Int32 nAlignment );
146 };
147 
148 // ----------------------------------------------------------------------------
149 
150 class PhoneticSettings : public WorkbookHelper
151 {
152 public:
153     explicit            PhoneticSettings( const WorkbookHelper& rHelper );
154 
155     /** Imports phonetic settings from the phoneticPr element. */
156     void                importPhoneticPr( const AttributeList& rAttribs );
157     /** Imports phonetic settings from the PHONETICPR record. */
158     void                importPhoneticPr( SequenceInputStream& rStrm );
159     /** Imports phonetic settings from the PHONETICPR record. */
160     void                importPhoneticPr( BiffInputStream& rStrm );
161 
162     /** Imports phonetic settings from a rich string. */
163     void                importStringData( SequenceInputStream& rStrm );
164     /** Imports phonetic settings from a rich string. */
165     void                importStringData( BiffInputStream& rStrm );
166 
167 private:
168     PhoneticDataModel   maModel;
169 };
170 
171 // ============================================================================
172 
173 /** Contains text data and positioning information for a phonetic text portion. */
174 class RichStringPhonetic : public WorkbookHelper
175 {
176 public:
177     explicit            RichStringPhonetic( const WorkbookHelper& rHelper );
178 
179     /** Sets text data for this phonetic portion. */
180     void                setText( const ::rtl::OUString& rText );
181     /** Imports attributes of a phonetic run (rPh element). */
182     void                importPhoneticRun( const AttributeList& rAttribs );
183     /** Sets the associated range in base text for this phonetic portion. */
184     void                setBaseRange( sal_Int32 nBasePos, sal_Int32 nBaseEnd );
185 
186 private:
187     ::rtl::OUString     maText;         /// Portion text.
188     sal_Int32           mnBasePos;      /// Start position in base text.
189     sal_Int32           mnBaseEnd;      /// One-past-end position in base text.
190 };
191 
192 typedef ::boost::shared_ptr< RichStringPhonetic > RichStringPhoneticRef;
193 
194 // ----------------------------------------------------------------------------
195 
196 /** Represents a phonetic text portion in a rich-string with phonetic text.
197     Used in binary filters only. */
198 struct PhoneticPortionModel
199 {
200     sal_Int32           mnPos;          /// First character in phonetic text.
201     sal_Int32           mnBasePos;      /// First character in base text.
202     sal_Int32           mnBaseLen;      /// Number of characters in base text.
203 
PhoneticPortionModeloox::xls::PhoneticPortionModel204     explicit inline     PhoneticPortionModel() : mnPos( -1 ), mnBasePos( -1 ), mnBaseLen( 0 ) {}
PhoneticPortionModeloox::xls::PhoneticPortionModel205     explicit inline     PhoneticPortionModel( sal_Int32 nPos, sal_Int32 nBasePos, sal_Int32 nBaseLen ) :
206                             mnPos( nPos ), mnBasePos( nBasePos ), mnBaseLen( nBaseLen ) {}
207 
208     void                read( SequenceInputStream& rStrm );
209     void                read( BiffInputStream& rStrm );
210 };
211 
212 // ----------------------------------------------------------------------------
213 
214 /** A vector with all phonetic portions in a rich-string. */
215 class PhoneticPortionModelList : public ::std::vector< PhoneticPortionModel >
216 {
217 public:
PhoneticPortionModelList()218     inline explicit     PhoneticPortionModelList() {}
219 
220     /** Appends a rich-string phonetic portion. */
221     void                appendPortion( const PhoneticPortionModel& rPortion );
222     /** Reads all phonetic portions from the passed stream. */
223     void                importPortions( SequenceInputStream& rStrm );
224     /** Reads phonetic portion data from the passed stream. */
225     ::rtl::OUString     importPortions( BiffInputStream& rStrm, sal_Int32 nPhoneticSize );
226 };
227 
228 // ============================================================================
229 
230 /** Contains string data and a list of formatting runs for a rich formatted string. */
231 class RichString : public WorkbookHelper
232 {
233 public:
234     explicit            RichString( const WorkbookHelper& rHelper );
235 
236     /** Appends and returns a portion object for a plain string (t element). */
237     RichStringPortionRef importText( const AttributeList& rAttribs );
238     /** Appends and returns a portion object for a new formatting run (r element). */
239     RichStringPortionRef importRun( const AttributeList& rAttribs );
240     /** Appends and returns a phonetic text object for a new phonetic run (rPh element). */
241     RichStringPhoneticRef importPhoneticRun( const AttributeList& rAttribs );
242     /** Imports phonetic settings from the rPhoneticPr element. */
243     void                importPhoneticPr( const AttributeList& rAttribs );
244 
245     /** Imports a Unicode rich-string from the passed record stream. */
246     void                importString( SequenceInputStream& rStrm, bool bRich );
247 
248     /** Imports nChars byte characters from the passed BIFF stream and appends a new text portion. */
249     void                importCharArray( BiffInputStream& rStrm, sal_uInt16 nChars, rtl_TextEncoding eTextEnc );
250     /** Imports a byte string from the passed BIFF stream and appends new text portions. */
251     void                importByteString( BiffInputStream& rStrm, rtl_TextEncoding eTextEnc, BiffStringFlags nFlags = BIFF_STR_DEFAULT );
252     /** Imports a Unicode rich-string from the passed BIFF stream and appends new text portions. */
253     void                importUniString( BiffInputStream& rStrm, BiffStringFlags nFlags = BIFF_STR_DEFAULT );
254 
255     /** Final processing after import of all strings. */
256     void                finalizeImport();
257 
258     /** Tries to extract a plain string from this object. Returns the string,
259         if there is only one unformatted portion. */
260     bool                extractPlainString(
261                             ::rtl::OUString& orString,
262                             const Font* pFirstPortionFont = 0 ) const;
263 
264     /** Converts the string and writes it into the passed XText.
265         @param rxText  The XText interface of the target object.
266         @param bReplaceOld  True = replace old contents of the text object.
267         @param pFirstPortionFont  Optional font providing additional rich-text
268             formatting for the first text portion, e.g. font escapement. */
269     void                convert(
270                             const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& rxText,
271                             bool bReplaceOld,
272                             const Font* pFirstPortionFont = 0 ) const;
273 
274 private:
275     /** Creates, appends, and returns a new empty string portion. */
276     RichStringPortionRef createPortion();
277     /** Creates, appends, and returns a new empty phonetic text portion. */
278     RichStringPhoneticRef createPhonetic();
279 
280     /** Create base text portions from the passed string and character formatting. */
281     void                createTextPortions( const ::rtl::OString& rText, rtl_TextEncoding eTextEnc, FontPortionModelList& rPortions );
282     /** Create base text portions from the passed string and character formatting. */
283     void                createTextPortions( const ::rtl::OUString& rText, FontPortionModelList& rPortions );
284     /** Create phonetic text portions from the passed string and portion data. */
285     void                createPhoneticPortions( const ::rtl::OUString& rText, PhoneticPortionModelList& rPortions, sal_Int32 nBaseLen );
286 
287 private:
288     typedef RefVector< RichStringPortion >  PortionVector;
289     typedef RefVector< RichStringPhonetic > PhoneticVector;
290 
291     PortionVector       maTextPortions; /// String portions with font data.
292     PhoneticSettings    maPhonSettings; /// Phonetic settings for this string.
293     PhoneticVector      maPhonPortions; /// Phonetic text portions.
294 };
295 
296 typedef ::boost::shared_ptr< RichString > RichStringRef;
297 
298 // ============================================================================
299 
300 } // namespace xls
301 } // namespace oox
302 
303 #endif
304