| 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(GetOriginForSkiaDrawing()); |
| 420 // TODO(msw): Establish a vertical baseline for strings of mixed font heights. | |
| 421 size_t height = default_style().font.GetHeight(); | |
| 422 | |
| 423 SkScalar x = SkIntToScalar(offset.x()); | 383 SkScalar x = SkIntToScalar(offset.x()); |
| 424 SkScalar y = SkIntToScalar(offset.y()); | 384 SkScalar y = SkIntToScalar(offset.y()); |
| 425 // Center the text vertically in the display area. | |
| 426 y += (display_rect().height() - height) / 2; | |
| 427 // Offset by the font size to account for Skia expecting y to be the bottom. | |
| 428 y += default_style().font.GetFontSize(); | |
| 429 | |
| 430 SkPaint paint; | |
| 431 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
| 432 paint.setStyle(SkPaint::kFill_Style); | |
| 433 paint.setAntiAlias(true); | |
| 434 paint.setSubpixelText(true); | |
| 435 paint.setLCDRenderText(true); | |
| 436 | 385 |
| 437 std::vector<SkPoint> pos; | 386 std::vector<SkPoint> pos; |
| 387 |
| 388 internal::SkiaTextRenderer renderer(canvas); |
| 438 for (size_t i = 0; i < runs_.size(); ++i) { | 389 for (size_t i = 0; i < runs_.size(); ++i) { |
| 439 // Get the run specified by the visual-to-logical map. | 390 // Get the run specified by the visual-to-logical map. |
| 440 internal::TextRun* run = runs_[visual_to_logical_[i]]; | 391 internal::TextRun* run = runs_[visual_to_logical_[i]]; |
| 441 | 392 |
| 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. | 393 // Based on WebCore::skiaDrawText. |
| 457 pos.resize(run->glyph_count); | 394 pos.resize(run->glyph_count); |
| 395 SkScalar glyph_x = x; |
| 458 for (int glyph = 0; glyph < run->glyph_count; glyph++) { | 396 for (int glyph = 0; glyph < run->glyph_count; glyph++) { |
| 459 pos[glyph].set(x + run->offsets[glyph].du, | 397 pos[glyph].set(glyph_x + run->offsets[glyph].du, |
| 460 y + run->offsets[glyph].dv); | 398 y + run->offsets[glyph].dv); |
| 461 x += SkIntToScalar(run->advance_widths[glyph]); | 399 glyph_x += SkIntToScalar(run->advance_widths[glyph]); |
| 462 } | 400 } |
| 463 | 401 |
| 464 size_t byte_length = run->glyph_count * sizeof(WORD); | 402 renderer.SetFont(run->font); |
| 465 canvas_skia->drawPosText(run->glyphs.get(), byte_length, &pos[0], paint); | 403 renderer.SetForegroundColor(run->foreground); |
| 404 renderer.DrawPosText(&pos[0], run->glyphs.get(), run->glyph_count); |
| 466 | 405 |
| 467 if (run->strike || run->underline) | 406 if (run->strike || run->underline) |
| 468 DrawTextRunDecorations(canvas_skia, paint, *run, run_x, y); | 407 renderer.DrawDecorations(x, y, run->width, run->underline, run->strike); |
| 408 |
| 409 x = glyph_x; |
| 469 } | 410 } |
| 470 } | 411 } |
| 471 | 412 |
| 472 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { | 413 size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) { |
| 473 EnsureLayout(); | 414 EnsureLayout(); |
| 474 | 415 |
| 475 if (text().empty()) | 416 if (text().empty()) |
| 476 return 0; | 417 return 0; |
| 477 | 418 |
| 478 if (index >= text().length()) { | 419 if (index >= text().length()) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 &script_items[0], | 484 &script_items[0], |
| 544 &script_items_count); | 485 &script_items_count); |
| 545 } | 486 } |
| 546 DCHECK(SUCCEEDED(hr)); | 487 DCHECK(SUCCEEDED(hr)); |
| 547 | 488 |
| 548 if (script_items_count <= 0) | 489 if (script_items_count <= 0) |
| 549 return; | 490 return; |
| 550 | 491 |
| 551 // Build the list of runs, merge font/underline styles. | 492 // Build the list of runs, merge font/underline styles. |
| 552 // TODO(msw): Only break for font changes, not color etc. See TextRun comment. | 493 // TODO(msw): Only break for font changes, not color etc. See TextRun comment. |
| 553 // TODO(msw): Apply the overriding selection and composition styles. | 494 StyleRanges styles(style_ranges()); |
| 554 StyleRanges::const_iterator style = style_ranges().begin(); | 495 ApplyCompositionAndSelectionStyles(&styles); |
| 496 StyleRanges::const_iterator style = styles.begin(); |
| 555 SCRIPT_ITEM* script_item = &script_items[0]; | 497 SCRIPT_ITEM* script_item = &script_items[0]; |
| 556 for (int run_break = 0; run_break < text_length;) { | 498 for (int run_break = 0; run_break < text_length;) { |
| 557 internal::TextRun* run = new internal::TextRun(); | 499 internal::TextRun* run = new internal::TextRun(); |
| 558 run->range.set_start(run_break); | 500 run->range.set_start(run_break); |
| 559 run->font = style->font; | 501 run->font = style->font; |
| 560 run->foreground = style->foreground; | 502 run->foreground = style->foreground; |
| 561 run->strike = style->strike; | 503 run->strike = style->strike; |
| 562 run->underline = style->underline; | 504 run->underline = style->underline; |
| 563 run->script_analysis = script_item->a; | 505 run->script_analysis = script_item->a; |
| 564 | 506 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; | 731 internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]]; |
| 790 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : | 732 return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) : |
| 791 FirstSelectionModelInsideRun(next); | 733 FirstSelectionModelInsideRun(next); |
| 792 } | 734 } |
| 793 | 735 |
| 794 RenderText* RenderText::CreateRenderText() { | 736 RenderText* RenderText::CreateRenderText() { |
| 795 return new RenderTextWin; | 737 return new RenderTextWin; |
| 796 } | 738 } |
| 797 | 739 |
| 798 } // namespace gfx | 740 } // namespace gfx |
| OLD | NEW |