forked from Mr-jinfa/android-screen-and-push-h264
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextRenderer.cpp
executable file
·360 lines (315 loc) · 12 KB
/
TextRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "ScreenRecord"
//#define LOG_NDEBUG 0
#include <utils/Log.h>
#include "TextRenderer.h"
#include <assert.h>
#include <malloc.h>
#include <string.h>
namespace android {
#include "FontBitmap.h"
};
using namespace android;
const char TextRenderer::kWhitespace[] = " \t\n\r";
bool TextRenderer::mInitialized = false;
uint32_t TextRenderer::mXOffset[FontBitmap::numGlyphs];
void TextRenderer::initOnce() {
if (!mInitialized) {
initXOffset();
mInitialized = true;
}
}
void TextRenderer::initXOffset() {
// Generate a table of X offsets. They start at zero and reset whenever
// we move down a line (i.e. the Y offset changes). The offset increases
// by one pixel more than the width because the generator left a gap to
// avoid reading pixels from adjacent glyphs in the texture filter.
uint16_t offset = 0;
uint16_t prevYOffset = (int16_t) -1;
for (unsigned int i = 0; i < FontBitmap::numGlyphs; i++) {
if (prevYOffset != FontBitmap::yoffset[i]) {
prevYOffset = FontBitmap::yoffset[i];
offset = 0;
}
mXOffset[i] = offset;
offset += FontBitmap::glyphWidth[i] + 1;
}
}
static bool isPowerOfTwo(uint32_t val) {
// a/k/a "is exactly one bit set"; note returns true for 0
return (val & (val -1)) == 0;
}
static uint32_t powerOfTwoCeil(uint32_t val) {
// drop it, smear the bits across, pop it
val--;
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
val++;
return val;
}
float TextRenderer::getGlyphHeight() const {
return FontBitmap::maxGlyphHeight;
}
status_t TextRenderer::loadIntoTexture() {
ALOGV("Font::loadIntoTexture");
glGenTextures(1, &mTextureName);
if (mTextureName == 0) {
ALOGE("glGenTextures failed: %#x", glGetError());
return UNKNOWN_ERROR;
}
glBindTexture(GL_TEXTURE_2D, mTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// The pixel data is stored as combined color+alpha, 8 bits per pixel.
// It's guaranteed to be a power-of-two wide, but we cut off the height
// where the data ends. We want to expand it to a power-of-two bitmap
// with ARGB data and hand that to glTexImage2D.
if (!isPowerOfTwo(FontBitmap::width)) {
ALOGE("npot glyph bitmap width %u", FontBitmap::width);
return UNKNOWN_ERROR;
}
uint32_t potHeight = powerOfTwoCeil(FontBitmap::height);
uint8_t* rgbaPixels = new uint8_t[FontBitmap::width * potHeight * 4];
memset(rgbaPixels, 0, FontBitmap::width * potHeight * 4);
uint8_t* pix = rgbaPixels;
for (unsigned int i = 0; i < FontBitmap::width * FontBitmap::height; i++) {
uint8_t alpha, color;
if ((FontBitmap::pixels[i] & 1) == 0) {
// black pixel with varying alpha
color = 0x00;
alpha = FontBitmap::pixels[i] & ~1;
} else {
// opaque grey pixel
color = FontBitmap::pixels[i] & ~1;
alpha = 0xff;
}
*pix++ = color;
*pix++ = color;
*pix++ = color;
*pix++ = alpha;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FontBitmap::width, potHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, rgbaPixels);
delete[] rgbaPixels;
GLint glErr = glGetError();
if (glErr != 0) {
ALOGE("glTexImage2D failed: %#x", glErr);
return UNKNOWN_ERROR;
}
return NO_ERROR;
}
void TextRenderer::setProportionalScale(float linesPerScreen) {
if (mScreenWidth == 0 || mScreenHeight == 0) {
ALOGW("setFontScale: can't set scale for width=%d height=%d",
mScreenWidth, mScreenHeight);
return;
}
float tallest = mScreenWidth > mScreenHeight ? mScreenWidth : mScreenHeight;
setScale(tallest / (linesPerScreen * getGlyphHeight()));
}
float TextRenderer::computeScaledStringWidth(const String8& str8) const {
// String8.length() isn't documented, but I'm assuming it will return
// the number of characters rather than the number of bytes. Since
// we can only display ASCII we want to ignore anything else, so we
// just convert to char* -- but String8 doesn't document what it does
// with values outside 0-255. So just convert to char* and use strlen()
// to see what we get.
const char* str = str8.string();
return computeScaledStringWidth(str, strlen(str));
}
size_t TextRenderer::glyphIndex(char ch) const {
size_t chi = ch - FontBitmap::firstGlyphChar;
if (chi >= FontBitmap::numGlyphs) {
chi = '?' - FontBitmap::firstGlyphChar;
}
assert(chi < FontBitmap::numGlyphs);
return chi;
}
float TextRenderer::computeScaledStringWidth(const char* str,
size_t len) const {
float width = 0.0f;
for (size_t i = 0; i < len; i++) {
size_t chi = glyphIndex(str[i]);
float glyphWidth = FontBitmap::glyphWidth[chi];
width += (glyphWidth - 1 - FontBitmap::outlineWidth) * mScale;
}
return width;
}
void TextRenderer::drawString(const Program& program, const float* texMatrix,
float x, float y, const String8& str8) const {
ALOGV("drawString %.3f,%.3f '%s' (scale=%.3f)", x, y, str8.string(),mScale);
initOnce();
// We want to draw the entire string with a single GLES call. We
// generate two arrays, one with screen coordinates, one with texture
// coordinates. Need two triangles per character.
const char* str = str8.string();
size_t len = strlen(str); // again, unsure about String8 handling
const size_t quadCoords =
2 /*triangles*/ * 3 /*vertex/tri*/ * 2 /*coord/vertex*/;
float vertices[len * quadCoords];
float texes[len * quadCoords];
float fullTexWidth = FontBitmap::width;
float fullTexHeight = powerOfTwoCeil(FontBitmap::height);
for (size_t i = 0; i < len; i++) {
size_t chi = glyphIndex(str[i]);
float glyphWidth = FontBitmap::glyphWidth[chi];
float glyphHeight = FontBitmap::maxGlyphHeight;
float vertLeft = x;
float vertRight = x + glyphWidth * mScale;
float vertTop = y;
float vertBottom = y + glyphHeight * mScale;
// Lowest-numbered glyph is in top-left of bitmap, which puts it at
// the bottom-left in texture coordinates.
float texLeft = mXOffset[chi] / fullTexWidth;
float texRight = (mXOffset[chi] + glyphWidth) / fullTexWidth;
float texTop = FontBitmap::yoffset[chi] / fullTexHeight;
float texBottom = (FontBitmap::yoffset[chi] + glyphHeight) /
fullTexHeight;
size_t off = i * quadCoords;
vertices[off + 0] = vertLeft;
vertices[off + 1] = vertBottom;
vertices[off + 2] = vertRight;
vertices[off + 3] = vertBottom;
vertices[off + 4] = vertLeft;
vertices[off + 5] = vertTop;
vertices[off + 6] = vertLeft;
vertices[off + 7] = vertTop;
vertices[off + 8] = vertRight;
vertices[off + 9] = vertBottom;
vertices[off + 10] = vertRight;
vertices[off + 11] = vertTop;
texes[off + 0] = texLeft;
texes[off + 1] = texBottom;
texes[off + 2] = texRight;
texes[off + 3] = texBottom;
texes[off + 4] = texLeft;
texes[off + 5] = texTop;
texes[off + 6] = texLeft;
texes[off + 7] = texTop;
texes[off + 8] = texRight;
texes[off + 9] = texBottom;
texes[off + 10] = texRight;
texes[off + 11] = texTop;
// We added 1-pixel padding in the texture, so we want to advance by
// one less. Also, each glyph is surrounded by a black outline, which
// we want to merge.
x += (glyphWidth - 1 - FontBitmap::outlineWidth) * mScale;
}
program.drawTriangles(mTextureName, texMatrix, vertices, texes,
len * quadCoords / 2);
}
float TextRenderer::drawWrappedString(const Program& texRender,
float xpos, float ypos, const String8& str) {
ALOGV("drawWrappedString %.3f,%.3f '%s'", xpos, ypos, str.string());
initOnce();
if (mScreenWidth == 0 || mScreenHeight == 0) {
ALOGW("drawWrappedString: can't wrap with width=%d height=%d",
mScreenWidth, mScreenHeight);
return ypos;
}
const float indentWidth = mIndentMult * getScale();
if (xpos < mBorderWidth) {
xpos = mBorderWidth;
}
if (ypos < mBorderWidth) {
ypos = mBorderWidth;
}
const size_t maxWidth = (mScreenWidth - mBorderWidth) - xpos;
if (maxWidth < 1) {
ALOGE("Unable to render text: xpos=%.3f border=%.3f width=%u",
xpos, mBorderWidth, mScreenWidth);
return ypos;
}
float stringWidth = computeScaledStringWidth(str);
if (stringWidth <= maxWidth) {
// Trivial case.
drawString(texRender, Program::kIdentity, xpos, ypos, str);
ypos += getScaledGlyphHeight();
} else {
// We need to break the string into pieces, ideally at whitespace
// boundaries.
char* mangle = strdup(str.string());
char* start = mangle;
while (start != NULL) {
float xposAdj = (start == mangle) ? xpos : xpos + indentWidth;
char* brk = breakString(start,
(float) (mScreenWidth - mBorderWidth - xposAdj));
if (brk == NULL) {
// draw full string
drawString(texRender, Program::kIdentity, xposAdj, ypos,
String8(start));
start = NULL;
} else {
// draw partial string
char ch = *brk;
*brk = '\0';
drawString(texRender, Program::kIdentity, xposAdj, ypos,
String8(start));
*brk = ch;
start = brk;
if (strchr(kWhitespace, ch) != NULL) {
// if we broke on whitespace, skip past it
start++;
}
}
ypos += getScaledGlyphHeight();
}
free(mangle);
}
return ypos;
}
char* TextRenderer::breakString(const char* str, float maxWidth) const {
// Ideally we'd do clever things like binary search. Not bothering.
ALOGV("breakString '%s' %.3f", str, maxWidth);
size_t len = strlen(str);
if (len == 0) {
// Caller should detect this and not advance ypos.
return NULL;
}
float stringWidth = computeScaledStringWidth(str, len);
if (stringWidth <= maxWidth) {
return NULL; // trivial -- use full string
}
// Find the longest string that will fit.
size_t goodPos = 0;
for (size_t i = 0; i < len; i++) {
stringWidth = computeScaledStringWidth(str, i);
if (stringWidth < maxWidth) {
goodPos = i;
} else {
break; // too big
}
}
if (goodPos == 0) {
// space is too small to hold any glyph; output a single char
ALOGW("Couldn't find a nonzero prefix that fit from '%s'", str);
goodPos = 1;
}
// Scan back for whitespace. If we can't find any we'll just have
// an ugly mid-word break.
for (size_t i = goodPos; i > 0; i--) {
if (strchr(kWhitespace, str[i]) != NULL) {
goodPos = i;
break;
}
}
ALOGV("goodPos=%zu for str='%s'", goodPos, str);
return const_cast<char*>(str + goodPos);
}