1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef GFX_GLYPHEXTENTS_H 7 #define GFX_GLYPHEXTENTS_H 8 9 #include "gfxFont.h" 10 #include "gfxRect.h" 11 #include "nsTHashtable.h" 12 #include "nsHashKeys.h" 13 #include "nsTArray.h" 14 #include "mozilla/MemoryReporting.h" 15 16 class gfxContext; 17 18 namespace mozilla { 19 namespace gfx { 20 class DrawTarget; 21 } // namespace gfx 22 } // namespace mozilla 23 24 /** 25 * This stores glyph bounds information for a particular gfxFont, at 26 * a particular appunits-per-dev-pixel ratio (because the compressed glyph 27 * width array is stored in appunits). 28 * 29 * We store a hashtable from glyph IDs to float bounding rects. For the 30 * common case where the glyph has no horizontal left bearing, and no 31 * y overflow above the font ascent or below the font descent, and tight 32 * bounding boxes are not required, we avoid storing the glyph ID in the 33 * hashtable and instead consult an array of 16-bit glyph XMost values (in 34 * appunits). This array always has an entry for the font's space glyph --- the 35 * width is assumed to be zero. 36 */ 37 class gfxGlyphExtents { 38 typedef mozilla::gfx::DrawTarget DrawTarget; 39 40 public: gfxGlyphExtents(int32_t aAppUnitsPerDevUnit)41 explicit gfxGlyphExtents(int32_t aAppUnitsPerDevUnit) 42 : mAppUnitsPerDevUnit(aAppUnitsPerDevUnit) { 43 MOZ_COUNT_CTOR(gfxGlyphExtents); 44 } 45 ~gfxGlyphExtents(); 46 47 enum { INVALID_WIDTH = 0xFFFF }; 48 NotifyGlyphsChanged()49 void NotifyGlyphsChanged() { mTightGlyphExtents.Clear(); } 50 51 // returns INVALID_WIDTH => not a contained glyph 52 // Otherwise the glyph has no before-bearing or vertical bearings, 53 // and the result is its width measured from the baseline origin, in 54 // appunits. GetContainedGlyphWidthAppUnits(uint32_t aGlyphID)55 uint16_t GetContainedGlyphWidthAppUnits(uint32_t aGlyphID) const { 56 return mContainedGlyphWidths.Get(aGlyphID); 57 } 58 IsGlyphKnown(uint32_t aGlyphID)59 bool IsGlyphKnown(uint32_t aGlyphID) const { 60 return mContainedGlyphWidths.Get(aGlyphID) != INVALID_WIDTH || 61 mTightGlyphExtents.GetEntry(aGlyphID) != nullptr; 62 } 63 IsGlyphKnownWithTightExtents(uint32_t aGlyphID)64 bool IsGlyphKnownWithTightExtents(uint32_t aGlyphID) const { 65 return mTightGlyphExtents.GetEntry(aGlyphID) != nullptr; 66 } 67 68 // Get glyph extents; a rectangle relative to the left baseline origin 69 // Returns true on success. Can fail on OOM or when aContext is null 70 // and extents were not (successfully) prefetched. 71 bool GetTightGlyphExtentsAppUnits(gfxFont* aFont, DrawTarget* aDrawTarget, 72 uint32_t aGlyphID, gfxRect* aExtents); 73 SetContainedGlyphWidthAppUnits(uint32_t aGlyphID,uint16_t aWidth)74 void SetContainedGlyphWidthAppUnits(uint32_t aGlyphID, uint16_t aWidth) { 75 mContainedGlyphWidths.Set(aGlyphID, aWidth); 76 } 77 void SetTightGlyphExtents(uint32_t aGlyphID, const gfxRect& aExtentsAppUnits); 78 GetAppUnitsPerDevUnit()79 int32_t GetAppUnitsPerDevUnit() { return mAppUnitsPerDevUnit; } 80 81 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 82 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 83 84 private: 85 class HashEntry : public nsUint32HashKey { 86 public: 87 // When constructing a new entry in the hashtable, we'll leave this 88 // blank. The caller of Put() will fill this in. HashEntry(KeyTypePointer aPtr)89 explicit HashEntry(KeyTypePointer aPtr) 90 : nsUint32HashKey(aPtr), x(0.0), y(0.0), width(0.0), height(0.0) {} HashEntry(HashEntry && aOther)91 HashEntry(HashEntry&& aOther) 92 : nsUint32HashKey(std::move(aOther)), 93 x(aOther.x), 94 y(aOther.y), 95 width(aOther.width), 96 height(aOther.height) {} 97 98 float x, y, width, height; 99 }; 100 101 enum { 102 BLOCK_SIZE_BITS = 7, 103 BLOCK_SIZE = 1 << BLOCK_SIZE_BITS 104 }; // 128-glyph blocks 105 106 class GlyphWidths { 107 public: 108 void Set(uint32_t aIndex, uint16_t aValue); Get(uint32_t aIndex)109 uint16_t Get(uint32_t aIndex) const { 110 uint32_t block = aIndex >> BLOCK_SIZE_BITS; 111 if (block >= mBlocks.Length()) return INVALID_WIDTH; 112 uintptr_t bits = mBlocks[block]; 113 if (!bits) return INVALID_WIDTH; 114 uint32_t indexInBlock = aIndex & (BLOCK_SIZE - 1); 115 if (bits & 0x1) { 116 if (GetGlyphOffset(bits) != indexInBlock) return INVALID_WIDTH; 117 return GetWidth(bits); 118 } 119 uint16_t* widths = reinterpret_cast<uint16_t*>(bits); 120 return widths[indexInBlock]; 121 } 122 123 uint32_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 124 125 ~GlyphWidths(); 126 127 private: GetGlyphOffset(uintptr_t aBits)128 static uint32_t GetGlyphOffset(uintptr_t aBits) { 129 NS_ASSERTION(aBits & 0x1, "This is really a pointer..."); 130 return (aBits >> 1) & ((1 << BLOCK_SIZE_BITS) - 1); 131 } GetWidth(uintptr_t aBits)132 static uint32_t GetWidth(uintptr_t aBits) { 133 NS_ASSERTION(aBits & 0x1, "This is really a pointer..."); 134 return aBits >> (1 + BLOCK_SIZE_BITS); 135 } MakeSingle(uint32_t aGlyphOffset,uint16_t aWidth)136 static uintptr_t MakeSingle(uint32_t aGlyphOffset, uint16_t aWidth) { 137 return (aWidth << (1 + BLOCK_SIZE_BITS)) + (aGlyphOffset << 1) + 1; 138 } 139 140 nsTArray<uintptr_t> mBlocks; 141 }; 142 143 GlyphWidths mContainedGlyphWidths; 144 nsTHashtable<HashEntry> mTightGlyphExtents; 145 int32_t mAppUnitsPerDevUnit; 146 147 private: 148 // not implemented: 149 gfxGlyphExtents(const gfxGlyphExtents& aOther) = delete; 150 gfxGlyphExtents& operator=(const gfxGlyphExtents& aOther) = delete; 151 }; 152 153 #endif 154