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