Skip to content

Commit

Permalink
Remove the dependency on WTL::CLogFont
Browse files Browse the repository at this point in the history
  • Loading branch information
yukawa committed Jan 21, 2024
1 parent 271b6ae commit 54a70db
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 35 deletions.
5 changes: 2 additions & 3 deletions src/renderer/win32/indicator_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "base/util.h"
#include "protocol/commands.pb.h"
#include "protocol/renderer_command.pb.h"
#include "renderer/win32/win32_font_util.h"
#include "renderer/win32/win32_image_util.h"
#include "renderer/win32/win32_renderer_util.h"

Expand All @@ -62,7 +63,6 @@ using ATL::CWinTraits;
using WTL::CBitmap;
using WTL::CBitmapHandle;
using WTL::CDC;
using WTL::CLogFont;

using ::mozc::commands::Status;
typedef ::mozc::commands::RendererCommand::ApplicationInfo ApplicationInfo;
Expand Down Expand Up @@ -254,8 +254,7 @@ class IndicatorWindow::WindowImpl

void LoadSprite(int mode) {
BalloonImage::BalloonImageInfo info;
CLogFont logfont;
logfont.SetMessageBoxFont();
LOGFONT logfont = GetMessageBoxLogFont();
Util::WideToUtf8(logfont.lfFaceName, &info.label_font);

info.frame_color = RGBColor(1, 122, 204);
Expand Down
43 changes: 16 additions & 27 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 @@ -48,6 +49,7 @@
#include "base/logging.h"
#include "protocol/renderer_style.pb.h"
#include "renderer/renderer_style_handler.h"
#include "renderer/win32/win32_font_util.h"

namespace mozc {
namespace renderer {
Expand All @@ -59,7 +61,6 @@ using ::WTL::CDC;
using ::WTL::CDCHandle;
using ::WTL::CFont;
using ::WTL::CFontHandle;
using ::WTL::CLogFont;

namespace {

Expand Down Expand Up @@ -111,28 +112,24 @@ COLORREF GetTextColor(TextRenderer::FONT_TYPE type) {
return RGB(0, 0, 0);
}

CLogFont GetLogFont(TextRenderer::FONT_TYPE type) {
LOGFONT GetLogFont(TextRenderer::FONT_TYPE type) {
LOGFONT font = GetMessageBoxLogFont();

switch (type) {
case TextRenderer::FONTSET_SHORTCUT: {
CLogFont font;
font.SetMessageBoxFont();
font.MakeLarger(3);
font.lfHeight += (font.lfHeight > 0 ? 3 : -3);
font.lfWeight = FW_BOLD;
return font;
}
case TextRenderer::FONTSET_CANDIDATE: {
CLogFont font;
font.SetMessageBoxFont();
font.MakeLarger(3);
font.lfHeight += (font.lfHeight > 0 ? 3 : -3);
font.lfWeight = FW_NORMAL;
return font;
}
case TextRenderer::FONTSET_DESCRIPTION:
case TextRenderer::FONTSET_FOOTER_INDEX:
case TextRenderer::FONTSET_FOOTER_LABEL:
case TextRenderer::FONTSET_FOOTER_SUBLABEL: {
CLogFont font;
font.SetMessageBoxFont();
font.lfWeight = FW_NORMAL;
return font;
}
Expand All @@ -147,20 +144,14 @@ CLogFont GetLogFont(TextRenderer::FONT_TYPE type) {
const auto &infostyle = style.infolist_style();
switch (type) {
case TextRenderer::FONTSET_INFOLIST_CAPTION: {
CLogFont font;
font.SetMessageBoxFont();
font.lfHeight = -infostyle.caption_style().font_size();
return font;
}
case TextRenderer::FONTSET_INFOLIST_TITLE: {
CLogFont font;
font.SetMessageBoxFont();
font.lfHeight = -infostyle.title_style().font_size();
return font;
}
case TextRenderer::FONTSET_INFOLIST_DESCRIPTION: {
CLogFont font;
font.SetMessageBoxFont();
font.lfHeight = -infostyle.description_style().font_size();
return font;
}
Expand All @@ -169,8 +160,6 @@ CLogFont GetLogFont(TextRenderer::FONT_TYPE type) {
}

LOG(DFATAL) << "Unknown type: " << type;
CLogFont font;
font.SetMessageBoxFont();
return font;
}

Expand Down Expand Up @@ -213,30 +202,29 @@ 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();
}
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(::CreateFontIndirect(&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);
const auto previous_font = mem_dc_.SelectFont(
render_info_[font_type].font.get());
CRect rect;
mem_dc_.DrawTextW(str.data(), str.length(), &rect,
DT_NOPREFIX | DT_LEFT | DT_SINGLELINE | DT_CALCRECT);
Expand All @@ -246,7 +234,8 @@ class GdiTextRenderer : public TextRenderer {

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);
const auto previous_font = mem_dc_.SelectFont(
render_info_[font_type].font.get());
CRect rect(0, 0, width, 0);
mem_dc_.DrawTextW(str.data(), str.length(), &rect,
DT_NOPREFIX | DT_LEFT | DT_WORDBREAK | DT_CALCRECT);
Expand All @@ -265,7 +254,7 @@ class GdiTextRenderer : public TextRenderer {
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 old_font = dc.SelectFont(render_info.font.get());
const auto previous_color = dc.SetTextColor(render_info.color);
for (const TextRenderingInfo &info : display_list) {
CRect rect = ToCRect(info.rect);
Expand Down Expand Up @@ -465,7 +454,7 @@ class DirectWriteTextRenderer : public TextRenderer {
return color;
}

wil::com_ptr_nothrow<IDWriteTextFormat> CreateFormat(CLogFont logfont) {
wil::com_ptr_nothrow<IDWriteTextFormat> CreateFormat(const LOGFONT &logfont) {
HRESULT hr = S_OK;
wil::com_ptr_nothrow<IDWriteFont> font;
hr = dwrite_interop_->CreateFontFromLOGFONT(&logfont, font.put());
Expand Down
69 changes: 69 additions & 0 deletions src/renderer/win32/win32_font_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2010-2021, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef MOZC_RENDERER_WIN32_WIN32_FONT_UTIL_H_
#define MOZC_RENDERER_WIN32_WIN32_FONT_UTIL_H_

#include <windows.h>

namespace mozc {
namespace renderer {
namespace win32 {

inline LOGFONT GetMessageBoxLogFont() {
NONCLIENTMETRICS info = {};
info.cbSize = sizeof(info);
if (::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(info), &info, 0)) {
return info.lfMessageFont;
} else {
// Fallback font.
return {
.lfHeight = -12,
.lfWidth = 0,
.lfEscapement = 0,
.lfOrientation = 0,
.lfWeight = 400,
.lfItalic = 0,
.lfUnderline = 0,
.lfStrikeOut = 0,
.lfCharSet = 1,
.lfOutPrecision = 0,
.lfClipPrecision = 0,
.lfQuality = 0,
.lfPitchAndFamily = 0,
.lfFaceName = L"Segoe UI",
};
}
}

} // namespace win32
} // namespace renderer
} // namespace mozc

#endif // MOZC_RENDERER_WIN32_WIN32_FONT_UTIL_H_
17 changes: 14 additions & 3 deletions src/renderer/win32/win32_image_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ using ::WTL::CBitmapHandle;
using ::WTL::CDC;
using ::WTL::CFont;
using ::WTL::CFontHandle;
using ::WTL::CLogFont;

Rect GetBalloonBoundingRect(
double left, double top, double width, double height,
Expand Down Expand Up @@ -94,6 +93,18 @@ Rect GetBalloonBoundingRect(
return Rect(int_left, int_top, int_right - int_left, int_bottom - int_top);
}

LONG GetHeightFromDeciPoint(LONG height_dp, HDC dc_handle) {
POINT transformed = {
.x = 0,
// 72 points/inch, 10 decipoints/point
.y = ::MulDiv(::GetDeviceCaps(dc_handle, LOGPIXELSY), height_dp, 720),
};
POINT origin = {.x = 0, .y = 0};
::DPtoLP(dc_handle, &transformed, 1);
::DPtoLP(dc_handle, &origin, 1);
return -std::abs(transformed.y - origin.y);
}

class Balloon {
public:
Balloon(double left, double top, double width, double height,
Expand Down Expand Up @@ -418,10 +429,10 @@ std::vector<std::unique_ptr<TextLabel::BinarySubdivisionalPixel>> Get1bitGlyph(

std::wstring wide_fontname;
Util::Utf8ToWide(fontname, &wide_fontname);
CLogFont logfont;
LOGFONT logfont = {};
logfont.lfWeight = FW_NORMAL;
logfont.lfCharSet = DEFAULT_CHARSET;
logfont.SetHeightFromDeciPoint(font_point * 10 * kDivision, dc);
logfont.lfHeight = GetHeightFromDeciPoint(font_point * 10 * kDivision, dc);
logfont.lfQuality = NONANTIALIASED_QUALITY;
const errno_t error = wcscpy_s(logfont.lfFaceName, wide_fontname.c_str());
if (error != 0) {
Expand Down
3 changes: 1 addition & 2 deletions src/win32/base/text_icon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ using ::WTL::CDC;
using ::WTL::CFont;
using ::WTL::CFontHandle;
using ::WTL::CIconHandle;
using ::WTL::CLogFont;

RGBQUAD ToRGBQuad(DWORD color_ref) {
const RGBQUAD rgbquad = {GetBValue(color_ref), GetGValue(color_ref),
Expand Down Expand Up @@ -115,7 +114,7 @@ CIconHandle CreateMonochromeIconInternal(int bitmap_width, int bitmap_height,
}
CBitmapHandle old_bitmap = dc.SelectBitmap(src_dib);

CLogFont logfont;
LOGFONT logfont = {};
{
logfont.lfWeight = FW_NORMAL;
logfont.lfCharSet = DEFAULT_CHARSET;
Expand Down

0 comments on commit 54a70db

Please sign in to comment.