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, long nMaxHeight)
48  : StylePreviewRenderer(rShell, rOutputDev, pStyle, nMaxHeight)
49  , m_pFont()
50  , maFontColor(COL_AUTO)
51  , maHighlightColor(COL_AUTO)
53  , maPixelSize()
54  , maStyleName(mpStyle->GetName())
55 {
56 }
57 
59 {}
60 
62 {
63  m_pFont.reset();
64 
65  std::unique_ptr<SfxItemSet> pItemSet(mpStyle->GetItemSetForPreview());
66 
67  if (!pItemSet) return false;
68 
69  std::unique_ptr<SvxFont> pFont(new SvxFont);
70 
71  const SfxPoolItem* pItem;
72 
73  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
74  {
75  pFont->SetWeight(static_cast<const SvxWeightItem*>(pItem)->GetWeight());
76  }
77  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
78  {
79  pFont->SetItalic(static_cast<const SvxPostureItem*>(pItem)->GetPosture());
80  }
81  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
82  {
83  pFont->SetOutline(static_cast< const SvxContourItem*>(pItem)->GetValue());
84  }
85  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
86  {
87  pFont->SetShadow(static_cast<const SvxShadowedItem*>(pItem)->GetValue());
88  }
89  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
90  {
91  pFont->SetRelief(static_cast<const SvxCharReliefItem*>(pItem)->GetValue());
92  }
93  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
94  {
95  pFont->SetUnderline(static_cast< const SvxUnderlineItem*>(pItem)->GetLineStyle());
96  }
97  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
98  {
99  pFont->SetOverline(static_cast<const SvxOverlineItem*>(pItem)->GetValue());
100  }
101  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
102  {
103  pFont->SetStrikeout(static_cast<const SvxCrossedOutItem*>(pItem)->GetStrikeout());
104  }
105  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
106  {
107  pFont->SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
108  }
109  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
110  {
111  pFont->SetEmphasisMark(static_cast<const SvxEmphasisMarkItem*>(pItem)->GetEmphasisMark());
112  }
113  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_COLOR)) != nullptr)
114  {
115  maFontColor = static_cast<const SvxColorItem*>(pItem)->GetValue();
116  }
117  if ((pItem = pItemSet->GetItem(SID_ATTR_BRUSH_CHAR)) != nullptr)
118  {
119  maHighlightColor = static_cast<const SvxBrushItem*>(pItem)->GetColor();
120  }
121 
122  if (mpStyle->GetFamily() == SfxStyleFamily::Para)
123  {
124  if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
125  {
126  css::drawing::FillStyle aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
127  if (aFillStyle == drawing::FillStyle_SOLID)
128  {
129  if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
130  {
131  maBackgroundColor = static_cast<const XFillColorItem*>(pItem)->GetColorValue();
132  }
133  }
134  }
135  }
136 
137  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
138  {
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());
144  }
145  else
146  {
147  return false;
148  }
149 
150  if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
151  {
152  const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
153  Size aFontSize(0, pFontHeightItem->GetHeight());
155  pFont->SetFontSize(maPixelSize);
156 
157  vcl::Font aOldFont(mrOutputDev.GetFont());
158 
159  mrOutputDev.SetFont(*pFont);
160  tools::Rectangle aTextRect;
162  if (aTextRect.Bottom() > mnMaxHeight)
163  {
164  double ratio = double(mnMaxHeight) / aTextRect.Bottom();
165  maPixelSize.setWidth( maPixelSize.Width() * ratio );
166  maPixelSize.setHeight( maPixelSize.Height() * ratio );
167  pFont->SetFontSize(maPixelSize);
168  }
169  mrOutputDev.SetFont(aOldFont);
170  }
171  else
172  {
173  return false;
174  }
175 
176  m_pFont = std::move(pFont);
178  return true;
179 }
180 
182 {
183  assert(m_pFont);
184  Size aPixelSize = m_pFont->GetTextSize(&mrOutputDev, maStyleName);
185 
186  if (aPixelSize.Height() > mnMaxHeight)
187  aPixelSize.setHeight( mnMaxHeight );
188 
189  return aPixelSize;
190 }
191 
193 {
194  const OUString& rText = maStyleName;
195 
196  // setup the device & draw
197  mrOutputDev.Push(PushFlags::FONT | PushFlags::TEXTCOLOR | PushFlags::FILLCOLOR | PushFlags::TEXTFILLCOLOR);
198 
200  {
202  mrOutputDev.DrawRect(aRectangle);
203  }
204 
205  if (m_pFont)
207 
208  if (maFontColor != COL_AUTO)
210 
211  if (maHighlightColor != COL_AUTO)
213 
215 
216  Point aFontDrawPosition = aRectangle.TopLeft();
217  if (eRenderAlign == RenderAlign::CENTER)
218  {
219  if (aRectangle.GetHeight() > aPixelSize.Height())
220  aFontDrawPosition.AdjustY((aRectangle.GetHeight() - aPixelSize.Height()) / 2 );
221  }
222 
223  mrOutputDev.DrawText(aFontDrawPosition, rText);
224 
225  mrOutputDev.Pop();
226 
227  return true;
228 }
229 
230 } // end svx namespace
231 
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Point TopLeft() 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)
std::string GetValue
constexpr TypedWhichId< XFillStyleItem > XATTR_FILLSTYLE(XATTR_FILL_FIRST)
void SetTextFillColor()
SfxStyleFamily GetFamily() 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
tools::Long Bottom() 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
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
tools::Long Height() 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)
tools::Long GetHeight() const
virtual std::unique_ptr< SfxItemSet > GetItemSetForPreview()
bool IsStarSymbol(const OUString &rFontName)
void Push(PushFlags nFlags=PushFlags::ALL)
sal_Int16 GetCaseMap(sal_Int32 nToken)