From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- vcl/source/graphic/UnoGraphicDescriptor.cxx | 427 ++++++++++++++++++++++++++++ 1 file changed, 427 insertions(+) create mode 100644 vcl/source/graphic/UnoGraphicDescriptor.cxx (limited to 'vcl/source/graphic/UnoGraphicDescriptor.cxx') diff --git a/vcl/source/graphic/UnoGraphicDescriptor.cxx b/vcl/source/graphic/UnoGraphicDescriptor.cxx new file mode 100644 index 000000000..74a99eb0a --- /dev/null +++ b/vcl/source/graphic/UnoGraphicDescriptor.cxx @@ -0,0 +1,427 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +enum class UnoGraphicProperty +{ + GraphicType = 1 + , MimeType = 2 + , SizePixel = 3 + , Size100thMM = 4 + , BitsPerPixel = 5 + , Transparent = 6 + , Alpha = 7 + , Animated = 8 + , Linked = 9 + , OriginURL = 10 +}; + +} + +using namespace ::com::sun::star; + +namespace unographic { + + +GraphicDescriptor::GraphicDescriptor() : + ::comphelper::PropertySetHelper( createPropertySetInfo() ), + mpGraphic( nullptr ), + meType( GraphicType::NONE ), + mnBitsPerPixel ( 0 ), + mbTransparent ( false ) +{ +} + +GraphicDescriptor::~GraphicDescriptor() + noexcept +{ +} + +void GraphicDescriptor::init( const ::Graphic& rGraphic ) +{ + mpGraphic = &rGraphic; +} + +void GraphicDescriptor::init( const OUString& rURL ) +{ + std::unique_ptr pIStm(::utl::UcbStreamHelper::CreateStream( rURL, StreamMode::READ )); + + if( pIStm ) + implCreate( *pIStm, &rURL ); +} + +void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL ) +{ + std::unique_ptr pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm )); + + if( pIStm ) + implCreate( *pIStm, &rURL ); +} + +void GraphicDescriptor::implCreate( SvStream& rIStm, const OUString* pURL ) +{ + OUString aURL; + if( pURL ) + aURL = *pURL; + ::GraphicDescriptor aDescriptor( rIStm, &aURL ); + + mpGraphic = nullptr; + maMimeType.clear(); + meType = GraphicType::NONE; + mnBitsPerPixel = 0; + mbTransparent = false; + + if( !(aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GraphicFileFormat::NOT) ) + return; + + const char* pMimeType = nullptr; + sal_uInt8 cType = graphic::GraphicType::EMPTY; + + switch( aDescriptor.GetFileFormat() ) + { + case GraphicFileFormat::BMP: pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::GIF: pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::JPG: pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PCD: pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PCX: pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PNG: pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::TIF: pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::XBM: pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::XPM: pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PBM: pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PGM: pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PPM: pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::RAS: pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::TGA: pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::PSD: pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break; + case GraphicFileFormat::WEBP: pMimeType = MIMETYPE_WEBP; cType = graphic::GraphicType::PIXEL; break; + + case GraphicFileFormat::EPS: pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::DXF: pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::MET: pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::PCT: pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::SVM: pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::WMF: pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::EMF: pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break; + case GraphicFileFormat::SVG: pMimeType = MIMETYPE_SVG; cType = graphic::GraphicType::VECTOR; break; + + default: + break; + } + + if( graphic::GraphicType::EMPTY != cType ) + { + meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GraphicType::Bitmap : GraphicType::GdiMetafile ); + maMimeType = OUString( pMimeType, strlen(pMimeType), RTL_TEXTENCODING_ASCII_US ); + maSizePixel = aDescriptor.GetSizePixel(); + maSize100thMM = aDescriptor.GetSize_100TH_MM(); + mnBitsPerPixel = aDescriptor.GetBitsPerPixel(); + mbTransparent = ( graphic::GraphicType::VECTOR == cType ); + } +} + + +uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType ) +{ + uno::Any aAny; + + if( rType == cppu::UnoType::get()) + aAny <<= uno::Reference< lang::XServiceInfo >(this); + else if( rType == cppu::UnoType::get()) + aAny <<= uno::Reference< lang::XTypeProvider >(this); + else if( rType == cppu::UnoType::get()) + aAny <<= uno::Reference< beans::XPropertySet >(this); + else if( rType == cppu::UnoType::get()) + aAny <<= uno::Reference< beans::XPropertyState >(this); + else if( rType == cppu::UnoType::get()) + aAny <<= uno::Reference< beans::XMultiPropertySet >(this); + else + aAny = OWeakAggObject::queryAggregation( rType ); + + return aAny; +} + + +uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType ) +{ + return OWeakAggObject::queryInterface( rType ); +} + + +void SAL_CALL GraphicDescriptor::acquire() + noexcept +{ + OWeakAggObject::acquire(); +} + + +void SAL_CALL GraphicDescriptor::release() + noexcept +{ + OWeakAggObject::release(); +} + + +OUString SAL_CALL GraphicDescriptor::getImplementationName() +{ + return "com.sun.star.comp.graphic.GraphicDescriptor"; +} + +sal_Bool SAL_CALL GraphicDescriptor::supportsService( const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + + +uno::Sequence< OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames() +{ + return { "com.sun.star.graphic.GraphicDescriptor" }; +} + + +uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes() +{ + static const uno::Sequence< uno::Type > aTypes { + cppu::UnoType::get(), + cppu::UnoType::get(), + cppu::UnoType::get(), + cppu::UnoType::get(), + cppu::UnoType::get(), + cppu::UnoType::get() }; + return aTypes; +} + +uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId() +{ + return css::uno::Sequence(); +} + + +rtl::Reference<::comphelper::PropertySetInfo> GraphicDescriptor::createPropertySetInfo() +{ + static ::comphelper::PropertyMapEntry const aEntries[] = + { + { OUString( "GraphicType" ), static_cast< sal_Int32 >( UnoGraphicProperty::GraphicType ), cppu::UnoType< sal_Int8 >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "MimeType" ), static_cast< sal_Int32 >( UnoGraphicProperty::MimeType ), cppu::UnoType< OUString >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "SizePixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::SizePixel ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Size100thMM" ), static_cast< sal_Int32 >( UnoGraphicProperty::Size100thMM ), cppu::UnoType< awt::Size >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "BitsPerPixel" ), static_cast< sal_Int32 >( UnoGraphicProperty::BitsPerPixel ), cppu::UnoType< sal_uInt8 >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Transparent" ), static_cast< sal_Int32 >( UnoGraphicProperty::Transparent ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Alpha" ), static_cast< sal_Int32 >( UnoGraphicProperty::Alpha ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString( "Animated" ), static_cast< sal_Int32 >( UnoGraphicProperty::Animated ), cppu::UnoType< sal_Bool >::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString("Linked"), sal_Int32(UnoGraphicProperty::Linked), cppu::UnoType::get(), beans::PropertyAttribute::READONLY, 0 }, + { OUString("OriginURL"), sal_Int32(UnoGraphicProperty::OriginURL), cppu::UnoType::get(), beans::PropertyAttribute::READONLY, 0 }, + }; + + return rtl::Reference<::comphelper::PropertySetInfo>( new ::comphelper::PropertySetInfo(aEntries) ); +} + + +void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ ) +{ + // we only have readonly attributes +} + + +void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues ) +{ + SolarMutexGuard aGuard; + + while( *ppEntries ) + { + UnoGraphicProperty theProperty = static_cast< UnoGraphicProperty >( (*ppEntries)->mnHandle ); + switch( theProperty ) + { + case UnoGraphicProperty::GraphicType: + { + const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType ); + + *pValues <<= ( eType == GraphicType::Bitmap ? graphic::GraphicType::PIXEL : + ( eType == GraphicType::GdiMetafile ? graphic::GraphicType::VECTOR : + graphic::GraphicType::EMPTY ) ); + } + break; + + case UnoGraphicProperty::MimeType: + { + OUString aMimeType; + + if( mpGraphic ) + { + if( mpGraphic->IsGfxLink() ) + { + const char* pMimeType; + + switch( mpGraphic->GetGfxLink().GetType() ) + { + case GfxLinkType::NativeGif: pMimeType = MIMETYPE_GIF; break; + + // #i15508# added BMP type for better exports (checked, works) + case GfxLinkType::NativeBmp: pMimeType = MIMETYPE_BMP; break; + + case GfxLinkType::NativeJpg: pMimeType = MIMETYPE_JPG; break; + case GfxLinkType::NativePng: pMimeType = MIMETYPE_PNG; break; + case GfxLinkType::NativeWmf: pMimeType = MIMETYPE_WMF; break; + case GfxLinkType::NativeMet: pMimeType = MIMETYPE_MET; break; + case GfxLinkType::NativePct: pMimeType = MIMETYPE_PCT; break; + case GfxLinkType::NativeWebp: pMimeType = MIMETYPE_WEBP; break; + + // added Svg mimetype support + case GfxLinkType::NativeSvg: pMimeType = MIMETYPE_SVG; break; + case GfxLinkType::NativePdf: pMimeType = MIMETYPE_PDF; break; + + default: + pMimeType = nullptr; + break; + } + + if( pMimeType ) + aMimeType = OUString::createFromAscii( pMimeType ); + } + + if( aMimeType.isEmpty() && ( mpGraphic->GetType() != GraphicType::NONE ) ) + aMimeType = MIMETYPE_VCLGRAPHIC; + } + else + aMimeType = maMimeType; + + *pValues <<= aMimeType; + } + break; + + case UnoGraphicProperty::SizePixel: + { + awt::Size aAWTSize( 0, 0 ); + + if( mpGraphic ) + { + if( mpGraphic->GetType() == GraphicType::Bitmap ) + { + const Size aSizePix( mpGraphic->GetSizePixel() ); + aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() ); + } + } + else + aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() ); + + *pValues <<= aAWTSize; + } + break; + + case UnoGraphicProperty::Size100thMM: + { + awt::Size aAWTSize( 0, 0 ); + + if( mpGraphic ) + { + if( mpGraphic->GetPrefMapMode().GetMapUnit() != MapUnit::MapPixel ) + { + const Size aSizeLog( OutputDevice::LogicToLogic( + mpGraphic->GetPrefSize(), + mpGraphic->GetPrefMapMode(), + MapMode(MapUnit::Map100thMM)) ); + aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() ); + } + } + else + aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() ); + + *pValues <<= aAWTSize; + } + break; + + case UnoGraphicProperty::BitsPerPixel: + { + sal_uInt16 nBitsPerPixel = 0; + + if( mpGraphic ) + { + if( mpGraphic->GetType() == GraphicType::Bitmap ) + { + auto ePixelFormat = mpGraphic->GetBitmapEx().GetBitmap().getPixelFormat(); + nBitsPerPixel = vcl::pixelFormatBitCount(ePixelFormat); + } + } + else + nBitsPerPixel = mnBitsPerPixel; + + *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel); + } + break; + + case UnoGraphicProperty::Transparent: + { + *pValues <<= mpGraphic ? mpGraphic->IsTransparent() : mbTransparent; + } + break; + + case UnoGraphicProperty::Alpha: + { + *pValues <<= mpGraphic && mpGraphic->IsAlpha(); + } + break; + + case UnoGraphicProperty::Animated: + { + *pValues <<= mpGraphic && mpGraphic->IsAnimated(); + } + break; + + case UnoGraphicProperty::Linked: + { + *pValues <<= mpGraphic && !mpGraphic->getOriginURL().isEmpty(); + } + break; + + case UnoGraphicProperty::OriginURL: + { + OUString aOriginURL; + if (mpGraphic) + aOriginURL = mpGraphic->getOriginURL(); + + *pValues <<= aOriginURL; + } + break; + } + + ++ppEntries; + ++pValues; + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3