1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkDrawProcs_DEFINED
9 #define SkDrawProcs_DEFINED
10
11 #include "SkDraw.h"
12 #include "SkGlyph.h"
13
14 bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix&,
15 SkScalar* coverage);
16
17 /**
18 * If the current paint is set to stroke and the stroke-width when applied to
19 * the matrix is <= 1.0, then this returns true, and sets coverage (simulating
20 * a stroke by drawing a hairline with partial coverage). If any of these
21 * conditions are false, then this returns false and coverage is ignored.
22 */
SkDrawTreatAsHairline(const SkPaint & paint,const SkMatrix & matrix,SkScalar * coverage)23 inline bool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
24 SkScalar* coverage) {
25 if (SkPaint::kStroke_Style != paint.getStyle()) {
26 return false;
27 }
28
29 SkScalar strokeWidth = paint.getStrokeWidth();
30 if (0 == strokeWidth) {
31 *coverage = SK_Scalar1;
32 return true;
33 }
34
35 if (!paint.isAntiAlias()) {
36 return false;
37 }
38
39 return SkDrawTreatAAStrokeAsHairline(strokeWidth, matrix, coverage);
40 }
41
42 class SkTextAlignProc {
43 public:
SkTextAlignProc(SkPaint::Align align)44 SkTextAlignProc(SkPaint::Align align)
45 : fAlign(align) {
46 }
47
48 // Returns the glyph position, which may be rounded or not by the caller
49 // e.g. subpixel doesn't round.
operator()50 void operator()(const SkPoint& loc, const SkGlyph& glyph, SkPoint* dst) {
51 if (SkPaint::kLeft_Align == fAlign) {
52 dst->set(loc.fX, loc.fY);
53 } else if (SkPaint::kCenter_Align == fAlign) {
54 dst->set(loc.fX - SkFloatToScalar(glyph.fAdvanceX) / 2,
55 loc.fY - SkFloatToScalar(glyph.fAdvanceY) / 2);
56 } else {
57 SkASSERT(SkPaint::kRight_Align == fAlign);
58 dst->set(loc.fX - SkFloatToScalar(glyph.fAdvanceX),
59 loc.fY - SkFloatToScalar(glyph.fAdvanceY));
60 }
61 }
62 private:
63 const SkPaint::Align fAlign;
64 };
65
66 #endif
67