Skip to content

Commit

Permalink
Stop using WTL in text_renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
yukawa committed Jan 26, 2024
1 parent 614cd5a commit c45042a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
2 changes: 0 additions & 2 deletions src/renderer/win32/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ mozc_cc_library(
name = "text_renderer",
srcs = ["text_renderer.cc"],
hdrs = ["text_renderer.h"],
copts = copts_wtl(),
tags = MOZC_TAGS.WIN_ONLY,
target_compatible_with = ["@platforms//os:windows"],
visibility = [
Expand All @@ -176,7 +175,6 @@ mozc_cc_library(
"//bazel/win32:dwrite",
"//protocol:renderer_cc_proto",
"//renderer:renderer_style_handler",
"//third_party/wtl",
"@com_google_absl//absl/types:span",
"@com_microsoft_wil//:wil",
],
Expand Down
61 changes: 31 additions & 30 deletions src/renderer/win32/text_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <dwrite.h>
#include <objbase.h>
#include <wil/com.h>
#include <wil/resource.h>

#include <cstddef>
#include <memory>
Expand All @@ -56,10 +57,6 @@ namespace win32 {

using ::mozc::renderer::RendererStyle;
using ::mozc::renderer::RendererStyleHandler;
using ::WTL::CDC;
using ::WTL::CDCHandle;
using ::WTL::CFont;
using ::WTL::CFontHandle;

namespace {

Expand Down Expand Up @@ -191,8 +188,9 @@ DWORD GetGdiDrawTextStyle(TextRenderer::FONT_TYPE type) {

class GdiTextRenderer : public TextRenderer {
public:
GdiTextRenderer() : render_info_(SIZE_OF_FONT_TYPE) {
mem_dc_.CreateCompatibleDC();
GdiTextRenderer()
: render_info_(SIZE_OF_FONT_TYPE),
mem_dc_(::CreateCompatibleDC(nullptr)) {
OnThemeChanged();
}

Expand All @@ -201,71 +199,74 @@ class GdiTextRenderer : public TextRenderer {
RenderInfo() : color(0), style(0) {}
COLORREF color;
DWORD style;
CFont font;
wil::unique_hfont font;
};

// TextRenderer overrides:
void OnThemeChanged() override {
// delete old fonts
for (size_t i = 0; i < SIZE_OF_FONT_TYPE; ++i) {
if (!render_info_[i].font.IsNull()) {
render_info_[i].font.DeleteObject();
if (render_info_[i].font.is_valid()) {
render_info_[i].font.reset();
}
}

for (size_t i = 0; i < SIZE_OF_FONT_TYPE; ++i) {
const auto font_type = static_cast<FONT_TYPE>(i);
const auto &log_font = GetLogFont(font_type);
render_info_[i].style = GetGdiDrawTextStyle(font_type);
render_info_[i].font.CreateFontIndirectW(&log_font);
render_info_[i].font.reset(::CreateFontIndirectW(&log_font));
render_info_[i].color = GetTextColor(font_type);
}
}

Size MeasureString(FONT_TYPE font_type,
const std::wstring_view str) const override {
const auto previous_font = mem_dc_.SelectFont(render_info_[font_type].font);
CRect rect;
mem_dc_.DrawTextW(str.data(), str.length(), &rect,
DT_NOPREFIX | DT_LEFT | DT_SINGLELINE | DT_CALCRECT);
mem_dc_.SelectFont(previous_font);
{
const auto previous_font =
wil::SelectObject(mem_dc_.get(), render_info_[font_type].font.get());
::DrawTextW(mem_dc_.get(), str.data(), str.length(), &rect,
DT_NOPREFIX | DT_LEFT | DT_SINGLELINE | DT_CALCRECT);
}
return Size(rect.Width(), rect.Height());
}

Size MeasureStringMultiLine(FONT_TYPE font_type, const std::wstring_view str,
const int width) const override {
const auto previous_font = mem_dc_.SelectFont(render_info_[font_type].font);
CRect rect(0, 0, width, 0);
mem_dc_.DrawTextW(str.data(), str.length(), &rect,
DT_NOPREFIX | DT_LEFT | DT_WORDBREAK | DT_CALCRECT);
mem_dc_.SelectFont(previous_font);
{
const auto previous_font =
wil::SelectObject(mem_dc_.get(), render_info_[font_type].font.get());
::DrawTextW(mem_dc_.get(), str.data(), str.length(), &rect,
DT_NOPREFIX | DT_LEFT | DT_WORDBREAK | DT_CALCRECT);
}
return Size(rect.Width(), rect.Height());
}

void RenderText(CDCHandle dc, const std::wstring_view text, const Rect &rect,
void RenderText(HDC dc, const std::wstring_view text, const Rect &rect,
FONT_TYPE font_type) const override {
std::vector<TextRenderingInfo> infolist;
infolist.emplace_back(std::wstring(text), rect);
RenderTextList(dc, infolist, font_type);
}

void RenderTextList(CDCHandle dc,
void RenderTextList(HDC dc,
const absl::Span<const TextRenderingInfo> display_list,
FONT_TYPE font_type) const override {
const auto &render_info = render_info_[font_type];
const auto old_font = dc.SelectFont(render_info.font);
const auto previous_color = dc.SetTextColor(render_info.color);
const auto old_font = wil::SelectObject(dc, render_info.font.get());
const auto previous_color = ::SetTextColor(dc, render_info.color);
for (const TextRenderingInfo &info : display_list) {
CRect rect = ToCRect(info.rect);
dc.DrawTextW(info.text.data(), info.text.size(), &rect,
render_info.style);
::DrawTextW(dc, info.text.data(), info.text.size(), &rect,
render_info.style);
}
dc.SetTextColor(previous_color);
dc.SelectFont(old_font);
::SetTextColor(dc, previous_color);
}

std::vector<RenderInfo> render_info_;
mutable CDC mem_dc_;
wil::unique_hdc mem_dc_;
};

class DirectWriteTextRenderer : public TextRenderer {
Expand Down Expand Up @@ -353,14 +354,14 @@ class DirectWriteTextRenderer : public TextRenderer {
return MeasureStringImpl(font_type, str, width, true);
}

void RenderText(CDCHandle dc, const std::wstring_view text, const Rect &rect,
void RenderText(HDC dc, const std::wstring_view text, const Rect &rect,
FONT_TYPE font_type) const override {
std::vector<TextRenderingInfo> infolist;
infolist.emplace_back(std::wstring(text), rect);
RenderTextList(dc, infolist, font_type);
}

void RenderTextList(CDCHandle dc,
void RenderTextList(HDC dc,
const absl::Span<const TextRenderingInfo> display_list,
FONT_TYPE font_type) const override {
constexpr size_t kMaxTrial = 3;
Expand All @@ -385,7 +386,7 @@ class DirectWriteTextRenderer : public TextRenderer {
}

HRESULT RenderTextListImpl(
CDCHandle dc, const absl::Span<const TextRenderingInfo> display_list,
HDC dc, const absl::Span<const TextRenderingInfo> display_list,
FONT_TYPE font_type) const {
CRect total_rect;
for (const auto &item : display_list) {
Expand Down
9 changes: 2 additions & 7 deletions src/renderer/win32/text_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
#ifndef MOZC_RENDERER_WIN32_TEXT_RENDERER_H_
#define MOZC_RENDERER_WIN32_TEXT_RENDERER_H_

// clang-format off
#include <windows.h>
#include <atlbase.h>
#include <atlapp.h>
#include <atlmisc.h>
// clang-format on

#include <memory>
#include <string>
Expand Down Expand Up @@ -94,9 +89,9 @@ class TextRenderer {
std::wstring_view str,
int width) const = 0;
// Renders the given |text|.
virtual void RenderText(WTL::CDCHandle dc, std::wstring_view text,
virtual void RenderText(HDC dc, std::wstring_view text,
const Rect &rect, FONT_TYPE font_type) const = 0;
virtual void RenderTextList(WTL::CDCHandle dc,
virtual void RenderTextList(HDC dc,
absl::Span<const TextRenderingInfo> display_list,
FONT_TYPE font_type) const = 0;
};
Expand Down
1 change: 1 addition & 0 deletions src/renderer/win32/win32_font_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define MOZC_RENDERER_WIN32_WIN32_FONT_UTIL_H_

#include <windows.h>
#include <commctrl.h>

namespace mozc {
namespace renderer {
Expand Down

0 comments on commit c45042a

Please sign in to comment.