Subversion Repositories Scribus

Rev

Rev 12340 | 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>
4669 avox 32
 
33
//#include "text/paragraphlayout.h"
6144 avox 34
#include "text/frect.h"
8175 avox 35
#include "text/specialchars.h"
7970 cbradney 36
#include "sctextstruct.h"
37
#include "style.h"
12337 pierre 38
// #include "styles/charstyle.h"
39
// #include "styles/paragraphstyle.h"
40
#include "styles/styleslibrary.h"
8194 avox 41
#include "desaxe/saxio.h"
4669 avox 42
 
43
#ifdef NLS_CONFORMANCE
44
#define NLS_PRIVATE private
45
#else
46
#define NLS_PRIVATE public
47
#endif
48
 
7970 cbradney 49
class CharStyle;
50
class ParagraphStyle;
51
class PageItem;
52
//class ScTextEngine;
53
//class ScScriptItem;
54
class ScribusDoc;
55
class ScText_Shared;
8455 avox 56
class ResourceCollection;
7970 cbradney 57
 
4669 avox 58
 
6144 avox 59
struct LineSpec
60
{
61
        double x;
62
        double y;
63
        double width;
64
        double ascent;
65
        double descent;
66
 
67
        int firstItem;
68
        int lastItem;
69
        double naturalWidth;
70
};
71
 
4669 avox 72
/**
73
 * This class holds the text of a Scribus textframe and pointers to its
74
 * styles and embedded objects.
75
 *
76
 * The logical view of the text consists of a sequence of Unicode chars.
77
 * Partition objects keep track of the positions of style changes,
78
 * paragraph ends and embedded objects.
79
 *
80
 * The physical view consists of a sequence of ScriptItems. Each ScriptItem
81
 * corresponds to a subsequence of Unicode chars in the original sequence
82
 * and associates this with an array of glyph indices. Metrics information
83
 * give the physical position of the ScriptItem in the textframe and its
84
 * bounding box. For each glyph there's also its advance and the relative
85
 * offsets to its basepoint. Other information in the ScriptItem is only
86
 * used by the layouter.
87
 */
