-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqwt_text_engine.cpp
More file actions
148 lines (122 loc) · 4 KB
/
qwt_text_engine.cpp
File metadata and controls
148 lines (122 loc) · 4 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- 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 "qwt_text_engine.h"
#include "qwt_painter.h"
#include <qpainter.h>
#include <qpixmap.h>
#include <qimage.h>
#include <qmap.h>
#include <qwidget.h>
class AscentCache
{
public:
static int effectiveAscent( const QFont &font )
{
const QString fontKey = font.key();
QMap<QString, int>::const_iterator it =
d_ascentCache.find( fontKey );
if ( it == d_ascentCache.end() )
{
int ascent = findAscent( font );
it = d_ascentCache.insert( fontKey, ascent );
}
return ( *it );
}
private:
static int findAscent( const QFont &font )
{
static const QString dummy( "E" );
static const QColor white( Qt::white );
const QFontMetrics fm( font );
QPixmap pm( fm.width( dummy ), fm.height() );
pm.fill( white );
QPainter p( &pm );
p.setFont( font );
p.drawText( 0, 0, pm.width(), pm.height(), 0, dummy );
p.end();
const QImage img = pm.toImage();
int row = 0;
for ( row = 0; row < img.height(); row++ )
{
const QRgb *line = ( const QRgb * )img.scanLine( row );
const int w = pm.width();
for ( int col = 0; col < w; col++ )
{
if ( line[col] != white.rgb() )
return fm.ascent() - row + 1;
}
}
return fm.ascent();
}
static QMap<QString, int> d_ascentCache;
};
QMap<QString, int> AscentCache::d_ascentCache;
/*!
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 QwtPlainTextEngine::heightForWidth( const QFont& font, int flags,
const QString& text, double width )
{
const QFontMetricsF fm( font );
const QRectF rect = fm.boundingRect(
QRectF( 0, 0, width, QWIDGETSIZE_MAX ), flags, text );
return rect.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 QwtPlainTextEngine::textSize( const QFont &font,
int flags, const QString& text )
{
const QFontMetricsF fm( font );
const QRectF rect = fm.boundingRect(
QRectF( 0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ), flags, text );
return rect.size();
}
/*!
Return margins around the texts
\param font Font of the text
\param left Return 0
\param right Return 0
\param top Return value for the top margin
\param bottom Return value for the bottom margin
*/
void QwtPlainTextEngine::textMargins( const QFont &font, const QString &,
double &left, double &right, double &top, double &bottom )
{
left = right = top = 0;
const QFontMetricsF fm( font );
top = fm.ascent() - AscentCache::effectiveAscent( font );
bottom = fm.descent();
}
/*!
\brief Draw the text in a clipping rectangle
A wrapper for QPainter::drawText.
\param painter Painter
\param rect Clipping rectangle
\param flags Bitwise OR of the flags used like in QPainter::drawText
\param text Text to be rendered
*/
void QwtPlainTextEngine::draw( QPainter *painter, const QRectF &rect,
int flags, const QString& text )
{
painter->save();
QwtPainter::unscaleFont( painter );
painter->drawText( rect, flags, text );
painter->restore();
}