aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkDrawProcs.h
diff options
context:
space:
mode:
authorGravatar kkinnunen <kkinnunen@nvidia.com>2014-06-12 23:06:28 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-06-12 23:06:28 -0700
commitcb9a2c8934f009b6ee1ca73d662ac18b285085d9 (patch)
treec9f4368146e0c657075a463a769627e26980ec27 /src/core/SkDrawProcs.h
parent39e58adb99111acbcc0b115e44812a3090bd6a2b (diff)
Extract "text align proc" functions as reusable classes
Extract "text align proc" as reusable classes. These classes need to be used when writing GrTextContext subclasses. Moves "text align proc" code that is duplicated in SkDraw and SkBitmapTextContext to SkDrawProcs.h and SkTextMapState.h. This functionality is also used in the new GrStencilAndCoverTextContext. Creates new functor classes SkTextAlignProc and SkTextAlignProcScalar which represent the previous "text align procs". Moves TextMapState from SkDraw to SkTextMapStateProc and make it similar functor. The transform should be comparable in speed, as the compiler can and does avoid the call and eliminate some of the branches. R=jvanverth@google.com, reed@google.com Author: kkinnunen@nvidia.com Review URL: https://codereview.chromium.org/335573002
Diffstat (limited to 'src/core/SkDrawProcs.h')
-rw-r--r--src/core/SkDrawProcs.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/core/SkDrawProcs.h b/src/core/SkDrawProcs.h
index cefd2cce02..d059c67470 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