OLD | NEW |
---|---|
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. | |
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 = pango_find_base_dir(layout_text, -1); | |
76 if (base_dir == PANGO_DIRECTION_RTL || base_dir == PANGO_DIRECTION_WEAK_RTL) | |
77 return base::i18n::RIGHT_TO_LEFT; | |
78 return base::i18n::LEFT_TO_RIGHT; | |
79 } | |
80 | |
81 int RenderTextLinux::GetStringWidth() { | |
82 PangoLayout* layout = EnsureLayout(); | |
83 int width; | |
84 pango_layout_get_pixel_size(layout, &width, NULL); | |
85 return width; | |
86 } | |
87 | |
88 void RenderTextLinux::Draw(Canvas* canvas) { | |
89 PangoLayout* layout = EnsureLayout(); | |
90 Rect bounds(display_rect()); | |
91 | |
92 // Clip the canvas to the text display area. | |
93 CanvasSkia* canvas_skia = canvas->AsCanvasSkia(); | |
94 canvas_skia->ClipRectInt( | |
95 bounds.x(), bounds.y(), bounds.width(), bounds.height()); | |
96 | |
97 skia::ScopedPlatformPaint scoped_platform_paint(canvas_skia); | |
98 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | |
99 cairo_save(cr); | |
100 cairo_rectangle(cr, bounds.x(), bounds.y(), bounds.width(), bounds.height()); | |
101 cairo_clip(cr); | |
102 | |
103 int text_width, text_height; | |
104 pango_layout_get_pixel_size(layout, &text_width, &text_height); | |
105 Point offset(ToViewPoint(Point())); | |
106 // Vertically centered. | |
107 int text_y = offset.y() + ((bounds.height() - text_height) / 2); | |
108 // TODO(xji): need to use SkCanvas->drawPosText() for gpu acceleration. | |
109 cairo_move_to(cr, offset.x(), text_y); | |
110 pango_cairo_show_layout(cr, layout); | |
111 | |
112 cairo_restore(cr); | |
113 | |
114 // Paint cursor. | |
115 bounds = GetUpdatedCursorBounds(); | |
116 if (cursor_visible() && focused()) | |
117 canvas->DrawRectInt(kCursorColor, | |
118 bounds.x(), | |
119 bounds.y(), | |
120 bounds.width(), | |
121 bounds.height()); | |
122 } | |
123 | |
124 SelectionModel RenderTextLinux::FindCursorPosition(const Point& point) { | |
125 // TODO(xji): when points outside of text, return HOME/END position. | |
126 PangoLayout* layout = EnsureLayout(); | |
127 | |
128 if (text().empty()) | |
129 return SelectionModel(0, 0, SelectionModel::LEADING); | |
130 | |
131 Point p(ToTextPoint(point)); | |
132 int caret_pos, trailing; | |
133 pango_layout_xy_to_index(layout, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE, | |
134 &caret_pos, &trailing); | |
135 | |
136 size_t selection_end = caret_pos; | |
137 if (trailing > 0) { | |
138 const char* pango_text = pango_layout_get_text(layout); | |
139 const char* ch = g_utf8_offset_to_pointer(pango_text + caret_pos, trailing); | |
140 DCHECK_GE(ch, pango_text); | |
141 DCHECK_LE(ch, pango_text + strlen(pango_text)); | |
142 selection_end = ch - pango_text; | |
143 } | |
144 | |
145 return SelectionModel( | |
146 Utf8IndexToUtf16Index(selection_end), | |
147 Utf8IndexToUtf16Index(caret_pos), | |
148 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); | |
149 } | |
150 | |
151 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, | |
152 bool insert_mode) { | |
153 PangoLayout* layout = EnsureLayout(); | |
154 | |
155 size_t caret_pos = insert_mode ? selection.caret_pos() : | |
156 selection.selection_end(); | |
157 PangoRectangle pos; | |
158 pango_layout_index_to_pos(layout, Utf16IndexToUtf8Index(caret_pos), &pos); | |
159 | |
160 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
161 int x = pos.x; | |
162 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || | |
163 (!insert_mode && pos.width < 0)) | |
164 x += pos.width; | |
165 x = PANGO_PIXELS(x); | |
166 | |
167 int h = std::min(display_rect().height(), PANGO_PIXELS(pos.height)); | |
168 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); | |
169 bounds.set_origin(ToViewPoint(bounds.origin())); | |
170 | |
171 if (!insert_mode) | |
172 bounds.set_width(std::abs(pos.width)); | |
173 | |
174 return bounds; | |
175 } | |
176 | |
177 SelectionModel RenderTextLinux::GetLeftSelectionModel( | |
178 const SelectionModel& current, | |
179 BreakType break_type) { | |
180 EnsureLayout(); | |
181 | |
182 if (break_type == LINE_BREAK || text().empty()) | |
183 return LeftEndSelectionModel(); | |
184 if (break_type == CHARACTER_BREAK) | |
185 return LeftSelectionModel(current); | |
186 // TODO(xji): not implemented yet. | |
187 return RenderText::GetLeftSelectionModel(current, break_type); | |
188 } | |
189 | |
190 SelectionModel RenderTextLinux::GetRightSelectionModel( | |
191 const SelectionModel& current, | |
192 BreakType break_type) { | |
193 EnsureLayout(); | |
194 | |
195 if (break_type == LINE_BREAK || text().empty()) | |
196 return RightEndSelectionModel(); | |
197 if (break_type == CHARACTER_BREAK) | |
198 return RightSelectionModel(current); | |
199 // TODO(xji): not implemented yet. | |
200 return RenderText::GetRightSelectionModel(current, break_type); | |
201 } | |
202 | |
203 SelectionModel RenderTextLinux::LeftEndSelectionModel() { | |
204 if (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) { | |
205 if (layout_line_->runs) { | |
206 PangoLayoutRun* first_visual_run = | |
207 reinterpret_cast<PangoLayoutRun*>(layout_line_->runs->data); | |
208 PangoItem* item = first_visual_run->item; | |
209 if (item->analysis.level % 2 == 0) { // LTR. | |
210 size_t caret = Utf8IndexToUtf16Index(item->offset); | |
211 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | |
212 } else { // RTL. | |
213 size_t caret = Utf16IndexOfAdjacentGrapheme(item->offset + item->length, | |
214 PREVIOUS); | |
215 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | |
216 } | |
217 } | |
218 } | |
219 return SelectionModel(0, 0, SelectionModel::LEADING); | |
220 } | |
221 | |
222 SelectionModel RenderTextLinux::RightEndSelectionModel() { | |
223 if (GetTextDirection() == base::i18n::LEFT_TO_RIGHT) { | |
224 GSList* last_visual_run = GetLastRun(); | |
225 if (last_visual_run) { | |
226 PangoItem* item = | |
227 reinterpret_cast<PangoLayoutRun*>(last_visual_run->data)->item; | |
228 if (item->analysis.level % 2 == 0) { // LTR. | |
229 size_t caret = Utf16IndexOfAdjacentGrapheme(item->offset + item->length, | |
230 PREVIOUS); | |
231 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | |
232 } else { // RTL. | |
233 size_t caret = Utf8IndexToUtf16Index(item->offset); | |
234 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | |
235 } | |
236 } | |
237 } | |
238 return SelectionModel(0, 0, SelectionModel::LEADING); | |
239 } | |
240 | |
241 size_t RenderTextLinux::GetIndexOfPreviousGrapheme(size_t position) { | |
242 EnsureLayout(); | |
243 size_t index = Utf16IndexToUtf8Index(position); | |
244 return Utf16IndexOfAdjacentGrapheme(index, PREVIOUS); | |
245 } | |
246 | |
247 size_t RenderTextLinux::GetIndexOfNextGrapheme(size_t position) { | |
248 EnsureLayout(); | |
249 size_t index = Utf16IndexToUtf8Index(position); | |
250 return Utf16IndexOfAdjacentGrapheme(index, NEXT); | |
251 } | |
252 | |
253 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { | |
254 GSList* run = layout_line_->runs; | |
255 while (run) { | |
256 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
257 size_t run_start = Utf8IndexToUtf16Index(box->offset); | |
258 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length); | |
259 | |
260 if (position >= run_start && position < run_end) | |
261 return run; | |
262 run = run->next; | |
263 } | |
264 return NULL; | |
265 } | |
266 | |
267 size_t RenderTextLinux::Utf8IndexOfAdjacentGrapheme( | |
268 size_t utf8_index_of_current_grapheme, | |
269 RelativeLogicalPosition pos) const { | |
270 PangoLogAttr* log_attrs; | |
271 gint n_attrs; | |
272 pango_layout_get_log_attrs(layout_, &log_attrs, &n_attrs); | |
273 | |
274 const char* layout_text = pango_layout_get_text(layout_); | |
275 const char* ch = layout_text + utf8_index_of_current_grapheme; | |
276 int char_offset = static_cast<int>(g_utf8_pointer_to_offset(layout_text, ch)); | |
277 | |
278 if (pos == PREVIOUS) { | |
279 if (ch > layout_text) { | |
280 do { | |
281 ch = g_utf8_find_prev_char(layout_text, ch); | |
282 --char_offset; | |
283 } while (ch && *ch && !log_attrs[char_offset].is_cursor_position); | |
behdad_google
2011/09/06 21:57:17
I would change this while condition to:
while (la
xji
2011/09/07 17:14:36
Done.
| |
284 if (!ch) | |
285 ch = layout_text; | |
behdad_google
2011/09/06 21:57:17
Then you shouldn't need this.
xji
2011/09/07 17:14:36
Done.
| |
286 } | |
287 } else { | |
288 if (ch < layout_text + strlen(layout_text)) { | |
289 do { | |
290 ch = g_utf8_find_next_char(ch, NULL); | |
291 ++char_offset; | |
292 } while (ch && *ch && !log_attrs[char_offset].is_cursor_position); | |
behdad_google
2011/09/06 21:57:17
I would then do this one as:
while (ch < layout_t
xji
2011/09/07 17:14:36
Done.
| |
293 if (!ch || ch >= layout_text + strlen(layout_text)) | |
294 ch = layout_text + strlen(layout_text); | |
behdad_google
2011/09/06 21:57:17
You do three strlen()s in this block alone! Save
xji
2011/09/07 17:14:36
Done.
| |
295 } | |
296 } | |
297 | |
298 size_t utf8_index = static_cast<size_t>(ch - layout_text); | |
behdad_google
2011/09/06 21:57:17
So, you maintain both ch and char_offset. You can
xji
2011/09/07 17:14:36
This function is re-written by only use char_offse
| |
299 g_free(log_attrs); | |
behdad_google
2011/09/06 21:57:17
Ok, this is no good. Please save log_attrs in the
xji
2011/09/07 17:14:36
Done.
| |
300 return utf8_index; | |
301 } | |
302 | |
303 size_t RenderTextLinux::Utf16IndexOfAdjacentGrapheme( | |
304 size_t utf8_index_of_current_grapheme, | |
305 RelativeLogicalPosition pos) const { | |
306 size_t utf8_index = Utf8IndexOfAdjacentGrapheme( | |
307 utf8_index_of_current_grapheme, pos); | |
308 return Utf8IndexToUtf16Index(utf8_index); | |
309 } | |
310 | |
311 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( | |
312 const PangoItem* run) const { | |
313 size_t caret = Utf8IndexToUtf16Index(run->offset); | |
314 size_t cursor = Utf16IndexOfAdjacentGrapheme(run->offset, NEXT); | |
315 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
316 } | |
317 | |
318 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( | |
319 const PangoItem* run) const { | |
behdad_google
2011/09/06 21:57:17
Would be easier to call PangoItem instances item i
xji
2011/09/07 17:14:36
Done.
| |
320 size_t caret = Utf16IndexOfAdjacentGrapheme(run->offset + run->length, | |
321 PREVIOUS); | |
322 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
323 } | |
324 | |
325 // Assume caret_pos in |current| is n, 'l' represents leading in | |
326 // caret_placement and 't' represents trailing in caret_placement. Following | |
327 // is the calculation from (caret_pos, caret_placement) in |current| to | |
328 // (selection_end, caret_pos, caret_placement) when moving cursor left by | |
329 // one grapheme (for simplicity, assume each grapheme is one character). | |
330 // If n is in LTR run, | |
331 // (n, t) ---> (n, n, l). | |
332 // (n, l) ---> (n-1, n-1, l) if n is inside run (not at boundary). | |
333 // (n, l) ---> goto across run case if n is at run boundary. | |
334 // If n is in RTL run, | |
335 // (n, l) --> (n+1, n, t). | |
336 // (n, t) --> (n+2, n+1, t) if n is inside run. | |
337 // (n, t) --> goto across run case if n is at run boundary. | |
338 // If n is at run boundary, get its visually left run, | |
339 // If left run is LTR run, | |
340 // (n, t) --> (left run's end, left run's end, l). | |
341 // If left run is RTL run, | |
342 // (n, t) --> (left run's begin + 1, left run's begin, t). | |
343 SelectionModel RenderTextLinux::LeftSelectionModel( | |
behdad_google
2011/09/06 21:57:17
I need much more time to review this one for corre
| |
344 const SelectionModel& selection) { | |
345 size_t caret = selection.caret_pos(); | |
346 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
347 GSList* run = GetRunContainingPosition(caret); | |
348 DCHECK(run); | |
349 | |
350 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
351 size_t run_start = Utf8IndexToUtf16Index(box->offset); | |
352 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length); | |
353 | |
354 if (box->analysis.level % 2 == 0) { // LTR run. | |
355 if (caret_placement == SelectionModel::TRAILING) | |
356 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
357 else if (caret > run_start) { | |
358 caret = GetIndexOfPreviousGrapheme(caret); | |
359 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
360 } | |
361 } else { // RTL run. | |
362 if (caret_placement == SelectionModel::LEADING) { | |
363 size_t cursor = GetIndexOfNextGrapheme(caret); | |
364 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
365 } else if (selection.selection_end() < run_end) { | |
366 caret = GetIndexOfNextGrapheme(caret); | |
367 size_t cursor = GetIndexOfNextGrapheme(caret); | |
368 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
369 } | |
370 } | |
371 | |
372 // The character is at the begin of its run; advance to the previous visual | |
373 // run. | |
374 GSList* prev_run = GetPreviousRun(run); | |
375 if (!prev_run) | |
376 return LeftEndSelectionModel(); | |
377 | |
378 box = reinterpret_cast<PangoLayoutRun*>(prev_run->data)->item; | |
379 return (box->analysis.level % 2) ? FirstSelectionModelInsideRun(box) : | |
380 LastSelectionModelInsideRun(box); | |
381 } | |
382 | |
383 // Assume caret_pos in |current| is n, 'l' represents leading in | |
384 // caret_placement and 't' represents trailing in caret_placement. Following | |
385 // is the calculation from (caret_pos, caret_placement) in |current| to | |
386 // (selection_end, caret_pos, caret_placement) when moving cursor right by | |
387 // one grapheme (for simplicity, assume each grapheme is one character). | |
388 // If n is in LTR run, | |
389 // (n, l) ---> (n+1, n, t). | |
390 // (n, t) ---> (n+2, n+1, t) if n is inside run (not at boundary). | |
391 // (n, t) ---> goto across run case if n is at run boundary. | |
392 // If n is in RTL run, | |
393 // (n, t) --> (n, n, l). | |
394 // (n, l) --> (n-1, n-1, l) if n is inside run. | |
395 // (n, l) --> goto across run case if n is at run boundary. | |
396 // If n is at run boundary, get its visually right run, | |
397 // If right run is LTR run, | |
398 // (n, t) --> (right run's begin + 1, right run's begin, t). | |
399 // If right run is RTL run, | |
400 // (n, t) --> (right run's end, right run's end, l). | |
401 SelectionModel RenderTextLinux::RightSelectionModel( | |
402 const SelectionModel& selection) { | |
403 size_t caret = selection.caret_pos(); | |
404 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
405 GSList* run = GetRunContainingPosition(caret); | |
406 DCHECK(run); | |
407 | |
408 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
409 size_t run_start = Utf8IndexToUtf16Index(box->offset); | |
410 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length); | |
411 | |
412 if (box->analysis.level % 2 == 0) { // LTR run. | |
413 if (caret_placement == SelectionModel::LEADING) { | |
414 size_t cursor = GetIndexOfNextGrapheme(caret); | |
415 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
416 } else if (selection.selection_end() < run_end) { | |
417 caret = GetIndexOfNextGrapheme(caret); | |
418 size_t cursor = GetIndexOfNextGrapheme(caret); | |
419 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
420 } | |
421 } else { // RTL run. | |
422 if (caret_placement == SelectionModel::TRAILING) | |
423 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
424 else if (caret > run_start) { | |
425 caret = GetIndexOfPreviousGrapheme(caret); | |
426 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
427 } | |
428 } | |
429 | |
430 // The character is at the end of its run; advance to the next visual run. | |
431 GSList* next_run = run->next; | |
432 if (!next_run) | |
433 return RightEndSelectionModel(); | |
434 | |
435 box = reinterpret_cast<PangoLayoutRun*>(next_run->data)->item; | |
436 return (box->analysis.level % 2) ? LastSelectionModelInsideRun(box) : | |
437 FirstSelectionModelInsideRun(box); | |
438 } | |
439 | |
440 PangoLayout* RenderTextLinux::EnsureLayout() { | |
441 if (layout_ == NULL) { | |
442 CanvasSkia canvas(display_rect().width(), display_rect().height(), false); | |
443 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); | |
444 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | |
445 | |
446 layout_ = pango_cairo_create_layout(cr); | |
447 SetupPangoLayout( | |
448 layout_, | |
449 text(), | |
450 default_style().font, | |
451 display_rect().width(), | |
452 base::i18n::GetFirstStrongCharacterDirection(text()), | |
453 CanvasSkia::DefaultCanvasTextAlignment()); | |
454 | |
455 // No width set so that the x-axis position is relative to the start of the | |
456 // text. ToViewPoint and ToTextPoint take care of the position conversion | |
457 // between text space and view spaces. | |
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 |
OLD | NEW |