blob: 81ef5dee5026aa844f4b4245b2d19b46a888691a [file] [log] [blame]
kkinnunencb9a2c82014-06-12 23:06:28 -07001/*
2 * Copyright 2014 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 SkTextMapStateProc_DEFINED
9#define SkTextMapStateProc_DEFINED
10
11#include "SkPoint.h"
12#include "SkMatrix.h"
13
14class SkTextMapStateProc {
15public:
fmalita05c4a432014-09-29 06:29:53 -070016 SkTextMapStateProc(const SkMatrix& matrix, const SkPoint& offset, int scalarsPerPosition)
kkinnunencb9a2c82014-06-12 23:06:28 -070017 : fMatrix(matrix)
18 , fProc(matrix.getMapXYProc())
fmalita05c4a432014-09-29 06:29:53 -070019 , fOffset(offset)
20 , fScaleX(fMatrix.getScaleX()) {
kkinnunencb9a2c82014-06-12 23:06:28 -070021 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
22 if (1 == scalarsPerPosition) {
23 unsigned mtype = fMatrix.getType();
24 if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
25 fMapCase = kX;
26 } else {
fmalita05c4a432014-09-29 06:29:53 -070027 // Bake the matrix scale/translation components into fOffset,
28 // to expedite proc computations.
Mike Reeda99b6ce2017-02-04 11:04:26 -050029 fOffset.set(offset.x() * fMatrix.getScaleX() + fMatrix.getTranslateX(),
30 offset.y() * fMatrix.getScaleY() + fMatrix.getTranslateY());
fmalita05c4a432014-09-29 06:29:53 -070031
kkinnunencb9a2c82014-06-12 23:06:28 -070032 if (mtype & SkMatrix::kScale_Mask) {
33 fMapCase = kOnlyScaleX;
34 } else {
35 fMapCase = kOnlyTransX;
36 }
37 }
38 } else {
39 fMapCase = kXY;
40 }
41 }
42
43 void operator()(const SkScalar pos[], SkPoint* loc) const;
44
45private:
46 const SkMatrix& fMatrix;
47 enum {
48 kXY,
49 kOnlyScaleX,
50 kOnlyTransX,
51 kX
52 } fMapCase;
53 const SkMatrix::MapXYProc fProc;
fmalita05c4a432014-09-29 06:29:53 -070054 SkPoint fOffset; // In kOnly* mode, this includes the matrix translation component.
55 SkScalar fScaleX; // This is only used by kOnly... cases.
kkinnunencb9a2c82014-06-12 23:06:28 -070056};
57
58inline void SkTextMapStateProc::operator()(const SkScalar pos[], SkPoint* loc) const {
59 switch(fMapCase) {
60 case kXY:
fmalita05c4a432014-09-29 06:29:53 -070061 fProc(fMatrix, pos[0] + fOffset.x(), pos[1] + fOffset.y(), loc);
kkinnunencb9a2c82014-06-12 23:06:28 -070062 break;
63 case kOnlyScaleX:
Mike Reeda99b6ce2017-02-04 11:04:26 -050064 loc->set(fScaleX * *pos + fOffset.x(), fOffset.y());
kkinnunencb9a2c82014-06-12 23:06:28 -070065 break;
66 case kOnlyTransX:
fmalita05c4a432014-09-29 06:29:53 -070067 loc->set(*pos + fOffset.x(), fOffset.y());
kkinnunencb9a2c82014-06-12 23:06:28 -070068 break;
69 default:
70 SkASSERT(false);
71 case kX:
fmalita05c4a432014-09-29 06:29:53 -070072 fProc(fMatrix, *pos + fOffset.x(), fOffset.y(), loc);
kkinnunencb9a2c82014-06-12 23:06:28 -070073 break;
74 }
75}
76
77#endif