| 63 | */ |
| 64 | template <typename Metric> |
| 65 | void Layout(TextLayout* layout, |
| 66 | float width, |
| 67 | float* text_width, |
| 68 | Metric metrics, |
| 69 | bool measure_trailing_space) |
| 70 | { |
| 71 | TextGlyph* glyphs = layout->m_Glyphs.Begin(); |
| 72 | uint32_t num_glyphs = layout->m_Glyphs.Size(); |
| 73 | uint32_t cursor = 0; |
| 74 | float max_width = 0; |
| 75 | uint32_t c; |
| 76 | do |
| 77 | { |
| 78 | uint32_t n = 0, last_n = 0; |
| 79 | uint32_t row_start = cursor; |
| 80 | uint32_t last_cursor = cursor; |
| 81 | float w = 0, last_w = 0; |
| 82 | do |
| 83 | { |
| 84 | c = NextBreak(glyphs, num_glyphs, &cursor, &n); |
| 85 | if (n > 0) |
| 86 | { |
| 87 | int trim = 0; |
| 88 | if (c != 0) |
| 89 | trim = 1; |
| 90 | w = metrics(row_start, n - trim, measure_trailing_space); |
| 91 | if (dmMath::Abs(w) <= width) |
| 92 | { |
| 93 | last_n = n - trim; |
| 94 | last_w = w; |
| 95 | last_cursor = cursor; |
| 96 | if (c != CHAR_NEWLINE && !measure_trailing_space) |
| 97 | c = SkipWS(glyphs, num_glyphs, &cursor, &n); |
| 98 | } |
| 99 | else if (last_n != 0) |
| 100 | { |
| 101 | // rewind if we have more to scan |
| 102 | cursor = last_cursor; |
| 103 | c = glyphs[last_cursor++].m_Codepoint; |
| 104 | } |
| 105 | } |
| 106 | } while (dmMath::Abs(w) <= width && c != 0 && c != CHAR_NEWLINE); |
| 107 | if (dmMath::Abs(w) > width && last_n == 0) |
| 108 | { |
| 109 | int trim = 0; |
| 110 | if (c != 0) |
| 111 | trim = 1; |
| 112 | last_n = n - trim; |
| 113 | last_w = w; |
| 114 | } |
| 115 | |
| 116 | if ((c != 0 || last_n > 0)) |
| 117 | { |
| 118 | if (layout->m_Lines.Full()) |
| 119 | layout->m_Lines.OffsetCapacity(8); |
| 120 | |
| 121 | TextLine line = {0}; |
| 122 | line.m_Width = last_w; |