Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_linux.h" | 5 #include "ui/gfx/render_text_linux.h" |
| 6 | 6 |
| 7 #include <pango/pangocairo.h> | 7 #include <pango/pangocairo.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 current_line_ = pango_layout_get_line_readonly(layout_, 0); | 318 current_line_ = pango_layout_get_line_readonly(layout_, 0); |
| 319 pango_layout_line_ref(current_line_); | 319 pango_layout_line_ref(current_line_); |
| 320 | 320 |
| 321 pango_layout_get_log_attrs(layout_, &log_attrs_, &num_log_attrs_); | 321 pango_layout_get_log_attrs(layout_, &log_attrs_, &num_log_attrs_); |
| 322 } | 322 } |
| 323 } | 323 } |
| 324 | 324 |
| 325 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) { | 325 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) { |
| 326 PangoAttrList* attrs = pango_attr_list_new(); | 326 PangoAttrList* attrs = pango_attr_list_new(); |
| 327 | 327 |
| 328 int default_font_style = font_list().GetFontStyle(); | 328 // Splitting text runs to accommodate styling can break Arabic glyph shaping. |
| 329 for (StyleRanges::const_iterator i = style_ranges().begin(); | 329 // Only split text runs as needed for bold and italic font styles changes. |
| 330 i < style_ranges().end(); ++i) { | 330 BreakList<bool>::const_iterator bold = styles()[BOLD].breaks().begin(); |
| 331 // In Pango, different fonts means different runs, and it breaks Arabic | 331 BreakList<bool>::const_iterator italic = styles()[ITALIC].breaks().begin(); |
| 332 // shaping across run boundaries. So, set font only when it is different | 332 while (bold != styles()[BOLD].breaks().end() && |
| 333 // from the default font. | 333 italic != styles()[ITALIC].breaks().end()) { |
| 334 // TODO(xji): We'll eventually need to split up StyleRange into components | 334 const int style = (bold->second ? Font::BOLD : 0) | |
| 335 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges | 335 (italic->second ? Font::ITALIC : 0); |
| 336 // with the same Fonts (to avoid unnecessarily splitting up runs). | 336 const size_t bold_end = styles()[BOLD].GetRange(bold).end(); |
| 337 if (i->font_style != default_font_style) { | 337 const size_t italic_end = styles()[ITALIC].GetRange(italic).end(); |
| 338 FontList derived_font_list = font_list().DeriveFontList(i->font_style); | 338 const size_t style_end = std::min(bold_end, italic_end); |
| 339 if (style != font_list().GetFontStyle()) { | |
| 340 FontList derived_font_list = font_list().DeriveFontList(style); | |
| 339 ScopedPangoFontDescription desc(pango_font_description_from_string( | 341 ScopedPangoFontDescription desc(pango_font_description_from_string( |
| 340 derived_font_list.GetFontDescriptionString().c_str())); | 342 derived_font_list.GetFontDescriptionString().c_str())); |
| 341 | 343 |
| 342 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc.get()); | 344 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc.get()); |
| 343 pango_attr->start_index = TextIndexToLayoutIndex(i->range.start()); | 345 pango_attr->start_index = |
| 344 pango_attr->end_index = TextIndexToLayoutIndex(i->range.end()); | 346 TextIndexToLayoutIndex(std::max(bold->first, italic->first)); |
| 347 pango_attr->end_index = TextIndexToLayoutIndex(style_end); | |
| 345 pango_attr_list_insert(attrs, pango_attr); | 348 pango_attr_list_insert(attrs, pango_attr); |
| 346 } | 349 } |
| 350 bold += bold_end == style_end ? 1 : 0; | |
| 351 italic += italic_end == style_end ? 1 : 0; | |
| 347 } | 352 } |
| 353 DCHECK(bold == styles()[BOLD].breaks().end()); | |
| 354 DCHECK(italic == styles()[ITALIC].breaks().end()); | |
| 348 | 355 |
| 349 pango_layout_set_attributes(layout, attrs); | 356 pango_layout_set_attributes(layout, attrs); |
| 350 pango_attr_list_unref(attrs); | 357 pango_attr_list_unref(attrs); |
| 351 } | 358 } |
| 352 | 359 |
| 353 void RenderTextLinux::DrawVisualText(Canvas* canvas) { | 360 void RenderTextLinux::DrawVisualText(Canvas* canvas) { |
| 354 DCHECK(layout_); | 361 DCHECK(layout_); |
| 355 | 362 |
| 356 Vector2d offset(GetOffsetForDrawing()); | 363 Vector2d offset(GetOffsetForDrawing()); |
| 357 // Skia will draw glyphs with respect to the baseline. | 364 // Skia will draw glyphs with respect to the baseline. |
| 358 offset += Vector2d(0, PANGO_PIXELS(pango_layout_get_baseline(layout_))); | 365 offset += Vector2d(0, PANGO_PIXELS(pango_layout_get_baseline(layout_))); |
| 359 | 366 |
| 360 SkScalar x = SkIntToScalar(offset.x()); | 367 SkScalar x = SkIntToScalar(offset.x()); |
| 361 SkScalar y = SkIntToScalar(offset.y()); | 368 SkScalar y = SkIntToScalar(offset.y()); |
| 362 | 369 |
| 363 std::vector<SkPoint> pos; | 370 std::vector<SkPoint> pos; |
| 364 std::vector<uint16> glyphs; | 371 std::vector<uint16> glyphs; |
| 365 | 372 |
| 366 StyleRanges styles(style_ranges()); | |
| 367 ApplyCompositionAndSelectionStyles(&styles); | |
| 368 | |
| 369 // Pre-calculate UTF8 indices from UTF16 indices. | |
| 370 // TODO(asvitkine): Can we cache these? | |
| 371 std::vector<ui::Range> style_ranges_utf8; | |
| 372 style_ranges_utf8.reserve(styles.size()); | |
| 373 size_t start_index = 0; | |
| 374 for (size_t i = 0; i < styles.size(); ++i) { | |
| 375 size_t end_index = TextIndexToLayoutIndex(styles[i].range.end()); | |
| 376 style_ranges_utf8.push_back(ui::Range(start_index, end_index)); | |
| 377 start_index = end_index; | |
| 378 } | |
| 379 | |
| 380 internal::SkiaTextRenderer renderer(canvas); | 373 internal::SkiaTextRenderer renderer(canvas); |
| 381 ApplyFadeEffects(&renderer); | 374 ApplyFadeEffects(&renderer); |
| 382 ApplyTextShadows(&renderer); | 375 ApplyTextShadows(&renderer); |
| 383 | 376 |
| 384 // TODO(derat): Use font-specific params: http://crbug.com/125235 | 377 // TODO(derat): Use font-specific params: http://crbug.com/125235 |
| 385 const gfx::FontRenderParams& render_params = | 378 const gfx::FontRenderParams& render_params = |
| 386 gfx::GetDefaultFontRenderParams(); | 379 gfx::GetDefaultFontRenderParams(); |
| 387 const bool use_subpixel_rendering = | 380 const bool use_subpixel_rendering = |
| 388 render_params.subpixel_rendering != | 381 render_params.subpixel_rendering != |
| 389 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; | 382 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; |
| 390 renderer.SetFontSmoothingSettings( | 383 renderer.SetFontSmoothingSettings( |
| 391 render_params.antialiasing, | 384 render_params.antialiasing, |
| 392 use_subpixel_rendering && !background_is_transparent()); | 385 use_subpixel_rendering && !background_is_transparent()); |
| 393 | 386 |
| 387 // Temporarily apply composition underlines and selection colors. | |
| 388 ApplyCompositionAndSelectionStyles(); | |
| 389 | |
| 390 internal::StyleIterator style(colors(), styles()); | |
| 394 for (GSList* it = current_line_->runs; it; it = it->next) { | 391 for (GSList* it = current_line_->runs; it; it = it->next) { |
| 395 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); | 392 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); |
| 396 int glyph_count = run->glyphs->num_glyphs; | 393 int glyph_count = run->glyphs->num_glyphs; |
| 397 if (glyph_count == 0) | 394 if (glyph_count == 0) |
| 398 continue; | 395 continue; |
| 399 | 396 |
| 400 size_t run_start = run->item->offset; | |
| 401 size_t first_glyph_byte_index = run_start + run->glyphs->log_clusters[0]; | |
| 402 size_t style_increment = IsForwardMotion(CURSOR_RIGHT, run->item) ? 1 : -1; | |
| 403 | |
| 404 // Find the initial style for this run. | |
| 405 // TODO(asvitkine): Can we avoid looping here, e.g. by caching this per run? | |
| 406 int style = -1; | |
| 407 for (size_t i = 0; i < style_ranges_utf8.size(); ++i) { | |
| 408 if (IndexInRange(style_ranges_utf8[i], first_glyph_byte_index)) { | |
| 409 style = i; | |
| 410 break; | |
| 411 } | |
| 412 } | |
| 413 DCHECK_GE(style, 0); | |
| 414 | |
| 415 ScopedPangoFontDescription desc( | 397 ScopedPangoFontDescription desc( |
| 416 pango_font_describe(run->item->analysis.font)); | 398 pango_font_describe(run->item->analysis.font)); |
| 417 | 399 |
| 418 const std::string family_name = | 400 const std::string family_name = |
| 419 pango_font_description_get_family(desc.get()); | 401 pango_font_description_get_family(desc.get()); |
| 420 renderer.SetTextSize(GetPangoFontSizeInPixels(desc.get())); | 402 renderer.SetTextSize(GetPangoFontSizeInPixels(desc.get())); |
| 421 | 403 |
| 422 SkScalar glyph_x = x; | |
| 423 SkScalar start_x = x; | |
| 424 int start = 0; | |
| 425 | |
| 426 glyphs.resize(glyph_count); | 404 glyphs.resize(glyph_count); |
| 427 pos.resize(glyph_count); | 405 pos.resize(glyph_count); |
| 428 | 406 |
| 429 for (int i = 0; i < glyph_count; ++i) { | 407 // Track the glyph index and x-coordinate ranges for each styled range. |
|
Alexei Svitkine (slow)
2013/01/31 21:25:07
I was looking for a comment explaining the differe
msw
2013/02/01 00:08:08
Done; I also renamed x_end to match; hopefully thi
| |
| 430 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; | 408 int glyph_start = 0; |
| 431 glyphs[i] = static_cast<uint16>(glyph.glyph); | 409 int glyph_end = 0; |
| 432 // Use pango_units_to_double() rather than PANGO_PIXELS() here so that | 410 SkScalar x_end = x; |
| 433 // units won't get rounded to the pixel grid if we're using subpixel | |
| 434 // positioning. | |
| 435 pos[i].set(glyph_x + pango_units_to_double(glyph.geometry.x_offset), | |
| 436 y + pango_units_to_double(glyph.geometry.y_offset)); | |
| 437 | 411 |
| 438 // If this glyph is beyond the current style, draw the glyphs so far and | 412 // Track the current style and its text index range. |
| 439 // advance to the next style. | 413 style.UpdatePosition(GetGlyphTextIndex(run, glyph_start)); |
| 440 size_t glyph_byte_index = run_start + run->glyphs->log_clusters[i]; | 414 ui::Range style_range = style.GetRange(); |
| 441 DCHECK_GE(style, 0); | 415 |
| 442 DCHECK_LT(style, static_cast<int>(styles.size())); | 416 do { |
| 443 if (!IndexInRange(style_ranges_utf8[style], glyph_byte_index)) { | 417 const PangoGlyphInfo& glyph = run->glyphs->glyphs[glyph_end]; |
| 418 glyphs[glyph_end] = static_cast<uint16>(glyph.glyph); | |
| 419 // Use pango_units_to_double() rather than PANGO_PIXELS() here, so units | |
| 420 // are not rounded to the pixel grid if subpixel positioning is enabled. | |
| 421 pos[glyph_end].set(x_end + pango_units_to_double(glyph.geometry.x_offset), | |
| 422 y + pango_units_to_double(glyph.geometry.y_offset)); | |
| 423 x_end += pango_units_to_double(glyph.geometry.width); | |
| 424 | |
| 425 ++glyph_end; | |
| 426 const size_t glyph_text_index = (glyph_end == glyph_count) ? | |
| 427 style_range.end() : GetGlyphTextIndex(run, glyph_end); | |
| 428 if (!IndexInRange(style_range, glyph_text_index)) { | |
| 444 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph | 429 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph |
| 445 // but can span multiple styles, Pango splits the | 430 // but can span multiple styles, Pango splits the |
| 446 // styles evenly over the glyph. We can do this too by | 431 // styles evenly over the glyph. We can do this too by |
| 447 // clipping and drawing the glyph several times. | 432 // clipping and drawing the glyph several times. |
| 448 renderer.SetForegroundColor(styles[style].foreground); | 433 renderer.SetForegroundColor(style.color()); |
| 449 renderer.SetFontFamilyWithStyle(family_name, styles[style].font_style); | 434 const int font_style = (style.style(BOLD) ? Font::BOLD : 0) | |
| 450 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); | 435 (style.style(ITALIC) ? Font::ITALIC : 0); |
| 451 if (styles[style].underline) | 436 renderer.SetFontFamilyWithStyle(family_name, font_style); |
| 437 renderer.DrawPosText(&pos[glyph_start], &glyphs[glyph_start], | |
| 438 glyph_end - glyph_start); | |
| 439 if (style.style(UNDERLINE)) | |
| 452 SetPangoUnderlineMetrics(desc.get(), &renderer); | 440 SetPangoUnderlineMetrics(desc.get(), &renderer); |
| 453 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | 441 renderer.DrawDecorations(x, y, x_end - x, |
| 442 style.style(UNDERLINE), style.style(STRIKE), | |
| 443 style.style(DIAGONAL_STRIKE)); | |
| 444 style.UpdatePosition(glyph_text_index); | |
| 445 style_range = style.GetRange(); | |
| 446 glyph_start = glyph_end; | |
| 447 x = x_end; | |
| 448 } | |
| 449 } while (glyph_end < glyph_count); | |
| 450 } | |
| 454 | 451 |
| 455 start = i; | 452 // Undo the temporarily applied composition underlines and selection colors. |
| 456 start_x = glyph_x; | 453 UndoCompositionAndSelectionStyles(); |
| 457 // Loop to find the next style, in case the glyph spans multiple styles. | |
| 458 do { | |
| 459 style += style_increment; | |
| 460 } while (style >= 0 && style < static_cast<int>(styles.size()) && | |
| 461 !IndexInRange(style_ranges_utf8[style], glyph_byte_index)); | |
| 462 } | |
| 463 | |
| 464 glyph_x += pango_units_to_double(glyph.geometry.width); | |
| 465 } | |
| 466 | |
| 467 // Draw the remaining glyphs. | |
| 468 renderer.SetForegroundColor(styles[style].foreground); | |
| 469 renderer.SetFontFamilyWithStyle(family_name, styles[style].font_style); | |
| 470 renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start); | |
| 471 if (styles[style].underline) | |
| 472 SetPangoUnderlineMetrics(desc.get(), &renderer); | |
| 473 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | |
| 474 x = glyph_x; | |
| 475 } | |
| 476 } | 454 } |
| 477 | 455 |
| 478 GSList* RenderTextLinux::GetRunContainingCaret( | 456 GSList* RenderTextLinux::GetRunContainingCaret( |
| 479 const SelectionModel& caret) const { | 457 const SelectionModel& caret) const { |
| 480 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); | 458 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); |
| 481 LogicalCursorDirection affinity = caret.caret_affinity(); | 459 LogicalCursorDirection affinity = caret.caret_affinity(); |
| 482 GSList* run = current_line_->runs; | 460 GSList* run = current_line_->runs; |
| 483 while (run) { | 461 while (run) { |
| 484 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 462 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
| 485 ui::Range item_range(item->offset, item->offset + item->length); | 463 ui::Range item_range(item->offset, item->offset + item->length); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 g_free(ranges); | 509 g_free(ranges); |
| 532 return bounds; | 510 return bounds; |
| 533 } | 511 } |
| 534 | 512 |
| 535 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 513 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
| 536 if (selection_visual_bounds_.empty()) | 514 if (selection_visual_bounds_.empty()) |
| 537 selection_visual_bounds_ = CalculateSubstringBounds(selection()); | 515 selection_visual_bounds_ = CalculateSubstringBounds(selection()); |
| 538 return selection_visual_bounds_; | 516 return selection_visual_bounds_; |
| 539 } | 517 } |
| 540 | 518 |
| 519 size_t RenderTextLinux::GetGlyphTextIndex(PangoLayoutRun* run, | |
| 520 int glyph_index) const { | |
| 521 return LayoutIndexToTextIndex(run->item->offset + | |
| 522 run->glyphs->log_clusters[glyph_index]); | |
| 523 } | |
| 524 | |
| 541 RenderText* RenderText::CreateInstance() { | 525 RenderText* RenderText::CreateInstance() { |
| 542 return new RenderTextLinux; | 526 return new RenderTextLinux; |
| 543 } | 527 } |
| 544 | 528 |
| 545 } // namespace gfx | 529 } // namespace gfx |
| OLD | NEW |