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

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

Issue 659883002: Enable hidpi on Linux, refactor a bit on Windows to share Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: constants Created 6 years, 1 month 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
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>
(...skipping 10 matching lines...) Expand all
21 #include "ui/gfx/platform_font_pango.h" 21 #include "ui/gfx/platform_font_pango.h"
22 #include "ui/gfx/text_utils.h" 22 #include "ui/gfx/text_utils.h"
23 23
24 namespace gfx { 24 namespace gfx {
25 25
26 namespace { 26 namespace {
27 27
28 // Marker for accelerators in the text. 28 // Marker for accelerators in the text.
29 const gunichar kAcceleratorChar = '&'; 29 const gunichar kAcceleratorChar = '&';
30 30
31 const double kStandardDPI = 96.0;
32 const double kPointsPerInch = 72.0;
33
31 // Creates and returns a PangoContext. The caller owns the context. 34 // Creates and returns a PangoContext. The caller owns the context.
32 PangoContext* GetPangoContext() { 35 PangoContext* GetPangoContext() {
33 PangoFontMap* font_map = pango_cairo_font_map_get_default(); 36 PangoFontMap* font_map = pango_cairo_font_map_get_default();
34 return pango_font_map_create_context(font_map); 37 return pango_font_map_create_context(font_map);
35 } 38 }
36 39
37 // Creates a new cairo_font_options_t based on |params|. 40 // Creates a new cairo_font_options_t based on |params|.
38 cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) { 41 cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) {
39 cairo_font_options_t* cairo_font_options = cairo_font_options_create(); 42 cairo_font_options_t* cairo_font_options = cairo_font_options_create();
40 43
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 else 81 else
79 NOTREACHED() << "Unhandled hinting style " << params.hinting; 82 NOTREACHED() << "Unhandled hinting style " << params.hinting;
80 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); 83 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style);
81 cairo_font_options_set_hint_metrics(cairo_font_options, 84 cairo_font_options_set_hint_metrics(cairo_font_options,
82 CAIRO_HINT_METRICS_ON); 85 CAIRO_HINT_METRICS_ON);
83 } 86 }
84 87
85 return cairo_font_options; 88 return cairo_font_options;
86 } 89 }
87 90
88 // Returns the DPI that should be used by Pango.
89 double GetPangoDPI() {
90 static double dpi = -1.0;
91 if (dpi < 0.0) {
92 const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
93 if (delegate)
94 dpi = delegate->GetFontDPI();
95 if (dpi <= 0.0)
96 dpi = 96.0;
97 }
98 return dpi;
99 }
100
101 // Returns the number of pixels in a point.
102 // - multiply a point size by this to get pixels ("device units")
103 // - divide a pixel size by this to get points
104 double GetPixelsInPoint() {
105 static double pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch
106 return pixels_in_point;
107 }
108
109 } // namespace 91 } // namespace
110 92
111 void SetUpPangoLayout( 93 void SetUpPangoLayout(
112 PangoLayout* layout, 94 PangoLayout* layout,
113 const base::string16& text, 95 const base::string16& text,
114 const FontList& font_list, 96 const FontList& font_list,
115 base::i18n::TextDirection text_direction, 97 base::i18n::TextDirection text_direction,
116 int flags) { 98 int flags) {
117 cairo_font_options_t* cairo_font_options = CreateCairoFontOptions( 99 cairo_font_options_t* cairo_font_options = CreateCairoFontOptions(
118 font_list.GetPrimaryFont().GetFontRenderParams()); 100 font_list.GetPrimaryFont().GetFontRenderParams());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } else { 138 } else {
157 // Fading the text will be handled in the draw operation. 139 // Fading the text will be handled in the draw operation.
158 // Ensure that the text is only on one line. 140 // Ensure that the text is only on one line.
159 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); 141 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
160 pango_layout_set_width(layout, -1); 142 pango_layout_set_width(layout, -1);
161 } 143 }
162 144
163 // Set the layout's resolution to match the resolution used to convert from 145 // Set the layout's resolution to match the resolution used to convert from
164 // points to pixels. 146 // points to pixels.
165 pango_cairo_context_set_resolution(pango_layout_get_context(layout), 147 pango_cairo_context_set_resolution(pango_layout_get_context(layout),
166 GetPangoDPI()); 148 kStandardDPI);
167 149
168 // Set text and accelerator character if needed. 150 // Set text and accelerator character if needed.
169 if (flags & Canvas::SHOW_PREFIX) { 151 if (flags & Canvas::SHOW_PREFIX) {
170 // Escape the text string to be used as markup. 152 // Escape the text string to be used as markup.
171 std::string utf8 = base::UTF16ToUTF8(text); 153 std::string utf8 = base::UTF16ToUTF8(text);
172 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); 154 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size());
173 pango_layout_set_markup_with_accel(layout, 155 pango_layout_set_markup_with_accel(layout,
174 escaped_text, 156 escaped_text,
175 strlen(escaped_text), 157 strlen(escaped_text),
176 kAcceleratorChar, NULL); 158 kAcceleratorChar, NULL);
(...skipping 21 matching lines...) Expand all
198 font_list.GetFontDescriptionString().c_str())); 180 font_list.GetFontDescriptionString().c_str()));
199 pango_layout_set_font_description(layout, desc.get()); 181 pango_layout_set_font_description(layout, desc.get());
200 } 182 }
201 183
202 int GetPangoFontSizeInPixels(PangoFontDescription* pango_font) { 184 int GetPangoFontSizeInPixels(PangoFontDescription* pango_font) {
203 // If the size is absolute, then it's in Pango units rather than points. There 185 // If the size is absolute, then it's in Pango units rather than points. There
204 // are PANGO_SCALE Pango units in a device unit (pixel). 186 // are PANGO_SCALE Pango units in a device unit (pixel).
205 if (pango_font_description_get_size_is_absolute(pango_font)) 187 if (pango_font_description_get_size_is_absolute(pango_font))
206 return pango_font_description_get_size(pango_font) / PANGO_SCALE; 188 return pango_font_description_get_size(pango_font) / PANGO_SCALE;
207 189
190 const double scale = kStandardDPI / kPointsPerInch;
208 // Otherwise, we need to convert from points. 191 // Otherwise, we need to convert from points.
209 return static_cast<int>(GetPixelsInPoint() * 192 return static_cast<int>(
210 pango_font_description_get_size(pango_font) / PANGO_SCALE + 0.5); 193 scale * pango_font_description_get_size(pango_font) / PANGO_SCALE + 0.5);
211 } 194 }
212 195
213 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) { 196 PangoFontMetrics* GetPangoFontMetrics(PangoFontDescription* desc) {
214 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL; 197 static std::map<int, PangoFontMetrics*>* desc_to_metrics = NULL;
215 static PangoContext* context = NULL; 198 static PangoContext* context = NULL;
216 199
217 if (!context) { 200 if (!context) {
218 context = GetPangoContext(); 201 context = GetPangoContext();
219 pango_context_set_language(context, pango_language_get_default()); 202 pango_context_set_language(context, pango_language_get_default());
220 } 203 }
221 204
222 if (!desc_to_metrics) 205 if (!desc_to_metrics)
223 desc_to_metrics = new std::map<int, PangoFontMetrics*>(); 206 desc_to_metrics = new std::map<int, PangoFontMetrics*>();
224 207
225 const int desc_hash = pango_font_description_hash(desc); 208 const int desc_hash = pango_font_description_hash(desc);
226 std::map<int, PangoFontMetrics*>::iterator i = 209 std::map<int, PangoFontMetrics*>::iterator i =
227 desc_to_metrics->find(desc_hash); 210 desc_to_metrics->find(desc_hash);
228 211
229 if (i == desc_to_metrics->end()) { 212 if (i == desc_to_metrics->end()) {
230 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL); 213 PangoFontMetrics* metrics = pango_context_get_metrics(context, desc, NULL);
231 desc_to_metrics->insert(std::make_pair(desc_hash, metrics)); 214 desc_to_metrics->insert(std::make_pair(desc_hash, metrics));
232 return metrics; 215 return metrics;
233 } 216 }
234 return i->second; 217 return i->second;
235 } 218 }
236 219
237 } // namespace gfx 220 } // namespace gfx
OLDNEW
« ui/gfx/gfx.gyp ('K') | « ui/gfx/gfx.gyp ('k') | ui/gfx/screen_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698