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).list().begin(); |
331 // In Pango, different fonts means different runs, and it breaks Arabic | 331 BreakList<bool>::const_iterator italic = styles(ITALIC).list().begin(); |
332 // shaping across run boundaries. So, set font only when it is different | 332 while (bold != styles(BOLD).list().end() && |
333 // from the default font. | 333 italic != styles(ITALIC).list().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 = TextIndexToLayoutIndex(std::min(text().length(), |
337 if (i->font_style != default_font_style) { | 337 styles(BOLD).GetBreakEnd(bold))); |
338 FontList derived_font_list = font_list().DeriveFontList(i->font_style); | 338 const size_t italic_end = TextIndexToLayoutIndex(std::min(text().length(), |
| 339 styles(ITALIC).GetBreakEnd(italic))); |
| 340 const size_t end = std::min(bold_end, italic_end); |
| 341 if (style != Font::NORMAL) { |
| 342 FontList derived_font_list = font_list().DeriveFontList(style); |
339 ScopedPangoFontDescription desc(pango_font_description_from_string( | 343 ScopedPangoFontDescription desc(pango_font_description_from_string( |
340 derived_font_list.GetFontDescriptionString().c_str())); | 344 derived_font_list.GetFontDescriptionString().c_str())); |
341 | 345 |
342 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc.get()); | 346 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc.get()); |
343 pango_attr->start_index = TextIndexToLayoutIndex(i->range.start()); | 347 pango_attr->start_index = |
344 pango_attr->end_index = TextIndexToLayoutIndex(i->range.end()); | 348 TextIndexToLayoutIndex(std::max(bold->first, italic->first)); |
| 349 pango_attr->end_index = end; |
345 pango_attr_list_insert(attrs, pango_attr); | 350 pango_attr_list_insert(attrs, pango_attr); |
346 } | 351 } |
| 352 bold += bold_end == end ? 1 : 0; |
| 353 italic += italic_end == end ? 1 : 0; |
347 } | 354 } |
| 355 DCHECK(bold == styles(BOLD).list().end()); |
| 356 DCHECK(italic == styles(ITALIC).list().end()); |
348 | 357 |
349 pango_layout_set_attributes(layout, attrs); | 358 pango_layout_set_attributes(layout, attrs); |
350 pango_attr_list_unref(attrs); | 359 pango_attr_list_unref(attrs); |
351 } | 360 } |
352 | 361 |
353 void RenderTextLinux::DrawVisualText(Canvas* canvas) { | 362 void RenderTextLinux::DrawVisualText(Canvas* canvas) { |
354 DCHECK(layout_); | 363 DCHECK(layout_); |
355 | 364 |
356 Vector2d offset(GetOffsetForDrawing()); | 365 Vector2d offset(GetOffsetForDrawing()); |
357 // Skia will draw glyphs with respect to the baseline. | 366 // Skia will draw glyphs with respect to the baseline. |
358 offset += Vector2d(0, PANGO_PIXELS(pango_layout_get_baseline(layout_))); | 367 offset += Vector2d(0, PANGO_PIXELS(pango_layout_get_baseline(layout_))); |
359 | 368 |
360 SkScalar x = SkIntToScalar(offset.x()); | 369 SkScalar x = SkIntToScalar(offset.x()); |
361 SkScalar y = SkIntToScalar(offset.y()); | 370 SkScalar y = SkIntToScalar(offset.y()); |
362 | 371 |
363 std::vector<SkPoint> pos; | 372 std::vector<SkPoint> pos; |
364 std::vector<uint16> glyphs; | 373 std::vector<uint16> glyphs; |
365 | 374 |
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); | 375 internal::SkiaTextRenderer renderer(canvas); |
381 ApplyFadeEffects(&renderer); | 376 ApplyFadeEffects(&renderer); |
382 ApplyTextShadows(&renderer); | 377 ApplyTextShadows(&renderer); |
383 | 378 |
384 // TODO(derat): Use font-specific params: http://crbug.com/125235 | 379 // TODO(derat): Use font-specific params: http://crbug.com/125235 |
385 const gfx::FontRenderParams& render_params = | 380 const gfx::FontRenderParams& render_params = |
386 gfx::GetDefaultFontRenderParams(); | 381 gfx::GetDefaultFontRenderParams(); |
387 const bool use_subpixel_rendering = | 382 const bool use_subpixel_rendering = |
388 render_params.subpixel_rendering != | 383 render_params.subpixel_rendering != |
389 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; | 384 gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; |
390 renderer.SetFontSmoothingSettings( | 385 renderer.SetFontSmoothingSettings( |
391 render_params.antialiasing, | 386 render_params.antialiasing, |
392 use_subpixel_rendering && !background_is_transparent()); | 387 use_subpixel_rendering && !background_is_transparent()); |
393 | 388 |
| 389 // Temporarily apply composition underlines and selection colors. |
| 390 ApplyCompositionAndSelectionStyles(); |
| 391 |
| 392 // Track the current color and style with iterators. |
| 393 BreakList<SkColor>::const_iterator color = colors().list().begin(); |
| 394 BreakList<bool>::const_iterator style[NUM_TEXT_STYLES]; |
| 395 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) |
| 396 style[i] = styles(static_cast<TextStyle>(i)).list().begin(); |
| 397 |
394 for (GSList* it = current_line_->runs; it; it = it->next) { | 398 for (GSList* it = current_line_->runs; it; it = it->next) { |
395 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); | 399 PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); |
396 int glyph_count = run->glyphs->num_glyphs; | 400 int glyph_count = run->glyphs->num_glyphs; |
397 if (glyph_count == 0) | 401 if (glyph_count == 0) |
398 continue; | 402 continue; |
399 | 403 |
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( | 404 ScopedPangoFontDescription desc( |
416 pango_font_describe(run->item->analysis.font)); | 405 pango_font_describe(run->item->analysis.font)); |
417 | 406 |
418 const std::string family_name = | 407 const std::string family_name = |
419 pango_font_description_get_family(desc.get()); | 408 pango_font_description_get_family(desc.get()); |
420 renderer.SetTextSize(GetPangoFontSizeInPixels(desc.get())); | 409 renderer.SetTextSize(GetPangoFontSizeInPixels(desc.get())); |
421 | 410 |
422 SkScalar glyph_x = x; | 411 SkScalar glyph_x = x; |
423 SkScalar start_x = x; | 412 SkScalar start_x = x; |
424 int start = 0; | 413 int start = 0; |
425 | 414 |
426 glyphs.resize(glyph_count); | 415 glyphs.resize(glyph_count); |
427 pos.resize(glyph_count); | 416 pos.resize(glyph_count); |
428 | 417 |
429 for (int i = 0; i < glyph_count; ++i) { | 418 for (int i = 0; i < glyph_count; ++i) { |
430 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; | 419 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; |
431 glyphs[i] = static_cast<uint16>(glyph.glyph); | 420 glyphs[i] = static_cast<uint16>(glyph.glyph); |
432 // Use pango_units_to_double() rather than PANGO_PIXELS() here so that | 421 // Use pango_units_to_double() rather than PANGO_PIXELS() here so units |
433 // units won't get rounded to the pixel grid if we're using subpixel | 422 // are not rounded to the pixel grid if subpixel positioning is enabled. |
434 // positioning. | |
435 pos[i].set(glyph_x + pango_units_to_double(glyph.geometry.x_offset), | 423 pos[i].set(glyph_x + pango_units_to_double(glyph.geometry.x_offset), |
436 y + pango_units_to_double(glyph.geometry.y_offset)); | 424 y + pango_units_to_double(glyph.geometry.y_offset)); |
437 | 425 |
438 // If this glyph is beyond the current style, draw the glyphs so far and | 426 // Find the end of the current ranged style. If this glyph is beyond the |
439 // advance to the next style. | 427 // current style, draw the glyphs so far and advance to the next style. |
440 size_t glyph_byte_index = run_start + run->glyphs->log_clusters[i]; | 428 size_t glyph_index = run->item->offset + run->glyphs->log_clusters[i]; |
441 DCHECK_GE(style, 0); | 429 const size_t style_end_index = GetNextBreak(color, style); |
442 DCHECK_LT(style, static_cast<int>(styles.size())); | 430 if (glyph_index >= style_end_index) { |
443 if (!IndexInRange(style_ranges_utf8[style], glyph_byte_index)) { | |
444 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph | 431 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph |
445 // but can span multiple styles, Pango splits the | 432 // but can span multiple styles, Pango splits the |
446 // styles evenly over the glyph. We can do this too by | 433 // styles evenly over the glyph. We can do this too by |
447 // clipping and drawing the glyph several times. | 434 // clipping and drawing the glyph several times. |
448 renderer.SetForegroundColor(styles[style].foreground); | 435 renderer.SetForegroundColor(color->second); |
449 renderer.SetFontFamilyWithStyle(family_name, styles[style].font_style); | 436 const int font_style = (style[BOLD]->second ? Font::BOLD : 0) | |
| 437 (style[ITALIC]->second ? Font::ITALIC : 0); |
| 438 renderer.SetFontFamilyWithStyle(family_name, font_style); |
450 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); | 439 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); |
451 if (styles[style].underline) | 440 if (style[UNDERLINE]->second) |
452 SetPangoUnderlineMetrics(desc.get(), &renderer); | 441 SetPangoUnderlineMetrics(desc.get(), &renderer); |
453 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | 442 renderer.DrawDecorations(start_x, y, glyph_x - start_x, |
| 443 style[UNDERLINE]->second, style[STRIKE]->second, |
| 444 style[DIAGONAL_STRIKE]->second); |
454 | 445 |
455 start = i; | 446 start = i; |
456 start_x = glyph_x; | 447 start_x = glyph_x; |
457 // Loop to find the next style, in case the glyph spans multiple styles. | 448 AdvanceIterators(style_end_index, &color, style); |
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 } | 449 } |
463 | 450 |
464 glyph_x += pango_units_to_double(glyph.geometry.width); | 451 glyph_x += pango_units_to_double(glyph.geometry.width); |
465 } | 452 } |
466 | 453 |
467 // Draw the remaining glyphs. | 454 // Draw the remaining glyphs. |
468 renderer.SetForegroundColor(styles[style].foreground); | 455 renderer.SetForegroundColor(color->second); |
469 renderer.SetFontFamilyWithStyle(family_name, styles[style].font_style); | 456 const int font_style = (style[BOLD]->second ? Font::BOLD : 0) | |
| 457 (style[ITALIC]->second ? Font::ITALIC : 0); |
| 458 renderer.SetFontFamilyWithStyle(family_name, font_style); |
470 renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start); | 459 renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start); |
471 if (styles[style].underline) | 460 if (style[UNDERLINE]->second) |
472 SetPangoUnderlineMetrics(desc.get(), &renderer); | 461 SetPangoUnderlineMetrics(desc.get(), &renderer); |
473 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | 462 renderer.DrawDecorations(start_x, y, glyph_x - start_x, |
| 463 style[UNDERLINE]->second, style[STRIKE]->second, |
| 464 style[DIAGONAL_STRIKE]->second); |
| 465 |
474 x = glyph_x; | 466 x = glyph_x; |
475 } | 467 } |
| 468 |
| 469 // Undo the temporarily applied composition underlines and selection colors. |
| 470 UndoCompositionAndSelectionStyles(); |
476 } | 471 } |
477 | 472 |
478 GSList* RenderTextLinux::GetRunContainingCaret( | 473 GSList* RenderTextLinux::GetRunContainingCaret( |
479 const SelectionModel& caret) const { | 474 const SelectionModel& caret) const { |
480 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); | 475 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); |
481 LogicalCursorDirection affinity = caret.caret_affinity(); | 476 LogicalCursorDirection affinity = caret.caret_affinity(); |
482 GSList* run = current_line_->runs; | 477 GSList* run = current_line_->runs; |
483 while (run) { | 478 while (run) { |
484 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 479 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
485 ui::Range item_range(item->offset, item->offset + item->length); | 480 ui::Range item_range(item->offset, item->offset + item->length); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 if (selection_visual_bounds_.empty()) | 531 if (selection_visual_bounds_.empty()) |
537 selection_visual_bounds_ = CalculateSubstringBounds(selection()); | 532 selection_visual_bounds_ = CalculateSubstringBounds(selection()); |
538 return selection_visual_bounds_; | 533 return selection_visual_bounds_; |
539 } | 534 } |
540 | 535 |
541 RenderText* RenderText::CreateInstance() { | 536 RenderText* RenderText::CreateInstance() { |
542 return new RenderTextLinux; | 537 return new RenderTextLinux; |
543 } | 538 } |
544 | 539 |
545 } // namespace gfx | 540 } // namespace gfx |
OLD | NEW |