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_MINIKIN_SKIA_H_
18 #define _ANDROID_GRAPHICS_MINIKIN_SKIA_H_
19 
20 #include <SkRefCnt.h>
21 #include <cutils/compiler.h>
22 #include <minikin/MinikinFont.h>
23 
24 class SkPaint;
25 class SkTypeface;
26 
27 namespace android {
28 
29 class ANDROID_API MinikinFontSkia : public minikin::MinikinFont {
30 public:
31     explicit MinikinFontSkia(sk_sp<SkTypeface> typeface, const void* fontData, size_t fontSize,
32                              int ttcIndex, const std::vector<minikin::FontVariation>& axes);
33 
34     float GetHorizontalAdvance(uint32_t glyph_id, const minikin::MinikinPaint& paint,
35                                const minikin::FontFakery& fakery) const override;
36 
37     void GetBounds(minikin::MinikinRect* bounds, uint32_t glyph_id,
38                    const minikin::MinikinPaint& paint,
39                    const minikin::FontFakery& fakery) const override;
40 
41     void GetFontExtent(minikin::MinikinExtent* extent, const minikin::MinikinPaint& paint,
42                        const minikin::FontFakery& fakery) const override;
43 
44     SkTypeface* GetSkTypeface() const;
45     sk_sp<SkTypeface> RefSkTypeface() const;
46 
47     // Access to underlying raw font bytes
48     const void* GetFontData() const;
49     size_t GetFontSize() const;
50     int GetFontIndex() const;
51     const std::vector<minikin::FontVariation>& GetAxes() const;
52     std::shared_ptr<minikin::MinikinFont> createFontWithVariation(
53             const std::vector<minikin::FontVariation>&) const;
54 
55     static uint32_t packPaintFlags(const SkPaint* paint);
56     static void unpackPaintFlags(SkPaint* paint, uint32_t paintFlags);
57 
58     // set typeface and fake bold/italic parameters
59     static void populateSkPaint(SkPaint* paint, const minikin::MinikinFont* font,
60                                 minikin::FontFakery fakery);
61 
62 private:
63     sk_sp<SkTypeface> mTypeface;
64 
65     // A raw pointer to the font data - it should be owned by some other object with
66     // lifetime at least as long as this object.
67     const void* mFontData;
68     size_t mFontSize;
69     int mTtcIndex;
70     std::vector<minikin::FontVariation> mAxes;
71 };
72 
73 }  // namespace android
74 
75 #endif  // _ANDROID_GRAPHICS_MINIKIN_SKIA_H_
76