Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/doc/graphhelp.cxx
Line
Count
Source
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
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
21
#ifdef _WIN32
22
#include <prewin.h>
23
#include <postwin.h>
24
#endif
25
26
#include <com/sun/star/uno/Exception.hpp>
27
#include <com/sun/star/graphic/GraphicProvider.hpp>
28
#include <com/sun/star/graphic/XGraphicProvider.hpp>
29
#include <com/sun/star/graphic/XGraphic.hpp>
30
31
32
#include <vcl/gdimtf.hxx>
33
#include <vcl/graph.hxx>
34
#include <vcl/cvtgrf.hxx>
35
#include <vcl/bitmap.hxx>
36
#include <vcl/graphicfilter.hxx>
37
38
#include <tools/stream.hxx>
39
#include <unotools/ucbstreamhelper.hxx>
40
#include <comphelper/processfactory.hxx>
41
#include <comphelper/propertyvalue.hxx>
42
#include <o3tl/char16_t2wchar_t.hxx>
43
#include <o3tl/string_view.hxx>
44
45
#include "graphhelp.hxx"
46
#include <bitmaps.hlst>
47
48
#include <memory>
49
50
#if defined _WIN32
51
#include <tools/mapunit.hxx>
52
#include <unotools/tempfile.hxx>
53
#include <vcl/outdev.hxx>
54
#endif
55
56
using namespace css;
57
58
std::unique_ptr<SvMemoryStream> GraphicHelper::getFormatStrFromGDI_Impl( const GDIMetaFile* pGDIMeta, ConvertDataFormat nFormat )
59
0
{
60
0
    std::unique_ptr<SvMemoryStream> pResult;
61
0
    if ( pGDIMeta )
62
0
    {
63
0
        std::unique_ptr<SvMemoryStream> pStream(new SvMemoryStream( 65535, 65535 ));
64
0
        Graphic aGraph( *pGDIMeta );
65
0
        if ( GraphicConverter::Export( *pStream, aGraph, nFormat ) == ERRCODE_NONE )
66
0
            pResult = std::move(pStream);
67
0
    }
68
69
0
    return pResult;
70
0
}
71
72
73
// static
74
void* GraphicHelper::getEnhMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta )
75
0
{
76
0
    void* pResult = nullptr;
77
78
#ifdef _WIN32
79
    if ( pGDIMeta )
80
    {
81
        ::utl::TempFileNamed aTempFile( u"", true, u".emf" );
82
83
        OUString aMetaFile = aTempFile.GetFileName();
84
        OUString aMetaURL = aTempFile.GetURL();
85
86
        std::unique_ptr<SvStream> pStream = ::utl::UcbStreamHelper::CreateStream( aMetaURL, StreamMode::STD_READWRITE );
87
        if ( pStream )
88
        {
89
            Graphic aGraph( *pGDIMeta );
90
            ErrCode nFailed = GraphicConverter::Export( *pStream, aGraph, ConvertDataFormat::EMF );
91
            pStream->Flush();
92
            pStream.reset();
93
94
            if ( !nFailed )
95
                pResult = GetEnhMetaFileW( o3tl::toW(aMetaFile.getStr()) );
96
        }
97
    }
98
#else
99
0
    (void)pGDIMeta;  // unused
100
0
#endif
101
102
0
    return pResult;
103
0
}
104
105
106
// static
107
void* GraphicHelper::getWinMetaFileFromGDI_Impl( const GDIMetaFile* pGDIMeta, const Size& aMetaSize )
108
0
{
109
0
    void* pResult = nullptr;
110
111
#ifdef _WIN32
112
    if ( pGDIMeta )
113
    {
114
        SvMemoryStream pStream( 65535, 65535 );
115
        Graphic aGraph( *pGDIMeta );
116
        ErrCode nFailed = GraphicConverter::Export( pStream, aGraph, ConvertDataFormat::WMF );
117
        pStream.Flush();
118
        if ( !nFailed )
119
        {
120
            sal_uInt64 nLength = pStream.TellEnd();
121
            if ( nLength > 22 )
122
            {
123
                HMETAFILE hMeta = SetMetaFileBitsEx( nLength - 22,
124
                                static_cast< const unsigned char*>( pStream.GetData() ) + 22 );
125
126
                if ( hMeta )
127
                {
128
                    HGLOBAL hMemory = GlobalAlloc( GMEM_DDESHARE | GMEM_MOVEABLE, sizeof( METAFILEPICT ) );
129
130
                    if ( hMemory )
131
                    {
132
                        if (METAFILEPICT* pMF = static_cast<METAFILEPICT*>(GlobalLock(hMemory)))
133
                        {
134
                            pMF->hMF = hMeta;
135
                            pMF->mm = MM_ANISOTROPIC;
136
137
                            MapMode aWinMode( MapUnit::Map100thMM );
138
139
                            if ( aWinMode == pGDIMeta->GetPrefMapMode() )
140
                            {
141
                                pMF->xExt = aMetaSize.Width();
142
                                pMF->yExt = aMetaSize.Height();
143
                            }
144
                            else
145
                            {
146
                                Size aWinSize = OutputDevice::LogicToLogic( Size( aMetaSize.Width(), aMetaSize.Height() ),
147
                                                                            pGDIMeta->GetPrefMapMode(),
148
                                                                            aWinMode );
149
                                pMF->xExt = aWinSize.Width();
150
                                pMF->yExt = aWinSize.Height();
151
                            }
152
153
                            GlobalUnlock(hMemory);
154
                        }
155
                        pResult = static_cast<void*>(hMemory);
156
                    }
157
                    else
158
                           DeleteMetaFile( hMeta );
159
                }
160
            }
161
        }
162
    }
163
#else
164
0
    (void)pGDIMeta;  // unused
165
0
    (void)aMetaSize; // unused
166
0
#endif
167
168
169
0
    return pResult;
170
0
}
171
172
173
// static
174
bool GraphicHelper::getThumbnailFormatFromBitmap_Impl(const Bitmap& rBitmap, const uno::Reference<io::XStream>& xStream)
175
0
{
176
0
    if (rBitmap.IsEmpty() || !xStream.is())
177
0
        return false;
178
179
0
    std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xStream));
