| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "app/gfx/chrome_canvas.h" | |
| 6 | |
| 7 #include <pango/pango.h> | |
| 8 | |
| 9 #include "app/gfx/chrome_font.h" | |
| 10 #include "base/gfx/rect.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/string_util.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Returns a new pango font, free with pango_font_description_free(). | |
| 17 PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { | |
| 18 gfx::Font font = gfx_font; // Copy so we can call non-const methods. | |
| 19 PangoFontDescription* pfd = pango_font_description_new(); | |
| 20 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); | |
| 21 pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); | |
| 22 | |
| 23 switch (font.style()) { | |
| 24 case gfx::Font::NORMAL: | |
| 25 // Nothing to do, should already be PANGO_STYLE_NORMAL. | |
| 26 break; | |
| 27 case gfx::Font::BOLD: | |
| 28 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); | |
| 29 break; | |
| 30 case gfx::Font::ITALIC: | |
| 31 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); | |
| 32 break; | |
| 33 case gfx::Font::UNDERLINED: | |
| 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. | |
| 36 break; | |
| 37 } | |
| 38 | |
| 39 return pfd; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 ChromeCanvas::ChromeCanvas(int width, int height, bool is_opaque) | |
| 45 : skia::PlatformCanvasLinux(width, height, is_opaque) { | |
| 46 } | |
| 47 | |
| 48 ChromeCanvas::ChromeCanvas() : skia::PlatformCanvasLinux() { | |
| 49 } | |
| 50 | |
| 51 ChromeCanvas::~ChromeCanvas() { | |
| 52 } | |
| 53 | |
| 54 // static | |
| 55 void ChromeCanvas::SizeStringInt(const std::wstring& text, | |
| 56 const gfx::Font& font, | |
| 57 int* width, int* height, int flags) { | |
| 58 NOTIMPLEMENTED(); | |
| 59 } | |
| 60 | |
| 61 void ChromeCanvas::ApplySkiaMatrixToCairoContext(cairo_t* cr) { | |
| 62 const SkMatrix& skia_matrix = getTotalMatrix(); | |
| 63 cairo_matrix_t cairo_matrix; | |
| 64 cairo_matrix_init(&cairo_matrix, | |
| 65 SkScalarToFloat(skia_matrix.getScaleX()), | |
| 66 SkScalarToFloat(skia_matrix.getSkewY()), | |
| 67 SkScalarToFloat(skia_matrix.getSkewX()), | |
| 68 SkScalarToFloat(skia_matrix.getScaleY()), | |
| 69 SkScalarToFloat(skia_matrix.getTranslateX()), | |
| 70 SkScalarToFloat(skia_matrix.getTranslateY())); | |
| 71 cairo_set_matrix(cr, &cairo_matrix); | |
| 72 } | |
| 73 | |
| 74 void ChromeCanvas::DrawStringInt(const std::wstring& text, | |
| 75 const gfx::Font& font, | |
| 76 const SkColor& color, int x, int y, int w, | |
| 77 int h, int flags) { | |
| 78 cairo_surface_t* surface = beginPlatformPaint(); | |
| 79 cairo_t* cr = cairo_create(surface); | |
| 80 // 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 | |
| 82 // we draw at the right place. | |
| 83 ApplySkiaMatrixToCairoContext(cr); | |
| 84 PangoLayout* layout = pango_cairo_create_layout(cr); | |
| 85 | |
| 86 cairo_set_source_rgb(cr, | |
| 87 SkColorGetR(color) / 255.0, | |
| 88 SkColorGetG(color) / 255.0, | |
| 89 SkColorGetB(color) / 255.0); | |
| 90 | |
| 91 // TODO(deanm): Implement the rest of the ChromeCanvas flags. | |
| 92 if (!(flags & ChromeCanvas::NO_ELLIPSIS)) | |
| 93 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | |
| 94 | |
| 95 pango_layout_set_width(layout, w * PANGO_SCALE); | |
| 96 pango_layout_set_height(layout, h * PANGO_SCALE); | |
| 97 | |
| 98 if (flags & TEXT_ALIGN_CENTER) { | |
| 99 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | |
| 100 } else if (flags & TEXT_ALIGN_RIGHT) { | |
| 101 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | |
| 102 } | |
| 103 | |
| 104 if (flags & MULTI_LINE) { | |
| 105 pango_layout_set_wrap(layout, | |
| 106 (flags & CHARACTER_BREAK) ? PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); | |
| 107 } | |
| 108 | |
| 109 if (flags & NO_ELLIPSIS) | |
| 110 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | |
| 111 | |
| 112 std::string utf8 = WideToUTF8(text); | |
| 113 pango_layout_set_text(layout, utf8.data(), utf8.size()); | |
| 114 | |
| 115 PangoFontDescription* desc = PangoFontFromGfxFont(font); | |
| 116 pango_layout_set_font_description(layout, desc); | |
| 117 pango_font_description_free(desc); | |
| 118 | |
| 119 int width, height; | |
| 120 pango_layout_get_size(layout, &width, &height); | |
| 121 | |
| 122 if (flags & ChromeCanvas::TEXT_VALIGN_TOP) { | |
| 123 // Cairo should draw from the top left corner already. | |
| 124 } else if (flags & ChromeCanvas::TEXT_VALIGN_BOTTOM) { | |
| 125 y = y + (h - (height / PANGO_SCALE)); | |
| 126 } else { | |
| 127 // Vertically centered. | |
| 128 y = y + ((h - (height / PANGO_SCALE)) / 2); | |
| 129 } | |
| 130 | |
| 131 cairo_move_to(cr, x, y); | |
| 132 pango_cairo_show_layout(cr, layout); | |
| 133 | |
| 134 g_object_unref(layout); | |
| 135 cairo_destroy(cr); | |
| 136 // NOTE: beginPlatformPaint returned its surface, we shouldn't destroy it. | |
| 137 } | |
| OLD | NEW |