1 /* 2 * Copyright 2023 Google LLC 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 FontToolUtils_DEFINED 9 #define FontToolUtils_DEFINED 10 11 #include "include/core/SkColor.h" 12 #include "include/core/SkFontStyle.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkString.h" 15 #include "include/core/SkTypeface.h" 16 17 class SkBitmap; 18 class SkImage; 19 class SkFont; 20 class SkFontMgr; 21 22 namespace ToolUtils { 23 /** 24 * Returns a font that has a non-empty typeface. This could change, so don't depend on things like 25 * how it looks, font metrics, etc. 26 */ 27 SkFont DefaultPortableFont(); 28 29 sk_sp<SkTypeface> DefaultPortableTypeface(); 30 31 /** 32 * Returns a platform-independent text renderer. 33 */ 34 sk_sp<SkTypeface> CreatePortableTypeface(const char* name, SkFontStyle style); 35 36 /* Return a color emoji typeface with planets to scale if available. */ 37 sk_sp<SkTypeface> PlanetTypeface(); 38 39 enum class EmojiFontFormat { 40 Cbdt, 41 Sbix, 42 ColrV0, 43 Test, 44 Svg 45 }; 46 47 struct EmojiTestSample { 48 sk_sp<SkTypeface> typeface = nullptr; 49 const char* sampleText = ""; 50 }; 51 52 /** Return a color emoji typeface if available. */ 53 EmojiTestSample EmojiSample(); 54 55 /** Return a color emoji typeface of a specific color font format if available. */ 56 EmojiTestSample EmojiSample(EmojiFontFormat format); 57 58 /** Return a string representation of the requeste format. Useful for suffixing test names. */ 59 SkString NameForFontFormat(EmojiFontFormat format); 60 61 /** A simple SkUserTypeface for testing. */ 62 sk_sp<SkTypeface> SampleUserTypeface(); 63 64 SkBitmap CreateStringBitmap(int w, int h, SkColor c, int x, int y, int textSize, const char* str); 65 sk_sp<SkImage> CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, const char* str); 66 67 // This returns the SkFontMgr that has been compiled in and configured (e.g. via CLI flag) 68 sk_sp<SkFontMgr> TestFontMgr(); 69 70 // Must be called before the first call to TestFontMgr to have any effect. 71 void UsePortableFontMgr(); 72 73 // Returns true if this platform is Windows and this binary is being configured to run 74 // with the GDI font manager. 75 bool FontMgrIsGDI(); 76 77 // This returns the default SkTypeface returned by the TestFontMgr(). If there was no default 78 // Typeface, DefaultPortableTypeface() is returned instead. 79 sk_sp<SkTypeface> DefaultTypeface(); 80 81 // Returns a Typeface matching the given criteria as returned by TestFontMgr(). This may be different 82 // on different platforms. 83 sk_sp<SkTypeface> CreateTestTypeface(const char* name, SkFontStyle style); 84 85 // Load the resource with the provided name as a Typeface using TestFontMgr(). 86 sk_sp<SkTypeface> CreateTypefaceFromResource(const char* resource, int ttcIndex = 0); 87 88 // This returns a font using DefaultTypeface() 89 SkFont DefaultFont(); 90 91 } // namespace ToolUtils 92 93 #endif 94