/*************************************************************************** dsrichtext.h - description ------------------- begin : Fre Okt 17 2003 copyright : (C) 2003 by Dominik Seichter email : domseichter@web.de ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef DSRICHTEXT_H #define DSRICHTEXT_H #define TQT_TEXT_BUG /*#undef TQT_TEXT_BUG*/ #ifdef TQT_TEXT_BUG #include #include #include #include class DSRichText; /** This class represents one word, * formated with a font, a color and an * alignment. */ struct formated_word { TQString text; TQFont font; TQColor color; int alignment; bool line; }; typedef TQValueList WordList; /** This structure represents a single line. * Every line contains a list of formated_words. * Wordwrapping has been applied before the line * was created. * lineSpacing, ascent and leading come from the * biggest font in the line. */ struct formated_line { int width; int lineSpacing; int ascent; int leading; bool line; WordList formats; }; typedef TQValueList LineList; class TQColor; class TQPainter; /** A richtext rendering class, which draws a piece * of HTML formated richtext on any TQPainter. * This class works with all printer resolutions compared * to the buggy TQSimpleRichText * * rendering the richtext happens in 3 steps: *
    *
  • the HTML data is parsed into text_format structures. Each * of these text_format structures may represend multiple lines, but * also single words. This depends on how long a formated text is. * this step happens in the constructor and in parseParagraph()
  • *
  • the text_format structures are parsed into line structures. * In this step the wordwrapping is calculated. Each line structure * has again text_format structures. This is necessary because a line * of text may of course contain different formattings. A line struct is always * a single line of drawn text, not more and not less. * happens in draw() and fillLines()
  • *
  • the line structure is drawn line by line using TQPainter. * happens in draw()
  • *
* * Printing to the screen is easy and the usage does not differ from * TQSimpleRichText. Drawing on TQPrinter is a little bit more complicated. * The most important thing is that you may not use TQPainter::scale(). * A small example on how to print on a TQPrinter: *
  *  TQPrinter printer( TQPrinter::HighResolution );
  *  TQPainter painter( &printer );
  *  TQPaintDeviceMetrics metrics( &printer );
  *  double scalex = (double)metrics.logicalDpiX() / (double)TQPaintDevice::x11AppDpiX();
  *  double scaley = (double)metrics.logicalDpiY() / (double)TQPaintDevice::x11AppDpiY();
  *
  *  DSRichText dsr( "<html><p>Hallo World</p></html>" );
  *  dsr.setWidth( 200 ); // in screenresolution coordinates
  *  dsr.setHeight( 80 );  // in screenresolution coordinates
  *  painter.translate( 10 * scalex, 10 * scaley ); // draw at 10, 10 instead of 0, 0
  *  dsr.scale( scalex, scaley );
  *  dsr.draw( &painter );
  * 
* *@author Dominik Seichter */ class DSRichText { public: /** construct a DSRichText object. @p t is formated text as produces by TQTextEdit. */ DSRichText( const TQString & t ); ~DSRichText(); void setX( int x ); void setY( int y ); /** draw the formated text on the TQPainter* @p p */ void draw( TQPainter* p ); /** set the width of the clipping rectangle */ void setWidth( int width ); /** set the height of the clipping rectangle */ void setHeight( int height ); /** scale everything. This is necessary to print on devices * with another resolution than the screen. TQPainter::scale() won't work. */ void setScale( double x, double y ) { sx = x; sy = y; }; private: /** parse a single pare of

elements */ bool parseParagraph(); /** remove

and

from the TQString @p t and return it. * This function is also responsible for getting the * correct alignment of the paragraph. */ TQString parseParagraphTag( const TQString & t, int* alignment ); /** parse the align attribute of a

tag and return the corresponding TQt alignment value. */ int parseAlignment( const TQString & align ); /** parse the css style attribute of a tag and return a matching TQFont for these * style. The font color is saved in the TQColor* @p color. */ TQFont parseStyle( const TQString & s, TQColor* color ); TQString parse( const TQString & t, const TQString & find, const TQString & end, int start ); void parseWords( const TQString & t, formated_word* w, WordList* words ); void initFormat( formated_word* f, int alignment ); void initLine( TQValueList* l ); void updateSpacing( TQValueList* l, TQFontMetrics* fm ); /** draw the line @p line justified as blockquote */ void drawJustified( formated_line* line ); /** calculate the y-deviation needed, because of different font sizes in this line */ inline int yDeviation( const formated_line* line ); /** replace HTML escape sequences such as < to their real character meaning (i.e. < ) */ TQString replaceEscapeSequences( const TQString & t ); /** fill the line structures with data */ void fillLines(); int pos; // current position in text int start; // start of a tag int end; // end of a tag int x; // x position int y; // y position int w; // width of the text element int h; // height of the text element int xpos; // x position at the beginning int ypos; // y position at the beginning double sx; double sy; TQString text; TQFont m_base; TQColor m_color; TQPainter* painter; TQValueList line_p; TQValueList word_p; }; #endif // TQT_TEXT_BUG #endif