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