Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2144)

Unified Diff: src/core/SkDrawProcs.h

Issue 335573002: Extract "text align proc" functions as reusable classes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: address review comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkDraw.cpp ('k') | src/core/SkTextMapStateProc.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkDrawProcs.h
diff --git a/src/core/SkDrawProcs.h b/src/core/SkDrawProcs.h
index cefd2cce02ccac1aedf0d674203300ceb8b77445..d059c674701fe146ef9076c6068e42be1816c86e 100644
--- a/src/core/SkDrawProcs.h
+++ b/src/core/SkDrawProcs.h
@@ -10,6 +10,7 @@
#include "SkBlitter.h"
#include "SkDraw.h"
+#include "SkGlyph.h"
class SkAAClip;
class SkBlitter;
@@ -86,4 +87,53 @@ inline bool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
return SkDrawTreatAAStrokeAsHairline(strokeWidth, matrix, coverage);
}
+class SkTextAlignProc {
+public:
+ SkTextAlignProc(SkPaint::Align align)
+ : fAlign(align) {
+ }
+
+ // Returns the position of the glyph in fixed point, which may be rounded or not
+ // by the caller e.g. subpixel doesn't round.
+ // @param point interpreted as SkFixed [x, y].
+ void operator()(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) {
+ if (SkPaint::kLeft_Align == fAlign) {
+ dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
+ } else if (SkPaint::kCenter_Align == fAlign) {
+ dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
+ SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
+ } else {
+ SkASSERT(SkPaint::kRight_Align == fAlign);
+ dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
+ SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
+ }
+ }
+private:
+ const SkPaint::Align fAlign;
+};
+
+class SkTextAlignProcScalar {
+public:
+ SkTextAlignProcScalar(SkPaint::Align align)
+ : fAlign(align) {
+ }
+
+ // Returns the glyph position, which may be rounded or not by the caller
+ // e.g. subpixel doesn't round.
+ void operator()(const SkPoint& loc, const SkGlyph& glyph, SkPoint* dst) {
+ if (SkPaint::kLeft_Align == fAlign) {
+ dst->set(loc.fX, loc.fY);
+ } else if (SkPaint::kCenter_Align == fAlign) {
+ dst->set(loc.fX - SkFixedToScalar(glyph.fAdvanceX >> 1),
+ loc.fY - SkFixedToScalar(glyph.fAdvanceY >> 1));
+ } else {
+ SkASSERT(SkPaint::kRight_Align == fAlign);
+ dst->set(loc.fX - SkFixedToScalar(glyph.fAdvanceX),
+ loc.fY - SkFixedToScalar(glyph.fAdvanceY));
+ }
+ }
+private:
+ const SkPaint::Align fAlign;
+};
+
#endif
« no previous file with comments | « src/core/SkDraw.cpp ('k') | src/core/SkTextMapStateProc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698