20#include "KoFontDiaPreview.h"
22#include "KoTextFormat.h"
26#include <tqfontmetrics.h>
30#include <tqstringlist.h>
36#include "KoFontDiaPreview.moc"
39 : TQFrame( parent, name, fl )
40 ,m_text( i18n(
"The quick brown dog jumps over the lazy cat." ) )
41 ,displayText( i18n(
"The quick brown dog jumps over the lazy cat." ) )
42 ,m_font( KoGlobal::defaultFont() )
43 ,m_textColor( TQt::black )
44 ,m_backgroundColor( TQt::white )
45 ,m_shadowDistanceX( 0 )
46 ,m_shadowDistanceY( 0 )
47 ,m_shadowColor( TQt::black )
49 ,m_underliningStyle( 0 )
50 ,m_underliningColor( TQt::black )
51 ,m_wordByWord( false )
53 ,m_strikethroughStyle( 0 )
54 ,m_capitalisation( 0 )
60 setFrameStyle( TQFrame::WinPanel | TQFrame::Plain );
61 setBackgroundMode( PaletteBase );
62 setBackgroundColor( TQt::white );
63 setMinimumSize( 400, 100 );
66KoFontDiaPreview::~KoFontDiaPreview()
70void KoFontDiaPreview::setText(
const TQString &text )
76void KoFontDiaPreview::setFont(
const TQFont &font )
79 m_fontSize = m_font.pointSize();
83void KoFontDiaPreview::setFontColor(
const TQColor &textColor )
85 m_textColor = textColor;
89void KoFontDiaPreview::setBackgroundColor(
const TQColor &backgroundColor )
91 m_backgroundColor = backgroundColor;
95void KoFontDiaPreview::setShadow(
double sdx,
double sdy, TQColor shadowColor )
97 m_shadowDistanceX = sdx;
98 m_shadowDistanceY = sdy;
99 m_shadowColor = shadowColor;
103void KoFontDiaPreview::setUnderlining(
int underlining,
int underliningStyle,
const TQColor underliningColor,
bool wordByWord )
105 m_underlining = underlining;
106 m_underliningStyle = underliningStyle;
107 m_underliningColor = underliningColor;
108 m_wordByWord = wordByWord;
112void KoFontDiaPreview::setWordByWord(
bool wordByWord )
114 m_wordByWord = wordByWord;
118void KoFontDiaPreview::setStrikethrough(
int strikethrough,
int strikethroughStyle,
bool wordByWord )
120 m_strikethrough = strikethrough;
121 m_strikethroughStyle = strikethroughStyle;
122 m_wordByWord = wordByWord;
126void KoFontDiaPreview::setCapitalisation(
int capitalisation )
128 m_capitalisation = capitalisation;
132void KoFontDiaPreview::setSubSuperscript(
int subSuper,
int offset,
double relativeSize )
134 m_subSuper = subSuper;
136 m_relativeSize = relativeSize;
140TQString KoFontDiaPreview::formatCapitalisation(
const TQString &
string )
142 switch ( m_capitalisation )
144 case KoTextFormat::ATT_NONE :
146 case KoTextFormat::ATT_UPPER :
147 return string.upper();
148 case KoTextFormat::ATT_LOWER :
149 return string.lower();
150 case KoTextFormat::ATT_SMALL_CAPS :
151 return string.upper();
157void KoFontDiaPreview::drawContents( TQPainter* p )
164 double capitalisationCoeff;
165 TQFontMetrics fmCapitalisation( m_font );
167 switch ( m_capitalisation )
169 case KoTextFormat::ATT_NONE :
170 capitalisationCoeff = 1.0;
172 case KoTextFormat::ATT_UPPER :
173 capitalisationCoeff = 1.0;
175 case KoTextFormat::ATT_LOWER :
176 capitalisationCoeff = 1.0;
178 case KoTextFormat::ATT_SMALL_CAPS :
179 capitalisationCoeff = ((double)fmCapitalisation.boundingRect(
"x").height()/(
double)fmCapitalisation.boundingRect(
"X").height());
182 capitalisationCoeff = 1.0;
186 displayFont = m_font;
187 displayFont.setPointSizeFloat( m_font.pointSize() * capitalisationCoeff );
190 displayText = formatCapitalisation( m_text );
193 TQFontMetrics fm( displayFont );
194 TQRect br = fm.boundingRect( contentsRect().x(), contentsRect().y(), contentsRect().width(), contentsRect().height(), TQt::AlignCenter | TQt::WordBreak, displayText );
196 if ( br.width() > contentsRect().width() || br.height() > contentsRect().height() ) {
197 displayText = formatCapitalisation( i18n(
"Font too large for the preview pane" ) );
198 displayFont.setPointSizeFloat( 14 * capitalisationCoeff );
201 TQFontMetrics fm1( displayFont );
202 br = fm1.boundingRect( contentsRect().x(), contentsRect().y(), contentsRect().width(), contentsRect().height(), TQt::AlignCenter | TQt::WordBreak, displayText );
204 int xorg = tqRound( ( contentsRect().width() - br.width() ) / 2 ) + contentsRect().x() - fm1.leftBearing( displayText.at( 0 ) );
207 int subSuperOffset = 0;
208 switch ( m_subSuper ) {
210 displayFont.setPointSizeFloat( displayFont.pointSize() * m_relativeSize );
211 subSuperOffset = -( m_offset );
214 displayFont.setPointSizeFloat( displayFont.pointSize() * m_relativeSize );
215 subSuperOffset = fm1.height() / 6;
218 displayFont.setPointSizeFloat( displayFont.pointSize() * m_relativeSize );
219 subSuperOffset = 0 - ( fm1.height() / 2 );
222 displayFont.setPointSizeFloat( displayFont.pointSize() * m_relativeSize );
223 subSuperOffset = 0 - m_offset;
227 TQFontMetrics fm2( displayFont );
228 br = fm2.boundingRect( contentsRect().x(), contentsRect().y(), contentsRect().width(), contentsRect().height(), TQt::AlignCenter | TQt::WordBreak, displayText );
229 int yorg = tqRound( ( contentsRect().height() - br.height() ) / 2 ) + fm1.ascent() + subSuperOffset;
230 int sxorg = xorg + int( m_shadowDistanceX );
231 int syorg = yorg + int( m_shadowDistanceY );
232 TQStringList textWords = TQStringList::split(
" ", displayText );
237 int bx= TQMIN( x, sx );
241 int thicknessUnderline;
243 int widthStrikethrough;
244 int thicknessStrikethrough;
246 p->setFont(displayFont );
247 p->setPen( m_textColor );
250 for ( TQStringList::iterator it = textWords.begin(); it != textWords.end(); ++it ) {
252 if ( x + fm2.width( (*it) ) > contentsRect().width() ) {
253 y += fm1.lineSpacing();
254 sy += fm1.lineSpacing();
262 if ( (*it) == textWords.last() ) {
266 textDraw = (*it) +
" ";
269 if ( count == 1 ) boffset = TQABS(
int( m_shadowDistanceX ) );
272 if ( bx < xend && (bx + fm2.width( textDraw ) + boffset ) < xend && ( TQMIN( y, sy ) - fm2.ascent() ) < ( TQMIN( yorg, syorg ) - fm2.ascent() + fm2.height() + TQABS( m_shadowDistanceY ) ) ) {
273 p->fillRect( bx, TQMIN( yorg, syorg ) - fm2.ascent() + fm2.height() + TQABS(
int( m_shadowDistanceY ) ), fm2.width( textDraw ) + boffset , fm2.height() + TQABS(
int( m_shadowDistanceY ) ) - ( TQMIN( yorg, syorg ) - TQMIN( y, sy ) + fm2.height() + TQABS(
int( m_shadowDistanceY ) ) ), m_backgroundColor );
275 else if ( bx < xend && (bx + fm2.width( textDraw ) + boffset ) >= xend && ( TQMIN( y, sy ) - fm2.ascent() ) < ( TQMIN( yorg, syorg ) - fm2.ascent() + fm2.height() + TQABS( m_shadowDistanceY ) ) ) {
276 p->fillRect( bx, TQMIN( yorg, syorg ) - fm2.ascent() + fm2.height() + TQABS(
int( m_shadowDistanceY ) ), xend - bx , fm2.height() + TQABS(
int( m_shadowDistanceY ) ) - ( TQMIN( yorg, syorg ) - TQMIN( y, sy ) + fm2.height() + TQABS(
int( m_shadowDistanceY ) ) ), m_backgroundColor );
277 p->fillRect( xend, TQMIN( y, sy ) - fm2.ascent(), fm2.width( textDraw ) + boffset - xend + bx, fm2.height() + TQABS(
int( m_shadowDistanceY ) ), m_backgroundColor );
280 p->fillRect( bx, TQMIN( y, sy ) - fm2.ascent(), fm2.width( textDraw ) + boffset , fm2.height() + TQABS(
int( m_shadowDistanceY ) ), m_backgroundColor );
283 if ( count == 1 ) boffset = TQABS(
int( m_shadowDistanceX ) );
285 bx += fm2.width( textDraw ) + boffset;
287 if ( m_shadowDistanceX || m_shadowDistanceY )
290 p->setPen( m_shadowColor );
291 p->drawText( sx, sy, textDraw );
295 p->drawText( x, y, textDraw );
297 switch ( m_underlining ) {
298 case KoTextFormat::U_NONE:
300 case KoTextFormat::U_SIMPLE:
301 yUnderline = y + fm2.descent();
302 ( m_wordByWord )? widthUnderline = fm2.width( (*it) ): widthUnderline = fm2.width( textDraw );
303 thicknessUnderline = 1;
304 drawUnderline( x, yUnderline, widthUnderline, thicknessUnderline, m_underliningColor, p );
306 case KoTextFormat::U_DOUBLE:
307 yUnderline = y + fm2.descent();
308 ( m_wordByWord )? widthUnderline = fm2.width( (*it) ): widthUnderline = fm2.width( textDraw );
309 thicknessUnderline = 1;
310 drawUnderline( x, yUnderline, widthUnderline, thicknessUnderline, m_underliningColor, p );
311 yUnderline = y + tqRound( fm2.descent() / 2 );
312 drawUnderline( x, yUnderline, widthUnderline, thicknessUnderline, m_underliningColor, p );
314 case KoTextFormat::U_SIMPLE_BOLD:
315 yUnderline = y + fm2.descent();
316 ( m_wordByWord )? widthUnderline = fm2.width( (*it) ): widthUnderline = fm2.width( textDraw );
317 thicknessUnderline = tqRound( displayFont.pointSize() / 10 ) + 1;
318 drawUnderline( x, yUnderline, widthUnderline, thicknessUnderline, m_underliningColor, p );
320 case KoTextFormat::U_WAVE:
321 yUnderline = y + fm2.descent();
322 ( m_wordByWord )? widthUnderline = fm2.width( (*it) ): widthUnderline = fm2.width( textDraw );
323 thicknessUnderline = 1;
324 drawUnderlineWave( x, yUnderline, widthUnderline, thicknessUnderline, m_underliningColor, p );
330 switch ( m_strikethrough ) {
331 case KoTextFormat::S_NONE:
333 case KoTextFormat::S_SIMPLE:
334 yStrikethrough = y - tqRound( fm2.ascent() / 3 );
335 ( m_wordByWord )? widthStrikethrough = fm2.width( (*it) ): widthStrikethrough = fm2.width( textDraw );
336 thicknessStrikethrough = 1;
337 drawStrikethrough( x, yStrikethrough, widthStrikethrough, thicknessStrikethrough, p );
339 case KoTextFormat::S_DOUBLE:
340 yStrikethrough = y - tqRound( fm2.ascent() / 4 );
341 ( m_wordByWord )? widthStrikethrough = fm2.width( (*it) ): widthStrikethrough = fm2.width( textDraw );
342 thicknessStrikethrough = 1;
343 drawStrikethrough( x, yStrikethrough, widthStrikethrough, thicknessStrikethrough, p );
344 yStrikethrough = y - 2 * tqRound( fm2.ascent() / 4 );
345 drawStrikethrough( x, yStrikethrough, widthStrikethrough, thicknessStrikethrough, p );
347 case KoTextFormat::S_SIMPLE_BOLD:
348 yStrikethrough = y - tqRound( fm2.ascent() / 3 );
349 ( m_wordByWord )? widthStrikethrough = fm2.width( (*it) ): widthStrikethrough = fm2.width( textDraw );
350 thicknessStrikethrough = tqRound( displayFont.pointSize() / 10 ) + 1;
351 drawStrikethrough( x, yStrikethrough, widthStrikethrough, thicknessStrikethrough, p );
356 x += fm2.width( textDraw );
357 sx += fm2.width( textDraw );
364void KoFontDiaPreview::drawUnderline(
int x,
int y,
int width,
int thickness, TQColor & color, TQPainter *p )
367 switch ( m_underliningStyle ) {
368 case KoTextFormat::U_SOLID:
369 p->setPen( TQPen( color, thickness, TQt::SolidLine ) );
371 case KoTextFormat::U_DASH:
372 p->setPen( TQPen( color, thickness, TQt::DashLine ) );
374 case KoTextFormat::U_DOT:
375 p->setPen( TQPen( color, thickness, TQt::DotLine ) );
377 case KoTextFormat::U_DASH_DOT:
378 p->setPen( TQPen( color, thickness, TQt::DashDotLine ) );
380 case KoTextFormat::U_DASH_DOT_DOT:
381 p->setPen( TQPen( color, thickness, TQt::DashDotDotLine ) );
384 p->setPen( TQPen( color, thickness, TQt::SolidLine ) );
386 p->drawLine( x, y, x+ width, y );
390void KoFontDiaPreview::drawUnderlineWave(
int x,
int y,
int width,
int thickness, TQColor & color, TQPainter *p )
393 int offset = 2 * thickness;
394 TQPen pen(color, thickness, TQt::SolidLine);
395 pen.setCapStyle(TQt::RoundCap);
397 double anc=acos(1.0-2*(
static_cast<double>(offset-(x)%offset)/
static_cast<double>(offset)))/3.1415*180;
400 if(2*((x/offset)/2)==x/offset)
403 p->drawArc( (x/offset)*offset, y, offset, offset, 0, -tqRound(pos*anc*16) );
405 int zigzag_x = (x/offset+1)*offset;
406 for ( ; zigzag_x + offset <= width+x; zigzag_x += offset)
408 p->drawArc( zigzag_x, y, offset, offset, 0, pos*180*16 );
412 anc=acos(1.0-2*(
static_cast<double>((x+width)%offset)/
static_cast<double>(offset)))/3.1415*180;
413 p->drawArc( zigzag_x, y, offset, offset, 180*16, -tqRound(pos*anc*16) );
417void KoFontDiaPreview::drawStrikethrough(
int x,
int y,
int width,
int thickness, TQPainter *p )
420 switch ( m_strikethroughStyle ) {
421 case KoTextFormat::S_SOLID:
422 p->setPen( TQPen( TQt::black, thickness, TQt::SolidLine ) );
424 case KoTextFormat::S_DASH:
425 p->setPen( TQPen( TQt::black, thickness, TQt::DashLine ) );
427 case KoTextFormat::S_DOT:
428 p->setPen( TQPen( TQt::black, thickness, TQt::DotLine ) );
430 case KoTextFormat::S_DASH_DOT:
431 p->setPen( TQPen( TQt::black, thickness, TQt::DashDotLine ) );
433 case KoTextFormat::S_DASH_DOT_DOT:
434 p->setPen( TQPen( TQt::black, thickness, TQt::DashDotDotLine ) );
437 p->setPen( TQPen( TQt::black, thickness, TQt::SolidLine ) );
439 p->drawLine( x, y, x+ width, y );
KoFontDiaPreview(TQWidget *parent=0, const char *name=0, WFlags fl=0)
Constructor.