Skip to content
Snippets Groups Projects
Commit eeea2b7e authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "fix(high contrast text): draw a solid rectangle background behind text...

Merge "fix(high contrast text): draw a solid rectangle background behind text instead of a stroke border" into main
parents abcf3a1c 47ad8548
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,13 @@ flag {
bug: "186567103"
}
flag {
name: "high_contrast_text_small_text_rect"
namespace: "accessibility"
description: "Draw a solid rectangle background behind text instead of a stroke outline"
bug: "186567103"
}
flag {
name: "hdr_10bit_plus"
namespace: "core_graphics"
......
......@@ -18,6 +18,7 @@
#include <SkFontMetrics.h>
#include <SkRRect.h>
#include <minikin/MinikinRect.h>
#include "FeatureFlags.h"
#include "MinikinUtils.h"
......@@ -107,7 +108,13 @@ void Canvas::drawText(const uint16_t* text, int textSize, int start, int count,
// care of all alignment.
paint.setTextAlign(Paint::kLeft_Align);
DrawTextFunctor f(layout, this, paint, x, y, layout.getAdvance());
minikin::MinikinRect bounds;
// We only need the bounds to draw a rectangular background in high contrast mode. Let's save
// the cycles otherwise.
if (flags::high_contrast_text_small_text_rect() && isHighContrastText()) {
MinikinUtils::getBounds(&paint, bidiFlags, typeface, text, textSize, &bounds);
}
DrawTextFunctor f(layout, this, paint, x, y, layout.getAdvance(), bounds);
MinikinUtils::forFontRun(layout, &paint, f);
if (text_feature::fix_double_underline()) {
......
......@@ -33,6 +33,8 @@ namespace flags = com::android::graphics::hwui::flags;
namespace android {
inline constexpr int kHighContrastTextBorderWidth = 4;
static inline void drawStroke(SkScalar left, SkScalar right, SkScalar top, SkScalar thickness,
const Paint& paint, Canvas* canvas) {
const SkScalar strokeWidth = fmax(thickness, 1.0f);
......@@ -45,15 +47,26 @@ static void simplifyPaint(int color, Paint* paint) {
paint->setShader(nullptr);
paint->setColorFilter(nullptr);
paint->setLooper(nullptr);
paint->setStrokeWidth(4 + 0.04 * paint->getSkFont().getSize());
paint->setStrokeWidth(kHighContrastTextBorderWidth + 0.04 * paint->getSkFont().getSize());
paint->setStrokeJoin(SkPaint::kRound_Join);
paint->setLooper(nullptr);
}
class DrawTextFunctor {
public:
/**
* Creates a Functor to draw the given text layout.
*
* @param layout
* @param canvas
* @param paint
* @param x
* @param y
* @param totalAdvance
* @param bounds bounds of the text. Only required if high contrast text mode is enabled.
*/
DrawTextFunctor(const minikin::Layout& layout, Canvas* canvas, const Paint& paint, float x,
float y, float totalAdvance)
float y, float totalAdvance, const minikin::MinikinRect& bounds)
: layout(layout)
, canvas(canvas)
, paint(paint)
......@@ -61,7 +74,8 @@ public:
, y(y)
, totalAdvance(totalAdvance)
, underlinePosition(0)
, underlineThickness(0) {}
, underlineThickness(0)
, bounds(bounds) {}
void operator()(size_t start, size_t end) {
auto glyphFunc = [&](uint16_t* text, float* positions) {
......@@ -91,7 +105,16 @@ public:
Paint outlinePaint(paint);
simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, &outlinePaint);
outlinePaint.setStyle(SkPaint::kStrokeAndFill_Style);
canvas->drawGlyphs(glyphFunc, glyphCount, outlinePaint, x, y, totalAdvance);
if (flags::high_contrast_text_small_text_rect()) {
auto bgBounds(bounds);
auto padding = kHighContrastTextBorderWidth + 0.1f * paint.getSkFont().getSize();
bgBounds.offset(x, y);
canvas->drawRect(bgBounds.mLeft - padding, bgBounds.mTop - padding,
bgBounds.mRight + padding, bgBounds.mBottom + padding,
outlinePaint);
} else {
canvas->drawGlyphs(glyphFunc, glyphCount, outlinePaint, x, y, totalAdvance);
}
// inner
gDrawTextBlobMode = DrawTextBlobMode::HctInner;
......@@ -146,6 +169,7 @@ private:
float totalAdvance;
float underlinePosition;
float underlineThickness;
const minikin::MinikinRect& bounds;
};
} // namespace android
......@@ -103,8 +103,9 @@ DrawTextFunctor processFunctor(const std::vector<uint16_t>& text, Paint* paint)
// Create minikin::Layout
std::unique_ptr<Typeface> typeface(makeTypeface());
minikin::Layout layout = doLayout(text, *paint, typeface.get());
minikin::MinikinRect bounds;
DrawTextFunctor f(layout, &canvas, *paint, 0, 0, layout.getAdvance());
DrawTextFunctor f(layout, &canvas, *paint, 0, 0, layout.getAdvance(), bounds);
MinikinUtils::forFontRun(layout, paint, f);
return f;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment