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 <AutoDeleter.h>
31#include <Locker.h>
32
33#include <agg_conv_curve.h>
34#include <agg_conv_contour.h>
35#include <agg_conv_transform.h>
36
37#include "ServerFont.h"
38#include "FontEngine.h"
39#include "MultiLocker.h"
40#include "Referenceable.h"
41#include "Transformable.h"
42
43
44struct GlyphCache {
45	GlyphCache(uint32 glyphIndex, uint32 dataSize, glyph_data_type dataType,
46			const agg::rect_i& bounds, float advanceX, float advanceY,
47			float preciseAdvanceX, float preciseAdvanceY,
48			float insetLeft, float insetRight)
49		:
50		glyph_index(glyphIndex),
51		data((uint8*)malloc(dataSize)),
52		data_size(dataSize),
53		data_type(dataType),
54		bounds(bounds),
55		advance_x(advanceX),
56		advance_y(advanceY),
57		precise_advance_x(preciseAdvanceX),
58		precise_advance_y(preciseAdvanceY),
59		inset_left(insetLeft),
60		inset_right(insetRight),
61		hash_link(NULL)
62	{
63	}
64
65	~GlyphCache()
66	{
67		free(data);
68	}
69
70	uint32			glyph_index;
71	uint8*			data;
72	uint32			data_size;
73	glyph_data_type	data_type;
74	agg::rect_i		bounds;
75	float			advance_x;
76	float			advance_y;
77	float			precise_advance_x;
78	float			precise_advance_y;
79	float			inset_left;
80	float			inset_right;
81
82	GlyphCache*		hash_link;
83};
84
85class FontCache;
86
87class FontCacheEntry : public MultiLocker, public BReferenceable {
88 public:
89	typedef FontEngine::PathAdapter					GlyphPathAdapter;
90	typedef FontEngine::Gray8Adapter				GlyphGray8Adapter;
91	typedef GlyphGray8Adapter::embedded_scanline	GlyphGray8Scanline;
92	typedef FontEngine::MonoAdapter					GlyphMonoAdapter;
93	typedef GlyphMonoAdapter::embedded_scanline		GlyphMonoScanline;
94	typedef FontEngine::SubpixAdapter				SubpixAdapter;
95	typedef agg::conv_curve<GlyphPathAdapter>		CurveConverter;
96	typedef agg::conv_contour<CurveConverter>		ContourConverter;
97
98	typedef agg::conv_transform<CurveConverter, Transformable>
99													TransformedOutline;
100
101	typedef agg::conv_transform<ContourConverter, Transformable>
102													TransformedContourOutline;
103
104
105								FontCacheEntry();
106	virtual						~FontCacheEntry();
107
108			bool				Init(const ServerFont& font, bool forceVector);
109
110			bool				HasGlyphs(const char* utf8String,
111									ssize_t glyphCount) const;
112
113			const GlyphCache*	CachedGlyph(uint32 glyphCode);
114			const GlyphCache*	CreateGlyph(uint32 glyphCode,
115									FontCacheEntry* fallbackEntry = NULL);
116			bool				CanCreateGlyph(uint32 glyphCode);
117
118			void				InitAdaptors(const GlyphCache* glyph,
119									double x, double y,
120									GlyphMonoAdapter& monoAdapter,
121									GlyphGray8Adapter& gray8Adapter,
122									GlyphPathAdapter& pathAdapter,
123									double scale = 1.0);
124
125			bool				GetKerning(uint32 glyphCode1,
126									uint32 glyphCode2, double* x, double* y);
127
128	static	void				GenerateSignature(char* signature,
129									size_t signatureSize,
130									const ServerFont& font, bool forceVector);
131
132	// private to FontCache class:
133			void				UpdateUsage();
134			bigtime_t			LastUsed() const
135									{ return fLastUsedTime; }
136			uint64				UsedCount() const
137									{ return fUseCounter; }
138
139 private:
140								FontCacheEntry(const FontCacheEntry&);
141			const FontCacheEntry& operator=(const FontCacheEntry&);
142
143	static	glyph_rendering		_RenderTypeFor(const ServerFont& font,
144									bool forceVector);
145
146			class GlyphCachePool;
147
148			ObjectDeleter<GlyphCachePool>
149								fGlyphCache;
150			FontEngine			fEngine;
151
152	static	BLocker				sUsageUpdateLock;
153			bigtime_t			fLastUsedTime;
154			uint64				fUseCounter;
155};
156
157#endif // FONT_CACHE_ENTRY_H
158
159