Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 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 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_GRAPHICS_PAINT_H_ |
| 18 | #define ANDROID_GRAPHICS_PAINT_H_ |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 19 | |
Seigo Nonaka | 318ca04 | 2017-08-01 16:36:18 -0700 | [diff] [blame] | 20 | #include "Typeface.h" |
| 21 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
| 23 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 24 | #include <SkDrawLooper.h> |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 25 | #include <SkFont.h> |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 26 | #include <SkPaint.h> |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 27 | #include <string> |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 28 | |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 29 | #include <minikin/FontFamily.h> |
Seigo Nonaka | 0ca492f | 2018-05-25 14:52:22 -0700 | [diff] [blame] | 30 | #include <minikin/FamilyVariant.h> |
Seigo Nonaka | fb1b479 | 2019-03-08 14:05:08 -0800 | [diff] [blame] | 31 | #include <minikin/Hyphenator.h> |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 32 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 33 | namespace android { |
| 34 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 35 | class ANDROID_API Paint : public SkPaint { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 36 | public: |
Roozbeh Pournader | ca8a04a | 2017-06-06 18:30:29 -0700 | [diff] [blame] | 37 | // Default values for underlined and strikethrough text, |
| 38 | // as defined by Skia in SkTextFormatParams.h. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | constexpr static float kStdStrikeThru_Offset = (-6.0f / 21.0f); |
| 40 | constexpr static float kStdUnderline_Offset = (1.0f / 9.0f); |
Roozbeh Pournader | ca8a04a | 2017-06-06 18:30:29 -0700 | [diff] [blame] | 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 | |
Roozbeh Pournader | 1378a9d | 2017-07-13 12:45:20 -0700 | [diff] [blame] | 46 | constexpr static float kStdStrikeThru_Thickness = kStdUnderline_Thickness; |
| 47 | constexpr static float kStdStrikeThru_Top = |
| 48 | kStdStrikeThru_Offset - 0.5f * kStdStrikeThru_Thickness; |
| 49 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 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); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 57 | friend bool operator!=(const Paint& a, const Paint& b) { return !(a == b); } |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 58 | |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 59 | SkFont& getSkFont() { return mFont; } |
| 60 | const SkFont& getSkFont() const { return mFont; } |
| 61 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 62 | SkDrawLooper* getLooper() const { return mLooper.get(); } |
| 63 | void setLooper(sk_sp<SkDrawLooper> looper) { mLooper = std::move(looper); } |
| 64 | |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 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 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 71 | bool nothingToDraw() const { return !mLooper && SkPaint::nothingToDraw(); } |
| 72 | |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 73 | // End method shadowing |
| 74 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | void setLetterSpacing(float letterSpacing) { mLetterSpacing = letterSpacing; } |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 76 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 77 | float getLetterSpacing() const { return mLetterSpacing; } |
Behdad Esfahbod | fa80f74 | 2014-07-17 19:10:39 -0400 | [diff] [blame] | 78 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | void setWordSpacing(float wordSpacing) { mWordSpacing = wordSpacing; } |
Seigo Nonaka | 219e2c79 | 2016-11-15 19:01:45 +0900 | [diff] [blame] | 80 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 81 | float getWordSpacing() const { return mWordSpacing; } |
Seigo Nonaka | 219e2c79 | 2016-11-15 19:01:45 +0900 | [diff] [blame] | 82 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 83 | void setFontFeatureSettings(const std::string& fontFeatureSettings) { |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 84 | mFontFeatureSettings = fontFeatureSettings; |
| 85 | } |
| 86 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | std::string getFontFeatureSettings() const { return mFontFeatureSettings; } |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 88 | |
Seigo Nonaka | 20866c1 | 2017-10-26 16:02:01 -0700 | [diff] [blame] | 89 | void setMinikinLocaleListId(uint32_t minikinLocaleListId) { |
| 90 | mMinikinLocaleListId = minikinLocaleListId; |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Seigo Nonaka | 20866c1 | 2017-10-26 16:02:01 -0700 | [diff] [blame] | 93 | uint32_t getMinikinLocaleListId() const { return mMinikinLocaleListId; } |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 94 | |
Seigo Nonaka | 0ca492f | 2018-05-25 14:52:22 -0700 | [diff] [blame] | 95 | void setFamilyVariant(minikin::FamilyVariant variant) { mFamilyVariant = variant; } |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 96 | |
Seigo Nonaka | 0ca492f | 2018-05-25 14:52:22 -0700 | [diff] [blame] | 97 | minikin::FamilyVariant getFamilyVariant() const { return mFamilyVariant; } |
Derek Sollenberger | 6f03023 | 2014-08-07 08:10:39 -0400 | [diff] [blame] | 98 | |
Seigo Nonaka | fb1b479 | 2019-03-08 14:05:08 -0800 | [diff] [blame] | 99 | void setStartHyphenEdit(uint32_t startHyphen) { |
| 100 | mHyphenEdit = minikin::packHyphenEdit( |
| 101 | static_cast<minikin::StartHyphenEdit>(startHyphen), |
| 102 | minikin::endHyphenEdit(mHyphenEdit)); |
| 103 | } |
Raph Levien | 210a189 | 2015-03-09 14:42:14 -0700 | [diff] [blame] | 104 | |
Seigo Nonaka | fb1b479 | 2019-03-08 14:05:08 -0800 | [diff] [blame] | 105 | void setEndHyphenEdit(uint32_t endHyphen) { |
| 106 | mHyphenEdit = minikin::packHyphenEdit( |
| 107 | minikin::startHyphenEdit(mHyphenEdit), |
| 108 | static_cast<minikin::EndHyphenEdit>(endHyphen)); |
| 109 | } |
| 110 | |
| 111 | minikin::StartHyphenEdit getStartHyphenEdit() const { |
| 112 | return minikin::startHyphenEdit(mHyphenEdit); |
| 113 | } |
| 114 | |
| 115 | minikin::EndHyphenEdit getEndHyphenEdit() const { |
| 116 | return minikin::endHyphenEdit(mHyphenEdit); |
| 117 | } |
Raph Levien | 210a189 | 2015-03-09 14:42:14 -0700 | [diff] [blame] | 118 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 119 | void setAndroidTypeface(Typeface* typeface) { mTypeface = typeface; } |
Seigo Nonaka | 318ca04 | 2017-08-01 16:36:18 -0700 | [diff] [blame] | 120 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 121 | const Typeface* getAndroidTypeface() const { return mTypeface; } |
Seigo Nonaka | 318ca04 | 2017-08-01 16:36:18 -0700 | [diff] [blame] | 122 | |
Mike Reed | 16c142b | 2018-10-25 14:19:45 -0400 | [diff] [blame] | 123 | enum Align { |
| 124 | kLeft_Align, |
| 125 | kCenter_Align, |
| 126 | kRight_Align, |
| 127 | }; |
| 128 | Align getTextAlign() const { return mAlign; } |
| 129 | void setTextAlign(Align align) { mAlign = align; } |
| 130 | |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 131 | bool isStrikeThru() const { return mStrikeThru; } |
| 132 | void setStrikeThru(bool st) { mStrikeThru = st; } |
| 133 | |
| 134 | bool isUnderline() const { return mUnderline; } |
| 135 | void setUnderline(bool u) { mUnderline = u; } |
| 136 | |
| 137 | bool isDevKern() const { return mDevKern; } |
| 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 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 153 | private: |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 154 | SkFont mFont; |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 155 | sk_sp<SkDrawLooper> mLooper; |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 156 | |
Raph Levien | 210a189 | 2015-03-09 14:42:14 -0700 | [diff] [blame] | 157 | float mLetterSpacing = 0; |
Seigo Nonaka | 219e2c79 | 2016-11-15 19:01:45 +0900 | [diff] [blame] | 158 | float mWordSpacing = 0; |
Behdad Esfahbod | 805f6eb | 2014-07-29 18:43:03 -0400 | [diff] [blame] | 159 | std::string mFontFeatureSettings; |
Seigo Nonaka | 20866c1 | 2017-10-26 16:02:01 -0700 | [diff] [blame] | 160 | uint32_t mMinikinLocaleListId; |
Seigo Nonaka | 0ca492f | 2018-05-25 14:52:22 -0700 | [diff] [blame] | 161 | minikin::FamilyVariant mFamilyVariant; |
Raph Levien | 210a189 | 2015-03-09 14:42:14 -0700 | [diff] [blame] | 162 | uint32_t mHyphenEdit = 0; |
Seigo Nonaka | 20866c1 | 2017-10-26 16:02:01 -0700 | [diff] [blame] | 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. |
Seigo Nonaka | 318ca04 | 2017-08-01 16:36:18 -0700 | [diff] [blame] | 167 | const Typeface* mTypeface = nullptr; |
Mike Reed | 16c142b | 2018-10-25 14:19:45 -0400 | [diff] [blame] | 168 | Align mAlign = kLeft_Align; |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 169 | bool mStrikeThru = false; |
| 170 | bool mUnderline = false; |
| 171 | bool mDevKern = false; |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace android |
| 175 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 176 | #endif // ANDROID_GRAPHICS_PAINT_H_ |