lib

KoTextFormat.h
1#ifndef _KOTEXTFORMAT_H
2#define _KOTEXTFORMAT_H
3
4// File included by korichtext.h
5
6/* This file is part of the KDE project
7 Copyright (C) 2001 David Faure <faure@kde.org>
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#undef S_NONE // Solaris defines it in sys/signal.h
26
27#include <tqcolor.h>
28#include <tqfont.h>
29#include <tqstring.h>
30#include <tqdict.h>
31#include <koffice_export.h>
32
33class KoGenStyle;
34class TQFontMetrics;
35class KoCharStyle;
36class KoTextFormatCollection;
38class KoTextStringChar;
39class KoTextParag;
40class KoOasisContext;
41class KoSavingContext;
42
52class KOTEXT_EXPORT KoTextFormat
53{
54 friend class KoTextFormatCollection; // it sets 'collection'
55 //friend class KoTextDocument;
56
57 // Having it here allows inline methods returning d->blah, for speed
58private:
59class KoTextFormatPrivate
60{
61public:
62 KoTextFormatPrivate() : m_screenFont( 0L ), m_screenFontMetrics( 0L ),
63 m_refFont( 0L ), m_refFontMetrics( 0L ),
64 m_refAscent( -1 ), m_refDescent( -1 ), m_refHeight( -1 )
65
66 {
67 memset( m_screenWidths, 0, 256 * sizeof( ushort ) );
68 m_charStyle = 0L;
69 }
70 ~KoTextFormatPrivate()
71 {
72 clearCache();
73 }
74 void clearCache();
75 // caching for speedup when formatting
76 TQFont* m_screenFont; // font to be used when painting (zoom-dependent)
77 TQFontMetrics* m_screenFontMetrics; // font metrics on screen (zoom-dependent)
78 TQFont* m_refFont; // font to be used when formatting text for layout units
79 TQFontMetrics* m_refFontMetrics; // font metrics for m_refFontMetrics
80 int m_refAscent;
81 int m_refDescent;
82 int m_refHeight;
83 int m_offsetFromBaseLine;
84 ushort m_screenWidths[ 256 ];
85 // m_refWidths[ 256 ] would speed things up too, but ushort might not be enough for it
86 double m_relativeTextSize;
87 double m_underLineWidth;
88 KoCharStyle *m_charStyle;
89
90 double m_shadowDistanceX; // 0 in both x and y means no shadow
91 double m_shadowDistanceY;
92 TQColor m_shadowColor;
93 bool m_bWordByWord;
94 bool m_bHyphenation;
95};
96
97public:
98 enum Flags {
99 NoFlags,
100 Bold = 1,
101 Italic = 2,
102 Underline = 4,
103 Family = 8,
104 Size = 16,
105 Color = 32,
106 Misspelled = 64,
107 VAlign = 128,
108 // 256 is free for use
109 StrikeOut = 512, // style and type strikeout
110 TextBackgroundColor = 1024,
111 ExtendUnderLine = 2048, // color, style and type of underline
112 Language = 4096,
113 ShadowText = 8192,
114 OffsetFromBaseLine = 16384,
115 WordByWord = 32768,
116 Attribute = 65536, // lower/upper/smallcaps
117 Hyphenation = 131072,
118 UnderLineWidth = 262144,
119
120 Font = Bold | Italic | Underline | Family | Size,
121 // Format means "everything"
122 Format = Font | Color | Misspelled | VAlign | StrikeOut | TextBackgroundColor |
123 ExtendUnderLine | Language | ShadowText | OffsetFromBaseLine |
124 WordByWord | Attribute | Hyphenation | UnderLineWidth
125 };
126
127 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 !
128 enum UnderlineType { U_NONE = 0, U_SIMPLE = 1, U_DOUBLE = 2, U_SIMPLE_BOLD = 3, U_WAVE = 4};
129 enum StrikeOutType { S_NONE = 0, S_SIMPLE = 1, S_DOUBLE = 2, S_SIMPLE_BOLD = 3};
130 enum UnderlineStyle { U_SOLID = 0 , U_DASH = 1, U_DOT = 2, U_DASH_DOT = 3, U_DASH_DOT_DOT = 4};
131 enum StrikeOutStyle { S_SOLID = 0 , S_DASH = 1, S_DOT = 2, S_DASH_DOT = 3, S_DASH_DOT_DOT = 4};
132
133 enum AttributeStyle { ATT_NONE = 0, ATT_UPPER = 1, ATT_LOWER = 2 , ATT_SMALL_CAPS};
134
135
136 KoTextFormat();
138
141 KoTextFormat( const TQFont &f, const TQColor &c, const TQString &_language,
142 bool hyphenation, KoTextFormatCollection *parent = 0 );
143
145 KoTextFormat( const TQFont &_font,
146 VerticalAlignment _valign,
147 const TQColor & _color,
148 const TQColor & _backGroundColor,
149 const TQColor & _underlineColor,
150 KoTextFormat::UnderlineType _underlineType,
151 KoTextFormat::UnderlineStyle _underlineStyle,
152 KoTextFormat::StrikeOutType _strikeOutType,
153 KoTextFormat::StrikeOutStyle _strikeOutStyle,
154 KoTextFormat::AttributeStyle _fontAttribute,
155 const TQString &_language,
156 double _relativeTextSize,
157 int _offsetFromBaseLine,
158 bool _wordByWord,
159 bool _hyphenation,
160 double _shadowDistanceX,
161 double _shadowDistanceY,
162 const TQColor& shadowColor );
163
164 KoTextFormat( const KoTextFormat &fm );
165 //KoTextFormat makeTextFormat( const TQStyleSheetItem *style, const TQMap<TQString,TQString>& attr ) const;
166 KoTextFormat& operator=( const KoTextFormat &fm );
167 void copyFormat( const KoTextFormat &fm, int flags );
168 TQColor color() const;
169 TQFont font() const;
170 int pointSize() const { return font().pointSize(); }
171 bool isMisspelled() const;
172 VerticalAlignment vAlign() const;
173 //int minLeftBearing() const;
174 //int minRightBearing() const;
180 int width( const TQChar &c ) const;
181 int width( const TQString &str, int pos ) const;
182 int height() const; // in LU pixels
183 int ascent() const; // in LU pixels
184 int descent() const; // in LU pixels
185 //bool useLinkColor() const;
186 int offsetX() const; // in LU pixels
187 int offsetY() const; // in LU pixels
188
189 void setBold( bool b );
190 void setItalic( bool b );
191 void setUnderline( bool b );
192 void setFamily( const TQString &f );
193 void setPointSize( int s );
194 void setFont( const TQFont &f );
195 void setColor( const TQColor &c );
196 void setMisspelled( bool b );
197 void setVAlign( VerticalAlignment a );
198
199 bool operator==( const KoTextFormat &f ) const;
200 KoTextFormatCollection *parent() const;
201 void setCollection( KoTextFormatCollection *parent ) { collection = parent; }
202 TQString key() const;
203
204 static TQString getKey( const TQFont &f, const TQColor &c, bool misspelled, VerticalAlignment vAlign );
205
206 void addRef();
207 void removeRef();
208
210 int compare( const KoTextFormat & format ) const;
211
214 static TQColor defaultTextColor( TQPainter * painter );
215
216 void setStrikeOutType (StrikeOutType _type);
217 StrikeOutType strikeOutType()const {return m_strikeOutType;}
218
219 void setStrikeOutStyle( StrikeOutStyle _type );
220 StrikeOutStyle strikeOutStyle()const {return m_strikeOutStyle;}
221
222
223 void setTextBackgroundColor(const TQColor &);
224 TQColor textBackgroundColor()const {return m_textBackColor;}
225
226 void setTextUnderlineColor(const TQColor &);
227 TQColor textUnderlineColor()const {return m_textUnderlineColor;}
228
229 void setUnderlineType (UnderlineType _type);
230 UnderlineType underlineType()const {return m_underlineType;}
231
232 void setUnderlineStyle (UnderlineStyle _type);
233 UnderlineStyle underlineStyle()const {return m_underlineStyle;}
234
235 void setLanguage( const TQString & _lang);
236 TQString language() const { return m_language;}
237
238 void setHyphenation( bool b );
239 bool hyphenation() const { return d->m_bHyphenation; }
240
241 // This settings is a bit different - it's cached into the KoTextFormat,
242 // but it's not directly settable by the user, nor loaded/saved.
243 void setUnderLineWidth( double ulw );
244 double underLineWidth() const { return d->m_underLineWidth; }
245
246
247 void setAttributeFont( KoTextFormat::AttributeStyle _att );
248 KoTextFormat::AttributeStyle attributeFont() const { return m_attributeFont;}
249
250
251 double shadowDistanceX() const { return d->m_shadowDistanceX; }
252 double shadowDistanceY() const { return d->m_shadowDistanceY; }
253 TQColor shadowColor() const;
255 int shadowX( KoTextZoomHandler *zh ) const;
257 int shadowY( KoTextZoomHandler *zh ) const;
258 void setShadow( double shadowDistanceX, double shadowDistanceY, const TQColor& shadowColor );
260 TQString shadowAsCss() const;
261 static TQString shadowAsCss( double shadowDistanceX, double shadowDistanceY, const TQColor& shadowColor );
263 void parseShadowFromCss( const TQString& css );
264
265 double relativeTextSize() const { return d->m_relativeTextSize;}
266 void setRelativeTextSize( double _size );
267
268 //we store this offset into as point => int
269 int offsetFromBaseLine() const { return d->m_offsetFromBaseLine;}
270 void setOffsetFromBaseLine( int _offset );
271
272 bool wordByWord() const { return d->m_bWordByWord;}
273 void setWordByWord( bool _b );
274
275 bool doubleUnderline() const { return (m_underlineType==U_DOUBLE ); }
276 bool waveUnderline() const { return (m_underlineType==U_WAVE ); }
277 bool underline() const { return (m_underlineType==U_SIMPLE ); }
278 bool strikeOut() const { return (m_strikeOutType==S_SIMPLE ); }
279 bool doubleStrikeOut() const { return (m_strikeOutType==S_DOUBLE ); }
280 bool isStrikedOrUnderlined() const { return ((m_underlineType != U_NONE) ||(m_strikeOutType!=S_NONE));}
281
287 float refPointSize() const;
288
293 float screenPointSize( const KoTextZoomHandler* zh ) const;
294
300 const TQFontMetrics& refFontMetrics() const;
301
307 const TQFontMetrics& screenFontMetrics( const KoTextZoomHandler* zh ) const;
308
313 TQFont refFont() const;
314
320 TQFont screenFont( const KoTextZoomHandler* zh ) const;
321
322 TQFont smallCapsFont( const KoTextZoomHandler* zh, bool applyZoom ) const;
323
329 int charWidth( const KoTextZoomHandler* zh, bool applyZoom, const KoTextStringChar* c,
330 const KoTextParag* parag, int i ) const;
331
336 int charWidthLU( const KoTextStringChar* c,
337 const KoTextParag* parag, int i ) const;
338
339 void applyCharStyle( KoCharStyle *_style );
340 KoCharStyle *style() const;
341 static TQString underlineStyleToString( UnderlineStyle _lineType );
342 static TQString strikeOutStyleToString( StrikeOutStyle _lineType );
343 static UnderlineStyle stringToUnderlineStyle( const TQString & _str );
344 static StrikeOutStyle stringToStrikeOutStyle( const TQString & _str );
345
346 static TQString attributeFontToString( KoTextFormat::AttributeStyle _attr );
347 static AttributeStyle stringToAttributeFont( const TQString & _str );
348
349 TQString displayedString( const TQString& c )const;
350 static TQStringList underlineTypeList();
351 static TQStringList strikeOutTypeList();
352 static TQStringList fontAttributeList();
353 static TQStringList underlineStyleList();
354 static TQStringList strikeOutStyleList();
355
357 void load( KoOasisContext& context );
360 void save( KoGenStyle& gs, KoSavingContext& context, KoTextFormat * refFormat = 0 ) const;
361
362#ifndef NDEBUG
363 void printDebug();
364#endif
365
367 void zoomChanged();
368
369protected:
370 TQChar displayedChar( TQChar c )const;
371 void generateKey();
372
373private:
374 void update();
375
376 TQColor m_textBackColor;
377 TQColor m_textUnderlineColor;
378 UnderlineType m_underlineType;
379 StrikeOutType m_strikeOutType;
380 UnderlineStyle m_underlineStyle;
381 StrikeOutStyle m_strikeOutStyle;
382 TQString m_language;
383 AttributeStyle m_attributeFont;
384 class KoTextFormatPrivate;
385 KoTextFormatPrivate *d;
386
387 TQFont fn;
388 TQColor col;
389 uint missp : 1;
390 //uint linkColor : 1;
391 VerticalAlignment va;
392 KoTextFormatCollection *collection;
393 int ref;
394 TQString m_key;
395};
396
397#if defined(TQ_TEMPLATEDLL)
398// MOC_SKIP_BEGIN
399template class TQ_EXPORT TQDict<KoTextFormat>;
400// MOC_SKIP_END
401#endif
402
403class KOTEXT_EXPORT KoTextFormatCollection
404{
405 friend class KoTextDocument;
406 friend class KoTextFormat;
407
408public:
409 KoTextFormatCollection();
415 KoTextFormatCollection( const TQFont& defaultFont, const TQColor& defaultColor,
416 const TQString & defaultLanguage, bool defaultHyphenation );
417 /*virtual*/ ~KoTextFormatCollection();
418
419 void setDefaultFormat( KoTextFormat *f );
420 KoTextFormat *defaultFormat() const;
421 /*virtual*/ KoTextFormat *format( const KoTextFormat *f );
422 /*virtual*/ KoTextFormat *format( const KoTextFormat *of, const KoTextFormat *nf, int flags );
423 // Only used for the default format
424// /*virtual*/ KoTextFormat *format( const TQFont &f, const TQColor &c , const TQString &_language, bool hyphen );
425 /*virtual*/ void remove( KoTextFormat *f );
426 /*virtual*/ KoTextFormat *createFormat( const KoTextFormat &f ) { return new KoTextFormat( f ); }
427 // Only used for the default format
428// /*virtual*/ KoTextFormat *createFormat( const TQFont &f, const TQColor &c, const TQString & _language, bool hyphen ) { return new KoTextFormat( f, c, _language, hyphen, this ); }
429 void debug();
430
431 // Called when the zoom or resolution changes
432 void zoomChanged();
433
434 //void setPainter( TQPainter *p );
435 //TQStyleSheet *styleSheet() const { return sheet; }
436 //void setStyleSheet( TQStyleSheet *s ) { sheet = s; }
437 //void updateStyles();
438 //void updateFontSizes( int base );
439 //void updateFontAttributes( const TQFont &f, const TQFont &old );
440
441 TQDict<KoTextFormat> & dict() { return cKey; }
442
443private:
444 KoTextFormat *defFormat, *lastFormat, *cachedFormat;
445 TQDict<KoTextFormat> cKey;
446 KoTextFormat *cres;
447 TQFont cfont;
448 TQColor ccol;
449 TQString kof, knf;
450 int cflags;
451 //TQStyleSheet *sheet;
452};
453
454inline TQColor KoTextFormat::color() const
455{
456 return col;
457}
458
459inline TQFont KoTextFormat::font() const
460{
461 return fn;
462}
463
464inline bool KoTextFormat::isMisspelled() const
465{
466 return missp;
467}
468
469inline KoTextFormat::VerticalAlignment KoTextFormat::vAlign() const
470{
471 return va;
472}
473
474inline bool KoTextFormat::operator==( const KoTextFormat &f ) const
475{
476 return key() == f.key();
477}
478
479inline KoTextFormatCollection *KoTextFormat::parent() const
480{
481 return collection;
482}
483
484//inline bool KoTextFormat::useLinkColor() const
485//{
486// return linkColor;
487//}
488
489inline void KoTextFormatCollection::setDefaultFormat( KoTextFormat *f )
490{
491 defFormat = f;
492}
493
494inline KoTextFormat *KoTextFormatCollection::defaultFormat() const
495{
496 return defFormat;
497}
498
499#endif
A KoCharStyle is a set of formatting attributes (font, color, etc.) to be applied to a run of text.
A generic style, i.e.
Used during loading of Oasis format (and discarded at the end of the loading).
Used during saving to Oasis format (and discarded at the end of the saving).
Each character (KoTextStringChar) points to a KoTextFormat that defines the formatting of that charac...
This class extends KoZoomHandler to add support for WYSIWYG text layouting.