LCOV - code coverage report
Current view: top level - starmath/source - rect.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 286 328 87.2 %
Date: 2014-04-11 Functions: 19 23 82.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include <osl/diagnose.h>
      21             : #include <vcl/svapp.hxx>
      22             : #include <vcl/wrkwin.hxx>
      23             : #include <vcl/virdev.hxx>
      24             : 
      25             : 
      26             : #include "rect.hxx"
      27             : #include "types.hxx"
      28             : #include "utility.hxx"
      29             : #include "smmod.hxx"
      30             : 
      31             : 
      32             : 
      33             : 
      34             : 
      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[] =
      39             : {
      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')
      49             : };
      50             : 
      51        4265 : bool SmIsMathAlpha(const OUString &rText)
      52             :     // true iff symbol (from StarMath Font) should be treated as letter
      53             : {
      54        4265 :     if (rText.isEmpty())
      55          15 :         return false;
      56             : 
      57             :     OSL_ENSURE(rText.getLength() == 1, "Sm : string must be exactly one character long");
      58        4250 :     sal_Unicode cChar = rText[0];
      59             : 
      60             :     // is it a greek symbol?
      61        4250 :     if (sal_Unicode(0xE0AC) <= cChar  &&  cChar <= sal_Unicode(0xE0D4))
      62           0 :         return true;
      63             :     else
      64             :     {
      65             :         // appears it in 'aMathAlpha'?
      66        4250 :         const sal_Unicode *pChar = aMathAlpha;
      67      106250 :         while (*pChar  &&  *pChar != cChar)
      68       97750 :             pChar++;
      69        4250 :         return *pChar != '\0';
      70             :     }
      71             : }
      72             : 
      73             : 
      74             : 
      75             : 
      76             : // SmRect members
      77             : 
      78             : 
      79             : 
      80       15530 : SmRect::SmRect()
      81             :     // constructs empty rectangle at (0, 0) with width and height 0.
      82             : {
      83             :     OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: ooops...");
      84             :     OSL_ENSURE(aSize == Size(0, 0), "Sm: ooops...");
      85             : 
      86       15530 :     bHasBaseline = bHasAlignInfo = false;
      87             :     nBaseline = nAlignT = nAlignM = nAlignB =
      88             :     nGlyphTop = nGlyphBottom =
      89             :     nItalicLeftSpace = nItalicRightSpace =
      90       15530 :     nLoAttrFence = nHiAttrFence = 0;
      91       15530 :     nBorderWidth = 0;
      92       15530 : }
      93             : 
      94             : 
      95       15966 : SmRect::SmRect(const SmRect &rRect)
      96             : :   aTopLeft(rRect.aTopLeft),
      97       15966 :     aSize(rRect.aSize)
      98             : {
      99       15966 :     bHasBaseline  = rRect.bHasBaseline;
     100       15966 :     nBaseline     = rRect.nBaseline;
     101       15966 :     nAlignT       = rRect.nAlignT;
     102       15966 :     nAlignM       = rRect.nAlignM;
     103       15966 :     nAlignB       = rRect.nAlignB;
     104       15966 :     nGlyphTop     = rRect.nGlyphTop;
     105       15966 :     nGlyphBottom  = rRect.nGlyphBottom;
     106       15966 :     nHiAttrFence  = rRect.nHiAttrFence;
     107       15966 :     nLoAttrFence  = rRect.nLoAttrFence;
     108       15966 :     bHasAlignInfo = rRect.bHasAlignInfo;
     109       15966 :     nItalicLeftSpace  = rRect.nItalicLeftSpace;
     110       15966 :     nItalicRightSpace = rRect.nItalicRightSpace;
     111       15966 :     nBorderWidth  = rRect.nBorderWidth;
     112       15966 : }
     113             : 
     114             : 
     115          96 : void SmRect::CopyAlignInfo(const SmRect &rRect)
     116             : {
     117          96 :     nBaseline     = rRect.nBaseline;
     118          96 :     bHasBaseline  = rRect.bHasBaseline;
     119          96 :     nAlignT       = rRect.nAlignT;
     120          96 :     nAlignM       = rRect.nAlignM;
     121          96 :     nAlignB       = rRect.nAlignB;
     122          96 :     bHasAlignInfo = rRect.bHasAlignInfo;
     123          96 :     nLoAttrFence  = rRect.nLoAttrFence;
     124          96 :     nHiAttrFence  = rRect.nHiAttrFence;
     125          96 : }
     126             : 
     127             : 
     128        8666 : void SmRect::BuildRect(const OutputDevice &rDev, const SmFormat *pFormat,
     129             :                        const OUString &rText, sal_uInt16 nBorder)
     130             : {
     131             :     OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: Ooops...");
     132             : 
     133        8666 :     aSize = Size(rDev.GetTextWidth(rText), rDev.GetTextHeight());
     134             : 
     135        8666 :     const FontMetric  aFM (rDev.GetFontMetric());
     136        8666 :     bool              bIsMath  = aFM.GetName().equalsIgnoreAsciiCase( FONTNAME_MATH );
     137        8666 :     bool              bAllowSmaller = bIsMath && !SmIsMathAlpha(rText);
     138        8666 :     const long        nFontHeight = rDev.GetFont().GetSize().Height();
     139             : 
     140        8666 :     nBorderWidth  = nBorder;
     141        8666 :     bHasAlignInfo = true;
     142        8666 :     bHasBaseline  = true;
     143        8666 :     nBaseline     = aFM.GetAscent();
     144        8666 :     nAlignT       = nBaseline - nFontHeight * 750L / 1000L;
     145        8666 :     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        8666 :     nAlignB       = nBaseline;
     150             : 
     151             :     // workaround for printer fonts with very small (possible 0 or even
     152             :     // negative(!)) leading
     153        8666 :     if (aFM.GetIntLeading() < 5  &&  rDev.GetOutDevType() == OUTDEV_PRINTER)
     154             :     {
     155           9 :         OutputDevice    *pWindow = Application::GetDefaultDevice();
     156             : 
     157           9 :         pWindow->Push(PUSH_MAPMODE | PUSH_FONT);
     158             : 
     159           9 :         pWindow->SetMapMode(rDev.GetMapMode());
     160           9 :         pWindow->SetFont(rDev.GetFontMetric());
     161             : 
     162           9 :         long  nDelta = pWindow->GetFontMetric().GetIntLeading();
     163           9 :         if (nDelta == 0)
     164             :         {   // this value approx. fits a Leading of 80 at a
     165             :             // Fontheight of 422 (12pt)
     166           0 :             nDelta = nFontHeight * 8L / 43;
     167             :         }
     168           9 :         SetTop(GetTop() - nDelta);
     169             : 
     170           9 :         pWindow->Pop();
     171             :     }
     172             : 
     173             :     // get GlyphBoundRect
     174        8666 :     Rectangle  aGlyphRect;
     175        8666 :     bool bSuccess = SmGetGlyphBoundRect(rDev, rText, aGlyphRect);
     176        8666 :     if (!bSuccess)
     177             :         SAL_WARN("starmath", "Ooops... (Font missing?)");
     178             : 
     179        8666 :     nItalicLeftSpace  = GetLeft() - aGlyphRect.Left() + nBorderWidth;
     180        8666 :     nItalicRightSpace = aGlyphRect.Right() - GetRight() + nBorderWidth;
     181        8666 :     if (nItalicLeftSpace  < 0  &&  !bAllowSmaller)
     182         658 :         nItalicLeftSpace  = 0;
     183        8666 :     if (nItalicRightSpace < 0  &&  !bAllowSmaller)
     184         935 :         nItalicRightSpace = 0;
     185             : 
     186        8666 :     long  nDist = 0;
     187        8666 :     if (pFormat)
     188        7580 :         nDist = (rDev.GetFont().GetSize().Height()
     189        7580 :                 * pFormat->GetDistance(DIS_ORNAMENTSIZE)) / 100L;
     190             : 
     191        8666 :     nHiAttrFence = aGlyphRect.TopLeft().Y() - 1 - nBorderWidth - nDist;
     192        8666 :     nLoAttrFence = SmFromTo(GetAlignB(), GetBottom(), 0.0);
     193             : 
     194        8666 :     nGlyphTop    = aGlyphRect.Top() - nBorderWidth;
     195        8666 :     nGlyphBottom = aGlyphRect.Bottom() + nBorderWidth;
     196             : 
     197        8666 :     if (bAllowSmaller)
     198             :     {
     199             :         // for symbols and operators from the StarMath Font
     200             :         // we adjust upper and lower margin of the symbol
     201        4265 :         SetTop(nGlyphTop);
     202        4265 :         SetBottom(nGlyphBottom);
     203             :     }
     204             : 
     205        8666 :     if (nHiAttrFence < GetTop())
     206        4274 :         nHiAttrFence = GetTop();
     207             : 
     208        8666 :     if (nLoAttrFence > GetBottom())
     209        1133 :         nLoAttrFence = GetBottom();
     210             : 
     211             :     OSL_ENSURE(rText.isEmpty() || !IsEmpty(),
     212        8666 :                "Sm: empty rectangle created");
     213        8666 : }
     214             : 
     215             : 
     216        8666 : 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'
     219             : {
     220        8666 :     BuildRect(rDev, pFormat, rText, nEBorderWidth);
     221        8666 : }
     222             : 
     223             : 
     224        8666 : SmRect::SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
     225        8666 :                const OUString &rText, long nEBorderWidth)
     226             : {
     227             :     OSL_ENSURE( nEBorderWidth >= 0, "BorderWidth is negative" );
     228        8666 :     if (nEBorderWidth < 0)
     229           0 :         nEBorderWidth = 0;
     230        8666 :     Init(rDev, pFormat, rText, (sal_uInt16) nEBorderWidth);
     231        8666 : }
     232             : 
     233             : 
     234        1478 : 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        1478 : :   aSize(nWidth, nHeight)
     240             : {
     241             :     OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: ooops...");
     242             : 
     243        1478 :     bHasBaseline  = false;
     244        1478 :     bHasAlignInfo = true;
     245        1478 :     nBaseline     = 0;
     246        1478 :     nAlignT       = GetTop();
     247        1478 :     nAlignB       = GetBottom();
     248        1478 :     nAlignM       = (nAlignT + nAlignB) / 2;        // this is the default
     249        1478 :     nItalicLeftSpace = nItalicRightSpace = 0;
     250        1478 :     nGlyphTop    = nHiAttrFence  = GetTop();
     251        1478 :     nGlyphBottom = nLoAttrFence  = GetBottom();
     252        1478 :     nBorderWidth  = 0;
     253        1478 : }
     254             : 
     255             : 
     256        7985 : void SmRect::SetLeft(long nLeft)
     257             : {
     258        7985 :     if (nLeft <= GetRight())
     259        7922 :     {   aSize.Width() = GetRight() - nLeft + 1;
     260        7922 :         aTopLeft.X()  = nLeft;
     261             :     }
     262        7985 : }
     263             : 
     264             : 
     265        7985 : void SmRect::SetRight(long nRight)
     266             : {
     267        7985 :     if (nRight >= GetLeft())
     268        7985 :         aSize.Width() = nRight - GetLeft() + 1;
     269        7985 : }
     270             : 
     271             : 
     272       12389 : void SmRect::SetBottom(long nBottom)
     273             : {
     274       12389 :     if (nBottom >= GetTop())
     275       12374 :         aSize.Height() = nBottom - GetTop() + 1;
     276       12389 : }
     277             : 
     278             : 
     279       12259 : void SmRect::SetTop(long nTop)
     280             : {
     281       12259 :     if (nTop <= GetBottom())
     282       12241 :     {   aSize.Height()   = GetBottom() - nTop + 1;
     283       12241 :         aTopLeft.Y() = nTop;
     284             :     }
     285       12259 : }
     286             : 
     287             : 
     288       30744 : void SmRect::Move(const Point &rPosition)
     289             :     // move rectangle by position 'rPosition'.
     290             : {
     291       30744 :     aTopLeft  += rPosition;
     292             : 
     293       30744 :     long  nDelta = rPosition.Y();
     294       30744 :     nBaseline += nDelta;
     295       30744 :     nAlignT   += nDelta;
     296       30744 :     nAlignM   += nDelta;
     297       30744 :     nAlignB   += nDelta;
     298       30744 :     nGlyphTop    += nDelta;
     299       30744 :     nGlyphBottom += nDelta;
     300       30744 :     nHiAttrFence += nDelta;
     301       30744 :     nLoAttrFence += nDelta;
     302       30744 : }
     303             : 
     304             : 
     305        8097 : const Point SmRect::AlignTo(const SmRect &rRect, RectPos ePos,
     306             :                             RectHorAlign eHor, RectVerAlign eVer) const
     307        8097 : {   Point  aPos (GetTopLeft());
     308             :         // will become the topleft point of the new rectangle position
     309             : 
     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        8097 :     switch (ePos)
     313             :     {   case RP_LEFT :
     314         889 :             aPos.X() = rRect.GetItalicLeft() - GetItalicRightSpace()
     315         889 :                        - GetWidth();
     316         889 :             break;
     317             :         case RP_RIGHT :
     318        4836 :             aPos.X() = rRect.GetItalicRight() + 1 + GetItalicLeftSpace();
     319        4836 :             break;
     320             :         case RP_TOP :
     321         414 :             aPos.Y() = rRect.GetTop() - GetHeight();
     322         414 :             break;
     323             :         case RP_BOTTOM :
     324        1704 :             aPos.Y() = rRect.GetBottom() + 1;
     325        1704 :             break;
     326             :         case RP_ATTRIBUT :
     327         254 :             aPos.X() = rRect.GetItalicCenterX() - GetItalicWidth() / 2
     328         254 :                        + GetItalicLeftSpace();
     329         254 :             break;
     330             :         default :
     331             :             SAL_WARN("starmath", "unknown case");
     332             :     }
     333             : 
     334             :     // check if horizontal position is already set
     335        8097 :     if (ePos == RP_LEFT  ||  ePos == RP_RIGHT  ||  ePos == RP_ATTRIBUT)
     336             :         // correct error in current vertical position
     337        5979 :         switch (eVer)
     338             :         {   case RVA_TOP :
     339         443 :                 aPos.Y() += rRect.GetAlignT() - GetAlignT();
     340         443 :                 break;
     341             :             case RVA_MID :
     342         162 :                 aPos.Y() += rRect.GetAlignM() - GetAlignM();
     343         162 :                 break;
     344             :             case RVA_BASELINE :
     345             :                 // align baselines if possible else align mid's
     346        3966 :                 if (HasBaseline() && rRect.HasBaseline())
     347        3558 :                     aPos.Y() += rRect.GetBaseline() - GetBaseline();
     348             :                 else
     349         408 :                     aPos.Y() += rRect.GetAlignM() - GetAlignM();
     350        3966 :                 break;
     351             :             case RVA_BOTTOM :
     352         236 :                 aPos.Y() += rRect.GetAlignB() - GetAlignB();
     353         236 :                 break;
     354             :             case RVA_CENTERY :
     355         918 :                 aPos.Y() += rRect.GetCenterY() - GetCenterY();
     356         918 :                 break;
     357             :             case RVA_ATTRIBUT_HI:
     358         218 :                 aPos.Y() += rRect.GetHiAttrFence() - GetBottom();
     359         218 :                 break;
     360             :             case RVA_ATTRIBUT_MID :
     361          36 :                 aPos.Y() += SmFromTo(rRect.GetAlignB(), rRect.GetAlignT(), 0.4)
     362          36 :                             - GetCenterY();
     363          18 :                 break;
     364             :             case RVA_ATTRIBUT_LO :
     365          18 :                 aPos.Y() += rRect.GetLoAttrFence() - GetTop();
     366          18 :                 break;
     367             :         default :
     368             :                 SAL_WARN("starmath", "unknown case");
     369             :         }
     370             : 
     371             :     // check if vertical position is already set
     372        8097 :     if (ePos == RP_TOP  ||  ePos == RP_BOTTOM)
     373             :         // correct error in current horizontal position
     374        2118 :         switch (eHor)
     375             :         {   case RHA_LEFT :
     376           0 :                 aPos.X() += rRect.GetItalicLeft() - GetItalicLeft();
     377           0 :                 break;
     378             :             case RHA_CENTER :
     379        2063 :                 aPos.X() += rRect.GetItalicCenterX() - GetItalicCenterX();
     380        2063 :                 break;
     381             :             case RHA_RIGHT :
     382          55 :                 aPos.X() += rRect.GetItalicRight() - GetItalicRight();
     383          55 :                 break;
     384             :             default :
     385             :                 SAL_WARN("starmath", "unknown case");
     386             :         }
     387             : 
     388        8097 :     return aPos;
     389             : }
     390             : 
     391             : 
     392        8072 : 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!
     397             : {
     398        8072 :     if (rRect.IsEmpty())
     399          87 :         return *this;
     400             : 
     401        7985 :     long  nL  = rRect.GetLeft(),
     402        7985 :           nR  = rRect.GetRight(),
     403        7985 :           nT  = rRect.GetTop(),
     404        7985 :           nB  = rRect.GetBottom(),
     405        7985 :           nGT = rRect.nGlyphTop,
     406        7985 :           nGB = rRect.nGlyphBottom;
     407        7985 :     if (!IsEmpty())
     408             :     {   long  nTmp;
     409             : 
     410        7898 :         if ((nTmp = GetLeft()) < nL)
     411        6206 :             nL = nTmp;
     412        7898 :         if ((nTmp = GetRight()) > nR)
     413        2268 :             nR = nTmp;
     414        7898 :         if ((nTmp = GetTop()) < nT)
     415        3790 :             nT = nTmp;
     416        7898 :         if ((nTmp = GetBottom()) > nB)
     417        2568 :             nB = nTmp;
     418        7898 :         if ((nTmp = nGlyphTop) < nGT)
     419        4244 :             nGT = nTmp;
     420        7898 :         if ((nTmp = nGlyphBottom) > nGB)
     421        2840 :             nGB = nTmp;
     422             :     }
     423             : 
     424        7985 :     SetLeft(nL);
     425        7985 :     SetRight(nR);
     426        7985 :     SetTop(nT);
     427        7985 :     SetBottom(nB);
     428        7985 :     nGlyphTop    = nGT;
     429        7985 :     nGlyphBottom = nGB;
     430             : 
     431        7985 :     return *this;
     432             : }
     433             : 
     434             : 
     435        8072 : 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.
     441             : {
     442             :     // get some values used for (italic) spaces adaption
     443             :     // ! (need to be done before changing current SmRect) !
     444        8072 :     long  nL = std::min(GetItalicLeft(),  rRect.GetItalicLeft()),
     445        8072 :           nR = std::max(GetItalicRight(), rRect.GetItalicRight());
     446             : 
     447        8072 :     Union(rRect);
     448             : 
     449        8072 :     SetItalicSpaces(GetLeft() - nL, nR - GetRight());
     450             : 
     451        8072 :     if (!HasAlignInfo())
     452          96 :         CopyAlignInfo(rRect);
     453        7976 :     else if (rRect.HasAlignInfo())
     454        7961 :     {   nAlignT = std::min(GetAlignT(), rRect.GetAlignT());
     455        7961 :         nAlignB = std::max(GetAlignB(), rRect.GetAlignB());
     456        7961 :         nHiAttrFence = std::min(GetHiAttrFence(), rRect.GetHiAttrFence());
     457        7961 :         nLoAttrFence = std::max(GetLoAttrFence(), rRect.GetLoAttrFence());
     458             :         OSL_ENSURE(HasAlignInfo(), "Sm: ooops...");
     459             : 
     460        7961 :         switch (eCopyMode)
     461             :         {   case RCP_THIS:
     462             :                 // already done
     463        2442 :                 break;
     464             :             case RCP_ARG:
     465        1053 :                 CopyMBL(rRect);
     466        1053 :                 break;
     467             :             case RCP_NONE:
     468         734 :                 ClearBaseline();
     469         734 :                 nAlignM = (nAlignT + nAlignB) / 2;
     470         734 :                 break;
     471             :             case RCP_XOR:
     472        3732 :                 if (!HasBaseline())
     473         156 :                     CopyMBL(rRect);
     474        3732 :                 break;
     475             :             default :
     476             :                 SAL_WARN("starmath", "unknown case");
     477             :         }
     478             :     }
     479             : 
     480        8072 :     return *this;
     481             : }
     482             : 
     483             : 
     484         265 : 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)
     490             : {
     491             :     OSL_ENSURE(HasAlignInfo(), "Sm: no align info");
     492             : 
     493         265 :     ExtendBy(rRect, eCopyMode);
     494         265 :     nAlignM = nNewAlignM;
     495             : 
     496         265 :     return *this;
     497             : }
     498             : 
     499             : 
     500        1257 : 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.)
     506             : {
     507        1257 :     long  nOldAlignT   = GetAlignT(),
     508        1257 :           nOldAlignM   = GetAlignM(),
     509        1257 :           nOldAlignB   = GetAlignB(),
     510        1257 :           nOldBaseline = nBaseline;     //! depends not on 'HasBaseline'
     511        1257 :     bool  bOldHasAlignInfo = HasAlignInfo();
     512             : 
     513        1257 :     ExtendBy(rRect, eCopyMode);
     514             : 
     515        1257 :     if (bKeepVerAlignParams)
     516        1257 :     {   nAlignT   = nOldAlignT;
     517        1257 :         nAlignM   = nOldAlignM;
     518        1257 :         nAlignB   = nOldAlignB;
     519        1257 :         nBaseline = nOldBaseline;
     520        1257 :         bHasAlignInfo = bOldHasAlignInfo;
     521             :     }
     522             : 
     523        1257 :     return *this;
     524             : }
     525             : 
     526             : 
     527           0 : 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.
     532             : {
     533           0 :     bool  bIsInside = IsInsideItalicRect(rPoint);
     534             : 
     535             :     // build reference point to define the distance
     536           0 :     Point  aRef;
     537           0 :     if (bIsInside)
     538           0 :     {   Point  aIC (GetItalicCenterX(), GetCenterY());
     539             : 
     540           0 :         aRef.X() = rPoint.X() >= aIC.X() ? GetItalicRight() : GetItalicLeft();
     541           0 :         aRef.Y() = rPoint.Y() >= aIC.Y() ? GetBottom() : GetTop();
     542             :     }
     543             :     else
     544             :     {
     545             :         // x-coordinate
     546           0 :         if (rPoint.X() > GetItalicRight())
     547           0 :             aRef.X() = GetItalicRight();
     548           0 :         else if (rPoint.X() < GetItalicLeft())
     549           0 :             aRef.X() = GetItalicLeft();
     550             :         else
     551           0 :             aRef.X() = rPoint.X();
     552             :         // y-coordinate
     553           0 :         if (rPoint.Y() > GetBottom())
     554           0 :             aRef.Y() = GetBottom();
     555           0 :         else if (rPoint.Y() < GetTop())
     556           0 :             aRef.Y() = GetTop();
     557             :         else
     558           0 :             aRef.Y() = rPoint.Y();
     559             :     }
     560             : 
     561             :     // build distance vector
     562           0 :     Point  aDist (aRef - rPoint);
     563             : 
     564           0 :     long nAbsX = labs(aDist.X()),
     565           0 :          nAbsY = labs(aDist.Y());
     566             : 
     567           0 :     return bIsInside ? - std::min(nAbsX, nAbsY) : std::max (nAbsX, nAbsY);
     568             : }
     569             : 
     570             : 
     571           0 : bool SmRect::IsInsideRect(const Point &rPoint) const
     572             : {
     573           0 :     return     rPoint.Y() >= GetTop()
     574           0 :            &&  rPoint.Y() <= GetBottom()
     575           0 :            &&  rPoint.X() >= GetLeft()
     576           0 :            &&  rPoint.X() <= GetRight();
     577             : }
     578             : 
     579             : 
     580           0 : bool SmRect::IsInsideItalicRect(const Point &rPoint) const
     581             : {
     582           0 :     return     rPoint.Y() >= GetTop()
     583           0 :            &&  rPoint.Y() <= GetBottom()
     584           0 :            &&  rPoint.X() >= GetItalicLeft()
     585           0 :            &&  rPoint.X() <= GetItalicRight();
     586             : }
     587             : 
     588           0 : SmRect SmRect::AsGlyphRect() const
     589             : {
     590           0 :     SmRect aRect (*this);
     591           0 :     aRect.SetTop(nGlyphTop);
     592           0 :     aRect.SetBottom(nGlyphBottom);
     593           0 :     return aRect;
     594             : }
     595             : 
     596        8666 : bool SmGetGlyphBoundRect(const OutputDevice &rDev,
     597             :                          const OUString &rText, Rectangle &rRect)
     598             :     // basically the same as 'GetTextBoundRect' (in class 'OutputDevice')
     599             :     // but with a string as argument.
     600             : {
     601             :     // handle special case first
     602        8666 :     if (rText.isEmpty())
     603             :     {
     604          24 :         rRect.SetEmpty();
     605          24 :         return true;
     606             :     }
     607             : 
     608             :     // get a device where 'OutputDevice::GetTextBoundRect' will be successful
     609             :     OutputDevice *pGlyphDev;
     610        8642 :     if (rDev.GetOutDevType() != OUTDEV_PRINTER)
     611        8538 :         pGlyphDev = (OutputDevice *) &rDev;
     612             :     else
     613             :     {
     614             :         // since we format for the printer (where GetTextBoundRect will fail)
     615             :         // we need a virtual device here.
     616         104 :         pGlyphDev = &SM_MOD()->GetDefaultVirtualDev();
     617             :     }
     618             : 
     619        8642 :     const FontMetric  aDevFM (rDev.GetFontMetric());
     620             : 
     621        8642 :     pGlyphDev->Push(PUSH_FONT | PUSH_MAPMODE);
     622       17284 :     Font aFnt(rDev.GetFont());
     623        8642 :     aFnt.SetAlign(ALIGN_TOP);
     624             : 
     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        8642 :     Size aFntSize = aFnt.GetSize();
     629             : 
     630             :     // Workaround to avoid HUGE font sizes and resulting problems
     631        8642 :     long nScaleFactor = 1;
     632       17284 :     while( aFntSize.Height() > 2000 * nScaleFactor )
     633           0 :         nScaleFactor *= 2;
     634             : 
     635        8642 :     aFnt.SetSize( Size( aFntSize.Width() / nScaleFactor, aFntSize.Height() / nScaleFactor ) );
     636        8642 :     pGlyphDev->SetFont(aFnt);
     637             : 
     638        8642 :     long nTextWidth = rDev.GetTextWidth(rText);
     639        8642 :     Point aPoint;
     640        8642 :     Rectangle   aResult (aPoint, Size(nTextWidth, rDev.GetTextHeight())),
     641        8642 :                 aTmp;
     642             : 
     643        8642 :     bool bSuccess = pGlyphDev->GetTextBoundRect(aTmp, rText, 0, 0);
     644             :     OSL_ENSURE( bSuccess, "GetTextBoundRect failed" );
     645             : 
     646             : 
     647        8642 :     if (!aTmp.IsEmpty())
     648             :     {
     649       17282 :         aResult = Rectangle(aTmp.Left() * nScaleFactor, aTmp.Top() * nScaleFactor,
     650       25923 :                             aTmp.Right() * nScaleFactor, aTmp.Bottom() * nScaleFactor);
     651        8641 :         if (&rDev != pGlyphDev) /* only when rDev is a printer... */
     652             :         {
     653         103 :             long nGDTextWidth  = pGlyphDev->GetTextWidth(rText);
     654         103 :             if (nGDTextWidth != 0  &&
     655             :                 nTextWidth != nGDTextWidth)
     656             :             {
     657         103 :                 aResult.Right() *= nTextWidth;
     658         103 :                 aResult.Right() /= nGDTextWidth * nScaleFactor;
     659             :             }
     660             :         }
     661             :     }
     662             : 
     663             :     // move rectangle to match possibly different baselines
     664             :     // (because of different devices)
     665        8642 :     long nDelta = aDevFM.GetAscent() - pGlyphDev->GetFontMetric().GetAscent() * nScaleFactor;
     666        8642 :     aResult.Move(0, nDelta);
     667             : 
     668        8642 :     pGlyphDev->Pop();
     669             : 
     670        8642 :     rRect = aResult;
     671       17284 :     return bSuccess;
     672             : }
     673             : 
     674             : 
     675             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10