summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoTextFormat.h
blob: 0e7c1a13a567aa64d4d3ca98de078ef5fbfdcc7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#ifndef _KOTEXTFORMAT_H
#define _KOTEXTFORMAT_H

// File included by korichtext.h

/* This file is part of the KDE project
   Copyright (C) 2001 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#undef S_NONE // Solaris defines it in sys/signal.h

#include <tqcolor.h>
#include <tqfont.h>
#include <tqstring.h>
#include <tqdict.h>
#include <koffice_export.h>

class KoGenStyle;
class TQFontMetrics;
class KoCharStyle;
class KoTextFormatCollection;
class KoTextZoomHandler;
class KoTextStringChar;
class KoTextParag;
class KoOasisContext;
class KoSavingContext;

/**
 * Each character (KoTextStringChar) points to a KoTextFormat that defines the
 * formatting of that character (font, bold, italic, underline, misspelled etc.).
 *
 * Formats are stored in KoTextFormatCollection and are shared for all
 * characters with the same format. The API rule is that a const KoTextFormat*
 * is a temporary format (out of collection) and a KoTextFormat* is a format
 * from the collection.
 */
class KOTEXT_EXPORT KoTextFormat
{
    friend class KoTextFormatCollection; // it sets 'collection'
    //friend class KoTextDocument;

    // Having it here allows inline methods returning d->blah, for speed
private:
class KoTextFormatPrivate
{
public:
    KoTextFormatPrivate() : m_screenFont( 0L ), m_screenFontMetrics( 0L ),
                            m_refFont( 0L ), m_refFontMetrics( 0L ),
                            m_refAscent( -1 ), m_refDescent( -1 ), m_refHeight( -1 )

    {
        memset( m_screenWidths, 0, 256 * sizeof( ushort ) );
        m_charStyle = 0L;
    }
    ~KoTextFormatPrivate()
    {
        clearCache();
    }
    void clearCache();
    // caching for speedup when formatting
    TQFont* m_screenFont; // font to be used when painting (zoom-dependent)
    TQFontMetrics* m_screenFontMetrics; // font metrics on screen (zoom-dependent)
    TQFont* m_refFont; // font to be used when formatting text for tqlayout units
    TQFontMetrics* m_refFontMetrics; // font metrics for m_refFontMetrics
    int m_refAscent;
    int m_refDescent;
    int m_refHeight;
    int m_offsetFromBaseLine;
    ushort m_screenWidths[ 256 ];
    // m_refWidths[ 256 ] would speed things up too, but ushort might not be enough for it
    double m_relativeTextSize;
    double m_underLineWidth;
    KoCharStyle *m_charStyle;

    double m_shadowDistanceX; // 0 in both x and y means no shadow
    double m_shadowDistanceY;
    TQColor m_shadowColor;
    bool m_bWordByWord;
    bool m_bHyphenation;
};

public:
    enum Flags {
	NoFlags,
	Bold = 1,
	Italic = 2,
	Underline = 4,
	Family = 8,
	Size = 16,
	Color = 32,
	Misspelled = 64,
	VAlign = 128,
        // 256 is free for use
        StrikeOut = 512, // style and type strikeout
        TextBackgroundColor = 1024,
        ExtendUnderLine = 2048, // color, style and type of underline
        Language = 4096,
        ShadowText = 8192,
        OffsetFromBaseLine = 16384,
        WordByWord = 32768,
        Attribute = 65536, // lower/upper/smallcaps
        Hyphenation = 131072,
        UnderLineWidth = 262144,

	Font = Bold | Italic | Underline | Family | Size,
        // Format means "everything"
	Format = Font | Color | Misspelled | VAlign | StrikeOut | TextBackgroundColor |
                 ExtendUnderLine | Language | ShadowText | OffsetFromBaseLine |
                 WordByWord | Attribute | Hyphenation | UnderLineWidth
    };

    enum VerticalAlignment { AlignNormal, AlignSubScript, AlignSuperScript, AlignCustom }; // TQRT now has it in another order, but it's too late, we use this order in KWord's file format now !
    enum UnderlineType { U_NONE = 0, U_SIMPLE = 1, U_DOUBLE = 2, U_SIMPLE_BOLD = 3, U_WAVE = 4};
    enum StrikeOutType { S_NONE = 0, S_SIMPLE = 1, S_DOUBLE = 2, S_SIMPLE_BOLD = 3};
    enum UnderlineStyle { U_SOLID = 0 , U_DASH = 1, U_DOT = 2, U_DASH_DOT = 3, U_DASH_DOT_DOT = 4};
    enum StrikeOutStyle { S_SOLID = 0 , S_DASH = 1, S_DOT = 2, S_DASH_DOT = 3, S_DASH_DOT_DOT = 4};

