/
EnvelopeWarp.cpp
468 lines (392 loc) · 13 KB
/
EnvelopeWarp.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
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
#include "EnvelopeWarp.h"
#include "SkTextToPathIter.h"
#include "SkTypeface.h"
#include "SkGeometry.h"
#include <vector>
///****************************************************************************************************
TextArt::EnvelopeWarp::EnvelopeWarp(const SkPath& skeleton, const SkMatrix& matrix)
: bSkeleton_(skeleton)
, matrix_(matrix)
, isNormalRotated_(false)
, isSymmetric_(false)
, isTopBased_(false)
, k1_(1.0)
, xWeightingMode_(XWeightingMode_Linearly | XWeightingMode_Interpolating)
{
}
void TextArt::EnvelopeWarp::setTopSkeleton(const SkPath& skeleton)
{
tSkeleton_ = skeleton;
SkRect bBounds = bSkeleton_.getBounds();
const SkRect& tBounds = tSkeleton_.getBounds();
}
void TextArt::EnvelopeWarp::setIsNormalRotated(bool isRotated)
{
isNormalRotated_ = isRotated;
}
void TextArt::EnvelopeWarp::setIsSymmetric(bool isSymmetric)
{
isSymmetric_ = isSymmetric;
}
void TextArt::EnvelopeWarp::setIsTopBased(bool isTopBased)
{
isTopBased_ = isTopBased;
}
SkPath TextArt::EnvelopeWarp::warp(const std::string& text, SkTypeface* typeface)
{
SkPath warpedPath;
if (text.empty())
return warpedPath;
//prepare paint
SkPaint paint;
paint.setTextSize(SkIntToScalar(64));
paint.setTypeface(typeface);
paint.setTextAlign(SkPaint::kCenter_Align);
//measure Bottom path to center text on it
SkPathMeasure bMeasure(bSkeleton_, false);
SkScalar hBOffset = 0;
if (paint.getTextAlign() != SkPaint::kLeft_Align)
{
SkScalar pathLen = bMeasure.getLength();
if (paint.getTextAlign() == SkPaint::kCenter_Align)
{
pathLen = SkScalarHalf(pathLen);
}
hBOffset += pathLen;
}
//get text boundaries on normal(non-warped) state
{
SkMatrix scaleMartix;
scaleMartix.setIdentity();
SkTextToPathIter iter(text.c_str(), text.size(), paint, true);
const SkPath* glypthPath;
SkScalar xpos;
SkScalar scale = iter.getPathScale();
scaleMartix.setScale(scale, scale);
while (iter.next(&glypthPath, &xpos))
{
if (glypthPath)
{
//prepare resulting transformatiom Matrix
SkMatrix compositeMatrix(scaleMartix);
compositeMatrix.postTranslate(xpos + hBOffset, 0);
SkPath p;
(*glypthPath).transform(compositeMatrix, &p);
//get normal(without any warps) text boundaries
boundsRect_.join( p.getBounds() );
}
}
}
//center text on Top skeleton
SkPathMeasure tMeasure(tSkeleton_, false);
SkScalar hTOffset = 0;
{
if (paint.getTextAlign() != SkPaint::kLeft_Align)
{
SkScalar pathLen = tMeasure.getLength();
if (paint.getTextAlign() == SkPaint::kCenter_Align)
{
pathLen = SkScalarHalf(pathLen);
}
hTOffset += pathLen;
}
}
//warp text on Bottom and Top skeletons
{
SkTextToPathIter iter(text.c_str(), text.size(), paint, true);
SkScalar xpos;
SkMatrix scaleMartix;
SkScalar scale = iter.getPathScale();
scaleMartix.setScale(scale, scale);
SkPath line;
line.lineTo(SkIntToScalar(100), SkIntToScalar(0));
SkPathMeasure lineMeasure(line, false);
SkPathCrossing bCrossing(bSkeleton_);
SkPathCrossing tCrossing(tSkeleton_);
const SkPath* glypthPathOrig;
while (iter.next(&glypthPathOrig, &xpos))
{
if (glypthPathOrig)
{
SkPath glypthPath;
SkRect glypthBound;
glypthBound = (*glypthPathOrig).getBounds();
glypthPathOrig->offset(-glypthBound.fLeft, 0, &glypthPath);
morph(bSkeleton_, bMeasure, bCrossing,
tSkeleton_, tMeasure, tCrossing,
glypthPath, lineMeasure, scaleMartix,
xpos, hBOffset, hTOffset, warpedPath);
}
}
}
return warpedPath;
}
void TextArt::EnvelopeWarp::morph(SkPath& bSkeleton, SkPathMeasure& bMeasure, SkPathCrossing& bCrossing,
SkPath& tSkeleton, SkPathMeasure& tMeasure, SkPathCrossing& tCrossing,
SkPath& glypthPath, SkPathMeasure& lineMeasure, SkMatrix& scaleMatrix,
SkScalar xpos, SkScalar hBOffset, SkScalar hTOffset, SkPath& warpedPath)
{
SkRect glypthBound;
glypthBound = glypthPath.getBounds();
SkScalar k1 = 1.0;
if (!isSymmetric_)
{
SkScalar hBOffsetTmp = 0.0;
SkScalar hTOffsetTmp = 0.0;
if (isTopBased_)
{
glypthBound.fTop = 0;
glypthBound.fBottom = -boundsRect_.height();
SkMatrix compositeTMatrix(scaleMatrix);
compositeTMatrix.postTranslate(xpos + hTOffset, 0);
compositeTMatrix.postConcat(matrix_);
if ( getK(glypthBound, bCrossing, tCrossing, tMeasure, compositeTMatrix, k1, hBOffsetTmp, hTOffsetTmp) )
{
k1 = 1/k1;
hBOffset = hBOffsetTmp;
hTOffset = xpos + hTOffset;
}
}
else
{
glypthBound.fTop = -boundsRect_.height();
glypthBound.fBottom = 0;
SkMatrix compositeBMatrix(scaleMatrix);
compositeBMatrix.postTranslate(xpos + hBOffset, 0);
compositeBMatrix.postConcat(matrix_);
if ( getK(glypthBound, bCrossing, tCrossing, bMeasure, compositeBMatrix, k1, hBOffsetTmp, hTOffsetTmp) )
{
//use distance to Left on Top Skeleton for positioning Top glypthn
hTOffset = hTOffsetTmp;
hBOffset = xpos + hBOffset;
}
}
}
else
{
hBOffset = xpos + hBOffset;
hTOffset = hBOffset;
}
SkMatrix compositeBMatrix(scaleMatrix);
compositeBMatrix.postTranslate(hBOffset, 0);
compositeBMatrix.postConcat(matrix_);
//warp Glypth by bottom line
k1_ = isTopBased_ ? k1 : SkIntToScalar(1);
isTop = false;
SkPath bWarped;
morphpath(&bWarped, glypthPath, bMeasure, compositeBMatrix);
bWarped_.addPath(bWarped);
if (!tSkeleton_.isEmpty())
{
SkMatrix compositeTMatrix(scaleMatrix);
compositeTMatrix.postTranslate(hTOffset, 0);
compositeTMatrix.postConcat(matrix_);
//warp Glypth by top line
k1_ = !isTopBased_ ? k1 : SkIntToScalar(1);
isTop = true;
SkPath tWarped;
morphpath(&tWarped, glypthPath, tMeasure, compositeTMatrix);
tWarped_.addPath(tWarped);
//convert Glypth to Path to allow weighting
SkPath lineMorphed;
morphpath(&lineMorphed, glypthPath, lineMeasure, scaleMatrix);
weight(lineMorphed, tWarped, bWarped, &warpedPath);
}
else
warpedPath.addPath(bWarped_);
}
bool TextArt::EnvelopeWarp::getK(const SkRect& glypthBound, SkPathCrossing& bCrossing, SkPathCrossing& tCrossing,
SkPathMeasure& basePathMeasure, const SkMatrix& compositeMatrix,
SkScalar& k1, SkScalar& hBOffset, SkScalar& hTOffset)
{
SkScalar bCut = isTopBased_ ? SkIntToScalar(-3) : SkIntToScalar(+3);
SkPoint centerX[4], tCenterX[4];
//left normal
centerX[0].set(glypthBound.fLeft, glypthBound.fBottom + bCut);
centerX[1].set(glypthBound.fLeft, glypthBound.fTop);
//right normal
centerX[2].set(glypthBound.fRight, glypthBound.fBottom + bCut);
centerX[3].set(glypthBound.fRight, glypthBound.fTop);
//morph normal point by Bottom skeleton
isTop = isTopBased_;
k1_ = 1.0;
morphpoints(tCenterX, centerX, 4, basePathMeasure, compositeMatrix);
normal.moveTo(tCenterX[0]);
normal.lineTo(tCenterX[1]);
normal.moveTo(tCenterX[2]);
normal.lineTo(tCenterX[3]);
SkPoint bCrossR, bCrossL, tCrossL, tCrossR;
SkScalar bCrossDistR, bCrossDistL, tCrossDistL, tCrossDistR;
//get intersection between Normals and Skeletons
if (tCrossing.rayCrossing(tCenterX, &tCrossDistL, &tCrossL) &&
tCrossing.rayCrossing(&tCenterX[2], &tCrossDistR, &tCrossR) &&
bCrossing.rayCrossing(&tCenterX[0], &bCrossDistL, &bCrossL) &&
bCrossing.rayCrossing(&tCenterX[2], &bCrossDistR, &bCrossR) )
{
intersections.push_back(tCrossL);
intersections.push_back(tCrossR);
intersections.push_back(bCrossL);
intersections.push_back(bCrossR);
//calulate linear coeffiction for stretching Top glypth
k1 = (tCrossDistR - tCrossDistL) / (bCrossDistR - bCrossDistL);
//use distance to Left on Top Skeleton for positioning Top glypthn
hBOffset = bCrossDistL;
hTOffset = tCrossDistL;
return true;
}
return false;
}
void TextArt::EnvelopeWarp::morphpoints(SkPoint dst[], const SkPoint src[], int count,
SkPathMeasure& meas, const SkMatrix& matrix)
{
SkMatrix::MapXYProc proc = matrix.getMapXYProc();
for (int i = 0; i < count; i++)
{
SkPoint pos;
SkVector tangent;
SkPoint iSrc = src[i];
iSrc.fX = k1_ * iSrc.fX;
proc(matrix, iSrc.fX, iSrc.fY, &pos);
SkScalar sx = pos.fX;
SkScalar sy = pos.fY;
if (xWeightingMode_ & XWeightingMode_Linearly)
{ //in Linearly mode adjust Top text by TopLength/BottomLength relation
if (isTop)
{
//move text below the Top skeleton
sy -= boundsRect_.fTop;
}
}
if (!meas.getPosTan(sx, &pos, &tangent))
{
// set to 0 if the measure failed, so that we just set dst == pos
tangent.set(0, 0);
}
/* This is the old way (that explains our approach but is way too slow
SkMatrix matrix;
SkPoint pt;
pt.set(sx, sy);
matrix.setSinCos(tangent.fY, tangent.fX);
matrix.preTranslate(-sx, 0);
matrix.postTranslate(pos.fX, pos.fY);
matrix.mapPoints(&dst[i], &pt, 1);
*/
if (isNormalRotated_)
{
dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
pos.fY + SkScalarMul(tangent.fX, sy));
}
else
{
dst[i].set(pos.fX,
pos.fY + sy);
}
}
}
/* TODO
Need differentially more subdivisions when the follow-path is curvy. Not sure how to
determine that, but we need it. I guess a cheap answer is let the caller tell us,
but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
*/
void TextArt::EnvelopeWarp::morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
const SkMatrix& matrix)
{
SkPath::Iter iter(src, false);
SkPoint srcP[4], dstP[3];
SkPath::Verb verb;
while ((verb = iter.next(srcP)) != SkPath::kDone_Verb)
{
switch (verb)
{
case SkPath::kMove_Verb:
morphpoints(dstP, srcP, 1, meas, matrix);
dst->moveTo(dstP[0]);
break;
case SkPath::kLine_Verb:
/*
morphpoints(dstP, &srcP[1], 1, meas, matrix);
dst->lineTo(dstP[0]);
break;
*/
// turn lines into quads to look bendy
srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
morphpoints(dstP, srcP, 2, meas, matrix);
dst->quadTo(dstP[0], dstP[1]);
break;
case SkPath::kQuad_Verb:
morphpoints(dstP, &srcP[1], 2, meas, matrix);
dst->quadTo(dstP[0], dstP[1]);
break;
case SkPath::kCubic_Verb:
morphpoints(dstP, &srcP[1], 3, meas, matrix);
dst->cubicTo(dstP[0], dstP[1], dstP[2]);
break;
case SkPath::kClose_Verb:
dst->close();
break;
default:
SkDEBUGFAIL("unknown verb");
break;
}
}
}
void TextArt::EnvelopeWarp::weight(const SkPoint src[], const SkPoint tSrc[], const SkPoint bSrc[], int count, const SkRect& srcBounds, SkPoint dst[])
{
for (int i = 0; i < count; i++)
{
SkScalar origY = src[i].fY;
SkScalar origX = src[i].fX;
dst[i].fX = bSrc[i].fX;
dst[i].fY = origY;
if (srcBounds.fBottom > 0)
origY -= srcBounds.fBottom;
SkScalar k1 = SkScalarAbs( SkScalarDiv(origY, srcBounds.height()) );
dst[i].fY = SkScalarInterp(bSrc[i].fY, tSrc[i].fY, k1);
if (xWeightingMode_ & XWeightingMode_Interpolating)
{ //in Interpolating mode calculate X as interpolation beween Top and Bottom
dst[i].fX = SkScalarInterp(bSrc[i].fX, tSrc[i].fX, k1);
}
}
}
void TextArt::EnvelopeWarp::weight(const SkPath& path, const SkPath& top, const SkPath& bottom, SkPath* dst)
{
SkPath::Iter iterB(bottom, false);
SkPath::Iter iterT(top, false);
SkPath::Iter iter(path, false);
SkPoint tSrc[4], bSrc[4], src[4], dstP[4];
SkPath::Verb verbT, verbB, verb;
const SkRect& boundsSrc = path.getBounds();
while (((verbT = iterT.next(tSrc)) != SkPath::kDone_Verb) &&
((verbB = iterB.next(bSrc)) != SkPath::kDone_Verb) &&
((verb = iter.next(src)) != SkPath::kDone_Verb) )
{
SkASSERT(verbT==verbB);
SkASSERT(verbT==verb);
switch (verbT)
{
case SkPath::kMove_Verb:
weight(&src[0], &tSrc[0], &bSrc[0], 1, boundsSrc, dstP);
dst->moveTo(dstP[0]);
break;
case SkPath::kLine_Verb:
weight(&src[1], &tSrc[1], &bSrc[1], 1, boundsSrc, dstP);
dst->lineTo(dstP[0]);
break;
case SkPath::kQuad_Verb:
weight(&src[1], &tSrc[1], &bSrc[1], 2, boundsSrc, dstP);
dst->quadTo(dstP[0], dstP[1]);
break;
case SkPath::kCubic_Verb:
weight(&src[1], &tSrc[1], &bSrc[1], 3, boundsSrc, dstP);
dst->cubicTo(dstP[0], dstP[1], dstP[2]);
break;
case SkPath::kClose_Verb:
dst->close();
break;
default:
SkDEBUGFAIL("unknown verb");
break;
}
}
}