Subversion Repositories Scribus

Rev

Rev 24236 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8175 avox 1
/*
2
 For general Scribus (>=1.3.2) copyright and licensing information please refer
3
 to the COPYING file provided with the program. Following this notice may exist
4
 a copyright and/or license notice that predates the release of Scribus 1.3.2
5
 for which a new license (GPL+exception) is in place.
6
 */
7
/***************************************************************************
8
pageitem.cpp  -  description
9
-------------------
10
    begin                : Sat Apr 7 2001
11
    copyright            : (C) 2001 by Franz Schmid
12
    email                : Franz.Schmid@altmuehlnet.de
13
        ***************************************************************************/
14
 
15
/***************************************************************************
16
*                                                                         *
17
*   This program is free software; you can redistribute it and/or modify  *
18
*   it under the terms of the GNU General Public License as published by  *
19
*   the Free Software Foundation; either version 2 of the License, or     *
20
*   (at your option) any later version.                                   *
21
*                                                                         *
22
***************************************************************************/
23
 
24
 
4669 avox 25
#ifndef STORYTEXT_H_
26
#define STORYTEXT_H_
27
 
22463 jghali 28
#include <cassert>
10223 cbradney 29
#include <QObject>
30
#include <QString>
10366 jghali 31
#include <QList>
23660 craig 32
#include <unicode/uversion.h>
22463 jghali 33
 
21563 jghali 34
#include "itextsource.h"
17826 craig 35
#include "marks.h"
6144 avox 36
#include "text/frect.h"
8175 avox 37
#include "text/specialchars.h"
7970 cbradney 38
#include "sctextstruct.h"
39
#include "style.h"
8337 avox 40
#include "styles/charstyle.h"
41
#include "styles/paragraphstyle.h"
8194 avox 42
#include "desaxe/saxio.h"
4669 avox 43
 
7970 cbradney 44
class CharStyle;
45
class ParagraphStyle;
46
class PageItem;
47
class ScribusDoc;
48
class ScText_Shared;
8455 avox 49
class ResourceCollection;
21563 jghali 50
class ShapedTextCache;
4669 avox 51
 
23660 craig 52
U_NAMESPACE_BEGIN
53
class BreakIterator;
54
U_NAMESPACE_END
6144 avox 55
 
4669 avox 56
/**
57
 * This class holds the text of a Scribus textframe and pointers to its
58
 * styles and embedded objects.
59
 *
60
 * The logical view of the text consists of a sequence of Unicode chars.
61
 * Partition objects keep track of the positions of style changes,
62
 * paragraph ends and embedded objects.
63
 *
64
 * The physical view consists of a sequence of ScriptItems. Each ScriptItem
65
 * corresponds to a subsequence of Unicode chars in the original sequence
66
 * and associates this with an array of glyph indices. Metrics information
67
 * give the physical position of the ScriptItem in the textframe and its
68
 * bounding box. For each glyph there's also its advance and the relative
69
 * offsets to its basepoint. Other information in the ScriptItem is only
70
 * used by the layouter.
71
 */
