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

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

Issue 7511029: Implement Pango RenderText for Linux. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: sync, fix couple of bugs Created 9 years, 3 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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>
8
9 #include <algorithm>
10
11 #include "base/logging.h"
12 #include "ui/gfx/canvas_skia.h"
13 #include "ui/gfx/pango_util.h"
14 #include "unicode/uchar.h"
15 #include "unicode/ustring.h"
16
17 namespace {
18
19 // TODO(xji): instead of converting each R or G or B from 8-bit to 16-bit,
20 // it should also massage A in the conversion.
behdad_google 2011/09/06 16:05:03 Right. Do we have the background color around? I
xji 2011/09/06 19:54:13 Thanks for the offering. Looks like the background
21 int ConvertColorFrom8BitTo16Bit(int c) {
22 return (c << 8) + c;
23 }
24
25 } // namespace
26
27 // TODO(xji): index saved in upper layer is utf16 index. Pango uses utf8 index.
28 // Since caret_pos is used internally, we could save utf8 index for caret_pos
29 // to avoid conversion.
30
7 namespace gfx { 31 namespace gfx {
8 32
9 RenderTextLinux::RenderTextLinux() 33 RenderTextLinux::RenderTextLinux()
10 : RenderText() { 34 : layout_(NULL),
35 layout_line_(NULL) {
11 } 36 }
12 37
13 RenderTextLinux::~RenderTextLinux() { 38 RenderTextLinux::~RenderTextLinux() {
39 ResetLayout();
14 } 40 }
15 41
16 RenderText* RenderText::CreateRenderText() { 42 RenderText* RenderText::CreateRenderText() {
17 return new RenderTextLinux; 43 return new RenderTextLinux;
18 } 44 }
19 45
46 void RenderTextLinux::SetText(const string16& text) {
47 RenderText::SetText(text);
48 ResetLayout();
49 }
50
51 void RenderTextLinux::SetDisplayRect(const Rect&r) {
52 RenderText::SetDisplayRect(r);
53 ResetLayout();
54 }
55
56 void RenderTextLinux::SetCompositionRange(const ui::Range& composition_range) {
57 RenderText::SetCompositionRange(composition_range);
58 ResetLayout();
59 }
60
61 void RenderTextLinux::ApplyStyleRange(StyleRange style_range) {
62 RenderText::ApplyStyleRange(style_range);
63 ResetLayout();
64 }
65
66 void RenderTextLinux::ApplyDefaultStyle() {
67 RenderText::ApplyDefaultStyle();
68 ResetLayout();
69 }
70
71 base::i18n::TextDirection RenderTextLinux::GetTextDirection() {
72 EnsureLayout();
73
74 const char* layout_text = pango_layout_get_text(layout_);
75 PangoDirection base_dir =
76 pango_find_base_dir(layout_text, strlen(layout_text));
behdad_google 2011/09/06 16:05:03 Just pass -1 for length here.
xji 2011/09/06 19:54:13 Done.
77 if (base_dir == PANGO_DIRECTION_RTL || base_dir == PANGO_DIRECTION_WEAK_RTL)
78 return base::i18n::RIGHT_TO_LEFT;
79 return base::i18n::LEFT_TO_RIGHT;
80 }
81
82 int RenderTextLinux::GetStringWidth() {
83 PangoLayout* layout = EnsureLayout();
84 int width, height;
85 pango_layout_get_pixel_size(layout, &width, &height);
behdad_google 2011/09/06 16:05:03 You can pass NULL for height.
xji 2011/09/06 19:54:13 Done.
86 return width;
87 }
88
89 void RenderTextLinux::Draw(Canvas* canvas) {
90 PangoLayout* layout = EnsureLayout();
91 Rect bounds(display_rect());
92
93 // Clip the canvas to the text display area.
94 CanvasSkia* canvas_skia = canvas->AsCanvasSkia();
95 canvas_skia->ClipRectInt(
96 bounds.x(), bounds.y(), bounds.width(), bounds.height());
97
98 skia::ScopedPlatformPaint scoped_platform_paint(canvas_skia);
99 cairo_t* cr = scoped_platform_paint.GetPlatformSurface();
100 cairo_save(cr);
101 cairo_rectangle(cr, bounds.x(), bounds.y(), bounds.width(), bounds.height());
102 cairo_clip(cr);
behdad_google 2011/09/06 16:05:03 I have a feeling that you shouldn't need the skia
xji 2011/09/06 19:54:13 hm... Looking at CanvasSkia::DrawStringInt() again
103
104 int text_width, text_height;
105 pango_layout_get_pixel_size(layout, &text_width, &text_height);
106 Point offset(ToViewPoint(Point()));
107 // Vertically centered.
108 int text_y = offset.y() + ((bounds.height() - text_height) / 2);
109 // TODO(xji): need to use SkCanvas->drawPosText() for gpu acceleration.
110 cairo_move_to(cr, offset.x(), text_y);
111 pango_cairo_show_layout(cr, layout);
112
113 cairo_restore(cr);
114
115 // Paint cursor.
116 bounds = GetUpdatedCursorBounds();
117 if (cursor_visible() && focused() && !bounds.IsEmpty()) {
118 if (!bounds.IsEmpty())
behdad_google 2011/09/06 16:05:03 The second if is redundant since it's checked in t
xji 2011/09/06 19:54:13 Thanks for catching this. Actually, we do not chec
119 canvas->DrawRectInt(kCursorColor,
120 bounds.x(),
121 bounds.y(),
122 bounds.width(),
123 bounds.height());
124 }
125 }
126
127 SelectionModel RenderTextLinux::FindCursorPosition(const Point& point) {
128 // TODO(xji): when points outside of text, return HOME/END position.
129 PangoLayout* layout = EnsureLayout();
130
131 if (text().empty())
132 return SelectionModel(0, 0, SelectionModel::LEADING);
133
134 Point p(ToTextPoint(point));
135 int caret_pos, trailing;
136 pango_layout_xy_to_index(layout, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE,
137 &caret_pos, &trailing);
138
139 size_t selection_end = caret_pos;
140 if (trailing > 0) {
141 const char* pango_text = pango_layout_get_text(layout);
142 const char* ch = g_utf8_offset_to_pointer(pango_text + caret_pos, trailing);
143 DCHECK_GE(ch, pango_text);
144 DCHECK_LE(ch, pango_text + strlen(pango_text));
145 selection_end = ch - pango_text;
146 }
147
148 return SelectionModel(
149 Utf8IndexToUtf16Index(selection_end),
150 Utf8IndexToUtf16Index(caret_pos),
behdad_google 2011/09/06 16:05:03 I'm lost about what the two indices to SelectionMo
xji 2011/09/06 19:54:13 Hope the comments in render_text.h explains. I wil
151 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING);
152 }
153
154 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection,
155 bool insert_mode) {
behdad_google 2011/09/06 16:05:03 What does insert_mode==FALSE represent exactly? C
xji 2011/09/06 19:54:13 when insert mode is true, cursor is drawn as a ver
156 PangoLayout* layout = EnsureLayout();
157
158 size_t caret_pos = insert_mode ? selection.caret_pos() :
159 selection.selection_end();
160 PangoRectangle pos;
161 pango_layout_index_to_pos(layout, Utf16IndexToUtf8Index(caret_pos), &pos);
162
163 SelectionModel::CaretPlacement caret_placement = selection.caret_placement();
164 int x = pos.x;
165 if ((insert_mode && caret_placement == SelectionModel::TRAILING) ||
166 (!insert_mode && pos.width < 0))
behdad_google 2011/09/06 16:05:03 I can't understand why you need this if(). Why do
xji 2011/09/06 19:54:13 I do not quite understand your comments. As to the
167 x += pos.width;
168 x = x / PANGO_SCALE;
behdad_google 2011/09/06 16:05:03 Use PANGO_PIXELS().
xji 2011/09/06 19:54:13 Done.
169
170 int h = std::min(display_rect().height(), pos.height / PANGO_SCALE);
behdad_google 2011/09/06 16:05:03 Use PANGO_PIXELS().
xji 2011/09/06 19:54:13 Done.
171 Rect bounds(x, (display_rect().height() - h) / 2, 1, h);
172 bounds.set_origin(ToViewPoint(bounds.origin()));
173
174 if (!insert_mode)
175 bounds.set_width(std::abs(pos.width));
176
177 return bounds;
178 }
179
180 SelectionModel RenderTextLinux::GetLeftSelectionModel(
181 const SelectionModel& current,
182 BreakType break_type) {
183 EnsureLayout();
184
185 if (break_type == LINE_BREAK || text().empty())
186 return LeftEndSelectionModel();
187 if (break_type == CHARACTER_BREAK)
188 return LeftSelectionModel(current);
189 // TODO(xji): not implemented yet.
190 return RenderText::GetLeftSelectionModel(current, break_type);
191 }
192
193 SelectionModel RenderTextLinux::GetRightSelectionModel(
194 const SelectionModel& current,
195 BreakType break_type) {
196 EnsureLayout();
197
198 if (break_type == LINE_BREAK || text().empty())
199 return RightEndSelectionModel();
200 if (break_type == CHARACTER_BREAK)
201 return RightSelectionModel(current);
202 // TODO(xji): not implemented yet.
203 return RenderText::GetRightSelectionModel(current, break_type);
204 }
205
206 SelectionModel RenderTextLinux::LeftEndSelectionModel() {
207 if (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) {
208 GSList* first_visual_run = layout_line_->runs;
209 if (first_visual_run) {
behdad_google 2011/09/06 16:05:03 I would change the if to "if (layout_line_->runs)"
xji 2011/09/06 19:54:13 Done.
210 PangoItem* item =
211 reinterpret_cast<PangoLayoutRun*>(first_visual_run->data)->item;
212 if (item->analysis.level % 2 == 0) { // LTR.
behdad_google 2011/09/06 16:05:03 Ok, I've reviewed up to here. Will study the Sele
213 size_t caret = Utf8IndexToUtf16Index(item->offset);
214 return SelectionModel(text().length(), caret, SelectionModel::LEADING);
215 } else { // RTL.
216 size_t caret = Utf16IndexOfAdjacentGrapheme(item->offset + item->length,
217 PREVIOUS);
218 return SelectionModel(text().length(), caret, SelectionModel::TRAILING);
219 }
220 }
221 }
222 return SelectionModel(0, 0, SelectionModel::LEADING);
223 }
224
225 SelectionModel RenderTextLinux::RightEndSelectionModel() {
226 if (GetTextDirection() == base::i18n::LEFT_TO_RIGHT) {
227 GSList* last_visual_run = GetLastRun();
228 if (last_visual_run) {
229 PangoItem* item =
230 reinterpret_cast<PangoLayoutRun*>(last_visual_run->data)->item;
231 if (item->analysis.level % 2 == 0) { // LTR.
232 size_t caret = Utf16IndexOfAdjacentGrapheme(item->offset + item->length,
233 PREVIOUS);
234 return SelectionModel(text().length(), caret, SelectionModel::TRAILING);
235 } else { // RTL.
236 size_t caret = Utf8IndexToUtf16Index(item->offset);
237 return SelectionModel(text().length(), caret, SelectionModel::LEADING);
238 }
239 }
240 }
241 return SelectionModel(0, 0, SelectionModel::LEADING);
242 }
243
244 size_t RenderTextLinux::GetIndexOfPreviousGrapheme(size_t position) {
245 EnsureLayout();
246 size_t index = Utf16IndexToUtf8Index(position);
247 return Utf16IndexOfAdjacentGrapheme(index, PREVIOUS);
248 }
249
250 size_t RenderTextLinux::GetIndexOfNextGrapheme(size_t position) {
251 EnsureLayout();
252 size_t index = Utf16IndexToUtf8Index(position);
253 return Utf16IndexOfAdjacentGrapheme(index, NEXT);
254 }
255
256 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const {
257 GSList* run = layout_line_->runs;
258 while (run) {
259 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item;
260 size_t run_start = Utf8IndexToUtf16Index(box->offset);
261 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length);
262
263 if (position >= run_start && position < run_end)
264 return run;
265 run = run->next;
266 }
267 return NULL;
268 }
269
270 size_t RenderTextLinux::Utf8IndexOfAdjacentGrapheme(
271 size_t utf8_index_of_current_grapheme,
272 RelativeLogicalPosition pos) const {
273 PangoLogAttr* log_attrs;
274 gint n_attrs;
275 pango_layout_get_log_attrs(layout_, &log_attrs, &n_attrs);
276
277 const char* layout_text = pango_layout_get_text(layout_);
278 const char* ch = layout_text + utf8_index_of_current_grapheme;
279 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text, ch));
280
281 if (pos == PREVIOUS) {
282 if (ch > layout_text) {
283 do {
284 ch = g_utf8_find_prev_char(layout_text, ch);
285 --char_offset;
286 } while (ch && *ch && !log_attrs[char_offset].is_cursor_position);
287 if (!ch)
288 ch = layout_text;
289 }
290 } else {
291 if (ch < layout_text + strlen(layout_text)) {
292 do {
293 ch = g_utf8_find_next_char(ch, NULL);
294 ++char_offset;
295 } while (ch && *ch && !log_attrs[char_offset].is_cursor_position);
296 if (!ch || ch >= layout_text + strlen(layout_text))
297 ch = layout_text + strlen(layout_text);
298 }
299 }
300
301 size_t utf8_index = static_cast<size_t>(ch - layout_text);
302 g_free(log_attrs);
303 return utf8_index;
304 }
305
306 size_t RenderTextLinux::Utf16IndexOfAdjacentGrapheme(
307 size_t utf8_index_of_current_grapheme,
308 RelativeLogicalPosition pos) const {
309 size_t utf8_index = Utf8IndexOfAdjacentGrapheme(
310 utf8_index_of_current_grapheme, pos);
311 return Utf8IndexToUtf16Index(utf8_index);
312 }
313
314 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun(
315 const PangoItem* run) const {
316 size_t caret = Utf8IndexToUtf16Index(run->offset);
317 size_t cursor = Utf16IndexOfAdjacentGrapheme(run->offset, NEXT);
318 return SelectionModel(cursor, caret, SelectionModel::TRAILING);
319 }
320
321 SelectionModel RenderTextLinux::LastSelectionModelInsideRun(
322 const PangoItem* run) const {
323 size_t caret = Utf16IndexOfAdjacentGrapheme(run->offset + run->length,
324 PREVIOUS);
325 return SelectionModel(caret, caret, SelectionModel::LEADING);
326 }
327
328 // Assume caret_pos in |current| is n, 'l' represents leading in
329 // caret_placement and 't' represents trailing in caret_placement. Following
330 // is the calculation from (caret_pos, caret_placement) in |current| to
331 // (selection_end, caret_pos, caret_placement) when moving cursor left by
332 // one grapheme (for simplicity, assume each grapheme is one character).
333 // If n is in LTR run,
334 // (n, t) ---> (n, n, l).
335 // (n, l) ---> (n-1, n-1, l) if n is inside run (not at boundary).
336 // (n, l) ---> goto across run case if n is at run boundary.
337 // If n is in RTL run,
338 // (n, l) --> (n+1, n, t).
339 // (n, t) --> (n+2, n+1, t) if n is inside run.
340 // (n, t) --> goto across run case if n is at run boundary.
341 // If n is at run boundary, get its visually left run,
342 // If left run is LTR run,
343 // (n, t) --> (left run's end, left run's end, l).
344 // If left run is RTL run,
345 // (n, t) --> (left run's begin + 1, left run's begin, t).
346 SelectionModel RenderTextLinux::LeftSelectionModel(
347 const SelectionModel& selection) {
348 size_t caret = selection.caret_pos();
349 SelectionModel::CaretPlacement caret_placement = selection.caret_placement();
350 GSList* run = GetRunContainingPosition(caret);
351 DCHECK(run);
352
353 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item;
354 size_t run_start = Utf8IndexToUtf16Index(box->offset);
355 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length);
356
357 if (box->analysis.level % 2 == 0) { // LTR run.
358 if (caret_placement == SelectionModel::TRAILING)
359 return SelectionModel(caret, caret, SelectionModel::LEADING);
360 else if (caret > run_start) {
361 caret = GetIndexOfPreviousGrapheme(caret);
362 return SelectionModel(caret, caret, SelectionModel::LEADING);
363 }
364 } else { // RTL run.
365 if (caret_placement == SelectionModel::LEADING) {
366 size_t cursor = GetIndexOfNextGrapheme(caret);
367 return SelectionModel(cursor, caret, SelectionModel::TRAILING);
368 } else if (selection.selection_end() < run_end) {
369 caret = GetIndexOfNextGrapheme(caret);
370 size_t cursor = GetIndexOfNextGrapheme(caret);
371 return SelectionModel(cursor, caret, SelectionModel::TRAILING);
372 }
373 }
374
375 // The character is at the begin of its run; advance to the previous visual
376 // run.
377 GSList* prev_run = GetPreviousRun(run);
378 if (!prev_run) // TODO(xji): check whether this is expected.
379 return LeftEndSelectionModel();
380
381 box = reinterpret_cast<PangoLayoutRun*>(prev_run->data)->item;
382 return (box->analysis.level % 2) ? FirstSelectionModelInsideRun(box) :
383 LastSelectionModelInsideRun(box);
384 }
385
386 // Assume caret_pos in |current| is n, 'l' represents leading in
387 // caret_placement and 't' represents trailing in caret_placement. Following
388 // is the calculation from (caret_pos, caret_placement) in |current| to
389 // (selection_end, caret_pos, caret_placement) when moving cursor right by
390 // one grapheme (for simplicity, assume each grapheme is one character).
391 // If n is in LTR run,
392 // (n, l) ---> (n+1, n, t).
393 // (n, t) ---> (n+2, n+1, t) if n is inside run (not at boundary).
394 // (n, t) ---> goto across run case if n is at run boundary.
395 // If n is in RTL run,
396 // (n, t) --> (n, n, l).
397 // (n, l) --> (n-1, n-1, l) if n is inside run.
398 // (n, l) --> goto across run case if n is at run boundary.
399 // If n is at run boundary, get its visually right run,
400 // If right run is LTR run,
401 // (n, t) --> (right run's begin + 1, right run's begin, t).
402 // If right run is RTL run,
403 // (n, t) --> (right run's end, right run's end, l).
404 SelectionModel RenderTextLinux::RightSelectionModel(
405 const SelectionModel& selection) {
406 size_t caret = selection.caret_pos();
407 SelectionModel::CaretPlacement caret_placement = selection.caret_placement();
408 GSList* run = GetRunContainingPosition(caret);
409 DCHECK(run);
410
411 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item;
412 size_t run_start = Utf8IndexToUtf16Index(box->offset);
413 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length);
414
415 if (box->analysis.level % 2 == 0) { // LTR run.
416 if (caret_placement == SelectionModel::LEADING) {
417 size_t cursor = GetIndexOfNextGrapheme(caret);
418 return SelectionModel(cursor, caret, SelectionModel::TRAILING);
419 } else if (selection.selection_end() < run_end) {
420 caret = GetIndexOfNextGrapheme(caret);
421 size_t cursor = GetIndexOfNextGrapheme(caret);
422 return SelectionModel(cursor, caret, SelectionModel::TRAILING);
423 }
424 } else { // RTL run.
425 if (caret_placement == SelectionModel::TRAILING)
426 return SelectionModel(caret, caret, SelectionModel::LEADING);
427 else if (caret > run_start) {
428 caret = GetIndexOfPreviousGrapheme(caret);
429 return SelectionModel(caret, caret, SelectionModel::LEADING);
430 }
431 }
432
433 // The character is at the end of its run; advance to the next visual run.
434 GSList* next_run = run->next;
435 if (!next_run) // TODO(xji): what is the expected behavior?
436 return RightEndSelectionModel();
437
438 box = reinterpret_cast<PangoLayoutRun*>(next_run->data)->item;
439 return (box->analysis.level % 2) ? LastSelectionModelInsideRun(box) :
440 FirstSelectionModelInsideRun(box);
441 }
442
443 PangoLayout* RenderTextLinux::EnsureLayout() {
444 if (layout_ == NULL) {
445 CanvasSkia canvas(display_rect().width(), display_rect().height(), false);
446 skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
447 cairo_t* cr = scoped_platform_paint.GetPlatformSurface();
448
449 layout_ = pango_cairo_create_layout(cr);
450 SetupPangoLayout(
451 layout_,
452 text(),
453 default_style().font,
454 display_rect().width(),
455 base::i18n::GetFirstStrongCharacterDirection(text()),
456 CanvasSkia::DefaultCanvasTextAlignment());
457
458 pango_layout_set_width(layout_, -1);
459 pango_layout_set_height(layout_, display_rect().height() * PANGO_SCALE);
460 SetupPangoAttributes(layout_);
461
462 layout_line_ = pango_layout_get_line_readonly(layout_, 0);
463 pango_layout_line_ref(layout_line_);
464 }
465 return layout_;
466 }
467
468 void RenderTextLinux::ResetLayout() {
469 // set_cached_bounds_and_offset_valid(false) is done in RenderText for every
470 // operation that triggers ResetLayout().
471 if (layout_) {
472 g_object_unref(layout_);
473 layout_ = NULL;
474 }
475 if (layout_line_) {
476 pango_layout_line_unref(layout_line_);
477 layout_line_ = NULL;
478 }
479 }
480
481 void RenderTextLinux::SetupPangoAttributes(PangoLayout* layout) {
482 PangoAttrList* attrs = pango_attr_list_new();
483 // Set selection background color.
484 SkColor selection_color =
485 focused() ? kFocusedSelectionColor : kUnfocusedSelectionColor;
486 size_t start = std::min(MinOfSelection(), text().length());
487 size_t end = std::min(MaxOfSelection(), text().length());
488 PangoAttribute* pango_attr;
489 if (end > start) {
490 pango_attr = pango_attr_background_new(
491 ConvertColorFrom8BitTo16Bit(SkColorGetR(selection_color)),
492 ConvertColorFrom8BitTo16Bit(SkColorGetG(selection_color)),
493 ConvertColorFrom8BitTo16Bit(SkColorGetB(selection_color)));
494 AppendPangoAttribute(start, end, pango_attr, attrs);
495 }
496
497 StyleRanges ranges_of_style(style_ranges());
498 ApplyCompositionAndSelectionStyles(&ranges_of_style);
499
500 for (StyleRanges::const_iterator i = ranges_of_style.begin();
501 i < ranges_of_style.end(); ++i) {
502 start = std::min(i->range.start(), text().length());
503 end = std::min(i->range.end(), text().length());
504 if (start >= end)
505 continue;
506
507 const Font& font = !i->underline ? i->font :
508 i->font.DeriveFont(0, i->font.GetStyle() | Font::UNDERLINED);
509 PangoFontDescription* desc = font.GetNativeFont();
510 pango_attr = pango_attr_font_desc_new(desc);
511 AppendPangoAttribute(start, end, pango_attr, attrs);
512 pango_font_description_free(desc);
513
514 SkColor foreground = i->foreground;
515 pango_attr = pango_attr_foreground_new(
516 ConvertColorFrom8BitTo16Bit(SkColorGetR(foreground)),
517 ConvertColorFrom8BitTo16Bit(SkColorGetG(foreground)),
518 ConvertColorFrom8BitTo16Bit(SkColorGetB(foreground)));
519 AppendPangoAttribute(start, end, pango_attr, attrs);
520
521 if (i->strike) {
522 pango_attr = pango_attr_strikethrough_new(true);
523 AppendPangoAttribute(start, end, pango_attr, attrs);
524 }
525 }
526
527 pango_layout_set_attributes(layout, attrs);
528 pango_attr_list_unref(attrs);
529 }
530
531 void RenderTextLinux::AppendPangoAttribute(size_t start,
532 size_t end,
533 PangoAttribute* pango_attr,
534 PangoAttrList* attrs) {
535 pango_attr->start_index = Utf16IndexToUtf8Index(start);
536 pango_attr->end_index = Utf16IndexToUtf8Index(end);
537 pango_attr_list_insert(attrs, pango_attr);
538 }
539
540 GSList* RenderTextLinux::GetPreviousRun(GSList* run) const {
541 GSList* current = layout_line_->runs;
542 GSList* prev = NULL;
543 while (current) {
544 if (current == run)
545 return prev;
546 prev = current;
547 current = current->next;
548 }
549 return NULL;
550 }
551
552 GSList* RenderTextLinux::GetLastRun() const {
553 GSList* current = layout_line_->runs;
554 while (current && current->next) {
555 current = current->next;
556 }
557 return current;
558 }
559
560 size_t RenderTextLinux::Utf16IndexToUtf8Index(size_t index) const {
561 int32_t utf8_index = 0;
562 UErrorCode ec = U_ZERO_ERROR;
563 u_strToUTF8(NULL, 0, &utf8_index, text().data(), index, &ec);
564 // Even given a destination buffer as NULL and destination capacity as 0,
565 // if the output length is equal to or greater than the capacity, then the
566 // UErrorCode is set to U_STRING_NOT_TERMINATED_WARNING or
567 // U_BUFFER_OVERFLOW_ERROR respectively.
568 // Please refer to
569 // http://userguide.icu-project.org/strings#TOC-Using-C-Strings:-NUL-Terminate d-vs
570 // for detail (search for "Note that" below "Preflighting").
571 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR ||
572 ec == U_STRING_NOT_TERMINATED_WARNING);
573 return utf8_index;
574 }
575
576 size_t RenderTextLinux::Utf8IndexToUtf16Index(size_t index) const {
577 int32_t utf16_index = 0;
578 UErrorCode ec = U_ZERO_ERROR;
579 const char* layout_text = pango_layout_get_text(layout_);
580 u_strFromUTF8(NULL, 0, &utf16_index, layout_text, index, &ec);
581 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR ||
582 ec == U_STRING_NOT_TERMINATED_WARNING);
583 return utf16_index;
584 }
585
20 } // namespace gfx 586 } // namespace gfx
OLDNEW
« ui/gfx/render_text_linux.h ('K') | « ui/gfx/render_text_linux.h ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698