xref: /AOO41X/main/starmath/inc/rect.hxx (revision f6a9d5ca17e19b2857f1a47d10ddf4e3f0a53446)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef RECT_HXX
25 #define RECT_HXX
26 
27 #include <new>
28 
29 
30 #include <tools/gen.hxx>
31 #include <vcl/outdev.hxx>
32 #include <vcl/metric.hxx>
33 #include <tools/debug.hxx>
34 
35 #include "format.hxx"
36 
37 
38 sal_Bool SmGetGlyphBoundRect(const OutputDevice &rDev,
39                          const XubString &rText, Rectangle &rRect);
40 
41 sal_Bool SmIsMathAlpha(const XubString &rText);
42 
43 
SmFromTo(long nFrom,long nTo,double fRelDist)44 inline long SmFromTo(long nFrom, long nTo, double fRelDist)
45 {
46     return nFrom + (long) (fRelDist * (nTo - nFrom));
47 }
48 
49 
50 ////////////////////////////////////////
51 // SmRect
52 // ... (to be done)
53 // This Implementation assumes that the x-axis points to the right and the
54 // y-axis to the bottom.
55 // Note: however, italic spaces can be negative!
56 //
57 
58 // possible flags for the 'Draw' function below (just for debugging)
59 #define SM_RECT_CORE    0x0001
60 #define SM_RECT_ITALIC  0x0002
61 #define SM_RECT_LINES   0x0004
62 #define SM_RECT_MID     0x0008
63 
64 // possible positions and alignments for the 'AlignTo' function
65 enum RectPos
66     // (RP_LEFT : align the current object to the left of the argument, ...)
67 {   RP_LEFT, RP_RIGHT,
68     RP_TOP, RP_BOTTOM,
69     RP_ATTRIBUT
70 };
71 enum RectHorAlign
72 {   RHA_LEFT, RHA_CENTER, RHA_RIGHT
73 };
74 enum RectVerAlign
75 {   RVA_TOP, RVA_MID, RVA_BOTTOM, RVA_BASELINE, RVA_CENTERY,
76     RVA_ATTRIBUT_HI, RVA_ATTRIBUT_MID, RVA_ATTRIBUT_LO
77 };
78 
79 // different methods of copying baselines and mid's in 'ExtendBy' function
80 enum RectCopyMBL
81 {   RCP_THIS,   // keep baseline of current object even if it has none
82     RCP_ARG,    // as above but for the argument
83     RCP_NONE,   // result will have no baseline
84     RCP_XOR     // if current object has a baseline keep it else copy
85                 //   the arguments baseline (even if it has none)
86 };
87 
88 
89 class SmRect
90 {
91     Point   aTopLeft;
92     Size    aSize;
93     long    nBaseline,
94             nAlignT,
95             nAlignM,
96             nAlignB,
97             nGlyphTop,
98             nGlyphBottom,
99             nItalicLeftSpace,
100             nItalicRightSpace,
101             nLoAttrFence,
102             nHiAttrFence;
103     sal_uInt16  nBorderWidth;
104     sal_Bool    bHasBaseline,
105             bHasAlignInfo;
106 
107 protected:
108             void BuildRect (const OutputDevice &rDev, const SmFormat *pFormat,
109                             const XubString &rText, sal_uInt16 nBorderWidth);
110             void Init(const OutputDevice &rDev, const SmFormat *pFormat,
111                       const XubString &rText, sal_uInt16 nBorderWidth);
112 
ClearBaseline()113             void ClearBaseline()    { bHasBaseline = sal_False; };
114     inline  void CopyMBL(const SmRect& rRect);
115             void CopyAlignInfo(const SmRect& rRect);
116 
117             SmRect & Union(const SmRect &rRect);
118 
119 public:
120             SmRect();
121             SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
122                    const XubString &rText, long nBorderWidth);
123             SmRect(long nWidth, long nHeight);
124             SmRect(const SmRect &rRect);
125 
126 
GetBorderWidth() const127             sal_uInt16  GetBorderWidth() const  { return nBorderWidth; }
128 
129             void SetItalicSpaces(long nLeftSpace, long nRightSpace);
130 
SetWidth(sal_uLong nWidth)131             void SetWidth(sal_uLong nWidth)     { aSize.Width()  = nWidth; }
SetHeight(sal_uLong nHeight)132             void SetHeight(sal_uLong nHeight)   { aSize.Height() = nHeight; }
133 
134             void SetLeft(long nLeft);
135             void SetRight(long nRight);
136             void SetBottom(long nBottom);
137             void SetTop(long nTop);
138 
GetTopLeft() const139             const Point & GetTopLeft() const { return aTopLeft; }
140 
GetTop() const141             long GetTop()     const { return GetTopLeft().Y(); }
GetLeft() const142             long GetLeft()    const { return GetTopLeft().X(); }
GetBottom() const143             long GetBottom()  const { return GetTop() + GetHeight() - 1; }
GetRight() const144             long GetRight()   const { return GetLeft() + GetWidth() - 1; }
GetCenterX() const145             long GetCenterX() const { return (GetLeft() + GetRight()) / 2L; }
GetCenterY() const146             long GetCenterY() const { return (GetTop() + GetBottom()) / 2L; }
GetWidth() const147             long GetWidth()   const { return GetSize().Width(); }
GetHeight() const148             long GetHeight()  const { return GetSize().Height(); }
149 
GetItalicLeftSpace() const150             long GetItalicLeftSpace()  const { return nItalicLeftSpace; }
GetItalicRightSpace() const151             long GetItalicRightSpace() const { return nItalicRightSpace; }
152 
SetHiAttrFence(long nVal)153             void SetHiAttrFence(long nVal)  { nHiAttrFence = nVal; }
SetLoAttrFence(long nVal)154             void SetLoAttrFence(long nVal)  { nLoAttrFence = nVal; }
GetHiAttrFence() const155             long GetHiAttrFence() const     { return nHiAttrFence; }
GetLoAttrFence() const156             long GetLoAttrFence() const     { return nLoAttrFence; }
157 
GetItalicLeft() const158             long GetItalicLeft() const      { return GetLeft() - GetItalicLeftSpace(); }
GetItalicCenterX() const159             long GetItalicCenterX() const   { return (GetItalicLeft() + GetItalicRight()) / 2; }
GetItalicRight() const160             long GetItalicRight() const     { return GetRight() + GetItalicRightSpace(); }
GetItalicWidth() const161             long GetItalicWidth() const     { return GetWidth() + GetItalicLeftSpace() + GetItalicRightSpace(); }
162 
HasBaseline() const163             sal_Bool HasBaseline() const        { return bHasBaseline; }
164     inline  long GetBaseline() const;
GetBaselineOffset() const165             long GetBaselineOffset() const  { return GetBaseline() - GetTop(); }
166 
SetAlignTop(long nVal)167             void SetAlignTop(long nVal) { nAlignT = nVal; }
168 
GetAlignT() const169             long GetAlignT() const  { return nAlignT; }
GetAlignM() const170             long GetAlignM() const  { return nAlignM; }
GetAlignB() const171             long GetAlignB() const  { return nAlignB; }
172 
SetAlignT(long nVal)173             void SetAlignT(long nVal) { nAlignT = nVal; }
174 
GetCenter() const175             const Point  GetCenter() const
176             {   return Point(GetCenterX(), GetCenterY()); }
177 
GetSize() const178             const Size & GetSize() const    { return aSize; }
179 
GetItalicSize() const180             const Size  GetItalicSize() const
181             {   return Size(GetItalicWidth(), GetHeight()); }
182 
183             void Move  (const Point &rPosition);
MoveTo(const Point & rPosition)184             void MoveTo(const Point &rPosition) { Move(rPosition - GetTopLeft()); }
185 
IsEmpty() const186             sal_Bool IsEmpty() const
187             {
188                 return GetWidth() == 0  ||  GetHeight() == 0;
189             }
190 
HasAlignInfo() const191             sal_Bool HasAlignInfo() const { return bHasAlignInfo; }
192 
193             const Point AlignTo(const SmRect &rRect, RectPos ePos,
194                                 RectHorAlign eHor, RectVerAlign eVer) const;
195 
196             SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode);
197             SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
198                               long nNewAlignM);
199             SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
200                       sal_Bool bKeepVerAlignParams);
201 
202             long    OrientedDist(const Point &rPoint) const;
203             sal_Bool    IsInsideRect(const Point &rPoint) const;
204             sal_Bool    IsInsideItalicRect(const Point &rPoint) const;
205 
206     inline  SmRect & operator = (const SmRect &rRect);
207 
208     inline  Rectangle   AsRectangle() const;
209             SmRect      AsGlyphRect() const;
210 
211 #ifdef SM_RECT_DEBUG
212             void        Draw(OutputDevice &rDev, const Point &rPosition, int nFlags) const;
213 #endif
214 };
215 
216 
SetItalicSpaces(long nLeftSpace,long nRightSpace)217 inline void SmRect::SetItalicSpaces(long nLeftSpace, long nRightSpace)
218     // set extra spacing to the left and right for (italic)
219     // letters/text
220 {
221     nItalicLeftSpace  = nLeftSpace;
222     nItalicRightSpace = nRightSpace;
223 }
224 
225 
CopyMBL(const SmRect & rRect)226 inline void SmRect::CopyMBL(const SmRect &rRect)
227     // copy AlignM baseline and value of 'rRect'
228 {
229     nBaseline    = rRect.nBaseline;
230     bHasBaseline = rRect.bHasBaseline;
231     nAlignM      = rRect.nAlignM;
232 }
233 
234 
GetBaseline() const235 inline long SmRect::GetBaseline() const
236 {
237     DBG_ASSERT(HasBaseline(), "Sm: Baseline nicht vorhanden");
238     return nBaseline;
239 }
240 
241 
operator =(const SmRect & rRect)242 inline SmRect & SmRect::operator = (const SmRect &rRect)
243 {
244     new (this) SmRect(rRect);   // placement new
245     return *this;
246 }
247 
248 
AsRectangle() const249 inline Rectangle SmRect::AsRectangle() const
250 {
251     return Rectangle(Point(GetItalicLeft(), GetTop()), GetItalicSize());
252 }
253 
254 
255 
256 #endif
257