FreeTypeを使う 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(概要)
(サンプル1.Qt ConsoleApplicationでテキスト出力)
135行: 135行:
  
  
 +
<Syntaxhighlight2 lang="cpp" line=1>
 +
#include <QtCore/QCoreApplication>
 +
#include <QString>
 +
#include <QFile>
 +
#include <QTextStream>
 +
#include <QImage>
 +
#include <QColor>
 +
#include <QSize>
 +
 +
#include <ft2build.h>
 +
#include FT_FREETYPE_H
 +
 +
static unsigned int text[] = {
 +
    0x00003042, 0x00003044, 0x00003046, 0x00003048, //あ、い、う、え
 +
    0x0000304a, 0x0000304b, 0x0000304d, 0x0000304f, //お、か、き、く
 +
    0x00003051, 0x00003053, 0x00003055, 0x00003057, //け、こ、さ、し
 +
    0x00003059, 0x0000305b, 0x0000305d, 0x0000305f, //す、せ、そ、た
 +
    0x00003061, 0x00003064, 0x00003066, 0x00003068, //ち、つ、て、と
 +
    0x0000000a, //改行
 +
    0x00000061, 0x00000062, 0x00000063, 0x00000064, //a、b、c、d
 +
    0x00000065, 0x00000066, 0x00000067, 0x00000068, //e、f、g、h
 +
    0x00000069, 0x0000006a, 0x0000006b, 0x0000006c, //i、j、k、l
 +
    0x0000006d, 0x0000006e, 0x0000006f, 0x00000070, //m、n、o、p
 +
    0x00000071, 0x00000072, 0x00000073, 0x00000074, //q、r、s、t
 +
    0x00000075, 0x00000076, 0x00000077, 0x00000078, //u、v、w、x
 +
    0x00000079, 0x0000007a,                        //y, z
 +
    0x00000061, 0x00000062, 0x00000063, 0x00000064, //a、b、c、d
 +
    0x00000065, 0x00000066, 0x00000067, 0x00000068, //e、f、g、h
 +
    0x00000069, 0x0000006a, 0x0000006b, 0x0000006c, //i、j、k、l
 +
    0x0000006d, 0x0000006e, 0x0000006f, 0x00000070, //m、n、
 +
    0x0000000a, //改行
 +
};
 +
#define LEN (sizeof text / sizeof text[0])
 +
 +
#define WIDTH 1024
 +
#define HEIGHT 256
 +
#define LEFT 16
 +
#define TOP 32
 +
 +
static unsigned char canvas[HEIGHT][WIDTH];
 +
 +
static void draw(FT_Bitmap* bitmap, int x, int y) {
 +
    for (int j = 0; j < bitmap->rows; j++) {
 +
        for (int i = 0; i < bitmap->width; i++) {
 +
            unsigned char c = bitmap->buffer[j * bitmap->pitch + i];
 +
            if (c) {
 +
                if (y + j >= 0 && y + j < HEIGHT) {
 +
                    if (x + i >= 0 && x + i < WIDTH) {
 +
                        canvas[y + j][x + i] = c;
 +
                    }
 +
                }
 +
            }
 +
        }
 +
    }
 +
}
 +
 +
int main(int argc, char* argv[])
 +
{
 +
    QCoreApplication a(argc, argv);
 +
 +
    QString QString_FileName = "C:/FreeTypeStep01Test.txt";
 +
    QString QString_ImageFileName = "C:/FreeTypeStep01Test.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;
 +
    int error;
 +
 +
    error = FT_Init_FreeType(&library);
 +
 +
    if (error) {
 +
        fprintf(stderr, qPrintable("ft init error\n"));
 +
        exit(1);
 +
    }
 +
 +
 +
    FT_Face face;
 +
    error = FT_New_Face(library, qPrintable("C:\\Windows\\Fonts\\KozGoPr6N-Medium.otf"), 0, &face);
 +
    if (error) {
 +
        fprintf(stderr, qPrintable("new face error\n"));
 +
        exit(1);
 +
    }
 +
 +
    error = FT_Set_Char_Size(face, 0, 8 * 64, 300, 300);
 +
    if (error) {
 +
        fprintf(stderr, qPrintable("set size error\n"));
 +
        exit(1);
 +
    }
 +
 +
#define LEFT64(x) ((x) << 6) // 1/64が1の 32 bit 26.6 ニィロク ロク固定小数。
 +
#define RIGHT64(x) ((x) >> 6)
 +
 +
    FT_Vector pen;
 +
    pen.x = LEFT64(LEFT);
 +
    pen.y = RIGHT64(TOP);
 +
    unsigned int idces[LEN];
 +
    for (int i = 0; i < LEN; i++) {
 +
        if (text[i] == '\n') {
 +
            fprintf(stderr, qPrintable("%ld\n"), pen.x);
 +
            pen.x = LEFT64(LEFT);
 +
            pen.y += LEFT64(48);
 +
            continue;
 +
        }
 +
        idces[i] = FT_Get_Char_Index(face, text[i]);
 +
 +
        error = FT_Load_Glyph(face, idces[i], FT_LOAD_DEFAULT);
 +
        if (error) {
 +
            fprintf(stderr, qPrintable("no glyph error/\n"));
 +
            exit(1);
 +
        }
 +
 +
        error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
 +
        if (error) {
 +
            fprintf(stderr, qPrintable("glyph render error\n"));
 +
            exit(1);
 +
        }
 +
       
 +
        draw(&face->glyph->bitmap,
 +
            RIGHT64(pen.x) + face->glyph->bitmap_left,
 +
            RIGHT64(pen.y) + face->glyph->bitmap_top);
 +
 +
        pen.x += face->glyph->advance.x;
 +
    }
 +
    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));
 +
    }
 +
 +
    return a.exec();
 +
}
 +
</Syntaxhighlight2>
  
 
 
 
 
  
 
[[フォント TrueType 構造解析]]に戻る。
 
[[フォント TrueType 構造解析]]に戻る。

2022年8月12日 (金) 00:00時点における版



個人用ツール
名前空間

変種
操作
案内
ツールボックス