bump product version to 5.0.4.1
[LibreOffice.git] / starmath / source / rect.cxx
blobad8a2e481a340caa81ce7dafaece6a68ad1e2f40
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/wrkwin.hxx>
23 #include <vcl/virdev.hxx>
26 #include "rect.hxx"
27 #include "types.hxx"
28 #include "utility.hxx"
29 #include "smmod.hxx"
35 // '\0' terminated Array with symbol, which should be treat as letters in
36 // StarMath Font, (to get a normal (non-clipped) SmRect in contrast to the
37 // other operators and symbols).
38 static sal_Unicode const aMathAlpha[] =
40 MS_ALEPH, MS_IM, MS_RE,
41 MS_WP, sal_Unicode(0xE070), MS_EMPTYSET,
42 sal_Unicode(0x2113), sal_Unicode(0xE0D6), sal_Unicode(0x2107),
43 sal_Unicode(0x2127), sal_Unicode(0x210A), MS_HBAR,
44 MS_LAMBDABAR, MS_SETN, MS_SETZ,
45 MS_SETQ, MS_SETR, MS_SETC,
46 sal_Unicode(0x2373), sal_Unicode(0xE0A5), sal_Unicode(0x2112),
47 sal_Unicode(0x2130), sal_Unicode(0x2131),
48 sal_Unicode('\0')
51 bool SmIsMathAlpha(const OUString &rText)
52 // true iff symbol (from StarMath Font) should be treated as letter
54 if (rText.isEmpty())
55 return false;
57 OSL_ENSURE(rText.getLength() == 1, "Sm : string must be exactly one character long");
58 sal_Unicode cChar = rText[0];
60 // is it a greek symbol?
61 if (sal_Unicode(0xE0AC) <= cChar && cChar <= sal_Unicode(0xE0D4))
62 return true;
63 else
65 // appears it in 'aMathAlpha'?
66 const sal_Unicode *pChar = aMathAlpha;
67 while (*pChar && *pChar != cChar)
68 pChar++;
69 return *pChar != '\0';
76 // SmRect members
80 SmRect::SmRect()
81 // constructs empty rectangle at (0, 0) with width and height 0.
83 OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: ooops...");
84 OSL_ENSURE(aSize == Size(0, 0), "Sm: ooops...");
86 bHasBaseline = bHasAlignInfo = false;
87 nBaseline = nAlignT = nAlignM = nAlignB =
88 nGlyphTop = nGlyphBottom =
89 nItalicLeftSpace = nItalicRightSpace =
90 nLoAttrFence = nHiAttrFence = 0;
91 nBorderWidth = 0;
95 SmRect::SmRect(const SmRect &rRect)
96 : aTopLeft(rRect.aTopLeft),
97 aSize(rRect.aSize)
99 bHasBaseline = rRect.bHasBaseline;
100 nBaseline = rRect.nBaseline;
101 nAlignT = rRect.nAlignT;
102 nAlignM = rRect.nAlignM;
103 nAlignB = rRect.nAlignB;
104 nGlyphTop = rRect.nGlyphTop;
105 nGlyphBottom = rRect.nGlyphBottom;
106 nHiAttrFence = rRect.nHiAttrFence;
107 nLoAttrFence = rRect.nLoAttrFence;
108 bHasAlignInfo = rRect.bHasAlignInfo;
109 nItalicLeftSpace = rRect.nItalicLeftSpace;
110 nItalicRightSpace = rRect.nItalicRightSpace;
111 nBorderWidth = rRect.nBorderWidth;
115 void SmRect::CopyAlignInfo(const SmRect &rRect)
117 nBaseline = rRect.nBaseline;
118 bHasBaseline = rRect.bHasBaseline;
119 nAlignT = rRect.nAlignT;
120 nAlignM = rRect.nAlignM;
121 nAlignB = rRect.nAlignB;
122 bHasAlignInfo = rRect.bHasAlignInfo;
123 nLoAttrFence = rRect.nLoAttrFence;
124 nHiAttrFence = rRect.nHiAttrFence;
128 void SmRect::BuildRect(const OutputDevice &rDev, const SmFormat *pFormat,
129 const OUString &rText, sal_uInt16 nBorder)
131 OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: Ooops...");
133 aSize = Size(rDev.GetTextWidth(rText), rDev.GetTextHeight());
135 const FontMetric aFM (rDev.GetFontMetric());
136 bool bIsMath = aFM.GetName().equalsIgnoreAsciiCase( FONTNAME_MATH );
137 bool bAllowSmaller = bIsMath && !SmIsMathAlpha(rText);
138 const long nFontHeight = rDev.GetFont().GetSize().Height();
140 nBorderWidth = nBorder;
141 bHasAlignInfo = true;
142 bHasBaseline = true;
143 nBaseline = aFM.GetAscent();
144 nAlignT = nBaseline - nFontHeight * 750L / 1000L;
145 nAlignM = nBaseline - nFontHeight * 121L / 422L;
146 // that's where the horizontal bars of '+', '-', ... are
147 // (1/3 of ascent over baseline)
148 // (121 = 1/3 of 12pt ascent, 422 = 12pt fontheight)
149 nAlignB = nBaseline;
151 // workaround for printer fonts with very small (possible 0 or even
152 // negative(!)) leading
153 if (aFM.GetIntLeading() < 5 && rDev.GetOutDevType() == OUTDEV_PRINTER)
155 OutputDevice *pWindow = Application::GetDefaultDevice();
157 pWindow->Push(PushFlags::MAPMODE | PushFlags::FONT);
159 pWindow->SetMapMode(rDev.GetMapMode());
160 pWindow->SetFont(rDev.GetFontMetric());
162 long nDelta = pWindow->GetFontMetric().GetIntLeading();
163 if (nDelta == 0)
164 { // this value approx. fits a Leading of 80 at a
165 // Fontheight of 422 (12pt)
166 nDelta = nFontHeight * 8L / 43;
168 SetTop(GetTop() - nDelta);
170 pWindow->Pop();
173 // get GlyphBoundRect
174 Rectangle aGlyphRect;
175 bool bSuccess = SmGetGlyphBoundRect(rDev, rText, aGlyphRect);
176 if (!bSuccess)
177 SAL_WARN("starmath", "Ooops... (Font missing?)");
179 nItalicLeftSpace = GetLeft() - aGlyphRect.Left() + nBorderWidth;
180 nItalicRightSpace = aGlyphRect.Right() - GetRight() + nBorderWidth;
181 if (nItalicLeftSpace < 0 && !bAllowSmaller)
182 nItalicLeftSpace = 0;
183 if (nItalicRightSpace < 0 && !bAllowSmaller)
184 nItalicRightSpace = 0;
186 long nDist = 0;
187 if (pFormat)
188 nDist = (rDev.GetFont().GetSize().Height()
189 * pFormat->GetDistance(DIS_ORNAMENTSIZE)) / 100L;
191 nHiAttrFence = aGlyphRect.TopLeft().Y() - 1 - nBorderWidth - nDist;
192 nLoAttrFence = SmFromTo(GetAlignB(), GetBottom(), 0.0);
194 nGlyphTop = aGlyphRect.Top() - nBorderWidth;
195 nGlyphBottom = aGlyphRect.Bottom() + nBorderWidth;
197 if (bAllowSmaller)
199 // for symbols and operators from the StarMath Font
200 // we adjust upper and lower margin of the symbol
201 SetTop(nGlyphTop);
202 SetBottom(nGlyphBottom);
205 if (nHiAttrFence < GetTop())
206 nHiAttrFence = GetTop();
208 if (nLoAttrFence > GetBottom())
209 nLoAttrFence = GetBottom();
211 OSL_ENSURE(rText.isEmpty() || !IsEmpty(),
212 "Sm: empty rectangle created");
216 void SmRect::Init(const OutputDevice &rDev, const SmFormat *pFormat,
217 const OUString &rText, sal_uInt16 nEBorderWidth)
218 // get rectangle fitting for drawing 'rText' on OutputDevice 'rDev'
220 BuildRect(rDev, pFormat, rText, nEBorderWidth);
224 SmRect::SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
225 const OUString &rText, long nEBorderWidth)
227 OSL_ENSURE( nEBorderWidth >= 0, "BorderWidth is negative" );
228 if (nEBorderWidth < 0)
229 nEBorderWidth = 0;
230 Init(rDev, pFormat, rText, (sal_uInt16) nEBorderWidth);
234 SmRect::SmRect(long nWidth, long nHeight)
235 // this constructor should never be used for anything textlike because
236 // it will not provide useful values for baseline, AlignT and AlignB!
237 // It's purpose is to get a 'SmRect' for the horizontal line in fractions
238 // as used in 'SmBinVerNode'.
239 : aSize(nWidth, nHeight)
241 OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: ooops...");
243 bHasBaseline = false;
244 bHasAlignInfo = true;
245 nBaseline = 0;
246 nAlignT = GetTop();
247 nAlignB = GetBottom();
248 nAlignM = (nAlignT + nAlignB) / 2; // this is the default
249 nItalicLeftSpace = nItalicRightSpace = 0;
250 nGlyphTop = nHiAttrFence = GetTop();
251 nGlyphBottom = nLoAttrFence = GetBottom();
252 nBorderWidth = 0;
256 void SmRect::SetLeft(long nLeft)
258 if (nLeft <= GetRight())
259 { aSize.Width() = GetRight() - nLeft + 1;
260 aTopLeft.X() = nLeft;
265 void SmRect::SetRight(long nRight)
267 if (nRight >= GetLeft())
268 aSize.Width() = nRight - GetLeft() + 1;
272 void SmRect::SetBottom(long nBottom)
274 if (nBottom >= GetTop())
275 aSize.Height() = nBottom - GetTop() + 1;
279 void SmRect::SetTop(long nTop)
281 if (nTop <= GetBottom())
282 { aSize.Height() = GetBottom() - nTop + 1;
283 aTopLeft.Y() = nTop;
288 void SmRect::Move(const Point &rPosition)
289 // move rectangle by position 'rPosition'.
291 aTopLeft += rPosition;
293 long nDelta = rPosition.Y();
294 nBaseline += nDelta;
295 nAlignT += nDelta;
296 nAlignM += nDelta;
297 nAlignB += nDelta;
298 nGlyphTop += nDelta;
299 nGlyphBottom += nDelta;
300 nHiAttrFence += nDelta;
301 nLoAttrFence += nDelta;
305 const Point SmRect::AlignTo(const SmRect &rRect, RectPos ePos,
306 RectHorAlign eHor, RectVerAlign eVer) const
307 { Point aPos (GetTopLeft());
308 // will become the topleft point of the new rectangle position
310 // set horizontal or vertical new rectangle position depending on
311 // 'ePos' is one of 'RP_LEFT', 'RP_RIGHT' or 'RP_TOP', 'RP_BOTTOM'
312 switch (ePos)
313 { case RP_LEFT :
314 aPos.X() = rRect.GetItalicLeft() - GetItalicRightSpace()
315 - GetWidth();
316 break;
317 case RP_RIGHT :
318 aPos.X() = rRect.GetItalicRight() + 1 + GetItalicLeftSpace();
319 break;
320 case RP_TOP :
321 aPos.Y() = rRect.GetTop() - GetHeight();
322 break;
323 case RP_BOTTOM :
324 aPos.Y() = rRect.GetBottom() + 1;
325 break;
326 case RP_ATTRIBUT :
327 aPos.X() = rRect.GetItalicCenterX() - GetItalicWidth() / 2
328 + GetItalicLeftSpace();
329 break;
330 default :
331 SAL_WARN("starmath", "unknown case");
334 // check if horizontal position is already set
335 if (ePos == RP_LEFT || ePos == RP_RIGHT || ePos == RP_ATTRIBUT)
336 // correct error in current vertical position
337 switch (eVer)
338 { case RVA_TOP :
339 aPos.Y() += rRect.GetAlignT() - GetAlignT();
340 break;
341 case RVA_MID :
342 aPos.Y() += rRect.GetAlignM() - GetAlignM();
343 break;
344 case RVA_BASELINE :
345 // align baselines if possible else align mid's
346 if (HasBaseline() && rRect.HasBaseline())
347 aPos.Y() += rRect.GetBaseline() - GetBaseline();
348 else
349 aPos.Y() += rRect.GetAlignM() - GetAlignM();
350 break;
351 case RVA_BOTTOM :
352 aPos.Y() += rRect.GetAlignB() - GetAlignB();
353 break;
354 case RVA_CENTERY :
355 aPos.Y() += rRect.GetCenterY() - GetCenterY();
356 break;
357 case RVA_ATTRIBUT_HI:
358 aPos.Y() += rRect.GetHiAttrFence() - GetBottom();
359 break;
360 case RVA_ATTRIBUT_MID :
361 aPos.Y() += SmFromTo(rRect.GetAlignB(), rRect.GetAlignT(), 0.4)
362 - GetCenterY();
363 break;
364 case RVA_ATTRIBUT_LO :
365 aPos.Y() += rRect.GetLoAttrFence() - GetTop();
366 break;
367 default :
368 SAL_WARN("starmath", "unknown case");
371 // check if vertical position is already set
372 if (ePos == RP_TOP || ePos == RP_BOTTOM)
373 // correct error in current horizontal position
374 switch (eHor)
375 { case RHA_LEFT :
376 aPos.X() += rRect.GetItalicLeft() - GetItalicLeft();
377 break;
378 case RHA_CENTER :
379 aPos.X() += rRect.GetItalicCenterX() - GetItalicCenterX();
380 break;
381 case RHA_RIGHT :
382 aPos.X() += rRect.GetItalicRight() - GetItalicRight();
383 break;
384 default :
385 SAL_WARN("starmath", "unknown case");
388 return aPos;
392 SmRect & SmRect::Union(const SmRect &rRect)
393 // rectangle union of current one with 'rRect'. The result is to be the
394 // smallest rectangles that covers the space of both rectangles.
395 // (empty rectangles cover no space)
396 //! Italic correction is NOT taken into account here!
398 if (rRect.IsEmpty())
399 return *this;
401 long nL = rRect.GetLeft(),
402 nR = rRect.GetRight(),
403 nT = rRect.GetTop(),
404 nB = rRect.GetBottom(),
405 nGT = rRect.nGlyphTop,
406 nGB = rRect.nGlyphBottom;
407 if (!IsEmpty())
408 { long nTmp;
410 if ((nTmp = GetLeft()) < nL)
411 nL = nTmp;
412 if ((nTmp = GetRight()) > nR)
413 nR = nTmp;
414 if ((nTmp = GetTop()) < nT)
415 nT = nTmp;
416 if ((nTmp = GetBottom()) > nB)
417 nB = nTmp;
418 if ((nTmp = nGlyphTop) < nGT)
419 nGT = nTmp;
420 if ((nTmp = nGlyphBottom) > nGB)
421 nGB = nTmp;
424 SetLeft(nL);
425 SetRight(nR);
426 SetTop(nT);
427 SetBottom(nB);
428 nGlyphTop = nGT;
429 nGlyphBottom = nGB;
431 return *this;
435 SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode)
436 // let current rectangle be the union of itself and 'rRect'
437 // (the smallest rectangle surrounding both). Also adapt values for
438 // 'AlignT', 'AlignM', 'AlignB', baseline and italic-spaces.
439 // The baseline is set according to 'eCopyMode'.
440 // If one of the rectangles has no relevant info the other one is copied.
442 // get some values used for (italic) spaces adaption
443 // ! (need to be done before changing current SmRect) !
444 long nL = std::min(GetItalicLeft(), rRect.GetItalicLeft()),
445 nR = std::max(GetItalicRight(), rRect.GetItalicRight());
447 Union(rRect);
449 SetItalicSpaces(GetLeft() - nL, nR - GetRight());
451 if (!HasAlignInfo())
452 CopyAlignInfo(rRect);
453 else if (rRect.HasAlignInfo())
454 { nAlignT = std::min(GetAlignT(), rRect.GetAlignT());
455 nAlignB = std::max(GetAlignB(), rRect.GetAlignB());
456 nHiAttrFence = std::min(GetHiAttrFence(), rRect.GetHiAttrFence());
457 nLoAttrFence = std::max(GetLoAttrFence(), rRect.GetLoAttrFence());
458 OSL_ENSURE(HasAlignInfo(), "Sm: ooops...");
460 switch (eCopyMode)
461 { case RCP_THIS:
462 // already done
463 break;
464 case RCP_ARG:
465 CopyMBL(rRect);
466 break;
467 case RCP_NONE:
468 ClearBaseline();
469 nAlignM = (nAlignT + nAlignB) / 2;
470 break;
471 case RCP_XOR:
472 if (!HasBaseline())
473 CopyMBL(rRect);
474 break;
475 default :
476 SAL_WARN("starmath", "unknown case");
480 return *this;
484 SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
485 long nNewAlignM)
486 // as 'ExtendBy' but sets AlignM value to 'nNewAlignM'.
487 // (this version will be used in 'SmBinVerNode' to provide means to
488 // align eg "{a over b} over c" correctly where AlignM should not
489 // be (AlignT + AlignB) / 2)
491 OSL_ENSURE(HasAlignInfo(), "Sm: no align info");
493 ExtendBy(rRect, eCopyMode);
494 nAlignM = nNewAlignM;
496 return *this;
500 SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
501 bool bKeepVerAlignParams)
502 // as 'ExtendBy' but keeps original values for AlignT, -M and -B and
503 // baseline.
504 // (this is used in 'SmSupSubNode' where the sub-/supscripts shouldn't
505 // be allowed to modify these values.)
507 long nOldAlignT = GetAlignT(),
508 nOldAlignM = GetAlignM(),
509 nOldAlignB = GetAlignB(),
510 nOldBaseline = nBaseline; //! depends not on 'HasBaseline'
511 bool bOldHasAlignInfo = HasAlignInfo();
513 ExtendBy(rRect, eCopyMode);
515 if (bKeepVerAlignParams)
516 { nAlignT = nOldAlignT;
517 nAlignM = nOldAlignM;
518 nAlignB = nOldAlignB;
519 nBaseline = nOldBaseline;
520 bHasAlignInfo = bOldHasAlignInfo;
523 return *this;
527 long SmRect::OrientedDist(const Point &rPoint) const
528 // return oriented distance of rPoint to the current rectangle,
529 // especially the return value is <= 0 iff the point is inside the
530 // rectangle.
531 // For simplicity the maximum-norm is used.
533 bool bIsInside = IsInsideItalicRect(rPoint);
535 // build reference point to define the distance
536 Point aRef;
537 if (bIsInside)
538 { Point aIC (GetItalicCenterX(), GetCenterY());
540 aRef.X() = rPoint.X() >= aIC.X() ? GetItalicRight() : GetItalicLeft();
541 aRef.Y() = rPoint.Y() >= aIC.Y() ? GetBottom() : GetTop();
543 else
545 // x-coordinate
546 if (rPoint.X() > GetItalicRight())
547 aRef.X() = GetItalicRight();
548 else if (rPoint.X() < GetItalicLeft())
549 aRef.X() = GetItalicLeft();
550 else
551 aRef.X() = rPoint.X();
552 // y-coordinate
553 if (rPoint.Y() > GetBottom())
554 aRef.Y() = GetBottom();
555 else if (rPoint.Y() < GetTop())
556 aRef.Y() = GetTop();
557 else
558 aRef.Y() = rPoint.Y();
561 // build distance vector
562 Point aDist (aRef - rPoint);
564 long nAbsX = labs(aDist.X()),
565 nAbsY = labs(aDist.Y());
567 return bIsInside ? - std::min(nAbsX, nAbsY) : std::max (nAbsX, nAbsY);
571 bool SmRect::IsInsideRect(const Point &rPoint) const
573 return rPoint.Y() >= GetTop()
574 && rPoint.Y() <= GetBottom()
575 && rPoint.X() >= GetLeft()
576 && rPoint.X() <= GetRight();
580 bool SmRect::IsInsideItalicRect(const Point &rPoint) const
582 return rPoint.Y() >= GetTop()
583 && rPoint.Y() <= GetBottom()
584 && rPoint.X() >= GetItalicLeft()
585 && rPoint.X() <= GetItalicRight();
588 SmRect SmRect::AsGlyphRect() const
590 SmRect aRect (*this);
591 aRect.SetTop(nGlyphTop);
592 aRect.SetBottom(nGlyphBottom);
593 return aRect;
596 bool SmGetGlyphBoundRect(const vcl::RenderContext &rDev,
597 const OUString &rText, Rectangle &rRect)
598 // basically the same as 'GetTextBoundRect' (in class 'OutputDevice')
599 // but with a string as argument.
601 // handle special case first
602 if (rText.isEmpty())
604 rRect.SetEmpty();
605 return true;
608 // get a device where 'OutputDevice::GetTextBoundRect' will be successful
609 OutputDevice *pGlyphDev;
610 if (rDev.GetOutDevType() != OUTDEV_PRINTER)
611 pGlyphDev = const_cast<OutputDevice *>(&rDev);
612 else
614 // since we format for the printer (where GetTextBoundRect will fail)
615 // we need a virtual device here.
616 pGlyphDev = &SM_MOD()->GetDefaultVirtualDev();
619 const FontMetric aDevFM (rDev.GetFontMetric());
621 pGlyphDev->Push(PushFlags::FONT | PushFlags::MAPMODE);
622 vcl::Font aFnt(rDev.GetFont());
623 aFnt.SetAlign(ALIGN_TOP);
625 // use scale factor when calling GetTextBoundRect to counter
626 // negative effects from antialiasing which may otherwise result
627 // in significant incorrect bounding rectangles for some characters.
628 Size aFntSize = aFnt.GetSize();
630 // Workaround to avoid HUGE font sizes and resulting problems
631 long nScaleFactor = 1;
632 while( aFntSize.Height() > 2000 * nScaleFactor )
633 nScaleFactor *= 2;
635 aFnt.SetSize( Size( aFntSize.Width() / nScaleFactor, aFntSize.Height() / nScaleFactor ) );
636 pGlyphDev->SetFont(aFnt);
638 long nTextWidth = rDev.GetTextWidth(rText);
639 Point aPoint;
640 Rectangle aResult (aPoint, Size(nTextWidth, rDev.GetTextHeight())),
641 aTmp;
643 bool bSuccess = pGlyphDev->GetTextBoundRect(aTmp, rText, 0, 0);
644 OSL_ENSURE( bSuccess, "GetTextBoundRect failed" );
647 if (!aTmp.IsEmpty())
649 aResult = Rectangle(aTmp.Left() * nScaleFactor, aTmp.Top() * nScaleFactor,
650 aTmp.Right() * nScaleFactor, aTmp.Bottom() * nScaleFactor);
651 if (&rDev != pGlyphDev) /* only when rDev is a printer... */
653 long nGDTextWidth = pGlyphDev->GetTextWidth(rText);
654 if (nGDTextWidth != 0 &&
655 nTextWidth != nGDTextWidth)
657 aResult.Right() *= nTextWidth;
658 aResult.Right() /= nGDTextWidth * nScaleFactor;
663 // move rectangle to match possibly different baselines
664 // (because of different devices)
665 long nDelta = aDevFM.GetAscent() - pGlyphDev->GetFontMetric().GetAscent() * nScaleFactor;
666 aResult.Move(0, nDelta);
668 pGlyphDev->Pop();
670 rRect = aResult;
671 return bSuccess;
675 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */