Pango顯示文字,並保存爲圖片

Pango是一個開放源代碼的自由函數庫,用於高質量地渲染國際化的文字。Pango可以使用不同的後端字體,並提供了跨平臺支持。

使用Pango顯示文字,需要設置字體,因爲需要使用字體去本地查找對應的字體文件,從而解析出正確的字型。

下面的示例是顯示中文,設置字體爲黑體,另外設置字體大小、顯示位置等操作。

文字顯示後保存到圖片,並使用QT顯示出來。


PangoFontDescription *font_description = pango_font_description_new();
	pango_font_description_set_family(font_description, "simhei");//黑體
	pango_font_description_set_weight(font_description, PANGO_WEIGHT_BOLD);
	pango_font_description_set_absolute_size(font_description, 16 * PANGO_SCALE);//設置字體大小
	pango_layout_set_font_description(layout, font_description);

	std::wstring wstr = L"Pango是一個開放源代碼的自由函數庫,用於高質量地渲染國際化的文字。Pango可以使用不同的後端字體,並提供了跨平臺支持。";
	gchar *ch = g_utf16_to_utf8((gunichar2 *)wstr.c_str(), -1, NULL, NULL, NULL);//轉成8位 Unicode碼
	pango_layout_set_text(layout, ch, -1);

	//cairo_set_source_rgb(cr, 0.0, 1.0, 1.0);//設置字體顏色
	cairo_move_to(cr, 10.0, 50.0);//顯示位置
	pango_cairo_show_layout_line(cr, pango_layout_get_line(layout, 0));//顯示
如果顯示高棉語Khmer或者泰語等需要組合的文字(Shaped Text),也是可以正確顯示的,只需要把字體換成對應的字體名稱,需要顯示的文字當然也需要換成高棉語或者泰語。


當然,要顯示高棉語或者泰語,還需要下載對應的字體文件,如果自己電腦上沒有對應的字體的話。


需要依賴glib,pango,cairo等庫。

示例代碼:

#ifndef QSHOWTEXT_H
#define QSHOWTEXT_H

#include <QWidget>

class QSHowText : public QWidget
{
	Q_OBJECT

public:
	QSHowText(QWidget *parent = 0);
	~QSHowText();

private:
	
};

#endif // QSHOWTEXT_H
#include "qshowtext.h"
#include "pango/pangocairo.h"
#include <QHBoxLayout>
#include <QLabel>

QSHowText::QSHowText(QWidget *parent)
	: QWidget(parent)
{
	cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1000, 500);
	cairo_t *cr = cairo_create(surface);
	PangoLayout *layout = pango_cairo_create_layout(cr);

	PangoFontDescription *font_description = pango_font_description_new();
	pango_font_description_set_family(font_description, "Khmer UI");//高棉語
	pango_font_description_set_weight(font_description, PANGO_WEIGHT_BOLD);
	pango_font_description_set_absolute_size(font_description, 16 * PANGO_SCALE);//設置字體大小
	pango_layout_set_font_description(layout, font_description);

	std::wstring wstr = L"Pango ជាបណ្ណាល័យប្រភពបើកចំហឥតគិតថ្លៃសម្រាប់បង្ហាញអត្ថបទដែលអន្ដរជាតិដែលមានគុណភាពខ្ពស់។ Pango អាចប្រើកម្មវិធីខាងក្រោយពុម្ពអក្សរផ្សេងគ្នានិងផ្តល់ការគាំទ្រឆ្លងវេទិកា។";
	gchar *ch = g_utf16_to_utf8((gunichar2 *)wstr.c_str(), -1, NULL, NULL, NULL);//轉成8位 Unicode碼
	pango_layout_set_text(layout, ch, -1);

	//cairo_set_source_rgb(cr, 0.0, 1.0, 1.0);//設置字體顏色
	cairo_move_to(cr, 10.0, 50.0);//顯示位置
	pango_cairo_show_layout_line(cr, pango_layout_get_line(layout, 0));//顯示

	g_object_unref(layout);
	pango_font_description_free(font_description);

	QString strImage = "test.png";
	cairo_surface_write_to_png(cairo_get_target(cr), strImage.toStdString().c_str());

	QLabel *pLabel = new QLabel(this);
	pLabel->setPixmap(QPixmap(strImage));

	QHBoxLayout* pHBox = new QHBoxLayout(this);
	pHBox->addWidget(pLabel);
}

QSHowText::~QSHowText()
{

}
本文地址:http://blog.csdn.net/u011417605/article/details/53517774
發佈了119 篇原創文章 · 獲贊 91 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章