LCOV - code coverage report
Current view: top level - libreoffice/starmath/source - rect.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 276 327 84.4 %
Date: 2012-12-27 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        2128 : bool SmIsMathAlpha(const rtl::OUString &rText)
      52             :     // true iff symbol (from StarMath Font) should be treated as letter
      53             : {
      54        2128 :     if (rText.isEmpty())
      55           6 :         return false;
      56             : 
      57             :     OSL_ENSURE(rText.getLength() == 1, "Sm : string must be exactly one character long");
      58        2122 :     sal_Unicode cChar = rText[0];
      59             : 
      60             :     // is it a greek symbol?
      61        2122 :     if (sal_Unicode(0xE0AC) <= cChar  &&  cChar <= sal_Unicode(0xE0D4))
      62           0 :         return true;
      63             :     else
      64             :     {
      65             :         // appears it in 'aMathAlpha'?
      66        2122 :         const sal_Unicode *pChar = aMathAlpha;
      67       53050 :         while (*pChar  &&  *pChar != cChar)
      68       48806 :             pChar++;
      69        2122 :         return *pChar != sal_Unicode('\0');
      70             :     }
      71             : }
      72             : 
      73             : 
      74             : ////////////////////////////////////////
      75             : //
      76             : // SmRect members
      77             : //
      78             : 
      79             : 
      80       11967 : 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       11967 :     bHasBaseline = bHasAlignInfo = false;
      87             :     nBaseline = nAlignT = nAlignM = nAlignB =
      88             :     nGlyphTop = nGlyphBottom =
      89             :     nItalicLeftSpace = nItalicRightSpace =
      90       11967 :     nLoAttrFence = nHiAttrFence = 0;
      91       11967 :     nBorderWidth = 0;
      92       11967 : }
      93             : 
      94             : 
      95       10447 : SmRect::SmRect(const SmRect &rRect)
      96             : :   aTopLeft(rRect.aTopLeft),
      97       10447 :     aSize(rRect.aSize)
      98             : {
      99       10447 :     bHasBaseline  = rRect.bHasBaseline;
     100       10447 :     nBaseline     = rRect.nBaseline;
     101       10447 :     nAlignT       = rRect.nAlignT;
     102       10447 :     nAlignM       = rRect.nAlignM;
     103       10447 :     nAlignB       = rRect.nAlignB;
     104       10447 :     nGlyphTop     = rRect.nGlyphTop;
     105       10447 :     nGlyphBottom  = rRect.nGlyphBottom;
     106       10447 :     nHiAttrFence  = rRect.nHiAttrFence;
     107       10447 :     nLoAttrFence  = rRect.nLoAttrFence;
     108       10447 :     bHasAlignInfo = rRect.bHasAlignInfo;
     109       10447 :     nItalicLeftSpace  = rRect.nItalicLeftSpace;
     110       10447 :     nItalicRightSpace = rRect.nItalicRightSpace;
     111       10447 :     nBorderWidth  = rRect.nBorderWidth;
     112       10447 : }
     113             : 
     114             : 
     115          60 : void SmRect::CopyAlignInfo(const SmRect &rRect)
     116             : {
     117          60 :     nBaseline     = rRect.nBaseline;
     118          60 :     bHasBaseline  = rRect.bHasBaseline;
     119          60 :     nAlignT       = rRect.nAlignT;
     120          60 :     nAlignM       = rRect.nAlignM;
     121          60 :     nAlignB       = rRect.nAlignB;
     122          60 :     bHasAlignInfo = rRect.bHasAlignInfo;
     123          60 :     nLoAttrFence  = rRect.nLoAttrFence;
     124          60 :     nHiAttrFence  = rRect.nHiAttrFence;
     125          60 : }
     126             : 
     127             : 
     128        4692 : void SmRect::BuildRect(const OutputDevice &rDev, const SmFormat *pFormat,
     129             :                        const rtl::OUString &rText, sal_uInt16 nBorder)
     130             : {
     131             :     OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: Ooops...");
     132             : 
     133        4692 :     aSize = Size(rDev.GetTextWidth(rText), rDev.GetTextHeight());
     134             : 
     135        4692 :     const FontMetric  aFM (rDev.GetFontMetric());
     136        4692 :     bool              bIsMath  = aFM.GetName().EqualsIgnoreCaseAscii( FONTNAME_MATH );
     137        4692 :     bool              bAllowSmaller = bIsMath && !SmIsMathAlpha(rText);
     138        4692 :     const long        nFontHeight = rDev.GetFont().GetSize().Height();
     139             : 
     140        4692 :     nBorderWidth  = nBorder;
     141        4692 :     bHasAlignInfo = true;
     142        4692 :     bHasBaseline  = true;
     143        4692 :     nBaseline     = aFM.GetAscent();
     144        4692 :     nAlignT       = nBaseline - nFontHeight * 750L / 1000L;
     145        4692 :     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        4692 :     nAlignB       = nBaseline;
     150             : 
     151             :     // workaround for printer fonts with very small (possible 0 or even
     152             :     // negative(!)) leading
     153        4692 :     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        4692 :     Rectangle  aGlyphRect;
     175             : #if OSL_DEBUG_LEVEL > 1
     176             :     bool bSuccess =
     177             : #endif
     178        4692 :                 SmGetGlyphBoundRect(rDev, rText, aGlyphRect);
     179             : #if OSL_DEBUG_LEVEL > 1
     180             :     if (!bSuccess)
     181             :     {
     182             :         OSL_FAIL( "Sm : Ooops... (fehlt evtl. der Font?)");
     183             :     }
     184             : #endif
     185             : 
     186        4692 :     nItalicLeftSpace  = GetLeft() - aGlyphRect.Left() + nBorderWidth;
     187        4692 :     nItalicRightSpace = aGlyphRect.Right() - GetRight() + nBorderWidth;
     188        4692 :     if (nItalicLeftSpace  < 0  &&  !bAllowSmaller)
     189         372 :         nItalicLeftSpace  = 0;
     190        4692 :     if (nItalicRightSpace < 0  &&  !bAllowSmaller)
     191         606 :         nItalicRightSpace = 0;
     192             : 
     193        4692 :     long  nDist = 0;
     194        4692 :     if (pFormat)
     195        4032 :         nDist = (rDev.GetFont().GetSize().Height()
     196        4032 :                 * pFormat->GetDistance(DIS_ORNAMENTSIZE)) / 100L;
     197             : 
     198        4692 :     nHiAttrFence = aGlyphRect.TopLeft().Y() - 1 - nBorderWidth - nDist;
     199        4692 :     nLoAttrFence = SmFromTo(GetAlignB(), GetBottom(), 0.0);
     200             : 
     201        4692 :     nGlyphTop    = aGlyphRect.Top() - nBorderWidth;
     202        4692 :     nGlyphBottom = aGlyphRect.Bottom() + nBorderWidth;
     203             : 
     204        4692 :     if (bAllowSmaller)
     205             :     {
     206             :         // for symbols and operators from the StarMath Font
     207             :         // we adjust upper and lower margin of the symbol
     208        2128 :         SetTop(nGlyphTop);
     209        2128 :         SetBottom(nGlyphBottom);
     210             :     }
     211             : 
     212        4692 :     if (nHiAttrFence < GetTop())
     213        2140 :         nHiAttrFence = GetTop();
     214             : 
     215        4692 :     if (nLoAttrFence > GetBottom())
     216         582 :         nLoAttrFence = GetBottom();
     217             : 
     218             :     OSL_ENSURE(rText.isEmpty() || !IsEmpty(),
     219        4692 :                "Sm: empty rectangle created");
     220        4692 : }
     221             : 
     222             : 
     223        4692 : void SmRect::Init(const OutputDevice &rDev, const SmFormat *pFormat,
     224             :                   const rtl::OUString &rText, sal_uInt16 nEBorderWidth)
     225             :     // get rectangle fitting for drawing 'rText' on OutputDevice 'rDev'
     226             : {
     227        4692 :     BuildRect(rDev, pFormat, rText, nEBorderWidth);
     228        4692 : }
     229             : 
     230             : 
     231        4692 : SmRect::SmRect(const OutputDevice &rDev, const SmFormat *pFormat,
     232        4692 :                const rtl::OUString &rText, long nEBorderWidth)
     233             : {
     234             :     OSL_ENSURE( nEBorderWidth >= 0, "BorderWidth is negative" );
     235        4692 :     if (nEBorderWidth < 0)
     236           0 :         nEBorderWidth = 0;
     237        4692 :     Init(rDev, pFormat, rText, (sal_uInt16) nEBorderWidth);
     238        4692 : }
     239             : 
     240             : 
     241         700 : SmRect::SmRect(long nWidth, long nHeight)
     242             :     // this constructor should never be used for anything textlike because
     243             :     // it will not provide useful values for baseline, AlignT and AlignB!
     244             :     // It's purpose is to get a 'SmRect' for the horizontal line in fractions
     245             :     // as used in 'SmBinVerNode'.
     246         700 : :   aSize(nWidth, nHeight)
     247             : {
     248             :     OSL_ENSURE(aTopLeft == Point(0, 0), "Sm: ooops...");
     249             : 
     250         700 :     bHasBaseline  = false;
     251         700 :     bHasAlignInfo = true;
     252         700 :     nBaseline     = 0;
     253         700 :     nAlignT       = GetTop();
     254         700 :     nAlignB       = GetBottom();
     255         700 :     nAlignM       = (nAlignT + nAlignB) / 2;        // this is the default
     256         700 :     nItalicLeftSpace = nItalicRightSpace = 0;
     257         700 :     nGlyphTop    = nHiAttrFence  = GetTop();
     258         700 :     nGlyphBottom = nLoAttrFence  = GetBottom();
     259         700 :     nBorderWidth  = 0;
     260         700 : }
     261             : 
     262             : 
     263        4380 : void SmRect::SetLeft(long nLeft)
     264             : {
     265        4380 :     if (nLeft <= GetRight())
     266        4344 :     {   aSize.Width() = GetRight() - nLeft + 1;
     267        4344 :         aTopLeft.X()  = nLeft;
     268             :     }
     269        4380 : }
     270             : 
     271             : 
     272        4380 : void SmRect::SetRight(long nRight)
     273             : {
     274        4380 :     if (nRight >= GetLeft())
     275        4380 :         aSize.Width() = nRight - GetLeft() + 1;
     276        4380 : }
     277             : 
     278             : 
     279        6514 : void SmRect::SetBottom(long nBottom)
     280             : {
     281        6514 :     if (nBottom >= GetTop())
     282        6508 :         aSize.Height() = nBottom - GetTop() + 1;
     283        6514 : }
     284             : 
     285             : 
     286        6508 : void SmRect::SetTop(long nTop)
     287             : {
     288        6508 :     if (nTop <= GetBottom())
     289        6496 :     {   aSize.Height()   = GetBottom() - nTop + 1;
     290        6496 :         aTopLeft.Y() = nTop;
     291             :     }
     292        6508 : }
     293             : 
     294             : 
     295       22519 : void SmRect::Move(const Point &rPosition)
     296             :     // move rectangle by position 'rPosition'.
     297             : {
     298       22519 :     aTopLeft  += rPosition;
     299             : 
     300       22519 :     long  nDelta = rPosition.Y();
     301       22519 :     nBaseline += nDelta;
     302       22519 :     nAlignT   += nDelta;
     303       22519 :     nAlignM   += nDelta;
     304       22519 :     nAlignB   += nDelta;
     305       22519 :     nGlyphTop    += nDelta;
     306       22519 :     nGlyphBottom += nDelta;
     307       22519 :     nHiAttrFence += nDelta;
     308       22519 :     nLoAttrFence += nDelta;
     309       22519 : }
     310             : 
     311             : 
     312        4578 : const Point SmRect::AlignTo(const SmRect &rRect, RectPos ePos,
     313             :                             RectHorAlign eHor, RectVerAlign eVer) const
     314        4578 : {   Point  aPos (GetTopLeft());
     315             :         // will become the topleft point of the new rectangle position
     316             : 
     317             :     // set horizontal or vertical new rectangle position depending on
     318             :     // 'ePos' is one of 'RP_LEFT', 'RP_RIGHT' or 'RP_TOP', 'RP_BOTTOM'
     319        4578 :     switch (ePos)
     320             :     {   case RP_LEFT :
     321         447 :             aPos.X() = rRect.GetItalicLeft() - GetItalicRightSpace()
     322         447 :                        - GetWidth();
     323         447 :             break;
     324             :         case RP_RIGHT :
     325        2824 :             aPos.X() = rRect.GetItalicRight() + 1 + GetItalicLeftSpace();
     326        2824 :             break;
     327             :         case RP_TOP :
     328         268 :             aPos.Y() = rRect.GetTop() - GetHeight();
     329         268 :             break;
     330             :         case RP_BOTTOM :
     331         871 :             aPos.Y() = rRect.GetBottom() + 1;
     332         871 :             break;
     333             :         case RP_ATTRIBUT :
     334         168 :             aPos.X() = rRect.GetItalicCenterX() - GetItalicWidth() / 2
     335         168 :                        + GetItalicLeftSpace();
     336         168 :             break;
     337             :         default :
     338             :             OSL_FAIL("Sm: unknown case");
     339             :     }
     340             : 
     341             :     // check if horizontal position is already set
     342        4578 :     if (ePos == RP_LEFT  ||  ePos == RP_RIGHT  ||  ePos == RP_ATTRIBUT)
     343             :         // correct error in current vertical position
     344        3439 :         switch (eVer)
     345             :         {   case RVA_TOP :
     346         246 :                 aPos.Y() += rRect.GetAlignT() - GetAlignT();
     347         246 :                 break;
     348             :             case RVA_MID :
     349          87 :                 aPos.Y() += rRect.GetAlignM() - GetAlignM();
     350          87 :                 break;
     351             :             case RVA_BASELINE :
     352             :                 // align baselines if possible else align mid's
     353        2278 :                 if (HasBaseline() && rRect.HasBaseline())
     354        1996 :                     aPos.Y() += rRect.GetBaseline() - GetBaseline();
     355             :                 else
     356         282 :                     aPos.Y() += rRect.GetAlignM() - GetAlignM();
     357        2278 :                 break;
     358             :             case RVA_BOTTOM :
     359          96 :                 aPos.Y() += rRect.GetAlignB() - GetAlignB();
     360          96 :                 break;
     361             :             case RVA_CENTERY :
     362         564 :                 aPos.Y() += rRect.GetCenterY() - GetCenterY();
     363         564 :                 break;
     364             :             case RVA_ATTRIBUT_HI:
     365         144 :                 aPos.Y() += rRect.GetHiAttrFence() - GetBottom();
     366         144 :                 break;
     367             :             case RVA_ATTRIBUT_MID :
     368          24 :                 aPos.Y() += SmFromTo(rRect.GetAlignB(), rRect.GetAlignT(), 0.4)
     369          24 :                             - GetCenterY();
     370          12 :                 break;
     371             :             case RVA_ATTRIBUT_LO :
     372          12 :                 aPos.Y() += rRect.GetLoAttrFence() - GetTop();
     373          12 :                 break;
     374             :         default :
     375             :                 OSL_FAIL("Sm: unknown case");
     376             :         }
     377             : 
     378             :     // check if vertical position is already set
     379        4578 :     if (ePos == RP_TOP  ||  ePos == RP_BOTTOM)
     380             :         // correct error in current horizontal position
     381        1139 :         switch (eHor)
     382             :         {   case RHA_LEFT :
     383           0 :                 aPos.X() += rRect.GetItalicLeft() - GetItalicLeft();
     384           0 :                 break;
     385             :             case RHA_CENTER :
     386        1139 :                 aPos.X() += rRect.GetItalicCenterX() - GetItalicCenterX();
     387        1139 :                 break;
     388             :             case RHA_RIGHT :
     389           0 :                 aPos.X() += rRect.GetItalicRight() - GetItalicRight();
     390           0 :                 break;
     391             :             default :
     392             :                 OSL_FAIL("Sm: unknown case");
     393             :         }
     394             : 
     395        4578 :     return aPos;
     396             : }
     397             : 
     398             : 
     399        4518 : SmRect & SmRect::Union(const SmRect &rRect)
     400             :     // rectangle union of current one with 'rRect'. The result is to be the
     401             :     // smallest rectangles that covers the space of both rectangles.
     402             :     // (empty rectangles cover no space)
     403             :     //! Italic correction is NOT taken into account here!
     404             : {
     405        4518 :     if (rRect.IsEmpty())
     406         138 :         return *this;
     407             : 
     408        4380 :     long  nL  = rRect.GetLeft(),
     409        4380 :           nR  = rRect.GetRight(),
     410        4380 :           nT  = rRect.GetTop(),
     411        4380 :           nB  = rRect.GetBottom(),
     412        4380 :           nGT = rRect.nGlyphTop,
     413        4380 :           nGB = rRect.nGlyphBottom;
     414        4380 :     if (!IsEmpty())
     415             :     {   long  nTmp;
     416             : 
     417        4320 :         if ((nTmp = GetLeft()) < nL)
     418        3525 :             nL = nTmp;
     419        4320 :         if ((nTmp = GetRight()) > nR)
     420        1095 :             nR = nTmp;
     421        4320 :         if ((nTmp = GetTop()) < nT)
     422        2156 :             nT = nTmp;
     423        4320 :         if ((nTmp = GetBottom()) > nB)
     424        1522 :             nB = nTmp;
     425        4320 :         if ((nTmp = nGlyphTop) < nGT)
     426        2342 :             nGT = nTmp;
     427        4320 :         if ((nTmp = nGlyphBottom) > nGB)
     428        1758 :             nGB = nTmp;
     429             :     }
     430             : 
     431        4380 :     SetLeft(nL);
     432        4380 :     SetRight(nR);
     433        4380 :     SetTop(nT);
     434        4380 :     SetBottom(nB);
     435        4380 :     nGlyphTop    = nGT;
     436        4380 :     nGlyphBottom = nGB;
     437             : 
     438        4380 :     return *this;
     439             : }
     440             : 
     441             : 
     442        4518 : SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode)
     443             :     // let current rectangle be the union of itself and 'rRect'
     444             :     // (the smallest rectangle surrounding both). Also adapt values for
     445             :     // 'AlignT', 'AlignM', 'AlignB', baseline and italic-spaces.
     446             :     // The baseline is set according to 'eCopyMode'.
     447             :     // If one of the rectangles has no relevant info the other one is copied.
     448             : {
     449             :     // get some values used for (italic) spaces adaption
     450             :     // ! (need to be done before changing current SmRect) !
     451        4518 :     long  nL = Min(GetItalicLeft(),  rRect.GetItalicLeft()),
     452        4518 :           nR = Max(GetItalicRight(), rRect.GetItalicRight());
     453             : 
     454        4518 :     Union(rRect);
     455             : 
     456        4518 :     SetItalicSpaces(GetLeft() - nL, nR - GetRight());
     457             : 
     458        4518 :     if (!HasAlignInfo())
     459          60 :         CopyAlignInfo(rRect);
     460        4458 :     else if (rRect.HasAlignInfo())
     461        4452 :     {   nAlignT = Min(GetAlignT(), rRect.GetAlignT());
     462        4452 :         nAlignB = Max(GetAlignB(), rRect.GetAlignB());
     463        4452 :         nHiAttrFence = Min(GetHiAttrFence(), rRect.GetHiAttrFence());
     464        4452 :         nLoAttrFence = Max(GetLoAttrFence(), rRect.GetLoAttrFence());
     465             :         OSL_ENSURE(HasAlignInfo(), "Sm: ooops...");
     466             : 
     467        4452 :         switch (eCopyMode)
     468             :         {   case RCP_THIS:
     469             :                 // already done
     470        1401 :                 break;
     471             :             case RCP_ARG:
     472         453 :                 CopyMBL(rRect);
     473         453 :                 break;
     474             :             case RCP_NONE:
     475         464 :                 ClearBaseline();
     476         464 :                 nAlignM = (nAlignT + nAlignB) / 2;
     477         464 :                 break;
     478             :             case RCP_XOR:
     479        2134 :                 if (!HasBaseline())
     480          90 :                     CopyMBL(rRect);
     481        2134 :                 break;
     482             :             default :
     483             :                 OSL_FAIL("Sm: unknown case");
     484             :         }
     485             :     }
     486             : 
     487        4518 :     return *this;
     488             : }
     489             : 
     490             : 
     491         169 : SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
     492             :                           long nNewAlignM)
     493             :     // as 'ExtendBy' but sets AlignM value to 'nNewAlignM'.
     494             :     // (this version will be used in 'SmBinVerNode' to provide means to
     495             :     // align eg "{a over b} over c" correctly where AlignM should not
     496             :     // be (AlignT + AlignB) / 2)
     497             : {
     498             :     OSL_ENSURE(HasAlignInfo(), "Sm: no align info");
     499             : 
     500         169 :     ExtendBy(rRect, eCopyMode);
     501         169 :     nAlignM = nNewAlignM;
     502             : 
     503         169 :     return *this;
     504             : }
     505             : 
     506             : 
     507         684 : SmRect & SmRect::ExtendBy(const SmRect &rRect, RectCopyMBL eCopyMode,
     508             :                           bool bKeepVerAlignParams)
     509             :     // as 'ExtendBy' but keeps original values for AlignT, -M and -B and
     510             :     // baseline.
     511             :     // (this is used in 'SmSupSubNode' where the sub-/supscripts shouldn't
     512             :     // be allowed to modify these values.)
     513             : {
     514         684 :     long  nOldAlignT   = GetAlignT(),
     515         684 :           nOldAlignM   = GetAlignM(),
     516         684 :           nOldAlignB   = GetAlignB(),
     517         684 :           nOldBaseline = nBaseline;     //! depends not on 'HasBaseline'
     518         684 :     bool  bOldHasAlignInfo = HasAlignInfo();
     519             : 
     520         684 :     ExtendBy(rRect, eCopyMode);
     521             : 
     522         684 :     if (bKeepVerAlignParams)
     523         684 :     {   nAlignT   = nOldAlignT;
     524         684 :         nAlignM   = nOldAlignM;
     525         684 :         nAlignB   = nOldAlignB;
     526         684 :         nBaseline = nOldBaseline;
     527         684 :         bHasAlignInfo = bOldHasAlignInfo;
     528             :     }
     529             : 
     530         684 :     return *this;
     531             : }
     532             : 
     533             : 
     534           0 : long SmRect::OrientedDist(const Point &rPoint) const
     535             :     // return oriented distance of rPoint to the current rectangle,
     536             :     // especially the return value is <= 0 iff the point is inside the
     537             :     // rectangle.
     538             :     // For simplicity the maximum-norm is used.
     539             : {
     540           0 :     bool  bIsInside = IsInsideItalicRect(rPoint);
     541             : 
     542             :     // build reference point to define the distance
     543           0 :     Point  aRef;
     544           0 :     if (bIsInside)
     545           0 :     {   Point  aIC (GetItalicCenterX(), GetCenterY());
     546             : 
     547           0 :         aRef.X() = rPoint.X() >= aIC.X() ? GetItalicRight() : GetItalicLeft();
     548           0 :         aRef.Y() = rPoint.Y() >= aIC.Y() ? GetBottom() : GetTop();
     549             :     }
     550             :     else
     551             :     {
     552             :         // x-coordinate
     553           0 :         if (rPoint.X() > GetItalicRight())
     554           0 :             aRef.X() = GetItalicRight();
     555           0 :         else if (rPoint.X() < GetItalicLeft())
     556           0 :             aRef.X() = GetItalicLeft();
     557             :         else
     558           0 :             aRef.X() = rPoint.X();
     559             :         // y-coordinate
     560           0 :         if (rPoint.Y() > GetBottom())
     561           0 :             aRef.Y() = GetBottom();
     562           0 :         else if (rPoint.Y() < GetTop())
     563           0 :             aRef.Y() = GetTop();
     564             :         else
     565           0 :             aRef.Y() = rPoint.Y();
     566             :     }
     567             : 
     568             :     // build distance vector
     569           0 :     Point  aDist (aRef - rPoint);
     570             : 
     571           0 :     long nAbsX = labs(aDist.X()),
     572           0 :          nAbsY = labs(aDist.Y());
     573             : 
     574           0 :     return bIsInside ? - Min(nAbsX, nAbsY) : Max (nAbsX, nAbsY);
     575             : }
     576             : 
     577             : 
     578           0 : bool SmRect::IsInsideRect(const Point &rPoint) const
     579             : {
     580           0 :     return     rPoint.Y() >= GetTop()
     581           0 :            &&  rPoint.Y() <= GetBottom()
     582           0 :            &&  rPoint.X() >= GetLeft()
     583           0 :            &&  rPoint.X() <= GetRight();
     584             : }
     585             : 
     586             : 
     587           0 : bool SmRect::IsInsideItalicRect(const Point &rPoint) const
     588             : {
     589           0 :     return     rPoint.Y() >= GetTop()
     590           0 :            &&  rPoint.Y() <= GetBottom()
     591           0 :            &&  rPoint.X() >= GetItalicLeft()
     592           0 :            &&  rPoint.X() <= GetItalicRight();
     593             : }
     594             : 
     595           0 : SmRect SmRect::AsGlyphRect() const
     596             : {
     597           0 :     SmRect aRect (*this);
     598           0 :     aRect.SetTop(nGlyphTop);
     599           0 :     aRect.SetBottom(nGlyphBottom);
     600           0 :     return aRect;
     601             : }
     602             : 
     603        4692 : bool SmGetGlyphBoundRect(const OutputDevice &rDev,
     604             :                          const rtl::OUString &rText, Rectangle &rRect)
     605             :     // basically the same as 'GetTextBoundRect' (in class 'OutputDevice')
     606             :     // but with a string as argument.
     607             : {
     608             :     // handle special case first
     609        4692 :     if (rText.isEmpty())
     610             :     {
     611           6 :         rRect.SetEmpty();
     612           6 :         return true;
     613             :     }
     614             : 
     615             :     // get a device where 'OutputDevice::GetTextBoundRect' will be successful
     616             :     OutputDevice *pGlyphDev;
     617        4686 :     if (rDev.GetOutDevType() != OUTDEV_PRINTER)
     618        4672 :         pGlyphDev = (OutputDevice *) &rDev;
     619             :     else
     620             :     {
     621             :         // since we format for the printer (where GetTextBoundRect will fail)
     622             :         // we need a virtual device here.
     623          14 :         pGlyphDev = &SM_MOD()->GetDefaultVirtualDev();
     624             :     }
     625             : 
     626        4686 :     const FontMetric  aDevFM (rDev.GetFontMetric());
     627             : 
     628        4686 :     pGlyphDev->Push(PUSH_FONT | PUSH_MAPMODE);
     629        4686 :     Font aFnt(rDev.GetFont());
     630        4686 :     aFnt.SetAlign(ALIGN_TOP);
     631             : 
     632             :     // use scale factor when calling GetTextBoundRect to counter
     633             :     // negative effects from antialiasing which may otherwise result
     634             :     // in significant incorrect bounding rectangles for some charcters.
     635        4686 :     Size aFntSize = aFnt.GetSize();
     636             : 
     637             :     // Workaround to avoid HUGE font sizes and resulting problems
     638        4686 :     long nScaleFactor = 1;
     639        9414 :     while( aFntSize.Height() > 2000 * nScaleFactor )
     640          42 :         nScaleFactor *= 2;
     641             : 
     642        4686 :     aFnt.SetSize( Size( aFntSize.Width() / nScaleFactor, aFntSize.Height() / nScaleFactor ) );
     643        4686 :     pGlyphDev->SetFont(aFnt);
     644             : 
     645        4686 :     long nTextWidth = rDev.GetTextWidth(rText);
     646        4686 :     Point aPoint;
     647        4686 :     Rectangle   aResult (aPoint, Size(nTextWidth, rDev.GetTextHeight())),
     648        4686 :                 aTmp;
     649             : 
     650        4686 :     bool bSuccess = pGlyphDev->GetTextBoundRect(aTmp, rText, 0, 0);
     651             :     OSL_ENSURE( bSuccess, "GetTextBoundRect failed" );
     652             : 
     653             : 
     654        4686 :     if (!aTmp.IsEmpty())
     655             :     {
     656        7758 :         aResult = Rectangle(aTmp.Left() * nScaleFactor, aTmp.Top() * nScaleFactor,
     657       11637 :                             aTmp.Right() * nScaleFactor, aTmp.Bottom() * nScaleFactor);
     658        3879 :         if (&rDev != pGlyphDev) /* only when rDev is a printer... */
     659             :         {
     660          14 :             long nGDTextWidth  = pGlyphDev->GetTextWidth(rText);
     661          14 :             if (nGDTextWidth != 0  &&
     662             :                 nTextWidth != nGDTextWidth)
     663             :             {
     664          14 :                 aResult.Right() *= nTextWidth;
     665          14 :                 aResult.Right() /= nGDTextWidth * nScaleFactor;
     666             :             }
     667             :         }
     668             :     }
     669             : 
     670             :     // move rectangle to match possibly different baselines
     671             :     // (because of different devices)
     672        4686 :     long nDelta = aDevFM.GetAscent() - pGlyphDev->GetFontMetric().GetAscent() * nScaleFactor;
     673        4686 :     aResult.Move(0, nDelta);
     674             : 
     675        4686 :     pGlyphDev->Pop();
     676             : 
     677        4686 :     rRect = aResult;
     678        4686 :     return bSuccess;
     679             : }
     680             : 
     681             : 
     682             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10