21563 jghali 72
class SCRIBUS_API StoryText : public QObject, public SaxIO, public ITextSource
7953 cbradney 73
{
7942 avox 74
        Q_OBJECT
75
 
22993 jghali 76
public:
17408 fschmid 77
        StoryText(ScribusDoc *doc);
22993 jghali 78
        StoryText();
79
        StoryText(const StoryText & other);
80
        StoryText& operator= (const StoryText & other);
81
        virtual ~StoryText();
23456 jghali 82
 
83
        bool hasBulletOrNum() const;
84
        bool hasTextMarks() const;
85
        bool marksCountChanged() const;
86
        void resetMarksCountChanged();
22993 jghali 87
 
17408 fschmid 88
        void setDoc(ScribusDoc *docin);
8266 avox 89
 
90
        static const Xml_string saxxDefaultElem;
8834 cbradney 91
        static void  desaxeRules(const Xml_string& prefixPattern, desaxe::Digester& ruleset, Xml_string elemtag = saxxDefaultElem);
8266 avox 92
 
8800 jghali 93
        virtual void saxx(SaxHandler& handler, const Xml_string& elemtag) const;
8266 avox 94
        virtual void saxx(SaxHandler& handler)                     const { saxx(handler, saxxDefaultElem); }
95
 
16639 jghali 96
        int  cursorPosition() const;
97
        void setCursorPosition(int pos, bool relative = false);
98
        void normalizeCursorPosition();
99
        int  normalizedCursorPosition();
100
 
21563 jghali 101
        void moveCursorForward();
102
        void moveCursorBackward();
103
        void moveCursorLeft();
104
        void moveCursorRight();
105
 
106
        void moveCursorWordLeft();
107
        void moveCursorWordRight();
108
 
22993 jghali 109
        void clear();
6144 avox 110
        StoryText copy() const;
17541 jghali 111
 
112
        // Find text in story
22127 jghali 113
        int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive, int* pLen = 0) const;
17541 jghali 114
        int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
17501 craig 115
 
116
        // Add, change, replace
16639 jghali 117
        // Insert chars from another StoryText object at current cursor position
16626 jghali 118
        void insert(const StoryText& other, bool onlySelection = false);
16639 jghali 119
        // Insert chars from another StoryText object at specific position
6887 avox 120
        void insert(int pos, const StoryText& other, bool onlySelection = false);
16639 jghali 121
        // Append chars from another StoryText object
6887 avox 122
        void append(const StoryText& other) { insert(length(), other, false); }
16639 jghali 123
        // Remove len chars at specific position
4669 avox 124
        void removeChars(int pos, uint len);
18843 fschmid 125
        // Removes trailing empty paragraphs
126
        void trim();
16639 jghali 127
        // Insert chars at current cursor position
22635 craig 128
        void insertChars(const QString& txt, bool applyNeighbourStyle = false);
16639 jghali 129
        // Insert chars ar specific position
22295 craig 130
        void insertChars(int pos, const QString& txt, bool applyNeighbourStyle = false);
16639 jghali 131
        // Insert inline object at current cursor position
17407 fschmid 132
        void insertObject(int obj);
16639 jghali 133
        // Insert object at specific position
17407 fschmid 134
        void insertObject(int pos, int obj);
17826 craig 135
        // Insert mark at cursor or specific position
23456 jghali 136
        void insertMark(Mark* mark, int pos = -1);
23396 jghali 137
        // Replace a character
4669 avox 138
        void replaceChar(int pos, QChar ch);
23396 jghali 139
        // Replace current selection with specified text
140
        void replaceSelection(const QString& newText);
17501 craig 141
        // Replaced a word, and return the difference in length between old and new
142
        int replaceWord(int pos, QString newWord);
17407 fschmid 143
        void replaceObject(int pos, int obj);
4669 avox 144
 
22639 craig 145
        void hyphenateWord(int pos, uint len, const char* hyphens);
5559 avox 146
 
17674 jghali 147
        // Retrieve length of story text
4669 avox 148
        int length() const;
17674 jghali 149
 
150
        // Get content at specific position as plain text
151
        // Internal paragraph separator are converted to 
152
        // unix new lines for better compatibility with
153
        // text editors
154
        QString plainText() const;
155
 
21563 jghali 156
        // TextSource methods 
157
 
158
        virtual bool isBlockStart(int pos) const;
159
        virtual int nextBlockStart(int pos) const;
160
        virtual InlineFrame object(int pos) const;
161
        virtual bool hasExpansionPoint(int pos) const;
162
        virtual ExpansionPoint expansionPoint(int pos) const;
163
 
16639 jghali 164
        // Get char at current cursor position
21563 jghali 165
//      QChar   text() const;
16639 jghali 166
        // Get char at specific position
16626 jghali 167
        QChar   text(int pos) const;
16639 jghali 168
        // Get text with len chars at specific position
