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