Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/render_text_win.h" | 5 #include "ui/gfx/render_text_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 } else { | 147 } else { |
| 148 result = ui::Range(run.logical_clusters[run_range.start()], | 148 result = ui::Range(run.logical_clusters[run_range.start()], |
| 149 run_range.end() < run.range.length() ? | 149 run_range.end() < run.range.length() ? |
| 150 run.logical_clusters[run_range.end()] : run.glyph_count); | 150 run.logical_clusters[run_range.end()] : run.glyph_count); |
| 151 } | 151 } |
| 152 DCHECK(!result.is_reversed()); | 152 DCHECK(!result.is_reversed()); |
| 153 DCHECK(ui::Range(0, run.glyph_count).Contains(result)); | 153 DCHECK(ui::Range(0, run.glyph_count).Contains(result)); |
| 154 return result; | 154 return result; |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Starting from |start_char|, finds a suitable line break position at or before | |
| 158 // |available_width| using word break info from |breaks|. If |empty_line| is | |
| 159 // true, this function is careful to not roll back to the line beginning. | |
|
msw
2013/08/15 02:44:17
nit: careful or guaranteed? That may matter to cal
ckocagil
2013/08/16 16:33:15
Done.
| |
| 160 // Returns whether the run was broken. | |
| 161 bool BreakRunAtWidth(const internal::TextRun& run, | |
| 162 const BreakList<size_t>& breaks, | |
| 163 size_t start_char, | |
| 164 int available_width, | |
| 165 bool empty_line, | |
| 166 int* width, | |
| 167 size_t* pos) { | |
| 168 DCHECK(run.range.Contains(ui::Range(start_char))); | |
| 169 BreakList<size_t>::const_iterator current_word = breaks.GetBreak(start_char); | |
| 170 BreakList<size_t>::const_iterator next_word = current_word + 1; | |
| 171 // x distance from |current_word|. | |
| 172 int current_word_x = 0; | |
|
msw
2013/08/15 02:44:17
|word_width| or |current_word_width| seems clearer
ckocagil
2013/08/16 16:33:15
Done, it's |word_width| now.
| |
| 173 // x distance from |start_char|. | |
| 174 int x = 0; | |
|
msw
2013/08/15 02:44:17
just use |*width| instead of declaring |x|.
ckocagil
2013/08/16 16:33:15
Done.
| |
| 175 | |
| 176 for (size_t i = start_char; i < run.range.end(); ++i) { | |
|
msw
2013/08/15 02:44:17
1) I suspect you'll need to check IsCursorablePosi
ckocagil
2013/08/16 16:33:15
TextRun::logical_clusters has cluster information,
| |
| 177 if (next_word != breaks.breaks().end() && i >= next_word->first) { | |
| 178 current_word = next_word++; | |
| 179 current_word_x = 0; | |
| 180 } | |
| 181 | |
| 182 ui::Range glyphs = CharRangeToGlyphRange(run, ui::Range(i, i + 1)); | |
| 183 int char_width = 0; | |
| 184 for (size_t j = glyphs.start(); j < glyphs.end(); ++j) | |
| 185 char_width += run.advance_widths[j]; | |
| 186 | |
| 187 x += char_width; | |
| 188 current_word_x += char_width; | |
| 189 | |
| 190 if (x > available_width) { | |
| 191 if (!empty_line || current_word_x < x) { | |
| 192 *width = x - current_word_x; | |
| 193 *pos = std::max(current_word->first, start_char); | |
| 194 } else if (char_width < x) { | |
| 195 *width = x - char_width; | |
| 196 *pos = i; | |
| 197 } else { | |
| 198 *width = x; | |
| 199 *pos = i + 1; | |
| 200 } | |
| 201 | |
| 202 return true; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 *width = x; | |
| 207 *pos = run.range.end(); | |
| 208 return false; | |
| 209 } | |
| 210 | |
| 157 } // namespace | 211 } // namespace |
| 158 | 212 |
| 159 namespace internal { | 213 namespace internal { |
| 160 | 214 |
| 161 TextRun::TextRun() | 215 TextRun::TextRun() |
| 162 : font_style(0), | 216 : font_style(0), |
| 163 strike(false), | 217 strike(false), |
| 164 diagonal_strike(false), | 218 diagonal_strike(false), |
| 165 underline(false), | 219 underline(false), |
| 166 width(0), | 220 width(0), |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 190 run->glyph_count, | 244 run->glyph_count, |
| 191 run->logical_clusters.get(), | 245 run->logical_clusters.get(), |
| 192 run->visible_attributes.get(), | 246 run->visible_attributes.get(), |
| 193 run->advance_widths.get(), | 247 run->advance_widths.get(), |
| 194 &run->script_analysis, | 248 &run->script_analysis, |
| 195 &x); | 249 &x); |
| 196 DCHECK(SUCCEEDED(hr)); | 250 DCHECK(SUCCEEDED(hr)); |
| 197 return run->preceding_run_widths + x; | 251 return run->preceding_run_widths + x; |
| 198 } | 252 } |
| 199 | 253 |
| 254 struct LineSegmentWin : LineSegment { | |
| 255 const internal::TextRun* run; | |
|
Alexei Svitkine (slow)
2013/08/14 12:59:43
How about instead of storing a pointer to the run,
ckocagil
2013/08/14 14:26:30
I'm not sure about this, can you guess what kind o
ckocagil
2013/08/16 16:33:15
Done.
| |
| 256 }; | |
| 257 | |
| 258 // Internal class to help break a text into lines. | |
| 259 class LineBreaker { | |
| 260 public: | |
| 261 LineBreaker(int max_width, bool multiline, const BreakList<size_t>* words) | |
|
msw
2013/08/15 02:44:17
nit: one param per line when the initialization li
ckocagil
2013/08/16 16:33:15
Done.
| |
| 262 : max_width_(max_width), | |
| 263 multiline_(multiline), | |
|
msw
2013/08/15 02:44:17
Why is LineBreaker even used if multiline is false
ckocagil
2013/08/16 16:33:15
I don't think we should create a new function for
| |
| 264 words_(words), | |
| 265 preceding_line_heights_(0), | |
| 266 pos_(0), | |
| 267 text_x_(0), | |
| 268 line_x_(0), | |
| 269 current_pos_(0), | |
| 270 current_width_(0) { | |
| 271 SkipLine(); | |
| 272 } | |
| 273 | |
| 274 // Breaks the given |runs| into |lines|. Should be called at most once for | |
| 275 // each instance. If |multiline| is false, doesn't do any breaking and fills | |
|
msw
2013/08/15 02:44:17
nit: "each run instance", right? Better to be expl
ckocagil
2013/08/16 16:33:15
Oh, this comment doesn't even apply anymore. Remov
| |
| 276 // |lines| with a single Line. | |
| 277 void AddRun(const internal::TextRun& run) { | |
|
msw
2013/08/15 02:44:17
Make this private, scrap Finalize and only expose
Alexei Svitkine (slow)
2013/08/15 14:51:05
I disagree. I think it's wasteful to create a temp
msw
2013/08/15 15:42:09
I'll concede here, LineBreaker isn't too complex f
| |
| 278 ResetPos(run.range.start()); | |
| 279 int width = run.width; | |
| 280 if (multiline_ && line_x_ + width > max_width_) | |
| 281 width = BreakRun(run); | |
| 282 // Remaining part of the run fits the line, add it as well. | |
| 283 Advance(run.range.end(), width); | |
| 284 CommitSegment(run); | |
|
Alexei Svitkine (slow)
2013/08/14 12:59:43
Advance() and CommitSegment() always get called to
msw
2013/08/15 02:44:17
Agreed, I like "commit" for adding text, and this
ckocagil
2013/08/16 16:33:15
All done! Advance() and CommitSegment() are now me
| |
| 285 } | |
| 286 | |
| 287 // Finishes line breaking and outputs the results. Can be called at most once. | |
| 288 void Finalize(std::vector<internal::Line>* lines) { | |
| 289 DCHECK(!lines_.empty()); | |
| 290 lines->swap(lines_); | |
| 291 } | |
| 292 | |
| 293 private: | |
| 294 // Breaks a run into segments of at most |max_width| width, adds the segments, | |
|
msw
2013/08/15 02:44:17
nit: |max_width_|
ckocagil
2013/08/16 16:33:15
Done.
| |
| 295 // returns the width of the final segment that fits the current line. | |
| 296 int BreakRun(const internal::TextRun& run) { | |
| 297 int width = 0; | |
| 298 size_t pos = 0; | |
| 299 | |
| 300 // Break the run until it fits the current line. | |
| 301 while (BreakRunAtWidth(run, *words_, pos_, max_width_ - line_x_, | |
|
msw
2013/08/15 02:44:17
This is de-referencing words_, which might technic
ckocagil
2013/08/16 16:33:15
Added DCHECK.
| |
| 302 line_x_ == 0, &width, &pos)) { | |
| 303 DCHECK(pos_ < run.range.end()); | |
|
msw
2013/08/15 02:44:17
nit: DCHECK_LT.
ckocagil
2013/08/16 16:33:15
Done.
| |
| 304 Advance(pos, width); | |
| 305 CommitSegment(run); | |
| 306 SkipLine(); | |
| 307 } | |
| 308 | |
| 309 return width; | |
| 310 } | |
| 311 | |
| 312 // RTL runs are broken in logical order but displayed in visual order. To find | |
| 313 // the text-space coordinate (where it would fall in a single-line text) | |
| 314 // |x_pos| of RTL segments, segment widths are applied in reverse order. | |
| 315 // e.g. {[5, 10], [10, 40]} will become {[35, 40], [5, 35]}. | |
| 316 void PopRtl() { | |
| 317 if (rtl_segments_.empty()) | |
| 318 return; | |
| 319 int x = rtl_segments_[0]->x_pos.start(); | |
| 320 for (size_t i = rtl_segments_.size(); i > 0; --i) { | |
| 321 LineSegment* segment = rtl_segments_[i - 1]; | |
| 322 segment->x_pos = ui::Range(x, x + segment->x_pos.length()); | |
| 323 x += segment->x_pos.length(); | |
| 324 } | |
| 325 rtl_segments_.clear(); | |
| 326 } | |
| 327 | |
| 328 void ResetPos(size_t new_pos) { | |
|
msw
2013/08/15 02:44:17
This is called once, consider inlining it.
ckocagil
2013/08/16 16:33:15
Done.
| |
| 329 pos_ = new_pos; | |
| 330 current_pos_ = new_pos; | |
| 331 current_width_ = 0; | |
| 332 } | |
| 333 | |
| 334 void Advance(size_t new_pos, int amount) { | |
| 335 current_pos_ = new_pos; | |
| 336 current_width_ += amount; | |
| 337 } | |
| 338 | |
| 339 void SkipLine() { | |
|
msw
2013/08/15 02:44:17
nit: AdvanceLine seems more appropriate.
ckocagil
2013/08/16 16:33:15
Done.
| |
| 340 if (!lines_.empty()) | |
| 341 preceding_line_heights_ += lines_.back().height; | |
| 342 line_x_ = 0; | |
| 343 lines_.push_back(internal::Line()); | |
| 344 } | |
| 345 | |
| 346 void CommitSegment(const internal::TextRun& run) { | |
| 347 if (pos_ == current_pos_) { | |
| 348 DCHECK(current_width_ == 0); | |
|
msw
2013/08/15 02:44:17
nit: DCHECK_EQ
ckocagil
2013/08/16 16:33:15
Done.
| |
| 349 return; | |
| 350 } | |
| 351 internal::LineSegmentWin* segment = new internal::LineSegmentWin; | |
| 352 segment->run = &run; | |
| 353 segment->char_pos = ui::Range(pos_, current_pos_); | |
| 354 segment->x_pos = ui::Range(text_x_, text_x_ + current_width_); | |
| 355 lines_.back().segments.push_back(segment); | |
|
msw
2013/08/15 02:44:17
nit: cache lines_.back() as a local ptr |line|.
ckocagil
2013/08/16 16:33:15
Done.
| |
| 356 lines_.back().width += segment->x_pos.length(); | |
| 357 lines_.back().height = std::max(lines_.back().height, | |
| 358 segment->run->font.GetHeight()); | |
| 359 lines_.back().baseline = std::max(lines_.back().baseline, | |
|
msw
2013/08/15 02:44:17
I think the line height and baseline might have to
ckocagil
2013/08/16 16:33:15
Done. FontList looks like it can only contain font
| |
| 360 segment->run->font.GetBaseline()); | |
| 361 lines_.back().preceding_heights = preceding_line_heights_; | |
|
msw
2013/08/15 02:44:17
Must this be altered on each committed segment? Ca
ckocagil
2013/08/16 16:33:15
Done.
| |
| 362 if (run.script_analysis.fRTL) { | |
| 363 rtl_segments_.push_back(segment); | |
| 364 if (current_pos_ == run.range.end()) | |
| 365 PopRtl(); | |
| 366 } | |
| 367 pos_ = current_pos_; | |
| 368 text_x_ += current_width_; | |
| 369 line_x_ += current_width_; | |
| 370 current_width_ = 0; | |
| 371 } | |
| 372 | |
| 373 int max_width_; | |
|
msw
2013/08/15 02:44:17
nit: const
ckocagil
2013/08/16 16:33:15
Done.
| |
| 374 bool multiline_; | |
|
msw
2013/08/15 02:44:17
nit: const
ckocagil
2013/08/16 16:33:15
Done. Also done for |words_| below.
| |
| 375 const BreakList<size_t>* words_; | |
| 376 | |
| 377 // Stores the resulting lines. | |
| 378 std::vector<internal::Line> lines_; | |
| 379 | |
| 380 // Position information of the last committed segment. |pos_| is the segment's | |
| 381 // end character position. |text_x_| is the text-space and |line_x_| is the | |
|
msw
2013/08/15 02:44:17
nit: |text_x_| and |line_x_| are text-space and li
ckocagil
2013/08/16 16:33:15
Done.
| |
| 382 // line-space x coordinate of |pos_|. | |
| 383 size_t pos_; | |
| 384 int text_x_; | |
| 385 int line_x_; | |
| 386 int preceding_line_heights_; | |
| 387 | |
| 388 // The end character position and width of the current segment to be | |
| 389 // committed. The segment will have the character range [pos_, current_pos_) | |
| 390 // and width |current_width_|. | |
| 391 size_t current_pos_; | |
|
msw
2013/08/15 02:44:17
Would |next_pos_| and |next_width_| be more approp
ckocagil
2013/08/16 16:33:15
They're gone.
| |
| 392 int current_width_; | |
| 393 | |
| 394 // Segments to be applied by |PopRtl()|. | |
| 395 std::vector<LineSegment*> rtl_segments_; | |
| 396 | |
| 397 DISALLOW_COPY_AND_ASSIGN(LineBreaker); | |
| 398 }; | |
| 399 | |
| 200 } // namespace internal | 400 } // namespace internal |
| 201 | 401 |
| 202 // static | 402 // static |
| 203 HDC RenderTextWin::cached_hdc_ = NULL; | 403 HDC RenderTextWin::cached_hdc_ = NULL; |
| 204 | 404 |
| 205 // static | 405 // static |
| 206 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; | 406 std::map<std::string, Font> RenderTextWin::successful_substitute_fonts_; |
| 207 | 407 |
| 208 RenderTextWin::RenderTextWin() | 408 RenderTextWin::RenderTextWin() |
| 209 : RenderText(), | 409 : RenderText(), |
| 210 common_baseline_(0), | 410 common_baseline_(0), |
| 211 needs_layout_(false) { | 411 needs_layout_(false) { |
| 212 set_truncate_length(kMaxUniscribeTextLength); | 412 set_truncate_length(kMaxUniscribeTextLength); |
| 213 | 413 |
| 214 memset(&script_control_, 0, sizeof(script_control_)); | 414 memset(&script_control_, 0, sizeof(script_control_)); |
| 215 memset(&script_state_, 0, sizeof(script_state_)); | 415 memset(&script_state_, 0, sizeof(script_state_)); |
| 216 | 416 |
| 217 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); | 417 MoveCursorTo(EdgeSelectionModel(CURSOR_LEFT)); |
| 218 } | 418 } |
| 219 | 419 |
| 220 RenderTextWin::~RenderTextWin() { | 420 RenderTextWin::~RenderTextWin() { |
| 221 } | 421 } |
| 222 | 422 |
| 223 Size RenderTextWin::GetStringSize() { | 423 Size RenderTextWin::GetStringSize() { |
| 224 EnsureLayout(); | 424 EnsureLayout(); |
| 225 return string_size_; | 425 return string_size_; |
| 226 } | 426 } |
| 227 | 427 |
| 428 Size RenderTextWin::GetMultilineTextSize() { | |
| 429 EnsureLayout(); | |
| 430 if (!multiline()) | |
| 431 return Size(display_rect().width(), string_size_.height()); | |
| 432 return Size(display_rect().width(), | |
|
msw
2013/08/15 02:44:17
Should this actually be dislpay_rect().width() if
ckocagil
2013/08/16 16:33:15
Done.
| |
| 433 lines().back().preceding_heights + lines().back().height); | |
| 434 } | |
| 435 | |
| 228 int RenderTextWin::GetBaseline() { | 436 int RenderTextWin::GetBaseline() { |
| 229 EnsureLayout(); | 437 EnsureLayout(); |
| 230 return common_baseline_; | 438 return common_baseline_; |
| 231 } | 439 } |
| 232 | 440 |
| 233 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { | 441 SelectionModel RenderTextWin::FindCursorPosition(const Point& point) { |
| 234 if (text().empty()) | 442 if (text().empty()) |
| 235 return SelectionModel(); | 443 return SelectionModel(); |
| 236 | 444 |
| 237 EnsureLayout(); | 445 EnsureLayout(); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 GetGlyphXBoundary(run, layout_index, true)); | 584 GetGlyphXBoundary(run, layout_index, true)); |
| 377 } | 585 } |
| 378 | 586 |
| 379 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { | 587 std::vector<Rect> RenderTextWin::GetSubstringBounds(const ui::Range& range) { |
| 380 DCHECK(!needs_layout_); | 588 DCHECK(!needs_layout_); |
| 381 DCHECK(ui::Range(0, text().length()).Contains(range)); | 589 DCHECK(ui::Range(0, text().length()).Contains(range)); |
| 382 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), | 590 ui::Range layout_range(TextIndexToLayoutIndex(range.start()), |
| 383 TextIndexToLayoutIndex(range.end())); | 591 TextIndexToLayoutIndex(range.end())); |
| 384 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); | 592 DCHECK(ui::Range(0, GetLayoutText().length()).Contains(layout_range)); |
| 385 | 593 |
| 386 std::vector<Rect> bounds; | 594 std::vector<ui::Range> bounds; |
|
msw
2013/08/15 02:44:17
nit: declare |bounds| after the early return.
ckocagil
2013/08/16 16:33:15
Done.
| |
| 595 std::vector<Rect> rects; | |
| 387 if (layout_range.is_empty()) | 596 if (layout_range.is_empty()) |
| 388 return bounds; | 597 return rects; |
| 389 | 598 |
| 390 // Add a Rect for each run/selection intersection. | 599 // Add a Range for each run/selection intersection. |
| 391 // TODO(msw): The bounds should probably not always be leading the range ends. | 600 // TODO(msw): The bounds should probably not always be leading the range ends. |
| 392 for (size_t i = 0; i < runs_.size(); ++i) { | 601 for (size_t i = 0; i < runs_.size(); ++i) { |
| 393 const internal::TextRun* run = runs_[visual_to_logical_[i]]; | 602 const internal::TextRun* run = runs_[visual_to_logical_[i]]; |
| 394 ui::Range intersection = run->range.Intersect(layout_range); | 603 ui::Range intersection = run->range.Intersect(layout_range); |
| 395 if (intersection.IsValid()) { | 604 if (intersection.IsValid()) { |
| 396 DCHECK(!intersection.is_reversed()); | 605 DCHECK(!intersection.is_reversed()); |
| 397 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), | 606 ui::Range range_x(GetGlyphXBoundary(run, intersection.start(), false), |
| 398 GetGlyphXBoundary(run, intersection.end(), false)); | 607 GetGlyphXBoundary(run, intersection.end(), false)); |
| 399 Rect rect(range_x.GetMin(), 0, range_x.length(), run->font.GetHeight()); | 608 if (range_x.is_empty()) |
| 400 rect.set_origin(ToViewPoint(rect.origin())); | 609 continue; |
| 401 // Union this with the last rect if they're adjacent. | 610 range_x = ui::Range(range_x.GetMin(), range_x.GetMax()); |
| 402 if (!bounds.empty() && rect.SharesEdgeWith(bounds.back())) { | 611 // Union this with the last range if they're adjacent. |
| 403 rect.Union(bounds.back()); | 612 DCHECK(bounds.empty() || bounds.back().GetMin() != range_x.GetMax()); |
| 613 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { | |
| 614 range_x = ui::Range(bounds.back().GetMin(), range_x.GetMax()); | |
| 404 bounds.pop_back(); | 615 bounds.pop_back(); |
| 405 } | 616 } |
| 406 bounds.push_back(rect); | 617 bounds.push_back(range_x); |
| 407 } | 618 } |
| 408 } | 619 } |
| 409 return bounds; | 620 for (size_t i = 0; i < bounds.size(); ++i) { |
| 621 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); | |
| 622 rects.insert(rects.end(), current_rects.begin(), current_rects.end()); | |
| 623 } | |
| 624 return rects; | |
| 410 } | 625 } |
| 411 | 626 |
| 412 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { | 627 size_t RenderTextWin::TextIndexToLayoutIndex(size_t index) const { |
| 413 DCHECK_LE(index, text().length()); | 628 DCHECK_LE(index, text().length()); |
| 414 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; | 629 ptrdiff_t i = obscured() ? ui::UTF16IndexToOffset(text(), 0, index) : index; |
| 415 CHECK_GE(i, 0); | 630 CHECK_GE(i, 0); |
| 416 // Clamp layout indices to the length of the text actually used for layout. | 631 // Clamp layout indices to the length of the text actually used for layout. |
| 417 return std::min<size_t>(GetLayoutText().length(), i); | 632 return std::min<size_t>(GetLayoutText().length(), i); |
| 418 } | 633 } |
| 419 | 634 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 441 position < LayoutIndexToTextIndex(GetLayoutText().length()) && | 656 position < LayoutIndexToTextIndex(GetLayoutText().length()) && |
| 442 GetGlyphBounds(position) != GetGlyphBounds(position - 1); | 657 GetGlyphBounds(position) != GetGlyphBounds(position - 1); |
| 443 } | 658 } |
| 444 | 659 |
| 445 void RenderTextWin::ResetLayout() { | 660 void RenderTextWin::ResetLayout() { |
| 446 // Layout is performed lazily as needed for drawing/metrics. | 661 // Layout is performed lazily as needed for drawing/metrics. |
| 447 needs_layout_ = true; | 662 needs_layout_ = true; |
| 448 } | 663 } |
| 449 | 664 |
| 450 void RenderTextWin::EnsureLayout() { | 665 void RenderTextWin::EnsureLayout() { |
| 451 if (!needs_layout_) | 666 if (needs_layout_) { |
| 452 return; | 667 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. |
| 453 // TODO(msw): Skip complex processing if ScriptIsComplex returns false. | 668 ItemizeLogicalText(); |
| 454 ItemizeLogicalText(); | 669 if (!runs_.empty()) |
| 455 if (!runs_.empty()) | 670 LayoutVisualText(); |
| 456 LayoutVisualText(); | 671 needs_layout_ = false; |
| 457 needs_layout_ = false; | 672 std::vector<internal::Line> lines; |
| 673 set_lines(&lines); | |
| 674 } | |
| 675 // Compute lines if they're not valid. This is separate from the layout steps | |
| 676 // above to avoid text layout and shaping when we resize |display_rect_|. | |
| 677 if (lines().empty()) | |
| 678 ComputeLines(); | |
| 679 } | |
| 680 | |
| 681 void RenderTextWin::ComputeLines() { | |
|
msw
2013/08/15 02:44:17
Inline this in EnsureLayout
ckocagil
2013/08/16 16:33:15
Done.
| |
| 682 DCHECK(!needs_layout_); | |
| 683 std::vector<internal::Line> lines; | |
| 684 internal::LineBreaker line_breaker(display_rect().width(), multiline(), | |
| 685 multiline() ? &GetLineBreaks() : 0); | |
|
msw
2013/08/15 02:44:17
Use NULL, not 0. Moreso, avoid LineBreaker for sin
ckocagil
2013/08/16 16:33:15
Replaced 0 with NULL.
I'm using LineBreaker for s
msw
2013/08/16 19:05:02
It's just weird to create and use a LineBreaker to
ckocagil
2013/08/20 21:25:07
Expanded the comment above the definition of LineB
| |
| 686 for (size_t i = 0; i < runs_.size(); ++i) | |
| 687 line_breaker.AddRun(*runs_[visual_to_logical_[i]]); | |
| 688 line_breaker.Finalize(&lines); | |
| 689 DCHECK(!lines.empty()); | |
| 690 set_lines(&lines); | |
| 458 } | 691 } |
| 459 | 692 |
| 460 void RenderTextWin::DrawVisualText(Canvas* canvas) { | 693 void RenderTextWin::DrawVisualText(Canvas* canvas) { |
| 461 DCHECK(!needs_layout_); | 694 DCHECK(!needs_layout_); |
| 462 | 695 DCHECK(!multiline() || !lines().empty()); |
| 463 // Skia will draw glyphs with respect to the baseline. | |
| 464 Vector2d offset(GetTextOffset() + Vector2d(0, common_baseline_)); | |
| 465 | |
| 466 SkScalar x = SkIntToScalar(offset.x()); | |
| 467 SkScalar y = SkIntToScalar(offset.y()); | |
| 468 | 696 |
| 469 std::vector<SkPoint> pos; | 697 std::vector<SkPoint> pos; |
| 470 | 698 |
| 471 internal::SkiaTextRenderer renderer(canvas); | 699 internal::SkiaTextRenderer renderer(canvas); |
| 472 ApplyFadeEffects(&renderer); | 700 ApplyFadeEffects(&renderer); |
| 473 ApplyTextShadows(&renderer); | 701 ApplyTextShadows(&renderer); |
| 474 | 702 |
| 475 bool smoothing_enabled; | 703 bool smoothing_enabled; |
| 476 bool cleartype_enabled; | 704 bool cleartype_enabled; |
| 477 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); | 705 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); |
| 478 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. | 706 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. |
| 479 renderer.SetFontSmoothingSettings( | 707 renderer.SetFontSmoothingSettings( |
| 480 smoothing_enabled, cleartype_enabled && !background_is_transparent()); | 708 smoothing_enabled, cleartype_enabled && !background_is_transparent()); |
| 481 | 709 |
| 482 ApplyCompositionAndSelectionStyles(); | 710 ApplyCompositionAndSelectionStyles(); |
| 483 | 711 |
| 484 for (size_t i = 0; i < runs_.size(); ++i) { | 712 for (size_t i = 0; i < lines().size(); ++i) { |
| 485 // Get the run specified by the visual-to-logical map. | 713 const internal::Line& line = lines()[i]; |
| 486 internal::TextRun* run = runs_[visual_to_logical_[i]]; | 714 Vector2d line_offset = GetLineOffset(i); |
| 715 Vector2d text_offset = line_offset + Vector2d(0, line.baseline); | |
|
msw
2013/08/15 02:44:17
Hmm, I'm not clear on why we add the baseline to t
ckocagil
2013/08/16 16:33:15
AFAICT Skia vertically aligns (0, 0) with the base
| |
| 716 int preceding_segment_widths = 0; | |
| 487 | 717 |
| 488 // Skip painting empty runs and runs outside the display rect area. | 718 // Skip painting empty lines or lines outside the display rect area. |
| 489 if ((run->glyph_count == 0) || (x >= display_rect().right()) || | 719 if (!display_rect().Intersects(Rect(PointAtOffsetFromOrigin(line_offset), |
| 490 (x + run->width <= display_rect().x())) { | 720 Size(line.width, line.height)))) |
| 491 x += run->width; | |
| 492 continue; | 721 continue; |
| 722 | |
| 723 for (size_t j = 0; j < line.segments.size(); ++j) { | |
| 724 const internal::LineSegmentWin* segment = | |
| 725 static_cast<internal::LineSegmentWin*>(line.segments[j]); | |
| 726 const int segment_width = segment->x_pos.length(); | |
| 727 const internal::TextRun* run = segment->run; | |
| 728 DCHECK(!segment->char_pos.is_empty()); | |
| 729 DCHECK(run->range.Contains(segment->char_pos)); | |
| 730 ui::Range glyphs = CharRangeToGlyphRange(*run, segment->char_pos); | |
| 731 if (glyphs.is_empty()) { | |
| 732 DCHECK(segment_width == 0); | |
| 733 continue; | |
| 734 } | |
| 735 // Skip painting segments outside the display rect area. | |
|
msw
2013/08/15 02:44:17
This should only happen for visible lines if the l
ckocagil
2013/08/16 16:33:15
Done.
| |
| 736 Rect segment_bounds(PointAtOffsetFromOrigin(line_offset) + | |
|
msw
2013/08/15 02:44:17
nit: const
ckocagil
2013/08/16 16:33:15
Done.
| |
| 737 Vector2d(preceding_segment_widths, 0), | |
| 738 Size(segment_width, line.height)); | |
| 739 if (!display_rect().Intersects(segment_bounds)) { | |
| 740 preceding_segment_widths += segment_width; | |
| 741 continue; | |
| 742 } | |
| 743 | |
| 744 int segment_x = 0; | |
| 745 pos.resize(glyphs.length()); | |
|
msw
2013/08/15 02:44:17
Does it help to keep using a terminal pos entry?
ckocagil
2013/08/16 16:33:15
I don't see a place where it would.
msw
2013/08/16 19:05:02
At the SkScalar width = (colored_glyphs.end() < gl
ckocagil
2013/08/20 00:09:06
Done.
| |
| 746 for (size_t g = glyphs.start(); g < glyphs.end(); ++g) { | |
| 747 pos[g - glyphs.start()].set( | |
| 748 SkIntToScalar(text_offset.x() + preceding_segment_widths + | |
| 749 segment_x + run->offsets[g].du), | |
| 750 SkIntToScalar(text_offset.y() + run->offsets[g].dv)); | |
| 751 segment_x += run->advance_widths[g]; | |
| 752 } | |
| 753 | |
| 754 renderer.SetTextSize(run->font.GetFontSize()); | |
| 755 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style); | |
| 756 | |
| 757 for (BreakList<SkColor>::const_iterator it = | |
| 758 colors().GetBreak(run->range.start()); | |
| 759 it != colors().breaks().end() && it->first < run->range.end(); | |
| 760 ++it) { | |
| 761 ui::Range intersection = colors().GetRange(it).Intersect( | |
|
msw
2013/08/15 02:44:17
nit: const
ckocagil
2013/08/16 16:33:15
Done.
| |
| 762 segment->char_pos); | |
| 763 ui::Range colored_glyphs = CharRangeToGlyphRange(*run, intersection); | |
|
msw
2013/08/15 02:44:17
nit: const
ckocagil
2013/08/16 16:33:15
Done.
| |
| 764 DCHECK(glyphs.Contains(colored_glyphs)); | |
| 765 if (colored_glyphs.is_empty()) | |
| 766 continue; | |
| 767 renderer.SetForegroundColor(it->second); | |
| 768 renderer.DrawPosText(&pos[colored_glyphs.start() - glyphs.start()], | |
| 769 &run->glyphs[colored_glyphs.start()], | |
| 770 colored_glyphs.length()); | |
| 771 SkScalar width = (colored_glyphs.end() < glyphs.end() | |
|
msw
2013/08/15 02:44:17
nit: const
ckocagil
2013/08/16 16:33:15
Done.
| |
| 772 ? pos[colored_glyphs.end() - glyphs.start()].x() | |
|
msw
2013/08/15 02:44:17
nit: '?' goes on the line above, ':' goes at the e
ckocagil
2013/08/16 16:33:15
Done.
| |
| 773 : pos[0].x() + SkIntToScalar(segment_width)) | |
| 774 - pos[colored_glyphs.start() - glyphs.start()].x(); | |
| 775 renderer.DrawDecorations( | |
| 776 pos[colored_glyphs.start() - glyphs.start()].x(), text_offset.y(), | |
| 777 SkScalarCeilToInt(width), run->underline, run->strike, | |
| 778 run->diagonal_strike); | |
| 779 } | |
| 780 | |
| 781 preceding_segment_widths += segment_width; | |
| 493 } | 782 } |
| 494 | |
| 495 // Based on WebCore::skiaDrawText. |pos| contains the positions of glyphs. | |
| 496 // An extra terminal |pos| entry is added to simplify width calculations. | |
| 497 pos.resize(run->glyph_count + 1); | |
| 498 SkScalar glyph_x = x; | |
| 499 for (int glyph = 0; glyph < run->glyph_count; glyph++) { | |
| 500 pos[glyph].set(glyph_x + run->offsets[glyph].du, | |
| 501 y + run->offsets[glyph].dv); | |
| 502 glyph_x += SkIntToScalar(run->advance_widths[glyph]); | |
| 503 } | |
| 504 pos.back().set(glyph_x, y); | |
| 505 | |
| 506 renderer.SetTextSize(run->font.GetFontSize()); | |
| 507 renderer.SetFontFamilyWithStyle(run->font.GetFontName(), run->font_style); | |
| 508 | |
| 509 for (BreakList<SkColor>::const_iterator it = | |
| 510 colors().GetBreak(run->range.start()); | |
| 511 it != colors().breaks().end() && it->first < run->range.end(); | |
| 512 ++it) { | |
| 513 const ui::Range glyph_range = CharRangeToGlyphRange(*run, | |
| 514 colors().GetRange(it).Intersect(run->range)); | |
| 515 if (glyph_range.is_empty()) | |
| 516 continue; | |
| 517 renderer.SetForegroundColor(it->second); | |
| 518 renderer.DrawPosText(&pos[glyph_range.start()], | |
| 519 &run->glyphs[glyph_range.start()], | |
| 520 glyph_range.length()); | |
| 521 const SkScalar width = pos[glyph_range.end()].x() - | |
| 522 pos[glyph_range.start()].x(); | |
| 523 renderer.DrawDecorations(pos[glyph_range.start()].x(), y, | |
| 524 SkScalarCeilToInt(width), run->underline, | |
| 525 run->strike, run->diagonal_strike); | |
| 526 } | |
| 527 | |
| 528 DCHECK_EQ(glyph_x - x, run->width); | |
| 529 x = glyph_x; | |
| 530 } | 783 } |
| 531 | 784 |
| 532 UndoCompositionAndSelectionStyles(); | 785 UndoCompositionAndSelectionStyles(); |
| 533 } | 786 } |
| 534 | 787 |
| 535 void RenderTextWin::ItemizeLogicalText() { | 788 void RenderTextWin::ItemizeLogicalText() { |
| 536 runs_.clear(); | 789 runs_.clear(); |
| 537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw | 790 // Make |string_size_|'s height and |common_baseline_| tall enough to draw |
| 538 // often-used characters which are rendered with fonts in the font list. | 791 // often-used characters which are rendered with fonts in the font list. |
| 539 string_size_ = Size(0, font_list().GetHeight()); | 792 string_size_ = Size(0, font_list().GetHeight()); |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 897 size_t position = LayoutIndexToTextIndex(run->range.end()); | 1150 size_t position = LayoutIndexToTextIndex(run->range.end()); |
| 898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | 1151 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
| 899 return SelectionModel(position, CURSOR_FORWARD); | 1152 return SelectionModel(position, CURSOR_FORWARD); |
| 900 } | 1153 } |
| 901 | 1154 |
| 902 RenderText* RenderText::CreateInstance() { | 1155 RenderText* RenderText::CreateInstance() { |
| 903 return new RenderTextWin; | 1156 return new RenderTextWin; |
| 904 } | 1157 } |
| 905 | 1158 |
| 906 } // namespace gfx | 1159 } // namespace gfx |
| OLD | NEW |