Root/
Source at commit 69d6c00 created 9 years 8 months ago. By Nebuleon Fumika, TextDialog: Improve the average and worst-case performance of word wrapping | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
3 | * massimiliano.torromeo@gmail.com * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include "textdialog.h" |
22 | |
23 | #include "gmenu2x.h" |
24 | #include "utilities.h" |
25 | |
26 | using namespace std; |
27 | |
28 | TextDialog::TextDialog(GMenu2X *gmenu2x, const string &title, const string &description, const string &icon, vector<string> *text) |
29 | : Dialog(gmenu2x) |
30 | { |
31 | this->text = text; |
32 | this->title = title; |
33 | this->description = description; |
34 | this->icon = icon; |
35 | preProcess(); |
36 | } |
37 | |
38 | void TextDialog::preProcess() { |
39 | unsigned i = 0; |
40 | |
41 | while (i < text->size()) { |
42 | /* Clean the end of the string, allowing lines that are indented at |
43 | * the start to stay as such. */ |
44 | string line = rtrim(text->at(i)); |
45 | |
46 | if (gmenu2x->font->getTextWidth(line) > (int) gmenu2x->resX - 15) { |
47 | /* At least one full character must fit, in order to advance. */ |
48 | size_t fits = 1; |
49 | while (fits < line.length() && !isUTF8Starter(line[fits])) { |
50 | fits++; |
51 | } |
52 | size_t doesntFit = fits; |
53 | |
54 | /* This preprocessing finds an upper bound on the number of |
55 | * bytes of full characters that fit on the screen, 2^n, in |
56 | * n steps. */ |
57 | do { |
58 | fits = doesntFit; /* what didn't fit has been determined to fit by a previous iteration */ |
59 | doesntFit = min(2 * fits, line.length()); |
60 | while (doesntFit < line.length() && !isUTF8Starter(line[doesntFit])) { |
61 | doesntFit++; |
62 | } |
63 | } while (doesntFit <= line.length() |
64 | && gmenu2x->font->getTextWidth(line.substr(0, doesntFit)) <= (int) gmenu2x->resX - 15); |
65 | |
66 | /* End this loop when N characters fit but N + 1 don't. */ |
67 | while (fits + 1 < doesntFit) { |
68 | size_t guess = fits + (doesntFit - fits) / 2; |
69 | if (!isUTF8Starter(line[guess])) |
70 | { |
71 | size_t oldGuess = guess; |
72 | /* Adjust the guess to the nearest UTF-8 starter that is |
73 | * not 'fits' or 'doesntFit'. */ |
74 | for (size_t offset = 1; offset < (doesntFit - fits) / 2 - 1; offset++) { |
75 | if (isUTF8Starter(line[guess - offset])) { |
76 | guess -= offset; |
77 | break; |
78 | } else if (isUTF8Starter(line[guess + offset])) { |
79 | guess += offset; |
80 | break; |
81 | } |
82 | } |
83 | /* If there's no such character, exit early. */ |
84 | if (guess == oldGuess) { |
85 | break; |
86 | } |
87 | } |
88 | if (gmenu2x->font->getTextWidth(line.substr(0, guess)) <= (int) gmenu2x->resX - 15) { |
89 | fits = guess; |
90 | } else { |
91 | doesntFit = guess; |
92 | } |
93 | } |
94 | |
95 | /* The line shall be split at the last space-separated word that |
96 | * fully fits, or otherwise at the last character that fits. */ |
97 | size_t lastSpace = line.find_last_of(" \t\r", fits); |
98 | if (lastSpace != string::npos) { |
99 | fits = lastSpace; |
100 | } |
101 | |
102 | /* Insert the rest in a new slot after this line. |
103 | * TODO (Nebuleon) Don't use a vector for this, because all later |
104 | * elements are moved, which is inefficient. */ |
105 | text->insert(text->begin() + i + 1, ltrim(line.substr(fits))); |
106 | line = rtrim(line.substr(0, fits)); |
107 | } |
108 | |
109 | /* Put the trimmed whole line or the smaller split of the split line |
110 | * back into the same slot */ |
111 | text->at(i) = line; |
112 | |
113 | i++; |
114 | } |
115 | } |
116 | |
117 | void TextDialog::drawText(vector<string> *text, unsigned int y, |
118 | unsigned int firstRow, unsigned int rowsPerPage) |
119 | { |
120 | const int fontHeight = gmenu2x->font->getLineSpacing(); |
121 | |
122 | for (unsigned i = firstRow; i < firstRow + rowsPerPage && i < text->size(); i++) { |
123 | const string &line = text->at(i); |
124 | int rowY = y + (i - firstRow) * fontHeight; |
125 | if (line == "----") { // horizontal ruler |
126 | rowY += fontHeight / 2; |
127 | gmenu2x->s->box(5, rowY, gmenu2x->resX - 16, 1, 255, 255, 255, 130); |
128 | gmenu2x->s->box(5, rowY+1, gmenu2x->resX - 16, 1, 0, 0, 0, 130); |
129 | } else { |
130 | gmenu2x->font->write(gmenu2x->s, line, 5, rowY); |
131 | } |
132 | } |
133 | |
134 | gmenu2x->drawScrollBar(rowsPerPage, text->size(), firstRow); |
135 | } |
136 | |
137 | void TextDialog::exec() { |
138 | bool close = false; |
139 | |
140 | Surface bg(gmenu2x->bg); |
141 | |
142 | //link icon |
143 | if (!fileExists(icon)) |
144 | drawTitleIcon("icons/ebook.png",true,&bg); |
145 | else |
146 | drawTitleIcon(icon,false,&bg); |
147 | writeTitle(title,&bg); |
148 | writeSubTitle(description,&bg); |
149 | |
150 | gmenu2x->drawButton(&bg, "start", gmenu2x->tr["Exit"], |
151 | gmenu2x->drawButton(&bg, "cancel", "", |
152 | gmenu2x->drawButton(&bg, "down", gmenu2x->tr["Scroll"], |
153 | gmenu2x->drawButton(&bg, "up", "", 5)-10))-10); |
154 | |
155 | bg.convertToDisplayFormat(); |
156 | |
157 | const int fontHeight = gmenu2x->font->getLineSpacing(); |
158 | unsigned int contentY, contentHeight; |
159 | tie(contentY, contentHeight) = gmenu2x->getContentArea(); |
160 | const unsigned rowsPerPage = contentHeight / fontHeight; |
161 | contentY += (contentHeight % fontHeight) / 2; |
162 | |
163 | unsigned firstRow = 0; |
164 | while (!close) { |
165 | bg.blit(gmenu2x->s, 0, 0); |
166 | drawText(text, contentY, firstRow, rowsPerPage); |
167 | gmenu2x->s->flip(); |
168 | |
169 | switch(gmenu2x->input.waitForPressedButton()) { |
170 | case InputManager::UP: |
171 | if (firstRow > 0) firstRow--; |
172 | break; |
173 | case InputManager::DOWN: |
174 | if (firstRow + rowsPerPage < text->size()) firstRow++; |
175 | break; |
176 | case InputManager::ALTLEFT: |
177 | if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1; |
178 | else firstRow = 0; |
179 | break; |
180 | case InputManager::ALTRIGHT: |
181 | if (firstRow + rowsPerPage*2 -1 < text->size()) { |
182 | firstRow += rowsPerPage-1; |
183 | } else { |
184 | firstRow = text->size() < rowsPerPage ? |
185 | 0 : text->size() - rowsPerPage; |
186 | } |
187 | break; |
188 | case InputManager::SETTINGS: |
189 | case InputManager::CANCEL: |
190 | close = true; |
191 | break; |
192 | default: |
193 | break; |
194 | } |
195 | } |
196 | } |
197 |
Branches:
install_locations
master
opkrun
packages