tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / starmath / source / smdetect.cxx
blob56e2157e55c36c6d4f12792a05b3d9c407f5ad5e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "smdetect.hxx"
21 #include <cppuhelper/supportsservice.hxx>
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <com/sun/star/ucb/ContentCreationException.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <sfx2/docfile.hxx>
26 #include <unotools/mediadescriptor.hxx>
27 #include <sot/storage.hxx>
28 #include <comphelper/diagnose_ex.hxx>
30 #include "eqnolefilehdr.hxx"
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::io;
35 using namespace ::com::sun::star::task;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::lang;
38 using utl::MediaDescriptor;
40 SmFilterDetect::SmFilterDetect()
44 SmFilterDetect::~SmFilterDetect()
48 OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor )
50 MediaDescriptor aMediaDesc( lDescriptor );
51 uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM], uno::UNO_QUERY );
52 if ( !xInStream.is() )
53 return OUString();
55 SfxMedium aMedium;
56 aMedium.UseInteractionHandler( false );
57 aMedium.setStreamToLoadFrom( xInStream, true );
59 SvStream *pInStrm = aMedium.GetInStream();
60 if ( !pInStrm || pInStrm->GetError() )
61 return OUString();
63 // Do not attempt to create an SotStorage on a
64 // 0-length stream as that would create the compound
65 // document header on the stream and effectively write to
66 // disk!
67 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
68 if ( pInStrm->remainingSize() == 0 )
69 return OUString();
71 bool bStorageOk = false;
72 try
74 rtl::Reference<SotStorage> aStorage = new SotStorage(pInStrm, false);
75 bStorageOk = !aStorage->GetError();
76 if (bStorageOk)
78 if ( aStorage->IsStream(u"Equation Native"_ustr) )
80 sal_uInt8 nVersion;
81 if ( GetMathTypeVersion( aStorage.get(), nVersion ) && nVersion <=3 )
82 return u"math_MathType_3x"_ustr;
86 catch (const css::ucb::ContentCreationException &)
88 TOOLS_WARN_EXCEPTION("starmath", "SmFilterDetect::detect caught" );
91 if (!bStorageOk)
93 // 200 should be enough for the XML
94 // version, encoding and !DOCTYPE
95 // stuff I hope?
96 static const sal_uInt16 nBufferSize = 200;
97 char aBuffer[nBufferSize+1];
98 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
99 pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW ); // avoid BOM marker
100 auto nBytesRead = pInStrm->ReadBytes( aBuffer, nBufferSize );
101 if (nBytesRead >= 6)
103 aBuffer[nBytesRead] = 0;
104 bool bIsMathType = false;
105 if (0 == strncmp( "<?xml", aBuffer, 5))
106 bIsMathType = (strstr( aBuffer, "<math>" ) ||
107 strstr( aBuffer, "<math " ) ||
108 strstr( aBuffer, "<math:math " ));
109 else
110 // this is the old <math tag to MathML in the beginning of the XML file
111 bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) ||
112 0 == strncmp( "<math> ", aBuffer, 7) ||
113 0 == strncmp( "<math:math> ", aBuffer, 12));
115 if ( bIsMathType )
116 return u"math_MathML_XML_Math"_ustr;
120 return OUString();
123 /* XServiceInfo */
124 OUString SAL_CALL SmFilterDetect::getImplementationName()
126 return u"com.sun.star.comp.math.FormatDetector"_ustr;
129 /* XServiceInfo */
130 sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName )
132 return cppu::supportsService(this, sServiceName);
135 /* XServiceInfo */
136 Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames()
138 return { u"com.sun.star.frame.ExtendedTypeDetection"_ustr };
141 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
142 math_FormatDetector_get_implementation(uno::XComponentContext* /*pCtx*/,
143 uno::Sequence<uno::Any> const& /*rSeq*/)
145 return cppu::acquire(new SmFilterDetect);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */