Skip to content

Commit

Permalink
Stop using WTL in win32_image_util.cc
Browse files Browse the repository at this point in the history
As part of our effort to remove the dependency on WTL (#861), this
commit updates

  renderer/win32/win32_image_util.cc

so that WTL dependencies can be removed.

There must be no observable behavior change.

#codehealth

PiperOrigin-RevId: 601058242
  • Loading branch information
yukawa authored and hiroyuki-komatsu committed Jan 24, 2024
1 parent 8974313 commit 614cd5a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/renderer/win32/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,15 @@ mozc_cc_library(
name = "win32_image_util",
srcs = ["win32_image_util.cc"],
hdrs = ["win32_image_util.h"],
copts = copts_wtl(),
tags = MOZC_TAGS.WIN_ONLY,
target_compatible_with = ["@platforms//os:windows"],
deps = [
"//base:coordinates",
"//base:logging",
"//base:util",
"//third_party/wtl",
"//bazel/win32:gdi32",
"//bazel/win32:user32",
"@com_microsoft_wil//:wil",
],
)

Expand Down
67 changes: 27 additions & 40 deletions src/renderer/win32/win32_image_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@

#include "renderer/win32/win32_image_util.h"

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

#include <algorithm>
#include <bitset>
Expand All @@ -44,6 +39,7 @@
#include <memory>
#include <vector>

#include "base/coordinates.h"
#include "base/logging.h"
#include "base/util.h"

Expand All @@ -57,12 +53,6 @@ using ::mozc::renderer::win32::internal::SafeFrameBuffer;
using ::mozc::renderer::win32::internal::SubdivisionalPixel;
using ::mozc::renderer::win32::internal::TextLabel;

using ::WTL::CBitmap;
using ::WTL::CBitmapHandle;
using ::WTL::CDC;
using ::WTL::CFont;
using ::WTL::CFontHandle;

Rect GetBalloonBoundingRect(
double left, double top, double width, double height,
double balloon_tail_height, double balloon_tail_width,
Expand Down Expand Up @@ -418,46 +408,43 @@ std::vector<std::unique_ptr<TextLabel::BinarySubdivisionalPixel>> Get1bitGlyph(
bitmap_info.color_palette[1] = kForegroundColor; // white

uint8_t *buffer = nullptr;
CBitmap dib;
dib.CreateDIBSection(
wil::unique_hbitmap dib(::CreateDIBSection(
nullptr, reinterpret_cast<const BITMAPINFO *>(&bitmap_info),
DIB_RGB_COLORS, reinterpret_cast<void **>(&buffer), nullptr, 0);
DIB_RGB_COLORS, reinterpret_cast<void **>(&buffer), nullptr, 0));

CDC dc;
dc.CreateCompatibleDC(nullptr);
CBitmapHandle old_bitmap = dc.SelectBitmap(dib);
wil::unique_hdc dc(::CreateCompatibleDC(nullptr));
wil::unique_select_object old_bitmap(wil::SelectObject(dc.get(), dib.get()));

std::wstring wide_fontname;
Util::Utf8ToWide(fontname, &wide_fontname);
LOGFONT logfont = {};
logfont.lfWeight = FW_NORMAL;
logfont.lfCharSet = DEFAULT_CHARSET;
logfont.lfHeight = GetHeightFromDeciPoint(font_point * 10 * kDivision, dc);
logfont.lfHeight = GetHeightFromDeciPoint(font_point * 10 * kDivision,
dc.get());
logfont.lfQuality = NONANTIALIASED_QUALITY;
const errno_t error = wcscpy_s(logfont.lfFaceName, wide_fontname.c_str());
if (error != 0) {
LOG(ERROR) << "Failed to copy fontname: " << fontname;
return pixels;
}

CFont font_handle;
font_handle.CreateFontIndirectW(&logfont);
const CFontHandle old_font = dc.SelectFont(font_handle);
const CPoint lefttop(
static_cast<int>(floor((left - bounding_rect.Left()) * kDivision)),
static_cast<int>(floor((top - bounding_rect.Top()) * kDivision)));
const CSize size(static_cast<int>(ceil(width * kDivision)),
static_cast<int>(ceil(height * kDivision)));

CRect rect(lefttop, size);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(255, 255, 255));
wil::unique_hfont font_handle(::CreateFontIndirectW(&logfont));
wil::unique_select_object old_font(
wil::SelectObject(dc.get(), font_handle.get()));
const int rect_left =
static_cast<int>(floor((left - bounding_rect.Left()) * kDivision));
const int rect_top =
static_cast<int>(floor((top - bounding_rect.Top()) * kDivision));
RECT rect = {rect_left, rect_top,
rect_left + static_cast<int>(ceil(width * kDivision)),
rect_top + static_cast<int>(ceil(height * kDivision))};
::SetBkMode(dc.get(), TRANSPARENT);
::SetTextColor(dc.get(), RGB(255, 255, 255));
std::wstring wide_text;
Util::Utf8ToWide(text, &wide_text);
dc.DrawTextW(wide_text.c_str(), wide_text.size(), &rect,
DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_CENTER);
dc.SelectFont(old_font);
dc.SelectBitmap(old_bitmap);
::DrawTextW(dc.get(), wide_text.c_str(), wide_text.size(), &rect,
DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_CENTER);
::GdiFlush();

// DIB section requires 32-bit alignment for each line.
Expand Down Expand Up @@ -643,9 +630,9 @@ HBITMAP BalloonImage::CreateInternal(const BalloonImageInfo &info,
bitmap_info.bmiHeader.biCompression = BI_RGB;
bitmap_info.bmiHeader.biSizeImage = 0;
PBGRA *buffer = nullptr;
CBitmap dib;
dib.CreateDIBSection(nullptr, &bitmap_info, DIB_RGB_COLORS,
reinterpret_cast<void **>(&buffer), nullptr, 0);
wil::unique_hbitmap dib(
::CreateDIBSection(nullptr, &bitmap_info, DIB_RGB_COLORS,
reinterpret_cast<void **>(&buffer), nullptr, 0));

struct Accessor {
public:
Expand Down Expand Up @@ -735,7 +722,7 @@ HBITMAP BalloonImage::CreateInternal(const BalloonImageInfo &info,
}
}

return dib.Detach();
return dib.release();
}

namespace internal {
Expand Down

0 comments on commit 614cd5a

Please sign in to comment.