blob: d059c674701fe146ef9076c6068e42be1816c86e [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
reed@google.com5bdfb332013-05-02 18:55:44 +000011#include "SkBlitter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkDraw.h"
kkinnunencb9a2c82014-06-12 23:06:28 -070013#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
reed@google.com045e62d2011-10-24 12:19:46 +000015class SkAAClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +000016class SkBlitter;
17
18struct SkDraw1Glyph {
bungeman@google.com2211b622012-01-13 15:02:58 +000019 const SkDraw* fDraw;
bungeman@google.com2211b622012-01-13 15:02:58 +000020 const SkRegion* fClip;
21 const SkAAClip* fAAClip;
22 SkBlitter* fBlitter;
23 SkGlyphCache* fCache;
reed@google.com5bdfb332013-05-02 18:55:44 +000024 const SkPaint* fPaint;
bungeman@google.com2211b622012-01-13 15:02:58 +000025 SkIRect fClipBounds;
bungeman@google.com94471032013-02-25 15:55:13 +000026 /** Half the sampling frequency of the rasterized glyph in x. */
27 SkFixed fHalfSampleX;
28 /** Half the sampling frequency of the rasterized glyph in y. */
29 SkFixed fHalfSampleY;
bungeman@google.com2211b622012-01-13 15:02:58 +000030
bungeman@google.com94471032013-02-25 15:55:13 +000031 /** Draws one glyph.
32 *
33 * The x and y are pre-biased, so implementations may just truncate them.
34 * i.e. half the sampling frequency has been added.
35 * e.g. 1/2 or 1/(2^(SkGlyph::kSubBits+1)) has already been added.
36 * This added bias can be found in fHalfSampleX,Y.
37 */
bungeman@google.com2211b622012-01-13 15:02:58 +000038 typedef void (*Proc)(const SkDraw1Glyph&, SkFixed x, SkFixed y, const SkGlyph&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039
reed@google.com5bdfb332013-05-02 18:55:44 +000040 Proc init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
41 const SkPaint&);
42
43 // call this instead of fBlitter->blitMask() since this wrapper will handle
44 // the case when the mask is ARGB32_Format
45 //
46 void blitMask(const SkMask& mask, const SkIRect& clip) const {
47 if (SkMask::kARGB32_Format == mask.fFormat) {
48 this->blitMaskAsSprite(mask);
49 } else {
50 fBlitter->blitMask(mask, clip);
51 }
52 }
53
54 // mask must be kARGB32_Format
55 void blitMaskAsSprite(const SkMask& mask) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056};
57
58struct SkDrawProcs {
59 SkDraw1Glyph::Proc fD1GProc;
60};
61
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000062bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix&,
63 SkScalar* coverage);
64
reed@google.comecadf992011-09-19 19:18:18 +000065/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066 * If the current paint is set to stroke and the stroke-width when applied to
bsalomon@google.comdd1be602012-01-18 20:34:00 +000067 * the matrix is <= 1.0, then this returns true, and sets coverage (simulating
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068 * a stroke by drawing a hairline with partial coverage). If any of these
bsalomon@google.comdd1be602012-01-18 20:34:00 +000069 * conditions are false, then this returns false and coverage is ignored.
reed@google.comecadf992011-09-19 19:18:18 +000070 */
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000071inline bool SkDrawTreatAsHairline(const SkPaint& paint, const SkMatrix& matrix,
72 SkScalar* coverage) {
73 if (SkPaint::kStroke_Style != paint.getStyle()) {
74 return false;
75 }
76
77 SkScalar strokeWidth = paint.getStrokeWidth();
78 if (0 == strokeWidth) {
79 *coverage = SK_Scalar1;
80 return true;
81 }
82
83 if (!paint.isAntiAlias()) {
84 return false;
85 }
86
87 return SkDrawTreatAAStrokeAsHairline(strokeWidth, matrix, coverage);
88}
reed@google.comecadf992011-09-19 19:18:18 +000089
kkinnunencb9a2c82014-06-12 23:06:28 -070090class SkTextAlignProc {
91public:
92 SkTextAlignProc(SkPaint::Align align)
93 : fAlign(align) {
94 }
95
96 // Returns the position of the glyph in fixed point, which may be rounded or not
97 // by the caller e.g. subpixel doesn't round.
98 // @param point interpreted as SkFixed [x, y].
99 void operator()(const SkPoint& loc, const SkGlyph& glyph, SkIPoint* dst) {
100 if (SkPaint::kLeft_Align == fAlign) {
101 dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY));
102 } else if (SkPaint::kCenter_Align == fAlign) {
103 dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1),
104 SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1));
105 } else {
106 SkASSERT(SkPaint::kRight_Align == fAlign);
107 dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX,
108 SkScalarToFixed(loc.fY) - glyph.fAdvanceY);
109 }
110 }
111private:
112 const SkPaint::Align fAlign;
113};
114
115class SkTextAlignProcScalar {
116public:
117 SkTextAlignProcScalar(SkPaint::Align align)
118 : fAlign(align) {
119 }
120
121 // Returns the glyph position, which may be rounded or not by the caller
122 // e.g. subpixel doesn't round.
123 void operator()(const SkPoint& loc, const SkGlyph& glyph, SkPoint* dst) {
124 if (SkPaint::kLeft_Align == fAlign) {
125 dst->set(loc.fX, loc.fY);
126 } else if (SkPaint::kCenter_Align == fAlign) {
127 dst->set(loc.fX - SkFixedToScalar(glyph.fAdvanceX >> 1),
128 loc.fY - SkFixedToScalar(glyph.fAdvanceY >> 1));
129 } else {
130 SkASSERT(SkPaint::kRight_Align == fAlign);
131 dst->set(loc.fX - SkFixedToScalar(glyph.fAdvanceX),
132 loc.fY - SkFixedToScalar(glyph.fAdvanceY));
133 }
134 }
135private:
136 const SkPaint::Align fAlign;
137};
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139#endif