Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: ui/gfx/render_text_linux.cc

Issue 11535014: Replace StyleRange with BreakList; update RenderText, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update iteration and helpers; add tests; etc. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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);
406 int glyph_start = 0;
407 int glyph_end = 0;
Alexei Svitkine (slow) 2013/01/31 16:33:34 Can you add a comment explaining what glyph_start
msw 2013/01/31 20:27:39 Done.
408 SkScalar x_end = x;
409 style.UpdatePosition(LayoutIndexToTextIndex(
Alexei Svitkine (slow) 2013/01/31 16:33:34 You seem to use the following pattern a lot in thi
msw 2013/01/31 20:27:39 Done. It's only used 3 times, but I obliged.
410 run->item->offset + run->glyphs->log_clusters[glyph_start]));
428 411
429 for (int i = 0; i < glyph_count; ++i) { 412 do {
430 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; 413 const PangoGlyphInfo& glyph = run->glyphs->glyphs[glyph_end];
431 glyphs[i] = static_cast<uint16>(glyph.glyph); 414 glyphs[glyph_end] = static_cast<uint16>(glyph.glyph);
432 // Use pango_units_to_double() rather than PANGO_PIXELS() here so that 415 // 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 416 // are not rounded to the pixel grid if subpixel positioning is enabled.
434 // positioning. 417 pos[glyph_end].set(x_end + pango_units_to_double(glyph.geometry.x_offset),
435 pos[i].set(glyph_x + pango_units_to_double(glyph.geometry.x_offset), 418 y + pango_units_to_double(glyph.geometry.y_offset));
436 y + pango_units_to_double(glyph.geometry.y_offset)); 419 x_end += pango_units_to_double(glyph.geometry.width);
437 420
438 // If this glyph is beyond the current style, draw the glyphs so far and 421 ++glyph_end;
439 // advance to the next style. 422 const size_t i = (glyph_end == glyph_count) ? style.GetRange().end() :
Alexei Svitkine (slow) 2013/01/31 16:33:34 Nit: Change |i| to |glyph_text_index|?
Alexei Svitkine (slow) 2013/01/31 16:33:34 Cache result of style.GetRange() since you're usin
msw 2013/01/31 20:27:39 Done.
msw 2013/01/31 20:27:39 Done.
440 size_t glyph_byte_index = run_start + run->glyphs->log_clusters[i]; 423 LayoutIndexToTextIndex(run->item->offset +
441 DCHECK_GE(style, 0); 424 run->glyphs->log_clusters[glyph_end]);
442 DCHECK_LT(style, static_cast<int>(styles.size())); 425 if (i < style.GetRange().start() || i >= style.GetRange().end()) {
Alexei Svitkine (slow) 2013/01/31 16:33:34 Can you use !IndexInRange(style.GetRange(), i) lik
msw 2013/01/31 20:27:39 Done.
443 if (!IndexInRange(style_ranges_utf8[style], glyph_byte_index)) {
444 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph 426 // TODO(asvitkine): For cases like "fi", where "fi" is a single glyph
445 // but can span multiple styles, Pango splits the 427 // but can span multiple styles, Pango splits the
446 // styles evenly over the glyph. We can do this too by 428 // styles evenly over the glyph. We can do this too by
447 // clipping and drawing the glyph several times. 429 // clipping and drawing the glyph several times.
448 renderer.SetForegroundColor(styles[style].foreground); 430 style.UpdatePosition(LayoutIndexToTextIndex(
449 renderer.SetFontFamilyWithStyle(family_name, styles[style].font_style); 431 run->item->offset + run->glyphs->log_clusters[glyph_start]));
450 renderer.DrawPosText(&pos[start], &glyphs[start], i - start); 432 renderer.SetForegroundColor(style.color());
451 if (styles[style].underline) 433 const int font_style = (style.style(BOLD) ? Font::BOLD : 0) |
434 (style.style(ITALIC) ? Font::ITALIC : 0);
435 renderer.SetFontFamilyWithStyle(family_name, font_style);
436 renderer.DrawPosText(&pos[glyph_start], &glyphs[glyph_start],
437 glyph_end - glyph_start);
438 if (style.style(UNDERLINE))
452 SetPangoUnderlineMetrics(desc.get(), &renderer); 439 SetPangoUnderlineMetrics(desc.get(), &renderer);
453 renderer.DrawDecorations(start_x, y, glyph_x - start_x, styles[style]); 440 renderer.DrawDecorations(x, y, x_end - x,
441 style.style(UNDERLINE), style.style(STRIKE),
442 style.style(DIAGONAL_STRIKE));
443 glyph_start = glyph_end;
444 x = x_end;
445 }
446 } while (glyph_end < glyph_count);
447 }
454 448
455 start = i; 449 // Undo the temporarily applied composition underlines and selection colors.
456 start_x = glyph_x; 450 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 } 451 }
477 452
478 GSList* RenderTextLinux::GetRunContainingCaret( 453 GSList* RenderTextLinux::GetRunContainingCaret(
479 const SelectionModel& caret) const { 454 const SelectionModel& caret) const {
480 size_t position = TextIndexToLayoutIndex(caret.caret_pos()); 455 size_t position = TextIndexToLayoutIndex(caret.caret_pos());
481 LogicalCursorDirection affinity = caret.caret_affinity(); 456 LogicalCursorDirection affinity = caret.caret_affinity();
482 GSList* run = current_line_->runs; 457 GSList* run = current_line_->runs;
483 while (run) { 458 while (run) {
484 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item; 459 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(run->data)->item;
485 ui::Range item_range(item->offset, item->offset + item->length); 460 ui::Range item_range(item->offset, item->offset + item->length);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 if (selection_visual_bounds_.empty()) 511 if (selection_visual_bounds_.empty())
537 selection_visual_bounds_ = CalculateSubstringBounds(selection()); 512 selection_visual_bounds_ = CalculateSubstringBounds(selection());
538 return selection_visual_bounds_; 513 return selection_visual_bounds_;
539 } 514 }
540 515
541 RenderText* RenderText::CreateInstance() { 516 RenderText* RenderText::CreateInstance() {
542 return new RenderTextLinux; 517 return new RenderTextLinux;
543 } 518 }
544 519
545 } // namespace gfx 520 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698