Example #1
0
//------------------------------------------------------------------------------
// Name: draw_label
// Desc:
//------------------------------------------------------------------------------
void GraphNode::draw_label(const textlabel_t *textlabel) {
	QPainter painter(&picture_);

	painter.setPen(textlabel->fontcolor);

	// Since I always just take the points from graph_ and pass them to Qt
	// as pixel I also have to set the pixel size of the font.
	QFont font(textlabel->fontname, textlabel->fontsize);
	font.setPixelSize(textlabel->fontsize);

	if(!font.exactMatch()) {
		QFontInfo fontinfo(font);
		qWarning("replacing font \"%s\" by font \"%s\"", qPrintable(font.family()), qPrintable(fontinfo.family()));
	}

	painter.setFont(font);

	QString text(QString::fromUtf8(textlabel->text));
	QFontMetricsF fm(painter.fontMetrics());
	QRectF rect(fm.boundingRect(text));

#ifdef ND_coord_i
	rect.moveCenter(graph_->gToQ(textlabel->p, false));
#else
	rect.moveCenter(graph_->gToQ(textlabel->pos, false));
#endif
	painter.drawText(rect.adjusted(-2, -2, +2, +2), Qt::AlignCenter, text);
}
Example #2
0
bool FONT::Load(
	const std::string & fontinfopath,
	const std::string & texpath,
	const std::string & texname,
	ContentManager & content,
	std::ostream & error_output,
	bool mipmap)
{
	TEXTUREINFO texinfo;
	texinfo.mipmap = mipmap;
	texinfo.repeatu = false;
	texinfo.repeatv = false;
	if (!content.load(texpath, texname, texinfo, font_texture)) return false;

	std::ifstream fontinfo(fontinfopath.c_str());
	if (!fontinfo)
	{
		error_output << "Can't find font information file: " << fontinfopath << std::endl;
		return false;
	}

	const std::string sizestr("size=");
	const float sw = font_texture->GetScale() / font_texture->GetW();
	const float sh = font_texture->GetScale() / font_texture->GetH();
	while (fontinfo)
	{
		std::string curstr;
		fontinfo >> curstr;
		if (curstr == "char")
		{
			unsigned int cur_id(0);
			if (!Parse("id=", cur_id, fontinfo, fontinfopath, error_output)) return false;

			CHARINFO & info = charinfo[cur_id];
			if (!Parse("x=", info.x, fontinfo, fontinfopath, error_output)) return false;
			if (!Parse("y=", info.y, fontinfo, fontinfopath, error_output)) return false;
			if (!Parse("width=", info.width, fontinfo, fontinfopath, error_output)) return false;
			if (!Parse("height=", info.height, fontinfo, fontinfopath, error_output)) return false;
			if (!Parse("xoffset=", info.xoffset, fontinfo, fontinfopath, error_output)) return false;
			if (!Parse("yoffset=", info.yoffset, fontinfo, fontinfopath, error_output)) return false;
			if (!Parse("xadvance=", info.xadvance, fontinfo, fontinfopath, error_output)) return false;
			fontinfo >> curstr >> curstr; //don't care

			info.x *= sw;
			info.y *= sh;
			info.width *= sw;
			info.height *= sh;
			info.xoffset *= sw;
			info.yoffset *= sh;
			info.xadvance *= sw;
			info.loaded = true;
		}
		else if (curstr.compare(0, sizestr.size(), sizestr) == 0)
Example #3
0
//------------------------------------------------------------------------------
// Name: drawLabel
// Desc:
//------------------------------------------------------------------------------
void GraphNode::drawLabel(const QString &text) {

	QPainter painter(&picture_);
	painter.setBrush(QBrush(color_));
	painter.setPen(TextColor);

	// Since I always just take the points from graph_ and pass them to Qt
	// as pixel I also have to set the pixel size of the font.
	QFont font(NodeFont);
	font.setPixelSize(LabelFontSize);

	if(!font.exactMatch()) {
		QFontInfo fontinfo(font);
		qWarning("replacing font '%s' by font '%s'", qPrintable(font.family()), qPrintable(fontinfo.family()));
	}

	painter.setFont(font);
	
	// just to calculate the proper bounding box
	QRectF textBoundingRect;
	painter.drawText(QRectF(), Qt::AlignLeft | Qt::AlignTop, text, &textBoundingRect);
	
	// set some reasonable minimums
	if(textBoundingRect.width() < NodeWidth) {
		textBoundingRect.setWidth(NodeWidth);
	}
	
	if(textBoundingRect.height() < NodeHeight) {
		textBoundingRect.setHeight(NodeHeight);
	}

	// set the bounding box and then really draw it
	picture_.setBoundingRect(textBoundingRect.adjusted(-2, -2, +2, +2).toRect());
		
#if 1
	QTextDocument doc;
	doc.setDefaultFont(font);
	doc.setDocumentMargin(0);
	doc.setPlainText(text);
	auto highligher = new SyntaxHighlighter(&doc);
	doc.drawContents(&painter, textBoundingRect);
#else
	painter.drawText(textBoundingRect.adjusted(-2, -2, +2, +2), Qt::AlignLeft | Qt::AlignTop, text);
#endif
}
void tst_QFont::exactMatch()
{
    QFont font;

    // Check if a non-existing font hasn't an exact match
    font = QFont( "BogusFont", 33 );
    QVERIFY( !font.exactMatch() );

#ifdef Q_OS_WIN
    QSKIP("Exact matching on windows misses a lot because of the sample chars");
#endif


    if (QGuiApplication::platformName() == QLatin1String("xcb")) {
        QVERIFY(QFont("sans").exactMatch());
        QVERIFY(QFont("sans-serif").exactMatch());
        QVERIFY(QFont("serif").exactMatch());
        QVERIFY(QFont("monospace").exactMatch());
    }

    QSKIP("This test is bogus on Unix with support for font aliases in fontconfig");

    QFontDatabase fdb;

    QList<QFontDatabase::WritingSystem> systems = fdb.writingSystems();
    for (int system = 0; system < systems.count(); ++system) {
        QStringList families = fdb.families(systems[system]);
        if (families.isEmpty())
            return;

        QStringList::ConstIterator f_it, f_end = families.end();
        for (f_it = families.begin(); f_it != f_end; ++f_it) {
            const QString &family = *f_it;
            if (family.contains('['))
                continue;

            QStringList styles = fdb.styles(family);
            QVERIFY(!styles.isEmpty());
            QStringList::ConstIterator s_it, s_end = styles.end();
            for (s_it = styles.begin(); s_it != s_end; ++s_it) {
                const QString &style = *s_it;

                if (fdb.isSmoothlyScalable(family, style)) {
                    // smoothly scalable font... don't need to load every pointsize
                    font = fdb.font(family, style, 12);
                    QFontInfo fontinfo(font);

                    if (! fontinfo.exactMatch()) {
                        // Unfortunately, this can fail, since
                        // QFontDatabase does not fill in all font
                        // properties.  Check to make sure that the
                        // test didn't fail for obvious reasons

                        if (fontinfo.family().isEmpty()
                                && fontinfo.pointSize() == 0) {
                            // this is a box rendering engine... this can happen from
                            // time to time, especially on X11 with iso10646-1 or
                            // unknown font encodings
                            continue;
                        }

#ifdef Q_OS_WIN
                        if (font.family().startsWith("MS ") || fontinfo.family().startsWith("MS ")) {
                            /* qDebug("Family matching skipped for MS-Alias font: %s, fontinfo: %s",
                               font.family().latin1(), fontinfo.family().latin1());
                               */
                        } else
#endif
                        {
                            if (!(font.family() == fontinfo.family()
                                        || fontinfo.family().contains(font.family())
                                        || fontinfo.family().isEmpty())) {
                                qDebug("Test about to fail for font: %s, fontinfo: %s",
                                        font.family().toLatin1().constData(),
                                        fontinfo.family().toLatin1().constData());
                            }
                            QVERIFY(font.family() == fontinfo.family()
                                    || fontinfo.family().contains(font.family())
                                    || fontinfo.family().isEmpty());
                        }
                        if (font.pointSize() != -1) {
                            QVERIFY(font.pointSize() == fontinfo.pointSize());
                        } else {
                            QVERIFY(font.pixelSize() == fontinfo.pixelSize());
                        }
                        QVERIFY(font.italic() == fontinfo.italic());
                        if (font.weight() != fontinfo.weight()) {
                            qDebug("font is %s", font.toString().toLatin1().constData());
                        }
                        QVERIFY(font.weight() == fontinfo.weight());
                    } else {
                        font.setFixedPitch(!fontinfo.fixedPitch());
                        QFontInfo fontinfo1(font);
                        QVERIFY( !fontinfo1.exactMatch() );

                        font.setFixedPitch(fontinfo.fixedPitch());
                        QFontInfo fontinfo2(font);
                        QVERIFY( fontinfo2.exactMatch() );
                    }
                }
#if 0
                // ############## can only work if we have float point sizes in QFD
                else {
                    QList<int> sizes = fdb.pointSizes(family, style);
                    QVERIFY(!sizes.isEmpty());
                    QList<int>::ConstIterator z_it, z_end = sizes.end();
                    for (z_it = sizes.begin(); z_it != z_end; ++z_it) {
                        const int size = *z_it;

                        // Initialize the font, and check if it is an exact match
                        font = fdb.font(family, style, size);
                        QFontInfo fontinfo(font, (QFont::Script) script);

                        if (! fontinfo.exactMatch()) {
                            // Unfortunately, this can fail, since
                            // QFontDatabase does not fill in all font
                            // properties.  Check to make sure that the
                            // test didn't fail for obvious reasons

                            if (fontinfo.family().isEmpty()
                                    && fontinfo.pointSize() == 0) {
                                // this is a box rendering engine... this can happen from
                                // time to time, especially on X11 with iso10646-1 or
                                // unknown font encodings
                                continue;
                            }

                            // no need to skip MS-fonts here it seems
                            if (!(font.family() == fontinfo.family()
                                        || fontinfo.family().contains(font.family())
                                        || fontinfo.family().isEmpty())) {
                                qDebug("Test about to fail for font: %s, fontinfo: %s",
                                        font.family().latin1(), fontinfo.family().latin1());
                            }
                            QVERIFY(font.family() == fontinfo.family()
                                    || fontinfo.family().contains(font.family())
                                    || fontinfo.family().isEmpty());
                            if (font.pointSize() != -1) {
                                QVERIFY(font.pointSize() == fontinfo.pointSize());
                            } else {
                                QVERIFY(font.pixelSize() == fontinfo.pixelSize());
                            }
                            QVERIFY(font.italic() == fontinfo.italic());
                            QVERIFY(font.weight() == fontinfo.weight());
                        } else {
                            font.setFixedPitch(!fontinfo.fixedPitch());
                            QFontInfo fontinfo1(font, (QFont::Script) script);
                            QVERIFY( !fontinfo1.exactMatch() );

                            font.setFixedPitch(fontinfo.fixedPitch());
                            QFontInfo fontinfo2(font, (QFont::Script) script);
                            QVERIFY( fontinfo2.exactMatch() );
                        }
                    }
                }
#endif
            }
        }
    }
}
Example #5
0
QFont const & getFont(FontInfo const & f)
{
	return fontinfo(f).font;
}
Example #6
0
GuiFontMetrics const & getFontMetrics(FontInfo const & f)
{
	return fontinfo(f).metrics;
}
Example #7
0
FontMetrics const & FontLoader::metrics(FontInfo const & f)
{
	return fontinfo(f).metrics;
}