| 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/chrome_canvas.h" | 5 #include "app/gfx/canvas.h" |
| 6 | 6 |
| 7 #include <pango/pango.h> | 7 #include <pango/pango.h> |
| 8 | 8 |
| 9 #include "app/gfx/chrome_font.h" | 9 #include "app/gfx/font.h" |
| 10 #include "base/gfx/rect.h" | 10 #include "base/gfx/rect.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Returns a new pango font, free with pango_font_description_free(). | 16 // Returns a new pango font, free with pango_font_description_free(). |
| 17 PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { | 17 PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { |
| 18 gfx::Font font = gfx_font; // Copy so we can call non-const methods. | 18 gfx::Font font = gfx_font; // Copy so we can call non-const methods. |
| 19 PangoFontDescription* pfd = pango_font_description_new(); | 19 PangoFontDescription* pfd = pango_font_description_new(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 // TODO(deanm): How to do underlined? Where do we use it? Probably have | 34 // TODO(deanm): How to do underlined? Where do we use it? Probably have |
| 35 // to paint it ourselves, see pango_font_metrics_get_underline_position. | 35 // to paint it ourselves, see pango_font_metrics_get_underline_position. |
| 36 break; | 36 break; |
| 37 } | 37 } |
| 38 | 38 |
| 39 return pfd; | 39 return pfd; |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) | 44 namespace gfx { |
| 45 |
| 46 Canvas::Canvas(int width, int height, bool is_opaque) |
| 45 : skia::PlatformCanvasLinux(width, height, is_opaque) { | 47 : skia::PlatformCanvasLinux(width, height, is_opaque) { |
| 46 } | 48 } |
| 47 | 49 |
| 48 ChromeCanvas::ChromeCanvas() : skia::PlatformCanvasLinux() { | 50 Canvas::Canvas() : skia::PlatformCanvasLinux() { |
| 49 } | 51 } |
| 50 | 52 |
| 51 ChromeCanvas::~ChromeCanvas() { | 53 Canvas::~Canvas() { |
| 52 } | 54 } |
| 53 | 55 |
| 54 // static | 56 // static |
| 55 void ChromeCanvas::SizeStringInt(const std::wstring& text, | 57 void Canvas::SizeStringInt(const std::wstring& text, |
| 56 const gfx::Font& font, | 58 const gfx::Font& font, |
| 57 int* width, int* height, int flags) { | 59 int* width, int* height, int flags) { |
| 58 NOTIMPLEMENTED(); | 60 NOTIMPLEMENTED(); |
| 59 } | 61 } |
| 60 | 62 |
| 61 void ChromeCanvas::ApplySkiaMatrixToCairoContext(cairo_t* cr) { | 63 void Canvas::ApplySkiaMatrixToCairoContext(cairo_t* cr) { |
| 62 const SkMatrix& skia_matrix = getTotalMatrix(); | 64 const SkMatrix& skia_matrix = getTotalMatrix(); |
| 63 cairo_matrix_t cairo_matrix; | 65 cairo_matrix_t cairo_matrix; |
| 64 cairo_matrix_init(&cairo_matrix, | 66 cairo_matrix_init(&cairo_matrix, |
| 65 SkScalarToFloat(skia_matrix.getScaleX()), | 67 SkScalarToFloat(skia_matrix.getScaleX()), |
| 66 SkScalarToFloat(skia_matrix.getSkewY()), | 68 SkScalarToFloat(skia_matrix.getSkewY()), |
| 67 SkScalarToFloat(skia_matrix.getSkewX()), | 69 SkScalarToFloat(skia_matrix.getSkewX()), |
| 68 SkScalarToFloat(skia_matrix.getScaleY()), | 70 SkScalarToFloat(skia_matrix.getScaleY()), |
| 69 SkScalarToFloat(skia_matrix.getTranslateX()), | 71 SkScalarToFloat(skia_matrix.getTranslateX()), |
| 70 SkScalarToFloat(skia_matrix.getTranslateY())); | 72 SkScalarToFloat(skia_matrix.getTranslateY())); |
| 71 cairo_set_matrix(cr, &cairo_matrix); | 73 cairo_set_matrix(cr, &cairo_matrix); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void ChromeCanvas::DrawStringInt(const std::wstring& text, | 76 void Canvas::DrawStringInt(const std::wstring& text, |
| 75 const gfx::Font& font, | 77 const gfx::Font& font, |
| 76 const SkColor& color, int x, int y, int w, | 78 const SkColor& color, int x, int y, int w, int h, |
| 77 int h, int flags) { | 79 int flags) { |
| 78 cairo_surface_t* surface = beginPlatformPaint(); | 80 cairo_surface_t* surface = beginPlatformPaint(); |
| 79 cairo_t* cr = cairo_create(surface); | 81 cairo_t* cr = cairo_create(surface); |
| 80 // We're going to draw onto the surface directly. This circumvents the matrix | 82 // We're going to draw onto the surface directly. This circumvents the matrix |
| 81 // installed by Skia. Apply the matrix from skia to cairo so they align and | 83 // installed by Skia. Apply the matrix from skia to cairo so they align and |
| 82 // we draw at the right place. | 84 // we draw at the right place. |
| 83 ApplySkiaMatrixToCairoContext(cr); | 85 ApplySkiaMatrixToCairoContext(cr); |
| 84 PangoLayout* layout = pango_cairo_create_layout(cr); | 86 PangoLayout* layout = pango_cairo_create_layout(cr); |
| 85 | 87 |
| 86 cairo_set_source_rgb(cr, | 88 cairo_set_source_rgb(cr, |
| 87 SkColorGetR(color) / 255.0, | 89 SkColorGetR(color) / 255.0, |
| 88 SkColorGetG(color) / 255.0, | 90 SkColorGetG(color) / 255.0, |
| 89 SkColorGetB(color) / 255.0); | 91 SkColorGetB(color) / 255.0); |
| 90 | 92 |
| 91 // TODO(deanm): Implement the rest of the ChromeCanvas flags. | 93 // TODO(deanm): Implement the rest of the Canvas flags. |
| 92 if (!(flags & ChromeCanvas::NO_ELLIPSIS)) | 94 if (!(flags & Canvas::NO_ELLIPSIS)) |
| 93 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | 95 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); |
| 94 | 96 |
| 95 pango_layout_set_width(layout, w * PANGO_SCALE); | 97 pango_layout_set_width(layout, w * PANGO_SCALE); |
| 96 pango_layout_set_height(layout, h * PANGO_SCALE); | 98 pango_layout_set_height(layout, h * PANGO_SCALE); |
| 97 | 99 |
| 98 if (flags & TEXT_ALIGN_CENTER) { | 100 if (flags & TEXT_ALIGN_CENTER) { |
| 99 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | 101 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); |
| 100 } else if (flags & TEXT_ALIGN_RIGHT) { | 102 } else if (flags & TEXT_ALIGN_RIGHT) { |
| 101 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | 103 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); |
| 102 } | 104 } |
| 103 | 105 |
| 104 if (flags & MULTI_LINE) { | 106 if (flags & MULTI_LINE) { |
| 105 pango_layout_set_wrap(layout, | 107 pango_layout_set_wrap(layout, |
| 106 (flags & CHARACTER_BREAK) ? PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); | 108 (flags & CHARACTER_BREAK) ? PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); |
| 107 } | 109 } |
| 108 | 110 |
| 109 if (flags & NO_ELLIPSIS) | 111 if (flags & NO_ELLIPSIS) |
| 110 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | 112 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
| 111 | 113 |
| 112 std::string utf8 = WideToUTF8(text); | 114 std::string utf8 = WideToUTF8(text); |
| 113 pango_layout_set_text(layout, utf8.data(), utf8.size()); | 115 pango_layout_set_text(layout, utf8.data(), utf8.size()); |
| 114 | 116 |
| 115 PangoFontDescription* desc = PangoFontFromGfxFont(font); | 117 PangoFontDescription* desc = PangoFontFromGfxFont(font); |
| 116 pango_layout_set_font_description(layout, desc); | 118 pango_layout_set_font_description(layout, desc); |
| 117 pango_font_description_free(desc); | 119 pango_font_description_free(desc); |
| 118 | 120 |
| 119 int width, height; | 121 int width, height; |
| 120 pango_layout_get_size(layout, &width, &height); | 122 pango_layout_get_size(layout, &width, &height); |
| 121 | 123 |
| 122 if (flags & ChromeCanvas::TEXT_VALIGN_TOP) { | 124 if (flags & Canvas::TEXT_VALIGN_TOP) { |
| 123 // Cairo should draw from the top left corner already. | 125 // Cairo should draw from the top left corner already. |
| 124 } else if (flags & ChromeCanvas::TEXT_VALIGN_BOTTOM) { | 126 } else if (flags & Canvas::TEXT_VALIGN_BOTTOM) { |
| 125 y = y + (h - (height / PANGO_SCALE)); | 127 y = y + (h - (height / PANGO_SCALE)); |
| 126 } else { | 128 } else { |
| 127 // Vertically centered. | 129 // Vertically centered. |
| 128 y = y + ((h - (height / PANGO_SCALE)) / 2); | 130 y = y + ((h - (height / PANGO_SCALE)) / 2); |
| 129 } | 131 } |
| 130 | 132 |
| 131 cairo_move_to(cr, x, y); | 133 cairo_move_to(cr, x, y); |
| 132 pango_cairo_show_layout(cr, layout); | 134 pango_cairo_show_layout(cr, layout); |
| 133 | 135 |
| 134 g_object_unref(layout); | 136 g_object_unref(layout); |
| 135 cairo_destroy(cr); | 137 cairo_destroy(cr); |
| 136 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. | 138 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. |
| 137 } | 139 } |
| 140 |
| 141 } // namespace gfx |
| OLD | NEW |