xref: /aosp_15_r20/frameworks/minikin/tests/util/FreeTypeMinikinFontForTest.h (revision 834a2baab5fdfc28e9a428ee87c7ea8f6a06a53d)
1 /*
2  * Copyright (C) 2018 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 MINIKIN_TEST_FREE_TYPE_MINIKIN_FONT_FOR_TEST_H
18 #define MINIKIN_TEST_FREE_TYPE_MINIKIN_FONT_FOR_TEST_H
19 
20 #include <ft2build.h>
21 
22 #include <string>
23 
24 #include "minikin/Buffer.h"
25 #include "minikin/Font.h"
26 #include "minikin/MinikinFont.h"
27 #include "minikin/MinikinFontFactory.h"
28 #include FT_FREETYPE_H
29 
30 #include "minikin/Macros.h"
31 
32 namespace minikin {
33 
34 class FreeTypeMinikinFontForTest : public MinikinFont {
35 public:
36     FreeTypeMinikinFontForTest(const std::string& font_path, int index,
37                                const VariationSettings& axes);
FreeTypeMinikinFontForTest(const std::string & font_path,int index)38     FreeTypeMinikinFontForTest(const std::string& font_path, int index)
39             : FreeTypeMinikinFontForTest(font_path, index, VariationSettings()) {}
FreeTypeMinikinFontForTest(const std::string & font_path)40     FreeTypeMinikinFontForTest(const std::string& font_path)
41             : FreeTypeMinikinFontForTest(font_path, 0, VariationSettings()) {}
42     virtual ~FreeTypeMinikinFontForTest();
43 
44     // MinikinFont overrides.
45     float GetHorizontalAdvance(uint32_t glyph_id, const MinikinPaint& paint,
46                                const FontFakery& fakery) const override;
47     void GetBounds(MinikinRect* bounds, uint32_t glyph_id, const MinikinPaint& paint,
48                    const FontFakery& fakery) const override;
49     void GetFontExtent(MinikinExtent* extent, const MinikinPaint& paint,
50                        const FontFakery& fakery) const override;
51 
GetFontPath()52     const std::string& GetFontPath() const override { return mFontPath; }
GetFontData()53     const void* GetFontData() const { return mFontData; }
GetFontSize()54     size_t GetFontSize() const { return mFontSize; }
GetFontIndex()55     int GetFontIndex() const { return mFontIndex; }
GetAxes()56     const VariationSettings& GetAxes() const { return mAxes; }
57     std::shared_ptr<MinikinFont> createFontWithVariation(const VariationSettings&) const;
58 
59 private:
60     const std::string mFontPath;
61     const int mFontIndex;
62     void* mFontData;
63     size_t mFontSize;
64     VariationSettings mAxes;
65 
66     FT_Library mFtLibrary;
67     FT_Face mFtFace;
68 
69     MINIKIN_PREVENT_COPY_AND_ASSIGN(FreeTypeMinikinFontForTest);
70 };
71 
72 class FreeTypeMinikinFontForTestFactory : MinikinFontFactory {
73 private:
74     FreeTypeMinikinFontForTestFactory();
75 
76 public:
77     static void init();
78 
79     void write(BufferWriter* writer, const MinikinFont* typeface) const override;
80 
81     std::shared_ptr<MinikinFont> create(BufferReader reader) const override;
82 
83     void skip(BufferReader* reader) const override;
84 };
85 
86 }  // namespace minikin
87 
88 #endif  // MINIKIN_TEST_FREE_TYPE_MINIKIN_FONT_FOR_TEST_H
89