1 /* 2 * Copyright 2007-2009, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Maxim Shemanarev <mcseemagg@yahoo.com> 7 * Stephan Aßmus <superstippi@gmx.de> 8 * Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk> 9 */ 10 11 //---------------------------------------------------------------------------- 12 // Anti-Grain Geometry - Version 2.4 13 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) 14 // 15 // Permission to copy, use, modify, sell and distribute this software 16 // is granted provided this copyright notice appears in all copies. 17 // This software is provided "as is" without express or implied 18 // warranty, and with no claim as to its suitability for any purpose. 19 // 20 //---------------------------------------------------------------------------- 21 // Contact: mcseem@antigrain.com 22 // mcseemagg@yahoo.com 23 // http://www.antigrain.com 24 //---------------------------------------------------------------------------- 25 26 #ifndef FONT_CACHE_ENTRY_H 27 #define FONT_CACHE_ENTRY_H 28 29 30 #include <Locker.h> 31 32 #include <agg_conv_curve.h> 33 #include <agg_conv_contour.h> 34 #include <agg_conv_transform.h> 35 36 #include "ServerFont.h" 37 #include "FontEngine.h" 38 #include "MultiLocker.h" 39 #include "Referenceable.h" 40 #include "Transformable.h" 41 42 43 struct GlyphCache { 44 GlyphCache(uint32 glyphIndex, uint32 dataSize, glyph_data_type dataType, 45 const agg::rect_i& bounds, float advanceX, float advanceY, 46 float insetLeft, float insetRight) 47 : 48 glyph_index(glyphIndex), 49 data((uint8*)malloc(dataSize)), 50 data_size(dataSize), 51 data_type(dataType), 52 bounds(bounds), 53 advance_x(advanceX), 54 advance_y(advanceY), 55 inset_left(insetLeft), 56 inset_right(insetRight), 57 hash_link(NULL) 58 { 59 } 60 61 ~GlyphCache() 62 { 63 free(data); 64 } 65 66 uint32 glyph_index; 67 uint8* data; 68 uint32 data_size; 69 glyph_data_type data_type; 70 agg::rect_i bounds; 71 float advance_x; 72 float advance_y; 73 float inset_left; 74 float inset_right; 75 76 GlyphCache* hash_link; 77 }; 78 79 class FontCache; 80 81 class FontCacheEntry : public MultiLocker, public BReferenceable { 82 public: 83 typedef FontEngine::PathAdapter GlyphPathAdapter; 84 typedef FontEngine::Gray8Adapter GlyphGray8Adapter; 85 typedef GlyphGray8Adapter::embedded_scanline GlyphGray8Scanline; 86 typedef FontEngine::MonoAdapter GlyphMonoAdapter; 87 typedef GlyphMonoAdapter::embedded_scanline GlyphMonoScanline; 88 typedef FontEngine::SubpixAdapter SubpixAdapter; 89 typedef agg::conv_curve<GlyphPathAdapter> CurveConverter; 90 typedef agg::conv_contour<CurveConverter> ContourConverter; 91 92 typedef agg::conv_transform<CurveConverter, Transformable> 93 TransformedOutline; 94 95 typedef agg::conv_transform<ContourConverter, Transformable> 96 TransformedContourOutline; 97 98 99 FontCacheEntry(); 100 virtual ~FontCacheEntry(); 101 102 bool Init(const ServerFont& font); 103 104 bool HasGlyphs(const char* utf8String, 105 ssize_t glyphCount) const; 106 107 const GlyphCache* CachedGlyph(uint32 glyphCode); 108 const GlyphCache* CreateGlyph(uint32 glyphCode, 109 FontCacheEntry* fallbackEntry = NULL); 110 111 void InitAdaptors(const GlyphCache* glyph, 112 double x, double y, 113 GlyphMonoAdapter& monoAdapter, 114 GlyphGray8Adapter& gray8Adapter, 115 GlyphPathAdapter& pathAdapter, 116 double scale = 1.0); 117 118 bool GetKerning(uint32 glyphCode1, 119 uint32 glyphCode2, double* x, double* y); 120 121 static void GenerateSignature(char* signature, 122 size_t signatureSize, 123 const ServerFont& font); 124 125 // private to FontCache class: 126 void UpdateUsage(); 127 bigtime_t LastUsed() const 128 { return fLastUsedTime; } 129 uint64 UsedCount() const 130 { return fUseCounter; } 131 132 private: 133 FontCacheEntry(const FontCacheEntry&); 134 const FontCacheEntry& operator=(const FontCacheEntry&); 135 136 static glyph_rendering _RenderTypeFor(const ServerFont& font); 137 138 class GlyphCachePool; 139 140 GlyphCachePool* fGlyphCache; 141 FontEngine fEngine; 142 143 static BLocker sUsageUpdateLock; 144 bigtime_t fLastUsedTime; 145 uint64 fUseCounter; 146 }; 147 148 #endif // FONT_CACHE_ENTRY_H 149 150