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 <fontconfig/fontconfig.h> | 7 #include <fontconfig/fontconfig.h> |
8 #include <pango/pangocairo.h> | 8 #include <pango/pangocairo.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 std::vector<RenderText::FontSpan> RenderTextLinux::GetFontSpansForTesting() { | 141 std::vector<RenderText::FontSpan> RenderTextLinux::GetFontSpansForTesting() { |
142 EnsureLayout(); | 142 EnsureLayout(); |
143 | 143 |
144 std::vector<RenderText::FontSpan> spans; | 144 std::vector<RenderText::FontSpan> spans; |
145 for (GSList* it = current_line_->runs; it; it = it->next) { | 145 for (GSList* it = current_line_->runs; it; it = it->next) { |
146 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(it->data)->item; | 146 PangoItem* item = reinterpret_cast<PangoLayoutRun*>(it->data)->item; |
147 const int start = LayoutIndexToTextIndex(item->offset); | 147 const int start = LayoutIndexToTextIndex(item->offset); |
148 const int end = LayoutIndexToTextIndex(item->offset + item->length); | 148 const int end = LayoutIndexToTextIndex(item->offset + item->length); |
149 const ui::Range range(start, end); | 149 const ui::Range range(start, end); |
150 | 150 |
151 PangoFontDescription* native_font = | 151 ScopedPangoFontDescription desc(pango_font_describe(item->analysis.font)); |
152 pango_font_describe(item->analysis.font); | 152 spans.push_back(RenderText::FontSpan(Font(desc.get()), range)); |
153 spans.push_back(RenderText::FontSpan(Font(native_font), range)); | |
154 pango_font_description_free(native_font); | |
155 } | 153 } |
156 | 154 |
157 return spans; | 155 return spans; |
158 } | 156 } |
159 | 157 |
160 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( | 158 SelectionModel RenderTextLinux::AdjacentCharSelectionModel( |
161 const SelectionModel& selection, | 159 const SelectionModel& selection, |
162 VisualCursorDirection direction) { | 160 VisualCursorDirection direction) { |
163 GSList* run = GetRunContainingCaret(selection); | 161 GSList* run = GetRunContainingCaret(selection); |
164 if (!run) { | 162 if (!run) { |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 for (StyleRanges::const_iterator i = style_ranges().begin(); | 332 for (StyleRanges::const_iterator i = style_ranges().begin(); |
335 i < style_ranges().end(); ++i) { | 333 i < style_ranges().end(); ++i) { |
336 // In Pango, different fonts means different runs, and it breaks Arabic | 334 // In Pango, different fonts means different runs, and it breaks Arabic |
337 // shaping across run boundaries. So, set font only when it is different | 335 // shaping across run boundaries. So, set font only when it is different |
338 // from the default font. | 336 // from the default font. |
339 // TODO(xji): We'll eventually need to split up StyleRange into components | 337 // TODO(xji): We'll eventually need to split up StyleRange into components |
340 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges | 338 // (ColorRange, FontRange, etc.) so that we can combine adjacent ranges |
341 // with the same Fonts (to avoid unnecessarily splitting up runs). | 339 // with the same Fonts (to avoid unnecessarily splitting up runs). |
342 if (i->font_style != default_font_style) { | 340 if (i->font_style != default_font_style) { |
343 FontList derived_font_list = font_list().DeriveFontList(i->font_style); | 341 FontList derived_font_list = font_list().DeriveFontList(i->font_style); |
344 PangoFontDescription* desc = pango_font_description_from_string( | 342 ScopedPangoFontDescription desc(pango_font_description_from_string( |
345 derived_font_list.GetFontDescriptionString().c_str()); | 343 derived_font_list.GetFontDescriptionString().c_str())); |
346 | 344 |
347 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc); | 345 PangoAttribute* pango_attr = pango_attr_font_desc_new(desc.get()); |
348 pango_attr->start_index = TextIndexToLayoutIndex(i->range.start()); | 346 pango_attr->start_index = TextIndexToLayoutIndex(i->range.start()); |
349 pango_attr->end_index = TextIndexToLayoutIndex(i->range.end()); | 347 pango_attr->end_index = TextIndexToLayoutIndex(i->range.end()); |
350 pango_attr_list_insert(attrs, pango_attr); | 348 pango_attr_list_insert(attrs, pango_attr); |
351 pango_font_description_free(desc); | |
352 } | 349 } |
353 } | 350 } |
354 | 351 |
355 pango_layout_set_attributes(layout, attrs); | 352 pango_layout_set_attributes(layout, attrs); |
356 pango_attr_list_unref(attrs); | 353 pango_attr_list_unref(attrs); |
357 } | 354 } |
358 | 355 |
359 void RenderTextLinux::DrawVisualText(Canvas* canvas) { | 356 void RenderTextLinux::DrawVisualText(Canvas* canvas) { |
360 DCHECK(layout_); | 357 DCHECK(layout_); |
361 | 358 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // TODO(asvitkine): Can we avoid looping here, e.g. by caching this per run? | 401 // TODO(asvitkine): Can we avoid looping here, e.g. by caching this per run? |
405 int style = -1; | 402 int style = -1; |
406 for (size_t i = 0; i < style_ranges_utf8.size(); ++i) { | 403 for (size_t i = 0; i < style_ranges_utf8.size(); ++i) { |
407 if (IndexInRange(style_ranges_utf8[i], first_glyph_byte_index)) { | 404 if (IndexInRange(style_ranges_utf8[i], first_glyph_byte_index)) { |
408 style = i; | 405 style = i; |
409 break; | 406 break; |
410 } | 407 } |
411 } | 408 } |
412 DCHECK_GE(style, 0); | 409 DCHECK_GE(style, 0); |
413 | 410 |
414 PangoFontDescription* native_font = | 411 ScopedPangoFontDescription desc( |
415 pango_font_describe(run->item->analysis.font); | 412 pango_font_describe(run->item->analysis.font)); |
416 | 413 |
417 const std::string family_name = | 414 const std::string family_name = |
418 pango_font_description_get_family(native_font); | 415 pango_font_description_get_family(desc.get()); |
419 renderer.SetTextSize(GetPangoFontSizeInPixels(native_font)); | 416 renderer.SetTextSize(GetPangoFontSizeInPixels(desc.get())); |
420 | |
421 pango_font_description_free(native_font); | |
422 | 417 |
423 SkScalar glyph_x = x; | 418 SkScalar glyph_x = x; |
424 SkScalar start_x = x; | 419 SkScalar start_x = x; |
425 int start = 0; | 420 int start = 0; |
426 | 421 |
427 glyphs.resize(glyph_count); | 422 glyphs.resize(glyph_count); |
428 pos.resize(glyph_count); | 423 pos.resize(glyph_count); |
429 | 424 |
430 for (int i = 0; i < glyph_count; ++i) { | 425 for (int i = 0; i < glyph_count; ++i) { |
431 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; | 426 const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 return bounds; | 541 return bounds; |
547 } | 542 } |
548 | 543 |
549 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { | 544 std::vector<Rect> RenderTextLinux::GetSelectionBounds() { |
550 if (selection_visual_bounds_.empty()) | 545 if (selection_visual_bounds_.empty()) |
551 selection_visual_bounds_ = CalculateSubstringBounds(selection()); | 546 selection_visual_bounds_ = CalculateSubstringBounds(selection()); |
552 return selection_visual_bounds_; | 547 return selection_visual_bounds_; |
553 } | 548 } |
554 | 549 |
555 } // namespace gfx | 550 } // namespace gfx |
OLD | NEW |