Subversion Repositories Scribus

Rev

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