22295 craig 169
        QString text(int pos, uint len) const;
24041 jghali 170
        // Get sentence at any position within it
17501 craig 171
        QString sentence(int pos, int &posn);
24041 jghali 172
        // Get word starting at position
173
        QString word(int pos);
16600 jghali 174
 
175
        bool hasObject(int pos) const;
22993 jghali 176
        PageItem* getItem(int pos) const; // deprecated
23625 jghali 177
        int  findMark(const Mark* mrk, int startPos = 0) const;
23440 jghali 178
        bool hasMark(int pos, const Mark* mrk = nullptr) const;
17826 craig 179
        Mark *mark(int pos) const;
22993 jghali 180
        void replaceMark(int pos, Mark* mrk);
21563 jghali 181
        void applyMarkCharstyle(Mark* mrk, CharStyle& currStyle) const;
18524 avox 182
 
21563 jghali 183
        bool isHighSurrogate(int pos) const;
184
        bool isLowSurrogate(int pos) const;
185
 
16639 jghali 186
        // Get charstyle at current cursor position
16626 jghali 187
        const CharStyle& charStyle() const;
16639 jghali 188
        // Get charstyle at specific position
4669 avox 189
        const CharStyle& charStyle(int pos) const;
16639 jghali 190
        // Get paragraph style at current cursor position
191
        const ParagraphStyle& paragraphStyle() const;
192
        // Get paragraph style at specific position
22993 jghali 193
        const ParagraphStyle& paragraphStyle(int pos) const;
194
        const ParagraphStyle& defaultStyle() const;
195
        void setDefaultStyle(const ParagraphStyle& style);
196
        void setCharStyle(int pos, uint len, const CharStyle& style);
197
        void setStyle(int pos, const ParagraphStyle& style);
198
        void applyCharStyle(int pos, uint len, const CharStyle& style);
199
        void applyStyle(int pos, const ParagraphStyle& style, bool rmDirectFormatting = false);
200
        void eraseCharStyle(int pos, uint len, const CharStyle& style);
201
        void eraseStyle(int pos, const ParagraphStyle& style);
22631 craig 202
        void replaceStyles(const QMap<QString,QString>& newNameForOld);
7442 avox 203
        void replaceCharStyles(QMap<QString,QString> newNameForOld);
16681 jghali 204
 
205
        // Cleanup legacy formatting for whole story, ie remove direct
206
        // formatting for parameters already set at paragraph level
207
        void fixLegacyFormatting();
7442 avox 208
 
16681 jghali 209
        // Cleanup legacy formatting for paragraph at position pos
210
        void fixLegacyFormatting(int pos);
211
 
8455 avox 212
        void getNamedResources(ResourceCollection& lists) const;
213
        void replaceNamedResources(ResourceCollection& newNames);
214
 
22993 jghali 215
        uint nrOfParagraphs() const;
16626 jghali 216
        int startOfParagraph() const;
22993 jghali 217
        int startOfParagraph(uint index) const;
16626 jghali 218
        int endOfParagraph() const;
22993 jghali 219
        int endOfParagraph(uint index) const;
16626 jghali 220
        uint nrOfParagraph() const;
16069 craig 221
        uint nrOfParagraph(int pos) const;
4669 avox 222
 
22993 jghali 223
        uint nrOfRuns() const;
224
        int startOfRun(uint index) const;
225
        int endOfRun(uint index) const;
4669 avox 226
 
5184 avox 227
// positioning
228
        int nextChar(int pos);
229
        int prevChar(int pos);
17878 jghali 230
        int firstWord();
5184 avox 231
        int nextWord(int pos);
232
        int prevWord(int pos);
17501 craig 233
        int endOfWord(int pos) const;
5184 avox 234
        int nextSentence(int pos);
235
        int prevSentence(int pos);
20558 craig 236
        int endOfSentence(int pos) const;
5184 avox 237
        int nextParagraph(int pos);
238
        int prevParagraph(int pos);
239
 
240
// these need valid layout:
241
 
