///////////////////////////////////////////////////////
| 690 | |
| 691 | //////////////////////////////////////////////////////////// |
| 692 | IntRect Font::findGlyphRect(Page& page, Vector2u size) const |
| 693 | { |
| 694 | // Find the line that fits well the glyph |
| 695 | Row* row = nullptr; |
| 696 | float bestRatio = 0; |
| 697 | for (auto it = page.rows.begin(); it != page.rows.end() && !row; ++it) |
| 698 | { |
| 699 | const float ratio = static_cast<float>(size.y) / static_cast<float>(it->height); |
| 700 | |
| 701 | // Ignore rows that are either too small or too high |
| 702 | if ((ratio < 0.7f) || (ratio > 1.f)) |
| 703 | continue; |
| 704 | |
| 705 | // Check if there's enough horizontal space left in the row |
| 706 | if (size.x > page.texture.getSize().x - it->width) |
| 707 | continue; |
| 708 | |
| 709 | // Make sure that this new row is the best found so far |
| 710 | if (ratio < bestRatio) |
| 711 | continue; |
| 712 | |
| 713 | // The current row passed all the tests: we can select it |
| 714 | row = &*it; |
| 715 | bestRatio = ratio; |
| 716 | } |
| 717 | |
| 718 | // If we didn't find a matching row, create a new one (10% taller than the glyph) |
| 719 | if (!row) |
| 720 | { |
| 721 | const unsigned int rowHeight = size.y + size.y / 10; |
| 722 | while ((page.nextRow + rowHeight >= page.texture.getSize().y) || (size.x >= page.texture.getSize().x)) |
| 723 | { |
| 724 | // Not enough space: resize the texture if possible |
| 725 | const Vector2u textureSize = page.texture.getSize(); |
| 726 | if ((textureSize.x * 2 <= Texture::getMaximumSize()) && (textureSize.y * 2 <= Texture::getMaximumSize())) |
| 727 | { |
| 728 | // Make the texture 2 times bigger |
| 729 | Texture newTexture; |
| 730 | if (!newTexture.resize(textureSize * 2u)) |
| 731 | { |
| 732 | err() << "Failed to create new page texture" << std::endl; |
| 733 | return {{0, 0}, {2, 2}}; |
| 734 | } |
| 735 | |
| 736 | newTexture.setSmooth(m_isSmooth); |
| 737 | newTexture.update(page.texture); |
| 738 | page.texture.swap(newTexture); |
| 739 | } |
| 740 | else |
| 741 | { |
| 742 | // Oops, we've reached the maximum texture size... |
| 743 | err() << "Failed to add a new character to the font: the maximum texture size has been reached" |
| 744 | << std::endl; |
| 745 | return {{0, 0}, {2, 2}}; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // We can now create the new row |