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 "SlsFramePainter.hxx"
21 #include <vcl/outdev.hxx>
22 #include <osl/diagnose.h>
23 
24 namespace sd::slidesorter::view {
25 
FramePainter(const BitmapEx & rShadowBitmap)26 FramePainter::FramePainter (const BitmapEx& rShadowBitmap)
27     : maTopLeft(rShadowBitmap,-1,-1),
28       maTop(rShadowBitmap,0,-1),
29       maTopRight(rShadowBitmap,+1,-1),
30       maLeft(rShadowBitmap,-1,0),
31       maRight(rShadowBitmap,+1,0),
32       maBottomLeft(rShadowBitmap,-1,+1),
33       maBottom(rShadowBitmap,0,+1),
34       maBottomRight(rShadowBitmap,+1,+1),
35       maCenter(rShadowBitmap,0,0),
36       mbIsValid(false)
37 {
38     if (rShadowBitmap.GetSizePixel().Width() == rShadowBitmap.GetSizePixel().Height()
39         && (rShadowBitmap.GetSizePixel().Width()-1)%2 == 0
40         && ((rShadowBitmap.GetSizePixel().Width()-1)/2)%2 == 1)
41     {
42         mbIsValid = true;
43     }
44     else
45     {
46         OSL_ASSERT(rShadowBitmap.GetSizePixel().Width() == rShadowBitmap.GetSizePixel().Height());
47         OSL_ASSERT((rShadowBitmap.GetSizePixel().Width()-1)%2 == 0);
48         OSL_ASSERT(((rShadowBitmap.GetSizePixel().Width()-1)/2)%2 == 1);
49     }
50 }
51 
~FramePainter()52 FramePainter::~FramePainter()
53 {
54 }
55 
PaintFrame(OutputDevice & rDevice,const::tools::Rectangle & rBox) const56 void FramePainter::PaintFrame (
57     OutputDevice& rDevice,
58     const ::tools::Rectangle& rBox) const
59 {
60     if ( ! mbIsValid)
61         return;
62 
63     // Paint the shadow.
64     maTopLeft.PaintCorner(rDevice, rBox.TopLeft());
65     maTopRight.PaintCorner(rDevice, rBox.TopRight());
66     maBottomLeft.PaintCorner(rDevice, rBox.BottomLeft());
67     maBottomRight.PaintCorner(rDevice, rBox.BottomRight());
68     maLeft.PaintSide(rDevice, rBox.TopLeft(), rBox.BottomLeft(), maTopLeft, maBottomLeft);
69     maRight.PaintSide(rDevice, rBox.TopRight(), rBox.BottomRight(), maTopRight, maBottomRight);
70     maTop.PaintSide(rDevice, rBox.TopLeft(), rBox.TopRight(), maTopLeft, maTopRight);
71     maBottom.PaintSide(rDevice, rBox.BottomLeft(), rBox.BottomRight(), maBottomLeft, maBottomRight);
72     maCenter.PaintCenter(rDevice,rBox);
73 }
74 
AdaptColor(const Color aNewColor)75 void FramePainter::AdaptColor (
76     const Color aNewColor)
77 {
78     // Get the source color.
79     if (maCenter.maBitmap.IsEmpty())
80         return;
81     const Color aSourceColor = maCenter.maBitmap.GetPixelColor(0,0);
82 
83     // Erase the center bitmap.
84     maCenter.maBitmap.SetEmpty();
85 
86     // Replace the color in all bitmaps.
87     maTopLeft.maBitmap.Replace(aSourceColor, aNewColor);
88     maTop.maBitmap.Replace(aSourceColor, aNewColor);
89     maTopRight.maBitmap.Replace(aSourceColor, aNewColor);
90     maLeft.maBitmap.Replace(aSourceColor, aNewColor);
91     maCenter.maBitmap.Replace(aSourceColor, aNewColor);
92     maRight.maBitmap.Replace(aSourceColor, aNewColor);
93     maBottomLeft.maBitmap.Replace(aSourceColor, aNewColor);
94     maBottom.maBitmap.Replace(aSourceColor, aNewColor);
95     maBottomRight.maBitmap.Replace(aSourceColor, aNewColor);
96 }
97 
98 //===== FramePainter::OffsetBitmap ============================================
99 
OffsetBitmap(const BitmapEx & rBitmap,const sal_Int32 nHorizontalPosition,const sal_Int32 nVerticalPosition)100 FramePainter::OffsetBitmap::OffsetBitmap (
101     const BitmapEx& rBitmap,
102     const sal_Int32 nHorizontalPosition,
103     const sal_Int32 nVerticalPosition)
104     : maBitmap(),
105       maOffset()
106 {
107     OSL_ASSERT(nHorizontalPosition>=-1 && nHorizontalPosition<=+1);
108     OSL_ASSERT(nVerticalPosition>=-1 && nVerticalPosition<=+1);
109 
110     const sal_Int32 nS (1);
111     const sal_Int32 nC (::std::max<sal_Int32>(0,(rBitmap.GetSizePixel().Width()-nS)/2));
112     const sal_Int32 nO (nC/2);
113 
114     const Point aOrigin(
115         nHorizontalPosition<0 ? 0 : (nHorizontalPosition == 0 ? nC : nC+nS),
116         nVerticalPosition<0 ? 0 : (nVerticalPosition == 0 ? nC : nC+nS));
117     const Size aSize(
118         nHorizontalPosition==0 ? nS : nC,
119         nVerticalPosition==0 ? nS : nC);
120     maBitmap = BitmapEx(rBitmap, aOrigin, aSize);
121     if (maBitmap.IsEmpty())
122         return;
123     maOffset = Point(
124         nHorizontalPosition<0 ? -nO : nHorizontalPosition>0 ? -nO : 0,
125         nVerticalPosition<0 ? -nO : nVerticalPosition>0 ? -nO : 0);
126 
127     // Enlarge the side bitmaps so that painting the frame requires less
128     // paint calls.
129     const sal_Int32 nSideBitmapSize (64);
130     if (nHorizontalPosition == 0 && nVerticalPosition == 0)
131     {
132         maBitmap.Scale(Size(nSideBitmapSize,nSideBitmapSize));
133     }
134     else if (nHorizontalPosition == 0)
135     {
136         maBitmap.Scale(Size(nSideBitmapSize,aSize.Height()));
137     }
138     else if (nVerticalPosition == 0)
139     {
140         maBitmap.Scale(Size(maBitmap.GetSizePixel().Width(), nSideBitmapSize));
141     }
142 }
143 
PaintCorner(OutputDevice & rDevice,const Point & rAnchor) const144 void FramePainter::OffsetBitmap::PaintCorner (
145     OutputDevice& rDevice,
146     const Point& rAnchor) const
147 {
148     if ( ! maBitmap.IsEmpty())
149         rDevice.DrawBitmapEx(rAnchor+maOffset, maBitmap);
150 }
151 
PaintSide(OutputDevice & rDevice,const Point & rAnchor1,const Point & rAnchor2,const OffsetBitmap & rCornerBitmap1,const OffsetBitmap & rCornerBitmap2) const152 void FramePainter::OffsetBitmap::PaintSide (
153     OutputDevice& rDevice,
154     const Point& rAnchor1,
155     const Point& rAnchor2,
156     const OffsetBitmap& rCornerBitmap1,
157     const OffsetBitmap& rCornerBitmap2) const
158 {
159     if (maBitmap.IsEmpty())
160         return;
161 
162     const Size aBitmapSize (maBitmap.GetSizePixel());
163     if (rAnchor1.Y() == rAnchor2.Y())
164     {
165         // Side is horizontal.
166         const sal_Int32 nY (rAnchor1.Y() + maOffset.Y());
167         const sal_Int32 nLeft (
168             rAnchor1.X()
169             + rCornerBitmap1.maBitmap.GetSizePixel().Width()
170             + rCornerBitmap1.maOffset.X());
171         const sal_Int32 nRight (
172             rAnchor2.X()
173             + rCornerBitmap2.maOffset.X()
174             - 1);
175         for (sal_Int32 nX=nLeft; nX<=nRight; nX+=aBitmapSize.Width())
176         {
177             rDevice.DrawBitmapEx(
178                 Point(nX,nY),
179                 Size(std::min(aBitmapSize.Width(),static_cast<::tools::Long>(nRight-nX+1)),aBitmapSize.Height()),
180                 maBitmap);
181         }
182     }
183     else if (rAnchor1.X() == rAnchor2.X())
184     {
185         // Side is vertical.
186         const sal_Int32 nX (rAnchor1.X() + maOffset.X());
187         const sal_Int32 nTop (
188             rAnchor1.Y()
189             + rCornerBitmap1.maBitmap.GetSizePixel().Height()
190             + rCornerBitmap1.maOffset.Y());
191         const sal_Int32 nBottom (
192             rAnchor2.Y()
193             + rCornerBitmap2.maOffset.Y()
194             - 1);
195         for (sal_Int32 nY=nTop; nY<=nBottom; nY+=aBitmapSize.Height())
196         {
197             rDevice.DrawBitmapEx(
198                 Point(nX,nY),
199                 Size(aBitmapSize.Width(), std::min(aBitmapSize.Height(), static_cast<::tools::Long>(nBottom-nY+1))),
200                 maBitmap);
201         }
202     }
203     else
204     {
205         // Diagonal sides indicates an error.
206         OSL_ASSERT(false);
207     }
208 }
209 
PaintCenter(OutputDevice & rDevice,const::tools::Rectangle & rBox) const210 void FramePainter::OffsetBitmap::PaintCenter (
211     OutputDevice& rDevice,
212     const ::tools::Rectangle& rBox) const
213 {
214     const Size aBitmapSize (maBitmap.GetSizePixel());
215     for (::tools::Long nY=rBox.Top(); nY<=rBox.Bottom(); nY+=aBitmapSize.Height())
216         for (::tools::Long nX=rBox.Left(); nX<=rBox.Right(); nX+=aBitmapSize.Width())
217             rDevice.DrawBitmapEx(
218                 Point(nX,nY),
219                 Size(
220                     ::std::min(aBitmapSize.Width(), rBox.Right()-nX+1),
221                     std::min(aBitmapSize.Height(), rBox.Bottom()-nY+1)),
222                 maBitmap);
223 }
224 
225 } // end of namespace sd::slidesorter::view
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
228