1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_GRAPHICS_PAINT_H_
18 #define ANDROID_GRAPHICS_PAINT_H_
19 
20 #include "Typeface.h"
21 
22 #include <cutils/compiler.h>
23 
24 #include <SkDrawLooper.h>
25 #include <SkFont.h>
26 #include <SkPaint.h>
27 #include <string>
28 
29 #include <minikin/FontFamily.h>
30 #include <minikin/FamilyVariant.h>
31 #include <minikin/Hyphenator.h>
32 
33 namespace android {
34 
35 class ANDROID_API Paint : public SkPaint {
36 public:
37     // Default values for underlined and strikethrough text,
38     // as defined by Skia in SkTextFormatParams.h.
39     constexpr static float kStdStrikeThru_Offset = (-6.0f / 21.0f);
40     constexpr static float kStdUnderline_Offset = (1.0f / 9.0f);
41     constexpr static float kStdUnderline_Thickness = (1.0f / 18.0f);
42 
43     constexpr static float kStdUnderline_Top =
44             kStdUnderline_Offset - 0.5f * kStdUnderline_Thickness;
45 
46     constexpr static float kStdStrikeThru_Thickness = kStdUnderline_Thickness;
47     constexpr static float kStdStrikeThru_Top =
48             kStdStrikeThru_Offset - 0.5f * kStdStrikeThru_Thickness;
49 
50     Paint();
51     Paint(const Paint& paint);
52     ~Paint();
53 
54     Paint& operator=(const Paint& other);
55 
56     friend bool operator==(const Paint& a, const Paint& b);
57     friend bool operator!=(const Paint& a, const Paint& b) { return !(a == b); }
58 
getSkFont()59     SkFont& getSkFont() { return mFont; }
getSkFont()60     const SkFont& getSkFont() const { return mFont; }
61 
getLooper()62     SkDrawLooper* getLooper() const { return mLooper.get(); }
setLooper(sk_sp<SkDrawLooper> looper)63     void setLooper(sk_sp<SkDrawLooper> looper) { mLooper = std::move(looper); }
64 
65     // These shadow the methods on SkPaint, but we need to so we can keep related
66     // attributes in-sync.
67 
68     void reset();
69     void setAntiAlias(bool);
70 
nothingToDraw()71     bool nothingToDraw() const { return !mLooper && SkPaint::nothingToDraw(); }
72 
73     // End method shadowing
74 
setLetterSpacing(float letterSpacing)75     void setLetterSpacing(float letterSpacing) { mLetterSpacing = letterSpacing; }
76 
getLetterSpacing()77     float getLetterSpacing() const { return mLetterSpacing; }
78 
setWordSpacing(float wordSpacing)79     void setWordSpacing(float wordSpacing) { mWordSpacing = wordSpacing; }
80 
getWordSpacing()81     float getWordSpacing() const { return mWordSpacing; }
82 
setFontFeatureSettings(const std::string & fontFeatureSettings)83     void setFontFeatureSettings(const std::string& fontFeatureSettings) {
84         mFontFeatureSettings = fontFeatureSettings;
85     }
86 
getFontFeatureSettings()87     std::string getFontFeatureSettings() const { return mFontFeatureSettings; }
88 
setMinikinLocaleListId(uint32_t minikinLocaleListId)89     void setMinikinLocaleListId(uint32_t minikinLocaleListId) {
90         mMinikinLocaleListId = minikinLocaleListId;
91     }
92 
getMinikinLocaleListId()93     uint32_t getMinikinLocaleListId() const { return mMinikinLocaleListId; }
94 
setFamilyVariant(minikin::FamilyVariant variant)95     void setFamilyVariant(minikin::FamilyVariant variant) { mFamilyVariant = variant; }
96 
getFamilyVariant()97     minikin::FamilyVariant getFamilyVariant() const { return mFamilyVariant; }
98 
setStartHyphenEdit(uint32_t startHyphen)99     void setStartHyphenEdit(uint32_t startHyphen) {
100         mHyphenEdit = minikin::packHyphenEdit(
101             static_cast<minikin::StartHyphenEdit>(startHyphen),
102             minikin::endHyphenEdit(mHyphenEdit));
103     }
104 
setEndHyphenEdit(uint32_t endHyphen)105     void setEndHyphenEdit(uint32_t endHyphen) {
106         mHyphenEdit = minikin::packHyphenEdit(
107             minikin::startHyphenEdit(mHyphenEdit),
108             static_cast<minikin::EndHyphenEdit>(endHyphen));
109     }
110 
getStartHyphenEdit()111     minikin::StartHyphenEdit getStartHyphenEdit() const {
112         return minikin::startHyphenEdit(mHyphenEdit);
113     }
114 
getEndHyphenEdit()115     minikin::EndHyphenEdit getEndHyphenEdit() const {
116         return minikin::endHyphenEdit(mHyphenEdit);
117     }
118 
setAndroidTypeface(Typeface * typeface)119     void setAndroidTypeface(Typeface* typeface) { mTypeface = typeface; }
120 
getAndroidTypeface()121     const Typeface* getAndroidTypeface() const { return mTypeface; }
122 
123     enum Align {
124         kLeft_Align,
125         kCenter_Align,
126         kRight_Align,
127     };
getTextAlign()128     Align getTextAlign() const { return mAlign; }
setTextAlign(Align align)129     void setTextAlign(Align align) { mAlign = align; }
130 
isStrikeThru()131     bool isStrikeThru() const { return mStrikeThru; }
setStrikeThru(bool st)132     void setStrikeThru(bool st) { mStrikeThru = st; }
133 
isUnderline()134     bool isUnderline() const { return mUnderline; }
setUnderline(bool u)135     void setUnderline(bool u) { mUnderline = u; }
136 
isDevKern()137     bool isDevKern() const { return mDevKern; }
setDevKern(bool d)138     void setDevKern(bool d) { mDevKern = d; }
139 
140     // The Java flags (Paint.java) no longer fit into the native apis directly.
141     // These methods handle converting to and from them and the native representations
142     // in android::Paint.
143 
144     uint32_t getJavaFlags() const;
145     void setJavaFlags(uint32_t);
146 
147     // Helpers that return or apply legacy java flags to SkPaint, ignoring all flags
148     // that are meant for SkFont or Paint (e.g. underline, strikethru)
149     // The only respected flags are : [ antialias, dither, filterBitmap ]
150     static uint32_t GetSkPaintJavaFlags(const SkPaint&);
151     static void SetSkPaintJavaFlags(SkPaint*, uint32_t flags);
152 
153 private:
154     SkFont mFont;
155     sk_sp<SkDrawLooper> mLooper;
156 
157     float mLetterSpacing = 0;
158     float mWordSpacing = 0;
159     std::string mFontFeatureSettings;
160     uint32_t mMinikinLocaleListId;
161     minikin::FamilyVariant mFamilyVariant;
162     uint32_t mHyphenEdit = 0;
163     // The native Typeface object has the same lifetime of the Java Typeface
164     // object. The Java Paint object holds a strong reference to the Java Typeface
165     // object. Thus, following pointer can never be a dangling pointer. Note that
166     // nullptr is valid: it means the default typeface.
167     const Typeface* mTypeface = nullptr;
168     Align mAlign = kLeft_Align;
169     bool mStrikeThru = false;
170     bool mUnderline = false;
171     bool mDevKern = false;
172 };
173 
174 }  // namespace android
175 
176 #endif  // ANDROID_GRAPHICS_PAINT_H_
177