8194 avox 88
class SCRIBUS_API StoryText : public QObject, public SaxIO
7953 cbradney 89
{
7942 avox 90
        Q_OBJECT
91
 
4669 avox 92
 public:
93
        StoryText(ScribusDoc * doc);
6144 avox 94
        StoryText();
4669 avox 95
        StoryText(const StoryText & other);
96
        StoryText& operator= (const StoryText & other);
97
        virtual ~StoryText();
8266 avox 98
 
99
        static const Xml_string saxxDefaultElem;
8834 cbradney 100
        static void  desaxeRules(const Xml_string& prefixPattern, desaxe::Digester& ruleset, Xml_string elemtag = saxxDefaultElem);
8266 avox 101
 
8800 jghali 102
        virtual void saxx(SaxHandler& handler, const Xml_string& elemtag) const;
8266 avox 103
        virtual void saxx(SaxHandler& handler)                     const { saxx(handler, saxxDefaultElem); }
104
 
4669 avox 105
        void clear();
6144 avox 106
        StoryText copy() const;
6887 avox 107
        void insert(int pos, const StoryText& other, bool onlySelection = false);
108
        void append(const StoryText& other) { insert(length(), other, false); }
4669 avox 109
        void removeChars(int pos, uint len);
9645 avox 110
        void insertChars(int pos, QString txt, bool applyNeighbourStyle = false);
4669 avox 111
        void insertObject(int pos, PageItem* obj);
112
        void replaceChar(int pos, QChar ch);
113
 
5559 avox 114
        void hyphenateWord(int pos, uint len, char* hyphens);
115
 
4669 avox 116
        int length() const;
117
        QChar text(int pos) const;
118
        QString text(int pos, uint len) const;
119
        PageItem* object(int pos) const;
5559 avox 120
 
121
        int nextCharPos(int c);
122
        int prevCharPos(int c);
123
        int nextWordPos(int c);
124
        int prevWordPos(int c);
125
        int nextLinePos(int c, double oldX);
126
        int prevLinePos(int c, double oldX);
127
        int nextFramePos(int c);
128
        int prevFramePos(int c);
129
 
12343 pierre 130
        const QString charStyle(int pos) const;
131
        const QString paragraphStyle(int pos) const;
132
        const QString defaultStyle() const;
12337 pierre 133
        void setDefaultStyle(const QString& style);
134
        void setCharStyle(int pos, uint len, const QString& style);
135
        void setStyle(int pos, const QString& style);
136
        void applyCharStyle(int pos, uint len, const QString& style);
12340 pierre 137
        void applyStyle(int pos, const QString& style);
138
        void eraseCharStyle(int pos, uint len, const QString& style);
139
        void eraseStyle(int pos, const QString& style);
7442 avox 140
        void replaceStyles(QMap<QString,QString> newNameForOld);
141
        void replaceCharStyles(QMap<QString,QString> newNameForOld);
142
 
8455 avox 143
        void getNamedResources(ResourceCollection& lists) const;
12343 pierre 144
//      void replaceNamedResources(ResourceCollection& newNames);
8455 avox 145
 
4669 avox 146
        uint nrOfParagraphs() const;
147
        int startOfParagraph(uint index) const;
148
        int endOfParagraph(uint index) const;
10623 avox 149
        int nrOfParagraph(uint index) const;
4669 avox 150
 
151
        uint nrOfRuns() const;
152
        int startOfRun(uint index) const;
153
        int endOfRun(uint index) const;
154
 
5184 avox 155
// positioning
156
        int nextChar(int pos);
157
        int prevChar(int pos);
158
        int nextWord(int pos);
159
        int prevWord(int pos);
160
        int nextSentence(int pos);
161
        int prevSentence(int pos);
162
        int nextParagraph(int pos);
163
        int prevParagraph(int pos);
164
 
165
// these need valid layout:
166
 
167
        int startOfLine(int pos);
168
        int endOfLine(int pos);
7501 avox 169
        int prevLine(int pos);
170
        int nextLine(int pos);
5184 avox 171
        int startOfFrame(int pos);
172
        int endOfFrame(int pos);
173
 
4669 avox 174
// selection
175
 
176
        void selectAll();
177
        void deselectAll();
178
        void removeSelection();
179
        void select(int pos, uint len, bool on = true);
180
        bool selected(int pos) const;
181
        int startOfSelection() const;
182
        int endOfSelection() const;
183
        int lengthOfSelection() const;
184
 
185
 
186
//  when physical view doesn't match logical view any more:
187
 
188
        /// call this if the shape of an embedded object changes (redos layout)
189
        void invalidateObject(const PageItem* embedded);
190
        /// call this if the shape of the paragraph changes (redos layout)
191
        void invalidateLayout();
7942 avox 192
 
193
public slots:
194
        /// call this if some logical style changes (redos shaping and layout)
4669 avox 195
        void invalidateAll();
196
 
197
 
198
 
7942 avox 199
public:
4669 avox 200
// physical view
201
 
5184 avox 202
        bool overflows() const;
4669 avox 203
        /// layouts the text -- FIXME: better interface for this
204
        int layout(int startItem);
205
        uint nrOfItems() const;
7442 avox 206
//private:
4669 avox 207
        ScText * item(uint index);
6144 avox 208
        const ScText * item(uint index) const;
7442 avox 209
public:
210
        ScText * item_p(uint index) { return item(index); }
4669 avox 211
//      void bidiReorder(uint firstItem, uint lastItem, uint indices[]) const;
212
        /** returns the Unicode string which belongs to this ScScriptItem */
213
        const QString itemText(uint index) const;
214
        /** returns the CharStyle which belongs to this ScScriptItem */
12343 pierre 215
        QString itemStyle(uint index) const;
4669 avox 216
        /// returns the character postion at the start of this item
217
        int startOfItem(uint index) const;
218
        /// returns the character position after this item
219
        int endOfItem(uint index) const;
220
//      const ScTextEngine * engineForRun(uint index) const;
221
//      const ScTextEngine * engineForItem(uint index) const;
222
 
223
//      ParagraphLayout layouter;
224
 
225
        int screenToPosition(FPoint coord) const;
6144 avox 226
        FRect  boundingBox(int pos, uint len = 1) const;
4669 avox 227
 
8452 jghali 228
        uint lines() const { return (uint) m_lines.count(); }
7501 avox 229
 
6144 avox 230
        LineSpec line(uint i) const { return m_lines[i]; }
231
 
7501 avox 232
        void appendLine(const LineSpec& ls)
233
        {
7521 avox 234
                assert( ls.firstItem >= 0 );
235
                assert( ls.firstItem < length() );
236
                assert( ls.lastItem >= 0 && ls.firstItem - ls.lastItem < 1 );
237
                assert( ls.lastItem < length() );
7501 avox 238
                m_lines.append(ls);
239
                if (lastFrameItem < firstFrameItem) {
240
                        firstFrameItem = ls.firstItem;
241
                        lastFrameItem = ls.lastItem;
242
                }
243
                else {
8562 jghali 244
                        firstFrameItem = qMin(firstFrameItem, ls.firstItem);
245
                        lastFrameItem = qMax(lastFrameItem, ls.lastItem);
7501 avox 246
                }
247
        }
248
 
249
        void clearLines()
250
        {
251
                m_lines.clear();
252
                firstFrameItem = 0;
253
                lastFrameItem = -1;
254
        }
255
 
256
        int firstInFrame() { return firstFrameItem; }
257
        int lastInFrame() { return lastFrameItem; }
258
 
259
private:
4669 avox 260
        ScribusDoc * doc;
261
        int selFirst, selLast;
262
        int firstFrameItem, lastFrameItem;
10366 jghali 263
        QList<LineSpec> m_lines;
6144 avox 264
        bool m_validLayout;
7501 avox 265
        double m_magicX;
266
        int m_lastMagicPos;
12093 jghali 267
 
268
        QString textWithSmartHyphens (int pos, uint len) const;
269
        void    insertCharsWithSmartHyphens(int pos, QString txt, bool applyNeighbourStyle = false);
6144 avox 270
 
4669 avox 271
        /// mark these runs as invalid, ie. need itemize and shaping
272
        void invalidate(int firstRun, int lastRun);
8175 avox 273
        void removeParSep(int pos);
274
        void insertParSep(int pos);
275
 
276
        //      int splitRun(int pos);
4669 avox 277
 
278
        /** bring physical view in sync with logical one.
279
         *  This gets called automatically from all physical view methods
280
         */
281
//      void validate();
282
        /// private data structure
5721 avox 283
        ScText_Shared * d;
4669 avox 284
        /// gives the physical view which was last given to the layouter
285
//      uint layouterVersion;
286
        /// is true after layout() has been exercised
287
//      bool layouterValid;
288
 };
7970 cbradney 289
 
290
 
4669 avox 291
#endif /*STORYTEXT_H_*/