首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DirectWrite空格字符换行

DirectWrite空格字符换行
EN

Stack Overflow用户
提问于 2019-03-17 16:42:23
回答 1查看 338关注 0票数 1

首先,我为我糟糕的英语感到抱歉。但是这几天我不能解决我的问题。我正在开发一个简单的文本编辑器,它使用DirectWrite来渲染文本。当我呈现包含文本的缓冲区时,direct2d会对我的单词进行换行,但当我键入空格字符‘’或制表符'\t‘时,这不起作用。我的光标移出了编辑器的窗口,并且没有“跳转”到新行。

代码语言:javascript
复制
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;
}

我希望有人能理解我的问题所在。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-03-17 21:24:43

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55205287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档