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 #pragma once
21
22 #include <rtl/ustring.hxx>
23 #include <sal/log.hxx>
24 #include <tools/gen.hxx>
25 #include <vcl/outdev.hxx>
26
27 class SmFormat;
28
29
SmFromTo(tools::Long nFrom,tools::Long nTo,double fRelDist)30 inline tools::Long SmFromTo(tools::Long nFrom, tools::Long nTo, double fRelDist)
31 {
32 return nFrom + static_cast<tools::Long>(fRelDist * (nTo - nFrom));
33 }
34
35
36 // SmRect
37 // ... (to be done)
38 // This Implementation assumes that the x-axis points to the right and the
39 // y-axis to the bottom.
40 // Note: however, italic spaces can be negative!
41
42
43 // possible positions and alignments for the 'AlignTo' function
44 enum class RectPos
45 {
46 Left, // align the current object to the left of the argument
47 Right,
48 Top,
49 Bottom,
50 Attribute
51 };
52
53 enum class RectHorAlign
54 {
55 Left,
56 Center,
57 Right
58 };
59
60 enum class RectVerAlign
61 {
62 Top,
63 Mid,
64 Bottom,
65 Baseline,
66 CenterY,
67 AttributeHi,
68 AttributeMid,
69 AttributeLo
70 };
71
72 // different methods of copying baselines and mid's in 'ExtendBy' function
73 enum class RectCopyMBL
74 {
75 This, // keep baseline of current object even if it has none
76 Arg, // as above but for the argument
77 None, // result will have no baseline
78 Xor // if current object has a baseline keep it else copy
79 // the arguments baseline (even if it has none)
80 };
81
82
83 class SmRect
84 {
85 Point aTopLeft;
86 Size aSize;
87 tools::Long nBaseline,
88 nAlignT,
89 nAlignM,
90 nAlignB,
91 nGlyphTop,
92 nGlyphBottom,
93 nItalicLeftSpace,
94 nItalicRightSpace,
95 nLoAttrFence,
96 nHiAttrFence;
97 sal_uInt16 nBorderWidth;
98 bool bHasBaseline,
99 bHasAlignInfo;
100
101 inline void CopyMBL(const SmRect& rRect);
102 void CopyAlignInfo(const SmRect& rRect);
103
104 void Union(const SmRect &rRect);
105
106 public:
107 SmRect();
108 SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
109 const OUString &rText, sal_uInt16 nBorderWidth);
110 SmRect(tools::Long nWidth, tools::Long nHeight);
111
112
GetBorderWidth() const113 sal_uInt16 GetBorderWidth() const { return nBorderWidth; }
114
115 void SetItalicSpaces(tools::Long nLeftSpace, tools::Long nRightSpace);
116
SetWidth(sal_uLong nWidth)117 void SetWidth(sal_uLong nWidth) { aSize.setWidth(nWidth); }
118
119 void SetLeft(tools::Long nLeft);
120 void SetRight(tools::Long nRight);
121 void SetBottom(tools::Long nBottom);
122 void SetTop(tools::Long nTop);
123
GetTopLeft() const124 const Point & GetTopLeft() const { return aTopLeft; }
125
GetTop() const126 tools::Long GetTop() const { return GetTopLeft().Y(); }
GetLeft() const127 tools::Long GetLeft() const { return GetTopLeft().X(); }
GetBottom() const128 tools::Long GetBottom() const { return GetTop() + GetHeight() - 1; }
GetRight() const129 tools::Long GetRight() const { return GetLeft() + GetWidth() - 1; }
GetCenterY() const130 tools::Long GetCenterY() const { return (GetTop() + GetBottom()) / 2; }
GetWidth() const131 tools::Long GetWidth() const { return GetSize().Width(); }
GetHeight() const132 tools::Long GetHeight() const { return GetSize().Height(); }
133
GetItalicLeftSpace() const134 tools::Long GetItalicLeftSpace() const { return nItalicLeftSpace; }
GetItalicRightSpace() const135 tools::Long GetItalicRightSpace() const { return nItalicRightSpace; }
136
GetHiAttrFence() const137 tools::Long GetHiAttrFence() const { return nHiAttrFence; }
GetLoAttrFence() const138 tools::Long GetLoAttrFence() const { return nLoAttrFence; }
139
GetItalicLeft() const140 tools::Long GetItalicLeft() const { return GetLeft() - GetItalicLeftSpace(); }
GetItalicCenterX() const141 tools::Long GetItalicCenterX() const { return (GetItalicLeft() + GetItalicRight()) / 2; }
GetItalicRight() const142 tools::Long GetItalicRight() const { return GetRight() + GetItalicRightSpace(); }
GetItalicWidth() const143 tools::Long GetItalicWidth() const { return GetWidth() + GetItalicLeftSpace() + GetItalicRightSpace(); }
144
HasBaseline() const145 bool HasBaseline() const { return bHasBaseline; }
146 inline tools::Long GetBaseline() const;
GetBaselineOffset() const147 tools::Long GetBaselineOffset() const { return GetBaseline() - GetTop(); }
148
GetAlignT() const149 tools::Long GetAlignT() const { return nAlignT; }
GetAlignM() const150 tools::Long GetAlignM() const { return nAlignM; }
GetAlignB() const151 tools::Long GetAlignB() const { return nAlignB; }
152
GetSize() const153 const Size & GetSize() const { return aSize; }
154
GetItalicSize() const155 Size GetItalicSize() const
156 { return Size(GetItalicWidth(), GetHeight()); }
157
158 void Move (const Point &rPosition);
MoveTo(const Point & rPosition)159 void MoveTo(const Point &rPosition) { Move(rPosition - GetTopLeft()); }
160
IsEmpty() const161 bool IsEmpty() const
162 {
163 return GetWidth() == 0 || GetHeight() == 0;
164 }
165
HasAlignInfo() const166 bool HasAlignInfo() const { return bHasAlignInfo; }
167
168 Point AlignTo(const SmRect &rRect, RectPos ePos,
169 RectHorAlign eHor, RectVerAlign eVer) const;
170
171 SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode);
172 void ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
173 tools::Long nNewAlignM);
174 SmRect & ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
175 bool bKeepVerAlignParams);
176
177 tools::Long OrientedDist(const Point &rPoint) const;
178 bool IsInsideRect(const Point &rPoint) const;
179 bool IsInsideItalicRect(const Point &rPoint) const;
180
181 inline tools::Rectangle AsRectangle() const;
182 SmRect AsGlyphRect() const;
183 };
184
185
SetItalicSpaces(tools::Long nLeftSpace,tools::Long nRightSpace)186 inline void SmRect::SetItalicSpaces(tools::Long nLeftSpace, tools::Long nRightSpace)
187 // set extra spacing to the left and right for (italic)
188 // letters/text
189 {
190 nItalicLeftSpace = nLeftSpace;
191 nItalicRightSpace = nRightSpace;
192 }
193
194
CopyMBL(const SmRect & rRect)195 inline void SmRect::CopyMBL(const SmRect &rRect)
196 // copy AlignM baseline and value of 'rRect'
197 {
198 nBaseline = rRect.nBaseline;
199 bHasBaseline = rRect.bHasBaseline;
200 nAlignM = rRect.nAlignM;
201 }
202
203
GetBaseline() const204 inline tools::Long SmRect::GetBaseline() const
205 {
206 SAL_WARN_IF( !HasBaseline(), "starmath", "Baseline does not exist" );
207 return nBaseline;
208 }
209
210
AsRectangle() const211 inline tools::Rectangle SmRect::AsRectangle() const
212 {
213 return tools::Rectangle(Point(GetItalicLeft(), GetTop()), GetItalicSize());
214 }
215
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
217