bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / styles / CommonStylePreviewRenderer.cxx
blob65690bed792ba613cecea92fef83429d1b5e1ea3
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 <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 <editeng/brushitem.hxx>
24 #include <editeng/fontitem.hxx>
25 #include <editeng/fhgtitem.hxx>
26 #include <editeng/charreliefitem.hxx>
27 #include <editeng/contouritem.hxx>
28 #include <editeng/colritem.hxx>
29 #include <editeng/crossedoutitem.hxx>
30 #include <editeng/emphasismarkitem.hxx>
31 #include <editeng/postitem.hxx>
32 #include <editeng/shdditem.hxx>
33 #include <editeng/udlnitem.hxx>
34 #include <editeng/wghtitem.hxx>
35 #include <editeng/svxfont.hxx>
36 #include <editeng/cmapitem.hxx>
38 #include <editeng/editids.hrc>
40 using namespace css;
42 namespace svx
45 CommonStylePreviewRenderer::CommonStylePreviewRenderer(
46 const SfxObjectShell& rShell, OutputDevice& rOutputDev,
47 SfxStyleSheetBase* pStyle, tools::Long nMaxHeight)
48 : StylePreviewRenderer(rShell, rOutputDev, pStyle, nMaxHeight)
49 , m_pFont()
50 , maFontColor(COL_AUTO)
51 , maHighlightColor(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<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<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 = static_cast<const SvxColorItem*>(pItem)->GetValue();
117 if ((pItem = pItemSet->GetItem(SID_ATTR_BRUSH_CHAR)) != nullptr)
119 maHighlightColor = static_cast<const SvxBrushItem*>(pItem)->GetColor();
122 if (mpStyle->GetFamily() == SfxStyleFamily::Para)
124 if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
126 css::drawing::FillStyle aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
127 if (aFillStyle == drawing::FillStyle_SOLID)
129 if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
131 maBackgroundColor = static_cast<const XFillColorItem*>(pItem)->GetColorValue();
137 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
139 const SvxFontItem* pFontItem = static_cast<const SvxFontItem*>(pItem);
140 if (IsStarSymbol(pFontItem->GetFamilyName()))
141 return false;
142 pFont->SetFamilyName(pFontItem->GetFamilyName());
143 pFont->SetStyleName(pFontItem->GetStyleName());
145 else
147 return false;
150 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
152 const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
153 Size aFontSize(0, pFontHeightItem->GetHeight());
154 maPixelSize = mrOutputDev.LogicToPixel(aFontSize, MapMode(mrShell.GetMapUnit()));
155 pFont->SetFontSize(maPixelSize);
157 vcl::Font aOldFont(mrOutputDev.GetFont());
159 mrOutputDev.SetFont(*pFont);
160 tools::Rectangle aTextRect;
161 mrOutputDev.GetTextBoundRect(aTextRect, mpStyle->GetName());
162 if (aTextRect.Bottom() > mnMaxHeight)
164 double ratio = double(mnMaxHeight) / aTextRect.Bottom();
165 maPixelSize.setWidth( maPixelSize.Width() * ratio );
166 maPixelSize.setHeight( maPixelSize.Height() * ratio );
167 pFont->SetFontSize(maPixelSize);
169 mrOutputDev.SetFont(aOldFont);
171 else
173 return false;
176 m_pFont = std::move(pFont);
177 maPixelSize = getRenderSize();
178 return true;
181 Size CommonStylePreviewRenderer::getRenderSize() const
183 assert(m_pFont);
184 Size aPixelSize = m_pFont->GetTextSize(mrOutputDev, maStyleName);
186 if (aPixelSize.Height() > mnMaxHeight)
187 aPixelSize.setHeight( mnMaxHeight );
189 return aPixelSize;
192 bool CommonStylePreviewRenderer::render(const tools::Rectangle& aRectangle, RenderAlign eRenderAlign)
194 const OUString& rText = maStyleName;
196 // setup the device & draw
197 mrOutputDev.Push(PushFlags::FONT | PushFlags::TEXTCOLOR | PushFlags::FILLCOLOR | PushFlags::TEXTFILLCOLOR);
199 if (maBackgroundColor != COL_AUTO)
201 mrOutputDev.SetFillColor(maBackgroundColor);
202 mrOutputDev.DrawRect(aRectangle);
205 if (m_pFont)
206 mrOutputDev.SetFont(*m_pFont);
208 if (maFontColor != COL_AUTO)
209 mrOutputDev.SetTextColor(maFontColor);
211 if (maHighlightColor != COL_AUTO)
212 mrOutputDev.SetTextFillColor(maHighlightColor);
214 Size aPixelSize(m_pFont ? maPixelSize : mrOutputDev.GetFont().GetFontSize());
216 Point aFontDrawPosition = aRectangle.TopLeft();
217 if (eRenderAlign == RenderAlign::CENTER)
219 if (aRectangle.GetHeight() > aPixelSize.Height())
220 aFontDrawPosition.AdjustY((aRectangle.GetHeight() - aPixelSize.Height()) / 2 );
223 mrOutputDev.DrawText(aFontDrawPosition, rText);
225 mrOutputDev.Pop();
227 return true;
230 } // end svx namespace
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */