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>
27 #include <editeng/contouritem.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 
40 using namespace css;
41 
42 namespace svx
43 {
44 
45 CommonStylePreviewRenderer::CommonStylePreviewRenderer(
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_pFont.reset();
62 
63  std::optional<SfxItemSet> pItemSet(mpStyle->GetItemSetForPreview());
64 
65  if (!pItemSet) return false;
66 
67  std::unique_ptr<SvxFont> pFont(new SvxFont);
68 
69  const SfxPoolItem* pItem;
70 
71  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
72  {
73  pFont->SetWeight(static_cast<const SvxWeightItem*>(pItem)->GetWeight());
74  }
75  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
76  {
77  pFont->SetItalic(static_cast<const SvxPostureItem*>(pItem)->GetPosture());
78  }
79  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
80  {
81  pFont->SetOutline(static_cast< const SvxContourItem*>(pItem)->GetValue());
82  }
83  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
84  {
85  pFont->SetShadow(static_cast<const SvxShadowedItem*>(pItem)->GetValue());
86  }
87  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
88  {
89  pFont->SetRelief(static_cast<const SvxCharReliefItem*>(pItem)->GetValue());
90  }
91  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
92  {
93  pFont->SetUnderline(static_cast< const SvxUnderlineItem*>(pItem)->GetLineStyle());
94  }
95  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
96  {
97  pFont->SetOverline(static_cast<const SvxOverlineItem*>(pItem)->GetValue());
98  }
99  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
100  {
101  pFont->SetStrikeout(static_cast<const SvxCrossedOutItem*>(pItem)->GetStrikeout());
102  }
103  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
104  {
105  pFont->SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
106  }
107  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
108  {
109  pFont->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  pFont->SetFamilyName(pFontItem->GetFamilyName());
141  pFont->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  pFont->SetFontSize(maPixelSize);
154 
155  vcl::Font aOldFont(mrOutputDev.GetFont());
156 
157  mrOutputDev.SetFont(*pFont);
158  tools::Rectangle aTextRect;
160  if (aTextRect.Bottom() > mnMaxHeight)
161  {
162  double ratio = double(mnMaxHeight) / aTextRect.Bottom();
163  maPixelSize.setWidth( maPixelSize.Width() * ratio );
164  maPixelSize.setHeight( maPixelSize.Height() * ratio );
165  pFont->SetFontSize(maPixelSize);
166  }
167  mrOutputDev.SetFont(aOldFont);
168  }
169  else
170  {
171  return false;
172  }
173 
174  m_pFont = std::move(pFont);
176  return true;
177 }
178 
180 {
181  assert(m_pFont);
182  Size aPixelSize = m_pFont->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_pFont)
205 
206  if (maFontColor != COL_AUTO)
208 
209  if (maHighlightColor != COL_AUTO)
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  mrOutputDev.DrawText(aFontDrawPosition, rText);
222 
223  mrOutputDev.Pop();
224 
225  return true;
226 }
227 
228 } // end svx namespace
229 
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual std::optional< SfxItemSet > GetItemSetForPreview()
std::string GetValue
long Long
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)
constexpr::Color COL_AUTO(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
void SetTextFillColor()
SfxStyleFamily GetFamily() const
void SetFamilyName(const OUString &rFamilyName)
const OUString & GetName() const
const vcl::Font & GetFont() const
void DrawRect(const tools::Rectangle &rRect)
const OUString & GetStyleName() const
SfxStyleSheetBase * mpStyle
const SfxObjectShell & mrShell
void SetFillColor()
void SetTextColor(const Color &rColor)
const Size & GetFontSize() const
OutputDevice & mrOutputDev
constexpr Point TopLeft() const
constexpr tools::Long Bottom() const
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
const OUString & GetFamilyName() const
constexpr TypedWhichId< XFillColorItem > XATTR_FILLCOLOR(XATTR_FILL_FIRST+1)
sal_uInt32 GetHeight() const
virtual bool render(const tools::Rectangle &aRectangle, RenderAlign eRenderAlign=RenderAlign::CENTER) override
Color maBackgroundColor
constexpr tools::Long Height() const
void SetFont(const vcl::Font &rNewFont)
MapUnit GetMapUnit() const
bool GetTextBoundRect(tools::Rectangle &rRect, const OUString &rStr, sal_Int32 nBase=0, sal_Int32 nIndex=0, sal_Int32 nLen=-1, sal_uLong nLayoutWidth=0, const tools::Long *pDXArray=nullptr, const SalLayoutGlyphs *pGlyphs=nullptr) const
void setHeight(tools::Long nHeight)
bool IsStarSymbol(const OUString &rFontName)
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)
sal_Int16 GetCaseMap(sal_Int32 nToken)
constexpr tools::Long GetHeight() const