FreeTypeを使う 新しいページはコチラ
提供: yonewiki
(→サンプル3.公式サイトのErik Mollerさん作成 Qt版) |
(→サンプル3.公式サイトのErik Mollerさん作成 Qt版) |
||
1,035行: | 1,035行: | ||
[[ファイル:FreeTypeStep03Test.jpg|||none|実行結果]] | [[ファイル:FreeTypeStep03Test.jpg|||none|実行結果]] | ||
+ | |||
+ | === ''' サンプル4.公式サイトのErik Mollerさん作成その2 Qt版 ''' === | ||
+ | これは、FreeType2.3.10以降で動作するとされているサンプルで、スタンドアロン B/W(Black/White:白黒:モノトーン)ラスタライザftrster.cの使用方法だそうです。なにが、それほど画期的なモノなのかよくわかっていませんが、フォントファイルを使わずにグラフィックを生成できます。内側と外側の概念があって塗りつぶしもできるようになっています。内側の中に外側だけの図形を描画して、穴をあけたような、図を描画できるかまでは把握していません。文字からの図形にかぎられていたのが、簡単な図形なら、描画できるようになったのが画期的なのかもしれません。 | ||
+ | |||
+ | <Syntaxhighlight2 lang="cpp" line="1"> | ||
+ | #include <QtCore/QCoreApplication> | ||
+ | |||
+ | #include "ftraster.c" | ||
+ | #include "ftraster.h" | ||
+ | #include "ftmisc.h" | ||
+ | #include <fstream> | ||
+ | |||
+ | #include <ft2build.h> | ||
+ | #include FT_FREETYPE_H | ||
+ | |||
+ | #include FT_SYSTEM_H | ||
+ | #include FT_OUTLINE_H | ||
+ | |||
+ | struct Vec2 { | ||
+ | Vec2(float a, float b) : x(a), y(b){} | ||
+ | float x, y; | ||
+ | }; | ||
+ | |||
+ | static Vec2 k_shape[] = { | ||
+ | Vec2(- 3, -18), Vec2( 0, -12), Vec2( 6, -10), Vec2( 12, - 6), Vec2( 12, - 4), | ||
+ | Vec2( 11, - 4), Vec2( 10, - 5), Vec2( 10, 1), Vec2( 9, 6), Vec2( 7, 10), | ||
+ | Vec2( 5, 12), Vec2( 4, 15), Vec2( 3, 14), Vec2( 1, 13), Vec2(- 1, 13), | ||
+ | Vec2(- 5, 11), Vec2(- 8, 8), Vec2(-11, 2), Vec2(-11, - 2), Vec2(-14, 0), | ||
+ | Vec2(-14, - 2), Vec2(-11, - 7), Vec2(- 9, - 9), Vec2(- 8, - 9), Vec2(- 5, -12), | ||
+ | Vec2(- 5, -14), Vec2(- 7, -15), Vec2(- 8, -14), Vec2(- 9, -15), Vec2(- 9, -17), | ||
+ | Vec2(- 7, -17), Vec2(- 6, -18) | ||
+ | }; | ||
+ | |||
+ | void* MY_Alloc_Func(FT_Memory memory, long size){ | ||
+ | return malloc((size_t)size); | ||
+ | } | ||
+ | |||
+ | void MY_Free_Func(FT_Memory memory, void* block) { | ||
+ | free(block); | ||
+ | } | ||
+ | |||
+ | void* MY_Realloc_Func(FT_Memory, long cur_size, long new_size, void* block) { | ||
+ | return realloc(block, (size_t)new_size); | ||
+ | } | ||
+ | |||
+ | static FT_Memory mem; | ||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | QCoreApplication a(argc, argv); | ||
+ | |||
+ | mem = new FT_MemoryRec_; | ||
+ | mem->alloc = MY_Alloc_Func; | ||
+ | mem->free = MY_Free_Func; | ||
+ | mem->realloc = MY_Realloc_Func; | ||
+ | |||
+ | FT_Outline_ outline; | ||
+ | outline.n_contours = 1; | ||
+ | outline.n_points = sizeof(k_shape) / sizeof(Vec2); | ||
+ | outline.points = new FT_Vector[outline.n_points]; | ||
+ | |||
+ | for (int i = 0; i < outline.n_points; ++i) { | ||
+ | FT_Vector v; | ||
+ | v.x = (20 + k_shape[i].x) * 10 * 64; | ||
+ | v.y = (20 + k_shape[i].y) * 10 * 64; | ||
+ | outline.points[i] = v; | ||
+ | } | ||
+ | outline.tags = new char[outline.n_points]; | ||
+ | for (int i = 0; i < outline.n_points; ++i) { | ||
+ | outline.tags[i] = 1; | ||
+ | } | ||
+ | outline.contours = new short[outline.n_contours]; | ||
+ | outline.contours[0] = outline.n_points - 1; | ||
+ | outline.flags = 0; | ||
+ | |||
+ | const int width = 500; | ||
+ | const int rows = 400; | ||
+ | |||
+ | const int pitch_mono = (width + 7) >> 3; | ||
+ | |||
+ | FT_Bitmap bmp; | ||
+ | FT_Raster_Params params; | ||
+ | |||
+ | const int kRenderPoolSize = 1024 * 1024; | ||
+ | unsigned char* renderPool = new unsigned char[kRenderPoolSize]; | ||
+ | |||
+ | bmp.buffer = new unsigned char[rows * pitch_mono]; | ||
+ | memset(bmp.buffer, 0, rows * pitch_mono); | ||
+ | bmp.width = width; | ||
+ | bmp.rows = rows; | ||
+ | bmp.pitch = pitch_mono; | ||
+ | bmp.pixel_mode = FT_PIXEL_MODE_MONO; | ||
+ | |||
+ | memset(¶ms, 0, sizeof(params)); | ||
+ | params.source = &outline; | ||
+ | params.target = &bmp; | ||
+ | |||
+ | FT_Raster raster; | ||
+ | |||
+ | //FT_Raster_Funcs ft_standard_raster; | ||
+ | ft_standard_raster.raster_new(mem, &raster); | ||
+ | ft_standard_raster.raster_reset(raster, renderPool, kRenderPoolSize); | ||
+ | ft_standard_raster.raster_render(raster, ¶ms); | ||
+ | |||
+ | std::ofstream out_mono("out-mono.pbm", std::ios::binary); | ||
+ | out_mono << "P4 " << width << " " << rows << "\n"; | ||
+ | out_mono.write((const char*)bmp.buffer, rows * pitch_mono); | ||
+ | |||
+ | delete[] renderPool; | ||
+ | delete[] bmp.buffer; | ||
+ | delete[] outline.points; | ||
+ | delete[] outline.tags; | ||
+ | delete[] outline.contours; | ||
+ | delete mem; | ||
+ | |||
+ | return a.exec(); | ||
+ | } | ||
+ | |||
+ | </Syntaxhighlight2> | ||
+ | |||
+ | |||
+ | 実行結果 | ||
+ | |||
+ | [[ファイル:Out-mono.bmp|||none|実行結果]] | ||
+ | |||
+ | |||
+ | 上記のようなプログラムで更に、いろいろと必要なプログラムをつまびかないと動作させられませんでした。 | ||
+ | |||
+ | プロジェクトにftimege.hとftmisc.h | ||
[[フォント TrueType 構造解析]]に戻る。 | [[フォント TrueType 構造解析]]に戻る。 |