【问题标题】:DirectWrite space char wrappingDirectWrite 空间字符换行
【发布时间】:2019-08-07 20:56:12
【问题描述】:

首先对不起我的英语不好。但是最近几天我无法解决我的问题。我正在开发一个使用 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;
}

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

【问题讨论】:

    标签: direct2d directwrite


    【解决方案1】:

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

    DirectWrite 文本布局格式化功能对于一般的 UI 文本渲染来说已经足够了,任何更复杂的东西都需要更低级别的 DirectWrite API(文本分析器)和更复杂的应用程序端逻辑。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2020-10-27
    • 1970-01-01
    • 2014-07-30
    • 2011-01-17
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    相关资源
    最近更新 更多