qwt_text_engine.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_TEXT_ENGINE_H
  10. #define QWT_TEXT_ENGINE_H 1
  11. #include "qwt_global.h"
  12. #include <qsize.h>
  13. class QFont;
  14. class QRectF;
  15. class QString;
  16. class QPainter;
  17. /*!
  18. \brief Abstract base class for rendering text strings
  19. A text engine is responsible for rendering texts for a
  20. specific text format. They are used by QwtText to render a text.
  21. QwtPlainTextEngine and QwtRichTextEngine are part of the Qwt library.
  22. The implementation of QwtMathMLTextEngine uses code from the
  23. Qt solution package. Because of license implications it is built into
  24. a separate library.
  25. \sa QwtText::setTextEngine()
  26. */
  27. class QWT_EXPORT QwtTextEngine
  28. {
  29. public:
  30. virtual ~QwtTextEngine();
  31. /*!
  32. Find the height for a given width
  33. \param font Font of the text
  34. \param flags Bitwise OR of the flags used like in QPainter::drawText
  35. \param text Text to be rendered
  36. \param width Width
  37. \return Calculated height
  38. */
  39. virtual double heightForWidth( const QFont &font, int flags,
  40. const QString &text, double width ) const = 0;
  41. /*!
  42. Returns the size, that is needed to render text
  43. \param font Font of the text
  44. \param flags Bitwise OR of the flags like in for QPainter::drawText
  45. \param text Text to be rendered
  46. \return Calculated size
  47. */
  48. virtual QSizeF textSize( const QFont &font, int flags,
  49. const QString &text ) const = 0;
  50. /*!
  51. Test if a string can be rendered by this text engine
  52. \param text Text to be tested
  53. \return true, if it can be rendered
  54. */
  55. virtual bool mightRender( const QString &text ) const = 0;
  56. /*!
  57. Return margins around the texts
  58. The textSize might include margins around the
  59. text, like QFontMetrics::descent(). In situations
  60. where texts need to be aligned in detail, knowing
  61. these margins might improve the layout calculations.
  62. \param font Font of the text
  63. \param text Text to be rendered
  64. \param left Return value for the left margin
  65. \param right Return value for the right margin
  66. \param top Return value for the top margin
  67. \param bottom Return value for the bottom margin
  68. */
  69. virtual void textMargins( const QFont &font, const QString &text,
  70. double &left, double &right, double &top, double &bottom ) const = 0;
  71. /*!
  72. Draw the text in a clipping rectangle
  73. \param painter Painter
  74. \param rect Clipping rectangle
  75. \param flags Bitwise OR of the flags like in for QPainter::drawText()
  76. \param text Text to be rendered
  77. */
  78. virtual void draw( QPainter *painter, const QRectF &rect,
  79. int flags, const QString &text ) const = 0;
  80. protected:
  81. QwtTextEngine();
  82. };
  83. /*!
  84. \brief A text engine for plain texts
  85. QwtPlainTextEngine renders texts using the basic Qt classes
  86. QPainter and QFontMetrics.
  87. */
  88. class QWT_EXPORT QwtPlainTextEngine: public QwtTextEngine
  89. {
  90. public:
  91. QwtPlainTextEngine();
  92. virtual ~QwtPlainTextEngine();
  93. virtual double heightForWidth( const QFont &font, int flags,
  94. const QString &text, double width ) const;
  95. virtual QSizeF textSize( const QFont &font, int flags,
  96. const QString &text ) const;
  97. virtual void draw( QPainter *painter, const QRectF &rect,
  98. int flags, const QString &text ) const;
  99. virtual bool mightRender( const QString & ) const;
  100. virtual void textMargins( const QFont &, const QString &,
  101. double &left, double &right, double &top, double &bottom ) const;
  102. private:
  103. class PrivateData;
  104. PrivateData *d_data;
  105. };
  106. #ifndef QT_NO_RICHTEXT
  107. /*!
  108. \brief A text engine for Qt rich texts
  109. QwtRichTextEngine renders Qt rich texts using the classes
  110. of the Scribe framework of Qt.
  111. */
  112. class QWT_EXPORT QwtRichTextEngine: public QwtTextEngine
  113. {
  114. public:
  115. QwtRichTextEngine();
  116. virtual double heightForWidth( const QFont &font, int flags,
  117. const QString &text, double width ) const;
  118. virtual QSizeF textSize( const QFont &font, int flags,
  119. const QString &text ) const;
  120. virtual void draw( QPainter *painter, const QRectF &rect,
  121. int flags, const QString &text ) const;
  122. virtual bool mightRender( const QString & ) const;
  123. virtual void textMargins( const QFont &, const QString &,
  124. double &left, double &right, double &top, double &bottom ) const;
  125. private:
  126. QString taggedText( const QString &, int flags ) const;
  127. };
  128. #endif // !QT_NO_RICHTEXT
  129. #endif