Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/gfx/canvas.h" | 5 #include "app/gfx/canvas.h" |
| 6 | 6 |
| 7 #include <cairo/cairo.h> | 7 #include <cairo/cairo.h> |
| 8 #include <gtk/gtk.h> | |
| 8 #include <pango/pango.h> | 9 #include <pango/pango.h> |
| 9 #include <pango/pangocairo.h> | 10 #include <pango/pangocairo.h> |
| 10 | 11 |
| 11 #include "app/gfx/font.h" | 12 #include "app/gfx/font.h" |
| 12 #include "base/gfx/rect.h" | 13 #include "base/gfx/rect.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 19 // Font settings that we initialize once and then use when drawing text in | |
| 20 // DrawStringInt(). | |
| 21 static cairo_font_options_t* cairo_font_options = NULL; | |
|
Daniel Erat
2009/08/05 22:36:03
Is there a better place to be doing this? I could
agl
2009/08/05 22:45:57
I think it's ok where it is. You shouldn't trigger
| |
| 22 | |
| 18 // Returns a new pango font, free with pango_font_description_free(). | 23 // Returns a new pango font, free with pango_font_description_free(). |
| 19 PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { | 24 PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { |
| 20 gfx::Font font = gfx_font; // Copy so we can call non-const methods. | 25 gfx::Font font = gfx_font; // Copy so we can call non-const methods. |
| 21 PangoFontDescription* pfd = pango_font_description_new(); | 26 PangoFontDescription* pfd = pango_font_description_new(); |
| 22 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); | 27 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); |
| 23 pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); | 28 pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); |
| 24 | 29 |
| 25 switch (font.style()) { | 30 switch (font.style()) { |
| 26 case gfx::Font::NORMAL: | 31 case gfx::Font::NORMAL: |
| 27 // Nothing to do, should already be PANGO_STYLE_NORMAL. | 32 // Nothing to do, should already be PANGO_STYLE_NORMAL. |
| 28 break; | 33 break; |
| 29 case gfx::Font::BOLD: | 34 case gfx::Font::BOLD: |
| 30 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); | 35 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); |
| 31 break; | 36 break; |
| 32 case gfx::Font::ITALIC: | 37 case gfx::Font::ITALIC: |
| 33 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); | 38 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); |
| 34 break; | 39 break; |
| 35 case gfx::Font::UNDERLINED: | 40 case gfx::Font::UNDERLINED: |
| 36 // TODO(deanm): How to do underlined? Where do we use it? Probably have | 41 // TODO(deanm): How to do underlined? Where do we use it? Probably have |
| 37 // to paint it ourselves, see pango_font_metrics_get_underline_position. | 42 // to paint it ourselves, see pango_font_metrics_get_underline_position. |
| 38 break; | 43 break; |
| 39 } | 44 } |
| 40 | 45 |
| 41 return pfd; | 46 return pfd; |
| 42 } | 47 } |
| 43 | 48 |
| 49 // Update |cairo_font_options| based on GtkSettings, allocating it if needed. | |
| 50 static void UpdateCairoFontOptions() { | |
| 51 if (!cairo_font_options) | |
| 52 cairo_font_options = cairo_font_options_create(); | |
| 53 | |
| 54 GtkSettings* gtk_settings = gtk_settings_get_default(); | |
| 55 gint antialias = 0; | |
| 56 gint hinting = 0; | |
| 57 gchar* hint_style = NULL; | |
| 58 gchar* rgba_style = NULL; | |
| 59 g_object_get(gtk_settings, | |
| 60 "gtk-xft-antialias", &antialias, | |
| 61 "gtk-xft-hinting", &hinting, | |
| 62 "gtk-xft-hintstyle", &hint_style, | |
| 63 "gtk-xft-rgba", &rgba_style, | |
| 64 NULL); | |
| 65 | |
| 66 // g_object_get() doesn't tell us whether the properties were present or not, | |
| 67 // but if they aren't (because gnome-settings-daemon isn't running), we'll get | |
| 68 // NULL values for the strings. | |
| 69 if (hint_style && rgba_style) { | |
| 70 if (!antialias) { | |
| 71 cairo_font_options_set_antialias(cairo_font_options, | |
| 72 CAIRO_ANTIALIAS_NONE); | |
| 73 } else if (strcmp(rgba_style, "none") == 0) { | |
| 74 cairo_font_options_set_antialias(cairo_font_options, | |
| 75 CAIRO_ANTIALIAS_GRAY); | |
| 76 } else { | |
| 77 cairo_font_options_set_antialias(cairo_font_options, | |
| 78 CAIRO_ANTIALIAS_SUBPIXEL); | |
| 79 cairo_subpixel_order_t cairo_subpixel_order = | |
| 80 CAIRO_SUBPIXEL_ORDER_DEFAULT; | |
| 81 if (strcmp(rgba_style, "rgb") == 0) { | |
| 82 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; | |
| 83 } else if (strcmp(rgba_style, "bgr") == 0) { | |
| 84 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; | |
| 85 } else if (strcmp(rgba_style, "vrgb") == 0) { | |
| 86 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; | |
| 87 } else if (strcmp(rgba_style, "vbgr") == 0) { | |
| 88 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; | |
| 89 } | |
| 90 cairo_font_options_set_subpixel_order(cairo_font_options, | |
| 91 cairo_subpixel_order); | |
| 92 } | |
| 93 | |
| 94 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; | |
| 95 if (hinting == 0 || strcmp(hint_style, "hintnone") == 0) { | |
| 96 cairo_hint_style = CAIRO_HINT_STYLE_NONE; | |
| 97 } else if (strcmp(hint_style, "hintslight") == 0) { | |
| 98 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; | |
| 99 } else if (strcmp(hint_style, "hintmedium") == 0) { | |
| 100 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; | |
| 101 } else if (strcmp(hint_style, "hintfull") == 0) { | |
| 102 cairo_hint_style = CAIRO_HINT_STYLE_FULL; | |
| 103 } | |
| 104 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); | |
| 105 } | |
| 106 | |
| 107 if (hint_style) | |
| 108 g_free(hint_style); | |
| 109 if (rgba_style) | |
| 110 g_free(rgba_style); | |
| 111 } | |
| 112 | |
| 44 } // namespace | 113 } // namespace |
| 45 | 114 |
| 46 namespace gfx { | 115 namespace gfx { |
| 47 | 116 |
| 48 Canvas::Canvas(int width, int height, bool is_opaque) | 117 Canvas::Canvas(int width, int height, bool is_opaque) |
| 49 : skia::PlatformCanvas(width, height, is_opaque) { | 118 : skia::PlatformCanvas(width, height, is_opaque) { |
| 50 } | 119 } |
| 51 | 120 |
| 52 Canvas::Canvas() : skia::PlatformCanvas() { | 121 Canvas::Canvas() : skia::PlatformCanvas() { |
| 53 } | 122 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 cairo_surface_destroy(surface); | 164 cairo_surface_destroy(surface); |
| 96 } | 165 } |
| 97 | 166 |
| 98 void Canvas::DrawStringInt(const std::wstring& text, | 167 void Canvas::DrawStringInt(const std::wstring& text, |
| 99 const gfx::Font& font, | 168 const gfx::Font& font, |
| 100 const SkColor& color, int x, int y, int w, int h, | 169 const SkColor& color, int x, int y, int w, int h, |
| 101 int flags) { | 170 int flags) { |
| 102 cairo_t* cr = beginPlatformPaint(); | 171 cairo_t* cr = beginPlatformPaint(); |
| 103 PangoLayout* layout = pango_cairo_create_layout(cr); | 172 PangoLayout* layout = pango_cairo_create_layout(cr); |
| 104 | 173 |
| 174 if (!cairo_font_options) | |
| 175 UpdateCairoFontOptions(); | |
| 176 // This needs to be done early on; it has no effect when called just before | |
| 177 // pango_cairo_show_layout(). | |
| 178 pango_cairo_context_set_font_options( | |
| 179 pango_layout_get_context(layout), cairo_font_options); | |
| 180 | |
| 105 // Callers of DrawStringInt handle RTL layout themselves, so tell pango to not | 181 // Callers of DrawStringInt handle RTL layout themselves, so tell pango to not |
| 106 // scope out RTL characters. | 182 // scope out RTL characters. |
| 107 pango_layout_set_auto_dir(layout, FALSE); | 183 pango_layout_set_auto_dir(layout, FALSE); |
| 108 | 184 |
| 109 cairo_set_source_rgb(cr, | 185 cairo_set_source_rgb(cr, |
| 110 SkColorGetR(color) / 255.0, | 186 SkColorGetR(color) / 255.0, |
| 111 SkColorGetG(color) / 255.0, | 187 SkColorGetG(color) / 255.0, |
| 112 SkColorGetB(color) / 255.0); | 188 SkColorGetB(color) / 255.0); |
| 113 | 189 |
| 114 // TODO(deanm): Implement the rest of the Canvas flags. | 190 // TODO(deanm): Implement the rest of the Canvas flags. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 } | 228 } |
| 153 | 229 |
| 154 cairo_move_to(cr, x, y); | 230 cairo_move_to(cr, x, y); |
| 155 pango_cairo_show_layout(cr, layout); | 231 pango_cairo_show_layout(cr, layout); |
| 156 | 232 |
| 157 g_object_unref(layout); | 233 g_object_unref(layout); |
| 158 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. | 234 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. |
| 159 } | 235 } |
| 160 | 236 |
| 161 } // namespace gfx | 237 } // namespace gfx |
| OLD | NEW |