Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Unified Diff: ui/gfx/platform_font_win.cc

Issue 696913002: Retrieve font metrics from skia if DirectWrite is enabled for font metrics calculations in the chro… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed build error Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/platform_font_win.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/platform_font_win.cc
diff --git a/ui/gfx/platform_font_win.cc b/ui/gfx/platform_font_win.cc
index 6641935d2f9a93a4c9e6018f28a567cf1fd704cf..5bac0f9339c9f7deaf3487ac5ca8fb40052d740b 100644
--- a/ui/gfx/platform_font_win.cc
+++ b/ui/gfx/platform_font_win.cc
@@ -20,6 +20,7 @@
#include "base/win/scoped_hdc.h"
#include "base/win/scoped_select_object.h"
#include "base/win/win_util.h"
+#include "third_party/skia/include/core/SkTypeface.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_render_params.h"
@@ -82,7 +83,7 @@ PlatformFontWin::AdjustFontCallback
PlatformFontWin::GetMinimumFontSizeCallback
PlatformFontWin::get_minimum_font_size_callback = NULL;
-IDWriteFactory* PlatformFontWin::direct_write_factory_ = NULL;
+bool PlatformFontWin::use_skia_for_font_metrics_ = false;
////////////////////////////////////////////////////////////////////////////////
// PlatformFontWin, public
@@ -250,10 +251,8 @@ PlatformFontWin::HFontRef* PlatformFontWin::GetBaseFontRef() {
PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRef(HFONT font) {
TEXTMETRIC font_metrics;
- if (direct_write_factory_) {
- base::win::ScopedGDIObject<HFONT> gdi_font(font);
- font = ConvertGDIFontToDirectWriteFont(gdi_font);
- }
+ if (use_skia_for_font_metrics_)
+ return CreateHFontRefFromSkia(font);
{
base::win::ScopedGetDC screen_dc(NULL);
@@ -318,40 +317,52 @@ Font PlatformFontWin::DeriveWithCorrectedSize(HFONT base_font) {
}
// static
-HFONT PlatformFontWin::ConvertGDIFontToDirectWriteFont(HFONT gdi_font) {
- // This function uses the DirectWrite Gdi interop interfaces to convert the
- // gdi font passed in to a HFONT which is compatible with DirectWrite font
- // metrics which could be different from GDI font metrics. This ensures that
- // widgets like labels which use font metrics to calculate bounds have the
- // same calculations as skia which uses DirectWrite.
- DCHECK(direct_write_factory_);
- base::win::ScopedComPtr<IDWriteGdiInterop> gdi_interop;
- HRESULT hr = direct_write_factory_->GetGdiInterop(gdi_interop.Receive());
- if (FAILED(hr)) {
- CHECK(false);
- return NULL;
- }
-
- base::win::ScopedGetDC screen_dc(NULL);
- gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT);
-
- base::win::ScopedSelectObject scoped_font(screen_dc, gdi_font);
-
- base::win::ScopedComPtr<IDWriteFontFace> font_face_gdi;
- hr = gdi_interop->CreateFontFaceFromHdc(screen_dc, font_face_gdi.Receive());
- if (FAILED(hr)) {
- CHECK(false);
- return NULL;
- }
+PlatformFontWin::HFontRef* PlatformFontWin::CreateHFontRefFromSkia(
+ HFONT gdi_font) {
+ LOGFONT font_info = {0};
+ GetObject(gdi_font, sizeof(LOGFONT), &font_info);
+
+ int skia_style = SkTypeface::kNormal;
+ if (font_info.lfWeight >= FW_SEMIBOLD &&
+ font_info.lfWeight <= FW_ULTRABOLD) {
+ skia_style |= SkTypeface::kBold;
+ }
+ if (font_info.lfItalic)
+ skia_style |= SkTypeface::kItalic;
+
+ skia::RefPtr<SkTypeface> skia_face = skia::AdoptRef(
+ SkTypeface::CreateFromName(
+ base::SysWideToUTF8(font_info.lfFaceName).c_str(),
+ static_cast<SkTypeface::Style>(skia_style)));
+ BOOL antialiasing = TRUE;
+ SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &antialiasing, 0);
+
+ SkPaint paint;
+ paint.setAntiAlias(!!antialiasing);
+ paint.setTypeface(skia_face.get());
+ SkPaint::FontMetrics skia_metrics;
+ paint.getFontMetrics(&skia_metrics);
+
+ // The calculations below are similar to those in the CreateHFontRef
+ // function.
+ const int height =
+ std::max<int>(1, std::ceil(fabs(skia_metrics.fAscent)) +
+ std::ceil(skia_metrics.fDescent));
+ const int baseline = std::max<int>(1, std::ceil(fabs(skia_metrics.fAscent)));
+ const int cap_height = std::max<int>(1,
+ std::ceil(fabs(skia_metrics.fAscent)) - skia_metrics.fLeading);
+ const int ave_char_width = std::max<int>(1, skia_metrics.fAvgCharWidth);
+ const int font_size = std::max<int>(1, height - skia_metrics.fLeading);
- LOGFONT dwrite_to_gdi_log_font = {0};
- hr = gdi_interop->ConvertFontFaceToLOGFONT(font_face_gdi,
- &dwrite_to_gdi_log_font);
- if (FAILED(hr)) {
- CHECK(false);
- return NULL;
- }
- return CreateFontIndirect(&dwrite_to_gdi_log_font);
+ int style = 0;
+ if (skia_style & SkTypeface::kItalic)
+ style |= Font::ITALIC;
+ if (font_info.lfUnderline)
+ style |= Font::UNDERLINE;
+ if (font_info.lfWeight >= kTextMetricWeightBold)
+ style |= Font::BOLD;
+ return new HFontRef(gdi_font, font_size, height, baseline, cap_height,
+ ave_char_width, style);
}
PlatformFontWin::PlatformFontWin(HFontRef* hfont_ref) : font_ref_(hfont_ref) {
« no previous file with comments | « ui/gfx/platform_font_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698