LibreOffice Module vcl (master)  1
imap2.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 <comphelper/string.hxx>
22 #include <string.h>
23 #include <rtl/strbuf.hxx>
24 #include <vcl/outdev.hxx>
25 #include <vcl/svapp.hxx>
26 #include <tools/urlobj.hxx>
27 
28 #include <svl/urihelper.hxx>
29 #include <vcl/imap.hxx>
30 #include <vcl/imapobj.hxx>
31 #include <vcl/imaprect.hxx>
32 #include <vcl/imapcirc.hxx>
33 #include <vcl/imappoly.hxx>
34 
35 #include <math.h>
36 
37 #define NOTEOL(c) ((c)!='\0')
38 
39 void IMapObject::AppendCERNCoords(OStringBuffer& rBuf, const Point& rPoint100)
40 {
41  const Point aPixPt( Application::GetDefaultDevice()->LogicToPixel( rPoint100, MapMode( MapUnit::Map100thMM ) ) );
42 
43  rBuf.append('(');
44  rBuf.append(static_cast<sal_Int32>(aPixPt.X()));
45  rBuf.append(',');
46  rBuf.append(static_cast<sal_Int32>(aPixPt.Y()));
47  rBuf.append(") ");
48 }
49 
50 void IMapObject::AppendNCSACoords(OStringBuffer& rBuf, const Point& rPoint100)
51 {
52  const Point aPixPt( Application::GetDefaultDevice()->LogicToPixel( rPoint100, MapMode( MapUnit::Map100thMM ) ) );
53 
54  rBuf.append(static_cast<sal_Int32>(aPixPt.X()));
55  rBuf.append(',');
56  rBuf.append(static_cast<sal_Int32>(aPixPt.Y()));
57  rBuf.append(' ');
58 }
59 
60 void IMapObject::AppendCERNURL(OStringBuffer& rBuf) const
61 {
62  rBuf.append(OUStringToOString(URIHelper::simpleNormalizedMakeRelative("", aURL), osl_getThreadTextEncoding()));
63 }
64 
65 void IMapObject::AppendNCSAURL(OStringBuffer& rBuf) const
66 {
67  rBuf.append(OUStringToOString(URIHelper::simpleNormalizedMakeRelative("", aURL), osl_getThreadTextEncoding()));
68  rBuf.append(' ');
69 }
70 
72 {
73  OStringBuffer aStrBuf("rectangle ");
74 
75  AppendCERNCoords(aStrBuf, aRect.TopLeft());
77  AppendCERNURL(aStrBuf);
78 
79  rOStm.WriteLine(aStrBuf.makeStringAndClear());
80 }
81 
83 {
84  OStringBuffer aStrBuf("rect ");
85 
86  AppendNCSAURL(aStrBuf);
87  AppendNCSACoords(aStrBuf, aRect.TopLeft());
89 
90  rOStm.WriteLine(aStrBuf.makeStringAndClear());
91 }
92 
94 {
95  OStringBuffer aStrBuf("circle ");
96 
97  AppendCERNCoords(aStrBuf, aCenter);
98  aStrBuf.append(nRadius);
99  aStrBuf.append(' ');
100  AppendCERNURL(aStrBuf);
101 
102  rOStm.WriteLine(aStrBuf.makeStringAndClear());
103 }
104 
106 {
107  OStringBuffer aStrBuf("circle ");
108 
109  AppendNCSAURL(aStrBuf);
110  AppendNCSACoords(aStrBuf, aCenter);
111  AppendNCSACoords(aStrBuf, aCenter + Point(nRadius, 0));
112 
113  rOStm.WriteLine(aStrBuf.makeStringAndClear());
114 }
115 
117 {
118  OStringBuffer aStrBuf("polygon ");
119  const sal_uInt16 nCount = aPoly.GetSize();
120 
121  for (sal_uInt16 i = 0; i < nCount; ++i)
122  AppendCERNCoords(aStrBuf, aPoly[i]);
123 
124  AppendCERNURL(aStrBuf);
125 
126  rOStm.WriteLine(aStrBuf.makeStringAndClear());
127 }
128 
130 {
131  OStringBuffer aStrBuf("poly ");
132  const sal_uInt16 nCount = std::min( aPoly.GetSize(), sal_uInt16(100) );
133 
134  AppendNCSAURL(aStrBuf);
135 
136  for (sal_uInt16 i = 0; i < nCount; ++i)
137  AppendNCSACoords(aStrBuf, aPoly[i]);
138 
139  rOStm.WriteLine(aStrBuf.makeStringAndClear());
140 }
141 
142 void ImageMap::Write( SvStream& rOStm, IMapFormat nFormat ) const
143 {
144  switch( nFormat )
145  {
146  case IMapFormat::Binary : Write( rOStm ); break;
147  case IMapFormat::CERN : ImpWriteCERN( rOStm ); break;
148  case IMapFormat::NCSA : ImpWriteNCSA( rOStm ); break;
149 
150  default:
151  break;
152  }
153 }
154 
155 void ImageMap::ImpWriteCERN( SvStream& rOStm ) const
156 {
157  size_t nCount = maList.size();
158 
159  for ( size_t i = 0; i < nCount; i++ )
160  {
161  IMapObject* pObj = maList[ i ].get();
162 
163  switch( pObj->GetType() )
164  {
166  static_cast<IMapRectangleObject*>( pObj )->WriteCERN( rOStm );
167  break;
168 
170  static_cast<IMapCircleObject*>( pObj )->WriteCERN( rOStm );
171  break;
172 
174  static_cast<IMapPolygonObject*>( pObj )->WriteCERN( rOStm );
175  break;
176 
177  default:
178  break;
179  }
180  }
181 }
182 
183 void ImageMap::ImpWriteNCSA( SvStream& rOStm ) const
184 {
185  size_t nCount = maList.size();
186 
187  for ( size_t i = 0; i < nCount; i++ )
188  {
189  IMapObject* pObj = maList[ i ].get();
190 
191  switch( pObj->GetType() )
192  {
194  static_cast<IMapRectangleObject*>( pObj )->WriteNCSA( rOStm );
195  break;
196 
198  static_cast<IMapCircleObject*>( pObj )->WriteNCSA( rOStm );
199  break;
200 
202  static_cast<IMapPolygonObject*>( pObj )->WriteNCSA( rOStm );
203  break;
204 
205  default:
206  break;
207  }
208  }
209 }
210 
212 {
213  sal_uLong nRet = IMAP_ERR_FORMAT;
214 
215  if ( nFormat == IMapFormat::Detect )
216  nFormat = ImpDetectFormat( rIStm );
217 
218  switch ( nFormat )
219  {
220  case IMapFormat::Binary : Read( rIStm ); break;
221  case IMapFormat::CERN : ImpReadCERN( rIStm ); break;
222  case IMapFormat::NCSA : ImpReadNCSA( rIStm ); break;
223 
224  default:
225  break;
226  }
227 
228  if ( !rIStm.GetError() )
229  nRet = IMAP_ERR_OK;
230 
231  return nRet;
232 }
233 
235 {
236  // delete old content
237  ClearImageMap();
238 
239  OString aStr;
240  while ( rIStm.ReadLine( aStr ) )
241  ImpReadCERNLine( aStr );
242 }
243 
244 void ImageMap::ImpReadCERNLine( std::string_view rLine )
245 {
246  OString aStr = comphelper::string::stripStart(rLine, ' ');
247  aStr = comphelper::string::stripStart(aStr, '\t');
248  aStr = aStr.replaceAll(";", "");
249  aStr = aStr.toAsciiLowerCase();
250 
251  const char* pStr = aStr.getStr();
252  char cChar = *pStr++;
253 
254  // find instruction
255  OStringBuffer aBuf;
256  while ((cChar >= 'a') && (cChar <= 'z'))
257  {
258  aBuf.append(cChar);
259  cChar = *pStr++;
260  }
261  OString aToken = aBuf.makeStringAndClear();
262 
263  if ( !(NOTEOL( cChar )) )
264  return;
265 
266  if ( ( aToken == "rectangle" ) || ( aToken == "rect" ) )
267  {
268  const Point aTopLeft( ImpReadCERNCoords( &pStr ) );
269  const Point aBottomRight( ImpReadCERNCoords( &pStr ) );
270  const OUString aURL( ImpReadCERNURL( &pStr ) );
271  const tools::Rectangle aRect( aTopLeft, aBottomRight );
272 
273  maList.emplace_back( new IMapRectangleObject( aRect, aURL, OUString(), OUString(), OUString(), OUString() ) );
274  }
275  else if ( ( aToken == "circle" ) || ( aToken == "circ" ) )
276  {
277  const Point aCenter( ImpReadCERNCoords( &pStr ) );
278  const tools::Long nRadius = ImpReadCERNRadius( &pStr );
279  const OUString aURL( ImpReadCERNURL( &pStr ) );
280 
281  maList.emplace_back( new IMapCircleObject( aCenter, nRadius, aURL, OUString(), OUString(), OUString(), OUString() ) );
282  }
283  else if ( ( aToken == "polygon" ) || ( aToken == "poly" ) )
284  {
285  const sal_uInt16 nCount = comphelper::string::getTokenCount(aStr, '(') - 1;
286  tools::Polygon aPoly( nCount );
287 
288  for ( sal_uInt16 i = 0; i < nCount; i++ )
289  aPoly[ i ] = ImpReadCERNCoords( &pStr );
290 
291  const OUString aURL = ImpReadCERNURL( &pStr );
292 
293  maList.emplace_back( new IMapPolygonObject( aPoly, aURL, OUString(), OUString(), OUString(), OUString() ) );
294  }
295 }
296 
297 Point ImageMap::ImpReadCERNCoords( const char** ppStr )
298 {
299  OUStringBuffer aStrX;
300  OUStringBuffer aStrY;
301  Point aPt;
302  char cChar = *(*ppStr)++;
303 
304  while( NOTEOL( cChar ) && ( ( cChar < '0' ) || ( cChar > '9' ) ) )
305  cChar = *(*ppStr)++;
306 
307  if ( NOTEOL( cChar ) )
308  {
309  while( NOTEOL( cChar ) && ( cChar >= '0' ) && ( cChar <= '9' ) )
310  {
311  aStrX.append( cChar );
312  cChar = *(*ppStr)++;
313  }
314 
315  if ( NOTEOL( cChar ) )
316  {
317  while( NOTEOL( cChar ) && ( ( cChar < '0' ) || ( cChar > '9' ) ) )
318  cChar = *(*ppStr)++;
319 
320  while( NOTEOL( cChar ) && ( cChar >= '0' ) && ( cChar <= '9' ) )
321  {
322  aStrY.append( cChar );
323  cChar = *(*ppStr)++;
324  }
325 
326  if ( NOTEOL( cChar ) )
327  while( NOTEOL( cChar ) && ( cChar != ')' ) )
328  cChar = *(*ppStr)++;
329 
330  aPt = Point( aStrX.makeStringAndClear().toInt32(), aStrY.makeStringAndClear().toInt32() );
331  }
332  }
333 
334  return aPt;
335 }
336 
338 {
339  OUStringBuffer aStr;
340  char cChar = *(*ppStr)++;
341 
342  while( NOTEOL( cChar ) && ( ( cChar < '0' ) || ( cChar > '9' ) ) )
343  cChar = *(*ppStr)++;
344 
345  if ( NOTEOL( cChar ) )
346  {
347  while( NOTEOL( cChar ) && ( cChar >= '0' ) && ( cChar <= '9' ) )
348  {
349  aStr.append( cChar );
350  cChar = *(*ppStr)++;
351  }
352  }
353 
354  return aStr.makeStringAndClear().toInt32();
355 }
356 
357 OUString ImageMap::ImpReadCERNURL( const char** ppStr )
358 {
359  OUString aStr(OUString::createFromAscii(*ppStr));
360 
361  aStr = comphelper::string::strip(aStr, ' ');
362  aStr = comphelper::string::strip(aStr, '\t');
363 
364  return INetURLObject::GetAbsURL( "", aStr );
365 }
366 
368 {
369  // delete old content
370  ClearImageMap();
371 
372  OString aStr;
373  while ( rIStm.ReadLine( aStr ) )
374  ImpReadNCSALine( aStr );
375 }
376 
377 void ImageMap::ImpReadNCSALine( std::string_view rLine )
378 {
379  OString aStr = comphelper::string::stripStart(rLine, ' ');
380  aStr = comphelper::string::stripStart(aStr, '\t');
381  aStr = aStr.replaceAll(";", "");
382  aStr = aStr.toAsciiLowerCase();
383 
384  const char* pStr = aStr.getStr();
385  char cChar = *pStr++;
386 
387  // find instruction
388  OStringBuffer aBuf;
389  while ((cChar >= 'a') && (cChar <= 'z'))
390  {
391  aBuf.append(cChar);
392  cChar = *pStr++;
393  }
394  OString aToken = aBuf.makeStringAndClear();
395 
396  if ( !(NOTEOL( cChar )) )
397  return;
398 
399  if ( aToken == "rect" )
400  {
401  const OUString aURL( ImpReadNCSAURL( &pStr ) );
402  const Point aTopLeft( ImpReadNCSACoords( &pStr ) );
403  const Point aBottomRight( ImpReadNCSACoords( &pStr ) );
404  const tools::Rectangle aRect( aTopLeft, aBottomRight );
405 
406  maList.emplace_back( new IMapRectangleObject( aRect, aURL, OUString(), OUString(), OUString(), OUString() ) );
407  }
408  else if ( aToken == "circle" )
409  {
410  const OUString aURL( ImpReadNCSAURL( &pStr ) );
411  const Point aCenter( ImpReadNCSACoords( &pStr ) );
412  const Point aDX( aCenter - ImpReadNCSACoords( &pStr ) );
413  tools::Long nRadius = static_cast<tools::Long>(sqrt( static_cast<double>(aDX.X()) * aDX.X() +
414  static_cast<double>(aDX.Y()) * aDX.Y() ));
415 
416  maList.emplace_back( new IMapCircleObject( aCenter, nRadius, aURL, OUString(), OUString(), OUString(), OUString() ) );
417  }
418  else if ( aToken == "poly" )
419  {
420  const sal_uInt16 nCount = comphelper::string::getTokenCount(aStr, ',') - 1;
421  const OUString aURL( ImpReadNCSAURL( &pStr ) );
422  tools::Polygon aPoly( nCount );
423 
424  for ( sal_uInt16 i = 0; i < nCount; i++ )
425  aPoly[ i ] = ImpReadNCSACoords( &pStr );
426 
427  maList.emplace_back( new IMapPolygonObject( aPoly, aURL, OUString(), OUString(), OUString(), OUString() ) );
428  }
429 }
430 
431 OUString ImageMap::ImpReadNCSAURL( const char** ppStr )
432 {
433  OUStringBuffer aStr;
434  char cChar = *(*ppStr)++;
435 
436  while( NOTEOL( cChar ) && ( ( cChar == ' ' ) || ( cChar == '\t' ) ) )
437  cChar = *(*ppStr)++;
438 
439  if ( NOTEOL( cChar ) )
440  {
441  while( NOTEOL( cChar ) && ( cChar != ' ' ) && ( cChar != '\t' ) )
442  {
443  aStr.append( cChar );
444  cChar = *(*ppStr)++;
445  }
446  }
447 
448  return INetURLObject::GetAbsURL( "", aStr.makeStringAndClear() );
449 }
450 
451 Point ImageMap::ImpReadNCSACoords( const char** ppStr )
452 {
453  OUStringBuffer aStrX;
454  OUStringBuffer aStrY;
455  Point aPt;
456  char cChar = *(*ppStr)++;
457 
458  while( NOTEOL( cChar ) && ( ( cChar < '0' ) || ( cChar > '9' ) ) )
459  cChar = *(*ppStr)++;
460 
461  if ( NOTEOL( cChar ) )
462  {
463  while( NOTEOL( cChar ) && ( cChar >= '0' ) && ( cChar <= '9' ) )
464  {
465  aStrX.append( cChar );
466  cChar = *(*ppStr)++;
467  }
468 
469  if ( NOTEOL( cChar ) )
470  {
471  while( NOTEOL( cChar ) && ( ( cChar < '0' ) || ( cChar > '9' ) ) )
472  cChar = *(*ppStr)++;
473 
474  while( NOTEOL( cChar ) && ( cChar >= '0' ) && ( cChar <= '9' ) )
475  {
476  aStrY.append( cChar );
477  cChar = *(*ppStr)++;
478  }
479 
480  aPt = Point( aStrX.makeStringAndClear().toInt32(), aStrY.makeStringAndClear().toInt32() );
481  }
482  }
483 
484  return aPt;
485 }
486 
488 {
489  sal_uInt64 nPos = rIStm.Tell();
491  char cMagic[6];
492 
493  rIStm.ReadBytes(cMagic, sizeof(cMagic));
494 
495  // if we do not have an internal formats
496  // we check the format
497  if ( memcmp( cMagic, IMAPMAGIC, sizeof( cMagic ) ) )
498  {
499  tools::Long nCount = 128;
500 
501  rIStm.Seek( nPos );
502  OString aStr;
503  while ( rIStm.ReadLine( aStr ) && nCount-- )
504  {
505  aStr = aStr.toAsciiLowerCase();
506 
507  if ( (aStr.indexOf("rect") != -1) ||
508  (aStr.indexOf("circ") != -1) ||
509  (aStr.indexOf("poly") != -1) )
510  {
511  if ( ( aStr.indexOf('(') != -1 ) &&
512  ( aStr.indexOf(')') != -1 ) )
513  {
514  nRet = IMapFormat::CERN;
515  }
516  else
517  nRet = IMapFormat::NCSA;
518 
519  break;
520  }
521  }
522  }
523 
524  rIStm.Seek( nPos );
525 
526  return nRet;
527 }
528 
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString aURL
Definition: imapobj.hxx:59
static Point ImpReadCERNCoords(const char **ppStr)
Definition: imap2.cxx:297
tools::Rectangle aRect
Definition: imaprect.hxx:30
void ImpWriteNCSA(SvStream &rOStm) const
Definition: imap2.cxx:183
URL aURL
void Write(SvStream &rOStm) const
Definition: imap.cxx:919
sal_Int32 nRadius
Definition: imapcirc.hxx:31
static IMapFormat ImpDetectFormat(SvStream &rIStm)
Definition: imap2.cxx:487
OString strip(std::string_view rIn, char c)
sal_uIntPtr sal_uLong
long Long
aBuf
#define NOTEOL(c)
Definition: imap2.cxx:37
sal_uInt64 Seek(sal_uInt64 nPos)
IMapFormat
Definition: imapobj.hxx:44
bool WriteLine(std::string_view rStr)
tools::Polygon aPoly
Definition: imappoly.hxx:31
#define IMAPMAGIC
Definition: imapobj.hxx:39
SVL_DLLPUBLIC OUString simpleNormalizedMakeRelative(OUString const &baseUriReference, OUString const &uriReference)
std::vector< std::unique_ptr< IMapObject > > maList
Definition: imap.hxx:39
static OutputDevice * GetDefaultDevice()
Get the default "device" (in this case the default window).
Definition: svapp.cxx:1069
void WriteCERN(SvStream &rOStm) const
Definition: imap2.cxx:71
void AppendNCSAURL(OStringBuffer &rBuf) const
Definition: imap2.cxx:65
static Point ImpReadNCSACoords(const char **ppStr)
Definition: imap2.cxx:451
ErrCode GetError() const
bool ReadLine(OString &rStr, sal_Int32 nMaxBytesToRead=0xFFFE)
void ImpReadNCSALine(std::string_view rLine)
Definition: imap2.cxx:377
int nCount
static OUString GetAbsURL(OUString const &rTheBaseURIRef, OUString const &rTheRelURIRef, EncodeMechanism eEncodeMechanism=EncodeMechanism::WasEncoded, DecodeMechanism eDecodeMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
void ImpReadNCSA(SvStream &rOStm)
Definition: imap2.cxx:367
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
static void AppendNCSACoords(OStringBuffer &rBuf, const Point &rPoint100)
Definition: imap2.cxx:50
int i
static void AppendCERNCoords(OStringBuffer &rBuf, const Point &rPoint100)
Definition: imap2.cxx:39
void ImpWriteCERN(SvStream &rOStm) const
Definition: imap2.cxx:155
sal_uInt16 GetSize() const
constexpr Point TopLeft() const
std::size_t ReadBytes(void *pData, std::size_t nSize)
static OUString ImpReadCERNURL(const char **ppStr)
Definition: imap2.cxx:357
void ImpReadCERNLine(std::string_view rLine)
Definition: imap2.cxx:244
void WriteNCSA(SvStream &rOStm) const
Definition: imap2.cxx:82
#define IMAP_ERR_OK
Definition: imapobj.hxx:52
OString stripStart(std::string_view rIn, char c)
void Read(SvStream &rIStm)
Definition: imap.cxx:955
void WriteNCSA(SvStream &rOStm) const
Definition: imap2.cxx:129
static OUString ImpReadNCSAURL(const char **ppStr)
Definition: imap2.cxx:431
sal_uInt64 Tell() const
void WriteCERN(SvStream &rOStm) const
Definition: imap2.cxx:93
void WriteNCSA(SvStream &rOStm) const
Definition: imap2.cxx:105
constexpr Point BottomRight() const
virtual IMapObjectType GetType() const =0
#define IMAP_ERR_FORMAT
Definition: imapobj.hxx:53
void AppendCERNURL(OStringBuffer &rBuf) const
Definition: imap2.cxx:60
void WriteCERN(SvStream &rOStm) const
Definition: imap2.cxx:116
aStr
static tools::Long ImpReadCERNRadius(const char **ppStr)
Definition: imap2.cxx:337
void ClearImageMap()
Definition: imap.cxx:614
sal_uInt16 nPos
void ImpReadCERN(SvStream &rOStm)
Definition: imap2.cxx:234