Bump version to 6.4-15
[LibreOffice.git] / svx / source / styles / CommonStylePreviewRenderer.cxx
blob886200571d749482dfdacc1375faa442ab53cc13
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 <memory>
12 #include <svx/CommonStylePreviewRenderer.hxx>
14 #include <sfx2/objsh.hxx>
15 #include <svl/style.hxx>
16 #include <svl/itemset.hxx>
17 #include <vcl/outdev.hxx>
19 #include <com/sun/star/drawing/FillStyle.hpp>
20 #include <svx/xdef.hxx>
21 #include <svx/xfillit0.hxx>
22 #include <svx/xflclit.hxx>
23 #include <svx/xcolit.hxx>
24 #include <editeng/fontitem.hxx>
25 #include <editeng/fhgtitem.hxx>
26 #include <editeng/boxitem.hxx>
27 #include <editeng/charreliefitem.hxx>
28 #include <editeng/contouritem.hxx>
29 #include <editeng/colritem.hxx>
30 #include <editeng/crossedoutitem.hxx>
31 #include <editeng/emphasismarkitem.hxx>
32 #include <editeng/flstitem.hxx>
33 #include <editeng/lineitem.hxx>
34 #include <editeng/postitem.hxx>
35 #include <editeng/shdditem.hxx>
36 #include <editeng/udlnitem.hxx>
37 #include <editeng/wghtitem.hxx>
38 #include <editeng/svxfont.hxx>
39 #include <editeng/cmapitem.hxx>
41 #include <editeng/editids.hrc>
43 using namespace css;
45 namespace svx
48 CommonStylePreviewRenderer::CommonStylePreviewRenderer(
49 const SfxObjectShell& rShell, OutputDevice& rOutputDev,
50 SfxStyleSheetBase* pStyle, long nMaxHeight)
51 : StylePreviewRenderer(rShell, rOutputDev, pStyle, nMaxHeight)
52 , m_pFont()
53 , maFontColor(COL_AUTO)
54 , maBackgroundColor(COL_AUTO)
55 , maPixelSize()
56 , maStyleName(mpStyle->GetName())
60 CommonStylePreviewRenderer::~CommonStylePreviewRenderer()
63 bool CommonStylePreviewRenderer::recalculate()
65 m_pFont.reset();
67 std::unique_ptr<SfxItemSet> pItemSet(mpStyle->GetItemSetForPreview());
69 if (!pItemSet) return false;
71 std::unique_ptr<SvxFont> pFont(new SvxFont);
73 const SfxPoolItem* pItem;
75 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
77 pFont->SetWeight(static_cast<const SvxWeightItem*>(pItem)->GetWeight());
79 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
81 pFont->SetItalic(static_cast<const SvxPostureItem*>(pItem)->GetPosture());
83 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
85 pFont->SetOutline(static_cast< const SvxContourItem*>(pItem)->GetValue());
87 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
89 pFont->SetShadow(static_cast<const SvxShadowedItem*>(pItem)->GetValue());
91 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
93 pFont->SetRelief(static_cast<const SvxCharReliefItem*>(pItem)->GetValue());
95 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
97 pFont->SetUnderline(static_cast< const SvxUnderlineItem*>(pItem)->GetLineStyle());
99 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
101 pFont->SetOverline(static_cast<const SvxOverlineItem*>(pItem)->GetValue());
103 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
105 pFont->SetStrikeout(static_cast<const SvxCrossedOutItem*>(pItem)->GetStrikeout());
107 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
109 pFont->SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
111 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
113 pFont->SetEmphasisMark(static_cast<const SvxEmphasisMarkItem*>(pItem)->GetEmphasisMark());
115 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_COLOR)) != nullptr)
117 maFontColor = static_cast<const SvxColorItem*>(pItem)->GetValue();
120 if (mpStyle->GetFamily() == SfxStyleFamily::Para)
122 if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
124 css::drawing::FillStyle aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
125 if (aFillStyle == drawing::FillStyle_SOLID)
127 if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
129 maBackgroundColor = static_cast<const XFillColorItem*>(pItem)->GetColorValue();
135 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
137 const SvxFontItem* pFontItem = static_cast<const SvxFontItem*>(pItem);
138 if (IsStarSymbol(pFontItem->GetFamilyName()))
139 return false;
140 pFont->SetFamilyName(pFontItem->GetFamilyName());
141 pFont->SetStyleName(pFontItem->GetStyleName());
143 else
145 return false;
148 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
150 const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
151 Size aFontSize(0, pFontHeightItem->GetHeight());
152 maPixelSize = mrOutputDev.LogicToPixel(aFontSize, MapMode(mrShell.GetMapUnit()));
153 pFont->SetFontSize(maPixelSize);
155 vcl::Font aOldFont(mrOutputDev.GetFont());
157 mrOutputDev.SetFont(*pFont);
158 tools::Rectangle aTextRect;
159 mrOutputDev.GetTextBoundRect(aTextRect, mpStyle->GetName());
160 if (aTextRect.Bottom() > mnMaxHeight)
162 double ratio = double(mnMaxHeight) / aTextRect.Bottom();
163 maPixelSize.setWidth( maPixelSize.Width() * ratio );
164 maPixelSize.setHeight( maPixelSize.Height() * ratio );
165 pFont->SetFontSize(maPixelSize);
167 mrOutputDev.SetFont(aOldFont);
169 else
171 return false;
174 m_pFont = std::move(pFont);
175 return true;
178 Size CommonStylePreviewRenderer::getRenderSize()
180 assert(m_pFont);
181 maPixelSize = m_pFont->GetTextSize(&mrOutputDev, maStyleName);
182 if (maPixelSize.Height() > mnMaxHeight)
183 maPixelSize.setHeight( mnMaxHeight );
184 return maPixelSize;
187 bool CommonStylePreviewRenderer::render(const tools::Rectangle& aRectangle, RenderAlign eRenderAlign)
189 const OUString& rText = maStyleName;
191 // setup the device & draw
192 vcl::Font aOldFont(mrOutputDev.GetFont());
193 Color aOldColor(mrOutputDev.GetTextColor());
194 Color aOldFillColor(mrOutputDev.GetFillColor());
196 if (maBackgroundColor != COL_AUTO)
198 mrOutputDev.SetFillColor(maBackgroundColor);
199 mrOutputDev.DrawRect(aRectangle);
202 if (m_pFont)
204 mrOutputDev.SetFont(*m_pFont);
206 if (maFontColor != COL_AUTO)
207 mrOutputDev.SetTextColor(maFontColor);
209 Size aPixelSize(m_pFont ? maPixelSize : mrOutputDev.GetFont().GetFontSize());
211 Point aFontDrawPosition = aRectangle.TopLeft();
212 if (eRenderAlign == RenderAlign::CENTER)
214 if (aRectangle.GetHeight() > aPixelSize.Height())
215 aFontDrawPosition.AdjustY((aRectangle.GetHeight() - aPixelSize.Height()) / 2 );
218 mrOutputDev.DrawText(aFontDrawPosition, rText);
220 mrOutputDev.SetFillColor(aOldFillColor);
221 mrOutputDev.SetTextColor(aOldColor);
222 mrOutputDev.SetFont(aOldFont);
224 return true;
227 } // end svx namespace
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */