xref: /haiku/headers/private/interface/WidthBuffer.h (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
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 // TODO: enable this as soon as we are sure opentracker works
34 // with our libraries, since using a BFont here (as Dano does) is much better,
35 // as fonts can be classified also by spacing mode and other attributes.
36 #define USE_DANO_WIDTHBUFFER 0
37 
38 struct _width_table_ {
39 #if USE_DANO_WIDTHBUFFER
40 	BFont font;				// corresponding font
41 #else
42 	int32 fontCode;			// font code
43 	float fontSize;			// font size
44 #endif
45 	int32 hashCount;		// number of hashed items
46 	int32 tableCount;		// size of table
47 	void *widths;			// width table
48 };
49 
50 
51 class _BTextGapBuffer_;
52 class _BWidthBuffer_ : public _BTextViewSupportBuffer_<_width_table_> {
53 public:
54 	_BWidthBuffer_();
55 	virtual ~_BWidthBuffer_();
56 
57 	float StringWidth(const char *inText, int32 fromOffset, int32 length,
58 		const BFont *inStyle);
59 	float StringWidth(_BTextGapBuffer_ &gapBuffer, int32 fromOffset, int32 length,
60 		const BFont *inStyle);
61 
62 private:
63 	bool FindTable(const BFont *font, int32 *outIndex);
64 	int32 InsertTable(const BFont *font);
65 
66 	bool GetEscapement(uint32 value, int32 index, float *escapement);
67 	float HashEscapements(const char *chars, int32 numChars, int32 numBytes,
68 		int32 tableIndex, const BFont *font);
69 
70 	static uint32 Hash(uint32);
71 };
72 
73 #endif // __WIDTHBUFFER_H
74