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 } | |
msw
2011/08/29 20:38:05
} // namespace
xji
2011/08/30 00:39:41
Done.
| |
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)); | |
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); | |
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 // TODO(xji): use ScopedPlatformPaint. | |
msw
2011/08/29 20:38:05
You should be able to do:
skia::ScopedPlatformPain
xji
2011/08/30 00:39:41
Done.
| |
99 cairo_t* cr = skia::BeginPlatformPaint(canvas_skia); | |
100 cairo_save(cr); | |
101 cairo_rectangle(cr, bounds.x(), bounds.y(), bounds.width(), bounds.height()); | |
102 cairo_clip(cr); | |
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 cairo_move_to(cr, offset.x(), text_y); | |
110 pango_cairo_show_layout(cr, layout); | |
msw
2011/08/29 20:38:05
We ought to check with a Pango/Skia expert to dete
xji
2011/08/30 00:39:41
talked with Mike Reed, who said that "The only way
msw
2011/08/30 01:17:07
Ok, that's definitely outside the scope of this ch
| |
111 | |
112 // Destructor. | |
113 cairo_restore(cr); | |
114 skia::EndPlatformPaint(canvas_skia); | |
115 | |
116 // Paint cursor. | |
117 bounds = GetUpdatedCursorBounds(); | |
118 if (cursor_visible() && focused() && !bounds.IsEmpty()) { | |
119 if (!bounds.IsEmpty()) | |
120 canvas->DrawRectInt(kCursorColor, | |
msw
2011/08/29 20:38:05
outdent this line by one space.
xji
2011/08/30 00:39:41
Done.
| |
121 bounds.x(), | |
122 bounds.y(), | |
123 bounds.width(), | |
124 bounds.height()); | |
125 } | |
126 } | |
127 | |
128 SelectionModel RenderTextLinux::FindCursorPosition(const Point& point) { | |
129 // TODO(xji): when points outside of text, return HOME/END position. | |
130 PangoLayout* layout = EnsureLayout(); | |
131 | |
132 if (text().empty()) | |
133 return SelectionModel(0, 0, SelectionModel::LEADING); | |
134 | |
135 Point p(ToTextPoint(point)); | |
136 int caret_pos, trailing; | |
137 pango_layout_xy_to_index(layout, p.x() * PANGO_SCALE, p.y() * PANGO_SCALE, | |
138 &caret_pos, &trailing); | |
139 | |
140 size_t selection_end = caret_pos; | |
141 if (trailing > 0) { | |
142 const char* pango_text = pango_layout_get_text(layout); | |
143 const char* ch = g_utf8_offset_to_pointer(pango_text + caret_pos, trailing); | |
144 DCHECK_GE(ch, pango_text); | |
145 DCHECK_LE(ch, pango_text + strlen(pango_text)); | |
146 selection_end = ch - pango_text; | |
147 } | |
148 | |
149 return SelectionModel( | |
150 Utf8IndexToUtf16Index(selection_end), | |
151 Utf8IndexToUtf16Index(caret_pos), | |
152 trailing > 0 ? SelectionModel::TRAILING : SelectionModel::LEADING); | |
153 } | |
154 | |
155 Rect RenderTextLinux::GetCursorBounds(const SelectionModel& selection, | |
156 bool insert_mode) { | |
157 PangoLayout* layout = EnsureLayout(); | |
158 | |
159 size_t caret_pos = insert_mode ? selection.caret_pos() : | |
160 selection.selection_end(); | |
161 PangoRectangle pos; | |
162 pango_layout_index_to_pos(layout, Utf16IndexToUtf8Index(caret_pos), &pos); | |
163 | |
164 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
165 int x = pos.x; | |
166 if ((insert_mode && caret_placement == SelectionModel::TRAILING) || | |
167 (!insert_mode && pos.width < 0)) | |
168 x += pos.width; | |
169 x = x / PANGO_SCALE; | |
170 | |
171 int h = std::min(display_rect().height(), pos.height / PANGO_SCALE); | |
172 Rect bounds(x, (display_rect().height() - h) / 2, 0, h); | |
173 bounds.set_origin(ToViewPoint(bounds.origin())); | |
174 | |
175 if (!insert_mode) | |
176 bounds.set_width(std::abs(pos.width)); | |
177 | |
178 return bounds; | |
179 } | |
180 | |
181 SelectionModel RenderTextLinux::GetLeftSelectionModel( | |
182 const SelectionModel& current, | |
183 BreakType break_type) { | |
184 EnsureLayout(); | |
185 | |
186 if (break_type == LINE_BREAK || text().empty()) | |
187 return LeftEndSelectionModel(); | |
188 if (break_type == CHARACTER_BREAK) | |
189 return LeftSelectionModel(current); | |
190 // TODO(xji): not implemented yet. | |
191 return RenderText::GetLeftSelectionModel(current, break_type); | |
192 } | |
193 | |
194 SelectionModel RenderTextLinux::GetRightSelectionModel( | |
195 const SelectionModel& current, | |
196 BreakType break_type) { | |
197 EnsureLayout(); | |
198 | |
199 if (break_type == LINE_BREAK || text().empty()) | |
200 return RightEndSelectionModel(); | |
201 if (break_type == CHARACTER_BREAK) | |
202 return RightSelectionModel(current); | |
203 // TODO(xji): not implemented yet. | |
204 return RenderText::GetRightSelectionModel(current, break_type); | |
205 } | |
206 | |
207 size_t RenderTextLinux::GetIndexOfPreviousGrapheme(size_t position) { | |
208 EnsureLayout(); | |
209 return Utf16IndexOfAdjacentGrapheme(position, PREVIOUS); | |
210 } | |
211 | |
212 SelectionModel RenderTextLinux::LeftEndSelectionModel() { | |
213 if (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) { | |
214 GSList* first_visual_run = layout_line_->runs; | |
215 if (first_visual_run) { | |
216 PangoItem* item = | |
217 reinterpret_cast<PangoLayoutRun*>(first_visual_run->data)->item; | |
218 if (item->analysis.level % 2 == 0) { // LTR. | |
219 size_t caret = Utf8IndexToUtf16Index(item->offset); | |
220 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | |
msw
2011/08/29 20:38:05
Why do you set the cursor to text().length() here?
xji
2011/08/30 00:39:41
pango's text direction is determined by its first
msw
2011/08/30 01:17:07
Ah, you're right. Sorry, this behavior can just be
xji
2011/08/30 04:39:39
Yes. that is right.
| |
221 } else { // RTL. | |
222 // TODO(xji): Utf8IndexToUtf16Index conversion should be able to avoid. | |
223 size_t caret = Utf8IndexToUtf16Index(item->offset + item->length); | |
224 caret = Utf16IndexOfAdjacentGrapheme(caret, PREVIOUS); | |
225 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | |
msw
2011/08/29 20:38:05
Shouldn't this cursor be at position corresponding
xji
2011/08/30 00:39:41
same reason as above. in RTL context, Left end is
| |
226 } | |
227 } | |
228 } | |
229 return SelectionModel(0, 0, SelectionModel::LEADING); | |
230 } | |
231 | |
232 SelectionModel RenderTextLinux::RightEndSelectionModel() { | |
233 if (GetTextDirection() == base::i18n::LEFT_TO_RIGHT) { | |
234 GSList* last_visual_run = GetLastRun(); | |
235 if (last_visual_run) { | |
236 PangoItem* item = | |
237 reinterpret_cast<PangoLayoutRun*>(last_visual_run->data)->item; | |
238 if (item->analysis.level % 2 == 0) { // LTR. | |
239 // TODO(xji): Utf8IndexToUtf16Index conversion should be able to avoid. | |
240 size_t caret = Utf8IndexToUtf16Index(item->offset + item->length); | |
241 caret = Utf16IndexOfAdjacentGrapheme(caret, PREVIOUS); | |
242 return SelectionModel(text().length(), caret, SelectionModel::TRAILING); | |
msw
2011/08/29 20:38:05
Shouldn't this cursor be at item->offset + item->l
xji
2011/08/30 00:39:41
ditto.
in LTR context, right end is logical end, c
| |
243 } else { // RTL. | |
244 size_t caret = Utf8IndexToUtf16Index(item->offset); | |
245 return SelectionModel(text().length(), caret, SelectionModel::LEADING); | |
msw
2011/08/29 20:38:05
shouldn't this cursor be at item->offset?
xji
2011/08/30 00:39:41
ditto.
in LTR context, right end is logical end, c
| |
246 } | |
247 } | |
248 } | |
249 return SelectionModel(0, 0, SelectionModel::LEADING); | |
250 } | |
251 | |
252 GSList* RenderTextLinux::GetRunContainingPosition(size_t position) const { | |
253 GSList* run = layout_line_->runs; | |
254 while (run) { | |
255 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
256 size_t run_start = Utf8IndexToUtf16Index(box->offset); | |
257 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length); | |
258 | |
259 if (position >= run_start && position < run_end) | |
260 return run; | |
261 run = run->next; | |
262 } | |
263 return NULL; | |
264 } | |
265 | |
266 size_t RenderTextLinux::Utf8IndexOfAdjacentGrapheme( | |
267 size_t utf16_index_of_current_grapheme, | |
268 RelativeLogicalPosition pos) const { | |
msw
2011/08/29 20:38:05
I implemented a similar function:
size_t IndexOfAd
| |
269 PangoLogAttr* log_attrs; | |
270 gint n_attrs; | |
271 pango_layout_get_log_attrs(layout_, &log_attrs, &n_attrs); | |
272 | |
273 const char* layout_text = pango_layout_get_text(layout_); | |
274 const char* ch = layout_text + | |
275 Utf16IndexToUtf8Index(utf16_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); | |
284 if (!ch) | |
285 ch = layout_text; | |
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); | |
293 if (!ch || ch >= layout_text + strlen(layout_text)) | |
294 ch = layout_text + strlen(layout_text); | |
295 } | |
296 } | |
297 | |
298 size_t utf8_index = static_cast<size_t>(ch - layout_text); | |
299 g_free(log_attrs); | |
300 return utf8_index; | |
301 } | |
302 | |
303 size_t RenderTextLinux::Utf16IndexOfAdjacentGrapheme( | |
304 size_t utf16_index_of_current_grapheme, | |
305 RelativeLogicalPosition pos) const { | |
306 size_t utf8_index = Utf8IndexOfAdjacentGrapheme( | |
307 utf16_index_of_current_grapheme, pos); | |
308 return Utf8IndexToUtf16Index(utf8_index); | |
309 } | |
310 | |
311 SelectionModel RenderTextLinux::FirstSelectionModelInsideRun( | |
312 const PangoItem* run) const { | |
313 // TODO(xji): Utf8IndexToUtf16Index conversion should be able to avoid. | |
314 size_t caret = Utf8IndexToUtf16Index(run->offset); | |
315 size_t cursor = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
316 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
317 } | |
318 | |
319 SelectionModel RenderTextLinux::LastSelectionModelInsideRun( | |
320 const PangoItem* run) const { | |
321 // TODO(xji): Utf8IndexToUtf16Index conversion should be able to avoid. | |
322 size_t caret = Utf8IndexToUtf16Index(run->offset + run->length); | |
323 caret = Utf16IndexOfAdjacentGrapheme(caret, PREVIOUS); | |
324 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
325 } | |
326 | |
327 // Assume caret_pos in |current| is n, 'l' represents leading in | |
328 // caret_placement and 't' represents trailing in caret_placement. Following | |
329 // is the calculation from (caret_pos, caret_placement) in |current| to | |
330 // (selection_end, caret_pos, caret_placement) when moving cursor left by | |
331 // one grapheme (for simplicity, assume each grapheme is one character). | |
332 // If n is in LTR run, | |
333 // (n, t) ---> (n, n, l). | |
334 // (n, l) ---> (n-1, n-1, l) if n is inside run (not at boundary). | |
335 // (n, l) ---> goto across run case if n is at run boundary. | |
336 // If n is in RTL run, | |
337 // (n, l) --> (n+1, n, t). | |
338 // (n, t) --> (n+2, n+1, t) if n is inside run. | |
339 // (n, t) --> goto across run case if n is at run boundary. | |
340 // If n is at run boundary, get its visually left run, | |
341 // If left run is LTR run, | |
342 // (n, t) --> (left run's end, left run's end, l). | |
343 // If left run is RTL run, | |
344 // (n, t) --> (left run's begin + 1, left run's begin, t). | |
345 SelectionModel RenderTextLinux::LeftSelectionModel( | |
346 const SelectionModel& selection) { | |
347 size_t caret = selection.caret_pos(); | |
348 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
349 GSList* run = GetRunContainingPosition(caret); | |
350 DCHECK(run); | |
351 | |
352 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
353 size_t run_start = Utf8IndexToUtf16Index(box->offset); | |
354 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length); | |
355 | |
356 if (box->analysis.level % 2 == 0) { // LTR run. | |
357 if (caret_placement == SelectionModel::TRAILING) | |
358 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
359 else if (caret > run_start) { | |
360 caret = Utf16IndexOfAdjacentGrapheme(caret, PREVIOUS); | |
361 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
362 } | |
363 } else { // RTL run. | |
364 if (caret_placement == SelectionModel::LEADING) { | |
365 size_t cursor = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
366 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
367 } else if (selection.selection_end() < run_end) { | |
368 caret = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
369 size_t cursor = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
370 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
371 } | |
372 } | |
373 | |
374 // The character is at the begin of its run; advance to the previous visual | |
375 // run. | |
376 GSList* prev_run = GetPreviousRun(run); | |
377 if (!prev_run) // TODO(xji): check whether this is expected. | |
msw
2011/08/29 20:38:05
If the caret is at the beginning of the first run,
xji
2011/08/30 00:39:41
Given an example, logical text "ABCdef", visually
| |
378 return LeftEndSelectionModel(); | |
379 | |
380 box = reinterpret_cast<PangoLayoutRun*>(prev_run->data)->item; | |
381 return (box->analysis.level % 2) ? FirstSelectionModelInsideRun(box) : | |
382 LastSelectionModelInsideRun(box); | |
383 } | |
384 | |
385 // Assume caret_pos in |current| is n, 'l' represents leading in | |
386 // caret_placement and 't' represents trailing in caret_placement. Following | |
387 // is the calculation from (caret_pos, caret_placement) in |current| to | |
388 // (selection_end, caret_pos, caret_placement) when moving cursor right by | |
389 // one grapheme (for simplicity, assume each grapheme is one character). | |
390 // If n is in LTR run, | |
391 // (n, l) ---> (n+1, n, t). | |
392 // (n, t) ---> (n+2, n+1, t) if n is inside run (not at boundary). | |
393 // (n, t) ---> goto across run case if n is at run boundary. | |
394 // If n is in RTL run, | |
395 // (n, t) --> (n, n, l). | |
396 // (n, l) --> (n-1, n-1, l) if n is inside run. | |
397 // (n, l) --> goto across run case if n is at run boundary. | |
398 // If n is at run boundary, get its visually right run, | |
399 // If right run is LTR run, | |
400 // (n, t) --> (right run's begin + 1, right run's begin, t). | |
401 // If right run is RTL run, | |
402 // (n, t) --> (right run's end, right run's end, l). | |
403 SelectionModel RenderTextLinux::RightSelectionModel( | |
404 const SelectionModel& selection) { | |
405 size_t caret = selection.caret_pos(); | |
406 SelectionModel::CaretPlacement caret_placement = selection.caret_placement(); | |
407 GSList* run = GetRunContainingPosition(caret); | |
408 DCHECK(run); | |
409 | |
410 PangoItem* box = reinterpret_cast<PangoLayoutRun*>(run->data)->item; | |
411 size_t run_start = Utf8IndexToUtf16Index(box->offset); | |
412 size_t run_end = Utf8IndexToUtf16Index(box->offset + box->length); | |
413 | |
414 if (box->analysis.level % 2 == 0) { // LTR run. | |
415 if (caret_placement == SelectionModel::LEADING) { | |
416 size_t cursor = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
417 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
418 } else if (selection.selection_end() < run_end) { | |
419 caret = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
420 size_t cursor = Utf16IndexOfAdjacentGrapheme(caret, NEXT); | |
421 return SelectionModel(cursor, caret, SelectionModel::TRAILING); | |
422 } | |
423 } else { // RTL run. | |
424 if (caret_placement == SelectionModel::TRAILING) | |
425 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
426 else if (caret > run_start) { | |
427 caret = Utf16IndexOfAdjacentGrapheme(caret, PREVIOUS); | |
428 return SelectionModel(caret, caret, SelectionModel::LEADING); | |
429 } | |
430 } | |
431 | |
432 // The character is at the end of its run; advance to the next visual run. | |
433 GSList* next_run = run->next; | |
434 if (!next_run) // TODO(xji): what is the expected behavior? | |
msw
2011/08/29 20:38:05
If the caret happens to be greater than or equal t
xji
2011/08/30 00:39:41
ditto as above reason.
consider case "abcDEF" visu
| |
435 return RightEndSelectionModel(); | |
436 | |
437 box = reinterpret_cast<PangoLayoutRun*>(next_run->data)->item; | |
438 return (box->analysis.level % 2) ? LastSelectionModelInsideRun(box) : | |
439 FirstSelectionModelInsideRun(box); | |
440 } | |
441 | |
442 PangoLayout* RenderTextLinux::EnsureLayout() { | |
443 if (layout_ == NULL) { | |
444 CanvasSkia canvas(display_rect().width(), display_rect().height(), false); | |
445 // TODO(xji): can use ScopedPlatformPaint ? | |
msw
2011/08/29 20:38:05
You should be able to do:
skia::ScopedPlatformPain
xji
2011/08/30 00:39:41
Done.
| |
446 cairo_t* cr = skia::BeginPlatformPaint(&canvas); | |
447 | |
448 layout_ = pango_cairo_create_layout(cr); | |
449 SetupPangoLayout( | |
450 layout_, | |
451 text(), | |
452 default_style().font, | |
453 display_rect().width(), | |
454 base::i18n::GetFirstStrongCharacterDirection(text()), | |
455 CanvasSkia::DefaultCanvasTextAlignment()); | |
456 | |
457 pango_layout_set_height(layout_, display_rect().height() * PANGO_SCALE); | |
458 SetupPangoAttributes(layout_); | |
459 | |
460 skia::EndPlatformPaint(&canvas); | |
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 trigger ResetLayout(). | |
msw
2011/08/29 20:38:05
trigger*s*, and we should probably add some common
xji
2011/08/30 00:39:41
Done. agree on the consolidation part.
| |
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); | |
msw
2011/08/29 20:38:05
This function is private, and all the callers chec
xji
2011/08/30 00:39:41
I am removing the 'end' check here.
it should chec
| |
536 size_t e = std::min(end, text().length()); | |
537 pango_attr->end_index = Utf16IndexToUtf8Index(e); | |
538 pango_attr_list_insert(attrs, pango_attr); | |
539 } | |
540 | |
541 GSList* RenderTextLinux::GetPreviousRun(GSList* run) const { | |
542 GSList* current = layout_line_->runs; | |
543 GSList* prev = NULL; | |
544 while (current) { | |
545 if (current == run) | |
546 return prev; | |
547 prev = current; | |
548 current = current->next; | |
549 } | |
550 return NULL; | |
551 } | |
552 | |
553 GSList* RenderTextLinux::GetLastRun() const { | |
554 GSList* current = layout_line_->runs; | |
555 while (current && current->next) { | |
556 current = current->next; | |
557 } | |
558 return current; | |
559 } | |
560 | |
561 size_t RenderTextLinux::Utf16IndexToUtf8Index(size_t index) const { | |
562 int32_t utf8_index = 0; | |
563 UErrorCode ec = U_ZERO_ERROR; | |
564 u_strToUTF8(NULL, 0, &utf8_index, text().data(), index, &ec); | |
565 // Even given a destination buffer as NULL and destination capacity as 0, | |
566 // if the output length is equal to or greater than the capacity, then the | |
567 // UErrorCode is set to U_STRING_NOT_TERMINATED_WARNING or | |
568 // U_BUFFER_OVERFLOW_ERROR respectively. | |
569 // Please refer to | |
570 // http://userguide.icu-project.org/strings#TOC-Using-C-Strings:-NUL-Terminate d-vs | |
571 // for detail (search for "Note that" below "Preflighting"). | |
572 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | |
573 ec == U_STRING_NOT_TERMINATED_WARNING); | |
574 return utf8_index; | |
575 } | |
576 | |
577 size_t RenderTextLinux::Utf8IndexToUtf16Index(size_t index) const { | |
578 int32_t utf16_index = 0; | |
579 UErrorCode ec = U_ZERO_ERROR; | |
580 const char* layout_text = pango_layout_get_text(layout_); | |
581 u_strFromUTF8(NULL, 0, &utf16_index, layout_text, index, &ec); | |
582 DCHECK(ec == U_BUFFER_OVERFLOW_ERROR || | |
583 ec == U_STRING_NOT_TERMINATED_WARNING); | |
584 return utf16_index; | |
585 } | |
586 | |
20 } // namespace gfx | 587 } // namespace gfx |
OLD | NEW |