00001 #ifndef _KOTEXTFORMAT_H
00002 #define _KOTEXTFORMAT_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #undef S_NONE // Solaris defines it in sys/signal.h
00026
00027 #include <QColor>
00028 #include <QFont>
00029 #include <QString>
00030 #include <q3dict.h>
00031 #include <koffice_export.h>
00032
00033 class KoGenStyle;
00034 class QFontMetrics;
00035 class KoCharStyle;
00036 class KoTextFormatCollection;
00037 class KoTextZoomHandler;
00038 class KoTextStringChar;
00039 class KoTextParag;
00040 class KoOasisContext;
00041 class KoSavingContext;
00042
00052 class KOTEXT_EXPORT KoTextFormat
00053 {
00054 friend class KoTextFormatCollection;
00055
00056
00057
00058 private:
00059 class KoTextFormatPrivate
00060 {
00061 public:
00062 KoTextFormatPrivate() : m_screenFont( 0L ), m_screenFontMetrics( 0L ),
00063 m_refFont( 0L ), m_refFontMetrics( 0L ),
00064 m_refAscent( -1 ), m_refDescent( -1 ), m_refHeight( -1 )
00065
00066 {
00067 memset( m_screenWidths, 0, 256 * sizeof( ushort ) );
00068 m_charStyle = 0L;
00069 }
00070 ~KoTextFormatPrivate()
00071 {
00072 clearCache();
00073 }
00074 void clearCache();
00075
00076 QFont* m_screenFont;
00077 QFontMetrics* m_screenFontMetrics;
00078 QFont* m_refFont;
00079 QFontMetrics* m_refFontMetrics;
00080 int m_refAscent;
00081 int m_refDescent;
00082 int m_refHeight;
00083 int m_offsetFromBaseLine;
00084 ushort m_screenWidths[ 256 ];
00085
00086 double m_relativeTextSize;
00087 double m_underLineWidth;
00088 KoCharStyle *m_charStyle;
00089
00090 double m_shadowDistanceX;
00091 double m_shadowDistanceY;
00092 QColor m_shadowColor;
00093 bool m_bWordByWord;
00094 bool m_bHyphenation;
00095 };
00096
00097 public:
00098 enum Flags {
00099 NoFlags,
00100 Bold = 1,
00101 Italic = 2,
00102 Underline = 4,
00103 Family = 8,
00104 Size = 16,
00105 Color = 32,
00106 Misspelled = 64,
00107 VAlign = 128,
00108
00109 StrikeOut = 512,
00110 TextBackgroundColor = 1024,
00111 ExtendUnderLine = 2048,
00112 Language = 4096,
00113 ShadowText = 8192,
00114 OffsetFromBaseLine = 16384,
00115 WordByWord = 32768,
00116 Attribute = 65536,
00117 Hyphenation = 131072,
00118 UnderLineWidth = 262144,
00119
00120 Font = Bold | Italic | Underline | Family | Size,
00121
00122 Format = Font | Color | Misspelled | VAlign | StrikeOut | TextBackgroundColor |
00123 ExtendUnderLine | Language | ShadowText | OffsetFromBaseLine |
00124 WordByWord | Attribute | Hyphenation | UnderLineWidth
00125 };
00126
00127 enum VerticalAlignment { AlignNormal, AlignSubScript, AlignSuperScript, AlignCustom };
00128 enum UnderlineType { U_NONE = 0, U_SIMPLE = 1, U_DOUBLE = 2, U_SIMPLE_BOLD = 3, U_WAVE = 4};
00129 enum StrikeOutType { S_NONE = 0, S_SIMPLE = 1, S_DOUBLE = 2, S_SIMPLE_BOLD = 3};
00130 enum UnderlineStyle { U_SOLID = 0 , U_DASH = 1, U_DOT = 2, U_DASH_DOT = 3, U_DASH_DOT_DOT = 4};
00131 enum StrikeOutStyle { S_SOLID = 0 , S_DASH = 1, S_DOT = 2, S_DASH_DOT = 3, S_DASH_DOT_DOT = 4};
00132
00133 enum AttributeStyle { ATT_NONE = 0, ATT_UPPER = 1, ATT_LOWER = 2 , ATT_SMALL_CAPS};
00134
00135
00136 KoTextFormat();
00137 ~KoTextFormat();
00138
00141 KoTextFormat( const QFont &f, const QColor &c, const QString &_language,
00142 bool hyphenation, KoTextFormatCollection *parent = 0 );
00143
00145 KoTextFormat( const QFont &_font,
00146 VerticalAlignment _valign,
00147 const QColor & _color,
00148 const QColor & _backGroundColor,
00149 const QColor & _underlineColor,
00150 KoTextFormat::UnderlineType _underlineType,
00151 KoTextFormat::UnderlineStyle _underlineStyle,
00152 KoTextFormat::StrikeOutType _strikeOutType,
00153 KoTextFormat::StrikeOutStyle _strikeOutStyle,
00154 KoTextFormat::AttributeStyle _fontAttribute,
00155 const QString &_language,
00156 double _relativeTextSize,
00157 int _offsetFromBaseLine,
00158 bool _wordByWord,
00159 bool _hyphenation,
00160 double _shadowDistanceX,
00161 double _shadowDistanceY,
00162 const QColor& shadowColor );
00163
00164 KoTextFormat( const KoTextFormat &fm );
00165
00166 KoTextFormat& operator=( const KoTextFormat &fm );
00167 void copyFormat( const KoTextFormat &fm, int flags );
00168 QColor color() const;
00169 QFont font() const;
00170 int pointSize() const { return font().pointSize(); }
00171 bool isMisspelled() const;
00172 VerticalAlignment vAlign() const;
00173
00174
00180 int width( const QChar &c ) const;
00181 int width( const QString &str, int pos ) const;
00182 int height() const;
00183 int ascent() const;
00184 int descent() const;
00185
00186 int offsetX() const;
00187 int offsetY() const;
00188
00189 void setBold( bool b );
00190 void setItalic( bool b );
00191 void setUnderline( bool b );
00192 void setFamily( const QString &f );
00193 void setPointSize( int s );
00194 void setFont( const QFont &f );
00195 void setColor( const QColor &c );
00196 void setMisspelled( bool b );
00197 void setVAlign( VerticalAlignment a );
00198
00199 bool operator==( const KoTextFormat &f ) const;
00200 KoTextFormatCollection *parent() const;
00201 void setCollection( KoTextFormatCollection *parent ) { collection = parent; }
00202 QString key() const;
00203
00204 static QString getKey( const QFont &f, const QColor &c, bool misspelled, VerticalAlignment vAlign );
00205
00206 void addRef();
00207 void removeRef();
00208
00210 int compare( const KoTextFormat & format ) const;
00211
00214 static QColor defaultTextColor( QPainter * painter );
00215
00216 void setStrikeOutType (StrikeOutType _type);
00217 StrikeOutType strikeOutType()const {return m_strikeOutType;}
00218
00219 void setStrikeOutStyle( StrikeOutStyle _type );
00220 StrikeOutStyle strikeOutStyle()const {return m_strikeOutStyle;}
00221
00222
00223 void setTextBackgroundColor(const QColor &);
00224 QColor textBackgroundColor()const {return m_textBackColor;}
00225
00226 void setTextUnderlineColor(const QColor &);
00227 QColor textUnderlineColor()const {return m_textUnderlineColor;}
00228
00229 void setUnderlineType (UnderlineType _type);
00230 UnderlineType underlineType()const {return m_underlineType;}
00231
00232 void setUnderlineStyle (UnderlineStyle _type);
00233 UnderlineStyle underlineStyle()const {return m_underlineStyle;}
00234
00235 void setLanguage( const QString & _lang);
00236 QString language() const { return m_language;}
00237
00238 void setHyphenation( bool b );
00239 bool hyphenation() const { return d->m_bHyphenation; }
00240
00241
00242
00243 void setUnderLineWidth( double ulw );
00244 double underLineWidth() const { return d->m_underLineWidth; }
00245
00246
00247 void setAttributeFont( KoTextFormat::AttributeStyle _att );
00248 KoTextFormat::AttributeStyle attributeFont() const { return m_attributeFont;}
00249
00250
00251 double shadowDistanceX() const { return d->m_shadowDistanceX; }
00252 double shadowDistanceY() const { return d->m_shadowDistanceY; }
00253 QColor shadowColor() const;
00255 int shadowX( KoTextZoomHandler *zh ) const;
00257 int shadowY( KoTextZoomHandler *zh ) const;
00258 void setShadow( double shadowDistanceX, double shadowDistanceY, const QColor& shadowColor );
00260 QString shadowAsCss() const;
00261 static QString shadowAsCss( double shadowDistanceX, double shadowDistanceY, const QColor& shadowColor );
00263 void parseShadowFromCss( const QString& css );
00264
00265 double relativeTextSize() const { return d->m_relativeTextSize;}
00266 void setRelativeTextSize( double _size );
00267
00268
00269 int offsetFromBaseLine() const { return d->m_offsetFromBaseLine;}
00270 void setOffsetFromBaseLine( int _offset );
00271
00272 bool wordByWord() const { return d->m_bWordByWord;}
00273 void setWordByWord( bool _b );
00274
00275 bool doubleUnderline() const { return (m_underlineType==U_DOUBLE ); }
00276 bool waveUnderline() const { return (m_underlineType==U_WAVE ); }
00277 bool underline() const { return (m_underlineType==U_SIMPLE ); }
00278 bool strikeOut() const { return (m_strikeOutType==S_SIMPLE ); }
00279 bool doubleStrikeOut() const { return (m_strikeOutType==S_DOUBLE ); }
00280 bool isStrikedOrUnderlined() const { return ((m_underlineType != U_NONE) ||(m_strikeOutType!=S_NONE));}
00281
00287 float refPointSize() const;
00288
00293 float screenPointSize( const KoTextZoomHandler* zh ) const;
00294
00300 const QFontMetrics& refFontMetrics() const;
00301
00307 const QFontMetrics& screenFontMetrics( const KoTextZoomHandler* zh ) const;
00308
00313 QFont refFont() const;
00314
00320 QFont screenFont( const KoTextZoomHandler* zh ) const;
00321
00322 QFont smallCapsFont( const KoTextZoomHandler* zh, bool applyZoom ) const;
00323
00329 int charWidth( const KoTextZoomHandler* zh, bool applyZoom, const KoTextStringChar* c,
00330 const KoTextParag* parag, int i ) const;
00331
00336 int charWidthLU( const KoTextStringChar* c,
00337 const KoTextParag* parag, int i ) const;
00338
00339 void applyCharStyle( KoCharStyle *_style );
00340 KoCharStyle *style() const;
00341 static QString underlineStyleToString( UnderlineStyle _lineType );
00342 static QString strikeOutStyleToString( StrikeOutStyle _lineType );
00343 static UnderlineStyle stringToUnderlineStyle( const QString & _str );
00344 static StrikeOutStyle stringToStrikeOutStyle( const QString & _str );
00345
00346 static QString attributeFontToString( KoTextFormat::AttributeStyle _attr );
00347 static AttributeStyle stringToAttributeFont( const QString & _str );
00348
00349 QString displayedString( const QString& c )const;
00350 static QStringList underlineTypeList();
00351 static QStringList strikeOutTypeList();
00352 static QStringList fontAttributeList();
00353 static QStringList underlineStyleList();
00354 static QStringList strikeOutStyleList();
00355
00357 void load( KoOasisContext& context );
00360 void save( KoGenStyle& gs, KoSavingContext& context, KoTextFormat * refFormat = 0 ) const;
00361
00362 #ifndef NDEBUG
00363 void printDebug();
00364 #endif
00365
00367 void zoomChanged();
00368
00369 protected:
00370 QChar displayedChar( QChar c )const;
00371 void generateKey();
00372
00373 private:
00374 void update();
00375
00376 QColor m_textBackColor;
00377 QColor m_textUnderlineColor;
00378 UnderlineType m_underlineType;
00379 StrikeOutType m_strikeOutType;
00380 UnderlineStyle m_underlineStyle;
00381 StrikeOutStyle m_strikeOutStyle;
00382 QString m_language;
00383 AttributeStyle m_attributeFont;
00384 KoTextFormatPrivate *d;
00385
00386 QFont fn;
00387 QColor col;
00388 uint missp : 1;
00389
00390 VerticalAlignment va;
00391 KoTextFormatCollection *collection;
00392 int ref;
00393 QString m_key;
00394 };
00395
00396 #if defined(Q_TEMPLATEDLL)
00397 #ifndef Q_MOC_RUN
00398 template class Q_EXPORT Q3Dict<KoTextFormat>;
00399 #endif
00400 #endif
00401
00402 class KOTEXT_EXPORT KoTextFormatCollection
00403 {
00404 friend class KoTextDocument;
00405 friend class KoTextFormat;
00406
00407 public:
00408 KoTextFormatCollection();
00414 KoTextFormatCollection( const QFont& defaultFont, const QColor& defaultColor,
00415 const QString & defaultLanguage, bool defaultHyphenation );
00416 ~KoTextFormatCollection();
00417
00418 void setDefaultFormat( KoTextFormat *f );
00419 KoTextFormat *defaultFormat() const;
00420 KoTextFormat *format( const KoTextFormat *f );
00421 KoTextFormat *format( const KoTextFormat *of, const KoTextFormat *nf, int flags );
00422
00423
00424 void remove( KoTextFormat *f );
00425 KoTextFormat *createFormat( const KoTextFormat &f ) { return new KoTextFormat( f ); }
00426
00427
00428 void debug();
00429
00430
00431 void zoomChanged();
00432
00433
00434
00435
00436
00437
00438
00439
00440 Q3Dict<KoTextFormat> & dict() { return cKey; }
00441
00442 private:
00443 KoTextFormat *defFormat, *lastFormat, *cachedFormat;
00444 Q3Dict<KoTextFormat> cKey;
00445 KoTextFormat *cres;
00446 QFont cfont;
00447 QColor ccol;
00448 QString kof, knf;
00449 int cflags;
00450
00451 };
00452
00453 inline QColor KoTextFormat::color() const
00454 {
00455 return col;
00456 }
00457
00458 inline QFont KoTextFormat::font() const
00459 {
00460 return fn;
00461 }
00462
00463 inline bool KoTextFormat::isMisspelled() const
00464 {
00465 return missp;
00466 }
00467
00468 inline KoTextFormat::VerticalAlignment KoTextFormat::vAlign() const
00469 {
00470 return va;
00471 }
00472
00473 inline bool KoTextFormat::operator==( const KoTextFormat &f ) const
00474 {
00475 return key() == f.key();
00476 }
00477
00478 inline KoTextFormatCollection *KoTextFormat::parent() const
00479 {
00480 return collection;
00481 }
00482
00483
00484
00485
00486
00487
00488 inline void KoTextFormatCollection::setDefaultFormat( KoTextFormat *f )
00489 {
00490 defFormat = f;
00491 }
00492
00493 inline KoTextFormat *KoTextFormatCollection::defaultFormat() const
00494 {
00495 return defFormat;
00496 }
00497
00498 #endif