1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/render_text_win.h"
#include <algorithm>
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/win/scoped_hdc.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/platform_font.h"
namespace {
// The maximum supported number of Uniscribe runs; a SCRIPT_ITEM is 8 bytes.
// TODO(msw): Review memory use/failure? Max string length? Alternate approach?
const int kGuessItems = 100;
const int kMaxItems = 10000;
// The maximum supported number of Uniscribe glyphs; a glyph is 1 word.
// TODO(msw): Review memory use/failure? Max string length? Alternate approach?
const int kMaxGlyphs = 100000;
// Callback to |EnumEnhMetaFile()| to intercept font creation.
int CALLBACK MetaFileEnumProc(HDC hdc,
HANDLETABLE* table,
CONST ENHMETARECORD* record,
int table_entries,
LPARAM log_font) {
if (record->iType == EMR_EXTCREATEFONTINDIRECTW) {
const EMREXTCREATEFONTINDIRECTW* create_font_record =
reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record);
*reinterpret_cast<LOGFONT*>(log_font) = create_font_record->elfw.elfLogFont;
}
return 1;
}
// Finds a fallback font to use to render the specified |text| with respect to
// an initial |font|. Returns the resulting font via out param |result|. Returns
// |true| if a fallback font was found.
// Adapted from WebKit's |FontCache::GetFontDataForCharacters()|.
bool ChooseFallbackFont(HDC hdc,
const gfx::Font& font,
const wchar_t* text,
int text_length,
gfx::Font* result) {
// Use a meta file to intercept the fallback font chosen by Uniscribe.
HDC meta_file_dc = CreateEnhMetaFile(hdc, NULL, NULL, NULL);
if (!meta_file_dc)
return false;
SelectObject(meta_file_dc, font.GetNativeFont());
SCRIPT_STRING_ANALYSIS script_analysis;
HRESULT hresult =
ScriptStringAnalyse(meta_file_dc, text, text_length, 0, -1,
SSA_METAFILE | SSA_FALLBACK | SSA_GLYPHS | SSA_LINK,
0, NULL, NULL, NULL, NULL, NULL, &script_analysis);
if (SUCCEEDED(hresult)) {
hresult = ScriptStringOut(script_analysis, 0, 0, 0, NULL, 0, 0, FALSE);
ScriptStringFree(&script_analysis);
}
bool found_fallback = false;
HENHMETAFILE meta_file = CloseEnhMetaFile(meta_file_dc);
if (SUCCEEDED(hresult)) {
LOGFONT log_font;
log_font.lfFaceName[0] = 0;
EnumEnhMetaFile(0, meta_file, MetaFileEnumProc, &log_font, NULL);
if (log_font.lfFaceName[0]) {
*result = gfx::Font(UTF16ToUTF8(log_font.lfFaceName), font.GetFontSize());
found_fallback = true;
}
}
DeleteEnhMetaFile(meta_file);
return found_fallback;
}
} // namespace
namespace gfx {
namespace internal {
TextRun::TextRun()
: strike(false),
underline(false),
width(0),
preceding_run_widths(0),
glyph_count(0),
script_cache(NULL) {
}
TextRun::~TextRun() {
ScriptFreeCache(&script_cache);
}
} // namespace internal
RenderTextWin::RenderTextWin()
: RenderText(),
script_control_(),
script_state_(),
string_width_(0),
needs_layout_(false) {
// Omitting default constructors for script_* would leave POD uninitialized.
HRESULT hr = 0;
// TODO(msw): Call ScriptRecordDigitSubstitution on WM_SETTINGCHANGE message.
// TODO(msw): Use Chrome/profile locale/language settings?
hr = ScriptRecordDigitSubstitution(LOCALE_USER_DEFAULT, &digit_substitute_);
DCHECK(SUCCEEDED(hr));
hr = ScriptApplyDigitSubstitution(&digit_substitute_,
&script_control_,
&script_state_);
DCHECK(SUCCEEDED(hr));
script_control_.fMergeNeutralItems = true;
MoveCursorTo(LeftEndSelectionModel());
}
RenderTextWin::~RenderTextWin() {
STLDeleteContainerPointers(runs_.begin(), runs_.end());
}
int RenderTextWin::GetStringWidth() {
EnsureLayout();
return string_width_;
}
SelectionModel RenderTextWin::FindCursorPosition(const Point& point) {
if (text().empty())
return SelectionModel();
EnsureLayout();
// Find the run that contains the point and adjust the argument location.
Point p(ToTextPoint(point));
size_t run_index = GetRunContainingPoint(p);
if (run_index == runs_.size())
return (p.x() < 0) ? LeftEndSelectionModel() : RightEndSelectionModel();
internal::TextRun* run = runs_[run_index];
int position = 0, trailing = 0;
HRESULT hr = ScriptXtoCP(p.x() - run->preceding_run_widths,
run->range.length(),
run->glyph_count,
run->logical_clusters.get(),
run->visible_attributes.get(),
run->advance_widths.get(),
&(run->script_analysis),
&position,
&trailing);
DCHECK(SUCCEEDED(hr));
position += run->range.start();
size_t cursor = position + trailing;
DCHECK_GE(cursor, 0U);
DCHECK_LE(cursor, text().length());
return SelectionModel(cursor, position,
(trailing > 0) ? SelectionModel::TRAILING : SelectionModel::LEADING);
}
Rect RenderTextWin::GetCursorBounds(const SelectionModel& selection,
bool insert_mode) {
EnsureLayout();
// Highlight the logical cursor (selection end) when not in insert mode.
size_t pos = insert_mode ? selection.caret_pos() : selection.selection_end();
size_t run_index = GetRunContainingPosition(pos);
internal::TextRun* run = run_index == runs_.size() ? NULL : runs_[run_index];
int start_x = 0, end_x = 0;
if (run) {
HRESULT hr = 0;
hr = ScriptCPtoX(pos - run->range.start(),
false,
run->range.length(),
run->glyph_count,
run->logical_clusters.get(),
run->visible_attributes.get(),
run->advance_widths.get(),
&(run->script_analysis),
&start_x);
DCHECK(SUCCEEDED(hr));
hr = ScriptCPtoX(pos - run->range.start(),
true,
run->range.length(),
run->glyph_count,
run->logical_clusters.get(),
run->visible_attributes.get(),
run->advance_widths.get(),
&(run->script_analysis),
&end_x);
DCHECK(SUCCEEDED(hr));
}
// TODO(msw): Use the last visual run's font instead of the default font?
int height = run ? run->font.GetHeight() : default_style().font.GetHeight();
Rect rect(std::min(start_x, end_x), 0, std::abs(end_x - start_x), height);
// Offset to the run start or the right/left end for an out of bounds index.
// Also center the rect vertically in the display area.
int text_end_offset = base::i18n::IsRTL() ? 0 : GetStringWidth();
rect.Offset((run ? run->preceding_run_widths : text_end_offset),
(display_rect().height() - rect.height()) / 2);
// Adjust for leading/trailing in insert mode.
if (insert_mode && run) {
bool leading = selection.caret_placement() == SelectionModel::LEADING;
// Adjust the x value for right-side placement.
if (run->script_analysis.fRTL == leading)
rect.set_x(rect.right());
rect.set_width(0);
}
rect.set_origin(ToViewPoint(rect.origin()));
return rect;
}
SelectionModel RenderTextWin::GetLeftSelectionModel(
const SelectionModel& selection,
BreakType break_type) {
EnsureLayout();
if (break_type == LINE_BREAK || text().empty())
return LeftEndSelectionModel();
if (break_type == CHARACTER_BREAK)
return LeftSelectionModel(selection);
// TODO(msw): Implement word breaking.
return RenderText::GetLeftSelectionModel(selection, break_type);
}
SelectionModel RenderTextWin::GetRightSelectionModel(
const SelectionModel& selection,
BreakType break_type) {
EnsureLayout();
if (break_type == LINE_BREAK || text().empty())
return RightEndSelectionModel();
if (break_type == CHARACTER_BREAK)
return RightSelectionModel(selection);
// TODO(msw): Implement word breaking.
return RenderText::GetRightSelectionModel(selection, break_type);
}
SelectionModel RenderTextWin::LeftEndSelectionModel() {
if (text().empty())
return SelectionModel(0, 0, SelectionModel::LEADING);
EnsureLayout();
size_t cursor = base::i18n::IsRTL() ? text().length() : 0;
internal::TextRun* run = runs_[visual_to_logical_[0]];
size_t caret;
SelectionModel::CaretPlacement placement;
if (run->script_analysis.fRTL) {
caret = IndexOfAdjacentGrapheme(run->range.end(), false);
placement = SelectionModel::TRAILING;
} else {
caret = run->range.start();
placement = SelectionModel::LEADING;
}
return SelectionModel(cursor, caret, placement);
}
SelectionModel RenderTextWin::RightEndSelectionModel() {
if (text().empty())
return SelectionModel(0, 0, SelectionModel::LEADING);
EnsureLayout();
size_t cursor = base::i18n::IsRTL() ? 0 : text().length();
internal::TextRun* run = runs_[visual_to_logical_[runs_.size() - 1]];
size_t caret;
SelectionModel::CaretPlacement placement;
if (run->script_analysis.fRTL) {
caret = run->range.start();
placement = SelectionModel::LEADING;
} else {
caret = IndexOfAdjacentGrapheme(run->range.end(), false);
placement = SelectionModel::TRAILING;
}
return SelectionModel(cursor, caret, placement);
}
void RenderTextWin::GetSubstringBounds(size_t from,
size_t to,
std::vector<Rect>* bounds) {
DCHECK(!needs_layout_);
ui::Range range(from, to);
DCHECK(ui::Range(0, text().length()).Contains(range));
Point display_offset(GetUpdatedDisplayOffset());
HRESULT hr = 0;
bounds->clear();
if (from == to)
return;
// Add a Rect for each run/selection intersection.
// TODO(msw): The bounds should probably not always be leading the range ends.
for (size_t i = 0; i < runs_.size(); ++i) {
internal::TextRun* run = runs_[visual_to_logical_[i]];
ui::Range intersection = run->range.Intersect(range);
if (intersection.IsValid()) {
DCHECK(!intersection.is_reversed());
int start_offset = 0;
hr = ScriptCPtoX(intersection.start() - run->range.start(),
false,
run->range.length(),
run->glyph_count,
run->logical_clusters.get(),
run->visible_attributes.get(),
run->advance_widths.get(),
&(run->script_analysis),
&start_offset);
DCHECK(SUCCEEDED(hr));
int end_offset = 0;
hr = ScriptCPtoX(intersection.end() - run->range.start(),
false,
run->range.length(),
run->glyph_count,
run->logical_clusters.get(),
run->visible_attributes.get(),
run->advance_widths.get(),
&(run->script_analysis),
&end_offset);
DCHECK(SUCCEEDED(hr));
if (start_offset > end_offset)
std::swap(start_offset, end_offset);
Rect rect(run->preceding_run_widths + start_offset, 0,
end_offset - start_offset, run->font.GetHeight());
// Center the rect vertically in the display area.
rect.Offset(0, (display_rect().height() - rect.height()) / 2);
rect.set_origin(ToViewPoint(rect.origin()));
// Union this with the last rect if they're adjacent.
if (!bounds->empty() && rect.SharesEdgeWith(bounds->back())) {
rect = rect.Union(bounds->back());
bounds->pop_back();
}
bounds->push_back(rect);
}
}
}
bool RenderTextWin::IsCursorablePosition(size_t position) {
if (position == 0 || position == text().length())
return true;
EnsureLayout();
size_t run_index = GetRunContainingPosition(position);
if (run_index >= runs_.size())
return false;
internal::TextRun* run = runs_[run_index];
size_t start = run->range.start();
if (position == start)
return true;
return run->logical_clusters[position - start] !=
run->logical_clusters[position - start - 1];
}
void RenderTextWin::UpdateLayout() {
// Layout is performed lazily as needed for drawing/metrics.
needs_layout_ = true;
}
void RenderTextWin::EnsureLayout() {
if (!needs_layout_)
return;
// TODO(msw): Skip complex processing if ScriptIsComplex returns false.
ItemizeLogicalText();
if (!runs_.empty())
LayoutVisualText();
needs_layout_ = false;
}
void RenderTextWin::DrawVisualText(Canvas* canvas) {
DCHECK(!needs_layout_);
Point offset(GetOriginForSkiaDrawing());
SkScalar x = SkIntToScalar(offset.x());
SkScalar y = SkIntToScalar(offset.y());
std::vector<SkPoint> pos;
internal::SkiaTextRenderer renderer(canvas);
for (size_t i = 0; i < runs_.size(); ++i) {
// Get the run specified by the visual-to-logical map.
internal::TextRun* run = runs_[visual_to_logical_[i]];
// Based on WebCore::skiaDrawText.
pos.resize(run->glyph_count);
SkScalar glyph_x = x;
for (int glyph = 0; glyph < run->glyph_count; glyph++) {
pos[glyph].set(glyph_x + run->offsets[glyph].du,
y + run->offsets[glyph].dv);
glyph_x += SkIntToScalar(run->advance_widths[glyph]);
}
renderer.SetFont(run->font);
renderer.SetForegroundColor(run->foreground);
renderer.DrawPosText(&pos[0], run->glyphs.get(), run->glyph_count);
if (run->strike || run->underline)
renderer.DrawDecorations(x, y, run->width, run->underline, run->strike);
x = glyph_x;
}
}
size_t RenderTextWin::IndexOfAdjacentGrapheme(size_t index, bool next) {
EnsureLayout();
if (text().empty())
return 0;
if (index >= text().length()) {
if (next || index > text().length()) {
return text().length();
} else {
// The requested |index| is at the end of the text. Use the index of the
// last character to find the grapheme.
index = text().length() - 1;
if (IsCursorablePosition(index))
return index;
}
}
size_t run_index = GetRunContainingPosition(index);
DCHECK(run_index < runs_.size());
internal::TextRun* run = runs_[run_index];
size_t start = run->range.start();
size_t ch = index - start;
if (!next) {
// If |ch| is the start of the run, use the preceding run, if any.
if (ch == 0) {
if (run_index == 0)
return 0;
run = runs_[run_index - 1];
start = run->range.start();
ch = run->range.length();
}
// Loop to find the start of the grapheme.
WORD cluster = run->logical_clusters[ch - 1];
do {
ch--;
} while (ch > 0 && run->logical_clusters[ch - 1] == cluster);
} else {
WORD cluster = run->logical_clusters[ch];
while (ch < run->range.length() && run->logical_clusters[ch] == cluster)
ch++;
}
return start + ch;
}
void RenderTextWin::ItemizeLogicalText() {
STLDeleteContainerPointers(runs_.begin(), runs_.end());
runs_.clear();
string_width_ = 0;
if (text().empty())
return;
const wchar_t* raw_text = text().c_str();
const int text_length = text().length();
HRESULT hr = E_OUTOFMEMORY;
int script_items_count = 0;
std::vector<SCRIPT_ITEM> script_items;
for (size_t n = kGuessItems; hr == E_OUTOFMEMORY && n < kMaxItems; n *= 2) {
// Derive the array of Uniscribe script items from the logical text.
// ScriptItemize always adds a terminal array item so that the length of the
// last item can be derived from the terminal SCRIPT_ITEM::iCharPos.
script_items.resize(n);
hr = ScriptItemize(raw_text,
text_length,
n - 1,
&script_control_,
&script_state_,
&script_items[0],
&script_items_count);
}
DCHECK(SUCCEEDED(hr));
if (script_items_count <= 0)
return;
// Build the list of runs, merge font/underline styles.
// TODO(msw): Only break for font changes, not color etc. See TextRun comment.
StyleRanges styles(style_ranges());
ApplyCompositionAndSelectionStyles(&styles);
StyleRanges::const_iterator style = styles.begin();
SCRIPT_ITEM* script_item = &script_items[0];
for (int run_break = 0; run_break < text_length;) {
internal::TextRun* run = new internal::TextRun();
run->range.set_start(run_break);
run->font = style->font;
run->foreground = style->foreground;
run->strike = style->strike;
run->underline = style->underline;
run->script_analysis = script_item->a;
// Find the range end and advance the structures as needed.
int script_item_end = (script_item + 1)->iCharPos;
int style_range_end = style->range.end();
run_break = std::min(script_item_end, style_range_end);
if (script_item_end <= style_range_end)
script_item++;
if (script_item_end >= style_range_end)
style++;
run->range.set_end(run_break);
runs_.push_back(run);
}
}
void RenderTextWin::LayoutVisualText() {
HRESULT hr = E_FAIL;
base::win::ScopedCreateDC hdc(CreateCompatibleDC(NULL));
std::vector<internal::TextRun*>::const_iterator run_iter;
for (run_iter = runs_.begin(); run_iter < runs_.end(); ++run_iter) {
internal::TextRun* run = *run_iter;
size_t run_length = run->range.length();
const wchar_t* run_text = &(text()[run->range.start()]);
bool tried_fallback = false;
// Select the font desired for glyph generation.
SelectObject(hdc, run->font.GetNativeFont());
run->logical_clusters.reset(new WORD[run_length]);
run->glyph_count = 0;
// Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx
size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16);
while (max_glyphs < kMaxGlyphs) {
run->glyphs.reset(new WORD[max_glyphs]);
run->visible_attributes.reset(new SCRIPT_VISATTR[max_glyphs]);
hr = ScriptShape(hdc,
&run->script_cache,
run_text,
run_length,
max_glyphs,
&(run->script_analysis),
run->glyphs.get(),
run->logical_clusters.get(),
run->visible_attributes.get(),
&(run->glyph_count));
if (hr == E_OUTOFMEMORY) {
max_glyphs *= 2;
} else if (hr == USP_E_SCRIPT_NOT_IN_FONT) {
// Only try font fallback if it hasn't yet been attempted for this run.
if (tried_fallback) {
// TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can
// crash on certain surrogate pairs with SCRIPT_UNDEFINED.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=341500
// And http://maxradi.us/documents/uniscribe/
run->script_analysis.eScript = SCRIPT_UNDEFINED;
// Reset |hr| to 0 to not trigger the DCHECK() below when a font is
// not found that can display the text. This is expected behavior
// under Windows XP without additional language packs installed and
// may also happen on newer versions when trying to display text in
// an obscure script that the system doesn't have the right font for.
hr = 0;
break;
}
// The run's font doesn't contain the required glyphs, use an alternate.
if (ChooseFallbackFont(hdc, run->font, run_text, run_length,
&run->font)) {
ScriptFreeCache(&run->script_cache);
SelectObject(hdc, run->font.GetNativeFont());
}
tried_fallback = true;
} else {
break;
}
}
DCHECK(SUCCEEDED(hr));
if (run->glyph_count > 0) {
run->advance_widths.reset(new int[run->glyph_count]);
run->offsets.reset(new GOFFSET[run->glyph_count]);
hr = ScriptPlace(hdc,
&run->script_cache,
run->glyphs.get(),
run->glyph_count,
run->visible_attributes.get(),
&(run->script_analysis),
run->advance_widths.get(),
run->offsets.get(),
&(run->abc_widths));
DCHECK(SUCCEEDED(hr));
}
}
if (runs_.size() > 0) {
// Build the array of bidirectional embedding levels.
scoped_array<BYTE> levels(new BYTE[runs_.size()]);
for (size_t i = 0; i < runs_.size(); ++i)
levels[i] = runs_[i]->script_analysis.s.uBidiLevel;
// Get the maps between visual and logical run indices.
visual_to_logical_.reset(new int[runs_.size()]);
logical_to_visual_.reset(new int[runs_.size()]);
hr = ScriptLayout(runs_.size(),
levels.get(),
visual_to_logical_.get(),
logical_to_visual_.get());
DCHECK(SUCCEEDED(hr));
}
// Precalculate run width information.
size_t preceding_run_widths = 0;
for (size_t i = 0; i < runs_.size(); ++i) {
internal::TextRun* run = runs_[visual_to_logical_[i]];
run->preceding_run_widths = preceding_run_widths;
const ABC& abc = run->abc_widths;
run->width = abc.abcA + abc.abcB + abc.abcC;
preceding_run_widths += run->width;
}
string_width_ = preceding_run_widths;
}
size_t RenderTextWin::GetRunContainingPosition(size_t position) const {
DCHECK(!needs_layout_);
// Find the text run containing the argument position.
size_t run = 0;
for (; run < runs_.size(); ++run)
if (runs_[run]->range.start() <= position &&
runs_[run]->range.end() > position)
break;
return run;
}
size_t RenderTextWin::GetRunContainingPoint(const Point& point) const {
DCHECK(!needs_layout_);
// Find the text run containing the argument point (assumed already offset).
size_t run = 0;
for (; run < runs_.size(); ++run)
if (runs_[run]->preceding_run_widths <= point.x() &&
runs_[run]->preceding_run_widths + runs_[run]->width > point.x())
break;
return run;
}
SelectionModel RenderTextWin::FirstSelectionModelInsideRun(
internal::TextRun* run) {
size_t caret = run->range.start();
size_t cursor = IndexOfAdjacentGrapheme(caret, true);
return SelectionModel(cursor, caret, SelectionModel::TRAILING);
}
SelectionModel RenderTextWin::LastSelectionModelInsideRun(
internal::TextRun* run) {
size_t caret = IndexOfAdjacentGrapheme(run->range.end(), false);
return SelectionModel(caret, caret, SelectionModel::LEADING);
}
SelectionModel RenderTextWin::LeftSelectionModel(
const SelectionModel& selection) {
DCHECK(!needs_layout_);
size_t caret = selection.caret_pos();
SelectionModel::CaretPlacement caret_placement = selection.caret_placement();
size_t run_index = GetRunContainingPosition(caret);
DCHECK(run_index < runs_.size());
internal::TextRun* run = runs_[run_index];
// If the caret's associated character is in a LTR run.
if (!run->script_analysis.fRTL) {
if (caret_placement == SelectionModel::TRAILING)
return SelectionModel(caret, caret, SelectionModel::LEADING);
else if (caret > run->range.start()) {
caret = IndexOfAdjacentGrapheme(caret, false);
return SelectionModel(caret, caret, SelectionModel::LEADING);
}
} else { // The caret's associated character is in a RTL run.
if (caret_placement == SelectionModel::LEADING) {
size_t cursor = IndexOfAdjacentGrapheme(caret, true);
return SelectionModel(cursor, caret, SelectionModel::TRAILING);
} else if (selection.selection_end() < run->range.end()) {
caret = IndexOfAdjacentGrapheme(caret, true);
size_t cursor = IndexOfAdjacentGrapheme(caret, true);
return SelectionModel(cursor, caret, SelectionModel::TRAILING);
}
}
// The character is at the begin of its run; go to the previous visual run.
size_t visual_index = logical_to_visual_[run_index];
if (visual_index == 0)
return LeftEndSelectionModel();
internal::TextRun* prev = runs_[visual_to_logical_[visual_index - 1]];
return prev->script_analysis.fRTL ? FirstSelectionModelInsideRun(prev) :
LastSelectionModelInsideRun(prev);
}
SelectionModel RenderTextWin::RightSelectionModel(
const SelectionModel& selection) {
DCHECK(!needs_layout_);
size_t caret = selection.caret_pos();
SelectionModel::CaretPlacement caret_placement = selection.caret_placement();
size_t run_index = GetRunContainingPosition(caret);
DCHECK(run_index < runs_.size());
internal::TextRun* run = runs_[run_index];
// If the caret's associated character is in a LTR run.
if (!run->script_analysis.fRTL) {
if (caret_placement == SelectionModel::LEADING) {
size_t cursor = IndexOfAdjacentGrapheme(caret, true);
return SelectionModel(cursor, caret, SelectionModel::TRAILING);
} else if (selection.selection_end() < run->range.end()) {
caret = IndexOfAdjacentGrapheme(caret, true);
size_t cursor = IndexOfAdjacentGrapheme(caret, true);
return SelectionModel(cursor, caret, SelectionModel::TRAILING);
}
} else { // The caret's associated character is in a RTL run.
if (caret_placement == SelectionModel::TRAILING)
return SelectionModel(caret, caret, SelectionModel::LEADING);
else if (caret > run->range.start()) {
caret = IndexOfAdjacentGrapheme(caret, false);
return SelectionModel(caret, caret, SelectionModel::LEADING);
}
}
// The character is at the end of its run; go to the next visual run.
size_t visual_index = logical_to_visual_[run_index];
if (visual_index == runs_.size() - 1)
return RightEndSelectionModel();
internal::TextRun* next = runs_[visual_to_logical_[visual_index + 1]];
return next->script_analysis.fRTL ? LastSelectionModelInsideRun(next) :
FirstSelectionModelInsideRun(next);
}
RenderText* RenderText::CreateRenderText() {
return new RenderTextWin;
}
} // namespace gfx
|