1/*
2 * Copyright 2007, 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_ENGINE_H
27#define FONT_ENGINE_H
28
29#include <SupportDefs.h>
30
31#include <ft2build.h>
32#include FT_FREETYPE_H
33
34#include <agg_scanline_storage_aa.h>
35#include <agg_scanline_storage_bin.h>
36#include <agg_scanline_u.h>
37#include <agg_scanline_bin.h>
38#include <agg_path_storage_integer.h>
39#include <agg_rasterizer_scanline_aa.h>
40#include <agg_conv_curve.h>
41#include <agg_trans_affine.h>
42
43#include "agg_scanline_storage_subpix.h"
44#include "agg_scanline_u_subpix.h"
45#include "agg_scanline_u_subpix_avrg_filtering.h"
46
47#include "GlobalSubpixelSettings.h"
48
49
50enum glyph_rendering {
51	glyph_ren_native_mono,
52	glyph_ren_native_gray8,
53	glyph_ren_outline,
54	glyph_ren_subpix
55};
56
57
58enum glyph_data_type {
59	glyph_data_invalid	= 0,
60	glyph_data_mono		= 1,
61	glyph_data_gray8	= 2,
62	glyph_data_outline	= 3,
63	glyph_data_subpix   = 4
64};
65
66
67class FontEngine {
68 public:
69	typedef agg::serialized_scanlines_adaptor_subpix<uint8>	SubpixAdapter;
70	typedef agg::serialized_scanlines_adaptor_aa<uint8>		Gray8Adapter;
71	typedef agg::serialized_scanlines_adaptor_bin			MonoAdapter;
72	typedef agg::scanline_storage_aa8						ScanlineStorageAA;
73	typedef agg::scanline_storage_subpix8					ScanlineStorageSubpix;
74	typedef agg::scanline_storage_bin						ScanlineStorageBin;
75	typedef agg::serialized_integer_path_adaptor<int32, 6>	PathAdapter;
76
77								FontEngine();
78	virtual						~FontEngine();
79
80			bool				Init(const char* fontFilePath,
81									unsigned face_index, double size,
82									FT_Encoding char_map,
83									glyph_rendering ren_type,
84									bool hinting,
85									const void* fontFileBuffer = NULL,
86									const long fontFileBufferSize = 0);
87
88			int					LastError() const
89									{ return fLastError; }
90			unsigned			CountFaces() const;
91			bool				Hinting() const
92									{ return fHinting; }
93
94
95			uint32				GlyphIndexForGlyphCode(uint32 glyphCode) const;
96			bool				PrepareGlyph(uint32 glyphIndex);
97
98			uint32				DataSize() const
99									{ return fDataSize; }
100			glyph_data_type		DataType() const
101									{ return fDataType; }
102			const agg::rect_i&	Bounds() const
103									{ return fBounds; }
104			double				AdvanceX() const
105									{ return fAdvanceX; }
106			double				AdvanceY() const
107									{ return fAdvanceY; }
108			double				PreciseAdvanceX() const
109									{ return fPreciseAdvanceX; }
110			double				PreciseAdvanceY() const
111									{ return fPreciseAdvanceY; }
112			double				InsetLeft() const
113									{ return fInsetLeft; }
114			double				InsetRight() const
115									{ return fInsetRight; }
116
117			void				WriteGlyphTo(uint8* data) const;
118
119
120			bool				GetKerning(uint32 first, uint32 second,
121									double* x, double* y);
122
123 private:
124			// disallowed stuff:
125								FontEngine(const FontEngine&);
126			const FontEngine&	operator=(const FontEngine&);
127
128			int					fLastError;
129			bool				fLibraryInitialized;
130			FT_Library			fLibrary;	// handle to library
131			FT_Face				fFace;	  // FreeType font face handle
132
133			glyph_rendering		fGlyphRendering;
134			bool				fHinting;
135
136			// members needed to generate individual glyphs according
137			// to glyph rendering type
138			uint32				fDataSize;
139			glyph_data_type		fDataType;
140			agg::rect_i			fBounds;
141			double				fAdvanceX;
142			double				fAdvanceY;
143			double				fPreciseAdvanceX;
144			double				fPreciseAdvanceY;
145			double				fInsetLeft;
146			double				fInsetRight;
147
148			// these members are for caching memory allocations
149			// when rendering glyphs
150	typedef agg::path_storage_integer<int32, 6>		PathStorageType;
151	typedef agg::conv_curve<PathStorageType>		CurveConverterType;
152
153			PathStorageType		fPath;
154			CurveConverterType	fCurves;
155			agg::scanline_u8	fScanlineAA;
156			agg::scanline_bin	fScanlineBin;
157#ifdef AVERAGE_BASED_SUBPIXEL_FILTERING
158			agg::scanline_u8_subpix_avrg_filtering fScanlineSubpix;
159#else
160			agg::scanline_u8_subpix fScanlineSubpix;
161#endif
162
163			ScanlineStorageAA	fScanlineStorageAA;
164			ScanlineStorageBin	fScanlineStorageBin;
165			ScanlineStorageSubpix fScanlineStorageSubpix;
166};
167
168
169#endif // FONT_ENGINE_H
170