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

Side by Side Diff: ui/gfx/pango_util.cc

Issue 406493002: linux: Get font DPI from GTK. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 unified diff | Download patch | Annotate | Revision Log
« ui/gfx/linux_font_delegate.h ('K') | « ui/gfx/linux_font_delegate.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/pango_util.h" 5 #include "ui/gfx/pango_util.h"
6 6
7 #include <cairo/cairo.h> 7 #include <cairo/cairo.h>
8 #include <pango/pango.h> 8 #include <pango/pango.h>
9 #include <pango/pangocairo.h> 9 #include <pango/pangocairo.h>
10 #include <string> 10 #include <string>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <map> 13 #include <map>
14 14
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "ui/gfx/canvas.h" 17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/font_list.h" 18 #include "ui/gfx/font_list.h"
19 #include "ui/gfx/font_render_params.h" 19 #include "ui/gfx/font_render_params.h"
20 #include "ui/gfx/linux_font_delegate.h"
20 #include "ui/gfx/platform_font_pango.h" 21 #include "ui/gfx/platform_font_pango.h"
21 #include "ui/gfx/text_utils.h" 22 #include "ui/gfx/text_utils.h"
22 23
23 namespace gfx { 24 namespace gfx {
24 25
25 namespace { 26 namespace {
26 27
27 // Marker for accelerators in the text. 28 // Marker for accelerators in the text.
28 const gunichar kAcceleratorChar = '&'; 29 const gunichar kAcceleratorChar = '&';
29 30
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 else 78 else
78 NOTREACHED() << "Unhandled hinting style " << params.hinting; 79 NOTREACHED() << "Unhandled hinting style " << params.hinting;
79 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); 80 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style);
80 cairo_font_options_set_hint_metrics(cairo_font_options, 81 cairo_font_options_set_hint_metrics(cairo_font_options,
81 CAIRO_HINT_METRICS_ON); 82 CAIRO_HINT_METRICS_ON);
82 } 83 }
83 84
84 return cairo_font_options; 85 return cairo_font_options;
85 } 86 }
86 87
87 // Returns the resolution (DPI) used by pango. A negative value means the 88 // Returns the DPI that should be used by Pango.
88 // resolution hasn't been set. 89 double GetPangoDPI() {
89 double GetPangoResolution() { 90 static double dpi = 0.0;
90 static double resolution; 91 static bool determined_dpi = false;
msw 2014/07/18 03:39:33 nit: |dpi| can't be negative, init to -1 and s/det
Daniel Erat 2014/07/18 04:20:25 good call; done
91 static bool determined_resolution = false; 92 if (!determined_dpi) {
92 if (!determined_resolution) { 93 const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
93 determined_resolution = true; 94 if (delegate)
94 PangoContext* default_context = GetPangoContext(); 95 dpi = delegate->GetFontDPI();
95 resolution = pango_cairo_context_get_resolution(default_context); 96 if (dpi <= 0.0)
96 g_object_unref(default_context); 97 dpi = 96.0;
98 determined_dpi = true;
97 } 99 }
98 return resolution; 100 return dpi;
99 } 101 }
100 102
101 // Returns the number of pixels in a point. 103 // Returns the number of pixels in a point.
102 // - multiply a point size by this to get pixels ("device units") 104 // - multiply a point size by this to get pixels ("device units")
103 // - divide a pixel size by this to get points 105 // - divide a pixel size by this to get points
104 float GetPixelsInPoint() { 106 double GetPixelsInPoint() {
105 static float pixels_in_point = 1.0; 107 static double pixels_in_point = 1.0;
106 static bool determined_value = false; 108 static bool determined_value = false;
msw 2014/07/18 03:39:33 nit: ditto for |pixels_in_point| and |determined_v
Daniel Erat 2014/07/18 04:20:26 Done.
107
108 if (!determined_value) { 109 if (!determined_value) {
109 // http://goo.gl/UIh5m: "This is a scale factor between points specified in 110 pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch
110 // a PangoFontDescription and Cairo units. The default value is 96, meaning
111 // that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)."
112 double pango_dpi = GetPangoResolution();
113 if (pango_dpi <= 0)
114 pango_dpi = 96.0;
115 pixels_in_point = pango_dpi / 72.0; // 72 points in an inch
116 determined_value = true; 111 determined_value = true;
117 } 112 }
118
119 return pixels_in_point; 113 return pixels_in_point;
120 } 114 }
121 115
122 } // namespace 116 } // namespace
123 117
124 void SetUpPangoLayout( 118 void SetUpPangoLayout(
125 PangoLayout* layout, 119 PangoLayout* layout,
126 const base::string16& text, 120 const base::string16& text,
127 const FontList& font_list, 121 const FontList& font_list,
128 base::i18n::TextDirection text_direction, 122 base::i18n::TextDirection text_direction,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 160 }
167 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) { 161 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) {
168 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); 162 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
169 } else { 163 } else {
170 // Fading the text will be handled in the draw operation. 164 // Fading the text will be handled in the draw operation.
171 // Ensure that the text is only on one line. 165 // Ensure that the text is only on one line.
172 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); 166 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
173 pango_layout_set_width(layout, -1); 167 pango_layout_set_width(layout, -1);
174 } 168 }
175 169
176 // Set the resolution to match that used by Gtk. If we don't set the 170 // Set the layout's resolution to match the resolution used to convert from
177 // resolution and the resolution differs from the default, Gtk and Chrome end 171 // points to pixels.
178 // up drawing at different sizes. 172 pango_cairo_context_set_resolution(pango_layout_get_context(layout),
179 double resolution = GetPangoResolution(); 173 GetPangoDPI());
180 if (resolution > 0) {
181 pango_cairo_context_set_resolution(pango_layout_get_context(layout),
182 resolution);
183 }
184 174
185 // Set text and accelerator character if needed. 175 // Set text and accelerator character if needed.
186 if (flags & Canvas::SHOW_PREFIX) { 176 if (flags & Canvas::SHOW_PREFIX) {
187 // Escape the text string to be used as markup. 177 // Escape the text string to be used as markup.
188 std::string utf8 = base::UTF16ToUTF8(text); 178 std::string utf8 = base::UTF16ToUTF8(text);
189 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); 179 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size());
190 pango_layout_set_markup_with_accel(layout, 180 pango_layout_set_markup_with_accel(layout,
191 escaped_text, 181 escaped_text,
192 strlen(escaped_text), 182 strlen(escaped_text),
193 kAcceleratorChar, NULL); 183 kAcceleratorChar, NULL);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 235
246 if (i == desc_to_metrics->end()) { 236 if (i == desc_to_metrics->end()) {
247 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); 237 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL);
248 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); 238 desc_to_metrics->insert(std::make_pair(desc_hash, metrics));
249 return metrics; 239 return metrics;
250 } 240 }
251 return i->second; 241 return i->second;
252 } 242 }
253 243
254 } // namespace gfx 244 } // namespace gfx
OLDNEW
« ui/gfx/linux_font_delegate.h ('K') | « ui/gfx/linux_font_delegate.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698