1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2003 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include <qstring.h>
#include <qpainter.h>
#include "qwt_mathml_text_engine.h"
#include "qwt_mml_document.h"
//! Constructor
QwtMathMLTextEngine::QwtMathMLTextEngine()
{
}
//! Destructor
QwtMathMLTextEngine::~QwtMathMLTextEngine()
{
}
/*!
Find the height for a given width
\param font Font of the text
\param flags Bitwise OR of the flags used like in QPainter::drawText
\param text Text to be rendered
\param width Width
\return Calculated height
*/
double QwtMathMLTextEngine::heightForWidth( const QFont& font, int flags,
const QString& text, double width ) const
{
Q_UNUSED( width )
return textSize( font, flags, text ).height();
}
/*!
Returns the size, that is needed to render text
\param font Font of the text
\param flags Bitwise OR of the flags used like in QPainter::drawText
\param text Text to be rendered
\return Caluclated size
*/
QSizeF QwtMathMLTextEngine::textSize( const QFont &font,
int flags, const QString& text ) const
{
Q_UNUSED( flags );
static QString t;
static QSize sz;
if ( text != t )
{
QwtMathMLDocument doc;
doc.setContent( text );
doc.setBaseFontPointSize( font.pointSize() );
sz = doc.size();
t = text;
}
return sz;
}
/*!
Return margins around the texts
\param left Return 0
\param right Return 0
\param top Return 0
\param bottom Return 0
*/
void QwtMathMLTextEngine::textMargins( const QFont &, const QString &,
double &left, double &right, double &top, double &bottom ) const
{
left = right = top = bottom = 0;
}
/*!
Draw the text in a clipping rectangle
\param painter Painter
\param rect Clipping rectangle
\param flags Bitwise OR of the flags like in for QPainter::drawText
\param text Text to be rendered
*/
void QwtMathMLTextEngine::draw( QPainter *painter, const QRectF &rect,
int flags, const QString& text ) const
{
QwtMathMLDocument doc;
doc.setContent( text );
doc.setBaseFontPointSize( painter->font().pointSize() );
const QSizeF docSize = doc.size();
QPointF pos = rect.topLeft();
if ( rect.width() > docSize.width() )
{
if ( flags & Qt::AlignRight )
pos.setX( rect.right() - docSize.width() );
if ( flags & Qt::AlignHCenter )
pos.setX( rect.center().x() - docSize.width() / 2 );
}
if ( rect.height() > docSize.height() )
{
if ( flags & Qt::AlignBottom )
pos.setY( rect.bottom() - docSize.height() );
if ( flags & Qt::AlignVCenter )
pos.setY( rect.center().y() - docSize.height() / 2 );
}
doc.paint( painter, pos.toPoint() );
}
/*!
Test if a string can be rendered by QwtMathMLTextEngine
\param text Text to be tested
\return true, if text begins with "<math>".
*/
bool QwtMathMLTextEngine::mightRender( const QString &text ) const
{
return text.trimmed().startsWith( "<math" );
}
|