180
181
0
    if (pStream->GetError())
182
0
        return false;
183
184
0
    Bitmap bitmap(rBitmap);
185
0
    bitmap.Convert(BmpConversion::N8BitColors);
186
187
0
    GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
188
189
0
    if (rFilter.compressAsPNG(bitmap, *pStream) != ERRCODE_NONE)
190
0
        return false;
191
192
0
    pStream->FlushBuffer();
193
194
0
    return !pStream->GetError();
195
0
}
196
197
// static
198
bool GraphicHelper::getThumbnailReplacement_Impl(std::u16string_view rResID, const uno::Reference< io::XStream >& xStream )
199
0
{
200
0
    bool bResult = false;
201
0
    if (!rResID.empty() && xStream.is())
202
0
    {
203
0
        const uno::Reference< uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
204
0
        try
205
0
        {
206
0
            uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
207
0
            const OUString aURL{OUString::Concat("private:graphicrepository/") + rResID};
208
209
0
            uno::Sequence< beans::PropertyValue > aMediaProps{ comphelper::makePropertyValue(u"URL"_ustr,
210
0
                                                                                             aURL) };
211
212
0
            uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
213
0
            if ( xGraphic.is() )
214
0
            {
215
0
                uno::Sequence< beans::PropertyValue > aStoreProps{
216
0
                    comphelper::makePropertyValue(u"OutputStream"_ustr, xStream),
217
0
                    comphelper::makePropertyValue(u"MimeType"_ustr, u"image/png"_ustr)
218
0
                };
219
220
0
                xGraphProvider->storeGraphic( xGraphic, aStoreProps );
221
0
                bResult = true;
222
0
            }
223
0
        }
224
0
        catch(const uno::Exception&)
225
0
        {
226
0
        }
227
0
    }
228
229
0
    return bResult;
230
0
}
231
232
// static
233
OUString GraphicHelper::getThumbnailReplacementIDByFactoryName_Impl( std::u16string_view aFactoryShortName )
234
0
{
235
0
    OUString sResult;
236
237
0
    if ( aFactoryShortName == u"scalc" )
238
0
    {
239
0
        sResult = BMP_128X128_CALC_DOC;
240
0
    }
241
0
    else if ( aFactoryShortName == u"sdraw" )
242
0
    {
243
0
        sResult = BMP_128X128_DRAW_DOC;
244
0
    }
245
0
    else if ( aFactoryShortName == u"simpress" )
246
0
    {
247
0
        sResult = BMP_128X128_IMPRESS_DOC;
248
0
    }
249
0
    else if ( aFactoryShortName == u"smath" )
250
0
    {
251
0
        sResult = BMP_128X128_MATH_DOC;
252
0
    }
253
0
    else if ( aFactoryShortName == u"swriter" || o3tl::starts_with(aFactoryShortName, u"swriter/") )
254
0
    {
255
0
        sResult = BMP_128X128_WRITER_DOC;
256
0
    }
257
258
0
    return sResult;
259
0
}
260
261
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */