2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkBlurMask.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
2016-07-28 16:47:24 +00:00
|
|
|
#include "SkTextBlob.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkWriteBuffer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2016-04-04 17:02:58 +00:00
|
|
|
p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
p.setMaskFilter(nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x11);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrc);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x40);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrc);
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*2);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStrokeAndFill_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*4);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*3/2);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kClear);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*3);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x20);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrc);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x60);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0xFF);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kClear);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrcOver);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkDiscretePathEffect.h"
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrcOut);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(false);
|
2014-02-26 13:27:37 +00:00
|
|
|
SkLayerRasterizer::Builder rastBuilder2;
|
|
|
|
r5(&rastBuilder2, p);
|
2016-03-21 20:25:16 +00:00
|
|
|
p.setRasterizer(rastBuilder2.detach());
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kClear);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "Sk2DPathEffect.h"
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
|
2011-08-09 18:47:40 +00:00
|
|
|
SkPath path;
|
|
|
|
path.addCircle(0, 0, radius);
|
2016-03-18 18:22:57 +00:00
|
|
|
return SkPath2DPathEffect::Make(matrix, path);
|
2011-08-09 18:47:40 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
|
|
|
|
lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice));
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
|
|
|
|
lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice));
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kClear);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
p.setPathEffect(nullptr);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrcOver);
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
|
|
|
|
lattice.postRotate(SkIntToScalar(30), 0, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kClear);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
p.setPathEffect(nullptr);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrcOver);
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr raster_proc gRastProcs[] = {
|
2008-12-17 15:59:43 +00:00
|
|
|
r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
|
|
|
|
};
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void apply_shader(SkPaint* paint, int index) {
|
2008-12-17 15:59:43 +00:00
|
|
|
raster_proc proc = gRastProcs[index];
|
2016-03-21 20:25:16 +00:00
|
|
|
if (proc) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
2014-02-26 13:27:37 +00:00
|
|
|
SkLayerRasterizer::Builder rastBuilder;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(true);
|
2014-02-26 13:27:37 +00:00
|
|
|
proc(&rastBuilder, p);
|
2016-03-21 20:25:16 +00:00
|
|
|
paint->setRasterizer(rastBuilder.detach());
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#if 0
|
|
|
|
SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
|
2011-02-07 15:30:46 +00:00
|
|
|
paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
paint->setColor(SK_ColorBLUE);
|
|
|
|
}
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(texteffects, canvas, 460, 680) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2010-05-17 14:50:04 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(56));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = paint.getTextSize();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
SkString str("Hamburgefons");
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-10-16 17:48:11 +00:00
|
|
|
for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
apply_shader(&paint, i);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
// paint.setMaskFilter(nullptr);
|
2012-08-13 00:52:07 +00:00
|
|
|
// paint.setColor(SK_ColorBLACK);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawText(str.c_str(), str.size(), x, y, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
y += paint.getFontSpacing();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2015-12-21 16:35:51 +00:00
|
|
|
|
2016-02-09 21:25:45 +00:00
|
|
|
static SkPath create_underline(const SkTDArray<SkScalar>& intersections,
|
|
|
|
SkScalar last, SkScalar finalPos,
|
|
|
|
SkScalar uPos, SkScalar uWidth, SkScalar textSize) {
|
|
|
|
SkPath underline;
|
|
|
|
SkScalar end = last;
|
|
|
|
for (int index = 0; index < intersections.count(); index += 2) {
|
|
|
|
SkScalar start = intersections[index] - uWidth;;
|
|
|
|
end = intersections[index + 1] + uWidth;
|
|
|
|
if (start > last && last + textSize / 12 < start) {
|
|
|
|
underline.moveTo(last, uPos);
|
|
|
|
underline.lineTo(start, uPos);
|
|
|
|
}
|
|
|
|
last = end;
|
|
|
|
}
|
|
|
|
if (end < finalPos) {
|
|
|
|
underline.moveTo(end, uPos);
|
|
|
|
underline.lineTo(finalPos, uPos);
|
|
|
|
}
|
|
|
|
return underline;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_intercepts(const char* test, size_t len, SkScalar x, SkScalar y,
|
|
|
|
const SkPaint& paint, SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
|
|
|
|
SkScalar uPos = y + uWidth;
|
|
|
|
SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
|
|
|
|
int count = paint.getTextIntercepts(test, len, x, y, bounds, nullptr);
|
|
|
|
SkASSERT(!(count % 2));
|
|
|
|
if (count) {
|
|
|
|
intersections->setCount(count);
|
|
|
|
paint.getTextIntercepts(test, len, x, y, bounds, intersections->begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(fancyunderline, canvas, 900, 1350) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
const char* fam[] = { "sans-serif", "serif", "monospace" };
|
|
|
|
const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
|
|
|
|
SkPoint textPt = { 10, 80 };
|
2016-07-28 16:47:24 +00:00
|
|
|
for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint, fam[font]);
|
2016-02-09 21:25:45 +00:00
|
|
|
for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
const SkScalar uWidth = textSize / 15;
|
|
|
|
paint.setStrokeWidth(uWidth);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
canvas->drawText(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint);
|
|
|
|
|
|
|
|
SkTDArray<SkScalar> intersections;
|
|
|
|
find_intercepts(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint, uWidth,
|
|
|
|
&intersections);
|
|
|
|
|
|
|
|
SkScalar start = textPt.fX;
|
|
|
|
SkScalar end = paint.measureText(test, sizeof(test) - 1) + textPt.fX;
|
|
|
|
SkScalar uPos = textPt.fY + uWidth;
|
|
|
|
SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(underline, paint);
|
|
|
|
|
|
|
|
canvas->translate(0, textSize * 1.3f);
|
|
|
|
}
|
|
|
|
canvas->translate(0, 60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_intercepts(const char* test, size_t len, const SkPoint* pos, const SkPaint& paint,
|
|
|
|
SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
|
|
|
|
SkScalar uPos = pos[0].fY + uWidth;
|
|
|
|
SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
|
|
|
|
int count = paint.getPosTextIntercepts(test, len, pos, bounds, nullptr);
|
|
|
|
SkASSERT(!(count % 2));
|
|
|
|
if (count) {
|
|
|
|
intersections->setCount(count);
|
|
|
|
paint.getPosTextIntercepts(test, len, pos, bounds, intersections->begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(fancyposunderline, canvas, 900, 1350) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
const char* fam[] = { "sans-serif", "serif", "monospace" };
|
|
|
|
const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
|
|
|
|
SkPoint textPt = { 10, 80 };
|
2016-07-28 16:47:24 +00:00
|
|
|
for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint, fam[font]);
|
2016-02-09 21:25:45 +00:00
|
|
|
for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
const SkScalar uWidth = textSize / 15;
|
|
|
|
paint.setStrokeWidth(uWidth);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
|
|
|
|
SkTDArray<SkScalar> widths;
|
|
|
|
widths.setCount(widthCount);
|
|
|
|
(void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
|
|
|
|
SkTDArray<SkPoint> pos;
|
|
|
|
pos.setCount(widthCount);
|
|
|
|
SkScalar posX = textPt.fX;
|
|
|
|
for (int index = 0; index < widthCount; ++index) {
|
|
|
|
pos[index].fX = posX;
|
|
|
|
posX += widths[index];
|
|
|
|
pos[index].fY = textPt.fY + (textSize / 25) * (index % 4);
|
|
|
|
}
|
|
|
|
canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
|
|
|
|
|
|
|
|
SkTDArray<SkScalar> intersections;
|
|
|
|
find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
|
|
|
|
|
|
|
|
SkScalar start = textPt.fX;
|
|
|
|
SkScalar end = posX;
|
|
|
|
SkScalar uPos = textPt.fY + uWidth;
|
|
|
|
SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(underline, paint);
|
|
|
|
|
|
|
|
canvas->translate(0, textSize * 1.3f);
|
|
|
|
}
|
|
|
|
canvas->translate(0, 60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 16:47:24 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) {
|
2016-07-28 16:47:24 +00:00
|
|
|
SkPaint blobPaint(paint);
|
|
|
|
|
|
|
|
const size_t textLen = strlen(text);
|
|
|
|
const int glyphCount = blobPaint.textToGlyphs(text, textLen, nullptr);
|
|
|
|
SkAutoTArray<SkGlyphID> glyphs(glyphCount);
|
|
|
|
blobPaint.textToGlyphs(text, textLen, glyphs.get());
|
|
|
|
|
|
|
|
blobPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
const size_t glyphTextBytes = SkTo<uint32_t>(glyphCount) * sizeof(SkGlyphID);
|
|
|
|
const int widthCount = blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, nullptr);
|
|
|
|
SkAssertResult(widthCount == glyphCount);
|
|
|
|
|
|
|
|
SkAutoTArray<SkScalar> widths(glyphCount);
|
|
|
|
blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, widths.get());
|
|
|
|
|
|
|
|
SkTextBlobBuilder blobBuilder;
|
|
|
|
int glyphIndex = 0;
|
|
|
|
SkScalar advance = 0;
|
|
|
|
|
|
|
|
// Default-positioned run.
|
|
|
|
{
|
|
|
|
const int defaultRunLen = glyphCount / 3;
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRun(blobPaint,
|
|
|
|
defaultRunLen,
|
|
|
|
advance, 0);
|
|
|
|
memcpy(buf.glyphs, glyphs.get(), SkTo<uint32_t>(defaultRunLen) * sizeof(SkGlyphID));
|
|
|
|
|
|
|
|
for (int i = 0; i < defaultRunLen; ++i) {
|
|
|
|
advance += widths[glyphIndex++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontal-positioned run.
|
|
|
|
{
|
|
|
|
const int horizontalRunLen = glyphCount / 3;
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPosH(blobPaint,
|
|
|
|
horizontalRunLen,
|
|
|
|
0);
|
|
|
|
memcpy(buf.glyphs, glyphs.get() + glyphIndex,
|
|
|
|
SkTo<uint32_t>(horizontalRunLen) * sizeof(SkGlyphID));
|
|
|
|
for (int i = 0; i < horizontalRunLen; ++i) {
|
|
|
|
buf.pos[i] = advance;
|
|
|
|
advance += widths[glyphIndex++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full-positioned run.
|
|
|
|
{
|
|
|
|
const int fullRunLen = glyphCount - glyphIndex;
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPos(blobPaint, fullRunLen);
|
|
|
|
memcpy(buf.glyphs, glyphs.get() + glyphIndex,
|
|
|
|
SkTo<uint32_t>(fullRunLen) * sizeof(SkGlyphID));
|
|
|
|
for (int i = 0; i < fullRunLen; ++i) {
|
|
|
|
buf.pos[i * 2 + 0] = advance; // x offset
|
|
|
|
buf.pos[i * 2 + 1] = 0; // y offset
|
|
|
|
advance += widths[glyphIndex++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
return blobBuilder.make();
|
2016-07-28 16:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous ns
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(fancyblobunderline, canvas, 1480, 1380) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
const char* fam[] = { "sans-serif", "serif", "monospace" };
|
|
|
|
const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
|
|
|
|
const SkPoint blobOffset = { 10, 80 };
|
|
|
|
|
|
|
|
for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
|
|
|
|
sk_tool_utils::set_portable_typeface(&paint, fam[font]);
|
|
|
|
for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
const SkScalar uWidth = textSize / 15;
|
|
|
|
paint.setStrokeWidth(uWidth);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, test);
|
|
|
|
canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint);
|
2016-07-28 16:47:24 +00:00
|
|
|
|
|
|
|
const SkScalar uPos = uWidth;
|
|
|
|
const SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
|
|
|
|
const int interceptCount = paint.getTextBlobIntercepts(blob.get(), bounds, nullptr);
|
|
|
|
SkASSERT(!(interceptCount % 2));
|
|
|
|
|
|
|
|
SkTDArray<SkScalar> intercepts;
|
|
|
|
intercepts.setCount(interceptCount);
|
|
|
|
paint.getTextBlobIntercepts(blob.get(), bounds, intercepts.begin());
|
|
|
|
|
|
|
|
const SkScalar start = blob->bounds().left();
|
|
|
|
const SkScalar end = blob->bounds().right();
|
|
|
|
SkPath underline = create_underline(intercepts, start, end, uPos, uWidth, textSize);
|
|
|
|
underline.offset(blobOffset.x(), blobOffset.y());
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(underline, paint);
|
|
|
|
|
|
|
|
canvas->translate(0, textSize * 1.3f);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->translate(0, 60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 21:25:45 +00:00
|
|
|
DEF_SIMPLE_GM(fancyunderlinebars, canvas, 1500, 460) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
const char test[] = " .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_";
|
|
|
|
SkPoint textPt = { 10, 80 };
|
|
|
|
sk_tool_utils::set_portable_typeface(&paint, "serif");
|
|
|
|
for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
SkScalar uWidth = textSize / 15;
|
|
|
|
paint.setStrokeWidth(uWidth);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
|
|
|
|
SkTDArray<SkScalar> widths;
|
|
|
|
widths.setCount(widthCount);
|
|
|
|
(void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
|
|
|
|
SkTDArray<SkPoint> pos;
|
|
|
|
pos.setCount(widthCount);
|
|
|
|
SkScalar posX = textPt.fX;
|
|
|
|
pos[0] = textPt;
|
|
|
|
posX += widths[0];
|
|
|
|
for (int index = 1; index < widthCount; ++index) {
|
|
|
|
pos[index].fX = posX;
|
|
|
|
posX += widths[index];
|
|
|
|
pos[index].fY = textPt.fY - (textSize / 50) * (index / 5) + textSize / 50 * 4;
|
|
|
|
}
|
|
|
|
canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
|
|
|
|
|
|
|
|
SkTDArray<SkScalar> intersections;
|
|
|
|
find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
|
|
|
|
|
|
|
|
SkScalar start = textPt.fX;
|
|
|
|
SkScalar end = posX;
|
|
|
|
SkScalar uPos = pos[0].fY + uWidth;
|
|
|
|
SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(underline, paint);
|
|
|
|
canvas->translate(0, textSize * 1.3f);
|
|
|
|
}
|
|
|
|
}
|