18987 avox 242
//      int startOfLine(int pos);
243
//      int endOfLine(int pos);
244
//      int prevLine(int pos);
245
//      int nextLine(int pos);
246
//      int startOfFrame(int pos);
247
//      int endOfFrame(int pos);
5184 avox 248
 
4669 avox 249
// selection
250
 
22993 jghali 251
        void selectAll();
252
        void deselectAll();
253
        void removeSelection();
254
        void extendSelection(int oldPos, int newPos);
13054 pierre 255
        int selectWord(int pos);
22271 craig 256
        void select(int pos, int len, bool on = true);
23396 jghali 257
        QString selectedText() const;
22993 jghali 258
        bool selected(int pos) const;
4669 avox 259
        int startOfSelection() const;
260
        int endOfSelection() const;
22832 craig 261
        int selectionLength() const;
23940 jghali 262
        bool hasSelection() const;
23396 jghali 263
 
21563 jghali 264
        // break iterators
23660 craig 265
        static icu::BreakIterator* getGraphemeIterator();
266
        static icu::BreakIterator* getWordIterator();
267
        static icu::BreakIterator* getSentenceIterator();
268
        static icu::BreakIterator* getLineIterator();
24251 craig 269
 
18524 avox 270
// layout helpers
271
 
21563 jghali 272
        ShapedTextCache* shapedTextCache() { return m_shapedTextCache; }
273
 
18524 avox 274
        LayoutFlags flags(int pos) const;
275
        bool hasFlag(int pos, LayoutFlags flag) const;
276
        void setFlag(int pos, LayoutFlags flag);
277
        void clearFlag(int pos, LayoutFlags flag);
278
 
4669 avox 279
//  when physical view doesn't match logical view any more:
280
 
281
        /// call this if the shape of an embedded object changes (redos layout)
22993 jghali 282
        void invalidateObject(const PageItem* embedded);
283
        /// call this if the shape of the paragraph changes (redos layout)
284
        void invalidateLayout();
7942 avox 285
 
286
public slots:
287
        /// call this if some logical style changes (redos shaping and layout)
22993 jghali 288
        void invalidateAll();
4669 avox 289
 
18987 avox 290
signals:
21515 jghali 291
        void changed(int firstItem, int endItem);
4669 avox 292
 
18524 avox 293
private:
23534 jghali 294
        ScText * item(int index);
295
        const ScText * item(int index) const;
21563 jghali 296
        void fixSurrogateSelection();
7501 avox 297
 
298
private:
23626 jghali 299
        ScribusDoc * m_doc;
21563 jghali 300
        ShapedTextCache* m_shapedTextCache;
23660 craig 301
        static icu::BreakIterator* m_graphemeIterator;
302
        static icu::BreakIterator* m_wordIterator;
303
        static icu::BreakIterator* m_sentenceIterator;
304
        static icu::BreakIterator* m_lineIterator;
12093 jghali 305
 
15009 jghali 306
        QString textWithSoftHyphens (int pos, uint len) const;
22635 craig 307
        void    insertCharsWithSoftHyphens(int pos, const QString& txt, bool applyNeighbourStyle = false);
6144 avox 308
 
22993 jghali 309
        /// mark these runs as invalid, ie. need itemize and shaping
310
        void invalidate(int firstRun, int lastRun);
311
        void removeParSep(int pos);
312
        void insertParSep(int pos);
8175 avox 313
 
314
        //      int splitRun(int pos);
22993 jghali 315
 
316
        /** bring physical view in sync with logical one.
317
         *  This gets called automatically from all physical view methods
4669 avox 318
         */
319
//      void validate();
22993 jghali 320
        /// private data structure
321
        ScText_Shared * d;
322
        /// gives the physical view which was last given to the layouter
4669 avox 323
//      uint layouterVersion;
324
        /// is true after layout() has been exercised
325
//      bool layouterValid;
326
 };
7970 cbradney 327
 
328
 
4669 avox 329
#endif /*STORYTEXT_H_*/