blob: 15c5cf866a537c47ef37e4644057faa808292139 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed868074b2014-06-03 10:53:59 -07007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkDrawProcs_DEFINED
9#define SkDrawProcs_DEFINED
10
11#include "SkDraw.h"
kkinnunencb9a2c82014-06-12 23:06:28 -070012#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000014bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix&,
15 SkScalar* coverage);
16
reed@google.comecadf992011-09-19 19:18:18 +000017/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000018 * If the current paint is set to stroke and the stroke-width when applied to
bsalomon@google.comdd1be602012-01-18 20:34:00 +000019 * the matrix is <= 1.0, then this returns true, and sets coverage (simulating
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020 * a stroke by drawing a hairline with partial coverage). If any of these
bsalomon@google.comdd1be602012-01-18 20:34:00 +000021 * conditions are false, then this returns false and coverage is ignored.
reed@google.comecadf992011-09-19 19:18:18 +000022 */
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000023inline 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}
reed@google.comecadf992011-09-19 19:18:18 +000041
kkinnunencb9a2c82014-06-12 23:06:28 -070042class SkTextAlignProc {
43public:
44 SkTextAlignProc(SkPaint::Align align)
45 : fAlign(align) {
46 }
47
kkinnunencb9a2c82014-06-12 23:06:28 -070048 // Returns the glyph position, which may be rounded or not by the caller
49 // e.g. subpixel doesn't round.
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) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -070054 dst->set(loc.fX - SkFloatToScalar(glyph.fAdvanceX) / 2,
55 loc.fY - SkFloatToScalar(glyph.fAdvanceY) / 2);
kkinnunencb9a2c82014-06-12 23:06:28 -070056 } else {
57 SkASSERT(SkPaint::kRight_Align == fAlign);
benjaminwagner6b3eacb2016-03-24 19:07:58 -070058 dst->set(loc.fX - SkFloatToScalar(glyph.fAdvanceX),
59 loc.fY - SkFloatToScalar(glyph.fAdvanceY));
kkinnunencb9a2c82014-06-12 23:06:28 -070060 }
61 }
62private:
63 const SkPaint::Align fAlign;
64};
65
reed@android.com8a1c16f2008-12-17 15:59:43 +000066#endif