xref: /haiku/headers/private/interface/WidthBuffer.h (revision 45bd7bb3db9d9e4dcb02b89a3e7c2bf382c0a88c)
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: WidthBuffer 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 
32 #include <Locker.h>
33 #include <TextView.h>
34 
35 #include "TextViewSupportBuffer.h"
36 
37 
38 class BFont;
39 
40 
41 // TODO: enable this as soon as we are sure opentracker works
42 // with our libraries, since using a BFont here (as Dano does) is much better,
43 // as fonts can be classified also by spacing mode and other attributes.
44 #define USE_DANO_WIDTHBUFFER 0
45 
46 namespace BPrivate {
47 
48 class TextGapBuffer;
49 
50 struct _width_table_ {
51 #if USE_DANO_WIDTHBUFFER
52 	BFont font;				// corresponding font
53 #else
54 	int32 fontCode;			// font code
55 	float fontSize;			// font size
56 #endif
57 	int32 hashCount;		// number of hashed items
58 	int32 tableCount;		// size of table
59 	void *widths;			// width table
60 };
61 
62 class WidthBuffer : public _BTextViewSupportBuffer_<_width_table_> {
63 public:
64 	WidthBuffer();
65 	virtual ~WidthBuffer();
66 
67 	float StringWidth(const char *inText, int32 fromOffset, int32 length,
68 		const BFont *inStyle);
69 	float StringWidth(TextGapBuffer &gapBuffer, int32 fromOffset,
70 		int32 length, const BFont *inStyle);
71 
72 private:
73 	bool FindTable(const BFont *font, int32 *outIndex);
74 	int32 InsertTable(const BFont *font);
75 
76 	bool GetEscapement(uint32 value, int32 index, float *escapement);
77 	float HashEscapements(const char *chars, int32 numChars, int32 numBytes,
78 		int32 tableIndex, const BFont *font);
79 
80 	static uint32 Hash(uint32);
81 
82 private:
83 	BLocker	fLock;
84 };
85 
86 extern WidthBuffer* gWidthBuffer;
87 
88 } // namespace BPrivate
89 
90 using BPrivate::WidthBuffer;
91 
92 #if __GNUC__ < 3
93 //! NetPositive binary compatibility support
94 
95 class _BWidthBuffer_ : public _BTextViewSupportBuffer_<BPrivate::_width_table_> {
96 	_BWidthBuffer_();
97 	virtual ~_BWidthBuffer_();
98 };
99 
100 extern
101 _BWidthBuffer_* gCompatibilityWidthBuffer;
102 
103 #endif // __GNUC__ < 3
104 
105 #endif // __WIDTHBUFFER_H
106