    enum AttributeStyle { ATT_NONE = 0, ATT_UPPER = 1, ATT_LOWER = 2 , ATT_SMALL_CAPS};


    KoTextFormat();
    ~KoTextFormat();

    /// A simple text format with some default settings
    /// Only used for the default format
    KoTextFormat( const TQFont &f, const TQColor &c, const TQString &_language,
                  bool hyphenation, KoTextFormatCollection *parent = 0 );

    /// A complete text format (used by KoFontDia)
    KoTextFormat( const TQFont &_font,
                  VerticalAlignment _valign,
                  const TQColor & _color,
                  const TQColor & _backGroundColor,
                  const TQColor & _underlineColor,
                  KoTextFormat::UnderlineType _underlineType,
                  KoTextFormat::UnderlineStyle _underlineStyle,
                  KoTextFormat::StrikeOutType _strikeOutType,
                  KoTextFormat::StrikeOutStyle _strikeOutStyle,
                  KoTextFormat::AttributeStyle _fontAttribute,
                  const TQString &_language,
                  double _relativeTextSize,
                  int _offsetFromBaseLine,
                  bool _wordByWord,
                  bool _hyphenation,
                  double _shadowDistanceX,
                  double _shadowDistanceY,
                  const TQColor& shadowColor );

    KoTextFormat( const KoTextFormat &fm );
    //KoTextFormat makeTextFormat( const TQStyleSheetItem *style, const TQMap<TQString,TQString>& attr ) const;
    KoTextFormat& operator=( const KoTextFormat &fm );
    void copyFormat( const KoTextFormat &fm, int flags );
    TQColor color() const;
    TQFont font() const;
    int pointSize() const { return font().pointSize(); }
    bool isMisspelled() const;
    VerticalAlignment vAlign() const;
    //int minLeftBearing() const;
    //int minRightBearing() const;
    /**
     * Return the width of one char (from a string, not necessarily from a paragraph) in LU pixels.
     * Do not call this for custom items, or for complex glyphs.
     * But this can still be used for ' ' (for parag counters), 'x' (for tabs) etc.
     */
    int width( const TQChar &c ) const;
    int width( const TQString &str, int pos ) const;
    int height() const; // in LU pixels
    int ascent() const; // in LU pixels
    int descent() const; // in LU pixels
    //bool useLinkColor() const;
    int offsetX() const; // in LU pixels
    int offsetY() const; // in LU pixels

    void setBold( bool b );
    void setItalic( bool b );
    void setUnderline( bool b );
    void setFamily( const TQString &f );
    void setPointSize( int s );
    void setFont( const TQFont &f );
    void setColor( const TQColor &c );
    void setMisspelled( bool b );
    void setVAlign( VerticalAlignment a );

    bool operator==( const KoTextFormat &f ) const;
    KoTextFormatCollection *parent() const;
    void setCollection( KoTextFormatCollection *parent ) { collection = parent; }
    TQString key() const;

    static TQString getKey( const TQFont &f, const TQColor &c, bool misspelled, VerticalAlignment vAlign );

    void addRef();
    void removeRef();

    /** Return a set of flags showing the differences between this and 'format' */
    int compare( const KoTextFormat & format ) const;

    /** Call this when a text color is set to 'invalid', meaning 'whatever the
     * default for the color scheme is' */
    static TQColor defaultTextColor( TQPainter * painter );

    void setStrikeOutType (StrikeOutType _type);
    StrikeOutType strikeOutType()const {return m_strikeOutType;}

    void setStrikeOutStyle( StrikeOutStyle _type );
    StrikeOutStyle strikeOutStyle()const {return m_strikeOutStyle;}


    void setTextBackgroundColor(const TQColor &);
    TQColor textBackgroundColor()const {return m_textBackColor;}

    void setTextUnderlineColor(const TQColor &);
    TQColor textUnderlineColor()const {return m_textUnderlineColor;}

    void setUnderlineType (UnderlineType _type);
    UnderlineType underlineType()const {return m_underlineType;}

    void setUnderlineStyle (UnderlineStyle _type);
    UnderlineStyle underlineStyle()const {return m_underlineStyle;}

    void setLanguage( const TQString & _lang);
    TQString language() const { return m_language;}

    void setHyphenation( bool b );
    bool hyphenation() const { return d->m_bHyphenation; }

    // This settings is a bit different - it's cached into the KoTextFormat,
    // but it's not directly settable by the user, nor loaded/saved.
    void setUnderLineWidth( double ulw );
    double underLineWidth() const { return d->m_underLineWidth; }


    void setAttributeFont( KoTextFormat::AttributeStyle _att );
    KoTextFormat::AttributeStyle attributeFont() const { return m_attributeFont;}


    double shadowDistanceX() const { return d->m_shadowDistanceX; }
    double shadowDistanceY() const { return d->m_shadowDistanceY; }
    TQColor shadowColor() const;
    /// Return the amount of pixels for the horizontal shadow distance at a given zoom level
    int shadowX( KoTextZoomHandler *zh ) const;
    /// Return the amount of pixels for the vertical shadow distance at a given zoom level
    int shadowY( KoTextZoomHandler *zh ) const;
    void setShadow( double shadowDistanceX, double shadowDistanceY, const TQColor& shadowColor );
    /// Return css string for the shadow, used when saving
    TQString shadowAsCss() const;
    static TQString shadowAsCss( double shadowDistanceX, double shadowDistanceY, const TQColor& shadowColor );
    /// Load shadow attributes from a css string, used when loading
    void parseShadowFromCss( const TQString& css );

    double relativeTextSize() const { return d->m_relativeTextSize;}
    void setRelativeTextSize( double _size );

    //we store this offset into as point => int
    int offsetFromBaseLine() const { return d->m_offsetFromBaseLine;}
    void setOffsetFromBaseLine( int _offset );

    bool wordByWord() const { return d->m_bWordByWord;}
    void setWordByWord( bool _b );

    bool doubleUnderline() const { return (m_underlineType==U_DOUBLE ); }
    bool waveUnderline() const { return (m_underlineType==U_WAVE ); }
    bool underline() const { return (m_underlineType==U_SIMPLE ); }
    bool strikeOut() const { return (m_strikeOutType==S_SIMPLE ); }
    bool doubleStrikeOut() const { return (m_strikeOutType==S_DOUBLE ); }
    bool isStrikedOrUnderlined() const { return ((m_underlineType != U_NONE) ||(m_strikeOutType!=S_NONE));}

    /**
     * @return the reference point size, i.e. the size specified by the user.
     * This is the one used during formatting, independently from the zoom level.
     * This method takes care of superscript and subscript (smaller font).
     */
    float refPointSize() const;

    /**
     * @return the point size to use on screen, given @p zh
     * This method takes care of superscript and subscript (smaller font).
     */
    float screenPointSize( const KoTextZoomHandler* zh ) const;

    /**
     * @return the metrics for the reference font, i.e. with the size specified by the user.
     * This is the one used during formatting, independently from the zoom level.
     * This method takes care of superscript and subscript (smaller font).
     */
    const TQFontMetrics& refFontMetrics() const;

    /**
     * Returns the font metrics for the font used at the zoom & resolution
     * given by 'zh'. Despite the name, this is probably valid for printing too.
     * This method takes care of superscript and subscript (smaller font).
     */
    const TQFontMetrics& screenFontMetrics( const KoTextZoomHandler* zh ) const;

    /**
     * @return the reference font, i.e. with the size specified by the user.
     * This is used at text tqlayout time (e.g. kotextformatter)
     */
    TQFont refFont() const;

    /**
     * Returns the font to be used at the zoom & resolution given by 'zh'.
     * Despite the name, this is probably valid for printing too.
     * This method takes care of superscript and subscript (smaller font).
     */
    TQFont screenFont( const KoTextZoomHandler* zh ) const;

    TQFont smallCapsFont( const KoTextZoomHandler* zh, bool applyZoom ) const;

    /**
     * Return the width of one char in one paragraph.
     * Used by KoTextFormatter twice: once for the 100% zoom pointsize (via charWidthLU),
     * and once for the current zoom pointsize.
     */
    int charWidth( const KoTextZoomHandler* zh, bool applyZoom, const KoTextStringChar* c,
                   const KoTextParag* parag, int i ) const;

    /**
     * Return the width of one char in LU pixels.
     * Equivalent to ptToLayoutUnitPt( charWidth( 0L, false, c, parag, i ) )
     */
    int charWidthLU( const KoTextStringChar* c,
                     const KoTextParag* parag, int i ) const;

    void applyCharStyle( KoCharStyle *_style );
    KoCharStyle *style() const;
    static TQString underlineStyleToString( UnderlineStyle _lineType );
    static TQString strikeOutStyleToString( StrikeOutStyle _lineType );
    static UnderlineStyle stringToUnderlineStyle( const TQString & _str );
    static StrikeOutStyle stringToStrikeOutStyle( const TQString & _str );

    static TQString attributeFontToString( KoTextFormat::AttributeStyle _attr );
    static AttributeStyle stringToAttributeFont( const TQString & _str );

