[英]Draw text outline with Freetype
我已經在程序中實現了FreeType,我可以使用顏色和樣式(粗體,斜體,下划線)繪制文本。
現在,我想對我的文字進行輪廓處理。 我該怎么做?
(來源: googlecode.com )
我想再次進行測試:在“大綱”模式下繪制“背景”文本,在常規模式下繪制前景文本。 你怎么看待這件事?
(來源: googlecode.com )
渲染具有2個遍的文本是關鍵,但是您必須正確放置文本。 首先,您應該渲染整個輪廓,然后在其頂部渲染文本。
渲染輪廓:
// initialize stroker, so you can create outline font
FT_Stroker stroker;
FT_Stroker_New(library, &stroker);
// 2 * 64 result in 2px outline
FT_Stroker_Set(stroker, 2 * 64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
...
// generation of an outline for single glyph:
FT_UInt glyphIndex = FT_Get_Char_Index(face, glyphId);
FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
FT_Glyph glyph;
FT_Get_Glyph(face->glyph, &glyph);
FT_Glyph_StrokeBorder(&glyph, stroker, false, true);
FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, nullptr, true);
FT_BitmapGlyph bitmapGlyph = reinterpret_cast<FT_BitmapGlyph>(glyph);
// blit the glyph here on your target surface.
// For positioning use bitmapGlyph->left, bitmapGlyph->top
// For iteration over the glyph data use bitmapGlyph->bitmap.buffer, bitmapGlyph->bitmap.width, bitmapGlyph->bitmap.rows, bitmapGlyph->bitmap.pitch.
接下來,您必須在塗抹輪廓線的相同數據上呈現文本本身。 使用上面的代碼,但刪除FT_Glyph_StrokeBorder(&glyph, stroker, false, true);
線。 這樣,您將在大綱上顯示文本。
要獲得這種“卡通”文字效果,您必須執行4次傳遞:3個輪廓+ 1個文字。 在毛刺階段應進行紋理化或施加漸變。
繪制文本,然后對每個未完全着色的像素進行第二遍處理。為每個像素計算距最近的彩色像素有多遠。 如果它小於X,其中X是輪廓的所需寬度,則使用輪廓顏色對其進行着色。
對於大文本,這樣做可能會很慢,但是可以對其進行優化,並對其結果進行緩存以使其以可接受的速度快速運行。 這種方法可以完全自由使用各種輪廓和陰影效果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.