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

Unified Diff: chrome/browser/ui/libgtk2ui/gtk2_ui.cc

Issue 2441043002: [linux] Allow font to use below 1.0f scale (Closed)
Patch Set: Allow font to use below 1.0f scale Created 4 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
Index: chrome/browser/ui/libgtk2ui/gtk2_ui.cc
diff --git a/chrome/browser/ui/libgtk2ui/gtk2_ui.cc b/chrome/browser/ui/libgtk2ui/gtk2_ui.cc
index 23e63371bcb732a49b6032a57e99c7d32c50ce36..ae9675f6dc039669e1d4633dbb464c657d10c563 100644
--- a/chrome/browser/ui/libgtk2ui/gtk2_ui.cc
+++ b/chrome/browser/ui/libgtk2ui/gtk2_ui.cc
@@ -7,6 +7,8 @@
#include <math.h>
#include <pango/pango.h>
#include <X11/Xcursor/Xcursor.h>
+
+#include <cmath>
#include <set>
#include <utility>
@@ -34,6 +36,7 @@
#include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
#include "chrome/browser/ui/libgtk2ui/unity_service.h"
#include "chrome/browser/ui/libgtk2ui/x11_input_method_context_impl_gtk2.h"
+#include "chrome/common/chrome_switches.h"
#include "chrome/grit/theme_resources.h"
#include "components/grit/components_scaled_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
@@ -84,6 +87,8 @@ namespace libgtk2ui {
namespace {
+const double kDefaultDPI = 96;
+
class GtkButtonImageSource : public gfx::ImageSkiaSource {
public:
GtkButtonImageSource(const char* idr_string, gfx::Size size)
@@ -353,43 +358,49 @@ gfx::FontRenderParams GetGtkFontRenderParams() {
}
double GetDPI() {
Evan Stade 2016/10/25 17:27:02 nit: GetDpi
- // Linux chrome currently does not support dynamic DPI changes.
- // Keep using the first value detected.
- static double dpi = -1.f;
- if (dpi < 0) {
- const double kDefaultDPI = 96;
-
- if (display::Display::HasForceDeviceScaleFactor()) {
- dpi = display::Display::GetForcedDeviceScaleFactor() * kDefaultDPI;
- return dpi;
- }
+ if (display::Display::HasForceDeviceScaleFactor())
+ return display::Display::GetForcedDeviceScaleFactor() * kDefaultDPI;
+
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableXDpiForDsf)) {
+ XDisplay* xdisplay = gfx::GetXDisplay();
+ int xscreen = DefaultScreen(xdisplay);
+ return (DisplayHeight(xdisplay, xscreen) * 25.4) /
+ DisplayHeightMM(xdisplay, xscreen);
+ }
- GtkSettings* gtk_settings = gtk_settings_get_default();
- CHECK(gtk_settings);
- gint gtk_dpi = -1;
- g_object_get(gtk_settings, "gtk-xft-dpi", &gtk_dpi, NULL);
+ GtkSettings* gtk_settings = gtk_settings_get_default();
+ CHECK(gtk_settings);
+ gint gtk_dpi = -1;
+ g_object_get(gtk_settings, "gtk-xft-dpi", &gtk_dpi, NULL);
- // GTK multiplies the DPI by 1024 before storing it.
- dpi = (gtk_dpi > 0) ? gtk_dpi / 1024.0 : kDefaultDPI;
+ // GTK multiplies the DPI by 1024 before storing it.
+ return (gtk_dpi > 0) ? gtk_dpi / 1024.0 : kDefaultDPI;
+}
- // DSF is always >=1.0 on win/cros and lower DSF has never been considered
- // nor tested.
- dpi = std::max(kDefaultDPI, dpi);
- }
- return dpi;
+float GetRawDeviceScaleFactor() {
+ if (display::Display::HasForceDeviceScaleFactor())
+ return display::Display::GetForcedDeviceScaleFactor();
+ return GetDPI() / kDefaultDPI;
}
-// Queries GTK for its font DPI setting and returns the number of pixels in a
-// point.
-double GetPixelsInPoint(float device_scale_factor) {
- double dpi = GetDPI();
+// Returns the font size for the *raw* device scale factor in points.
+// The |device_scale_factor| is used to cancel the scale to be applied by UI
+// and to compensate the scale when the device_scale_factor is floored.
+double GetFontSizePixelsInPoint(float device_scale_factor) {
+ // There are 72 points in an inch.
+ double point = GetDPI() / 72.0;
// Take device_scale_factor into account — if Chrome already scales the
// entire UI up by 2x, we should not also scale up.
- dpi /= device_scale_factor;
-
- // There are 72 points in an inch.
- return dpi / 72.0;
+ point /= device_scale_factor;
+
+ // Allow the scale lower than 1.0 only for fonts. Don't always use
+ // the raw value however, beacuse the 1.0~1.3 is rounded to 1.0.
+ float raw_scale = GetRawDeviceScaleFactor();
+ if (raw_scale < 1.0f)
+ return point * raw_scale / device_scale_factor;
+ return point;
}
views::LinuxUI::NonClientMiddleClickAction GetDefaultMiddleClickAction() {
@@ -413,8 +424,7 @@ Gtk2UI::Gtk2UI()
: default_font_size_pixels_(0),
default_font_style_(gfx::Font::NORMAL),
default_font_weight_(gfx::Font::Weight::NORMAL),
- middle_click_action_(GetDefaultMiddleClickAction()),
- device_scale_factor_(1.0) {
+ middle_click_action_(GetDefaultMiddleClickAction()) {
GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess());
}
@@ -813,7 +823,7 @@ void Gtk2UI::LoadGtkValues() {
colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
color_utils::BlendTowardOppositeLuma(label_color, 50);
- UpdateDefaultFont();
+ UpdateDeviceScaleFactor();
// Build the various icon tints.
GetNormalButtonTintHSL(&button_tint_);
@@ -1046,8 +1056,8 @@ void Gtk2UI::UpdateDefaultFont() {
// Round the value when converting to pixels to match GTK's logic.
const double size_points = pango_font_description_get_size(desc) /
static_cast<double>(PANGO_SCALE);
- default_font_size_pixels_ = static_cast<int>(
- GetPixelsInPoint(device_scale_factor_) * size_points + 0.5);
+ default_font_size_pixels_ = std::round(
+ GetFontSizePixelsInPoint(GetDeviceScaleFactor()) * size_points);
query.point_size = static_cast<int>(size_points);
}
@@ -1068,20 +1078,20 @@ void Gtk2UI::ResetStyle() {
NativeThemeGtk2::instance()->NotifyObservers();
}
-void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor) {
- device_scale_factor_ = device_scale_factor;
+void Gtk2UI::UpdateDeviceScaleFactor() {
+ // Note: Linux chrome currently does not support dynamic DPI
+ // changes. This is to allow flags to override the DPI settings
+ // during startup.
+ float scale = GetRawDeviceScaleFactor();
+
+ // Blacklist scaling factors <130% (crbug.com/484400) and round
+ // to 1 decimal to prevent rendering problems (crbug.com/485183).
+ device_scale_factor_ = scale < 1.3f ? 1.0f : roundf(scale * 10) / 10;
UpdateDefaultFont();
}
float Gtk2UI::GetDeviceScaleFactor() const {
- if (display::Display::HasForceDeviceScaleFactor())
- return display::Display::GetForcedDeviceScaleFactor();
- const int kCSSDefaultDPI = 96;
- const float scale = GetDPI() / kCSSDefaultDPI;
-
- // Blacklist scaling factors <130% (crbug.com/484400) and round
- // to 1 decimal to prevent rendering problems (crbug.com/485183).
- return scale < 1.3f ? 1.0f : roundf(scale * 10) / 10;
+ return device_scale_factor_;
}
} // namespace libgtk2ui
« no previous file with comments | « chrome/browser/ui/libgtk2ui/gtk2_ui.h ('k') | chrome/browser/ui/views/chrome_browser_main_extra_parts_views_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698