1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "PresenterTextView.hxx"
23 #include <i18nlangtag/mslangid.hxx>
24 #include <cppcanvas/vclfactory.hxx>
25 #include <svl/itempool.hxx>
26 #include <svl/itemset.hxx>
27 #include <unotools/linguprops.hxx>
28 #include <unotools/lingucfg.hxx>
29 #include <editeng/colritem.hxx>
30 #include <editeng/editeng.hxx>
31 #include <editeng/editstat.hxx>
32 #include <editeng/eeitem.hxx>
33 #include <editeng/fhgtitem.hxx>
34 #include <editeng/fontitem.hxx>
35 #include <svx/xflclit.hxx>
36 #include <vcl/bitmapex.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/virdev.hxx>
39 #include <com/sun/star/awt/FontDescriptor.hpp>
40 #include <com/sun/star/awt/Size.hpp>
41 #include <com/sun/star/rendering/XSpriteCanvas.hpp>
42 #include <com/sun/star/rendering/XBitmapCanvas.hpp>
43 #include <com/sun/star/uno/XComponentContext.hpp>
44 #include <com/sun/star/util/Color.hpp>
45 #include <com/sun/star/i18n/ScriptType.hpp>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::lang
;
51 static const OUStringLiteral
gsTextPropertyName("Text");
52 static const OUStringLiteral
gsBitmapPropertyName("Bitmap");
53 static const OUStringLiteral
gsSizePropertyName("Size");
54 static const OUStringLiteral
gsBackgroundColorPropertyName("BackgroundColor");
55 static const OUStringLiteral
gsTextColorPropertyName("TextColor");
56 static const OUStringLiteral
gsFontDescriptorPropertyName("FontDescriptor");
57 static const OUStringLiteral
gsTopPropertyName("Top");
58 static const OUStringLiteral
gsTopRelativePropertyName("RelativeTop");
59 static const OUStringLiteral
gsTotalHeightPropertyName("TotalHeight");
61 namespace sd
{ namespace presenter
{
63 // PresenterTextView::Implementation
64 class PresenterTextView::Implementation
70 void SetCanvas (const cppcanvas::CanvasSharedPtr
& rCanvas
);
71 void SetSize (const Size aSize
);
72 void SetBackgroundColor (const Color aColor
);
73 void SetTextColor (const Color aColor
);
74 void SetFontDescriptor (const awt::FontDescriptor
& rFontDescriptor
);
75 sal_Int32
GetTop() const { return mnTop
;}
76 void SetTop (const sal_Int32 nTop
);
77 void SetText (const OUString
& Text
);
78 sal_Int32
ParseDistance (const OUString
& rsDistance
) const;
79 Reference
<rendering::XBitmap
> const & GetBitmap();
80 sal_Int32
GetTotalHeight();
83 Reference
<rendering::XBitmap
> mxBitmap
;
84 cppcanvas::CanvasSharedPtr mpCanvas
;
85 VclPtr
<VirtualDevice
> mpOutputDevice
;
86 std::unique_ptr
<EditEngine
> mpEditEngine
;
87 SfxItemPool
* mpEditEngineItemPool
;
91 sal_Int32 mnTotalHeight
;
97 PresenterTextView::PresenterTextView ()
98 : PresenterTextViewInterfaceBase(),
99 mpImplementation(new Implementation())
103 PresenterTextView::~PresenterTextView()
107 void SAL_CALL
PresenterTextView::disposing()
109 mpImplementation
.reset();
113 void SAL_CALL
PresenterTextView::initialize (const Sequence
<Any
>& rArguments
)
117 if (rArguments
.getLength() != 1)
119 throw RuntimeException("PresenterTextView: invalid number of arguments",
120 static_cast<XWeak
*>(this));
123 Reference
<rendering::XCanvas
> xCanvas (rArguments
[0], UNO_QUERY_THROW
);
124 mpImplementation
->SetCanvas(
125 cppcanvas::VCLFactory::createCanvas(xCanvas
));
128 Any
PresenterTextView::GetPropertyValue (const OUString
& rsPropertyName
)
132 if (rsPropertyName
== gsBitmapPropertyName
)
134 return Any(mpImplementation
->GetBitmap());
136 else if (rsPropertyName
== gsTopPropertyName
)
138 return Any(mpImplementation
->GetTop());
140 else if (rsPropertyName
== gsTotalHeightPropertyName
)
142 return Any(mpImplementation
->GetTotalHeight());
148 Any
PresenterTextView::SetPropertyValue (
149 const OUString
& rsPropertyName
,
150 const css::uno::Any
& rValue
)
155 if (rsPropertyName
== gsTextPropertyName
)
158 if (rValue
>>= sText
)
159 mpImplementation
->SetText(sText
);
161 else if (rsPropertyName
== gsSizePropertyName
)
164 if (rValue
>>= aSize
)
165 mpImplementation
->SetSize(Size(aSize
.Width
,aSize
.Height
));
167 else if (rsPropertyName
== gsBackgroundColorPropertyName
)
169 util::Color aColor
= util::Color();
170 if (rValue
>>= aColor
)
171 mpImplementation
->SetBackgroundColor(Color(aColor
));
173 else if (rsPropertyName
== gsTextColorPropertyName
)
175 util::Color aColor
= util::Color();
176 if (rValue
>>= aColor
)
177 mpImplementation
->SetTextColor(Color(aColor
));
179 else if (rsPropertyName
== gsFontDescriptorPropertyName
)
181 awt::FontDescriptor aFontDescriptor
;
182 if (rValue
>>= aFontDescriptor
)
183 mpImplementation
->SetFontDescriptor(aFontDescriptor
);
185 else if (rsPropertyName
== gsTopPropertyName
)
189 mpImplementation
->SetTop(nTop
);
191 else if (rsPropertyName
== gsTopRelativePropertyName
)
194 if (rValue
>>= sDistance
)
195 mpImplementation
->SetTop(
196 mpImplementation
->GetTop()
197 + mpImplementation
->ParseDistance(sDistance
));
202 void PresenterTextView::ThrowIfDisposed()
204 if (PresenterTextViewInterfaceBase::rBHelper
.bDisposed
205 || PresenterTextViewInterfaceBase::rBHelper
.bInDispose
|| mpImplementation
== nullptr)
207 throw lang::DisposedException ("PresenterTextView object has already been disposed",
208 static_cast<uno::XWeak
*>(this));
212 // PresenterTextView::Implementation
213 PresenterTextView::Implementation::Implementation()
216 mpOutputDevice(VclPtr
<VirtualDevice
>::Create(*Application::GetDefaultDevice(), DeviceFormat::DEFAULT
, DeviceFormat::DEFAULT
)),
217 mpEditEngineItemPool(EditEngine::CreatePool()),
223 mpOutputDevice
->SetMapMode(MapMode(MapUnit::MapPixel
));
225 // set fonts to be used
226 SvtLinguOptions aOpt
;
227 SvtLinguConfig().GetOptions( aOpt
);
230 LanguageType nFallbackLang
;
232 DefaultFontType nFontType
;
233 sal_uInt16 nFontInfoId
;
236 // info to get western font to be used
237 { LANGUAGE_ENGLISH_US
, LANGUAGE_NONE
,
238 DefaultFontType::SERIF
, EE_CHAR_FONTINFO
},
239 // info to get CJK font to be used
240 { LANGUAGE_JAPANESE
, LANGUAGE_NONE
,
241 DefaultFontType::CJK_TEXT
, EE_CHAR_FONTINFO_CJK
},
242 // info to get CTL font to be used
243 { LANGUAGE_ARABIC_SAUDI_ARABIA
, LANGUAGE_NONE
,
244 DefaultFontType::CTL_TEXT
, EE_CHAR_FONTINFO_CTL
}
246 aTable
[0].nLang
= MsLangId::resolveSystemLanguageByScriptType(aOpt
.nDefaultLanguage
, css::i18n::ScriptType::LATIN
);
247 aTable
[1].nLang
= MsLangId::resolveSystemLanguageByScriptType(aOpt
.nDefaultLanguage_CJK
, css::i18n::ScriptType::ASIAN
);
248 aTable
[2].nLang
= MsLangId::resolveSystemLanguageByScriptType(aOpt
.nDefaultLanguage_CTL
, css::i18n::ScriptType::COMPLEX
);
250 for (FontDta
& rFntDta
: aTable
)
252 LanguageType nLang
= (LANGUAGE_NONE
== rFntDta
.nLang
) ?
253 rFntDta
.nFallbackLang
: rFntDta
.nLang
;
254 vcl::Font aFont
= OutputDevice::GetDefaultFont(
255 rFntDta
.nFontType
, nLang
, GetDefaultFontFlags::OnlyOne
);
256 mpEditEngineItemPool
->SetPoolDefaultItem(
258 aFont
.GetFamilyType(),
259 aFont
.GetFamilyName(),
260 aFont
.GetStyleName(),
263 rFntDta
.nFontInfoId
));
266 mpEditEngine
.reset( new EditEngine (mpEditEngineItemPool
) );
268 mpEditEngine
->EnableUndo (true);
269 mpEditEngine
->SetDefTab (sal_uInt16(
270 Application::GetDefaultDevice()->GetTextWidth("XXXX")));
272 mpEditEngine
->SetControlWord(
273 EEControlBits(mpEditEngine
->GetControlWord() | EEControlBits::AUTOINDENTING
) &
274 EEControlBits(~EEControlBits::UNDOATTRIBS
) &
275 EEControlBits(~EEControlBits::PASTESPECIAL
) );
277 mpEditEngine
->SetWordDelimiters (" .=+-*/(){}[];\"");
278 mpEditEngine
->SetRefMapMode(MapMode(MapUnit::MapPixel
));
279 mpEditEngine
->SetPaperSize (Size(800, 0));
280 mpEditEngine
->EraseVirtualDevice();
281 mpEditEngine
->ClearModifyFlag();
284 PresenterTextView::Implementation::~Implementation()
286 mpEditEngine
.reset();
287 SfxItemPool::Free(mpEditEngineItemPool
);
288 mpOutputDevice
.disposeAndClear();
291 void PresenterTextView::Implementation::SetCanvas (const cppcanvas::CanvasSharedPtr
& rpCanvas
)
297 void PresenterTextView::Implementation::SetSize (const Size aSize
)
299 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
302 mpEditEngine
->SetPaperSize(maSize
);
307 void PresenterTextView::Implementation::SetBackgroundColor (const Color aColor
)
311 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
312 DBG_ASSERT(mpEditEngineItemPool
!=nullptr, "EditEngineItemPool missing");
313 mpEditEngine
->SetBackgroundColor(aColor
);
314 mpEditEngine
->EnableAutoColor(false);
315 mpEditEngine
->ForceAutoColor(false);
318 void PresenterTextView::Implementation::SetTextColor (const Color aColor
)
322 DBG_ASSERT(mpEditEngineItemPool
!=nullptr, "EditEngineItemPool missing");
323 mpEditEngineItemPool
->SetPoolDefaultItem(SvxColorItem(aColor
, EE_CHAR_COLOR
));
326 void PresenterTextView::Implementation::SetFontDescriptor (
327 const awt::FontDescriptor
& rFontDescriptor
)
331 DBG_ASSERT(mpEditEngineItemPool
!=nullptr, "EditEngineItemPool missing");
333 const sal_Int32 nFontHeight
= rFontDescriptor
.Height
;
335 SvxFontHeightItem
aFontHeight(
336 Application::GetDefaultDevice()->LogicToPixel(
337 Size(0, nFontHeight
), MapMode (MapUnit::MapPoint
)).Height(),
340 mpEditEngineItemPool
->SetPoolDefaultItem( aFontHeight
);
341 aFontHeight
.SetWhich (EE_CHAR_FONTHEIGHT_CJK
);
342 mpEditEngineItemPool
->SetPoolDefaultItem( aFontHeight
);
343 aFontHeight
.SetWhich (EE_CHAR_FONTHEIGHT_CTL
);
344 mpEditEngineItemPool
->SetPoolDefaultItem( aFontHeight
);
346 SvxFontItem
aSvxFontItem (EE_CHAR_FONTINFO
);
347 aSvxFontItem
.SetFamilyName( rFontDescriptor
.Name
);
348 mpEditEngineItemPool
->SetPoolDefaultItem(aSvxFontItem
);
357 void PresenterTextView::Implementation::SetTop (const sal_Int32 nTop
)
367 void PresenterTextView::Implementation::SetText (const OUString
& rText
)
369 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
371 mpEditEngine
->SetPaperSize(maSize
);
376 sal_Int32
PresenterTextView::Implementation::ParseDistance (const OUString
& rsDistance
) const
378 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
379 sal_Int32
nDistance (0);
380 if (rsDistance
.endsWith("px"))
382 nDistance
= rsDistance
.copy(0,rsDistance
.getLength()-2).toInt32();
384 else if (rsDistance
.endsWith("l"))
386 const sal_Int32
nLines (rsDistance
.copy(0,rsDistance
.getLength()-1).toInt32());
387 // Take the height of the first line as the height of every line.
388 const sal_uInt32
nFirstLineHeight (mpEditEngine
->GetLineHeight(0));
389 nDistance
= nFirstLineHeight
* nLines
;
395 Reference
<rendering::XBitmap
> const & PresenterTextView::Implementation::GetBitmap()
397 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
399 if ( ! mxBitmap
.is())
401 mpOutputDevice
.disposeAndClear();
402 mpOutputDevice
= VclPtr
<VirtualDevice
>::Create(*Application::GetDefaultDevice(),
403 DeviceFormat::DEFAULT
, DeviceFormat::DEFAULT
);
404 mpOutputDevice
->SetMapMode(MapMode(MapUnit::MapPixel
));
405 mpOutputDevice
->SetOutputSizePixel(maSize
);
406 mpOutputDevice
->SetLineColor();
407 mpOutputDevice
->SetFillColor();
408 mpOutputDevice
->SetBackground(Wallpaper());
409 mpOutputDevice
->Erase();
411 MapMode
aMapMode (mpOutputDevice
->GetMapMode());
412 aMapMode
.SetOrigin(Point(0,0));
413 mpOutputDevice
->SetMapMode(aMapMode
);
414 const ::tools::Rectangle
aWindowBox (Point(0,0), maSize
);
415 mpOutputDevice
->DrawRect(aWindowBox
);
417 mpEditEngine
->Clear();
418 mpEditEngine
->SetText(msText
);
419 mpEditEngine
->SetPaperSize(maSize
);
421 mpEditEngine
->Draw(mpOutputDevice
, aWindowBox
, Point(0,mnTop
));
423 const BitmapEx
aBitmap (mpOutputDevice
->GetBitmapEx(Point(0,0), maSize
));
424 mxBitmap
= cppcanvas::VCLFactory::createBitmap(
432 sal_Int32
PresenterTextView::Implementation::GetTotalHeight()
434 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
436 if (mnTotalHeight
< 0)
438 if ( ! mxBitmap
.is())
440 mnTotalHeight
= mpEditEngine
->GetTextHeight();
442 return mnTotalHeight
;
445 void PresenterTextView::Implementation::CheckTop()
447 DBG_ASSERT(mpEditEngine
!=nullptr, "EditEngine missing");
449 if (mpEditEngine
!=nullptr && mnTotalHeight
< 0)
450 mnTotalHeight
= mpEditEngine
->GetTextHeight();
451 if (mpEditEngine
!=nullptr && mnTop
>= mnTotalHeight
)
452 mnTop
= mnTotalHeight
- mpEditEngine
->GetLineHeight(0);
454 if (mnTotalHeight
< maSize
.Height())
457 if (mnTotalHeight
- mnTop
< maSize
.Height())
458 mnTop
= mnTotalHeight
- maSize
.Height();
464 } } // end of namespace ::sd::presenter
467 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
468 com_sun_star_comp_Draw_PresenterTextView_get_implementation(css::uno::XComponentContext
*,
469 css::uno::Sequence
<css::uno::Any
> const &)
471 return cppu::acquire(new sd::presenter::PresenterTextView
);
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */