File: notefontviewer.cpp

package info (click to toggle)
rosegarden4 1.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 22,344 kB
  • ctags: 14,022
  • sloc: cpp: 131,139; sh: 9,429; perl: 2,620; xml: 2,231; makefile: 607; python: 374; ansic: 339; ruby: 173; php: 2
file content (327 lines) | stat: -rw-r--r-- 8,002 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// -*- c-basic-offset: 4 -*-
/*
    Rosegarden-4
    A sequencer and musical notation editor.

    This program is Copyright 2000-2005
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>

    The moral right of the authors to claim authorship of this work
    has been asserted.

    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.  See the file
    COPYING included with this distribution for more information.
*/

/*
    This file was originally based on fontdisplayer.cpp from the Qt
    example program "qfd", copyright 1992-2000 TrollTech AS.  The
    original file said:

** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.

*/

#include <qframe.h>
#include <qpainter.h>
#include <qlabel.h>
#include <qvbox.h>
#include <kcombobox.h>
#include <ktoolbar.h>
#include <klocale.h>
#include <kapplication.h>
#include <kmessagebox.h>

#include "notefontviewer.h"

#include "config.h"

#ifdef HAVE_XFT
#include <ft2build.h>
#include <X11/Xft/Xft.h>
#endif

FontViewFrame::FontViewFrame( int pixelSize, QWidget* parent, const char* name ) :
    QFrame(parent,name),
    m_fontSize(pixelSize),
    m_tableFont(0)
{
    setBackgroundMode(PaletteBase);
    setFrameStyle(Panel|Sunken);
    setMargin(8);
    setRow(0);
}

FontViewFrame::~FontViewFrame()
{
    // empty
}

void
FontViewFrame::setFont(QString font)
{
    m_fontName = font;
    loadFont();
    update();
}

void
FontViewFrame::loadFont()
{
#ifdef HAVE_XFT
    if (m_tableFont) {
	XftFontClose(x11AppDisplay(), (XftFont *)m_tableFont);
    }
    m_tableFont = 0;

    static bool haveDir = false;
    if (!haveDir) {
	FcConfigAppFontAddDir(FcConfigGetCurrent(),
			      (const FcChar8 *)"/opt/kde3/share/apps/rosegarden/fonts");
	haveDir = true;
    }

    FcPattern *pattern = FcPatternCreate();
    FcPatternAddString(pattern, FC_FAMILY, (FcChar8 *)m_fontName.latin1());
    FcPatternAddInteger(pattern, FC_PIXEL_SIZE, m_fontSize);
    
    FcConfigSubstitute(FcConfigGetCurrent(), pattern, FcMatchPattern);

    FcResult result = FcResultMatch;
    FcPattern *match = FcFontMatch(FcConfigGetCurrent(), pattern, &result);
    FcPatternDestroy(pattern);

    if (!match || result != FcResultMatch) {
	KMessageBox::error(this, i18n("Error: Unable to match font name %1").arg(m_fontName));
	return;
    }

    FcChar8 *matchFamily;
    FcPatternGetString(match, FC_FAMILY, 0, &matchFamily);

    if (QString((const char *)matchFamily).lower() != m_fontName.lower()) {
	KMessageBox::sorry(this, i18n("Warning: No good match for font name %1 (best is %2)").
			     arg(m_fontName).arg(QString((const char *)matchFamily)));
	m_fontName = (const char *)matchFamily;
    }

    m_tableFont = XftFontOpenPattern(x11AppDisplay(), match);

    if (!m_tableFont) {
	KMessageBox::error(this, i18n("Error: Unable to open best-match font %1").
			   arg(QString((const char *)matchFamily)));
    }
#endif
}

void FontViewFrame::setGlyphs(bool glyphs)
{
    m_glyphs = glyphs;
    update();
}

QSize FontViewFrame::sizeHint() const
{
    return QSize(16 * m_fontSize * 3 / 2 + margin() + 2 * frameWidth(),
		 16 * m_fontSize * 3 / 2 + margin() + 2 * frameWidth());
}

QSize FontViewFrame::cellSize() const
{
    QFontMetrics fm = fontMetrics();
    return QSize( fm.maxWidth(), fm.lineSpacing()+1 );
}

