LibreOffice Module svx (master) 1
CommonStylePreviewRenderer.cxx
Go to the documentation of this file.
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 */
10
11#include <memory>
13
14#include <sfx2/objsh.hxx>
15#include <svl/style.hxx>
16#include <svl/itemset.hxx>
17#include <vcl/outdev.hxx>
18
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>
28#include <editeng/colritem.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>
37
38#include <editeng/editids.hrc>
39
40using namespace css;
41
42namespace svx
43{
44
46 const SfxObjectShell& rShell, OutputDevice& rOutputDev,
47 SfxStyleSheetBase* pStyle, tools::Long nMaxHeight)
48 : StylePreviewRenderer(rShell, rOutputDev, pStyle, nMaxHeight)
49 , maFontColor(COL_AUTO)
50 , maHighlightColor(COL_AUTO)
52 , maStyleName(mpStyle->GetName())
53{
54}
55
57{}
58
60{
61 m_oFont.reset();
62
63 std::optional<SfxItemSet> pItemSet(mpStyle->GetItemSetForPreview());
64
65 if (!pItemSet) return false;
66
67 SvxFont aFont;
68
69 const SfxPoolItem* pItem;
70
71 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
72 {
73 aFont.SetWeight(static_cast<const SvxWeightItem*>(pItem)->GetWeight());
74 }
75 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
76 {
77 aFont.SetItalic(static_cast<const SvxPostureItem*>(pItem)->GetPosture());
78 }
79 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
80 {
81 aFont.SetOutline(static_cast< const SvxContourItem*>(pItem)->GetValue());
82 }
83 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
84 {
85 aFont.SetShadow(static_cast<const SvxShadowedItem*>(pItem)->GetValue());
86 }
87 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
88 {
89 aFont.SetRelief(static_cast<const SvxCharReliefItem*>(pItem)->GetValue());
90 }
91 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
92 {
93 aFont.SetUnderline(static_cast< const SvxUnderlineItem*>(pItem)->GetLineStyle());
94 }
95 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
96 {
97 aFont.SetOverline(static_cast<const SvxOverlineItem*>(pItem)->GetValue());
98 }
99 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
100 {
101 aFont.SetStrikeout(static_cast<const SvxCrossedOutItem*>(pItem)->GetStrikeout());
102 }
103 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
104 {
105 aFont.SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
106 }
107 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
108 {
109 aFont.SetEmphasisMark(static_cast<const SvxEmphasisMarkItem*>(pItem)->GetEmphasisMark());
110 }
111 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_COLOR)) != nullptr)
112 {
113 maFontColor = static_cast<const SvxColorItem*>(pItem)->GetValue();
114 }
115 if ((pItem = pItemSet->GetItem(SID_ATTR_BRUSH_CHAR)) != nullptr)
116 {
117 maHighlightColor = static_cast<const SvxBrushItem*>(pItem)->GetColor();
118 }
119
120 if (mpStyle->GetFamily() == SfxStyleFamily::Para)
121 {
122 if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
123 {
124 css::drawing::FillStyle aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
125 if (aFillStyle == drawing::FillStyle_SOLID)
126 {
127 if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
128 {
129 maBackgroundColor = static_cast<const XFillColorItem*>(pItem)->GetColorValue();
130 }
131 }
132 }
133 }
134
135 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
136 {
137 const SvxFontItem* pFontItem = static_cast<const SvxFontItem*>(pItem);
138 if (IsStarSymbol(pFontItem->GetFamilyName()))
139 return false;
140 aFont.SetFamilyName(pFontItem->GetFamilyName());
141 aFont.SetStyleName(pFontItem->GetStyleName());
142 }
143 else
144 {
145 return false;
146 }
147
148 if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
149 {
150 const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
151 Size aFontSize(0, pFontHeightItem->GetHeight());
153 aFont.SetFontSize(maPixelSize);
154
155 vcl::Font aOldFont(mrOutputDev.GetFont());
156
157 mrOutputDev.SetFont(aFont);
158 tools::Rectangle aTextRect;
160 if (aTextRect.Bottom() > mnMaxHeight)
161 {
162 double ratio = double(mnMaxHeight) / aTextRect.Bottom();
165 aFont.SetFontSize(maPixelSize);
166 }
167 mrOutputDev.SetFont(aOldFont);
168 }
169 else
170 {
171 return false;
172 }
173
174 m_oFont = aFont;
176 return true;
177}
178
180{
181 assert(m_oFont);
182 Size aPixelSize = m_oFont->GetTextSize(mrOutputDev, maStyleName);
183
184 if (aPixelSize.Height() > mnMaxHeight)
185 aPixelSize.setHeight( mnMaxHeight );
186
187 return aPixelSize;
188}
189
191{
192 const OUString& rText = maStyleName;
193
194 // setup the device & draw
196
198 {
200 mrOutputDev.DrawRect(aRectangle);
201 }
202
203 if (m_oFont)
205
206 if (maFontColor != COL_AUTO)
208
211
213
214 Point aFontDrawPosition = aRectangle.TopLeft();
215 if (eRenderAlign == RenderAlign::CENTER)
216 {
217 if (aRectangle.GetHeight() > aPixelSize.Height())
218 aFontDrawPosition.AdjustY((aRectangle.GetHeight() - aPixelSize.Height()) / 2 );
219 }
220
221 if (m_oFont)
222 m_oFont->QuickDrawText( &mrOutputDev, aFontDrawPosition, rText, 0, rText.getLength(), {} );
223 else
224 mrOutputDev.DrawText(aFontDrawPosition, rText);
225
227
228 return true;
229}
230
231} // end svx namespace
232
233/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Color maBackgroundColor
const vcl::Font & GetFont() const
void SetFont(const vcl::Font &rNewFont)
void DrawRect(const tools::Rectangle &rRect)
void SetTextColor(const Color &rColor)
void SetFillColor()
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
void SetTextFillColor()
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, std::vector< tools::Rectangle > *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
bool GetTextBoundRect(tools::Rectangle &rRect, const OUString &rStr, sal_Int32 nBase=0, sal_Int32 nIndex=0, sal_Int32 nLen=-1, sal_uLong nLayoutWidth=0, o3tl::span< const sal_Int32 > pDXArray={}, const SalLayoutGlyphs *pGlyphs=nullptr) const
MapUnit GetMapUnit() const
const OUString & GetName() const
SfxStyleFamily GetFamily() const
virtual std::optional< SfxItemSet > GetItemSetForPreview()
constexpr tools::Long Height() const
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
sal_uInt32 GetHeight() const
const OUString & GetStyleName() const
const OUString & GetFamilyName() const
void SetCaseMap(const SvxCaseMap eNew)
SfxStyleSheetBase * mpStyle
OutputDevice & mrOutputDev
const SfxObjectShell & mrShell
CommonStylePreviewRenderer(const SfxObjectShell &rShell, OutputDevice &rOutputDev, SfxStyleSheetBase *pStyle, tools::Long nMaxHeight)
virtual bool render(const tools::Rectangle &aRectangle, RenderAlign eRenderAlign=RenderAlign::CENTER) override
constexpr Point TopLeft() const
constexpr tools::Long GetHeight() const
constexpr tools::Long Bottom() const
const Size & GetFontSize() const
constexpr ::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
virtual OUString GetName() const override
UNOTOOLS_DLLPUBLIC bool IsStarSymbol(std::u16string_view rFontName)
sal_Int16 GetCaseMap(sal_Int32 nToken)
long Long
std::string GetValue
constexpr TypedWhichId< XFillColorItem > XATTR_FILLCOLOR(XATTR_FILL_FIRST+1)
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)