1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 INCLUDED_SC_SOURCE_FILTER_INC_RICHSTRING_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_RICHSTRING_HXX
23 #include <oox/helper/refvector.hxx>
24 #include "stylesbuffer.hxx"
28 class ScEditEngineDefaulter
;
30 namespace com
{ namespace sun
{ namespace star
{
31 namespace text
{ class XText
; }
34 namespace oox
{ class SequenceInputStream
; }
39 /** Contains text data and font attributes for a part of a rich formatted string. */
40 class RichStringPortion
: public WorkbookHelper
43 explicit RichStringPortion( const WorkbookHelper
& rHelper
);
45 /** Sets text data for this portion. */
46 void setText( const OUString
& rText
);
47 /** Creates and returns a new font formatting object. */
48 FontRef
const & createFont();
49 /** Links this portion to a font object from the global font list. */
50 void setFontId( sal_Int32 nFontId
);
52 /** Final processing after import of all strings. */
53 void finalizeImport();
55 /** Returns the text data of this portion. */
56 const OUString
& getText() const { return maText
; }
57 /** Returns true, if the portion contains font formatting. */
58 bool hasFont() const { return mxFont
.get() != nullptr; }
60 /** Converts the portion and replaces or appends to the passed XText. */
62 const css::uno::Reference
< css::text::XText
>& rxText
,
64 void convert( ScEditEngineDefaulter
& rEE
, ESelection
& rSelection
, const oox::xls::Font
* pFont
);
66 void writeFontProperties(
67 const css::uno::Reference
< css::text::XText
>& rxText
) const;
70 OUString maText
; /// Portion text.
71 FontRef mxFont
; /// Embedded portion font, may be empty.
72 sal_Int32 mnFontId
; /// Link to global font list.
73 bool mbConverted
; /// Without repeatedly convert
76 typedef std::shared_ptr
< RichStringPortion
> RichStringPortionRef
;
78 /** Represents a position in a rich-string containing current font identifier.
80 This object stores the position of a formatted character in a rich-string
81 and the identifier of a font from the global font list used to format this
82 and the following characters. Used in binary filters only.
84 struct FontPortionModel
86 sal_Int32 mnPos
; /// First character in the string.
87 sal_Int32 mnFontId
; /// Font identifier for the next characters.
89 explicit FontPortionModel() : mnPos( 0 ), mnFontId( -1 ) {}
90 explicit FontPortionModel( sal_Int32 nPos
) : mnPos( nPos
), mnFontId( -1 ) {}
92 void read( SequenceInputStream
& rStrm
);
95 /** A vector with all font portions in a rich-string. */
96 class FontPortionModelList
{
97 ::std::vector
< FontPortionModel
> mvModels
;
100 explicit FontPortionModelList() : mvModels() {}
102 bool empty() const { return mvModels
.empty(); }
104 const FontPortionModel
& back() const { return mvModels
.back(); }
105 const FontPortionModel
& front() const { return mvModels
.front(); }
107 void push_back(const FontPortionModel
& rModel
) { mvModels
.push_back(rModel
); }
109 void insert(::std::vector
< FontPortionModel
>::iterator it
,
110 const FontPortionModel
& rModel
)
111 { mvModels
.insert(it
, rModel
); }
113 ::std::vector
< FontPortionModel
>::iterator
begin() { return mvModels
.begin(); }
115 /** Appends a rich-string font identifier. */
116 void appendPortion( const FontPortionModel
& rPortion
);
117 /** Reads count and font identifiers from the passed stream. */
118 void importPortions( SequenceInputStream
& rStrm
);
121 struct PhoneticDataModel
123 sal_Int32 mnFontId
; /// Font identifier for text formatting.
124 sal_Int32 mnType
; /// Phonetic text type.
125 sal_Int32 mnAlignment
; /// Phonetic portion alignment.
127 explicit PhoneticDataModel();
129 /** Sets the passed data from binary import. */
130 void setBiffData( sal_Int32 nType
, sal_Int32 nAlignment
);
133 class PhoneticSettings
: public WorkbookHelper
136 explicit PhoneticSettings( const WorkbookHelper
& rHelper
);
138 /** Imports phonetic settings from the phoneticPr element. */
139 void importPhoneticPr( const AttributeList
& rAttribs
);
140 /** Imports phonetic settings from the PHONETICPR record. */
141 void importPhoneticPr( SequenceInputStream
& rStrm
);
143 /** Imports phonetic settings from a rich string. */
144 void importStringData( SequenceInputStream
& rStrm
);
147 PhoneticDataModel maModel
;
150 /** Contains text data and positioning information for a phonetic text portion. */
151 class RichStringPhonetic
: public WorkbookHelper
154 explicit RichStringPhonetic( const WorkbookHelper
& rHelper
);
156 /** Sets text data for this phonetic portion. */
157 void setText( const OUString
& rText
);
158 /** Imports attributes of a phonetic run (rPh element). */
159 void importPhoneticRun( const AttributeList
& rAttribs
);
160 /** Sets the associated range in base text for this phonetic portion. */
161 void setBaseRange( sal_Int32 nBasePos
, sal_Int32 nBaseEnd
);
164 OUString maText
; /// Portion text.
165 sal_Int32 mnBasePos
; /// Start position in base text.
166 sal_Int32 mnBaseEnd
; /// One-past-end position in base text.
169 typedef std::shared_ptr
< RichStringPhonetic
> RichStringPhoneticRef
;
171 /** Represents a phonetic text portion in a rich-string with phonetic text.
172 Used in binary filters only. */
173 struct PhoneticPortionModel
175 sal_Int32 mnPos
; /// First character in phonetic text.
176 sal_Int32 mnBasePos
; /// First character in base text.
177 sal_Int32 mnBaseLen
; /// Number of characters in base text.
179 explicit PhoneticPortionModel() : mnPos( -1 ), mnBasePos( -1 ), mnBaseLen( 0 ) {}
180 explicit PhoneticPortionModel( sal_Int32 nPos
, sal_Int32 nBasePos
, sal_Int32 nBaseLen
) :
181 mnPos( nPos
), mnBasePos( nBasePos
), mnBaseLen( nBaseLen
) {}
183 void read( SequenceInputStream
& rStrm
);
186 /** A vector with all phonetic portions in a rich-string. */
187 class PhoneticPortionModelList
190 explicit PhoneticPortionModelList() : mvModels() {}
192 bool empty() const { return mvModels
.empty(); }
194 const PhoneticPortionModel
& back() const { return mvModels
.back(); }
196 void push_back(const PhoneticPortionModel
& rModel
) { mvModels
.push_back(rModel
); }
198 ::std::vector
< PhoneticPortionModel
>::const_iterator
begin() const { return mvModels
.begin(); }
200 /** Appends a rich-string phonetic portion. */
201 void appendPortion( const PhoneticPortionModel
& rPortion
);
202 /** Reads all phonetic portions from the passed stream. */
203 void importPortions( SequenceInputStream
& rStrm
);
206 ::std::vector
< PhoneticPortionModel
> mvModels
;
209 /** Contains string data and a list of formatting runs for a rich formatted string. */
210 class RichString
: public WorkbookHelper
213 explicit RichString( const WorkbookHelper
& rHelper
);
215 /** Appends and returns a portion object for a plain string (t element). */
216 RichStringPortionRef
importText();
217 /** Appends and returns a portion object for a new formatting run (r element). */
218 RichStringPortionRef
importRun();
219 /** Appends and returns a phonetic text object for a new phonetic run (rPh element). */
220 RichStringPhoneticRef
importPhoneticRun( const AttributeList
& rAttribs
);
221 /** Imports phonetic settings from the rPhoneticPr element. */
222 void importPhoneticPr( const AttributeList
& rAttribs
);
224 /** Imports a Unicode rich-string from the passed record stream. */
225 void importString( SequenceInputStream
& rStrm
, bool bRich
);
227 /** Final processing after import of all strings. */
228 void finalizeImport();
230 /** Tries to extract a plain string from this object. Returns the string,
231 if there is only one unformatted portion. */
232 bool extractPlainString(
234 const oox::xls::Font
* pFirstPortionFont
) const;
236 /** Converts the string and writes it into the passed XText, replace old contents of the text object,.
237 @param rxText The XText interface of the target object.
239 void convert( const css::uno::Reference
< css::text::XText
>& rxText
) const;
240 std::unique_ptr
<EditTextObject
> convert( ScEditEngineDefaulter
& rEE
, const oox::xls::Font
* pFont
) const;
243 /** Creates, appends, and returns a new empty string portion. */
244 RichStringPortionRef
createPortion();
245 /** Creates, appends, and returns a new empty phonetic text portion. */
246 RichStringPhoneticRef
createPhonetic();
248 /** Create base text portions from the passed string and character formatting. */
249 void createTextPortions( const OUString
& rText
, FontPortionModelList
& rPortions
);
250 /** Create phonetic text portions from the passed string and portion data. */
251 void createPhoneticPortions( const OUString
& rText
, PhoneticPortionModelList
& rPortions
, sal_Int32 nBaseLen
);
254 typedef RefVector
< RichStringPortion
> PortionVector
;
255 typedef RefVector
< RichStringPhonetic
> PhoneticVector
;
257 PortionVector maTextPortions
; /// String portions with font data.
258 PhoneticSettings maPhonSettings
; /// Phonetic settings for this string.
259 PhoneticVector maPhonPortions
; /// Phonetic text portions.
262 typedef std::shared_ptr
< RichString
> RichStringRef
;
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */