1/* 2 * Copyright (C) 2012 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_HWUI_FONT_H 18#define ANDROID_HWUI_FONT_H 19 20#include <vector> 21 22#include <utils/KeyedVector.h> 23 24#include <SkScalar.h> 25#include <SkPaint.h> 26#include <SkPathMeasure.h> 27#include <SkTypeface.h> 28 29#include "FontUtil.h" 30#include "../Rect.h" 31#include "../Matrix.h" 32 33class SkGlyphCache; 34 35namespace android { 36namespace uirenderer { 37 38/////////////////////////////////////////////////////////////////////////////// 39// Font 40/////////////////////////////////////////////////////////////////////////////// 41 42struct CachedGlyphInfo; 43class CacheTexture; 44class FontRenderer; 45 46/** 47 * Represents a font, defined by a Skia font id and a font size. A font is used 48 * to generate glyphs and cache them in the FontState. 49 */ 50class Font { 51public: 52 enum Style { 53 kFakeBold = 1 54 }; 55 56 struct FontDescription { 57 FontDescription(const SkPaint* paint, const SkMatrix& matrix); 58 59 static int compare(const FontDescription& lhs, const FontDescription& rhs); 60 61 hash_t hash() const; 62 63 bool operator==(const FontDescription& other) const { 64 return compare(*this, other) == 0; 65 } 66 67 bool operator!=(const FontDescription& other) const { 68 return compare(*this, other) != 0; 69 } 70 71 SkFontID mFontId; 72 float mFontSize; 73 int mFlags; 74 float mItalicStyle; 75 float mScaleX; 76 uint8_t mStyle; 77 float mStrokeWidth; 78 bool mAntiAliasing; 79 uint8_t mHinting; 80 SkMatrix mLookupTransform; 81 SkMatrix mInverseLookupTransform; 82 }; 83 84 ~Font(); 85 86 void render(const SkPaint* paint, const glyph_t* glyphs, 87 int numGlyphs, int x, int y, const float* positions); 88 89 void render(const SkPaint* paint, const glyph_t* glyphs, 90 int numGlyphs, const SkPath* path, float hOffset, float vOffset); 91 92 const Font::FontDescription& getDescription() const { 93 return mDescription; 94 } 95 96 /** 97 * Creates a new font associated with the specified font state. 98 */ 99 static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix); 100 101private: 102 friend class FontRenderer; 103 104 Font(FontRenderer* state, const Font::FontDescription& desc); 105 106 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*, 107 uint32_t, uint32_t, Rect*, const float*); 108 109 enum RenderMode { 110 FRAMEBUFFER, 111 BITMAP, 112 MEASURE, 113 }; 114 115 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs); 116 117 void render(const SkPaint* paint, const glyph_t* glyphs, 118 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, 119 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions); 120 121 void measure(const SkPaint* paint, const glyph_t* glyphs, 122 int numGlyphs, Rect *bounds, const float* positions); 123 124 void invalidateTextureCache(CacheTexture* cacheTexture = nullptr); 125 126 CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching); 127 void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph, 128 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching); 129 130 void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y, 131 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, 132 Rect* bounds, const float* pos); 133 void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, 134 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, 135 Rect* bounds, const float* pos); 136 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y, 137 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, 138 Rect* bounds, const float* pos); 139 void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, 140 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, 141 Rect* bounds, const float* pos); 142 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset, 143 SkPathMeasure& measure, SkPoint* position, SkVector* tangent); 144 145 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit, 146 bool precaching = false); 147 148 FontRenderer* mState; 149 FontDescription mDescription; 150 151 // Cache of glyphs 152 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs; 153 154 bool mIdentityTransform; 155}; 156 157inline int strictly_order_type(const Font::FontDescription& lhs, 158 const Font::FontDescription& rhs) { 159 return Font::FontDescription::compare(lhs, rhs) < 0; 160} 161 162inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) { 163 return Font::FontDescription::compare(lhs, rhs); 164} 165 166inline hash_t hash_type(const Font::FontDescription& entry) { 167 return entry.hash(); 168} 169 170}; // namespace uirenderer 171}; // namespace android 172 173#endif // ANDROID_HWUI_FONT_H 174