bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / styles / CommonStylePreviewRenderer.cxx
blobfefd43b55d57a0bb685d2ebf819efe260ed27bcc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <svx/CommonStylePreviewRenderer.hxx>
13 #include <sfx2/objsh.hxx>
14 #include <svl/style.hxx>
15 #include <svl/itemset.hxx>
17 #include <com/sun/star/drawing/FillStyle.hpp>
18 #include <svx/xdef.hxx>
19 #include <svx/xfillit0.hxx>
20 #include <svx/xflclit.hxx>
21 #include <svx/xcolit.hxx>
22 #include <editeng/fontitem.hxx>
23 #include <editeng/fhgtitem.hxx>
24 #include <editeng/boxitem.hxx>
25 #include <editeng/charreliefitem.hxx>
26 #include <editeng/contouritem.hxx>
27 #include <editeng/colritem.hxx>
28 #include <editeng/crossedoutitem.hxx>
29 #include <editeng/emphasismarkitem.hxx>
30 #include <editeng/flstitem.hxx>
31 #include <editeng/lineitem.hxx>
32 #include <editeng/postitem.hxx>
33 #include <editeng/shdditem.hxx>
34 #include <editeng/udlnitem.hxx>
35 #include <editeng/wghtitem.hxx>
36 #include <editeng/svxfont.hxx>
37 #include <editeng/cmapitem.hxx>
39 #include <editeng/editids.hrc>
41 using namespace css;
43 namespace svx
46 CommonStylePreviewRenderer::CommonStylePreviewRenderer(
47 const SfxObjectShell& rShell, OutputDevice& rOutputDev,
48 SfxStyleSheetBase* pStyle, long nMaxHeight)
49 : StylePreviewRenderer(rShell, rOutputDev, pStyle, nMaxHeight)
50 , m_pFont()
51 , maFontColor(COL_AUTO)
52 , maBackgroundColor(COL_AUTO)
53 , maPixelSize()
54 , maStyleName(mpStyle->GetName())
58 CommonStylePreviewRenderer::~CommonStylePreviewRenderer()
61 bool CommonStylePreviewRenderer::recalculate()
63 m_pFont.reset();
65 std::unique_ptr<SfxItemSet> pItemSet(mpStyle->GetItemSetForPreview());
67 if (!pItemSet) return false;
69 std::unique_ptr<SvxFont> pFont(new SvxFont);
71 const SfxPoolItem* pItem;
73 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
75 pFont->SetWeight(static_cast<const SvxWeightItem*>(pItem)->GetWeight());
77 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
79 pFont->SetItalic(static_cast<const SvxPostureItem*>(pItem)->GetPosture());
81 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
83 pFont->SetOutline(static_cast< const SvxContourItem*>(pItem)->GetValue());
85 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
87 pFont->SetShadow(static_cast<const SvxShadowedItem*>(pItem)->GetValue());
89 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
91 pFont->SetRelief(static_cast<FontRelief>(static_cast<const SvxCharReliefItem*>(pItem)->GetValue()));
93 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
95 pFont->SetUnderline(static_cast< const SvxUnderlineItem*>(pItem)->GetLineStyle());
97 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
99 pFont->SetOverline(static_cast<FontUnderline>(static_cast<const SvxOverlineItem*>(pItem)->GetValue()));
101 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
103 pFont->SetStrikeout(static_cast<const SvxCrossedOutItem*>(pItem)->GetStrikeout());
105 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
107 pFont->SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
109 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
111 pFont->SetEmphasisMark(static_cast<const SvxEmphasisMarkItem*>(pItem)->GetEmphasisMark());
113 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_COLOR)) != nullptr)
115 maFontColor = Color(static_cast<const SvxColorItem*>(pItem)->GetValue());
118 if (mpStyle->GetFamily() == SFX_STYLE_FAMILY_PARA)
120 if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
122 sal_uInt16 aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
123 if (aFillStyle == drawing::FillStyle_SOLID)
125 if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
127 maBackgroundColor = Color(static_cast<const XFillColorItem*>(pItem)->GetColorValue());
133 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
135 const SvxFontItem* pFontItem = static_cast<const SvxFontItem*>(pItem);
136 pFont->SetName(pFontItem->GetFamilyName());
137 pFont->SetStyleName(pFontItem->GetStyleName());
139 else
141 return false;
144 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
146 const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
147 Size aFontSize(0, pFontHeightItem->GetHeight());
148 maPixelSize = Size(mrOutputDev.LogicToPixel(aFontSize, mrShell.GetMapUnit()));
149 pFont->SetSize(maPixelSize);
151 vcl::Font aOldFont(mrOutputDev.GetFont());
153 mrOutputDev.SetFont(*pFont);
154 Rectangle aTextRect;
155 mrOutputDev.GetTextBoundRect(aTextRect, mpStyle->GetName());
156 if (aTextRect.Bottom() > mnMaxHeight)
158 double ratio = double(mnMaxHeight) / aTextRect.Bottom();
159 maPixelSize.Width() *= ratio;
160 maPixelSize.Height() *= ratio;
161 pFont->SetSize(maPixelSize);
163 mrOutputDev.SetFont(aOldFont);
165 else
167 return false;
170 m_pFont = std::move(pFont);
171 return true;
174 Size CommonStylePreviewRenderer::getRenderSize()
176 assert(m_pFont);
177 maPixelSize = m_pFont->GetTextSize(&mrOutputDev, maStyleName);
178 if (maPixelSize.Height() > mnMaxHeight)
179 maPixelSize.Height() = mnMaxHeight;
180 return maPixelSize;
183 bool CommonStylePreviewRenderer::render(const Rectangle& aRectangle)
185 // setup the device & draw
186 vcl::Font aOldFont(mrOutputDev.GetFont());
187 Color aOldColor(mrOutputDev.GetTextColor());
188 Color aOldFillColor(mrOutputDev.GetFillColor());
190 if (maBackgroundColor != COL_AUTO)
192 mrOutputDev.SetFillColor(maBackgroundColor);
193 mrOutputDev.DrawRect(aRectangle);
196 if (m_pFont)
198 mrOutputDev.SetFont(*m_pFont);
200 if (maFontColor != COL_AUTO)
201 mrOutputDev.SetTextColor(maFontColor);
203 Size aPixelSize((m_pFont) ? maPixelSize : mrOutputDev.GetFont().GetSize());
205 Point aFontDrawPosition = aRectangle.TopLeft();
206 if (aRectangle.GetHeight() > aPixelSize.Height())
207 aFontDrawPosition.Y() += (aRectangle.GetHeight() - aPixelSize.Height()) / 2;
209 mrOutputDev.DrawText(aFontDrawPosition, maStyleName);
211 mrOutputDev.SetFillColor(aOldFillColor);
212 mrOutputDev.SetTextColor(aOldColor);
213 mrOutputDev.SetFont(aOldFont);
215 return true;
218 } // end svx namespace
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */