xref: /haiku/headers/private/interface/WidthBuffer.h (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2  * Copyright (c) 2003-2004 OpenBeOS
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  * File:		WidthBuffer.cpp
23  * Author:		Stefano Ceccherini (burton666@libero.it)
24  * Description: _BWidthBuffer_ stores charachters widths in a hash table, to be able
25  *				to retrieve them without passing through the app server.
26  *				Used by BTextView and OpenTracker.
27  */
28 #ifndef __WIDTHBUFFER_H
29 #define __WIDTHBUFFER_H
30 
31 #include "TextViewSupportBuffer.h"
32 
33 class BFont; // forward declaration
34 
35 // TODO: enable this as soon as we are sure opentracker works
36 // with our libraries, since using a BFont here (as Dano does) is much better,
37 // as fonts can be classified also by spacing mode and other attributes.
38 #define USE_DANO_WIDTHBUFFER 0
39 
40 struct _width_table_ {
41 #if USE_DANO_WIDTHBUFFER
42 	BFont font;				// corresponding font
43 #else
44 	int32 fontCode;			// font code
45 	float fontSize;			// font size
46 #endif
47 	int32 hashCount;		// number of hashed items
48 	int32 tableCount;		// size of table
49 	void *widths;			// width table
50 };
51 
52 
53 class _BTextGapBuffer_;
54 class _BWidthBuffer_ : public _BTextViewSupportBuffer_<_width_table_> {
55 public:
56 	_BWidthBuffer_();
57 	virtual ~_BWidthBuffer_();
58 
59 	float StringWidth(const char *inText, int32 fromOffset, int32 length,
60 		const BFont *inStyle);
61 	float StringWidth(_BTextGapBuffer_ &gapBuffer, int32 fromOffset, int32 length,
62 		const BFont *inStyle);
63 
64 private:
65 	bool FindTable(const BFont *font, int32 *outIndex);
66 	int32 InsertTable(const BFont *font);
67 
68 	bool GetEscapement(uint32 value, int32 index, float *escapement);
69 	float HashEscapements(const char *chars, int32 numChars, int32 numBytes,
70 		int32 tableIndex, const BFont *font);
71 
72 	static uint32 Hash(uint32);
73 };
74 
75 #endif // __WIDTHBUFFER_H
76