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/fontitem.hxx>
24 #include <editeng/fhgtitem.hxx>
26 #include <editeng/contouritem.hxx>
27 #include <editeng/colritem.hxx>
30 #include <editeng/postitem.hxx>
31 #include <editeng/shdditem.hxx>
32 #include <editeng/udlnitem.hxx>
33 #include <editeng/wghtitem.hxx>
34 #include <editeng/svxfont.hxx>
35 #include <editeng/cmapitem.hxx>
36 
37 #include <editeng/editids.hrc>
38 
39 using namespace css;
40 
41 namespace svx
42 {
43 
44 CommonStylePreviewRenderer::CommonStylePreviewRenderer(
45  const SfxObjectShell& rShell, OutputDevice& rOutputDev,
46  SfxStyleSheetBase* pStyle, long nMaxHeight)
47  : StylePreviewRenderer(rShell, rOutputDev, pStyle, nMaxHeight)
48  , m_pFont()
49  , maFontColor(COL_AUTO)
51  , maPixelSize()
52  , maStyleName(mpStyle->GetName())
53 {
54 }
55 
57 {}
58 
60 {
61  m_pFont.reset();
62 
63  std::unique_ptr<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 
116  if (mpStyle->GetFamily() == SfxStyleFamily::Para)
117  {
118  if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
119  {
120  css::drawing::FillStyle aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
121  if (aFillStyle == drawing::FillStyle_SOLID)
122  {
123  if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
124  {
125  maBackgroundColor = static_cast<const XFillColorItem*>(pItem)->GetColorValue();
126  }
127  }
128  }
129  }
130 
131  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
132  {
133  const SvxFontItem* pFontItem = static_cast<const SvxFontItem*>(pItem);
134  if (IsStarSymbol(pFontItem->GetFamilyName()))
135  return false;
136  pFont->SetFamilyName(pFontItem->GetFamilyName());
137  pFont->SetStyleName(pFontItem->GetStyleName());
138  }
139  else
140  {
141  return false;
142  }
143 
144  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
145  {
146  const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
147  Size aFontSize(0, pFontHeightItem->GetHeight());
149  pFont->SetFontSize(maPixelSize);
150 
151  vcl::Font aOldFont(mrOutputDev.GetFont());
152 
153  mrOutputDev.SetFont(*pFont);
154  tools::Rectangle aTextRect;
156  if (aTextRect.Bottom() > mnMaxHeight)
157  {
158  double ratio = double(mnMaxHeight) / aTextRect.Bottom();
159  maPixelSize.setWidth( maPixelSize.Width() * ratio );
160  maPixelSize.setHeight( maPixelSize.Height() * ratio );
161  pFont->SetFontSize(maPixelSize);
162  }
163  mrOutputDev.SetFont(aOldFont);
164  }
165  else
166  {
167  return false;
168  }
169 
170  m_pFont = std::move(pFont);
172  return true;
173 }
174 
176 {
177  assert(m_pFont);
178  Size aPixelSize = m_pFont->GetTextSize(&mrOutputDev, maStyleName);
179 
180  if (aPixelSize.Height() > mnMaxHeight)
181  aPixelSize.setHeight( mnMaxHeight );
182 
183  return aPixelSize;
184 }
185 
187 {
188  const OUString& rText = maStyleName;
189 
190  // setup the device & draw
191  vcl::Font aOldFont(mrOutputDev.GetFont());
192  Color aOldColor(mrOutputDev.GetTextColor());
193  Color aOldFillColor(mrOutputDev.GetFillColor());
194 
196  {
198  mrOutputDev.DrawRect(aRectangle);
199  }
200 
201  if (m_pFont)
202  {
204  }
205  if (maFontColor != COL_AUTO)
207 
209 
210  Point aFontDrawPosition = aRectangle.TopLeft();
211  if (eRenderAlign == RenderAlign::CENTER)
212  {
213  if (aRectangle.GetHeight() > aPixelSize.Height())
214  aFontDrawPosition.AdjustY((aRectangle.GetHeight() - aPixelSize.Height()) / 2 );
215  }
216 
217  mrOutputDev.DrawText(aFontDrawPosition, rText);
218 
219  mrOutputDev.SetFillColor(aOldFillColor);
220  mrOutputDev.SetTextColor(aOldColor);
221  mrOutputDev.SetFont(aOldFont);
222 
223  return true;
224 }
225 
226 } // end svx namespace
227 
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Point TopLeft() const
const Color & GetTextColor() const
void DrawText(const Point &rStartPt, const OUString &rStr, sal_Int32 nIndex=0, sal_Int32 nLen=-1, MetricVector *pVector=nullptr, OUString *pDisplayText=nullptr, const SalLayoutGlyphs *pLayoutCache=nullptr)
long GetHeight() const
std::string GetValue
long Height() const
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)
SfxStyleFamily GetFamily() 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 long *pDXArray=nullptr, const SalLayoutGlyphs *pGlyphs=nullptr) const
void SetFamilyName(const OUString &rFamilyName)
constexpr::Color COL_AUTO(0xFF, 0xFF, 0xFF, 0xFF)
const OUString & GetName() const
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
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)
long Bottom() const
const Size & GetFontSize() const
OutputDevice & mrOutputDev
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
void SetFont(const vcl::Font &rNewFont)
MapUnit GetMapUnit() const
virtual std::unique_ptr< SfxItemSet > GetItemSetForPreview()
bool IsStarSymbol(const OUString &rFontName)
sal_Int16 GetCaseMap(sal_Int32 nToken)
const Color & GetFillColor() const
void setHeight(long nHeight)