| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/render_text_win.h" | 5 #include "ui/gfx/render_text_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/win/scoped_hdc.h" | 13 #include "base/win/scoped_hdc.h" |
| 14 #include "third_party/skia/include/core/SkTypeface.h" | |
| 15 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/canvas_skia.h" | 15 #include "ui/gfx/canvas_skia.h" |
| 17 #include "ui/gfx/platform_font.h" | 16 #include "ui/gfx/platform_font.h" |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 // The maximum supported number of Uniscribe runs; a SCRIPT_ITEM is 8 bytes. | 20 // The maximum supported number of Uniscribe runs; a SCRIPT_ITEM is 8 bytes. |
| 22 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? | 21 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? |
| 23 const int kGuessItems = 100; | 22 const int kGuessItems = 100; |
| 24 const int kMaxItems = 10000; | 23 const int kMaxItems = 10000; |
| 25 | 24 |
| 26 // The maximum supported number of Uniscribe glyphs; a glyph is 1 word. | 25 // The maximum supported number of Uniscribe glyphs; a glyph is 1 word. |
| 27 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? | 26 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? |
| 28 const int kMaxGlyphs = 100000; | 27 const int kMaxGlyphs = 100000; |
| 29 | 28 |
| 30 // Draw underline and strike through text decorations. | |
| 31 // Based on |SkCanvas::DrawTextDecorations()| and constants from: | |
| 32 // third_party/skia/src/core/SkTextFormatParams.h | |
| 33 void DrawTextRunDecorations(SkCanvas* canvas_skia, | |
| 34 const SkPaint& paint, | |
| 35 const gfx::internal::TextRun& run, | |
| 36 SkScalar x, | |
| 37 SkScalar y) { | |
| 38 // Fraction of the text size to lower a strike through below the baseline. | |
| 39 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); | |
| 40 // Fraction of the text size to lower an underline below the baseline. | |
| 41 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); | |
| 42 // Fraction of the text size to use for a strike through or under-line. | |
| 43 const SkScalar kLineThickness = (SK_Scalar1 / 18); | |
| 44 | |
| 45 SkScalar text_size = paint.getTextSize(); | |
| 46 SkScalar height = SkScalarMul(text_size, kLineThickness); | |
| 47 SkRect r; | |
| 48 | |
| 49 r.fLeft = x; | |
| 50 r.fRight = x + run.width; | |
| 51 | |
| 52 if (run.underline) { | |
| 53 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); | |
| 54 r.fTop = offset; | |
| 55 r.fBottom = offset + height; | |
| 56 canvas_skia->drawRect(r, paint); | |
| 57 } | |
| 58 if (run.strike) { | |
| 59 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); | |
| 60 r.fTop = offset; | |
| 61 r.fBottom = offset + height; | |
| 62 canvas_skia->drawRect(r, paint); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 // Callback to |EnumEnhMetaFile()| to intercept font creation. | 29 // Callback to |EnumEnhMetaFile()| to intercept font creation. |
| 67 int CALLBACK MetaFileEnumProc(HDC hdc, | 30 int CALLBACK MetaFileEnumProc(HDC hdc, |
| 68 HANDLETABLE* table, | 31 HANDLETABLE* table, |
| 69 CONST ENHMETARECORD* record, | 32 CONST ENHMETARECORD* record, |
| 70 int table_entries, | 33 int table_entries, |
| 71 LPARAM log_font) { | 34 LPARAM log_font) { |
| 72 if (record->iType == EMR_EXTCREATEFONTINDIRECTW) { | 35 if (record->iType == EMR_EXTCREATEFONTINDIRECTW) { |
| 73 const EMREXTCREATEFONTINDIRECTW* create_font_record = | 36 const EMREXTCREATEFONTINDIRECTW* create_font_record = |
| 74 reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record); | 37 reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record); |
| 75 *reinterpret_cast<LOGFONT*>(log_font) = create_font_record->elfw.elfLogFont; | 38 *reinterpret_cast<LOGFONT*>(log_font) = create_font_record->elfw.elfLogFont; |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 if (!needs_layout_) | 370 if (!needs_layout_) |
| 408 return; | 371 return; |
| 409 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. | 372 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. |
| 410 ItemizeLogicalText(); | 373 ItemizeLogicalText(); |
| 411 if (!runs_.empty()) | 374 if (!runs_.empty()) |
| 412 LayoutVisualText(); | 375 LayoutVisualText(); |
| 413 needs_layout_ = false; | 376 needs_layout_ = false; |
| 414 } | 377 } |
| 415 | 378 |
| 416 void RenderTextWin::DrawVisualText(Canvas* canvas) { | 379 void RenderTextWin::DrawVisualText(Canvas* canvas) { |
| 417 SkCanvas* canvas_skia = canvas->GetSkCanvas(); | 380 DCHECK(!needs_layout_); |
| 418 | 381 |
| 419 Point offset(ToViewPoint(Point())); | 382 Point offset(ToViewPoint(Point())); |
| 420 // TODO(msw): Establish a vertical baseline for strings of mixed font heights. | 383 // TODO(msw): Establish a vertical baseline for strings of mixed font heights. |
| 421 size_t height = default_style().font.GetHeight(); | 384 size_t height = default_style().font.GetHeight(); |
| 422 | 385 |
| 423 SkScalar x = SkIntToScalar(offset.x()); | 386 SkScalar x = SkIntToScalar(offset.x()); |
| 424 SkScalar y = SkIntToScalar(offset.y()); | 387 SkScalar y = SkIntToScalar(offset.y()); |
| 425 // Center the text vertically in the display area. | 388 // Center the text vertically in the display area. |
| 426 y += (display_rect().height() - height) / 2; | 389 y += (display_rect().height() - height) / 2; |
| 427 // Offset by the font size to account for Skia expecting y to be the bottom. | 390 // Offset by the font size to account for Skia expecting y to be the bottom. |
| 428 y += default_style().font.GetFontSize(); | 391 y += default_style().font.GetFontSize(); |
| 429 | 392 |
| 430 SkPaint paint; | 393 internal::SkiaTextRenderer renderer(canvas); |
| 431 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 394 renderer.Init(); |
| 432 paint.setStyle(SkPaint::kFill_Style); | |
| 433 paint.setAntiAlias(true); | |
| 434 paint.setSubpixelText(true); | |
| 435 paint.setLCDRenderText(true); | |
| 436 | 395 |
| 437 std::vector<SkPoint> pos; | 396 std::vector<SkPoint> pos; |
| 438 for (size_t i = 0; i < runs_.size(); ++i) { | 397 for (size_t i = 0; i < runs_.size(); ++i) { |
| 439 // Get the run specified by the visual-to-logical map. | 398 // Get the run specified by the visual-to-logical map. |
| 440 internal::TextRun* run = runs_[visual_to_logical_[i]]; | 399 internal::TextRun* run = runs_[visual_to_logical_[i]]; |
| 441 | 400 |
| 442 // TODO(msw): Font default/fallback and style integration. | |
| 443 SkTypeface::Style style = SkTypeface::kNormal; | |
| 444 SkTypeface* typeface = | |
| 445 SkTypeface::CreateFromName(run->font.GetFontName().c_str(), style); | |
| 446 if (typeface) { | |
| 447 paint.setTypeface(typeface); | |
| 448 // |paint| adds its own ref. Release the ref from CreateFromName. | |
| 449 typeface->unref(); | |
| 450 } | |
| 451 paint.setTextSize(run->font.GetFontSize()); | |
| 452 paint.setColor(run->foreground); | |
| 453 | |
| 454 SkScalar run_x = x; | |
| 455 | |
| 456 // Based on WebCore::skiaDrawText. | 401 // Based on WebCore::skiaDrawText. |
| 457 pos.resize(run->glyph_count); | 402 pos.resize(run->glyph_count); |
| 403 SkScalar glyph_x = x; |
| 458 for (int glyph = 0; glyph < run->glyph_count; glyph++) { | 404 for (int glyph = 0; glyph < run->glyph_count; glyph++) { |
| 459 pos[glyph].set(x + run->offsets[glyph].du, | 405 pos[glyph].set(glyph_x + run->offsets[glyph].du, |
| 460 y + run->offsets[glyph].dv); | 406 y + run->offsets[glyph].dv); |
| 461 x += SkIntToScalar(run->advance_widths[glyph]); | 407 glyph_x += SkIntToScalar(run->advance_widths[glyph]); |
| 462 } | 408 } |
| 463 | 409 |
| 464 size_t byte_length = run->glyph_count * sizeof(WORD); | 410 renderer.SetFont(run->font); |
| 465 canvas_skia->drawPosText(run->glyphs.get(), byte_length, &pos[0], paint); | 411 renderer.SetForegroundColor(run->foreground); |
| 412 renderer.DrawPosText(&pos[0], run->glyphs.get(), run->glyph_count); |
| 466 | 413 |
| 467 if (run->strike || run->underline) | 414 if (run->strike || run->underline) |
| 468 DrawTextRunDecorations(canvas_skia, paint, *run, run_x, y); | 415 renderer.DrawDecorations(x, y, run->width, run->underline, run->strike); |
| 416 |
| 417 x = glyph_x; |
| 469 } | 418 } |
| 470 } | 419 } |
| 471 | 420 |
| 472 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { | 421 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { |
| 473 EnsureLayout(); | 422 EnsureLayout(); |
| 474 | 423 |
| 475 if (text().empty()) | 424 if (text().empty()) |
| 476 return 0; | 425 return 0; |
| 477 | 426 |
| 478 if (index >= text().length()) { | 427 if (index >= text().length()) { |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; | 732 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; |
| 784 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : | 733 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : |
| 785 FirstSelectionModelInsideRun(next); | 734 FirstSelectionModelInsideRun(next); |
| 786 } | 735 } |
| 787 | 736 |
| 788 RenderText* RenderText::CreateRenderText() { | 737 RenderText* RenderText::CreateRenderText() { |
| 789 return new RenderTextWin; | 738 return new RenderTextWin; |
| 790 } | 739 } |
| 791 | 740 |
| 792 } // namespace gfx | 741 } // namespace gfx |
| OLD | NEW |