LCOV - code coverage report
Current view: top level - vcl/source/gdi - pdfwriter_impl.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 1 6825 0.1 %
Date: 2014-04-14 Functions: 2 232 0.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #include <sal/types.h>
      21             : 
      22             : #include <math.h>
      23             : #include <algorithm>
      24             : #include <lcms2.h>
      25             : 
      26             : #include <basegfx/matrix/b2dhommatrix.hxx>
      27             : #include <basegfx/polygon/b2dpolygon.hxx>
      28             : #include <basegfx/polygon/b2dpolygontools.hxx>
      29             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      30             : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
      31             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      32             : #include <boost/scoped_array.hpp>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : #include <com/sun/star/util/URL.hpp>
      35             : #include <com/sun/star/util/URLTransformer.hpp>
      36             : #include <comphelper/processfactory.hxx>
      37             : #include <comphelper/string.hxx>
      38             : #include <cppuhelper/implbase1.hxx>
      39             : #include <i18nlangtag/languagetag.hxx>
      40             : #include <osl/file.h>
      41             : #include <osl/thread.h>
      42             : #include <rtl/crc.h>
      43             : #include <rtl/digest.h>
      44             : #include <rtl/ustrbuf.hxx>
      45             : #include <tools/debug.hxx>
      46             : #include <tools/stream.hxx>
      47             : #include <tools/urlobj.hxx>
      48             : #include <tools/zcodec.hxx>
      49             : #include <vcl/bitmapex.hxx>
      50             : #include <vcl/bmpacc.hxx>
      51             : #include <vcl/cvtgrf.hxx>
      52             : #include <vcl/image.hxx>
      53             : #include <vcl/lineinfo.hxx>
      54             : #include <vcl/metric.hxx>
      55             : #include <vcl/settings.hxx>
      56             : #include <vcl/strhelper.hxx>
      57             : #include <vcl/svapp.hxx>
      58             : #include <vcl/virdev.hxx>
      59             : 
      60             : #include "fontsubset.hxx"
      61             : #include "outdev.h"
      62             : #include "PhysicalFontFace.hxx"
      63             : #include "salgdi.hxx"
      64             : #include "sallayout.hxx"
      65             : #include "textlayout.hxx"
      66             : 
      67             : #include "pdfwriter_impl.hxx"
      68             : 
      69             : #if !defined(ANDROID) && !defined(IOS)
      70             : // NSS header files for PDF signing support
      71             : #include "nss.h"
      72             : #include "cert.h"
      73             : #include "hasht.h"
      74             : #include "sechash.h"
      75             : #include "cms.h"
      76             : #include "cmst.h"
      77             : #endif
      78             : 
      79             : #include <config_eot.h>
      80             : 
      81             : #if ENABLE_EOT
      82             : #include <libeot/libeot.h>
      83             : #endif
      84             : 
      85             : using namespace vcl;
      86             : 
      87             : #if (OSL_DEBUG_LEVEL < 3)
      88             : #define COMPRESS_PAGES
      89             : #else
      90             : #define DEBUG_DISABLE_PDFCOMPRESSION // also do not compress streams
      91             : #endif
      92             : 
      93             : #if !defined(ANDROID) && !defined(IOS)
      94             : #define MAX_SIGNATURE_CONTENT_LENGTH 0x4000
      95             : #endif
      96             : 
      97             : #ifdef DO_TEST_PDF
      98             : class PDFTestOutputStream : public PDFOutputStream
      99             : {
     100             :     public:
     101             :     virtual ~PDFTestOutputStream();
     102             :     virtual void write( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xStream );
     103             : };
     104             : 
     105             : PDFTestOutputStream::~PDFTestOutputStream()
     106             : {
     107             : }
     108             : 
     109             : void PDFTestOutputStream::write( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xStream )
     110             : {
     111             :     OString aStr( "lalala\ntest\ntest\ntest" );
     112             :     com::sun::star::uno::Sequence< sal_Int8 > aData( aStr.getLength() );
     113             :     memcpy( aData.getArray(), aStr.getStr(), aStr.getLength() );
     114             :     xStream->writeBytes( aData );
     115             : }
     116             : 
     117             : // this test code cannot be used to test PDF/A-1 because it forces
     118             : // control item (widgets) to bypass the structure controlling
     119             : // the embedding of such elements in actual run
     120             : void doTestCode()
     121             : {
     122             :     static const char* pHome = getenv( "HOME"  );
     123             :     OUString aTestFile( "file://"  );
     124             :     aTestFile += OUString( pHome, strlen( pHome ), RTL_TEXTENCODING_MS_1252 );
     125             :     aTestFile += "/pdf_export_test.pdf";
     126             : 
     127             :     PDFWriter::PDFWriterContext aContext;
     128             :     aContext.URL            = aTestFile;
     129             :     aContext.Version        = PDFWriter::PDF_1_4;
     130             :     aContext.Tagged         = true;
     131             :     aContext.InitialPage    = 2;
     132             :     aContext.DocumentInfo.Title = "PDF export test document";
     133             :     aContext.DocumentInfo.Producer = "VCL";
     134             : 
     135             :     aContext.SignPDF        = true;
     136             :     aContext.SignLocation   = "Burdur";
     137             :     aContext.SignReason     = "Some valid reason to sign";
     138             :     aContext.SignContact    = "signer@example.com";
     139             : 
     140             :     com::sun::star::uno::Reference< com::sun::star::beans::XMaterialHolder > xEnc;
     141             :     PDFWriter aWriter( aContext, xEnc );
     142             :     aWriter.NewPage( 595, 842 );
     143             :     aWriter.BeginStructureElement( PDFWriter::Document );
     144             :     // set duration of 3 sec for first page
     145             :     aWriter.SetAutoAdvanceTime( 3 );
     146             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     147             : 
     148             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     149             :     aWriter.SetLineColor( Color( COL_LIGHTGREEN ) );
     150             :     aWriter.DrawRect( Rectangle( Point( 2000, 200 ), Size( 8000, 3000 ) ), 5000, 2000 );
     151             : 
     152             :     aWriter.SetFont( Font( OUString( "Times" ), Size( 0, 500 ) ) );
     153             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     154             :     aWriter.SetLineColor( Color( COL_BLACK ) );
     155             :     aWriter.SetFillColor( Color( COL_LIGHTBLUE ) );
     156             : 
     157             :     Rectangle aRect( Point( 5000, 5000 ), Size( 6000, 3000 ) );
     158             :     aWriter.DrawRect( aRect );
     159             :     aWriter.DrawText( aRect, OUString( "Link annot 1" ) );
     160             :     sal_Int32 nFirstLink = aWriter.CreateLink( aRect );
     161             :     PDFNote aNote;
     162             :     aNote.Title = "A small test note";
     163             :     aNote.Contents = "There is no business like show business like no business i know. Everything about it is appealing.";
     164             :     aWriter.CreateNote( Rectangle( Point( aRect.Right(), aRect.Top() ), Size( 6000, 3000 ) ), aNote );
     165             : 
     166             :     Rectangle aTargetRect( Point( 3000, 23000 ), Size( 12000, 6000 ) );
     167             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     168             :     aWriter.DrawRect( aTargetRect );
     169             :     aWriter.DrawText( aTargetRect, "Dest second link" );
     170             :     sal_Int32 nSecondDest = aWriter.CreateDest( aTargetRect );
     171             : 
     172             :     aWriter.BeginStructureElement( PDFWriter::Section );
     173             :     aWriter.BeginStructureElement( PDFWriter::Heading );
     174             :     aWriter.DrawText( Point(4500, 9000), "A small structure test" );
     175             :     aWriter.EndStructureElement();
     176             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     177             :     aWriter.SetStructureAttribute( PDFWriter::WritingMode, PDFWriter::LrTb );
     178             :     aWriter.SetStructureAttribute( PDFWriter::TextDecorationType, PDFWriter::Underline );
     179             :     aWriter.DrawText( Rectangle( Point( 4500, 10000 ), Size( 12000, 6000 ) ),
     180             :                      "It was the best of PDF, it was the worst of PDF ... or so. This is a pretty nonsensical text to denote a paragraph. I suggest you stop reading it. Because if you read on you might get bored. So continue on your on risk. Hey, you're still here ? Why do you continue to read this as it is of no use at all ? OK, it's your time, but still... . Woah, i even get bored writing this, so let's end this here and now.",
     181             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     182             :                       );
     183             :     aWriter.SetActualText( "It was the best of PDF, it was the worst of PDF ... or so. This is a pretty nonsensical text to denote a paragraph. I suggest you stop reading it. Because if you read on you might get bored. So continue on your on risk. Hey, you're still here ? Why do you continue to read this as it is of no use at all ? OK, it's your time, but still... . Woah, i even get bored writing this, so let's end this here and now." );
     184             :     aWriter.SetAlternateText( "This paragraph contains some lengthy nonsense to test structural element emission of PDFWriter." );
     185             :     aWriter.EndStructureElement();
     186             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     187             :     aWriter.SetStructureAttribute( PDFWriter::WritingMode, PDFWriter::LrTb );
     188             :     aWriter.DrawText( Rectangle( Point( 4500, 19000 ), Size( 12000, 1000 ) ),
     189             :                       "This paragraph is nothing special either but ends on the next page structurewise",
     190             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     191             :                       );
     192             : 
     193             :     aWriter.NewPage( 595, 842 );
     194             :     // test AddStream interface
     195             :     aWriter.AddStream( "text/plain", new PDFTestOutputStream(), true );
     196             :     // set transitional mode
     197             :     aWriter.SetPageTransition( PDFWriter::WipeRightToLeft, 1500 );
     198             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     199             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     200             :     aWriter.SetFont( Font( OUString( "Times" ), Size( 0, 500 ) ) );
     201             :     aWriter.DrawText( Rectangle( Point( 4500, 1500 ), Size( 12000, 3000 ) ),
     202             :                       "Here's where all things come to an end ... well at least the paragaph from the last page.",
     203             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     204             :                       );
     205             :     aWriter.EndStructureElement();
     206             : 
     207             :     aWriter.SetFillColor( Color( COL_LIGHTBLUE ) );
     208             :     // disable structure
     209             :     aWriter.BeginStructureElement( PDFWriter::NonStructElement );
     210             :     aWriter.DrawRect( aRect );
     211             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     212             :     aWriter.DrawText( aRect, "Link annot 2" );
     213             :     sal_Int32 nSecondLink = aWriter.CreateLink( aRect );
     214             : 
     215             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     216             :     aWriter.BeginStructureElement( PDFWriter::ListItem );
     217             :     aWriter.DrawRect( aTargetRect );
     218             :     aWriter.DrawText( aTargetRect, "Dest first link" );
     219             :     sal_Int32 nFirstDest = aWriter.CreateDest( aTargetRect );
     220             :     // enable structure
     221             :     aWriter.EndStructureElement();
     222             : 
     223             :     aWriter.EndStructureElement();
     224             :     aWriter.EndStructureElement();
     225             :     aWriter.BeginStructureElement( PDFWriter::Figure );
     226             :     aWriter.BeginStructureElement( PDFWriter::Caption );
     227             :     aWriter.DrawText( Point( 4500, 9000 ), "Some drawing stuff inside the structure" );
     228             :     aWriter.EndStructureElement();
     229             : 
     230             :     // test clipping
     231             :     basegfx::B2DPolyPolygon aClip;
     232             :     basegfx::B2DPolygon aClipPoly;
     233             :     aClipPoly.append( basegfx::B2DPoint( 8250, 9600 ) );
     234             :     aClipPoly.append( basegfx::B2DPoint( 16500, 11100 ) );
     235             :     aClipPoly.append( basegfx::B2DPoint( 8250, 12600 ) );
     236             :     aClipPoly.append( basegfx::B2DPoint( 4500, 11100 ) );
     237             :     aClipPoly.setClosed( true );
     238             :     aClip.append( aClipPoly );
     239             : 
     240             :     aWriter.Push( PUSH_CLIPREGION | PUSH_FILLCOLOR );
     241             :     aWriter.SetClipRegion( aClip );
     242             :     aWriter.DrawEllipse( Rectangle( Point( 4500, 9600 ), Size( 12000, 3000 ) ) );
     243             :     aWriter.MoveClipRegion( 1000, 500 );
     244             :     aWriter.SetFillColor( Color( COL_RED ) );
     245             :     aWriter.DrawEllipse( Rectangle( Point( 4500, 9600 ), Size( 12000, 3000 ) ) );
     246             :     aWriter.Pop();
     247             :     // test transparency
     248             :     // draw background
     249             :     Rectangle aTranspRect( Point( 7500, 13500 ), Size( 9000, 6000 ) );
     250             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     251             :     aWriter.DrawRect( aTranspRect );
     252             :     aWriter.BeginTransparencyGroup();
     253             : 
     254             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     255             :     aWriter.DrawEllipse( aTranspRect );
     256             :     aWriter.SetTextColor( Color( COL_LIGHTBLUE ) );
     257             :     aWriter.DrawText( aTranspRect,
     258             :                       "Some transparent text",
     259             :                       TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
     260             : 
     261             :     aWriter.EndTransparencyGroup( aTranspRect, 50 );
     262             : 
     263             :     // prepare an alpha mask
     264             :     Bitmap aTransMask( Size( 256, 256 ), 8, &Bitmap::GetGreyPalette( 256 ) );
     265             :     BitmapWriteAccess* pAcc = aTransMask.AcquireWriteAccess();
     266             :     for( int nX = 0; nX < 256; nX++ )
     267             :         for( int nY = 0; nY < 256; nY++ )
     268             :             pAcc->SetPixel( nX, nY, BitmapColor( (sal_uInt8)((nX+nY)/2) ) );
     269             :     aTransMask.ReleaseAccess( pAcc );
     270             :     aTransMask.SetPrefMapMode( MAP_MM );
     271             :     aTransMask.SetPrefSize( Size( 10, 10 ) );
     272             : 
     273             :     aWriter.DrawBitmap( Point( 600, 13500 ), Size( 3000, 3000 ), aTransMask );
     274             : 
     275             :     aTranspRect = Rectangle( Point( 4200, 13500 ), Size( 3000, 3000 ) );
     276             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     277             :     aWriter.DrawRect( aTranspRect );
     278             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     279             :     aWriter.DrawEllipse( aTranspRect );
     280             :     aWriter.SetTextColor( Color( COL_LIGHTBLUE ) );
     281             :     aWriter.DrawText( aTranspRect,
     282             :                       "Some transparent text",
     283             :                       TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
     284             :     aTranspRect = Rectangle( Point( 1500, 16500 ), Size( 4800, 3000 ) );
     285             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     286             :     aWriter.DrawRect( aTranspRect );
     287             : 
     288             :     Bitmap aImageBmp( Size( 256, 256 ), 24 );
     289             :     pAcc = aImageBmp.AcquireWriteAccess();
     290             :     pAcc->SetFillColor( Color( 0xff, 0, 0xff ) );
     291             :     pAcc->FillRect( Rectangle( Point( 0, 0 ), Size( 256, 256 ) ) );
     292             :     aImageBmp.ReleaseAccess( pAcc );
     293             :     BitmapEx aBmpEx( aImageBmp, AlphaMask( aTransMask ) );
     294             :     aWriter.DrawBitmapEx( Point( 1500, 19500 ), Size( 4800, 3000 ), aBmpEx );
     295             : 
     296             :     aWriter.EndStructureElement();
     297             :     aWriter.EndStructureElement();
     298             : 
     299             :     LineInfo aLI( LINE_DASH, 3 );
     300             :     aLI.SetDashCount( 2 );
     301             :     aLI.SetDashLen( 50 );
     302             :     aLI.SetDotCount( 2 );
     303             :     aLI.SetDotLen( 25 );
     304             :     aLI.SetDistance( 15 );
     305             :     Point aLIPoints[] = { Point( 4000, 10000 ),
     306             :                           Point( 8000, 12000 ),
     307             :                           Point( 3000, 19000 ) };
     308             :     Polygon aLIPoly( 3, aLIPoints );
     309             :     aWriter.SetLineColor( Color( COL_BLUE ) );
     310             :     aWriter.SetFillColor();
     311             :     aWriter.DrawPolyLine( aLIPoly, aLI );
     312             : 
     313             :     aLI.SetDashCount( 4 );
     314             :     aLIPoly.Move( 1000, 1000 );
     315             :     aWriter.DrawPolyLine( aLIPoly, aLI );
     316             : 
     317             :     aWriter.NewPage( 595, 842 );
     318             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     319             :     Wallpaper aWall( aTransMask );
     320             :     aWall.SetStyle( WALLPAPER_TILE );
     321             :     aWriter.DrawWallpaper( Rectangle( Point( 4400, 4200 ), Size( 10200, 6300 ) ), aWall );
     322             : 
     323             :     aWriter.NewPage( 595, 842 );
     324             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     325             :     aWriter.SetFont( Font( OUString( "Times" ), Size( 0, 500 ) ) );
     326             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     327             :     aRect = Rectangle( Point( 4500, 6000 ), Size( 6000, 1500 ) );
     328             :     aWriter.DrawRect( aRect );
     329             :     aWriter.DrawText( aRect, "www.heise.de" );
     330             :     sal_Int32 nURILink = aWriter.CreateLink( aRect );
     331             :     aWriter.SetLinkURL( nURILink, OUString( "http://www.heise.de" ) );
     332             : 
     333             :     aWriter.SetLinkDest( nFirstLink, nFirstDest );
     334             :     aWriter.SetLinkDest( nSecondLink, nSecondDest );
     335             : 
     336             :     // include a button
     337             :     PDFWriter::PushButtonWidget aBtn;
     338             :     aBtn.Name = "testButton";
     339             :     aBtn.Description = "A test button";
     340             :     aBtn.Text = "hit me";
     341             :     aBtn.Location = Rectangle( Point( 4500, 9000 ), Size( 4500, 3000 ) );
     342             :     aBtn.Border = aBtn.Background = true;
     343             :     aWriter.CreateControl( aBtn );
     344             : 
     345             :     // include a uri button
     346             :     PDFWriter::PushButtonWidget aUriBtn;
     347             :     aUriBtn.Name = "wwwButton";
     348             :     aUriBtn.Description = "A URI button";
     349             :     aUriBtn.Text = "to www";
     350             :     aUriBtn.Location = Rectangle( Point( 9500, 9000 ), Size( 4500, 3000 ) );
     351             :     aUriBtn.Border = aUriBtn.Background = true;
     352             :     aUriBtn.URL = "http://www.heise.de";
     353             :     aWriter.CreateControl( aUriBtn );
     354             : 
     355             :     // include a dest button
     356             :     PDFWriter::PushButtonWidget aDstBtn;
     357             :     aDstBtn.Name = "destButton";
     358             :     aDstBtn.Description = "A Dest button";
     359             :     aDstBtn.Text = "to paragraph";
     360             :     aDstBtn.Location = Rectangle( Point( 14500, 9000 ), Size( 4500, 3000 ) );
     361             :     aDstBtn.Border = aDstBtn.Background = true;
     362             :     aDstBtn.Dest = nFirstDest;
     363             :     aWriter.CreateControl( aDstBtn );
     364             : 
     365             :     PDFWriter::CheckBoxWidget aCBox;
     366             :     aCBox.Name = "textCheckBox";
     367             :     aCBox.Description = "A test check box";
     368             :     aCBox.Text = "check me";
     369             :     aCBox.Location = Rectangle( Point( 4500, 13500 ), Size( 3000, 750 ) );
     370             :     aCBox.Checked = true;
     371             :     aCBox.Border = aCBox.Background = false;
     372             :     aWriter.CreateControl( aCBox );
     373             : 
     374             :     PDFWriter::CheckBoxWidget aCBox2;
     375             :     aCBox2.Name = "textCheckBox2";
     376             :     aCBox2.Description = "Another test check box";
     377             :     aCBox2.Text = "check me right";
     378             :     aCBox2.Location = Rectangle( Point( 4500, 14250 ), Size( 3000, 750 ) );
     379             :     aCBox2.Checked = true;
     380             :     aCBox2.Border = aCBox2.Background = false;
     381             :     aCBox2.ButtonIsLeft = false;
     382             :     aWriter.CreateControl( aCBox2 );
     383             : 
     384             :     PDFWriter::RadioButtonWidget aRB1;
     385             :     aRB1.Name = "rb1_1";
     386             :     aRB1.Description = "radio 1 button 1";
     387             :     aRB1.Text = "Despair";
     388             :     aRB1.Location = Rectangle( Point( 4500, 15000 ), Size( 6000, 1000 ) );
     389             :     aRB1.Selected = true;
     390             :     aRB1.RadioGroup = 1;
     391             :     aRB1.Border = aRB1.Background = true;
     392             :     aRB1.ButtonIsLeft = false;
     393             :     aRB1.BorderColor = Color( COL_LIGHTGREEN );
     394             :     aRB1.BackgroundColor = Color( COL_LIGHTBLUE );
     395             :     aRB1.TextColor = Color( COL_LIGHTRED );
     396             :     aRB1.TextFont = Font( OUString( "Courier" ), Size( 0, 800 ) );
     397             :     aWriter.CreateControl( aRB1 );
     398             : 
     399             :     PDFWriter::RadioButtonWidget aRB2;
     400             :     aRB2.Name = "rb2_1";
     401             :     aRB2.Description = "radio 2 button 1";
     402             :     aRB2.Text = "Joy";
     403             :     aRB2.Location = Rectangle( Point( 10500, 15000 ), Size( 3000, 1000 ) );
     404             :     aRB2.Selected = true;
     405             :     aRB2.RadioGroup = 2;
     406             :     aWriter.CreateControl( aRB2 );
     407             : 
     408             :     PDFWriter::RadioButtonWidget aRB3;
     409             :     aRB3.Name = "rb1_2";
     410             :     aRB3.Description = "radio 1 button 2";
     411             :     aRB3.Text = "Desperation";
     412             :     aRB3.Location = Rectangle( Point( 4500, 16000 ), Size( 3000, 1000 ) );
     413             :     aRB3.Selected = true;
     414             :     aRB3.RadioGroup = 1;
     415             :     aWriter.CreateControl( aRB3 );
     416             : 
     417             :     PDFWriter::EditWidget aEditBox;
     418             :     aEditBox.Name = "testEdit";
     419             :     aEditBox.Description = "A test edit field";
     420             :     aEditBox.Text = "A little test text";
     421             :     aEditBox.TextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER;
     422             :     aEditBox.Location = Rectangle( Point( 10000, 18000 ), Size( 5000, 1500 ) );
     423             :     aEditBox.MaxLen = 100;
     424             :     aEditBox.Border = aEditBox.Background = true;
     425             :     aEditBox.BorderColor = Color( COL_BLACK );
     426             :     aWriter.CreateControl( aEditBox );
     427             : 
     428             :     // normal list box
     429             :     PDFWriter::ListBoxWidget aLstBox;
     430             :     aLstBox.Name = "testListBox";
     431             :     aLstBox.Text = "One";
     432             :     aLstBox.Description = "select me";
     433             :     aLstBox.Location = Rectangle( Point( 4500, 18000 ), Size( 3000, 1500 ) );
     434             :     aLstBox.Sort = true;
     435             :     aLstBox.MultiSelect = true;
     436             :     aLstBox.Border = aLstBox.Background = true;
     437             :     aLstBox.BorderColor = Color( COL_BLACK );
     438             :     aLstBox.Entries.push_back( OUString( "One"  ) );
     439             :     aLstBox.Entries.push_back( OUString( "Two"  ) );
     440             :     aLstBox.Entries.push_back( OUString( "Three"  ) );
     441             :     aLstBox.Entries.push_back( OUString( "Four"  ) );
     442             :     aLstBox.SelectedEntries.push_back( 1 );
     443             :     aLstBox.SelectedEntries.push_back( 2 );
     444             :     aWriter.CreateControl( aLstBox );
     445             : 
     446             :     // dropdown list box
     447             :     aLstBox.Name = "testDropDownListBox";
     448             :     aLstBox.DropDown = true;
     449             :     aLstBox.Location = Rectangle( Point( 4500, 19500 ), Size( 3000, 500 ) );
     450             :     aWriter.CreateControl( aLstBox );
     451             : 
     452             :     // combo box
     453             :     PDFWriter::ComboBoxWidget aComboBox;
     454             :     aComboBox.Name = "testComboBox";
     455             :     aComboBox.Text = "test a combobox";
     456             :     aComboBox.Entries.push_back( OUString( "Larry"  ) );
     457             :     aComboBox.Entries.push_back( OUString( "Curly"  ) );
     458             :     aComboBox.Entries.push_back( OUString( "Moe"  ) );
     459             :     aComboBox.Location = Rectangle( Point( 4500, 20000 ), Size( 3000, 500 ) );
     460             :     aWriter.CreateControl( aComboBox );
     461             : 
     462             :     // test outlines
     463             :     sal_Int32 nPage1OL = aWriter.CreateOutlineItem();
     464             :     aWriter.SetOutlineItemText( nPage1OL, OUString( "Page 1"  ) );
     465             :     aWriter.SetOutlineItemDest( nPage1OL, nSecondDest );
     466             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2"  ), nSecondDest );
     467             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2 revisited"  ), nSecondDest );
     468             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2 again"  ), nSecondDest );
     469             :     sal_Int32 nPage2OL = aWriter.CreateOutlineItem();
     470             :     aWriter.SetOutlineItemText( nPage2OL, OUString( "Page 2"  ) );
     471             :     aWriter.CreateOutlineItem( nPage2OL, OUString( "Dest 1"  ), nFirstDest );
     472             : 
     473             :     aWriter.EndStructureElement(); // close document
     474             : 
     475             :     aWriter.Emit();
     476             : }
     477             : #endif
     478             : 
     479             : static const sal_Int32 nLog10Divisor = 1;
     480             : static const double fDivisor = 10.0;
     481             : 
     482           0 : static inline double pixelToPoint( double px ) { return px/fDivisor; }
     483           0 : static inline sal_Int32 pointToPixel( double pt ) { return sal_Int32(pt*fDivisor); }
     484             : 
     485             : const sal_uInt8 PDFWriterImpl::s_nPadString[32] =
     486             : {
     487             :     0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
     488             :     0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A
     489             : };
     490             : 
     491           0 : static void appendHex( sal_Int8 nInt, OStringBuffer& rBuffer )
     492             : {
     493             :     static const sal_Char pHexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
     494             :                                            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     495           0 :     rBuffer.append( pHexDigits[ (nInt >> 4) & 15 ] );
     496           0 :     rBuffer.append( pHexDigits[ nInt & 15 ] );
     497           0 : }
     498             : 
     499           0 : static void appendName( const OUString& rStr, OStringBuffer& rBuffer )
     500             : {
     501             : // FIXME i59651 add a check for max length of 127 chars? Per PDF spec 1.4, appendix C.1
     502             : // I guess than when reading the #xx sequence it will count for a single character.
     503           0 :     OString aStr( OUStringToOString( rStr, RTL_TEXTENCODING_UTF8 ) );
     504           0 :     const sal_Char* pStr = aStr.getStr();
     505           0 :     int nLen = aStr.getLength();
     506           0 :     for( int i = 0; i < nLen; i++ )
     507             :     {
     508             :         /*  #i16920# PDF recommendation: output UTF8, any byte
     509             :          *  outside the interval [33(=ASCII'!');126(=ASCII'~')]
     510             :          *  should be escaped hexadecimal
     511             :          *  for the sake of ghostscript which also reads PDF
     512             :          *  but has a narrower acceptance rate we only pass
     513             :          *  alphanumerics and '-' literally.
     514             :          */
     515           0 :         if( (pStr[i] >= 'A' && pStr[i] <= 'Z' ) ||
     516           0 :             (pStr[i] >= 'a' && pStr[i] <= 'z' ) ||
     517           0 :             (pStr[i] >= '0' && pStr[i] <= '9' ) ||
     518           0 :             pStr[i] == '-' )
     519             :         {
     520           0 :             rBuffer.append( pStr[i] );
     521             :         }
     522             :         else
     523             :         {
     524           0 :             rBuffer.append( '#' );
     525           0 :             appendHex( (sal_Int8)pStr[i], rBuffer );
     526             :         }
     527           0 :     }
     528           0 : }
     529             : 
     530           0 : static void appendName( const sal_Char* pStr, OStringBuffer& rBuffer )
     531             : {
     532             : //FIXME i59651 see above
     533           0 :     while( pStr && *pStr )
     534             :     {
     535           0 :         if( (*pStr >= 'A' && *pStr <= 'Z' ) ||
     536           0 :             (*pStr >= 'a' && *pStr <= 'z' ) ||
     537           0 :             (*pStr >= '0' && *pStr <= '9' ) ||
     538           0 :             *pStr == '-' )
     539             :         {
     540           0 :             rBuffer.append( *pStr );
     541             :         }
     542             :         else
     543             :         {
     544           0 :             rBuffer.append( '#' );
     545           0 :             appendHex( (sal_Int8)*pStr, rBuffer );
     546             :         }
     547           0 :         pStr++;
     548             :     }
     549           0 : }
     550             : 
     551             : //used only to emit encoded passwords
     552           0 : static void appendLiteralString( const sal_Char* pStr, sal_Int32 nLength, OStringBuffer& rBuffer )
     553             : {
     554           0 :     while( nLength )
     555             :     {
     556           0 :         switch( *pStr )
     557             :         {
     558             :         case '\n' :
     559           0 :             rBuffer.append( "\\n" );
     560           0 :             break;
     561             :         case '\r' :
     562           0 :             rBuffer.append( "\\r" );
     563           0 :             break;
     564             :         case '\t' :
     565           0 :             rBuffer.append( "\\t" );
     566           0 :             break;
     567             :         case '\b' :
     568           0 :             rBuffer.append( "\\b" );
     569           0 :             break;
     570             :         case '\f' :
     571           0 :             rBuffer.append( "\\f" );
     572           0 :             break;
     573             :         case '(' :
     574             :         case ')' :
     575             :         case '\\' :
     576           0 :             rBuffer.append( "\\" );
     577           0 :             rBuffer.append( (sal_Char) *pStr );
     578           0 :             break;
     579             :         default:
     580           0 :             rBuffer.append( (sal_Char) *pStr );
     581           0 :             break;
     582             :         }
     583           0 :         pStr++;
     584           0 :         nLength--;
     585             :     }
     586           0 : }
     587             : 
     588             : /**--->i56629
     589             :  * Convert a string before using it.
     590             :  *
     591             :  * This string conversion function is needed because the destination name
     592             :  * in a PDF file seen through an Internet browser should be
     593             :  * specially crafted, in order to be used directly by the browser.
     594             :  * In this way the fragment part of a hyperlink to a PDF file (e.g. something
     595             :  * as 'test1/test2/a-file.pdf\#thefragment) will be (hopefully) interpreted by the
     596             :  * PDF reader (currently only Adobe Reader plug-in seems to be working that way) called
     597             :  * from inside the Internet browser as: 'open the file test1/test2/a-file.pdf
     598             :  * and go to named destination thefragment using default zoom'.
     599             :  * The conversion is needed because in case of a fragment in the form: Slide%201
     600             :  * (meaning Slide 1) as it is converted obeying the Inet rules, it will become Slide25201
     601             :  * using this conversion, in both the generated named destinations, fragment and GoToR
     602             :  * destination.
     603             :  *
     604             :  * The names for destinations are name objects and so they don't need to be encrypted
     605             :  * even though they expose the content of PDF file (e.g. guessing the PDF content from the
     606             :  * destination name).
     607             :  *
     608             :  * Fhurter limitation: it is advisable to use standard ASCII characters for
     609             :  * OOo bookmarks.
     610             : */
     611           0 : static void appendDestinationName( const OUString& rString, OStringBuffer& rBuffer )
     612             : {
     613           0 :     const sal_Unicode* pStr = rString.getStr();
     614           0 :     sal_Int32 nLen = rString.getLength();
     615           0 :     for( int i = 0; i < nLen; i++ )
     616             :     {
     617           0 :         sal_Unicode aChar = pStr[i];
     618           0 :         if( (aChar >= '0' && aChar <= '9' ) ||
     619           0 :             (aChar >= 'a' && aChar <= 'z' ) ||
     620           0 :             (aChar >= 'A' && aChar <= 'Z' ) ||
     621             :             aChar == '-' )
     622             :         {
     623           0 :             rBuffer.append((sal_Char)aChar);
     624             :         }
     625             :         else
     626             :         {
     627           0 :             sal_Int8 aValueHigh = sal_Int8(aChar >> 8);
     628           0 :             if(aValueHigh > 0)
     629           0 :                 appendHex( aValueHigh, rBuffer );
     630           0 :             appendHex( (sal_Int8)(aChar & 255 ), rBuffer );
     631             :         }
     632             :     }
     633           0 : }
     634             : //<--- i56629
     635             : 
     636           0 : static void appendUnicodeTextString( const OUString& rString, OStringBuffer& rBuffer )
     637             : {
     638           0 :     rBuffer.append( "FEFF" );
     639           0 :     const sal_Unicode* pStr = rString.getStr();
     640           0 :     sal_Int32 nLen = rString.getLength();
     641           0 :     for( int i = 0; i < nLen; i++ )
     642             :     {
     643           0 :         sal_Unicode aChar = pStr[i];
     644           0 :         appendHex( (sal_Int8)(aChar >> 8), rBuffer );
     645           0 :         appendHex( (sal_Int8)(aChar & 255 ), rBuffer );
     646             :     }
     647           0 : }
     648             : 
     649           0 : void PDFWriterImpl::createWidgetFieldName( sal_Int32 i_nWidgetIndex, const PDFWriter::AnyWidget& i_rControl )
     650             : {
     651             :     /* #i80258# previously we use appendName here
     652             :        however we need a slightly different coding scheme than the normal
     653             :        name encoding for field names
     654             :     */
     655           0 :     const OUString& rName = (m_aContext.Version > PDFWriter::PDF_1_2) ? i_rControl.Name : i_rControl.Text;
     656           0 :     OString aStr( OUStringToOString( rName, RTL_TEXTENCODING_UTF8 ) );
     657           0 :     const sal_Char* pStr = aStr.getStr();
     658           0 :     int nLen = aStr.getLength();
     659             : 
     660           0 :     OStringBuffer aBuffer( rName.getLength()+64 );
     661           0 :     for( int i = 0; i < nLen; i++ )
     662             :     {
     663             :         /*  #i16920# PDF recommendation: output UTF8, any byte
     664             :          *  outside the interval [32(=ASCII' ');126(=ASCII'~')]
     665             :          *  should be escaped hexadecimal
     666             :          */
     667           0 :         if( (pStr[i] >= 32 && pStr[i] <= 126 ) )
     668           0 :             aBuffer.append( pStr[i] );
     669             :         else
     670             :         {
     671           0 :             aBuffer.append( '#' );
     672           0 :             appendHex( (sal_Int8)pStr[i], aBuffer );
     673             :         }
     674             :     }
     675             : 
     676           0 :     OString aFullName( aBuffer.makeStringAndClear() );
     677             : 
     678             :     /* #i82785# create hierarchical fields down to the for each dot in i_rName */
     679           0 :     sal_Int32 nTokenIndex = 0, nLastTokenIndex = 0;
     680           0 :     OString aPartialName;
     681           0 :     OString aDomain;
     682           0 :     do
     683             :     {
     684           0 :         nLastTokenIndex = nTokenIndex;
     685           0 :         aPartialName = aFullName.getToken( 0, '.', nTokenIndex );
     686           0 :         if( nTokenIndex != -1 )
     687             :         {
     688             :             // find or create a hierarchical field
     689             :             // first find the fully qualified name up to this field
     690           0 :             aDomain = aFullName.copy( 0, nTokenIndex-1 );
     691           0 :             boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator it = m_aFieldNameMap.find( aDomain );
     692           0 :             if( it == m_aFieldNameMap.end() )
     693             :             {
     694             :                  // create new hierarchy field
     695           0 :                 sal_Int32 nNewWidget = m_aWidgets.size();
     696           0 :                 m_aWidgets.push_back( PDFWidget() );
     697           0 :                 m_aWidgets[nNewWidget].m_nObject = createObject();
     698           0 :                 m_aWidgets[nNewWidget].m_eType = PDFWriter::Hierarchy;
     699           0 :                 m_aWidgets[nNewWidget].m_aName = aPartialName;
     700           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[nNewWidget].m_nObject;
     701           0 :                 m_aFieldNameMap[aDomain] = nNewWidget;
     702           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[nNewWidget].m_nObject;
     703           0 :                 if( nLastTokenIndex > 0 )
     704             :                 {
     705             :                     // this field is not a root field and
     706             :                     // needs to be inserted to its parent
     707           0 :                     OString aParentDomain( aDomain.copy( 0, nLastTokenIndex-1 ) );
     708           0 :                     it = m_aFieldNameMap.find( aParentDomain );
     709             :                     OSL_ENSURE( it != m_aFieldNameMap.end(), "field name not found" );
     710           0 :                     if( it != m_aFieldNameMap.end()  )
     711             :                     {
     712             :                         OSL_ENSURE( it->second < sal_Int32(m_aWidgets.size()), "invalid field number entry" );
     713           0 :                         if( it->second < sal_Int32(m_aWidgets.size()) )
     714             :                         {
     715           0 :                             PDFWidget& rParentField( m_aWidgets[it->second] );
     716           0 :                             rParentField.m_aKids.push_back( m_aWidgets[nNewWidget].m_nObject );
     717           0 :                             rParentField.m_aKidsIndex.push_back( nNewWidget );
     718           0 :                             m_aWidgets[nNewWidget].m_nParent = rParentField.m_nObject;
     719             :                         }
     720           0 :                     }
     721             :                 }
     722             :             }
     723           0 :             else if( m_aWidgets[it->second].m_eType != PDFWriter::Hierarchy )
     724             :             {
     725             :                 // this is invalid, someone tries to have a terminal field as parent
     726             :                 // example: a button with the name foo.bar exists and
     727             :                 // another button is named foo.bar.no
     728             :                 // workaround: put the second terminal field as much up in the hierarchy as
     729             :                 // necessary to have a non-terminal field as parent (or none at all)
     730             :                 // since it->second already is terminal, we just need to use its parent
     731           0 :                 aDomain = OString();
     732           0 :                 aPartialName = aFullName.copy( aFullName.lastIndexOf( '.' )+1 );
     733           0 :                 if( nLastTokenIndex > 0 )
     734             :                 {
     735           0 :                     aDomain = aFullName.copy( 0, nLastTokenIndex-1 );
     736           0 :                     OStringBuffer aBuf( aDomain.getLength() + 1 + aPartialName.getLength() );
     737           0 :                     aBuf.append( aDomain );
     738           0 :                     aBuf.append( '.' );
     739           0 :                     aBuf.append( aPartialName );
     740           0 :                     aFullName = aBuf.makeStringAndClear();
     741             :                 }
     742             :                 else
     743           0 :                     aFullName = aPartialName;
     744           0 :                 break;
     745             :             }
     746             :         }
     747           0 :     } while( nTokenIndex != -1 );
     748             : 
     749             :     // insert widget into its hierarchy field
     750           0 :     if( !aDomain.isEmpty() )
     751             :     {
     752           0 :         boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator it = m_aFieldNameMap.find( aDomain );
     753           0 :         if( it != m_aFieldNameMap.end() )
     754             :         {
     755             :             OSL_ENSURE( it->second >= 0 && it->second < sal_Int32( m_aWidgets.size() ), "invalid field index" );
     756           0 :             if( it->second >= 0 && it->second < sal_Int32(m_aWidgets.size()) )
     757             :             {
     758           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[it->second].m_nObject;
     759           0 :                 m_aWidgets[it->second].m_aKids.push_back( m_aWidgets[i_nWidgetIndex].m_nObject);
     760           0 :                 m_aWidgets[it->second].m_aKidsIndex.push_back( i_nWidgetIndex );
     761             :             }
     762             :         }
     763             :     }
     764             : 
     765           0 :     if( aPartialName.isEmpty() )
     766             :     {
     767             :         // how funny, an empty field name
     768           0 :         if( i_rControl.getType() == PDFWriter::RadioButton )
     769             :         {
     770           0 :             aPartialName  = "RadioGroup";
     771           0 :             aPartialName += OString::number( static_cast<const PDFWriter::RadioButtonWidget&>(i_rControl).RadioGroup );
     772             :         }
     773             :         else
     774           0 :             aPartialName = OString( "Widget" );
     775             :     }
     776             : 
     777           0 :     if( ! m_aContext.AllowDuplicateFieldNames )
     778             :     {
     779           0 :         boost::unordered_map<OString, sal_Int32, OStringHash>::iterator it = m_aFieldNameMap.find( aFullName );
     780             : 
     781           0 :         if( it != m_aFieldNameMap.end() ) // not unique
     782             :         {
     783           0 :             boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator check_it;
     784           0 :             OString aTry;
     785           0 :             sal_Int32 nTry = 2;
     786           0 :             do
     787             :             {
     788           0 :                 OStringBuffer aUnique( aFullName.getLength() + 16 );
     789           0 :                 aUnique.append( aFullName );
     790           0 :                 aUnique.append( '_' );
     791           0 :                 aUnique.append( nTry++ );
     792           0 :                 aTry = aUnique.makeStringAndClear();
     793           0 :                 check_it = m_aFieldNameMap.find( aTry );
     794           0 :             } while( check_it != m_aFieldNameMap.end() );
     795           0 :             aFullName = aTry;
     796           0 :             m_aFieldNameMap[ aFullName ] = i_nWidgetIndex;
     797           0 :             aPartialName = aFullName.copy( aFullName.lastIndexOf( '.' )+1 );
     798             :         }
     799             :         else
     800           0 :             m_aFieldNameMap[ aFullName ] = i_nWidgetIndex;
     801             :     }
     802             : 
     803             :     // finally
     804           0 :     m_aWidgets[i_nWidgetIndex].m_aName = aPartialName;
     805           0 : }
     806             : 
     807           0 : static void appendFixedInt( sal_Int32 nValue, OStringBuffer& rBuffer, sal_Int32 nPrecision = nLog10Divisor )
     808             : {
     809           0 :     if( nValue < 0 )
     810             :     {
     811           0 :         rBuffer.append( '-' );
     812           0 :         nValue = -nValue;
     813             :     }
     814           0 :     sal_Int32 nFactor = 1, nDiv = nPrecision;
     815           0 :     while( nDiv-- )
     816           0 :         nFactor *= 10;
     817             : 
     818           0 :     sal_Int32 nInt      = nValue / nFactor;
     819           0 :     rBuffer.append( nInt );
     820           0 :     if( nFactor > 1 )
     821             :     {
     822           0 :         sal_Int32 nDecimal  = nValue % nFactor;
     823           0 :         if( nDecimal )
     824             :         {
     825           0 :             rBuffer.append( '.' );
     826             :             // omit trailing zeros
     827           0 :             while( (nDecimal % 10) == 0 )
     828           0 :                 nDecimal /= 10;
     829           0 :             rBuffer.append( nDecimal );
     830             :         }
     831             :     }
     832           0 : }
     833             : 
     834             : // appends a double. PDF does not accept exponential format, only fixed point
     835           0 : static void appendDouble( double fValue, OStringBuffer& rBuffer, sal_Int32 nPrecision = 5 )
     836             : {
     837           0 :     bool bNeg = false;
     838           0 :     if( fValue < 0.0 )
     839             :     {
     840           0 :         bNeg = true;
     841           0 :         fValue=-fValue;
     842             :     }
     843             : 
     844           0 :     sal_Int64 nInt = (sal_Int64)fValue;
     845           0 :     fValue -= (double)nInt;
     846             :     // optimizing hardware may lead to a value of 1.0 after the subtraction
     847           0 :     if( fValue == 1.0 || log10( 1.0-fValue ) <= -nPrecision )
     848             :     {
     849           0 :         nInt++;
     850           0 :         fValue = 0.0;
     851             :     }
     852           0 :     sal_Int64 nFrac = 0;
     853           0 :     if( fValue )
     854             :     {
     855           0 :         fValue *= pow( 10.0, (double)nPrecision );
     856           0 :         nFrac = (sal_Int64)fValue;
     857             :     }
     858           0 :     if( bNeg && ( nInt || nFrac ) )
     859           0 :         rBuffer.append( '-' );
     860           0 :     rBuffer.append( nInt );
     861           0 :     if( nFrac )
     862             :     {
     863             :         int i;
     864           0 :         rBuffer.append( '.' );
     865           0 :         sal_Int64 nBound = (sal_Int64)(pow( 10.0, nPrecision - 1.0 )+0.5);
     866           0 :         for ( i = 0; ( i < nPrecision ) && nFrac; i++ )
     867             :         {
     868           0 :             sal_Int64 nNumb = nFrac / nBound;
     869           0 :             nFrac -= nNumb * nBound;
     870           0 :             rBuffer.append( nNumb );
     871           0 :             nBound /= 10;
     872             :         }
     873             :     }
     874           0 : }
     875             : 
     876           0 : static void appendColor( const Color& rColor, OStringBuffer& rBuffer, bool bConvertToGrey = false )
     877             : {
     878             : 
     879           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     880             :     {
     881           0 :         if( bConvertToGrey )
     882             :         {
     883           0 :             sal_uInt8 cByte = rColor.GetLuminance();
     884           0 :             appendDouble( (double)cByte / 255.0, rBuffer );
     885             :         }
     886             :         else
     887             :         {
     888           0 :             appendDouble( (double)rColor.GetRed() / 255.0, rBuffer );
     889           0 :             rBuffer.append( ' ' );
     890           0 :             appendDouble( (double)rColor.GetGreen() / 255.0, rBuffer );
     891           0 :             rBuffer.append( ' ' );
     892           0 :             appendDouble( (double)rColor.GetBlue() / 255.0, rBuffer );
     893             :         }
     894             :     }
     895           0 : }
     896             : 
     897           0 : void PDFWriterImpl::appendStrokingColor( const Color& rColor, OStringBuffer& rBuffer )
     898             : {
     899           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     900             :     {
     901           0 :         bool bGrey = m_aContext.ColorMode == PDFWriter::DrawGreyscale;
     902           0 :         appendColor( rColor, rBuffer, bGrey );
     903           0 :         rBuffer.append( bGrey ? " G" : " RG" );
     904             :     }
     905           0 : }
     906             : 
     907           0 : void PDFWriterImpl::appendNonStrokingColor( const Color& rColor, OStringBuffer& rBuffer )
     908             : {
     909           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     910             :     {
     911           0 :         bool bGrey = m_aContext.ColorMode == PDFWriter::DrawGreyscale;
     912           0 :         appendColor( rColor, rBuffer, bGrey );
     913           0 :         rBuffer.append( bGrey ? " g" : " rg" );
     914             :     }
     915           0 : }
     916             : 
     917             : // matrix helper class
     918             : // TODO: use basegfx matrix class instead or derive from it
     919             : namespace vcl // TODO: use anonymous namespace to keep this class local
     920             : {
     921             : /*  for sparse matrices of the form (2D linear transformations)
     922             :  *  f[0] f[1] 0
     923             :  *  f[2] f[3] 0
     924             :  *  f[4] f[5] 1
     925             :  */
     926             : class Matrix3
     927             : {
     928             :     double f[6];
     929             : 
     930           0 :     void set( double *pn ) { for( int i = 0 ; i < 6; i++ ) f[i] = pn[i]; }
     931             : public:
     932             :     Matrix3();
     933           0 :     ~Matrix3() {}
     934             : 
     935             :     void skew( double alpha, double beta );
     936             :     void scale( double sx, double sy );
     937             :     void rotate( double angle );
     938             :     void translate( double tx, double ty );
     939             :     bool invert();
     940             : 
     941             :     void append( PDFWriterImpl::PDFPage& rPage, OStringBuffer& rBuffer, Point* pBack = NULL );
     942             : 
     943             :     Point transform( const Point& rPoint ) const;
     944             : };
     945             : }
     946             : 
     947           0 : Matrix3::Matrix3()
     948             : {
     949             :     // initialize to unity
     950           0 :     f[0] = 1.0;
     951           0 :     f[1] = 0.0;
     952           0 :     f[2] = 0.0;
     953           0 :     f[3] = 1.0;
     954           0 :     f[4] = 0.0;
     955           0 :     f[5] = 0.0;
     956           0 : }
     957             : 
     958           0 : Point Matrix3::transform( const Point& rOrig ) const
     959             : {
     960           0 :     double x = (double)rOrig.X(), y = (double)rOrig.Y();
     961           0 :     return Point( (int)(x*f[0] + y*f[2] + f[4]), (int)(x*f[1] + y*f[3] + f[5]) );
     962             : }
     963             : 
     964           0 : void Matrix3::skew( double alpha, double beta )
     965             : {
     966             :     double fn[6];
     967           0 :     double tb = tan( beta );
     968           0 :     fn[0] = f[0] + f[2]*tb;
     969           0 :     fn[1] = f[1];
     970           0 :     fn[2] = f[2] + f[3]*tb;
     971           0 :     fn[3] = f[3];
     972           0 :     fn[4] = f[4] + f[5]*tb;
     973           0 :     fn[5] = f[5];
     974           0 :     if( alpha != 0.0 )
     975             :     {
     976           0 :         double ta = tan( alpha );
     977           0 :         fn[1] += f[0]*ta;
     978           0 :         fn[3] += f[2]*ta;
     979           0 :         fn[5] += f[4]*ta;
     980             :     }
     981           0 :     set( fn );
     982           0 : }
     983             : 
     984           0 : void Matrix3::scale( double sx, double sy )
     985             : {
     986             :     double fn[6];
     987           0 :     fn[0] = sx*f[0];
     988           0 :     fn[1] = sy*f[1];
     989           0 :     fn[2] = sx*f[2];
     990           0 :     fn[3] = sy*f[3];
     991           0 :     fn[4] = sx*f[4];
     992           0 :     fn[5] = sy*f[5];
     993           0 :     set( fn );
     994           0 : }
     995             : 
     996           0 : void Matrix3::rotate( double angle )
     997             : {
     998             :     double fn[6];
     999           0 :     double fSin = sin(angle);
    1000           0 :     double fCos = cos(angle);
    1001           0 :     fn[0] = f[0]*fCos - f[1]*fSin;
    1002           0 :     fn[1] = f[0]*fSin + f[1]*fCos;
    1003           0 :     fn[2] = f[2]*fCos - f[3]*fSin;
    1004           0 :     fn[3] = f[2]*fSin + f[3]*fCos;
    1005           0 :     fn[4] = f[4]*fCos - f[5]*fSin;
    1006           0 :     fn[5] = f[4]*fSin + f[5]*fCos;
    1007           0 :     set( fn );
    1008           0 : }
    1009             : 
    1010           0 : void Matrix3::translate( double tx, double ty )
    1011             : {
    1012           0 :     f[4] += tx;
    1013           0 :     f[5] += ty;
    1014           0 : }
    1015             : 
    1016           0 : bool Matrix3::invert()
    1017             : {
    1018             :     // short circuit trivial cases
    1019           0 :     if( f[1]==f[2] && f[1]==0.0 && f[0]==f[3] && f[0]==1.0 )
    1020             :     {
    1021           0 :         f[4] = -f[4];
    1022           0 :         f[5] = -f[5];
    1023           0 :         return true;
    1024             :     }
    1025             : 
    1026             :     // check determinant
    1027           0 :     const double fDet = f[0]*f[3]-f[1]*f[2];
    1028           0 :     if( fDet == 0.0 )
    1029           0 :         return false;
    1030             : 
    1031             :     // invert the matrix
    1032             :     double fn[6];
    1033           0 :     fn[0] = +f[3] / fDet;
    1034           0 :     fn[1] = -f[1] / fDet;
    1035           0 :     fn[2] = -f[2] / fDet;
    1036           0 :     fn[3] = +f[0] / fDet;
    1037             : 
    1038             :     // apply inversion to translation
    1039           0 :     fn[4] = -(f[4]*fn[0] + f[5]*fn[2]);
    1040           0 :     fn[5] = -(f[4]*fn[1] + f[5]*fn[3]);
    1041             : 
    1042           0 :     set( fn );
    1043           0 :     return true;
    1044             : }
    1045             : 
    1046           0 : void Matrix3::append( PDFWriterImpl::PDFPage& rPage, OStringBuffer& rBuffer, Point* pBack )
    1047             : {
    1048           0 :     appendDouble( f[0], rBuffer );
    1049           0 :     rBuffer.append( ' ' );
    1050           0 :     appendDouble( f[1], rBuffer );
    1051           0 :     rBuffer.append( ' ' );
    1052           0 :     appendDouble( f[2], rBuffer );
    1053           0 :     rBuffer.append( ' ' );
    1054           0 :     appendDouble( f[3], rBuffer );
    1055           0 :     rBuffer.append( ' ' );
    1056           0 :     rPage.appendPoint( Point( (long)f[4], (long)f[5] ), rBuffer, false, pBack );
    1057           0 : }
    1058             : 
    1059           0 : static void appendResourceMap( OStringBuffer& rBuf, const char* pPrefix, const PDFWriterImpl::ResourceMap& rList )
    1060             : {
    1061           0 :     if( rList.empty() )
    1062           0 :         return;
    1063           0 :     rBuf.append( '/' );
    1064           0 :     rBuf.append( pPrefix );
    1065           0 :     rBuf.append( "<<" );
    1066           0 :     int ni = 0;
    1067           0 :     for( PDFWriterImpl::ResourceMap::const_iterator it = rList.begin(); it != rList.end(); ++it )
    1068             :     {
    1069           0 :         if( !it->first.isEmpty() && it->second > 0 )
    1070             :         {
    1071           0 :             rBuf.append( '/' );
    1072           0 :             rBuf.append( it->first );
    1073           0 :             rBuf.append( ' ' );
    1074           0 :             rBuf.append( it->second );
    1075           0 :             rBuf.append( " 0 R" );
    1076           0 :             if( ((++ni) & 7) == 0 )
    1077           0 :                 rBuf.append( '\n' );
    1078             :         }
    1079             :     }
    1080           0 :     rBuf.append( ">>\n" );
    1081             : }
    1082             : 
    1083           0 : void PDFWriterImpl::ResourceDict::append( OStringBuffer& rBuf, sal_Int32 nFontDictObject )
    1084             : {
    1085           0 :     rBuf.append( "<</Font " );
    1086           0 :     rBuf.append( nFontDictObject );
    1087           0 :     rBuf.append( " 0 R\n" );
    1088           0 :     appendResourceMap( rBuf, "XObject", m_aXObjects );
    1089           0 :     appendResourceMap( rBuf, "ExtGState", m_aExtGStates );
    1090           0 :     appendResourceMap( rBuf, "Shading", m_aShadings );
    1091           0 :     appendResourceMap( rBuf, "Pattern", m_aPatterns );
    1092           0 :     rBuf.append( "/ProcSet[/PDF/Text" );
    1093           0 :     if( !m_aXObjects.empty() )
    1094           0 :         rBuf.append( "/ImageC/ImageI/ImageB" );
    1095           0 :     rBuf.append( "]\n>>\n" );
    1096           0 : };
    1097             : 
    1098           0 : PDFWriterImpl::PDFPage::PDFPage( PDFWriterImpl* pWriter, sal_Int32 nPageWidth, sal_Int32 nPageHeight, PDFWriter::Orientation eOrientation )
    1099             :         :
    1100             :         m_pWriter( pWriter ),
    1101             :         m_nPageWidth( nPageWidth ),
    1102             :         m_nPageHeight( nPageHeight ),
    1103             :         m_eOrientation( eOrientation ),
    1104             :         m_nPageObject( 0 ),  // invalid object number
    1105             :         m_nPageIndex( -1 ), // invalid index
    1106             :         m_nStreamLengthObject( 0 ),
    1107             :         m_nBeginStreamPos( 0 ),
    1108             :         m_eTransition( PDFWriter::Regular ),
    1109             :         m_nTransTime( 0 ),
    1110             :         m_nDuration( 0 ),
    1111           0 :         m_bHasWidgets( false )
    1112             : {
    1113             :     // object ref must be only ever updated in emit()
    1114           0 :     m_nPageObject = m_pWriter->createObject();
    1115           0 : }
    1116             : 
    1117           0 : PDFWriterImpl::PDFPage::~PDFPage()
    1118             : {
    1119           0 : }
    1120             : 
    1121           0 : void PDFWriterImpl::PDFPage::beginStream()
    1122             : {
    1123             : #if OSL_DEBUG_LEVEL > 1
    1124             :     {
    1125             :         OStringBuffer aLine( "PDFWriterImpl::PDFPage::beginStream, +" );
    1126             :          m_pWriter->emitComment( aLine.getStr() );
    1127             :     }
    1128             : #endif
    1129           0 :     m_aStreamObjects.push_back(m_pWriter->createObject());
    1130           0 :     if( ! m_pWriter->updateObject( m_aStreamObjects.back() ) )
    1131           0 :         return;
    1132             : 
    1133           0 :     m_nStreamLengthObject = m_pWriter->createObject();
    1134             :     // write content stream header
    1135           0 :     OStringBuffer aLine;
    1136           0 :     aLine.append( m_aStreamObjects.back() );
    1137           0 :     aLine.append( " 0 obj\n<</Length " );
    1138           0 :     aLine.append( m_nStreamLengthObject );
    1139           0 :     aLine.append( " 0 R" );
    1140             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1141           0 :     aLine.append( "/Filter/FlateDecode" );
    1142             : #endif
    1143           0 :     aLine.append( ">>\nstream\n" );
    1144           0 :     if( ! m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() ) )
    1145           0 :         return;
    1146           0 :     if( osl_File_E_None != osl_getFilePos( m_pWriter->m_aFile, &m_nBeginStreamPos ) )
    1147             :     {
    1148           0 :         osl_closeFile( m_pWriter->m_aFile );
    1149           0 :         m_pWriter->m_bOpen = false;
    1150             :     }
    1151             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1152           0 :     m_pWriter->beginCompression();
    1153             : #endif
    1154           0 :     m_pWriter->checkAndEnableStreamEncryption( m_aStreamObjects.back() );
    1155             : }
    1156             : 
    1157           0 : void PDFWriterImpl::PDFPage::endStream()
    1158             : {
    1159             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1160           0 :     m_pWriter->endCompression();
    1161             : #endif
    1162             :     sal_uInt64 nEndStreamPos;
    1163           0 :     if( osl_File_E_None != osl_getFilePos( m_pWriter->m_aFile, &nEndStreamPos ) )
    1164             :     {
    1165           0 :         osl_closeFile( m_pWriter->m_aFile );
    1166           0 :         m_pWriter->m_bOpen = false;
    1167           0 :         return;
    1168             :     }
    1169           0 :     m_pWriter->disableStreamEncryption();
    1170           0 :     if( ! m_pWriter->writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    1171           0 :         return;
    1172             :     // emit stream length object
    1173           0 :     if( ! m_pWriter->updateObject( m_nStreamLengthObject ) )
    1174           0 :         return;
    1175           0 :     OStringBuffer aLine;
    1176           0 :     aLine.append( m_nStreamLengthObject );
    1177           0 :     aLine.append( " 0 obj\n" );
    1178           0 :     aLine.append( (sal_Int64)(nEndStreamPos-m_nBeginStreamPos) );
    1179           0 :     aLine.append( "\nendobj\n\n" );
    1180           0 :     m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() );
    1181             : }
    1182             : 
    1183           0 : bool PDFWriterImpl::PDFPage::emit(sal_Int32 nParentObject )
    1184             : {
    1185             :     // emit page object
    1186           0 :     if( ! m_pWriter->updateObject( m_nPageObject ) )
    1187           0 :         return false;
    1188           0 :     OStringBuffer aLine;
    1189             : 
    1190           0 :     aLine.append( m_nPageObject );
    1191             :     aLine.append( " 0 obj\n"
    1192           0 :                   "<</Type/Page/Parent " );
    1193           0 :     aLine.append( nParentObject );
    1194           0 :     aLine.append( " 0 R" );
    1195           0 :     aLine.append( "/Resources " );
    1196           0 :     aLine.append( m_pWriter->getResourceDictObj() );
    1197           0 :     aLine.append( " 0 R" );
    1198           0 :     if( m_nPageWidth && m_nPageHeight )
    1199             :     {
    1200           0 :         aLine.append( "/MediaBox[0 0 " );
    1201           0 :         aLine.append( m_nPageWidth );
    1202           0 :         aLine.append( ' ' );
    1203           0 :         aLine.append( m_nPageHeight );
    1204           0 :         aLine.append( "]" );
    1205             :     }
    1206           0 :     switch( m_eOrientation )
    1207             :     {
    1208           0 :         case PDFWriter::Landscape: aLine.append( "/Rotate 90\n" );break;
    1209           0 :         case PDFWriter::Seascape:  aLine.append( "/Rotate -90\n" );break;
    1210           0 :         case PDFWriter::Portrait:  aLine.append( "/Rotate 0\n" );break;
    1211             : 
    1212             :         case PDFWriter::Inherit:
    1213             :         default:
    1214           0 :             break;
    1215             :     }
    1216           0 :     int nAnnots = m_aAnnotations.size();
    1217           0 :     if( nAnnots > 0 )
    1218             :     {
    1219           0 :         aLine.append( "/Annots[\n" );
    1220           0 :         for( int i = 0; i < nAnnots; i++ )
    1221             :         {
    1222           0 :             aLine.append( m_aAnnotations[i] );
    1223           0 :             aLine.append( " 0 R" );
    1224           0 :             aLine.append( ((i+1)%15) ? " " : "\n" );
    1225             :         }
    1226           0 :         aLine.append( "]\n" );
    1227             :     }
    1228           0 :     if( m_aMCIDParents.size() > 0 )
    1229             :     {
    1230           0 :         OStringBuffer aStructParents( 1024 );
    1231           0 :         aStructParents.append( "[ " );
    1232           0 :         int nParents = m_aMCIDParents.size();
    1233           0 :         for( int i = 0; i < nParents; i++ )
    1234             :         {
    1235           0 :             aStructParents.append( m_aMCIDParents[i] );
    1236           0 :             aStructParents.append( " 0 R" );
    1237           0 :             aStructParents.append( ((i%10) == 9) ? "\n" : " " );
    1238             :         }
    1239           0 :         aStructParents.append( "]" );
    1240           0 :         m_pWriter->m_aStructParentTree.push_back( aStructParents.makeStringAndClear() );
    1241             : 
    1242           0 :         aLine.append( "/StructParents " );
    1243           0 :         aLine.append( sal_Int32(m_pWriter->m_aStructParentTree.size()-1) );
    1244           0 :         aLine.append( "\n" );
    1245             :     }
    1246           0 :     if( m_nDuration > 0 )
    1247             :     {
    1248           0 :         aLine.append( "/Dur " );
    1249           0 :         aLine.append( (sal_Int32)m_nDuration );
    1250           0 :         aLine.append( "\n" );
    1251             :     }
    1252           0 :     if( m_eTransition != PDFWriter::Regular && m_nTransTime > 0 )
    1253             :     {
    1254             :         // transition duration
    1255           0 :         aLine.append( "/Trans<</D " );
    1256           0 :         appendDouble( (double)m_nTransTime/1000.0, aLine, 3 );
    1257           0 :         aLine.append( "\n" );
    1258           0 :         const char *pStyle = NULL, *pDm = NULL, *pM = NULL, *pDi = NULL;
    1259           0 :         switch( m_eTransition )
    1260             :         {
    1261             :             case PDFWriter::SplitHorizontalInward:
    1262           0 :                 pStyle = "Split"; pDm = "H"; pM = "I"; break;
    1263             :             case PDFWriter::SplitHorizontalOutward:
    1264           0 :                 pStyle = "Split"; pDm = "H"; pM = "O"; break;
    1265             :             case PDFWriter::SplitVerticalInward:
    1266           0 :                 pStyle = "Split"; pDm = "V"; pM = "I"; break;
    1267             :             case PDFWriter::SplitVerticalOutward:
    1268           0 :                 pStyle = "Split"; pDm = "V"; pM = "O"; break;
    1269             :             case PDFWriter::BlindsHorizontal:
    1270           0 :                 pStyle = "Blinds"; pDm = "H"; break;
    1271             :             case PDFWriter::BlindsVertical:
    1272           0 :                 pStyle = "Blinds"; pDm = "V"; break;
    1273             :             case PDFWriter::BoxInward:
    1274           0 :                 pStyle = "Box"; pM = "I"; break;
    1275             :             case PDFWriter::BoxOutward:
    1276           0 :                 pStyle = "Box"; pM = "O"; break;
    1277             :             case PDFWriter::WipeLeftToRight:
    1278           0 :                 pStyle = "Wipe"; pDi = "0"; break;
    1279             :             case PDFWriter::WipeBottomToTop:
    1280           0 :                 pStyle = "Wipe"; pDi = "90"; break;
    1281             :             case PDFWriter::WipeRightToLeft:
    1282           0 :                 pStyle = "Wipe"; pDi = "180"; break;
    1283             :             case PDFWriter::WipeTopToBottom:
    1284           0 :                 pStyle = "Wipe"; pDi = "270"; break;
    1285             :             case PDFWriter::Dissolve:
    1286           0 :                 pStyle = "Dissolve"; break;
    1287             :             case PDFWriter::GlitterLeftToRight:
    1288           0 :                 pStyle = "Glitter"; pDi = "0"; break;
    1289             :             case PDFWriter::GlitterTopToBottom:
    1290           0 :                 pStyle = "Glitter"; pDi = "270"; break;
    1291             :             case PDFWriter::GlitterTopLeftToBottomRight:
    1292           0 :                 pStyle = "Glitter"; pDi = "315"; break;
    1293             :             case PDFWriter::Regular:
    1294           0 :                 break;
    1295             :         }
    1296             :         // transition style
    1297           0 :         if( pStyle )
    1298             :         {
    1299           0 :             aLine.append( "/S/" );
    1300           0 :             aLine.append( pStyle );
    1301           0 :             aLine.append( "\n" );
    1302             :         }
    1303           0 :         if( pDm )
    1304             :         {
    1305           0 :             aLine.append( "/Dm/" );
    1306           0 :             aLine.append( pDm );
    1307           0 :             aLine.append( "\n" );
    1308             :         }
    1309           0 :         if( pM )
    1310             :         {
    1311           0 :             aLine.append( "/M/" );
    1312           0 :             aLine.append( pM );
    1313           0 :             aLine.append( "\n" );
    1314             :         }
    1315           0 :         if( pDi  )
    1316             :         {
    1317           0 :             aLine.append( "/Di " );
    1318           0 :             aLine.append( pDi );
    1319           0 :             aLine.append( "\n" );
    1320             :         }
    1321           0 :         aLine.append( ">>\n" );
    1322             :     }
    1323           0 :     if( m_pWriter->getVersion() > PDFWriter::PDF_1_3 && ! m_pWriter->m_bIsPDF_A1 )
    1324             :     {
    1325           0 :         aLine.append( "/Group<</S/Transparency/CS/DeviceRGB/I true>>" );
    1326             :     }
    1327           0 :     aLine.append( "/Contents" );
    1328           0 :     unsigned int nStreamObjects = m_aStreamObjects.size();
    1329           0 :     if( nStreamObjects > 1 )
    1330           0 :         aLine.append( '[' );
    1331           0 :     for( unsigned int i = 0; i < m_aStreamObjects.size(); i++ )
    1332             :     {
    1333           0 :         aLine.append( ' ' );
    1334           0 :         aLine.append( m_aStreamObjects[i] );
    1335           0 :         aLine.append( " 0 R" );
    1336             :     }
    1337           0 :     if( nStreamObjects > 1 )
    1338           0 :         aLine.append( ']' );
    1339           0 :     aLine.append( ">>\nendobj\n\n" );
    1340           0 :     return m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() );
    1341             : }
    1342             : 
    1343             : namespace vcl
    1344             : {
    1345             : template < class GEOMETRY >
    1346           0 : GEOMETRY lcl_convert( const MapMode& _rSource, const MapMode& _rDest, OutputDevice* _pPixelConversion, const GEOMETRY& _rObject )
    1347             : {
    1348           0 :     GEOMETRY aPoint;
    1349           0 :     if ( MAP_PIXEL == _rSource.GetMapUnit() )
    1350             :     {
    1351           0 :         aPoint = _pPixelConversion->PixelToLogic( _rObject, _rDest );
    1352             :     }
    1353             :     else
    1354             :     {
    1355           0 :         aPoint = OutputDevice::LogicToLogic( _rObject, _rSource, _rDest );
    1356             :     }
    1357           0 :     return aPoint;
    1358             : }
    1359             : }
    1360             : 
    1361           0 : void PDFWriterImpl::PDFPage::appendPoint( const Point& rPoint, OStringBuffer& rBuffer, bool bNeg, Point* pOutPoint ) const
    1362             : {
    1363           0 :     if( pOutPoint )
    1364             :     {
    1365           0 :         Point aPoint( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1366             :                                    m_pWriter->m_aMapMode,
    1367             :                                    m_pWriter->getReferenceDevice(),
    1368           0 :                                    rPoint ) );
    1369           0 :         *pOutPoint = aPoint;
    1370             :     }
    1371             : 
    1372           0 :     Point aPoint( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1373             :                                m_pWriter->m_aMapMode,
    1374             :                                m_pWriter->getReferenceDevice(),
    1375           0 :                                rPoint ) );
    1376             : 
    1377           0 :     sal_Int32 nValue    = aPoint.X();
    1378           0 :     if( bNeg )
    1379           0 :         nValue = -nValue;
    1380             : 
    1381           0 :     appendFixedInt( nValue, rBuffer );
    1382             : 
    1383           0 :     rBuffer.append( ' ' );
    1384             : 
    1385           0 :     nValue      = pointToPixel(getHeight()) - aPoint.Y();
    1386           0 :     if( bNeg )
    1387           0 :         nValue = -nValue;
    1388             : 
    1389           0 :     appendFixedInt( nValue, rBuffer );
    1390           0 : }
    1391             : 
    1392           0 : void PDFWriterImpl::PDFPage::appendPixelPoint( const basegfx::B2DPoint& rPoint, OStringBuffer& rBuffer ) const
    1393             : {
    1394           0 :     double fValue   = pixelToPoint(rPoint.getX());
    1395             : 
    1396           0 :     appendDouble( fValue, rBuffer, nLog10Divisor );
    1397           0 :     rBuffer.append( ' ' );
    1398           0 :     fValue      = double(getHeight()) - pixelToPoint(rPoint.getY());
    1399           0 :     appendDouble( fValue, rBuffer, nLog10Divisor );
    1400           0 : }
    1401             : 
    1402           0 : void PDFWriterImpl::PDFPage::appendRect( const Rectangle& rRect, OStringBuffer& rBuffer ) const
    1403             : {
    1404           0 :     appendPoint( rRect.BottomLeft() + Point( 0, 1 ), rBuffer );
    1405           0 :     rBuffer.append( ' ' );
    1406           0 :     appendMappedLength( (sal_Int32)rRect.GetWidth(), rBuffer, false );
    1407           0 :     rBuffer.append( ' ' );
    1408           0 :     appendMappedLength( (sal_Int32)rRect.GetHeight(), rBuffer, true );
    1409           0 :     rBuffer.append( " re" );
    1410           0 : }
    1411             : 
    1412           0 : void PDFWriterImpl::PDFPage::convertRect( Rectangle& rRect ) const
    1413             : {
    1414           0 :     Point aLL = lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1415             :                              m_pWriter->m_aMapMode,
    1416             :                              m_pWriter->getReferenceDevice(),
    1417           0 :                              rRect.BottomLeft() + Point( 0, 1 )
    1418           0 :                              );
    1419           0 :     Size aSize = lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1420             :                               m_pWriter->m_aMapMode,
    1421             :                               m_pWriter->getReferenceDevice(),
    1422           0 :                               rRect.GetSize() );
    1423           0 :     rRect.Left()    = aLL.X();
    1424           0 :     rRect.Right()   = aLL.X() + aSize.Width();
    1425           0 :     rRect.Top()     = pointToPixel(getHeight()) - aLL.Y();
    1426           0 :     rRect.Bottom()  = rRect.Top() + aSize.Height();
    1427           0 : }
    1428             : 
    1429           0 : void PDFWriterImpl::PDFPage::appendPolygon( const Polygon& rPoly, OStringBuffer& rBuffer, bool bClose ) const
    1430             : {
    1431           0 :     sal_uInt16 nPoints = rPoly.GetSize();
    1432             :     /*
    1433             :      *  #108582# applications do weird things
    1434             :      */
    1435           0 :     sal_uInt32 nBufLen = rBuffer.getLength();
    1436           0 :     if( nPoints > 0 )
    1437             :     {
    1438           0 :         const sal_uInt8* pFlagArray = rPoly.GetConstFlagAry();
    1439           0 :         appendPoint( rPoly[0], rBuffer );
    1440           0 :         rBuffer.append( " m\n" );
    1441           0 :         for( sal_uInt16 i = 1; i < nPoints; i++ )
    1442             :         {
    1443           0 :             if( pFlagArray && pFlagArray[i] == POLY_CONTROL && nPoints-i > 2 )
    1444             :             {
    1445             :                 // bezier
    1446             :                 DBG_ASSERT( pFlagArray[i+1] == POLY_CONTROL && pFlagArray[i+2] != POLY_CONTROL, "unexpected sequence of control points" );
    1447           0 :                 appendPoint( rPoly[i], rBuffer );
    1448           0 :                 rBuffer.append( " " );
    1449           0 :                 appendPoint( rPoly[i+1], rBuffer );
    1450           0 :                 rBuffer.append( " " );
    1451           0 :                 appendPoint( rPoly[i+2], rBuffer );
    1452           0 :                 rBuffer.append( " c" );
    1453           0 :                 i += 2; // add additionally consumed points
    1454             :             }
    1455             :             else
    1456             :             {
    1457             :                 // line
    1458           0 :                 appendPoint( rPoly[i], rBuffer );
    1459           0 :                 rBuffer.append( " l" );
    1460             :             }
    1461           0 :             if( (rBuffer.getLength() - nBufLen) > 65 )
    1462             :             {
    1463           0 :                 rBuffer.append( "\n" );
    1464           0 :                 nBufLen = rBuffer.getLength();
    1465             :             }
    1466             :             else
    1467           0 :                 rBuffer.append( " " );
    1468             :         }
    1469           0 :         if( bClose )
    1470           0 :             rBuffer.append( "h\n" );
    1471             :     }
    1472           0 : }
    1473             : 
    1474           0 : void PDFWriterImpl::PDFPage::appendPolygon( const basegfx::B2DPolygon& rPoly, OStringBuffer& rBuffer, bool bClose ) const
    1475             : {
    1476           0 :     basegfx::B2DPolygon aPoly( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1477             :                                             m_pWriter->m_aMapMode,
    1478             :                                             m_pWriter->getReferenceDevice(),
    1479           0 :                                             rPoly ) );
    1480             : 
    1481           0 :     if( basegfx::tools::isRectangle( aPoly ) )
    1482             :     {
    1483           0 :         basegfx::B2DRange aRange( aPoly.getB2DRange() );
    1484           0 :         basegfx::B2DPoint aBL( aRange.getMinX(), aRange.getMaxY() );
    1485           0 :         appendPixelPoint( aBL, rBuffer );
    1486           0 :         rBuffer.append( ' ' );
    1487           0 :         appendMappedLength( aRange.getWidth(), rBuffer, false, NULL, nLog10Divisor );
    1488           0 :         rBuffer.append( ' ' );
    1489           0 :         appendMappedLength( aRange.getHeight(), rBuffer, true, NULL, nLog10Divisor );
    1490           0 :         rBuffer.append( " re\n" );
    1491           0 :         return;
    1492             :     }
    1493           0 :     sal_uInt32 nPoints = aPoly.count();
    1494           0 :     if( nPoints > 0 )
    1495             :     {
    1496           0 :         sal_uInt32 nBufLen = rBuffer.getLength();
    1497           0 :         basegfx::B2DPoint aLastPoint( aPoly.getB2DPoint( 0 ) );
    1498           0 :         appendPixelPoint( aLastPoint, rBuffer );
    1499           0 :         rBuffer.append( " m\n" );
    1500           0 :         for( sal_uInt32 i = 1; i <= nPoints; i++ )
    1501             :         {
    1502           0 :             if( i != nPoints || aPoly.isClosed() )
    1503             :             {
    1504           0 :                 sal_uInt32 nCurPoint  = i % nPoints;
    1505           0 :                 sal_uInt32 nLastPoint = i-1;
    1506           0 :                 basegfx::B2DPoint aPoint( aPoly.getB2DPoint( nCurPoint ) );
    1507           0 :                 if( aPoly.isNextControlPointUsed( nLastPoint ) &&
    1508           0 :                     aPoly.isPrevControlPointUsed( nCurPoint ) )
    1509             :                 {
    1510           0 :                     appendPixelPoint( aPoly.getNextControlPoint( nLastPoint ), rBuffer );
    1511           0 :                     rBuffer.append( ' ' );
    1512           0 :                     appendPixelPoint( aPoly.getPrevControlPoint( nCurPoint ), rBuffer );
    1513           0 :                     rBuffer.append( ' ' );
    1514           0 :                     appendPixelPoint( aPoint, rBuffer );
    1515           0 :                     rBuffer.append( " c" );
    1516             :                 }
    1517           0 :                 else if( aPoly.isNextControlPointUsed( nLastPoint ) )
    1518             :                 {
    1519           0 :                     appendPixelPoint( aPoly.getNextControlPoint( nLastPoint ), rBuffer );
    1520           0 :                     rBuffer.append( ' ' );
    1521           0 :                     appendPixelPoint( aPoint, rBuffer );
    1522           0 :                     rBuffer.append( " y" );
    1523             :                 }
    1524           0 :                 else if( aPoly.isPrevControlPointUsed( nCurPoint ) )
    1525             :                 {
    1526           0 :                     appendPixelPoint( aPoly.getPrevControlPoint( nCurPoint ), rBuffer );
    1527           0 :                     rBuffer.append( ' ' );
    1528           0 :                     appendPixelPoint( aPoint, rBuffer );
    1529           0 :                     rBuffer.append( " v" );
    1530             :                 }
    1531             :                 else
    1532             :                 {
    1533           0 :                     appendPixelPoint( aPoint, rBuffer );
    1534           0 :                     rBuffer.append( " l" );
    1535             :                 }
    1536           0 :                 if( (rBuffer.getLength() - nBufLen) > 65 )
    1537             :                 {
    1538           0 :                     rBuffer.append( "\n" );
    1539           0 :                     nBufLen = rBuffer.getLength();
    1540             :                 }
    1541             :                 else
    1542           0 :                     rBuffer.append( " " );
    1543             :             }
    1544             :         }
    1545           0 :         if( bClose )
    1546           0 :             rBuffer.append( "h\n" );
    1547           0 :     }
    1548             : }
    1549             : 
    1550           0 : void PDFWriterImpl::PDFPage::appendPolyPolygon( const PolyPolygon& rPolyPoly, OStringBuffer& rBuffer, bool bClose ) const
    1551             : {
    1552           0 :     sal_uInt16 nPolygons = rPolyPoly.Count();
    1553           0 :     for( sal_uInt16 n = 0; n < nPolygons; n++ )
    1554           0 :         appendPolygon( rPolyPoly[n], rBuffer, bClose );
    1555           0 : }
    1556             : 
    1557           0 : void PDFWriterImpl::PDFPage::appendPolyPolygon( const basegfx::B2DPolyPolygon& rPolyPoly, OStringBuffer& rBuffer, bool bClose ) const
    1558             : {
    1559           0 :     sal_uInt32 nPolygons = rPolyPoly.count();
    1560           0 :     for( sal_uInt32 n = 0; n < nPolygons; n++ )
    1561           0 :         appendPolygon( rPolyPoly.getB2DPolygon( n ), rBuffer, bClose );
    1562           0 : }
    1563             : 
    1564           0 : void PDFWriterImpl::PDFPage::appendMappedLength( sal_Int32 nLength, OStringBuffer& rBuffer, bool bVertical, sal_Int32* pOutLength ) const
    1565             : {
    1566           0 :     sal_Int32 nValue = nLength;
    1567           0 :     if ( nLength < 0 )
    1568             :     {
    1569           0 :         rBuffer.append( '-' );
    1570           0 :         nValue = -nLength;
    1571             :     }
    1572           0 :     Size aSize( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1573             :                              m_pWriter->m_aMapMode,
    1574             :                              m_pWriter->getReferenceDevice(),
    1575           0 :                              Size( nValue, nValue ) ) );
    1576           0 :     nValue = bVertical ? aSize.Height() : aSize.Width();
    1577           0 :     if( pOutLength )
    1578           0 :         *pOutLength = ((nLength < 0 ) ? -nValue : nValue);
    1579             : 
    1580           0 :     appendFixedInt( nValue, rBuffer, 1 );
    1581           0 : }
    1582             : 
    1583           0 : void PDFWriterImpl::PDFPage::appendMappedLength( double fLength, OStringBuffer& rBuffer, bool bVertical, sal_Int32* pOutLength, sal_Int32 nPrecision ) const
    1584             : {
    1585           0 :     Size aSize( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1586             :                              m_pWriter->m_aMapMode,
    1587             :                              m_pWriter->getReferenceDevice(),
    1588           0 :                              Size( 1000, 1000 ) ) );
    1589           0 :     if( pOutLength )
    1590           0 :         *pOutLength = (sal_Int32)(fLength*(double)(bVertical ? aSize.Height() : aSize.Width())/1000.0);
    1591           0 :     fLength *= pixelToPoint((double)(bVertical ? aSize.Height() : aSize.Width()) / 1000.0);
    1592           0 :     appendDouble( fLength, rBuffer, nPrecision );
    1593           0 : }
    1594             : 
    1595           0 : bool PDFWriterImpl::PDFPage::appendLineInfo( const LineInfo& rInfo, OStringBuffer& rBuffer ) const
    1596             : {
    1597           0 :     if(LINE_DASH == rInfo.GetStyle() && rInfo.GetDashLen() != rInfo.GetDotLen())
    1598             :     {
    1599             :         // dashed and non-degraded case, check for implementation limits of dash array
    1600             :         // in PDF reader apps (e.g. acroread)
    1601           0 :         if(2 * (rInfo.GetDashCount() + rInfo.GetDotCount()) > 10)
    1602             :         {
    1603           0 :             return false;
    1604             :         }
    1605             :     }
    1606             : 
    1607           0 :     if(basegfx::B2DLINEJOIN_NONE != rInfo.GetLineJoin())
    1608             :     {
    1609             :         // LineJoin used, ExtLineInfo required
    1610           0 :         return false;
    1611             :     }
    1612             : 
    1613           0 :     if(com::sun::star::drawing::LineCap_BUTT != rInfo.GetLineCap())
    1614             :     {
    1615             :         // LineCap used, ExtLineInfo required
    1616           0 :         return false;
    1617             :     }
    1618             : 
    1619           0 :     if( rInfo.GetStyle() == LINE_DASH )
    1620             :     {
    1621           0 :         rBuffer.append( "[ " );
    1622           0 :         if( rInfo.GetDashLen() == rInfo.GetDotLen() ) // degraded case
    1623             :         {
    1624           0 :             appendMappedLength( (sal_Int32)rInfo.GetDashLen(), rBuffer );
    1625           0 :             rBuffer.append( ' ' );
    1626           0 :             appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1627           0 :             rBuffer.append( ' ' );
    1628             :         }
    1629             :         else
    1630             :         {
    1631           0 :             for( int n = 0; n < rInfo.GetDashCount(); n++ )
    1632             :             {
    1633           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDashLen(), rBuffer );
    1634           0 :                 rBuffer.append( ' ' );
    1635           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1636           0 :                 rBuffer.append( ' ' );
    1637             :             }
    1638           0 :             for( int m = 0; m < rInfo.GetDotCount(); m++ )
    1639             :             {
    1640           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDotLen(), rBuffer );
    1641           0 :                 rBuffer.append( ' ' );
    1642           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1643           0 :                 rBuffer.append( ' ' );
    1644             :             }
    1645             :         }
    1646           0 :         rBuffer.append( "] 0 d\n" );
    1647             :     }
    1648             : 
    1649           0 :     if( rInfo.GetWidth() > 1 )
    1650             :     {
    1651           0 :         appendMappedLength( (sal_Int32)rInfo.GetWidth(), rBuffer );
    1652           0 :         rBuffer.append( " w\n" );
    1653             :     }
    1654           0 :     else if( rInfo.GetWidth() == 0 )
    1655             :     {
    1656             :         // "pixel" line
    1657           0 :         appendDouble( 72.0/double(m_pWriter->getReferenceDevice()->ImplGetDPIX()), rBuffer );
    1658           0 :         rBuffer.append( " w\n" );
    1659             :     }
    1660             : 
    1661           0 :     return true;
    1662             : }
    1663             : 
    1664           0 : void PDFWriterImpl::PDFPage::appendWaveLine( sal_Int32 nWidth, sal_Int32 nY, sal_Int32 nDelta, OStringBuffer& rBuffer ) const
    1665             : {
    1666           0 :     if( nWidth <= 0 )
    1667           0 :         return;
    1668           0 :     if( nDelta < 1 )
    1669           0 :         nDelta = 1;
    1670             : 
    1671           0 :     rBuffer.append( "0 " );
    1672           0 :     appendMappedLength( nY, rBuffer, true );
    1673           0 :     rBuffer.append( " m\n" );
    1674           0 :     for( sal_Int32 n = 0; n < nWidth; )
    1675             :     {
    1676           0 :         n += nDelta;
    1677           0 :         appendMappedLength( n, rBuffer, false );
    1678           0 :         rBuffer.append( ' ' );
    1679           0 :         appendMappedLength( nDelta+nY, rBuffer, true );
    1680           0 :         rBuffer.append( ' ' );
    1681           0 :         n += nDelta;
    1682           0 :         appendMappedLength( n, rBuffer, false );
    1683           0 :         rBuffer.append( ' ' );
    1684           0 :         appendMappedLength( nY, rBuffer, true );
    1685           0 :         rBuffer.append( " v " );
    1686           0 :         if( n < nWidth )
    1687             :         {
    1688           0 :             n += nDelta;
    1689           0 :             appendMappedLength( n, rBuffer, false );
    1690           0 :             rBuffer.append( ' ' );
    1691           0 :             appendMappedLength( nY-nDelta, rBuffer, true );
    1692           0 :             rBuffer.append( ' ' );
    1693           0 :             n += nDelta;
    1694           0 :             appendMappedLength( n, rBuffer, false );
    1695           0 :             rBuffer.append( ' ' );
    1696           0 :             appendMappedLength( nY, rBuffer, true );
    1697           0 :             rBuffer.append( " v\n" );
    1698             :         }
    1699             :     }
    1700           0 :     rBuffer.append( "S\n" );
    1701             : }
    1702             : 
    1703           0 :  PDFWriterImpl::PDFWriterImpl( const PDFWriter::PDFWriterContext& rContext,
    1704             :                                const com::sun::star::uno::Reference< com::sun::star::beans::XMaterialHolder >& xEnc,
    1705             :                                PDFWriter& i_rOuterFace)
    1706             :         :
    1707             :         m_pReferenceDevice( NULL ),
    1708             :         m_aMapMode( MAP_POINT, Point(), Fraction( 1L, pointToPixel(1) ), Fraction( 1L, pointToPixel(1) ) ),
    1709             :         m_nCurrentStructElement( 0 ),
    1710             :         m_bEmitStructure( true ),
    1711             :         m_bNewMCID( false ),
    1712             :         m_nNextFID( 1 ),
    1713             :         m_nInheritedPageWidth( 595 ),  // default A4
    1714             :         m_nInheritedPageHeight( 842 ), // default A4
    1715             :         m_eInheritedOrientation( PDFWriter::Portrait ),
    1716             :         m_nCurrentPage( -1 ),
    1717             :         m_nCatalogObject(0),
    1718             :         m_nSignatureObject( -1 ),
    1719             :         m_nSignatureContentOffset( 0 ),
    1720             :         m_nSignatureLastByteRangeNoOffset( 0 ),
    1721             :         m_nResourceDict( -1 ),
    1722             :         m_nFontDictObject( -1 ),
    1723             :         m_aFile(0),
    1724             :         m_bOpen(false),
    1725             :         m_pCodec( NULL ),
    1726             :         m_pMemStream(NULL),
    1727           0 :         m_aDocDigest( rtl_digest_createMD5() ),
    1728             :         m_aCipher( (rtlCipher)NULL ),
    1729             :         m_aDigest( NULL ),
    1730             :         m_nKeyLength(0),
    1731             :         m_nRC4KeyLength(0),
    1732             :         m_bEncryptThisStream( false ),
    1733             :         m_nAccessPermissions(0),
    1734             :         m_pEncryptionBuffer( NULL ),
    1735             :         m_nEncryptionBufferSize( 0 ),
    1736             :         m_bIsPDF_A1( false ),
    1737           0 :         m_rOuterFace( i_rOuterFace )
    1738             : {
    1739             : #ifdef DO_TEST_PDF
    1740             :     static bool bOnce = true;
    1741             :     if( bOnce )
    1742             :     {
    1743             :         bOnce = false;
    1744             :         doTestCode();
    1745             :     }
    1746             : #endif
    1747           0 :     m_aContext = rContext;
    1748           0 :     m_aStructure.push_back( PDFStructureElement() );
    1749           0 :     m_aStructure[0].m_nOwnElement       = 0;
    1750           0 :     m_aStructure[0].m_nParentElement    = 0;
    1751             : 
    1752           0 :     Font aFont;
    1753           0 :     aFont.SetName( OUString( "Times" ) );
    1754           0 :     aFont.SetSize( Size( 0, 12 ) );
    1755             : 
    1756           0 :     GraphicsState aState;
    1757           0 :     aState.m_aMapMode       = m_aMapMode;
    1758           0 :     aState.m_aFont          = aFont;
    1759           0 :     m_aGraphicsStack.push_front( aState );
    1760             : 
    1761           0 :     oslFileError  aError = osl_openFile( m_aContext.URL.pData, &m_aFile, osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
    1762           0 :     if( aError != osl_File_E_None )
    1763             :     {
    1764           0 :         if( aError == osl_File_E_EXIST )
    1765             :         {
    1766           0 :             aError = osl_openFile( m_aContext.URL.pData, &m_aFile, osl_File_OpenFlag_Write );
    1767           0 :             if( aError == osl_File_E_None )
    1768           0 :                 aError = osl_setFileSize( m_aFile, 0 );
    1769             :         }
    1770             :     }
    1771           0 :     if( aError != osl_File_E_None )
    1772           0 :         return;
    1773             : 
    1774           0 :     m_bOpen = true;
    1775             : 
    1776             :     // setup DocInfo
    1777           0 :     setupDocInfo();
    1778             : 
    1779             :     /* prepare the cypher engine, can be done in CTOR, free in DTOR */
    1780           0 :     m_aCipher = rtl_cipher_createARCFOUR( rtl_Cipher_ModeStream );
    1781           0 :     m_aDigest = rtl_digest_createMD5();
    1782             : 
    1783             :     /* the size of the Codec default maximum */
    1784           0 :     checkEncryptionBufferSize( 0x4000 );
    1785             : 
    1786           0 :     if( xEnc.is() )
    1787           0 :         prepareEncryption( xEnc );
    1788             : 
    1789           0 :     if( m_aContext.Encryption.Encrypt() )
    1790             :     {
    1791             :         // sanity check
    1792           0 :         if( m_aContext.Encryption.OValue.size() != ENCRYPTED_PWD_SIZE ||
    1793           0 :             m_aContext.Encryption.UValue.size() != ENCRYPTED_PWD_SIZE ||
    1794           0 :             m_aContext.Encryption.EncryptionKey.size() != MAXIMUM_RC4_KEY_LENGTH
    1795             :            )
    1796             :         {
    1797             :             // the field lengths are invalid ? This was not setup by initEncryption.
    1798             :             // do not encrypt after all
    1799           0 :             m_aContext.Encryption.OValue.clear();
    1800           0 :             m_aContext.Encryption.UValue.clear();
    1801             :             OSL_ENSURE( false, "encryption data failed sanity check, encryption disabled" );
    1802             :         }
    1803             :         else // setup key lengths
    1804           0 :             m_nAccessPermissions = computeAccessPermissions( m_aContext.Encryption, m_nKeyLength, m_nRC4KeyLength );
    1805             :     }
    1806             : 
    1807             :     // write header
    1808           0 :     OStringBuffer aBuffer( 20 );
    1809           0 :     aBuffer.append( "%PDF-" );
    1810           0 :     switch( m_aContext.Version )
    1811             :     {
    1812           0 :         case PDFWriter::PDF_1_2: aBuffer.append( "1.2" );break;
    1813           0 :         case PDFWriter::PDF_1_3: aBuffer.append( "1.3" );break;
    1814             :         case PDFWriter::PDF_A_1:
    1815             :         default:
    1816           0 :         case PDFWriter::PDF_1_4: aBuffer.append( "1.4" );break;
    1817           0 :         case PDFWriter::PDF_1_5: aBuffer.append( "1.5" );break;
    1818             :     }
    1819             :     // append something binary as comment (suggested in PDF Reference)
    1820           0 :     aBuffer.append( "\n%\303\244\303\274\303\266\303\237\n" );
    1821           0 :     if( !writeBuffer( aBuffer.getStr(), aBuffer.getLength() ) )
    1822             :     {
    1823           0 :         osl_closeFile( m_aFile );
    1824           0 :         m_bOpen = false;
    1825           0 :         return;
    1826             :     }
    1827             : 
    1828             :     // insert outline root
    1829           0 :     m_aOutline.push_back( PDFOutlineEntry() );
    1830             : 
    1831           0 :     m_bIsPDF_A1 = (m_aContext.Version == PDFWriter::PDF_A_1);
    1832           0 :     if( m_bIsPDF_A1 )
    1833           0 :         m_aContext.Version = PDFWriter::PDF_1_4; //meaning we need PDF 1.4, PDF/A flavour
    1834             : }
    1835             : 
    1836           0 : PDFWriterImpl::~PDFWriterImpl()
    1837             : {
    1838           0 :     if( m_aDocDigest )
    1839           0 :         rtl_digest_destroyMD5( m_aDocDigest );
    1840           0 :     delete static_cast<VirtualDevice*>(m_pReferenceDevice);
    1841             : 
    1842           0 :     if( m_aCipher )
    1843           0 :         rtl_cipher_destroyARCFOUR( m_aCipher );
    1844           0 :     if( m_aDigest )
    1845           0 :         rtl_digest_destroyMD5( m_aDigest );
    1846             : 
    1847           0 :     rtl_freeMemory( m_pEncryptionBuffer );
    1848           0 : }
    1849             : 
    1850           0 : void PDFWriterImpl::setupDocInfo()
    1851             : {
    1852           0 :     std::vector< sal_uInt8 > aId;
    1853           0 :     computeDocumentIdentifier( aId, m_aContext.DocumentInfo, m_aCreationDateString, m_aCreationMetaDateString );
    1854           0 :     if( m_aContext.Encryption.DocumentIdentifier.empty() )
    1855           0 :         m_aContext.Encryption.DocumentIdentifier = aId;
    1856           0 : }
    1857             : 
    1858           0 : void PDFWriterImpl::computeDocumentIdentifier( std::vector< sal_uInt8 >& o_rIdentifier,
    1859             :                                                const vcl::PDFWriter::PDFDocInfo& i_rDocInfo,
    1860             :                                                OString& o_rCString1,
    1861             :                                                OString& o_rCString2
    1862             :                                                )
    1863             : {
    1864           0 :     o_rIdentifier.clear();
    1865             : 
    1866             :     //build the document id
    1867           0 :     OString aInfoValuesOut;
    1868           0 :     OStringBuffer aID( 1024 );
    1869           0 :     if( !i_rDocInfo.Title.isEmpty() )
    1870           0 :         appendUnicodeTextString( i_rDocInfo.Title, aID );
    1871           0 :     if( !i_rDocInfo.Author.isEmpty() )
    1872           0 :         appendUnicodeTextString( i_rDocInfo.Author, aID );
    1873           0 :     if( !i_rDocInfo.Subject.isEmpty() )
    1874           0 :         appendUnicodeTextString( i_rDocInfo.Subject, aID );
    1875           0 :     if( !i_rDocInfo.Keywords.isEmpty() )
    1876           0 :         appendUnicodeTextString( i_rDocInfo.Keywords, aID );
    1877           0 :     if( !i_rDocInfo.Creator.isEmpty() )
    1878           0 :         appendUnicodeTextString( i_rDocInfo.Creator, aID );
    1879           0 :     if( !i_rDocInfo.Producer.isEmpty() )
    1880           0 :         appendUnicodeTextString( i_rDocInfo.Producer, aID );
    1881             : 
    1882             :     TimeValue aTVal, aGMT;
    1883             :     oslDateTime aDT;
    1884           0 :     osl_getSystemTime( &aGMT );
    1885           0 :     osl_getLocalTimeFromSystemTime( &aGMT, &aTVal );
    1886           0 :     osl_getDateTimeFromTimeValue( &aTVal, &aDT );
    1887           0 :     OStringBuffer aCreationDateString(64), aCreationMetaDateString(64);
    1888           0 :     aCreationDateString.append( "D:" );
    1889           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/1000)%10)) );
    1890           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/100)%10)) );
    1891           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/10)%10)) );
    1892           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year)%10)) );
    1893           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Month/10)%10)) );
    1894           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Month)%10)) );
    1895           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Day/10)%10)) );
    1896           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Day)%10)) );
    1897           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Hours/10)%10)) );
    1898           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Hours)%10)) );
    1899           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Minutes/10)%10)) );
    1900           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Minutes)%10)) );
    1901           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Seconds/10)%10)) );
    1902           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Seconds)%10)) );
    1903             : 
    1904             :     //--> i59651, we fill the Metadata date string as well, if PDF/A is requested
    1905             :     // according to ISO 19005-1:2005 6.7.3 the date is corrected for
    1906             :     // local time zone offset UTC only, whereas Acrobat 8 seems
    1907             :     // to use the localtime notation only
    1908             :     // according to a recommendation in XMP Specification (Jan 2004, page 75)
    1909             :     // the Acrobat way seems the right approach
    1910           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/1000)%10)) );
    1911           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/100)%10)) );
    1912           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/10)%10)) );
    1913           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year)%10)) );
    1914           0 :     aCreationMetaDateString.append( "-" );
    1915           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Month/10)%10)) );
    1916           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Month)%10)) );
    1917           0 :     aCreationMetaDateString.append( "-" );
    1918           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Day/10)%10)) );
    1919           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Day)%10)) );
    1920           0 :     aCreationMetaDateString.append( "T" );
    1921           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Hours/10)%10)) );
    1922           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Hours)%10)) );
    1923           0 :     aCreationMetaDateString.append( ":" );
    1924           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Minutes/10)%10)) );
    1925           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Minutes)%10)) );
    1926           0 :     aCreationMetaDateString.append( ":" );
    1927           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Seconds/10)%10)) );
    1928           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Seconds)%10)) );
    1929             : 
    1930           0 :     sal_uInt32 nDelta = 0;
    1931           0 :     if( aGMT.Seconds > aTVal.Seconds )
    1932             :     {
    1933           0 :         aCreationDateString.append( "-" );
    1934           0 :         nDelta = aGMT.Seconds-aTVal.Seconds;
    1935           0 :         aCreationMetaDateString.append( "-" );
    1936             :     }
    1937           0 :     else if( aGMT.Seconds < aTVal.Seconds )
    1938             :     {
    1939           0 :         aCreationDateString.append( "+" );
    1940           0 :         nDelta = aTVal.Seconds-aGMT.Seconds;
    1941           0 :         aCreationMetaDateString.append( "+" );
    1942             :     }
    1943             :     else
    1944             :     {
    1945           0 :         aCreationDateString.append( "Z" );
    1946           0 :         aCreationMetaDateString.append( "Z" );
    1947             : 
    1948             :     }
    1949           0 :     if( nDelta )
    1950             :     {
    1951           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/36000)%10)) );
    1952           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/3600)%10)) );
    1953           0 :         aCreationDateString.append( "'" );
    1954           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/600)%6)) );
    1955           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/60)%10)) );
    1956             : 
    1957           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/36000)%10)) );
    1958           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/3600)%10)) );
    1959           0 :         aCreationMetaDateString.append( ":" );
    1960           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/600)%6)) );
    1961           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/60)%10)) );
    1962             :     }
    1963           0 :     aCreationDateString.append( "'" );
    1964           0 :     aID.append( aCreationDateString.getStr(), aCreationDateString.getLength() );
    1965             : 
    1966           0 :     aInfoValuesOut = aID.makeStringAndClear();
    1967           0 :     o_rCString1 = aCreationDateString.makeStringAndClear();
    1968           0 :     o_rCString2 = aCreationMetaDateString.makeStringAndClear();
    1969             : 
    1970           0 :     rtlDigest aDigest = rtl_digest_createMD5();
    1971             :     OSL_ENSURE( aDigest != NULL, "PDFWriterImpl::computeDocumentIdentifier: cannot obtain a digest object !" );
    1972           0 :     if( aDigest )
    1973             :     {
    1974           0 :         rtlDigestError nError = rtl_digest_updateMD5( aDigest, &aGMT, sizeof( aGMT ) );
    1975           0 :         if( nError == rtl_Digest_E_None )
    1976           0 :             nError = rtl_digest_updateMD5( aDigest, aInfoValuesOut.getStr(), aInfoValuesOut.getLength() );
    1977           0 :         if( nError == rtl_Digest_E_None )
    1978             :         {
    1979           0 :             o_rIdentifier = std::vector< sal_uInt8 >( 16, 0 );
    1980             :             //the binary form of the doc id is needed for encryption stuff
    1981           0 :             rtl_digest_getMD5( aDigest, &o_rIdentifier[0], 16 );
    1982             :         }
    1983           0 :         rtl_digest_destroyMD5(aDigest);
    1984           0 :     }
    1985           0 : }
    1986             : 
    1987             : /* i12626 methods */
    1988             : /*
    1989             : check if the Unicode string must be encrypted or not, perform the requested task,
    1990             : append the string as unicode hex, encrypted if needed
    1991             :  */
    1992           0 : inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    1993             : {
    1994           0 :     rOutBuffer.append( "<" );
    1995           0 :     if( m_aContext.Encryption.Encrypt() )
    1996             :     {
    1997           0 :         const sal_Unicode* pStr = rInString.getStr();
    1998           0 :         sal_Int32 nLen = rInString.getLength();
    1999             :         //prepare a unicode string, encrypt it
    2000           0 :         if( checkEncryptionBufferSize( nLen*2 ) )
    2001             :         {
    2002           0 :             enableStringEncryption( nInObjectNumber );
    2003           0 :             sal_uInt8 *pCopy = m_pEncryptionBuffer;
    2004           0 :             sal_Int32 nChars = 2;
    2005           0 :             *pCopy++ = 0xFE;
    2006           0 :             *pCopy++ = 0xFF;
    2007             :             // we need to prepare a byte stream from the unicode string buffer
    2008           0 :             for( int i = 0; i < nLen; i++ )
    2009             :             {
    2010           0 :                 sal_Unicode aUnChar = pStr[i];
    2011           0 :                 *pCopy++ = (sal_uInt8)( aUnChar >> 8 );
    2012           0 :                 *pCopy++ = (sal_uInt8)( aUnChar & 255 );
    2013           0 :                 nChars += 2;
    2014             :             }
    2015             :             //encrypt in place
    2016           0 :             rtl_cipher_encodeARCFOUR( m_aCipher, m_pEncryptionBuffer, nChars, m_pEncryptionBuffer, nChars );
    2017             :             //now append, hexadecimal (appendHex), the encrypted result
    2018           0 :             for(int i = 0; i < nChars; i++)
    2019           0 :                 appendHex( m_pEncryptionBuffer[i], rOutBuffer );
    2020             :         }
    2021             :     }
    2022             :     else
    2023           0 :         appendUnicodeTextString( rInString, rOutBuffer );
    2024           0 :     rOutBuffer.append( ">" );
    2025           0 : }
    2026             : 
    2027           0 : inline void PDFWriterImpl::appendLiteralStringEncrypt( OStringBuffer& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2028             : {
    2029           0 :     rOutBuffer.append( "(" );
    2030           0 :     sal_Int32 nChars = rInString.getLength();
    2031             :     //check for encryption, if ok, encrypt the string, then convert with appndLiteralString
    2032           0 :     if( m_aContext.Encryption.Encrypt() && checkEncryptionBufferSize( nChars ) )
    2033             :     {
    2034             :         //encrypt the string in a buffer, then append it
    2035           0 :         enableStringEncryption( nInObjectNumber );
    2036           0 :         rtl_cipher_encodeARCFOUR( m_aCipher, rInString.getStr(), nChars, m_pEncryptionBuffer, nChars );
    2037           0 :         appendLiteralString( (const sal_Char*)m_pEncryptionBuffer, nChars, rOutBuffer );
    2038             :     }
    2039             :     else
    2040           0 :         appendLiteralString( rInString.getStr(), nChars , rOutBuffer );
    2041           0 :     rOutBuffer.append( ")" );
    2042           0 : }
    2043             : 
    2044           0 : inline void PDFWriterImpl::appendLiteralStringEncrypt( const OString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2045             : {
    2046           0 :     OStringBuffer aBufferString( rInString );
    2047           0 :     appendLiteralStringEncrypt( aBufferString, nInObjectNumber, rOutBuffer);
    2048           0 : }
    2049             : 
    2050           0 : void PDFWriterImpl::appendLiteralStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer, rtl_TextEncoding nEnc )
    2051             : {
    2052           0 :     OString aBufferString( OUStringToOString( rInString, nEnc ) );
    2053           0 :     sal_Int32 nLen = aBufferString.getLength();
    2054           0 :     OStringBuffer aBuf( nLen );
    2055           0 :     const sal_Char* pT = aBufferString.getStr();
    2056             : 
    2057           0 :     for( sal_Int32 i = 0; i < nLen; i++, pT++ )
    2058             :     {
    2059           0 :         if( (*pT & 0x80) == 0 )
    2060           0 :             aBuf.append( *pT );
    2061             :         else
    2062             :         {
    2063           0 :             aBuf.append( '<' );
    2064           0 :             appendHex( *pT, aBuf );
    2065           0 :             aBuf.append( '>' );
    2066             :         }
    2067             :     }
    2068           0 :     aBufferString = aBuf.makeStringAndClear();
    2069           0 :     appendLiteralStringEncrypt( aBufferString, nInObjectNumber, rOutBuffer);
    2070           0 : }
    2071             : 
    2072             : /* end i12626 methods */
    2073             : 
    2074           0 : void PDFWriterImpl::emitComment( const char* pComment )
    2075             : {
    2076           0 :     OStringBuffer aLine( 64 );
    2077           0 :     aLine.append( "% " );
    2078           0 :     aLine.append( (const sal_Char*)pComment );
    2079           0 :     aLine.append( "\n" );
    2080           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    2081           0 : }
    2082             : 
    2083           0 : bool PDFWriterImpl::compressStream( SvMemoryStream* pStream )
    2084             : {
    2085             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2086           0 :     pStream->Seek( STREAM_SEEK_TO_END );
    2087           0 :     sal_uLong nEndPos = pStream->Tell();
    2088           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    2089           0 :     ZCodec* pCodec = new ZCodec( 0x4000, 0x4000 );
    2090           0 :     SvMemoryStream aStream;
    2091           0 :     pCodec->BeginCompression();
    2092           0 :     pCodec->Write( aStream, (const sal_uInt8*)pStream->GetData(), nEndPos );
    2093           0 :     pCodec->EndCompression();
    2094           0 :     delete pCodec;
    2095           0 :     nEndPos = aStream.Tell();
    2096           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    2097           0 :     aStream.Seek( STREAM_SEEK_TO_BEGIN );
    2098           0 :     pStream->SetStreamSize( nEndPos );
    2099           0 :     pStream->Write( aStream.GetData(), nEndPos );
    2100           0 :     return true;
    2101             : #else
    2102             :     (void)pStream;
    2103             :     return false;
    2104             : #endif
    2105             : }
    2106             : 
    2107           0 : void PDFWriterImpl::beginCompression()
    2108             : {
    2109             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2110           0 :     m_pCodec = new ZCodec( 0x4000, 0x4000 );
    2111           0 :     m_pMemStream = new SvMemoryStream();
    2112           0 :     m_pCodec->BeginCompression();
    2113             : #endif
    2114           0 : }
    2115             : 
    2116           0 : void PDFWriterImpl::endCompression()
    2117             : {
    2118             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2119           0 :     if( m_pCodec )
    2120             :     {
    2121           0 :         m_pCodec->EndCompression();
    2122           0 :         delete m_pCodec;
    2123           0 :         m_pCodec = NULL;
    2124           0 :         sal_uInt64 nLen = m_pMemStream->Tell();
    2125           0 :         m_pMemStream->Seek( 0 );
    2126           0 :         writeBuffer( m_pMemStream->GetData(), nLen );
    2127           0 :         delete m_pMemStream;
    2128           0 :         m_pMemStream = NULL;
    2129             :     }
    2130             : #endif
    2131           0 : }
    2132             : 
    2133           0 : bool PDFWriterImpl::writeBuffer( const void* pBuffer, sal_uInt64 nBytes )
    2134             : {
    2135           0 :     if( ! m_bOpen ) // we are already down the drain
    2136           0 :         return false;
    2137             : 
    2138           0 :     if( ! nBytes ) // huh ?
    2139           0 :         return true;
    2140             : 
    2141           0 :     if( m_aOutputStreams.begin() != m_aOutputStreams.end() )
    2142             :     {
    2143           0 :         m_aOutputStreams.front().m_pStream->Seek( STREAM_SEEK_TO_END );
    2144           0 :         m_aOutputStreams.front().m_pStream->Write( pBuffer, sal::static_int_cast<sal_Size>(nBytes) );
    2145           0 :         return true;
    2146             :     }
    2147             : 
    2148             :     sal_uInt64 nWritten;
    2149           0 :     if( m_pCodec )
    2150             :     {
    2151           0 :         m_pCodec->Write( *m_pMemStream, static_cast<const sal_uInt8*>(pBuffer), (sal_uLong)nBytes );
    2152           0 :         nWritten = nBytes;
    2153             :     }
    2154             :     else
    2155             :     {
    2156           0 :         bool  buffOK = true;
    2157           0 :         if( m_bEncryptThisStream )
    2158             :         {
    2159             :             /* implement the encryption part of the PDF spec encryption algorithm 3.1 */
    2160           0 :             if( ( buffOK = checkEncryptionBufferSize( static_cast<sal_Int32>(nBytes) ) ) )
    2161             :                 rtl_cipher_encodeARCFOUR( m_aCipher,
    2162             :                                           (sal_uInt8*)pBuffer, static_cast<sal_Size>(nBytes),
    2163           0 :                                           m_pEncryptionBuffer, static_cast<sal_Size>(nBytes) );
    2164             :         }
    2165             : 
    2166           0 :         const void* pWriteBuffer = ( m_bEncryptThisStream && buffOK ) ? m_pEncryptionBuffer  : pBuffer;
    2167           0 :         if( m_aDocDigest )
    2168           0 :             rtl_digest_updateMD5( m_aDocDigest, pWriteBuffer, static_cast<sal_uInt32>(nBytes) );
    2169             : 
    2170           0 :         if( osl_writeFile( m_aFile,
    2171             :                            pWriteBuffer,
    2172           0 :                            nBytes, &nWritten ) != osl_File_E_None )
    2173           0 :             nWritten = 0;
    2174             : 
    2175           0 :         if( nWritten != nBytes )
    2176             :         {
    2177           0 :             osl_closeFile( m_aFile );
    2178           0 :             m_bOpen = false;
    2179             :         }
    2180             :     }
    2181             : 
    2182           0 :     return nWritten == nBytes;
    2183             : }
    2184             : 
    2185           0 : OutputDevice* PDFWriterImpl::getReferenceDevice()
    2186             : {
    2187           0 :     if( ! m_pReferenceDevice )
    2188             :     {
    2189           0 :         VirtualDevice*  pVDev = new VirtualDevice( 0 );
    2190             : 
    2191           0 :         m_pReferenceDevice = pVDev;
    2192             : 
    2193           0 :         if( m_aContext.DPIx == 0 || m_aContext.DPIy == 0 )
    2194           0 :             pVDev->SetReferenceDevice( VirtualDevice::REFDEV_MODE_PDF1 );
    2195             :         else
    2196           0 :             pVDev->SetReferenceDevice( m_aContext.DPIx, m_aContext.DPIy );
    2197             : 
    2198           0 :         pVDev->SetOutputSizePixel( Size( 640, 480 ) );
    2199           0 :         pVDev->SetMapMode( MAP_MM );
    2200             : 
    2201           0 :         m_pReferenceDevice->mpPDFWriter = this;
    2202           0 :         m_pReferenceDevice->ImplUpdateFontData( true );
    2203             :     }
    2204           0 :     return m_pReferenceDevice;
    2205             : }
    2206             : 
    2207           0 : class ImplPdfBuiltinFontData : public PhysicalFontFace
    2208             : {
    2209             : private:
    2210             :     const PDFWriterImpl::BuiltinFont& mrBuiltin;
    2211             : 
    2212             : public:
    2213             :     enum {PDF_FONT_MAGIC = 0xBDFF0A1C };
    2214             :                                         ImplPdfBuiltinFontData( const PDFWriterImpl::BuiltinFont& );
    2215           0 :     const PDFWriterImpl::BuiltinFont*   GetBuiltinFont() const  { return &mrBuiltin; }
    2216             : 
    2217           0 :     virtual PhysicalFontFace*           Clone() const SAL_OVERRIDE { return new ImplPdfBuiltinFontData(*this); }
    2218             :     virtual ImplFontEntry*              CreateFontInstance( FontSelectPattern& ) const SAL_OVERRIDE;
    2219           0 :     virtual sal_IntPtr                  GetFontId() const SAL_OVERRIDE { return reinterpret_cast<sal_IntPtr>(&mrBuiltin); }
    2220             : };
    2221             : 
    2222           0 : inline const ImplPdfBuiltinFontData* GetPdfFontData( const PhysicalFontFace* pFontData )
    2223             : {
    2224           0 :     const ImplPdfBuiltinFontData* pFD = NULL;
    2225           0 :     if( pFontData && pFontData->CheckMagic( ImplPdfBuiltinFontData::PDF_FONT_MAGIC ) )
    2226           0 :         pFD = static_cast<const ImplPdfBuiltinFontData*>( pFontData );
    2227           0 :     return pFD;
    2228             : }
    2229             : 
    2230           0 : static ImplDevFontAttributes GetDevFontAttributes( const PDFWriterImpl::BuiltinFont& rBuiltin )
    2231             : {
    2232           0 :     ImplDevFontAttributes aDFA;
    2233           0 :     aDFA.SetFamilyName( OUString::createFromAscii( rBuiltin.m_pName ) );
    2234           0 :     aDFA.SetStyleName( OUString::createFromAscii( rBuiltin.m_pStyleName ) );
    2235           0 :     aDFA.SetFamilyType( rBuiltin.m_eFamily );
    2236           0 :     aDFA.SetSymbolFlag( rBuiltin.m_eCharSet != RTL_TEXTENCODING_MS_1252 );
    2237           0 :     aDFA.SetPitch( rBuiltin.m_ePitch );
    2238           0 :     aDFA.SetWeight( rBuiltin.m_eWeight );
    2239           0 :     aDFA.SetItalic( rBuiltin.m_eItalic );
    2240           0 :     aDFA.SetWidthType( rBuiltin.m_eWidthType );
    2241             : 
    2242           0 :     aDFA.mbOrientation  = true;
    2243           0 :     aDFA.mbDevice       = true;
    2244           0 :     aDFA.mnQuality      = 50000;
    2245           0 :     aDFA.mbSubsettable  = false;
    2246           0 :     aDFA.mbEmbeddable   = false;
    2247           0 :     return aDFA;
    2248             : }
    2249             : 
    2250           0 : ImplPdfBuiltinFontData::ImplPdfBuiltinFontData( const PDFWriterImpl::BuiltinFont& rBuiltin )
    2251             : :   PhysicalFontFace( GetDevFontAttributes(rBuiltin), PDF_FONT_MAGIC ),
    2252           0 :     mrBuiltin( rBuiltin )
    2253           0 : {}
    2254             : 
    2255           0 : ImplFontEntry* ImplPdfBuiltinFontData::CreateFontInstance( FontSelectPattern& rFSD ) const
    2256             : {
    2257           0 :     ImplFontEntry* pEntry = new ImplFontEntry( rFSD );
    2258           0 :     return pEntry;
    2259             : }
    2260             : 
    2261             : // - PDFWriterImpl -
    2262             : 
    2263           0 : sal_Int32 PDFWriterImpl::newPage( sal_Int32 nPageWidth, sal_Int32 nPageHeight, PDFWriter::Orientation eOrientation )
    2264             : {
    2265           0 :     endPage();
    2266           0 :     m_nCurrentPage = m_aPages.size();
    2267           0 :     m_aPages.push_back( PDFPage(this, nPageWidth, nPageHeight, eOrientation ) );
    2268           0 :     m_aPages.back().m_nPageIndex = m_nCurrentPage;
    2269           0 :     m_aPages.back().beginStream();
    2270             : 
    2271             :     // setup global graphics state
    2272             :     // linewidth is "1 pixel" by default
    2273           0 :     OStringBuffer aBuf( 16 );
    2274           0 :     appendDouble( 72.0/double(getReferenceDevice()->ImplGetDPIX()), aBuf );
    2275           0 :     aBuf.append( " w\n" );
    2276           0 :     writeBuffer( aBuf.getStr(), aBuf.getLength() );
    2277             : 
    2278           0 :     return m_nCurrentPage;
    2279             : }
    2280             : 
    2281           0 : void PDFWriterImpl::endPage()
    2282             : {
    2283           0 :     if( m_aPages.begin() != m_aPages.end() )
    2284             :     {
    2285             :         // close eventual MC sequence
    2286           0 :         endStructureElementMCSeq();
    2287             : 
    2288             :         // sanity check
    2289           0 :         if( m_aOutputStreams.begin() != m_aOutputStreams.end() )
    2290             :         {
    2291             :             OSL_FAIL( "redirection across pages !!!" );
    2292           0 :             m_aOutputStreams.clear(); // leak !
    2293           0 :             m_aMapMode.SetOrigin( Point() );
    2294             :         }
    2295             : 
    2296           0 :         m_aGraphicsStack.clear();
    2297           0 :         m_aGraphicsStack.push_back( GraphicsState() );
    2298             : 
    2299             :         // this should pop the PDF graphics stack if necessary
    2300           0 :         updateGraphicsState();
    2301             : 
    2302           0 :         m_aPages.back().endStream();
    2303             : 
    2304             :         // reset the default font
    2305           0 :         Font aFont;
    2306           0 :         aFont.SetName( OUString( "Times" ) );
    2307           0 :         aFont.SetSize( Size( 0, 12 ) );
    2308             : 
    2309           0 :         m_aCurrentPDFState = m_aGraphicsStack.front();
    2310           0 :         m_aGraphicsStack.front().m_aFont =  aFont;
    2311             : 
    2312           0 :         for( std::list<BitmapEmit>::iterator it = m_aBitmaps.begin();
    2313           0 :              it != m_aBitmaps.end(); ++it )
    2314             :         {
    2315           0 :             if( ! it->m_aBitmap.IsEmpty() )
    2316             :             {
    2317           0 :                 writeBitmapObject( *it );
    2318           0 :                 it->m_aBitmap = BitmapEx();
    2319             :             }
    2320             :         }
    2321           0 :         for( std::list<JPGEmit>::iterator jpeg = m_aJPGs.begin(); jpeg != m_aJPGs.end(); ++jpeg )
    2322             :         {
    2323           0 :             if( jpeg->m_pStream )
    2324             :             {
    2325           0 :                 writeJPG( *jpeg );
    2326           0 :                 delete jpeg->m_pStream;
    2327           0 :                 jpeg->m_pStream = NULL;
    2328           0 :                 jpeg->m_aMask = Bitmap();
    2329             :             }
    2330             :         }
    2331           0 :         for( std::list<TransparencyEmit>::iterator t = m_aTransparentObjects.begin();
    2332           0 :              t != m_aTransparentObjects.end(); ++t )
    2333             :         {
    2334           0 :             if( t->m_pContentStream )
    2335             :             {
    2336           0 :                 writeTransparentObject( *t );
    2337           0 :                 delete t->m_pContentStream;
    2338           0 :                 t->m_pContentStream = NULL;
    2339             :             }
    2340           0 :         }
    2341             :     }
    2342           0 : }
    2343             : 
    2344           0 : sal_Int32 PDFWriterImpl::createObject()
    2345             : {
    2346           0 :     m_aObjects.push_back( ~0U );
    2347           0 :     return m_aObjects.size();
    2348             : }
    2349             : 
    2350           0 : bool PDFWriterImpl::updateObject( sal_Int32 n )
    2351             : {
    2352           0 :     if( ! m_bOpen )
    2353           0 :         return false;
    2354             : 
    2355           0 :     sal_uInt64 nOffset = ~0U;
    2356           0 :     oslFileError aError = osl_getFilePos( m_aFile, &nOffset );
    2357             :     DBG_ASSERT( aError == osl_File_E_None, "could not register object" );
    2358           0 :     if( aError != osl_File_E_None )
    2359             :     {
    2360           0 :         osl_closeFile( m_aFile );
    2361           0 :         m_bOpen = false;
    2362             :     }
    2363           0 :     m_aObjects[ n-1 ] = nOffset;
    2364           0 :     return aError == osl_File_E_None;
    2365             : }
    2366             : 
    2367             : #define CHECK_RETURN( x ) if( !(x) ) return 0
    2368             : 
    2369           0 : sal_Int32 PDFWriterImpl::emitStructParentTree( sal_Int32 nObject )
    2370             : {
    2371           0 :     if( nObject > 0 )
    2372             :     {
    2373           0 :         OStringBuffer aLine( 1024 );
    2374             : 
    2375           0 :         aLine.append( nObject );
    2376             :         aLine.append( " 0 obj\n"
    2377           0 :                       "<</Nums[\n" );
    2378           0 :         sal_Int32 nTreeItems = m_aStructParentTree.size();
    2379           0 :         for( sal_Int32 n = 0; n < nTreeItems; n++ )
    2380             :         {
    2381           0 :             aLine.append( n );
    2382           0 :             aLine.append( ' ' );
    2383           0 :             aLine.append( m_aStructParentTree[n] );
    2384           0 :             aLine.append( "\n" );
    2385             :         }
    2386           0 :         aLine.append( "]>>\nendobj\n\n" );
    2387           0 :         CHECK_RETURN( updateObject( nObject ) );
    2388           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2389             :     }
    2390           0 :     return nObject;
    2391             : }
    2392             : 
    2393           0 : const sal_Char* PDFWriterImpl::getAttributeTag( PDFWriter::StructAttribute eAttr )
    2394             : {
    2395           0 :     static std::map< PDFWriter::StructAttribute, const char* > aAttributeStrings;
    2396             :     // fill maps once
    2397           0 :     if( aAttributeStrings.empty() )
    2398             :     {
    2399           0 :         aAttributeStrings[ PDFWriter::Placement ]           = "Placement";
    2400           0 :         aAttributeStrings[ PDFWriter::WritingMode ]         = "WritingMode";
    2401           0 :         aAttributeStrings[ PDFWriter::SpaceBefore ]         = "SpaceBefore";
    2402           0 :         aAttributeStrings[ PDFWriter::SpaceAfter ]          = "SpaceAfter";
    2403           0 :         aAttributeStrings[ PDFWriter::StartIndent ]         = "StartIndent";
    2404           0 :         aAttributeStrings[ PDFWriter::EndIndent ]           = "EndIndent";
    2405           0 :         aAttributeStrings[ PDFWriter::TextIndent ]          = "TextIndent";
    2406           0 :         aAttributeStrings[ PDFWriter::TextAlign ]           = "TextAlign";
    2407           0 :         aAttributeStrings[ PDFWriter::Width ]               = "Width";
    2408           0 :         aAttributeStrings[ PDFWriter::Height ]              = "Height";
    2409           0 :         aAttributeStrings[ PDFWriter::BlockAlign ]          = "BlockAlign";
    2410           0 :         aAttributeStrings[ PDFWriter::InlineAlign ]         = "InlineAlign";
    2411           0 :         aAttributeStrings[ PDFWriter::LineHeight ]          = "LineHeight";
    2412           0 :         aAttributeStrings[ PDFWriter::BaselineShift ]       = "BaselineShift";
    2413           0 :         aAttributeStrings[ PDFWriter::TextDecorationType ]  = "TextDecorationType";
    2414           0 :         aAttributeStrings[ PDFWriter::ListNumbering ]       = "ListNumbering";
    2415           0 :         aAttributeStrings[ PDFWriter::RowSpan ]             = "RowSpan";
    2416           0 :         aAttributeStrings[ PDFWriter::ColSpan ]             = "ColSpan";
    2417           0 :         aAttributeStrings[ PDFWriter::LinkAnnotation ]      = "LinkAnnotation";
    2418             :     }
    2419             : 
    2420             :     std::map< PDFWriter::StructAttribute, const char* >::const_iterator it =
    2421           0 :         aAttributeStrings.find( eAttr );
    2422             : 
    2423             : #if OSL_DEBUG_LEVEL > 1
    2424             :     if( it == aAttributeStrings.end() )
    2425             :         fprintf( stderr, "invalid PDFWriter::StructAttribute %d\n", eAttr );
    2426             : #endif
    2427             : 
    2428           0 :     return it != aAttributeStrings.end() ? it->second : "";
    2429             : }
    2430             : 
    2431           0 : const sal_Char* PDFWriterImpl::getAttributeValueTag( PDFWriter::StructAttributeValue eVal )
    2432             : {
    2433           0 :     static std::map< PDFWriter::StructAttributeValue, const char* > aValueStrings;
    2434             : 
    2435           0 :     if( aValueStrings.empty() )
    2436             :     {
    2437           0 :         aValueStrings[ PDFWriter::NONE ]                    = "None";
    2438           0 :         aValueStrings[ PDFWriter::Block ]                   = "Block";
    2439           0 :         aValueStrings[ PDFWriter::Inline ]                  = "Inline";
    2440           0 :         aValueStrings[ PDFWriter::Before ]                  = "Before";
    2441           0 :         aValueStrings[ PDFWriter::After ]                   = "After";
    2442           0 :         aValueStrings[ PDFWriter::Start ]                   = "Start";
    2443           0 :         aValueStrings[ PDFWriter::End ]                     = "End";
    2444           0 :         aValueStrings[ PDFWriter::LrTb ]                    = "LrTb";
    2445           0 :         aValueStrings[ PDFWriter::RlTb ]                    = "RlTb";
    2446           0 :         aValueStrings[ PDFWriter::TbRl ]                    = "TbRl";
    2447           0 :         aValueStrings[ PDFWriter::Center ]                  = "Center";
    2448           0 :         aValueStrings[ PDFWriter::Justify ]                 = "Justify";
    2449           0 :         aValueStrings[ PDFWriter::Auto ]                    = "Auto";
    2450           0 :         aValueStrings[ PDFWriter::Middle ]                  = "Middle";
    2451           0 :         aValueStrings[ PDFWriter::Normal ]                  = "Normal";
    2452           0 :         aValueStrings[ PDFWriter::Underline ]               = "Underline";
    2453           0 :         aValueStrings[ PDFWriter::Overline ]                = "Overline";
    2454           0 :         aValueStrings[ PDFWriter::LineThrough ]             = "LineThrough";
    2455           0 :         aValueStrings[ PDFWriter::Disc ]                    = "Disc";
    2456           0 :         aValueStrings[ PDFWriter::Circle ]                  = "Circle";
    2457           0 :         aValueStrings[ PDFWriter::Square ]                  = "Square";
    2458           0 :         aValueStrings[ PDFWriter::Decimal ]                 = "Decimal";
    2459           0 :         aValueStrings[ PDFWriter::UpperRoman ]              = "UpperRoman";
    2460           0 :         aValueStrings[ PDFWriter::LowerRoman ]              = "LowerRoman";
    2461           0 :         aValueStrings[ PDFWriter::UpperAlpha ]              = "UpperAlpha";
    2462           0 :         aValueStrings[ PDFWriter::LowerAlpha ]              = "LowerAlpha";
    2463             :     }
    2464             : 
    2465             :     std::map< PDFWriter::StructAttributeValue, const char* >::const_iterator it =
    2466           0 :         aValueStrings.find( eVal );
    2467             : 
    2468             : #if OSL_DEBUG_LEVEL > 1
    2469             :     if( it == aValueStrings.end() )
    2470             :         fprintf( stderr, "invalid PDFWriter::StructAttributeValue %d\n", eVal );
    2471             : #endif
    2472             : 
    2473           0 :     return it != aValueStrings.end() ? it->second : "";
    2474             : }
    2475             : 
    2476           0 : static void appendStructureAttributeLine( PDFWriter::StructAttribute i_eAttr, const PDFWriterImpl::PDFStructureAttribute& i_rVal, OStringBuffer& o_rLine, bool i_bIsFixedInt )
    2477             : {
    2478           0 :     o_rLine.append( "/" );
    2479           0 :     o_rLine.append( PDFWriterImpl::getAttributeTag( i_eAttr ) );
    2480             : 
    2481           0 :     if( i_rVal.eValue != PDFWriter::Invalid )
    2482             :     {
    2483           0 :         o_rLine.append( "/" );
    2484           0 :         o_rLine.append( PDFWriterImpl::getAttributeValueTag( i_rVal.eValue ) );
    2485             :     }
    2486             :     else
    2487             :     {
    2488             :         // numerical value
    2489           0 :         o_rLine.append( " " );
    2490           0 :         if( i_bIsFixedInt )
    2491           0 :             appendFixedInt( i_rVal.nValue, o_rLine );
    2492             :         else
    2493           0 :             o_rLine.append( i_rVal.nValue );
    2494             :     }
    2495           0 :     o_rLine.append( "\n" );
    2496           0 : }
    2497             : 
    2498           0 : OString PDFWriterImpl::emitStructureAttributes( PDFStructureElement& i_rEle )
    2499             : {
    2500             :     // create layout, list and table attribute sets
    2501           0 :     OStringBuffer aLayout(256), aList(64), aTable(64);
    2502           0 :     for( PDFStructAttributes::const_iterator it = i_rEle.m_aAttributes.begin();
    2503           0 :          it != i_rEle.m_aAttributes.end(); ++it )
    2504             :     {
    2505           0 :         if( it->first == PDFWriter::ListNumbering )
    2506           0 :             appendStructureAttributeLine( it->first, it->second, aList, true );
    2507           0 :         else if( it->first == PDFWriter::RowSpan ||
    2508           0 :                  it->first == PDFWriter::ColSpan )
    2509           0 :             appendStructureAttributeLine( it->first, it->second, aTable, false );
    2510           0 :         else if( it->first == PDFWriter::LinkAnnotation )
    2511             :         {
    2512           0 :             sal_Int32 nLink = it->second.nValue;
    2513             :             std::map< sal_Int32, sal_Int32 >::const_iterator link_it =
    2514           0 :                 m_aLinkPropertyMap.find( nLink );
    2515           0 :             if( link_it != m_aLinkPropertyMap.end() )
    2516           0 :                 nLink = link_it->second;
    2517           0 :             if( nLink >= 0 && nLink < (sal_Int32)m_aLinks.size() )
    2518             :             {
    2519             :                 // update struct parent of link
    2520           0 :                 OStringBuffer aStructParentEntry( 32 );
    2521           0 :                 aStructParentEntry.append( i_rEle.m_nObject );
    2522           0 :                 aStructParentEntry.append( " 0 R" );
    2523           0 :                 m_aStructParentTree.push_back( aStructParentEntry.makeStringAndClear() );
    2524           0 :                 m_aLinks[ nLink ].m_nStructParent = m_aStructParentTree.size()-1;
    2525             : 
    2526           0 :                 sal_Int32 nRefObject = createObject();
    2527           0 :                 OStringBuffer aRef( 256 );
    2528           0 :                 aRef.append( nRefObject );
    2529             :                 aRef.append( " 0 obj\n"
    2530           0 :                              "<</Type/OBJR/Obj " );
    2531           0 :                 aRef.append( m_aLinks[ nLink ].m_nObject );
    2532             :                 aRef.append( " 0 R>>\n"
    2533             :                              "endobj\n\n"
    2534           0 :                              );
    2535           0 :                 updateObject( nRefObject );
    2536           0 :                 writeBuffer( aRef.getStr(), aRef.getLength() );
    2537             : 
    2538           0 :                 i_rEle.m_aKids.push_back( PDFStructureElementKid( nRefObject ) );
    2539             :             }
    2540             :             else
    2541             :             {
    2542             :                 OSL_FAIL( "unresolved link id for Link structure" );
    2543             : #if OSL_DEBUG_LEVEL > 1
    2544             :                 fprintf( stderr, "unresolved link id %" SAL_PRIdINT32 " for Link structure\n", nLink );
    2545             :                 {
    2546             :                     OStringBuffer aLine( "unresolved link id " );
    2547             :                     aLine.append( nLink );
    2548             :                     aLine.append( " for Link structure" );
    2549             :                     emitComment( aLine.getStr() );
    2550             :                 }
    2551             : #endif
    2552             :             }
    2553             :         }
    2554             :         else
    2555           0 :             appendStructureAttributeLine( it->first, it->second, aLayout, true );
    2556             :     }
    2557           0 :     if( ! i_rEle.m_aBBox.IsEmpty() )
    2558             :     {
    2559           0 :         aLayout.append( "/BBox[" );
    2560           0 :         appendFixedInt( i_rEle.m_aBBox.Left(), aLayout );
    2561           0 :         aLayout.append( " " );
    2562           0 :         appendFixedInt( i_rEle.m_aBBox.Top(), aLayout );
    2563           0 :         aLayout.append( " " );
    2564           0 :         appendFixedInt( i_rEle.m_aBBox.Right(), aLayout );
    2565           0 :         aLayout.append( " " );
    2566           0 :         appendFixedInt( i_rEle.m_aBBox.Bottom(), aLayout );
    2567           0 :         aLayout.append( "]\n" );
    2568             :     }
    2569             : 
    2570           0 :     std::vector< sal_Int32 > aAttribObjects;
    2571           0 :     if( !aLayout.isEmpty() )
    2572             :     {
    2573           0 :         aAttribObjects.push_back( createObject() );
    2574           0 :         updateObject( aAttribObjects.back() );
    2575           0 :         OStringBuffer aObj( 64 );
    2576           0 :         aObj.append( aAttribObjects.back() );
    2577             :         aObj.append( " 0 obj\n"
    2578           0 :                      "<</O/Layout\n" );
    2579           0 :         aLayout.append( ">>\nendobj\n\n" );
    2580           0 :         writeBuffer( aObj.getStr(), aObj.getLength() );
    2581           0 :         writeBuffer( aLayout.getStr(), aLayout.getLength() );
    2582             :     }
    2583           0 :     if( !aList.isEmpty() )
    2584             :     {
    2585           0 :         aAttribObjects.push_back( createObject() );
    2586           0 :         updateObject( aAttribObjects.back() );
    2587           0 :         OStringBuffer aObj( 64 );
    2588           0 :         aObj.append( aAttribObjects.back() );
    2589             :         aObj.append( " 0 obj\n"
    2590           0 :                      "<</O/List\n" );
    2591           0 :         aList.append( ">>\nendobj\n\n" );
    2592           0 :         writeBuffer( aObj.getStr(), aObj.getLength() );
    2593           0 :         writeBuffer( aList.getStr(), aList.getLength() );
    2594             :     }
    2595           0 :     if( !aTable.isEmpty() )
    2596             :     {
    2597           0 :         aAttribObjects.push_back( createObject() );
    2598           0 :         updateObject( aAttribObjects.back() );
    2599           0 :         OStringBuffer aObj( 64 );
    2600           0 :         aObj.append( aAttribObjects.back() );
    2601             :         aObj.append( " 0 obj\n"
    2602           0 :                      "<</O/Table\n" );
    2603           0 :         aTable.append( ">>\nendobj\n\n" );
    2604           0 :         writeBuffer( aObj.getStr(), aObj.getLength() );
    2605           0 :         writeBuffer( aTable.getStr(), aTable.getLength() );
    2606             :     }
    2607             : 
    2608           0 :     OStringBuffer aRet( 64 );
    2609           0 :     if( aAttribObjects.size() > 1 )
    2610           0 :         aRet.append( " [" );
    2611           0 :     for( std::vector< sal_Int32 >::const_iterator at_it = aAttribObjects.begin();
    2612           0 :          at_it != aAttribObjects.end(); ++at_it )
    2613             :     {
    2614           0 :         aRet.append( " " );
    2615           0 :         aRet.append( *at_it );
    2616           0 :         aRet.append( " 0 R" );
    2617             :     }
    2618           0 :     if( aAttribObjects.size() > 1 )
    2619           0 :         aRet.append( " ]" );
    2620           0 :     return aRet.makeStringAndClear();
    2621             : }
    2622             : 
    2623           0 : sal_Int32 PDFWriterImpl::emitStructure( PDFStructureElement& rEle )
    2624             : {
    2625           0 :     if(
    2626             :        // do not emit NonStruct and its children
    2627           0 :        rEle.m_eType == PDFWriter::NonStructElement &&
    2628           0 :        rEle.m_nOwnElement != rEle.m_nParentElement // but of course emit the struct tree root
    2629             :        )
    2630           0 :         return 0;
    2631             : 
    2632           0 :     for( std::list< sal_Int32 >::const_iterator it = rEle.m_aChildren.begin(); it != rEle.m_aChildren.end(); ++it )
    2633             :     {
    2634           0 :         if( *it > 0 && *it < sal_Int32(m_aStructure.size()) )
    2635             :         {
    2636           0 :             PDFStructureElement& rChild = m_aStructure[ *it ];
    2637           0 :             if( rChild.m_eType != PDFWriter::NonStructElement )
    2638             :             {
    2639           0 :                 if( rChild.m_nParentElement == rEle.m_nOwnElement )
    2640           0 :                     emitStructure( rChild );
    2641             :                 else
    2642             :                 {
    2643             :                     OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure element" );
    2644             : #if OSL_DEBUG_LEVEL > 1
    2645             :                     fprintf( stderr, "PDFWriterImpl::emitStructure: invalid child structure elemnt with id %" SAL_PRIdINT32 "\n", *it );
    2646             : #endif
    2647             :                 }
    2648             :             }
    2649             :         }
    2650             :         else
    2651             :         {
    2652             :             OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure id" );
    2653             : #if OSL_DEBUG_LEVEL > 1
    2654             :             fprintf( stderr, "PDFWriterImpl::emitStructure: invalid child structure id %" SAL_PRIdINT32 "\n", *it );
    2655             : #endif
    2656             :         }
    2657             :     }
    2658             : 
    2659           0 :     OStringBuffer aLine( 512 );
    2660           0 :     aLine.append( rEle.m_nObject );
    2661             :     aLine.append( " 0 obj\n"
    2662           0 :                   "<</Type" );
    2663           0 :     sal_Int32 nParentTree = -1;
    2664           0 :     if( rEle.m_nOwnElement == rEle.m_nParentElement )
    2665             :     {
    2666           0 :         nParentTree = createObject();
    2667           0 :         CHECK_RETURN( nParentTree );
    2668           0 :         aLine.append( "/StructTreeRoot\n" );
    2669           0 :         aLine.append( "/ParentTree " );
    2670           0 :         aLine.append( nParentTree );
    2671           0 :         aLine.append( " 0 R\n" );
    2672           0 :         if( ! m_aRoleMap.empty() )
    2673             :         {
    2674           0 :             aLine.append( "/RoleMap<<" );
    2675           0 :             for( boost::unordered_map<OString,OString,OStringHash>::const_iterator
    2676           0 :                  it = m_aRoleMap.begin(); it != m_aRoleMap.end(); ++it )
    2677             :             {
    2678           0 :                 aLine.append( '/' );
    2679           0 :                 aLine.append(it->first);
    2680           0 :                 aLine.append( '/' );
    2681           0 :                 aLine.append( it->second );
    2682           0 :                 aLine.append( '\n' );
    2683             :             }
    2684           0 :             aLine.append( ">>\n" );
    2685             :         }
    2686             :     }
    2687             :     else
    2688             :     {
    2689             :         aLine.append( "/StructElem\n"
    2690           0 :                       "/S/" );
    2691           0 :         if( !rEle.m_aAlias.isEmpty() )
    2692           0 :             aLine.append( rEle.m_aAlias );
    2693             :         else
    2694           0 :             aLine.append( getStructureTag( rEle.m_eType ) );
    2695             :         aLine.append( "\n"
    2696           0 :                       "/P " );
    2697           0 :         aLine.append( m_aStructure[ rEle.m_nParentElement ].m_nObject );
    2698             :         aLine.append( " 0 R\n"
    2699           0 :                       "/Pg " );
    2700           0 :         aLine.append( rEle.m_nFirstPageObject );
    2701           0 :         aLine.append( " 0 R\n" );
    2702           0 :         if( !rEle.m_aActualText.isEmpty() )
    2703             :         {
    2704           0 :             aLine.append( "/ActualText" );
    2705           0 :             appendUnicodeTextStringEncrypt( rEle.m_aActualText, rEle.m_nObject, aLine );
    2706           0 :             aLine.append( "\n" );
    2707             :         }
    2708           0 :         if( !rEle.m_aAltText.isEmpty() )
    2709             :         {
    2710           0 :             aLine.append( "/Alt" );
    2711           0 :             appendUnicodeTextStringEncrypt( rEle.m_aAltText, rEle.m_nObject, aLine );
    2712           0 :             aLine.append( "\n" );
    2713             :         }
    2714             :     }
    2715           0 :     if( ! rEle.m_aBBox.IsEmpty() || rEle.m_aAttributes.size() )
    2716             :     {
    2717           0 :         OString aAttribs =  emitStructureAttributes( rEle );
    2718           0 :         if( !aAttribs.isEmpty() )
    2719             :         {
    2720           0 :             aLine.append( "/A" );
    2721           0 :             aLine.append( aAttribs );
    2722           0 :             aLine.append( "\n" );
    2723           0 :         }
    2724             :     }
    2725           0 :     if( !rEle.m_aLocale.Language.isEmpty() )
    2726             :     {
    2727             :         /* PDF allows only RFC 3066, which is only partly BCP 47 and does not
    2728             :          * include script tags and others.
    2729             :          * http://pdf.editme.com/pdfua-naturalLanguageSpecification
    2730             :          * http://partners.adobe.com/public/developer/en/pdf/PDFReference16.pdf#page=886
    2731             :          * https://www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf#M13.9.19332.1Heading.97.Natural.Language.Specification
    2732             :          * */
    2733           0 :         LanguageTag aLanguageTag( rEle.m_aLocale);
    2734           0 :         OUString aLanguage, aScript, aCountry;
    2735           0 :         aLanguageTag.getIsoLanguageScriptCountry( aLanguage, aScript, aCountry);
    2736           0 :         if (!aLanguage.isEmpty())
    2737             :         {
    2738           0 :             OUStringBuffer aLocBuf( 16 );
    2739           0 :             aLocBuf.append( aLanguage );
    2740           0 :             if( !aCountry.isEmpty() )
    2741             :             {
    2742           0 :                 aLocBuf.append( '-' );
    2743           0 :                 aLocBuf.append( aCountry );
    2744             :             }
    2745           0 :             aLine.append( "/Lang" );
    2746           0 :             appendLiteralStringEncrypt( aLocBuf.makeStringAndClear(), rEle.m_nObject, aLine );
    2747           0 :             aLine.append( "\n" );
    2748           0 :         }
    2749             :     }
    2750           0 :     if( ! rEle.m_aKids.empty() )
    2751             :     {
    2752           0 :         unsigned int i = 0;
    2753           0 :         aLine.append( "/K[" );
    2754           0 :         for( std::list< PDFStructureElementKid >::const_iterator it =
    2755           0 :                  rEle.m_aKids.begin(); it != rEle.m_aKids.end(); ++it, i++ )
    2756             :         {
    2757           0 :             if( it->nMCID == -1 )
    2758             :             {
    2759           0 :                 aLine.append( it->nObject );
    2760           0 :                 aLine.append( " 0 R" );
    2761           0 :                 aLine.append( ( (i & 15) == 15 ) ? "\n" : " " );
    2762             :             }
    2763             :             else
    2764             :             {
    2765           0 :                 if( it->nObject == rEle.m_nFirstPageObject )
    2766             :                 {
    2767           0 :                     aLine.append( it->nMCID );
    2768           0 :                     aLine.append( " " );
    2769             :                 }
    2770             :                 else
    2771             :                 {
    2772           0 :                     aLine.append( "<</Type/MCR/Pg " );
    2773           0 :                     aLine.append( it->nObject );
    2774           0 :                     aLine.append( " 0 R /MCID " );
    2775           0 :                     aLine.append( it->nMCID );
    2776           0 :                     aLine.append( ">>\n" );
    2777             :                 }
    2778             :             }
    2779             :         }
    2780           0 :         aLine.append( "]\n" );
    2781             :     }
    2782           0 :     aLine.append( ">>\nendobj\n\n" );
    2783             : 
    2784           0 :     CHECK_RETURN( updateObject( rEle.m_nObject ) );
    2785           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2786             : 
    2787           0 :     CHECK_RETURN( emitStructParentTree( nParentTree ) );
    2788             : 
    2789           0 :     return rEle.m_nObject;
    2790             : }
    2791             : 
    2792           0 : bool PDFWriterImpl::emitGradients()
    2793             : {
    2794           0 :     for( std::list<GradientEmit>::iterator it = m_aGradients.begin();
    2795           0 :          it != m_aGradients.end(); ++it )
    2796             :     {
    2797           0 :         if ( !writeGradientFunction( *it ) ) return false;
    2798             :     }
    2799           0 :     return true;
    2800             : }
    2801             : 
    2802           0 : bool PDFWriterImpl::emitTilings()
    2803             : {
    2804           0 :     OStringBuffer aTilingObj( 1024 );
    2805             : 
    2806           0 :     for( std::vector<TilingEmit>::iterator it = m_aTilings.begin(); it != m_aTilings.end(); ++it )
    2807             :     {
    2808             :         DBG_ASSERT( it->m_pTilingStream, "tiling without stream" );
    2809           0 :         if( ! it->m_pTilingStream )
    2810           0 :             continue;
    2811             : 
    2812           0 :         aTilingObj.setLength( 0 );
    2813             : 
    2814             :         #if OSL_DEBUG_LEVEL > 1
    2815             :         emitComment( "PDFWriterImpl::emitTilings" );
    2816             :         #endif
    2817             : 
    2818           0 :         sal_Int32 nX = (sal_Int32)it->m_aRectangle.Left();
    2819           0 :         sal_Int32 nY = (sal_Int32)it->m_aRectangle.Top();
    2820           0 :         sal_Int32 nW = (sal_Int32)it->m_aRectangle.GetWidth();
    2821           0 :         sal_Int32 nH = (sal_Int32)it->m_aRectangle.GetHeight();
    2822           0 :         if( it->m_aCellSize.Width() == 0 )
    2823           0 :             it->m_aCellSize.Width() = nW;
    2824           0 :         if( it->m_aCellSize.Height() == 0 )
    2825           0 :             it->m_aCellSize.Height() = nH;
    2826             : 
    2827           0 :         bool bDeflate = compressStream( it->m_pTilingStream );
    2828           0 :         it->m_pTilingStream->Seek( STREAM_SEEK_TO_END );
    2829           0 :         sal_Size nTilingStreamSize = it->m_pTilingStream->Tell();
    2830           0 :         it->m_pTilingStream->Seek( STREAM_SEEK_TO_BEGIN );
    2831             : 
    2832             :         // write pattern object
    2833           0 :         aTilingObj.append( it->m_nObject );
    2834           0 :         aTilingObj.append( " 0 obj\n" );
    2835             :         aTilingObj.append( "<</Type/Pattern/PatternType 1\n"
    2836             :                            "/PaintType 1\n"
    2837             :                            "/TilingType 2\n"
    2838           0 :                            "/BBox[" );
    2839           0 :         appendFixedInt( nX, aTilingObj );
    2840           0 :         aTilingObj.append( ' ' );
    2841           0 :         appendFixedInt( nY, aTilingObj );
    2842           0 :         aTilingObj.append( ' ' );
    2843           0 :         appendFixedInt( nX+nW, aTilingObj );
    2844           0 :         aTilingObj.append( ' ' );
    2845           0 :         appendFixedInt( nY+nH, aTilingObj );
    2846             :         aTilingObj.append( "]\n"
    2847           0 :                            "/XStep " );
    2848           0 :         appendFixedInt( it->m_aCellSize.Width(), aTilingObj );
    2849             :         aTilingObj.append( "\n"
    2850           0 :                            "/YStep " );
    2851           0 :         appendFixedInt( it->m_aCellSize.Height(), aTilingObj );
    2852           0 :         aTilingObj.append( "\n" );
    2853           0 :         if( it->m_aTransform.matrix[0] != 1.0 ||
    2854           0 :             it->m_aTransform.matrix[1] != 0.0 ||
    2855           0 :             it->m_aTransform.matrix[3] != 0.0 ||
    2856           0 :             it->m_aTransform.matrix[4] != 1.0 ||
    2857           0 :             it->m_aTransform.matrix[2] != 0.0 ||
    2858           0 :             it->m_aTransform.matrix[5] != 0.0 )
    2859             :         {
    2860           0 :             aTilingObj.append( "/Matrix [" );
    2861             :             // TODO: scaling, mirroring on y, etc
    2862           0 :             appendDouble( it->m_aTransform.matrix[0], aTilingObj );
    2863           0 :             aTilingObj.append( ' ' );
    2864           0 :             appendDouble( it->m_aTransform.matrix[1], aTilingObj );
    2865           0 :             aTilingObj.append( ' ' );
    2866           0 :             appendDouble( it->m_aTransform.matrix[3], aTilingObj );
    2867           0 :             aTilingObj.append( ' ' );
    2868           0 :             appendDouble( it->m_aTransform.matrix[4], aTilingObj );
    2869           0 :             aTilingObj.append( ' ' );
    2870           0 :             appendDouble( it->m_aTransform.matrix[2], aTilingObj );
    2871           0 :             aTilingObj.append( ' ' );
    2872           0 :             appendDouble( it->m_aTransform.matrix[5], aTilingObj );
    2873           0 :             aTilingObj.append( "]\n" );
    2874             :         }
    2875           0 :         aTilingObj.append( "/Resources" );
    2876           0 :         it->m_aResources.append( aTilingObj, getFontDictObject() );
    2877           0 :         if( bDeflate )
    2878           0 :             aTilingObj.append( "/Filter/FlateDecode" );
    2879           0 :         aTilingObj.append( "/Length " );
    2880           0 :         aTilingObj.append( (sal_Int32)nTilingStreamSize );
    2881           0 :         aTilingObj.append( ">>\nstream\n" );
    2882           0 :         if ( !updateObject( it->m_nObject ) ) return false;
    2883           0 :         if ( !writeBuffer( aTilingObj.getStr(), aTilingObj.getLength() ) ) return false;
    2884           0 :         checkAndEnableStreamEncryption( it->m_nObject );
    2885           0 :         bool written = writeBuffer( it->m_pTilingStream->GetData(), nTilingStreamSize );
    2886           0 :         delete it->m_pTilingStream;
    2887           0 :         it->m_pTilingStream = NULL;
    2888           0 :         if( !written )
    2889           0 :             return false;
    2890           0 :         disableStreamEncryption();
    2891           0 :         aTilingObj.setLength( 0 );
    2892           0 :         aTilingObj.append( "\nendstream\nendobj\n\n" );
    2893           0 :         if ( !writeBuffer( aTilingObj.getStr(), aTilingObj.getLength() ) ) return false;
    2894             :     }
    2895           0 :     return true;
    2896             : }
    2897             : 
    2898           0 : sal_Int32 PDFWriterImpl::emitBuiltinFont( const PhysicalFontFace* pFont, sal_Int32 nFontObject )
    2899             : {
    2900           0 :     const ImplPdfBuiltinFontData* pFD = GetPdfFontData( pFont );
    2901           0 :     if( !pFD )
    2902           0 :         return 0;
    2903           0 :     const BuiltinFont* pBuiltinFont = pFD->GetBuiltinFont();
    2904             : 
    2905           0 :     OStringBuffer aLine( 1024 );
    2906             : 
    2907           0 :     if( nFontObject <= 0 )
    2908           0 :         nFontObject = createObject();
    2909           0 :     CHECK_RETURN( updateObject( nFontObject ) );
    2910           0 :     aLine.append( nFontObject );
    2911             :     aLine.append( " 0 obj\n"
    2912           0 :                   "<</Type/Font/Subtype/Type1/BaseFont/" );
    2913           0 :     appendName( pBuiltinFont->m_pPSName, aLine );
    2914           0 :     aLine.append( "\n" );
    2915           0 :     if( pBuiltinFont->m_eCharSet == RTL_TEXTENCODING_MS_1252 )
    2916           0 :          aLine.append( "/Encoding/WinAnsiEncoding\n" );
    2917           0 :     aLine.append( ">>\nendobj\n\n" );
    2918           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2919           0 :     return nFontObject;
    2920             : }
    2921             : 
    2922           0 : std::map< sal_Int32, sal_Int32 > PDFWriterImpl::emitSystemFont( const PhysicalFontFace* pFont, EmbedFont& rEmbed )
    2923             : {
    2924           0 :     std::map< sal_Int32, sal_Int32 > aRet;
    2925             : 
    2926           0 :     sal_Int32 nFontDescriptor = 0;
    2927           0 :     OString aSubType( "/Type1" );
    2928           0 :     FontSubsetInfo aInfo;
    2929             :     // fill in dummy values
    2930           0 :     aInfo.m_nAscent = 1000;
    2931           0 :     aInfo.m_nDescent = 200;
    2932           0 :     aInfo.m_nCapHeight = 1000;
    2933           0 :     aInfo.m_aFontBBox = Rectangle( Point( -200, -200 ), Size( 1700, 1700 ) );
    2934           0 :     aInfo.m_aPSName = pFont->GetFamilyName();
    2935             :     sal_Int32 pWidths[256];
    2936           0 :     memset( pWidths, 0, sizeof(pWidths) );
    2937           0 :     if( pFont->IsEmbeddable() )
    2938             :     {
    2939           0 :         const unsigned char* pFontData = NULL;
    2940           0 :         long nFontLen = 0;
    2941             :         sal_Ucs nEncodedCodes[256];
    2942             :         sal_Int32 pEncWidths[256];
    2943           0 :         if( (pFontData = (const unsigned char*)m_pReferenceDevice->mpGraphics->GetEmbedFontData( pFont, nEncodedCodes, pEncWidths, aInfo, &nFontLen )) != NULL )
    2944             :         {
    2945           0 :             m_pReferenceDevice->mpGraphics->FreeEmbedFontData( pFontData, nFontLen );
    2946           0 :             for( int i = 0; i < 256; i++ )
    2947             :             {
    2948           0 :                 if( nEncodedCodes[i] >= 32 && nEncodedCodes[i] < 256 )
    2949             :                 {
    2950           0 :                     pWidths[i] = pEncWidths[ i ];
    2951             :                 }
    2952             :             }
    2953             :         }
    2954             :     }
    2955           0 :     else if( pFont->mbSubsettable )
    2956             :     {
    2957           0 :         aSubType = OString( "/TrueType" );
    2958           0 :         Int32Vector aGlyphWidths;
    2959           0 :         Ucs2UIntMap aUnicodeMap;
    2960           0 :         m_pReferenceDevice->mpGraphics->GetGlyphWidths( pFont, false, aGlyphWidths, aUnicodeMap );
    2961             : 
    2962           0 :         OUString aTmpName;
    2963           0 :         osl_createTempFile( NULL, NULL, &aTmpName.pData );
    2964             :         sal_GlyphId aGlyphIds[ 256 ];
    2965             :         sal_uInt8 pEncoding[ 256 ];
    2966             :         sal_Int32 pDuWidths[ 256 ];
    2967             : 
    2968           0 :         memset( aGlyphIds, 0, sizeof( aGlyphIds ) );
    2969           0 :         memset( pEncoding, 0, sizeof( pEncoding ) );
    2970           0 :         memset( pDuWidths, 0, sizeof( pDuWidths ) );
    2971             : 
    2972           0 :         for( sal_Ucs c = 32; c < 256; c++ )
    2973             :         {
    2974           0 :             pEncoding[c] = c;
    2975           0 :             aGlyphIds[c] = 0;
    2976           0 :             if( aUnicodeMap.find( c ) != aUnicodeMap.end() )
    2977           0 :                 pWidths[ c ] = aGlyphWidths[ aUnicodeMap[ c ] ];
    2978             :         }
    2979             : 
    2980           0 :         m_pReferenceDevice->mpGraphics->CreateFontSubset( aTmpName, pFont, aGlyphIds, pEncoding, pDuWidths, 256, aInfo );
    2981           0 :         osl_removeFile( aTmpName.pData );
    2982             :     }
    2983             :     else
    2984             :     {
    2985             :         OSL_FAIL( "system font neither embeddable nor subsettable" );
    2986             :     }
    2987             : 
    2988             :     // write font descriptor
    2989           0 :     nFontDescriptor = emitFontDescriptor( pFont, aInfo, 0, 0 );
    2990           0 :     if( nFontDescriptor )
    2991             :     {
    2992             :         // write font object
    2993           0 :         sal_Int32 nObject = createObject();
    2994           0 :         if( updateObject( nObject ) )
    2995             :         {
    2996           0 :             OStringBuffer aLine( 1024 );
    2997           0 :             aLine.append( nObject );
    2998             :             aLine.append( " 0 obj\n"
    2999           0 :                           "<</Type/Font/Subtype" );
    3000           0 :             aLine.append( aSubType );
    3001           0 :             aLine.append( "/BaseFont/" );
    3002           0 :             appendName( aInfo.m_aPSName, aLine );
    3003           0 :             aLine.append( "\n" );
    3004           0 :             if( !pFont->IsSymbolFont() )
    3005           0 :                 aLine.append( "/Encoding/WinAnsiEncoding\n" );
    3006             :             aLine.append( "/FirstChar 32 /LastChar 255\n"
    3007           0 :                           "/Widths[" );
    3008           0 :             for( int i = 32; i < 256; i++ )
    3009             :             {
    3010           0 :                 aLine.append( pWidths[i] );
    3011           0 :                 aLine.append( ((i&15) == 15) ? "\n" : " " );
    3012             :             }
    3013             :             aLine.append( "]\n"
    3014           0 :                           "/FontDescriptor " );
    3015           0 :             aLine.append( nFontDescriptor );
    3016             :             aLine.append( " 0 R>>\n"
    3017           0 :                           "endobj\n\n" );
    3018           0 :             writeBuffer( aLine.getStr(), aLine.getLength() );
    3019             : 
    3020           0 :             aRet[ rEmbed.m_nNormalFontID ] = nObject;
    3021             :         }
    3022             :     }
    3023             : 
    3024           0 :     return aRet;
    3025             : }
    3026             : 
    3027             : typedef int ThreeInts[3];
    3028           0 : static bool getPfbSegmentLengths( const unsigned char* pFontBytes, int nByteLen,
    3029             :     ThreeInts& rSegmentLengths )
    3030             : {
    3031           0 :     if( !pFontBytes || (nByteLen < 0) )
    3032           0 :         return false;
    3033           0 :     const unsigned char* pPtr = pFontBytes;
    3034           0 :     const unsigned char* pEnd = pFontBytes + nByteLen;
    3035             : 
    3036           0 :     for( int i = 0; i < 3; ++i) {
    3037             :         // read segment1 header
    3038           0 :         if( pPtr+6 >= pEnd )
    3039           0 :             return false;
    3040           0 :         if( (pPtr[0] != 0x80) || (pPtr[1] >= 0x03) )
    3041           0 :             return false;
    3042           0 :         const int nLen = (pPtr[5]<<24) + (pPtr[4]<<16) + (pPtr[3]<<8) + pPtr[2];
    3043           0 :         if( nLen <= 0)
    3044           0 :             return false;
    3045           0 :         rSegmentLengths[i] = nLen;
    3046           0 :         pPtr += nLen + 6;
    3047             :     }
    3048             : 
    3049             :     // read segment-end header
    3050           0 :     if( pPtr+2 >= pEnd )
    3051           0 :         return false;
    3052           0 :     if( (pPtr[0] != 0x80) || (pPtr[1] != 0x03) )
    3053           0 :         return false;
    3054             : 
    3055           0 :     return true;
    3056             : }
    3057             : 
    3058           0 : struct FontException : public std::exception
    3059             : {
    3060             : };
    3061             : 
    3062             : // TODO: always subset instead of embedding the full font => this method becomes obsolete then
    3063           0 : std::map< sal_Int32, sal_Int32 > PDFWriterImpl::emitEmbeddedFont( const PhysicalFontFace* pFont, EmbedFont& rEmbed )
    3064             : {
    3065           0 :     std::map< sal_Int32, sal_Int32 > aRet;
    3066             : 
    3067           0 :     sal_Int32 nStreamObject = 0;
    3068           0 :     sal_Int32 nFontDescriptor = 0;
    3069             : 
    3070             :     // prepare font encoding
    3071           0 :     const Ucs2SIntMap* pEncoding = m_pReferenceDevice->mpGraphics->GetFontEncodingVector( pFont, NULL );
    3072           0 :     sal_Int32 nToUnicodeStream = 0;
    3073             :     sal_uInt8 nEncoding[256];
    3074             :     sal_Ucs nEncodedCodes[256];
    3075           0 :     std::vector<sal_Ucs> aUnicodes;
    3076           0 :     aUnicodes.reserve( 256 );
    3077             :     sal_Int32 pUnicodesPerGlyph[256];
    3078             :     sal_Int32 pEncToUnicodeIndex[256];
    3079           0 :     if( pEncoding )
    3080             :     {
    3081           0 :         memset( nEncoding, 0, sizeof(nEncoding) );
    3082           0 :         memset( nEncodedCodes, 0, sizeof(nEncodedCodes) );
    3083           0 :         memset( pUnicodesPerGlyph, 0, sizeof(pUnicodesPerGlyph) );
    3084           0 :         memset( pEncToUnicodeIndex, 0, sizeof(pEncToUnicodeIndex) );
    3085           0 :         for( Ucs2SIntMap::const_iterator it = pEncoding->begin(); it != pEncoding->end(); ++it )
    3086             :         {
    3087           0 :             if( it->second != -1 )
    3088             :             {
    3089           0 :                 sal_Int32 nCode = (sal_Int32)(it->second & 0x000000ff);
    3090           0 :                 nEncoding[ nCode ] = static_cast<sal_uInt8>( nCode );
    3091           0 :                 nEncodedCodes[ nCode ] = it->first;
    3092           0 :                 pEncToUnicodeIndex[ nCode ] = static_cast<sal_Int32>(aUnicodes.size());
    3093           0 :                 aUnicodes.push_back( it->first );
    3094           0 :                 pUnicodesPerGlyph[ nCode ] = 1;
    3095             :             }
    3096             :         }
    3097             :     }
    3098             : 
    3099           0 :     FontSubsetInfo aInfo;
    3100             :     sal_Int32 pWidths[256];
    3101           0 :     const unsigned char* pFontData = NULL;
    3102           0 :     long nFontLen = 0;
    3103             :     sal_Int32 nLength1, nLength2;
    3104             :     try
    3105             :     {
    3106           0 :         if( (pFontData = (const unsigned char*)m_pReferenceDevice->mpGraphics->GetEmbedFontData( pFont, nEncodedCodes, pWidths, aInfo, &nFontLen )) != NULL )
    3107             :         {
    3108           0 :             if( (aInfo.m_nFontType & FontSubsetInfo::ANY_TYPE1) == 0 )
    3109           0 :                 throw FontException();
    3110             :             // see whether it is pfb or pfa; if it is a pfb, fill ranges
    3111             :             // of 6 bytes that are not part of the font program
    3112           0 :             std::list< int > aSections;
    3113           0 :             std::list< int >::const_iterator it;
    3114           0 :             int nIndex = 0;
    3115           0 :             while( (nIndex < nFontLen-1) && pFontData[nIndex] == 0x80 )
    3116             :             {
    3117           0 :                 aSections.push_back( nIndex );
    3118           0 :                 if( pFontData[nIndex+1] == 0x03 )
    3119           0 :                     break;
    3120             :                 sal_Int32 nBytes =
    3121           0 :                 ((sal_Int32)pFontData[nIndex+2])            |
    3122           0 :                 ((sal_Int32)pFontData[nIndex+3]) << 8       |
    3123           0 :                 ((sal_Int32)pFontData[nIndex+4]) << 16      |
    3124           0 :                 ((sal_Int32)pFontData[nIndex+5]) << 24;
    3125           0 :                 nIndex += nBytes+6;
    3126             :             }
    3127             : 
    3128             :             // search for eexec
    3129             :             // TODO: use getPfbSegmentLengths() if possible to skip the search thingies below
    3130           0 :             nIndex = 0;
    3131             :             int nEndAsciiIndex;
    3132             :             int nBeginBinaryIndex;
    3133             :             int nEndBinaryIndex;
    3134           0 :             do
    3135             :             {
    3136           0 :                 while( nIndex < nFontLen-4 &&
    3137           0 :                     ( pFontData[nIndex] != 'e'  ||
    3138           0 :                         pFontData[nIndex+1] != 'e' ||
    3139           0 :                         pFontData[nIndex+2] != 'x' ||
    3140           0 :                         pFontData[nIndex+3] != 'e' ||
    3141           0 :                         pFontData[nIndex+4] != 'c'
    3142             :                         )
    3143             :                     )
    3144             :                 {
    3145           0 :                     ++nIndex;
    3146             :                 }
    3147             :                 // check whether we are in a excluded section
    3148           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3149             :                     ;
    3150           0 :             } while( it != aSections.end() && nIndex < nFontLen-4 );
    3151             :             // this should end the ascii part
    3152           0 :             if( nIndex > nFontLen-5 )
    3153           0 :                 throw FontException();
    3154             : 
    3155           0 :             nEndAsciiIndex = nIndex+4;
    3156             :             // now count backwards until we can account for 512 '0'
    3157             :             // which is the endmarker of the (hopefully) binary data
    3158             :             // do not count the pfb header sections
    3159           0 :             int nFound = 0;
    3160           0 :             nIndex =  nFontLen-1;
    3161           0 :             while( nIndex > 0 && nFound < 512 )
    3162             :             {
    3163           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3164             :                     ;
    3165           0 :                 if( it == aSections.end() )
    3166             :                 {
    3167             :                     // inside the 512 '0' block there may only be whitespace
    3168             :                     // according to T1 spec; probably it would be to simple
    3169             :                     // if all fonts complied
    3170           0 :                     if( pFontData[nIndex] == '0' )
    3171           0 :                         nFound++;
    3172           0 :                         else if( nFound > 0                 &&
    3173           0 :                             pFontData[nIndex] != '\r'       &&
    3174           0 :                         pFontData[nIndex] != '\t'       &&
    3175           0 :                         pFontData[nIndex] != '\n'       &&
    3176           0 :                         pFontData[nIndex] != ' ' )
    3177           0 :                         break;
    3178             :                 }
    3179           0 :                 nIndex--;
    3180             :             }
    3181             : 
    3182           0 :             if( nIndex < 1 || nIndex <= nEndAsciiIndex )
    3183           0 :                 throw FontException();
    3184             : 
    3185             :             // nLength3 is the rest of the file - excluding any section headers
    3186             :             // nIndex now points before the first of the 512 '0' characters marking the
    3187             :             // fixed content portion
    3188           0 :             sal_Int32 nLength3 = nFontLen - nIndex - 1;
    3189           0 :             for( it = aSections.begin(); it != aSections.end(); ++it )
    3190             :             {
    3191             :                 // special case: nIndex inside a section marker
    3192           0 :                 if( nIndex >= (*it) && (*it)+6 > nIndex )
    3193           0 :                     nLength3 -= (*it)+6 - nIndex;
    3194           0 :                 else if( *it >= nIndex  )
    3195             :                 {
    3196           0 :                     if( *it < nFontLen - 6 )
    3197           0 :                         nLength3 -= 6;
    3198             :                     else // the last section 0x8003 is only 2 bytes after all
    3199           0 :                         nLength3 -= (nFontLen - *it);
    3200             :                 }
    3201             :             }
    3202             : 
    3203             :             // there may be whitespace to ignore before the 512 '0'
    3204           0 :             while( pFontData[nIndex] == '\r' || pFontData[nIndex] == '\n' )
    3205             :             {
    3206           0 :                 nIndex--;
    3207           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3208             :                     ;
    3209           0 :                 if( it != aSections.end() )
    3210             :                 {
    3211           0 :                     nIndex = (*it)-1;
    3212           0 :                     break; // this is surely a binary boundary, in ascii case it wouldn't matter
    3213             :                 }
    3214             :             }
    3215           0 :             nEndBinaryIndex = nIndex;
    3216             : 
    3217             :             // search for beginning of binary section
    3218           0 :             nBeginBinaryIndex = nEndAsciiIndex;
    3219           0 :             do
    3220             :             {
    3221           0 :                 nBeginBinaryIndex++;
    3222           0 :                 for( it = aSections.begin(); it != aSections.end() && (nBeginBinaryIndex < *it || nBeginBinaryIndex > ((*it) + 5) ); ++it )
    3223             :                     ;
    3224           0 :                     } while( nBeginBinaryIndex < nEndBinaryIndex &&
    3225           0 :                         ( pFontData[nBeginBinaryIndex] == '\r'  ||
    3226           0 :                             pFontData[nBeginBinaryIndex] == '\n'    ||
    3227           0 :                             it != aSections.end() ) );
    3228             : 
    3229             :                     // it seems to be vital to copy the exact whitespace between binary data
    3230             :                     // and eexec, else a invalid font results. so make nEndAsciiIndex
    3231             :                     // always immediate in front of nBeginBinaryIndex
    3232           0 :                     nEndAsciiIndex = nBeginBinaryIndex-1;
    3233           0 :                     for( it = aSections.begin(); it != aSections.end() && (nEndAsciiIndex < *it || nEndAsciiIndex > ((*it)+5)); ++it )
    3234             :                         ;
    3235           0 :                     if( it != aSections.end() )
    3236           0 :                         nEndAsciiIndex = (*it)-1;
    3237             : 
    3238           0 :                     nLength1 = nEndAsciiIndex+1; // including the last character
    3239           0 :                     for( it = aSections.begin(); it != aSections.end() && *it < nEndAsciiIndex; ++it )
    3240           0 :                         nLength1 -= 6; // decrease by pfb section size
    3241             : 
    3242             :                     // if the first four bytes are all ascii hex characters, then binary data
    3243             :                     // has to be converted to real binary data
    3244           0 :                     for( nIndex = 0; nIndex < 4 &&
    3245           0 :                         ( ( pFontData[ nBeginBinaryIndex+nIndex ] >= '0' && pFontData[ nBeginBinaryIndex+nIndex ] <= '9' ) ||
    3246           0 :                             ( pFontData[ nBeginBinaryIndex+nIndex ] >= 'a' && pFontData[ nBeginBinaryIndex+nIndex ] <= 'f' ) ||
    3247           0 :                             ( pFontData[ nBeginBinaryIndex+nIndex ] >= 'A' && pFontData[ nBeginBinaryIndex+nIndex ] <= 'F' )
    3248             :                             ); ++nIndex )
    3249             :                         ;
    3250           0 :                     bool bConvertHexData = true;
    3251           0 :                     if( nIndex < 4 )
    3252             :                     {
    3253           0 :                         bConvertHexData = false;
    3254           0 :                         nLength2 = nEndBinaryIndex - nBeginBinaryIndex + 1; // include the last byte
    3255           0 :                         for( it = aSections.begin(); it != aSections.end(); ++it )
    3256           0 :                             if( *it > nBeginBinaryIndex && *it < nEndBinaryIndex )
    3257           0 :                                 nLength2 -= 6;
    3258             :                     }
    3259             :                     else
    3260             :                     {
    3261             :                         // count the hex ascii characters to get nLength2
    3262           0 :                         nLength2 = 0;
    3263           0 :                         int nNextSectionIndex = 0;
    3264           0 :                         for( it = aSections.begin(); it != aSections.end() && *it < nBeginBinaryIndex; ++it )
    3265             :                             ;
    3266           0 :                         if( it != aSections.end() )
    3267           0 :                             nNextSectionIndex = *it;
    3268           0 :                         for( nIndex = nBeginBinaryIndex; nIndex <= nEndBinaryIndex; nIndex++ )
    3269             :                         {
    3270           0 :                             if( nIndex == nNextSectionIndex )
    3271             :                             {
    3272           0 :                                 nIndex += 6;
    3273           0 :                                 ++it;
    3274           0 :                                 nNextSectionIndex = (it == aSections.end() ? 0 : *it );
    3275             :                             }
    3276           0 :                             if( ( pFontData[ nIndex ] >= '0' && pFontData[ nIndex ] <= '9' ) ||
    3277           0 :                                 ( pFontData[ nIndex ] >= 'a' && pFontData[ nIndex ] <= 'f' ) ||
    3278           0 :                             ( pFontData[ nIndex ] >= 'A' && pFontData[ nIndex ] <= 'F' ) )
    3279           0 :                             nLength2++;
    3280             :                         }
    3281             :                         DBG_ASSERT( !(nLength2 & 1), "uneven number of hex chars in binary pfa section" );
    3282           0 :                         nLength2 /= 2;
    3283             :                     }
    3284             : 
    3285             :                     // now we can actually write the font stream !
    3286             :                     #if OSL_DEBUG_LEVEL > 1
    3287             :                     emitComment( " PDFWriterImpl::emitEmbeddedFont" );
    3288             :                     #endif
    3289           0 :                     OStringBuffer aLine( 512 );
    3290           0 :                     nStreamObject = createObject();
    3291           0 :                     if( !updateObject(nStreamObject))
    3292           0 :                         throw FontException();
    3293           0 :                     sal_Int32 nStreamLengthObject = createObject();
    3294           0 :                     aLine.append( nStreamObject );
    3295             :                     aLine.append( " 0 obj\n"
    3296           0 :                         "<</Length " );
    3297           0 :                     aLine.append( nStreamLengthObject );
    3298             :                     aLine.append( " 0 R"
    3299             :                         #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3300             :                         "/Filter/FlateDecode"
    3301             :                         #endif
    3302           0 :                         "/Length1 " );
    3303           0 :                     aLine.append( nLength1 );
    3304           0 :                     aLine.append( " /Length2 " );
    3305           0 :                     aLine.append( nLength2 );
    3306           0 :                     aLine.append( " /Length3 ");
    3307           0 :                     aLine.append( nLength3 );
    3308             :                     aLine.append( ">>\n"
    3309           0 :                         "stream\n" );
    3310           0 :                     if( !writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3311           0 :                         throw FontException();
    3312             : 
    3313           0 :                     sal_uInt64 nBeginStreamPos = 0;
    3314           0 :                     osl_getFilePos( m_aFile, &nBeginStreamPos );
    3315             : 
    3316           0 :                     beginCompression();
    3317           0 :                     checkAndEnableStreamEncryption( nStreamObject );
    3318             : 
    3319             :                     // write ascii section
    3320           0 :                     if( aSections.begin() == aSections.end() )
    3321             :                     {
    3322           0 :                         if( ! writeBuffer( pFontData, nEndAsciiIndex+1 ) )
    3323           0 :                             throw FontException();
    3324             :                     }
    3325             :                     else
    3326             :                     {
    3327             :                         // first section always starts at 0
    3328           0 :                         it = aSections.begin();
    3329           0 :                         nIndex = (*it)+6;
    3330           0 :                         ++it;
    3331           0 :                         while( *it < nEndAsciiIndex )
    3332             :                         {
    3333           0 :                             if( ! writeBuffer( pFontData+nIndex, (*it)-nIndex ) )
    3334           0 :                                 throw FontException();
    3335           0 :                             nIndex = (*it)+6;
    3336           0 :                             ++it;
    3337             :                         }
    3338             :                         // write partial last section
    3339           0 :                         if( ! writeBuffer( pFontData+nIndex, nEndAsciiIndex-nIndex+1 ) )
    3340           0 :                             throw FontException();
    3341             :                     }
    3342             : 
    3343             :                     // write binary section
    3344           0 :                     if( ! bConvertHexData )
    3345             :                     {
    3346           0 :                         if( aSections.begin() == aSections.end() )
    3347             :                         {
    3348           0 :                             if( ! writeBuffer( pFontData+nBeginBinaryIndex, nFontLen-nBeginBinaryIndex ) )
    3349           0 :                                 throw FontException();
    3350             :                         }
    3351             :                         else
    3352             :                         {
    3353           0 :                             for( it = aSections.begin(); *it < nBeginBinaryIndex; ++it )
    3354             :                                 ;
    3355             :                             // write first partial section
    3356           0 :                             if( ! writeBuffer( pFontData+nBeginBinaryIndex, (*it) - nBeginBinaryIndex ) )
    3357           0 :                                 throw FontException();
    3358             :                             // write following sections
    3359           0 :                             while( it != aSections.end() )
    3360             :                             {
    3361           0 :                                 nIndex = (*it)+6;
    3362           0 :                                 ++it;
    3363           0 :                                 if( nIndex < nFontLen ) // last section marker is usually the EOF which has only 2 bytes
    3364             :                                 {
    3365           0 :                                     sal_Int32 nSectionLen = (it == aSections.end()) ? nFontLen - nIndex : (*it) - nIndex;
    3366           0 :                                     if( ! writeBuffer( pFontData+nIndex, nSectionLen ) )
    3367           0 :                                         throw FontException();
    3368             :                                 }
    3369             :                             }
    3370             :                         }
    3371             :                     }
    3372             :                     else
    3373             :                     {
    3374           0 :                         boost::shared_array<unsigned char> pWriteBuffer( new unsigned char[ nLength2 ] );
    3375           0 :                         memset( pWriteBuffer.get(), 0, nLength2 );
    3376           0 :                         int nWriteIndex = 0;
    3377             : 
    3378           0 :                         int nNextSectionIndex = 0;
    3379           0 :                         for( it = aSections.begin(); it != aSections.end() && *it < nBeginBinaryIndex; ++it )
    3380             :                             ;
    3381           0 :                         if( it != aSections.end() )
    3382           0 :                             nNextSectionIndex = *it;
    3383           0 :                         for( nIndex = nBeginBinaryIndex; nIndex <= nEndBinaryIndex; nIndex++ )
    3384             :                         {
    3385           0 :                             if( nIndex == nNextSectionIndex )
    3386             :                             {
    3387           0 :                                 nIndex += 6;
    3388           0 :                                 ++it;
    3389           0 :                                 nNextSectionIndex = (it == aSections.end() ? nFontLen : *it );
    3390             :                             }
    3391           0 :                             unsigned char cNibble = 0x80;
    3392           0 :                             if( pFontData[ nIndex ] >= '0' && pFontData[ nIndex ] <= '9' )
    3393           0 :                                 cNibble = pFontData[nIndex] - '0';
    3394           0 :                             else if( pFontData[ nIndex ] >= 'a' && pFontData[ nIndex ] <= 'f' )
    3395           0 :                                 cNibble = pFontData[nIndex] - 'a' + 10;
    3396           0 :                             else if( pFontData[ nIndex ] >= 'A' && pFontData[ nIndex ] <= 'F' )
    3397           0 :                                 cNibble = pFontData[nIndex] - 'A' + 10;
    3398           0 :                             if( cNibble != 0x80 )
    3399             :                             {
    3400           0 :                                 if( !(nWriteIndex & 1 ) )
    3401           0 :                                     cNibble <<= 4;
    3402           0 :                                 pWriteBuffer.get()[ nWriteIndex/2 ] |= cNibble;
    3403           0 :                                 nWriteIndex++;
    3404             :                             }
    3405             :                         }
    3406           0 :                         if( ! writeBuffer( pWriteBuffer.get(), nLength2 ) )
    3407           0 :                             throw FontException();
    3408           0 :                         if( aSections.empty() )
    3409             :                         {
    3410           0 :                             if( ! writeBuffer( pFontData+nIndex, nFontLen-nIndex ) )
    3411           0 :                                 throw FontException();
    3412             :                         }
    3413             :                         else
    3414             :                         {
    3415             :                             // write rest of this section
    3416           0 :                             if( nIndex < nNextSectionIndex )
    3417             :                             {
    3418           0 :                                 if( ! writeBuffer( pFontData+nIndex, nNextSectionIndex - nIndex ) )
    3419           0 :                                     throw FontException();
    3420             :                             }
    3421             :                             // write following sections
    3422           0 :                             while( it != aSections.end() )
    3423             :                             {
    3424           0 :                                 nIndex = (*it)+6;
    3425           0 :                                 ++it;
    3426           0 :                                 if( nIndex < nFontLen ) // last section marker is usually the EOF which has only 2 bytes
    3427             :                                 {
    3428           0 :                                     sal_Int32 nSectionLen = (it == aSections.end()) ? nFontLen - nIndex : (*it) - nIndex;
    3429           0 :                                     if( ! writeBuffer( pFontData+nIndex, nSectionLen ) )
    3430           0 :                                         throw FontException();
    3431             :                                 }
    3432             :                             }
    3433           0 :                         }
    3434             :                     }
    3435           0 :                     endCompression();
    3436           0 :                     disableStreamEncryption();
    3437             : 
    3438           0 :                     sal_uInt64 nEndStreamPos = 0;
    3439           0 :                     osl_getFilePos( m_aFile, &nEndStreamPos );
    3440             : 
    3441             :                     // and finally close the stream
    3442           0 :                     aLine.setLength( 0 );
    3443           0 :                     aLine.append( "\nendstream\nendobj\n\n" );
    3444           0 :                     if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3445           0 :                         throw FontException();
    3446             : 
    3447             :                     // write stream length object
    3448           0 :                     aLine.setLength( 0 );
    3449           0 :                     if( ! updateObject( nStreamLengthObject ) )
    3450           0 :                         throw FontException();
    3451           0 :                     aLine.append( nStreamLengthObject );
    3452           0 :                     aLine.append( " 0 obj\n" );
    3453           0 :                     aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos ) );
    3454           0 :                     aLine.append( "\nendobj\n\n" );
    3455           0 :                     if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3456           0 :                         throw FontException();
    3457             :         }
    3458             :         else
    3459             :         {
    3460           0 :             OStringBuffer aErrorComment( 256 );
    3461           0 :             aErrorComment.append( "GetEmbedFontData failed for font \"" );
    3462           0 :             aErrorComment.append( OUStringToOString( pFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ) );
    3463           0 :             aErrorComment.append( '\"' );
    3464           0 :             if( pFont->GetSlant() == ITALIC_NORMAL )
    3465           0 :                 aErrorComment.append( " italic" );
    3466           0 :             else if( pFont->GetSlant() == ITALIC_OBLIQUE )
    3467           0 :                 aErrorComment.append( " oblique" );
    3468           0 :             aErrorComment.append( " weight=" );
    3469           0 :             aErrorComment.append( sal_Int32(pFont->GetWeight()) );
    3470           0 :             emitComment( aErrorComment.getStr() );
    3471             :         }
    3472             : 
    3473           0 :         if( nStreamObject )
    3474             :         {
    3475             :             // write font descriptor
    3476           0 :             nFontDescriptor = emitFontDescriptor( pFont, aInfo, 0, nStreamObject );
    3477             :         }
    3478             : 
    3479           0 :         if( nFontDescriptor )
    3480             :         {
    3481           0 :             if( pEncoding )
    3482           0 :                 nToUnicodeStream = createToUnicodeCMap( nEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, SAL_N_ELEMENTS(nEncoding) );
    3483             : 
    3484             :             // write font object
    3485           0 :             sal_Int32 nObject = createObject();
    3486           0 :             if( ! updateObject( nObject ) )
    3487           0 :                 throw FontException();
    3488             : 
    3489           0 :             OStringBuffer aLine( 1024 );
    3490           0 :             aLine.append( nObject );
    3491             :             aLine.append( " 0 obj\n"
    3492           0 :                 "<</Type/Font/Subtype/Type1/BaseFont/" );
    3493           0 :             appendName( aInfo.m_aPSName, aLine );
    3494           0 :             aLine.append( "\n" );
    3495           0 :             if( !pFont->IsSymbolFont() &&  pEncoding == 0 )
    3496           0 :                 aLine.append( "/Encoding/WinAnsiEncoding\n" );
    3497           0 :             if( nToUnicodeStream )
    3498             :             {
    3499           0 :                 aLine.append( "/ToUnicode " );
    3500           0 :                 aLine.append( nToUnicodeStream );
    3501           0 :                 aLine.append( " 0 R\n" );
    3502             :             }
    3503             :             aLine.append( "/FirstChar 0 /LastChar 255\n"
    3504           0 :                 "/Widths[" );
    3505           0 :             for( int i = 0; i < 256; i++ )
    3506             :             {
    3507           0 :                 aLine.append( pWidths[i] );
    3508           0 :                 aLine.append( ((i&15) == 15) ? "\n" : " " );
    3509             :             }
    3510             :             aLine.append( "]\n"
    3511           0 :                 "/FontDescriptor " );
    3512           0 :             aLine.append( nFontDescriptor );
    3513             :             aLine.append( " 0 R>>\n"
    3514           0 :                 "endobj\n\n" );
    3515           0 :             if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3516           0 :                 throw FontException();
    3517             : 
    3518           0 :             aRet[ rEmbed.m_nNormalFontID ] = nObject;
    3519             : 
    3520             :             // write additional encodings
    3521           0 :             for( std::list< EmbedEncoding >::iterator enc_it = rEmbed.m_aExtendedEncodings.begin(); enc_it != rEmbed.m_aExtendedEncodings.end(); ++enc_it )
    3522             :             {
    3523             :                 sal_Int32 aEncWidths[ 256 ];
    3524             :                 // emit encoding dict
    3525           0 :                 sal_Int32 nEncObject = createObject();
    3526           0 :                 if( ! updateObject( nEncObject ) )
    3527           0 :                     throw FontException();
    3528             : 
    3529           0 :                 OutputDevice* pRef = getReferenceDevice();
    3530           0 :                 pRef->Push( PUSH_FONT | PUSH_MAPMODE );
    3531           0 :                 pRef->SetMapMode( MapMode( MAP_PIXEL ) );
    3532           0 :                 Font aFont( pFont->GetFamilyName(), pFont->GetStyleName(), Size( 0, 1000 ) );
    3533           0 :                 aFont.SetWeight( pFont->GetWeight() );
    3534           0 :                 aFont.SetItalic( pFont->GetSlant() );
    3535           0 :                 aFont.SetPitch( pFont->GetPitch() );
    3536           0 :                 pRef->SetFont( aFont );
    3537           0 :                 pRef->ImplNewFont();
    3538             : 
    3539           0 :                 aLine.setLength( 0 );
    3540           0 :                 aLine.append( nEncObject );
    3541             :                 aLine.append( " 0 obj\n"
    3542           0 :                     "<</Type/Encoding/Differences[ 0\n" );
    3543           0 :                 int nEncoded = 0;
    3544           0 :                 aUnicodes.clear();
    3545           0 :                 for( std::vector< EmbedCode >::iterator str_it = enc_it->m_aEncVector.begin(); str_it != enc_it->m_aEncVector.end(); ++str_it )
    3546             :                 {
    3547           0 :                     OUString aStr( str_it->m_aUnicode );
    3548           0 :                     aEncWidths[nEncoded] = pRef->GetTextWidth( aStr );
    3549           0 :                     nEncodedCodes[nEncoded] = str_it->m_aUnicode;
    3550           0 :                     nEncoding[nEncoded] = sal::static_int_cast<sal_uInt8>(nEncoded);
    3551           0 :                     pEncToUnicodeIndex[nEncoded] = static_cast<sal_Int32>(aUnicodes.size());
    3552           0 :                     aUnicodes.push_back( nEncodedCodes[nEncoded] );
    3553           0 :                     pUnicodesPerGlyph[nEncoded] = 1;
    3554             : 
    3555           0 :                     aLine.append( " /" );
    3556           0 :                     aLine.append( str_it->m_aName );
    3557           0 :                     if( !((++nEncoded) & 15) )
    3558           0 :                         aLine.append( "\n" );
    3559           0 :                 }
    3560             :                 aLine.append( "]>>\n"
    3561           0 :                     "endobj\n\n" );
    3562             : 
    3563           0 :                 pRef->Pop();
    3564             : 
    3565           0 :                 if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3566           0 :                     throw FontException();
    3567             : 
    3568           0 :                 nToUnicodeStream = createToUnicodeCMap( nEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, nEncoded );
    3569             : 
    3570           0 :                 nObject = createObject();
    3571           0 :                 if( ! updateObject( nObject ) )
    3572           0 :                     throw FontException();
    3573             : 
    3574           0 :                 aLine.setLength( 0 );
    3575           0 :                 aLine.append( nObject );
    3576             :                 aLine.append( " 0 obj\n"
    3577           0 :                     "<</Type/Font/Subtype/Type1/BaseFont/" );
    3578           0 :                 appendName( aInfo.m_aPSName, aLine );
    3579           0 :                 aLine.append( "\n" );
    3580           0 :                 aLine.append( "/Encoding " );
    3581           0 :                 aLine.append( nEncObject );
    3582           0 :                 aLine.append( " 0 R\n" );
    3583           0 :                 if( nToUnicodeStream )
    3584             :                 {
    3585           0 :                     aLine.append( "/ToUnicode " );
    3586           0 :                     aLine.append( nToUnicodeStream );
    3587           0 :                     aLine.append( " 0 R\n" );
    3588             :                 }
    3589             :                 aLine.append( "/FirstChar 0\n"
    3590           0 :                     "/LastChar " );
    3591           0 :                 aLine.append( (sal_Int32)(nEncoded-1) );
    3592             :                 aLine.append( "\n"
    3593           0 :                     "/Widths[" );
    3594           0 :                 for( int i = 0; i < nEncoded; i++ )
    3595             :                 {
    3596           0 :                     aLine.append( aEncWidths[i] );
    3597           0 :                     aLine.append( ((i&15) == 15) ? "\n" : " " );
    3598             :                 }
    3599             :                 aLine.append( " ]\n"
    3600           0 :                     "/FontDescriptor " );
    3601           0 :                 aLine.append( nFontDescriptor );
    3602             :                 aLine.append( " 0 R>>\n"
    3603           0 :                     "endobj\n\n" );
    3604           0 :                 if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3605           0 :                     throw FontException();
    3606             : 
    3607           0 :                 aRet[ enc_it->m_nFontID ] = nObject;
    3608           0 :             }
    3609             :         }
    3610             :     }
    3611           0 :     catch( FontException& )
    3612             :     {
    3613             :         // these do nothing in case there was no compression or encryption ongoing
    3614           0 :         endCompression();
    3615           0 :         disableStreamEncryption();
    3616             :     }
    3617             : 
    3618           0 :     if( pFontData )
    3619           0 :         m_pReferenceDevice->mpGraphics->FreeEmbedFontData( pFontData, nFontLen );
    3620             : 
    3621           0 :     return aRet;
    3622             : }
    3623             : 
    3624           0 : static void appendSubsetName( int nSubsetID, const OUString& rPSName, OStringBuffer& rBuffer )
    3625             : {
    3626           0 :     if( nSubsetID )
    3627             :     {
    3628           0 :         for( int i = 0; i < 6; i++ )
    3629             :         {
    3630           0 :             int nOffset = (nSubsetID % 26);
    3631           0 :             nSubsetID /= 26;
    3632           0 :             rBuffer.append( (sal_Char)('A'+nOffset) );
    3633             :         }
    3634           0 :         rBuffer.append( '+' );
    3635             :     }
    3636           0 :     appendName( rPSName, rBuffer );
    3637           0 : }
    3638             : 
    3639           0 : sal_Int32 PDFWriterImpl::createToUnicodeCMap( sal_uInt8* pEncoding,
    3640             :                                               sal_Ucs* pUnicodes,
    3641             :                                               sal_Int32* pUnicodesPerGlyph,
    3642             :                                               sal_Int32* pEncToUnicodeIndex,
    3643             :                                               int nGlyphs )
    3644             : {
    3645           0 :     int nMapped = 0, n = 0;
    3646           0 :     for( n = 0; n < nGlyphs; n++ )
    3647           0 :         if( pUnicodes[pEncToUnicodeIndex[n]] && pUnicodesPerGlyph[n] )
    3648           0 :             nMapped++;
    3649             : 
    3650           0 :     if( nMapped == 0 )
    3651           0 :         return 0;
    3652             : 
    3653           0 :     sal_Int32 nStream = createObject();
    3654           0 :     CHECK_RETURN( updateObject( nStream ) );
    3655             : 
    3656           0 :     OStringBuffer aContents( 1024 );
    3657             :     aContents.append(
    3658             :                      "/CIDInit/ProcSet findresource begin\n"
    3659             :                      "12 dict begin\n"
    3660             :                      "begincmap\n"
    3661             :                      "/CIDSystemInfo<<\n"
    3662             :                      "/Registry (Adobe)\n"
    3663             :                      "/Ordering (UCS)\n"
    3664             :                      "/Supplement 0\n"
    3665             :                      ">> def\n"
    3666             :                      "/CMapName/Adobe-Identity-UCS def\n"
    3667             :                      "/CMapType 2 def\n"
    3668             :                      "1 begincodespacerange\n"
    3669             :                      "<00> <FF>\n"
    3670             :                      "endcodespacerange\n"
    3671           0 :                      );
    3672           0 :     int nCount = 0;
    3673           0 :     for( n = 0; n < nGlyphs; n++ )
    3674             :     {
    3675           0 :         if( pUnicodes[pEncToUnicodeIndex[n]] && pUnicodesPerGlyph[n] )
    3676             :         {
    3677           0 :             if( (nCount % 100) == 0 )
    3678             :             {
    3679           0 :                 if( nCount )
    3680           0 :                     aContents.append( "endbfchar\n" );
    3681           0 :                 aContents.append( (sal_Int32)((nMapped-nCount > 100) ? 100 : nMapped-nCount ) );
    3682           0 :                 aContents.append( " beginbfchar\n" );
    3683             :             }
    3684           0 :             aContents.append( '<' );
    3685           0 :             appendHex( (sal_Int8)pEncoding[n], aContents );
    3686           0 :             aContents.append( "> <" );
    3687             :             // TODO: handle unicodes>U+FFFF
    3688           0 :             sal_Int32 nIndex = pEncToUnicodeIndex[n];
    3689           0 :             for( sal_Int32 j = 0; j < pUnicodesPerGlyph[n]; j++ )
    3690             :             {
    3691           0 :                 appendHex( (sal_Int8)(pUnicodes[nIndex + j] / 256), aContents );
    3692           0 :                 appendHex( (sal_Int8)(pUnicodes[nIndex + j] & 255), aContents );
    3693             :             }
    3694           0 :             aContents.append( ">\n" );
    3695           0 :             nCount++;
    3696             :         }
    3697             :     }
    3698             :     aContents.append( "endbfchar\n"
    3699             :                       "endcmap\n"
    3700             :                       "CMapName currentdict /CMap defineresource pop\n"
    3701             :                       "end\n"
    3702           0 :                       "end\n" );
    3703             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3704           0 :     ZCodec* pCodec = new ZCodec( 0x4000, 0x4000 );
    3705           0 :     SvMemoryStream aStream;
    3706           0 :     pCodec->BeginCompression();
    3707           0 :     pCodec->Write( aStream, (const sal_uInt8*)aContents.getStr(), aContents.getLength() );
    3708           0 :     pCodec->EndCompression();
    3709           0 :     delete pCodec;
    3710             : #endif
    3711             : 
    3712             :     #if OSL_DEBUG_LEVEL > 1
    3713             :     emitComment( "PDFWriterImpl::createToUnicodeCMap" );
    3714             :     #endif
    3715           0 :     OStringBuffer aLine( 40 );
    3716             : 
    3717           0 :     aLine.append( nStream );
    3718           0 :     aLine.append( " 0 obj\n<</Length " );
    3719             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3720           0 :     sal_Int32 nLen = (sal_Int32)aStream.Tell();
    3721           0 :     aStream.Seek( 0 );
    3722           0 :     aLine.append( nLen );
    3723           0 :     aLine.append( "/Filter/FlateDecode" );
    3724             : #else
    3725             :     aLine.append( aContents.getLength() );
    3726             : #endif
    3727           0 :     aLine.append( ">>\nstream\n" );
    3728           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3729           0 :     checkAndEnableStreamEncryption( nStream );
    3730             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3731           0 :     CHECK_RETURN( writeBuffer( aStream.GetData(), nLen ) );
    3732             : #else
    3733             :     CHECK_RETURN( writeBuffer( aContents.getStr(), aContents.getLength() ) );
    3734             : #endif
    3735           0 :     disableStreamEncryption();
    3736           0 :     aLine.setLength( 0 );
    3737             :     aLine.append( "\nendstream\n"
    3738           0 :                   "endobj\n\n" );
    3739           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3740           0 :     return nStream;
    3741             : }
    3742             : 
    3743           0 : sal_Int32 PDFWriterImpl::emitFontDescriptor( const PhysicalFontFace* pFont, FontSubsetInfo& rInfo, sal_Int32 nSubsetID, sal_Int32 nFontStream )
    3744             : {
    3745           0 :     OStringBuffer aLine( 1024 );
    3746             :     // get font flags, see PDF reference 1.4 p. 358
    3747             :     // possibly characters outside Adobe standard encoding
    3748             :     // so set Symbolic flag
    3749           0 :     sal_Int32 nFontFlags = (1<<2);
    3750           0 :     if( pFont->GetSlant() == ITALIC_NORMAL || pFont->GetSlant() == ITALIC_OBLIQUE )
    3751           0 :         nFontFlags |= (1 << 6);
    3752           0 :     if( pFont->GetPitch() == PITCH_FIXED )
    3753           0 :         nFontFlags |= 1;
    3754           0 :     if( pFont->GetFamilyType() == FAMILY_SCRIPT )
    3755           0 :         nFontFlags |= (1 << 3);
    3756           0 :     else if( pFont->GetFamilyType() == FAMILY_ROMAN )
    3757           0 :         nFontFlags |= (1 << 1);
    3758             : 
    3759           0 :     sal_Int32 nFontDescriptor = createObject();
    3760           0 :     CHECK_RETURN( updateObject( nFontDescriptor ) );
    3761           0 :     aLine.setLength( 0 );
    3762           0 :     aLine.append( nFontDescriptor );
    3763             :     aLine.append( " 0 obj\n"
    3764           0 :                   "<</Type/FontDescriptor/FontName/" );
    3765           0 :     appendSubsetName( nSubsetID, rInfo.m_aPSName, aLine );
    3766             :     aLine.append( "\n"
    3767           0 :                   "/Flags " );
    3768           0 :     aLine.append( nFontFlags );
    3769             :     aLine.append( "\n"
    3770           0 :                   "/FontBBox[" );
    3771             :     // note: Top and Bottom are reversed in VCL and PDF rectangles
    3772           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.TopLeft().X() );
    3773           0 :     aLine.append( ' ' );
    3774           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.TopLeft().Y() );
    3775           0 :     aLine.append( ' ' );
    3776           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.BottomRight().X() );
    3777           0 :     aLine.append( ' ' );
    3778           0 :     aLine.append( (sal_Int32)(rInfo.m_aFontBBox.BottomRight().Y()+1) );
    3779           0 :     aLine.append( "]/ItalicAngle " );
    3780           0 :     if( pFont->GetSlant() == ITALIC_OBLIQUE || pFont->GetSlant() == ITALIC_NORMAL )
    3781           0 :         aLine.append( "-30" );
    3782             :     else
    3783           0 :         aLine.append( "0" );
    3784             :     aLine.append( "\n"
    3785           0 :                   "/Ascent " );
    3786           0 :     aLine.append( (sal_Int32)rInfo.m_nAscent );
    3787             :     aLine.append( "\n"
    3788           0 :                   "/Descent " );
    3789           0 :     aLine.append( (sal_Int32)-rInfo.m_nDescent );
    3790             :     aLine.append( "\n"
    3791           0 :                   "/CapHeight " );
    3792           0 :     aLine.append( (sal_Int32)rInfo.m_nCapHeight );
    3793             :     // According to PDF reference 1.4 StemV is required
    3794             :     // seems a tad strange to me, but well ...
    3795             :     aLine.append( "\n"
    3796           0 :                   "/StemV 80\n" );
    3797           0 :     if( nFontStream )
    3798             :     {
    3799           0 :         aLine.append( "/FontFile" );
    3800           0 :         switch( rInfo.m_nFontType )
    3801             :         {
    3802             :             case FontSubsetInfo::SFNT_TTF:
    3803           0 :                 aLine.append( '2' );
    3804           0 :                 break;
    3805             :             case FontSubsetInfo::TYPE1_PFA:
    3806             :             case FontSubsetInfo::TYPE1_PFB:
    3807             :             case FontSubsetInfo::ANY_TYPE1:
    3808           0 :                 break;
    3809             :             default:
    3810             :                 OSL_FAIL( "unknown fonttype in PDF font descriptor" );
    3811           0 :                 return 0;
    3812             :         }
    3813           0 :         aLine.append( ' ' );
    3814           0 :         aLine.append( nFontStream );
    3815           0 :         aLine.append( " 0 R\n" );
    3816             :     }
    3817             :     aLine.append( ">>\n"
    3818           0 :                   "endobj\n\n" );
    3819           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3820             : 
    3821           0 :     return nFontDescriptor;
    3822             : }
    3823             : 
    3824           0 : void PDFWriterImpl::appendBuiltinFontsToDict( OStringBuffer& rDict ) const
    3825             : {
    3826           0 :     for( std::map< sal_Int32, sal_Int32 >::const_iterator it =
    3827           0 :          m_aBuiltinFontToObjectMap.begin(); it != m_aBuiltinFontToObjectMap.end(); ++it )
    3828             :     {
    3829           0 :         rDict.append( m_aBuiltinFonts[it->first].getNameObject() );
    3830           0 :         rDict.append( ' ' );
    3831           0 :         rDict.append( it->second );
    3832           0 :         rDict.append( " 0 R" );
    3833             :     }
    3834           0 : }
    3835             : 
    3836           0 : bool PDFWriterImpl::emitFonts()
    3837             : {
    3838           0 :     if( ! m_pReferenceDevice->ImplGetGraphics() )
    3839           0 :         return false;
    3840             : 
    3841           0 :     OStringBuffer aLine( 1024 );
    3842             : 
    3843           0 :     std::map< sal_Int32, sal_Int32 > aFontIDToObject;
    3844             : 
    3845           0 :     OUString aTmpName;
    3846           0 :     osl_createTempFile( NULL, NULL, &aTmpName.pData );
    3847           0 :     for( FontSubsetData::iterator it = m_aSubsets.begin(); it != m_aSubsets.end(); ++it )
    3848             :     {
    3849           0 :         for( FontEmitList::iterator lit = it->second.m_aSubsets.begin(); lit != it->second.m_aSubsets.end(); ++lit )
    3850             :         {
    3851             :             sal_GlyphId aGlyphIds[ 256 ];
    3852             :             sal_Int32 pWidths[ 256 ];
    3853             :             sal_uInt8 pEncoding[ 256 ];
    3854             :             sal_Int32 pEncToUnicodeIndex[ 256 ];
    3855             :             sal_Int32 pUnicodesPerGlyph[ 256 ];
    3856           0 :             std::vector<sal_Ucs> aUnicodes;
    3857           0 :             aUnicodes.reserve( 256 );
    3858           0 :             int nGlyphs = 1;
    3859             :             // fill arrays and prepare encoding index map
    3860           0 :             sal_Int32 nToUnicodeStream = 0;
    3861             : 
    3862           0 :             memset( aGlyphIds, 0, sizeof( aGlyphIds ) );
    3863           0 :             memset( pEncoding, 0, sizeof( pEncoding ) );
    3864           0 :             memset( pUnicodesPerGlyph, 0, sizeof( pUnicodesPerGlyph ) );
    3865           0 :             memset( pEncToUnicodeIndex, 0, sizeof( pEncToUnicodeIndex ) );
    3866           0 :             for( FontEmitMapping::iterator fit = lit->m_aMapping.begin(); fit != lit->m_aMapping.end();++fit )
    3867             :             {
    3868           0 :                 sal_uInt8 nEnc = fit->second.getGlyphId();
    3869             : 
    3870             :                 DBG_ASSERT( aGlyphIds[nEnc] == 0 && pEncoding[nEnc] == 0, "duplicate glyph" );
    3871             :                 DBG_ASSERT( nEnc <= lit->m_aMapping.size(), "invalid glyph encoding" );
    3872             : 
    3873           0 :                 aGlyphIds[ nEnc ] = fit->first;
    3874           0 :                 pEncoding[ nEnc ] = nEnc;
    3875           0 :                 pEncToUnicodeIndex[ nEnc ] = static_cast<sal_Int32>(aUnicodes.size());
    3876           0 :                 pUnicodesPerGlyph[ nEnc ] = fit->second.countCodes();
    3877           0 :                 for( sal_Int32 n = 0; n < pUnicodesPerGlyph[ nEnc ]; n++ )
    3878           0 :                     aUnicodes.push_back( fit->second.getCode( n ) );
    3879           0 :                 if( fit->second.getCode(0) )
    3880           0 :                     nToUnicodeStream = 1;
    3881           0 :                 if( nGlyphs < 256 )
    3882           0 :                     nGlyphs++;
    3883             :                 else
    3884             :                 {
    3885             :                     OSL_FAIL( "too many glyphs for subset" );
    3886             :                 }
    3887             :             }
    3888           0 :             FontSubsetInfo aSubsetInfo;
    3889           0 :             if( m_pReferenceDevice->mpGraphics->CreateFontSubset( aTmpName, it->first, aGlyphIds, pEncoding, pWidths, nGlyphs, aSubsetInfo ) )
    3890             :             {
    3891             :                 // create font stream
    3892             :                 oslFileHandle aFontFile;
    3893           0 :                 if ( osl_File_E_None != osl_openFile( aTmpName.pData, &aFontFile, osl_File_OpenFlag_Read ) ) return false;
    3894             :                 // get file size
    3895             :                 sal_uInt64 nLength1;
    3896           0 :                 if ( osl_File_E_None != osl_setFilePos( aFontFile, osl_Pos_End, 0 ) ) return false;
    3897           0 :                 if ( osl_File_E_None != osl_getFilePos( aFontFile, &nLength1 ) ) return false;
    3898           0 :                 if ( osl_File_E_None != osl_setFilePos( aFontFile, osl_Pos_Absolut, 0 ) ) return false;
    3899             : 
    3900             :                 #if OSL_DEBUG_LEVEL > 1
    3901             :                 emitComment( "PDFWriterImpl::emitFonts" );
    3902             :                 #endif
    3903           0 :                 sal_Int32 nFontStream = createObject();
    3904           0 :                 sal_Int32 nStreamLengthObject = createObject();
    3905           0 :                 if ( !updateObject( nFontStream ) ) return false;
    3906           0 :                 aLine.setLength( 0 );
    3907           0 :                 aLine.append( nFontStream );
    3908             :                 aLine.append( " 0 obj\n"
    3909           0 :                              "<</Length " );
    3910           0 :                 aLine.append( (sal_Int32)nStreamLengthObject );
    3911             :                 aLine.append( " 0 R"
    3912             :                              #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3913             :                              "/Filter/FlateDecode"
    3914             :                              #endif
    3915           0 :                              "/Length1 " );
    3916             : 
    3917           0 :                 sal_uInt64 nStartPos = 0;
    3918           0 :                 if( aSubsetInfo.m_nFontType == FontSubsetInfo::SFNT_TTF )
    3919             :                 {
    3920           0 :                     aLine.append( (sal_Int32)nLength1 );
    3921             : 
    3922             :                     aLine.append( ">>\n"
    3923           0 :                                  "stream\n" );
    3924           0 :                     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    3925           0 :                     if ( osl_File_E_None != osl_getFilePos( m_aFile, &nStartPos ) ) return false;
    3926             : 
    3927             :                     // copy font file
    3928           0 :                     beginCompression();
    3929           0 :                     checkAndEnableStreamEncryption( nFontStream );
    3930           0 :                     sal_Bool bEOF = sal_False;
    3931           0 :                     do
    3932             :                     {
    3933             :                         char buf[8192];
    3934             :                         sal_uInt64 nRead;
    3935           0 :                         if ( osl_File_E_None != osl_readFile( aFontFile, buf, sizeof( buf ), &nRead ) ) return false;
    3936           0 :                         if ( !writeBuffer( buf, nRead ) ) return false;
    3937           0 :                         if ( osl_File_E_None != osl_isEndOfFile( aFontFile, &bEOF ) ) return false;
    3938           0 :                     } while( ! bEOF );
    3939             :                 }
    3940           0 :                 else if( (aSubsetInfo.m_nFontType & FontSubsetInfo::CFF_FONT) != 0 )
    3941             :                 {
    3942             :                     // TODO: implement
    3943             :                     OSL_FAIL( "PDFWriterImpl does not support CFF-font subsets yet!" );
    3944             :                 }
    3945           0 :                 else if( (aSubsetInfo.m_nFontType & FontSubsetInfo::TYPE1_PFB) != 0 ) // TODO: also support PFA?
    3946             :                 {
    3947           0 :                     boost::shared_array<unsigned char> pBuffer( new unsigned char[ nLength1 ] );
    3948             : 
    3949           0 :                     sal_uInt64 nBytesRead = 0;
    3950           0 :                     if ( osl_File_E_None != osl_readFile( aFontFile, pBuffer.get(), nLength1, &nBytesRead ) ) return false;
    3951             :                     DBG_ASSERT( nBytesRead==nLength1, "PDF-FontSubset read incomplete!" );
    3952           0 :                     if ( osl_File_E_None != osl_setFilePos( aFontFile, osl_Pos_Absolut, 0 ) ) return false;
    3953             :                     // get the PFB-segment lengths
    3954           0 :                     ThreeInts aSegmentLengths = {0,0,0};
    3955           0 :                     getPfbSegmentLengths( pBuffer.get(), (int)nBytesRead, aSegmentLengths );
    3956             :                     // the lengths below are mandatory for PDF-exported Type1 fonts
    3957             :                     // because the PFB segment headers get stripped! WhyOhWhy.
    3958           0 :                     aLine.append( (sal_Int32)aSegmentLengths[0] );
    3959           0 :                     aLine.append( "/Length2 " );
    3960           0 :                     aLine.append( (sal_Int32)aSegmentLengths[1] );
    3961           0 :                     aLine.append( "/Length3 " );
    3962           0 :                     aLine.append( (sal_Int32)aSegmentLengths[2] );
    3963             : 
    3964             :                     aLine.append( ">>\n"
    3965           0 :                                  "stream\n" );
    3966           0 :                     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    3967           0 :                     if ( osl_File_E_None != osl_getFilePos( m_aFile, &nStartPos ) ) return false;
    3968             : 
    3969             :                     // emit PFB-sections without section headers
    3970           0 :                     beginCompression();
    3971           0 :                     checkAndEnableStreamEncryption( nFontStream );
    3972           0 :                     if ( !writeBuffer( &pBuffer[6], aSegmentLengths[0] ) ) return false;
    3973           0 :                     if ( !writeBuffer( &pBuffer[12] + aSegmentLengths[0], aSegmentLengths[1] ) ) return false;
    3974           0 :                     if ( !writeBuffer( &pBuffer[18] + aSegmentLengths[0] + aSegmentLengths[1], aSegmentLengths[2] ) ) return false;
    3975             :                 }
    3976             :                 else
    3977             :                 {
    3978           0 :                     fprintf( stderr, "PDF: CreateFontSubset result in not yet supported format=%d\n",aSubsetInfo.m_nFontType);
    3979           0 :                     aLine.append( "0 >>\nstream\n" );
    3980             :                 }
    3981             : 
    3982           0 :                 endCompression();
    3983           0 :                 disableStreamEncryption();
    3984             :                 // close the file
    3985           0 :                 osl_closeFile( aFontFile );
    3986             : 
    3987           0 :                 sal_uInt64 nEndPos = 0;
    3988           0 :                 if ( osl_File_E_None != osl_getFilePos( m_aFile, &nEndPos ) ) return false;
    3989             :                 // end the stream
    3990           0 :                 aLine.setLength( 0 );
    3991           0 :                 aLine.append( "\nendstream\nendobj\n\n" );
    3992           0 :                 if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    3993             : 
    3994             :                 // emit stream length object
    3995           0 :                 if ( !updateObject( nStreamLengthObject ) ) return false;
    3996           0 :                 aLine.setLength( 0 );
    3997           0 :                 aLine.append( nStreamLengthObject );
    3998           0 :                 aLine.append( " 0 obj\n" );
    3999           0 :                 aLine.append( (sal_Int64)(nEndPos-nStartPos) );
    4000           0 :                 aLine.append( "\nendobj\n\n" );
    4001           0 :                 if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    4002             : 
    4003             :                 // write font descriptor
    4004           0 :                 sal_Int32 nFontDescriptor = emitFontDescriptor( it->first, aSubsetInfo, lit->m_nFontID, nFontStream );
    4005             : 
    4006           0 :                 if( nToUnicodeStream )
    4007           0 :                     nToUnicodeStream = createToUnicodeCMap( pEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, nGlyphs );
    4008             : 
    4009           0 :                 sal_Int32 nFontObject = createObject();
    4010           0 :                 if ( !updateObject( nFontObject ) ) return false;
    4011           0 :                 aLine.setLength( 0 );
    4012           0 :                 aLine.append( nFontObject );
    4013             : 
    4014           0 :                 aLine.append( " 0 obj\n" );
    4015           0 :                 aLine.append( ((aSubsetInfo.m_nFontType & FontSubsetInfo::ANY_TYPE1) != 0) ?
    4016             :                              "<</Type/Font/Subtype/Type1/BaseFont/" :
    4017           0 :                              "<</Type/Font/Subtype/TrueType/BaseFont/" );
    4018           0 :                 appendSubsetName( lit->m_nFontID, aSubsetInfo.m_aPSName, aLine );
    4019             :                 aLine.append( "\n"
    4020             :                              "/FirstChar 0\n"
    4021           0 :                              "/LastChar " );
    4022           0 :                 aLine.append( (sal_Int32)(nGlyphs-1) );
    4023             :                 aLine.append( "\n"
    4024           0 :                              "/Widths[" );
    4025           0 :                 for( int i = 0; i < nGlyphs; i++ )
    4026             :                 {
    4027           0 :                     aLine.append( pWidths[ i ] );
    4028           0 :                     aLine.append( ((i & 15) == 15) ? "\n" : " " );
    4029             :                 }
    4030             :                 aLine.append( "]\n"
    4031           0 :                              "/FontDescriptor " );
    4032           0 :                 aLine.append( nFontDescriptor );
    4033           0 :                 aLine.append( " 0 R\n" );
    4034           0 :                 if( nToUnicodeStream )
    4035             :                 {
    4036           0 :                     aLine.append( "/ToUnicode " );
    4037           0 :                     aLine.append( nToUnicodeStream );
    4038           0 :                     aLine.append( " 0 R\n" );
    4039             :                 }
    4040             :                 aLine.append( ">>\n"
    4041           0 :                              "endobj\n\n" );
    4042           0 :                 if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    4043             : 
    4044           0 :                 aFontIDToObject[ lit->m_nFontID ] = nFontObject;
    4045             :             }
    4046             :             else
    4047             :             {
    4048           0 :                 const PhysicalFontFace* pFont = it->first;
    4049           0 :                 OStringBuffer aErrorComment( 256 );
    4050           0 :                 aErrorComment.append( "CreateFontSubset failed for font \"" );
    4051           0 :                 aErrorComment.append( OUStringToOString( pFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ) );
    4052           0 :                 aErrorComment.append( '\"' );
    4053           0 :                 if( pFont->GetSlant() == ITALIC_NORMAL )
    4054           0 :                     aErrorComment.append( " italic" );
    4055           0 :                 else if( pFont->GetSlant() == ITALIC_OBLIQUE )
    4056           0 :                     aErrorComment.append( " oblique" );
    4057           0 :                 aErrorComment.append( " weight=" );
    4058           0 :                 aErrorComment.append( sal_Int32(pFont->GetWeight()) );
    4059           0 :                 emitComment( aErrorComment.getStr() );
    4060             :             }
    4061           0 :         }
    4062             :     }
    4063           0 :     osl_removeFile( aTmpName.pData );
    4064             : 
    4065             :     // emit embedded fonts
    4066           0 :     for( FontEmbedData::iterator eit = m_aEmbeddedFonts.begin(); eit != m_aEmbeddedFonts.end(); ++eit )
    4067             :     {
    4068           0 :         std::map< sal_Int32, sal_Int32 > aObjects = emitEmbeddedFont( eit->first, eit->second );
    4069           0 :         for( std::map< sal_Int32, sal_Int32 >::iterator fit = aObjects.begin(); fit != aObjects.end(); ++fit )
    4070             :         {
    4071           0 :             if ( !fit->second ) return false;
    4072           0 :             aFontIDToObject[ fit->first ] = fit->second;
    4073             :         }
    4074           0 :     }
    4075             : 
    4076             :     // emit system fonts
    4077           0 :     for( FontEmbedData::iterator sit = m_aSystemFonts.begin(); sit != m_aSystemFonts.end(); ++sit )
    4078             :     {
    4079           0 :         std::map< sal_Int32, sal_Int32 > aObjects = emitSystemFont( sit->first, sit->second );
    4080           0 :         for( std::map< sal_Int32, sal_Int32 >::iterator fit = aObjects.begin(); fit != aObjects.end(); ++fit )
    4081             :         {
    4082           0 :             if ( !fit->second ) return false;
    4083           0 :             aFontIDToObject[ fit->first ] = fit->second;
    4084             :         }
    4085           0 :     }
    4086             : 
    4087           0 :     OStringBuffer aFontDict( 1024 );
    4088           0 :     aFontDict.append( getFontDictObject() );
    4089             :     aFontDict.append( " 0 obj\n"
    4090           0 :                      "<<" );
    4091           0 :     int ni = 0;
    4092           0 :     for( std::map< sal_Int32, sal_Int32 >::iterator mit = aFontIDToObject.begin(); mit != aFontIDToObject.end(); ++mit )
    4093             :     {
    4094           0 :         aFontDict.append( "/F" );
    4095           0 :         aFontDict.append( mit->first );
    4096           0 :         aFontDict.append( ' ' );
    4097           0 :         aFontDict.append( mit->second );
    4098           0 :         aFontDict.append( " 0 R" );
    4099           0 :         if( ((++ni) & 7) == 0 )
    4100           0 :             aFontDict.append( '\n' );
    4101             :     }
    4102             :     // emit builtin font for widget apperances / variable text
    4103           0 :     for( std::map< sal_Int32, sal_Int32 >::iterator it = m_aBuiltinFontToObjectMap.begin();
    4104           0 :         it != m_aBuiltinFontToObjectMap.end(); ++it )
    4105             :     {
    4106           0 :         ImplPdfBuiltinFontData aData(m_aBuiltinFonts[it->first]);
    4107           0 :         it->second = emitBuiltinFont( &aData, it->second );
    4108           0 :     }
    4109           0 :     appendBuiltinFontsToDict( aFontDict );
    4110           0 :     aFontDict.append( "\n>>\nendobj\n\n" );
    4111             : 
    4112           0 :     if ( !updateObject( getFontDictObject() ) ) return false;
    4113           0 :     if ( !writeBuffer( aFontDict.getStr(), aFontDict.getLength() ) ) return false;
    4114           0 :     return true;
    4115             : }
    4116             : 
    4117           0 : sal_Int32 PDFWriterImpl::emitResources()
    4118             : {
    4119             :     // emit shadings
    4120           0 :     if( ! m_aGradients.empty() )
    4121           0 :         CHECK_RETURN( emitGradients() );
    4122             :     // emit tilings
    4123           0 :     if( ! m_aTilings.empty() )
    4124           0 :         CHECK_RETURN( emitTilings() );
    4125             : 
    4126             :     // emit font dict
    4127           0 :     CHECK_RETURN( emitFonts() );
    4128             : 
    4129             :     // emit Resource dict
    4130           0 :     OStringBuffer aLine( 512 );
    4131           0 :     sal_Int32 nResourceDict = getResourceDictObj();
    4132           0 :     CHECK_RETURN( updateObject( nResourceDict ) );
    4133           0 :     aLine.setLength( 0 );
    4134           0 :     aLine.append( nResourceDict );
    4135           0 :     aLine.append( " 0 obj\n" );
    4136           0 :     m_aGlobalResourceDict.append( aLine, getFontDictObject() );
    4137           0 :     aLine.append( "endobj\n\n" );
    4138           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4139           0 :     return nResourceDict;
    4140             : }
    4141             : 
    4142           0 : sal_Int32 PDFWriterImpl::updateOutlineItemCount( std::vector< sal_Int32 >& rCounts,
    4143             :                                                  sal_Int32 nItemLevel,
    4144             :                                                  sal_Int32 nCurrentItemId )
    4145             : {
    4146             :     /* The /Count number of an item is
    4147             :        positive: the number of visible subitems
    4148             :        negative: the negative number of subitems that will become visible if
    4149             :                  the item gets opened
    4150             :        see PDF ref 1.4 p 478
    4151             :     */
    4152             : 
    4153           0 :     sal_Int32 nCount = 0;
    4154             : 
    4155           0 :     if( m_aContext.OpenBookmarkLevels < 0           || // all levels arevisible
    4156           0 :         m_aContext.OpenBookmarkLevels >= nItemLevel    // this level is visible
    4157             :       )
    4158             :     {
    4159           0 :         PDFOutlineEntry& rItem = m_aOutline[ nCurrentItemId ];
    4160           0 :         sal_Int32 nChildren = rItem.m_aChildren.size();
    4161           0 :         for( sal_Int32 i = 0; i < nChildren; i++ )
    4162           0 :             nCount += updateOutlineItemCount( rCounts, nItemLevel+1, rItem.m_aChildren[i] );
    4163           0 :         rCounts[nCurrentItemId] = nCount;
    4164             :         // return 1 (this item) + visible sub items
    4165           0 :         if( nCount < 0 )
    4166           0 :             nCount = 0;
    4167           0 :         nCount++;
    4168             :     }
    4169             :     else
    4170             :     {
    4171             :         // this bookmark level is invisible
    4172           0 :         PDFOutlineEntry& rItem = m_aOutline[ nCurrentItemId ];
    4173           0 :         sal_Int32 nChildren = rItem.m_aChildren.size();
    4174           0 :         rCounts[ nCurrentItemId ] = -sal_Int32(rItem.m_aChildren.size());
    4175           0 :         for( sal_Int32 i = 0; i < nChildren; i++ )
    4176           0 :             updateOutlineItemCount( rCounts, nItemLevel+1, rItem.m_aChildren[i] );
    4177           0 :         nCount = -1;
    4178             :     }
    4179             : 
    4180           0 :     return nCount;
    4181             : }
    4182             : 
    4183           0 : sal_Int32 PDFWriterImpl::emitOutline()
    4184             : {
    4185           0 :     int i, nItems = m_aOutline.size();
    4186             : 
    4187             :     // do we have an outline at all ?
    4188           0 :     if( nItems < 2 )
    4189           0 :         return 0;
    4190             : 
    4191             :     // reserve object numbers for all outline items
    4192           0 :     for( i = 0; i < nItems; ++i )
    4193           0 :         m_aOutline[i].m_nObject = createObject();
    4194             : 
    4195             :     // update all parent, next and prev object ids
    4196           0 :     for( i = 0; i < nItems; ++i )
    4197             :     {
    4198           0 :         PDFOutlineEntry& rItem = m_aOutline[i];
    4199           0 :         int nChildren = rItem.m_aChildren.size();
    4200             : 
    4201           0 :         if( nChildren )
    4202             :         {
    4203           0 :             for( int n = 0; n < nChildren; ++n )
    4204             :             {
    4205           0 :                 PDFOutlineEntry& rChild = m_aOutline[ rItem.m_aChildren[n] ];
    4206             : 
    4207           0 :                 rChild.m_nParentObject = rItem.m_nObject;
    4208           0 :                 rChild.m_nPrevObject = (n > 0) ? m_aOutline[ rItem.m_aChildren[n-1] ].m_nObject : 0;
    4209           0 :                 rChild.m_nNextObject = (n < nChildren-1) ? m_aOutline[ rItem.m_aChildren[n+1] ].m_nObject : 0;
    4210             :             }
    4211             : 
    4212             :         }
    4213             :     }
    4214             : 
    4215             :     // calculate Count entries for all items
    4216           0 :     std::vector< sal_Int32 > aCounts( nItems );
    4217           0 :     updateOutlineItemCount( aCounts, 0, 0 );
    4218             : 
    4219             :     // emit hierarchy
    4220           0 :     for( i = 0; i < nItems; ++i )
    4221             :     {
    4222           0 :         PDFOutlineEntry& rItem = m_aOutline[i];
    4223           0 :         OStringBuffer aLine( 1024 );
    4224             : 
    4225           0 :         CHECK_RETURN( updateObject( rItem.m_nObject ) );
    4226           0 :         aLine.append( rItem.m_nObject );
    4227           0 :         aLine.append( " 0 obj\n" );
    4228           0 :         aLine.append( "<<" );
    4229             :         // number of visible children (all levels)
    4230           0 :         if( i > 0 || aCounts[0] > 0 )
    4231             :         {
    4232           0 :             aLine.append( "/Count " );
    4233           0 :             aLine.append( aCounts[i] );
    4234             :         }
    4235           0 :         if( ! rItem.m_aChildren.empty() )
    4236             :         {
    4237             :             // children list: First, Last
    4238           0 :             aLine.append( "/First " );
    4239           0 :             aLine.append( m_aOutline[rItem.m_aChildren.front()].m_nObject );
    4240           0 :             aLine.append( " 0 R/Last " );
    4241           0 :             aLine.append( m_aOutline[rItem.m_aChildren.back()].m_nObject );
    4242           0 :             aLine.append( " 0 R\n" );
    4243             :         }
    4244           0 :         if( i > 0 )
    4245             :         {
    4246             :             // Title, Dest, Parent, Prev, Next
    4247           0 :             aLine.append( "/Title" );
    4248           0 :             appendUnicodeTextStringEncrypt( rItem.m_aTitle, rItem.m_nObject, aLine );
    4249           0 :             aLine.append( "\n" );
    4250             :             // Dest is not required
    4251           0 :             if( rItem.m_nDestID >= 0 && rItem.m_nDestID < (sal_Int32)m_aDests.size() )
    4252             :             {
    4253           0 :                 aLine.append( "/Dest" );
    4254           0 :                 appendDest( rItem.m_nDestID, aLine );
    4255             :             }
    4256           0 :             aLine.append( "/Parent " );
    4257           0 :             aLine.append( rItem.m_nParentObject );
    4258           0 :             aLine.append( " 0 R" );
    4259           0 :             if( rItem.m_nPrevObject )
    4260             :             {
    4261           0 :                 aLine.append( "/Prev " );
    4262           0 :                 aLine.append( rItem.m_nPrevObject );
    4263           0 :                 aLine.append( " 0 R" );
    4264             :             }
    4265           0 :             if( rItem.m_nNextObject )
    4266             :             {
    4267           0 :                 aLine.append( "/Next " );
    4268           0 :                 aLine.append( rItem.m_nNextObject );
    4269           0 :                 aLine.append( " 0 R" );
    4270             :             }
    4271             :         }
    4272           0 :         aLine.append( ">>\nendobj\n\n" );
    4273           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4274           0 :     }
    4275             : 
    4276           0 :     return m_aOutline[0].m_nObject;
    4277             : }
    4278             : 
    4279             : #undef CHECK_RETURN
    4280             : #define CHECK_RETURN( x ) if( !x ) return false
    4281             : 
    4282           0 : bool PDFWriterImpl::appendDest( sal_Int32 nDestID, OStringBuffer& rBuffer )
    4283             : {
    4284           0 :     if( nDestID < 0 || nDestID >= (sal_Int32)m_aDests.size() )
    4285             :     {
    4286             : #if OSL_DEBUG_LEVEL > 1
    4287             :         fprintf( stderr, "ERROR: invalid dest %d requested\n", (int)nDestID );
    4288             : #endif
    4289           0 :         return false;
    4290             :     }
    4291             : 
    4292           0 :     const PDFDest& rDest        = m_aDests[ nDestID ];
    4293           0 :     const PDFPage& rDestPage    = m_aPages[ rDest.m_nPage ];
    4294             : 
    4295           0 :     rBuffer.append( '[' );
    4296           0 :     rBuffer.append( rDestPage.m_nPageObject );
    4297           0 :     rBuffer.append( " 0 R" );
    4298             : 
    4299           0 :     switch( rDest.m_eType )
    4300             :     {
    4301             :         case PDFWriter::XYZ:
    4302             :         default:
    4303           0 :             rBuffer.append( "/XYZ " );
    4304           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4305           0 :             rBuffer.append( ' ' );
    4306           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4307           0 :             rBuffer.append( " 0" );
    4308           0 :             break;
    4309             :         case PDFWriter::Fit:
    4310           0 :             rBuffer.append( "/Fit" );
    4311           0 :             break;
    4312             :         case PDFWriter::FitRectangle:
    4313           0 :             rBuffer.append( "/FitR " );
    4314           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4315           0 :             rBuffer.append( ' ' );
    4316           0 :             appendFixedInt( rDest.m_aRect.Top(), rBuffer );
    4317           0 :             rBuffer.append( ' ' );
    4318           0 :             appendFixedInt( rDest.m_aRect.Right(), rBuffer );
    4319           0 :             rBuffer.append( ' ' );
    4320           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4321           0 :             break;
    4322             :         case PDFWriter::FitHorizontal:
    4323           0 :             rBuffer.append( "/FitH " );
    4324           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4325           0 :             break;
    4326             :         case PDFWriter::FitVertical:
    4327           0 :             rBuffer.append( "/FitV " );
    4328           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4329           0 :             break;
    4330             :         case PDFWriter::FitPageBoundingBox:
    4331           0 :             rBuffer.append( "/FitB" );
    4332           0 :             break;
    4333             :         case PDFWriter::FitPageBoundingBoxHorizontal:
    4334           0 :             rBuffer.append( "/FitBH " );
    4335           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4336           0 :             break;
    4337             :         case PDFWriter::FitPageBoundingBoxVertical:
    4338           0 :             rBuffer.append( "/FitBV " );
    4339           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4340           0 :             break;
    4341             :     }
    4342           0 :     rBuffer.append( ']' );
    4343             : 
    4344           0 :     return true;
    4345             : }
    4346             : 
    4347           0 : bool PDFWriterImpl::emitLinkAnnotations()
    4348             : {
    4349           0 :     int nAnnots = m_aLinks.size();
    4350           0 :     for( int i = 0; i < nAnnots; i++ )
    4351             :     {
    4352           0 :         const PDFLink& rLink            = m_aLinks[i];
    4353           0 :         if( ! updateObject( rLink.m_nObject ) )
    4354           0 :             continue;
    4355             : 
    4356           0 :         OStringBuffer aLine( 1024 );
    4357           0 :         aLine.append( rLink.m_nObject );
    4358           0 :         aLine.append( " 0 obj\n" );
    4359             : //i59651  key /F set bits Print to 1 rest to 0. We don't set NoZoom NoRotate to 1, since it's a 'should'
    4360             : // see PDF 8.4.2 and ISO 19005-1:2005 6.5.3
    4361           0 :         aLine.append( "<</Type/Annot" );
    4362           0 :         if( m_bIsPDF_A1 )
    4363           0 :             aLine.append( "/F 4" );
    4364           0 :         aLine.append( "/Subtype/Link/Border[0 0 0]/Rect[" );
    4365             : 
    4366           0 :         appendFixedInt( rLink.m_aRect.Left()-7, aLine );//the +7 to have a better shape of the border rectangle
    4367           0 :         aLine.append( ' ' );
    4368           0 :         appendFixedInt( rLink.m_aRect.Top(), aLine );
    4369           0 :         aLine.append( ' ' );
    4370           0 :         appendFixedInt( rLink.m_aRect.Right()+7, aLine );//the +7 to have a better shape of the border rectangle
    4371           0 :         aLine.append( ' ' );
    4372           0 :         appendFixedInt( rLink.m_aRect.Bottom(), aLine );
    4373           0 :         aLine.append( "]" );
    4374           0 :         if( rLink.m_nDest >= 0 )
    4375             :         {
    4376           0 :             aLine.append( "/Dest" );
    4377           0 :             appendDest( rLink.m_nDest, aLine );
    4378             :         }
    4379             :         else
    4380             :         {
    4381             : /*--->i56629
    4382             : destination is external to the document, so
    4383             : we check in the following sequence:
    4384             : 
    4385             :  if target type is neither .pdf, nor .od[tpgs], then
    4386             :           check if relative or absolute and act accordingly (use URI or 'launch application' as requested)
    4387             :                              end processing
    4388             :  else if target is .od[tpgs]: then
    4389             :       if conversion of type from od[tpgs]  to pdf is requested, convert it and this becomes the new target file
    4390             :       processing continue
    4391             : 
    4392             :  if (new)target is .pdf : then
    4393             :      if GotToR is requested, then
    4394             :            convert the target in GoToR where the fragment of the URI is
    4395             :            considered the named destination in the target file, set relative or absolute as requested
    4396             :      else strip the fragment from URL and then set URI or 'launch application' as requested
    4397             : */
    4398             : 
    4399             : // FIXME: check if the decode mechanisms for URL processing throughout this implementation
    4400             : // are the correct one!!
    4401             : 
    4402             : // extract target file type
    4403           0 :             INetURLObject aDocumentURL( m_aContext.BaseURL );
    4404           0 :             INetURLObject aTargetURL( rLink.m_aURL );
    4405           0 :             sal_Int32   nSetGoToRMode = 0;
    4406           0 :             bool    bTargetHasPDFExtension = false;
    4407           0 :             INetProtocol eTargetProtocol = aTargetURL.GetProtocol();
    4408           0 :             bool    bIsUNCPath = false;
    4409             : 
    4410             :             // check if the protocol is a known one, or if there is no protocol at all (on target only)
    4411             :             // if there is no protocol, make the target relative to the current document directory
    4412             :             // getting the needed URL information from the current document path
    4413           0 :             if( eTargetProtocol == INET_PROT_NOT_VALID )
    4414             :             {
    4415           0 :                 if( rLink.m_aURL.getLength() > 4 && rLink.m_aURL.startsWith("\\\\\\\\"))
    4416             :                 {
    4417           0 :                     bIsUNCPath = true;
    4418             :                 }
    4419             :                 else
    4420             :                 {
    4421           0 :                     INetURLObject aNewBase( aDocumentURL );//duplicate document URL
    4422           0 :                     aNewBase.removeSegment(); //remove last segment from it, obtaining the base URL of the
    4423             :                                               //target document
    4424           0 :                     aNewBase.insertName( rLink.m_aURL );
    4425           0 :                     aTargetURL = aNewBase;//reassign the new target URL
    4426             :                     //recompute the target protocol, with the new URL
    4427             :                     //normal URL processing resumes
    4428           0 :                     eTargetProtocol = aTargetURL.GetProtocol();
    4429             :                 }
    4430             :             }
    4431             : 
    4432           0 :             OUString aFileExtension = aTargetURL.GetFileExtension();
    4433             : 
    4434             :             // Check if the URL ends in '/': if yes it's a directory,
    4435             :             // it will be forced to a URI link.
    4436             :             // possibly a malformed URI, leave it as it is, force as URI
    4437           0 :             if( aTargetURL.hasFinalSlash() )
    4438           0 :                 m_aContext.DefaultLinkAction = PDFWriter::URIAction;
    4439             : 
    4440           0 :             if( !aFileExtension.isEmpty() )
    4441             :             {
    4442           0 :                 if( m_aContext.ConvertOOoTargetToPDFTarget )
    4443             :                 {
    4444           0 :                     bool bChangeFileExtensionToPDF = false;
    4445             :                     //examine the file type (.odm .odt. .odp, odg, ods)
    4446           0 :                     if( aFileExtension.equalsIgnoreAsciiCase( "odm" ) )
    4447           0 :                         bChangeFileExtensionToPDF = true;
    4448           0 :                     if( aFileExtension.equalsIgnoreAsciiCase( "odt" ) )
    4449           0 :                         bChangeFileExtensionToPDF = true;
    4450           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase( "odp" ) )
    4451           0 :                         bChangeFileExtensionToPDF = true;
    4452           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase( "odg" ) )
    4453           0 :                         bChangeFileExtensionToPDF = true;
    4454           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase( "ods" ) )
    4455           0 :                         bChangeFileExtensionToPDF = true;
    4456           0 :                     if( bChangeFileExtensionToPDF )
    4457           0 :                         aTargetURL.setExtension(OUString( "pdf"  ) );
    4458             :                 }
    4459             :                 //check if extension is pdf, see if GoToR should be forced
    4460           0 :                 bTargetHasPDFExtension = aTargetURL.GetFileExtension().equalsIgnoreAsciiCase( "pdf" );
    4461           0 :                 if( m_aContext.ForcePDFAction && bTargetHasPDFExtension )
    4462           0 :                     nSetGoToRMode++;
    4463             :             }
    4464             :             //prepare the URL, if relative or not
    4465           0 :             INetProtocol eBaseProtocol = aDocumentURL.GetProtocol();
    4466             :             //queue the string common to all types of actions
    4467           0 :             aLine.append( "/A<</Type/Action/S");
    4468           0 :             if( bIsUNCPath ) // handle Win UNC paths
    4469             :             {
    4470           0 :                 aLine.append( "/Launch/Win<</F" );
    4471             :                 // INetURLObject is not good with UNC paths, use original path
    4472           0 :                 appendLiteralStringEncrypt(  rLink.m_aURL, rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4473           0 :                 aLine.append( ">>" );
    4474             :             }
    4475             :             else
    4476             :             {
    4477           0 :                 bool bSetRelative = false;
    4478           0 :                 bool bFileSpec = false;
    4479             :                 //check if relative file link is requested and if the protocol is 'file://'
    4480           0 :                 if( m_aContext.RelFsys && eBaseProtocol == eTargetProtocol && eTargetProtocol == INET_PROT_FILE )
    4481           0 :                     bSetRelative = true;
    4482             : 
    4483           0 :                 OUString aFragment = aTargetURL.GetMark( INetURLObject::NO_DECODE /*DECODE_WITH_CHARSET*/ ); //fragment as is,
    4484           0 :                 if( nSetGoToRMode == 0 )
    4485             :                 {
    4486           0 :                     switch( m_aContext.DefaultLinkAction )
    4487             :                     {
    4488             :                     default:
    4489             :                     case PDFWriter::URIAction :
    4490             :                     case PDFWriter::URIActionDestination :
    4491           0 :                         aLine.append( "/URI/URI" );
    4492           0 :                         break;
    4493             :                     case PDFWriter::LaunchAction:
    4494             :                         // now:
    4495             :                         // if a launch action is requested and the hyperlink target has a fragment
    4496             :                         // and the target file does not have a pdf extension, or it's not a 'file:://'
    4497             :                         // protocol then force the uri action on it
    4498             :                         // This code will permit the correct opening of application on web pages,
    4499             :                         // the one that normally have fragments (but I may be wrong...)
    4500             :                         // and will force the use of URI when the protocol is not file:
    4501           0 :                         if( (!aFragment.isEmpty() && !bTargetHasPDFExtension) ||
    4502             :                                         eTargetProtocol != INET_PROT_FILE )
    4503             :                         {
    4504           0 :                             aLine.append( "/URI/URI" );
    4505             :                         }
    4506             :                         else
    4507             :                         {
    4508           0 :                             aLine.append( "/Launch/F" );
    4509           0 :                             bFileSpec = true;
    4510             :                         }
    4511           0 :                         break;
    4512             :                     }
    4513             :                 }
    4514             : 
    4515             :                 //fragment are encoded in the same way as in the named destination processing
    4516           0 :                 if( nSetGoToRMode )
    4517             :                 {
    4518             :                     //add the fragment
    4519           0 :                     OUString aURLNoMark = aTargetURL.GetURLNoMark( INetURLObject::DECODE_WITH_CHARSET );
    4520           0 :                     aLine.append("/GoToR");
    4521           0 :                     aLine.append("/F");
    4522           0 :                     bFileSpec = true;
    4523             :                     appendLiteralStringEncrypt( bSetRelative ? INetURLObject::GetRelURL( m_aContext.BaseURL, aURLNoMark,
    4524             :                                                                                          INetURLObject::WAS_ENCODED,
    4525             :                                                                                          INetURLObject::DECODE_WITH_CHARSET ) :
    4526           0 :                                                                    aURLNoMark, rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4527           0 :                     if( !aFragment.isEmpty() )
    4528             :                     {
    4529           0 :                         aLine.append("/D/");
    4530           0 :                         appendDestinationName( aFragment , aLine );
    4531           0 :                     }
    4532             :                 }
    4533             :                 else
    4534             :                 {
    4535             :                     // change the fragment to accommodate the bookmark (only if the file extension
    4536             :                     // is PDF and the requested action is of the correct type)
    4537           0 :                     if(m_aContext.DefaultLinkAction == PDFWriter::URIActionDestination &&
    4538           0 :                                bTargetHasPDFExtension && !aFragment.isEmpty() )
    4539             :                     {
    4540           0 :                         OStringBuffer aLineLoc( 1024 );
    4541           0 :                         appendDestinationName( aFragment , aLineLoc );
    4542             :                         //substitute the fragment
    4543           0 :                         aTargetURL.SetMark( OStringToOUString(aLineLoc.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US) );
    4544             :                     }
    4545           0 :                     OUString aURL = aTargetURL.GetMainURL( bFileSpec ? INetURLObject::DECODE_WITH_CHARSET : INetURLObject::NO_DECODE );
    4546             :                     appendLiteralStringEncrypt(bSetRelative ? INetURLObject::GetRelURL( m_aContext.BaseURL, aURL,
    4547             :                                                                                         INetURLObject::WAS_ENCODED,
    4548             :                                                                                             bFileSpec ? INetURLObject::DECODE_WITH_CHARSET : INetURLObject::NO_DECODE
    4549             :                                                                                             ) :
    4550           0 :                                                                                aURL , rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4551           0 :                 }
    4552             : //<--- i56629
    4553             :             }
    4554           0 :             aLine.append( ">>\n" );
    4555             :         }
    4556           0 :         if( rLink.m_nStructParent > 0 )
    4557             :         {
    4558           0 :             aLine.append( "/StructParent " );
    4559           0 :             aLine.append( rLink.m_nStructParent );
    4560             :         }
    4561           0 :         aLine.append( ">>\nendobj\n\n" );
    4562           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4563           0 :     }
    4564             : 
    4565           0 :     return true;
    4566             : }
    4567             : 
    4568           0 : bool PDFWriterImpl::emitNoteAnnotations()
    4569             : {
    4570             :     // emit note annotations
    4571           0 :     int nAnnots = m_aNotes.size();
    4572           0 :     for( int i = 0; i < nAnnots; i++ )
    4573             :     {
    4574           0 :         const PDFNoteEntry& rNote       = m_aNotes[i];
    4575           0 :         if( ! updateObject( rNote.m_nObject ) )
    4576           0 :             return false;
    4577             : 
    4578           0 :         OStringBuffer aLine( 1024 );
    4579           0 :         aLine.append( rNote.m_nObject );
    4580           0 :         aLine.append( " 0 obj\n" );
    4581             : //i59651  key /F set bits Print to 1 rest to 0. We don't set NoZoom NoRotate to 1, since it's a 'should'
    4582             : // see PDF 8.4.2 and ISO 19005-1:2005 6.5.3
    4583           0 :         aLine.append( "<</Type/Annot" );
    4584           0 :         if( m_bIsPDF_A1 )
    4585           0 :             aLine.append( "/F 4" );
    4586           0 :         aLine.append( "/Subtype/Text/Rect[" );
    4587             : 
    4588           0 :         appendFixedInt( rNote.m_aRect.Left(), aLine );
    4589           0 :         aLine.append( ' ' );
    4590           0 :         appendFixedInt( rNote.m_aRect.Top(), aLine );
    4591           0 :         aLine.append( ' ' );
    4592           0 :         appendFixedInt( rNote.m_aRect.Right(), aLine );
    4593           0 :         aLine.append( ' ' );
    4594           0 :         appendFixedInt( rNote.m_aRect.Bottom(), aLine );
    4595           0 :         aLine.append( "]" );
    4596             : 
    4597             :         // contents of the note (type text string)
    4598           0 :         aLine.append( "/Contents\n" );
    4599           0 :         appendUnicodeTextStringEncrypt( rNote.m_aContents.Contents, rNote.m_nObject, aLine );
    4600           0 :         aLine.append( "\n" );
    4601             : 
    4602             :         // optional title
    4603           0 :         if( !rNote.m_aContents.Title.isEmpty() )
    4604             :         {
    4605           0 :             aLine.append( "/T" );
    4606           0 :             appendUnicodeTextStringEncrypt( rNote.m_aContents.Title, rNote.m_nObject, aLine );
    4607           0 :             aLine.append( "\n" );
    4608             :         }
    4609             : 
    4610           0 :         aLine.append( ">>\nendobj\n\n" );
    4611           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4612           0 :     }
    4613           0 :     return true;
    4614             : }
    4615             : 
    4616           0 : Font PDFWriterImpl::replaceFont( const Font& rControlFont, const Font&  rAppSetFont )
    4617             : {
    4618           0 :     bool bAdjustSize = false;
    4619             : 
    4620           0 :     Font aFont( rControlFont );
    4621           0 :     if( aFont.GetName().isEmpty() )
    4622             :     {
    4623           0 :         aFont = rAppSetFont;
    4624           0 :         if( rControlFont.GetHeight() )
    4625           0 :             aFont.SetSize( Size( 0, rControlFont.GetHeight() ) );
    4626             :         else
    4627           0 :             bAdjustSize = true;
    4628           0 :         if( rControlFont.GetItalic() != ITALIC_DONTKNOW )
    4629           0 :             aFont.SetItalic( rControlFont.GetItalic() );
    4630           0 :         if( rControlFont.GetWeight() != WEIGHT_DONTKNOW )
    4631           0 :             aFont.SetWeight( rControlFont.GetWeight() );
    4632             :     }
    4633           0 :     else if( ! aFont.GetHeight() )
    4634             :     {
    4635           0 :         aFont.SetSize( rAppSetFont.GetSize() );
    4636           0 :         bAdjustSize = true;
    4637             :     }
    4638           0 :     if( bAdjustSize )
    4639             :     {
    4640           0 :         Size aFontSize = aFont.GetSize();
    4641           0 :         OutputDevice* pDefDev = Application::GetDefaultDevice();
    4642           0 :         aFontSize = OutputDevice::LogicToLogic( aFontSize, pDefDev->GetMapMode(), getMapMode() );
    4643           0 :         aFont.SetSize( aFontSize );
    4644             :     }
    4645           0 :     return aFont;
    4646             : }
    4647             : 
    4648           0 : sal_Int32 PDFWriterImpl::getBestBuiltinFont( const Font& rFont )
    4649             : {
    4650           0 :     sal_Int32 nBest = 4; // default to Helvetica
    4651           0 :     OUString aFontName( rFont.GetName() );
    4652           0 :     aFontName = aFontName.toAsciiLowerCase();
    4653             : 
    4654           0 :     if( aFontName.indexOf( "times" ) != -1 )
    4655           0 :         nBest = 8;
    4656           0 :     else if( aFontName.indexOf( "courier" ) != -1 )
    4657           0 :         nBest = 0;
    4658           0 :     else if( aFontName.indexOf( "dingbats" ) != -1 )
    4659           0 :         nBest = 13;
    4660           0 :     else if( aFontName.indexOf( "symbol" ) != -1 )
    4661           0 :         nBest = 12;
    4662           0 :     if( nBest < 12 )
    4663             :     {
    4664           0 :         if( rFont.GetItalic() == ITALIC_OBLIQUE || rFont.GetItalic() == ITALIC_NORMAL )
    4665           0 :             nBest += 1;
    4666           0 :         if( rFont.GetWeight() > WEIGHT_MEDIUM )
    4667           0 :             nBest += 2;
    4668             :     }
    4669             : 
    4670           0 :     if( m_aBuiltinFontToObjectMap.find( nBest ) == m_aBuiltinFontToObjectMap.end() )
    4671           0 :         m_aBuiltinFontToObjectMap[ nBest ] = createObject();
    4672             : 
    4673           0 :     return nBest;
    4674             : }
    4675             : 
    4676           0 : static inline const Color& replaceColor( const Color& rCol1, const Color& rCol2 )
    4677             : {
    4678           0 :     return (rCol1 == Color( COL_TRANSPARENT )) ? rCol2 : rCol1;
    4679             : }
    4680             : 
    4681           0 : void PDFWriterImpl::createDefaultPushButtonAppearance( PDFWidget& rButton, const PDFWriter::PushButtonWidget& rWidget )
    4682             : {
    4683           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4684             : 
    4685             :     // save graphics state
    4686           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4687             : 
    4688             :     // transform relative to control's coordinates since an
    4689             :     // appearance stream is a form XObject
    4690             :     // this relies on the m_aRect member of rButton NOT already being transformed
    4691             :     // to default user space
    4692           0 :     if( rWidget.Background || rWidget.Border )
    4693             :     {
    4694           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetLightColor() ) : Color( COL_TRANSPARENT ) );
    4695           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetDialogColor() ) : Color( COL_TRANSPARENT ) );
    4696           0 :         drawRectangle( rWidget.Location );
    4697             :     }
    4698             :     // prepare font to use
    4699           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetPushButtonFont() );
    4700           0 :     setFont( aFont );
    4701           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetButtonTextColor() ) );
    4702             : 
    4703           0 :     drawText( rButton.m_aRect, rButton.m_aText, rButton.m_nTextStyle );
    4704             : 
    4705             :     // create DA string while local mapmode is still in place
    4706             :     // (that is before endRedirect())
    4707           0 :     OStringBuffer aDA( 256 );
    4708           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetButtonTextColor() ), aDA );
    4709           0 :     Font aDummyFont( OUString( "Helvetica" ), aFont.GetSize() );
    4710           0 :     sal_Int32 nDummyBuiltin = getBestBuiltinFont( aDummyFont );
    4711           0 :     aDA.append( ' ' );
    4712           0 :     aDA.append( m_aBuiltinFonts[nDummyBuiltin].getNameObject() );
    4713           0 :     aDA.append( ' ' );
    4714           0 :     m_aPages[m_nCurrentPage].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4715           0 :     aDA.append( " Tf" );
    4716           0 :     rButton.m_aDAString = aDA.makeStringAndClear();
    4717             : 
    4718           0 :     pop();
    4719             : 
    4720           0 :     rButton.m_aAppearances[ "N" ][ "Standard" ] = new SvMemoryStream();
    4721             : 
    4722             :     /* seems like a bad hack but at least works in both AR5 and 6:
    4723             :        we draw the button ourselves and tell AR
    4724             :        the button would be totally transparent with no text
    4725             : 
    4726             :        One would expect that simply setting a normal appearance
    4727             :        should suffice, but no, as soon as the user actually presses
    4728             :        the button and an action is tied to it (gasp! a button that
    4729             :        does something) the appearance gets replaced by some crap that AR
    4730             :        creates on the fly even if no DA or MK is given. On AR6 at least
    4731             :        the DA and MK work as expected, but on AR5 this creates a region
    4732             :        filled with the background color but nor text. Urgh.
    4733             :     */
    4734           0 :     rButton.m_aMKDict = "/BC [] /BG [] /CA";
    4735           0 :     rButton.m_aMKDictCAString = "";
    4736           0 : }
    4737             : 
    4738           0 : Font PDFWriterImpl::drawFieldBorder( PDFWidget& rIntern,
    4739             :                                      const PDFWriter::AnyWidget& rWidget,
    4740             :                                      const StyleSettings& rSettings )
    4741             : {
    4742           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetFieldFont() );
    4743             : 
    4744           0 :     if( rWidget.Background || rWidget.Border )
    4745             :     {
    4746           0 :         if( rWidget.Border && rWidget.BorderColor == Color( COL_TRANSPARENT ) )
    4747             :         {
    4748           0 :             sal_Int32 nDelta = getReferenceDevice()->ImplGetDPIX() / 500;
    4749           0 :             if( nDelta < 1 )
    4750           0 :                 nDelta = 1;
    4751           0 :             setLineColor( Color( COL_TRANSPARENT ) );
    4752           0 :             Rectangle aRect = rIntern.m_aRect;
    4753           0 :             setFillColor( rSettings.GetLightBorderColor() );
    4754           0 :             drawRectangle( aRect );
    4755           0 :             aRect.Left()  += nDelta; aRect.Top()     += nDelta;
    4756           0 :             aRect.Right() -= nDelta; aRect.Bottom()  -= nDelta;
    4757           0 :             setFillColor( rSettings.GetFieldColor() );
    4758           0 :             drawRectangle( aRect );
    4759           0 :             setFillColor( rSettings.GetLightColor() );
    4760           0 :             drawRectangle( Rectangle( Point( aRect.Left(), aRect.Bottom()-nDelta ), aRect.BottomRight() ) );
    4761           0 :             drawRectangle( Rectangle( Point( aRect.Right()-nDelta, aRect.Top() ), aRect.BottomRight() ) );
    4762           0 :             setFillColor( rSettings.GetDarkShadowColor() );
    4763           0 :             drawRectangle( Rectangle( aRect.TopLeft(), Point( aRect.Left()+nDelta, aRect.Bottom() ) ) );
    4764           0 :             drawRectangle( Rectangle( aRect.TopLeft(), Point( aRect.Right(), aRect.Top()+nDelta ) ) );
    4765             :         }
    4766             :         else
    4767             :         {
    4768           0 :             setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetShadowColor() ) : Color( COL_TRANSPARENT ) );
    4769           0 :             setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    4770           0 :             drawRectangle( rIntern.m_aRect );
    4771             :         }
    4772             : 
    4773           0 :         if( rWidget.Border )
    4774             :         {
    4775             :             // adjust edit area accounting for border
    4776           0 :             sal_Int32 nDelta = aFont.GetHeight()/4;
    4777           0 :             if( nDelta < 1 )
    4778           0 :                 nDelta = 1;
    4779           0 :             rIntern.m_aRect.Left()  += nDelta;
    4780           0 :             rIntern.m_aRect.Top()   += nDelta;
    4781           0 :             rIntern.m_aRect.Right() -= nDelta;
    4782           0 :             rIntern.m_aRect.Bottom()-= nDelta;
    4783             :         }
    4784             :     }
    4785           0 :     return aFont;
    4786             : }
    4787             : 
    4788           0 : void PDFWriterImpl::createDefaultEditAppearance( PDFWidget& rEdit, const PDFWriter::EditWidget& rWidget )
    4789             : {
    4790           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4791           0 :     SvMemoryStream* pEditStream = new SvMemoryStream( 1024, 1024 );
    4792             : 
    4793           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4794             : 
    4795             :     // prepare font to use, draw field border
    4796           0 :     Font aFont = drawFieldBorder( rEdit, rWidget, rSettings );
    4797           0 :     sal_Int32 nBest = m_aContext.FieldsUseSystemFonts ? getSystemFont( aFont ): getBestBuiltinFont( aFont );
    4798             : 
    4799             :     // prepare DA string
    4800           0 :     OStringBuffer aDA( 32 );
    4801           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetFieldTextColor() ), aDA );
    4802           0 :     aDA.append( ' ' );
    4803           0 :     if( m_aContext.FieldsUseSystemFonts )
    4804             :     {
    4805           0 :         aDA.append( "/F" );
    4806           0 :         aDA.append( nBest );
    4807             : 
    4808           0 :         OStringBuffer aDR( 32 );
    4809           0 :         aDR.append( "/Font " );
    4810           0 :         aDR.append( getFontDictObject() );
    4811           0 :         aDR.append( " 0 R" );
    4812           0 :         rEdit.m_aDRDict = aDR.makeStringAndClear();
    4813             :     }
    4814             :     else
    4815           0 :         aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4816           0 :     aDA.append( ' ' );
    4817           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4818           0 :     aDA.append( " Tf" );
    4819             : 
    4820             :     /*  create an empty appearance stream, let the viewer create
    4821             :         the appearance at runtime. This is because AR5 seems to
    4822             :         paint the widget appearance always, and a dynamically created
    4823             :         appearance on top of it. AR6 is well behaved in that regard, so
    4824             :         that behaviour seems to be a bug. Anyway this empty appearance
    4825             :         relies on /NeedAppearances in the AcroForm dictionary set to "true"
    4826             :      */
    4827           0 :     beginRedirect( pEditStream, rEdit.m_aRect );
    4828           0 :     OStringBuffer aAppearance( 32 );
    4829           0 :     aAppearance.append( "/Tx BMC\nEMC\n" );
    4830           0 :     writeBuffer( aAppearance.getStr(), aAppearance.getLength() );
    4831             : 
    4832           0 :     endRedirect();
    4833           0 :     pop();
    4834             : 
    4835           0 :     rEdit.m_aAppearances[ "N" ][ "Standard" ] = pEditStream;
    4836             : 
    4837           0 :     rEdit.m_aDAString = aDA.makeStringAndClear();
    4838           0 : }
    4839             : 
    4840           0 : void PDFWriterImpl::createDefaultListBoxAppearance( PDFWidget& rBox, const PDFWriter::ListBoxWidget& rWidget )
    4841             : {
    4842           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4843           0 :     SvMemoryStream* pListBoxStream = new SvMemoryStream( 1024, 1024 );
    4844             : 
    4845           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4846             : 
    4847             :     // prepare font to use, draw field border
    4848           0 :     Font aFont = drawFieldBorder( rBox, rWidget, rSettings );
    4849           0 :     sal_Int32 nBest = m_aContext.FieldsUseSystemFonts ? getSystemFont( aFont ): getBestBuiltinFont( aFont );
    4850             : 
    4851           0 :     beginRedirect( pListBoxStream, rBox.m_aRect );
    4852           0 :     OStringBuffer aAppearance( 64 );
    4853             : 
    4854           0 :     setLineColor( Color( COL_TRANSPARENT ) );
    4855           0 :     setFillColor( replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) );
    4856           0 :     drawRectangle( rBox.m_aRect );
    4857             : 
    4858             :     // empty appearance, see createDefaultEditAppearance for reference
    4859           0 :     aAppearance.append( "/Tx BMC\nEMC\n" );
    4860           0 :     writeBuffer( aAppearance.getStr(), aAppearance.getLength() );
    4861             : 
    4862           0 :     endRedirect();
    4863           0 :     pop();
    4864             : 
    4865           0 :     rBox.m_aAppearances[ "N" ][ "Standard" ] = pListBoxStream;
    4866             : 
    4867             :     // prepare DA string
    4868           0 :     OStringBuffer aDA( 256 );
    4869             :     // prepare DA string
    4870           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetFieldTextColor() ), aDA );
    4871           0 :     aDA.append( ' ' );
    4872           0 :     if( m_aContext.FieldsUseSystemFonts )
    4873             :     {
    4874           0 :         aDA.append( "/F" );
    4875           0 :         aDA.append( nBest );
    4876             : 
    4877           0 :         OStringBuffer aDR( 32 );
    4878           0 :         aDR.append( "/Font " );
    4879           0 :         aDR.append( getFontDictObject() );
    4880           0 :         aDR.append( " 0 R" );
    4881           0 :         rBox.m_aDRDict = aDR.makeStringAndClear();
    4882             :     }
    4883             :     else
    4884           0 :         aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4885           0 :     aDA.append( ' ' );
    4886           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4887           0 :     aDA.append( " Tf" );
    4888           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    4889           0 : }
    4890             : 
    4891           0 : void PDFWriterImpl::createDefaultCheckBoxAppearance( PDFWidget& rBox, const PDFWriter::CheckBoxWidget& rWidget )
    4892             : {
    4893           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4894             : 
    4895             :     // save graphics state
    4896           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4897             : 
    4898           0 :     if( rWidget.Background || rWidget.Border )
    4899             :     {
    4900           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetCheckedColor() ) : Color( COL_TRANSPARENT ) );
    4901           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    4902           0 :         drawRectangle( rBox.m_aRect );
    4903             :     }
    4904             : 
    4905           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetRadioCheckFont() );
    4906           0 :     setFont( aFont );
    4907           0 :     Size aFontSize = aFont.GetSize();
    4908           0 :     if( aFontSize.Height() > rBox.m_aRect.GetHeight() )
    4909           0 :         aFontSize.Height() = rBox.m_aRect.GetHeight();
    4910           0 :     sal_Int32 nDelta = aFontSize.Height()/10;
    4911           0 :     if( nDelta < 1 )
    4912           0 :         nDelta = 1;
    4913             : 
    4914           0 :     Rectangle aCheckRect, aTextRect;
    4915           0 :     if( rWidget.ButtonIsLeft )
    4916             :     {
    4917           0 :         aCheckRect.Left()   = rBox.m_aRect.Left() + nDelta;
    4918           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    4919           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    4920           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    4921             : 
    4922             :         // #i74206# handle small controls without text area
    4923           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    4924             :         {
    4925           0 :             aCheckRect.Right()  -= nDelta;
    4926           0 :             aCheckRect.Top()    += nDelta/2;
    4927           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    4928             :         }
    4929             : 
    4930           0 :         aTextRect.Left()    = rBox.m_aRect.Left() + aCheckRect.GetWidth()+5*nDelta;
    4931           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    4932           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    4933           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    4934             :     }
    4935             :     else
    4936             :     {
    4937           0 :         aCheckRect.Left()   = rBox.m_aRect.Right() - nDelta - aFontSize.Height();
    4938           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    4939           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    4940           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    4941             : 
    4942             :         // #i74206# handle small controls without text area
    4943           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    4944             :         {
    4945           0 :             aCheckRect.Left()   += nDelta;
    4946           0 :             aCheckRect.Top()    += nDelta/2;
    4947           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    4948             :         }
    4949             : 
    4950           0 :         aTextRect.Left()    = rBox.m_aRect.Left();
    4951           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    4952           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    4953           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    4954             :     }
    4955           0 :     setLineColor( Color( COL_BLACK ) );
    4956           0 :     setFillColor( Color( COL_TRANSPARENT ) );
    4957           0 :     OStringBuffer aLW( 32 );
    4958           0 :     aLW.append( "q " );
    4959           0 :     m_aPages[m_nCurrentPage].appendMappedLength( nDelta, aLW );
    4960           0 :     aLW.append( " w " );
    4961           0 :     writeBuffer( aLW.getStr(), aLW.getLength() );
    4962           0 :     drawRectangle( aCheckRect );
    4963           0 :     writeBuffer( " Q\n", 3 );
    4964           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    4965           0 :     drawText( aTextRect, rBox.m_aText, rBox.m_nTextStyle );
    4966             : 
    4967           0 :     pop();
    4968             : 
    4969           0 :     OStringBuffer aDA( 256 );
    4970           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    4971           0 :     sal_Int32 nBest = getBestBuiltinFont( Font( OUString( "ZapfDingbats" ), aFont.GetSize() ) );
    4972           0 :     aDA.append( ' ' );
    4973           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4974           0 :     aDA.append( " 0 Tf" );
    4975           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    4976           0 :     rBox.m_aMKDict = "/CA";
    4977           0 :     rBox.m_aMKDictCAString = "8";
    4978           0 :     rBox.m_aRect = aCheckRect;
    4979             : 
    4980             :     // create appearance streams
    4981           0 :     sal_Char cMark = '8';
    4982           0 :     sal_Int32 nCharXOffset = 1000-m_aBuiltinFonts[13].m_aWidths[sal_Int32(cMark)];
    4983           0 :     nCharXOffset *= aCheckRect.GetHeight();
    4984           0 :     nCharXOffset /= 2000;
    4985             :     sal_Int32 nCharYOffset = 1000-
    4986           0 :         (m_aBuiltinFonts[13].m_nAscent+m_aBuiltinFonts[13].m_nDescent); // descent is negative
    4987           0 :     nCharYOffset *= aCheckRect.GetHeight();
    4988           0 :     nCharYOffset /= 2000;
    4989             : 
    4990           0 :     SvMemoryStream* pCheckStream = new SvMemoryStream( 256, 256 );
    4991           0 :     beginRedirect( pCheckStream, aCheckRect );
    4992           0 :     aDA.append( "/Tx BMC\nq BT\n" );
    4993           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    4994           0 :     aDA.append( ' ' );
    4995           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4996           0 :     aDA.append( ' ' );
    4997           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aCheckRect.GetHeight() ), aDA );
    4998           0 :     aDA.append( " Tf\n" );
    4999           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nCharXOffset, aDA );
    5000           0 :     aDA.append( " " );
    5001           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nCharYOffset, aDA );
    5002           0 :     aDA.append( " Td (" );
    5003           0 :     aDA.append( cMark );
    5004           0 :     aDA.append( ") Tj\nET\nQ\nEMC\n" );
    5005           0 :     writeBuffer( aDA.getStr(), aDA.getLength() );
    5006           0 :     endRedirect();
    5007           0 :     rBox.m_aAppearances[ "N" ][ "Yes" ] = pCheckStream;
    5008             : 
    5009           0 :     SvMemoryStream* pUncheckStream = new SvMemoryStream( 256, 256 );
    5010           0 :     beginRedirect( pUncheckStream, aCheckRect );
    5011           0 :     writeBuffer( "/Tx BMC\nEMC\n", 12 );
    5012           0 :     endRedirect();
    5013           0 :     rBox.m_aAppearances[ "N" ][ "Off" ] = pUncheckStream;
    5014           0 : }
    5015             : 
    5016           0 : void PDFWriterImpl::createDefaultRadioButtonAppearance( PDFWidget& rBox, const PDFWriter::RadioButtonWidget& rWidget )
    5017             : {
    5018           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    5019             : 
    5020             :     // save graphics state
    5021           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    5022             : 
    5023           0 :     if( rWidget.Background || rWidget.Border )
    5024             :     {
    5025           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetCheckedColor() ) : Color( COL_TRANSPARENT ) );
    5026           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    5027           0 :         drawRectangle( rBox.m_aRect );
    5028             :     }
    5029             : 
    5030           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetRadioCheckFont() );
    5031           0 :     setFont( aFont );
    5032           0 :     Size aFontSize = aFont.GetSize();
    5033           0 :     if( aFontSize.Height() > rBox.m_aRect.GetHeight() )
    5034           0 :         aFontSize.Height() = rBox.m_aRect.GetHeight();
    5035           0 :     sal_Int32 nDelta = aFontSize.Height()/10;
    5036           0 :     if( nDelta < 1 )
    5037           0 :         nDelta = 1;
    5038             : 
    5039           0 :     Rectangle aCheckRect, aTextRect;
    5040           0 :     if( rWidget.ButtonIsLeft )
    5041             :     {
    5042           0 :         aCheckRect.Left()   = rBox.m_aRect.Left() + nDelta;
    5043           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    5044           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    5045           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    5046             : 
    5047             :         // #i74206# handle small controls without text area
    5048           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    5049             :         {
    5050           0 :             aCheckRect.Right()  -= nDelta;
    5051           0 :             aCheckRect.Top()    += nDelta/2;
    5052           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    5053             :         }
    5054             : 
    5055           0 :         aTextRect.Left()    = rBox.m_aRect.Left() + aCheckRect.GetWidth()+5*nDelta;
    5056           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    5057           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    5058           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    5059             :     }
    5060             :     else
    5061             :     {
    5062           0 :         aCheckRect.Left()   = rBox.m_aRect.Right() - nDelta - aFontSize.Height();
    5063           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    5064           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    5065           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    5066             : 
    5067             :         // #i74206# handle small controls without text area
    5068           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    5069             :         {
    5070           0 :             aCheckRect.Left()   += nDelta;
    5071           0 :             aCheckRect.Top()    += nDelta/2;
    5072           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    5073             :         }
    5074             : 
    5075           0 :         aTextRect.Left()    = rBox.m_aRect.Left();
    5076           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    5077           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    5078           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    5079             :     }
    5080           0 :     setLineColor( Color( COL_BLACK ) );
    5081           0 :     setFillColor( Color( COL_TRANSPARENT ) );
    5082           0 :     OStringBuffer aLW( 32 );
    5083           0 :     aLW.append( "q " );
    5084           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nDelta, aLW );
    5085           0 :     aLW.append( " w " );
    5086           0 :     writeBuffer( aLW.getStr(), aLW.getLength() );
    5087           0 :     drawEllipse( aCheckRect );
    5088           0 :     writeBuffer( " Q\n", 3 );
    5089           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    5090           0 :     drawText( aTextRect, rBox.m_aText, rBox.m_nTextStyle );
    5091             : 
    5092           0 :     pop();
    5093             : 
    5094           0 :     OStringBuffer aDA( 256 );
    5095           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5096           0 :     sal_Int32 nBest = getBestBuiltinFont( Font( OUString( "ZapfDingbats" ), aFont.GetSize() ) );
    5097           0 :     aDA.append( ' ' );
    5098           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5099           0 :     aDA.append( " 0 Tf" );
    5100           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    5101             :     //to encrypt this (el)
    5102           0 :     rBox.m_aMKDict = "/CA";
    5103             :     //after this assignement, to m_aMKDic cannot be added anything
    5104           0 :     rBox.m_aMKDictCAString = "l";
    5105             : 
    5106           0 :     rBox.m_aRect = aCheckRect;
    5107             : 
    5108             :     // create appearance streams
    5109           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    5110           0 :     SvMemoryStream* pCheckStream = new SvMemoryStream( 256, 256 );
    5111             : 
    5112           0 :     beginRedirect( pCheckStream, aCheckRect );
    5113           0 :     aDA.append( "/Tx BMC\nq BT\n" );
    5114           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5115           0 :     aDA.append( ' ' );
    5116           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5117           0 :     aDA.append( ' ' );
    5118           0 :     m_aPages[m_nCurrentPage].appendMappedLength( sal_Int32( aCheckRect.GetHeight() ), aDA );
    5119           0 :     aDA.append( " Tf\n0 0 Td\nET\nQ\n" );
    5120           0 :     writeBuffer( aDA.getStr(), aDA.getLength() );
    5121           0 :     setFillColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    5122           0 :     setLineColor( Color( COL_TRANSPARENT ) );
    5123           0 :     aCheckRect.Left()   += 3*nDelta;
    5124           0 :     aCheckRect.Top()    += 3*nDelta;
    5125           0 :     aCheckRect.Bottom() -= 3*nDelta;
    5126           0 :     aCheckRect.Right()  -= 3*nDelta;
    5127           0 :     drawEllipse( aCheckRect );
    5128           0 :     writeBuffer( "\nEMC\n", 5 );
    5129           0 :     endRedirect();
    5130             : 
    5131           0 :     pop();
    5132           0 :     rBox.m_aAppearances[ "N" ][ "Yes" ] = pCheckStream;
    5133             : 
    5134           0 :     SvMemoryStream* pUncheckStream = new SvMemoryStream( 256, 256 );
    5135           0 :     beginRedirect( pUncheckStream, aCheckRect );
    5136           0 :     writeBuffer( "/Tx BMC\nEMC\n", 12 );
    5137           0 :     endRedirect();
    5138           0 :     rBox.m_aAppearances[ "N" ][ "Off" ] = pUncheckStream;
    5139           0 : }
    5140             : 
    5141           0 : bool PDFWriterImpl::emitAppearances( PDFWidget& rWidget, OStringBuffer& rAnnotDict )
    5142             : {
    5143             :     // TODO: check and insert default streams
    5144           0 :     OString aStandardAppearance;
    5145           0 :     switch( rWidget.m_eType )
    5146             :     {
    5147             :         case PDFWriter::CheckBox:
    5148           0 :             aStandardAppearance = OUStringToOString( rWidget.m_aValue, RTL_TEXTENCODING_ASCII_US );
    5149           0 :             break;
    5150             :         default:
    5151           0 :             break;
    5152             :     }
    5153             : 
    5154           0 :     if( rWidget.m_aAppearances.size() )
    5155             :     {
    5156           0 :         rAnnotDict.append( "/AP<<\n" );
    5157           0 :         for( PDFAppearanceMap::iterator dict_it = rWidget.m_aAppearances.begin(); dict_it != rWidget.m_aAppearances.end(); ++dict_it )
    5158             :         {
    5159           0 :             rAnnotDict.append( "/" );
    5160           0 :             rAnnotDict.append( dict_it->first );
    5161           0 :             bool bUseSubDict = (dict_it->second.size() > 1);
    5162           0 :             rAnnotDict.append( bUseSubDict ? "<<" : " " );
    5163             : 
    5164           0 :             for( PDFAppearanceStreams::const_iterator stream_it = dict_it->second.begin();
    5165           0 :                  stream_it != dict_it->second.end(); ++stream_it )
    5166             :             {
    5167           0 :                 SvMemoryStream* pApppearanceStream = stream_it->second;
    5168           0 :                 dict_it->second[ stream_it->first ] = NULL;
    5169             : 
    5170           0 :                 bool bDeflate = compressStream( pApppearanceStream );
    5171             : 
    5172           0 :                 pApppearanceStream->Seek( STREAM_SEEK_TO_END );
    5173           0 :                 sal_Int64 nStreamLen = pApppearanceStream->Tell();
    5174           0 :                 pApppearanceStream->Seek( STREAM_SEEK_TO_BEGIN );
    5175           0 :                 sal_Int32 nObject = createObject();
    5176           0 :                 CHECK_RETURN( updateObject( nObject ) );
    5177             :                 #if OSL_DEBUG_LEVEL > 1
    5178             :                 emitComment( "PDFWriterImpl::emitAppearances" );
    5179             :                 #endif
    5180           0 :                 OStringBuffer aLine;
    5181           0 :                 aLine.append( nObject );
    5182             : 
    5183             :                 aLine.append( " 0 obj\n"
    5184             :                               "<</Type/XObject\n"
    5185             :                               "/Subtype/Form\n"
    5186           0 :                               "/BBox[0 0 " );
    5187           0 :                 appendFixedInt( rWidget.m_aRect.GetWidth()-1, aLine );
    5188           0 :                 aLine.append( " " );
    5189           0 :                 appendFixedInt( rWidget.m_aRect.GetHeight()-1, aLine );
    5190             :                 aLine.append( "]\n"
    5191           0 :                               "/Resources " );
    5192           0 :                 aLine.append( getResourceDictObj() );
    5193             :                 aLine.append( " 0 R\n"
    5194           0 :                               "/Length " );
    5195           0 :                 aLine.append( nStreamLen );
    5196           0 :                 aLine.append( "\n" );
    5197           0 :                 if( bDeflate )
    5198           0 :                     aLine.append( "/Filter/FlateDecode\n" );
    5199           0 :                 aLine.append( ">>\nstream\n" );
    5200           0 :                 CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5201           0 :                 checkAndEnableStreamEncryption( nObject );
    5202           0 :                 CHECK_RETURN( writeBuffer( pApppearanceStream->GetData(), nStreamLen ) );
    5203           0 :                 disableStreamEncryption();
    5204           0 :                 CHECK_RETURN( writeBuffer( "\nendstream\nendobj\n\n", 19 ) );
    5205             : 
    5206           0 :                 if( bUseSubDict )
    5207             :                 {
    5208           0 :                     rAnnotDict.append( " /" );
    5209           0 :                     rAnnotDict.append( stream_it->first );
    5210           0 :                     rAnnotDict.append( " " );
    5211             :                 }
    5212           0 :                 rAnnotDict.append( nObject );
    5213           0 :                 rAnnotDict.append( " 0 R" );
    5214             : 
    5215           0 :                 delete pApppearanceStream;
    5216           0 :             }
    5217             : 
    5218           0 :             rAnnotDict.append( bUseSubDict ? ">>\n" : "\n" );
    5219             :         }
    5220           0 :         rAnnotDict.append( ">>\n" );
    5221           0 :         if( !aStandardAppearance.isEmpty() )
    5222             :         {
    5223           0 :             rAnnotDict.append( "/AS /" );
    5224           0 :             rAnnotDict.append( aStandardAppearance );
    5225           0 :             rAnnotDict.append( "\n" );
    5226             :         }
    5227             :     }
    5228             : 
    5229           0 :     return true;
    5230             : }
    5231             : 
    5232           0 : bool PDFWriterImpl::emitWidgetAnnotations()
    5233             : {
    5234           0 :     ensureUniqueRadioOnValues();
    5235             : 
    5236           0 :     int nAnnots = m_aWidgets.size();
    5237           0 :     for( int a = 0; a < nAnnots; a++ )
    5238             :     {
    5239           0 :         PDFWidget& rWidget = m_aWidgets[a];
    5240             : 
    5241           0 :         OStringBuffer aLine( 1024 );
    5242           0 :         OStringBuffer aValue( 256 );
    5243           0 :         aLine.append( rWidget.m_nObject );
    5244             :         aLine.append( " 0 obj\n"
    5245           0 :                       "<<" );
    5246           0 :         if( rWidget.m_eType != PDFWriter::Hierarchy )
    5247             :         {
    5248             :             // emit widget annotation only for terminal fields
    5249           0 :             if( rWidget.m_aKids.empty() )
    5250             :             {
    5251             :                 int iRectMargin;
    5252             : 
    5253           0 :                 aLine.append( "/Type/Annot/Subtype/Widget/F " );
    5254             : 
    5255           0 :                 if (rWidget.m_eType == PDFWriter::Signature)
    5256             :                 {
    5257           0 :                     aLine.append( "132\n" ); // Print & Locked
    5258           0 :                     iRectMargin = 0;
    5259             :                 }
    5260             :                 else
    5261             :                 {
    5262           0 :                     aLine.append( "4\n" );
    5263           0 :                     iRectMargin = 1;
    5264             :                 }
    5265             : 
    5266           0 :                 aLine.append("/Rect[" );
    5267           0 :                 appendFixedInt( rWidget.m_aRect.Left()-iRectMargin, aLine );
    5268           0 :                 aLine.append( ' ' );
    5269           0 :                 appendFixedInt( rWidget.m_aRect.Top()+iRectMargin, aLine );
    5270           0 :                 aLine.append( ' ' );
    5271           0 :                 appendFixedInt( rWidget.m_aRect.Right()+iRectMargin, aLine );
    5272           0 :                 aLine.append( ' ' );
    5273           0 :                 appendFixedInt( rWidget.m_aRect.Bottom()-iRectMargin, aLine );
    5274           0 :                 aLine.append( "]\n" );
    5275             :             }
    5276           0 :             aLine.append( "/FT/" );
    5277           0 :             switch( rWidget.m_eType )
    5278             :             {
    5279             :                 case PDFWriter::RadioButton:
    5280             :                 case PDFWriter::CheckBox:
    5281             :                     // for radio buttons only the RadioButton field, not the
    5282             :                     // CheckBox children should have a value, else acrobat reader
    5283             :                     // does not always check the right button
    5284             :                     // of course real check boxes (not belonging to a radio group)
    5285             :                     // need their values, too
    5286           0 :                     if( rWidget.m_eType == PDFWriter::RadioButton || rWidget.m_nRadioGroup < 0 )
    5287             :                     {
    5288           0 :                         aValue.append( "/" );
    5289             :                         // check for radio group with all buttons unpressed
    5290           0 :                         if( rWidget.m_aValue.isEmpty() )
    5291           0 :                             aValue.append( "Off" );
    5292             :                         else
    5293           0 :                             appendName( rWidget.m_aValue, aValue );
    5294             :                     }
    5295             :                     // fall-through
    5296             :                 case PDFWriter::PushButton:
    5297           0 :                     aLine.append( "Btn" );
    5298           0 :                     break;
    5299             :                 case PDFWriter::ListBox:
    5300           0 :                     if( rWidget.m_nFlags & 0x200000 ) // multiselect
    5301             :                     {
    5302           0 :                         aValue.append( "[" );
    5303           0 :                         for( unsigned int i = 0; i < rWidget.m_aSelectedEntries.size(); i++ )
    5304             :                         {
    5305           0 :                             sal_Int32 nEntry = rWidget.m_aSelectedEntries[i];
    5306           0 :                             if( nEntry >= 0 && nEntry < sal_Int32(rWidget.m_aListEntries.size()) )
    5307           0 :                                 appendUnicodeTextStringEncrypt( rWidget.m_aListEntries[ nEntry ], rWidget.m_nObject, aValue );
    5308             :                         }
    5309           0 :                         aValue.append( "]" );
    5310             :                     }
    5311           0 :                     else if( rWidget.m_aSelectedEntries.size() > 0 &&
    5312           0 :                              rWidget.m_aSelectedEntries[0] >= 0 &&
    5313           0 :                              rWidget.m_aSelectedEntries[0] < sal_Int32(rWidget.m_aListEntries.size()) )
    5314             :                     {
    5315           0 :                         appendUnicodeTextStringEncrypt( rWidget.m_aListEntries[ rWidget.m_aSelectedEntries[0] ], rWidget.m_nObject, aValue );
    5316             :                     }
    5317             :                     else
    5318           0 :                         appendUnicodeTextStringEncrypt( OUString(), rWidget.m_nObject, aValue );
    5319           0 :                     aLine.append( "Ch" );
    5320           0 :                     break;
    5321             :                 case PDFWriter::ComboBox:
    5322           0 :                     appendUnicodeTextStringEncrypt( rWidget.m_aValue, rWidget.m_nObject, aValue );
    5323           0 :                     aLine.append( "Ch" );
    5324           0 :                     break;
    5325             :                 case PDFWriter::Edit:
    5326           0 :                     aLine.append( "Tx" );
    5327           0 :                     appendUnicodeTextStringEncrypt( rWidget.m_aValue, rWidget.m_nObject, aValue );
    5328           0 :                     break;
    5329             :                 case PDFWriter::Signature:
    5330           0 :                     aLine.append( "Sig" );
    5331           0 :                     aValue.append(OUStringToOString(rWidget.m_aValue, RTL_TEXTENCODING_ASCII_US));
    5332           0 :                     break;
    5333             :                 case PDFWriter::Hierarchy: // make the compiler happy
    5334           0 :                     break;
    5335             :             }
    5336           0 :             aLine.append( "\n" );
    5337           0 :             aLine.append( "/P " );
    5338           0 :             aLine.append( m_aPages[ rWidget.m_nPage ].m_nPageObject );
    5339           0 :             aLine.append( " 0 R\n" );
    5340             :         }
    5341           0 :         if( rWidget.m_nParent )
    5342             :         {
    5343           0 :             aLine.append( "/Parent " );
    5344           0 :             aLine.append( rWidget.m_nParent );
    5345           0 :             aLine.append( " 0 R\n" );
    5346             :         }
    5347           0 :         if( rWidget.m_aKids.size() )
    5348             :         {
    5349           0 :             aLine.append( "/Kids[" );
    5350           0 :             for( unsigned int i = 0; i < rWidget.m_aKids.size(); i++ )
    5351             :             {
    5352           0 :                 aLine.append( rWidget.m_aKids[i] );
    5353           0 :                 aLine.append( " 0 R" );
    5354           0 :                 aLine.append( ( (i&15) == 15 ) ? "\n" : " " );
    5355             :             }
    5356           0 :             aLine.append( "]\n" );
    5357             :         }
    5358           0 :         if( !rWidget.m_aName.isEmpty() )
    5359             :         {
    5360           0 :             aLine.append( "/T" );
    5361           0 :             appendLiteralStringEncrypt( rWidget.m_aName, rWidget.m_nObject, aLine );
    5362           0 :             aLine.append( "\n" );
    5363             :         }
    5364           0 :         if( m_aContext.Version > PDFWriter::PDF_1_2 && !rWidget.m_aDescription.isEmpty() )
    5365             :         {
    5366             :             // the alternate field name should be unicode able since it is
    5367             :             // supposed to be used in UI
    5368           0 :             aLine.append( "/TU" );
    5369           0 :             appendUnicodeTextStringEncrypt( rWidget.m_aDescription, rWidget.m_nObject, aLine );
    5370           0 :             aLine.append( "\n" );
    5371             :         }
    5372             : 
    5373           0 :         if( rWidget.m_nFlags )
    5374             :         {
    5375           0 :             aLine.append( "/Ff " );
    5376           0 :             aLine.append( rWidget.m_nFlags );
    5377           0 :             aLine.append( "\n" );
    5378             :         }
    5379           0 :         if( !aValue.isEmpty() )
    5380             :         {
    5381           0 :             OString aVal = aValue.makeStringAndClear();
    5382           0 :             aLine.append( "/V " );
    5383           0 :             aLine.append( aVal );
    5384             :             aLine.append( "\n"
    5385           0 :                           "/DV " );
    5386           0 :             aLine.append( aVal );
    5387           0 :             aLine.append( "\n" );
    5388             :         }
    5389           0 :         if( rWidget.m_eType == PDFWriter::ListBox || rWidget.m_eType == PDFWriter::ComboBox )
    5390             :         {
    5391           0 :             sal_Int32 nTI = -1;
    5392           0 :             aLine.append( "/Opt[\n" );
    5393           0 :             sal_Int32 i = 0;
    5394           0 :             for( std::vector< OUString >::const_iterator it = rWidget.m_aListEntries.begin(); it != rWidget.m_aListEntries.end(); ++it, ++i )
    5395             :             {
    5396           0 :                 appendUnicodeTextStringEncrypt( *it, rWidget.m_nObject, aLine );
    5397           0 :                 aLine.append( "\n" );
    5398           0 :                 if( *it == rWidget.m_aValue )
    5399           0 :                     nTI = i;
    5400             :             }
    5401           0 :             aLine.append( "]\n" );
    5402           0 :             if( nTI > 0 )
    5403             :             {
    5404           0 :                 aLine.append( "/TI " );
    5405           0 :                 aLine.append( nTI );
    5406           0 :                 aLine.append( "\n" );
    5407           0 :                 if( rWidget.m_nFlags & 0x200000 ) // Multiselect
    5408             :                 {
    5409           0 :                     aLine.append( "/I [" );
    5410           0 :                     aLine.append( nTI );
    5411           0 :                     aLine.append( "]\n" );
    5412             :                 }
    5413             :             }
    5414             :         }
    5415           0 :         if( rWidget.m_eType == PDFWriter::Edit && rWidget.m_nMaxLen > 0 )
    5416             :         {
    5417           0 :             aLine.append( "/MaxLen " );
    5418           0 :             aLine.append( rWidget.m_nMaxLen );
    5419           0 :             aLine.append( "\n" );
    5420             :         }
    5421           0 :         if( rWidget.m_eType == PDFWriter::PushButton )
    5422             :         {
    5423           0 :             if(!m_bIsPDF_A1)
    5424             :             {
    5425           0 :                 OStringBuffer aDest;
    5426           0 :                 if( rWidget.m_nDest != -1 && appendDest( m_aDestinationIdTranslation[ rWidget.m_nDest ], aDest ) )
    5427             :                 {
    5428           0 :                     aLine.append( "/AA<</D<</Type/Action/S/GoTo/D " );
    5429           0 :                     aLine.append( aDest.makeStringAndClear() );
    5430           0 :                     aLine.append( ">>>>\n" );
    5431             :                 }
    5432           0 :                 else if( rWidget.m_aListEntries.empty() )
    5433             :                 {
    5434             :                     // create a reset form action
    5435           0 :                     aLine.append( "/AA<</D<</Type/Action/S/ResetForm>>>>\n" );
    5436             :                 }
    5437           0 :                 else if( rWidget.m_bSubmit )
    5438             :                 {
    5439             :                     // create a submit form action
    5440           0 :                     aLine.append( "/AA<</D<</Type/Action/S/SubmitForm/F" );
    5441           0 :                     appendLiteralStringEncrypt( rWidget.m_aListEntries.front(), rWidget.m_nObject, aLine, osl_getThreadTextEncoding() );
    5442           0 :                     aLine.append( "/Flags " );
    5443             : 
    5444           0 :                     sal_Int32 nFlags = 0;
    5445           0 :                     switch( m_aContext.SubmitFormat )
    5446             :                     {
    5447             :                     case PDFWriter::HTML:
    5448           0 :                         nFlags |= 4;
    5449           0 :                         break;
    5450             :                     case PDFWriter::XML:
    5451           0 :                         if( m_aContext.Version > PDFWriter::PDF_1_3 )
    5452           0 :                             nFlags |= 32;
    5453           0 :                         break;
    5454             :                     case PDFWriter::PDF:
    5455           0 :                         if( m_aContext.Version > PDFWriter::PDF_1_3 )
    5456           0 :                             nFlags |= 256;
    5457           0 :                         break;
    5458             :                     case PDFWriter::FDF:
    5459             :                     default:
    5460           0 :                         break;
    5461             :                     }
    5462           0 :                     if( rWidget.m_bSubmitGet )
    5463           0 :                         nFlags |= 8;
    5464           0 :                     aLine.append( nFlags );
    5465           0 :                     aLine.append( ">>>>\n" );
    5466             :                 }
    5467             :                 else
    5468             :                 {
    5469             :                     // create a URI action
    5470           0 :                     aLine.append( "/AA<</D<</Type/Action/S/URI/URI(" );
    5471           0 :                     aLine.append( OUStringToOString( rWidget.m_aListEntries.front(), RTL_TEXTENCODING_ASCII_US ) );
    5472           0 :                     aLine.append( ")>>>>\n" );
    5473           0 :                 }
    5474             :             }
    5475             :             else
    5476           0 :                 m_aErrors.insert( PDFWriter::Warning_FormAction_Omitted_PDFA );
    5477             :         }
    5478           0 :         if( !rWidget.m_aDAString.isEmpty() )
    5479             :         {
    5480           0 :             if( !rWidget.m_aDRDict.isEmpty() )
    5481             :             {
    5482           0 :                 aLine.append( "/DR<<" );
    5483           0 :                 aLine.append( rWidget.m_aDRDict );
    5484           0 :                 aLine.append( ">>\n" );
    5485             :             }
    5486             :             else
    5487             :             {
    5488           0 :                 aLine.append( "/DR<</Font<<" );
    5489           0 :                 appendBuiltinFontsToDict( aLine );
    5490           0 :                 aLine.append( ">>>>\n" );
    5491             :             }
    5492           0 :             aLine.append( "/DA" );
    5493           0 :             appendLiteralStringEncrypt( rWidget.m_aDAString, rWidget.m_nObject, aLine );
    5494           0 :             aLine.append( "\n" );
    5495           0 :             if( rWidget.m_nTextStyle & TEXT_DRAW_CENTER )
    5496           0 :                 aLine.append( "/Q 1\n" );
    5497           0 :             else if( rWidget.m_nTextStyle & TEXT_DRAW_RIGHT )
    5498           0 :                 aLine.append( "/Q 2\n" );
    5499             :         }
    5500             :         // appearance charactristics for terminal fields
    5501             :         // which are supposed to have an appearance constructed
    5502             :         // by the viewer application
    5503           0 :         if( !rWidget.m_aMKDict.isEmpty() )
    5504             :         {
    5505           0 :             aLine.append( "/MK<<" );
    5506           0 :             aLine.append( rWidget.m_aMKDict );
    5507             :             //add the CA string, encrypting it
    5508           0 :             appendLiteralStringEncrypt(rWidget.m_aMKDictCAString, rWidget.m_nObject, aLine);
    5509           0 :             aLine.append( ">>\n" );
    5510             :         }
    5511             : 
    5512           0 :         CHECK_RETURN( emitAppearances( rWidget, aLine ) );
    5513             : 
    5514             :         aLine.append( ">>\n"
    5515           0 :                       "endobj\n\n" );
    5516           0 :         CHECK_RETURN( updateObject( rWidget.m_nObject ) );
    5517           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5518           0 :     }
    5519           0 :     return true;
    5520             : }
    5521             : 
    5522           0 : bool PDFWriterImpl::emitAnnotations()
    5523             : {
    5524           0 :     if( m_aPages.size() < 1 )
    5525           0 :         return false;
    5526             : 
    5527           0 :     CHECK_RETURN( emitLinkAnnotations() );
    5528           0 :     CHECK_RETURN( emitNoteAnnotations() );
    5529           0 :     CHECK_RETURN( emitWidgetAnnotations() );
    5530             : 
    5531           0 :     return true;
    5532             : }
    5533             : 
    5534             : #undef CHECK_RETURN
    5535             : #define CHECK_RETURN( x ) if( !x ) return false
    5536             : 
    5537           0 : bool PDFWriterImpl::emitCatalog()
    5538             : {
    5539             :     // build page tree
    5540             :     // currently there is only one node that contains all leaves
    5541             : 
    5542             :     // first create a page tree node id
    5543           0 :     sal_Int32 nTreeNode = createObject();
    5544             : 
    5545             :     // emit global resource dictionary (page emit needs it)
    5546           0 :     CHECK_RETURN( emitResources() );
    5547             : 
    5548             :     // emit all pages
    5549           0 :     for( std::vector<PDFPage>::iterator it = m_aPages.begin(); it != m_aPages.end(); ++it )
    5550           0 :         if( ! it->emit( nTreeNode ) )
    5551           0 :             return false;
    5552             : 
    5553           0 :     sal_Int32 nNamedDestinationsDictionary = emitNamedDestinations();
    5554             : 
    5555           0 :     sal_Int32 nOutlineDict = emitOutline();
    5556             : 
    5557             :     // emit Output intent i59651
    5558           0 :     sal_Int32 nOutputIntentObject = emitOutputIntent();
    5559             : 
    5560             :     // emit metadata
    5561           0 :     sal_Int32 nMetadataObject = emitDocumentMetadata();
    5562             : 
    5563           0 :     sal_Int32 nStructureDict = 0;
    5564           0 :     if(m_aStructure.size() > 1)
    5565             :     {
    5566             :         // check if dummy structure containers are needed
    5567           0 :         addInternalStructureContainer(m_aStructure[0]);
    5568           0 :         nStructureDict = m_aStructure[0].m_nObject = createObject();
    5569           0 :         emitStructure( m_aStructure[ 0 ] );
    5570             :     }
    5571             : 
    5572             :     // adjust tree node file offset
    5573           0 :     if( ! updateObject( nTreeNode ) )
    5574           0 :         return false;
    5575             : 
    5576             :     // emit tree node
    5577           0 :     OStringBuffer aLine( 2048 );
    5578           0 :     aLine.append( nTreeNode );
    5579           0 :     aLine.append( " 0 obj\n" );
    5580           0 :     aLine.append( "<</Type/Pages\n" );
    5581           0 :     aLine.append( "/Resources " );
    5582           0 :     aLine.append( getResourceDictObj() );
    5583           0 :     aLine.append( " 0 R\n" );
    5584             : 
    5585           0 :     switch( m_eInheritedOrientation )
    5586             :     {
    5587           0 :         case PDFWriter::Landscape: aLine.append( "/Rotate 90\n" );break;
    5588           0 :         case PDFWriter::Seascape: aLine.append( "/Rotate -90\n" );break;
    5589             : 
    5590             :         case PDFWriter::Inherit: // actually Inherit would be a bug, but insignificant
    5591             :         case PDFWriter::Portrait:
    5592             :         default:
    5593           0 :             break;
    5594             :     }
    5595           0 :     sal_Int32 nMediaBoxWidth = 0;
    5596           0 :     sal_Int32 nMediaBoxHeight = 0;
    5597           0 :     if( m_aPages.empty() ) // sanity check, this should not happen
    5598             :     {
    5599           0 :         nMediaBoxWidth = m_nInheritedPageWidth;
    5600           0 :         nMediaBoxHeight = m_nInheritedPageHeight;
    5601             :     }
    5602             :     else
    5603             :     {
    5604           0 :         for( std::vector<PDFPage>::const_iterator iter = m_aPages.begin(); iter != m_aPages.end(); ++iter )
    5605             :         {
    5606           0 :             if( iter->m_nPageWidth > nMediaBoxWidth )
    5607           0 :                 nMediaBoxWidth = iter->m_nPageWidth;
    5608           0 :             if( iter->m_nPageHeight > nMediaBoxHeight )
    5609           0 :                 nMediaBoxHeight = iter->m_nPageHeight;
    5610             :         }
    5611             :     }
    5612           0 :     aLine.append( "/MediaBox[ 0 0 " );
    5613           0 :     aLine.append( nMediaBoxWidth );
    5614           0 :     aLine.append( ' ' );
    5615           0 :     aLine.append( nMediaBoxHeight );
    5616             :     aLine.append( " ]\n"
    5617           0 :                   "/Kids[ " );
    5618           0 :     unsigned int i = 0;
    5619           0 :     for( std::vector<PDFPage>::const_iterator iter = m_aPages.begin(); iter != m_aPages.end(); ++iter, i++ )
    5620             :     {
    5621           0 :         aLine.append( iter->m_nPageObject );
    5622           0 :         aLine.append( " 0 R" );
    5623           0 :         aLine.append( ( (i&15) == 15 ) ? "\n" : " " );
    5624             :     }
    5625             :     aLine.append( "]\n"
    5626           0 :                   "/Count " );
    5627           0 :     aLine.append( (sal_Int32)m_aPages.size() );
    5628             :     aLine.append( ">>\n"
    5629           0 :                   "endobj\n\n" );
    5630           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5631             : 
    5632             :     // emit annotation objects
    5633           0 :     CHECK_RETURN( emitAnnotations() );
    5634             : 
    5635             :     // emit Catalog
    5636           0 :     m_nCatalogObject = createObject();
    5637           0 :     if( ! updateObject( m_nCatalogObject ) )
    5638           0 :         return false;
    5639           0 :     aLine.setLength( 0 );
    5640           0 :     aLine.append( m_nCatalogObject );
    5641             :     aLine.append( " 0 obj\n"
    5642           0 :                   "<</Type/Catalog/Pages " );
    5643           0 :     aLine.append( nTreeNode );
    5644           0 :     aLine.append( " 0 R\n" );
    5645             : //--->i56629
    5646             :     // check if there are named destinations to emit (root must be inside the catalog)
    5647           0 :     if( nNamedDestinationsDictionary )
    5648             :     {
    5649           0 :         aLine.append("/Dests ");
    5650           0 :         aLine.append( nNamedDestinationsDictionary );
    5651           0 :         aLine.append( " 0 R\n" );
    5652             :     }
    5653             : //<----
    5654           0 :     if( m_aContext.PageLayout != PDFWriter::DefaultLayout )
    5655           0 :         switch(  m_aContext.PageLayout )
    5656             :         {
    5657             :         default :
    5658             :         case  PDFWriter::SinglePage :
    5659           0 :             aLine.append( "/PageLayout/SinglePage\n" );
    5660           0 :             break;
    5661             :         case  PDFWriter::Continuous :
    5662           0 :             aLine.append( "/PageLayout/OneColumn\n" );
    5663           0 :             break;
    5664             :         case  PDFWriter::ContinuousFacing :
    5665             :             // the flag m_aContext.FirstPageLeft below is used to set the page on the left side
    5666           0 :             aLine.append( "/PageLayout/TwoColumnRight\n" );//odd page on the right side
    5667           0 :             break;
    5668             :         }
    5669           0 :     if( m_aContext.PDFDocumentMode != PDFWriter::ModeDefault && !m_aContext.OpenInFullScreenMode )
    5670           0 :         switch(  m_aContext.PDFDocumentMode )
    5671             :         {
    5672             :         default :
    5673           0 :             aLine.append( "/PageMode/UseNone\n" );
    5674           0 :             break;
    5675             :         case PDFWriter::UseOutlines :
    5676           0 :             aLine.append( "/PageMode/UseOutlines\n" ); //document is opened with outline pane open
    5677           0 :             break;
    5678             :         case PDFWriter::UseThumbs :
    5679           0 :             aLine.append( "/PageMode/UseThumbs\n" ); //document is opened with thumbnails pane open
    5680           0 :             break;
    5681             :         }
    5682           0 :     else if( m_aContext.OpenInFullScreenMode )
    5683           0 :         aLine.append( "/PageMode/FullScreen\n" ); //document is opened full screen
    5684             : 
    5685           0 :     OStringBuffer aInitPageRef;
    5686           0 :     if( m_aContext.InitialPage >= 0 && m_aContext.InitialPage < (sal_Int32)m_aPages.size() )
    5687             :     {
    5688           0 :         aInitPageRef.append( m_aPages[m_aContext.InitialPage].m_nPageObject );
    5689           0 :         aInitPageRef.append( " 0 R" );
    5690             :     }
    5691             :     else
    5692           0 :         aInitPageRef.append( "0" );
    5693             : 
    5694           0 :     switch( m_aContext.PDFDocumentAction )
    5695             :     {
    5696             :     case PDFWriter::ActionDefault :     //do nothing, this is the Acrobat default
    5697             :     default:
    5698           0 :         if( aInitPageRef.getLength() > 1 )
    5699             :         {
    5700           0 :             aLine.append( "/OpenAction[" );
    5701           0 :             aLine.append( aInitPageRef.makeStringAndClear() );
    5702           0 :             aLine.append( " /XYZ null null 0]\n" );
    5703             :         }
    5704           0 :         break;
    5705             :     case PDFWriter::FitInWindow :
    5706           0 :         aLine.append( "/OpenAction[" );
    5707           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5708           0 :         aLine.append( " /Fit]\n" ); //Open fit page
    5709           0 :         break;
    5710             :     case PDFWriter::FitWidth :
    5711           0 :         aLine.append( "/OpenAction[" );
    5712           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5713           0 :         aLine.append( " /FitH " );
    5714           0 :         aLine.append( m_nInheritedPageHeight );//Open fit width
    5715           0 :         aLine.append( "]\n" );
    5716           0 :         break;
    5717             :     case PDFWriter::FitVisible :
    5718           0 :         aLine.append( "/OpenAction[" );
    5719           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5720           0 :         aLine.append( " /FitBH " );
    5721           0 :         aLine.append( m_nInheritedPageHeight );//Open fit visible
    5722           0 :         aLine.append( "]\n" );
    5723           0 :         break;
    5724             :     case PDFWriter::ActionZoom :
    5725           0 :         aLine.append( "/OpenAction[" );
    5726           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5727           0 :         aLine.append( " /XYZ null null " );
    5728           0 :         if( m_aContext.Zoom >= 50 && m_aContext.Zoom <= 1600 )
    5729           0 :             aLine.append( (double)m_aContext.Zoom/100.0 );
    5730             :         else
    5731           0 :             aLine.append( "0" );
    5732           0 :         aLine.append( "]\n" );
    5733           0 :         break;
    5734             :     }
    5735             : 
    5736             :     // viewer preferences, if we had some, then emit
    5737           0 :     if( m_aContext.HideViewerToolbar ||
    5738           0 :         ( m_aContext.Version > PDFWriter::PDF_1_3 && !m_aContext.DocumentInfo.Title.isEmpty() && m_aContext.DisplayPDFDocumentTitle ) ||
    5739           0 :         m_aContext.HideViewerMenubar ||
    5740           0 :         m_aContext.HideViewerWindowControls || m_aContext.FitWindow ||
    5741           0 :         m_aContext.CenterWindow || (m_aContext.FirstPageLeft  &&  m_aContext.PageLayout == PDFWriter::ContinuousFacing ) ||
    5742             :         m_aContext.OpenInFullScreenMode )
    5743             :     {
    5744           0 :         aLine.append( "/ViewerPreferences<<" );
    5745           0 :         if( m_aContext.HideViewerToolbar )
    5746           0 :             aLine.append( "/HideToolbar true\n" );
    5747           0 :         if( m_aContext.HideViewerMenubar )
    5748           0 :             aLine.append( "/HideMenubar true\n" );
    5749           0 :         if( m_aContext.HideViewerWindowControls )
    5750           0 :             aLine.append( "/HideWindowUI true\n" );
    5751           0 :         if( m_aContext.FitWindow )
    5752           0 :             aLine.append( "/FitWindow true\n" );
    5753           0 :         if( m_aContext.CenterWindow )
    5754           0 :             aLine.append( "/CenterWindow true\n" );
    5755           0 :         if( m_aContext.Version > PDFWriter::PDF_1_3 && !m_aContext.DocumentInfo.Title.isEmpty() && m_aContext.DisplayPDFDocumentTitle )
    5756           0 :             aLine.append( "/DisplayDocTitle true\n" );
    5757           0 :         if( m_aContext.FirstPageLeft &&  m_aContext.PageLayout == PDFWriter::ContinuousFacing )
    5758           0 :             aLine.append( "/Direction/R2L\n" );
    5759           0 :         if( m_aContext.OpenInFullScreenMode )
    5760           0 :             switch( m_aContext.PDFDocumentMode )
    5761             :             {
    5762             :             default :
    5763             :             case PDFWriter::ModeDefault :
    5764           0 :                 aLine.append( "/NonFullScreenPageMode/UseNone\n" );
    5765           0 :                 break;
    5766             :             case PDFWriter::UseOutlines :
    5767           0 :                 aLine.append( "/NonFullScreenPageMode/UseOutlines\n" );
    5768           0 :                 break;
    5769             :             case PDFWriter::UseThumbs :
    5770           0 :                 aLine.append( "/NonFullScreenPageMode/UseThumbs\n" );
    5771           0 :                 break;
    5772             :             }
    5773           0 :         aLine.append( ">>\n" );
    5774             :     }
    5775             : 
    5776           0 :     if( nOutlineDict )
    5777             :     {
    5778           0 :         aLine.append( "/Outlines " );
    5779           0 :         aLine.append( nOutlineDict );
    5780           0 :         aLine.append( " 0 R\n" );
    5781             :     }
    5782           0 :     if( nStructureDict )
    5783             :     {
    5784           0 :         aLine.append( "/StructTreeRoot " );
    5785           0 :         aLine.append( nStructureDict );
    5786           0 :         aLine.append( " 0 R\n" );
    5787             :     }
    5788           0 :     if( !m_aContext.DocumentLocale.Language.isEmpty() )
    5789             :     {
    5790             :         /* PDF allows only RFC 3066, see above in emitStructure(). */
    5791           0 :         LanguageTag aLanguageTag( m_aContext.DocumentLocale);
    5792           0 :         OUString aLanguage, aScript, aCountry;
    5793           0 :         aLanguageTag.getIsoLanguageScriptCountry( aLanguage, aScript, aCountry);
    5794           0 :         if (!aLanguage.isEmpty())
    5795             :         {
    5796           0 :             OUStringBuffer aLocBuf( 16 );
    5797           0 :             aLocBuf.append( aLanguage );
    5798           0 :             if( !aCountry.isEmpty() )
    5799             :             {
    5800           0 :                 aLocBuf.append( '-' );
    5801           0 :                 aLocBuf.append( aCountry );
    5802             :             }
    5803           0 :             aLine.append( "/Lang" );
    5804           0 :             appendLiteralStringEncrypt( aLocBuf.makeStringAndClear(), m_nCatalogObject, aLine );
    5805           0 :             aLine.append( "\n" );
    5806           0 :         }
    5807             :     }
    5808           0 :     if( m_aContext.Tagged && m_aContext.Version > PDFWriter::PDF_1_3 )
    5809             :     {
    5810           0 :         aLine.append( "/MarkInfo<</Marked true>>\n" );
    5811             :     }
    5812           0 :     if( m_aWidgets.size() > 0 )
    5813             :     {
    5814           0 :         aLine.append( "/AcroForm<</Fields[\n" );
    5815           0 :         int nWidgets = m_aWidgets.size();
    5816           0 :         int nOut = 0;
    5817           0 :         for( int j = 0; j < nWidgets; j++ )
    5818             :         {
    5819             :             // output only root fields
    5820           0 :             if( m_aWidgets[j].m_nParent < 1 )
    5821             :             {
    5822           0 :                 aLine.append( m_aWidgets[j].m_nObject );
    5823           0 :                 aLine.append( (nOut++ % 5)==4 ? " 0 R\n" : " 0 R " );
    5824             :             }
    5825             :         }
    5826           0 :         aLine.append( "\n]" );
    5827             : 
    5828             : #if !defined(ANDROID) && !defined(IOS)
    5829           0 :         if (m_nSignatureObject != -1)
    5830           0 :             aLine.append( "/SigFlags 3");
    5831             : #endif
    5832             : 
    5833           0 :         aLine.append( "/DR " );
    5834           0 :         aLine.append( getResourceDictObj() );
    5835           0 :         aLine.append( " 0 R" );
    5836             :         // /NeedAppearances must not be used if PDF is signed
    5837           0 :         if( m_bIsPDF_A1
    5838             : #if !defined(ANDROID) && !defined(IOS)
    5839           0 :             || ( m_nSignatureObject != -1 )
    5840             : #endif
    5841             :             )
    5842           0 :             aLine.append( ">>\n" );
    5843             :         else
    5844           0 :             aLine.append( "/NeedAppearances true>>\n" );
    5845             :     }
    5846             : 
    5847             : //--->i59651
    5848             :     //check if there is a Metadata object
    5849           0 :     if( nOutputIntentObject )
    5850             :     {
    5851           0 :         aLine.append("/OutputIntents[");
    5852           0 :         aLine.append( nOutputIntentObject );
    5853           0 :         aLine.append( " 0 R]" );
    5854             :     }
    5855             : 
    5856           0 :     if( nMetadataObject )
    5857             :     {
    5858           0 :         aLine.append("/Metadata ");
    5859           0 :         aLine.append( nMetadataObject );
    5860           0 :         aLine.append( " 0 R" );
    5861             :     }
    5862             : //<----
    5863             :     aLine.append( ">>\n"
    5864           0 :                   "endobj\n\n" );
    5865           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5866             : 
    5867           0 :     return true;
    5868             : }
    5869             : 
    5870             : #if !defined(ANDROID) && !defined(IOS)
    5871             : 
    5872           0 : bool PDFWriterImpl::emitSignature()
    5873             : {
    5874           0 :     if( !updateObject( m_nSignatureObject ) )
    5875           0 :         return false;
    5876             : 
    5877           0 :     OStringBuffer aLine( 0x5000 );
    5878           0 :     aLine.append( m_nSignatureObject );
    5879           0 :     aLine.append( " 0 obj\n" );
    5880           0 :     aLine.append("<</Contents <" );
    5881             : 
    5882           0 :     sal_uInt64 nOffset = ~0U;
    5883           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nOffset ) ) );
    5884             : 
    5885           0 :     m_nSignatureContentOffset = nOffset + aLine.getLength();
    5886             : 
    5887             :     // reserve some space for the PKCS#7 object
    5888           0 :     OStringBuffer aContentFiller( MAX_SIGNATURE_CONTENT_LENGTH );
    5889           0 :     comphelper::string::padToLength(aContentFiller, MAX_SIGNATURE_CONTENT_LENGTH, '0');
    5890           0 :     aLine.append( aContentFiller.makeStringAndClear() );
    5891           0 :     aLine.append( ">\n/Type/Sig/SubFilter/adbe.pkcs7.detached");
    5892             : 
    5893           0 :     if( !m_aContext.DocumentInfo.Author.isEmpty() )
    5894             :     {
    5895           0 :         aLine.append( "/Name" );
    5896           0 :         appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Author, m_nSignatureObject, aLine );
    5897             :     }
    5898             : 
    5899           0 :     aLine.append( " /M ");
    5900           0 :     appendLiteralStringEncrypt( m_aCreationDateString, m_nSignatureObject, aLine );
    5901             : 
    5902           0 :     aLine.append( " /ByteRange [ 0 ");
    5903           0 :     aLine.append( m_nSignatureContentOffset - 1, 10 );
    5904           0 :     aLine.append( " " );
    5905           0 :     aLine.append( m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1, 10 );
    5906           0 :     aLine.append( " " );
    5907             : 
    5908           0 :     m_nSignatureLastByteRangeNoOffset = nOffset + aLine.getLength();
    5909             : 
    5910             :     // mark the last ByteRange no and add some space. Now, we don't know
    5911             :     // how many bytes we need for this ByteRange value
    5912             :     // The real value will be overwritten in the finalizeSignature method
    5913           0 :     OStringBuffer aByteRangeFiller( 100  );
    5914           0 :     comphelper::string::padToLength(aByteRangeFiller, 100, ' ');
    5915           0 :     aLine.append( aByteRangeFiller.makeStringAndClear() );
    5916           0 :     aLine.append("  /Filter/Adobe.PPKMS");
    5917             : 
    5918             :     //emit reason, location and contactinfo
    5919           0 :     if ( !m_aContext.SignReason.isEmpty() )
    5920             :     {
    5921           0 :         aLine.append("/Reason");
    5922           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignReason, m_nSignatureObject, aLine );
    5923             :     }
    5924             : 
    5925           0 :     if ( !m_aContext.SignLocation.isEmpty() )
    5926             :     {
    5927           0 :         aLine.append("/Location");
    5928           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignLocation, m_nSignatureObject, aLine );
    5929             :     }
    5930             : 
    5931           0 :     if ( !m_aContext.SignContact.isEmpty() )
    5932             :     {
    5933           0 :         aLine.append("/ContactInfo");
    5934           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignContact, m_nSignatureObject, aLine );
    5935             :     }
    5936             : 
    5937           0 :     aLine.append(" >>\nendobj\n\n" );
    5938             : 
    5939           0 :     if (!writeBuffer( aLine.getStr(), aLine.getLength() ))
    5940           0 :         return false;
    5941             : 
    5942           0 :     return true;
    5943             : }
    5944             : 
    5945           0 : char *PDFSigningPKCS7PasswordCallback(PK11SlotInfo * /*slot*/, PRBool /*retry*/, void *arg)
    5946             : {
    5947           0 :     return (char *)arg;
    5948             : }
    5949             : 
    5950           0 : bool PDFWriterImpl::finalizeSignature()
    5951             : {
    5952             : 
    5953           0 :     if (!m_aContext.SignCertificate.is())
    5954           0 :         return false;
    5955             : 
    5956             :     // 1- calculate last ByteRange value
    5957           0 :     sal_uInt64 nOffset = ~0U;
    5958           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nOffset ) ) );
    5959             : 
    5960           0 :     sal_Int64 nLastByteRangeNo = nOffset - (m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1);
    5961             : 
    5962             :     // 2- overwrite the value to the m_nSignatureLastByteRangeNoOffset position
    5963           0 :     sal_uInt64 nWritten = 0;
    5964           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, m_nSignatureLastByteRangeNoOffset ) ) );
    5965           0 :     OStringBuffer aByteRangeNo( 256 );
    5966           0 :     aByteRangeNo.append( nLastByteRangeNo, 10);
    5967           0 :     aByteRangeNo.append( " ]" );
    5968             : 
    5969           0 :     if( osl_writeFile( m_aFile, aByteRangeNo.getStr(), aByteRangeNo.getLength(), &nWritten ) != osl_File_E_None )
    5970             :     {
    5971           0 :         CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, nOffset ) ) );
    5972           0 :         return false;
    5973             :     }
    5974             : 
    5975             :     // 3- create the PKCS#7 object using NSS
    5976           0 :     com::sun::star::uno::Sequence< sal_Int8 > derEncoded = m_aContext.SignCertificate->getEncoded();
    5977             : 
    5978           0 :     if (!derEncoded.hasElements())
    5979           0 :         return false;
    5980             : 
    5981           0 :     sal_Int8* n_derArray = derEncoded.getArray();
    5982           0 :     sal_Int32 n_derLength = derEncoded.getLength();
    5983             : 
    5984           0 :     NSS_NoDB_Init(".");
    5985             : 
    5986           0 :     CERTCertificate *cert = CERT_DecodeCertFromPackage(reinterpret_cast<char *>(n_derArray), n_derLength);
    5987             : 
    5988           0 :     if (!cert)
    5989             :     {
    5990             :         SAL_WARN("vcl.gdi", "PDF Signing: Error occured, certificate cannot be reconstructed.");
    5991           0 :         return false;
    5992             :     }
    5993             : 
    5994             :     SAL_WARN("vcl.gdi", "PDF Signing: Certificate Subject: " <<  cert->subjectName << "\n\tCertificate Issuer: " << cert->issuerName);
    5995             : 
    5996             :     // Prepare buffer and calculate PDF file digest
    5997           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, 0) ) );
    5998             : 
    5999           0 :     boost::scoped_ptr<HASHContext> hc(HASH_Create(HASH_AlgSHA1));
    6000             : 
    6001           0 :     if (!hc)
    6002             :     {
    6003             :         SAL_WARN("vcl.gdi", "PDF Signing: SHA1 HASH_Create failed!");
    6004           0 :         return false;
    6005             :     }
    6006             : 
    6007           0 :     HASH_Begin(hc.get());
    6008             : 
    6009           0 :     boost::scoped_array<char> buffer(new char[m_nSignatureContentOffset + 1]);
    6010             :     sal_uInt64 bytesRead;
    6011             : 
    6012             :     //FIXME: Check if SHA1 is calculated from the correct byterange
    6013             : 
    6014           0 :     CHECK_RETURN( (osl_File_E_None == osl_readFile( m_aFile, buffer.get(), m_nSignatureContentOffset - 1 , &bytesRead ) ) );
    6015           0 :     if (bytesRead != (sal_uInt64)m_nSignatureContentOffset - 1)
    6016             :         SAL_WARN("vcl.gdi", "PDF Signing: First buffer read failed!");
    6017             : 
    6018           0 :     HASH_Update(hc.get(), reinterpret_cast<const unsigned char*>(buffer.get()), bytesRead);
    6019             : 
    6020           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1) ) );
    6021           0 :     buffer.reset(new char[nLastByteRangeNo + 1]);
    6022           0 :     CHECK_RETURN( (osl_File_E_None == osl_readFile( m_aFile, buffer.get(), nLastByteRangeNo, &bytesRead ) ) );
    6023           0 :     if (bytesRead != (sal_uInt64) nLastByteRangeNo)
    6024             :         SAL_WARN("vcl.gdi", "PDF Signing: Second buffer read failed!");
    6025             : 
    6026           0 :     HASH_Update(hc.get(), reinterpret_cast<const unsigned char*>(buffer.get()), bytesRead);
    6027             : 
    6028             :     SECItem digest;
    6029             :     unsigned char hash[SHA1_LENGTH];
    6030           0 :     digest.data = hash;
    6031           0 :     HASH_End(hc.get(), digest.data, &digest.len, SHA1_LENGTH);
    6032           0 :     HASH_Destroy(hc.get());
    6033             : 
    6034           0 :     const char *pass = OUStringToOString( m_aContext.SignPassword, RTL_TEXTENCODING_UTF8 ).getStr();
    6035             : 
    6036           0 :     NSSCMSMessage *cms_msg = NSS_CMSMessage_Create(NULL);
    6037           0 :     if (!cms_msg)
    6038             :     {
    6039             :         SAL_WARN("vcl.gdi", "PDF signing: can't create new CMS message.");
    6040           0 :         return false;
    6041             :     }
    6042             : 
    6043           0 :     NSSCMSSignedData *cms_sd = NSS_CMSSignedData_Create(cms_msg);
    6044           0 :     if (!cms_sd)
    6045             :     {
    6046             :         SAL_WARN("vcl.gdi", "PDF signing: can't create CMS SignedData.");
    6047           0 :         return false;
    6048             :     }
    6049             : 
    6050           0 :     NSSCMSContentInfo *cms_cinfo = NSS_CMSMessage_GetContentInfo(cms_msg);
    6051           0 :     if (NSS_CMSContentInfo_SetContent_SignedData(cms_msg, cms_cinfo, cms_sd) != SECSuccess)
    6052             :     {
    6053             :         SAL_WARN("vcl.gdi", "PDF signing: Can't set CMS content signed data.");
    6054           0 :         return false;
    6055             :     }
    6056             : 
    6057           0 :     cms_cinfo = NSS_CMSSignedData_GetContentInfo(cms_sd);
    6058             :     //attach NULL data as detached data
    6059           0 :     if (NSS_CMSContentInfo_SetContent_Data(cms_msg, cms_cinfo, NULL, PR_TRUE) != SECSuccess)
    6060             :     {
    6061             :         SAL_WARN("vcl.gdi", "PDF signing: Can't set CMS content data.");
    6062           0 :         return false;
    6063             :     }
    6064             : 
    6065           0 :     NSSCMSSignerInfo *cms_signer = NSS_CMSSignerInfo_Create(cms_msg, cert, SEC_OID_SHA1);
    6066           0 :     if (!cms_signer)
    6067             :     {
    6068             :         SAL_WARN("vcl.gdi", "PDF signing: can't create CMS SignerInfo.");
    6069           0 :         return false;
    6070             :     }
    6071             : 
    6072           0 :     if (NSS_CMSSignerInfo_IncludeCerts(cms_signer, NSSCMSCM_CertChain, certUsageEmailSigner) != SECSuccess)
    6073             :     {
    6074             :         SAL_WARN("vcl.gdi", "PDF signing: can't include cert chain.");
    6075           0 :         return false;
    6076             :     }
    6077             : 
    6078           0 :     if (NSS_CMSSignerInfo_AddSigningTime(cms_signer, PR_Now()) != SECSuccess)
    6079             :     {
    6080             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signing time.");
    6081           0 :         return false;
    6082             :     }
    6083             : 
    6084           0 :     if (NSS_CMSSignedData_AddCertificate(cms_sd, cert) != SECSuccess)
    6085             :     {
    6086             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signer certificate.");
    6087           0 :         return false;
    6088             :     }
    6089             : 
    6090           0 :     if (NSS_CMSSignedData_AddSignerInfo(cms_sd, cms_signer) != SECSuccess)
    6091             :     {
    6092             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signer info.");
    6093           0 :         return false;
    6094             :     }
    6095             : 
    6096           0 :     if (NSS_CMSSignedData_SetDigestValue(cms_sd, SEC_OID_SHA1, &digest) != SECSuccess)
    6097             :     {
    6098             :         SAL_WARN("vcl.gdi", "PDF signing: can't set PDF digest value.");
    6099           0 :         return false;
    6100             :     }
    6101             : 
    6102             :     SAL_WARN("vcl.gdi","PKCS7 Object created successfully!");
    6103             : 
    6104             :     SECItem cms_output;
    6105           0 :     cms_output.data = 0;
    6106           0 :     cms_output.len = 0;
    6107           0 :     PLArenaPool *arena = PORT_NewArena(MAX_SIGNATURE_CONTENT_LENGTH);
    6108             :     NSSCMSEncoderContext *cms_ecx;
    6109             : 
    6110             :     //FIXME: Check if password is passed correctly to SEC_PKCS7CreateSignedData function
    6111           0 :     cms_ecx = NSS_CMSEncoder_Start(cms_msg, NULL, NULL, &cms_output, arena, (PK11PasswordFunc)::PDFSigningPKCS7PasswordCallback, (void *)pass, NULL, NULL, NULL, NULL);
    6112             : 
    6113           0 :     if (!cms_ecx)
    6114             :     {
    6115             :         SAL_WARN("vcl.gdi", "PDF Signing: can't start DER encoder.");
    6116           0 :         return false;
    6117             :     }
    6118             :     SAL_WARN("vcl.gdi", "PDF Signing: Started DER encoding.");
    6119             : 
    6120           0 :     if (NSS_CMSEncoder_Finish(cms_ecx) != SECSuccess)
    6121             :     {
    6122             :         SAL_WARN("vcl.gdi", "PDF Signing: can't finish DER encoder.");
    6123           0 :         return false;
    6124             :     }
    6125             :     SAL_WARN("vcl.gdi", "PDF Signing: Finished DER encoding.");
    6126             : 
    6127           0 :     OStringBuffer cms_hexbuffer;
    6128             : 
    6129           0 :     for (unsigned int i = 0; i < cms_output.len ; i++)
    6130           0 :         appendHex(cms_output.data[i], cms_hexbuffer);
    6131             : 
    6132             :     SAL_WARN("vcl.gdi","PKCS7 object encoded successfully!");
    6133             : 
    6134             :     // Set file pointer to the m_nSignatureContentOffset, we're ready to overwrite PKCS7 object
    6135           0 :     nWritten = 0;
    6136           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, m_nSignatureContentOffset) ) );
    6137           0 :     osl_writeFile(m_aFile, cms_hexbuffer.getStr(), cms_hexbuffer.getLength(), &nWritten);
    6138             : 
    6139           0 :     NSS_CMSMessage_Destroy(cms_msg);
    6140             : 
    6141           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, nOffset ) ) );
    6142           0 :     return true;
    6143             : }
    6144             : 
    6145             : #endif
    6146             : 
    6147           0 : sal_Int32 PDFWriterImpl::emitInfoDict( )
    6148             : {
    6149           0 :     sal_Int32 nObject = createObject();
    6150             : 
    6151           0 :     if( updateObject( nObject ) )
    6152             :     {
    6153           0 :         OStringBuffer aLine( 1024 );
    6154           0 :         aLine.append( nObject );
    6155             :         aLine.append( " 0 obj\n"
    6156           0 :                       "<<" );
    6157           0 :         if( !m_aContext.DocumentInfo.Title.isEmpty() )
    6158             :         {
    6159           0 :             aLine.append( "/Title" );
    6160           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Title, nObject, aLine );
    6161           0 :             aLine.append( "\n" );
    6162             :         }
    6163           0 :         if( !m_aContext.DocumentInfo.Author.isEmpty() )
    6164             :         {
    6165           0 :             aLine.append( "/Author" );
    6166           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Author, nObject, aLine );
    6167           0 :             aLine.append( "\n" );
    6168             :         }
    6169           0 :         if( !m_aContext.DocumentInfo.Subject.isEmpty() )
    6170             :         {
    6171           0 :             aLine.append( "/Subject" );
    6172           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Subject, nObject, aLine );
    6173           0 :             aLine.append( "\n" );
    6174             :         }
    6175           0 :         if( !m_aContext.DocumentInfo.Keywords.isEmpty() )
    6176             :         {
    6177           0 :             aLine.append( "/Keywords" );
    6178           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Keywords, nObject, aLine );
    6179           0 :             aLine.append( "\n" );
    6180             :         }
    6181           0 :         if( !m_aContext.DocumentInfo.Creator.isEmpty() )
    6182             :         {
    6183           0 :             aLine.append( "/Creator" );
    6184           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Creator, nObject, aLine );
    6185           0 :             aLine.append( "\n" );
    6186             :         }
    6187           0 :         if( !m_aContext.DocumentInfo.Producer.isEmpty() )
    6188             :         {
    6189           0 :             aLine.append( "/Producer" );
    6190           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Producer, nObject, aLine );
    6191           0 :             aLine.append( "\n" );
    6192             :         }
    6193             : 
    6194           0 :          aLine.append( "/CreationDate" );
    6195           0 :          appendLiteralStringEncrypt( m_aCreationDateString, nObject, aLine );
    6196           0 :         aLine.append( ">>\nendobj\n\n" );
    6197           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6198           0 :             nObject = 0;
    6199             :     }
    6200             :     else
    6201           0 :         nObject = 0;
    6202             : 
    6203           0 :     return nObject;
    6204             : }
    6205             : 
    6206             : //--->i56629
    6207             : // Part of this function may be shared with method appendDest.
    6208           0 : sal_Int32 PDFWriterImpl::emitNamedDestinations()
    6209             : {
    6210           0 :     sal_Int32  nCount = m_aNamedDests.size();
    6211           0 :     if( nCount <= 0 )
    6212           0 :         return 0;//define internal error
    6213             : 
    6214             :     //get the object number for all the destinations
    6215           0 :     sal_Int32 nObject = createObject();
    6216             : 
    6217           0 :     if( updateObject( nObject ) )
    6218             :     {
    6219             :         //emit the dictionary
    6220           0 :         OStringBuffer aLine( 1024 );
    6221           0 :         aLine.append( nObject );
    6222             :         aLine.append( " 0 obj\n"
    6223           0 :                       "<<" );
    6224             : 
    6225             :         sal_Int32  nDestID;
    6226           0 :         for( nDestID = 0; nDestID < nCount; nDestID++ )
    6227             :         {
    6228           0 :             const PDFNamedDest& rDest   = m_aNamedDests[ nDestID ];
    6229             :             // In order to correctly function both under an Internet browser and
    6230             :             // directly with a reader (provided the reader has the feature) we
    6231             :             // need to set the name of the destination the same way it will be encoded
    6232             :             // in an Internet link
    6233             :             INetURLObject aLocalURL(
    6234           0 :                 OUString( "http://ahost.ax"  ) ); //dummy location, won't be used
    6235           0 :             aLocalURL.SetMark( rDest.m_aDestName );
    6236             : 
    6237           0 :             const OUString aName   = aLocalURL.GetMark( INetURLObject::NO_DECODE ); //same coding as
    6238             :             // in link creation ( see PDFWriterImpl::emitLinkAnnotations )
    6239           0 :             const PDFPage& rDestPage    = m_aPages[ rDest.m_nPage ];
    6240             : 
    6241           0 :             aLine.append( '/' );
    6242           0 :             appendDestinationName( aName, aLine ); // this conversion must be done when forming the link to target ( see in emitCatalog )
    6243           0 :             aLine.append( '[' ); // the '[' can be emitted immediately, because the appendDestinationName function
    6244             :                                  //maps the preceeding character properly
    6245           0 :             aLine.append( rDestPage.m_nPageObject );
    6246           0 :             aLine.append( " 0 R" );
    6247             : 
    6248           0 :             switch( rDest.m_eType )
    6249             :             {
    6250             :             case PDFWriter::XYZ:
    6251             :             default:
    6252           0 :                 aLine.append( "/XYZ " );
    6253           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6254           0 :                 aLine.append( ' ' );
    6255           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6256           0 :                 aLine.append( " 0" );
    6257           0 :                 break;
    6258             :             case PDFWriter::Fit:
    6259           0 :                 aLine.append( "/Fit" );
    6260           0 :                 break;
    6261             :             case PDFWriter::FitRectangle:
    6262           0 :                 aLine.append( "/FitR " );
    6263           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6264           0 :                 aLine.append( ' ' );
    6265           0 :                 appendFixedInt( rDest.m_aRect.Top(), aLine );
    6266           0 :                 aLine.append( ' ' );
    6267           0 :                 appendFixedInt( rDest.m_aRect.Right(), aLine );
    6268           0 :                 aLine.append( ' ' );
    6269           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6270           0 :                 break;
    6271             :             case PDFWriter::FitHorizontal:
    6272           0 :                 aLine.append( "/FitH " );
    6273           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6274           0 :                 break;
    6275             :             case PDFWriter::FitVertical:
    6276           0 :                 aLine.append( "/FitV " );
    6277           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6278           0 :                 break;
    6279             :             case PDFWriter::FitPageBoundingBox:
    6280           0 :                 aLine.append( "/FitB" );
    6281           0 :                 break;
    6282             :             case PDFWriter::FitPageBoundingBoxHorizontal:
    6283           0 :                 aLine.append( "/FitBH " );
    6284           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6285           0 :                 break;
    6286             :             case PDFWriter::FitPageBoundingBoxVertical:
    6287           0 :                 aLine.append( "/FitBV " );
    6288           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6289           0 :                 break;
    6290             :             }
    6291           0 :             aLine.append( "]\n" );
    6292           0 :         }
    6293             : 
    6294             :         //close
    6295           0 :         aLine.append( ">>\nendobj\n\n" );
    6296           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6297           0 :             nObject = 0;
    6298             :     }
    6299             :     else
    6300           0 :         nObject = 0;
    6301             : 
    6302           0 :     return nObject;
    6303             : }
    6304             : //<--- i56629
    6305             : 
    6306             : //--->i59651
    6307             : // emits the output intent dictionary
    6308           0 : sal_Int32 PDFWriterImpl::emitOutputIntent()
    6309             : {
    6310           0 :     if( !m_bIsPDF_A1 )
    6311           0 :         return 0;
    6312             : 
    6313             :     //emit the sRGB standard profile, in ICC format, in a stream, per IEC61966-2.1
    6314             : 
    6315           0 :     OStringBuffer aLine( 1024 );
    6316           0 :     sal_Int32 nICCObject = createObject();
    6317           0 :     sal_Int32 nStreamLengthObject = createObject();
    6318             : 
    6319           0 :     aLine.append( nICCObject );
    6320             : // sRGB has 3 colors, hence /N 3 below (PDF 1.4 table 4.16)
    6321           0 :     aLine.append( " 0 obj\n<</N 3/Length " );
    6322           0 :     aLine.append( nStreamLengthObject );
    6323           0 :     aLine.append( " 0 R" );
    6324             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    6325           0 :     aLine.append( "/Filter/FlateDecode" );
    6326             : #endif
    6327           0 :     aLine.append( ">>\nstream\n" );
    6328           0 :     if ( !updateObject( nICCObject ) ) return 0;
    6329           0 :     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return 0;
    6330             :     //get file position
    6331           0 :     sal_uInt64 nBeginStreamPos = 0;
    6332           0 :     osl_getFilePos( m_aFile, &nBeginStreamPos );
    6333           0 :     beginCompression();
    6334           0 :     checkAndEnableStreamEncryption( nICCObject );
    6335           0 :     cmsHPROFILE hProfile = cmsCreate_sRGBProfile();
    6336             :     //force ICC profile version 2.1
    6337           0 :     cmsSetProfileVersion(hProfile, 2.1);
    6338           0 :     cmsUInt32Number nBytesNeeded = 0;
    6339           0 :     cmsSaveProfileToMem(hProfile, NULL, &nBytesNeeded);
    6340           0 :     if (!nBytesNeeded)
    6341           0 :       return 0;
    6342           0 :     std::vector<unsigned char> xBuffer(nBytesNeeded);
    6343           0 :     cmsSaveProfileToMem(hProfile, &xBuffer[0], &nBytesNeeded);
    6344           0 :     cmsCloseProfile(hProfile);
    6345           0 :     bool written = writeBuffer( &xBuffer[0], (sal_Int32) xBuffer.size() );
    6346           0 :     disableStreamEncryption();
    6347           0 :     endCompression();
    6348           0 :     sal_uInt64 nEndStreamPos = 0;
    6349           0 :     osl_getFilePos( m_aFile, &nEndStreamPos );
    6350             : 
    6351           0 :     if( !written )
    6352           0 :         return 0;
    6353           0 :     if( ! writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    6354           0 :         return 0 ;
    6355           0 :     aLine.setLength( 0 );
    6356             : 
    6357             :     //emit the stream length   object
    6358           0 :     if ( !updateObject( nStreamLengthObject ) ) return 0;
    6359           0 :     aLine.setLength( 0 );
    6360           0 :     aLine.append( nStreamLengthObject );
    6361           0 :     aLine.append( " 0 obj\n" );
    6362           0 :     aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos) );
    6363           0 :     aLine.append( "\nendobj\n\n" );
    6364           0 :     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return 0;
    6365           0 :     aLine.setLength( 0 );
    6366             : 
    6367             :     //emit the OutputIntent dictionary
    6368           0 :     sal_Int32 nOIObject = createObject();
    6369           0 :     if ( !updateObject( nOIObject ) ) return 0;
    6370           0 :     aLine.append( nOIObject );
    6371             :     aLine.append( " 0 obj\n"
    6372           0 :                   "<</Type/OutputIntent/S/GTS_PDFA1/OutputConditionIdentifier");
    6373             : 
    6374           0 :     OUString aComment( "sRGB IEC61966-2.1"  );
    6375           0 :     appendLiteralStringEncrypt( aComment ,nOIObject, aLine );
    6376           0 :     aLine.append("/DestOutputProfile ");
    6377           0 :     aLine.append( nICCObject );
    6378           0 :     aLine.append( " 0 R>>\nendobj\n\n" );;
    6379           0 :     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return 0;
    6380             : 
    6381           0 :     return nOIObject;
    6382             : }
    6383             : 
    6384             : // formats the string for the XML stream
    6385           0 : static void escapeStringXML( const OUString& rStr, OUString &rValue)
    6386             : {
    6387           0 :     const sal_Unicode* pUni = rStr.getStr();
    6388           0 :     int nLen = rStr.getLength();
    6389           0 :     for( ; nLen; nLen--, pUni++ )
    6390             :     {
    6391           0 :         switch( *pUni )
    6392             :         {
    6393             :         case sal_Unicode('&'):
    6394           0 :             rValue += "&amp;";
    6395           0 :         break;
    6396             :         case sal_Unicode('<'):
    6397           0 :             rValue += "&lt;";
    6398           0 :         break;
    6399             :         case sal_Unicode('>'):
    6400           0 :             rValue += "&gt;";
    6401           0 :         break;
    6402             :         case sal_Unicode('\''):
    6403           0 :             rValue += "&apos;";
    6404           0 :         break;
    6405             :         case sal_Unicode('"'):
    6406           0 :             rValue += "&quot;";
    6407           0 :         break;
    6408             :         default:
    6409           0 :             rValue += OUString( *pUni );
    6410           0 :             break;
    6411             :         }
    6412             :     }
    6413           0 : }
    6414             : 
    6415             : // emits the document metadata
    6416           0 : sal_Int32 PDFWriterImpl::emitDocumentMetadata()
    6417             : {
    6418           0 :     if( !m_bIsPDF_A1 )
    6419           0 :         return 0;
    6420             : 
    6421             :     //get the object number for all the destinations
    6422           0 :     sal_Int32 nObject = createObject();
    6423             : 
    6424           0 :     if( updateObject( nObject ) )
    6425             :     {
    6426             :         // the following string are written in UTF-8 unicode
    6427           0 :         OStringBuffer aMetadataStream( 8192 );
    6428             : 
    6429           0 :         aMetadataStream.append( "<?xpacket begin=\"" );
    6430             :         // these lines write Unicode "zero width non-breaking space character" (U+FEFF)
    6431             :         // (aka byte-order mark ) used as a byte-order marker.
    6432           0 :         aMetadataStream.append( OUStringToOString( OUString( sal_Unicode( 0xFEFF ) ), RTL_TEXTENCODING_UTF8 ) );
    6433           0 :         aMetadataStream.append( "\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" );
    6434           0 :         aMetadataStream.append( "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n" );
    6435           0 :         aMetadataStream.append( " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" );
    6436             :         //PDF/A part ( ISO 19005-1:2005 - 6.7.11 )
    6437           0 :         aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6438           0 :         aMetadataStream.append( "      xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n" );
    6439           0 :         aMetadataStream.append( "   <pdfaid:part>1</pdfaid:part>\n" );
    6440           0 :         aMetadataStream.append( "   <pdfaid:conformance>A</pdfaid:conformance>\n" );
    6441           0 :         aMetadataStream.append( "  </rdf:Description>\n" );
    6442             :         //... Dublin Core properties go here
    6443           0 :         if( !m_aContext.DocumentInfo.Title.isEmpty() ||
    6444           0 :             !m_aContext.DocumentInfo.Author.isEmpty() ||
    6445           0 :             !m_aContext.DocumentInfo.Subject.isEmpty() )
    6446             :         {
    6447           0 :             aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6448           0 :             aMetadataStream.append( "      xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" );
    6449           0 :             if( !m_aContext.DocumentInfo.Title.isEmpty() )
    6450             :             {
    6451             :                 // this is according to PDF/A-1, technical corrigendum 1 (2007-04-01)
    6452           0 :                 aMetadataStream.append( "   <dc:title>\n" );
    6453           0 :                 aMetadataStream.append( "    <rdf:Alt>\n" );
    6454           0 :                 aMetadataStream.append( "     <rdf:li xml:lang=\"x-default\">" );
    6455           0 :                 OUString aTitle;
    6456           0 :                 escapeStringXML( m_aContext.DocumentInfo.Title, aTitle );
    6457           0 :                 aMetadataStream.append( OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 )  );
    6458           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6459           0 :                 aMetadataStream.append( "    </rdf:Alt>\n" );
    6460           0 :                 aMetadataStream.append( "   </dc:title>\n" );
    6461             :             }
    6462           0 :             if( !m_aContext.DocumentInfo.Author.isEmpty() )
    6463             :             {
    6464           0 :                 aMetadataStream.append( "   <dc:creator>\n" );
    6465           0 :                 aMetadataStream.append( "    <rdf:Seq>\n" );
    6466           0 :                 aMetadataStream.append( "     <rdf:li>" );
    6467           0 :                 OUString aAuthor;
    6468           0 :                 escapeStringXML( m_aContext.DocumentInfo.Author, aAuthor );
    6469           0 :                 aMetadataStream.append( OUStringToOString( aAuthor , RTL_TEXTENCODING_UTF8 )  );
    6470           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6471           0 :                 aMetadataStream.append( "    </rdf:Seq>\n" );
    6472           0 :                 aMetadataStream.append( "   </dc:creator>\n" );
    6473             :             }
    6474           0 :             if( !m_aContext.DocumentInfo.Subject.isEmpty() )
    6475             :             {
    6476             :                 // this is according to PDF/A-1, technical corrigendum 1 (2007-04-01)
    6477           0 :                 aMetadataStream.append( "   <dc:description>\n" );
    6478           0 :                 aMetadataStream.append( "    <rdf:Alt>\n" );
    6479           0 :                 aMetadataStream.append( "     <rdf:li xml:lang=\"x-default\">" );
    6480           0 :                 OUString aSubject;
    6481           0 :                 escapeStringXML( m_aContext.DocumentInfo.Subject, aSubject );
    6482           0 :                 aMetadataStream.append( OUStringToOString( aSubject , RTL_TEXTENCODING_UTF8 )  );
    6483           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6484           0 :                 aMetadataStream.append( "    </rdf:Alt>\n" );
    6485           0 :                 aMetadataStream.append( "   </dc:description>\n" );
    6486             :             }
    6487           0 :             aMetadataStream.append( "  </rdf:Description>\n" );
    6488             :         }
    6489             : 
    6490             :         //... PDF properties go here
    6491           0 :         if( !m_aContext.DocumentInfo.Producer.isEmpty() ||
    6492           0 :             !m_aContext.DocumentInfo.Keywords.isEmpty() )
    6493             :         {
    6494           0 :             aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6495           0 :             aMetadataStream.append( "     xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n" );
    6496           0 :             if( !m_aContext.DocumentInfo.Producer.isEmpty() )
    6497             :             {
    6498           0 :                 aMetadataStream.append( "   <pdf:Producer>" );
    6499           0 :                 OUString aProducer;
    6500           0 :                 escapeStringXML( m_aContext.DocumentInfo.Producer, aProducer );
    6501           0 :                 aMetadataStream.append( OUStringToOString( aProducer , RTL_TEXTENCODING_UTF8 )  );
    6502           0 :                 aMetadataStream.append( "</pdf:Producer>\n" );
    6503             :             }
    6504           0 :             if( !m_aContext.DocumentInfo.Keywords.isEmpty() )
    6505             :             {
    6506           0 :                 aMetadataStream.append( "   <pdf:Keywords>" );
    6507           0 :                 OUString aKeywords;
    6508           0 :                 escapeStringXML( m_aContext.DocumentInfo.Keywords, aKeywords );
    6509           0 :                 aMetadataStream.append( OUStringToOString( aKeywords , RTL_TEXTENCODING_UTF8 )  );
    6510           0 :                 aMetadataStream.append( "</pdf:Keywords>\n" );
    6511             :             }
    6512           0 :             aMetadataStream.append( "  </rdf:Description>\n" );
    6513             :         }
    6514             : 
    6515           0 :         aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6516           0 :         aMetadataStream.append( "    xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\">\n" );
    6517           0 :         if( !m_aContext.DocumentInfo.Creator.isEmpty() )
    6518             :         {
    6519           0 :             aMetadataStream.append( "   <xmp:CreatorTool>" );
    6520           0 :             OUString aCreator;
    6521           0 :             escapeStringXML( m_aContext.DocumentInfo.Creator, aCreator );
    6522           0 :             aMetadataStream.append( OUStringToOString( aCreator , RTL_TEXTENCODING_UTF8 )  );
    6523           0 :             aMetadataStream.append( "</xmp:CreatorTool>\n" );
    6524             :         }
    6525             :         //creation date
    6526           0 :         aMetadataStream.append( "   <xmp:CreateDate>" );
    6527           0 :         aMetadataStream.append( m_aCreationMetaDateString );
    6528           0 :         aMetadataStream.append( "</xmp:CreateDate>\n" );
    6529             : 
    6530           0 :         aMetadataStream.append( "  </rdf:Description>\n" );
    6531           0 :         aMetadataStream.append( " </rdf:RDF>\n" );
    6532           0 :         aMetadataStream.append( "</x:xmpmeta>\n" );
    6533             : 
    6534             :         //add the padding
    6535           0 :         for( sal_Int32 nSpaces = 1; nSpaces <= 2100; nSpaces++ )
    6536             :         {
    6537           0 :             aMetadataStream.append( " " );
    6538           0 :             if( nSpaces % 100 == 0 )
    6539           0 :                 aMetadataStream.append( "\n" );
    6540             :         }
    6541             : 
    6542           0 :         aMetadataStream.append( "<?xpacket end=\"w\"?>\n" );
    6543             : 
    6544           0 :         OStringBuffer aMetadataObj( 1024 );
    6545             : 
    6546           0 :         aMetadataObj.append( nObject );
    6547           0 :         aMetadataObj.append( " 0 obj\n" );
    6548             : 
    6549           0 :         aMetadataObj.append( "<</Type/Metadata/Subtype/XML/Length " );
    6550             : 
    6551           0 :         aMetadataObj.append( (sal_Int32) aMetadataStream.getLength() );
    6552           0 :         aMetadataObj.append( ">>\nstream\n" );
    6553           0 :         if ( !writeBuffer( aMetadataObj.getStr(), aMetadataObj.getLength() ) )
    6554           0 :             return 0;
    6555             :         //emit the stream
    6556           0 :         if ( !writeBuffer( aMetadataStream.getStr(), aMetadataStream.getLength() ) )
    6557           0 :             return 0;
    6558             : 
    6559           0 :         aMetadataObj.setLength( 0 );
    6560           0 :         aMetadataObj.append( "\nendstream\nendobj\n\n" );
    6561           0 :         if( ! writeBuffer( aMetadataObj.getStr(), aMetadataObj.getLength() ) )
    6562           0 :             nObject = 0;
    6563             :     }
    6564             :     else
    6565           0 :         nObject = 0;
    6566             : 
    6567           0 :     return nObject;
    6568             : }
    6569             : //<---i59651
    6570             : 
    6571           0 : bool PDFWriterImpl::emitTrailer()
    6572             : {
    6573             :     // emit doc info
    6574           0 :     sal_Int32 nDocInfoObject = emitInfoDict( );
    6575             : 
    6576           0 :     sal_Int32 nSecObject = 0;
    6577             : 
    6578           0 :     if( m_aContext.Encryption.Encrypt() )
    6579             :     {
    6580             :         //emit the security information
    6581             :         //must be emitted as indirect dictionary object, since
    6582             :         //Acrobat Reader 5 works only with this kind of implementation
    6583           0 :         nSecObject = createObject();
    6584             : 
    6585           0 :         if( updateObject( nSecObject ) )
    6586             :         {
    6587           0 :             OStringBuffer aLineS( 1024 );
    6588           0 :             aLineS.append( nSecObject );
    6589             :             aLineS.append( " 0 obj\n"
    6590           0 :                            "<</Filter/Standard/V " );
    6591             :             // check the version
    6592           0 :             if( m_aContext.Encryption.Security128bit )
    6593           0 :                 aLineS.append( "2/Length 128/R 3" );
    6594             :             else
    6595           0 :                 aLineS.append( "1/R 2" );
    6596             : 
    6597             :             // emit the owner password, must not be encrypted
    6598           0 :             aLineS.append( "/O(" );
    6599           0 :             appendLiteralString( (const sal_Char*)&m_aContext.Encryption.OValue[0], sal_Int32(m_aContext.Encryption.OValue.size()), aLineS );
    6600           0 :             aLineS.append( ")/U(" );
    6601           0 :             appendLiteralString( (const sal_Char*)&m_aContext.Encryption.UValue[0], sal_Int32(m_aContext.Encryption.UValue.size()), aLineS );
    6602           0 :             aLineS.append( ")/P " );// the permission set
    6603           0 :             aLineS.append( m_nAccessPermissions );
    6604           0 :             aLineS.append( ">>\nendobj\n\n" );
    6605           0 :             if( !writeBuffer( aLineS.getStr(), aLineS.getLength() ) )
    6606           0 :                 nSecObject = 0;
    6607             :         }
    6608             :         else
    6609           0 :             nSecObject = 0;
    6610             :     }
    6611             :     // emit xref table
    6612             :     // remember start
    6613           0 :     sal_uInt64 nXRefOffset = 0;
    6614           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nXRefOffset )) );
    6615           0 :     CHECK_RETURN( writeBuffer( "xref\n", 5 ) );
    6616             : 
    6617           0 :     sal_Int32 nObjects = m_aObjects.size();
    6618           0 :     OStringBuffer aLine;
    6619           0 :     aLine.append( "0 " );
    6620           0 :     aLine.append( (sal_Int32)(nObjects+1) );
    6621           0 :     aLine.append( "\n" );
    6622           0 :     aLine.append( "0000000000 65535 f \n" );
    6623           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6624             : 
    6625           0 :     for( sal_Int32 i = 0; i < nObjects; i++ )
    6626             :     {
    6627           0 :         aLine.setLength( 0 );
    6628           0 :         OString aOffset = OString::number( m_aObjects[i] );
    6629           0 :         for( sal_Int32 j = 0; j < (10-aOffset.getLength()); j++ )
    6630           0 :             aLine.append( '0' );
    6631           0 :         aLine.append( aOffset );
    6632           0 :         aLine.append( " 00000 n \n" );
    6633             :         DBG_ASSERT( aLine.getLength() == 20, "invalid xref entry" );
    6634           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6635           0 :     }
    6636             : 
    6637             :     // prepare document checksum
    6638           0 :     OStringBuffer aDocChecksum( 2*RTL_DIGEST_LENGTH_MD5+1 );
    6639           0 :     if( m_aDocDigest )
    6640             :     {
    6641             :         sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
    6642           0 :         rtl_digest_getMD5( m_aDocDigest, nMD5Sum, sizeof(nMD5Sum) );
    6643           0 :         for( unsigned int i = 0; i < RTL_DIGEST_LENGTH_MD5; i++ )
    6644           0 :             appendHex( nMD5Sum[i], aDocChecksum );
    6645             :     }
    6646             :     // document id set in setDocInfo method
    6647             :     // emit trailer
    6648           0 :     aLine.setLength( 0 );
    6649             :     aLine.append( "trailer\n"
    6650           0 :                   "<</Size " );
    6651           0 :     aLine.append( (sal_Int32)(nObjects+1) );
    6652           0 :     aLine.append( "/Root " );
    6653           0 :     aLine.append( m_nCatalogObject );
    6654           0 :     aLine.append( " 0 R\n" );
    6655           0 :     if( nSecObject )
    6656             :     {
    6657           0 :         aLine.append( "/Encrypt ");
    6658           0 :         aLine.append( nSecObject );
    6659           0 :         aLine.append( " 0 R\n" );
    6660             :     }
    6661           0 :     if( nDocInfoObject )
    6662             :     {
    6663           0 :         aLine.append( "/Info " );
    6664           0 :         aLine.append( nDocInfoObject );
    6665           0 :         aLine.append( " 0 R\n" );
    6666             :     }
    6667           0 :     if( ! m_aContext.Encryption.DocumentIdentifier.empty() )
    6668             :     {
    6669           0 :         aLine.append( "/ID [ <" );
    6670           0 :         for( std::vector< sal_uInt8 >::const_iterator it = m_aContext.Encryption.DocumentIdentifier.begin();
    6671           0 :              it != m_aContext.Encryption.DocumentIdentifier.end(); ++it )
    6672             :         {
    6673           0 :             appendHex( sal_Int8(*it), aLine );
    6674             :         }
    6675             :         aLine.append( ">\n"
    6676           0 :                       "<" );
    6677           0 :         for( std::vector< sal_uInt8 >::const_iterator it = m_aContext.Encryption.DocumentIdentifier.begin();
    6678           0 :              it != m_aContext.Encryption.DocumentIdentifier.end(); ++it )
    6679             :         {
    6680           0 :             appendHex( sal_Int8(*it), aLine );
    6681             :         }
    6682           0 :         aLine.append( "> ]\n" );
    6683             :     }
    6684           0 :     if( !aDocChecksum.isEmpty() )
    6685             :     {
    6686           0 :         aLine.append( "/DocChecksum /" );
    6687           0 :         aLine.append( aDocChecksum.makeStringAndClear() );
    6688           0 :         aLine.append( "\n" );
    6689             :     }
    6690           0 :     if( m_aAdditionalStreams.size() > 0 )
    6691             :     {
    6692           0 :         aLine.append( "/AdditionalStreams [" );
    6693           0 :         for( unsigned int i = 0; i < m_aAdditionalStreams.size(); i++ )
    6694             :         {
    6695           0 :             aLine.append( "/" );
    6696           0 :             appendName( m_aAdditionalStreams[i].m_aMimeType, aLine );
    6697           0 :             aLine.append( " " );
    6698           0 :             aLine.append( m_aAdditionalStreams[i].m_nStreamObject );
    6699           0 :             aLine.append( " 0 R\n" );
    6700             :         }
    6701           0 :         aLine.append( "]\n" );
    6702             :     }
    6703             :     aLine.append( ">>\n"
    6704           0 :                   "startxref\n" );
    6705           0 :     aLine.append( (sal_Int64)nXRefOffset );
    6706             :     aLine.append( "\n"
    6707           0 :                   "%%EOF\n" );
    6708           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6709             : 
    6710           0 :     return true;
    6711             : }
    6712             : 
    6713             : struct AnnotationSortEntry
    6714             : {
    6715             :     sal_Int32 nTabOrder;
    6716             :     sal_Int32 nObject;
    6717             :     sal_Int32 nWidgetIndex;
    6718             : 
    6719           0 :     AnnotationSortEntry( sal_Int32 nTab, sal_Int32 nObj, sal_Int32 nI ) :
    6720             :         nTabOrder( nTab ),
    6721             :         nObject( nObj ),
    6722           0 :         nWidgetIndex( nI )
    6723           0 :     {}
    6724             : };
    6725             : 
    6726           0 : struct AnnotSortContainer
    6727             : {
    6728             :     std::set< sal_Int32 >               aObjects;
    6729             :     std::vector< AnnotationSortEntry >    aSortedAnnots;
    6730             : };
    6731             : 
    6732             : struct AnnotSorterLess
    6733             : {
    6734             :     std::vector< PDFWriterImpl::PDFWidget >& m_rWidgets;
    6735             : 
    6736           0 :     AnnotSorterLess( std::vector< PDFWriterImpl::PDFWidget >& rWidgets ) : m_rWidgets( rWidgets ) {}
    6737             : 
    6738           0 :     bool operator()( const AnnotationSortEntry& rLeft, const AnnotationSortEntry& rRight )
    6739             :     {
    6740           0 :         if( rLeft.nTabOrder < rRight.nTabOrder )
    6741           0 :             return true;
    6742           0 :         if( rRight.nTabOrder < rLeft.nTabOrder )
    6743           0 :             return false;
    6744           0 :         if( rLeft.nWidgetIndex < 0 && rRight.nWidgetIndex < 0 )
    6745           0 :             return false;
    6746           0 :         if( rRight.nWidgetIndex < 0 )
    6747           0 :             return true;
    6748           0 :         if( rLeft.nWidgetIndex < 0 )
    6749           0 :             return false;
    6750             :         // remember: widget rects are in PDF coordinates, so they are ordered down up
    6751           0 :         if( m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Top() >
    6752           0 :             m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Top() )
    6753           0 :             return true;
    6754           0 :         if( m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Top() >
    6755           0 :             m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Top() )
    6756           0 :             return false;
    6757           0 :         if( m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Left() <
    6758           0 :             m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Left() )
    6759           0 :             return true;
    6760           0 :         return false;
    6761             :     }
    6762             : };
    6763             : 
    6764           0 : void PDFWriterImpl::sortWidgets()
    6765             : {
    6766             :     // sort widget annotations on each page as per their
    6767             :     // TabOrder attribute
    6768           0 :     boost::unordered_map< sal_Int32, AnnotSortContainer > sorted;
    6769           0 :     int nWidgets = m_aWidgets.size();
    6770           0 :     for( int nW = 0; nW < nWidgets; nW++ )
    6771             :     {
    6772           0 :         const PDFWidget& rWidget = m_aWidgets[nW];
    6773           0 :         if( rWidget.m_nPage >= 0 )
    6774             :         {
    6775           0 :             AnnotSortContainer& rCont = sorted[ rWidget.m_nPage ];
    6776             :             // optimize vector allocation
    6777           0 :             if( rCont.aSortedAnnots.empty() )
    6778           0 :                 rCont.aSortedAnnots.reserve( m_aPages[ rWidget.m_nPage ].m_aAnnotations.size() );
    6779             :             // insert widget to tab sorter
    6780             :             // RadioButtons are not page annotations, only their individual check boxes are
    6781           0 :             if( rWidget.m_eType != PDFWriter::RadioButton )
    6782             :             {
    6783           0 :                 rCont.aObjects.insert( rWidget.m_nObject );
    6784           0 :                 rCont.aSortedAnnots.push_back( AnnotationSortEntry( rWidget.m_nTabOrder, rWidget.m_nObject, nW ) );
    6785             :             }
    6786             :         }
    6787             :     }
    6788           0 :     for( boost::unordered_map< sal_Int32, AnnotSortContainer >::iterator it = sorted.begin(); it != sorted.end(); ++it )
    6789             :     {
    6790             :         // append entries for non widget annotations
    6791           0 :         PDFPage& rPage = m_aPages[ it->first ];
    6792           0 :         unsigned int nAnnots = rPage.m_aAnnotations.size();
    6793           0 :         for( unsigned int nA = 0; nA < nAnnots; nA++ )
    6794           0 :             if( it->second.aObjects.find( rPage.m_aAnnotations[nA] ) == it->second.aObjects.end())
    6795           0 :                 it->second.aSortedAnnots.push_back( AnnotationSortEntry( 10000, rPage.m_aAnnotations[nA], -1 ) );
    6796             : 
    6797           0 :         AnnotSorterLess aLess( m_aWidgets );
    6798           0 :         std::stable_sort( it->second.aSortedAnnots.begin(), it->second.aSortedAnnots.end(), aLess );
    6799             :         // sanity check
    6800           0 :         if( it->second.aSortedAnnots.size() == nAnnots)
    6801             :         {
    6802           0 :             for( unsigned int nA = 0; nA < nAnnots; nA++ )
    6803           0 :                 rPage.m_aAnnotations[nA] = it->second.aSortedAnnots[nA].nObject;
    6804             :         }
    6805             :         else
    6806             :         {
    6807             :             DBG_ASSERT( false, "wrong number of sorted annotations" );
    6808             :             #if OSL_DEBUG_LEVEL > 0
    6809             :             fprintf( stderr, "PDFWriterImpl::sortWidgets(): wrong number of sorted assertions on page nr %ld\n"
    6810             :                      "    %ld sorted and %ld unsorted\n", (long int)it->first, (long int)it->second.aSortedAnnots.size(), (long int)nAnnots );
    6811             :             #endif
    6812             :         }
    6813           0 :     }
    6814             : 
    6815             :     // FIXME: implement tab order in structure tree for PDF 1.5
    6816           0 : }
    6817             : 
    6818             : namespace vcl {
    6819             : class PDFStreamIf :
    6820             :         public cppu::WeakImplHelper1< com::sun::star::io::XOutputStream >
    6821             : {
    6822             :     PDFWriterImpl*  m_pWriter;
    6823             :     bool            m_bWrite;
    6824             :     public:
    6825           0 :     PDFStreamIf( PDFWriterImpl* pWriter ) : m_pWriter( pWriter ), m_bWrite( true ) {}
    6826             :     virtual ~PDFStreamIf();
    6827             : 
    6828             :     virtual void SAL_CALL writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw(std::exception) SAL_OVERRIDE;
    6829             :     virtual void SAL_CALL flush() throw(std::exception) SAL_OVERRIDE;
    6830             :     virtual void SAL_CALL closeOutput() throw(std::exception) SAL_OVERRIDE;
    6831             : };
    6832             : }
    6833             : 
    6834           0 : PDFStreamIf::~PDFStreamIf()
    6835             : {
    6836           0 : }
    6837             : 
    6838           0 : void SAL_CALL  PDFStreamIf::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw(std::exception)
    6839             : {
    6840           0 :     if( m_bWrite && aData.getLength() )
    6841             :     {
    6842           0 :         sal_Int32 nBytes = aData.getLength();
    6843           0 :         m_pWriter->writeBuffer( aData.getConstArray(), nBytes );
    6844             :     }
    6845           0 : }
    6846             : 
    6847           0 : void SAL_CALL PDFStreamIf::flush() throw(std::exception)
    6848             : {
    6849           0 : }
    6850             : 
    6851           0 : void SAL_CALL PDFStreamIf::closeOutput() throw(std::exception)
    6852             : {
    6853           0 :     m_bWrite = false;
    6854           0 : }
    6855             : 
    6856           0 : bool PDFWriterImpl::emitAdditionalStreams()
    6857             : {
    6858           0 :     unsigned int nStreams = m_aAdditionalStreams.size();
    6859           0 :     for( unsigned int i = 0; i < nStreams; i++ )
    6860             :     {
    6861           0 :         PDFAddStream& rStream = m_aAdditionalStreams[i];
    6862           0 :         rStream.m_nStreamObject = createObject();
    6863           0 :         sal_Int32 nSizeObject = createObject();
    6864             : 
    6865           0 :         if( ! updateObject( rStream.m_nStreamObject ) )
    6866           0 :             return false;
    6867             : 
    6868           0 :         OStringBuffer aLine;
    6869           0 :         aLine.append( rStream.m_nStreamObject );
    6870           0 :         aLine.append( " 0 obj\n<</Length " );
    6871           0 :         aLine.append( nSizeObject );
    6872           0 :         aLine.append( " 0 R" );
    6873           0 :         if( rStream.m_bCompress )
    6874           0 :             aLine.append( "/Filter/FlateDecode" );
    6875           0 :         aLine.append( ">>\nstream\n" );
    6876           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6877           0 :             return false;
    6878           0 :         sal_uInt64 nBeginStreamPos = 0, nEndStreamPos = 0;
    6879           0 :         if( osl_File_E_None != osl_getFilePos( m_aFile, &nBeginStreamPos ) )
    6880             :         {
    6881           0 :             osl_closeFile( m_aFile );
    6882           0 :             m_bOpen = false;
    6883             :         }
    6884           0 :         if( rStream.m_bCompress )
    6885           0 :             beginCompression();
    6886             : 
    6887           0 :         checkAndEnableStreamEncryption( rStream.m_nStreamObject );
    6888           0 :         com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > xStream( new PDFStreamIf( this ) );
    6889           0 :         rStream.m_pStream->write( xStream );
    6890           0 :         xStream.clear();
    6891           0 :         delete rStream.m_pStream;
    6892           0 :         rStream.m_pStream = NULL;
    6893           0 :         disableStreamEncryption();
    6894             : 
    6895           0 :         if( rStream.m_bCompress )
    6896           0 :             endCompression();
    6897             : 
    6898           0 :         if( osl_File_E_None != osl_getFilePos( m_aFile, &nEndStreamPos ) )
    6899             :         {
    6900           0 :             osl_closeFile( m_aFile );
    6901           0 :             m_bOpen = false;
    6902           0 :             return false;
    6903             :         }
    6904           0 :         if( ! writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    6905           0 :             return false ;
    6906             :         // emit stream length object
    6907           0 :         if( ! updateObject( nSizeObject ) )
    6908           0 :             return false;
    6909           0 :         aLine.setLength( 0 );
    6910           0 :         aLine.append( nSizeObject );
    6911           0 :         aLine.append( " 0 obj\n" );
    6912           0 :         aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos) );
    6913           0 :         aLine.append( "\nendobj\n\n" );
    6914           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6915           0 :             return false;
    6916           0 :     }
    6917           0 :     return true;
    6918             : }
    6919             : 
    6920           0 : bool PDFWriterImpl::emit()
    6921             : {
    6922           0 :     endPage();
    6923             : 
    6924             :     // resort structure tree and annotations if necessary
    6925             :     // needed for widget tab order
    6926           0 :     sortWidgets();
    6927             : 
    6928             : #if !defined(ANDROID) && !defined(IOS)
    6929           0 :     if( m_aContext.SignPDF )
    6930             :     {
    6931             :         // sign the document
    6932           0 :         PDFWriter::SignatureWidget aSignature;
    6933           0 :         aSignature.Name = "Signature1";
    6934           0 :         createControl( aSignature, 0 );
    6935             :     }
    6936             : #endif
    6937             : 
    6938             :     // emit additional streams
    6939           0 :     CHECK_RETURN( emitAdditionalStreams() );
    6940             : 
    6941             :     // emit catalog
    6942           0 :     CHECK_RETURN( emitCatalog() );
    6943             : 
    6944             : #if !defined(ANDROID) && !defined(IOS)
    6945           0 :     if (m_nSignatureObject != -1) // if document is signed, emit sigdict
    6946           0 :         CHECK_RETURN( emitSignature() );
    6947             : #endif
    6948             : 
    6949             :     // emit trailer
    6950           0 :     CHECK_RETURN( emitTrailer() );
    6951             : 
    6952             : #if !defined(ANDROID) && !defined(IOS)
    6953           0 :     if (m_nSignatureObject != -1) // finalize the signature
    6954           0 :         CHECK_RETURN( finalizeSignature() );
    6955             : #endif
    6956             : 
    6957           0 :     osl_closeFile( m_aFile );
    6958           0 :     m_bOpen = false;
    6959             : 
    6960           0 :     return true;
    6961             : }
    6962             : 
    6963           0 : std::set< PDFWriter::ErrorCode > PDFWriterImpl::getErrors()
    6964             : {
    6965           0 :     return m_aErrors;
    6966             : }
    6967             : 
    6968           0 : sal_Int32 PDFWriterImpl::getSystemFont( const Font& i_rFont )
    6969             : {
    6970           0 :     getReferenceDevice()->Push();
    6971           0 :     getReferenceDevice()->SetFont( i_rFont );
    6972           0 :     getReferenceDevice()->ImplNewFont();
    6973             : 
    6974           0 :     const PhysicalFontFace* pDevFont = m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData;
    6975           0 :     sal_Int32 nFontID = 0;
    6976           0 :     FontEmbedData::iterator it = m_aSystemFonts.find( pDevFont );
    6977           0 :     if( it != m_aSystemFonts.end() )
    6978           0 :         nFontID = it->second.m_nNormalFontID;
    6979             :     else
    6980             :     {
    6981           0 :         nFontID = m_nNextFID++;
    6982           0 :         m_aSystemFonts[ pDevFont ] = EmbedFont();
    6983           0 :         m_aSystemFonts[ pDevFont ].m_nNormalFontID = nFontID;
    6984             :     }
    6985             : 
    6986           0 :     getReferenceDevice()->Pop();
    6987           0 :     getReferenceDevice()->ImplNewFont();
    6988             : 
    6989           0 :     return nFontID;
    6990             : }
    6991             : 
    6992           0 : void PDFWriterImpl::registerGlyphs( int nGlyphs,
    6993             :                                     sal_GlyphId* pGlyphs,
    6994             :                                     sal_Int32* pGlyphWidths,
    6995             :                                     sal_Ucs* pUnicodes,
    6996             :                                     sal_Int32* pUnicodesPerGlyph,
    6997             :                                     sal_uInt8* pMappedGlyphs,
    6998             :                                     sal_Int32* pMappedFontObjects,
    6999             :                                     const PhysicalFontFace* pFallbackFonts[] )
    7000             : {
    7001           0 :     const PhysicalFontFace* pDevFont = m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData;
    7002           0 :     sal_Ucs* pCurUnicode = pUnicodes;
    7003           0 :     for( int i = 0; i < nGlyphs; pCurUnicode += pUnicodesPerGlyph[i] , i++ )
    7004             :     {
    7005           0 :         const int nFontGlyphId = pGlyphs[i] & (GF_IDXMASK | GF_ISCHAR | GF_GSUB);
    7006           0 :         const PhysicalFontFace* pCurrentFont = pFallbackFonts[i] ? pFallbackFonts[i] : pDevFont;
    7007             : 
    7008           0 :         if( pCurrentFont->mbSubsettable )
    7009             :         {
    7010           0 :             FontSubset& rSubset = m_aSubsets[ pCurrentFont ];
    7011             :             // search for font specific glyphID
    7012           0 :             FontMapping::iterator it = rSubset.m_aMapping.find( nFontGlyphId );
    7013           0 :             if( it != rSubset.m_aMapping.end() )
    7014             :             {
    7015           0 :                 pMappedFontObjects[i] = it->second.m_nFontID;
    7016           0 :                 pMappedGlyphs[i] = it->second.m_nSubsetGlyphID;
    7017             :             }
    7018             :             else
    7019             :             {
    7020             :                 // create new subset if necessary
    7021           0 :                 if( rSubset.m_aSubsets.empty()
    7022           0 :                 || (rSubset.m_aSubsets.back().m_aMapping.size() > 254) )
    7023             :                 {
    7024           0 :                     rSubset.m_aSubsets.push_back( FontEmit( m_nNextFID++ ) );
    7025             :                 }
    7026             : 
    7027             :                 // copy font id
    7028           0 :                 pMappedFontObjects[i] = rSubset.m_aSubsets.back().m_nFontID;
    7029             :                 // create new glyph in subset
    7030           0 :                 sal_uInt8 nNewId = sal::static_int_cast<sal_uInt8>(rSubset.m_aSubsets.back().m_aMapping.size()+1);
    7031           0 :                 pMappedGlyphs[i] = nNewId;
    7032             : 
    7033             :                 // add new glyph to emitted font subset
    7034           0 :                 GlyphEmit& rNewGlyphEmit = rSubset.m_aSubsets.back().m_aMapping[ nFontGlyphId ];
    7035           0 :                 rNewGlyphEmit.setGlyphId( nNewId );
    7036           0 :                 for( sal_Int32 n = 0; n < pUnicodesPerGlyph[i]; n++ )
    7037           0 :                     rNewGlyphEmit.addCode( pCurUnicode[n] );
    7038             : 
    7039             :                 // add new glyph to font mapping
    7040           0 :                 Glyph& rNewGlyph = rSubset.m_aMapping[ nFontGlyphId ];
    7041           0 :                 rNewGlyph.m_nFontID = pMappedFontObjects[i];
    7042           0 :                 rNewGlyph.m_nSubsetGlyphID = nNewId;
    7043             :             }
    7044           0 :             getReferenceDevice()->ImplGetGraphics();
    7045           0 :             const bool bVertical = ((pGlyphs[i] & GF_ROTMASK) != 0);
    7046           0 :             pGlyphWidths[i] = m_aFontCache.getGlyphWidth( pCurrentFont,
    7047             :                                                           nFontGlyphId,
    7048             :                                                           bVertical,
    7049           0 :                                                           m_pReferenceDevice->mpGraphics );
    7050             :         }
    7051           0 :         else if( pCurrentFont->IsEmbeddable() )
    7052             :         {
    7053           0 :             sal_Int32 nFontID = 0;
    7054           0 :             FontEmbedData::iterator it = m_aEmbeddedFonts.find( pCurrentFont );
    7055           0 :             if( it != m_aEmbeddedFonts.end() )
    7056           0 :                 nFontID = it->second.m_nNormalFontID;
    7057             :             else
    7058             :             {
    7059           0 :                 nFontID = m_nNextFID++;
    7060           0 :                 m_aEmbeddedFonts[ pCurrentFont ] = EmbedFont();
    7061           0 :                 m_aEmbeddedFonts[ pCurrentFont ].m_nNormalFontID = nFontID;
    7062             :             }
    7063           0 :             EmbedFont& rEmbedFont = m_aEmbeddedFonts[pCurrentFont];
    7064             : 
    7065           0 :             const Ucs2SIntMap* pEncoding = NULL;
    7066           0 :             const Ucs2OStrMap* pNonEncoded = NULL;
    7067           0 :             getReferenceDevice()->ImplGetGraphics();
    7068           0 :             pEncoding = m_pReferenceDevice->mpGraphics->GetFontEncodingVector( pCurrentFont, &pNonEncoded );
    7069             : 
    7070           0 :             Ucs2SIntMap::const_iterator enc_it;
    7071           0 :             Ucs2OStrMap::const_iterator nonenc_it;
    7072             : 
    7073           0 :             sal_Int32 nCurFontID = nFontID;
    7074           0 :             sal_Ucs cChar = *pCurUnicode;
    7075           0 :             if( pEncoding )
    7076             :             {
    7077           0 :                 enc_it = pEncoding->find( cChar );
    7078           0 :                 if( enc_it != pEncoding->end() && enc_it->second > 0 )
    7079             :                 {
    7080             :                     DBG_ASSERT( (enc_it->second & 0xffffff00) == 0, "Invalid character code" );
    7081           0 :                     cChar = (sal_Ucs)enc_it->second;
    7082             :                 }
    7083           0 :                 else if( (enc_it == pEncoding->end() || enc_it->second == -1) &&
    7084           0 :                          pNonEncoded &&
    7085           0 :                          (nonenc_it = pNonEncoded->find( cChar )) != pNonEncoded->end() )
    7086             :                 {
    7087           0 :                     nCurFontID = 0;
    7088             :                     // find non encoded glyph
    7089           0 :                     for( std::list< EmbedEncoding >::iterator nec_it = rEmbedFont.m_aExtendedEncodings.begin(); nec_it != rEmbedFont.m_aExtendedEncodings.end(); ++nec_it )
    7090             :                     {
    7091           0 :                         if( nec_it->m_aCMap.find( cChar ) != nec_it->m_aCMap.end() )
    7092             :                         {
    7093           0 :                             nCurFontID = nec_it->m_nFontID;
    7094           0 :                             cChar = (sal_Ucs)nec_it->m_aCMap[ cChar ];
    7095           0 :                             break;
    7096             :                         }
    7097             :                     }
    7098           0 :                     if( nCurFontID == 0 ) // new nonencoded glyph
    7099             :                     {
    7100           0 :                         if( rEmbedFont.m_aExtendedEncodings.empty() || rEmbedFont.m_aExtendedEncodings.back().m_aEncVector.size() == 255 )
    7101             :                         {
    7102           0 :                             rEmbedFont.m_aExtendedEncodings.push_back( EmbedEncoding() );
    7103           0 :                             rEmbedFont.m_aExtendedEncodings.back().m_nFontID = m_nNextFID++;
    7104             :                         }
    7105           0 :                         EmbedEncoding& rEncoding = rEmbedFont.m_aExtendedEncodings.back();
    7106           0 :                         rEncoding.m_aEncVector.push_back( EmbedCode() );
    7107           0 :                         rEncoding.m_aEncVector.back().m_aUnicode = cChar;
    7108           0 :                         rEncoding.m_aEncVector.back().m_aName = nonenc_it->second;
    7109           0 :                         rEncoding.m_aCMap[ cChar ] = (sal_Int8)(rEncoding.m_aEncVector.size()-1);
    7110           0 :                         nCurFontID = rEncoding.m_nFontID;
    7111           0 :                         cChar = (sal_Ucs)rEncoding.m_aCMap[ cChar ];
    7112             :                     }
    7113             :                 }
    7114             :                 else
    7115           0 :                     pEncoding = NULL;
    7116             :             }
    7117           0 :             if( ! pEncoding )
    7118             :             {
    7119           0 :                 if( cChar & 0xff00 )
    7120             :                 {
    7121             :                     // some characters can be used by conversion
    7122           0 :                     if( cChar >= 0xf000 && cChar <= 0xf0ff ) // symbol encoding in private use area
    7123           0 :                         cChar -= 0xf000;
    7124             :                     else
    7125             :                     {
    7126           0 :                         OString aChar(&cChar, 1, RTL_TEXTENCODING_MS_1252);
    7127           0 :                         cChar = ((sal_Ucs)aChar[0]) & 0x00ff;
    7128             :                     }
    7129             :                 }
    7130             :             }
    7131             : 
    7132           0 :             pMappedGlyphs[ i ] = (sal_Int8)cChar;
    7133           0 :             pMappedFontObjects[ i ] = nCurFontID;
    7134           0 :             pGlyphWidths[ i ] = m_aFontCache.getGlyphWidth( pCurrentFont,
    7135             :                                                             (pEncoding ? *pCurUnicode : cChar) | GF_ISCHAR,
    7136             :                                                             false,
    7137           0 :                                                             m_pReferenceDevice->mpGraphics );
    7138             :         }
    7139             :     }
    7140           0 : }
    7141             : 
    7142           0 : void PDFWriterImpl::drawRelief( SalLayout& rLayout, const OUString& rText, bool bTextLines )
    7143             : {
    7144           0 :     push( PUSH_ALL );
    7145             : 
    7146           0 :     FontRelief eRelief = m_aCurrentPDFState.m_aFont.GetRelief();
    7147             : 
    7148           0 :     Color aTextColor = m_aCurrentPDFState.m_aFont.GetColor();
    7149           0 :     Color aTextLineColor = m_aCurrentPDFState.m_aTextLineColor;
    7150           0 :     Color aOverlineColor = m_aCurrentPDFState.m_aOverlineColor;
    7151           0 :     Color aReliefColor( COL_LIGHTGRAY );
    7152           0 :     if( aTextColor == COL_BLACK )
    7153           0 :         aTextColor = Color( COL_WHITE );
    7154           0 :     if( aTextLineColor == COL_BLACK )
    7155           0 :         aTextLineColor = Color( COL_WHITE );
    7156           0 :     if( aOverlineColor == COL_BLACK )
    7157           0 :         aOverlineColor = Color( COL_WHITE );
    7158           0 :     if( aTextColor == COL_WHITE )
    7159           0 :         aReliefColor = Color( COL_BLACK );
    7160             : 
    7161           0 :     Font aSetFont = m_aCurrentPDFState.m_aFont;
    7162           0 :     aSetFont.SetRelief( RELIEF_NONE );
    7163           0 :     aSetFont.SetShadow( false );
    7164             : 
    7165           0 :     aSetFont.SetColor( aReliefColor );
    7166           0 :     setTextLineColor( aReliefColor );
    7167           0 :     setOverlineColor( aReliefColor );
    7168           0 :     setFont( aSetFont );
    7169           0 :     long nOff = 1 + getReferenceDevice()->mnDPIX/300;
    7170           0 :     if( eRelief == RELIEF_ENGRAVED )
    7171           0 :         nOff = -nOff;
    7172             : 
    7173           0 :     rLayout.DrawOffset() += Point( nOff, nOff );
    7174           0 :     updateGraphicsState();
    7175           0 :     drawLayout( rLayout, rText, bTextLines );
    7176             : 
    7177           0 :     rLayout.DrawOffset() -= Point( nOff, nOff );
    7178           0 :     setTextLineColor( aTextLineColor );
    7179           0 :     setOverlineColor( aOverlineColor );
    7180           0 :     aSetFont.SetColor( aTextColor );
    7181           0 :     setFont( aSetFont );
    7182           0 :     updateGraphicsState();
    7183           0 :     drawLayout( rLayout, rText, bTextLines );
    7184             : 
    7185             :     // clean up the mess
    7186           0 :     pop();
    7187           0 : }
    7188             : 
    7189           0 : void PDFWriterImpl::drawShadow( SalLayout& rLayout, const OUString& rText, bool bTextLines )
    7190             : {
    7191           0 :     Font aSaveFont = m_aCurrentPDFState.m_aFont;
    7192           0 :     Color aSaveTextLineColor = m_aCurrentPDFState.m_aTextLineColor;
    7193           0 :     Color aSaveOverlineColor = m_aCurrentPDFState.m_aOverlineColor;
    7194             : 
    7195           0 :     Font& rFont = m_aCurrentPDFState.m_aFont;
    7196           0 :     if( rFont.GetColor() == Color( COL_BLACK ) || rFont.GetColor().GetLuminance() < 8 )
    7197           0 :         rFont.SetColor( Color( COL_LIGHTGRAY ) );
    7198             :     else
    7199           0 :         rFont.SetColor( Color( COL_BLACK ) );
    7200           0 :     rFont.SetShadow( false );
    7201           0 :     rFont.SetOutline( false );
    7202           0 :     setFont( rFont );
    7203           0 :     setTextLineColor( rFont.GetColor() );
    7204           0 :     setOverlineColor( rFont.GetColor() );
    7205           0 :     updateGraphicsState();
    7206             : 
    7207           0 :     long nOff = 1 + ((m_pReferenceDevice->mpFontEntry->mnLineHeight-24)/24);
    7208           0 :     if( rFont.IsOutline() )
    7209           0 :         nOff++;
    7210           0 :     rLayout.DrawBase() += Point( nOff, nOff );
    7211           0 :     drawLayout( rLayout, rText, bTextLines );
    7212           0 :     rLayout.DrawBase() -= Point( nOff, nOff );
    7213             : 
    7214           0 :     setFont( aSaveFont );
    7215           0 :     setTextLineColor( aSaveTextLineColor );
    7216           0 :     setOverlineColor( aSaveOverlineColor );
    7217           0 :     updateGraphicsState();
    7218           0 : }
    7219             : 
    7220           0 : void PDFWriterImpl::drawVerticalGlyphs(
    7221             :         const std::vector<PDFWriterImpl::PDFGlyph>& rGlyphs,
    7222             :         OStringBuffer& rLine,
    7223             :         const Point& rAlignOffset,
    7224             :         const Matrix3& rRotScale,
    7225             :         double fAngle,
    7226             :         double fXScale,
    7227             :         double fSkew,
    7228             :         sal_Int32 nFontHeight )
    7229             : {
    7230           0 :     long nXOffset = 0;
    7231           0 :     Point aCurPos( rGlyphs[0].m_aPos );
    7232           0 :     aCurPos = m_pReferenceDevice->PixelToLogic( aCurPos );
    7233           0 :     aCurPos += rAlignOffset;
    7234           0 :     for( size_t i = 0; i < rGlyphs.size(); i++ )
    7235             :     {
    7236             :         // have to emit each glyph on its own
    7237           0 :         double fDeltaAngle = 0.0;
    7238           0 :         double fYScale = 1.0;
    7239           0 :         double fTempXScale = fXScale;
    7240           0 :         double fSkewB = fSkew;
    7241           0 :         double fSkewA = 0.0;
    7242             : 
    7243           0 :         Point aDeltaPos;
    7244           0 :         if( ( rGlyphs[i].m_nGlyphId & GF_ROTMASK ) == GF_ROTL )
    7245             :         {
    7246           0 :             fDeltaAngle = M_PI/2.0;
    7247           0 :             aDeltaPos.X() = m_pReferenceDevice->GetFontMetric().GetAscent();
    7248           0 :             aDeltaPos.Y() = (int)((double)m_pReferenceDevice->GetFontMetric().GetDescent() * fXScale);
    7249           0 :             fYScale = fXScale;
    7250           0 :             fTempXScale = 1.0;
    7251           0 :             fSkewA = -fSkewB;
    7252           0 :             fSkewB = 0.0;
    7253             :         }
    7254           0 :         else if( ( rGlyphs[i].m_nGlyphId & GF_ROTMASK ) == GF_ROTR )
    7255             :         {
    7256           0 :             fDeltaAngle = -M_PI/2.0;
    7257           0 :             aDeltaPos.X() = (int)((double)m_pReferenceDevice->GetFontMetric().GetDescent()*fXScale);
    7258           0 :             aDeltaPos.Y() = -m_pReferenceDevice->GetFontMetric().GetAscent();
    7259           0 :             fYScale = fXScale;
    7260           0 :             fTempXScale = 1.0;
    7261           0 :             fSkewA = fSkewB;
    7262           0 :             fSkewB = 0.0;
    7263             :         }
    7264           0 :         aDeltaPos += (m_pReferenceDevice->PixelToLogic( Point( (int)((double)nXOffset/fXScale), 0 ) ) - m_pReferenceDevice->PixelToLogic( Point() ) );
    7265           0 :         if( i < rGlyphs.size()-1 )
    7266             :         // #i120627# the text on the Y axis is reversed when export ppt file to PDF format
    7267             :         {
    7268           0 :             long nOffsetX = rGlyphs[i+1].m_aPos.X() - rGlyphs[i].m_aPos.X();
    7269           0 :             long nOffsetY = rGlyphs[i+1].m_aPos.Y() - rGlyphs[i].m_aPos.Y();
    7270           0 :             nXOffset += (int)sqrt(double(nOffsetX*nOffsetX + nOffsetY*nOffsetY));
    7271             :         }
    7272           0 :         if( ! rGlyphs[i].m_nGlyphId )
    7273           0 :             continue;
    7274             : 
    7275           0 :         aDeltaPos = rRotScale.transform( aDeltaPos );
    7276             : 
    7277           0 :         Matrix3 aMat;
    7278           0 :         if( fSkewB != 0.0 || fSkewA != 0.0 )
    7279           0 :             aMat.skew( fSkewA, fSkewB );
    7280           0 :         aMat.scale( fTempXScale, fYScale );
    7281           0 :         aMat.rotate( fAngle+fDeltaAngle );
    7282           0 :         aMat.translate( aCurPos.X()+aDeltaPos.X(), aCurPos.Y()+aDeltaPos.Y() );
    7283           0 :         aMat.append( m_aPages.back(), rLine );
    7284           0 :         rLine.append( " Tm" );
    7285           0 :         if( i == 0 || rGlyphs[i-1].m_nMappedFontId != rGlyphs[i].m_nMappedFontId )
    7286             :         {
    7287           0 :             rLine.append( " /F" );
    7288           0 :             rLine.append( rGlyphs[i].m_nMappedFontId );
    7289           0 :             rLine.append( ' ' );
    7290           0 :             m_aPages.back().appendMappedLength( nFontHeight, rLine, true );
    7291           0 :             rLine.append( " Tf" );
    7292             :         }
    7293           0 :         rLine.append( "<" );
    7294           0 :         appendHex( rGlyphs[i].m_nMappedGlyphId, rLine );
    7295           0 :         rLine.append( ">Tj\n" );
    7296           0 :     }
    7297           0 : }
    7298             : 
    7299           0 : void PDFWriterImpl::drawHorizontalGlyphs(
    7300             :         const std::vector<PDFWriterImpl::PDFGlyph>& rGlyphs,
    7301             :         OStringBuffer& rLine,
    7302             :         const Point& rAlignOffset,
    7303             :         double fAngle,
    7304             :         double fXScale,
    7305             :         double fSkew,
    7306             :         sal_Int32 nFontHeight,
    7307             :         sal_Int32 nPixelFontHeight
    7308             :         )
    7309             : {
    7310             :     // horizontal (= normal) case
    7311             : 
    7312             :     // fill in  run end indices
    7313             :     // end is marked by index of the first glyph of the next run
    7314             :     // a run is marked by same mapped font id and same Y position
    7315           0 :     std::vector< sal_uInt32 > aRunEnds;
    7316           0 :     aRunEnds.reserve( rGlyphs.size() );
    7317           0 :     for( size_t i = 1; i < rGlyphs.size(); i++ )
    7318             :     {
    7319           0 :         if( rGlyphs[i].m_nMappedFontId != rGlyphs[i-1].m_nMappedFontId ||
    7320           0 :             rGlyphs[i].m_aPos.Y() != rGlyphs[i-1].m_aPos.Y() )
    7321             :         {
    7322           0 :             aRunEnds.push_back(i);
    7323             :         }
    7324             :     }
    7325             :     // last run ends at last glyph
    7326           0 :     aRunEnds.push_back( rGlyphs.size() );
    7327             : 
    7328             :     // loop over runs of the same font
    7329           0 :     sal_uInt32 nBeginRun = 0;
    7330           0 :     for( size_t nRun = 0; nRun < aRunEnds.size(); nRun++ )
    7331             :     {
    7332             :         // setup text matrix
    7333           0 :         Point aCurPos = rGlyphs[nBeginRun].m_aPos;
    7334             :         // back transformation to current coordinate system
    7335           0 :         aCurPos = m_pReferenceDevice->PixelToLogic( aCurPos );
    7336           0 :         aCurPos += rAlignOffset;
    7337             :         // the first run can be set with "Td" operator
    7338             :         // subsequent use of that operator would move
    7339             :         // the texline matrix relative to what was set before
    7340             :         // making use of that would drive us into rounding issues
    7341           0 :         Matrix3 aMat;
    7342           0 :         if( nRun == 0 && fAngle == 0.0 && fXScale == 1.0 && fSkew == 0.0 )
    7343             :         {
    7344           0 :             m_aPages.back().appendPoint( aCurPos, rLine, false );
    7345           0 :             rLine.append( " Td " );
    7346             :         }
    7347             :         else
    7348             :         {
    7349           0 :             if( fSkew != 0.0 )
    7350           0 :                 aMat.skew( 0.0, fSkew );
    7351           0 :             aMat.scale( fXScale, 1.0 );
    7352           0 :             aMat.rotate( fAngle );
    7353           0 :             aMat.translate( aCurPos.X(), aCurPos.Y() );
    7354           0 :             aMat.append( m_aPages.back(), rLine );
    7355           0 :             rLine.append( " Tm\n" );
    7356             :         }
    7357             :         // set up correct font
    7358           0 :         rLine.append( "/F" );
    7359           0 :         rLine.append( rGlyphs[nBeginRun].m_nMappedFontId );
    7360           0 :         rLine.append( ' ' );
    7361           0 :         m_aPages.back().appendMappedLength( nFontHeight, rLine, true );
    7362           0 :         rLine.append( " Tf" );
    7363             : 
    7364             :         // output glyphs using Tj or TJ
    7365           0 :         OStringBuffer aKernedLine( 256 ), aUnkernedLine( 256 );
    7366           0 :         aKernedLine.append( "[<" );
    7367           0 :         aUnkernedLine.append( '<' );
    7368           0 :         appendHex( rGlyphs[nBeginRun].m_nMappedGlyphId, aKernedLine );
    7369           0 :         appendHex( rGlyphs[nBeginRun].m_nMappedGlyphId, aUnkernedLine );
    7370             : 
    7371           0 :         aMat.invert();
    7372           0 :         bool bNeedKern = false;
    7373           0 :         for( sal_uInt32 nPos = nBeginRun+1; nPos < aRunEnds[nRun]; nPos++ )
    7374             :         {
    7375           0 :             appendHex( rGlyphs[nPos].m_nMappedGlyphId, aUnkernedLine );
    7376             :             // check if default glyph positioning is sufficient
    7377           0 :             const Point aThisPos = aMat.transform( rGlyphs[nPos].m_aPos );
    7378           0 :             const Point aPrevPos = aMat.transform( rGlyphs[nPos-1].m_aPos );
    7379           0 :             double fAdvance = aThisPos.X() - aPrevPos.X();
    7380           0 :             fAdvance *= 1000.0 / nPixelFontHeight;
    7381           0 :             const sal_Int32 nAdjustment = (sal_Int32)(rGlyphs[nPos-1].m_nNativeWidth - fAdvance + 0.5);
    7382           0 :             if( nAdjustment != 0 )
    7383             :             {
    7384             :                 // apply individual glyph positioning
    7385           0 :                 bNeedKern = true;
    7386           0 :                 aKernedLine.append( ">" );
    7387           0 :                 aKernedLine.append( nAdjustment );
    7388           0 :                 aKernedLine.append( "<" );
    7389             :             }
    7390           0 :             appendHex( rGlyphs[nPos].m_nMappedGlyphId, aKernedLine );
    7391             :         }
    7392           0 :         aKernedLine.append( ">]TJ\n" );
    7393           0 :         aUnkernedLine.append( ">Tj\n" );
    7394             :         rLine.append(
    7395           0 :             (bNeedKern ? aKernedLine : aUnkernedLine).makeStringAndClear() );
    7396             : 
    7397             :         // set beginning of next run
    7398           0 :         nBeginRun = aRunEnds[nRun];
    7399           0 :     }
    7400           0 : }
    7401             : 
    7402           0 : void PDFWriterImpl::drawLayout( SalLayout& rLayout, const OUString& rText, bool bTextLines )
    7403             : {
    7404             :     // relief takes precedence over shadow (see outdev3.cxx)
    7405           0 :     if(  m_aCurrentPDFState.m_aFont.GetRelief() != RELIEF_NONE )
    7406             :     {
    7407           0 :         drawRelief( rLayout, rText, bTextLines );
    7408           0 :         return;
    7409             :     }
    7410           0 :     else if( m_aCurrentPDFState.m_aFont.IsShadow() )
    7411           0 :         drawShadow( rLayout, rText, bTextLines );
    7412             : 
    7413           0 :     OStringBuffer aLine( 512 );
    7414             : 
    7415           0 :     const int nMaxGlyphs = 256;
    7416             : 
    7417             :     sal_GlyphId pGlyphs[nMaxGlyphs];
    7418             :     sal_Int32 pGlyphWidths[nMaxGlyphs];
    7419             :     sal_uInt8 pMappedGlyphs[nMaxGlyphs];
    7420             :     sal_Int32 pMappedFontObjects[nMaxGlyphs];
    7421           0 :     std::vector<sal_Ucs> aUnicodes;
    7422           0 :     aUnicodes.reserve( nMaxGlyphs );
    7423             :     sal_Int32 pUnicodesPerGlyph[nMaxGlyphs];
    7424             :     int pCharPosAry[nMaxGlyphs];
    7425             :     sal_Int32 nAdvanceWidths[nMaxGlyphs];
    7426           0 :     const PhysicalFontFace* pFallbackFonts[nMaxGlyphs] = { NULL };
    7427           0 :     bool bVertical = m_aCurrentPDFState.m_aFont.IsVertical();
    7428             :     int nGlyphs;
    7429           0 :     int nIndex = 0;
    7430           0 :     int nMinCharPos = 0, nMaxCharPos = rText.getLength()-1;
    7431           0 :     double fXScale = 1.0;
    7432           0 :     double fSkew = 0.0;
    7433           0 :     sal_Int32 nPixelFontHeight = m_pReferenceDevice->mpFontEntry->maFontSelData.mnHeight;
    7434           0 :     TextAlign eAlign = m_aCurrentPDFState.m_aFont.GetAlign();
    7435             : 
    7436             :     // transform font height back to current units
    7437             :     // note: the layout calculates in outdevs device pixel !!
    7438           0 :     sal_Int32 nFontHeight = m_pReferenceDevice->ImplDevicePixelToLogicHeight( nPixelFontHeight );
    7439           0 :     if( m_aCurrentPDFState.m_aFont.GetWidth() )
    7440             :     {
    7441           0 :         Font aFont( m_aCurrentPDFState.m_aFont );
    7442           0 :         aFont.SetWidth( 0 );
    7443           0 :         FontMetric aMetric = m_pReferenceDevice->GetFontMetric( aFont );
    7444           0 :         if( aMetric.GetWidth() != m_aCurrentPDFState.m_aFont.GetWidth() )
    7445             :         {
    7446             :             fXScale =
    7447           0 :                 (double)m_aCurrentPDFState.m_aFont.GetWidth() /
    7448           0 :                 (double)aMetric.GetWidth();
    7449             :         }
    7450             :         // force state before GetFontMetric
    7451           0 :         m_pReferenceDevice->ImplNewFont();
    7452             :     }
    7453             : 
    7454             :     // perform artificial italics if necessary
    7455           0 :     if( ( m_aCurrentPDFState.m_aFont.GetItalic() == ITALIC_NORMAL ||
    7456           0 :           m_aCurrentPDFState.m_aFont.GetItalic() == ITALIC_OBLIQUE ) &&
    7457           0 :         !( m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetSlant() == ITALIC_NORMAL ||
    7458           0 :            m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetSlant() == ITALIC_OBLIQUE )
    7459             :         )
    7460             :     {
    7461           0 :         fSkew = M_PI/12.0;
    7462             :     }
    7463             : 
    7464             :     // if the mapmode is distorted we need to adjust for that also
    7465           0 :     if( m_aCurrentPDFState.m_aMapMode.GetScaleX() != m_aCurrentPDFState.m_aMapMode.GetScaleY() )
    7466             :     {
    7467           0 :         fXScale *= double(m_aCurrentPDFState.m_aMapMode.GetScaleX()) / double(m_aCurrentPDFState.m_aMapMode.GetScaleY());
    7468             :     }
    7469             : 
    7470           0 :     int nAngle = m_aCurrentPDFState.m_aFont.GetOrientation();
    7471             :     // normalize angles
    7472           0 :     while( nAngle < 0 )
    7473           0 :         nAngle += 3600;
    7474           0 :     nAngle = nAngle % 3600;
    7475           0 :     double fAngle = (double)nAngle * M_PI / 1800.0;
    7476             : 
    7477           0 :     Matrix3 aRotScale;
    7478           0 :     aRotScale.scale( fXScale, 1.0 );
    7479           0 :     if( fAngle != 0.0 )
    7480           0 :         aRotScale.rotate( -fAngle );
    7481             : 
    7482           0 :     bool bPop = false;
    7483           0 :     bool bABold = false;
    7484             :     // artificial bold necessary ?
    7485           0 :     if( m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetWeight() <= WEIGHT_MEDIUM &&
    7486           0 :         m_pReferenceDevice->mpFontEntry->maFontSelData.GetWeight() > WEIGHT_MEDIUM )
    7487             :     {
    7488           0 :         if( ! bPop )
    7489           0 :             aLine.append( "q " );
    7490           0 :         bPop = true;
    7491           0 :         bABold = true;
    7492             :     }
    7493             :     // setup text colors (if necessary)
    7494           0 :     Color aStrokeColor( COL_TRANSPARENT );
    7495           0 :     Color aNonStrokeColor( COL_TRANSPARENT );
    7496             : 
    7497           0 :     if( m_aCurrentPDFState.m_aFont.IsOutline() )
    7498             :     {
    7499           0 :         aStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7500           0 :         aNonStrokeColor = Color( COL_WHITE );
    7501             :     }
    7502             :     else
    7503           0 :         aNonStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7504           0 :     if( bABold )
    7505           0 :         aStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7506             : 
    7507           0 :     if( aStrokeColor != Color( COL_TRANSPARENT ) && aStrokeColor != m_aCurrentPDFState.m_aLineColor )
    7508             :     {
    7509           0 :         if( ! bPop )
    7510           0 :             aLine.append( "q " );
    7511           0 :         bPop = true;
    7512           0 :         appendStrokingColor( aStrokeColor, aLine );
    7513           0 :         aLine.append( "\n" );
    7514             :     }
    7515           0 :     if( aNonStrokeColor != Color( COL_TRANSPARENT ) && aNonStrokeColor != m_aCurrentPDFState.m_aFillColor )
    7516             :     {
    7517           0 :         if( ! bPop )
    7518           0 :             aLine.append( "q " );
    7519           0 :         bPop = true;
    7520           0 :         appendNonStrokingColor( aNonStrokeColor, aLine );
    7521           0 :         aLine.append( "\n" );
    7522             :     }
    7523             : 
    7524             :     // begin text object
    7525           0 :     aLine.append( "BT\n" );
    7526             :     // outline attribute ?
    7527           0 :     if( m_aCurrentPDFState.m_aFont.IsOutline() || bABold )
    7528             :     {
    7529             :         // set correct text mode, set stroke width
    7530           0 :         aLine.append( "2 Tr " ); // fill, then stroke
    7531             : 
    7532           0 :         if( m_aCurrentPDFState.m_aFont.IsOutline() )
    7533             :         {
    7534             :             // unclear what to do in case of outline and artificial bold
    7535             :             // for the time being outline wins
    7536           0 :             aLine.append( "0.25 w \n" );
    7537             :         }
    7538             :         else
    7539             :         {
    7540           0 :             double fW = (double)m_aCurrentPDFState.m_aFont.GetHeight() / 30.0;
    7541           0 :             m_aPages.back().appendMappedLength( fW, aLine );
    7542           0 :             aLine.append ( " w\n" );
    7543             :         }
    7544             :     }
    7545             : 
    7546           0 :     FontMetric aRefDevFontMetric = m_pReferenceDevice->GetFontMetric();
    7547             : 
    7548             :     // collect the glyphs into a single array
    7549           0 :     const int nTmpMaxGlyphs = rLayout.GetOrientation() ? 1 : nMaxGlyphs; // #i97991# temporary workaround for #i87686#
    7550           0 :     std::vector< PDFGlyph > aGlyphs;
    7551           0 :     aGlyphs.reserve( nTmpMaxGlyphs );
    7552             :     // first get all the glyphs and register them; coordinates still in Pixel
    7553           0 :     Point aGNGlyphPos;
    7554           0 :     while( (nGlyphs = rLayout.GetNextGlyphs( nTmpMaxGlyphs, pGlyphs, aGNGlyphPos, nIndex, nAdvanceWidths, pCharPosAry, pFallbackFonts )) != 0 )
    7555             :     {
    7556           0 :         aUnicodes.clear();
    7557           0 :         for( int i = 0; i < nGlyphs; i++ )
    7558             :         {
    7559             :             // default case: 1 glyph is one unicode
    7560           0 :             pUnicodesPerGlyph[i] = 1;
    7561           0 :             if( (pGlyphs[i] & GF_ISCHAR) )
    7562             :             {
    7563           0 :                 aUnicodes.push_back( static_cast<sal_Ucs>(pGlyphs[i] & GF_IDXMASK) );
    7564             :             }
    7565           0 :             else if( pCharPosAry[i] >= nMinCharPos && pCharPosAry[i] <= nMaxCharPos )
    7566             :             {
    7567           0 :                 int nChars = 1;
    7568           0 :                 aUnicodes.push_back( rText[ pCharPosAry[i] ] );
    7569           0 :                 pUnicodesPerGlyph[i] = 1;
    7570             :                 // try to handle ligatures and such
    7571           0 :                 if( i < nGlyphs-1 )
    7572             :                 {
    7573           0 :                     nChars = pCharPosAry[i+1] - pCharPosAry[i];
    7574             :                     // #i115618# fix for simple RTL+CTL cases
    7575             :                     // TODO: sanitize for RTL ligatures, more complex CTL, etc.
    7576           0 :                     if( nChars < 0 )
    7577           0 :                         nChars = -nChars;
    7578           0 :                     else if( nChars == 0 )
    7579           0 :                         nChars = 1;
    7580           0 :                     pUnicodesPerGlyph[i] = nChars;
    7581           0 :                     for( int n = 1; n < nChars; n++ )
    7582           0 :                         aUnicodes.push_back( rText[ pCharPosAry[i] + n ] );
    7583             :                 }
    7584             :                 // #i36691# hack that is needed because currently the pGlyphs[]
    7585             :                 // argument is ignored for embeddable fonts and so the layout
    7586             :                 // engine's glyph work is ignored (i.e. char mirroring)
    7587             :                 // TODO: a real solution would be to map the layout engine's
    7588             :                 // glyphid (i.e. FreeType's synthetic glyphid for a Type1 font)
    7589             :                 // back to unicode and then to embeddable font's encoding
    7590           0 :                 if( getReferenceDevice()->GetLayoutMode() & TEXT_LAYOUT_BIDI_RTL )
    7591             :                 {
    7592           0 :                     size_t nI = aUnicodes.size()-1;
    7593           0 :                     for( int n = 0; n < nChars; n++, nI-- )
    7594           0 :                         aUnicodes[nI] = static_cast<sal_Ucs>(GetMirroredChar(aUnicodes[nI]));
    7595           0 :                 }
    7596             :             }
    7597             :             else
    7598           0 :                 aUnicodes.push_back( 0 );
    7599             :             // note: in case of ctl one character may result
    7600             :             // in multiple glyphs. The current SalLayout
    7601             :             // implementations set -1 then to indicate that no direct
    7602             :             // mapping is possible
    7603             :         }
    7604             : 
    7605           0 :         registerGlyphs( nGlyphs, pGlyphs, pGlyphWidths, &aUnicodes[0], pUnicodesPerGlyph, pMappedGlyphs, pMappedFontObjects, pFallbackFonts );
    7606             : 
    7607           0 :         for( int i = 0; i < nGlyphs; i++ )
    7608             :         {
    7609             :             aGlyphs.push_back( PDFGlyph( aGNGlyphPos,
    7610             :                                          pGlyphWidths[i],
    7611           0 :                                          pGlyphs[i],
    7612             :                                          pMappedFontObjects[i],
    7613           0 :                                          pMappedGlyphs[i] ) );
    7614           0 :             if( bVertical )
    7615           0 :                 aGNGlyphPos.Y() += nAdvanceWidths[i]/rLayout.GetUnitsPerPixel();
    7616             :             else
    7617           0 :                 aGNGlyphPos.X() += nAdvanceWidths[i]/rLayout.GetUnitsPerPixel();
    7618             :         }
    7619             :     }
    7620             : 
    7621           0 :     Point aAlignOffset;
    7622           0 :     if ( eAlign == ALIGN_BOTTOM )
    7623           0 :         aAlignOffset.Y() -= aRefDevFontMetric.GetDescent();
    7624           0 :     else if ( eAlign == ALIGN_TOP )
    7625           0 :         aAlignOffset.Y() += aRefDevFontMetric.GetAscent();
    7626           0 :     if( aAlignOffset.X() || aAlignOffset.Y() )
    7627           0 :         aAlignOffset = aRotScale.transform( aAlignOffset );
    7628             : 
    7629             :     /* #159153# do not emit an empty glyph vector; this can happen if e.g. the original
    7630             :        string contained only on of the UTF16 BOMs
    7631             :     */
    7632           0 :     if( ! aGlyphs.empty() )
    7633             :     {
    7634           0 :         if( bVertical )
    7635           0 :             drawVerticalGlyphs( aGlyphs, aLine, aAlignOffset, aRotScale, fAngle, fXScale, fSkew, nFontHeight );
    7636             :         else
    7637           0 :             drawHorizontalGlyphs( aGlyphs, aLine, aAlignOffset, fAngle, fXScale, fSkew, nFontHeight, nPixelFontHeight );
    7638             :     }
    7639             : 
    7640             :     // end textobject
    7641           0 :     aLine.append( "ET\n" );
    7642           0 :     if( bPop )
    7643           0 :         aLine.append( "Q\n" );
    7644             : 
    7645           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    7646             : 
    7647             :     // draw eventual textlines
    7648           0 :     FontStrikeout eStrikeout = m_aCurrentPDFState.m_aFont.GetStrikeout();
    7649           0 :     FontUnderline eUnderline = m_aCurrentPDFState.m_aFont.GetUnderline();
    7650           0 :     FontUnderline eOverline  = m_aCurrentPDFState.m_aFont.GetOverline();
    7651           0 :     if( bTextLines &&
    7652             :         (
    7653           0 :          ( eUnderline != UNDERLINE_NONE && eUnderline != UNDERLINE_DONTKNOW ) ||
    7654           0 :          ( eOverline  != UNDERLINE_NONE && eOverline  != UNDERLINE_DONTKNOW ) ||
    7655           0 :          ( eStrikeout != STRIKEOUT_NONE && eStrikeout != STRIKEOUT_DONTKNOW )
    7656             :          )
    7657             :         )
    7658             :     {
    7659           0 :         bool bUnderlineAbove = OutputDevice::ImplIsUnderlineAbove( m_aCurrentPDFState.m_aFont );
    7660           0 :         if( m_aCurrentPDFState.m_aFont.IsWordLineMode() )
    7661             :         {
    7662           0 :             Point aPos, aStartPt;
    7663           0 :             sal_Int32 nWidth = 0, nAdvance=0;
    7664           0 :             for( int nStart = 0;;)
    7665             :             {
    7666             :                 sal_GlyphId aGlyphId;
    7667           0 :                 if( !rLayout.GetNextGlyphs( 1, &aGlyphId, aPos, nStart, &nAdvance ) )
    7668           0 :                     break;
    7669             : 
    7670           0 :                 if( !rLayout.IsSpacingGlyph( aGlyphId ) )
    7671             :                 {
    7672           0 :                     if( !nWidth )
    7673           0 :                         aStartPt = aPos;
    7674             : 
    7675           0 :                     nWidth += nAdvance;
    7676             :                 }
    7677           0 :                 else if( nWidth > 0 )
    7678             :                 {
    7679           0 :                     drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7680             :                                   m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7681           0 :                                   eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7682           0 :                     nWidth = 0;
    7683             :                 }
    7684           0 :             }
    7685             : 
    7686           0 :             if( nWidth > 0 )
    7687             :             {
    7688           0 :                 drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7689             :                               m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7690           0 :                               eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7691             :             }
    7692             :         }
    7693             :         else
    7694             :         {
    7695           0 :             Point aStartPt = rLayout.GetDrawPosition();
    7696           0 :             int nWidth = rLayout.GetTextWidth() / rLayout.GetUnitsPerPixel();
    7697           0 :             drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7698             :                           m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7699           0 :                           eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7700             :         }
    7701             :     }
    7702             : 
    7703             :     // write eventual emphasis marks
    7704           0 :     if( m_aCurrentPDFState.m_aFont.GetEmphasisMark() & EMPHASISMARK_STYLE )
    7705             :     {
    7706           0 :         PolyPolygon             aEmphPoly;
    7707           0 :         Rectangle               aEmphRect1;
    7708           0 :         Rectangle               aEmphRect2;
    7709             :         long                    nEmphYOff;
    7710             :         long                    nEmphWidth;
    7711             :         long                    nEmphHeight;
    7712             :         bool                    bEmphPolyLine;
    7713             :         FontEmphasisMark        nEmphMark;
    7714             : 
    7715           0 :         push( PUSH_ALL );
    7716             : 
    7717           0 :         aLine.setLength( 0 );
    7718           0 :         aLine.append( "q\n" );
    7719             : 
    7720           0 :         nEmphMark = m_pReferenceDevice->ImplGetEmphasisMarkStyle( m_aCurrentPDFState.m_aFont );
    7721           0 :         if ( nEmphMark & EMPHASISMARK_POS_BELOW )
    7722           0 :             nEmphHeight = m_pReferenceDevice->mnEmphasisDescent;
    7723             :         else
    7724           0 :             nEmphHeight = m_pReferenceDevice->mnEmphasisAscent;
    7725             :         m_pReferenceDevice->ImplGetEmphasisMark( aEmphPoly,
    7726             :                                                  bEmphPolyLine,
    7727             :                                                  aEmphRect1,
    7728             :                                                  aEmphRect2,
    7729             :                                                  nEmphYOff,
    7730             :                                                  nEmphWidth,
    7731             :                                                  nEmphMark,
    7732             :                                                  m_pReferenceDevice->ImplDevicePixelToLogicWidth(nEmphHeight),
    7733           0 :                                                  m_pReferenceDevice->mpFontEntry->mnOrientation );
    7734           0 :         if ( bEmphPolyLine )
    7735             :         {
    7736           0 :             setLineColor( m_aCurrentPDFState.m_aFont.GetColor() );
    7737           0 :             setFillColor( Color( COL_TRANSPARENT ) );
    7738             :         }
    7739             :         else
    7740             :         {
    7741           0 :             setFillColor( m_aCurrentPDFState.m_aFont.GetColor() );
    7742           0 :             setLineColor( Color( COL_TRANSPARENT ) );
    7743             :         }
    7744           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    7745             : 
    7746           0 :         Point aOffset = Point(0,0);
    7747             : 
    7748           0 :         if ( nEmphMark & EMPHASISMARK_POS_BELOW )
    7749           0 :             aOffset.Y() += m_pReferenceDevice->mpFontEntry->maMetric.mnDescent + nEmphYOff;
    7750             :         else
    7751           0 :             aOffset.Y() -= m_pReferenceDevice->mpFontEntry->maMetric.mnAscent + nEmphYOff;
    7752             : 
    7753           0 :         long nEmphWidth2     = nEmphWidth / 2;
    7754           0 :         long nEmphHeight2    = nEmphHeight / 2;
    7755           0 :         aOffset += Point( nEmphWidth2, nEmphHeight2 );
    7756             : 
    7757           0 :         if ( eAlign == ALIGN_BOTTOM )
    7758           0 :             aOffset.Y() -= m_pReferenceDevice->mpFontEntry->maMetric.mnDescent;
    7759           0 :         else if ( eAlign == ALIGN_TOP )
    7760           0 :             aOffset.Y() += m_pReferenceDevice->mpFontEntry->maMetric.mnAscent;
    7761             : 
    7762           0 :         for( int nStart = 0;;)
    7763             :         {
    7764           0 :             Point aPos;
    7765             :             sal_GlyphId aGlyphId;
    7766             :             sal_Int32 nAdvance;
    7767           0 :             if( !rLayout.GetNextGlyphs( 1, &aGlyphId, aPos, nStart, &nAdvance ) )
    7768           0 :                 break;
    7769             : 
    7770           0 :             if( !rLayout.IsSpacingGlyph( aGlyphId ) )
    7771             :             {
    7772           0 :                 Point aAdjOffset = aOffset;
    7773           0 :                 aAdjOffset.X() += (nAdvance - nEmphWidth) / 2;
    7774           0 :                 aAdjOffset = aRotScale.transform( aAdjOffset );
    7775             : 
    7776           0 :                 aAdjOffset -= Point( nEmphWidth2, nEmphHeight2 );
    7777             : 
    7778           0 :                 aPos += aAdjOffset;
    7779           0 :                 aPos = m_pReferenceDevice->PixelToLogic( aPos );
    7780           0 :                 drawEmphasisMark( aPos.X(), aPos.Y(),
    7781             :                                   aEmphPoly, bEmphPolyLine,
    7782           0 :                                   aEmphRect1, aEmphRect2 );
    7783             :             }
    7784           0 :         }
    7785             : 
    7786           0 :         writeBuffer( "Q\n", 2 );
    7787           0 :         pop();
    7788           0 :     }
    7789             : 
    7790             : }
    7791             : 
    7792           0 : void PDFWriterImpl::drawEmphasisMark( long nX, long nY,
    7793             :                                       const PolyPolygon& rPolyPoly, bool bPolyLine,
    7794             :                                       const Rectangle& rRect1, const Rectangle& rRect2 )
    7795             : {
    7796             :     // TODO: pass nWidth as width of this mark
    7797             :     // long nWidth = 0;
    7798             : 
    7799           0 :     if ( rPolyPoly.Count() )
    7800             :     {
    7801           0 :         if ( bPolyLine )
    7802             :         {
    7803           0 :             Polygon aPoly = rPolyPoly.GetObject( 0 );
    7804           0 :             aPoly.Move( nX, nY );
    7805           0 :             drawPolyLine( aPoly );
    7806             :         }
    7807             :         else
    7808             :         {
    7809           0 :             PolyPolygon aPolyPoly = rPolyPoly;
    7810           0 :             aPolyPoly.Move( nX, nY );
    7811           0 :             drawPolyPolygon( aPolyPoly );
    7812             :         }
    7813             :     }
    7814             : 
    7815           0 :     if ( !rRect1.IsEmpty() )
    7816             :     {
    7817           0 :         Rectangle aRect( Point( nX+rRect1.Left(),
    7818           0 :                                 nY+rRect1.Top() ), rRect1.GetSize() );
    7819           0 :         drawRectangle( aRect );
    7820             :     }
    7821             : 
    7822           0 :     if ( !rRect2.IsEmpty() )
    7823             :     {
    7824           0 :         Rectangle aRect( Point( nX+rRect2.Left(),
    7825           0 :                                 nY+rRect2.Top() ), rRect2.GetSize() );
    7826             : 
    7827           0 :         drawRectangle( aRect );
    7828             :     }
    7829           0 : }
    7830             : 
    7831           0 : void PDFWriterImpl::drawText( const Point& rPos, const OUString& rText, sal_Int32 nIndex, sal_Int32 nLen, bool bTextLines )
    7832             : {
    7833           0 :     MARK( "drawText" );
    7834             : 
    7835           0 :     updateGraphicsState();
    7836             : 
    7837             :     // get a layout from the OuputDevice's SalGraphics
    7838             :     // this also enforces font substitution and sets the font on SalGraphics
    7839           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos );
    7840           0 :     if( pLayout )
    7841             :     {
    7842           0 :         drawLayout( *pLayout, rText, bTextLines );
    7843           0 :         pLayout->Release();
    7844             :     }
    7845           0 : }
    7846             : 
    7847           0 : void PDFWriterImpl::drawTextArray( const Point& rPos, const OUString& rText, const sal_Int32* pDXArray, sal_Int32 nIndex, sal_Int32 nLen, bool bTextLines )
    7848             : {
    7849           0 :     MARK( "drawText with array" );
    7850             : 
    7851           0 :     updateGraphicsState();
    7852             : 
    7853             :     // get a layout from the OuputDevice's SalGraphics
    7854             :     // this also enforces font substitution and sets the font on SalGraphics
    7855           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos, 0, pDXArray );
    7856           0 :     if( pLayout )
    7857             :     {
    7858           0 :         drawLayout( *pLayout, rText, bTextLines );
    7859           0 :         pLayout->Release();
    7860             :     }
    7861           0 : }
    7862             : 
    7863           0 : void PDFWriterImpl::drawStretchText( const Point& rPos, sal_uLong nWidth, const OUString& rText, sal_Int32 nIndex, sal_Int32 nLen, bool bTextLines )
    7864             : {
    7865           0 :     MARK( "drawStretchText" );
    7866             : 
    7867           0 :     updateGraphicsState();
    7868             : 
    7869             :     // get a layout from the OuputDevice's SalGraphics
    7870             :     // this also enforces font substitution and sets the font on SalGraphics
    7871           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos, nWidth );
    7872           0 :     if( pLayout )
    7873             :     {
    7874           0 :         drawLayout( *pLayout, rText, bTextLines );
    7875           0 :         pLayout->Release();
    7876             :     }
    7877           0 : }
    7878             : 
    7879           0 : void PDFWriterImpl::drawText( const Rectangle& rRect, const OUString& rOrigStr, sal_uInt16 nStyle, bool bTextLines )
    7880             : {
    7881           0 :     long        nWidth          = rRect.GetWidth();
    7882           0 :     long        nHeight         = rRect.GetHeight();
    7883             : 
    7884           0 :     if ( nWidth <= 0 || nHeight <= 0 )
    7885           0 :         return;
    7886             : 
    7887           0 :     MARK( "drawText with rectangle" );
    7888             : 
    7889           0 :     updateGraphicsState();
    7890             : 
    7891             :     // clip with rectangle
    7892           0 :     OStringBuffer aLine;
    7893           0 :     aLine.append( "q " );
    7894           0 :     m_aPages.back().appendRect( rRect, aLine );
    7895           0 :     aLine.append( " W* n\n" );
    7896           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    7897             : 
    7898             :     // if disabled text is needed, put in here
    7899             : 
    7900           0 :     Point       aPos            = rRect.TopLeft();
    7901             : 
    7902           0 :     long        nTextHeight     = m_pReferenceDevice->GetTextHeight();
    7903           0 :     sal_Int32   nMnemonicPos    = -1;
    7904             : 
    7905           0 :     OUString aStr = rOrigStr;
    7906           0 :     if ( nStyle & TEXT_DRAW_MNEMONIC )
    7907           0 :         aStr = m_pReferenceDevice->GetNonMnemonicString( aStr, nMnemonicPos );
    7908             : 
    7909             :     // multiline text
    7910           0 :     if ( nStyle & TEXT_DRAW_MULTILINE )
    7911             :     {
    7912           0 :         OUString           aLastLine;
    7913           0 :         ImplMultiTextLineInfo   aMultiLineInfo;
    7914             :         ImplTextLineInfo*       pLineInfo;
    7915             :         sal_Int32               i;
    7916             :         sal_Int32               nLines;
    7917             :         sal_Int32               nFormatLines;
    7918             : 
    7919           0 :         if ( nTextHeight )
    7920             :         {
    7921           0 :             ::vcl::DefaultTextLayout aLayout( *m_pReferenceDevice );
    7922           0 :             OutputDevice::ImplGetTextLines( aMultiLineInfo, nWidth, aStr, nStyle, aLayout );
    7923           0 :             nLines = nHeight/nTextHeight;
    7924           0 :             nFormatLines = aMultiLineInfo.Count();
    7925           0 :             if ( !nLines )
    7926           0 :                 nLines = 1;
    7927           0 :             if ( nFormatLines > nLines )
    7928             :             {
    7929           0 :                 if ( nStyle & TEXT_DRAW_ENDELLIPSIS )
    7930             :                 {
    7931             :                     // handle last line
    7932           0 :                     nFormatLines = nLines-1;
    7933             : 
    7934           0 :                     pLineInfo = aMultiLineInfo.GetLine( nFormatLines );
    7935           0 :                     aLastLine = convertLineEnd(aStr.copy(pLineInfo->GetIndex()), LINEEND_LF);
    7936             :                     // replace line feed by space
    7937           0 :                     aLastLine = aLastLine.replace('\n', ' ');
    7938           0 :                     aLastLine = m_pReferenceDevice->GetEllipsisString( aLastLine, nWidth, nStyle );
    7939           0 :                     nStyle &= ~(TEXT_DRAW_VCENTER | TEXT_DRAW_BOTTOM);
    7940           0 :                     nStyle |= TEXT_DRAW_TOP;
    7941             :                 }
    7942             :             }
    7943             : 
    7944             :             // vertical alignment
    7945           0 :             if ( nStyle & TEXT_DRAW_BOTTOM )
    7946           0 :                 aPos.Y() += nHeight-(nFormatLines*nTextHeight);
    7947           0 :             else if ( nStyle & TEXT_DRAW_VCENTER )
    7948           0 :                 aPos.Y() += (nHeight-(nFormatLines*nTextHeight))/2;
    7949             : 
    7950             :             // draw all lines excluding the last
    7951           0 :             for ( i = 0; i < nFormatLines; i++ )
    7952             :             {
    7953           0 :                 pLineInfo = aMultiLineInfo.GetLine( i );
    7954           0 :                 if ( nStyle & TEXT_DRAW_RIGHT )
    7955           0 :                     aPos.X() += nWidth-pLineInfo->GetWidth();
    7956           0 :                 else if ( nStyle & TEXT_DRAW_CENTER )
    7957           0 :                     aPos.X() += (nWidth-pLineInfo->GetWidth())/2;
    7958           0 :                 sal_Int32 nIndex = pLineInfo->GetIndex();
    7959           0 :                 sal_Int32 nLineLen = pLineInfo->GetLen();
    7960           0 :                 drawText( aPos, aStr, nIndex, nLineLen, bTextLines );
    7961             :                 // mnemonics should not appear in documents,
    7962             :                 // if the need arises, put them in here
    7963           0 :                 aPos.Y() += nTextHeight;
    7964           0 :                 aPos.X() = rRect.Left();
    7965             :             }
    7966             : 
    7967             :             // output last line left adjusted since it was shortened
    7968           0 :             if (!aLastLine.isEmpty())
    7969           0 :                 drawText( aPos, aLastLine, 0, aLastLine.getLength(), bTextLines );
    7970           0 :         }
    7971             :     }
    7972             :     else
    7973             :     {
    7974           0 :         long nTextWidth = m_pReferenceDevice->GetTextWidth( aStr );
    7975             : 
    7976             :         // Evt. Text kuerzen
    7977           0 :         if ( nTextWidth > nWidth )
    7978             :         {
    7979           0 :             if ( nStyle & (TEXT_DRAW_ENDELLIPSIS | TEXT_DRAW_PATHELLIPSIS | TEXT_DRAW_NEWSELLIPSIS) )
    7980             :             {
    7981           0 :                 aStr = m_pReferenceDevice->GetEllipsisString( aStr, nWidth, nStyle );
    7982           0 :                 nStyle &= ~(TEXT_DRAW_CENTER | TEXT_DRAW_RIGHT);
    7983           0 :                 nStyle |= TEXT_DRAW_LEFT;
    7984           0 :                 nTextWidth = m_pReferenceDevice->GetTextWidth( aStr );
    7985             :             }
    7986             :         }
    7987             : 
    7988             :         // vertical alignment
    7989           0 :         if ( nStyle & TEXT_DRAW_RIGHT )
    7990           0 :             aPos.X() += nWidth-nTextWidth;
    7991           0 :         else if ( nStyle & TEXT_DRAW_CENTER )
    7992           0 :             aPos.X() += (nWidth-nTextWidth)/2;
    7993             : 
    7994           0 :         if ( nStyle & TEXT_DRAW_BOTTOM )
    7995           0 :             aPos.Y() += nHeight-nTextHeight;
    7996           0 :         else if ( nStyle & TEXT_DRAW_VCENTER )
    7997           0 :             aPos.Y() += (nHeight-nTextHeight)/2;
    7998             : 
    7999             :         // mnemonics should be inserted here if the need arises
    8000             : 
    8001             :         // draw the actual text
    8002           0 :         drawText( aPos, aStr, 0, aStr.getLength(), bTextLines );
    8003             :     }
    8004             : 
    8005             :     // reset clip region to original value
    8006           0 :     aLine.setLength( 0 );
    8007           0 :     aLine.append( "Q\n" );
    8008           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8009             : }
    8010             : 
    8011           0 : void PDFWriterImpl::drawLine( const Point& rStart, const Point& rStop )
    8012             : {
    8013           0 :     MARK( "drawLine" );
    8014             : 
    8015           0 :     updateGraphicsState();
    8016             : 
    8017           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    8018           0 :         return;
    8019             : 
    8020           0 :     OStringBuffer aLine;
    8021           0 :     m_aPages.back().appendPoint( rStart, aLine );
    8022           0 :     aLine.append( " m " );
    8023           0 :     m_aPages.back().appendPoint( rStop, aLine );
    8024           0 :     aLine.append( " l S\n" );
    8025             : 
    8026           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8027             : }
    8028             : 
    8029           0 : void PDFWriterImpl::drawLine( const Point& rStart, const Point& rStop, const LineInfo& rInfo )
    8030             : {
    8031           0 :     MARK( "drawLine with LineInfo" );
    8032           0 :     updateGraphicsState();
    8033             : 
    8034           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    8035           0 :         return;
    8036             : 
    8037           0 :     if( rInfo.GetStyle() == LINE_SOLID && rInfo.GetWidth() < 2 )
    8038             :     {
    8039           0 :         drawLine( rStart, rStop );
    8040           0 :         return;
    8041             :     }
    8042             : 
    8043           0 :     OStringBuffer aLine;
    8044             : 
    8045           0 :     aLine.append( "q " );
    8046           0 :     if( m_aPages.back().appendLineInfo( rInfo, aLine ) )
    8047             :     {
    8048           0 :         m_aPages.back().appendPoint( rStart, aLine );
    8049           0 :         aLine.append( " m " );
    8050           0 :         m_aPages.back().appendPoint( rStop, aLine );
    8051           0 :         aLine.append( " l S Q\n" );
    8052             : 
    8053           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    8054             :     }
    8055             :     else
    8056             :     {
    8057           0 :         PDFWriter::ExtLineInfo aInfo;
    8058           0 :         convertLineInfoToExtLineInfo( rInfo, aInfo );
    8059           0 :         Point aPolyPoints[2] = { rStart, rStop };
    8060           0 :         Polygon aPoly( 2, aPolyPoints );
    8061           0 :         drawPolyLine( aPoly, aInfo );
    8062           0 :     }
    8063             : }
    8064             : 
    8065             : #define HCONV( x ) m_pReferenceDevice->ImplDevicePixelToLogicHeight( x )
    8066             : 
    8067           0 : void PDFWriterImpl::drawWaveTextLine( OStringBuffer& aLine, long nWidth, FontUnderline eTextLine, Color aColor, bool bIsAbove )
    8068             : {
    8069             :     // note: units in pFontEntry are ref device pixel
    8070           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8071           0 :     long            nLineHeight = 0;
    8072           0 :     long            nLinePos = 0;
    8073             : 
    8074           0 :     appendStrokingColor( aColor, aLine );
    8075           0 :     aLine.append( "\n" );
    8076             : 
    8077           0 :     if ( bIsAbove )
    8078             :     {
    8079           0 :         if ( !pFontEntry->maMetric.mnAboveWUnderlineSize )
    8080           0 :             m_pReferenceDevice->ImplInitAboveTextLineSize();
    8081           0 :         nLineHeight = HCONV( pFontEntry->maMetric.mnAboveWUnderlineSize );
    8082           0 :         nLinePos = HCONV( pFontEntry->maMetric.mnAboveWUnderlineOffset );
    8083             :     }
    8084             :     else
    8085             :     {
    8086           0 :         if ( !pFontEntry->maMetric.mnWUnderlineSize )
    8087           0 :             m_pReferenceDevice->ImplInitTextLineSize();
    8088           0 :         nLineHeight = HCONV( pFontEntry->maMetric.mnWUnderlineSize );
    8089           0 :         nLinePos = HCONV( pFontEntry->maMetric.mnWUnderlineOffset );
    8090             :     }
    8091           0 :     if ( (eTextLine == UNDERLINE_SMALLWAVE) && (nLineHeight > 3) )
    8092           0 :         nLineHeight = 3;
    8093             : 
    8094           0 :     long nLineWidth = getReferenceDevice()->mnDPIX/450;
    8095           0 :     if ( ! nLineWidth )
    8096           0 :         nLineWidth = 1;
    8097             : 
    8098           0 :     if ( eTextLine == UNDERLINE_BOLDWAVE )
    8099           0 :         nLineWidth = 3*nLineWidth;
    8100             : 
    8101           0 :     m_aPages.back().appendMappedLength( (sal_Int32)nLineWidth, aLine );
    8102           0 :     aLine.append( " w " );
    8103             : 
    8104           0 :     if ( eTextLine == UNDERLINE_DOUBLEWAVE )
    8105             :     {
    8106           0 :         long nOrgLineHeight = nLineHeight;
    8107           0 :         nLineHeight /= 3;
    8108           0 :         if ( nLineHeight < 2 )
    8109             :         {
    8110           0 :             if ( nOrgLineHeight > 1 )
    8111           0 :                 nLineHeight = 2;
    8112             :             else
    8113           0 :                 nLineHeight = 1;
    8114             :         }
    8115           0 :         long nLineDY = nOrgLineHeight-(nLineHeight*2);
    8116           0 :         if ( nLineDY < nLineWidth )
    8117           0 :             nLineDY = nLineWidth;
    8118           0 :         long nLineDY2 = nLineDY/2;
    8119           0 :         if ( !nLineDY2 )
    8120           0 :             nLineDY2 = 1;
    8121             : 
    8122           0 :         nLinePos -= nLineWidth-nLineDY2;
    8123             : 
    8124           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, 2*nLineHeight, aLine );
    8125             : 
    8126           0 :         nLinePos += nLineWidth+nLineDY;
    8127           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, 2*nLineHeight, aLine );
    8128             :     }
    8129             :     else
    8130             :     {
    8131           0 :         if ( eTextLine != UNDERLINE_BOLDWAVE )
    8132           0 :             nLinePos -= nLineWidth/2;
    8133           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, nLineHeight, aLine );
    8134             :     }
    8135           0 : }
    8136             : 
    8137           0 : void PDFWriterImpl::drawStraightTextLine( OStringBuffer& aLine, long nWidth, FontUnderline eTextLine, Color aColor, bool bIsAbove )
    8138             : {
    8139             :     // note: units in pFontEntry are ref device pixel
    8140           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8141           0 :     long            nLineHeight = 0;
    8142           0 :     long            nLinePos  = 0;
    8143           0 :     long            nLinePos2 = 0;
    8144             : 
    8145           0 :     if ( eTextLine > UNDERLINE_BOLDWAVE )
    8146           0 :         eTextLine = UNDERLINE_SINGLE;
    8147             : 
    8148           0 :     switch ( eTextLine )
    8149             :     {
    8150             :         case UNDERLINE_SINGLE:
    8151             :         case UNDERLINE_DOTTED:
    8152             :         case UNDERLINE_DASH:
    8153             :         case UNDERLINE_LONGDASH:
    8154             :         case UNDERLINE_DASHDOT:
    8155             :         case UNDERLINE_DASHDOTDOT:
    8156           0 :             if ( bIsAbove )
    8157             :             {
    8158           0 :                 if ( !pFontEntry->maMetric.mnAboveUnderlineSize )
    8159           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8160           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveUnderlineSize );
    8161           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveUnderlineOffset );
    8162             :             }
    8163             :             else
    8164             :             {
    8165           0 :                 if ( !pFontEntry->maMetric.mnUnderlineSize )
    8166           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8167           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnUnderlineSize );
    8168           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnUnderlineOffset );
    8169             :             }
    8170           0 :             break;
    8171             :         case UNDERLINE_BOLD:
    8172             :         case UNDERLINE_BOLDDOTTED:
    8173             :         case UNDERLINE_BOLDDASH:
    8174             :         case UNDERLINE_BOLDLONGDASH:
    8175             :         case UNDERLINE_BOLDDASHDOT:
    8176             :         case UNDERLINE_BOLDDASHDOTDOT:
    8177           0 :             if ( bIsAbove )
    8178             :             {
    8179           0 :                 if ( !pFontEntry->maMetric.mnAboveBUnderlineSize )
    8180           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8181           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveBUnderlineSize );
    8182           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveBUnderlineOffset );
    8183             :             }
    8184             :             else
    8185             :             {
    8186           0 :                 if ( !pFontEntry->maMetric.mnBUnderlineSize )
    8187           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8188           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnBUnderlineSize );
    8189           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnBUnderlineOffset );
    8190           0 :                 nLinePos += nLineHeight/2;
    8191             :             }
    8192           0 :             break;
    8193             :         case UNDERLINE_DOUBLE:
    8194           0 :             if ( bIsAbove )
    8195             :             {
    8196           0 :                 if ( !pFontEntry->maMetric.mnAboveDUnderlineSize )
    8197           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8198           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveDUnderlineSize );
    8199           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveDUnderlineOffset1 );
    8200           0 :                 nLinePos2   = HCONV( pFontEntry->maMetric.mnAboveDUnderlineOffset2 );
    8201             :             }
    8202             :             else
    8203             :             {
    8204           0 :                 if ( !pFontEntry->maMetric.mnDUnderlineSize )
    8205           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8206           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnDUnderlineSize );
    8207           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnDUnderlineOffset1 );
    8208           0 :                 nLinePos2   = HCONV( pFontEntry->maMetric.mnDUnderlineOffset2 );
    8209             :             }
    8210             :         default:
    8211           0 :             break;
    8212             :     }
    8213             : 
    8214           0 :     if ( nLineHeight )
    8215             :     {
    8216           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, true );
    8217           0 :         aLine.append( " w " );
    8218           0 :         appendStrokingColor( aColor, aLine );
    8219           0 :         aLine.append( "\n" );
    8220             : 
    8221           0 :         switch ( eTextLine )
    8222             :         {
    8223             :             case UNDERLINE_DOTTED:
    8224             :             case UNDERLINE_BOLDDOTTED:
    8225           0 :                 aLine.append( "[ " );
    8226           0 :                 m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8227           0 :                 aLine.append( " ] 0 d\n" );
    8228           0 :                 break;
    8229             :             case UNDERLINE_DASH:
    8230             :             case UNDERLINE_LONGDASH:
    8231             :             case UNDERLINE_BOLDDASH:
    8232             :             case UNDERLINE_BOLDLONGDASH:
    8233             :                 {
    8234           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8235           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8236           0 :                     if ( ( eTextLine == UNDERLINE_LONGDASH ) || ( eTextLine == UNDERLINE_BOLDLONGDASH ) )
    8237           0 :                         nDashLength = 8*nLineHeight;
    8238             : 
    8239           0 :                     aLine.append( "[ " );
    8240           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8241           0 :                     aLine.append( ' ' );
    8242           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8243           0 :                     aLine.append( " ] 0 d\n" );
    8244             :                 }
    8245           0 :                 break;
    8246             :             case UNDERLINE_DASHDOT:
    8247             :             case UNDERLINE_BOLDDASHDOT:
    8248             :                 {
    8249           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8250           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8251           0 :                     aLine.append( "[ " );
    8252           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8253           0 :                     aLine.append( ' ' );
    8254           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8255           0 :                     aLine.append( ' ' );
    8256           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8257           0 :                     aLine.append( ' ' );
    8258           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8259           0 :                     aLine.append( " ] 0 d\n" );
    8260             :                 }
    8261           0 :                 break;
    8262             :             case UNDERLINE_DASHDOTDOT:
    8263             :             case UNDERLINE_BOLDDASHDOTDOT:
    8264             :                 {
    8265           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8266           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8267           0 :                     aLine.append( "[ " );
    8268           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8269           0 :                     aLine.append( ' ' );
    8270           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8271           0 :                     aLine.append( ' ' );
    8272           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8273           0 :                     aLine.append( ' ' );
    8274           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8275           0 :                     aLine.append( ' ' );
    8276           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8277           0 :                     aLine.append( ' ' );
    8278           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8279           0 :                     aLine.append( " ] 0 d\n" );
    8280             :                 }
    8281           0 :                 break;
    8282             :             default:
    8283           0 :                 break;
    8284             :         }
    8285             : 
    8286           0 :         aLine.append( "0 " );
    8287           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8288           0 :         aLine.append( " m " );
    8289           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, false );
    8290           0 :         aLine.append( ' ' );
    8291           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8292           0 :         aLine.append( " l S\n" );
    8293           0 :         if ( eTextLine == UNDERLINE_DOUBLE )
    8294             :         {
    8295           0 :             aLine.append( "0 " );
    8296           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8297           0 :             aLine.append( " m " );
    8298           0 :             m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, false );
    8299           0 :             aLine.append( ' ' );
    8300           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8301           0 :             aLine.append( " l S\n" );
    8302             :         }
    8303             :     }
    8304           0 : }
    8305             : 
    8306           0 : void PDFWriterImpl::drawStrikeoutLine( OStringBuffer& aLine, long nWidth, FontStrikeout eStrikeout, Color aColor )
    8307             : {
    8308             :     // note: units in pFontEntry are ref device pixel
    8309           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8310           0 :     long            nLineHeight = 0;
    8311           0 :     long            nLinePos  = 0;
    8312           0 :     long            nLinePos2 = 0;
    8313             : 
    8314           0 :     if ( eStrikeout > STRIKEOUT_X )
    8315           0 :         eStrikeout = STRIKEOUT_SINGLE;
    8316             : 
    8317           0 :     switch ( eStrikeout )
    8318             :     {
    8319             :         case STRIKEOUT_SINGLE:
    8320           0 :             if ( !pFontEntry->maMetric.mnStrikeoutSize )
    8321           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8322           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnStrikeoutSize );
    8323           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnStrikeoutOffset );
    8324           0 :             break;
    8325             :         case STRIKEOUT_BOLD:
    8326           0 :             if ( !pFontEntry->maMetric.mnBStrikeoutSize )
    8327           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8328           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnBStrikeoutSize );
    8329           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnBStrikeoutOffset );
    8330           0 :             break;
    8331             :         case STRIKEOUT_DOUBLE:
    8332           0 :             if ( !pFontEntry->maMetric.mnDStrikeoutSize )
    8333           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8334           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnDStrikeoutSize );
    8335           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnDStrikeoutOffset1 );
    8336           0 :             nLinePos2   = HCONV( pFontEntry->maMetric.mnDStrikeoutOffset2 );
    8337           0 :             break;
    8338             :         default:
    8339           0 :             break;
    8340             :     }
    8341             : 
    8342           0 :     if ( nLineHeight )
    8343             :     {
    8344           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, true );
    8345           0 :         aLine.append( " w " );
    8346           0 :         appendStrokingColor( aColor, aLine );
    8347           0 :         aLine.append( "\n" );
    8348             : 
    8349           0 :         aLine.append( "0 " );
    8350           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8351           0 :         aLine.append( " m " );
    8352           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, true );
    8353           0 :         aLine.append( ' ' );
    8354           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8355           0 :         aLine.append( " l S\n" );
    8356             : 
    8357           0 :         if ( eStrikeout == STRIKEOUT_DOUBLE )
    8358             :         {
    8359           0 :             aLine.append( "0 " );
    8360           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8361           0 :             aLine.append( " m " );
    8362           0 :             m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, true );
    8363           0 :             aLine.append( ' ' );
    8364           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8365           0 :             aLine.append( " l S\n" );
    8366             :         }
    8367             :     }
    8368           0 : }
    8369             : 
    8370           0 : void PDFWriterImpl::drawStrikeoutChar( const Point& rPos, long nWidth, FontStrikeout eStrikeout )
    8371             : {
    8372             :     //See qadevOOo/testdocs/StrikeThrough.odt for examples if you need
    8373             :     //to tweak this
    8374             : 
    8375           0 :     OUString aStrikeoutChar = eStrikeout == STRIKEOUT_SLASH ? OUString( "/" ) : OUString( "X" );
    8376           0 :     OUString aStrikeout = aStrikeoutChar;
    8377           0 :     while( m_pReferenceDevice->GetTextWidth( aStrikeout ) < nWidth )
    8378           0 :         aStrikeout += aStrikeout;
    8379             : 
    8380             :     // do not get broader than nWidth modulo 1 character
    8381           0 :     while( m_pReferenceDevice->GetTextWidth( aStrikeout ) >= nWidth )
    8382           0 :         aStrikeout = aStrikeout.replaceAt( 0, 1, "" );
    8383           0 :     aStrikeout += aStrikeoutChar;
    8384           0 :     bool bShadow = m_aCurrentPDFState.m_aFont.IsShadow();
    8385           0 :     if ( bShadow )
    8386             :     {
    8387           0 :         Font aFont = m_aCurrentPDFState.m_aFont;
    8388           0 :         aFont.SetShadow( false );
    8389           0 :         setFont( aFont );
    8390           0 :         updateGraphicsState();
    8391             :     }
    8392             : 
    8393             :     // strikeout string is left aligned non-CTL text
    8394           0 :     sal_uLong nOrigTLM = m_pReferenceDevice->GetLayoutMode();
    8395           0 :     m_pReferenceDevice->SetLayoutMode( TEXT_LAYOUT_BIDI_STRONG|TEXT_LAYOUT_COMPLEX_DISABLED );
    8396             : 
    8397           0 :     push( PUSH_CLIPREGION );
    8398           0 :     FontMetric aRefDevFontMetric = m_pReferenceDevice->GetFontMetric();
    8399           0 :     Rectangle aRect;
    8400           0 :     aRect.Left() = rPos.X();
    8401           0 :     aRect.Right() = aRect.Left()+nWidth;
    8402           0 :     aRect.Bottom() = rPos.Y()+aRefDevFontMetric.GetDescent();
    8403           0 :     aRect.Top() = rPos.Y()-aRefDevFontMetric.GetAscent();
    8404             : 
    8405           0 :     ImplFontEntry* pFontEntry = m_pReferenceDevice->mpFontEntry;
    8406           0 :     if (pFontEntry->mnOrientation)
    8407             :     {
    8408           0 :         Polygon aPoly( aRect );
    8409           0 :         aPoly.Rotate( rPos, pFontEntry->mnOrientation);
    8410           0 :         aRect = aPoly.GetBoundRect();
    8411             :     }
    8412             : 
    8413           0 :     intersectClipRegion( aRect );
    8414           0 :     drawText( rPos, aStrikeout, 0, aStrikeout.getLength(), false );
    8415           0 :     pop();
    8416             : 
    8417           0 :     m_pReferenceDevice->SetLayoutMode( nOrigTLM );
    8418             : 
    8419           0 :     if ( bShadow )
    8420             :     {
    8421           0 :         Font aFont = m_aCurrentPDFState.m_aFont;
    8422           0 :         aFont.SetShadow( true );
    8423           0 :         setFont( aFont );
    8424           0 :         updateGraphicsState();
    8425           0 :     }
    8426           0 : }
    8427             : 
    8428           0 : void PDFWriterImpl::drawTextLine( const Point& rPos, long nWidth, FontStrikeout eStrikeout, FontUnderline eUnderline, FontUnderline eOverline, bool bUnderlineAbove )
    8429             : {
    8430           0 :     if ( !nWidth ||
    8431           0 :          ( ((eStrikeout == STRIKEOUT_NONE)||(eStrikeout == STRIKEOUT_DONTKNOW)) &&
    8432           0 :            ((eUnderline == UNDERLINE_NONE)||(eUnderline == UNDERLINE_DONTKNOW)) &&
    8433           0 :            ((eOverline  == UNDERLINE_NONE)||(eOverline  == UNDERLINE_DONTKNOW)) ) )
    8434           0 :         return;
    8435             : 
    8436           0 :     MARK( "drawTextLine" );
    8437           0 :     updateGraphicsState();
    8438             : 
    8439             :     // note: units in pFontEntry are ref device pixel
    8440           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8441           0 :     Color           aUnderlineColor = m_aCurrentPDFState.m_aTextLineColor;
    8442           0 :     Color           aOverlineColor  = m_aCurrentPDFState.m_aOverlineColor;
    8443           0 :     Color           aStrikeoutColor = m_aCurrentPDFState.m_aFont.GetColor();
    8444           0 :     bool            bStrikeoutDone = false;
    8445           0 :     bool            bUnderlineDone = false;
    8446           0 :     bool            bOverlineDone  = false;
    8447             : 
    8448           0 :     if ( (eStrikeout == STRIKEOUT_SLASH) || (eStrikeout == STRIKEOUT_X) )
    8449             :     {
    8450           0 :         drawStrikeoutChar( rPos, nWidth, eStrikeout );
    8451           0 :         bStrikeoutDone = true;
    8452             :     }
    8453             : 
    8454           0 :     Point aPos( rPos );
    8455           0 :     TextAlign eAlign = m_aCurrentPDFState.m_aFont.GetAlign();
    8456           0 :     if( eAlign == ALIGN_TOP )
    8457           0 :         aPos.Y() += HCONV( pFontEntry->maMetric.mnAscent );
    8458           0 :     else if( eAlign == ALIGN_BOTTOM )
    8459           0 :         aPos.Y() -= HCONV( pFontEntry->maMetric.mnDescent );
    8460             : 
    8461           0 :     OStringBuffer aLine( 512 );
    8462             :     // save GS
    8463           0 :     aLine.append( "q " );
    8464             : 
    8465             :     // rotate and translate matrix
    8466           0 :     double fAngle = (double)m_aCurrentPDFState.m_aFont.GetOrientation() * M_PI / 1800.0;
    8467           0 :     Matrix3 aMat;
    8468           0 :     aMat.rotate( fAngle );
    8469           0 :     aMat.translate( aPos.X(), aPos.Y() );
    8470           0 :     aMat.append( m_aPages.back(), aLine );
    8471           0 :     aLine.append( " cm\n" );
    8472             : 
    8473           0 :     if ( aUnderlineColor.GetTransparency() != 0 )
    8474           0 :         aUnderlineColor = aStrikeoutColor;
    8475             : 
    8476           0 :     if ( (eUnderline == UNDERLINE_SMALLWAVE) ||
    8477           0 :          (eUnderline == UNDERLINE_WAVE) ||
    8478           0 :          (eUnderline == UNDERLINE_DOUBLEWAVE) ||
    8479             :          (eUnderline == UNDERLINE_BOLDWAVE) )
    8480             :     {
    8481           0 :         drawWaveTextLine( aLine, nWidth, eUnderline, aUnderlineColor, bUnderlineAbove );
    8482           0 :         bUnderlineDone = true;
    8483             :     }
    8484             : 
    8485           0 :     if ( (eOverline == UNDERLINE_SMALLWAVE) ||
    8486           0 :          (eOverline == UNDERLINE_WAVE) ||
    8487           0 :          (eOverline == UNDERLINE_DOUBLEWAVE) ||
    8488             :          (eOverline == UNDERLINE_BOLDWAVE) )
    8489             :     {
    8490           0 :         drawWaveTextLine( aLine, nWidth, eOverline, aOverlineColor, true );
    8491           0 :         bOverlineDone = true;
    8492             :     }
    8493             : 
    8494           0 :     if ( !bUnderlineDone )
    8495             :     {
    8496           0 :         drawStraightTextLine( aLine, nWidth, eUnderline, aUnderlineColor, bUnderlineAbove );
    8497             :     }
    8498             : 
    8499           0 :     if ( !bOverlineDone )
    8500             :     {
    8501           0 :         drawStraightTextLine( aLine, nWidth, eOverline, aOverlineColor, true );
    8502             :     }
    8503             : 
    8504           0 :     if ( !bStrikeoutDone )
    8505             :     {
    8506           0 :         drawStrikeoutLine( aLine, nWidth, eStrikeout, aStrikeoutColor );
    8507             :     }
    8508             : 
    8509           0 :     aLine.append( "Q\n" );
    8510           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8511             : }
    8512             : 
    8513           0 : void PDFWriterImpl::drawPolygon( const Polygon& rPoly )
    8514             : {
    8515           0 :     MARK( "drawPolygon" );
    8516             : 
    8517           0 :     updateGraphicsState();
    8518             : 
    8519           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8520           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8521           0 :         return;
    8522             : 
    8523           0 :     int nPoints = rPoly.GetSize();
    8524           0 :     OStringBuffer aLine( 20 * nPoints );
    8525           0 :     m_aPages.back().appendPolygon( rPoly, aLine );
    8526           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8527           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8528           0 :         aLine.append( "B*\n" );
    8529           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8530           0 :         aLine.append( "S\n" );
    8531             :     else
    8532           0 :         aLine.append( "f*\n" );
    8533             : 
    8534           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8535             : }
    8536             : 
    8537           0 : void PDFWriterImpl::drawPolyPolygon( const PolyPolygon& rPolyPoly )
    8538             : {
    8539           0 :     MARK( "drawPolyPolygon" );
    8540             : 
    8541           0 :     updateGraphicsState();
    8542             : 
    8543           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8544           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8545           0 :         return;
    8546             : 
    8547           0 :     int nPolygons = rPolyPoly.Count();
    8548             : 
    8549           0 :     OStringBuffer aLine( 40 * nPolygons );
    8550           0 :     m_aPages.back().appendPolyPolygon( rPolyPoly, aLine );
    8551           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8552           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8553           0 :         aLine.append( "B*\n" );
    8554           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8555           0 :         aLine.append( "S\n" );
    8556             :     else
    8557           0 :         aLine.append( "f*\n" );
    8558             : 
    8559           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8560             : }
    8561             : 
    8562           0 : void PDFWriterImpl::drawTransparent( const PolyPolygon& rPolyPoly, sal_uInt32 nTransparentPercent )
    8563             : {
    8564             :     DBG_ASSERT( nTransparentPercent <= 100, "invalid alpha value" );
    8565           0 :     nTransparentPercent = nTransparentPercent % 100;
    8566             : 
    8567           0 :     MARK( "drawTransparent" );
    8568             : 
    8569           0 :     updateGraphicsState();
    8570             : 
    8571           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8572           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8573           0 :         return;
    8574             : 
    8575           0 :     if( m_bIsPDF_A1 || m_aContext.Version < PDFWriter::PDF_1_4 )
    8576             :     {
    8577             :         m_aErrors.insert( m_bIsPDF_A1 ?
    8578             :                           PDFWriter::Warning_Transparency_Omitted_PDFA :
    8579           0 :                           PDFWriter::Warning_Transparency_Omitted_PDF13 );
    8580             : 
    8581           0 :         drawPolyPolygon( rPolyPoly );
    8582           0 :         return;
    8583             :     }
    8584             : 
    8585             :     // create XObject
    8586           0 :     m_aTransparentObjects.push_back( TransparencyEmit() );
    8587             :     // FIXME: polygons with beziers may yield incorrect bound rect
    8588           0 :     m_aTransparentObjects.back().m_aBoundRect     = rPolyPoly.GetBoundRect();
    8589             :     // convert rectangle to default user space
    8590           0 :     m_aPages.back().convertRect( m_aTransparentObjects.back().m_aBoundRect );
    8591           0 :     m_aTransparentObjects.back().m_nObject          = createObject();
    8592           0 :     m_aTransparentObjects.back().m_nExtGStateObject = createObject();
    8593           0 :     m_aTransparentObjects.back().m_fAlpha           = (double)(100-nTransparentPercent) / 100.0;
    8594           0 :     m_aTransparentObjects.back().m_pContentStream   = new SvMemoryStream( 256, 256 );
    8595             :     // create XObject's content stream
    8596           0 :     OStringBuffer aContent( 256 );
    8597           0 :     m_aPages.back().appendPolyPolygon( rPolyPoly, aContent );
    8598           0 :     if( m_aCurrentPDFState.m_aLineColor != Color( COL_TRANSPARENT ) &&
    8599           0 :         m_aCurrentPDFState.m_aFillColor != Color( COL_TRANSPARENT ) )
    8600           0 :         aContent.append( " B*\n" );
    8601           0 :     else if( m_aCurrentPDFState.m_aLineColor != Color( COL_TRANSPARENT ) )
    8602           0 :         aContent.append( " S\n" );
    8603             :     else
    8604           0 :         aContent.append( " f*\n" );
    8605           0 :     m_aTransparentObjects.back().m_pContentStream->Write( aContent.getStr(), aContent.getLength() );
    8606             : 
    8607           0 :     OStringBuffer aObjName( 16 );
    8608           0 :     aObjName.append( "Tr" );
    8609           0 :     aObjName.append( m_aTransparentObjects.back().m_nObject );
    8610           0 :     OString aTrName( aObjName.makeStringAndClear() );
    8611           0 :     aObjName.append( "EGS" );
    8612           0 :     aObjName.append( m_aTransparentObjects.back().m_nExtGStateObject );
    8613           0 :     OString aExtName( aObjName.makeStringAndClear() );
    8614             : 
    8615           0 :     OStringBuffer aLine( 80 );
    8616             :     // insert XObject
    8617           0 :     aLine.append( "q /" );
    8618           0 :     aLine.append( aExtName );
    8619           0 :     aLine.append( " gs /" );
    8620           0 :     aLine.append( aTrName );
    8621           0 :     aLine.append( " Do Q\n" );
    8622           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8623             : 
    8624           0 :     pushResource( ResXObject, aTrName, m_aTransparentObjects.back().m_nObject );
    8625           0 :     pushResource( ResExtGState, aExtName, m_aTransparentObjects.back().m_nExtGStateObject );
    8626             : }
    8627             : 
    8628           0 : void PDFWriterImpl::pushResource( ResourceKind eKind, const OString& rResource, sal_Int32 nObject )
    8629             : {
    8630           0 :     if( nObject >= 0 )
    8631             :     {
    8632           0 :         switch( eKind )
    8633             :         {
    8634             :             case ResXObject:
    8635           0 :                 m_aGlobalResourceDict.m_aXObjects[ rResource ] = nObject;
    8636           0 :                 if( ! m_aOutputStreams.empty() )
    8637           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aXObjects[ rResource ] = nObject;
    8638           0 :                 break;
    8639             :             case ResExtGState:
    8640           0 :                 m_aGlobalResourceDict.m_aExtGStates[ rResource ] = nObject;
    8641           0 :                 if( ! m_aOutputStreams.empty() )
    8642           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aExtGStates[ rResource ] = nObject;
    8643           0 :                 break;
    8644             :             case ResShading:
    8645           0 :                 m_aGlobalResourceDict.m_aShadings[ rResource ] = nObject;
    8646           0 :                 if( ! m_aOutputStreams.empty() )
    8647           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aShadings[ rResource ] = nObject;
    8648           0 :                 break;
    8649             :             case ResPattern:
    8650           0 :                 m_aGlobalResourceDict.m_aPatterns[ rResource ] = nObject;
    8651           0 :                 if( ! m_aOutputStreams.empty() )
    8652           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aPatterns[ rResource ] = nObject;
    8653           0 :                 break;
    8654             :         }
    8655             :     }
    8656           0 : }
    8657             : 
    8658           0 : void PDFWriterImpl::beginRedirect( SvStream* pStream, const Rectangle& rTargetRect )
    8659             : {
    8660           0 :     push( PUSH_ALL );
    8661             : 
    8662             :     // force reemitting clip region inside the new stream, and
    8663             :     // prevent emitting an unbalanced "Q" at the start
    8664           0 :     clearClipRegion();
    8665             :     // this is needed to point m_aCurrentPDFState at the pushed state
    8666             :     // ... but it's pointless to actually write into the "outer" stream here!
    8667           0 :     updateGraphicsState(NOWRITE);
    8668             : 
    8669           0 :     m_aOutputStreams.push_front( StreamRedirect() );
    8670           0 :     m_aOutputStreams.front().m_pStream = pStream;
    8671           0 :     m_aOutputStreams.front().m_aMapMode = m_aMapMode;
    8672             : 
    8673           0 :     if( !rTargetRect.IsEmpty() )
    8674             :     {
    8675           0 :         m_aOutputStreams.front().m_aTargetRect =
    8676           0 :             lcl_convert( m_aGraphicsStack.front().m_aMapMode,
    8677             :                          m_aMapMode,
    8678             :                          getReferenceDevice(),
    8679           0 :                          rTargetRect );
    8680           0 :         Point aDelta = m_aOutputStreams.front().m_aTargetRect.BottomLeft();
    8681           0 :         long nPageHeight = pointToPixel(m_aPages[m_nCurrentPage].getHeight());
    8682           0 :         aDelta.Y() = -(nPageHeight - m_aOutputStreams.front().m_aTargetRect.Bottom());
    8683           0 :         m_aMapMode.SetOrigin( m_aMapMode.GetOrigin() + aDelta );
    8684             :     }
    8685             : 
    8686             :     // setup graphics state for independent object stream
    8687             : 
    8688             :     // force reemitting colors
    8689           0 :     m_aCurrentPDFState.m_aLineColor = Color( COL_TRANSPARENT );
    8690           0 :     m_aCurrentPDFState.m_aFillColor = Color( COL_TRANSPARENT );
    8691           0 : }
    8692             : 
    8693           0 : SvStream* PDFWriterImpl::endRedirect()
    8694             : {
    8695           0 :     SvStream* pStream = NULL;
    8696           0 :     if( ! m_aOutputStreams.empty() )
    8697             :     {
    8698           0 :         pStream     = m_aOutputStreams.front().m_pStream;
    8699           0 :         m_aMapMode  = m_aOutputStreams.front().m_aMapMode;
    8700           0 :         m_aOutputStreams.pop_front();
    8701             :     }
    8702             : 
    8703           0 :     pop();
    8704             : 
    8705           0 :     m_aCurrentPDFState.m_aLineColor = Color( COL_TRANSPARENT );
    8706           0 :     m_aCurrentPDFState.m_aFillColor = Color( COL_TRANSPARENT );
    8707             : 
    8708             :     // needed after pop() to set m_aCurrentPDFState
    8709           0 :     updateGraphicsState(NOWRITE);
    8710             : 
    8711           0 :     return pStream;
    8712             : }
    8713             : 
    8714           0 : void PDFWriterImpl::beginTransparencyGroup()
    8715             : {
    8716           0 :     updateGraphicsState();
    8717           0 :     if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    8718           0 :         beginRedirect( new SvMemoryStream( 1024, 1024 ), Rectangle() );
    8719           0 : }
    8720             : 
    8721           0 : void PDFWriterImpl::endTransparencyGroup( const Rectangle& rBoundingBox, sal_uInt32 nTransparentPercent )
    8722             : {
    8723             :     DBG_ASSERT( nTransparentPercent <= 100, "invalid alpha value" );
    8724           0 :     nTransparentPercent = nTransparentPercent % 100;
    8725             : 
    8726           0 :     if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    8727             :     {
    8728             :         // create XObject
    8729           0 :         m_aTransparentObjects.push_back( TransparencyEmit() );
    8730           0 :         m_aTransparentObjects.back().m_aBoundRect   = rBoundingBox;
    8731             :         // convert rectangle to default user space
    8732           0 :         m_aPages.back().convertRect( m_aTransparentObjects.back().m_aBoundRect );
    8733           0 :         m_aTransparentObjects.back().m_nObject      = createObject();
    8734           0 :         m_aTransparentObjects.back().m_fAlpha       = (double)(100-nTransparentPercent) / 100.0;
    8735             :         // get XObject's content stream
    8736           0 :         m_aTransparentObjects.back().m_pContentStream = static_cast<SvMemoryStream*>(endRedirect());
    8737           0 :         m_aTransparentObjects.back().m_nExtGStateObject = createObject();
    8738             : 
    8739           0 :         OStringBuffer aObjName( 16 );
    8740           0 :         aObjName.append( "Tr" );
    8741           0 :         aObjName.append( m_aTransparentObjects.back().m_nObject );
    8742           0 :         OString aTrName( aObjName.makeStringAndClear() );
    8743           0 :         aObjName.append( "EGS" );
    8744           0 :         aObjName.append( m_aTransparentObjects.back().m_nExtGStateObject );
    8745           0 :         OString aExtName( aObjName.makeStringAndClear() );
    8746             : 
    8747           0 :         OStringBuffer aLine( 80 );
    8748             :         // insert XObject
    8749           0 :         aLine.append( "q /" );
    8750           0 :         aLine.append( aExtName );
    8751           0 :         aLine.append( " gs /" );
    8752           0 :         aLine.append( aTrName );
    8753           0 :         aLine.append( " Do Q\n" );
    8754           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    8755             : 
    8756           0 :         pushResource( ResXObject, aTrName, m_aTransparentObjects.back().m_nObject );
    8757           0 :         pushResource( ResExtGState, aExtName, m_aTransparentObjects.back().m_nExtGStateObject );
    8758             :     }
    8759           0 : }
    8760             : 
    8761           0 : void PDFWriterImpl::drawRectangle( const Rectangle& rRect )
    8762             : {
    8763           0 :     MARK( "drawRectangle" );
    8764             : 
    8765           0 :     updateGraphicsState();
    8766             : 
    8767           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8768           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8769           0 :         return;
    8770             : 
    8771           0 :     OStringBuffer aLine( 40 );
    8772           0 :     m_aPages.back().appendRect( rRect, aLine );
    8773             : 
    8774           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8775           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8776           0 :         aLine.append( " B*\n" );
    8777           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8778           0 :         aLine.append( " S\n" );
    8779             :     else
    8780           0 :         aLine.append( " f*\n" );
    8781             : 
    8782           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8783             : }
    8784             : 
    8785           0 : void PDFWriterImpl::drawRectangle( const Rectangle& rRect, sal_uInt32 nHorzRound, sal_uInt32 nVertRound )
    8786             : {
    8787           0 :     MARK( "drawRectangle with rounded edges" );
    8788             : 
    8789           0 :     if( !nHorzRound && !nVertRound )
    8790           0 :         drawRectangle( rRect );
    8791             : 
    8792           0 :     updateGraphicsState();
    8793             : 
    8794           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8795           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8796           0 :         return;
    8797             : 
    8798           0 :     if( nHorzRound > (sal_uInt32)rRect.GetWidth()/2 )
    8799           0 :         nHorzRound = rRect.GetWidth()/2;
    8800           0 :     if( nVertRound > (sal_uInt32)rRect.GetHeight()/2 )
    8801           0 :         nVertRound = rRect.GetHeight()/2;
    8802             : 
    8803           0 :     Point aPoints[16];
    8804           0 :     const double kappa = 0.5522847498;
    8805           0 :     const sal_uInt32 kx = (sal_uInt32)((kappa*(double)nHorzRound)+0.5);
    8806           0 :     const sal_uInt32 ky = (sal_uInt32)((kappa*(double)nVertRound)+0.5);
    8807             : 
    8808           0 :     aPoints[1]  = Point( rRect.TopLeft().X() + nHorzRound, rRect.TopLeft().Y() );
    8809           0 :     aPoints[0]  = Point( aPoints[1].X() - kx, aPoints[1].Y() );
    8810           0 :     aPoints[2]  = Point( rRect.TopRight().X()+1 - nHorzRound, aPoints[1].Y() );
    8811           0 :     aPoints[3]  = Point( aPoints[2].X()+kx, aPoints[2].Y() );
    8812             : 
    8813           0 :     aPoints[5]  = Point( rRect.TopRight().X()+1, rRect.TopRight().Y()+nVertRound );
    8814           0 :     aPoints[4]  = Point( aPoints[5].X(), aPoints[5].Y()-ky );
    8815           0 :     aPoints[6]  = Point( aPoints[5].X(), rRect.BottomRight().Y()+1 - nVertRound );
    8816           0 :     aPoints[7]  = Point( aPoints[6].X(), aPoints[6].Y()+ky );
    8817             : 
    8818           0 :     aPoints[9]  = Point( rRect.BottomRight().X()+1-nHorzRound, rRect.BottomRight().Y()+1 );
    8819           0 :     aPoints[8]  = Point( aPoints[9].X()+kx, aPoints[9].Y() );
    8820           0 :     aPoints[10] = Point( rRect.BottomLeft().X() + nHorzRound, aPoints[9].Y() );
    8821           0 :     aPoints[11] = Point( aPoints[10].X()-kx, aPoints[10].Y() );
    8822             : 
    8823           0 :     aPoints[13] = Point( rRect.BottomLeft().X(), rRect.BottomLeft().Y()+1-nVertRound );
    8824           0 :     aPoints[12] = Point( aPoints[13].X(), aPoints[13].Y()+ky );
    8825           0 :     aPoints[14] = Point( rRect.TopLeft().X(), rRect.TopLeft().Y()+nVertRound );
    8826           0 :     aPoints[15] = Point( aPoints[14].X(), aPoints[14].Y()-ky );
    8827             : 
    8828           0 :     OStringBuffer aLine( 80 );
    8829           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8830           0 :     aLine.append( " m " );
    8831           0 :     m_aPages.back().appendPoint( aPoints[2], aLine );
    8832           0 :     aLine.append( " l " );
    8833           0 :     m_aPages.back().appendPoint( aPoints[3], aLine );
    8834           0 :     aLine.append( ' ' );
    8835           0 :     m_aPages.back().appendPoint( aPoints[4], aLine );
    8836           0 :     aLine.append( ' ' );
    8837           0 :     m_aPages.back().appendPoint( aPoints[5], aLine );
    8838           0 :     aLine.append( " c\n" );
    8839           0 :     m_aPages.back().appendPoint( aPoints[6], aLine );
    8840           0 :     aLine.append( " l " );
    8841           0 :     m_aPages.back().appendPoint( aPoints[7], aLine );
    8842           0 :     aLine.append( ' ' );
    8843           0 :     m_aPages.back().appendPoint( aPoints[8], aLine );
    8844           0 :     aLine.append( ' ' );
    8845           0 :     m_aPages.back().appendPoint( aPoints[9], aLine );
    8846           0 :     aLine.append( " c\n" );
    8847           0 :     m_aPages.back().appendPoint( aPoints[10], aLine );
    8848           0 :     aLine.append( " l " );
    8849           0 :     m_aPages.back().appendPoint( aPoints[11], aLine );
    8850           0 :     aLine.append( ' ' );
    8851           0 :     m_aPages.back().appendPoint( aPoints[12], aLine );
    8852           0 :     aLine.append( ' ' );
    8853           0 :     m_aPages.back().appendPoint( aPoints[13], aLine );
    8854           0 :     aLine.append( " c\n" );
    8855           0 :     m_aPages.back().appendPoint( aPoints[14], aLine );
    8856           0 :     aLine.append( " l " );
    8857           0 :     m_aPages.back().appendPoint( aPoints[15], aLine );
    8858           0 :     aLine.append( ' ' );
    8859           0 :     m_aPages.back().appendPoint( aPoints[0], aLine );
    8860           0 :     aLine.append( ' ' );
    8861           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8862           0 :     aLine.append( " c " );
    8863             : 
    8864           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8865           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8866           0 :         aLine.append( "b*\n" );
    8867           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8868           0 :         aLine.append( "s\n" );
    8869             :     else
    8870           0 :         aLine.append( "f*\n" );
    8871             : 
    8872           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8873             : }
    8874             : 
    8875           0 : void PDFWriterImpl::drawEllipse( const Rectangle& rRect )
    8876             : {
    8877           0 :     MARK( "drawEllipse" );
    8878             : 
    8879           0 :     updateGraphicsState();
    8880             : 
    8881           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8882           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8883           0 :         return;
    8884             : 
    8885           0 :     Point aPoints[12];
    8886           0 :     const double kappa = 0.5522847498;
    8887           0 :     const sal_uInt32 kx = (sal_uInt32)((kappa*(double)rRect.GetWidth()/2.0)+0.5);
    8888           0 :     const sal_uInt32 ky = (sal_uInt32)((kappa*(double)rRect.GetHeight()/2.0)+0.5);
    8889             : 
    8890           0 :     aPoints[1]  = Point( rRect.TopLeft().X() + rRect.GetWidth()/2, rRect.TopLeft().Y() );
    8891           0 :     aPoints[0]  = Point( aPoints[1].X() - kx, aPoints[1].Y() );
    8892           0 :     aPoints[2]  = Point( aPoints[1].X() + kx, aPoints[1].Y() );
    8893             : 
    8894           0 :     aPoints[4]  = Point( rRect.TopRight().X()+1, rRect.TopRight().Y() + rRect.GetHeight()/2 );
    8895           0 :     aPoints[3]  = Point( aPoints[4].X(), aPoints[4].Y() - ky );
    8896           0 :     aPoints[5]  = Point( aPoints[4].X(), aPoints[4].Y() + ky );
    8897             : 
    8898           0 :     aPoints[7]  = Point( rRect.BottomLeft().X() + rRect.GetWidth()/2, rRect.BottomLeft().Y()+1 );
    8899           0 :     aPoints[6]  = Point( aPoints[7].X() + kx, aPoints[7].Y() );
    8900           0 :     aPoints[8]  = Point( aPoints[7].X() - kx, aPoints[7].Y() );
    8901             : 
    8902           0 :     aPoints[10] = Point( rRect.TopLeft().X(), rRect.TopLeft().Y() + rRect.GetHeight()/2 );
    8903           0 :     aPoints[9]  = Point( aPoints[10].X(), aPoints[10].Y() + ky );
    8904           0 :     aPoints[11] = Point( aPoints[10].X(), aPoints[10].Y() - ky );
    8905             : 
    8906           0 :     OStringBuffer aLine( 80 );
    8907           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8908           0 :     aLine.append( " m " );
    8909           0 :     m_aPages.back().appendPoint( aPoints[2], aLine );
    8910           0 :     aLine.append( ' ' );
    8911           0 :     m_aPages.back().appendPoint( aPoints[3], aLine );
    8912           0 :     aLine.append( ' ' );
    8913           0 :     m_aPages.back().appendPoint( aPoints[4], aLine );
    8914           0 :     aLine.append( " c\n" );
    8915           0 :     m_aPages.back().appendPoint( aPoints[5], aLine );
    8916           0 :     aLine.append( ' ' );
    8917           0 :     m_aPages.back().appendPoint( aPoints[6], aLine );
    8918           0 :     aLine.append( ' ' );
    8919           0 :     m_aPages.back().appendPoint( aPoints[7], aLine );
    8920           0 :     aLine.append( " c\n" );
    8921           0 :     m_aPages.back().appendPoint( aPoints[8], aLine );
    8922           0 :     aLine.append( ' ' );
    8923           0 :     m_aPages.back().appendPoint( aPoints[9], aLine );
    8924           0 :     aLine.append( ' ' );
    8925           0 :     m_aPages.back().appendPoint( aPoints[10], aLine );
    8926           0 :     aLine.append( " c\n" );
    8927           0 :     m_aPages.back().appendPoint( aPoints[11], aLine );
    8928           0 :     aLine.append( ' ' );
    8929           0 :     m_aPages.back().appendPoint( aPoints[0], aLine );
    8930           0 :     aLine.append( ' ' );
    8931           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8932           0 :     aLine.append( " c " );
    8933             : 
    8934           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8935           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8936           0 :         aLine.append( "b*\n" );
    8937           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8938           0 :         aLine.append( "s\n" );
    8939             :     else
    8940           0 :         aLine.append( "f*\n" );
    8941             : 
    8942           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8943             : }
    8944             : 
    8945           0 : static double calcAngle( const Rectangle& rRect, const Point& rPoint )
    8946             : {
    8947           0 :     Point aOrigin((rRect.Left()+rRect.Right()+1)/2,
    8948           0 :                   (rRect.Top()+rRect.Bottom()+1)/2);
    8949           0 :     Point aPoint = rPoint - aOrigin;
    8950             : 
    8951           0 :     double fX = (double)aPoint.X();
    8952           0 :     double fY = (double)-aPoint.Y();
    8953             : 
    8954           0 :     if( rRect.GetWidth() > rRect.GetHeight() )
    8955           0 :         fY = fY*((double)rRect.GetWidth()/(double)rRect.GetHeight());
    8956           0 :     else if( rRect.GetHeight() > rRect.GetWidth() )
    8957           0 :         fX = fX*((double)rRect.GetHeight()/(double)rRect.GetWidth());
    8958           0 :     return atan2( fY, fX );
    8959             : }
    8960             : 
    8961           0 : void PDFWriterImpl::drawArc( const Rectangle& rRect, const Point& rStart, const Point& rStop, bool bWithPie, bool bWithChord )
    8962             : {
    8963           0 :     MARK( "drawArc" );
    8964             : 
    8965           0 :     updateGraphicsState();
    8966             : 
    8967           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8968           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8969           0 :         return;
    8970             : 
    8971             :     // calculate start and stop angles
    8972           0 :     const double fStartAngle = calcAngle( rRect, rStart );
    8973           0 :     double fStopAngle  = calcAngle( rRect, rStop );
    8974           0 :     while( fStopAngle < fStartAngle )
    8975           0 :         fStopAngle += 2.0*M_PI;
    8976           0 :     const int nFragments = (int)((fStopAngle-fStartAngle)/(M_PI/2.0))+1;
    8977           0 :     const double fFragmentDelta = (fStopAngle-fStartAngle)/(double)nFragments;
    8978           0 :     const double kappa = fabs( 4.0 * (1.0-cos(fFragmentDelta/2.0))/sin(fFragmentDelta/2.0) / 3.0);
    8979           0 :     const double halfWidth = (double)rRect.GetWidth()/2.0;
    8980           0 :     const double halfHeight = (double)rRect.GetHeight()/2.0;
    8981             : 
    8982           0 :     const Point aCenter( (rRect.Left()+rRect.Right()+1)/2,
    8983           0 :                          (rRect.Top()+rRect.Bottom()+1)/2 );
    8984             : 
    8985           0 :     OStringBuffer aLine( 30*nFragments );
    8986           0 :     Point aPoint( (int)(halfWidth * cos(fStartAngle) ),
    8987           0 :                   -(int)(halfHeight * sin(fStartAngle) ) );
    8988           0 :     aPoint += aCenter;
    8989           0 :     m_aPages.back().appendPoint( aPoint, aLine );
    8990           0 :     aLine.append( " m " );
    8991           0 :     if( !basegfx::fTools::equal(fStartAngle, fStopAngle) )
    8992             :     {
    8993           0 :         for( int i = 0; i < nFragments; i++ )
    8994             :         {
    8995           0 :             const double fStartFragment = fStartAngle + (double)i*fFragmentDelta;
    8996           0 :             const double fStopFragment = fStartFragment + fFragmentDelta;
    8997           0 :             aPoint = Point( (int)(halfWidth * (cos(fStartFragment) - kappa*sin(fStartFragment) ) ),
    8998           0 :                             -(int)(halfHeight * (sin(fStartFragment) + kappa*cos(fStartFragment) ) ) );
    8999           0 :             aPoint += aCenter;
    9000           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9001           0 :             aLine.append( ' ' );
    9002             : 
    9003           0 :             aPoint = Point( (int)(halfWidth * (cos(fStopFragment) + kappa*sin(fStopFragment) ) ),
    9004           0 :                             -(int)(halfHeight * (sin(fStopFragment) - kappa*cos(fStopFragment) ) ) );
    9005           0 :             aPoint += aCenter;
    9006           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9007           0 :             aLine.append( ' ' );
    9008             : 
    9009           0 :             aPoint = Point( (int)(halfWidth * cos(fStopFragment) ),
    9010           0 :                             -(int)(halfHeight * sin(fStopFragment) ) );
    9011           0 :             aPoint += aCenter;
    9012           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9013           0 :             aLine.append( " c\n" );
    9014             :         }
    9015             :     }
    9016           0 :     if( bWithChord || bWithPie )
    9017             :     {
    9018           0 :         if( bWithPie )
    9019             :         {
    9020           0 :             m_aPages.back().appendPoint( aCenter, aLine );
    9021           0 :             aLine.append( " l " );
    9022             :         }
    9023           0 :         aLine.append( "h " );
    9024             :     }
    9025           0 :     if( ! bWithChord && ! bWithPie )
    9026           0 :         aLine.append( "S\n" );
    9027           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    9028           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    9029           0 :         aLine.append( "B*\n" );
    9030           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    9031           0 :         aLine.append( "S\n" );
    9032             :     else
    9033           0 :         aLine.append( "f*\n" );
    9034             : 
    9035           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9036             : }
    9037             : 
    9038           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly )
    9039             : {
    9040           0 :     MARK( "drawPolyLine" );
    9041             : 
    9042           0 :     sal_uInt16 nPoints = rPoly.GetSize();
    9043           0 :     if( nPoints < 2 )
    9044           0 :         return;
    9045             : 
    9046           0 :     updateGraphicsState();
    9047             : 
    9048           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9049           0 :         return;
    9050             : 
    9051           0 :     OStringBuffer aLine( 20 * nPoints );
    9052           0 :     m_aPages.back().appendPolygon( rPoly, aLine, rPoly[0] == rPoly[nPoints-1] );
    9053           0 :     aLine.append( "S\n" );
    9054             : 
    9055           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9056             : }
    9057             : 
    9058           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly, const LineInfo& rInfo )
    9059             : {
    9060           0 :     MARK( "drawPolyLine with LineInfo" );
    9061             : 
    9062           0 :     updateGraphicsState();
    9063             : 
    9064           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9065           0 :         return;
    9066             : 
    9067           0 :     OStringBuffer aLine;
    9068           0 :     aLine.append( "q " );
    9069           0 :     if( m_aPages.back().appendLineInfo( rInfo, aLine ) )
    9070             :     {
    9071           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9072           0 :         drawPolyLine( rPoly );
    9073           0 :         writeBuffer( "Q\n", 2 );
    9074             :     }
    9075             :     else
    9076             :     {
    9077           0 :         PDFWriter::ExtLineInfo aInfo;
    9078           0 :         convertLineInfoToExtLineInfo( rInfo, aInfo );
    9079           0 :         drawPolyLine( rPoly, aInfo );
    9080           0 :     }
    9081             : }
    9082             : 
    9083           0 : void PDFWriterImpl::convertLineInfoToExtLineInfo( const LineInfo& rIn, PDFWriter::ExtLineInfo& rOut )
    9084             : {
    9085             :     DBG_ASSERT( rIn.GetStyle() == LINE_DASH, "invalid conversion" );
    9086           0 :     rOut.m_fLineWidth           = rIn.GetWidth();
    9087           0 :     rOut.m_fTransparency        = 0.0;
    9088           0 :     rOut.m_eCap                 = PDFWriter::capButt;
    9089           0 :     rOut.m_eJoin                = PDFWriter::joinMiter;
    9090           0 :     rOut.m_fMiterLimit          = 10;
    9091           0 :     rOut.m_aDashArray.clear();
    9092             : 
    9093             :     // add DashDot to DashArray
    9094           0 :     const int nDashes   = rIn.GetDashCount();
    9095           0 :     const int nDashLen  = rIn.GetDashLen();
    9096           0 :     const int nDistance = rIn.GetDistance();
    9097             : 
    9098           0 :     for( int n  = 0; n < nDashes; n++ )
    9099             :     {
    9100           0 :         rOut.m_aDashArray.push_back( nDashLen );
    9101           0 :         rOut.m_aDashArray.push_back( nDistance );
    9102             :     }
    9103           0 :     const int nDots   = rIn.GetDotCount();
    9104           0 :     const int nDotLen = rIn.GetDotLen();
    9105             : 
    9106           0 :     for( int n  = 0; n < nDots; n++ )
    9107             :     {
    9108           0 :         rOut.m_aDashArray.push_back( nDotLen );
    9109           0 :         rOut.m_aDashArray.push_back( nDistance );
    9110             :     }
    9111             : 
    9112             :     // add LineJoin
    9113           0 :     switch(rIn.GetLineJoin())
    9114             :     {
    9115             :         case basegfx::B2DLINEJOIN_BEVEL :
    9116             :         {
    9117           0 :             rOut.m_eJoin = PDFWriter::joinBevel;
    9118           0 :             break;
    9119             :         }
    9120             :         default : // basegfx::B2DLINEJOIN_NONE :
    9121             :         // Pdf has no 'none' lineJoin, default is miter
    9122             :         case basegfx::B2DLINEJOIN_MIDDLE :
    9123             :         case basegfx::B2DLINEJOIN_MITER :
    9124             :         {
    9125           0 :             rOut.m_eJoin = PDFWriter::joinMiter;
    9126           0 :             break;
    9127             :         }
    9128             :         case basegfx::B2DLINEJOIN_ROUND :
    9129             :         {
    9130           0 :             rOut.m_eJoin = PDFWriter::joinRound;
    9131           0 :             break;
    9132             :         }
    9133             :     }
    9134             : 
    9135             :     // add LineCap
    9136           0 :     switch(rIn.GetLineCap())
    9137             :     {
    9138             :         default: /* com::sun::star::drawing::LineCap_BUTT */
    9139             :         {
    9140           0 :             rOut.m_eCap = PDFWriter::capButt;
    9141           0 :             break;
    9142             :         }
    9143             :         case com::sun::star::drawing::LineCap_ROUND:
    9144             :         {
    9145           0 :             rOut.m_eCap = PDFWriter::capRound;
    9146           0 :             break;
    9147             :         }
    9148             :         case com::sun::star::drawing::LineCap_SQUARE:
    9149             :         {
    9150           0 :             rOut.m_eCap = PDFWriter::capSquare;
    9151           0 :             break;
    9152             :         }
    9153             :     }
    9154           0 : }
    9155             : 
    9156           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly, const PDFWriter::ExtLineInfo& rInfo )
    9157             : {
    9158           0 :     MARK( "drawPolyLine with ExtLineInfo" );
    9159             : 
    9160           0 :     updateGraphicsState();
    9161             : 
    9162           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9163           0 :         return;
    9164             : 
    9165           0 :     if( rInfo.m_fTransparency >= 1.0 )
    9166           0 :         return;
    9167             : 
    9168           0 :     if( rInfo.m_fTransparency != 0.0 )
    9169           0 :         beginTransparencyGroup();
    9170             : 
    9171           0 :     OStringBuffer aLine;
    9172           0 :     aLine.append( "q " );
    9173           0 :     m_aPages.back().appendMappedLength( rInfo.m_fLineWidth, aLine );
    9174           0 :     aLine.append( " w" );
    9175           0 :     if( rInfo.m_aDashArray.size() < 10 ) // implmentation limit of acrobat reader
    9176             :     {
    9177           0 :         switch( rInfo.m_eCap )
    9178             :         {
    9179             :             default:
    9180           0 :             case PDFWriter::capButt:   aLine.append( " 0 J" );break;
    9181           0 :             case PDFWriter::capRound:  aLine.append( " 1 J" );break;
    9182           0 :             case PDFWriter::capSquare: aLine.append( " 2 J" );break;
    9183             :         }
    9184           0 :         switch( rInfo.m_eJoin )
    9185             :         {
    9186             :             default:
    9187             :             case PDFWriter::joinMiter:
    9188             :             {
    9189           0 :                 double fLimit = rInfo.m_fMiterLimit;
    9190           0 :                 if( rInfo.m_fLineWidth < rInfo.m_fMiterLimit )
    9191           0 :                     fLimit = fLimit / rInfo.m_fLineWidth;
    9192           0 :                 if( fLimit < 1.0 )
    9193           0 :                     fLimit = 1.0;
    9194           0 :                 aLine.append( " 0 j " );
    9195           0 :                 appendDouble( fLimit, aLine );
    9196           0 :                 aLine.append( " M" );
    9197             :             }
    9198           0 :             break;
    9199           0 :             case PDFWriter::joinRound:  aLine.append( " 1 j" );break;
    9200           0 :             case PDFWriter::joinBevel:  aLine.append( " 2 j" );break;
    9201             :         }
    9202           0 :         if( rInfo.m_aDashArray.size() > 0 )
    9203             :         {
    9204           0 :             aLine.append( " [ " );
    9205           0 :             for( std::vector<double>::const_iterator it = rInfo.m_aDashArray.begin();
    9206           0 :                  it != rInfo.m_aDashArray.end(); ++it )
    9207             :             {
    9208           0 :                 m_aPages.back().appendMappedLength( *it, aLine );
    9209           0 :                 aLine.append( ' ' );
    9210             :             }
    9211           0 :             aLine.append( "] 0 d" );
    9212             :         }
    9213           0 :         aLine.append( "\n" );
    9214           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9215           0 :         drawPolyLine( rPoly );
    9216             :     }
    9217             :     else
    9218             :     {
    9219           0 :         basegfx::B2DPolygon aPoly(rPoly.getB2DPolygon());
    9220           0 :         basegfx::B2DPolyPolygon aPolyPoly;
    9221             : 
    9222           0 :         basegfx::tools::applyLineDashing(aPoly, rInfo.m_aDashArray, &aPolyPoly);
    9223             : 
    9224             :         // Old applyLineDashing subdivided the polygon. New one will create bezier curve segments.
    9225             :         // To mimic old behaviour, apply subdivide here. If beziers shall be written (better quality)
    9226             :         // this line needs to be removed and the loop below adapted accordingly
    9227           0 :         aPolyPoly = basegfx::tools::adaptiveSubdivideByAngle(aPolyPoly);
    9228             : 
    9229           0 :         const sal_uInt32 nPolygonCount(aPolyPoly.count());
    9230             : 
    9231           0 :         for( sal_uInt32 nPoly = 0; nPoly < nPolygonCount; nPoly++ )
    9232             :         {
    9233           0 :             aLine.append( (nPoly != 0 && (nPoly & 7) == 0) ? "\n" : " " );
    9234           0 :             aPoly = aPolyPoly.getB2DPolygon( nPoly );
    9235           0 :             const sal_uInt32 nPointCount(aPoly.count());
    9236             : 
    9237           0 :             if(nPointCount)
    9238             :             {
    9239           0 :                 const sal_uInt32 nEdgeCount(aPoly.isClosed() ? nPointCount : nPointCount - 1);
    9240           0 :                 basegfx::B2DPoint aCurrent(aPoly.getB2DPoint(0));
    9241             : 
    9242           0 :                 for(sal_uInt32 a(0); a < nEdgeCount; a++)
    9243             :                 {
    9244           0 :                     if( a > 0 )
    9245           0 :                         aLine.append( " " );
    9246           0 :                     const sal_uInt32 nNextIndex((a + 1) % nPointCount);
    9247           0 :                     const basegfx::B2DPoint aNext(aPoly.getB2DPoint(nNextIndex));
    9248             : 
    9249           0 :                     m_aPages.back().appendPoint( Point( FRound(aCurrent.getX()),
    9250             :                                                         FRound(aCurrent.getY()) ),
    9251           0 :                                                  aLine );
    9252           0 :                     aLine.append( " m " );
    9253           0 :                     m_aPages.back().appendPoint( Point( FRound(aNext.getX()),
    9254             :                                                         FRound(aNext.getY()) ),
    9255           0 :                                                  aLine );
    9256           0 :                     aLine.append( " l" );
    9257             : 
    9258             :                     // prepare next edge
    9259           0 :                     aCurrent = aNext;
    9260           0 :                 }
    9261             :             }
    9262             :         }
    9263           0 :         aLine.append( " S " );
    9264           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9265             :     }
    9266           0 :     writeBuffer( "Q\n", 2 );
    9267             : 
    9268           0 :     if( rInfo.m_fTransparency != 0.0 )
    9269             :     {
    9270             :         // FIXME: actually this may be incorrect with bezier polygons
    9271           0 :         Rectangle aBoundRect( rPoly.GetBoundRect() );
    9272             :         // avoid clipping with thick lines
    9273           0 :         if( rInfo.m_fLineWidth > 0.0 )
    9274             :         {
    9275           0 :             sal_Int32 nLW = sal_Int32(rInfo.m_fLineWidth);
    9276           0 :             aBoundRect.Top()    -= nLW;
    9277           0 :             aBoundRect.Left()   -= nLW;
    9278           0 :             aBoundRect.Right()  += nLW;
    9279           0 :             aBoundRect.Bottom() += nLW;
    9280             :         }
    9281           0 :         endTransparencyGroup( aBoundRect, (sal_uInt16)(100.0*rInfo.m_fTransparency) );
    9282           0 :     }
    9283             : }
    9284             : 
    9285           0 : void PDFWriterImpl::drawPixel( const Point& rPoint, const Color& rColor )
    9286             : {
    9287           0 :     MARK( "drawPixel" );
    9288             : 
    9289           0 :     Color aColor = ( rColor == Color( COL_TRANSPARENT ) ? m_aGraphicsStack.front().m_aLineColor : rColor );
    9290             : 
    9291           0 :     if( aColor == Color( COL_TRANSPARENT ) )
    9292           0 :         return;
    9293             : 
    9294             :     // pixels are drawn in line color, so have to set
    9295             :     // the nonstroking color to line color
    9296           0 :     Color aOldFillColor = m_aGraphicsStack.front().m_aFillColor;
    9297           0 :     setFillColor( aColor );
    9298             : 
    9299           0 :     updateGraphicsState();
    9300             : 
    9301           0 :     OStringBuffer aLine( 20 );
    9302           0 :     m_aPages.back().appendPoint( rPoint, aLine );
    9303           0 :     aLine.append( ' ' );
    9304           0 :     appendDouble( 1.0/double(getReferenceDevice()->ImplGetDPIX()), aLine );
    9305           0 :     aLine.append( ' ' );
    9306           0 :     appendDouble( 1.0/double(getReferenceDevice()->ImplGetDPIY()), aLine );
    9307           0 :     aLine.append( " re f\n" );
    9308           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9309             : 
    9310           0 :     setFillColor( aOldFillColor );
    9311             : }
    9312             : 
    9313             : class AccessReleaser
    9314             : {
    9315             :     BitmapReadAccess* m_pAccess;
    9316             : public:
    9317           0 :     AccessReleaser( BitmapReadAccess* pAccess ) : m_pAccess( pAccess ){}
    9318           0 :     ~AccessReleaser() { delete m_pAccess; }
    9319             : };
    9320             : 
    9321           0 : bool PDFWriterImpl::writeTransparentObject( TransparencyEmit& rObject )
    9322             : {
    9323           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9324             : 
    9325           0 :     bool bFlateFilter = compressStream( rObject.m_pContentStream );
    9326           0 :     rObject.m_pContentStream->Seek( STREAM_SEEK_TO_END );
    9327           0 :     sal_uLong nSize = rObject.m_pContentStream->Tell();
    9328           0 :     rObject.m_pContentStream->Seek( STREAM_SEEK_TO_BEGIN );
    9329             :     #if OSL_DEBUG_LEVEL > 1
    9330             :     emitComment( "PDFWriterImpl::writeTransparentObject" );
    9331             :     #endif
    9332           0 :     OStringBuffer aLine( 512 );
    9333           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9334           0 :     aLine.append( rObject.m_nObject );
    9335             :     aLine.append( " 0 obj\n"
    9336             :                   "<</Type/XObject\n"
    9337             :                   "/Subtype/Form\n"
    9338           0 :                   "/BBox[ " );
    9339           0 :     appendFixedInt( rObject.m_aBoundRect.Left(), aLine );
    9340           0 :     aLine.append( ' ' );
    9341           0 :     appendFixedInt( rObject.m_aBoundRect.Top(), aLine );
    9342           0 :     aLine.append( ' ' );
    9343           0 :     appendFixedInt( rObject.m_aBoundRect.Right(), aLine );
    9344           0 :     aLine.append( ' ' );
    9345           0 :     appendFixedInt( rObject.m_aBoundRect.Bottom()+1, aLine );
    9346           0 :     aLine.append( " ]\n" );
    9347           0 :     if( ! rObject.m_pSoftMaskStream )
    9348             :     {
    9349           0 :         if( ! m_bIsPDF_A1 )
    9350             :         {
    9351           0 :             aLine.append( "/Group<</S/Transparency/CS/DeviceRGB/K true>>\n" );
    9352             :         }
    9353             :     }
    9354             :     /* #i42884# the PDF reference recommends that each Form XObject
    9355             :     *  should have a resource dict; alas if that is the same object
    9356             :     *  as the one of the page it triggers an endless recursion in
    9357             :     *  acroread 5 (6 and up have that fixed). Since we have only one
    9358             :     *  resource dict anyway, let's use the one from the page by NOT
    9359             :     *  emitting a Resources entry.
    9360             :     */
    9361             : 
    9362           0 :     aLine.append( "/Length " );
    9363           0 :     aLine.append( (sal_Int32)(nSize) );
    9364           0 :     aLine.append( "\n" );
    9365           0 :     if( bFlateFilter )
    9366           0 :         aLine.append( "/Filter/FlateDecode\n" );
    9367             :     aLine.append( ">>\n"
    9368           0 :                   "stream\n" );
    9369           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9370           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9371           0 :     CHECK_RETURN( writeBuffer( rObject.m_pContentStream->GetData(), nSize ) );
    9372           0 :     disableStreamEncryption();
    9373           0 :     aLine.setLength( 0 );
    9374             :     aLine.append( "\n"
    9375             :                   "endstream\n"
    9376           0 :                   "endobj\n\n" );
    9377           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9378             : 
    9379             :     // write ExtGState dict for this XObject
    9380           0 :     aLine.setLength( 0 );
    9381           0 :     aLine.append( rObject.m_nExtGStateObject );
    9382             :     aLine.append( " 0 obj\n"
    9383           0 :                   "<<" );
    9384           0 :     if( ! rObject.m_pSoftMaskStream )
    9385             :     {
    9386             : //i59651
    9387           0 :         if( m_bIsPDF_A1 )
    9388             :         {
    9389           0 :             aLine.append( "/CA 1.0/ca 1.0" );
    9390           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9391             :         }
    9392             :         else
    9393             :         {
    9394           0 :             aLine.append(  "/CA " );
    9395           0 :             appendDouble( rObject.m_fAlpha, aLine );
    9396             :             aLine.append( "\n"
    9397           0 :                           "   /ca " );
    9398           0 :             appendDouble( rObject.m_fAlpha, aLine );
    9399             :         }
    9400           0 :         aLine.append( "\n" );
    9401             :     }
    9402             :     else
    9403             :     {
    9404           0 :         if( m_bIsPDF_A1 )
    9405             :         {
    9406           0 :             aLine.append( "/SMask/None" );
    9407           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9408             :         }
    9409             :         else
    9410             :         {
    9411           0 :             rObject.m_pSoftMaskStream->Seek( STREAM_SEEK_TO_END );
    9412           0 :             sal_Int32 nMaskSize = (sal_Int32)rObject.m_pSoftMaskStream->Tell();
    9413           0 :             rObject.m_pSoftMaskStream->Seek( STREAM_SEEK_TO_BEGIN );
    9414           0 :             sal_Int32 nMaskObject = createObject();
    9415           0 :             aLine.append( "/SMask<</Type/Mask/S/Luminosity/G " );
    9416           0 :             aLine.append( nMaskObject );
    9417           0 :             aLine.append( " 0 R>>\n" );
    9418             : 
    9419           0 :             OStringBuffer aMask;
    9420           0 :             aMask.append( nMaskObject );
    9421             :             aMask.append( " 0 obj\n"
    9422             :                           "<</Type/XObject\n"
    9423             :                           "/Subtype/Form\n"
    9424           0 :                           "/BBox[" );
    9425           0 :             appendFixedInt( rObject.m_aBoundRect.Left(), aMask );
    9426           0 :             aMask.append( ' ' );
    9427           0 :             appendFixedInt( rObject.m_aBoundRect.Top(), aMask );
    9428           0 :             aMask.append( ' ' );
    9429           0 :             appendFixedInt( rObject.m_aBoundRect.Right(), aMask );
    9430           0 :             aMask.append( ' ' );
    9431           0 :             appendFixedInt( rObject.m_aBoundRect.Bottom()+1, aMask );
    9432           0 :             aMask.append( "]\n" );
    9433             : 
    9434             :             /* #i42884# see above */
    9435           0 :             aMask.append( "/Group<</S/Transparency/CS/DeviceRGB>>\n" );
    9436           0 :             aMask.append( "/Length " );
    9437           0 :             aMask.append( nMaskSize );
    9438             :             aMask.append( ">>\n"
    9439           0 :                           "stream\n" );
    9440           0 :             CHECK_RETURN( updateObject( nMaskObject ) );
    9441           0 :             checkAndEnableStreamEncryption(  nMaskObject );
    9442           0 :             CHECK_RETURN( writeBuffer( aMask.getStr(), aMask.getLength() ) );
    9443           0 :             CHECK_RETURN( writeBuffer( rObject.m_pSoftMaskStream->GetData(), nMaskSize ) );
    9444           0 :             disableStreamEncryption();
    9445           0 :             aMask.setLength( 0 );
    9446             :             aMask.append( "\nendstream\n"
    9447           0 :                           "endobj\n\n" );
    9448           0 :             CHECK_RETURN( writeBuffer( aMask.getStr(), aMask.getLength() ) );
    9449             :         }
    9450             :     }
    9451             :     aLine.append( ">>\n"
    9452           0 :                   "endobj\n\n" );
    9453           0 :     CHECK_RETURN( updateObject( rObject.m_nExtGStateObject ) );
    9454           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9455             : 
    9456           0 :     return true;
    9457             : }
    9458             : 
    9459           0 : bool PDFWriterImpl::writeGradientFunction( GradientEmit& rObject )
    9460             : {
    9461             :     // LO internal gradient -> PDF shading type:
    9462             :     //  * GradientStyle_LINEAR: axial shading, using sampled-function with 2 samples
    9463             :     //                          [t=0:colorStart, t=1:colorEnd]
    9464             :     //  * GradientStyle_AXIAL: axial shading, using sampled-function with 3 samples
    9465             :     //                          [t=0:colorEnd, t=0.5:colorStart, t=1:colorEnd]
    9466             :     //  * other styles: function shading with aSize.Width() * aSize.Height() samples
    9467           0 :     sal_Int32 nFunctionObject = createObject();
    9468           0 :     CHECK_RETURN( updateObject( nFunctionObject ) );
    9469             : 
    9470           0 :     VirtualDevice aDev;
    9471           0 :     aDev.SetOutputSizePixel( rObject.m_aSize );
    9472           0 :     aDev.SetMapMode( MapMode( MAP_PIXEL ) );
    9473           0 :     if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
    9474           0 :         aDev.SetDrawMode( aDev.GetDrawMode() |
    9475             :                           ( DRAWMODE_GRAYLINE | DRAWMODE_GRAYFILL | DRAWMODE_GRAYTEXT |
    9476           0 :                             DRAWMODE_GRAYBITMAP | DRAWMODE_GRAYGRADIENT ) );
    9477           0 :     aDev.DrawGradient( Rectangle( Point( 0, 0 ), rObject.m_aSize ), rObject.m_aGradient );
    9478             : 
    9479           0 :     Bitmap aSample = aDev.GetBitmap( Point( 0, 0 ), rObject.m_aSize );
    9480           0 :     BitmapReadAccess* pAccess = aSample.AcquireReadAccess();
    9481           0 :     AccessReleaser aReleaser( pAccess );
    9482             : 
    9483           0 :     Size aSize = aSample.GetSizePixel();
    9484             : 
    9485           0 :     sal_Int32 nStreamLengthObject = createObject();
    9486             :     #if OSL_DEBUG_LEVEL > 1
    9487             :     emitComment( "PDFWriterImpl::writeGradientFunction" );
    9488             :     #endif
    9489           0 :     OStringBuffer aLine( 120 );
    9490           0 :     aLine.append( nFunctionObject );
    9491             :     aLine.append( " 0 obj\n"
    9492           0 :                   "<</FunctionType 0\n");
    9493           0 :     switch (rObject.m_aGradient.GetStyle())
    9494             :     {
    9495             :         case GradientStyle_LINEAR:
    9496             :         case GradientStyle_AXIAL:
    9497           0 :             aLine.append("/Domain[ 0 1]\n");
    9498           0 :             break;
    9499             :         default:
    9500           0 :             aLine.append("/Domain[ 0 1 0 1]\n");
    9501             :     }
    9502           0 :     aLine.append("/Size[ " );
    9503           0 :     switch (rObject.m_aGradient.GetStyle())
    9504             :     {
    9505             :         case GradientStyle_LINEAR:
    9506           0 :             aLine.append('2');
    9507           0 :             break;
    9508             :         case GradientStyle_AXIAL:
    9509           0 :             aLine.append('3');
    9510           0 :             break;
    9511             :         default:
    9512           0 :             aLine.append( (sal_Int32)aSize.Width() );
    9513           0 :             aLine.append( ' ' );
    9514           0 :             aLine.append( (sal_Int32)aSize.Height() );
    9515             :     }
    9516             :     aLine.append( " ]\n"
    9517             :                   "/BitsPerSample 8\n"
    9518             :                   "/Range[ 0 1 0 1 0 1 ]\n"
    9519             :                   "/Order 3\n"
    9520           0 :                   "/Length " );
    9521           0 :     aLine.append( nStreamLengthObject );
    9522             :     aLine.append( " 0 R\n"
    9523             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9524             :                   "/Filter/FlateDecode"
    9525             : #endif
    9526             :                   ">>\n"
    9527           0 :                   "stream\n" );
    9528           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9529             : 
    9530           0 :     sal_uInt64 nStartStreamPos = 0;
    9531           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nStartStreamPos )) );
    9532             : 
    9533           0 :     checkAndEnableStreamEncryption( nFunctionObject );
    9534           0 :     beginCompression();
    9535             :     sal_uInt8 aCol[3];
    9536           0 :     switch (rObject.m_aGradient.GetStyle())
    9537             :     {
    9538             :         case GradientStyle_AXIAL:
    9539           0 :             aCol[0] = rObject.m_aGradient.GetEndColor().GetRed();
    9540           0 :             aCol[1] = rObject.m_aGradient.GetEndColor().GetGreen();
    9541           0 :             aCol[2] = rObject.m_aGradient.GetEndColor().GetBlue();
    9542           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9543             :         case GradientStyle_LINEAR:
    9544             :         {
    9545           0 :             aCol[0] = rObject.m_aGradient.GetStartColor().GetRed();
    9546           0 :             aCol[1] = rObject.m_aGradient.GetStartColor().GetGreen();
    9547           0 :             aCol[2] = rObject.m_aGradient.GetStartColor().GetBlue();
    9548           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9549             : 
    9550           0 :             aCol[0] = rObject.m_aGradient.GetEndColor().GetRed();
    9551           0 :             aCol[1] = rObject.m_aGradient.GetEndColor().GetGreen();
    9552           0 :             aCol[2] = rObject.m_aGradient.GetEndColor().GetBlue();
    9553           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9554           0 :             break;
    9555             :         }
    9556             :         default:
    9557           0 :             for( int y = aSize.Height()-1; y >= 0; y-- )
    9558             :             {
    9559           0 :                 for( int x = 0; x < aSize.Width(); x++ )
    9560             :                 {
    9561           0 :                     BitmapColor aColor = pAccess->GetColor( y, x );
    9562           0 :                     aCol[0] = aColor.GetRed();
    9563           0 :                     aCol[1] = aColor.GetGreen();
    9564           0 :                     aCol[2] = aColor.GetBlue();
    9565           0 :                     CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9566           0 :                 }
    9567             :             }
    9568             :     }
    9569           0 :     endCompression();
    9570           0 :     disableStreamEncryption();
    9571             : 
    9572           0 :     sal_uInt64 nEndStreamPos = 0;
    9573           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nEndStreamPos )) );
    9574             : 
    9575           0 :     aLine.setLength( 0 );
    9576           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9577           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9578             : 
    9579             :     // write stream length
    9580           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
    9581           0 :     aLine.setLength( 0 );
    9582           0 :     aLine.append( nStreamLengthObject );
    9583           0 :     aLine.append( " 0 obj\n" );
    9584           0 :     aLine.append( (sal_Int64)(nEndStreamPos-nStartStreamPos) );
    9585           0 :     aLine.append( "\nendobj\n\n" );
    9586           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9587             : 
    9588           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9589           0 :     aLine.setLength( 0 );
    9590           0 :     aLine.append( rObject.m_nObject );
    9591           0 :     aLine.append( " 0 obj\n");
    9592           0 :     switch (rObject.m_aGradient.GetStyle())
    9593             :     {
    9594             :         case GradientStyle_LINEAR:
    9595             :         case GradientStyle_AXIAL:
    9596           0 :             aLine.append("<</ShadingType 2\n");
    9597           0 :             break;
    9598             :         default:
    9599           0 :             aLine.append("<</ShadingType 1\n");
    9600             :     }
    9601             :     aLine.append("/ColorSpace/DeviceRGB\n"
    9602           0 :                   "/AntiAlias true\n");
    9603             : 
    9604             :     // Determination of shading axis
    9605             :     // See: OutputDevice::ImplDrawLinearGradient for reference
    9606           0 :     Rectangle aRect;
    9607           0 :     aRect.Left() = aRect.Top() = 0;
    9608           0 :     aRect.Right() = aSize.Width();
    9609           0 :     aRect.Bottom() = aSize.Height();
    9610             : 
    9611           0 :     Rectangle aBoundRect;
    9612           0 :     Point     aCenter;
    9613           0 :     sal_uInt16    nAngle = rObject.m_aGradient.GetAngle() % 3600;
    9614           0 :     rObject.m_aGradient.GetBoundRect( aRect, aBoundRect, aCenter );
    9615             : 
    9616           0 :     const bool bLinear = (rObject.m_aGradient.GetStyle() == GradientStyle_LINEAR);
    9617           0 :     double fBorder = aBoundRect.GetHeight() * rObject.m_aGradient.GetBorder() / 100.0;
    9618           0 :     if ( !bLinear )
    9619             :     {
    9620           0 :         fBorder /= 2.0;
    9621             :     }
    9622             : 
    9623           0 :     aBoundRect.Bottom() -= fBorder;
    9624           0 :     if (!bLinear)
    9625             :     {
    9626           0 :         aBoundRect.Top() += fBorder;
    9627             :     }
    9628             : 
    9629           0 :     switch (rObject.m_aGradient.GetStyle())
    9630             :     {
    9631             :         case GradientStyle_LINEAR:
    9632             :         case GradientStyle_AXIAL:
    9633             :         {
    9634             :             aLine.append("/Domain[ 0 1 ]\n"
    9635           0 :                     "/Coords[ " );
    9636           0 :             Polygon     aPoly( 2 );
    9637           0 :             aPoly[0] = aBoundRect.BottomCenter();
    9638           0 :             aPoly[1] = aBoundRect.TopCenter();
    9639           0 :             aPoly.Rotate( aCenter, 3600 - nAngle );
    9640             : 
    9641           0 :             aLine.append( (sal_Int32) aPoly[0].X() );
    9642           0 :             aLine.append( " " );
    9643           0 :             aLine.append( (sal_Int32) aPoly[0].Y() );
    9644           0 :             aLine.append( " " );
    9645           0 :             aLine.append( (sal_Int32) aPoly[1].X());
    9646           0 :             aLine.append( " ");
    9647           0 :             aLine.append( (sal_Int32) aPoly[1].Y());
    9648           0 :             aLine.append( " ]\n");
    9649           0 :             aLine.append("/Extend [true true]\n");
    9650           0 :             break;
    9651             :         }
    9652             :         default:
    9653             :             aLine.append("/Domain[ 0 1 0 1 ]\n"
    9654           0 :                     "/Matrix[ " );
    9655           0 :             aLine.append( (sal_Int32)aSize.Width() );
    9656           0 :             aLine.append( " 0 0 " );
    9657           0 :             aLine.append( (sal_Int32)aSize.Height() );
    9658           0 :             aLine.append( " 0 0 ]\n");
    9659             :     }
    9660           0 :     aLine.append("/Function " );
    9661           0 :     aLine.append( nFunctionObject );
    9662             :     aLine.append( " 0 R\n"
    9663             :                   ">>\n"
    9664           0 :                   "endobj\n\n" );
    9665           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9666             : 
    9667           0 :     return true;
    9668             : }
    9669             : 
    9670           0 : bool PDFWriterImpl::writeJPG( JPGEmit& rObject )
    9671             : {
    9672           0 :     CHECK_RETURN( rObject.m_pStream );
    9673           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9674             : 
    9675           0 :     sal_Int32 nLength = 0;
    9676           0 :     rObject.m_pStream->Seek( STREAM_SEEK_TO_END );
    9677           0 :     nLength = rObject.m_pStream->Tell();
    9678           0 :     rObject.m_pStream->Seek( STREAM_SEEK_TO_BEGIN );
    9679             : 
    9680           0 :     sal_Int32 nMaskObject = 0;
    9681           0 :     if( !!rObject.m_aMask )
    9682             :     {
    9683           0 :         if( rObject.m_aMask.GetBitCount() == 1 ||
    9684           0 :             ( rObject.m_aMask.GetBitCount() == 8 && m_aContext.Version >= PDFWriter::PDF_1_4 && !m_bIsPDF_A1 )//i59651
    9685             :             )
    9686             :         {
    9687           0 :             nMaskObject = createObject();
    9688             :         }
    9689           0 :         else if( m_bIsPDF_A1 )
    9690           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9691           0 :         else if( m_aContext.Version < PDFWriter::PDF_1_4 )
    9692           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDF13 );
    9693             : 
    9694             :     }
    9695             :     #if OSL_DEBUG_LEVEL > 1
    9696             :     emitComment( "PDFWriterImpl::writeJPG" );
    9697             :     #endif
    9698             : 
    9699           0 :     OStringBuffer aLine(200);
    9700           0 :     aLine.append( rObject.m_nObject );
    9701             :     aLine.append( " 0 obj\n"
    9702           0 :                   "<</Type/XObject/Subtype/Image/Width " );
    9703           0 :     aLine.append( (sal_Int32)rObject.m_aID.m_aPixelSize.Width() );
    9704           0 :     aLine.append( " /Height " );
    9705           0 :     aLine.append( (sal_Int32)rObject.m_aID.m_aPixelSize.Height() );
    9706           0 :     aLine.append( " /BitsPerComponent 8 " );
    9707           0 :     if( rObject.m_bTrueColor )
    9708           0 :         aLine.append( "/ColorSpace/DeviceRGB" );
    9709             :     else
    9710           0 :         aLine.append( "/ColorSpace/DeviceGray" );
    9711           0 :     aLine.append( "/Filter/DCTDecode/Length " );
    9712           0 :     aLine.append( nLength );
    9713           0 :     if( nMaskObject )
    9714             :     {
    9715           0 :         aLine.append( rObject.m_aMask.GetBitCount() == 1 ? " /Mask " : " /SMask " );
    9716           0 :         aLine.append( nMaskObject );
    9717           0 :         aLine.append( " 0 R " );
    9718             :     }
    9719           0 :     aLine.append( ">>\nstream\n" );
    9720           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9721             : 
    9722           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9723           0 :     CHECK_RETURN( writeBuffer( rObject.m_pStream->GetData(), nLength ) );
    9724           0 :     disableStreamEncryption();
    9725             : 
    9726           0 :     aLine.setLength( 0 );
    9727           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9728           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9729             : 
    9730           0 :     if( nMaskObject )
    9731             :     {
    9732           0 :         BitmapEmit aEmit;
    9733           0 :         aEmit.m_nObject = nMaskObject;
    9734           0 :         if( rObject.m_aMask.GetBitCount() == 1 )
    9735           0 :             aEmit.m_aBitmap = BitmapEx( rObject.m_aMask, rObject.m_aMask );
    9736           0 :         else if( rObject.m_aMask.GetBitCount() == 8 )
    9737           0 :             aEmit.m_aBitmap = BitmapEx( rObject.m_aMask, AlphaMask( rObject.m_aMask ) );
    9738           0 :         writeBitmapObject( aEmit, true );
    9739             :     }
    9740             : 
    9741           0 :     return true;
    9742             : }
    9743             : 
    9744           0 : bool PDFWriterImpl::writeBitmapObject( BitmapEmit& rObject, bool bMask )
    9745             : {
    9746           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9747             : 
    9748           0 :     Bitmap  aBitmap;
    9749           0 :     Color   aTransparentColor( COL_TRANSPARENT );
    9750           0 :     bool    bWriteMask = false;
    9751           0 :     if( ! bMask )
    9752             :     {
    9753           0 :         aBitmap = rObject.m_aBitmap.GetBitmap();
    9754           0 :         if( rObject.m_aBitmap.IsAlpha() )
    9755             :         {
    9756           0 :             if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    9757           0 :                 bWriteMask = true;
    9758             :             // else draw without alpha channel
    9759             :         }
    9760             :         else
    9761             :         {
    9762           0 :             switch( rObject.m_aBitmap.GetTransparentType() )
    9763             :             {
    9764             :                 case TRANSPARENT_NONE:
    9765             :                     // comes from drawMask function
    9766           0 :                     if( aBitmap.GetBitCount() == 1 && rObject.m_bDrawMask )
    9767           0 :                         bMask = true;
    9768           0 :                     break;
    9769             :                 case TRANSPARENT_COLOR:
    9770           0 :                     aTransparentColor = rObject.m_aBitmap.GetTransparentColor();
    9771           0 :                     break;
    9772             :                 case TRANSPARENT_BITMAP:
    9773           0 :                     bWriteMask = true;
    9774           0 :                     break;
    9775             :             }
    9776             :         }
    9777             :     }
    9778             :     else
    9779             :     {
    9780           0 :         if( m_aContext.Version < PDFWriter::PDF_1_4 || ! rObject.m_aBitmap.IsAlpha() )
    9781             :         {
    9782           0 :             aBitmap = rObject.m_aBitmap.GetMask();
    9783           0 :             aBitmap.Convert( BMP_CONVERSION_1BIT_THRESHOLD );
    9784             :             DBG_ASSERT( aBitmap.GetBitCount() == 1, "mask conversion failed" );
    9785             :         }
    9786           0 :         else if( aBitmap.GetBitCount() != 8 )
    9787             :         {
    9788           0 :             aBitmap = rObject.m_aBitmap.GetAlpha().GetBitmap();
    9789           0 :             aBitmap.Convert( BMP_CONVERSION_8BIT_GREYS );
    9790             :             DBG_ASSERT( aBitmap.GetBitCount() == 8, "alpha mask conversion failed" );
    9791             :         }
    9792             :     }
    9793             : 
    9794           0 :     BitmapReadAccess* pAccess = aBitmap.AcquireReadAccess();
    9795           0 :     AccessReleaser aReleaser( pAccess );
    9796             : 
    9797             :     bool bTrueColor;
    9798             :     sal_Int32 nBitsPerComponent;
    9799           0 :     switch( aBitmap.GetBitCount() )
    9800             :     {
    9801             :         case 1:
    9802             :         case 2:
    9803             :         case 4:
    9804             :         case 8:
    9805           0 :             bTrueColor = false;
    9806           0 :             nBitsPerComponent = aBitmap.GetBitCount();
    9807           0 :             break;
    9808             :         default:
    9809           0 :             bTrueColor = true;
    9810           0 :             nBitsPerComponent = 8;
    9811           0 :             break;
    9812             :     }
    9813             : 
    9814           0 :     sal_Int32 nStreamLengthObject   = createObject();
    9815           0 :     sal_Int32 nMaskObject           = 0;
    9816             : 
    9817             :     #if OSL_DEBUG_LEVEL > 1
    9818             :     emitComment( "PDFWriterImpl::writeBitmapObject" );
    9819             :     #endif
    9820           0 :     OStringBuffer aLine(1024);
    9821           0 :     aLine.append( rObject.m_nObject );
    9822             :     aLine.append( " 0 obj\n"
    9823           0 :                   "<</Type/XObject/Subtype/Image/Width " );
    9824           0 :     aLine.append( (sal_Int32)aBitmap.GetSizePixel().Width() );
    9825           0 :     aLine.append( "/Height " );
    9826           0 :     aLine.append( (sal_Int32)aBitmap.GetSizePixel().Height() );
    9827           0 :     aLine.append( "/BitsPerComponent " );
    9828           0 :     aLine.append( nBitsPerComponent );
    9829           0 :     aLine.append( "/Length " );
    9830           0 :     aLine.append( nStreamLengthObject );
    9831           0 :     aLine.append( " 0 R\n" );
    9832             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9833           0 :     if( nBitsPerComponent != 1 )
    9834             :     {
    9835           0 :         aLine.append( "/Filter/FlateDecode" );
    9836             :     }
    9837             :     else
    9838             :     {
    9839           0 :         aLine.append( "/Filter/CCITTFaxDecode/DecodeParms<</K -1/BlackIs1 true/Columns " );
    9840           0 :         aLine.append( (sal_Int32)aBitmap.GetSizePixel().Width() );
    9841           0 :         aLine.append( ">>\n" );
    9842             :     }
    9843             : #endif
    9844           0 :     if( ! bMask )
    9845             :     {
    9846           0 :         aLine.append( "/ColorSpace" );
    9847           0 :         if( bTrueColor )
    9848           0 :             aLine.append( "/DeviceRGB\n" );
    9849           0 :         else if( aBitmap.HasGreyPalette() )
    9850             :         {
    9851           0 :             aLine.append( "/DeviceGray\n" );
    9852           0 :             if( aBitmap.GetBitCount() == 1 )
    9853             :             {
    9854             :                 // #i47395# 1 bit bitmaps occasionally have an inverted grey palette
    9855           0 :                 sal_Int32 nBlackIndex = pAccess->GetBestPaletteIndex( BitmapColor( Color( COL_BLACK ) ) );
    9856             :                 DBG_ASSERT( nBlackIndex == 0 || nBlackIndex == 1, "wrong black index" );
    9857           0 :                 if( nBlackIndex == 1 )
    9858           0 :                     aLine.append( "/Decode[1 0]\n" );
    9859             :             }
    9860             :         }
    9861             :         else
    9862             :         {
    9863           0 :             aLine.append( "[ /Indexed/DeviceRGB " );
    9864           0 :             aLine.append( (sal_Int32)(pAccess->GetPaletteEntryCount()-1) );
    9865           0 :             aLine.append( "\n<" );
    9866           0 :             if( m_aContext.Encryption.Encrypt() )
    9867             :             {
    9868           0 :                 enableStringEncryption( rObject.m_nObject );
    9869             :                 //check encryption buffer size
    9870           0 :                 if( checkEncryptionBufferSize( pAccess->GetPaletteEntryCount()*3 ) )
    9871             :                 {
    9872           0 :                     int nChar = 0;
    9873             :                     //fill the encryption buffer
    9874           0 :                     for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9875             :                     {
    9876           0 :                         const BitmapColor& rColor = pAccess->GetPaletteColor( i );
    9877           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetRed();
    9878           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetGreen();
    9879           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetBlue();
    9880             :                     }
    9881             :                     //encrypt the colorspace lookup table
    9882           0 :                     rtl_cipher_encodeARCFOUR( m_aCipher, m_pEncryptionBuffer, nChar, m_pEncryptionBuffer, nChar );
    9883             :                     //now queue the data for output
    9884           0 :                     nChar = 0;
    9885           0 :                     for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9886             :                     {
    9887           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9888           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9889           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9890             :                     }
    9891             :                 }
    9892             :             }
    9893             :             else //no encryption requested (PDF/A-1a program flow drops here)
    9894             :             {
    9895           0 :                 for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9896             :                 {
    9897           0 :                     const BitmapColor& rColor = pAccess->GetPaletteColor( i );
    9898           0 :                     appendHex( rColor.GetRed(), aLine );
    9899           0 :                     appendHex( rColor.GetGreen(), aLine );
    9900           0 :                     appendHex( rColor.GetBlue(), aLine );
    9901             :                 }
    9902             :             }
    9903           0 :             aLine.append( ">\n]\n" );
    9904             :         }
    9905             :     }
    9906             :     else
    9907             :     {
    9908           0 :         if( aBitmap.GetBitCount() == 1 )
    9909             :         {
    9910           0 :             aLine.append( "/ImageMask true\n" );
    9911           0 :             sal_Int32 nBlackIndex = pAccess->GetBestPaletteIndex( BitmapColor( Color( COL_BLACK ) ) );
    9912             :             DBG_ASSERT( nBlackIndex == 0 || nBlackIndex == 1, "wrong black index" );
    9913           0 :             if( nBlackIndex )
    9914           0 :                 aLine.append( "/Decode[ 1 0 ]\n" );
    9915             :             else
    9916           0 :                 aLine.append( "/Decode[ 0 1 ]\n" );
    9917             :         }
    9918           0 :         else if( aBitmap.GetBitCount() == 8 )
    9919             :         {
    9920             :             aLine.append( "/ColorSpace/DeviceGray\n"
    9921           0 :                           "/Decode [ 1 0 ]\n" );
    9922             :         }
    9923             :     }
    9924             : 
    9925           0 :     if( ! bMask && m_aContext.Version > PDFWriter::PDF_1_2 && !m_bIsPDF_A1 )//i59651
    9926             :     {
    9927           0 :         if( bWriteMask )
    9928             :         {
    9929           0 :             nMaskObject = createObject();
    9930           0 :             if( rObject.m_aBitmap.IsAlpha() && m_aContext.Version > PDFWriter::PDF_1_3 )
    9931           0 :                 aLine.append( "/SMask " );
    9932             :             else
    9933           0 :                 aLine.append( "/Mask " );
    9934           0 :             aLine.append( nMaskObject );
    9935           0 :             aLine.append( " 0 R\n" );
    9936             :         }
    9937           0 :         else if( aTransparentColor != Color( COL_TRANSPARENT ) )
    9938             :         {
    9939           0 :             aLine.append( "/Mask[ " );
    9940           0 :             if( bTrueColor )
    9941             :             {
    9942           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetRed() );
    9943           0 :                 aLine.append( ' ' );
    9944           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetRed() );
    9945           0 :                 aLine.append( ' ' );
    9946           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetGreen() );
    9947           0 :                 aLine.append( ' ' );
    9948           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetGreen() );
    9949           0 :                 aLine.append( ' ' );
    9950           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetBlue() );
    9951           0 :                 aLine.append( ' ' );
    9952           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetBlue() );
    9953             :             }
    9954             :             else
    9955             :             {
    9956           0 :                 sal_Int32 nIndex = pAccess->GetBestPaletteIndex( BitmapColor( aTransparentColor ) );
    9957           0 :                 aLine.append( nIndex );
    9958             :             }
    9959           0 :             aLine.append( " ]\n" );
    9960           0 :         }
    9961             :     }
    9962           0 :     else if( m_bIsPDF_A1 && (bWriteMask || aTransparentColor != Color( COL_TRANSPARENT )) )
    9963           0 :         m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9964             : 
    9965             :     aLine.append( ">>\n"
    9966           0 :                   "stream\n" );
    9967           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9968           0 :     sal_uInt64 nStartPos = 0;
    9969           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nStartPos )) );
    9970             : 
    9971           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9972             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9973           0 :     if( nBitsPerComponent == 1 )
    9974             :     {
    9975           0 :         writeG4Stream( pAccess );
    9976             :     }
    9977             :     else
    9978             : #endif
    9979             :     {
    9980           0 :         beginCompression();
    9981           0 :         if( ! bTrueColor || pAccess->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB )
    9982             :         {
    9983           0 :             const int nScanLineBytes = 1 + ( pAccess->GetBitCount() * ( pAccess->Width() - 1 ) / 8U );
    9984             : 
    9985           0 :             for( int i = 0; i < pAccess->Height(); i++ )
    9986             :             {
    9987           0 :                 CHECK_RETURN( writeBuffer( pAccess->GetScanline( i ), nScanLineBytes ) );
    9988             :             }
    9989             :         }
    9990             :         else
    9991             :         {
    9992           0 :             const int nScanLineBytes = pAccess->Width()*3;
    9993           0 :             boost::shared_array<sal_uInt8> pCol( new sal_uInt8[ nScanLineBytes ] );
    9994           0 :             for( int y = 0; y < pAccess->Height(); y++ )
    9995             :             {
    9996           0 :                 for( int x = 0; x < pAccess->Width(); x++ )
    9997             :                 {
    9998           0 :                     BitmapColor aColor = pAccess->GetColor( y, x );
    9999           0 :                     pCol[3*x+0] = aColor.GetRed();
   10000           0 :                     pCol[3*x+1] = aColor.GetGreen();
   10001           0 :                     pCol[3*x+2] = aColor.GetBlue();
   10002           0 :                 }
   10003           0 :                 CHECK_RETURN( writeBuffer( pCol.get(), nScanLineBytes ) );
   10004           0 :             }
   10005             :         }
   10006           0 :         endCompression();
   10007             :     }
   10008           0 :     disableStreamEncryption();
   10009             : 
   10010           0 :     sal_uInt64 nEndPos = 0;
   10011           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nEndPos )) );
   10012           0 :     aLine.setLength( 0 );
   10013           0 :     aLine.append( "\nendstream\nendobj\n\n" );
   10014           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
   10015           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
   10016           0 :     aLine.setLength( 0 );
   10017           0 :     aLine.append( nStreamLengthObject );
   10018           0 :     aLine.append( " 0 obj\n" );
   10019           0 :     aLine.append( (sal_Int64)(nEndPos-nStartPos) );
   10020           0 :     aLine.append( "\nendobj\n\n" );
   10021           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
   10022             : 
   10023           0 :     if( nMaskObject )
   10024             :     {
   10025           0 :         BitmapEmit aEmit;
   10026           0 :         aEmit.m_nObject             = nMaskObject;
   10027           0 :         aEmit.m_aBitmap             = rObject.m_aBitmap;
   10028           0 :         return writeBitmapObject( aEmit, true );
   10029             :     }
   10030             : 
   10031           0 :     return true;
   10032             : }
   10033             : 
   10034           0 : void PDFWriterImpl::drawJPGBitmap( SvStream& rDCTData, bool bIsTrueColor, const Size& rSizePixel, const Rectangle& rTargetArea, const Bitmap& rMask )
   10035             : {
   10036           0 :     MARK( "drawJPGBitmap" );
   10037             : 
   10038           0 :     OStringBuffer aLine( 80 );
   10039           0 :     updateGraphicsState();
   10040             : 
   10041             :     // #i40055# sanity check
   10042           0 :     if( ! (rTargetArea.GetWidth() && rTargetArea.GetHeight() ) )
   10043           0 :         return;
   10044           0 :     if( ! (rSizePixel.Width() && rSizePixel.Height()) )
   10045           0 :         return;
   10046             : 
   10047           0 :     rDCTData.Seek( 0 );
   10048           0 :     if( bIsTrueColor && m_aContext.ColorMode == PDFWriter::DrawGreyscale )
   10049             :     {
   10050             :         // need to convert to grayscale;
   10051             :         // load stream to bitmap and draw the bitmap instead
   10052           0 :         Graphic aGraphic;
   10053           0 :         GraphicConverter::Import( rDCTData, aGraphic, CVT_JPG );
   10054           0 :         Bitmap aBmp( aGraphic.GetBitmap() );
   10055           0 :         if( !!rMask && rMask.GetSizePixel() == aBmp.GetSizePixel() )
   10056             :         {
   10057           0 :             BitmapEx aBmpEx( aBmp, rMask );
   10058           0 :             drawBitmap( rTargetArea.TopLeft(), rTargetArea.GetSize(), aBmpEx );
   10059             :         }
   10060             :         else
   10061           0 :             drawBitmap( rTargetArea.TopLeft(), rTargetArea.GetSize(), aBmp );
   10062           0 :         return;
   10063             :     }
   10064             : 
   10065           0 :     SvMemoryStream* pStream = new SvMemoryStream;
   10066           0 :     pStream->WriteStream( rDCTData );
   10067           0 :     pStream->Seek( STREAM_SEEK_TO_END );
   10068             : 
   10069           0 :     BitmapID aID;
   10070           0 :     aID.m_aPixelSize    = rSizePixel;
   10071           0 :     aID.m_nSize         = pStream->Tell();
   10072           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
   10073           0 :     aID.m_nChecksum     = rtl_crc32( 0, pStream->GetData(), aID.m_nSize );
   10074           0 :     if( ! rMask.IsEmpty() )
   10075           0 :         aID.m_nMaskChecksum = rMask.GetChecksum();
   10076             : 
   10077           0 :     std::list< JPGEmit >::const_iterator it;
   10078           0 :     for( it = m_aJPGs.begin(); it != m_aJPGs.end() && ! (aID == it->m_aID); ++it )
   10079             :         ;
   10080           0 :     if( it == m_aJPGs.end() )
   10081             :     {
   10082           0 :         m_aJPGs.push_front( JPGEmit() );
   10083           0 :         JPGEmit& rEmit = m_aJPGs.front();
   10084           0 :         rEmit.m_nObject     = createObject();
   10085           0 :         rEmit.m_aID         = aID;
   10086           0 :         rEmit.m_pStream     = pStream;
   10087           0 :         rEmit.m_bTrueColor  = bIsTrueColor;
   10088           0 :         if( !! rMask && rMask.GetSizePixel() == rSizePixel )
   10089           0 :             rEmit.m_aMask   = rMask;
   10090             : 
   10091           0 :         it = m_aJPGs.begin();
   10092             :     }
   10093             :     else
   10094           0 :         delete pStream;
   10095             : 
   10096           0 :     aLine.append( "q " );
   10097           0 :     sal_Int32 nCheckWidth = 0;
   10098           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rTargetArea.GetWidth(), aLine, false, &nCheckWidth );
   10099           0 :     aLine.append( " 0 0 " );
   10100           0 :     sal_Int32 nCheckHeight = 0;
   10101           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rTargetArea.GetHeight(), aLine, true, &nCheckHeight );
   10102           0 :     aLine.append( ' ' );
   10103           0 :     m_aPages.back().appendPoint( rTargetArea.BottomLeft(), aLine );
   10104           0 :     aLine.append( " cm\n/Im" );
   10105           0 :     aLine.append( it->m_nObject );
   10106           0 :     aLine.append( " Do Q\n" );
   10107           0 :     if( nCheckWidth == 0 || nCheckHeight == 0 )
   10108             :     {
   10109             :         // #i97512# avoid invalid current matrix
   10110           0 :         aLine.setLength( 0 );
   10111           0 :         aLine.append( "\n%jpeg image /Im" );
   10112           0 :         aLine.append( it->m_nObject );
   10113           0 :         aLine.append( " scaled to zero size, omitted\n" );
   10114             :     }
   10115           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10116             : 
   10117           0 :     OStringBuffer aObjName( 16 );
   10118           0 :     aObjName.append( "Im" );
   10119           0 :     aObjName.append( it->m_nObject );
   10120           0 :     pushResource( ResXObject, aObjName.makeStringAndClear(), it->m_nObject );
   10121             : 
   10122             : }
   10123             : 
   10124           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const BitmapEmit& rBitmap, const Color& rFillColor )
   10125             : {
   10126           0 :     OStringBuffer aLine( 80 );
   10127           0 :     updateGraphicsState();
   10128             : 
   10129           0 :     aLine.append( "q " );
   10130           0 :     if( rFillColor != Color( COL_TRANSPARENT ) )
   10131             :     {
   10132           0 :         appendNonStrokingColor( rFillColor, aLine );
   10133           0 :         aLine.append( ' ' );
   10134             :     }
   10135           0 :     sal_Int32 nCheckWidth = 0;
   10136           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rDestSize.Width(), aLine, false, &nCheckWidth );
   10137           0 :     aLine.append( " 0 0 " );
   10138           0 :     sal_Int32 nCheckHeight = 0;
   10139           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rDestSize.Height(), aLine, true, &nCheckHeight );
   10140           0 :     aLine.append( ' ' );
   10141           0 :     m_aPages.back().appendPoint( rDestPoint + Point( 0, rDestSize.Height()-1 ), aLine );
   10142           0 :     aLine.append( " cm\n/Im" );
   10143           0 :     aLine.append( rBitmap.m_nObject );
   10144           0 :     aLine.append( " Do Q\n" );
   10145           0 :     if( nCheckWidth == 0 || nCheckHeight == 0 )
   10146             :     {
   10147             :         // #i97512# avoid invalid current matrix
   10148           0 :         aLine.setLength( 0 );
   10149           0 :         aLine.append( "\n%bitmap image /Im" );
   10150           0 :         aLine.append( rBitmap.m_nObject );
   10151           0 :         aLine.append( " scaled to zero size, omitted\n" );
   10152             :     }
   10153           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10154           0 : }
   10155             : 
   10156           0 : const PDFWriterImpl::BitmapEmit& PDFWriterImpl::createBitmapEmit( const BitmapEx& i_rBitmap, bool bDrawMask )
   10157             : {
   10158           0 :     BitmapEx aBitmap( i_rBitmap );
   10159           0 :     if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
   10160             :     {
   10161           0 :         BmpConversion eConv = BMP_CONVERSION_8BIT_GREYS;
   10162           0 :         int nDepth = aBitmap.GetBitmap().GetBitCount();
   10163           0 :         if( nDepth <= 4 )
   10164           0 :             eConv = BMP_CONVERSION_4BIT_GREYS;
   10165           0 :         if( nDepth > 1 )
   10166           0 :             aBitmap.Convert( eConv );
   10167             :     }
   10168           0 :     BitmapID aID;
   10169           0 :     aID.m_aPixelSize        = aBitmap.GetSizePixel();
   10170           0 :     aID.m_nSize             = aBitmap.GetBitCount();
   10171           0 :     aID.m_nChecksum         = aBitmap.GetBitmap().GetChecksum();
   10172           0 :     aID.m_nMaskChecksum     = 0;
   10173           0 :     if( aBitmap.IsAlpha() )
   10174           0 :         aID.m_nMaskChecksum = aBitmap.GetAlpha().GetChecksum();
   10175             :     else
   10176             :     {
   10177           0 :         Bitmap aMask = aBitmap.GetMask();
   10178           0 :         if( ! aMask.IsEmpty() )
   10179           0 :             aID.m_nMaskChecksum = aMask.GetChecksum();
   10180             :     }
   10181           0 :     std::list< BitmapEmit >::const_iterator it;
   10182           0 :     for( it = m_aBitmaps.begin(); it != m_aBitmaps.end(); ++it )
   10183             :     {
   10184           0 :         if( aID == it->m_aID )
   10185           0 :             break;
   10186             :     }
   10187           0 :     if( it == m_aBitmaps.end() )
   10188             :     {
   10189           0 :         m_aBitmaps.push_front( BitmapEmit() );
   10190           0 :         m_aBitmaps.front().m_aID        = aID;
   10191           0 :         m_aBitmaps.front().m_aBitmap    = aBitmap;
   10192           0 :         m_aBitmaps.front().m_nObject    = createObject();
   10193           0 :         m_aBitmaps.front().m_bDrawMask  = bDrawMask;
   10194           0 :         it = m_aBitmaps.begin();
   10195             :     }
   10196             : 
   10197           0 :     OStringBuffer aObjName( 16 );
   10198           0 :     aObjName.append( "Im" );
   10199           0 :     aObjName.append( it->m_nObject );
   10200           0 :     pushResource( ResXObject, aObjName.makeStringAndClear(), it->m_nObject );
   10201             : 
   10202           0 :     return *it;
   10203             : }
   10204             : 
   10205           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const Bitmap& rBitmap )
   10206             : {
   10207           0 :     MARK( "drawBitmap (Bitmap)" );
   10208             : 
   10209             :     // #i40055# sanity check
   10210           0 :     if( ! (rDestSize.Width() && rDestSize.Height()) )
   10211           0 :         return;
   10212             : 
   10213           0 :     const BitmapEmit& rEmit = createBitmapEmit( BitmapEx( rBitmap ) );
   10214           0 :     drawBitmap( rDestPoint, rDestSize, rEmit, Color( COL_TRANSPARENT ) );
   10215             : }
   10216             : 
   10217           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const BitmapEx& rBitmap )
   10218             : {
   10219           0 :     MARK( "drawBitmap (BitmapEx)" );
   10220             : 
   10221             :     // #i40055# sanity check
   10222           0 :     if( ! (rDestSize.Width() && rDestSize.Height()) )
   10223           0 :         return;
   10224             : 
   10225           0 :     const BitmapEmit& rEmit = createBitmapEmit( rBitmap );
   10226           0 :     drawBitmap( rDestPoint, rDestSize, rEmit, Color( COL_TRANSPARENT ) );
   10227             : }
   10228             : 
   10229           0 : sal_Int32 PDFWriterImpl::createGradient( const Gradient& rGradient, const Size& rSize )
   10230             : {
   10231           0 :     Size aPtSize( lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10232             :                                MapMode( MAP_POINT ),
   10233             :                                getReferenceDevice(),
   10234           0 :                                rSize ) );
   10235             :     // check if we already have this gradient
   10236           0 :     std::list<GradientEmit>::iterator it;
   10237             :     // rounding to point will generally lose some pixels
   10238             :     // round up to point boundary
   10239           0 :     aPtSize.Width()++;
   10240           0 :     aPtSize.Height()++;
   10241           0 :     for( it = m_aGradients.begin(); it != m_aGradients.end(); ++it )
   10242             :     {
   10243           0 :         if( it->m_aGradient == rGradient )
   10244             :         {
   10245           0 :             if( it->m_aSize == aPtSize )
   10246           0 :                 break;
   10247             :         }
   10248             :     }
   10249           0 :     if( it == m_aGradients.end() )
   10250             :     {
   10251           0 :         m_aGradients.push_front( GradientEmit() );
   10252           0 :         m_aGradients.front().m_aGradient    = rGradient;
   10253           0 :         m_aGradients.front().m_nObject      = createObject();
   10254           0 :         m_aGradients.front().m_aSize        = aPtSize;
   10255           0 :         it = m_aGradients.begin();
   10256             :     }
   10257             : 
   10258           0 :     OStringBuffer aObjName( 16 );
   10259           0 :     aObjName.append( 'P' );
   10260           0 :     aObjName.append( it->m_nObject );
   10261           0 :     pushResource( ResShading, aObjName.makeStringAndClear(), it->m_nObject );
   10262             : 
   10263           0 :     return it->m_nObject;
   10264             : }
   10265             : 
   10266           0 : void PDFWriterImpl::drawGradient( const Rectangle& rRect, const Gradient& rGradient )
   10267             : {
   10268           0 :     MARK( "drawGradient (Rectangle)" );
   10269             : 
   10270           0 :     if( m_aContext.Version == PDFWriter::PDF_1_2 )
   10271             :     {
   10272           0 :         drawRectangle( rRect );
   10273           0 :         return;
   10274             :     }
   10275             : 
   10276           0 :     sal_Int32 nGradient = createGradient( rGradient, rRect.GetSize() );
   10277             : 
   10278           0 :     Point aTranslate( rRect.BottomLeft() );
   10279           0 :     aTranslate += Point( 0, 1 );
   10280             : 
   10281           0 :     updateGraphicsState();
   10282             : 
   10283           0 :     OStringBuffer aLine( 80 );
   10284           0 :     aLine.append( "q 1 0 0 1 " );
   10285           0 :     m_aPages.back().appendPoint( aTranslate, aLine );
   10286           0 :     aLine.append( " cm " );
   10287             :     // if a stroke is appended reset the clip region before stroke
   10288           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
   10289           0 :         aLine.append( "q " );
   10290           0 :     aLine.append( "0 0 " );
   10291           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetWidth(), aLine, false );
   10292           0 :     aLine.append( ' ' );
   10293           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetHeight(), aLine, true );
   10294           0 :     aLine.append( " re W n\n" );
   10295             : 
   10296           0 :     aLine.append( "/P" );
   10297           0 :     aLine.append( nGradient );
   10298           0 :     aLine.append( " sh " );
   10299           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
   10300             :     {
   10301           0 :         aLine.append( "Q 0 0 " );
   10302           0 :         m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetWidth(), aLine, false );
   10303           0 :         aLine.append( ' ' );
   10304           0 :         m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetHeight(), aLine, true );
   10305           0 :         aLine.append( " re S " );
   10306             :     }
   10307           0 :     aLine.append( "Q\n" );
   10308           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10309             : }
   10310             : 
   10311           0 : void PDFWriterImpl::drawHatch( const PolyPolygon& rPolyPoly, const Hatch& rHatch )
   10312             : {
   10313           0 :     MARK( "drawHatch" );
   10314             : 
   10315           0 :     updateGraphicsState();
   10316             : 
   10317           0 :     if( rPolyPoly.Count() )
   10318             :     {
   10319           0 :         PolyPolygon     aPolyPoly( rPolyPoly );
   10320             : 
   10321           0 :         aPolyPoly.Optimize( POLY_OPTIMIZE_NO_SAME );
   10322           0 :         push( PUSH_LINECOLOR );
   10323           0 :         setLineColor( rHatch.GetColor() );
   10324           0 :         getReferenceDevice()->ImplDrawHatch( aPolyPoly, rHatch, false );
   10325           0 :         pop();
   10326             :     }
   10327           0 : }
   10328             : 
   10329           0 : void PDFWriterImpl::drawWallpaper( const Rectangle& rRect, const Wallpaper& rWall )
   10330             : {
   10331           0 :     MARK( "drawWallpaper" );
   10332             : 
   10333           0 :     bool bDrawColor         = false;
   10334           0 :     bool bDrawGradient      = false;
   10335           0 :     bool bDrawBitmap        = false;
   10336             : 
   10337           0 :     BitmapEx aBitmap;
   10338           0 :     Point aBmpPos = rRect.TopLeft();
   10339           0 :     Size aBmpSize;
   10340           0 :     if( rWall.IsBitmap() )
   10341             :     {
   10342           0 :         aBitmap = rWall.GetBitmap();
   10343           0 :         aBmpSize = lcl_convert( aBitmap.GetPrefMapMode(),
   10344           0 :                                 getMapMode(),
   10345             :                                 getReferenceDevice(),
   10346           0 :                                 aBitmap.GetPrefSize() );
   10347           0 :         Rectangle aRect( rRect );
   10348           0 :         if( rWall.IsRect() )
   10349             :         {
   10350           0 :             aRect = rWall.GetRect();
   10351           0 :             aBmpPos = aRect.TopLeft();
   10352           0 :             aBmpSize = aRect.GetSize();
   10353             :         }
   10354           0 :         if( rWall.GetStyle() != WALLPAPER_SCALE )
   10355             :         {
   10356           0 :             if( rWall.GetStyle() != WALLPAPER_TILE )
   10357             :             {
   10358           0 :                 bDrawBitmap     = true;
   10359           0 :                 if( rWall.IsGradient() )
   10360           0 :                     bDrawGradient = true;
   10361             :                 else
   10362           0 :                     bDrawColor = true;
   10363           0 :                 switch( rWall.GetStyle() )
   10364             :                 {
   10365             :                     case WALLPAPER_TOPLEFT:
   10366           0 :                         break;
   10367             :                     case WALLPAPER_TOP:
   10368           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10369           0 :                         break;
   10370             :                     case WALLPAPER_LEFT:
   10371           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10372           0 :                         break;
   10373             :                     case WALLPAPER_TOPRIGHT:
   10374           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10375           0 :                         break;
   10376             :                     case WALLPAPER_CENTER:
   10377           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10378           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10379           0 :                         break;
   10380             :                     case WALLPAPER_RIGHT:
   10381           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10382           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10383           0 :                         break;
   10384             :                     case WALLPAPER_BOTTOMLEFT:
   10385           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10386           0 :                         break;
   10387             :                     case WALLPAPER_BOTTOM:
   10388           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10389           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10390           0 :                         break;
   10391             :                     case WALLPAPER_BOTTOMRIGHT:
   10392           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10393           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10394           0 :                         break;
   10395             :                     default: ;
   10396             :                 }
   10397             :             }
   10398             :             else
   10399             :             {
   10400             :                 // push the bitmap
   10401           0 :                 const BitmapEmit& rEmit = createBitmapEmit( BitmapEx( aBitmap ) );
   10402             : 
   10403             :                 // convert to page coordinates; this needs to be done here
   10404             :                 // since the emit does not know the page anymore
   10405           0 :                 Rectangle aConvertRect( aBmpPos, aBmpSize );
   10406           0 :                 m_aPages.back().convertRect( aConvertRect );
   10407             : 
   10408           0 :                 OStringBuffer aNameBuf(16);
   10409           0 :                 aNameBuf.append( "Im" );
   10410           0 :                 aNameBuf.append( rEmit.m_nObject );
   10411           0 :                 OString aImageName( aNameBuf.makeStringAndClear() );
   10412             : 
   10413             :                 // push the pattern
   10414           0 :                 OStringBuffer aTilingStream( 32 );
   10415           0 :                 appendFixedInt( aConvertRect.GetWidth(), aTilingStream );
   10416           0 :                 aTilingStream.append( " 0 0 " );
   10417           0 :                 appendFixedInt( aConvertRect.GetHeight(), aTilingStream );
   10418           0 :                 aTilingStream.append( " 0 0 cm\n/" );
   10419           0 :                 aTilingStream.append( aImageName );
   10420           0 :                 aTilingStream.append( " Do\n" );
   10421             : 
   10422           0 :                 m_aTilings.push_back( TilingEmit() );
   10423           0 :                 m_aTilings.back().m_nObject         = createObject();
   10424           0 :                 m_aTilings.back().m_aRectangle      = Rectangle( Point( 0, 0 ), aConvertRect.GetSize() );
   10425           0 :                 m_aTilings.back().m_pTilingStream   = new SvMemoryStream();
   10426           0 :                 m_aTilings.back().m_pTilingStream->Write( aTilingStream.getStr(), aTilingStream.getLength() );
   10427             :                 // phase the tiling so wallpaper begins on upper left
   10428           0 :                 m_aTilings.back().m_aTransform.matrix[2] = double(aConvertRect.Left() % aConvertRect.GetWidth()) / fDivisor;
   10429           0 :                 m_aTilings.back().m_aTransform.matrix[5] = double(aConvertRect.Top() % aConvertRect.GetHeight()) / fDivisor;
   10430           0 :                 m_aTilings.back().m_aResources.m_aXObjects[aImageName] = rEmit.m_nObject;
   10431             : 
   10432           0 :                 updateGraphicsState();
   10433             : 
   10434           0 :                 OStringBuffer aObjName( 16 );
   10435           0 :                 aObjName.append( 'P' );
   10436           0 :                 aObjName.append( m_aTilings.back().m_nObject );
   10437           0 :                 OString aPatternName( aObjName.makeStringAndClear() );
   10438           0 :                 pushResource( ResPattern, aPatternName, m_aTilings.back().m_nObject );
   10439             : 
   10440             :                 // fill a rRect with the pattern
   10441           0 :                 OStringBuffer aLine( 100 );
   10442           0 :                 aLine.append( "q /Pattern cs /" );
   10443           0 :                 aLine.append( aPatternName );
   10444           0 :                 aLine.append( " scn " );
   10445           0 :                 m_aPages.back().appendRect( rRect, aLine );
   10446           0 :                 aLine.append( " f Q\n" );
   10447           0 :                 writeBuffer( aLine.getStr(), aLine.getLength() );
   10448             :             }
   10449             :         }
   10450             :         else
   10451             :         {
   10452           0 :             aBmpPos     = aRect.TopLeft();
   10453           0 :             aBmpSize    = aRect.GetSize();
   10454           0 :             bDrawBitmap = true;
   10455             :         }
   10456             : 
   10457           0 :         if( aBitmap.IsTransparent() )
   10458             :         {
   10459           0 :             if( rWall.IsGradient() )
   10460           0 :                 bDrawGradient = true;
   10461             :             else
   10462           0 :                 bDrawColor = true;
   10463             :         }
   10464             :     }
   10465           0 :     else if( rWall.IsGradient() )
   10466           0 :         bDrawGradient = true;
   10467             :     else
   10468           0 :         bDrawColor = true;
   10469             : 
   10470           0 :     if( bDrawGradient )
   10471             :     {
   10472           0 :         drawGradient( rRect, rWall.GetGradient() );
   10473             :     }
   10474           0 :     if( bDrawColor )
   10475             :     {
   10476           0 :         Color aOldLineColor = m_aGraphicsStack.front().m_aLineColor;
   10477           0 :         Color aOldFillColor = m_aGraphicsStack.front().m_aFillColor;
   10478           0 :         setLineColor( Color( COL_TRANSPARENT ) );
   10479           0 :         setFillColor( rWall.GetColor() );
   10480           0 :         drawRectangle( rRect );
   10481           0 :         setLineColor( aOldLineColor );
   10482           0 :         setFillColor( aOldFillColor );
   10483             :     }
   10484           0 :     if( bDrawBitmap )
   10485             :     {
   10486             :         // set temporary clip region since aBmpPos and aBmpSize
   10487             :         // may be outside rRect
   10488           0 :         OStringBuffer aLine( 20 );
   10489           0 :         aLine.append( "q " );
   10490           0 :         m_aPages.back().appendRect( rRect, aLine );
   10491           0 :         aLine.append( " W n\n" );
   10492           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10493           0 :         drawBitmap( aBmpPos, aBmpSize, aBitmap );
   10494           0 :         writeBuffer( "Q\n", 2 );
   10495           0 :     }
   10496           0 : }
   10497             : 
   10498           0 : void PDFWriterImpl::updateGraphicsState(Mode const mode)
   10499             : {
   10500           0 :     OStringBuffer aLine( 256 );
   10501           0 :     GraphicsState& rNewState = m_aGraphicsStack.front();
   10502             :     // first set clip region since it might invalidate everything else
   10503             : 
   10504           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateClipRegion) )
   10505             :     {
   10506           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateClipRegion;
   10507             : 
   10508           0 :         if( m_aCurrentPDFState.m_bClipRegion != rNewState.m_bClipRegion ||
   10509           0 :             ( rNewState.m_bClipRegion && m_aCurrentPDFState.m_aClipRegion != rNewState.m_aClipRegion ) )
   10510             :         {
   10511           0 :             if( m_aCurrentPDFState.m_bClipRegion && m_aCurrentPDFState.m_aClipRegion.count() )
   10512             :             {
   10513           0 :                 aLine.append( "Q " );
   10514             :                 // invalidate everything but the clip region
   10515           0 :                 m_aCurrentPDFState = GraphicsState();
   10516           0 :                 rNewState.m_nUpdateFlags = sal::static_int_cast<sal_uInt16>(~GraphicsState::updateClipRegion);
   10517             :             }
   10518           0 :             if( rNewState.m_bClipRegion && rNewState.m_aClipRegion.count() )
   10519             :             {
   10520             :                 // clip region is always stored in private PDF mapmode
   10521           0 :                 MapMode aNewMapMode = rNewState.m_aMapMode;
   10522           0 :                 rNewState.m_aMapMode = m_aMapMode;
   10523           0 :                 getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10524           0 :                 m_aCurrentPDFState.m_aMapMode = rNewState.m_aMapMode;
   10525             : 
   10526           0 :                 aLine.append( "q " );
   10527           0 :                 m_aPages.back().appendPolyPolygon( rNewState.m_aClipRegion, aLine );
   10528           0 :                 aLine.append( "W* n\n" );
   10529           0 :                 rNewState.m_aMapMode = aNewMapMode;
   10530           0 :                 getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10531           0 :                 m_aCurrentPDFState.m_aMapMode = rNewState.m_aMapMode;
   10532             :             }
   10533             :         }
   10534             :     }
   10535             : 
   10536           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateMapMode) )
   10537             :     {
   10538           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateMapMode;
   10539           0 :         getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10540             :     }
   10541             : 
   10542           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateFont) )
   10543             :     {
   10544           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateFont;
   10545           0 :         getReferenceDevice()->SetFont( rNewState.m_aFont );
   10546           0 :         getReferenceDevice()->ImplNewFont();
   10547             :     }
   10548             : 
   10549           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateLayoutMode) )
   10550             :     {
   10551           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateLayoutMode;
   10552           0 :         getReferenceDevice()->SetLayoutMode( rNewState.m_nLayoutMode );
   10553             :     }
   10554             : 
   10555           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateDigitLanguage) )
   10556             :     {
   10557           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateDigitLanguage;
   10558           0 :         getReferenceDevice()->SetDigitLanguage( rNewState.m_aDigitLanguage );
   10559             :     }
   10560             : 
   10561           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateLineColor) )
   10562             :     {
   10563           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateLineColor;
   10564           0 :         if( m_aCurrentPDFState.m_aLineColor != rNewState.m_aLineColor &&
   10565           0 :             rNewState.m_aLineColor != Color( COL_TRANSPARENT ) )
   10566             :         {
   10567           0 :             appendStrokingColor( rNewState.m_aLineColor, aLine );
   10568           0 :             aLine.append( "\n" );
   10569             :         }
   10570             :     }
   10571             : 
   10572           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateFillColor) )
   10573             :     {
   10574           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateFillColor;
   10575           0 :         if( m_aCurrentPDFState.m_aFillColor != rNewState.m_aFillColor &&
   10576           0 :             rNewState.m_aFillColor != Color( COL_TRANSPARENT ) )
   10577             :         {
   10578           0 :             appendNonStrokingColor( rNewState.m_aFillColor, aLine );
   10579           0 :             aLine.append( "\n" );
   10580             :         }
   10581             :     }
   10582             : 
   10583           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateTransparentPercent) )
   10584             :     {
   10585           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateTransparentPercent;
   10586           0 :         if( m_aContext.Version >= PDFWriter::PDF_1_4 && m_aCurrentPDFState.m_nTransparentPercent != rNewState.m_nTransparentPercent )
   10587             :         {
   10588             :             // TODO: switch extended graphicsstate
   10589             :         }
   10590             :     }
   10591             : 
   10592             :     // everything is up to date now
   10593           0 :     m_aCurrentPDFState = m_aGraphicsStack.front();
   10594           0 :     if ((mode != NOWRITE) &&  !aLine.isEmpty())
   10595           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10596           0 : }
   10597             : 
   10598             : /* #i47544# imitate OutputDevice behaviour:
   10599             : *  if a font with a nontransparent color is set, it overwrites the current
   10600             : *  text color. OTOH setting the text color will overwrite the color of the font.
   10601             : */
   10602           0 : void PDFWriterImpl::setFont( const Font& rFont )
   10603             : {
   10604           0 :     Color aColor = rFont.GetColor();
   10605           0 :     if( aColor == Color( COL_TRANSPARENT ) )
   10606           0 :         aColor = m_aGraphicsStack.front().m_aFont.GetColor();
   10607           0 :     m_aGraphicsStack.front().m_aFont = rFont;
   10608           0 :     m_aGraphicsStack.front().m_aFont.SetColor( aColor );
   10609           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateFont;
   10610           0 : }
   10611             : 
   10612           0 : void PDFWriterImpl::push( sal_uInt16 nFlags )
   10613             : {
   10614             :     OSL_ENSURE( !m_aGraphicsStack.empty(), "invalid graphics stack" );
   10615           0 :     m_aGraphicsStack.push_front( m_aGraphicsStack.front() );
   10616           0 :     m_aGraphicsStack.front().m_nFlags = nFlags;
   10617           0 : }
   10618             : 
   10619           0 : void PDFWriterImpl::pop()
   10620             : {
   10621             :     OSL_ENSURE( m_aGraphicsStack.size() > 1, "pop without push" );
   10622           0 :     if( m_aGraphicsStack.size() < 2 )
   10623           0 :         return;
   10624             : 
   10625           0 :     GraphicsState aState = m_aGraphicsStack.front();
   10626           0 :     m_aGraphicsStack.pop_front();
   10627           0 :     GraphicsState& rOld = m_aGraphicsStack.front();
   10628             : 
   10629             :     // move those parameters back that were not pushed
   10630             :     // in the first place
   10631           0 :     if( ! (aState.m_nFlags & PUSH_LINECOLOR) )
   10632           0 :         setLineColor( aState.m_aLineColor );
   10633           0 :     if( ! (aState.m_nFlags & PUSH_FILLCOLOR) )
   10634           0 :         setFillColor( aState.m_aFillColor );
   10635           0 :     if( ! (aState.m_nFlags & PUSH_FONT) )
   10636           0 :         setFont( aState.m_aFont );
   10637           0 :     if( ! (aState.m_nFlags & PUSH_TEXTCOLOR) )
   10638           0 :         setTextColor( aState.m_aFont.GetColor() );
   10639           0 :     if( ! (aState.m_nFlags & PUSH_MAPMODE) )
   10640           0 :         setMapMode( aState.m_aMapMode );
   10641           0 :     if( ! (aState.m_nFlags & PUSH_CLIPREGION) )
   10642             :     {
   10643             :         // do not use setClipRegion here
   10644             :         // it would convert again assuming the current mapmode
   10645           0 :         rOld.m_aClipRegion = aState.m_aClipRegion;
   10646           0 :         rOld.m_bClipRegion = aState.m_bClipRegion;
   10647             :     }
   10648           0 :     if( ! (aState.m_nFlags & PUSH_TEXTLINECOLOR ) )
   10649           0 :         setTextLineColor( aState.m_aTextLineColor );
   10650           0 :     if( ! (aState.m_nFlags & PUSH_OVERLINECOLOR ) )
   10651           0 :         setOverlineColor( aState.m_aOverlineColor );
   10652           0 :     if( ! (aState.m_nFlags & PUSH_TEXTALIGN ) )
   10653           0 :         setTextAlign( aState.m_aFont.GetAlign() );
   10654           0 :     if( ! (aState.m_nFlags & PUSH_TEXTFILLCOLOR) )
   10655           0 :         setTextFillColor( aState.m_aFont.GetFillColor() );
   10656           0 :     if( ! (aState.m_nFlags & PUSH_REFPOINT) )
   10657             :     {
   10658             :         // what ?
   10659             :     }
   10660             :     // invalidate graphics state
   10661           0 :     m_aGraphicsStack.front().m_nUpdateFlags = sal::static_int_cast<sal_uInt16>(~0U);
   10662             : }
   10663             : 
   10664           0 : void PDFWriterImpl::setMapMode( const MapMode& rMapMode )
   10665             : {
   10666           0 :     m_aGraphicsStack.front().m_aMapMode = rMapMode;
   10667           0 :     getReferenceDevice()->SetMapMode( rMapMode );
   10668           0 :     m_aCurrentPDFState.m_aMapMode = rMapMode;
   10669           0 : }
   10670             : 
   10671           0 : void PDFWriterImpl::setClipRegion( const basegfx::B2DPolyPolygon& rRegion )
   10672             : {
   10673           0 :     basegfx::B2DPolyPolygon aRegion = getReferenceDevice()->LogicToPixel( rRegion, m_aGraphicsStack.front().m_aMapMode );
   10674           0 :     aRegion = getReferenceDevice()->PixelToLogic( aRegion, m_aMapMode );
   10675           0 :     m_aGraphicsStack.front().m_aClipRegion = aRegion;
   10676           0 :     m_aGraphicsStack.front().m_bClipRegion = true;
   10677           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10678           0 : }
   10679             : 
   10680           0 : void PDFWriterImpl::moveClipRegion( sal_Int32 nX, sal_Int32 nY )
   10681             : {
   10682           0 :     if( m_aGraphicsStack.front().m_bClipRegion && m_aGraphicsStack.front().m_aClipRegion.count() )
   10683             :     {
   10684           0 :         Point aPoint( lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10685             :                                    m_aMapMode,
   10686             :                                    getReferenceDevice(),
   10687           0 :                                    Point( nX, nY ) ) );
   10688           0 :         aPoint -= lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10689             :                                m_aMapMode,
   10690             :                                getReferenceDevice(),
   10691           0 :                                Point() );
   10692           0 :         basegfx::B2DHomMatrix aMat;
   10693           0 :         aMat.translate( aPoint.X(), aPoint.Y() );
   10694           0 :         m_aGraphicsStack.front().m_aClipRegion.transform( aMat );
   10695           0 :         m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10696             :     }
   10697           0 : }
   10698             : 
   10699           0 : bool PDFWriterImpl::intersectClipRegion( const Rectangle& rRect )
   10700             : {
   10701             :     basegfx::B2DPolyPolygon aRect( basegfx::tools::createPolygonFromRect(
   10702           0 :         basegfx::B2DRectangle( rRect.Left(), rRect.Top(), rRect.Right(), rRect.Bottom() ) ) );
   10703           0 :     return intersectClipRegion( aRect );
   10704             : }
   10705             : 
   10706           0 : bool PDFWriterImpl::intersectClipRegion( const basegfx::B2DPolyPolygon& rRegion )
   10707             : {
   10708           0 :     basegfx::B2DPolyPolygon aRegion( getReferenceDevice()->LogicToPixel( rRegion, m_aGraphicsStack.front().m_aMapMode ) );
   10709           0 :     aRegion = getReferenceDevice()->PixelToLogic( aRegion, m_aMapMode );
   10710           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10711           0 :     if( m_aGraphicsStack.front().m_bClipRegion )
   10712             :     {
   10713           0 :         basegfx::B2DPolyPolygon aOld( basegfx::tools::prepareForPolygonOperation( m_aGraphicsStack.front().m_aClipRegion ) );
   10714           0 :         aRegion = basegfx::tools::prepareForPolygonOperation( aRegion );
   10715           0 :         m_aGraphicsStack.front().m_aClipRegion = basegfx::tools::solvePolygonOperationAnd( aOld, aRegion );
   10716             :     }
   10717             :     else
   10718             :     {
   10719           0 :         m_aGraphicsStack.front().m_aClipRegion = aRegion;
   10720           0 :         m_aGraphicsStack.front().m_bClipRegion = true;
   10721             :     }
   10722           0 :     return true;
   10723             : }
   10724             : 
   10725           0 : void PDFWriterImpl::createNote( const Rectangle& rRect, const PDFNote& rNote, sal_Int32 nPageNr )
   10726             : {
   10727           0 :     if( nPageNr < 0 )
   10728           0 :         nPageNr = m_nCurrentPage;
   10729             : 
   10730           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10731           0 :         return;
   10732             : 
   10733           0 :     m_aNotes.push_back( PDFNoteEntry() );
   10734           0 :     m_aNotes.back().m_nObject       = createObject();
   10735           0 :     m_aNotes.back().m_aContents     = rNote;
   10736           0 :     m_aNotes.back().m_aRect         = rRect;
   10737             :     // convert to default user space now, since the mapmode may change
   10738           0 :     m_aPages[nPageNr].convertRect( m_aNotes.back().m_aRect );
   10739             : 
   10740             :     // insert note to page's annotation list
   10741           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( m_aNotes.back().m_nObject );
   10742             : }
   10743             : 
   10744           0 : sal_Int32 PDFWriterImpl::createLink( const Rectangle& rRect, sal_Int32 nPageNr )
   10745             : {
   10746           0 :     if( nPageNr < 0 )
   10747           0 :         nPageNr = m_nCurrentPage;
   10748             : 
   10749           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10750           0 :         return -1;
   10751             : 
   10752           0 :     sal_Int32 nRet = m_aLinks.size();
   10753             : 
   10754           0 :     m_aLinks.push_back( PDFLink() );
   10755           0 :     m_aLinks.back().m_nObject   = createObject();
   10756           0 :     m_aLinks.back().m_nPage     = nPageNr;
   10757           0 :     m_aLinks.back().m_aRect     = rRect;
   10758             :     // convert to default user space now, since the mapmode may change
   10759           0 :     m_aPages[nPageNr].convertRect( m_aLinks.back().m_aRect );
   10760             : 
   10761             :     // insert link to page's annotation list
   10762           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( m_aLinks.back().m_nObject );
   10763             : 
   10764           0 :     return nRet;
   10765             : }
   10766             : 
   10767             : //--->i56629
   10768           0 : sal_Int32 PDFWriterImpl::createNamedDest( const OUString& sDestName, const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10769             : {
   10770           0 :     if( nPageNr < 0 )
   10771           0 :         nPageNr = m_nCurrentPage;
   10772             : 
   10773           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10774           0 :         return -1;
   10775             : 
   10776           0 :     sal_Int32 nRet = m_aNamedDests.size();
   10777             : 
   10778           0 :     m_aNamedDests.push_back( PDFNamedDest() );
   10779           0 :     m_aNamedDests.back().m_aDestName = sDestName;
   10780           0 :     m_aNamedDests.back().m_nPage = nPageNr;
   10781           0 :     m_aNamedDests.back().m_eType = eType;
   10782           0 :     m_aNamedDests.back().m_aRect = rRect;
   10783             :     // convert to default user space now, since the mapmode may change
   10784           0 :     m_aPages[nPageNr].convertRect( m_aNamedDests.back().m_aRect );
   10785             : 
   10786           0 :     return nRet;
   10787             : }
   10788             : //<---i56629
   10789             : 
   10790           0 : sal_Int32 PDFWriterImpl::createDest( const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10791             : {
   10792           0 :     if( nPageNr < 0 )
   10793           0 :         nPageNr = m_nCurrentPage;
   10794             : 
   10795           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10796           0 :         return -1;
   10797             : 
   10798           0 :     sal_Int32 nRet = m_aDests.size();
   10799             : 
   10800           0 :     m_aDests.push_back( PDFDest() );
   10801           0 :     m_aDests.back().m_nPage = nPageNr;
   10802           0 :     m_aDests.back().m_eType = eType;
   10803           0 :     m_aDests.back().m_aRect = rRect;
   10804             :     // convert to default user space now, since the mapmode may change
   10805           0 :     m_aPages[nPageNr].convertRect( m_aDests.back().m_aRect );
   10806             : 
   10807           0 :     return nRet;
   10808             : }
   10809             : 
   10810           0 : sal_Int32 PDFWriterImpl::registerDestReference( sal_Int32 nDestId, const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10811             : {
   10812           0 :     return m_aDestinationIdTranslation[ nDestId ] = createDest( rRect, nPageNr, eType );
   10813             : }
   10814             : 
   10815           0 : sal_Int32 PDFWriterImpl::setLinkDest( sal_Int32 nLinkId, sal_Int32 nDestId )
   10816             : {
   10817           0 :     if( nLinkId < 0 || nLinkId >= (sal_Int32)m_aLinks.size() )
   10818           0 :         return -1;
   10819           0 :     if( nDestId < 0 || nDestId >= (sal_Int32)m_aDests.size() )
   10820           0 :         return -2;
   10821             : 
   10822           0 :     m_aLinks[ nLinkId ].m_nDest = nDestId;
   10823             : 
   10824           0 :     return 0;
   10825             : }
   10826             : 
   10827           0 : sal_Int32 PDFWriterImpl::setLinkURL( sal_Int32 nLinkId, const OUString& rURL )
   10828             : {
   10829           0 :     if( nLinkId < 0 || nLinkId >= (sal_Int32)m_aLinks.size() )
   10830           0 :         return -1;
   10831             : 
   10832           0 :     m_aLinks[ nLinkId ].m_nDest = -1;
   10833             : 
   10834             :     using namespace ::com::sun::star;
   10835             : 
   10836           0 :     if (!m_xTrans.is())
   10837             :     {
   10838           0 :         uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
   10839           0 :         m_xTrans = util::URLTransformer::create(xContext);;
   10840             :     }
   10841             : 
   10842           0 :     util::URL aURL;
   10843           0 :     aURL.Complete = rURL;
   10844             : 
   10845           0 :     m_xTrans->parseStrict( aURL );
   10846             : 
   10847           0 :     m_aLinks[ nLinkId ].m_aURL  = aURL.Complete;
   10848             : 
   10849           0 :     return 0;
   10850             : }
   10851             : 
   10852           0 : void PDFWriterImpl::setLinkPropertyId( sal_Int32 nLinkId, sal_Int32 nPropertyId )
   10853             : {
   10854           0 :     m_aLinkPropertyMap[ nPropertyId ] = nLinkId;
   10855           0 : }
   10856             : 
   10857           0 : sal_Int32 PDFWriterImpl::createOutlineItem( sal_Int32 nParent, const OUString& rText, sal_Int32 nDestID )
   10858             : {
   10859             :     // create new item
   10860           0 :     sal_Int32 nNewItem = m_aOutline.size();
   10861           0 :     m_aOutline.push_back( PDFOutlineEntry() );
   10862             : 
   10863             :     // set item attributes
   10864           0 :     setOutlineItemParent( nNewItem, nParent );
   10865           0 :     setOutlineItemText( nNewItem, rText );
   10866           0 :     setOutlineItemDest( nNewItem, nDestID );
   10867             : 
   10868           0 :     return nNewItem;
   10869             : }
   10870             : 
   10871           0 : sal_Int32 PDFWriterImpl::setOutlineItemParent( sal_Int32 nItem, sal_Int32 nNewParent )
   10872             : {
   10873           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() )
   10874           0 :         return -1;
   10875             : 
   10876           0 :     int nRet = 0;
   10877             : 
   10878           0 :     if( nNewParent < 0 || nNewParent >= (sal_Int32)m_aOutline.size() || nNewParent == nItem )
   10879             :     {
   10880           0 :         nNewParent = 0;
   10881           0 :         nRet = -2;
   10882             :     }
   10883             :     // remove item from previous parent
   10884           0 :     sal_Int32 nParentID = m_aOutline[ nItem ].m_nParentID;
   10885           0 :     if( nParentID >= 0 && nParentID < (sal_Int32)m_aOutline.size() )
   10886             :     {
   10887           0 :         PDFOutlineEntry& rParent = m_aOutline[ nParentID ];
   10888             : 
   10889           0 :         for( std::vector<sal_Int32>::iterator it = rParent.m_aChildren.begin();
   10890           0 :              it != rParent.m_aChildren.end(); ++it )
   10891             :         {
   10892           0 :             if( *it == nItem )
   10893             :             {
   10894           0 :                 rParent.m_aChildren.erase( it );
   10895           0 :                 break;
   10896             :             }
   10897             :         }
   10898             :     }
   10899             : 
   10900             :     // insert item to new parent's list of children
   10901           0 :     m_aOutline[ nNewParent ].m_aChildren.push_back( nItem );
   10902             : 
   10903           0 :     return nRet;
   10904             : }
   10905             : 
   10906           0 : sal_Int32 PDFWriterImpl::setOutlineItemText( sal_Int32 nItem, const OUString& rText )
   10907             : {
   10908           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() )
   10909           0 :         return -1;
   10910             : 
   10911           0 :     m_aOutline[ nItem ].m_aTitle = psp::WhitespaceToSpace( rText );
   10912           0 :     return 0;
   10913             : }
   10914             : 
   10915           0 : sal_Int32 PDFWriterImpl::setOutlineItemDest( sal_Int32 nItem, sal_Int32 nDestID )
   10916             : {
   10917           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() ) // item does not exist
   10918           0 :         return -1;
   10919           0 :     if( nDestID < 0 || nDestID >= (sal_Int32)m_aDests.size() ) // dest does not exist
   10920           0 :         return -2;
   10921           0 :     m_aOutline[nItem].m_nDestID = nDestID;
   10922           0 :     return 0;
   10923             : }
   10924             : 
   10925           0 : const sal_Char* PDFWriterImpl::getStructureTag( PDFWriter::StructElement eType )
   10926             : {
   10927           0 :     static std::map< PDFWriter::StructElement, const char* > aTagStrings;
   10928           0 :     if( aTagStrings.empty() )
   10929             :     {
   10930           0 :         aTagStrings[ PDFWriter::NonStructElement] = "NonStruct";
   10931           0 :         aTagStrings[ PDFWriter::Document ]      = "Document";
   10932           0 :         aTagStrings[ PDFWriter::Part ]          = "Part";
   10933           0 :         aTagStrings[ PDFWriter::Article ]       = "Art";
   10934           0 :         aTagStrings[ PDFWriter::Section ]       = "Sect";
   10935           0 :         aTagStrings[ PDFWriter::Division ]      = "Div";
   10936           0 :         aTagStrings[ PDFWriter::BlockQuote ]    = "BlockQuote";
   10937           0 :         aTagStrings[ PDFWriter::Caption ]       = "Caption";
   10938           0 :         aTagStrings[ PDFWriter::TOC ]           = "TOC";
   10939           0 :         aTagStrings[ PDFWriter::TOCI ]          = "TOCI";
   10940           0 :         aTagStrings[ PDFWriter::Index ]         = "Index";
   10941           0 :         aTagStrings[ PDFWriter::Paragraph ]     = "P";
   10942           0 :         aTagStrings[ PDFWriter::Heading ]       = "H";
   10943           0 :         aTagStrings[ PDFWriter::H1 ]            = "H1";
   10944           0 :         aTagStrings[ PDFWriter::H2 ]            = "H2";
   10945           0 :         aTagStrings[ PDFWriter::H3 ]            = "H3";
   10946           0 :         aTagStrings[ PDFWriter::H4 ]            = "H4";
   10947           0 :         aTagStrings[ PDFWriter::H5 ]            = "H5";
   10948           0 :         aTagStrings[ PDFWriter::H6 ]            = "H6";
   10949           0 :         aTagStrings[ PDFWriter::List ]          = "L";
   10950           0 :         aTagStrings[ PDFWriter::ListItem ]      = "LI";
   10951           0 :         aTagStrings[ PDFWriter::LILabel ]       = "Lbl";
   10952           0 :         aTagStrings[ PDFWriter::LIBody ]        = "LBody";
   10953           0 :         aTagStrings[ PDFWriter::Table ]         = "Table";
   10954           0 :         aTagStrings[ PDFWriter::TableRow ]      = "TR";
   10955           0 :         aTagStrings[ PDFWriter::TableHeader ]   = "TH";
   10956           0 :         aTagStrings[ PDFWriter::TableData ]     = "TD";
   10957           0 :         aTagStrings[ PDFWriter::Span ]          = "Span";
   10958           0 :         aTagStrings[ PDFWriter::Quote ]         = "Quote";
   10959           0 :         aTagStrings[ PDFWriter::Note ]          = "Note";
   10960           0 :         aTagStrings[ PDFWriter::Reference ]     = "Reference";
   10961           0 :         aTagStrings[ PDFWriter::BibEntry ]      = "BibEntry";
   10962           0 :         aTagStrings[ PDFWriter::Code ]          = "Code";
   10963           0 :         aTagStrings[ PDFWriter::Link ]          = "Link";
   10964           0 :         aTagStrings[ PDFWriter::Figure ]        = "Figure";
   10965           0 :         aTagStrings[ PDFWriter::Formula ]       = "Formula";
   10966           0 :         aTagStrings[ PDFWriter::Form ]          = "Form";
   10967             :     }
   10968             : 
   10969           0 :     std::map< PDFWriter::StructElement, const char* >::const_iterator it = aTagStrings.find( eType );
   10970             : 
   10971           0 :     return it != aTagStrings.end() ? it->second : "Div";
   10972             : }
   10973             : 
   10974           0 : void PDFWriterImpl::beginStructureElementMCSeq()
   10975             : {
   10976           0 :     if( m_bEmitStructure &&
   10977           0 :         m_nCurrentStructElement > 0 && // StructTreeRoot
   10978           0 :         ! m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // already opened sequence
   10979             :         )
   10980             :     {
   10981           0 :         PDFStructureElement& rEle = m_aStructure[ m_nCurrentStructElement ];
   10982           0 :         OStringBuffer aLine( 128 );
   10983           0 :         sal_Int32 nMCID = m_aPages[ m_nCurrentPage ].m_aMCIDParents.size();
   10984           0 :         aLine.append( "/" );
   10985           0 :         if( !rEle.m_aAlias.isEmpty() )
   10986           0 :             aLine.append( rEle.m_aAlias );
   10987             :         else
   10988           0 :             aLine.append( getStructureTag( rEle.m_eType ) );
   10989           0 :         aLine.append( "<</MCID " );
   10990           0 :         aLine.append( nMCID );
   10991           0 :         aLine.append( ">>BDC\n" );
   10992           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10993             : 
   10994             :         // update the element's content list
   10995             : #if OSL_DEBUG_LEVEL > 1
   10996             :         fprintf( stderr, "beginning marked content id %" SAL_PRIdINT32 " on page object %" SAL_PRIdINT32 ", structure first page = %" SAL_PRIdINT32 "\n",
   10997             :                  nMCID,
   10998             :                  m_aPages[ m_nCurrentPage ].m_nPageObject,
   10999             :                  rEle.m_nFirstPageObject );
   11000             : #endif
   11001           0 :         rEle.m_aKids.push_back( PDFStructureElementKid( nMCID, m_aPages[m_nCurrentPage].m_nPageObject ) );
   11002             :         // update the page's mcid parent list
   11003           0 :         m_aPages[ m_nCurrentPage ].m_aMCIDParents.push_back( rEle.m_nObject );
   11004             :         // mark element MC sequence as open
   11005           0 :         rEle.m_bOpenMCSeq = true;
   11006             :     }
   11007             :     // handle artifacts
   11008           0 :     else if( ! m_bEmitStructure && m_aContext.Tagged &&
   11009           0 :                m_nCurrentStructElement > 0 &&
   11010           0 :                m_aStructure[ m_nCurrentStructElement ].m_eType == PDFWriter::NonStructElement &&
   11011           0 :              ! m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // already opened sequence
   11012             :              )
   11013             :     {
   11014           0 :         OStringBuffer aLine( 128 );
   11015           0 :         aLine.append( "/Artifact BMC\n" );
   11016           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   11017             :         // mark element MC sequence as open
   11018           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq = true;
   11019             :     }
   11020           0 : }
   11021             : 
   11022           0 : void PDFWriterImpl::endStructureElementMCSeq()
   11023             : {
   11024           0 :     if( m_nCurrentStructElement > 0 && // StructTreeRoot
   11025           0 :         ( m_bEmitStructure || m_aStructure[ m_nCurrentStructElement ].m_eType == PDFWriter::NonStructElement ) &&
   11026           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // must have an opened MC sequence
   11027             :         )
   11028             :     {
   11029           0 :         writeBuffer( "EMC\n", 4 );
   11030           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq = false;
   11031             :     }
   11032           0 : }
   11033             : 
   11034           0 : bool PDFWriterImpl::checkEmitStructure()
   11035             : {
   11036           0 :     bool bEmit = false;
   11037           0 :     if( m_aContext.Tagged )
   11038             :     {
   11039           0 :         bEmit = true;
   11040           0 :         sal_Int32 nEle = m_nCurrentStructElement;
   11041           0 :         while( nEle > 0 && nEle < sal_Int32(m_aStructure.size()) )
   11042             :         {
   11043           0 :             if( m_aStructure[ nEle ].m_eType == PDFWriter::NonStructElement )
   11044             :             {
   11045           0 :                 bEmit = false;
   11046           0 :                 break;
   11047             :             }
   11048           0 :             nEle = m_aStructure[ nEle ].m_nParentElement;
   11049             :         }
   11050             :     }
   11051           0 :     return bEmit;
   11052             : }
   11053             : 
   11054           0 : sal_Int32 PDFWriterImpl::beginStructureElement( PDFWriter::StructElement eType, const OUString& rAlias )
   11055             : {
   11056           0 :     if( m_nCurrentPage < 0 )
   11057           0 :         return -1;
   11058             : 
   11059           0 :     if( ! m_aContext.Tagged )
   11060           0 :         return -1;
   11061             : 
   11062             :     // close eventual current MC sequence
   11063           0 :     endStructureElementMCSeq();
   11064             : 
   11065           0 :     if( m_nCurrentStructElement == 0 &&
   11066           0 :         eType != PDFWriter::Document && eType != PDFWriter::NonStructElement )
   11067             :     {
   11068             :         // struct tree root hit, but not beginning document
   11069             :         // this might happen with setCurrentStructureElement
   11070             :         // silently insert structure into document again if one properly exists
   11071           0 :         if( ! m_aStructure[ 0 ].m_aChildren.empty() )
   11072             :         {
   11073           0 :             PDFWriter::StructElement childType = PDFWriter::NonStructElement;
   11074           0 :             sal_Int32 nNewCurElement = 0;
   11075           0 :             const std::list< sal_Int32 >& rRootChildren = m_aStructure[0].m_aChildren;
   11076           0 :             for( std::list< sal_Int32 >::const_iterator it = rRootChildren.begin();
   11077           0 :                  childType != PDFWriter::Document && it != rRootChildren.end(); ++it )
   11078             :             {
   11079           0 :                 nNewCurElement = *it;
   11080           0 :                 childType = m_aStructure[ nNewCurElement ].m_eType;
   11081             :             }
   11082           0 :             if( childType == PDFWriter::Document )
   11083             :             {
   11084           0 :                 m_nCurrentStructElement = nNewCurElement;
   11085             :                 DBG_ASSERT( false, "Structure element inserted to StructTreeRoot that is not a document" );
   11086             :             }
   11087             :             else {
   11088             :                 OSL_FAIL( "document structure in disorder !" );
   11089             :             }
   11090             :         }
   11091             :         else {
   11092             :             OSL_FAIL( "PDF document structure MUST be contained in a Document element" );
   11093             :         }
   11094             :     }
   11095             : 
   11096           0 :     sal_Int32 nNewId = sal_Int32(m_aStructure.size());
   11097           0 :     m_aStructure.push_back( PDFStructureElement() );
   11098           0 :     PDFStructureElement& rEle = m_aStructure.back();
   11099           0 :     rEle.m_eType            = eType;
   11100           0 :     rEle.m_nOwnElement      = nNewId;
   11101           0 :     rEle.m_nParentElement   = m_nCurrentStructElement;
   11102           0 :     rEle.m_nFirstPageObject = m_aPages[ m_nCurrentPage ].m_nPageObject;
   11103           0 :     m_aStructure[ m_nCurrentStructElement ].m_aChildren.push_back( nNewId );
   11104           0 :     m_nCurrentStructElement = nNewId;
   11105             : 
   11106             :     // handle alias names
   11107           0 :     if( !rAlias.isEmpty() && eType != PDFWriter::NonStructElement )
   11108             :     {
   11109           0 :         OStringBuffer aNameBuf( rAlias.getLength() );
   11110           0 :         appendName( rAlias, aNameBuf );
   11111           0 :         OString aAliasName( aNameBuf.makeStringAndClear() );
   11112           0 :         rEle.m_aAlias = aAliasName;
   11113           0 :         m_aRoleMap[ aAliasName ] = getStructureTag( eType );
   11114             :     }
   11115             : 
   11116             : #if OSL_DEBUG_LEVEL > 1
   11117             :     OStringBuffer aLine( "beginStructureElement " );
   11118             :     aLine.append( m_nCurrentStructElement );
   11119             :     aLine.append( ": " );
   11120             :     aLine.append( getStructureTag( eType ) );
   11121             :     if( !rEle.m_aAlias.isEmpty() )
   11122             :     {
   11123             :         aLine.append( " aliased as \"" );
   11124             :         aLine.append( rEle.m_aAlias );
   11125             :         aLine.append( '\"' );
   11126             :     }
   11127             :     emitComment( aLine.getStr() );
   11128             : #endif
   11129             : 
   11130             :     // check whether to emit structure henceforth
   11131           0 :     m_bEmitStructure = checkEmitStructure();
   11132             : 
   11133           0 :     if( m_bEmitStructure ) // don't create nonexistant objects
   11134             :     {
   11135           0 :         rEle.m_nObject      = createObject();
   11136             :         // update parent's kids list
   11137           0 :         m_aStructure[ rEle.m_nParentElement ].m_aKids.push_back( rEle.m_nObject );
   11138             :     }
   11139           0 :     return nNewId;
   11140             : }
   11141             : 
   11142           0 : void PDFWriterImpl::endStructureElement()
   11143             : {
   11144           0 :     if( m_nCurrentPage < 0 )
   11145           0 :         return;
   11146             : 
   11147           0 :     if( ! m_aContext.Tagged )
   11148           0 :         return;
   11149             : 
   11150           0 :     if( m_nCurrentStructElement == 0 )
   11151             :     {
   11152             :         // hit the struct tree root, that means there is an endStructureElement
   11153             :         // without corresponding beginStructureElement
   11154           0 :         return;
   11155             :     }
   11156             : 
   11157             :     // end the marked content sequence
   11158           0 :     endStructureElementMCSeq();
   11159             : 
   11160             : #if OSL_DEBUG_LEVEL > 1
   11161             :     OStringBuffer aLine( "endStructureElement " );
   11162             :     aLine.append( m_nCurrentStructElement );
   11163             :     aLine.append( ": " );
   11164             :     aLine.append( getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ) );
   11165             :     if( !m_aStructure[ m_nCurrentStructElement ].m_aAlias.isEmpty() )
   11166             :     {
   11167             :         aLine.append( " aliased as \"" );
   11168             :         aLine.append( m_aStructure[ m_nCurrentStructElement ].m_aAlias );
   11169             :         aLine.append( '\"' );
   11170             :     }
   11171             : #endif
   11172             : 
   11173             :     // "end" the structure element, the parent becomes current element
   11174           0 :     m_nCurrentStructElement = m_aStructure[ m_nCurrentStructElement ].m_nParentElement;
   11175             : 
   11176             :     // check whether to emit structure henceforth
   11177           0 :     m_bEmitStructure = checkEmitStructure();
   11178             : 
   11179             : #if OSL_DEBUG_LEVEL > 1
   11180             :     if( m_bEmitStructure )
   11181             :         emitComment( aLine.getStr() );
   11182             : #endif
   11183             : }
   11184             : 
   11185             : //---> i94258
   11186             : /*
   11187             :  * This function adds an internal structure list container to overcome the 8191 elements array limitation
   11188             :  * in kids element emission.
   11189             :  * Recursive function
   11190             :  *
   11191             :  */
   11192           0 : void PDFWriterImpl::addInternalStructureContainer( PDFStructureElement& rEle )
   11193             : {
   11194           0 :     if( rEle.m_eType == PDFWriter::NonStructElement &&
   11195           0 :         rEle.m_nOwnElement != rEle.m_nParentElement )
   11196           0 :         return;
   11197             : 
   11198           0 :     for( std::list< sal_Int32 >::const_iterator it = rEle.m_aChildren.begin(); it != rEle.m_aChildren.end(); ++it )
   11199             :     {
   11200           0 :         if( *it > 0 && *it < sal_Int32(m_aStructure.size()) )
   11201             :         {
   11202           0 :             PDFStructureElement& rChild = m_aStructure[ *it ];
   11203           0 :             if( rChild.m_eType != PDFWriter::NonStructElement )
   11204             :             {
   11205             :                 //triggered when a child of the rEle element is found
   11206           0 :                 if( rChild.m_nParentElement == rEle.m_nOwnElement )
   11207           0 :                     addInternalStructureContainer( rChild );//examine the child
   11208             :                 else
   11209             :                 {
   11210             :                     OSL_FAIL( "PDFWriterImpl::addInternalStructureContainer: invalid child structure element" );
   11211             : #if OSL_DEBUG_LEVEL > 1
   11212             :                     fprintf( stderr, "PDFWriterImpl::addInternalStructureContainer: invalid child structure elemnt with id %" SAL_PRIdINT32 "\n", *it );
   11213             : #endif
   11214             :                 }
   11215             :             }
   11216             :         }
   11217             :         else
   11218             :         {
   11219             :             OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure id" );
   11220             : #if OSL_DEBUG_LEVEL > 1
   11221             :             fprintf( stderr, "PDFWriterImpl::addInternalStructureContainer: invalid child structure id %" SAL_PRIdINT32 "\n", *it );
   11222             : #endif
   11223             :         }
   11224             :     }
   11225             : 
   11226           0 :     if( rEle.m_nOwnElement != rEle.m_nParentElement )
   11227             :     {
   11228           0 :         if( !rEle.m_aKids.empty() )
   11229             :         {
   11230           0 :             if( rEle.m_aKids.size() > ncMaxPDFArraySize ) {
   11231             :                 //then we need to add the containers for the kids elements
   11232             :                 // a list to be used for the new kid element
   11233           0 :                 std::list< PDFStructureElementKid > aNewKids;
   11234           0 :                 std::list< sal_Int32 > aNewChildren;
   11235             : 
   11236             :                 // add Div in RoleMap, in case no one else did (TODO: is it needed? Is it dangerous?)
   11237           0 :                 OStringBuffer aNameBuf( "Div" );
   11238           0 :                 OString aAliasName( aNameBuf.makeStringAndClear() );
   11239           0 :                 m_aRoleMap[ aAliasName ] = getStructureTag( PDFWriter::Division );
   11240             : 
   11241           0 :                 while( rEle.m_aKids.size() > ncMaxPDFArraySize )
   11242             :                 {
   11243           0 :                     sal_Int32 nCurrentStructElement = rEle.m_nOwnElement;
   11244           0 :                     sal_Int32 nNewId = sal_Int32(m_aStructure.size());
   11245           0 :                     m_aStructure.push_back( PDFStructureElement() );
   11246           0 :                     PDFStructureElement& rEleNew = m_aStructure.back();
   11247           0 :                     rEleNew.m_aAlias            = aAliasName;
   11248           0 :                     rEleNew.m_eType             = PDFWriter::Division; // a new Div type container
   11249           0 :                     rEleNew.m_nOwnElement       = nNewId;
   11250           0 :                     rEleNew.m_nParentElement    = nCurrentStructElement;
   11251             :                     //inherit the same page as the first child to be reparented
   11252           0 :                     rEleNew.m_nFirstPageObject  = m_aStructure[ rEle.m_aChildren.front() ].m_nFirstPageObject;
   11253           0 :                     rEleNew.m_nObject           = createObject();//assign a PDF object number
   11254             :                     //add the object to the kid list of the parent
   11255           0 :                     aNewKids.push_back( PDFStructureElementKid( rEleNew.m_nObject ) );
   11256           0 :                     aNewChildren.push_back( nNewId );
   11257             : 
   11258           0 :                     std::list< sal_Int32 >::iterator aChildEndIt( rEle.m_aChildren.begin() );
   11259           0 :                     std::list< PDFStructureElementKid >::iterator aKidEndIt( rEle.m_aKids.begin() );
   11260           0 :                     advance( aChildEndIt, ncMaxPDFArraySize );
   11261           0 :                     advance( aKidEndIt, ncMaxPDFArraySize );
   11262             : 
   11263             :                     rEleNew.m_aKids.splice( rEleNew.m_aKids.begin(),
   11264             :                                             rEle.m_aKids,
   11265             :                                             rEle.m_aKids.begin(),
   11266           0 :                                             aKidEndIt );
   11267             :                     rEleNew.m_aChildren.splice( rEleNew.m_aChildren.begin(),
   11268             :                                                 rEle.m_aChildren,
   11269             :                                                 rEle.m_aChildren.begin(),
   11270           0 :                                                 aChildEndIt );
   11271             :                     // set the kid's new parent
   11272           0 :                     for( std::list< sal_Int32 >::const_iterator it = rEleNew.m_aChildren.begin();
   11273           0 :                          it != rEleNew.m_aChildren.end(); ++it )
   11274             :                     {
   11275           0 :                         m_aStructure[ *it ].m_nParentElement = nNewId;
   11276             :                     }
   11277             :                 }
   11278             :                 //finally add the new kids resulting from the container added
   11279           0 :                 rEle.m_aKids.insert( rEle.m_aKids.begin(), aNewKids.begin(), aNewKids.end() );
   11280           0 :                 rEle.m_aChildren.insert( rEle.m_aChildren.begin(), aNewChildren.begin(), aNewChildren.end() );
   11281             :             }
   11282             :         }
   11283             :     }
   11284             : }
   11285             : //<--- i94258
   11286             : 
   11287           0 : bool PDFWriterImpl::setCurrentStructureElement( sal_Int32 nEle )
   11288             : {
   11289           0 :     bool bSuccess = false;
   11290             : 
   11291           0 :     if( m_aContext.Tagged && nEle >= 0 && nEle < sal_Int32(m_aStructure.size()) )
   11292             :     {
   11293             :         // end eventual previous marked content sequence
   11294           0 :         endStructureElementMCSeq();
   11295             : 
   11296           0 :         m_nCurrentStructElement = nEle;
   11297           0 :         m_bEmitStructure = checkEmitStructure();
   11298             : #if OSL_DEBUG_LEVEL > 1
   11299             :         OStringBuffer aLine( "setCurrentStructureElement " );
   11300             :         aLine.append( m_nCurrentStructElement );
   11301             :         aLine.append( ": " );
   11302             :         aLine.append( getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ) );
   11303             :         if( !m_aStructure[ m_nCurrentStructElement ].m_aAlias.isEmpty() )
   11304             :         {
   11305             :             aLine.append( " aliased as \"" );
   11306             :             aLine.append( m_aStructure[ m_nCurrentStructElement ].m_aAlias );
   11307             :             aLine.append( '\"' );
   11308             :         }
   11309             :         if( ! m_bEmitStructure )
   11310             :             aLine.append( " (inside NonStruct)" );
   11311             :         emitComment( aLine.getStr() );
   11312             : #endif
   11313           0 :         bSuccess = true;
   11314             :     }
   11315             : 
   11316           0 :     return bSuccess;
   11317             : }
   11318             : 
   11319           0 : bool PDFWriterImpl::setStructureAttribute( enum PDFWriter::StructAttribute eAttr, enum PDFWriter::StructAttributeValue eVal )
   11320             : {
   11321           0 :     if( !m_aContext.Tagged )
   11322           0 :         return false;
   11323             : 
   11324           0 :     bool bInsert = false;
   11325           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11326             :     {
   11327           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11328           0 :         switch( eAttr )
   11329             :         {
   11330             :             case PDFWriter::Placement:
   11331           0 :                 if( eVal == PDFWriter::Block        ||
   11332           0 :                     eVal == PDFWriter::Inline       ||
   11333           0 :                     eVal == PDFWriter::Before       ||
   11334           0 :                     eVal == PDFWriter::Start        ||
   11335             :                     eVal == PDFWriter::End )
   11336           0 :                     bInsert = true;
   11337           0 :                 break;
   11338             :             case PDFWriter::WritingMode:
   11339           0 :                 if( eVal == PDFWriter::LrTb         ||
   11340           0 :                     eVal == PDFWriter::RlTb         ||
   11341             :                     eVal == PDFWriter::TbRl )
   11342             :                 {
   11343           0 :                     bInsert = true;
   11344             :                 }
   11345           0 :                 break;
   11346             :             case PDFWriter::TextAlign:
   11347           0 :                 if( eVal == PDFWriter::Start        ||
   11348           0 :                     eVal == PDFWriter::Center       ||
   11349           0 :                     eVal == PDFWriter::End          ||
   11350             :                     eVal == PDFWriter::Justify )
   11351             :                 {
   11352           0 :                     if( eType == PDFWriter::Paragraph   ||
   11353           0 :                         eType == PDFWriter::Heading     ||
   11354           0 :                         eType == PDFWriter::H1          ||
   11355           0 :                         eType == PDFWriter::H2          ||
   11356           0 :                         eType == PDFWriter::H3          ||
   11357           0 :                         eType == PDFWriter::H4          ||
   11358           0 :                         eType == PDFWriter::H5          ||
   11359           0 :                         eType == PDFWriter::H6          ||
   11360           0 :                         eType == PDFWriter::List        ||
   11361           0 :                         eType == PDFWriter::ListItem    ||
   11362           0 :                         eType == PDFWriter::LILabel     ||
   11363           0 :                         eType == PDFWriter::LIBody      ||
   11364           0 :                         eType == PDFWriter::Table       ||
   11365           0 :                         eType == PDFWriter::TableRow    ||
   11366           0 :                         eType == PDFWriter::TableHeader ||
   11367             :                         eType == PDFWriter::TableData )
   11368             :                     {
   11369           0 :                         bInsert = true;
   11370             :                     }
   11371             :                 }
   11372           0 :                 break;
   11373             :             case PDFWriter::Width:
   11374             :             case PDFWriter::Height:
   11375           0 :                 if( eVal == PDFWriter::Auto )
   11376             :                 {
   11377           0 :                     if( eType == PDFWriter::Figure      ||
   11378           0 :                         eType == PDFWriter::Formula     ||
   11379           0 :                         eType == PDFWriter::Form        ||
   11380           0 :                         eType == PDFWriter::Table       ||
   11381           0 :                         eType == PDFWriter::TableHeader ||
   11382             :                         eType == PDFWriter::TableData )
   11383             :                     {
   11384           0 :                         bInsert = true;
   11385             :                     }
   11386             :                 }
   11387           0 :                 break;
   11388             :             case PDFWriter::BlockAlign:
   11389           0 :                 if( eVal == PDFWriter::Before       ||
   11390           0 :                     eVal == PDFWriter::Middle       ||
   11391           0 :                     eVal == PDFWriter::After        ||
   11392             :                     eVal == PDFWriter::Justify )
   11393             :                 {
   11394           0 :                     if( eType == PDFWriter::TableHeader ||
   11395             :                         eType == PDFWriter::TableData )
   11396             :                     {
   11397           0 :                         bInsert = true;
   11398             :                     }
   11399             :                 }
   11400           0 :                 break;
   11401             :             case PDFWriter::InlineAlign:
   11402           0 :                 if( eVal == PDFWriter::Start        ||
   11403           0 :                     eVal == PDFWriter::Center       ||
   11404             :                     eVal == PDFWriter::End )
   11405             :                 {
   11406           0 :                     if( eType == PDFWriter::TableHeader ||
   11407             :                         eType == PDFWriter::TableData )
   11408             :                     {
   11409           0 :                         bInsert = true;
   11410             :                     }
   11411             :                 }
   11412           0 :                 break;
   11413             :             case PDFWriter::LineHeight:
   11414           0 :                 if( eVal == PDFWriter::Normal       ||
   11415             :                     eVal == PDFWriter::Auto )
   11416             :                 {
   11417             :                     // only for ILSE and BLSE
   11418           0 :                     if( eType == PDFWriter::Paragraph   ||
   11419           0 :                         eType == PDFWriter::Heading     ||
   11420           0 :                         eType == PDFWriter::H1          ||
   11421           0 :                         eType == PDFWriter::H2          ||
   11422           0 :                         eType == PDFWriter::H3          ||
   11423           0 :                         eType == PDFWriter::H4          ||
   11424           0 :                         eType == PDFWriter::H5          ||
   11425           0 :                         eType == PDFWriter::H6          ||
   11426           0 :                         eType == PDFWriter::List        ||
   11427           0 :                         eType == PDFWriter::ListItem    ||
   11428           0 :                         eType == PDFWriter::LILabel     ||
   11429           0 :                         eType == PDFWriter::LIBody      ||
   11430           0 :                         eType == PDFWriter::Table       ||
   11431           0 :                         eType == PDFWriter::TableRow    ||
   11432           0 :                         eType == PDFWriter::TableHeader ||
   11433           0 :                         eType == PDFWriter::TableData   ||
   11434           0 :                         eType == PDFWriter::Span        ||
   11435           0 :                         eType == PDFWriter::Quote       ||
   11436           0 :                         eType == PDFWriter::Note        ||
   11437           0 :                         eType == PDFWriter::Reference   ||
   11438           0 :                         eType == PDFWriter::BibEntry    ||
   11439           0 :                         eType == PDFWriter::Code        ||
   11440             :                         eType == PDFWriter::Link )
   11441             :                     {
   11442           0 :                         bInsert = true;
   11443             :                     }
   11444             :                 }
   11445           0 :                 break;
   11446             :             case PDFWriter::TextDecorationType:
   11447           0 :                 if( eVal == PDFWriter::NONE         ||
   11448           0 :                     eVal == PDFWriter::Underline    ||
   11449           0 :                     eVal == PDFWriter::Overline     ||
   11450             :                     eVal == PDFWriter::LineThrough )
   11451             :                 {
   11452             :                     // only for ILSE and BLSE
   11453           0 :                     if( eType == PDFWriter::Paragraph   ||
   11454           0 :                         eType == PDFWriter::Heading     ||
   11455           0 :                         eType == PDFWriter::H1          ||
   11456           0 :                         eType == PDFWriter::H2          ||
   11457           0 :                         eType == PDFWriter::H3          ||
   11458           0 :                         eType == PDFWriter::H4          ||
   11459           0 :                         eType == PDFWriter::H5          ||
   11460           0 :                         eType == PDFWriter::H6          ||
   11461           0 :                         eType == PDFWriter::List        ||
   11462           0 :                         eType == PDFWriter::ListItem    ||
   11463           0 :                         eType == PDFWriter::LILabel     ||
   11464           0 :                         eType == PDFWriter::LIBody      ||
   11465           0 :                         eType == PDFWriter::Table       ||
   11466           0 :                         eType == PDFWriter::TableRow    ||
   11467           0 :                         eType == PDFWriter::TableHeader ||
   11468           0 :                         eType == PDFWriter::TableData   ||
   11469           0 :                         eType == PDFWriter::Span        ||
   11470           0 :                         eType == PDFWriter::Quote       ||
   11471           0 :                         eType == PDFWriter::Note        ||
   11472           0 :                         eType == PDFWriter::Reference   ||
   11473           0 :                         eType == PDFWriter::BibEntry    ||
   11474           0 :                         eType == PDFWriter::Code        ||
   11475             :                         eType == PDFWriter::Link )
   11476             :                     {
   11477           0 :                         bInsert = true;
   11478             :                     }
   11479             :                 }
   11480           0 :                 break;
   11481             :             case PDFWriter::ListNumbering:
   11482           0 :                 if( eVal == PDFWriter::NONE         ||
   11483           0 :                     eVal == PDFWriter::Disc         ||
   11484           0 :                     eVal == PDFWriter::Circle       ||
   11485           0 :                     eVal == PDFWriter::Square       ||
   11486           0 :                     eVal == PDFWriter::Decimal      ||
   11487           0 :                     eVal == PDFWriter::UpperRoman   ||
   11488           0 :                     eVal == PDFWriter::LowerRoman   ||
   11489           0 :                     eVal == PDFWriter::UpperAlpha   ||
   11490             :                     eVal == PDFWriter::LowerAlpha )
   11491             :                 {
   11492           0 :                     if( eType == PDFWriter::List )
   11493           0 :                         bInsert = true;
   11494             :                 }
   11495           0 :                 break;
   11496           0 :             default: break;
   11497             :         }
   11498             :     }
   11499             : 
   11500           0 :     if( bInsert )
   11501           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAttributes[ eAttr ] = PDFStructureAttribute( eVal );
   11502             : #if OSL_DEBUG_LEVEL > 1
   11503             :     else if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11504             :         fprintf( stderr, "rejecting setStructureAttribute( %s, %s ) on %s (%s) element\n",
   11505             :                  getAttributeTag( eAttr ),
   11506             :                  getAttributeValueTag( eVal ),
   11507             :                  getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ),
   11508             :                  m_aStructure[ m_nCurrentStructElement ].m_aAlias.getStr()
   11509             :                  );
   11510             : #endif
   11511             : 
   11512           0 :     return bInsert;
   11513             : }
   11514             : 
   11515           0 : bool PDFWriterImpl::setStructureAttributeNumerical( enum PDFWriter::StructAttribute eAttr, sal_Int32 nValue )
   11516             : {
   11517           0 :     if( ! m_aContext.Tagged )
   11518           0 :         return false;
   11519             : 
   11520           0 :     bool bInsert = false;
   11521           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11522             :     {
   11523           0 :         if( eAttr == PDFWriter::Language )
   11524             :         {
   11525           0 :             m_aStructure[ m_nCurrentStructElement ].m_aLocale = LanguageTag( (LanguageType)nValue ).getLocale();
   11526           0 :             return true;
   11527             :         }
   11528             : 
   11529           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11530           0 :         switch( eAttr )
   11531             :         {
   11532             :             case PDFWriter::SpaceBefore:
   11533             :             case PDFWriter::SpaceAfter:
   11534             :             case PDFWriter::StartIndent:
   11535             :             case PDFWriter::EndIndent:
   11536             :                 // just for BLSE
   11537           0 :                 if( eType == PDFWriter::Paragraph   ||
   11538           0 :                     eType == PDFWriter::Heading     ||
   11539           0 :                     eType == PDFWriter::H1          ||
   11540           0 :                     eType == PDFWriter::H2          ||
   11541           0 :                     eType == PDFWriter::H3          ||
   11542           0 :                     eType == PDFWriter::H4          ||
   11543           0 :                     eType == PDFWriter::H5          ||
   11544           0 :                     eType == PDFWriter::H6          ||
   11545           0 :                     eType == PDFWriter::List        ||
   11546           0 :                     eType == PDFWriter::ListItem    ||
   11547           0 :                     eType == PDFWriter::LILabel     ||
   11548           0 :                     eType == PDFWriter::LIBody      ||
   11549           0 :                     eType == PDFWriter::Table       ||
   11550           0 :                     eType == PDFWriter::TableRow    ||
   11551           0 :                     eType == PDFWriter::TableHeader ||
   11552             :                     eType == PDFWriter::TableData )
   11553             :                 {
   11554           0 :                     bInsert = true;
   11555             :                 }
   11556           0 :                 break;
   11557             :             case PDFWriter::TextIndent:
   11558             :                 // paragraph like BLSE and additional elements
   11559           0 :                 if( eType == PDFWriter::Paragraph   ||
   11560           0 :                     eType == PDFWriter::Heading     ||
   11561           0 :                     eType == PDFWriter::H1          ||
   11562           0 :                     eType == PDFWriter::H2          ||
   11563           0 :                     eType == PDFWriter::H3          ||
   11564           0 :                     eType == PDFWriter::H4          ||
   11565           0 :                     eType == PDFWriter::H5          ||
   11566           0 :                     eType == PDFWriter::H6          ||
   11567           0 :                     eType == PDFWriter::LILabel     ||
   11568           0 :                     eType == PDFWriter::LIBody      ||
   11569           0 :                     eType == PDFWriter::TableHeader ||
   11570             :                     eType == PDFWriter::TableData )
   11571             :                 {
   11572           0 :                     bInsert = true;
   11573             :                 }
   11574           0 :                 break;
   11575             :             case PDFWriter::Width:
   11576             :             case PDFWriter::Height:
   11577           0 :                 if( eType == PDFWriter::Figure      ||
   11578           0 :                     eType == PDFWriter::Formula     ||
   11579           0 :                     eType == PDFWriter::Form        ||
   11580           0 :                     eType == PDFWriter::Table       ||
   11581           0 :                     eType == PDFWriter::TableHeader ||
   11582             :                     eType == PDFWriter::TableData )
   11583             :                 {
   11584           0 :                     bInsert = true;
   11585             :                 }
   11586           0 :                 break;
   11587             :             case PDFWriter::LineHeight:
   11588             :             case PDFWriter::BaselineShift:
   11589             :                 // only for ILSE and BLSE
   11590           0 :                 if( eType == PDFWriter::Paragraph   ||
   11591           0 :                     eType == PDFWriter::Heading     ||
   11592           0 :                     eType == PDFWriter::H1          ||
   11593           0 :                     eType == PDFWriter::H2          ||
   11594           0 :                     eType == PDFWriter::H3          ||
   11595           0 :                     eType == PDFWriter::H4          ||
   11596           0 :                     eType == PDFWriter::H5          ||
   11597           0 :                     eType == PDFWriter::H6          ||
   11598           0 :                     eType == PDFWriter::List        ||
   11599           0 :                     eType == PDFWriter::ListItem    ||
   11600           0 :                     eType == PDFWriter::LILabel     ||
   11601           0 :                     eType == PDFWriter::LIBody      ||
   11602           0 :                     eType == PDFWriter::Table       ||
   11603           0 :                     eType == PDFWriter::TableRow    ||
   11604           0 :                     eType == PDFWriter::TableHeader ||
   11605           0 :                     eType == PDFWriter::TableData   ||
   11606           0 :                     eType == PDFWriter::Span        ||
   11607           0 :                     eType == PDFWriter::Quote       ||
   11608           0 :                     eType == PDFWriter::Note        ||
   11609           0 :                     eType == PDFWriter::Reference   ||
   11610           0 :                     eType == PDFWriter::BibEntry    ||
   11611           0 :                     eType == PDFWriter::Code        ||
   11612             :                     eType == PDFWriter::Link )
   11613             :                 {
   11614           0 :                         bInsert = true;
   11615             :                 }
   11616           0 :                 break;
   11617             :             case PDFWriter::RowSpan:
   11618             :             case PDFWriter::ColSpan:
   11619             :                 // only for table cells
   11620           0 :                 if( eType == PDFWriter::TableHeader ||
   11621             :                     eType == PDFWriter::TableData )
   11622             :                 {
   11623           0 :                     bInsert = true;
   11624             :                 }
   11625           0 :                 break;
   11626             :             case PDFWriter::LinkAnnotation:
   11627           0 :                 if( eType == PDFWriter::Link )
   11628           0 :                     bInsert = true;
   11629           0 :                 break;
   11630           0 :             default: break;
   11631             :         }
   11632             :     }
   11633             : 
   11634           0 :     if( bInsert )
   11635           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAttributes[ eAttr ] = PDFStructureAttribute( nValue );
   11636             : #if OSL_DEBUG_LEVEL > 1
   11637             :     else if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11638             :         fprintf( stderr, "rejecting setStructureAttributeNumerical( %s, %d ) on %s (%s) element\n",
   11639             :                  getAttributeTag( eAttr ),
   11640             :                  (int)nValue,
   11641             :                  getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ),
   11642             :                  m_aStructure[ m_nCurrentStructElement ].m_aAlias.getStr() );
   11643             : #endif
   11644             : 
   11645           0 :     return bInsert;
   11646             : }
   11647             : 
   11648           0 : void PDFWriterImpl::setStructureBoundingBox( const Rectangle& rRect )
   11649             : {
   11650           0 :     sal_Int32 nPageNr = m_nCurrentPage;
   11651           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() || !m_aContext.Tagged )
   11652           0 :         return;
   11653             : 
   11654           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11655             :     {
   11656           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11657           0 :         if( eType == PDFWriter::Figure      ||
   11658           0 :             eType == PDFWriter::Formula     ||
   11659           0 :             eType == PDFWriter::Form        ||
   11660             :             eType == PDFWriter::Table )
   11661             :         {
   11662           0 :             m_aStructure[ m_nCurrentStructElement ].m_aBBox = rRect;
   11663             :             // convert to default user space now, since the mapmode may change
   11664           0 :             m_aPages[nPageNr].convertRect( m_aStructure[ m_nCurrentStructElement ].m_aBBox );
   11665             :         }
   11666             :     }
   11667             : }
   11668             : 
   11669           0 : void PDFWriterImpl::setActualText( const OUString& rText )
   11670             : {
   11671           0 :     if( m_aContext.Tagged && m_nCurrentStructElement > 0 && m_bEmitStructure )
   11672             :     {
   11673           0 :         m_aStructure[ m_nCurrentStructElement ].m_aActualText = rText;
   11674             :     }
   11675           0 : }
   11676             : 
   11677           0 : void PDFWriterImpl::setAlternateText( const OUString& rText )
   11678             : {
   11679           0 :     if( m_aContext.Tagged && m_nCurrentStructElement > 0 && m_bEmitStructure )
   11680             :     {
   11681           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAltText = rText;
   11682             :     }
   11683           0 : }
   11684             : 
   11685           0 : void PDFWriterImpl::setAutoAdvanceTime( sal_uInt32 nSeconds, sal_Int32 nPageNr )
   11686             : {
   11687           0 :     if( nPageNr < 0 )
   11688           0 :         nPageNr = m_nCurrentPage;
   11689             : 
   11690           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11691           0 :         return;
   11692             : 
   11693           0 :     m_aPages[ nPageNr ].m_nDuration = nSeconds;
   11694             : }
   11695             : 
   11696           0 : void PDFWriterImpl::setPageTransition( PDFWriter::PageTransition eType, sal_uInt32 nMilliSec, sal_Int32 nPageNr )
   11697             : {
   11698           0 :     if( nPageNr < 0 )
   11699           0 :         nPageNr = m_nCurrentPage;
   11700             : 
   11701           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11702           0 :         return;
   11703             : 
   11704           0 :     m_aPages[ nPageNr ].m_eTransition   = eType;
   11705           0 :     m_aPages[ nPageNr ].m_nTransTime    = nMilliSec;
   11706             : }
   11707             : 
   11708           0 : void PDFWriterImpl::ensureUniqueRadioOnValues()
   11709             : {
   11710             :     // loop over radio groups
   11711           0 :     for( std::map<sal_Int32,sal_Int32>::const_iterator group = m_aRadioGroupWidgets.begin();
   11712           0 :          group != m_aRadioGroupWidgets.end(); ++group )
   11713             :     {
   11714           0 :         PDFWidget& rGroupWidget = m_aWidgets[ group->second ];
   11715             :         // check whether all kids have a unique OnValue
   11716           0 :         boost::unordered_map< OUString, sal_Int32, OUStringHash > aOnValues;
   11717           0 :         int nChildren = rGroupWidget.m_aKidsIndex.size();
   11718           0 :         bool bIsUnique = true;
   11719           0 :         for( int nKid = 0; nKid < nChildren && bIsUnique; nKid++ )
   11720             :         {
   11721           0 :             int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11722           0 :             const OUString& rVal = m_aWidgets[nKidIndex].m_aOnValue;
   11723             :             #if OSL_DEBUG_LEVEL > 1
   11724             :             fprintf( stderr, "OnValue: %s\n", OUStringToOString( rVal, RTL_TEXTENCODING_UTF8 ).getStr() );
   11725             :             #endif
   11726           0 :             if( aOnValues.find( rVal ) == aOnValues.end() )
   11727             :             {
   11728           0 :                 aOnValues[ rVal ] = 1;
   11729             :             }
   11730             :             else
   11731             :             {
   11732           0 :                 bIsUnique = false;
   11733             :             }
   11734             :         }
   11735           0 :         if( ! bIsUnique )
   11736             :         {
   11737             :             #if OSL_DEBUG_LEVEL > 1
   11738             :             fprintf( stderr, "enforcing unique OnValues\n" );
   11739             :             #endif
   11740             :             // make unique by using ascending OnValues
   11741           0 :             for( int nKid = 0; nKid < nChildren; nKid++ )
   11742             :             {
   11743           0 :                 int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11744           0 :                 PDFWidget& rKid = m_aWidgets[nKidIndex];
   11745           0 :                 rKid.m_aOnValue = OUString::number( nKid+1 );
   11746           0 :                 if( rKid.m_aValue != "Off" )
   11747           0 :                     rKid.m_aValue = rKid.m_aOnValue;
   11748             :             }
   11749             :         }
   11750             :         // finally move the "Yes" appearance to the OnValue appearance
   11751           0 :         for( int nKid = 0; nKid < nChildren; nKid++ )
   11752             :         {
   11753           0 :             int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11754           0 :             PDFWidget& rKid = m_aWidgets[nKidIndex];
   11755           0 :             PDFAppearanceMap::iterator app_it = rKid.m_aAppearances.find( "N" );
   11756           0 :             if( app_it != rKid.m_aAppearances.end() )
   11757             :             {
   11758           0 :                 PDFAppearanceStreams::iterator stream_it = app_it->second.find( "Yes" );
   11759           0 :                 if( stream_it != app_it->second.end() )
   11760             :                 {
   11761           0 :                     SvMemoryStream* pStream = stream_it->second;
   11762           0 :                     app_it->second.erase( stream_it );
   11763           0 :                     OStringBuffer aBuf( rKid.m_aOnValue.getLength()*2 );
   11764           0 :                     appendName( rKid.m_aOnValue, aBuf );
   11765           0 :                     (app_it->second)[ aBuf.makeStringAndClear() ] = pStream;
   11766             :                 }
   11767             :                 #if OSL_DEBUG_LEVEL > 1
   11768             :                 else
   11769             :                     fprintf( stderr, "error: RadioButton without \"Yes\" stream\n" );
   11770             :                 #endif
   11771             :             }
   11772             :             // update selected radio button
   11773           0 :             if( rKid.m_aValue != "Off" )
   11774             :             {
   11775           0 :                 rGroupWidget.m_aValue = rKid.m_aValue;
   11776             :             }
   11777             :         }
   11778           0 :     }
   11779           0 : }
   11780             : 
   11781           0 : sal_Int32 PDFWriterImpl::findRadioGroupWidget( const PDFWriter::RadioButtonWidget& rBtn )
   11782             : {
   11783           0 :     sal_Int32 nRadioGroupWidget = -1;
   11784             : 
   11785           0 :     std::map< sal_Int32, sal_Int32 >::const_iterator it = m_aRadioGroupWidgets.find( rBtn.RadioGroup );
   11786             : 
   11787           0 :     if( it == m_aRadioGroupWidgets.end() )
   11788             :     {
   11789           0 :         m_aRadioGroupWidgets[ rBtn.RadioGroup ] = nRadioGroupWidget =
   11790           0 :             sal_Int32(m_aWidgets.size());
   11791             : 
   11792             :         // new group, insert the radiobutton
   11793           0 :         m_aWidgets.push_back( PDFWidget() );
   11794           0 :         m_aWidgets.back().m_nObject     = createObject();
   11795           0 :         m_aWidgets.back().m_nPage       = m_nCurrentPage;
   11796           0 :         m_aWidgets.back().m_eType       = PDFWriter::RadioButton;
   11797           0 :         m_aWidgets.back().m_nRadioGroup = rBtn.RadioGroup;
   11798           0 :         m_aWidgets.back().m_nFlags |= 0x0000C000;   // NoToggleToOff and Radio bits
   11799             : 
   11800           0 :         createWidgetFieldName( sal_Int32(m_aWidgets.size()-1), rBtn );
   11801             :     }
   11802             :     else
   11803           0 :         nRadioGroupWidget = it->second;
   11804             : 
   11805           0 :     return nRadioGroupWidget;
   11806             : }
   11807             : 
   11808           0 : sal_Int32 PDFWriterImpl::createControl( const PDFWriter::AnyWidget& rControl, sal_Int32 nPageNr )
   11809             : {
   11810           0 :     if( nPageNr < 0 )
   11811           0 :         nPageNr = m_nCurrentPage;
   11812             : 
   11813           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11814           0 :         return -1;
   11815             : 
   11816           0 :     bool sigHidden(true);
   11817           0 :     sal_Int32 nNewWidget = m_aWidgets.size();
   11818           0 :     m_aWidgets.push_back( PDFWidget() );
   11819             : 
   11820           0 :     m_aWidgets.back().m_nObject         = createObject();
   11821           0 :     m_aWidgets.back().m_aRect           = rControl.Location;
   11822           0 :     m_aWidgets.back().m_nPage           = nPageNr;
   11823           0 :     m_aWidgets.back().m_eType           = rControl.getType();
   11824             : 
   11825           0 :     sal_Int32 nRadioGroupWidget = -1;
   11826             :     // for unknown reasons the radio buttons of a radio group must not have a
   11827             :     // field name, else the buttons are in fact check boxes -
   11828             :     // that is multiple buttons of the radio group can be selected
   11829           0 :     if( rControl.getType() == PDFWriter::RadioButton )
   11830           0 :         nRadioGroupWidget = findRadioGroupWidget( static_cast<const PDFWriter::RadioButtonWidget&>(rControl) );
   11831             :     else
   11832             :     {
   11833           0 :         createWidgetFieldName( nNewWidget, rControl );
   11834             :     }
   11835             : 
   11836             :     // caution: m_aWidgets must not be changed after here or rNewWidget may be invalid
   11837           0 :     PDFWidget& rNewWidget           = m_aWidgets[nNewWidget];
   11838           0 :     rNewWidget.m_aDescription       = rControl.Description;
   11839           0 :     rNewWidget.m_aText              = rControl.Text;
   11840             :     rNewWidget.m_nTextStyle         = rControl.TextStyle &
   11841             :         (  TEXT_DRAW_LEFT | TEXT_DRAW_CENTER | TEXT_DRAW_RIGHT | TEXT_DRAW_TOP |
   11842             :            TEXT_DRAW_VCENTER | TEXT_DRAW_BOTTOM |
   11843           0 :            TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK  );
   11844           0 :     rNewWidget.m_nTabOrder          = rControl.TabOrder;
   11845             : 
   11846             :     // various properties are set via the flags (/Ff) property of the field dict
   11847           0 :     if( rControl.ReadOnly )
   11848           0 :         rNewWidget.m_nFlags |= 1;
   11849           0 :     if( rControl.getType() == PDFWriter::PushButton )
   11850             :     {
   11851           0 :         const PDFWriter::PushButtonWidget& rBtn = static_cast<const PDFWriter::PushButtonWidget&>(rControl);
   11852           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11853             :             rNewWidget.m_nTextStyle =
   11854             :                 TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER |
   11855           0 :                 TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11856             : 
   11857           0 :         rNewWidget.m_nFlags |= 0x00010000;
   11858           0 :         if( !rBtn.URL.isEmpty() )
   11859           0 :             rNewWidget.m_aListEntries.push_back( rBtn.URL );
   11860           0 :         rNewWidget.m_bSubmit    = rBtn.Submit;
   11861           0 :         rNewWidget.m_bSubmitGet = rBtn.SubmitGet;
   11862           0 :         rNewWidget.m_nDest      = rBtn.Dest;
   11863           0 :         createDefaultPushButtonAppearance( rNewWidget, rBtn );
   11864             :     }
   11865           0 :     else if( rControl.getType() == PDFWriter::RadioButton )
   11866             :     {
   11867           0 :         const PDFWriter::RadioButtonWidget& rBtn = static_cast<const PDFWriter::RadioButtonWidget&>(rControl);
   11868           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11869             :             rNewWidget.m_nTextStyle =
   11870           0 :                 TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11871             :         /*  PDF sees a RadioButton group as one radio button with
   11872             :          *  children which are in turn check boxes
   11873             :          *
   11874             :          *  so we need to create a radio button on demand for a new group
   11875             :          *  and insert a checkbox for each RadioButtonWidget as its child
   11876             :          */
   11877           0 :         rNewWidget.m_eType          = PDFWriter::CheckBox;
   11878           0 :         rNewWidget.m_nRadioGroup    = rBtn.RadioGroup;
   11879             : 
   11880             :         DBG_ASSERT( nRadioGroupWidget >= 0 && nRadioGroupWidget < (sal_Int32)m_aWidgets.size(), "no radio group parent" );
   11881             : 
   11882           0 :         PDFWidget& rRadioButton = m_aWidgets[nRadioGroupWidget];
   11883           0 :         rRadioButton.m_aKids.push_back( rNewWidget.m_nObject );
   11884           0 :         rRadioButton.m_aKidsIndex.push_back( nNewWidget );
   11885           0 :         rNewWidget.m_nParent = rRadioButton.m_nObject;
   11886             : 
   11887           0 :         rNewWidget.m_aValue     = "Off";
   11888           0 :         rNewWidget.m_aOnValue   = rBtn.OnValue;
   11889           0 :         if( rRadioButton.m_aValue.isEmpty() && rBtn.Selected )
   11890             :         {
   11891           0 :             rNewWidget.m_aValue     = rNewWidget.m_aOnValue;
   11892           0 :             rRadioButton.m_aValue   = rNewWidget.m_aOnValue;
   11893             :         }
   11894           0 :         createDefaultRadioButtonAppearance( rNewWidget, rBtn );
   11895             : 
   11896             :         // union rect of radio group
   11897           0 :         Rectangle aRect = rNewWidget.m_aRect;
   11898           0 :         m_aPages[ nPageNr ].convertRect( aRect );
   11899           0 :         rRadioButton.m_aRect.Union( aRect );
   11900             :     }
   11901           0 :     else if( rControl.getType() == PDFWriter::CheckBox )
   11902             :     {
   11903           0 :         const PDFWriter::CheckBoxWidget& rBox = static_cast<const PDFWriter::CheckBoxWidget&>(rControl);
   11904           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11905             :             rNewWidget.m_nTextStyle =
   11906           0 :                 TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11907             : 
   11908           0 :         rNewWidget.m_aValue = rBox.Checked ? OUString("Yes") : OUString("Off" );
   11909             :         // create default appearance before m_aRect gets transformed
   11910           0 :         createDefaultCheckBoxAppearance( rNewWidget, rBox );
   11911             :     }
   11912           0 :     else if( rControl.getType() == PDFWriter::ListBox )
   11913             :     {
   11914           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11915           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_VCENTER;
   11916             : 
   11917           0 :         const PDFWriter::ListBoxWidget& rLstBox = static_cast<const PDFWriter::ListBoxWidget&>(rControl);
   11918           0 :         rNewWidget.m_aListEntries     = rLstBox.Entries;
   11919           0 :         rNewWidget.m_aSelectedEntries = rLstBox.SelectedEntries;
   11920           0 :         rNewWidget.m_aValue           = rLstBox.Text;
   11921           0 :         if( rLstBox.DropDown )
   11922           0 :             rNewWidget.m_nFlags |= 0x00020000;
   11923           0 :         if( rLstBox.Sort )
   11924           0 :             rNewWidget.m_nFlags |= 0x00080000;
   11925           0 :         if( rLstBox.MultiSelect && !rLstBox.DropDown && (int)m_aContext.Version > (int)PDFWriter::PDF_1_3 )
   11926           0 :             rNewWidget.m_nFlags |= 0x00200000;
   11927             : 
   11928           0 :         createDefaultListBoxAppearance( rNewWidget, rLstBox );
   11929             :     }
   11930           0 :     else if( rControl.getType() == PDFWriter::ComboBox )
   11931             :     {
   11932           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11933           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_VCENTER;
   11934             : 
   11935           0 :         const PDFWriter::ComboBoxWidget& rBox = static_cast<const PDFWriter::ComboBoxWidget&>(rControl);
   11936           0 :         rNewWidget.m_aValue         = rBox.Text;
   11937           0 :         rNewWidget.m_aListEntries   = rBox.Entries;
   11938           0 :         rNewWidget.m_nFlags |= 0x00060000; // combo and edit flag
   11939           0 :         if( rBox.Sort )
   11940           0 :             rNewWidget.m_nFlags |= 0x00080000;
   11941             : 
   11942           0 :         PDFWriter::ListBoxWidget aLBox;
   11943           0 :         aLBox.Name              = rBox.Name;
   11944           0 :         aLBox.Description       = rBox.Description;
   11945           0 :         aLBox.Text              = rBox.Text;
   11946           0 :         aLBox.TextStyle         = rBox.TextStyle;
   11947           0 :         aLBox.ReadOnly          = rBox.ReadOnly;
   11948           0 :         aLBox.Border            = rBox.Border;
   11949           0 :         aLBox.BorderColor       = rBox.BorderColor;
   11950           0 :         aLBox.Background        = rBox.Background;
   11951           0 :         aLBox.BackgroundColor   = rBox.BackgroundColor;
   11952           0 :         aLBox.TextFont          = rBox.TextFont;
   11953           0 :         aLBox.TextColor         = rBox.TextColor;
   11954           0 :         aLBox.DropDown          = true;
   11955           0 :         aLBox.Sort              = rBox.Sort;
   11956           0 :         aLBox.MultiSelect       = false;
   11957           0 :         aLBox.Entries           = rBox.Entries;
   11958             : 
   11959           0 :         createDefaultListBoxAppearance( rNewWidget, aLBox );
   11960             :     }
   11961           0 :     else if( rControl.getType() == PDFWriter::Edit )
   11962             :     {
   11963           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11964           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER;
   11965             : 
   11966           0 :         const PDFWriter::EditWidget& rEdit = static_cast<const  PDFWriter::EditWidget&>(rControl);
   11967           0 :         if( rEdit.MultiLine )
   11968             :         {
   11969           0 :             rNewWidget.m_nFlags |= 0x00001000;
   11970           0 :             rNewWidget.m_nTextStyle |= TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11971             :         }
   11972           0 :         if( rEdit.Password )
   11973           0 :             rNewWidget.m_nFlags |= 0x00002000;
   11974           0 :         if( rEdit.FileSelect && m_aContext.Version > PDFWriter::PDF_1_3 )
   11975           0 :             rNewWidget.m_nFlags |= 0x00100000;
   11976           0 :         rNewWidget.m_nMaxLen = rEdit.MaxLen;
   11977           0 :         rNewWidget.m_aValue = rEdit.Text;
   11978             : 
   11979           0 :         createDefaultEditAppearance( rNewWidget, rEdit );
   11980             :     }
   11981             : #if !defined(ANDROID) && !defined(IOS)
   11982           0 :     else if( rControl.getType() == PDFWriter::Signature)
   11983             :     {
   11984           0 :         const PDFWriter::SignatureWidget& rSig = static_cast<const PDFWriter::SignatureWidget&>(rControl);
   11985           0 :         sigHidden = rSig.SigHidden;
   11986             : 
   11987           0 :         if ( sigHidden )
   11988           0 :             rNewWidget.m_aRect = Rectangle(0, 0, 0, 0);
   11989             : 
   11990           0 :         m_nSignatureObject = createObject();
   11991           0 :         rNewWidget.m_aValue = OUString::number( m_nSignatureObject );
   11992           0 :         rNewWidget.m_aValue += " 0 R";
   11993             :         // let's add a fake appearance
   11994           0 :         rNewWidget.m_aAppearances[ "N" ][ "Standard" ] = new SvMemoryStream();
   11995             :     }
   11996             : #endif
   11997             : 
   11998             :     // if control is a hidden signature, do not convert coordinates since we
   11999             :     // need /Rect [ 0 0 0 0 ]
   12000           0 :     if ( ! ( ( rControl.getType() == PDFWriter::Signature ) && ( sigHidden ) ) )
   12001             :     {
   12002             :         // convert to default user space now, since the mapmode may change
   12003             :         // note: create default appearances before m_aRect gets transformed
   12004           0 :         m_aPages[ nPageNr ].convertRect( rNewWidget.m_aRect );
   12005             :     }
   12006             : 
   12007             :     // insert widget to page's annotation list
   12008           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( rNewWidget.m_nObject );
   12009             : 
   12010             :     // mark page as having widgets
   12011           0 :     m_aPages[ nPageNr ].m_bHasWidgets = true;
   12012             : 
   12013           0 :     return nNewWidget;
   12014             : }
   12015             : 
   12016           0 : void PDFWriterImpl::addStream( const OUString& rMimeType, PDFOutputStream* pStream, bool bCompress )
   12017             : {
   12018           0 :     if( pStream )
   12019             :     {
   12020           0 :         m_aAdditionalStreams.push_back( PDFAddStream() );
   12021           0 :         PDFAddStream& rStream = m_aAdditionalStreams.back();
   12022           0 :         rStream.m_aMimeType = !rMimeType.isEmpty()
   12023             :                               ? OUString( rMimeType )
   12024           0 :                               : OUString( "application/octet-stream"  );
   12025           0 :         rStream.m_pStream = pStream;
   12026           0 :         rStream.m_bCompress = bCompress;
   12027             :     }
   12028           3 : }
   12029             : 
   12030             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10