Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / filter / inc / richstring.hxx
blob96065d3fbe23e429d41ab7fdeed212f48530b143
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef OOX_XLS_RICHSTRING_HXX
21 #define OOX_XLS_RICHSTRING_HXX
23 #include "oox/helper/refvector.hxx"
24 #include "stylesbuffer.hxx"
26 class EditTextObject;
27 struct ESelection;
28 class ScEditEngineDefaulter;
30 namespace com { namespace sun { namespace star {
31 namespace text { class XText; }
32 } } }
34 namespace oox {
35 namespace xls {
37 // ============================================================================
39 /** Flags used to specify import/export mode of strings. */
40 typedef sal_Int32 BiffStringFlags;
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.
49 // ============================================================================
51 /** Contains text data and font attributes for a part of a rich formatted string. */
52 class RichStringPortion : public WorkbookHelper
54 public:
55 explicit RichStringPortion( const WorkbookHelper& rHelper );
57 /** Sets text data for this portion. */
58 void setText( const 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 );
64 /** Final processing after import of all strings. */
65 void finalizeImport();
67 /** Returns the text data of this portion. */
68 inline const OUString& getText() const { return maText; }
69 /** Returns true, if the portion fontains font formatting. */
70 inline bool hasFont() const { return mxFont.get() != 0; }
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,
76 const Font* pFont, bool bReplace );
77 void convert( ScEditEngineDefaulter& rEE, ESelection& rSelection, const Font* pFont );
79 void writeFontProperties(
80 const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& rxText,
81 const Font* pFont ) const;
83 private:
84 OUString maText; /// Portion text.
85 FontRef mxFont; /// Embedded portion font, may be empty.
86 sal_Int32 mnFontId; /// Link to global font list.
89 typedef ::boost::shared_ptr< RichStringPortion > RichStringPortionRef;
91 // ----------------------------------------------------------------------------
93 enum BiffFontPortionMode
95 BIFF_FONTPORTION_8BIT, /// Font portion with 8-bit values.
96 BIFF_FONTPORTION_16BIT, /// Font portion with 16-bit values.
97 BIFF_FONTPORTION_OBJ /// Font portion in OBJ or TXO record.
100 // ----------------------------------------------------------------------------
102 /** Represents a position in a rich-string containing current font identifier.
104 This object stores the position of a formatted character in a rich-string
105 and the identifier of a font from the global font list used to format this
106 and the following characters. Used in binary filters only.
108 struct FontPortionModel
110 sal_Int32 mnPos; /// First character in the string.
111 sal_Int32 mnFontId; /// Font identifier for the next characters.
113 explicit inline FontPortionModel() : mnPos( 0 ), mnFontId( -1 ) {}
114 explicit inline FontPortionModel( sal_Int32 nPos, sal_Int32 nFontId ) :
115 mnPos( nPos ), mnFontId( nFontId ) {}
117 void read( SequenceInputStream& rStrm );
120 // ----------------------------------------------------------------------------
122 /** A vector with all font portions in a rich-string. */
123 class FontPortionModelList : public ::std::vector< FontPortionModel >
125 public:
126 inline explicit FontPortionModelList() {}
128 /** Appends a rich-string font identifier. */
129 void appendPortion( const FontPortionModel& rPortion );
130 /** Reads count and font identifiers from the passed stream. */
131 void importPortions( SequenceInputStream& rStrm );
134 // ============================================================================
136 struct PhoneticDataModel
138 sal_Int32 mnFontId; /// Font identifier for text formatting.
139 sal_Int32 mnType; /// Phonetic text type.
140 sal_Int32 mnAlignment; /// Phonetic portion alignment.
142 explicit PhoneticDataModel();
144 /** Sets the passed data from binary import. */
145 void setBiffData( sal_Int32 nType, sal_Int32 nAlignment );
148 // ----------------------------------------------------------------------------
150 class PhoneticSettings : public WorkbookHelper
152 public:
153 explicit PhoneticSettings( const WorkbookHelper& rHelper );
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 );
160 /** Imports phonetic settings from a rich string. */
161 void importStringData( SequenceInputStream& rStrm );
163 private:
164 PhoneticDataModel maModel;
167 // ============================================================================
169 /** Contains text data and positioning information for a phonetic text portion. */
170 class RichStringPhonetic : public WorkbookHelper
172 public:
173 explicit RichStringPhonetic( const WorkbookHelper& rHelper );
175 /** Sets text data for this phonetic portion. */
176 void setText( const OUString& rText );
177 /** Imports attributes of a phonetic run (rPh element). */
178 void importPhoneticRun( const AttributeList& rAttribs );
179 /** Sets the associated range in base text for this phonetic portion. */
180 void setBaseRange( sal_Int32 nBasePos, sal_Int32 nBaseEnd );
182 private:
183 OUString maText; /// Portion text.
184 sal_Int32 mnBasePos; /// Start position in base text.
185 sal_Int32 mnBaseEnd; /// One-past-end position in base text.
188 typedef ::boost::shared_ptr< RichStringPhonetic > RichStringPhoneticRef;
190 // ----------------------------------------------------------------------------
192 /** Represents a phonetic text portion in a rich-string with phonetic text.
193 Used in binary filters only. */
194 struct PhoneticPortionModel
196 sal_Int32 mnPos; /// First character in phonetic text.
197 sal_Int32 mnBasePos; /// First character in base text.
198 sal_Int32 mnBaseLen; /// Number of characters in base text.
200 explicit inline PhoneticPortionModel() : mnPos( -1 ), mnBasePos( -1 ), mnBaseLen( 0 ) {}
201 explicit inline PhoneticPortionModel( sal_Int32 nPos, sal_Int32 nBasePos, sal_Int32 nBaseLen ) :
202 mnPos( nPos ), mnBasePos( nBasePos ), mnBaseLen( nBaseLen ) {}
204 void read( SequenceInputStream& rStrm );
207 // ----------------------------------------------------------------------------
209 /** A vector with all phonetic portions in a rich-string. */
210 class PhoneticPortionModelList : public ::std::vector< PhoneticPortionModel >
212 public:
213 inline explicit PhoneticPortionModelList() {}
215 /** Appends a rich-string phonetic portion. */
216 void appendPortion( const PhoneticPortionModel& rPortion );
217 /** Reads all phonetic portions from the passed stream. */
218 void importPortions( SequenceInputStream& rStrm );
221 // ============================================================================
223 /** Contains string data and a list of formatting runs for a rich formatted string. */
224 class RichString : public WorkbookHelper
226 public:
227 explicit RichString( const WorkbookHelper& rHelper );
229 /** Appends and returns a portion object for a plain string (t element). */
230 RichStringPortionRef importText( const AttributeList& rAttribs );
231 /** Appends and returns a portion object for a new formatting run (r element). */
232 RichStringPortionRef importRun( const AttributeList& rAttribs );
233 /** Appends and returns a phonetic text object for a new phonetic run (rPh element). */
234 RichStringPhoneticRef importPhoneticRun( const AttributeList& rAttribs );
235 /** Imports phonetic settings from the rPhoneticPr element. */
236 void importPhoneticPr( const AttributeList& rAttribs );
238 /** Imports a Unicode rich-string from the passed record stream. */
239 void importString( SequenceInputStream& rStrm, bool bRich );
241 /** Final processing after import of all strings. */
242 void finalizeImport();
244 /** Tries to extract a plain string from this object. Returns the string,
245 if there is only one unformatted portion. */
246 bool extractPlainString(
247 OUString& orString,
248 const Font* pFirstPortionFont = 0 ) const;
250 /** Converts the string and writes it into the passed XText.
251 @param rxText The XText interface of the target object.
252 @param bReplaceOld True = replace old contents of the text object.
253 @param pFirstPortionFont Optional font providing additional rich-text
254 formatting for the first text portion, e.g. font escapement. */
255 void convert(
256 const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& rxText,
257 bool bReplaceOld,
258 const Font* pFirstPortionFont = 0 ) const;
259 ::EditTextObject* convert( ScEditEngineDefaulter& rEE, const Font* pFont ) const;
261 private:
262 /** Creates, appends, and returns a new empty string portion. */
263 RichStringPortionRef createPortion();
264 /** Creates, appends, and returns a new empty phonetic text portion. */
265 RichStringPhoneticRef createPhonetic();
267 /** Create base text portions from the passed string and character formatting. */
268 void createTextPortions( const OUString& rText, FontPortionModelList& rPortions );
269 /** Create phonetic text portions from the passed string and portion data. */
270 void createPhoneticPortions( const OUString& rText, PhoneticPortionModelList& rPortions, sal_Int32 nBaseLen );
272 private:
273 typedef RefVector< RichStringPortion > PortionVector;
274 typedef RefVector< RichStringPhonetic > PhoneticVector;
276 PortionVector maTextPortions; /// String portions with font data.
277 PhoneticSettings maPhonSettings; /// Phonetic settings for this string.
278 PhoneticVector maPhonPortions; /// Phonetic text portions.
281 typedef ::boost::shared_ptr< RichString > RichStringRef;
283 // ============================================================================
285 } // namespace xls
286 } // namespace oox
288 #endif
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */