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

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: inline initialization of a static variable 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
« no previous file with comments | « 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 = -1.0;
90 static double resolution; 91 if (dpi < 0.0) {
91 static bool determined_resolution = false; 92 const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
92 if (!determined_resolution) { 93 if (delegate)
93 determined_resolution = true; 94 dpi = delegate->GetFontDPI();
94 PangoContext* default_context = GetPangoContext(); 95 if (dpi <= 0.0)
95 resolution = pango_cairo_context_get_resolution(default_context); 96 dpi = 96.0;
96 g_object_unref(default_context);
97 } 97 }
98 return resolution; 98 return dpi;
99 } 99 }
100 100
101 // Returns the number of pixels in a point. 101 // Returns the number of pixels in a point.
102 // - multiply a point size by this to get pixels ("device units") 102 // - multiply a point size by this to get pixels ("device units")
103 // - divide a pixel size by this to get points 103 // - divide a pixel size by this to get points
104 float GetPixelsInPoint() { 104 double GetPixelsInPoint() {
105 static float pixels_in_point = 1.0; 105 static double pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch
106 static bool determined_value = false;
107
108 if (!determined_value) {
109 // http://goo.gl/UIh5m: "This is a scale factor between points specified in
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;
117 }
118
119 return pixels_in_point; 106 return pixels_in_point;
120 } 107 }
121 108
122 } // namespace 109 } // namespace
123 110
124 void SetUpPangoLayout( 111 void SetUpPangoLayout(
125 PangoLayout* layout, 112 PangoLayout* layout,
126 const base::string16& text, 113 const base::string16& text,
127 const FontList& font_list, 114 const FontList& font_list,
128 base::i18n::TextDirection text_direction, 115 base::i18n::TextDirection text_direction,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 153 }
167 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) { 154 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) {
168 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); 155 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
169 } else { 156 } else {
170 // Fading the text will be handled in the draw operation. 157 // Fading the text will be handled in the draw operation.
171 // Ensure that the text is only on one line. 158 // Ensure that the text is only on one line.
172 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); 159 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
173 pango_layout_set_width(layout, -1); 160 pango_layout_set_width(layout, -1);
174 } 161 }
175 162
176 // Set the resolution to match that used by Gtk. If we don't set the 163 // 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 164 // points to pixels.
178 // up drawing at different sizes. 165 pango_cairo_context_set_resolution(pango_layout_get_context(layout),
179 double resolution = GetPangoResolution(); 166 GetPangoDPI());
180 if (resolution > 0) {
181 pango_cairo_context_set_resolution(pango_layout_get_context(layout),
182 resolution);
183 }
184 167
185 // Set text and accelerator character if needed. 168 // Set text and accelerator character if needed.
186 if (flags & Canvas::SHOW_PREFIX) { 169 if (flags & Canvas::SHOW_PREFIX) {
187 // Escape the text string to be used as markup. 170 // Escape the text string to be used as markup.
188 std::string utf8 = base::UTF16ToUTF8(text); 171 std::string utf8 = base::UTF16ToUTF8(text);
189 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); 172 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size());
190 pango_layout_set_markup_with_accel(layout, 173 pango_layout_set_markup_with_accel(layout,
191 escaped_text, 174 escaped_text,
192 strlen(escaped_text), 175 strlen(escaped_text),
193 kAcceleratorChar, NULL); 176 kAcceleratorChar, NULL);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 228
246 if (i == desc_to_metrics->end()) { 229 if (i == desc_to_metrics->end()) {
247 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); 230 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL);
248 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); 231 desc_to_metrics->insert(std::make_pair(desc_hash, metrics));
249 return metrics; 232 return metrics;
250 } 233 }
251 return i->second; 234 return i->second;
252 } 235 }
253 236
254 } // namespace gfx 237 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/linux_font_delegate.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698