LibreOffice Module sdext (master)  1
imagecontainer.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  * 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 #include <imagecontainer.hxx>
22 #include <genericelements.hxx>
23 #include <xmlemitter.hxx>
24 
25 #include <rtl/ustrbuf.hxx>
26 #include <sal/log.hxx>
27 #include <osl/diagnose.h>
28 
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 
31 using namespace com::sun::star;
32 
33 namespace pdfi
34 {
35 
36 namespace
37 {
38 
39 static const char aBase64EncodeTable[] =
40  { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
41  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
42  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
43  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
44  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
45 
46 OUString encodeBase64( const sal_Int8* i_pBuffer, const sal_uInt32 i_nBufferLength )
47 {
48  OUStringBuffer aBuf( (i_nBufferLength+1) * 4 / 3 );
49  const sal_Int32 nRemain(i_nBufferLength%3);
50  const sal_Int32 nFullTripleLength( i_nBufferLength - (i_nBufferLength%3));
51  sal_Int32 nBufPos( 0 );
52  for( sal_Int32 i = 0; i < nFullTripleLength; i += 3, nBufPos += 4 )
53  {
54  const sal_Int32 nBinary = (static_cast<sal_uInt8>(i_pBuffer[i + 0]) << 16) +
55  (static_cast<sal_uInt8>(i_pBuffer[i + 1]) << 8) +
56  static_cast<sal_uInt8>(i_pBuffer[i + 2]);
57 
58  aBuf.append("====");
59 
60  sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
61  aBuf[nBufPos] = aBase64EncodeTable [nIndex];
62 
63  nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
64  aBuf[nBufPos+1] = aBase64EncodeTable [nIndex];
65 
66  nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
67  aBuf[nBufPos+2] = aBase64EncodeTable [nIndex];
68 
69  nIndex = static_cast<sal_uInt8>((nBinary & 0x3F));
70  aBuf[nBufPos+3] = aBase64EncodeTable [nIndex];
71  }
72  if( nRemain > 0 )
73  {
74  aBuf.append("====");
75  sal_Int32 nBinary( 0 );
76  const sal_Int32 nStart(i_nBufferLength-nRemain);
77  switch(nRemain)
78  {
79  case 1: nBinary = static_cast<sal_uInt8>(i_pBuffer[nStart + 0]) << 16;
80  break;
81  case 2: nBinary = (static_cast<sal_uInt8>(i_pBuffer[nStart + 0]) << 16) +
82  (static_cast<sal_uInt8>(i_pBuffer[nStart + 1]) << 8);
83  break;
84  }
85  sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
86  aBuf[nBufPos] = aBase64EncodeTable [nIndex];
87 
88  nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
89  aBuf[nBufPos+1] = aBase64EncodeTable [nIndex];
90 
91  if( nRemain == 2 )
92  {
93  nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
94  aBuf[nBufPos+2] = aBase64EncodeTable [nIndex];
95  }
96  }
97 
98  return aBuf.makeStringAndClear();
99 }
100 
101 } // namespace
102 
103 ImageContainer::ImageContainer() :
104  m_aImages()
105 {}
106 
107 ImageId ImageContainer::addImage( const uno::Sequence<beans::PropertyValue>& xBitmap )
108 {
109  m_aImages.push_back( xBitmap );
110  return m_aImages.size()-1;
111 }
112 
114 {
115  OSL_ASSERT( nId >= 0 && nId < ImageId( m_aImages.size()) );
116 
117  const uno::Sequence<beans::PropertyValue>& rEntry( m_aImages[nId] );
118 
119  // find "InputSequence" property
120  const beans::PropertyValue* pAry(rEntry.getConstArray());
121  const sal_Int32 nLen(rEntry.getLength());
122  const beans::PropertyValue* pValue(
123  std::find_if(pAry, pAry+nLen,
124  [] (beans::PropertyValue const& v) -> bool {
125  return v.Name == "InputSequence";
126  }));
127 
128  if (pValue == pAry + nLen )
129  {
130  SAL_WARN("sdext.pdfimport", "InputSequence not found");
131  return;
132  }
133 
134  uno::Sequence<sal_Int8> aData;
135  if( !(pValue->Value >>= aData) )
136  {
137  SAL_WARN("sdext.pdfimport", "Wrong data type");
138  return;
139  }
140 
141  rContext.rEmitter.write( encodeBase64( aData.getConstArray(), aData.getLength() ));
142 }
143 
144 }
145 
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
const char aData[]
ImageId addImage(const css::uno::Sequence< css::beans::PropertyValue > &xBitmap)
signed char sal_Int8
aBuf
sal_Int32 ImageId
Definition: pdfihelper.hxx:46
int i
float v
const char aBase64EncodeTable[]
unsigned char sal_uInt8
std::vector< css::uno::Sequence< css::beans::PropertyValue > > m_aImages
XmlEmitter & rEmitter
#define SAL_WARN(area, stream)
void writeBase64EncodedStream(ImageId nImageId, EmitContext &rContext)
virtual void write(const OUString &rString)=0
Write PCTEXT as-is to output.