diff --git a/src/renderer/win32/indicator_window.cc b/src/renderer/win32/indicator_window.cc index 5c885b2fab..aa7d659d66 100644 --- a/src/renderer/win32/indicator_window.cc +++ b/src/renderer/win32/indicator_window.cc @@ -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" @@ -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; @@ -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); diff --git a/src/renderer/win32/text_renderer.cc b/src/renderer/win32/text_renderer.cc index 5f40e019ab..6188e80a68 100644 --- a/src/renderer/win32/text_renderer.cc +++ b/src/renderer/win32/text_renderer.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -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 { @@ -59,7 +61,6 @@ using ::WTL::CDC; using ::WTL::CDCHandle; using ::WTL::CFont; using ::WTL::CFontHandle; -using ::WTL::CLogFont; namespace { @@ -111,19 +112,17 @@ 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; } @@ -131,8 +130,6 @@ CLogFont GetLogFont(TextRenderer::FONT_TYPE type) { 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; } @@ -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; } @@ -169,8 +160,6 @@ CLogFont GetLogFont(TextRenderer::FONT_TYPE type) { } LOG(DFATAL) << "Unknown type: " << type; - CLogFont font; - font.SetMessageBoxFont(); return font; } @@ -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(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); @@ -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); @@ -265,7 +254,7 @@ class GdiTextRenderer : public TextRenderer { const absl::Span 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); @@ -465,7 +454,7 @@ class DirectWriteTextRenderer : public TextRenderer { return color; } - wil::com_ptr_nothrow CreateFormat(CLogFont logfont) { + wil::com_ptr_nothrow CreateFormat(const LOGFONT &logfont) { HRESULT hr = S_OK; wil::com_ptr_nothrow font; hr = dwrite_interop_->CreateFontFromLOGFONT(&logfont, font.put()); diff --git a/src/renderer/win32/win32_font_util.h b/src/renderer/win32/win32_font_util.h new file mode 100644 index 0000000000..c299506602 --- /dev/null +++ b/src/renderer/win32/win32_font_util.h @@ -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 + +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_ diff --git a/src/renderer/win32/win32_image_util.cc b/src/renderer/win32/win32_image_util.cc index 8d56edf3a5..8823051b54 100644 --- a/src/renderer/win32/win32_image_util.cc +++ b/src/renderer/win32/win32_image_util.cc @@ -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, @@ -94,6 +93,17 @@ 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, + .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, @@ -418,10 +428,10 @@ std::vector> 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) { diff --git a/src/win32/base/text_icon.cc b/src/win32/base/text_icon.cc index 2914fc3152..a2a24f19c8 100644 --- a/src/win32/base/text_icon.cc +++ b/src/win32/base/text_icon.cc @@ -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), @@ -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;