Fix SDF font positions when using global scale.

Also fixes a crash in the dftext GM when using SampleApp,
and adds new test case in dftext.

BUG=skia:2928
R=joshualitt@google.com, egdaniel@google.com

Author: jvanverth@google.com

Review URL: https://codereview.chromium.org/588223002
This commit is contained in:
jvanverth 2014-09-22 14:26:53 -07:00 committed by Commit bot
parent c06482494d
commit 76ce81e5e6
3 changed files with 50 additions and 22 deletions

View File

@ -51,10 +51,13 @@ protected:
SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag,
SkSurfaceProps::kLegacyFontHost_InitType);
SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, info, 0, &props));
SkCanvas* canvas = surface->getCanvas();
SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas;
#else
SkCanvas* canvas = inputCanvas;
#endif
// apply global scale to test glyph positioning
canvas->scale(1.05f, 1.05f);
canvas->clear(0xffffffff);
SkPaint paint;
@ -97,7 +100,7 @@ protected:
// check scaling down
paint.setLCDRenderText(true);
x = SkIntToScalar(700);
x = SkIntToScalar(680);
y = SkIntToScalar(20);
size_t arraySize = SK_ARRAY_COUNT(textSizes);
for (size_t i = 0; i < arraySize; ++i) {
@ -110,6 +113,29 @@ protected:
y += paint.getFontMetrics(NULL)*scaleFactor;
}
// check pos text
{
SkAutoCanvasRestore acr(canvas, true);
canvas->scale(2.0f, 2.0f);
SkAutoTArray<SkPoint> pos(textLen);
SkAutoTArray<SkScalar> widths(textLen);
paint.setTextSize(textSizes[0]);
paint.getTextWidths(text, textLen, &widths[0]);
SkScalar x = SkIntToScalar(340);
SkScalar y = SkIntToScalar(75);
for (unsigned int i = 0; i < textLen; ++i) {
pos[i].set(x, y);
x += widths[i];
}
canvas->drawPosText(text, textLen, &pos[0], paint);
}
// check gamma-corrected blending
const SkColor fg[] = {
0xFFFFFFFF,
@ -119,10 +145,10 @@ protected:
};
paint.setColor(0xFFF1F1F1);
SkRect r = SkRect::MakeLTRB(690, 250, 840, 460);
SkRect r = SkRect::MakeLTRB(670, 250, 820, 460);
canvas->drawRect(r, paint);
x = SkIntToScalar(700);
x = SkIntToScalar(680);
y = SkIntToScalar(270);
paint.setTextSize(SkIntToScalar(22));
for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
@ -133,10 +159,10 @@ protected:
}
paint.setColor(0xFF1F1F1F);
r = SkRect::MakeLTRB(840, 250, 990, 460);
r = SkRect::MakeLTRB(820, 250, 970, 460);
canvas->drawRect(r, paint);
x = SkIntToScalar(850);
x = SkIntToScalar(830);
y = SkIntToScalar(270);
paint.setTextSize(SkIntToScalar(22));
for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
@ -148,9 +174,11 @@ protected:
#if SK_SUPPORT_GPU
// render offscreen buffer
if (surface) {
SkImage* image = surface->newImageSnapshot();
image->draw(inputCanvas, 0, 0, NULL);
image->unref();
}
#endif
}

View File

@ -124,12 +124,13 @@ void GrDistanceFieldTextContext::setupCoverageEffect(const SkColor& filteredColo
GrTextureParams gammaParams(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
uint32_t textureUniqueID = fCurrTexture->getUniqueID();
const SkMatrix& ctm = fContext->getMatrix();
// set up any flags
uint32_t flags = 0;
flags |= fTextMatrix.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
flags |= fUseLCDText ? kUseLCD_DistanceFieldEffectFlag : 0;
flags |= fUseLCDText && fTextMatrix.rectStaysRect() ?
flags |= fUseLCDText && ctm.rectStaysRect() ?
kRectToRect_DistanceFieldEffectFlag : 0;
bool useBGR = SkPixelGeometryIsBGR(fDeviceProperties.fPixelGeometry);
flags |= fUseLCDText && useBGR ? kBGR_DistanceFieldEffectFlag : 0;
@ -176,7 +177,7 @@ void GrDistanceFieldTextContext::flushGlyphs() {
GrDrawState* drawState = fDrawTarget->drawState();
GrDrawState::AutoRestoreEffects are(drawState);
drawState->setFromPaint(fPaint, fTextMatrix, fContext->getRenderTarget());
drawState->setFromPaint(fPaint, fContext->getMatrix(), fContext->getRenderTarget());
if (fCurrVertex > 0) {
// setup our sampler state for our text texture/atlas
@ -445,28 +446,28 @@ inline void GrDistanceFieldTextContext::init(const GrPaint& paint, const SkPaint
fStrike = NULL;
fTextMatrix = fContext->getMatrix();
const SkMatrix& ctm = fContext->getMatrix();
// getMaxScale doesn't support perspective, so neither do we at the moment
SkASSERT(!fTextMatrix.hasPerspective());
SkScalar maxScale = fTextMatrix.getMaxScale();
SkASSERT(!ctm.hasPerspective());
SkScalar maxScale = ctm.getMaxScale();
SkScalar textSize = fSkPaint.getTextSize();
// if we have non-unity scale, we need to adjust our text size accordingly
// to avoid aliasing, and prescale the matrix by the inverse to end up with the same size
SkScalar scaledTextSize = textSize;
// if we have non-unity scale, we need to choose our base text size
// based on the SkPaint's text size multiplied by the max scale factor
// TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
textSize *= maxScale;
fTextMatrix.preScale(SK_Scalar1 / maxScale, SK_Scalar1 / maxScale);
scaledTextSize *= maxScale;
}
fCurrVertex = 0;
fVertices = NULL;
if (textSize <= kSmallDFFontLimit) {
if (scaledTextSize <= kSmallDFFontLimit) {
fTextRatio = textSize / kSmallDFFontSize;
fSkPaint.setTextSize(SkIntToScalar(kSmallDFFontSize));
} else if (textSize <= kMediumDFFontLimit) {
} else if (scaledTextSize <= kMediumDFFontLimit) {
fTextRatio = textSize / kMediumDFFontSize;
fSkPaint.setTextSize(SkIntToScalar(kMediumDFFontSize));
} else {

View File

@ -31,7 +31,6 @@ public:
private:
GrTextStrike* fStrike;
SkMatrix fTextMatrix;
SkScalar fTextRatio;
bool fUseLCDText;
bool fEnableDFRendering;