Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / starmath / source / rect.cxx
blob1ec45fbeb10d669b62a6f18a3bf2038fd272b30c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/diagnose.h>
21 #include <vcl/svapp.hxx>
22 #include <vcl/virdev.hxx>
24 #include <rect.hxx>
25 #include <types.hxx>
26 #include <smmod.hxx>
28 #include <cassert>
29 #include <unordered_set>
31 namespace {
33 bool SmGetGlyphBoundRect(const vcl::RenderContext &rDev,
34 const OUString &rText, tools::Rectangle &rRect)
35 // basically the same as 'GetTextBoundRect' (in class 'OutputDevice')
36 // but with a string as argument.
38 // handle special case first
39 if (rText.isEmpty())
41 rRect.SetEmpty();
42 return true;
45 // get a device where 'OutputDevice::GetTextBoundRect' will be successful
46 OutputDevice *pGlyphDev;
47 if (rDev.GetOutDevType() != OUTDEV_PRINTER)
48 pGlyphDev = const_cast<OutputDevice *>(&rDev);
49 else
51 // since we format for the printer (where GetTextBoundRect will fail)
52 // we need a virtual device here.
53 pGlyphDev = &SM_MOD()->GetDefaultVirtualDev();
56 const FontMetric aDevFM (rDev.GetFontMetric());
58 pGlyphDev->Push(PushFlags::FONT | PushFlags::MAPMODE);
59 vcl::Font aFnt(rDev.GetFont());
60 aFnt.SetAlignment(ALIGN_TOP);
62 // use scale factor when calling GetTextBoundRect to counter
63 // negative effects from antialiasing which may otherwise result
64 // in significant incorrect bounding rectangles for some characters.
65 Size aFntSize = aFnt.GetFontSize();
67 // Workaround to avoid HUGE font sizes and resulting problems
68 long nScaleFactor = 1;
69 while( aFntSize.Height() > 2000 * nScaleFactor )
70 nScaleFactor *= 2;
72 aFnt.SetFontSize( Size( aFntSize.Width() / nScaleFactor, aFntSize.Height() / nScaleFactor ) );
73 pGlyphDev->SetFont(aFnt);
75 long nTextWidth = rDev.GetTextWidth(rText);
76 tools::Rectangle aResult (Point(), Size(nTextWidth, rDev.GetTextHeight())),
77 aTmp;
79 bool bSuccess = pGlyphDev->GetTextBoundRect(aTmp, rText);
80 OSL_ENSURE( bSuccess, "GetTextBoundRect failed" );
83 if (!aTmp.IsEmpty())
85 aResult = tools::Rectangle(aTmp.Left() * nScaleFactor, aTmp.Top() * nScaleFactor,
86 aTmp.Right() * nScaleFactor, aTmp.Bottom() * nScaleFactor);
87 if (&rDev != pGlyphDev) /* only when rDev is a printer... */
89 long nGDTextWidth = pGlyphDev->GetTextWidth(rText);
90 if (nGDTextWidth != 0 &&
91 nTextWidth != nGDTextWidth)
93 aResult.SetRight( aResult.Right() * nTextWidth );
94 aResult.SetRight( aResult.Right() / ( nGDTextWidth * nScaleFactor) );
99 // move rectangle to match possibly different baselines
100 // (because of different devices)
101 long nDelta = aDevFM.GetAscent() - pGlyphDev->GetFontMetric().GetAscent() * nScaleFactor;
102 aResult.Move(0, nDelta);
104 pGlyphDev->Pop();
106 rRect = aResult;
107 return bSuccess;
110 bool SmIsMathAlpha(const OUString &rText)
111 // true iff symbol (from StarMath Font) should be treated as letter
113 // Set of symbols, which should be treated as letters in StarMath Font
114 // (to get a normal (non-clipped) SmRect in contrast to the other operators
115 // and symbols).
116 static std::unordered_set<sal_Unicode> const aMathAlpha({
117 MS_ALEPH, MS_IM, MS_RE,
118 MS_WP, u'\xE070', MS_EMPTYSET,
119 u'\x2113', u'\xE0D6', u'\x2107',
120 u'\x2127', u'\x210A', MS_HBAR,
121 MS_LAMBDABAR, MS_SETN, MS_SETZ,
122 MS_SETQ, MS_SETR, MS_SETC,
123 u'\x2373', u'\xE0A5', u'\x2112',
124 u'\x2130', u'\x2131'
127 if (rText.isEmpty())
128 return false;
130 OSL_ENSURE(rText.getLength() == 1, "Sm : string must be exactly one character long");
131 sal_Unicode cChar = rText[0];
133 // is it a greek symbol?
134 if (u'\xE0AC' <= cChar && cChar <= u'\xE0D4')
135 return true;
136 // or, does it appear in 'aMathAlpha'?
137 return aMathAlpha.find(cChar) != aMathAlpha.end();
143 SmRect::SmRect()
144 // constructs empty rectangle at (0, 0) with width and height 0.
145 : aTopLeft(0, 0)
146 , aSize(0, 0)
147 , nBaseline(0)
148 , nAlignT(0)
149 , nAlignM(0)
150 , nAlignB(0)
151 , nGlyphTop(0)
152 , nGlyphBottom(0)
153 , nItalicLeftSpace(0)
154 , nItalicRightSpace(0)
155 , nLoAttrFence(0)
156 , nHiAttrFence(0)
157 , nBorderWidth(0)
158 , bHasBaseline(false)
159 , bHasAlignInfo(false)
164 void SmRect::CopyAlignInfo(const SmRect &rRect)
166 nBaseline = rRect.nBaseline;
167 bHasBaseline = rRect.bHasBaseline;
168 nAlignT = rRect.nAlignT;
169 nAlignM = rRect.nAlignM;
170 nAlignB = rRect.nAlignB;
171 bHasAlignInfo = rRect.bHasAlignInfo;
172 nLoAttrFence = rRect.nLoAttrFence;
173 nHiAttrFence = rRect.nHiAttrFence;
177 SmRect::SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
178 const OUString &rText, sal_uInt16 nBorder)
179 // get rectangle fitting for drawing 'rText' on OutputDevice 'rDev'
180 : aTopLeft(0, 0)
181 , aSize(rDev.GetTextWidth(rText), rDev.GetTextHeight())
183 const FontMetric aFM (rDev.GetFontMetric());
184 bool bIsMath = aFM.GetFamilyName().equalsIgnoreAsciiCase( FONTNAME_MATH );
185 bool bAllowSmaller = bIsMath && !SmIsMathAlpha(rText);
186 const long nFontHeight = rDev.GetFont().GetFontSize().Height();
188 nBorderWidth = nBorder;
189 bHasAlignInfo = true;
190 bHasBaseline = true;
191 nBaseline = aFM.GetAscent();
192 nAlignT = nBaseline - nFontHeight * 750 / 1000;
193 nAlignM = nBaseline - nFontHeight * 121 / 422;
194 // that's where the horizontal bars of '+', '-', ... are
195 // (1/3 of ascent over baseline)
196 // (121 = 1/3 of 12pt ascent, 422 = 12pt fontheight)
197 nAlignB = nBaseline;
199 // workaround for printer fonts with very small (possible 0 or even
200 // negative(!)) leading
201 if (aFM.GetInternalLeading() < 5 && rDev.GetOutDevType() == OUTDEV_PRINTER)
203 OutputDevice *pWindow = Application::GetDefaultDevice();
205 pWindow->Push(PushFlags::MAPMODE | PushFlags::FONT);
207 pWindow->SetMapMode(rDev.GetMapMode());
208 pWindow->SetFont(rDev.GetFontMetric());
210 long nDelta = pWindow->GetFontMetric().GetInternalLeading();
211 if (nDelta == 0)
212 { // this value approx. fits a Leading of 80 at a
213 // Fontheight of 422 (12pt)
214 nDelta = nFontHeight * 8 / 43;
216 SetTop(GetTop() - nDelta);
218 pWindow->Pop();
221 // get GlyphBoundRect
222 tools::Rectangle aGlyphRect;
223 bool bSuccess = SmGetGlyphBoundRect(rDev, rText, aGlyphRect);
224 if (!bSuccess)
225 SAL_WARN("starmath", "Ooops... (Font missing?)");
227 nItalicLeftSpace = GetLeft() - aGlyphRect.Left() + nBorderWidth;
228 nItalicRightSpace = aGlyphRect.Right() - GetRight() + nBorderWidth;
229 if (nItalicLeftSpace < 0 && !bAllowSmaller)
230 nItalicLeftSpace = 0;
231 if (nItalicRightSpace < 0 && !bAllowSmaller)
232 nItalicRightSpace = 0;
234 long nDist = 0;
235 if (pFormat)
236 nDist = (rDev.GetFont().GetFontSize().Height()
237 * pFormat->GetDistance(DIS_ORNAMENTSIZE)) / 100;
239 nHiAttrFence = aGlyphRect.TopLeft().Y() - 1 - nBorderWidth - nDist;
240 nLoAttrFence = SmFromTo(GetAlignB(), GetBottom(), 0.0);
242 nGlyphTop = aGlyphRect.Top() - nBorderWidth;
243 nGlyphBottom = aGlyphRect.Bottom() + nBorderWidth;
245 if (bAllowSmaller)
247 // for symbols and operators from the StarMath Font
248 // we adjust upper and lower margin of the symbol
249 SetTop(nGlyphTop);
250 SetBottom(nGlyphBottom);
253 if (nHiAttrFence < GetTop())
254 nHiAttrFence = GetTop();
256 if (nLoAttrFence > GetBottom())
257 nLoAttrFence = GetBottom();
259 OSL_ENSURE(rText.isEmpty() || !IsEmpty(),
260 "Sm: empty rectangle created");
264 SmRect::SmRect(long nWidth, long nHeight)
265 // this constructor should never be used for anything textlike because
266 // it will not provide useful values for baseline, AlignT and AlignB!
267 // It's purpose is to get a 'SmRect' for the horizontal line in fractions
268 // as used in 'SmBinVerNode'.
269 : aTopLeft(0, 0)
270 , aSize(nWidth, nHeight)
271 , nBaseline(0)
272 , nItalicLeftSpace(0)
273 , nItalicRightSpace(0)
274 , nBorderWidth(0)
275 , bHasBaseline(false)
276 , bHasAlignInfo(true)
278 nAlignT = nGlyphTop = nHiAttrFence = GetTop();
279 nAlignB = nGlyphBottom = nLoAttrFence = GetBottom();
280 nAlignM = (nAlignT + nAlignB) / 2; // this is the default
284 void SmRect::SetLeft(long nLeft)
286 if (nLeft <= GetRight())
287 { aSize.setWidth( GetRight() - nLeft + 1 );
288 aTopLeft.setX( nLeft );
293 void SmRect::SetRight(long nRight)
295 if (nRight >= GetLeft())
296 aSize.setWidth( nRight - GetLeft() + 1 );
300 void SmRect::SetBottom(long nBottom)
302 if (nBottom >= GetTop())
303 aSize.setHeight( nBottom - GetTop() + 1 );
307 void SmRect::SetTop(long nTop)
309 if (nTop <= GetBottom())
310 { aSize.setHeight( GetBottom() - nTop + 1 );
311 aTopLeft.setY( nTop );
316 void SmRect::Move(const Point &rPosition)
317 // move rectangle by position 'rPosition'.
319 aTopLeft += rPosition;
321 long nDelta = rPosition.Y();
322 nBaseline += nDelta;
323 nAlignT += nDelta;
324 nAlignM += nDelta;
325 nAlignB += nDelta;
326 nGlyphTop += nDelta;
327 nGlyphBottom += nDelta;
328 nHiAttrFence += nDelta;
329 nLoAttrFence += nDelta;
333 const Point SmRect::AlignTo(const SmRect &rRect, RectPos ePos,
334 RectHorAlign eHor, RectVerAlign eVer) const
335 { Point aPos (GetTopLeft());
336 // will become the topleft point of the new rectangle position
338 // set horizontal or vertical new rectangle position depending on ePos
339 switch (ePos)
340 { case RectPos::Left:
341 aPos.setX( rRect.GetItalicLeft() - GetItalicRightSpace()
342 - GetWidth() );
343 break;
344 case RectPos::Right:
345 aPos.setX( rRect.GetItalicRight() + 1 + GetItalicLeftSpace() );
346 break;
347 case RectPos::Top:
348 aPos.setY( rRect.GetTop() - GetHeight() );
349 break;
350 case RectPos::Bottom:
351 aPos.setY( rRect.GetBottom() + 1 );
352 break;
353 case RectPos::Attribute:
354 aPos.setX( rRect.GetItalicCenterX() - GetItalicWidth() / 2
355 + GetItalicLeftSpace() );
356 break;
357 default:
358 assert(false);
361 // check if horizontal position is already set
362 if (ePos == RectPos::Left || ePos == RectPos::Right || ePos == RectPos::Attribute)
363 // correct error in current vertical position
364 switch (eVer)
365 { case RectVerAlign::Top :
366 aPos.AdjustY(rRect.GetAlignT() - GetAlignT() );
367 break;
368 case RectVerAlign::Mid :
369 aPos.AdjustY(rRect.GetAlignM() - GetAlignM() );
370 break;
371 case RectVerAlign::Baseline :
372 // align baselines if possible else align mid's
373 if (HasBaseline() && rRect.HasBaseline())
374 aPos.AdjustY(rRect.GetBaseline() - GetBaseline() );
375 else
376 aPos.AdjustY(rRect.GetAlignM() - GetAlignM() );
377 break;
378 case RectVerAlign::Bottom :
379 aPos.AdjustY(rRect.GetAlignB() - GetAlignB() );
380 break;
381 case RectVerAlign::CenterY :
382 aPos.AdjustY(rRect.GetCenterY() - GetCenterY() );
383 break;
384 case RectVerAlign::AttributeHi:
385 aPos.AdjustY(rRect.GetHiAttrFence() - GetBottom() );
386 break;
387 case RectVerAlign::AttributeMid :
388 aPos.AdjustY(SmFromTo(rRect.GetAlignB(), rRect.GetAlignT(), 0.4)
389 - GetCenterY() );
390 break;
391 case RectVerAlign::AttributeLo :
392 aPos.AdjustY(rRect.GetLoAttrFence() - GetTop() );
393 break;
394 default :
395 assert(false);
398 // check if vertical position is already set
399 if (ePos == RectPos::Top || ePos == RectPos::Bottom)
400 // correct error in current horizontal position
401 switch (eHor)
402 { case RectHorAlign::Left:
403 aPos.AdjustX(rRect.GetItalicLeft() - GetItalicLeft() );
404 break;
405 case RectHorAlign::Center:
406 aPos.AdjustX(rRect.GetItalicCenterX() - GetItalicCenterX() );
407 break;
408 case RectHorAlign::Right:
409 aPos.AdjustX(rRect.GetItalicRight() - GetItalicRight() );
410 break;
411 default:
412 assert(false);
415 return aPos;
419 void SmRect::Union(const SmRect &rRect)
420 // rectangle union of current one with 'rRect'. The result is to be the
421 // smallest rectangles that covers the space of both rectangles.
422 // (empty rectangles cover no space)
423 //! Italic correction is NOT taken into account here!
425 if (rRect.IsEmpty())
426 return;
428 long nL = rRect.GetLeft(),
429 nR = rRect.GetRight(),
430 nT = rRect.GetTop(),
431 nB = rRect.GetBottom(),
432 nGT = rRect.nGlyphTop,
433 nGB = rRect.nGlyphBottom;
434 if (!IsEmpty())
435 { long nTmp;
437 if ((nTmp = GetLeft()) < nL)
438 nL = nTmp;
439 if ((nTmp = GetRight()) > nR)
440 nR = nTmp;
441 if ((nTmp = GetTop()) < nT)
442 nT = nTmp;
443 if ((nTmp = GetBottom()) > nB)
444 nB = nTmp;
445 if ((nTmp = nGlyphTop) < nGT)
446 nGT = nTmp;
447 if ((nTmp = nGlyphBottom) > nGB)
448 nGB = nTmp;
451 SetLeft(nL);
452 SetRight(nR);
453 SetTop(nT);
454 SetBottom(nB);
455 nGlyphTop = nGT;
456 nGlyphBottom = nGB;
460 SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode)
461 // let current rectangle be the union of itself and 'rRect'
462 // (the smallest rectangle surrounding both). Also adapt values for
463 // 'AlignT', 'AlignM', 'AlignB', baseline and italic-spaces.
464 // The baseline is set according to 'eCopyMode'.
465 // If one of the rectangles has no relevant info the other one is copied.
467 // get some values used for (italic) spaces adaption
468 // ! (need to be done before changing current SmRect) !
469 long nL = std::min(GetItalicLeft(), rRect.GetItalicLeft()),
470 nR = std::max(GetItalicRight(), rRect.GetItalicRight());
472 Union(rRect);
474 SetItalicSpaces(GetLeft() - nL, nR - GetRight());
476 if (!HasAlignInfo())
477 CopyAlignInfo(rRect);
478 else if (rRect.HasAlignInfo())
480 assert(HasAlignInfo());
481 nAlignT = std::min(GetAlignT(), rRect.GetAlignT());
482 nAlignB = std::max(GetAlignB(), rRect.GetAlignB());
483 nHiAttrFence = std::min(GetHiAttrFence(), rRect.GetHiAttrFence());
484 nLoAttrFence = std::max(GetLoAttrFence(), rRect.GetLoAttrFence());
486 switch (eCopyMode)
487 { case RectCopyMBL::This:
488 // already done
489 break;
490 case RectCopyMBL::Arg:
491 CopyMBL(rRect);
492 break;
493 case RectCopyMBL::None:
494 bHasBaseline = false;
495 nAlignM = (nAlignT + nAlignB) / 2;
496 break;
497 case RectCopyMBL::Xor:
498 if (!HasBaseline())
499 CopyMBL(rRect);
500 break;
501 default :
502 assert(false);
506 return *this;
510 void SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
511 long nNewAlignM)
512 // as 'ExtendBy' but sets AlignM value to 'nNewAlignM'.
513 // (this version will be used in 'SmBinVerNode' to provide means to
514 // align eg "{a over b} over c" correctly where AlignM should not
515 // be (AlignT + AlignB) / 2)
517 OSL_ENSURE(HasAlignInfo(), "Sm: no align info");
519 ExtendBy(rRect, eCopyMode);
520 nAlignM = nNewAlignM;
524 SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
525 bool bKeepVerAlignParams)
526 // as 'ExtendBy' but keeps original values for AlignT, -M and -B and
527 // baseline.
528 // (this is used in 'SmSupSubNode' where the sub-/supscripts shouldn't
529 // be allowed to modify these values.)
531 long nOldAlignT = GetAlignT(),
532 nOldAlignM = GetAlignM(),
533 nOldAlignB = GetAlignB(),
534 nOldBaseline = nBaseline; //! depends not on 'HasBaseline'
535 bool bOldHasAlignInfo = HasAlignInfo();
537 ExtendBy(rRect, eCopyMode);
539 if (bKeepVerAlignParams)
540 { nAlignT = nOldAlignT;
541 nAlignM = nOldAlignM;
542 nAlignB = nOldAlignB;
543 nBaseline = nOldBaseline;
544 bHasAlignInfo = bOldHasAlignInfo;
547 return *this;
551 long SmRect::OrientedDist(const Point &rPoint) const
552 // return oriented distance of rPoint to the current rectangle,
553 // especially the return value is <= 0 iff the point is inside the
554 // rectangle.
555 // For simplicity the maximum-norm is used.
557 bool bIsInside = IsInsideItalicRect(rPoint);
559 // build reference point to define the distance
560 Point aRef;
561 if (bIsInside)
562 { Point aIC (GetItalicCenterX(), GetCenterY());
564 aRef.setX( rPoint.X() >= aIC.X() ? GetItalicRight() : GetItalicLeft() );
565 aRef.setY( rPoint.Y() >= aIC.Y() ? GetBottom() : GetTop() );
567 else
569 // x-coordinate
570 if (rPoint.X() > GetItalicRight())
571 aRef.setX( GetItalicRight() );
572 else if (rPoint.X() < GetItalicLeft())
573 aRef.setX( GetItalicLeft() );
574 else
575 aRef.setX( rPoint.X() );
576 // y-coordinate
577 if (rPoint.Y() > GetBottom())
578 aRef.setY( GetBottom() );
579 else if (rPoint.Y() < GetTop())
580 aRef.setY( GetTop() );
581 else
582 aRef.setY( rPoint.Y() );
585 // build distance vector
586 Point aDist (aRef - rPoint);
588 long nAbsX = labs(aDist.X()),
589 nAbsY = labs(aDist.Y());
591 return bIsInside ? - std::min(nAbsX, nAbsY) : std::max (nAbsX, nAbsY);
595 bool SmRect::IsInsideRect(const Point &rPoint) const
597 return rPoint.Y() >= GetTop()
598 && rPoint.Y() <= GetBottom()
599 && rPoint.X() >= GetLeft()
600 && rPoint.X() <= GetRight();
604 bool SmRect::IsInsideItalicRect(const Point &rPoint) const
606 return rPoint.Y() >= GetTop()
607 && rPoint.Y() <= GetBottom()
608 && rPoint.X() >= GetItalicLeft()
609 && rPoint.X() <= GetItalicRight();
612 SmRect SmRect::AsGlyphRect() const
614 SmRect aRect (*this);
615 aRect.SetTop(nGlyphTop);
616 aRect.SetBottom(nGlyphBottom);
617 return aRect;
620 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */