xref: /aoo41x/main/starmath/inc/rect.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef RECT_HXX
29 #define RECT_HXX
30 
31 #include <new>
32 
33 
34 #include <tools/gen.hxx>
35 #include <vcl/outdev.hxx>
36 #include <vcl/metric.hxx>
37 #include <tools/debug.hxx>
38 
39 #include "format.hxx"
40 
41 
42 sal_Bool SmGetGlyphBoundRect(const OutputDevice &rDev,
43 						 const XubString &rText, Rectangle &rRect);
44 
45 sal_Bool SmIsMathAlpha(const XubString &rText);
46 
47 
48 inline long SmFromTo(long nFrom, long nTo, double fRelDist)
49 {
50 	return nFrom + (long) (fRelDist * (nTo - nFrom));
51 }
52 
53 
54 ////////////////////////////////////////
55 // SmRect
56 // ... (to be done)
57 // This Implementation assumes that the x-axis points to the right and the
58 // y-axis to the bottom.
59 // Note: however, italic spaces can be negative!
60 //
61 
62 // possible flags for the 'Draw' function below (just for debugging)
63 #define SM_RECT_CORE	0x0001
64 #define SM_RECT_ITALIC	0x0002
65 #define SM_RECT_LINES	0x0004
66 #define SM_RECT_MID		0x0008
67 
68 // possible positions and alignments for the 'AlignTo' function
69 enum RectPos
70 	// (RP_LEFT : align the current object to the left of the argument, ...)
71 {	RP_LEFT, RP_RIGHT,
72 	RP_TOP, RP_BOTTOM,
73 	RP_ATTRIBUT
74 };
75 enum RectHorAlign
76 {	RHA_LEFT, RHA_CENTER, RHA_RIGHT
77 };
78 enum RectVerAlign
79 {	RVA_TOP, RVA_MID, RVA_BOTTOM, RVA_BASELINE, RVA_CENTERY,
80 	RVA_ATTRIBUT_HI, RVA_ATTRIBUT_MID, RVA_ATTRIBUT_LO
81 };
82 
83 // different methods of copying baselines and mid's in 'ExtendBy' function
84 enum RectCopyMBL
85 {	RCP_THIS,	// keep baseline of current object even if it has none
86 	RCP_ARG,	// as above but for the argument
87 	RCP_NONE,	// result will have no baseline
88 	RCP_XOR		// if current object has a baseline keep it else copy
89 				//	 the arguments baseline (even if it has none)
90 };
91 
92 
93 class SmRect
94 {
95 	Point	aTopLeft;
96 	Size	aSize;
97 	long	nBaseline,
98 			nAlignT,
99 			nAlignM,
100 			nAlignB,
101 			nGlyphTop,
102 			nGlyphBottom,
103 			nItalicLeftSpace,
104 			nItalicRightSpace,
105 			nLoAttrFence,
106 			nHiAttrFence;
107     sal_uInt16  nBorderWidth;
108 	sal_Bool	bHasBaseline,
109 			bHasAlignInfo;
110 
111 protected:
112 			void BuildRect (const OutputDevice &rDev, const SmFormat *pFormat,
113                             const XubString &rText, sal_uInt16 nBorderWidth);
114 			void Init(const OutputDevice &rDev, const SmFormat *pFormat,
115                       const XubString &rText, sal_uInt16 nBorderWidth);
116 
117 			void ClearBaseline()	{ bHasBaseline = sal_False; };
118 	inline	void CopyMBL(const SmRect& rRect);
119 			void CopyAlignInfo(const SmRect& rRect);
120 
121 			SmRect & Union(const SmRect &rRect);
122 
123 public:
124 			SmRect();
125 			SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
126                    const XubString &rText, long nBorderWidth);
127 			SmRect(long nWidth, long nHeight);
128 			SmRect(const SmRect &rRect);
129 
130 
131             sal_uInt16  GetBorderWidth() const  { return nBorderWidth; }
132 
133             void SetItalicSpaces(long nLeftSpace, long nRightSpace);
134 
135 			void SetWidth(sal_uLong nWidth)		{ aSize.Width()  = nWidth; }
136 			void SetHeight(sal_uLong nHeight)	{ aSize.Height() = nHeight; }
137 
138 			void SetLeft(long nLeft);
139 			void SetRight(long nRight);
140 			void SetBottom(long nBottom);
141 			void SetTop(long nTop);
142 
143 			const Point	& GetTopLeft() const { return aTopLeft; }
144 
145 			long GetTop()	  const	{ return GetTopLeft().Y(); }
146 			long GetLeft()	  const	{ return GetTopLeft().X(); }
147 			long GetBottom()  const	{ return GetTop() + GetHeight() - 1; }
148 			long GetRight()   const	{ return GetLeft() + GetWidth() - 1; }
149 			long GetCenterX() const	{ return (GetLeft() + GetRight()) / 2L; }
150 			long GetCenterY() const	{ return (GetTop() + GetBottom()) / 2L; }
151 			long GetWidth()   const	{ return GetSize().Width(); }
152 			long GetHeight()  const	{ return GetSize().Height(); }
153 
154 			long GetItalicLeftSpace()  const { return nItalicLeftSpace; }
155 			long GetItalicRightSpace() const { return nItalicRightSpace; }
156 
157 			void SetHiAttrFence(long nVal)	{ nHiAttrFence = nVal; }
158 			void SetLoAttrFence(long nVal)	{ nLoAttrFence = nVal; }
159 			long GetHiAttrFence() const 	{ return nHiAttrFence; }
160 			long GetLoAttrFence() const 	{ return nLoAttrFence; }
161 
162 			long GetItalicLeft() const		{ return GetLeft() - GetItalicLeftSpace(); }
163 			long GetItalicCenterX() const	{ return (GetItalicLeft() + GetItalicRight()) / 2; }
164 			long GetItalicRight() const		{ return GetRight() + GetItalicRightSpace(); }
165 			long GetItalicWidth() const		{ return GetWidth() + GetItalicLeftSpace() + GetItalicRightSpace(); }
166 
167 			sal_Bool HasBaseline() const		{ return bHasBaseline; }
168 	inline	long GetBaseline() const;
169 			long GetBaselineOffset() const	{ return GetBaseline() - GetTop(); }
170 
171 			void SetAlignTop(long nVal) { nAlignT = nVal; }
172 
173 			long GetAlignT() const	{ return nAlignT; }
174 			long GetAlignM() const	{ return nAlignM; }
175 			long GetAlignB() const	{ return nAlignB; }
176 
177 			void SetAlignT(long nVal) { nAlignT = nVal; }
178 
179 			const Point	 GetCenter() const
180 			{	return Point(GetCenterX(), GetCenterY()); }
181 
182 			const Size & GetSize() const	{ return aSize; }
183 
184 			const Size	GetItalicSize() const
185 			{	return Size(GetItalicWidth(), GetHeight()); }
186 
187 			void Move  (const Point &rPosition);
188 			void MoveTo(const Point &rPosition) { Move(rPosition - GetTopLeft()); }
189 
190 			sal_Bool IsEmpty() const
191 			{
192 				return GetWidth() == 0	||	GetHeight() == 0;
193 			}
194 
195 			sal_Bool HasAlignInfo() const { return bHasAlignInfo; }
196 
197 			const Point AlignTo(const SmRect &rRect, RectPos ePos,
198 								RectHorAlign eHor, RectVerAlign eVer) const;
199 
200 			SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode);
201 			SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
202 							  long nNewAlignM);
203 			SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
204 					  sal_Bool bKeepVerAlignParams);
205 
206 			long	OrientedDist(const Point &rPoint) const;
207 			sal_Bool	IsInsideRect(const Point &rPoint) const;
208 			sal_Bool	IsInsideItalicRect(const Point &rPoint) const;
209 
210 	inline	SmRect & operator = (const SmRect &rRect);
211 
212 	inline	Rectangle	AsRectangle() const;
213 			SmRect		AsGlyphRect() const;
214 
215 #ifdef SM_RECT_DEBUG
216 			void 		Draw(OutputDevice &rDev, const Point &rPosition, int nFlags) const;
217 #endif
218 };
219 
220 
221 inline void SmRect::SetItalicSpaces(long nLeftSpace, long nRightSpace)
222 	// set extra spacing to the left and right for (italic)
223 	// letters/text
224 {
225 	nItalicLeftSpace  = nLeftSpace;
226 	nItalicRightSpace = nRightSpace;
227 }
228 
229 
230 inline void SmRect::CopyMBL(const SmRect &rRect)
231 	// copy AlignM baseline and value of 'rRect'
232 {
233 	nBaseline	 = rRect.nBaseline;
234 	bHasBaseline = rRect.bHasBaseline;
235 	nAlignM		 = rRect.nAlignM;
236 }
237 
238 
239 inline long SmRect::GetBaseline() const
240 {
241 	DBG_ASSERT(HasBaseline(), "Sm: Baseline nicht vorhanden");
242 	return nBaseline;
243 }
244 
245 
246 inline SmRect & SmRect::operator = (const SmRect &rRect)
247 {
248 	new (this) SmRect(rRect);	// placement new
249 	return *this;
250 }
251 
252 
253 inline Rectangle SmRect::AsRectangle() const
254 {
255 	return Rectangle(Point(GetItalicLeft(), GetTop()), GetItalicSize());
256 }
257 
258 
259 
260 #endif
261