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 current glyph and the glyph at the start of its styled range. |
430 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; | 408 int glyph_index = 0; |
431 glyphs[i] = static_cast<uint16>(glyph.glyph); | 409 int style_start_glyph_index = glyph_index; |
432 // Use pango_units_to_double() rather than PANGO_PIXELS() here so that | |
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 | 410 |
438 // If this glyph is beyond the current style, draw the glyphs so far and | 411 // Track the x-coordinates for each styled range (|x| marks the current). |
439 // advance to the next style. | 412 SkScalar style_start_x = x; |
440 size_t glyph_byte_index = run_start + run->glyphs->log_clusters[i]; | 413 |
441 DCHECK_GE(style, 0); | 414 // Track the current style and its text (not layout) index range. |
442 DCHECK_LT(style, static_cast<int>(styles.size())); | 415 style.UpdatePosition(GetGlyphTextIndex(run, style_start_glyph_index)); |
443 if (!IndexInRange(style_ranges_utf8[style], glyph_byte_index)) { | 416 ui::Range style_range = style.GetRange(); |
| 417 |
| 418 do { |
| 419 const PangoGlyphInfo& glyph = run->glyphs->glyphs[glyph_index]; |
| 420 glyphs[glyph_index] = static_cast<uint16>(glyph.glyph); |
| 421 // Use pango_units_to_double() rather than PANGO_PIXELS() here, so units |
| 422 // are not rounded to the pixel grid if subpixel positioning is enabled. |
| 423 pos[glyph_index].set(x + pango_units_to_double(glyph.geometry.x_offset), |
| 424 y + pango_units_to_double(glyph.geometry.y_offset)); |
| 425 x += pango_units_to_double(glyph.geometry.width); |
| 426 |
| 427 ++glyph_index; |
| 428 const size_t glyph_text_index = (glyph_index == glyph_count) ? |
| 429 style_range.end() : GetGlyphTextIndex(run, glyph_index); |
| 430 if (!IndexInRange(style_range, glyph_text_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(style.color()); |
449 renderer.SetFontFamilyWithStyle(family_name, styles[style].font_style); | 436 const int font_style = (style.style(BOLD) ? Font::BOLD : 0) | |
450 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); | 437 (style.style(ITALIC) ? Font::ITALIC : 0); |
451 if (styles[style].underline) | 438 renderer.SetFontFamilyWithStyle(family_name, font_style); |
| 439 renderer.DrawPosText(&pos[style_start_glyph_index], |
| 440 &glyphs[style_start_glyph_index], |
| 441 glyph_index - style_start_glyph_index); |
| 442 if (style.style(UNDERLINE)) |
452 SetPangoUnderlineMetrics(desc.get(), &renderer); | 443 SetPangoUnderlineMetrics(desc.get(), &renderer); |
453 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); | 444 renderer.DrawDecorations(style_start_x, y, x - style_start_x, |
| 445 style.style(UNDERLINE), style.style(STRIKE), |
| 446 style.style(DIAGONAL_STRIKE)); |
| 447 style.UpdatePosition(glyph_text_index); |
| 448 style_range = style.GetRange(); |
| 449 style_start_glyph_index = glyph_index; |
| 450 style_start_x = x; |
| 451 } |
| 452 } while (glyph_index < glyph_count); |
| 453 } |
454 | 454 |
455 start = i; | 455 // Undo the temporarily applied composition underlines and selection colors. |
456 start_x = glyph_x; | 456 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 } | 457 } |
477 | 458 |
478 GSList* RenderTextLinux::GetRunContainingCaret( | 459 GSList* RenderTextLinux::GetRunContainingCaret( |
479 const SelectionModel& caret) const { | 460 const SelectionModel& caret) const { |
480 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); | 461 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); |
481 LogicalCursorDirection affinity = caret.caret_affinity(); | 462 LogicalCursorDirection affinity = caret.caret_affinity(); |
482 GSList* run = current_line_->runs; | 463 GSList* run = current_line_->runs; |
483 while (run) { | 464 while (run) { |
484 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | 465 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; |
485 ui::Range item_range(item->offset, item->offset + item->length); | 466 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); | 512 g_free(ranges); |
532 return bounds; | 513 return bounds; |
533 } | 514 } |
534 | 515 |
535 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 516 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
536 if (selection_visual_bounds_.empty()) | 517 if (selection_visual_bounds_.empty()) |
537 selection_visual_bounds_ = CalculateSubstringBounds(selection()); | 518 selection_visual_bounds_ = CalculateSubstringBounds(selection()); |
538 return selection_visual_bounds_; | 519 return selection_visual_bounds_; |
539 } | 520 } |
540 | 521 |
| 522 size_t RenderTextLinux::GetGlyphTextIndex(PangoLayoutRun* run, |
| 523 int glyph_index) const { |
| 524 return LayoutIndexToTextIndex(run->item->offset + |
| 525 run->glyphs->log_clusters[glyph_index]); |
| 526 } |
| 527 |
541 RenderText* RenderText::CreateInstance() { | 528 RenderText* RenderText::CreateInstance() { |
542 return new RenderTextLinux; | 529 return new RenderTextLinux; |
543 } | 530 } |
544 | 531 |
545 } // namespace gfx | 532 } // namespace gfx |
OLD | NEW |