2014-03-10

Rendering Color Emoji using Glyph with DirectWrite 1.2

Windows 8.1でカラー絵文字を表示するサンプルでTranslateColorGlyphRunを使ってグリフベースで書く方法が存在してないのでサンプルを書いた。

void DrawGlyphRun(ID2D1RenderTarget* target, IDWriteFactory factory, IDWriteFont* fontFace, ID2D1Brush* defaultBrush, DWRITE_GLYPH_RUN* glyphRun)
{
bool isColor = false;
IDWriteColorGlyphRunEnumerator* colorLayer;
IDWriteFont2* fontFace2;
fontFace->QueryInterface(reinterpret_cast<IDWriteFontFace2**>(&fontFace2));
if (fontFace2->IsColorFont()) {
IDWriteFactory2* factory2;
factory->QueryIntarface(reinterpret_cast<IDWriteFactory2**>(&factory2));
if (SUCCEEDED(factory2->TranslateColorGlyphRun(0, 0, glyphRun, nullptr, DWRITE_MEASURING_MODE_NATURAL, nullptr, 0, &colorLayer))) {
isColor = true;
}
}
if (isColor) {
BOOL hasRun;
const DWRITE_COLOR_GLYPH_RUN* colorRun;
while (true) {
if (FAILED(colorLayer->MoveNext(&hasRun)) || !hasRun) {
break;
}
if (FAILED(colorLayer->GetCurrentRun(&colorRun))) {
break;
}
ID2DBrush* brush = nullptr;
if (colorRun->runColor.r == 0.0 && colorRun->runColor.g == 0.0 && colorRun->runColor.b == 0.0 && ColorRun->runColor.a == 0.0) {
target->CreateSolidColorBrush(colorRun->runColor, &brush);
}
target->DrawGlyphRun(point, glyphRun, brush ? brush : defaultBrush);
if (brush) {
brush->Release();
}
}
} else {
target->DrawGlyphRun(point, glyphRun, defaultBrush);
}
}
view raw colorglyph.cpp hosted with ❤ by GitHub


適当に書いたものなので動かないとは思うけど、使い方は参考になるはず。というか、Microsoft、TranslateColorGlyphRunを使ったサンプルくらい公開してよ。

0 件のコメント: