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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
/*
* Copyright (C) 2014 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 "StaticLayout"
#include "ScopedIcuLocale.h"
#include "unicode/locid.h"
#include "unicode/brkiter.h"
#include "utils/misc.h"
#include "utils/Log.h"
#include "ScopedPrimitiveArray.h"
#include "JNIHelp.h"
#include "core_jni_helpers.h"
#include <cstdint>
#include <vector>
#include <list>
#include <algorithm>
namespace android {
struct JLineBreaksID {
jfieldID breaks;
jfieldID widths;
jfieldID flags;
};
static jclass gLineBreaks_class;
static JLineBreaksID gLineBreaks_fieldID;
class Builder {
public:
~Builder() {
utext_close(&mUText);
delete mBreakIterator;
}
void setLocale(const Locale& locale) {
delete mBreakIterator;
UErrorCode status = U_ZERO_ERROR;
mBreakIterator = BreakIterator::createLineInstance(locale, status);
// TODO: check status
}
void resize(size_t size) {
mTextBuf.resize(size);
}
uint16_t* buffer() {
return mTextBuf.data();
}
// set text to current contents of buffer
void setText() {
UErrorCode status = U_ZERO_ERROR;
utext_openUChars(&mUText, mTextBuf.data(), mTextBuf.size(), &status);
mBreakIterator->setText(&mUText, status);
}
void finish() {
if (mTextBuf.size() > MAX_TEXT_BUF_RETAIN) {
mTextBuf.clear();
mTextBuf.shrink_to_fit();
}
}
BreakIterator* breakIterator() const {
return mBreakIterator;
}
private:
const size_t MAX_TEXT_BUF_RETAIN = 32678;
BreakIterator* mBreakIterator = nullptr;
UText mUText = UTEXT_INITIALIZER;
std::vector<uint16_t>mTextBuf;
};
static const int CHAR_SPACE = 0x20;
static const int CHAR_TAB = 0x09;
static const int CHAR_NEWLINE = 0x0a;
static const int CHAR_ZWSP = 0x200b;
class TabStops {
public:
// specified stops must be a sorted array (allowed to be null)
TabStops(JNIEnv* env, jintArray stops, jint defaultTabWidth) :
mStops(env), mTabWidth(defaultTabWidth) {
if (stops != nullptr) {
mStops.reset(stops);
mNumStops = mStops.size();
} else {
mNumStops = 0;
}
}
float width(float widthSoFar) const {
const jint* mStopsArray = mStops.get();
for (int i = 0; i < mNumStops; i++) {
if (mStopsArray[i] > widthSoFar) {
return mStopsArray[i];
}
}
// find the next tabstop after widthSoFar
return static_cast<int>((widthSoFar + mTabWidth) / mTabWidth) * mTabWidth;
}
private:
ScopedIntArrayRO mStops;
const int mTabWidth;
int mNumStops;
// disable copying and assignment
TabStops(const TabStops&);
void operator=(const TabStops&);
};
enum PrimitiveType {
kPrimitiveType_Box,
kPrimitiveType_Glue,
kPrimitiveType_Penalty,
kPrimitiveType_Variable,
kPrimitiveType_Wordbreak
};
static const float PENALTY_INFINITY = 1e7; // forced non-break, negative infinity is forced break
struct Primitive {
PrimitiveType type;
int location;
// 'Box' has width
// 'Glue' has width
// 'Penalty' has width and penalty
// 'Variable' has tabStop
// 'Wordbreak' has penalty
union {
struct {
float width;
float penalty;
};
const TabStops* tabStop;
};
};
class LineWidth {
public:
LineWidth(float firstWidth, int firstWidthLineCount, float restWidth) :
mFirstWidth(firstWidth), mFirstWidthLineCount(firstWidthLineCount),
mRestWidth(restWidth) {}
float getLineWidth(int line) const {
return (line < mFirstWidthLineCount) ? mFirstWidth : mRestWidth;
}
private:
const float mFirstWidth;
const int mFirstWidthLineCount;
const float mRestWidth;
};
class LineBreaker {
public:
LineBreaker(const std::vector<Primitive>& primitives,
const LineWidth& lineWidth) :
mPrimitives(primitives), mLineWidth(lineWidth) {}
virtual ~LineBreaker() {}
virtual void computeBreaks(std::vector<int>* breaks, std::vector<float>* widths,
std::vector<unsigned char>* flags) const = 0;
protected:
const std::vector<Primitive>& mPrimitives;
const LineWidth& mLineWidth;
};
class OptimizingLineBreaker : public LineBreaker {
public:
OptimizingLineBreaker(const std::vector<Primitive>& primitives, const LineWidth& lineWidth) :
LineBreaker(primitives, lineWidth) {}
void computeBreaks(std::vector<int>* breaks, std::vector<float>* widths,
std::vector<unsigned char>* flags) const {
int numBreaks = mPrimitives.size();
Node* opt = new Node[numBreaks];
opt[0].prev = -1;
opt[0].prevCount = 0;
opt[0].width = 0;
opt[0].demerits = 0;
opt[0].flags = false;
opt[numBreaks - 1].prev = -1;
opt[numBreaks - 1].prevCount = 0;
std::list<int> active;
active.push_back(0);
int lastBreak = 0;
for (int i = 0; i < numBreaks; i++) {
const Primitive& p = mPrimitives[i];
if (p.type == kPrimitiveType_Penalty) {
const bool finalBreak = (i + 1 == numBreaks);
bool breakFound = false;
Node bestBreak;
for (std::list<int>::iterator it = active.begin(); it != active.end(); /* incrementing done in loop */) {
const int pos = *it;
bool flags;
float width, printedWidth;
const int lines = opt[pos].prevCount;
const float maxWidth = mLineWidth.getLineWidth(lines);
// we have to compute metrics every time --
// we can't really precompute this stuff and just deal with breaks
// because of the way tab characters work, this makes it computationally
// harder, but this way, we can still optimize while treating tab characters
// correctly
computeMetrics(pos, i, &width, &printedWidth, &flags);
if (printedWidth <= maxWidth) {
float demerits = computeDemerits(maxWidth, printedWidth,
finalBreak, p.penalty) + opt[pos].demerits;
if (!breakFound || demerits < bestBreak.demerits) {
bestBreak.prev = pos;
bestBreak.prevCount = opt[pos].prevCount + 1;
bestBreak.demerits = demerits;
bestBreak.width = printedWidth;
bestBreak.flags = flags;
breakFound = true;
}
++it;
} else {
active.erase(it++); // safe to delete like this
}
}
if (p.penalty == -PENALTY_INFINITY) {
active.clear();
}
if (breakFound) {
opt[i] = bestBreak;
active.push_back(i);
lastBreak = i;
}
if (active.empty()) {
// we can't give up!
float width, printedWidth;
bool flags;
const int lines = opt[lastBreak].prevCount;
const float maxWidth = mLineWidth.getLineWidth(lines);
const int breakIndex = desperateBreak(lastBreak, numBreaks, maxWidth, &width, &printedWidth, &flags);
opt[breakIndex].prev = lastBreak;
opt[breakIndex].prevCount = lines + 1;
opt[breakIndex].demerits = 0; // doesn't matter, it's the only one
opt[breakIndex].width = width;
opt[breakIndex].flags = flags;
active.push_back(breakIndex);
lastBreak = breakIndex;
i = breakIndex; // incremented by i++
}
}
}
int idx = numBreaks - 1;
int count = opt[idx].prevCount;
breaks->resize(count);
widths->resize(count);
flags->resize(count);
while (opt[idx].prev != -1) {
--count;
(*breaks)[count] = mPrimitives[idx].location;
(*widths)[count] = opt[idx].width;
(*flags)[count] = opt[idx].flags;
idx = opt[idx].prev;
}
delete[] opt;
}
private:
inline void computeMetrics(int start, int end, float* width, float* printedWidth, bool* flags) const {
bool f = false;
float w = 0, pw = 0;
for (int i = start; i < end; i++) {
const Primitive& p = mPrimitives[i];
if (p.type == kPrimitiveType_Box || p.type == kPrimitiveType_Glue) {
w += p.width;
if (p.type == kPrimitiveType_Box) {
pw = w;
}
} else if (p.type == kPrimitiveType_Variable) {
w = p.tabStop->width(w);
f = true;
}
}
*width = w;
*printedWidth = pw;
*flags = f;
}
inline float computeDemerits(float maxWidth, float width, bool finalBreak, float penalty) const {
float deviation = finalBreak ? 0 : maxWidth - width;
return (deviation * deviation) + penalty;
}
// returns end pos (chosen break), -1 if fail
inline int desperateBreak(int start, int limit, float maxWidth, float* width, float* printedWidth, bool* flags) const {
float w = 0, pw = 0;
bool breakFound = false;
int breakIndex = 0, firstTabIndex = INT_MAX;
for (int i = start; i < limit; i++) {
const Primitive& p = mPrimitives[i];
if (p.type == kPrimitiveType_Box || p.type == kPrimitiveType_Glue) {
w += p.width;
if (p.type == kPrimitiveType_Box) {
pw = w;
}
} else if (p.type == kPrimitiveType_Variable) {
w = p.tabStop->width(w);
firstTabIndex = std::min(firstTabIndex, i);
}
if (pw > maxWidth) {
if (breakFound) {
break;
} else {
// no choice, keep going
}
}
// must make progress
if (i > start && (p.type == kPrimitiveType_Penalty || p.type == kPrimitiveType_Wordbreak)) {
breakFound = true;
breakIndex = i;
}
}
if (breakFound) {
*width = w;
*printedWidth = pw;
*flags = (start <= firstTabIndex && firstTabIndex < breakIndex);
return breakIndex;
} else {
return -1;
}
}
struct Node {
int prev; // set to sentinel value (-1) for initial node
int prevCount; // number of breaks so far
float demerits;
float width;
bool flags;
};
};
class GreedyLineBreaker : public LineBreaker {
public:
GreedyLineBreaker(const std::vector<Primitive>& primitives, const LineWidth& lineWidth) :
LineBreaker(primitives, lineWidth) {}
void computeBreaks(std::vector<int>* breaks, std::vector<float>* widths,
std::vector<unsigned char>* flags) const {
int lineNum = 0;
float width = 0, printedWidth = 0;
bool breakFound = false, goodBreakFound = false;
int breakIndex = 0, goodBreakIndex = 0;
float breakWidth = 0, goodBreakWidth = 0;
int firstTabIndex = INT_MAX;
float maxWidth = mLineWidth.getLineWidth(lineNum);
const int numPrimitives = mPrimitives.size();
// greedily fit as many characters as possible on each line
// loop over all primitives, and choose the best break point
// (if possible, a break point without splitting a word)
// after going over the maximum length
for (int i = 0; i < numPrimitives; i++) {
const Primitive& p = mPrimitives[i];
// update the current line width
if (p.type == kPrimitiveType_Box || p.type == kPrimitiveType_Glue) {
width += p.width;
if (p.type == kPrimitiveType_Box) {
printedWidth = width;
}
} else if (p.type == kPrimitiveType_Variable) {
width = p.tabStop->width(width);
// keep track of first tab character in the region we are examining
// so we can determine whether or not a line contains a tab
firstTabIndex = std::min(firstTabIndex, i);
}
// find the best break point for the characters examined so far
if (printedWidth > maxWidth) {
if (breakFound || goodBreakFound) {
if (goodBreakFound) {
// a true line break opportunity existed in the characters examined so far,
// so there is no need to split a word
i = goodBreakIndex; // no +1 because of i++
lineNum++;
maxWidth = mLineWidth.getLineWidth(lineNum);
breaks->push_back(mPrimitives[goodBreakIndex].location);
widths->push_back(goodBreakWidth);
flags->push_back(firstTabIndex < goodBreakIndex);
firstTabIndex = INT_MAX;
} else {
// must split a word because there is no other option
i = breakIndex; // no +1 because of i++
lineNum++;
maxWidth = mLineWidth.getLineWidth(lineNum);
breaks->push_back(mPrimitives[breakIndex].location);
widths->push_back(breakWidth);
flags->push_back(firstTabIndex < breakIndex);
firstTabIndex = INT_MAX;
}
printedWidth = width = 0;
goodBreakFound = breakFound = false;
goodBreakWidth = breakWidth = 0;
continue;
} else {
// no choice, keep going... must make progress by putting at least one
// character on a line, even if part of that character is cut off --
// there is no other option
}
}
// update possible break points
if (p.type == kPrimitiveType_Penalty && p.penalty < PENALTY_INFINITY) {
// this does not handle penalties with width
// handle forced line break
if (p.penalty == -PENALTY_INFINITY) {
lineNum++;
maxWidth = mLineWidth.getLineWidth(lineNum);
breaks->push_back(p.location);
widths->push_back(printedWidth);
flags->push_back(firstTabIndex < i);
firstTabIndex = INT_MAX;
printedWidth = width = 0;
goodBreakFound = breakFound = false;
goodBreakWidth = breakWidth = 0;
continue;
}
if (i > breakIndex && (printedWidth <= maxWidth || breakFound == false)) {
breakFound = true;
breakIndex = i;
breakWidth = printedWidth;
}
if (i > goodBreakIndex && printedWidth <= maxWidth) {
goodBreakFound = true;
goodBreakIndex = i;
goodBreakWidth = printedWidth;
}
} else if (p.type == kPrimitiveType_Wordbreak) {
// only do this if necessary -- we don't want to break words
// when possible, but sometimes it is unavoidable
if (i > breakIndex && (printedWidth <= maxWidth || breakFound == false)) {
breakFound = true;
breakIndex = i;
breakWidth = printedWidth;
}
}
}
if (breakFound || goodBreakFound) {
// output last break if there are more characters to output
if (goodBreakFound) {
breaks->push_back(mPrimitives[goodBreakIndex].location);
widths->push_back(goodBreakWidth);
flags->push_back(firstTabIndex < goodBreakIndex);
} else {
breaks->push_back(mPrimitives[breakIndex].location);
widths->push_back(breakWidth);
flags->push_back(firstTabIndex < breakIndex);
}
}
}
};
static jint recycleCopy(JNIEnv* env, jobject recycle, jintArray recycleBreaks,
jfloatArray recycleWidths, jbooleanArray recycleFlags,
jint recycleLength, const std::vector<jint>& breaks,
const std::vector<jfloat>& widths, const std::vector<jboolean>& flags) {
int bufferLength = breaks.size();
if (recycleLength < bufferLength) {
// have to reallocate buffers
recycleBreaks = env->NewIntArray(bufferLength);
recycleWidths = env->NewFloatArray(bufferLength);
recycleFlags = env->NewBooleanArray(bufferLength);
env->SetObjectField(recycle, gLineBreaks_fieldID.breaks, recycleBreaks);
env->SetObjectField(recycle, gLineBreaks_fieldID.widths, recycleWidths);
env->SetObjectField(recycle, gLineBreaks_fieldID.flags, recycleFlags);
}
// copy data
env->SetIntArrayRegion(recycleBreaks, 0, breaks.size(), &breaks.front());
env->SetFloatArrayRegion(recycleWidths, 0, widths.size(), &widths.front());
env->SetBooleanArrayRegion(recycleFlags, 0, flags.size(), &flags.front());
return bufferLength;
}
void computePrimitives(const jchar* textArr, const jfloat* widthsArr, jint length, const std::vector<int>& breaks,
const TabStops& tabStopCalculator, std::vector<Primitive>* primitives) {
int breaksSize = breaks.size();
int breakIndex = 0;
Primitive p;
for (int i = 0; i < length; i++) {
p.location = i;
jchar c = textArr[i];
if (c == CHAR_SPACE || c == CHAR_ZWSP) {
p.type = kPrimitiveType_Glue;
p.width = widthsArr[i];
primitives->push_back(p);
} else if (c == CHAR_TAB) {
p.type = kPrimitiveType_Variable;
p.tabStop = &tabStopCalculator; // shared between all variable primitives
primitives->push_back(p);
} else if (c != CHAR_NEWLINE) {
while (breakIndex < breaksSize && breaks[breakIndex] < i) breakIndex++;
p.width = 0;
if (breakIndex < breaksSize && breaks[breakIndex] == i) {
p.type = kPrimitiveType_Penalty;
p.penalty = 0;
} else {
p.type = kPrimitiveType_Wordbreak;
}
if (widthsArr[i] != 0) {
primitives->push_back(p);
}
p.type = kPrimitiveType_Box;
p.width = widthsArr[i];
primitives->push_back(p);
}
}
// final break at end of everything
p.location = length;
p.type = kPrimitiveType_Penalty;
p.width = 0;
p.penalty = -PENALTY_INFINITY;
primitives->push_back(p);
}
static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr,
jcharArray inputText, jfloatArray widths, jint length,
jfloat firstWidth, jint firstWidthLineLimit, jfloat restWidth,
jintArray variableTabStops, jint defaultTabStop, jboolean optimize,
jobject recycle, jintArray recycleBreaks,
jfloatArray recycleWidths, jbooleanArray recycleFlags,
jint recycleLength) {
std::vector<int> breaks;
Builder* b = reinterpret_cast<Builder*>(nativePtr);
b->resize(length);
env->GetCharArrayRegion(inputText, 0, length, b->buffer());
b->setText();
// TODO: this array access is pretty inefficient, but we'll replace it anyway
ScopedFloatArrayRO widthsScopedArr(env, widths);
BreakIterator* breakIterator = b->breakIterator();
int loc = breakIterator->first();
while ((loc = breakIterator->next()) != BreakIterator::DONE) {
breaks.push_back(loc);
}
// TODO: all these allocations can be moved into the builder
std::vector<Primitive> primitives;
TabStops tabStops(env, variableTabStops, defaultTabStop);
computePrimitives(b->buffer(), widthsScopedArr.get(), length, breaks, tabStops, &primitives);
LineWidth lineWidth(firstWidth, firstWidthLineLimit, restWidth);
std::vector<int> computedBreaks;
std::vector<float> computedWidths;
std::vector<unsigned char> computedFlags;
if (optimize) {
OptimizingLineBreaker breaker(primitives, lineWidth);
breaker.computeBreaks(&computedBreaks, &computedWidths, &computedFlags);
} else {
GreedyLineBreaker breaker(primitives, lineWidth);
breaker.computeBreaks(&computedBreaks, &computedWidths, &computedFlags);
}
b->finish();
return recycleCopy(env, recycle, recycleBreaks, recycleWidths, recycleFlags, recycleLength,
computedBreaks, computedWidths, computedFlags);
}
static jlong nNewBuilder(JNIEnv*, jclass) {
return reinterpret_cast<jlong>(new Builder);
}
static void nFreeBuilder(JNIEnv*, jclass, jlong nativePtr) {
delete reinterpret_cast<Builder*>(nativePtr);
}
static void nFinishBuilder(JNIEnv*, jclass, jlong nativePtr) {
Builder* b = reinterpret_cast<Builder*>(nativePtr);
b->finish();
}
static void nBuilderSetLocale(JNIEnv* env, jclass, jlong nativePtr, jstring javaLocaleName) {
ScopedIcuLocale icuLocale(env, javaLocaleName);
Builder* b = reinterpret_cast<Builder*>(nativePtr);
if (icuLocale.valid()) {
b->setLocale(icuLocale.locale());
}
}
static JNINativeMethod gMethods[] = {
{"nNewBuilder", "()J", (void*) nNewBuilder},
{"nFreeBuilder", "(J)V", (void*) nFreeBuilder},
{"nFinishBuilder", "(J)V", (void*) nFinishBuilder},
{"nBuilderSetLocale", "(JLjava/lang/String;)V", (void*) nBuilderSetLocale},
{"nComputeLineBreaks", "(J[C[FIFIF[IIZLandroid/text/StaticLayout$LineBreaks;[I[F[ZI)I",
(void*) nComputeLineBreaks}
};
int register_android_text_StaticLayout(JNIEnv* env)
{
gLineBreaks_class = MakeGlobalRefOrDie(env,
FindClassOrDie(env, "android/text/StaticLayout$LineBreaks"));
gLineBreaks_fieldID.breaks = GetFieldIDOrDie(env, gLineBreaks_class, "breaks", "[I");
gLineBreaks_fieldID.widths = GetFieldIDOrDie(env, gLineBreaks_class, "widths", "[F");
gLineBreaks_fieldID.flags = GetFieldIDOrDie(env, gLineBreaks_class, "flags", "[Z");
return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods));
}
}
|