1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 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/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 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 . 18 */ 19 20 #pragma once 21 22 #include <rtl/ustring.hxx> 23 #include "xlstring.hxx" 24 25 // Byte/Unicode strings ======================================================= 26 27 class XclImpStream; 28 29 /** This class represents an unformatted or formatted string and provides importing from stream. */ 30 class XclImpString 31 { 32 public: 33 /** Constructs an empty string. */ 34 explicit XclImpString(); 35 /** Constructs an unformatted string. */ 36 explicit XclImpString( const OUString& rString ); 37 38 /** Reads a complete string from the passed stream. */ 39 void Read( XclImpStream& rStrm, XclStrFlags nFlags = XclStrFlags::NONE ); 40 41 /** Sets the passed string data. */ SetText(const OUString & rText)42 void SetText( const OUString& rText ) { maString = rText; } 43 /** Sets the passed formatting buffer. */ SetFormats(const XclFormatRunVec & rFormats)44 void SetFormats( const XclFormatRunVec& rFormats ) { maFormats = rFormats; } 45 /** Reads and appends the formatting information (run count and runs) from stream. */ ReadFormats(XclImpStream & rStrm)46 void ReadFormats( XclImpStream& rStrm ) { ReadFormats( rStrm, maFormats ); } 47 /** Reads and appends formatting runs from an OBJ or TXO record. */ ReadObjFormats(XclImpStream & rStrm,sal_uInt16 nFormatSize)48 void ReadObjFormats( XclImpStream& rStrm, sal_uInt16 nFormatSize ) { ReadObjFormats( rStrm, maFormats, nFormatSize ); } 49 50 /** Returns true, if the string is empty. */ IsEmpty() const51 bool IsEmpty() const { return maString.isEmpty(); } 52 /** Returns the pure text data of the string. */ GetText() const53 const OUString& GetText() const { return maString; } 54 55 /** Returns true, if the string contains formatting information. */ IsRich() const56 bool IsRich() const { return !maFormats.empty(); } 57 /** Returns the formatting run vector. */ GetFormats() const58 const XclFormatRunVec& GetFormats() const { return maFormats; } 59 60 /** Insert a formatting run to the passed format buffer. */ 61 static void AppendFormat( XclFormatRunVec& rFormats, sal_uInt16 nChar, sal_uInt16 nFontIdx ); 62 /** Reads and appends the formatting information (run count and runs) from stream. */ 63 static void ReadFormats( XclImpStream& rStrm, XclFormatRunVec& rFormats ); 64 /** Reads and appends nRunCount formatting runs from stream. */ 65 static void ReadFormats( XclImpStream& rStrm, XclFormatRunVec& rFormats, sal_uInt16 nRunCount ); 66 /** Reads and appends formatting runs from an OBJ or TXO record. */ 67 static void ReadObjFormats( XclImpStream& rStrm, XclFormatRunVec& rFormats, sal_uInt16 nFormatSize ); 68 69 private: 70 OUString maString; /// The text data of the string. 71 XclFormatRunVec maFormats; /// All formatting runs. 72 }; 73 74 // String iterator ============================================================ 75 76 /** Iterates over formatted string portions. */ 77 class XclImpStringIterator 78 { 79 public: 80 explicit XclImpStringIterator( const XclImpString& rString ); 81 82 /** Returns true, if the iterator references a valid text portion. */ Is() const83 bool Is() const { return mnTextBeg < mrText.getLength(); } 84 /** Returns the index of the current text portion. */ GetPortionIndex() const85 size_t GetPortionIndex() const { return mnPortion; } 86 /** Returns the string of the current text portion. */ 87 OUString GetPortionText() const; 88 /** Returns the font index of the current text portion. */ 89 sal_uInt16 GetPortionFont() const; 90 91 /** Moves iterator to next text portion. */ 92 XclImpStringIterator& operator++(); 93 94 private: 95 const OUString& mrText; /// The processed string. 96 const XclFormatRunVec& mrFormats; /// The vector of formatting runs. 97 size_t mnPortion; /// Current text portion. 98 sal_Int32 mnTextBeg; /// First character of current portion. 99 sal_Int32 mnTextEnd; /// First character of next portion. 100 size_t mnFormatsBeg; /// Formatting run index for current portion. 101 size_t mnFormatsEnd; /// Formatting run index for next portion. 102 }; 103 104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 105