1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_STARMATH_INC_RECT_HXX
21 #define INCLUDED_STARMATH_INC_RECT_HXX
22 
23 #include <rtl/ustring.hxx>
24 #include <sal/log.hxx>
25 #include <tools/gen.hxx>
26 #include <vcl/outdev.hxx>
27 
28 class SmFormat;
29 
30 
SmFromTo(long nFrom,long nTo,double fRelDist)31 inline long SmFromTo(long nFrom, long nTo, double fRelDist)
32 {
33     return nFrom + static_cast<long>(fRelDist * (nTo - nFrom));
34 }
35 
36 
37 // SmRect
38 // ... (to be done)
39 // This Implementation assumes that the x-axis points to the right and the
40 // y-axis to the bottom.
41 // Note: however, italic spaces can be negative!
42 
43 
44 // possible positions and alignments for the 'AlignTo' function
45 enum class RectPos
46 {
47     Left, // align the current object to the left of the argument
48     Right,
49     Top,
50     Bottom,
51     Attribute
52 };
53 
54 enum class RectHorAlign
55 {
56     Left,
57     Center,
58     Right
59 };
60 
61 enum class RectVerAlign
62 {
63     Top,
64     Mid,
65     Bottom,
66     Baseline,
67     CenterY,
68     AttributeHi,
69     AttributeMid,
70     AttributeLo
71 };
72 
73 // different methods of copying baselines and mid's in 'ExtendBy' function
74 enum class RectCopyMBL
75 {
76     This,   // keep baseline of current object even if it has none
77     Arg,    // as above but for the argument
78     None,   // result will have no baseline
79     Xor     // if current object has a baseline keep it else copy
80             //   the arguments baseline (even if it has none)
81 };
82 
83 
84 class SmRect
85 {
86     Point   aTopLeft;
87     Size    aSize;
88     long    nBaseline,
89             nAlignT,
90             nAlignM,
91             nAlignB,
92             nGlyphTop,
93             nGlyphBottom,
94             nItalicLeftSpace,
95             nItalicRightSpace,
96             nLoAttrFence,
97             nHiAttrFence;
98     sal_uInt16  nBorderWidth;
99     bool    bHasBaseline,
100             bHasAlignInfo;
101 
102     inline  void CopyMBL(const SmRect& rRect);
103             void CopyAlignInfo(const SmRect& rRect);
104 
105             void Union(const SmRect &rRect);
106 
107 public:
108             SmRect();
109             SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
110                    const OUString &rText, sal_uInt16 nBorderWidth);
111             SmRect(long nWidth, long nHeight);
112 
113 
GetBorderWidth() const114             sal_uInt16  GetBorderWidth() const  { return nBorderWidth; }
115 
116             void SetItalicSpaces(long nLeftSpace, long nRightSpace);
117 
SetWidth(sal_uLong nWidth)118             void SetWidth(sal_uLong nWidth)     { aSize.setWidth(nWidth); }
119 
120             void SetLeft(long nLeft);
121             void SetRight(long nRight);
122             void SetBottom(long nBottom);
123             void SetTop(long nTop);
124 
GetTopLeft() const125             const Point & GetTopLeft() const { return aTopLeft; }
126 
GetTop() const127             long GetTop()     const { return GetTopLeft().Y(); }
GetLeft() const128             long GetLeft()    const { return GetTopLeft().X(); }
GetBottom() const129             long GetBottom()  const { return GetTop() + GetHeight() - 1; }
GetRight() const130             long GetRight()   const { return GetLeft() + GetWidth() - 1; }
GetCenterY() const131             long GetCenterY() const { return (GetTop() + GetBottom()) / 2; }
GetWidth() const132             long GetWidth()   const { return GetSize().Width(); }
GetHeight() const133             long GetHeight()  const { return GetSize().Height(); }
134 
GetItalicLeftSpace() const135             long GetItalicLeftSpace()  const { return nItalicLeftSpace; }
GetItalicRightSpace() const136             long GetItalicRightSpace() const { return nItalicRightSpace; }
137 
GetHiAttrFence() const138             long GetHiAttrFence() const     { return nHiAttrFence; }
GetLoAttrFence() const139             long GetLoAttrFence() const     { return nLoAttrFence; }
140 
GetItalicLeft() const141             long GetItalicLeft() const      { return GetLeft() - GetItalicLeftSpace(); }
GetItalicCenterX() const142             long GetItalicCenterX() const   { return (GetItalicLeft() + GetItalicRight()) / 2; }
GetItalicRight() const143             long GetItalicRight() const     { return GetRight() + GetItalicRightSpace(); }
GetItalicWidth() const144             long GetItalicWidth() const     { return GetWidth() + GetItalicLeftSpace() + GetItalicRightSpace(); }
145 
HasBaseline() const146             bool HasBaseline() const        { return bHasBaseline; }
147     inline  long GetBaseline() const;
GetBaselineOffset() const148             long GetBaselineOffset() const  { return GetBaseline() - GetTop(); }
149 
GetAlignT() const150             long GetAlignT() const  { return nAlignT; }
GetAlignM() const151             long GetAlignM() const  { return nAlignM; }
GetAlignB() const152             long GetAlignB() const  { return nAlignB; }
153 
GetSize() const154             const Size & GetSize() const    { return aSize; }
155 
GetItalicSize() const156             Size  GetItalicSize() const
157             {   return Size(GetItalicWidth(), GetHeight()); }
158 
159             void Move  (const Point &rPosition);
MoveTo(const Point & rPosition)160             void MoveTo(const Point &rPosition) { Move(rPosition - GetTopLeft()); }
161 
IsEmpty() const162             bool IsEmpty() const
163             {
164                 return GetWidth() == 0  ||  GetHeight() == 0;
165             }
166 
HasAlignInfo() const167             bool HasAlignInfo() const { return bHasAlignInfo; }
168 
169             Point AlignTo(const SmRect &rRect, RectPos ePos,
170                                 RectHorAlign eHor, RectVerAlign eVer) const;
171 
172             SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode);
173             void     ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
174                               long nNewAlignM);
175             SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
176                       bool bKeepVerAlignParams);
177 
178             long    OrientedDist(const Point &rPoint) const;
179             bool    IsInsideRect(const Point &rPoint) const;
180             bool    IsInsideItalicRect(const Point &rPoint) const;
181 
182     inline  tools::Rectangle   AsRectangle() const;
183             SmRect      AsGlyphRect() const;
184 };
185 
186 
SetItalicSpaces(long nLeftSpace,long nRightSpace)187 inline void SmRect::SetItalicSpaces(long nLeftSpace, long nRightSpace)
188     // set extra spacing to the left and right for (italic)
189     // letters/text
190 {
191     nItalicLeftSpace  = nLeftSpace;
192     nItalicRightSpace = nRightSpace;
193 }
194 
195 
CopyMBL(const SmRect & rRect)196 inline void SmRect::CopyMBL(const SmRect &rRect)
197     // copy AlignM baseline and value of 'rRect'
198 {
199     nBaseline    = rRect.nBaseline;
200     bHasBaseline = rRect.bHasBaseline;
201     nAlignM      = rRect.nAlignM;
202 }
203 
204 
GetBaseline() const205 inline long SmRect::GetBaseline() const
206 {
207     SAL_WARN_IF( !HasBaseline(), "starmath", "Baseline does not exist" );
208     return nBaseline;
209 }
210 
211 
AsRectangle() const212 inline tools::Rectangle SmRect::AsRectangle() const
213 {
214     return tools::Rectangle(Point(GetItalicLeft(), GetTop()), GetItalicSize());
215 }
216 
217 #endif
218 
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
220