FreeTypeを使う 新しいページはコチラ
提供: yonewiki
(→サンプル1.Qt ConsoleApplicationでテキスト画像表示の解説) |
(→サンプル2.) |
||
469行: | 469行: | ||
137行目から最後まではテキストファイルの生成処理を実施している。このあたりはfreetypeとはもう関係ないので説明しない。 | 137行目から最後まではテキストファイルの生成処理を実施している。このあたりはfreetypeとはもう関係ないので説明しない。 | ||
− | === '''サンプル2. ''' === | + | === '''サンプル2. 公式サイト版 のQtConsolApp''' === |
+ | <Syntaxhighlight2 lang="cpp" line="1"> | ||
+ | #include <QtCore/QCoreApplication> | ||
+ | |||
+ | #include <QObject> | ||
+ | |||
+ | #include <QString> | ||
+ | #include <QFile> | ||
+ | #include <QTextStream> | ||
+ | #include <QImage> | ||
+ | #include <QColor> | ||
+ | #include <QSize> | ||
+ | |||
+ | #pragma execution_character_set("utf-8") | ||
+ | |||
+ | #include <ft2build.h> | ||
+ | #include FT_FREETYPE_H | ||
+ | |||
+ | #define WIDTH 640 | ||
+ | #define HEIGHT 480 | ||
+ | #define LEFT 32 | ||
+ | #define TOP 32 | ||
+ | |||
+ | static unsigned char canvas[HEIGHT][WIDTH]; | ||
+ | |||
+ | static void draw_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y) { | ||
+ | FT_Int i, j, p, q; | ||
+ | FT_Int x_max = x + bitmap->width; | ||
+ | FT_Int y_max = y + bitmap->rows; | ||
+ | |||
+ | for (i = x, p = 0; i < x_max; i++, p++) { | ||
+ | for (j = y, q = 0; j < y_max; j++, q++) { | ||
+ | if (i < 0 || j < 0 || i >= WIDTH || j >= HEIGHT) { | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | canvas[j][i] |= bitmap->buffer[q * bitmap->width + p]; | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main(int argc, char* argv[]) | ||
+ | { | ||
+ | QCoreApplication a(argc, argv); | ||
+ | |||
+ | QString QString_FileName = "C:/FreeTypeStep02Test.txt"; | ||
+ | QString QString_ImageFileName = "C:/FreeTypeStep02Test.bmp"; | ||
+ | QFile QFile_Text(QString_FileName); | ||
+ | |||
+ | QString QString_ImageRowMonoBuffer = ""; | ||
+ | |||
+ | QSize QSize_xy(WIDTH, HEIGHT); | ||
+ | QImage QImage_Canvas(QSize_xy, QImage::Format_ARGB32_Premultiplied);//空イメージ | ||
+ | |||
+ | FT_Library library; | ||
+ | FT_Face face; | ||
+ | |||
+ | |||
+ | FT_GlyphSlot slot; | ||
+ | FT_Matrix matrix; | ||
+ | FT_Vector pen; | ||
+ | FT_Error error; | ||
+ | |||
+ | QString text = QString::fromUtf8(u8"あいさがしの果てに\n辿り付いた寝屋川市駅\nというtest文章"); | ||
+ | |||
+ | // ヒラギノ角5 DShirkg5.ttc BIZ UD ゴシック BIZ-UDGothicR.ttc KozGoPr6N-Medium.otf | ||
+ | QString filename = "C:\\Windows\\Fonts\\BIZ-UDGothicR.ttc"; | ||
+ | QString SingleStr = ""; | ||
+ | |||
+ | |||
+ | double angle; | ||
+ | int target_height; | ||
+ | int n; | ||
+ | int num_chars; | ||
+ | |||
+ | |||
+ | |||
+ | num_chars = text.length(); | ||
+ | angle = (0.0 / 360.0) * 3.141592 * 2.0; | ||
+ | target_height = HEIGHT; | ||
+ | |||
+ | error = FT_Init_FreeType(&library); | ||
+ | |||
+ | if (error) { | ||
+ | fprintf(stderr, qPrintable("ft init error\n")); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | error = FT_New_Face(library, filename.toUtf8(), 0, &face); | ||
+ | if (error) { | ||
+ | fprintf(stderr, qPrintable("new face error\n")); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | error = FT_Set_Char_Size(face, 30 * 64, 0, 100, 0); | ||
+ | if (error) { | ||
+ | fprintf(stderr, qPrintable("set size error\n")); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | slot = face->glyph; | ||
+ | |||
+ | #define LEFT64(x) ((x) << 6) // 1/64が1の 32 bit 26.6 ニィロク ロク固定小数。 | ||
+ | #define RIGHT64(x) ((x) >> 6) | ||
+ | |||
+ | |||
+ | pen.x = LEFT64(LEFT); | ||
+ | pen.y = LEFT64(target_height - 200); | ||
+ | |||
+ | matrix.xx = (FT_Fixed)(cos(angle) * 0x10000L); | ||
+ | matrix.xy = (FT_Fixed)(-sin(angle) * 0x10000L); | ||
+ | matrix.yx = (FT_Fixed)(sin(angle) * 0x10000L); | ||
+ | matrix.yy = (FT_Fixed)(cos(angle) * 0x10000L); | ||
+ | |||
+ | unsigned int* idces = new unsigned int[num_chars]; | ||
+ | for (int i = 0; i < num_chars; i++) { | ||
+ | if (text.at(i) == '\n') { | ||
+ | fprintf(stderr, qPrintable("%ld\n"), pen.x); | ||
+ | pen.x = LEFT64(LEFT); | ||
+ | pen.y += LEFT64(48); | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | SingleStr = text.at(i); | ||
+ | |||
+ | FT_Set_Transform(face, &matrix, &pen); | ||
+ | |||
+ | QChar ch = SingleStr.at(0); | ||
+ | FT_ULong FT_ULong_code = ch.unicode(); | ||
+ | idces[i] = FT_Get_Char_Index(face, FT_ULong_code); | ||
+ | |||
+ | error = FT_Load_Char(face, FT_ULong_code, FT_LOAD_RENDER); | ||
+ | if (error) { | ||
+ | continue; | ||
+ | } | ||
+ | draw_bitmap(&slot->bitmap, slot->bitmap_left, HEIGHT - slot->bitmap_top); | ||
+ | |||
+ | pen.x += slot->advance.x; | ||
+ | pen.y += slot->advance.y; | ||
+ | } | ||
+ | fprintf(stderr, qPrintable("\n")); | ||
+ | |||
+ | for (int y = 0; y < HEIGHT; y++) { | ||
+ | for (int x = 0; x < WIDTH; x++) { | ||
+ | QImage_Canvas.setPixel(x, y, qRgb(canvas[y][x], canvas[y][x], canvas[y][x])); | ||
+ | } | ||
+ | } | ||
+ | QImage_Canvas.save(QString_ImageFileName, "BMP"); | ||
+ | printf(qPrintable("P3\n%d %d\n%d\n"), WIDTH, HEIGHT, 255); | ||
+ | |||
+ | if (QFile_Text.open(QIODevice::WriteOnly)) { | ||
+ | QTextStream out(&QFile_Text); | ||
+ | for (int y = 0; y < HEIGHT; y++) { | ||
+ | for (int x = 0; x < WIDTH; x++) { | ||
+ | if (canvas[y][x]) { | ||
+ | QString_ImageRowMonoBuffer = QString_ImageRowMonoBuffer + " 1"; | ||
+ | //printf(qPrintable("%2d"), 1); | ||
+ | } | ||
+ | else { | ||
+ | QString_ImageRowMonoBuffer = QString_ImageRowMonoBuffer + " 0"; | ||
+ | //printf(qPrintable("%2d"), 0); | ||
+ | } | ||
+ | } | ||
+ | QString_ImageRowMonoBuffer = QString_ImageRowMonoBuffer + "\n"; | ||
+ | //printf(qPrintable("\n")); | ||
+ | |||
+ | out << QString_ImageRowMonoBuffer; | ||
+ | |||
+ | QString_ImageRowMonoBuffer = ""; | ||
+ | |||
+ | } | ||
+ | QFile_Text.close(); | ||
+ | } | ||
+ | else { | ||
+ | QString QString_Error(qPrintable("No Open or Create Error\n")); | ||
+ | QString_Error = QString_Error + QFile_Text.errorString(); | ||
+ | fprintf(stderr, qPrintable(QString_Error)); | ||
+ | } | ||
+ | delete[] idces; | ||
+ | FT_done_Face (face); | ||
+ | FT_Done_FreeType (library); | ||
+ | return a.exec(); | ||
+ | } | ||
+ | </Syntaxhighlight2> | ||
+ | |||
[[フォント TrueType 構造解析]]に戻る。 | [[フォント TrueType 構造解析]]に戻る。 |