void FontViewFrame::paintEvent( QPaintEvent* e )
{
#ifdef HAVE_XFT
    if (!m_tableFont) return;

    QFrame::paintEvent(e);
    QPainter p(this);

    int ll = 25;
    int ml = frameWidth()+margin() + ll + 1;
    int mt = frameWidth()+margin();
    QSize cell((width()-16-ml)/17,(height()-16-mt)/17);

    if ( !cell.width() || !cell.height() )
        return;

    QColor body(255,255,192);
    QColor negative(255,192,192);
    QColor positive(192,192,255);
    QColor rnegative(255,128,128);
    QColor rpositive(128,128,255);

    Drawable drawable = (Drawable)handle();
    XftDraw *draw = XftDrawCreate(x11AppDisplay(), drawable,
				  (Visual *)x11Visual(), x11Colormap());

    QColor pen(Qt::black);
    XftColor col;
    col.color.red = pen.red () | pen.red() << 8;
    col.color.green = pen.green () | pen.green() << 8;
    col.color.blue = pen.blue () | pen.blue() << 8;
    col.color.alpha = 0xffff;
    col.pixel = pen.pixel();

    for (int j = 0; j <= 16; j++) {
        for (int i = 0; i <= 16; i++) {

	    int x = i*cell.width();
	    int y = j*cell.height();
	    
	    x += ml;
	    y += mt; // plus ascent
	    
	    if (i == 0) {
		if (j == 0) continue;
		p.setFont(kapp->font());
		QFontMetrics afm(kapp->font());
		QString s = QString("%1").arg(m_row * 256 + (j-1) * 16);
		p.drawText(x - afm.width(s), y, s);
		p.setPen(QColor(190, 190, 255));
		p.drawLine(0, y, width(), y);
		p.setPen(Qt::black);
		continue;
	    } else if (j == 0) {
		p.setFont(kapp->font());
		QString s = QString("%1").arg(i - 1);
		p.drawText(x, y, s);
		p.setPen(QColor(190, 190, 255));
		p.drawLine(x, 0, x, height());
		p.setPen(Qt::black);
		continue;
	    }
	    
	    p.save();
	    
	    if (m_glyphs) {
		FT_UInt  ui = m_row * 256 + (j-1) * 16 + i - 1;
		XftDrawGlyphs(draw, &col, (XftFont *)m_tableFont, x, y, &ui, 1);
	    } else {
		FcChar32 ch = m_row * 256 + (j-1) * 16 + i - 1;
		if (XftCharExists(x11AppDisplay(), (XftFont *)m_tableFont, ch)) {
		    XftDrawString32(draw, &col, (XftFont *)m_tableFont, x, y, &ch, 1);
		}
	    }
		
	    p.restore();
        }
    }
#endif
}

bool
FontViewFrame::hasRow(int r) const
{
#ifdef HAVE_XFT
    if (m_glyphs) {

	if (r < 256) return true;

    } else {

	for (int c = 0; c < 256; ++c) {
	    FcChar32 ch = r * 256 + c;
	    if (XftCharExists(x11AppDisplay(), (XftFont *)m_tableFont, ch)) {
		return true;
	    }
	}
    }
#endif
    return false;
}

void FontViewFrame::setRow(int row)
{
    m_row = row;
    update();
}

void
NoteFontViewer::slotViewChanged(int i)
{
    m_frame->setGlyphs(i == 0);

    m_rows->clear();
    int firstRow = -1;

    for (int r = 0; r < 256; ++r) {
	if (m_frame->hasRow(r)) {
	    m_rows->insertItem(QString("%1").arg(r));
	    if (firstRow < 0) firstRow = r;
	}
    }

    if (firstRow < 0) {
	m_rows->setEnabled(false);
	m_frame->setRow(0);
    } else {
	m_rows->setEnabled(true);
	m_frame->setRow(firstRow);
    }
}

void
NoteFontViewer::slotRowChanged(const QString &s)
{
    bool ok;
    int i = s.toInt(&ok);
    if (ok) m_frame->setRow(i);
}

void
NoteFontViewer::slotFontChanged(const QString &s)
{
    m_frame->setFont(s);
    slotViewChanged(m_view->currentItem());
}

NoteFontViewer::NoteFontViewer(QWidget *parent, QString noteFontName,
			       QStringList fontNames, int pixelSize) :
    KDialogBase(parent, 0, true,
		i18n("Note Font Viewer: %1").arg(noteFontName), Close)
{
    QVBox *box = makeVBoxMainWidget();
    KToolBar* controls = new KToolBar(box);
    controls->setMargin(3);

    (void) new QLabel(i18n("  Component: "), controls);
    m_font = new KComboBox(controls);

    for (QStringList::iterator i = fontNames.begin(); i != fontNames.end();
	 ++i) {
	m_font->insertItem(*i);
    }

    (void) new QLabel(i18n("  View: "), controls);
    m_view = new KComboBox(controls);

    m_view->insertItem("Glyphs");
    m_view->insertItem("Codes");

    (void) new QLabel(i18n("  Page: "), controls);
    m_rows = new KComboBox(controls);

    m_frame = new FontViewFrame(pixelSize, box);

    connect(m_font, SIGNAL(activated(const QString &)),
	    this, SLOT(slotFontChanged(const QString &)));

    connect(m_view, SIGNAL(activated(int)),
	    this, SLOT(slotViewChanged(int)));

    connect(m_rows,SIGNAL(activated(const QString &)),
	    this, SLOT(slotRowChanged(const QString &)));

    slotFontChanged(m_font->currentText());
}