DirectWrite空间字符包装

时间:2019-03-17 08:42:23

标签: direct2d directwrite

首先,对不起我的英语不好。但是最近几天我无法解决我的问题。我正在使用DirectWrite呈现文本的简单文本编辑器上工作。当我渲染包含文本的缓冲区时,direct2d会包装我的文字,但是当我键入空格字符''或制表符'\ t'时,这将不起作用。我的光标移到了编辑器的窗口之外,没有“跳到”新行。

int RenderSystem::Init(
    Settings* set,
    HWND hwnd
)
{
    if (system_is_init)
        return 0;

    settings = set;

    D2D1CreateFactory(
        D2D1_FACTORY_TYPE_SINGLE_THREADED,
        &factory
    );
    DWriteCreateFactory(
        DWRITE_FACTORY_TYPE_SHARED,
        __uuidof(IDWriteFactory),
        (IUnknown**)(&write_factory)
    );

    LoadFontCollection(L"Fonts/liberation-mono/liberation-mono.ttf");

    write_factory->CreateTextFormat(
        L"Liberation Mono",
        font_collection,
        DWRITE_FONT_WEIGHT_REGULAR,
        DWRITE_FONT_STYLE_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL,
        (float)settings->font_size,
        L"en-us",
        &text_format
    );

    text_format->SetWordWrapping(
        DWRITE_WORD_WRAPPING_WRAP
    );

    text_renderer = new BasicTextRenderer();

    return 0;
}


void RenderSystem::Render(
    ID2D1HwndRenderTarget* render_target,
    Buffer* buffer
)
{
    render_target->CreateSolidColorBrush(
        settings->text_foreground_color,
        &text_foreground_brush
    );
    render_target->CreateSolidColorBrush(
        settings->cursor_background_color,
        &cursor_background_brush
    );
    render_target->CreateSolidColorBrush(
        settings->cursor_foreground_colot,
        &cursor_foreground_brush
    );

    D2D1_SIZE_U render_target_size = render_target->GetPixelSize();
    IDWriteTextLayout* text_layout;

    UINT size;
    WCHAR* text = buffer->GetData(size); //Here i get text from my buffer
    write_factory->CreateTextLayout(
        text,
        size,
        text_format,
        (float)render_target_size.width,
        (float)render_target_size.height,
        &text_layout
    );
    delete[] text;

    DrawingContext* drawing_context = new DrawingContext(
        render_target,
        text_foreground_brush
    );

    DrawingEffect* cursor_effect = new DrawingEffect(
        cursor_foreground_brush,
        cursor_background_brush
    );

    DWRITE_TEXT_RANGE text_range;
    text_range.startPosition = buffer->GetCursorPos();
    text_range.length = 1;

    text_layout->SetDrawingEffect(
        cursor_effect,
        text_range
    );

    render_target->BeginDraw();
    render_target->Clear(settings->background_color);

    text_layout->Draw(
        drawing_context,
        text_renderer,
        0,
        0
    );

    render_target->EndDraw();

    text_layout->Release();
    text_foreground_brush->Release();
    cursor_background_brush->Release();
    cursor_foreground_brush->Release();
    delete drawing_context;
}

我希望有人能理解我的问题...谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为这不一定是错误的。这只是DirectWrite处理尾随空格字符的方式-它们不会影响换行宽度。您可以尝试不同的换行模式,但是可能需要使用DirectWrite API中的群集指标和换行位置自己实现布局逻辑。

DirectWrite文本布局格式设置功能足以用于常规UI文本呈现,任何更复杂的操作都需要较低级别的DirectWrite API(文本分析器)和应用程序端更复杂的逻辑。