LCOV - code coverage report
Current view: top level - starmath/source - rect.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 328 0.0 %
Date: 2014-04-14 Functions: 0 23 0.0 %
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           0 : bool SmIsMathAlpha(const OUString &rText)
      52             :     // true iff symbol (from StarMath Font) should be treated as letter
      53             : {
      54           0 :     if (rText.isEmpty())
      55           0 :         return false;
      56             : 
      57             :     OSL_ENSURE(rText.getLength() == 1, "Sm : string must be exactly one character long");
      58           0 :     sal_Unicode cChar = rText[0];
      59             : 
      60             :     // is it a greek symbol?
      61           0 :     if (sal_Unicode(0xE0AC) <= cChar  &&  cChar <= sal_Unicode(0xE0D4))
      62           0 :         return true;
      63             :     else
      64             :     {
      65             :         // appears it in 'aMathAlpha'?
      66           0 :         const sal_Unicode *pChar = aMathAlpha;
      67           0 :         while (*pChar  &&  *pChar != cChar)
      68           0 :             pChar++;
      69           0 :         return *pChar != '\0';
      70             :     }
      71             : }
      72             : 
      73             : 
      74             : 
      75             : 
      76             : // SmRect members
      77             : 
      78             : 
      79             : 
      80           0 : 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           0 :     bHasBaseline = bHasAlignInfo = false;
      87             :     nBaseline = nAlignT = nAlignM = nAlignB =
      88             :     nGlyphTop = nGlyphBottom =
      89             :     nItalicLeftSpace = nItalicRightSpace =
      90           0 :     nLoAttrFence = nHiAttrFence = 0;
      91           0 :     nBorderWidth = 0;
      92           0 : }
      93             : 
      94             : 
      95           0 : SmRect::SmRect(const SmRect &rRect)
      96             : :   aTopLeft(rRect.aTopLeft),
      97           0 :     aSize(rRect.aSize)
      98             : {
      99           0 :     bHasBaseline  = rRect.bHasBaseline;
     100           0 :     nBaseline     = rRect.nBaseline;
     101           0 :     nAlignT       = rRect.nAlignT;
     102           0 :     nAlignM       = rRect.nAlignM;
     103           0 :     nAlignB       = rRect.nAlignB;
     104           0 :     nGlyphTop     = rRect.nGlyphTop;
     105           0 :     nGlyphBottom  = rRect.nGlyphBottom;
     106           0 :     nHiAttrFence  = rRect.nHiAttrFence;
     107           0 :     nLoAttrFence  = rRect.nLoAttrFence;
     108           0 :     bHasAlignInfo = rRect.bHasAlignInfo;
     109           0 :     nItalicLeftSpace  = rRect.nItalicLeftSpace;
     110           0 :     nItalicRightSpace = rRect.nItalicRightSpace;
     111           0 :     nBorderWidth  = rRect.nBorderWidth;
     112           0 : }
     113             : 
     114             : 
     115           0 : void SmRect::CopyAlignInfo(const SmRect &rRect)
     116             : {
     117           0 :     nBaseline     = rRect.nBaseline;
     118           0 :     bHasBaseline  = rRect.bHasBaseline;
     119           0 :     nAlignT       = rRect.nAlignT;
     120           0 :     nAlignM       = rRect.nAlignM;
     121           0 :     nAlignB       = rRect.nAlignB;
     122           0 :     bHasAlignInfo = rRect.bHasAlignInfo;
     123           0 :     nLoAttrFence  = rRect.nLoAttrFence;
     124           0 :     nHiAttrFence  = rRect.nHiAttrFence;
     125           0 : }
     126             : 
     127             : 
     128           0 : 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           0 :     aSize = Size(rDev.GetTextWidth(rText), rDev.GetTextHeight());
     134             : 
     135           0 :     const FontMetric  aFM (rDev.GetFontMetric());
     136           0 :     bool              bIsMath  = aFM.GetName().equalsIgnoreAsciiCase( FONTNAME_MATH );
     137           0 :     bool              bAllowSmaller = bIsMath && !SmIsMathAlpha(rText);
     138           0 :     const long        nFontHeight = rDev.GetFont().GetSize().Height();
     139             : 
     140           0 :     nBorderWidth  = nBorder;
     141           0 :     bHasAlignInfo = true;
     142           0 :     bHasBaseline  = true;
     143           0 :     nBaseline     = aFM.GetAscent();
     144           0 :     nAlignT       = nBaseline - nFontHeight * 750L / 1000L;
     145           0 :     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           0 :     nAlignB       = nBaseline;
     150             : 
     151             :     // workaround for printer fonts with very small (possible 0 or even
     152             :     // negative(!)) leading
     153           0 :     if (aFM.GetIntLeading() < 5  &&  rDev.GetOutDevType() == OUTDEV_PRINTER)
     154             :     {
     155           0 :         OutputDevice    *pWindow = Application::GetDefaultDevice();
     156             : 
     157           0 :         pWindow->Push(PUSH_MAPMODE | PUSH_FONT);
     158             : 
     159           0 :         pWindow->SetMapMode(rDev.GetMapMode());
     160           0 :         pWindow->SetFont(rDev.GetFontMetric());
     161             : 
     162           0 :         long  nDelta = pWindow->GetFontMetric().GetIntLeading();
     163           0 :         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           0 :         SetTop(GetTop() - nDelta);
     169             : 
     170           0 :         pWindow->Pop();
     171             :     }
     172             : 
     173             :     // get GlyphBoundRect
     174           0 :     Rectangle  aGlyphRect;
     175           0 :     bool bSuccess = SmGetGlyphBoundRect(rDev, rText, aGlyphRect);
     176           0 :     if (!bSuccess)
     177             :         SAL_WARN("starmath", "Ooops... (Font missing?)");
     178             : 
     179           0 :     nItalicLeftSpace  = GetLeft() - aGlyphRect.Left() + nBorderWidth;
     180           0 :     nItalicRightSpace = aGlyphRect.Right() - GetRight() + nBorderWidth;
     181           0 :     if (nItalicLeftSpace  < 0  &&  !bAllowSmaller)
     182           0 :         nItalicLeftSpace  = 0;
     183           0 :     if (nItalicRightSpace < 0  &&  !bAllowSmaller)
     184           0 :         nItalicRightSpace = 0;
     185             : 
     186           0 :     long  nDist = 0;
     187           0 :     if (pFormat)
     188           0 :         nDist = (rDev.GetFont().GetSize().Height()
     189           0 :                 * pFormat->GetDistance(DIS_ORNAMENTSIZE)) / 100L;
     190             : 
     191           0 :     nHiAttrFence = aGlyphRect.TopLeft().Y() - 1 - nBorderWidth - nDist;
     192           0 :     nLoAttrFence = SmFromTo(GetAlignB(), GetBottom(), 0.0);
     193             : 
     194           0 :     nGlyphTop    = aGlyphRect.Top() - nBorderWidth;
     195           0 :     nGlyphBottom = aGlyphRect.Bottom() + nBorderWidth;
     196             : 
     197           0 :     if (bAllowSmaller)
     198             :     {
     199             :         // for symbols and operators from the StarMath Font
     200             :         // we adjust upper and lower margin of the symbol
     201           0 :         SetTop(nGlyphTop);
     202           0 :         SetBottom(nGlyphBottom);
     203             :     }
     204             : 
     205           0 :     if (nHiAttrFence < GetTop())
     206           0 :         nHiAttrFence = GetTop();
     207             : 
     208           0 :     if (nLoAttrFence > GetBottom())
     209           0 :         nLoAttrFence = GetBottom();
     210             : 
     211             :     OSL_ENSURE(rText.isEmpty() || !IsEmpty(),
     212           0 :                "Sm: empty rectangle created");
     213           0 : }
     214             : 
     215             : 
     216           0 : 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           0 :     BuildRect(rDev, pFormat, rText, nEBorderWidth);
     221           0 : }
     222             : 
     223             : 
     224           0 : SmRect::SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
     225           0 :                const OUString &rText, long nEBorderWidth)
     226             : {
     227             :     OSL_ENSURE( nEBorderWidth >= 0, "BorderWidth is negative" );
     228           0 :     if (nEBorderWidth < 0)
     229           0 :         nEBorderWidth = 0;
     230           0 :     Init(rDev, pFormat, rText, (sal_uInt16) nEBorderWidth);
     231           0 : }
     232             : 
     233             : 
     234           0 : 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           0 : :   aSize(nWidth, nHeight)
     240             : {
     241             :     OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: ooops...");
     242             : 
     243           0 :     bHasBaseline  = false;
     244           0 :     bHasAlignInfo = true;
     245           0 :     nBaseline     = 0;
     246           0 :     nAlignT       = GetTop();
     247           0 :     nAlignB       = GetBottom();
     248           0 :     nAlignM       = (nAlignT + nAlignB) / 2;        // this is the default
     249           0 :     nItalicLeftSpace = nItalicRightSpace = 0;
     250           0 :     nGlyphTop    = nHiAttrFence  = GetTop();
     251           0 :     nGlyphBottom = nLoAttrFence  = GetBottom();
     252           0 :     nBorderWidth  = 0;
     253           0 : }
     254             : 
     255             : 
     256           0 : void SmRect::SetLeft(long nLeft)
     257             : {
     258           0 :     if (nLeft <= GetRight())
     259           0 :     {   aSize.Width() = GetRight() - nLeft + 1;
     260           0 :         aTopLeft.X()  = nLeft;
     261             :     }
     262           0 : }
     263             : 
     264             : 
     265           0 : void SmRect::SetRight(long nRight)
     266             : {
     267           0 :     if (nRight >= GetLeft())
     268           0 :         aSize.Width() = nRight - GetLeft() + 1;
     269           0 : }
     270             : 
     271             : 
     272           0 : void SmRect::SetBottom(long nBottom)
     273             : {
     274           0 :     if (nBottom >= GetTop())
     275           0 :         aSize.Height() = nBottom - GetTop() + 1;
     276           0 : }
     277             : 
     278             : 
     279           0 : void SmRect::SetTop(long nTop)
     280             : {
     281           0 :     if (nTop <= GetBottom())
     282           0 :     {   aSize.Height()   = GetBottom() - nTop + 1;
     283           0 :         aTopLeft.Y() = nTop;
     284             :     }
     285           0 : }
     286             : 
     287             : 
     288           0 : void SmRect::Move(const Point &rPosition)
     289             :     // move rectangle by position 'rPosition'.
     290             : {
     291           0 :     aTopLeft  += rPosition;
     292             : 
     293           0 :     long  nDelta = rPosition.Y();
     294           0 :     nBaseline += nDelta;
     295           0 :     nAlignT   += nDelta;
     296           0 :     nAlignM   += nDelta;
     297           0 :     nAlignB   += nDelta;
     298           0 :     nGlyphTop    += nDelta;
     299           0 :     nGlyphBottom += nDelta;
     300           0 :     nHiAttrFence += nDelta;
     301           0 :     nLoAttrFence += nDelta;
     302           0 : }
     303             : 
     304             : 
     305           0 : const Point SmRect::AlignTo(const SmRect &rRect, RectPos ePos,
     306             :                             RectHorAlign eHor, RectVerAlign eVer) const
     307           0 : {   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           0 :     switch (ePos)
     313             :     {   case RP_LEFT :
     314           0 :             aPos.X() = rRect.GetItalicLeft() - GetItalicRightSpace()
     315           0 :                        - GetWidth();
     316           0 :             break;
     317             :         case RP_RIGHT :
     318           0 :             aPos.X() = rRect.GetItalicRight() + 1 + GetItalicLeftSpace();
     319           0 :             break;
     320             :         case RP_TOP :
     321           0 :             aPos.Y() = rRect.GetTop() - GetHeight();
     322           0 :             break;
     323             :         case RP_BOTTOM :
     324           0 :             aPos.Y() = rRect.GetBottom() + 1;
     325           0 :             break;
     326             :         case RP_ATTRIBUT :
     327           0 :             aPos.X() = rRect.GetItalicCenterX() - GetItalicWidth() / 2
     328           0 :                        + GetItalicLeftSpace();
     329           0 :             break;
     330             :         default :
     331             :             SAL_WARN("starmath", "unknown case");
     332             :     }
     333             : 
     334             :     // check if horizontal position is already set
     335           0 :     if (ePos == RP_LEFT  ||  ePos == RP_RIGHT  ||  ePos == RP_ATTRIBUT)
     336             :         // correct error in current vertical position
     337           0 :         switch (eVer)
     338             :         {   case RVA_TOP :
     339           0 :                 aPos.Y() += rRect.GetAlignT() - GetAlignT();
     340           0 :                 break;
     341             :             case RVA_MID :
     342           0 :                 aPos.Y() += rRect.GetAlignM() - GetAlignM();
     343           0 :                 break;
     344             :             case RVA_BASELINE :
     345             :                 // align baselines if possible else align mid's
     346           0 :                 if (HasBaseline() && rRect.HasBaseline())
     347           0 :                     aPos.Y() += rRect.GetBaseline() - GetBaseline();
     348             :                 else
     349           0 :                     aPos.Y() += rRect.GetAlignM() - GetAlignM();
     350           0 :                 break;
     351             :             case RVA_BOTTOM :
     352           0 :                 aPos.Y() += rRect.GetAlignB() - GetAlignB();
     353           0 :                 break;
     354             :             case RVA_CENTERY :
     355           0 :                 aPos.Y() += rRect.GetCenterY() - GetCenterY();
     356           0 :                 break;
     357             :             case RVA_ATTRIBUT_HI:
     358           0 :                 aPos.Y() += rRect.GetHiAttrFence() - GetBottom();
     359           0 :                 break;
     360             :             case RVA_ATTRIBUT_MID :
     361           0 :                 aPos.Y() += SmFromTo(rRect.GetAlignB(), rRect.GetAlignT(), 0.4)
     362           0 :                             - GetCenterY();
     363           0 :                 break;
     364             :             case RVA_ATTRIBUT_LO :
     365           0 :                 aPos.Y() += rRect.GetLoAttrFence() - GetTop();
     366           0 :                 break;
     367             :         default :
     368             :                 SAL_WARN("starmath", "unknown case");
     369             :         }
     370             : 
     371             :     // check if vertical position is already set
     372           0 :     if (ePos == RP_TOP  ||  ePos == RP_BOTTOM)
     373             :         // correct error in current horizontal position
     374           0 :         switch (eHor)
     375             :         {   case RHA_LEFT :
     376           0 :                 aPos.X() += rRect.GetItalicLeft() - GetItalicLeft();
     377           0 :                 break;
     378             :             case RHA_CENTER :
     379           0 :                 aPos.X() += rRect.GetItalicCenterX() - GetItalicCenterX();
     380           0 :                 break;
     381             :             case RHA_RIGHT :
     382           0 :                 aPos.X() += rRect.GetItalicRight() - GetItalicRight();
     383           0 :                 break;
     384             :             default :
     385             :                 SAL_WARN("starmath", "unknown case");
     386             :         }
     387             : 
     388           0 :     return aPos;
     389             : }
     390             : 
     391             : 
     392           0 : 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           0 :     if (rRect.IsEmpty())
     399           0 :         return *this;
     400             : 
     401           0 :     long  nL  = rRect.GetLeft(),
     402           0 :           nR  = rRect.GetRight(),
     403           0 :           nT  = rRect.GetTop(),
     404           0 :           nB  = rRect.GetBottom(),
     405           0 :           nGT = rRect.nGlyphTop,
     406           0 :           nGB = rRect.nGlyphBottom;
     407           0 :     if (!IsEmpty())
     408             :     {   long  nTmp;
     409             : 
     410           0 :         if ((nTmp = GetLeft()) < nL)
     411           0 :             nL = nTmp;
     412           0 :         if ((nTmp = GetRight()) > nR)
     413           0 :             nR = nTmp;
     414           0 :         if ((nTmp = GetTop()) < nT)
     415           0 :             nT = nTmp;
     416           0 :         if ((nTmp = GetBottom()) > nB)
     417           0 :             nB = nTmp;
     418           0 :         if ((nTmp = nGlyphTop) < nGT)
     419           0 :             nGT = nTmp;
     420           0 :         if ((nTmp = nGlyphBottom) > nGB)
     421           0 :             nGB = nTmp;
     422             :     }
     423             : 
     424           0 :     SetLeft(nL);
     425           0 :     SetRight(nR);
     426           0 :     SetTop(nT);
     427           0 :     SetBottom(nB);
     428           0 :     nGlyphTop    = nGT;
     429           0 :     nGlyphBottom = nGB;
     430             : 
     431           0 :     return *this;
     432             : }
     433             : 
     434             : 
     435           0 : 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           0 :     long  nL = std::min(GetItalicLeft(),  rRect.GetItalicLeft()),
     445           0 :           nR = std::max(GetItalicRight(), rRect.GetItalicRight());
     446             : 
     447           0 :     Union(rRect);
     448             : 
     449           0 :     SetItalicSpaces(GetLeft() - nL, nR - GetRight());
     450             : 
     451           0 :     if (!HasAlignInfo())
     452           0 :         CopyAlignInfo(rRect);
     453           0 :     else if (rRect.HasAlignInfo())
     454           0 :     {   nAlignT = std::min(GetAlignT(), rRect.GetAlignT());
     455           0 :         nAlignB = std::max(GetAlignB(), rRect.GetAlignB());
     456           0 :         nHiAttrFence = std::min(GetHiAttrFence(), rRect.GetHiAttrFence());
     457           0 :         nLoAttrFence = std::max(GetLoAttrFence(), rRect.GetLoAttrFence());
     458             :         OSL_ENSURE(HasAlignInfo(), "Sm: ooops...");
     459             : 
     460           0 :         switch (eCopyMode)
     461             :         {   case RCP_THIS:
     462             :                 // already done
     463           0 :                 break;
     464             :             case RCP_ARG:
     465           0 :                 CopyMBL(rRect);
     466           0 :                 break;
     467             :             case RCP_NONE:
     468           0 :                 ClearBaseline();
     469           0 :                 nAlignM = (nAlignT + nAlignB) / 2;
     470           0 :                 break;
     471             :             case RCP_XOR:
     472           0 :                 if (!HasBaseline())
     473           0 :                     CopyMBL(rRect);
     474           0 :                 break;
     475             :             default :
     476             :                 SAL_WARN("starmath", "unknown case");
     477             :         }
     478             :     }
     479             : 
     480           0 :     return *this;
     481             : }
     482             : 
     483             : 
     484           0 : 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           0 :     ExtendBy(rRect, eCopyMode);
     494           0 :     nAlignM = nNewAlignM;
     495             : 
     496           0 :     return *this;
     497             : }
     498             : 
     499             : 
     500           0 : 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           0 :     long  nOldAlignT   = GetAlignT(),
     508           0 :           nOldAlignM   = GetAlignM(),
     509           0 :           nOldAlignB   = GetAlignB(),
     510           0 :           nOldBaseline = nBaseline;     //! depends not on 'HasBaseline'
     511           0 :     bool  bOldHasAlignInfo = HasAlignInfo();
     512             : 
     513           0 :     ExtendBy(rRect, eCopyMode);
     514             : 
     515           0 :     if (bKeepVerAlignParams)
     516           0 :     {   nAlignT   = nOldAlignT;
     517           0 :         nAlignM   = nOldAlignM;
     518           0 :         nAlignB   = nOldAlignB;
     519           0 :         nBaseline = nOldBaseline;
     520           0 :         bHasAlignInfo = bOldHasAlignInfo;
     521             :     }
     522             : 
     523           0 :     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           0 : 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           0 :     if (rText.isEmpty())
     603             :     {
     604           0 :         rRect.SetEmpty();
     605           0 :         return true;
     606             :     }
     607             : 
     608             :     // get a device where 'OutputDevice::GetTextBoundRect' will be successful
     609             :     OutputDevice *pGlyphDev;
     610           0 :     if (rDev.GetOutDevType() != OUTDEV_PRINTER)
     611           0 :         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           0 :         pGlyphDev = &SM_MOD()->GetDefaultVirtualDev();
     617             :     }
     618             : 
     619           0 :     const FontMetric  aDevFM (rDev.GetFontMetric());
     620             : 
     621           0 :     pGlyphDev->Push(PUSH_FONT | PUSH_MAPMODE);
     622           0 :     Font aFnt(rDev.GetFont());
     623           0 :     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           0 :     Size aFntSize = aFnt.GetSize();
     629             : 
     630             :     // Workaround to avoid HUGE font sizes and resulting problems
     631           0 :     long nScaleFactor = 1;
     632           0 :     while( aFntSize.Height() > 2000 * nScaleFactor )
     633           0 :         nScaleFactor *= 2;
     634             : 
     635           0 :     aFnt.SetSize( Size( aFntSize.Width() / nScaleFactor, aFntSize.Height() / nScaleFactor ) );
     636           0 :     pGlyphDev->SetFont(aFnt);
     637             : 
     638           0 :     long nTextWidth = rDev.GetTextWidth(rText);
     639           0 :     Point aPoint;
     640           0 :     Rectangle   aResult (aPoint, Size(nTextWidth, rDev.GetTextHeight())),
     641           0 :                 aTmp;
     642             : 
     643           0 :     bool bSuccess = pGlyphDev->GetTextBoundRect(aTmp, rText, 0, 0);
     644             :     OSL_ENSURE( bSuccess, "GetTextBoundRect failed" );
     645             : 
     646             : 
     647           0 :     if (!aTmp.IsEmpty())
     648             :     {
     649           0 :         aResult = Rectangle(aTmp.Left() * nScaleFactor, aTmp.Top() * nScaleFactor,
     650           0 :                             aTmp.Right() * nScaleFactor, aTmp.Bottom() * nScaleFactor);
     651           0 :         if (&rDev != pGlyphDev) /* only when rDev is a printer... */
     652             :         {
     653           0 :             long nGDTextWidth  = pGlyphDev->GetTextWidth(rText);
     654           0 :             if (nGDTextWidth != 0  &&
     655             :                 nTextWidth != nGDTextWidth)
     656             :             {
     657           0 :                 aResult.Right() *= nTextWidth;
     658           0 :                 aResult.Right() /= nGDTextWidth * nScaleFactor;
     659             :             }
     660             :         }
     661             :     }
     662             : 
     663             :     // move rectangle to match possibly different baselines
     664             :     // (because of different devices)
     665           0 :     long nDelta = aDevFM.GetAscent() - pGlyphDev->GetFontMetric().GetAscent() * nScaleFactor;
     666           0 :     aResult.Move(0, nDelta);
     667             : 
     668           0 :     pGlyphDev->Pop();
     669             : 
     670           0 :     rRect = aResult;
     671           0 :     return bSuccess;
     672             : }
     673             : 
     674             : 
     675             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10