xref: /aoo4110/main/oox/inc/oox/xls/richstring.hxx (revision b1cdbd2c)
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 };
82 
83 typedef ::boost::shared_ptr< RichStringPortion > RichStringPortionRef;
84 
85 // ----------------------------------------------------------------------------
86 
87 enum BiffFontPortionMode
88 {
89     BIFF_FONTPORTION_8BIT,              /// Font portion with 8-bit values.
90     BIFF_FONTPORTION_16BIT,             /// Font portion with 16-bit values.
91     BIFF_FONTPORTION_OBJ                /// Font portion in OBJ or TXO record.
92 };
93 
94 // ----------------------------------------------------------------------------
95 
96 /** Represents a position in a rich-string containing current font identifier.
97 
98     This object stores the position of a formatted character in a rich-string
99     and the identifier of a font from the global font list used to format this
100     and the following characters. Used in binary filters only.
101  */
102 struct FontPortionModel
103 {
104     sal_Int32           mnPos;          /// First character in the string.
105     sal_Int32           mnFontId;       /// Font identifier for the next characters.
106 
FontPortionModeloox::xls::FontPortionModel107     explicit inline     FontPortionModel() : mnPos( 0 ), mnFontId( -1 ) {}
FontPortionModeloox::xls::FontPortionModel108     explicit inline     FontPortionModel( sal_Int32 nPos, sal_Int32 nFontId ) :
109                             mnPos( nPos ), mnFontId( nFontId ) {}
110 
111     void                read( SequenceInputStream& rStrm );
112     void                read( BiffInputStream& rStrm, BiffFontPortionMode eMode );
113 };
114 
115 // ----------------------------------------------------------------------------
116 
117 /** A vector with all font portions in a rich-string. */
118 class FontPortionModelList : public ::std::vector< FontPortionModel >
119 {
120 public:
FontPortionModelList()121     inline explicit     FontPortionModelList() {}
122 
123     /** Appends a rich-string font identifier. */
124     void                appendPortion( const FontPortionModel& rPortion );
125     /** Reads count and font identifiers from the passed stream. */
126     void                importPortions( SequenceInputStream& rStrm );
127     /** Reads nCount font identifiers from the passed stream. */
128     void                importPortions( BiffInputStream& rStrm, sal_uInt16 nCount, BiffFontPortionMode eMode );
129     /** Reads count and font identifiers from the passed stream. */
130     void                importPortions( BiffInputStream& rStrm, bool b16Bit );
131 };
132 
133 // ============================================================================
134 
135 struct PhoneticDataModel
136 {
137     sal_Int32           mnFontId;       /// Font identifier for text formatting.
138     sal_Int32           mnType;         /// Phonetic text type.
139     sal_Int32           mnAlignment;    /// Phonetic portion alignment.
140 
141     explicit            PhoneticDataModel();
142 
143     /** Sets the passed data from binary import. */
144     void                setBiffData( sal_Int32 nType, sal_Int32 nAlignment );
145 };
146 
147 // ----------------------------------------------------------------------------
148 
149 class PhoneticSettings : public WorkbookHelper
150 {
151 public:
152     explicit            PhoneticSettings( const WorkbookHelper& rHelper );
153 
154     /** Imports phonetic settings from the phoneticPr element. */
155     void                importPhoneticPr( const AttributeList& rAttribs );
156     /** Imports phonetic settings from the PHONETICPR record. */
157     void                importPhoneticPr( SequenceInputStream& rStrm );
158     /** Imports phonetic settings from the PHONETICPR record. */
159     void                importPhoneticPr( BiffInputStream& rStrm );
160 
161     /** Imports phonetic settings from a rich string. */
162     void                importStringData( SequenceInputStream& rStrm );
163     /** Imports phonetic settings from a rich string. */
164     void                importStringData( BiffInputStream& rStrm );
165 
166 private:
167     PhoneticDataModel   maModel;
168 };
169 
170 // ============================================================================
171 
172 /** Contains text data and positioning information for a phonetic text portion. */
173 class RichStringPhonetic : public WorkbookHelper
174 {
175 public:
176     explicit            RichStringPhonetic( const WorkbookHelper& rHelper );
177 
178     /** Sets text data for this phonetic portion. */
179     void                setText( const ::rtl::OUString& rText );
180     /** Imports attributes of a phonetic run (rPh element). */
181     void                importPhoneticRun( const AttributeList& rAttribs );
182     /** Sets the associated range in base text for this phonetic portion. */
183     void                setBaseRange( sal_Int32 nBasePos, sal_Int32 nBaseEnd );
184 
185 private:
186     ::rtl::OUString     maText;         /// Portion text.
187     sal_Int32           mnBasePos;      /// Start position in base text.
188     sal_Int32           mnBaseEnd;      /// One-past-end position in base text.
189 };
190 
191 typedef ::boost::shared_ptr< RichStringPhonetic > RichStringPhoneticRef;
192 
193 // ----------------------------------------------------------------------------
194 
195 /** Represents a phonetic text portion in a rich-string with phonetic text.
196     Used in binary filters only. */
197 struct PhoneticPortionModel
198 {
199     sal_Int32           mnPos;          /// First character in phonetic text.
200     sal_Int32           mnBasePos;      /// First character in base text.
201     sal_Int32           mnBaseLen;      /// Number of characters in base text.
202 
PhoneticPortionModeloox::xls::PhoneticPortionModel203     explicit inline     PhoneticPortionModel() : mnPos( -1 ), mnBasePos( -1 ), mnBaseLen( 0 ) {}
PhoneticPortionModeloox::xls::PhoneticPortionModel204     explicit inline     PhoneticPortionModel( sal_Int32 nPos, sal_Int32 nBasePos, sal_Int32 nBaseLen ) :
205                             mnPos( nPos ), mnBasePos( nBasePos ), mnBaseLen( nBaseLen ) {}
206 
207     void                read( SequenceInputStream& rStrm );
208     void                read( BiffInputStream& rStrm );
209 };
210 
211 // ----------------------------------------------------------------------------
212 
213 /** A vector with all phonetic portions in a rich-string. */
214 class PhoneticPortionModelList : public ::std::vector< PhoneticPortionModel >
215 {
216 public:
PhoneticPortionModelList()217     inline explicit     PhoneticPortionModelList() {}
218 
219     /** Appends a rich-string phonetic portion. */
220     void                appendPortion( const PhoneticPortionModel& rPortion );
221     /** Reads all phonetic portions from the passed stream. */
222     void                importPortions( SequenceInputStream& rStrm );
223     /** Reads phonetic portion data from the passed stream. */
224     ::rtl::OUString     importPortions( BiffInputStream& rStrm, sal_Int32 nPhoneticSize );
225 };
226 
227 // ============================================================================
228 
229 /** Contains string data and a list of formatting runs for a rich formatted string. */
230 class RichString : public WorkbookHelper
231 {
232 public:
233     explicit            RichString( const WorkbookHelper& rHelper );
234 
235     /** Appends and returns a portion object for a plain string (t element). */
236     RichStringPortionRef importText( const AttributeList& rAttribs );
237     /** Appends and returns a portion object for a new formatting run (r element). */
238     RichStringPortionRef importRun( const AttributeList& rAttribs );
239     /** Appends and returns a phonetic text object for a new phonetic run (rPh element). */
240     RichStringPhoneticRef importPhoneticRun( const AttributeList& rAttribs );
241     /** Imports phonetic settings from the rPhoneticPr element. */
242     void                importPhoneticPr( const AttributeList& rAttribs );
243 
244     /** Imports a Unicode rich-string from the passed record stream. */
245     void                importString( SequenceInputStream& rStrm, bool bRich );
246 
247     /** Imports nChars byte characters from the passed BIFF stream and appends a new text portion. */
248     void                importCharArray( BiffInputStream& rStrm, sal_uInt16 nChars, rtl_TextEncoding eTextEnc );
249     /** Imports a byte string from the passed BIFF stream and appends new text portions. */
250     void                importByteString( BiffInputStream& rStrm, rtl_TextEncoding eTextEnc, BiffStringFlags nFlags = BIFF_STR_DEFAULT );
251     /** Imports a Unicode rich-string from the passed BIFF stream and appends new text portions. */
252     void                importUniString( BiffInputStream& rStrm, BiffStringFlags nFlags = BIFF_STR_DEFAULT );
253 
254     /** Final processing after import of all strings. */
255     void                finalizeImport();
256 
257     /** Tries to extract a plain string from this object. Returns the string,
258         if there is only one unformatted portion. */
259     bool                extractPlainString(
260                             ::rtl::OUString& orString,
261                             const Font* pFirstPortionFont = 0 ) const;
262 
263     /** Converts the string and writes it into the passed XText.
264         @param rxText  The XText interface of the target object.
265         @param bReplaceOld  True = replace old contents of the text object.
266         @param pFirstPortionFont  Optional font providing additional rich-text
267             formatting for the first text portion, e.g. font escapement. */
268     void                convert(
269                             const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& rxText,
270                             bool bReplaceOld,
271                             const Font* pFirstPortionFont = 0 ) const;
272 
273 private:
274     /** Creates, appends, and returns a new empty string portion. */
275     RichStringPortionRef createPortion();
276     /** Creates, appends, and returns a new empty phonetic text portion. */
277     RichStringPhoneticRef createPhonetic();
278 
279     /** Create base text portions from the passed string and character formatting. */
280     void                createTextPortions( const ::rtl::OString& rText, rtl_TextEncoding eTextEnc, FontPortionModelList& rPortions );
281     /** Create base text portions from the passed string and character formatting. */
282     void                createTextPortions( const ::rtl::OUString& rText, FontPortionModelList& rPortions );
283     /** Create phonetic text portions from the passed string and portion data. */
284     void                createPhoneticPortions( const ::rtl::OUString& rText, PhoneticPortionModelList& rPortions, sal_Int32 nBaseLen );
285 
286 private:
287     typedef RefVector< RichStringPortion >  PortionVector;
288     typedef RefVector< RichStringPhonetic > PhoneticVector;
289 
290     PortionVector       maTextPortions; /// String portions with font data.
291     PhoneticSettings    maPhonSettings; /// Phonetic settings for this string.
292     PhoneticVector      maPhonPortions; /// Phonetic text portions.
293 };
294 
295 typedef ::boost::shared_ptr< RichString > RichStringRef;
296 
297 // ============================================================================
298 
299 } // namespace xls
300 } // namespace oox
301 
302 #endif
303