    TQString displayedString( const TQString& c )const;
    static TQStringList underlineTypeList();
    static TQStringList strikeOutTypeList();
    static TQStringList fontAttributeList();
    static TQStringList underlineStyleList();
    static TQStringList strikeOutStyleList();

    /// Load a text format from OASIS XML
    void load( KoOasisContext& context );
    /// Save a text format to OASIS XML
    /// Only saves what differs from @refFormat, if set.
    void save( KoGenStyle& gs, KoSavingContext& context, KoTextFormat * refFormat = 0 ) const;

#ifndef NDEBUG
    void printDebug();
#endif

    /// Called when the zoom or resolution changes
    void zoomChanged();

protected:
    TQChar displayedChar( TQChar c )const;
    void generateKey();

private:
    void update();

    TQColor m_textBackColor;
    TQColor m_textUnderlineColor;
    UnderlineType m_underlineType;
    StrikeOutType m_strikeOutType;
    UnderlineStyle m_underlineStyle;
    StrikeOutStyle m_strikeOutStyle;
    TQString m_language;
    AttributeStyle m_attributeFont;
    class KoTextFormatPrivate;
    KoTextFormatPrivate *d;

    TQFont fn;
    TQColor col;
    uint missp : 1;
    //uint linkColor : 1;
    VerticalAlignment va;
    KoTextFormatCollection *collection;
    int ref;
    TQString m_key;
};

#if defined(TQ_TEMPLATEDLL)
// TQMOC_SKIP_BEGIN
template class TQ_EXPORT TQDict<KoTextFormat>;
// TQMOC_SKIP_END
#endif

class KOTEXT_EXPORT KoTextFormatCollection
{
    friend class KoTextDocument;
    friend class KoTextFormat;

public:
    KoTextFormatCollection();
    /** Constructor.
     * @param defaultFont the font to use for the default format
     * @param defaultLanguage the language to use for the default format
     * @param defaultHyphenation the hyphenation setting for the default format
     */
    KoTextFormatCollection( const TQFont& defaultFont, const TQColor& defaultColor,
                            const TQString & defaultLanguage, bool defaultHyphenation );
    /*virtual*/ ~KoTextFormatCollection();

    void setDefaultFormat( KoTextFormat *f );
    KoTextFormat *defaultFormat() const;
    /*virtual*/ KoTextFormat *format( const KoTextFormat *f );
    /*virtual*/ KoTextFormat *format( const KoTextFormat *of, const KoTextFormat *nf, int flags );
    // Only used for the default format
//    /*virtual*/ KoTextFormat *format( const TQFont &f, const TQColor &c , const TQString &_language, bool hyphen );
    /*virtual*/ void remove( KoTextFormat *f );
    /*virtual*/ KoTextFormat *createFormat( const KoTextFormat &f ) { return new KoTextFormat( f ); }
    // Only used for the default format
//    /*virtual*/ KoTextFormat *createFormat( const TQFont &f, const TQColor &c, const TQString & _language, bool hyphen ) { return new KoTextFormat( f, c, _language, hyphen, this ); }
    void debug();

    // Called when the zoom or resolution changes
    void zoomChanged();

    //void setPainter( TQPainter *p );
    //TQStyleSheet *styleSheet() const { return sheet; }
    //void setStyleSheet( TQStyleSheet *s ) { sheet = s; }
    //void updateStyles();
    //void updateFontSizes( int base );
    //void updateFontAttributes( const TQFont &f, const TQFont &old );

    TQDict<KoTextFormat> & dict() { return cKey; }

private:
    KoTextFormat *defFormat, *lastFormat, *cachedFormat;
    TQDict<KoTextFormat> cKey;
    KoTextFormat *cres;
    TQFont cfont;
    TQColor ccol;
    TQString kof, knf;
    int cflags;
    //TQStyleSheet *sheet;
};

inline TQColor KoTextFormat::color() const
{
    return col;
}

inline TQFont KoTextFormat::font() const
{
    return fn;
}

inline bool KoTextFormat::isMisspelled() const
{
    return missp;
}

inline KoTextFormat::VerticalAlignment KoTextFormat::vAlign() const
{
    return va;
}

inline bool KoTextFormat::operator==( const KoTextFormat &f ) const
{
    return key() == f.key();
}

inline KoTextFormatCollection *KoTextFormat::parent() const
{
    return collection;
}

//inline bool KoTextFormat::useLinkColor() const
//{
//    return linkColor;
//}

inline void KoTextFormatCollection::setDefaultFormat( KoTextFormat *f )
{
    defFormat = f;
}

inline KoTextFormat *KoTextFormatCollection::defaultFormat() const
{
    return defFormat;
}

#endif