| 405 | } |
| 406 | |
| 407 | std::vector<SpanLayout::TextBlock> SpanLayout::FindTextBlocks() |
| 408 | { |
| 409 | std::vector<TextBlock> blocks; |
| 410 | std::vector<SpanObject>::iterator block_object_it; |
| 411 | |
| 412 | // Find first object that is not text: |
| 413 | for (block_object_it = objects.begin(); block_object_it != objects.end() && (*block_object_it).type == object_text; ++block_object_it); |
| 414 | |
| 415 | std::string::size_type pos = 0; |
| 416 | while (pos < text.size()) |
| 417 | { |
| 418 | // Find end of text block: |
| 419 | std::string::size_type end_pos; |
| 420 | switch (text[pos]) |
| 421 | { |
| 422 | case ' ': |
| 423 | case '\t': |
| 424 | case '\n': |
| 425 | end_pos = text.find_first_not_of(text[pos], pos); |
| 426 | break; |
| 427 | default: |
| 428 | end_pos = text.find_first_of(" \t\n", pos); |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | if (end_pos == std::string::npos) |
| 433 | end_pos = text.length(); |
| 434 | |
| 435 | // If we traversed past an object that is not text: |
| 436 | if (block_object_it != objects.end() && (*block_object_it).start < end_pos) |
| 437 | { |
| 438 | // End text block |
| 439 | end_pos = (*block_object_it).start; |
| 440 | if (end_pos > pos) |
| 441 | { |
| 442 | TextBlock block; |
| 443 | block.start = pos; |
| 444 | block.end = end_pos; |
| 445 | blocks.push_back(block); |
| 446 | } |
| 447 | |
| 448 | // Create object block: |
| 449 | pos = end_pos; |
| 450 | end_pos = pos + 1; |
| 451 | |
| 452 | TextBlock block; |
| 453 | block.start = pos; |
| 454 | block.end = end_pos; |
| 455 | blocks.push_back(block); |
| 456 | |
| 457 | // Find next object that is not text: |
| 458 | for (++block_object_it; block_object_it != objects.end() && (*block_object_it).type == object_text; ++block_object_it); |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | if (end_pos > pos) |
| 463 | { |
| 464 | TextBlock block; |