Search code examples
direct2ddirectwrite

IDWriteTextLayout doesn't trim Japanese word?


I followed an example here to trim texts using ellipsis sign: http://cx5software.sakura.ne.jp/blog/2011/01/18/directwrite-ellipsis-trimming-sign/

It works fine for English text, but it doesn't work for Japanese text. Does anyone know why ?

Here is the snippet of my code that tries to write a text: "日本語English"

                m_dwrite_factory->CreateTextFormat(L"Arial Unicode MS", NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 14.0f * 96.0f/72.0f, L"ja-JP", m_dwrite_text_format.GetAddressOf());

                std::wstring text(L"日本語English");

                IDWriteInlineObjectPtr trimming;
                m_dwrite_factory->CreateEllipsisTrimmingSign(m_dwrite_text_format.Get(), trimming.GetAddressOf());

                IDWriteTextLayoutPtr layout;
                m_dwrite_factory->CreateTextLayout(text.c_str(), (UINT32)text.length(), m_dwrite_text_format.Get(), 50.0f, 100.0f, layout.GetAddressOf());

                DWRITE_TRIMMING trimmingOpt = {DWRITE_TRIMMING_GRANULARITY_CHARACTER, 0, 0};
                layout->SetTrimming(&trimmingOpt, trimming.Get());

                d2d->DrawTextLayout(D2D1::Point2F(0, 0), layout.Get(), m_d2d_brush_2.Get());

And here is the resulting text. Note that the ellipsis sign is added on English characters but not on the Japanese text.

enter image description here


Solution

  • Ideographs are allowed to break lines between characters whereas English words do not have break opportunities between characters unless separated by space or other punctuation, based on the rules from Unicode UAX 14 (http://www.unicode.org/reports/tr14/). So your Japanese text wraps due to the narrow 50 DIPs width meaning each line is less than the layout width (so no trimming happens), but the English word cannot wrap and gets trimmed because it exceeds the layout width.