LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vcl/source/gdi - pdfwriter_impl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 6756 0.1 %
Date: 2013-07-09 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/config.h"
      21             : 
      22             : #include <math.h>
      23             : #include <algorithm>
      24             : 
      25             : #include <tools/urlobj.hxx>
      26             : 
      27             : #include <pdfwriter_impl.hxx>
      28             : 
      29             : #include <basegfx/polygon/b2dpolygon.hxx>
      30             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      31             : #include <basegfx/polygon/b2dpolygontools.hxx>
      32             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      33             : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
      34             : #include <basegfx/matrix/b2dhommatrix.hxx>
      35             : 
      36             : #include <osl/thread.h>
      37             : #include <osl/file.h>
      38             : 
      39             : #include <rtl/crc.h>
      40             : #include <rtl/digest.h>
      41             : #include <rtl/ustrbuf.hxx>
      42             : 
      43             : #include <tools/debug.hxx>
      44             : #include <tools/zcodec.hxx>
      45             : #include <tools/stream.hxx>
      46             : 
      47             : #include <i18nlangtag/languagetag.hxx>
      48             : 
      49             : #include <vcl/virdev.hxx>
      50             : #include <vcl/bmpacc.hxx>
      51             : #include <vcl/bitmapex.hxx>
      52             : #include <vcl/image.hxx>
      53             : #include <vcl/metric.hxx>
      54             : #include <vcl/svapp.hxx>
      55             : #include <vcl/lineinfo.hxx>
      56             : #include "vcl/cvtgrf.hxx"
      57             : #include "vcl/strhelper.hxx"
      58             : 
      59             : #include <fontsubset.hxx>
      60             : #include <outdev.h>
      61             : #include <sallayout.hxx>
      62             : #include <textlayout.hxx>
      63             : #include <salgdi.hxx>
      64             : 
      65             : #include <lcms2.h>
      66             : 
      67             : #include <comphelper/processfactory.hxx>
      68             : #include <comphelper/string.hxx>
      69             : 
      70             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      71             : #include <com/sun/star/util/URLTransformer.hpp>
      72             : #include <com/sun/star/util/URL.hpp>
      73             : 
      74             : #include "cppuhelper/implbase1.hxx"
      75             : 
      76             : #if !defined(ANDROID) && !defined(IOS)
      77             : // NSS header files for PDF signing support
      78             : #include "nss.h"
      79             : #include "cert.h"
      80             : #include "hasht.h"
      81             : #include "sechash.h"
      82             : #include "cms.h"
      83             : #include "cmst.h"
      84             : #endif
      85             : 
      86             : using namespace vcl;
      87             : 
      88             : 
      89             : #if (OSL_DEBUG_LEVEL < 3)
      90             : #define COMPRESS_PAGES
      91             : #else
      92             : #define DEBUG_DISABLE_PDFCOMPRESSION // also do not compress streams
      93             : #endif
      94             : 
      95             : #if !defined(ANDROID) && !defined(IOS)
      96             : #define MAX_SIGNATURE_CONTENT_LENGTH 0x4000
      97             : #endif
      98             : 
      99             : #ifdef DO_TEST_PDF
     100             : class PDFTestOutputStream : public PDFOutputStream
     101             : {
     102             :     public:
     103             :     virtual ~PDFTestOutputStream();
     104             :     virtual void write( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xStream );
     105             : };
     106             : 
     107             : PDFTestOutputStream::~PDFTestOutputStream()
     108             : {
     109             : }
     110             : 
     111             : void PDFTestOutputStream::write( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xStream )
     112             : {
     113             :     OString aStr( "lalala\ntest\ntest\ntest" );
     114             :     com::sun::star::uno::Sequence< sal_Int8 > aData( aStr.getLength() );
     115             :     memcpy( aData.getArray(), aStr.getStr(), aStr.getLength() );
     116             :     xStream->writeBytes( aData );
     117             : }
     118             : 
     119             : // this test code cannot be used to test PDF/A-1 because it forces
     120             : // control item (widgets) to bypass the structure controlling
     121             : // the embedding of such elements in actual run
     122             : void doTestCode()
     123             : {
     124             :     static const char* pHome = getenv( "HOME"  );
     125             :     OUString aTestFile( "file://"  );
     126             :     aTestFile += OUString( pHome, strlen( pHome ), RTL_TEXTENCODING_MS_1252 );
     127             :     aTestFile += OUString( "/pdf_export_test.pdf"  );
     128             : 
     129             :     PDFWriter::PDFWriterContext aContext;
     130             :     aContext.URL            = aTestFile;
     131             :     aContext.Version        = PDFWriter::PDF_1_4;
     132             :     aContext.Tagged         = true;
     133             :     aContext.InitialPage    = 2;
     134             :     aContext.DocumentInfo.Title = OUString( "PDF export test document"  );
     135             :     aContext.DocumentInfo.Producer = OUString( "VCL"  );
     136             : 
     137             :     aContext.SignPDF        = true;
     138             :     aContext.SignLocation   = OUString( "Burdur" );
     139             :     aContext.SignReason     = OUString( "Some valid reason to sign" );
     140             :     aContext.SignContact    = OUString( "signer@example.com" );
     141             : 
     142             :     com::sun::star::uno::Reference< com::sun::star::beans::XMaterialHolder > xEnc;
     143             :     PDFWriter aWriter( aContext, xEnc );
     144             :     aWriter.NewPage( 595, 842 );
     145             :     aWriter.BeginStructureElement( PDFWriter::Document );
     146             :     // set duration of 3 sec for first page
     147             :     aWriter.SetAutoAdvanceTime( 3 );
     148             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     149             : 
     150             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     151             :     aWriter.SetLineColor( Color( COL_LIGHTGREEN ) );
     152             :     aWriter.DrawRect( Rectangle( Point( 2000, 200 ), Size( 8000, 3000 ) ), 5000, 2000 );
     153             : 
     154             :     aWriter.SetFont( Font( String( "Times" ), Size( 0, 500 ) ) );
     155             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     156             :     aWriter.SetLineColor( Color( COL_BLACK ) );
     157             :     aWriter.SetFillColor( Color( COL_LIGHTBLUE ) );
     158             : 
     159             :     Rectangle aRect( Point( 5000, 5000 ), Size( 6000, 3000 ) );
     160             :     aWriter.DrawRect( aRect );
     161             :     aWriter.DrawText( aRect, String( "Link annot 1" ) );
     162             :     sal_Int32 nFirstLink = aWriter.CreateLink( aRect );
     163             :     PDFNote aNote;
     164             :     aNote.Title = String( "A small test note" );
     165             :     aNote.Contents = String( "There is no business like show business like no business i know. Everything about it is appealing." );
     166             :     aWriter.CreateNote( Rectangle( Point( aRect.Right(), aRect.Top() ), Size( 6000, 3000 ) ), aNote );
     167             : 
     168             :     Rectangle aTargetRect( Point( 3000, 23000 ), Size( 12000, 6000 ) );
     169             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     170             :     aWriter.DrawRect( aTargetRect );
     171             :     aWriter.DrawText( aTargetRect, String( "Dest second link" ) );
     172             :     sal_Int32 nSecondDest = aWriter.CreateDest( aTargetRect );
     173             : 
     174             :     aWriter.BeginStructureElement( PDFWriter::Section );
     175             :     aWriter.BeginStructureElement( PDFWriter::Heading );
     176             :     aWriter.DrawText( Point(4500, 9000), String( "A small structure test" ) );
     177             :     aWriter.EndStructureElement();
     178             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     179             :     aWriter.SetStructureAttribute( PDFWriter::WritingMode, PDFWriter::LrTb );
     180             :     aWriter.SetStructureAttribute( PDFWriter::TextDecorationType, PDFWriter::Underline );
     181             :     aWriter.DrawText( Rectangle( Point( 4500, 10000 ), Size( 12000, 6000 ) ),
     182             :                       String( "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." ),
     183             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     184             :                       );
     185             :     aWriter.SetActualText( String( "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." ) );
     186             :     aWriter.SetAlternateText( String( "This paragraph contains some lengthy nonsense to test structural element emission of PDFWriter." ) );
     187             :     aWriter.EndStructureElement();
     188             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     189             :     aWriter.SetStructureAttribute( PDFWriter::WritingMode, PDFWriter::LrTb );
     190             :     aWriter.DrawText( Rectangle( Point( 4500, 19000 ), Size( 12000, 1000 ) ),
     191             :                       String( "This paragraph is nothing special either but ends on the next page structurewise" ),
     192             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     193             :                       );
     194             : 
     195             :     aWriter.NewPage( 595, 842 );
     196             :     // test AddStream interface
     197             :     aWriter.AddStream( String( "text/plain" ), new PDFTestOutputStream(), true );
     198             :     // set transitional mode
     199             :     aWriter.SetPageTransition( PDFWriter::WipeRightToLeft, 1500 );
     200             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     201             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     202             :     aWriter.SetFont( Font( String( "Times" ), Size( 0, 500 ) ) );
     203             :     aWriter.DrawText( Rectangle( Point( 4500, 1500 ), Size( 12000, 3000 ) ),
     204             :                       String( "Here's where all things come to an end ... well at least the paragaph from the last page." ),
     205             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     206             :                       );
     207             :     aWriter.EndStructureElement();
     208             : 
     209             :     aWriter.SetFillColor( Color( COL_LIGHTBLUE ) );
     210             :     // disable structure
     211             :     aWriter.BeginStructureElement( PDFWriter::NonStructElement );
     212             :     aWriter.DrawRect( aRect );
     213             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     214             :     aWriter.DrawText( aRect, String( "Link annot 2" ) );
     215             :     sal_Int32 nSecondLink = aWriter.CreateLink( aRect );
     216             : 
     217             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     218             :     aWriter.BeginStructureElement( PDFWriter::ListItem );
     219             :     aWriter.DrawRect( aTargetRect );
     220             :     aWriter.DrawText( aTargetRect, String( "Dest first link" ) );
     221             :     sal_Int32 nFirstDest = aWriter.CreateDest( aTargetRect );
     222             :     // enable structure
     223             :     aWriter.EndStructureElement();
     224             : 
     225             :     aWriter.EndStructureElement();
     226             :     aWriter.EndStructureElement();
     227             :     aWriter.BeginStructureElement( PDFWriter::Figure );
     228             :     aWriter.BeginStructureElement( PDFWriter::Caption );
     229             :     aWriter.DrawText( Point( 4500, 9000 ), String( "Some drawing stuff inside the structure" ) );
     230             :     aWriter.EndStructureElement();
     231             : 
     232             :     // test clipping
     233             :     basegfx::B2DPolyPolygon aClip;
     234             :     basegfx::B2DPolygon aClipPoly;
     235             :     aClipPoly.append( basegfx::B2DPoint( 8250, 9600 ) );
     236             :     aClipPoly.append( basegfx::B2DPoint( 16500, 11100 ) );
     237             :     aClipPoly.append( basegfx::B2DPoint( 8250, 12600 ) );
     238             :     aClipPoly.append( basegfx::B2DPoint( 4500, 11100 ) );
     239             :     aClipPoly.setClosed( true );
     240             :     //aClipPoly.flip();
     241             :     aClip.append( aClipPoly );
     242             : 
     243             :     aWriter.Push( PUSH_CLIPREGION | PUSH_FILLCOLOR );
     244             :     aWriter.SetClipRegion( aClip );
     245             :     aWriter.DrawEllipse( Rectangle( Point( 4500, 9600 ), Size( 12000, 3000 ) ) );
     246             :     aWriter.MoveClipRegion( 1000, 500 );
     247             :     aWriter.SetFillColor( Color( COL_RED ) );
     248             :     aWriter.DrawEllipse( Rectangle( Point( 4500, 9600 ), Size( 12000, 3000 ) ) );
     249             :     aWriter.Pop();
     250             :     // test transparency
     251             :     // draw background
     252             :     Rectangle aTranspRect( Point( 7500, 13500 ), Size( 9000, 6000 ) );
     253             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     254             :     aWriter.DrawRect( aTranspRect );
     255             :     aWriter.BeginTransparencyGroup();
     256             : 
     257             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     258             :     aWriter.DrawEllipse( aTranspRect );
     259             :     aWriter.SetTextColor( Color( COL_LIGHTBLUE ) );
     260             :     aWriter.DrawText( aTranspRect,
     261             :                       String( "Some transparent text" ),
     262             :                       TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
     263             : 
     264             :     aWriter.EndTransparencyGroup( aTranspRect, 50 );
     265             : 
     266             :     // prepare an alpha mask
     267             :     Bitmap aTransMask( Size( 256, 256 ), 8, &Bitmap::GetGreyPalette( 256 ) );
     268             :     BitmapWriteAccess* pAcc = aTransMask.AcquireWriteAccess();
     269             :     for( int nX = 0; nX < 256; nX++ )
     270             :         for( int nY = 0; nY < 256; nY++ )
     271             :             pAcc->SetPixel( nX, nY, BitmapColor( (sal_uInt8)((nX+nY)/2) ) );
     272             :     aTransMask.ReleaseAccess( pAcc );
     273             :     aTransMask.SetPrefMapMode( MAP_MM );
     274             :     aTransMask.SetPrefSize( Size( 10, 10 ) );
     275             : 
     276             :     aWriter.DrawBitmap( Point( 600, 13500 ), Size( 3000, 3000 ), aTransMask );
     277             : 
     278             :     aTranspRect = Rectangle( Point( 4200, 13500 ), Size( 3000, 3000 ) );
     279             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     280             :     aWriter.DrawRect( aTranspRect );
     281             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     282             :     aWriter.DrawEllipse( aTranspRect );
     283             :     aWriter.SetTextColor( Color( COL_LIGHTBLUE ) );
     284             :     aWriter.DrawText( aTranspRect,
     285             :                       String( "Some transparent text" ),
     286             :                       TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
     287             :     aTranspRect = Rectangle( Point( 1500, 16500 ), Size( 4800, 3000 ) );
     288             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     289             :     aWriter.DrawRect( aTranspRect );
     290             : 
     291             :     Bitmap aImageBmp( Size( 256, 256 ), 24 );
     292             :     pAcc = aImageBmp.AcquireWriteAccess();
     293             :     pAcc->SetFillColor( Color( 0xff, 0, 0xff ) );
     294             :     pAcc->FillRect( Rectangle( Point( 0, 0 ), Size( 256, 256 ) ) );
     295             :     aImageBmp.ReleaseAccess( pAcc );
     296             :     BitmapEx aBmpEx( aImageBmp, AlphaMask( aTransMask ) );
     297             :     aWriter.DrawBitmapEx( Point( 1500, 19500 ), Size( 4800, 3000 ), aBmpEx );
     298             : 
     299             : 
     300             :     aWriter.EndStructureElement();
     301             :     aWriter.EndStructureElement();
     302             : 
     303             :     LineInfo aLI( LINE_DASH, 3 );
     304             :     aLI.SetDashCount( 2 );
     305             :     aLI.SetDashLen( 50 );
     306             :     aLI.SetDotCount( 2 );
     307             :     aLI.SetDotLen( 25 );
     308             :     aLI.SetDistance( 15 );
     309             :     Point aLIPoints[] = { Point( 4000, 10000 ),
     310             :                           Point( 8000, 12000 ),
     311             :                           Point( 3000, 19000 ) };
     312             :     Polygon aLIPoly( 3, aLIPoints );
     313             :     aWriter.SetLineColor( Color( COL_BLUE ) );
     314             :     aWriter.SetFillColor();
     315             :     aWriter.DrawPolyLine( aLIPoly, aLI );
     316             : 
     317             :     aLI.SetDashCount( 4 );
     318             :     aLIPoly.Move( 1000, 1000 );
     319             :     aWriter.DrawPolyLine( aLIPoly, aLI );
     320             : 
     321             :     aWriter.NewPage( 595, 842 );
     322             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     323             :     Wallpaper aWall( aTransMask );
     324             :     aWall.SetStyle( WALLPAPER_TILE );
     325             :     aWriter.DrawWallpaper( Rectangle( Point( 4400, 4200 ), Size( 10200, 6300 ) ), aWall );
     326             : 
     327             :     aWriter.NewPage( 595, 842 );
     328             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     329             :     aWriter.SetFont( Font( String( "Times" ), Size( 0, 500 ) ) );
     330             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     331             :     aRect = Rectangle( Point( 4500, 6000 ), Size( 6000, 1500 ) );
     332             :     aWriter.DrawRect( aRect );
     333             :     aWriter.DrawText( aRect, String( "www.heise.de" ) );
     334             :     sal_Int32 nURILink = aWriter.CreateLink( aRect );
     335             :     aWriter.SetLinkURL( nURILink, OUString( "http://www.heise.de" ) );
     336             : 
     337             :     aWriter.SetLinkDest( nFirstLink, nFirstDest );
     338             :     aWriter.SetLinkDest( nSecondLink, nSecondDest );
     339             : 
     340             :     // include a button
     341             :     PDFWriter::PushButtonWidget aBtn;
     342             :     aBtn.Name = OUString( "testButton"  );
     343             :     aBtn.Description = OUString( "A test button"  );
     344             :     aBtn.Text = OUString( "hit me"  );
     345             :     aBtn.Location = Rectangle( Point( 4500, 9000 ), Size( 4500, 3000 ) );
     346             :     aBtn.Border = aBtn.Background = true;
     347             :     aWriter.CreateControl( aBtn );
     348             : 
     349             :     // include a uri button
     350             :     PDFWriter::PushButtonWidget aUriBtn;
     351             :     aUriBtn.Name = OUString( "wwwButton"  );
     352             :     aUriBtn.Description = OUString( "A URI button"  );
     353             :     aUriBtn.Text = OUString( "to www"  );
     354             :     aUriBtn.Location = Rectangle( Point( 9500, 9000 ), Size( 4500, 3000 ) );
     355             :     aUriBtn.Border = aUriBtn.Background = true;
     356             :     aUriBtn.URL = OUString( "http://www.heise.de"  );
     357             :     aWriter.CreateControl( aUriBtn );
     358             : 
     359             :     // include a dest button
     360             :     PDFWriter::PushButtonWidget aDstBtn;
     361             :     aDstBtn.Name = OUString( "destButton"  );
     362             :     aDstBtn.Description = OUString( "A Dest button"  );
     363             :     aDstBtn.Text = OUString( "to paragraph"  );
     364             :     aDstBtn.Location = Rectangle( Point( 14500, 9000 ), Size( 4500, 3000 ) );
     365             :     aDstBtn.Border = aDstBtn.Background = true;
     366             :     aDstBtn.Dest = nFirstDest;
     367             :     aWriter.CreateControl( aDstBtn );
     368             : 
     369             :     PDFWriter::CheckBoxWidget aCBox;
     370             :     aCBox.Name = OUString( "textCheckBox"  );
     371             :     aCBox.Description = OUString( "A test check box"  );
     372             :     aCBox.Text = OUString( "check me"  );
     373             :     aCBox.Location = Rectangle( Point( 4500, 13500 ), Size( 3000, 750 ) );
     374             :     aCBox.Checked = true;
     375             :     aCBox.Border = aCBox.Background = false;
     376             :     aWriter.CreateControl( aCBox );
     377             : 
     378             :     PDFWriter::CheckBoxWidget aCBox2;
     379             :     aCBox2.Name = OUString( "textCheckBox2"  );
     380             :     aCBox2.Description = OUString( "Another test check box"  );
     381             :     aCBox2.Text = OUString( "check me right"  );
     382             :     aCBox2.Location = Rectangle( Point( 4500, 14250 ), Size( 3000, 750 ) );
     383             :     aCBox2.Checked = true;
     384             :     aCBox2.Border = aCBox2.Background = false;
     385             :     aCBox2.ButtonIsLeft = false;
     386             :     aWriter.CreateControl( aCBox2 );
     387             : 
     388             :     PDFWriter::RadioButtonWidget aRB1;
     389             :     aRB1.Name = OUString( "rb1_1"  );
     390             :     aRB1.Description = OUString( "radio 1 button 1"  );
     391             :     aRB1.Text = OUString( "Despair"  );
     392             :     aRB1.Location = Rectangle( Point( 4500, 15000 ), Size( 6000, 1000 ) );
     393             :     aRB1.Selected = true;
     394             :     aRB1.RadioGroup = 1;
     395             :     aRB1.Border = aRB1.Background = true;
     396             :     aRB1.ButtonIsLeft = false;
     397             :     aRB1.BorderColor = Color( COL_LIGHTGREEN );
     398             :     aRB1.BackgroundColor = Color( COL_LIGHTBLUE );
     399             :     aRB1.TextColor = Color( COL_LIGHTRED );
     400             :     aRB1.TextFont = Font( String( "Courier" ), Size( 0, 800 ) );
     401             :     aWriter.CreateControl( aRB1 );
     402             : 
     403             :     PDFWriter::RadioButtonWidget aRB2;
     404             :     aRB2.Name = OUString( "rb2_1"  );
     405             :     aRB2.Description = OUString( "radio 2 button 1"  );
     406             :     aRB2.Text = OUString( "Joy"  );
     407             :     aRB2.Location = Rectangle( Point( 10500, 15000 ), Size( 3000, 1000 ) );
     408             :     aRB2.Selected = true;
     409             :     aRB2.RadioGroup = 2;
     410             :     aWriter.CreateControl( aRB2 );
     411             : 
     412             :     PDFWriter::RadioButtonWidget aRB3;
     413             :     aRB3.Name = OUString( "rb1_2"  );
     414             :     aRB3.Description = OUString( "radio 1 button 2"  );
     415             :     aRB3.Text = OUString( "Desperation"  );
     416             :     aRB3.Location = Rectangle( Point( 4500, 16000 ), Size( 3000, 1000 ) );
     417             :     aRB3.Selected = true;
     418             :     aRB3.RadioGroup = 1;
     419             :     aWriter.CreateControl( aRB3 );
     420             : 
     421             :     PDFWriter::EditWidget aEditBox;
     422             :     aEditBox.Name = OUString( "testEdit"  );
     423             :     aEditBox.Description = OUString( "A test edit field"  );
     424             :     aEditBox.Text = OUString( "A little test text"  );
     425             :     aEditBox.TextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER;
     426             :     aEditBox.Location = Rectangle( Point( 10000, 18000 ), Size( 5000, 1500 ) );
     427             :     aEditBox.MaxLen = 100;
     428             :     aEditBox.Border = aEditBox.Background = true;
     429             :     aEditBox.BorderColor = Color( COL_BLACK );
     430             :     aWriter.CreateControl( aEditBox );
     431             : 
     432             :     // normal list box
     433             :     PDFWriter::ListBoxWidget aLstBox;
     434             :     aLstBox.Name = OUString( "testListBox"  );
     435             :     aLstBox.Text = OUString( "One"  );
     436             :     aLstBox.Description = OUString( "select me"  );
     437             :     aLstBox.Location = Rectangle( Point( 4500, 18000 ), Size( 3000, 1500 ) );
     438             :     aLstBox.Sort = true;
     439             :     aLstBox.MultiSelect = true;
     440             :     aLstBox.Border = aLstBox.Background = true;
     441             :     aLstBox.BorderColor = Color( COL_BLACK );
     442             :     aLstBox.Entries.push_back( OUString( "One"  ) );
     443             :     aLstBox.Entries.push_back( OUString( "Two"  ) );
     444             :     aLstBox.Entries.push_back( OUString( "Three"  ) );
     445             :     aLstBox.Entries.push_back( OUString( "Four"  ) );
     446             :     aLstBox.SelectedEntries.push_back( 1 );
     447             :     aLstBox.SelectedEntries.push_back( 2 );
     448             :     aWriter.CreateControl( aLstBox );
     449             : 
     450             :     // dropdown list box
     451             :     aLstBox.Name = OUString( "testDropDownListBox"  );
     452             :     aLstBox.DropDown = true;
     453             :     aLstBox.Location = Rectangle( Point( 4500, 19500 ), Size( 3000, 500 ) );
     454             :     aWriter.CreateControl( aLstBox );
     455             : 
     456             :     // combo box
     457             :     PDFWriter::ComboBoxWidget aComboBox;
     458             :     aComboBox.Name = OUString( "testComboBox"  );
     459             :     aComboBox.Text = OUString( "test a combobox"  );
     460             :     aComboBox.Entries.push_back( OUString( "Larry"  ) );
     461             :     aComboBox.Entries.push_back( OUString( "Curly"  ) );
     462             :     aComboBox.Entries.push_back( OUString( "Moe"  ) );
     463             :     aComboBox.Location = Rectangle( Point( 4500, 20000 ), Size( 3000, 500 ) );
     464             :     aWriter.CreateControl( aComboBox );
     465             : 
     466             :     // test outlines
     467             :     sal_Int32 nPage1OL = aWriter.CreateOutlineItem();
     468             :     aWriter.SetOutlineItemText( nPage1OL, OUString( "Page 1"  ) );
     469             :     aWriter.SetOutlineItemDest( nPage1OL, nSecondDest );
     470             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2"  ), nSecondDest );
     471             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2 revisited"  ), nSecondDest );
     472             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2 again"  ), nSecondDest );
     473             :     sal_Int32 nPage2OL = aWriter.CreateOutlineItem();
     474             :     aWriter.SetOutlineItemText( nPage2OL, OUString( "Page 2"  ) );
     475             :     aWriter.CreateOutlineItem( nPage2OL, OUString( "Dest 1"  ), nFirstDest );
     476             : 
     477             :     aWriter.EndStructureElement(); // close document
     478             : 
     479             :     aWriter.Emit();
     480             : }
     481             : #endif
     482             : 
     483             : static const sal_Int32 nLog10Divisor = 1;
     484             : static const double fDivisor = 10.0;
     485             : 
     486             : static inline double pixelToPoint( sal_Int32 px ) { return double(px)/fDivisor; }
     487           0 : static inline double pixelToPoint( double px ) { return px/fDivisor; }
     488           0 : static inline sal_Int32 pointToPixel( double pt ) { return sal_Int32(pt*fDivisor); }
     489             : 
     490             : const sal_uInt8 PDFWriterImpl::s_nPadString[32] =
     491             : {
     492             :     0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
     493             :     0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A
     494             : };
     495             : 
     496           0 : static void appendHex( sal_Int8 nInt, OStringBuffer& rBuffer )
     497             : {
     498             :     static const sal_Char pHexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
     499             :                                            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     500           0 :     rBuffer.append( pHexDigits[ (nInt >> 4) & 15 ] );
     501           0 :     rBuffer.append( pHexDigits[ nInt & 15 ] );
     502           0 : }
     503             : 
     504           0 : static void appendName( const OUString& rStr, OStringBuffer& rBuffer )
     505             : {
     506             : // FIXME i59651 add a check for max length of 127 chars? Per PDF spec 1.4, appendix C.1
     507             : // I guess than when reading the #xx sequence it will count for a single character.
     508           0 :     OString aStr( OUStringToOString( rStr, RTL_TEXTENCODING_UTF8 ) );
     509           0 :     const sal_Char* pStr = aStr.getStr();
     510           0 :     int nLen = aStr.getLength();
     511           0 :     for( int i = 0; i < nLen; i++ )
     512             :     {
     513             :         /*  #i16920# PDF recommendation: output UTF8, any byte
     514             :          *  outside the interval [33(=ASCII'!');126(=ASCII'~')]
     515             :          *  should be escaped hexadecimal
     516             :          *  for the sake of ghostscript which also reads PDF
     517             :          *  but has a narrower acceptance rate we only pass
     518             :          *  alphanumerics and '-' literally.
     519             :          */
     520           0 :         if( (pStr[i] >= 'A' && pStr[i] <= 'Z' ) ||
     521           0 :             (pStr[i] >= 'a' && pStr[i] <= 'z' ) ||
     522           0 :             (pStr[i] >= '0' && pStr[i] <= '9' ) ||
     523           0 :             pStr[i] == '-' )
     524             :         {
     525           0 :             rBuffer.append( pStr[i] );
     526             :         }
     527             :         else
     528             :         {
     529           0 :             rBuffer.append( '#' );
     530           0 :             appendHex( (sal_Int8)pStr[i], rBuffer );
     531             :         }
     532           0 :     }
     533           0 : }
     534             : 
     535           0 : static void appendName( const sal_Char* pStr, OStringBuffer& rBuffer )
     536             : {
     537             : //FIXME i59651 see above
     538           0 :     while( pStr && *pStr )
     539             :     {
     540           0 :         if( (*pStr >= 'A' && *pStr <= 'Z' ) ||
     541           0 :             (*pStr >= 'a' && *pStr <= 'z' ) ||
     542           0 :             (*pStr >= '0' && *pStr <= '9' ) ||
     543           0 :             *pStr == '-' )
     544             :         {
     545           0 :             rBuffer.append( *pStr );
     546             :         }
     547             :         else
     548             :         {
     549           0 :             rBuffer.append( '#' );
     550           0 :             appendHex( (sal_Int8)*pStr, rBuffer );
     551             :         }
     552           0 :         pStr++;
     553             :     }
     554           0 : }
     555             : 
     556             : //used only to emit encoded passwords
     557           0 : static void appendLiteralString( const sal_Char* pStr, sal_Int32 nLength, OStringBuffer& rBuffer )
     558             : {
     559           0 :     while( nLength )
     560             :     {
     561           0 :         switch( *pStr )
     562             :         {
     563             :         case '\n' :
     564           0 :             rBuffer.append( "\\n" );
     565           0 :             break;
     566             :         case '\r' :
     567           0 :             rBuffer.append( "\\r" );
     568           0 :             break;
     569             :         case '\t' :
     570           0 :             rBuffer.append( "\\t" );
     571           0 :             break;
     572             :         case '\b' :
     573           0 :             rBuffer.append( "\\b" );
     574           0 :             break;
     575             :         case '\f' :
     576           0 :             rBuffer.append( "\\f" );
     577           0 :             break;
     578             :         case '(' :
     579             :         case ')' :
     580             :         case '\\' :
     581           0 :             rBuffer.append( "\\" );
     582           0 :             rBuffer.append( (sal_Char) *pStr );
     583           0 :             break;
     584             :         default:
     585           0 :             rBuffer.append( (sal_Char) *pStr );
     586           0 :             break;
     587             :         }
     588           0 :         pStr++;
     589           0 :         nLength--;
     590             :     }
     591           0 : }
     592             : 
     593             : /**--->i56629
     594             :  * Convert a string before using it.
     595             :  *
     596             :  * This string conversion function is needed because the destination name
     597             :  * in a PDF file seen through an Internet browser should be
     598             :  * specially crafted, in order to be used directly by the browser.
     599             :  * In this way the fragment part of a hyperlink to a PDF file (e.g. something
     600             :  * as 'test1/test2/a-file.pdf#thefragment) will be (hopefully) interpreted by the
     601             :  * PDF reader (currently only Adobe Reader plug-in seems to be working that way) called
     602             :  * from inside the Internet browser as: 'open the file test1/test2/a-file.pdf
     603             :  * and go to named destination thefragment using default zoom'.
     604             :  * The conversion is needed because in case of a fragment in the form: Slide%201
     605             :  * (meaning Slide 1) as it is converted obeying the Inet rules, it will become Slide25201
     606             :  * using this conversion, in both the generated named destinations, fragment and GoToR
     607             :  * destination.
     608             :  *
     609             :  * The names for destinations are name objects and so they don't need to be encrypted
     610             :  * even though they expose the content of PDF file (e.g. guessing the PDF content from the
     611             :  * destination name).
     612             :  *
     613             :  * Fhurter limitation: it is advisable to use standard ASCII characters for
     614             :  * OOo bookmarks.
     615             : */
     616           0 : static void appendDestinationName( const OUString& rString, OStringBuffer& rBuffer )
     617             : {
     618           0 :     const sal_Unicode* pStr = rString.getStr();
     619           0 :     sal_Int32 nLen = rString.getLength();
     620           0 :     for( int i = 0; i < nLen; i++ )
     621             :     {
     622           0 :         sal_Unicode aChar = pStr[i];
     623           0 :         if( (aChar >= '0' && aChar <= '9' ) ||
     624           0 :             (aChar >= 'a' && aChar <= 'z' ) ||
     625           0 :             (aChar >= 'A' && aChar <= 'Z' ) ||
     626             :             aChar == '-' )
     627             :         {
     628           0 :             rBuffer.append((sal_Char)aChar);
     629             :         }
     630             :         else
     631             :         {
     632           0 :             sal_Int8 aValueHigh = sal_Int8(aChar >> 8);
     633           0 :             if(aValueHigh > 0)
     634           0 :                 appendHex( aValueHigh, rBuffer );
     635           0 :             appendHex( (sal_Int8)(aChar & 255 ), rBuffer );
     636             :         }
     637             :     }
     638           0 : }
     639             : //<--- i56629
     640             : 
     641           0 : static void appendUnicodeTextString( const OUString& rString, OStringBuffer& rBuffer )
     642             : {
     643           0 :     rBuffer.append( "FEFF" );
     644           0 :     const sal_Unicode* pStr = rString.getStr();
     645           0 :     sal_Int32 nLen = rString.getLength();
     646           0 :     for( int i = 0; i < nLen; i++ )
     647             :     {
     648           0 :         sal_Unicode aChar = pStr[i];
     649           0 :         appendHex( (sal_Int8)(aChar >> 8), rBuffer );
     650           0 :         appendHex( (sal_Int8)(aChar & 255 ), rBuffer );
     651             :     }
     652           0 : }
     653             : 
     654           0 : void PDFWriterImpl::createWidgetFieldName( sal_Int32 i_nWidgetIndex, const PDFWriter::AnyWidget& i_rControl )
     655             : {
     656             :     /* #i80258# previously we use appendName here
     657             :        however we need a slightly different coding scheme than the normal
     658             :        name encoding for field names
     659             :     */
     660           0 :     const OUString& rName = (m_aContext.Version > PDFWriter::PDF_1_2) ? i_rControl.Name : i_rControl.Text;
     661           0 :     OString aStr( OUStringToOString( rName, RTL_TEXTENCODING_UTF8 ) );
     662           0 :     const sal_Char* pStr = aStr.getStr();
     663           0 :     int nLen = aStr.getLength();
     664             : 
     665           0 :     OStringBuffer aBuffer( rName.getLength()+64 );
     666           0 :     for( int i = 0; i < nLen; i++ )
     667             :     {
     668             :         /*  #i16920# PDF recommendation: output UTF8, any byte
     669             :          *  outside the interval [32(=ASCII' ');126(=ASCII'~')]
     670             :          *  should be escaped hexadecimal
     671             :          */
     672           0 :         if( (pStr[i] >= 32 && pStr[i] <= 126 ) )
     673           0 :             aBuffer.append( pStr[i] );
     674             :         else
     675             :         {
     676           0 :             aBuffer.append( '#' );
     677           0 :             appendHex( (sal_Int8)pStr[i], aBuffer );
     678             :         }
     679             :     }
     680             : 
     681           0 :     OString aFullName( aBuffer.makeStringAndClear() );
     682             : 
     683             :     /* #i82785# create hierarchical fields down to the for each dot in i_rName */
     684           0 :     sal_Int32 nTokenIndex = 0, nLastTokenIndex = 0;
     685           0 :     OString aPartialName;
     686           0 :     OString aDomain;
     687           0 :     do
     688             :     {
     689           0 :         nLastTokenIndex = nTokenIndex;
     690           0 :         aPartialName = aFullName.getToken( 0, '.', nTokenIndex );
     691           0 :         if( nTokenIndex != -1 )
     692             :         {
     693             :             // find or create a hierarchical field
     694             :             // first find the fully qualified name up to this field
     695           0 :             aDomain = aFullName.copy( 0, nTokenIndex-1 );
     696           0 :             boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator it = m_aFieldNameMap.find( aDomain );
     697           0 :             if( it == m_aFieldNameMap.end() )
     698             :             {
     699             :                  // create new hierarchy field
     700           0 :                 sal_Int32 nNewWidget = m_aWidgets.size();
     701           0 :                 m_aWidgets.push_back( PDFWidget() );
     702           0 :                 m_aWidgets[nNewWidget].m_nObject = createObject();
     703           0 :                 m_aWidgets[nNewWidget].m_eType = PDFWriter::Hierarchy;
     704           0 :                 m_aWidgets[nNewWidget].m_aName = aPartialName;
     705           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[nNewWidget].m_nObject;
     706           0 :                 m_aFieldNameMap[aDomain] = nNewWidget;
     707           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[nNewWidget].m_nObject;
     708           0 :                 if( nLastTokenIndex > 0 )
     709             :                 {
     710             :                     // this field is not a root field and
     711             :                     // needs to be inserted to its parent
     712           0 :                     OString aParentDomain( aDomain.copy( 0, nLastTokenIndex-1 ) );
     713           0 :                     it = m_aFieldNameMap.find( aParentDomain );
     714             :                     OSL_ENSURE( it != m_aFieldNameMap.end(), "field name not found" );
     715           0 :                     if( it != m_aFieldNameMap.end()  )
     716             :                     {
     717             :                         OSL_ENSURE( it->second < sal_Int32(m_aWidgets.size()), "invalid field number entry" );
     718           0 :                         if( it->second < sal_Int32(m_aWidgets.size()) )
     719             :                         {
     720           0 :                             PDFWidget& rParentField( m_aWidgets[it->second] );
     721           0 :                             rParentField.m_aKids.push_back( m_aWidgets[nNewWidget].m_nObject );
     722           0 :                             rParentField.m_aKidsIndex.push_back( nNewWidget );
     723           0 :                             m_aWidgets[nNewWidget].m_nParent = rParentField.m_nObject;
     724             :                         }
     725           0 :                     }
     726             :                 }
     727             :             }
     728           0 :             else if( m_aWidgets[it->second].m_eType != PDFWriter::Hierarchy )
     729             :             {
     730             :                 // this is invalid, someone tries to have a terminal field as parent
     731             :                 // example: a button with the name foo.bar exists and
     732             :                 // another button is named foo.bar.no
     733             :                 // workaround: put the second terminal field as much up in the hierarchy as
     734             :                 // necessary to have a non-terminal field as parent (or none at all)
     735             :                 // since it->second already is terminal, we just need to use its parent
     736           0 :                 aDomain = OString();
     737           0 :                 aPartialName = aFullName.copy( aFullName.lastIndexOf( '.' )+1 );
     738           0 :                 if( nLastTokenIndex > 0 )
     739             :                 {
     740           0 :                     aDomain = aFullName.copy( 0, nLastTokenIndex-1 );
     741           0 :                     OStringBuffer aBuf( aDomain.getLength() + 1 + aPartialName.getLength() );
     742           0 :                     aBuf.append( aDomain );
     743           0 :                     aBuf.append( '.' );
     744           0 :                     aBuf.append( aPartialName );
     745           0 :                     aFullName = aBuf.makeStringAndClear();
     746             :                 }
     747             :                 else
     748           0 :                     aFullName = aPartialName;
     749           0 :                 break;
     750             :             }
     751             :         }
     752           0 :     } while( nTokenIndex != -1 );
     753             : 
     754             :     // insert widget into its hierarchy field
     755           0 :     if( !aDomain.isEmpty() )
     756             :     {
     757           0 :         boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator it = m_aFieldNameMap.find( aDomain );
     758           0 :         if( it != m_aFieldNameMap.end() )
     759             :         {
     760             :             OSL_ENSURE( it->second >= 0 && it->second < sal_Int32( m_aWidgets.size() ), "invalid field index" );
     761           0 :             if( it->second >= 0 && it->second < sal_Int32(m_aWidgets.size()) )
     762             :             {
     763           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[it->second].m_nObject;
     764           0 :                 m_aWidgets[it->second].m_aKids.push_back( m_aWidgets[i_nWidgetIndex].m_nObject);
     765           0 :                 m_aWidgets[it->second].m_aKidsIndex.push_back( i_nWidgetIndex );
     766             :             }
     767             :         }
     768             :     }
     769             : 
     770           0 :     if( aPartialName.isEmpty() )
     771             :     {
     772             :         // how funny, an empty field name
     773           0 :         if( i_rControl.getType() == PDFWriter::RadioButton )
     774             :         {
     775           0 :             aPartialName  = "RadioGroup";
     776           0 :             aPartialName += OString::valueOf( static_cast<const PDFWriter::RadioButtonWidget&>(i_rControl).RadioGroup );
     777             :         }
     778             :         else
     779           0 :             aPartialName = OString( "Widget" );
     780             :     }
     781             : 
     782           0 :     if( ! m_aContext.AllowDuplicateFieldNames )
     783             :     {
     784           0 :         boost::unordered_map<OString, sal_Int32, OStringHash>::iterator it = m_aFieldNameMap.find( aFullName );
     785             : 
     786           0 :         if( it != m_aFieldNameMap.end() ) // not unique
     787             :         {
     788           0 :             boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator check_it;
     789           0 :             OString aTry;
     790           0 :             sal_Int32 nTry = 2;
     791           0 :             do
     792             :             {
     793           0 :                 OStringBuffer aUnique( aFullName.getLength() + 16 );
     794           0 :                 aUnique.append( aFullName );
     795           0 :                 aUnique.append( '_' );
     796           0 :                 aUnique.append( nTry++ );
     797           0 :                 aTry = aUnique.makeStringAndClear();
     798           0 :                 check_it = m_aFieldNameMap.find( aTry );
     799           0 :             } while( check_it != m_aFieldNameMap.end() );
     800           0 :             aFullName = aTry;
     801           0 :             m_aFieldNameMap[ aFullName ] = i_nWidgetIndex;
     802           0 :             aPartialName = aFullName.copy( aFullName.lastIndexOf( '.' )+1 );
     803             :         }
     804             :         else
     805           0 :             m_aFieldNameMap[ aFullName ] = i_nWidgetIndex;
     806             :     }
     807             : 
     808             :     // finally
     809           0 :     m_aWidgets[i_nWidgetIndex].m_aName = aPartialName;
     810           0 : }
     811             : 
     812           0 : static void appendFixedInt( sal_Int32 nValue, OStringBuffer& rBuffer, sal_Int32 nPrecision = nLog10Divisor )
     813             : {
     814           0 :     if( nValue < 0 )
     815             :     {
     816           0 :         rBuffer.append( '-' );
     817           0 :         nValue = -nValue;
     818             :     }
     819           0 :     sal_Int32 nFactor = 1, nDiv = nPrecision;
     820           0 :     while( nDiv-- )
     821           0 :         nFactor *= 10;
     822             : 
     823           0 :     sal_Int32 nInt      = nValue / nFactor;
     824           0 :     rBuffer.append( nInt );
     825           0 :     if( nFactor > 1 )
     826             :     {
     827           0 :         sal_Int32 nDecimal  = nValue % nFactor;
     828           0 :         if( nDecimal )
     829             :         {
     830           0 :             rBuffer.append( '.' );
     831             :             // omit trailing zeros
     832           0 :             while( (nDecimal % 10) == 0 )
     833           0 :                 nDecimal /= 10;
     834           0 :             rBuffer.append( nDecimal );
     835             :         }
     836             :     }
     837           0 : }
     838             : 
     839             : 
     840             : // appends a double. PDF does not accept exponential format, only fixed point
     841           0 : static void appendDouble( double fValue, OStringBuffer& rBuffer, sal_Int32 nPrecision = 5 )
     842             : {
     843           0 :     bool bNeg = false;
     844           0 :     if( fValue < 0.0 )
     845             :     {
     846           0 :         bNeg = true;
     847           0 :         fValue=-fValue;
     848             :     }
     849             : 
     850           0 :     sal_Int64 nInt = (sal_Int64)fValue;
     851           0 :     fValue -= (double)nInt;
     852             :     // optimizing hardware may lead to a value of 1.0 after the subtraction
     853           0 :     if( fValue == 1.0 || log10( 1.0-fValue ) <= -nPrecision )
     854             :     {
     855           0 :         nInt++;
     856           0 :         fValue = 0.0;
     857             :     }
     858           0 :     sal_Int64 nFrac = 0;
     859           0 :     if( fValue )
     860             :     {
     861           0 :         fValue *= pow( 10.0, (double)nPrecision );
     862           0 :         nFrac = (sal_Int64)fValue;
     863             :     }
     864           0 :     if( bNeg && ( nInt || nFrac ) )
     865           0 :         rBuffer.append( '-' );
     866           0 :     rBuffer.append( nInt );
     867           0 :     if( nFrac )
     868             :     {
     869             :         int i;
     870           0 :         rBuffer.append( '.' );
     871           0 :         sal_Int64 nBound = (sal_Int64)(pow( 10.0, nPrecision - 1.0 )+0.5);
     872           0 :         for ( i = 0; ( i < nPrecision ) && nFrac; i++ )
     873             :         {
     874           0 :             sal_Int64 nNumb = nFrac / nBound;
     875           0 :             nFrac -= nNumb * nBound;
     876           0 :             rBuffer.append( nNumb );
     877           0 :             nBound /= 10;
     878             :         }
     879             :     }
     880           0 : }
     881             : 
     882             : 
     883           0 : static void appendColor( const Color& rColor, OStringBuffer& rBuffer, bool bConvertToGrey = false )
     884             : {
     885             : 
     886           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     887             :     {
     888           0 :         if( bConvertToGrey )
     889             :         {
     890           0 :             sal_uInt8 cByte = rColor.GetLuminance();
     891           0 :             appendDouble( (double)cByte / 255.0, rBuffer );
     892             :         }
     893             :         else
     894             :         {
     895           0 :             appendDouble( (double)rColor.GetRed() / 255.0, rBuffer );
     896           0 :             rBuffer.append( ' ' );
     897           0 :             appendDouble( (double)rColor.GetGreen() / 255.0, rBuffer );
     898           0 :             rBuffer.append( ' ' );
     899           0 :             appendDouble( (double)rColor.GetBlue() / 255.0, rBuffer );
     900             :         }
     901             :     }
     902           0 : }
     903             : 
     904           0 : void PDFWriterImpl::appendStrokingColor( const Color& rColor, OStringBuffer& rBuffer )
     905             : {
     906           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     907             :     {
     908           0 :         bool bGrey = m_aContext.ColorMode == PDFWriter::DrawGreyscale;
     909           0 :         appendColor( rColor, rBuffer, bGrey );
     910           0 :         rBuffer.append( bGrey ? " G" : " RG" );
     911             :     }
     912           0 : }
     913             : 
     914           0 : void PDFWriterImpl::appendNonStrokingColor( const Color& rColor, OStringBuffer& rBuffer )
     915             : {
     916           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     917             :     {
     918           0 :         bool bGrey = m_aContext.ColorMode == PDFWriter::DrawGreyscale;
     919           0 :         appendColor( rColor, rBuffer, bGrey );
     920           0 :         rBuffer.append( bGrey ? " g" : " rg" );
     921             :     }
     922           0 : }
     923             : 
     924             : // matrix helper class
     925             : // TODO: use basegfx matrix class instead or derive from it
     926             : namespace vcl // TODO: use anonymous namespace to keep this class local
     927             : {
     928             : /*  for sparse matrices of the form (2D linear transformations)
     929             :  *  f[0] f[1] 0
     930             :  *  f[2] f[3] 0
     931             :  *  f[4] f[5] 1
     932             :  */
     933             : class Matrix3
     934             : {
     935             :     double f[6];
     936             : 
     937           0 :     void set( double *pn ) { for( int i = 0 ; i < 6; i++ ) f[i] = pn[i]; }
     938             : public:
     939             :     Matrix3();
     940           0 :     ~Matrix3() {}
     941             : 
     942             :     void skew( double alpha, double beta );
     943             :     void scale( double sx, double sy );
     944             :     void rotate( double angle );
     945             :     void translate( double tx, double ty );
     946             :     bool invert();
     947             : 
     948             :     void append( PDFWriterImpl::PDFPage& rPage, OStringBuffer& rBuffer, Point* pBack = NULL );
     949             : 
     950             :     Point transform( const Point& rPoint ) const;
     951             : };
     952             : }
     953             : 
     954           0 : Matrix3::Matrix3()
     955             : {
     956             :     // initialize to unity
     957           0 :     f[0] = 1.0;
     958           0 :     f[1] = 0.0;
     959           0 :     f[2] = 0.0;
     960           0 :     f[3] = 1.0;
     961           0 :     f[4] = 0.0;
     962           0 :     f[5] = 0.0;
     963           0 : }
     964             : 
     965           0 : Point Matrix3::transform( const Point& rOrig ) const
     966             : {
     967           0 :     double x = (double)rOrig.X(), y = (double)rOrig.Y();
     968           0 :     return Point( (int)(x*f[0] + y*f[2] + f[4]), (int)(x*f[1] + y*f[3] + f[5]) );
     969             : }
     970             : 
     971           0 : void Matrix3::skew( double alpha, double beta )
     972             : {
     973             :     double fn[6];
     974           0 :     double tb = tan( beta );
     975           0 :     fn[0] = f[0] + f[2]*tb;
     976           0 :     fn[1] = f[1];
     977           0 :     fn[2] = f[2] + f[3]*tb;
     978           0 :     fn[3] = f[3];
     979           0 :     fn[4] = f[4] + f[5]*tb;
     980           0 :     fn[5] = f[5];
     981           0 :     if( alpha != 0.0 )
     982             :     {
     983           0 :         double ta = tan( alpha );
     984           0 :         fn[1] += f[0]*ta;
     985           0 :         fn[3] += f[2]*ta;
     986           0 :         fn[5] += f[4]*ta;
     987             :     }
     988           0 :     set( fn );
     989           0 : }
     990             : 
     991           0 : void Matrix3::scale( double sx, double sy )
     992             : {
     993             :     double fn[6];
     994           0 :     fn[0] = sx*f[0];
     995           0 :     fn[1] = sy*f[1];
     996           0 :     fn[2] = sx*f[2];
     997           0 :     fn[3] = sy*f[3];
     998           0 :     fn[4] = sx*f[4];
     999           0 :     fn[5] = sy*f[5];
    1000           0 :     set( fn );
    1001           0 : }
    1002             : 
    1003           0 : void Matrix3::rotate( double angle )
    1004             : {
    1005             :     double fn[6];
    1006           0 :     double fSin = sin(angle);
    1007           0 :     double fCos = cos(angle);
    1008           0 :     fn[0] = f[0]*fCos - f[1]*fSin;
    1009           0 :     fn[1] = f[0]*fSin + f[1]*fCos;
    1010           0 :     fn[2] = f[2]*fCos - f[3]*fSin;
    1011           0 :     fn[3] = f[2]*fSin + f[3]*fCos;
    1012           0 :     fn[4] = f[4]*fCos - f[5]*fSin;
    1013           0 :     fn[5] = f[4]*fSin + f[5]*fCos;
    1014           0 :     set( fn );
    1015           0 : }
    1016             : 
    1017           0 : void Matrix3::translate( double tx, double ty )
    1018             : {
    1019           0 :     f[4] += tx;
    1020           0 :     f[5] += ty;
    1021           0 : }
    1022             : 
    1023           0 : bool Matrix3::invert()
    1024             : {
    1025             :     // short circuit trivial cases
    1026           0 :     if( f[1]==f[2] && f[1]==0.0 && f[0]==f[3] && f[0]==1.0 )
    1027             :     {
    1028           0 :         f[4] = -f[4];
    1029           0 :         f[5] = -f[5];
    1030           0 :         return true;
    1031             :     }
    1032             : 
    1033             :     // check determinant
    1034           0 :     const double fDet = f[0]*f[3]-f[1]*f[2];
    1035           0 :     if( fDet == 0.0 )
    1036           0 :         return false;
    1037             : 
    1038             :     // invert the matrix
    1039             :     double fn[6];
    1040           0 :     fn[0] = +f[3] / fDet;
    1041           0 :     fn[1] = -f[1] / fDet;
    1042           0 :     fn[2] = -f[2] / fDet;
    1043           0 :     fn[3] = +f[0] / fDet;
    1044             : 
    1045             :     // apply inversion to translation
    1046           0 :     fn[4] = -(f[4]*fn[0] + f[5]*fn[2]);
    1047           0 :     fn[5] = -(f[4]*fn[1] + f[5]*fn[3]);
    1048             : 
    1049           0 :     set( fn );
    1050           0 :     return true;
    1051             : }
    1052             : 
    1053           0 : void Matrix3::append( PDFWriterImpl::PDFPage& rPage, OStringBuffer& rBuffer, Point* pBack )
    1054             : {
    1055           0 :     appendDouble( f[0], rBuffer );
    1056           0 :     rBuffer.append( ' ' );
    1057           0 :     appendDouble( f[1], rBuffer );
    1058           0 :     rBuffer.append( ' ' );
    1059           0 :     appendDouble( f[2], rBuffer );
    1060           0 :     rBuffer.append( ' ' );
    1061           0 :     appendDouble( f[3], rBuffer );
    1062           0 :     rBuffer.append( ' ' );
    1063           0 :     rPage.appendPoint( Point( (long)f[4], (long)f[5] ), rBuffer, false, pBack );
    1064           0 : }
    1065             : 
    1066           0 : static void appendResourceMap( OStringBuffer& rBuf, const char* pPrefix, const PDFWriterImpl::ResourceMap& rList )
    1067             : {
    1068           0 :     if( rList.empty() )
    1069           0 :         return;
    1070           0 :     rBuf.append( '/' );
    1071           0 :     rBuf.append( pPrefix );
    1072           0 :     rBuf.append( "<<" );
    1073           0 :     int ni = 0;
    1074           0 :     for( PDFWriterImpl::ResourceMap::const_iterator it = rList.begin(); it != rList.end(); ++it )
    1075             :     {
    1076           0 :         if( !it->first.isEmpty() && it->second > 0 )
    1077             :         {
    1078           0 :             rBuf.append( '/' );
    1079           0 :             rBuf.append( it->first );
    1080           0 :             rBuf.append( ' ' );
    1081           0 :             rBuf.append( it->second );
    1082           0 :             rBuf.append( " 0 R" );
    1083           0 :             if( ((++ni) & 7) == 0 )
    1084           0 :                 rBuf.append( '\n' );
    1085             :         }
    1086             :     }
    1087           0 :     rBuf.append( ">>\n" );
    1088             : }
    1089             : 
    1090           0 : void PDFWriterImpl::ResourceDict::append( OStringBuffer& rBuf, sal_Int32 nFontDictObject )
    1091             : {
    1092           0 :     rBuf.append( "<</Font " );
    1093           0 :     rBuf.append( nFontDictObject );
    1094           0 :     rBuf.append( " 0 R\n" );
    1095           0 :     appendResourceMap( rBuf, "XObject", m_aXObjects );
    1096           0 :     appendResourceMap( rBuf, "ExtGState", m_aExtGStates );
    1097           0 :     appendResourceMap( rBuf, "Shading", m_aShadings );
    1098           0 :     appendResourceMap( rBuf, "Pattern", m_aPatterns );
    1099           0 :     rBuf.append( "/ProcSet[/PDF/Text" );
    1100           0 :     if( !m_aXObjects.empty() )
    1101           0 :         rBuf.append( "/ImageC/ImageI/ImageB" );
    1102           0 :     rBuf.append( "]\n>>\n" );
    1103           0 : };
    1104             : 
    1105           0 : PDFWriterImpl::PDFPage::PDFPage( PDFWriterImpl* pWriter, sal_Int32 nPageWidth, sal_Int32 nPageHeight, PDFWriter::Orientation eOrientation )
    1106             :         :
    1107             :         m_pWriter( pWriter ),
    1108             :         m_nPageWidth( nPageWidth ),
    1109             :         m_nPageHeight( nPageHeight ),
    1110             :         m_eOrientation( eOrientation ),
    1111             :         m_nPageObject( 0 ),  // invalid object number
    1112             :         m_nPageIndex( -1 ), // invalid index
    1113             :         m_nStreamLengthObject( 0 ),
    1114             :         m_nBeginStreamPos( 0 ),
    1115             :         m_eTransition( PDFWriter::Regular ),
    1116             :         m_nTransTime( 0 ),
    1117             :         m_nDuration( 0 ),
    1118           0 :         m_bHasWidgets( false )
    1119             : {
    1120             :     // object ref must be only ever updated in emit()
    1121           0 :     m_nPageObject = m_pWriter->createObject();
    1122           0 : }
    1123             : 
    1124           0 : PDFWriterImpl::PDFPage::~PDFPage()
    1125             : {
    1126           0 : }
    1127             : 
    1128           0 : void PDFWriterImpl::PDFPage::beginStream()
    1129             : {
    1130             : #if OSL_DEBUG_LEVEL > 1
    1131             :     {
    1132             :         OStringBuffer aLine( "PDFWriterImpl::PDFPage::beginStream, +" );
    1133             :          m_pWriter->emitComment( aLine.getStr() );
    1134             :     }
    1135             : #endif
    1136           0 :     m_aStreamObjects.push_back(m_pWriter->createObject());
    1137           0 :     if( ! m_pWriter->updateObject( m_aStreamObjects.back() ) )
    1138           0 :         return;
    1139             : 
    1140           0 :     m_nStreamLengthObject = m_pWriter->createObject();
    1141             :     // write content stream header
    1142           0 :     OStringBuffer aLine;
    1143           0 :     aLine.append( m_aStreamObjects.back() );
    1144           0 :     aLine.append( " 0 obj\n<</Length " );
    1145           0 :     aLine.append( m_nStreamLengthObject );
    1146           0 :     aLine.append( " 0 R" );
    1147             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1148           0 :     aLine.append( "/Filter/FlateDecode" );
    1149             : #endif
    1150           0 :     aLine.append( ">>\nstream\n" );
    1151           0 :     if( ! m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() ) )
    1152           0 :         return;
    1153           0 :     if( osl_File_E_None != osl_getFilePos( m_pWriter->m_aFile, &m_nBeginStreamPos ) )
    1154             :     {
    1155           0 :         osl_closeFile( m_pWriter->m_aFile );
    1156           0 :         m_pWriter->m_bOpen = false;
    1157             :     }
    1158             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1159           0 :     m_pWriter->beginCompression();
    1160             : #endif
    1161           0 :     m_pWriter->checkAndEnableStreamEncryption( m_aStreamObjects.back() );
    1162             : }
    1163             : 
    1164           0 : void PDFWriterImpl::PDFPage::endStream()
    1165             : {
    1166             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1167           0 :     m_pWriter->endCompression();
    1168             : #endif
    1169             :     sal_uInt64 nEndStreamPos;
    1170           0 :     if( osl_File_E_None != osl_getFilePos( m_pWriter->m_aFile, &nEndStreamPos ) )
    1171             :     {
    1172           0 :         osl_closeFile( m_pWriter->m_aFile );
    1173           0 :         m_pWriter->m_bOpen = false;
    1174           0 :         return;
    1175             :     }
    1176           0 :     m_pWriter->disableStreamEncryption();
    1177           0 :     if( ! m_pWriter->writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    1178           0 :         return;
    1179             :     // emit stream length object
    1180           0 :     if( ! m_pWriter->updateObject( m_nStreamLengthObject ) )
    1181           0 :         return;
    1182           0 :     OStringBuffer aLine;
    1183           0 :     aLine.append( m_nStreamLengthObject );
    1184           0 :     aLine.append( " 0 obj\n" );
    1185           0 :     aLine.append( (sal_Int64)(nEndStreamPos-m_nBeginStreamPos) );
    1186           0 :     aLine.append( "\nendobj\n\n" );
    1187           0 :     m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() );
    1188             : }
    1189             : 
    1190           0 : bool PDFWriterImpl::PDFPage::emit(sal_Int32 nParentObject )
    1191             : {
    1192             :     // emit page object
    1193           0 :     if( ! m_pWriter->updateObject( m_nPageObject ) )
    1194           0 :         return false;
    1195           0 :     OStringBuffer aLine;
    1196             : 
    1197           0 :     aLine.append( m_nPageObject );
    1198             :     aLine.append( " 0 obj\n"
    1199           0 :                   "<</Type/Page/Parent " );
    1200           0 :     aLine.append( nParentObject );
    1201           0 :     aLine.append( " 0 R" );
    1202           0 :     aLine.append( "/Resources " );
    1203           0 :     aLine.append( m_pWriter->getResourceDictObj() );
    1204           0 :     aLine.append( " 0 R" );
    1205           0 :     if( m_nPageWidth && m_nPageHeight )
    1206             :     {
    1207           0 :         aLine.append( "/MediaBox[0 0 " );
    1208           0 :         aLine.append( m_nPageWidth );
    1209           0 :         aLine.append( ' ' );
    1210           0 :         aLine.append( m_nPageHeight );
    1211           0 :         aLine.append( "]" );
    1212             :     }
    1213           0 :     switch( m_eOrientation )
    1214             :     {
    1215           0 :         case PDFWriter::Landscape: aLine.append( "/Rotate 90\n" );break;
    1216           0 :         case PDFWriter::Seascape:  aLine.append( "/Rotate -90\n" );break;
    1217           0 :         case PDFWriter::Portrait:  aLine.append( "/Rotate 0\n" );break;
    1218             : 
    1219             :         case PDFWriter::Inherit:
    1220             :         default:
    1221           0 :             break;
    1222             :     }
    1223           0 :     int nAnnots = m_aAnnotations.size();
    1224           0 :     if( nAnnots > 0 )
    1225             :     {
    1226           0 :         aLine.append( "/Annots[\n" );
    1227           0 :         for( int i = 0; i < nAnnots; i++ )
    1228             :         {
    1229           0 :             aLine.append( m_aAnnotations[i] );
    1230           0 :             aLine.append( " 0 R" );
    1231           0 :             aLine.append( ((i+1)%15) ? " " : "\n" );
    1232             :         }
    1233           0 :         aLine.append( "]\n" );
    1234             :     }
    1235           0 :     if( m_aMCIDParents.size() > 0 )
    1236             :     {
    1237           0 :         OStringBuffer aStructParents( 1024 );
    1238           0 :         aStructParents.append( "[ " );
    1239           0 :         int nParents = m_aMCIDParents.size();
    1240           0 :         for( int i = 0; i < nParents; i++ )
    1241             :         {
    1242           0 :             aStructParents.append( m_aMCIDParents[i] );
    1243           0 :             aStructParents.append( " 0 R" );
    1244           0 :             aStructParents.append( ((i%10) == 9) ? "\n" : " " );
    1245             :         }
    1246           0 :         aStructParents.append( "]" );
    1247           0 :         m_pWriter->m_aStructParentTree.push_back( aStructParents.makeStringAndClear() );
    1248             : 
    1249           0 :         aLine.append( "/StructParents " );
    1250           0 :         aLine.append( sal_Int32(m_pWriter->m_aStructParentTree.size()-1) );
    1251           0 :         aLine.append( "\n" );
    1252             :     }
    1253           0 :     if( m_nDuration > 0 )
    1254             :     {
    1255           0 :         aLine.append( "/Dur " );
    1256           0 :         aLine.append( (sal_Int32)m_nDuration );
    1257           0 :         aLine.append( "\n" );
    1258             :     }
    1259           0 :     if( m_eTransition != PDFWriter::Regular && m_nTransTime > 0 )
    1260             :     {
    1261             :         // transition duration
    1262           0 :         aLine.append( "/Trans<</D " );
    1263           0 :         appendDouble( (double)m_nTransTime/1000.0, aLine, 3 );
    1264           0 :         aLine.append( "\n" );
    1265           0 :         const char *pStyle = NULL, *pDm = NULL, *pM = NULL, *pDi = NULL;
    1266           0 :         switch( m_eTransition )
    1267             :         {
    1268             :             case PDFWriter::SplitHorizontalInward:
    1269           0 :                 pStyle = "Split"; pDm = "H"; pM = "I"; break;
    1270             :             case PDFWriter::SplitHorizontalOutward:
    1271           0 :                 pStyle = "Split"; pDm = "H"; pM = "O"; break;
    1272             :             case PDFWriter::SplitVerticalInward:
    1273           0 :                 pStyle = "Split"; pDm = "V"; pM = "I"; break;
    1274             :             case PDFWriter::SplitVerticalOutward:
    1275           0 :                 pStyle = "Split"; pDm = "V"; pM = "O"; break;
    1276             :             case PDFWriter::BlindsHorizontal:
    1277           0 :                 pStyle = "Blinds"; pDm = "H"; break;
    1278             :             case PDFWriter::BlindsVertical:
    1279           0 :                 pStyle = "Blinds"; pDm = "V"; break;
    1280             :             case PDFWriter::BoxInward:
    1281           0 :                 pStyle = "Box"; pM = "I"; break;
    1282             :             case PDFWriter::BoxOutward:
    1283           0 :                 pStyle = "Box"; pM = "O"; break;
    1284             :             case PDFWriter::WipeLeftToRight:
    1285           0 :                 pStyle = "Wipe"; pDi = "0"; break;
    1286             :             case PDFWriter::WipeBottomToTop:
    1287           0 :                 pStyle = "Wipe"; pDi = "90"; break;
    1288             :             case PDFWriter::WipeRightToLeft:
    1289           0 :                 pStyle = "Wipe"; pDi = "180"; break;
    1290             :             case PDFWriter::WipeTopToBottom:
    1291           0 :                 pStyle = "Wipe"; pDi = "270"; break;
    1292             :             case PDFWriter::Dissolve:
    1293           0 :                 pStyle = "Dissolve"; break;
    1294             :             case PDFWriter::GlitterLeftToRight:
    1295           0 :                 pStyle = "Glitter"; pDi = "0"; break;
    1296             :             case PDFWriter::GlitterTopToBottom:
    1297           0 :                 pStyle = "Glitter"; pDi = "270"; break;
    1298             :             case PDFWriter::GlitterTopLeftToBottomRight:
    1299           0 :                 pStyle = "Glitter"; pDi = "315"; break;
    1300             :             case PDFWriter::Regular:
    1301           0 :                 break;
    1302             :         }
    1303             :         // transition style
    1304           0 :         if( pStyle )
    1305             :         {
    1306           0 :             aLine.append( "/S/" );
    1307           0 :             aLine.append( pStyle );
    1308           0 :             aLine.append( "\n" );
    1309             :         }
    1310           0 :         if( pDm )
    1311             :         {
    1312           0 :             aLine.append( "/Dm/" );
    1313           0 :             aLine.append( pDm );
    1314           0 :             aLine.append( "\n" );
    1315             :         }
    1316           0 :         if( pM )
    1317             :         {
    1318           0 :             aLine.append( "/M/" );
    1319           0 :             aLine.append( pM );
    1320           0 :             aLine.append( "\n" );
    1321             :         }
    1322           0 :         if( pDi  )
    1323             :         {
    1324           0 :             aLine.append( "/Di " );
    1325           0 :             aLine.append( pDi );
    1326           0 :             aLine.append( "\n" );
    1327             :         }
    1328           0 :         aLine.append( ">>\n" );
    1329             :     }
    1330           0 :     if( m_pWriter->getVersion() > PDFWriter::PDF_1_3 && ! m_pWriter->m_bIsPDF_A1 )
    1331             :     {
    1332           0 :         aLine.append( "/Group<</S/Transparency/CS/DeviceRGB/I true>>" );
    1333             :     }
    1334           0 :     aLine.append( "/Contents" );
    1335           0 :     unsigned int nStreamObjects = m_aStreamObjects.size();
    1336           0 :     if( nStreamObjects > 1 )
    1337           0 :         aLine.append( '[' );
    1338           0 :     for( unsigned int i = 0; i < m_aStreamObjects.size(); i++ )
    1339             :     {
    1340           0 :         aLine.append( ' ' );
    1341           0 :         aLine.append( m_aStreamObjects[i] );
    1342           0 :         aLine.append( " 0 R" );
    1343             :     }
    1344           0 :     if( nStreamObjects > 1 )
    1345           0 :         aLine.append( ']' );
    1346           0 :     aLine.append( ">>\nendobj\n\n" );
    1347           0 :     return m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() );
    1348             : }
    1349             : 
    1350             : namespace vcl
    1351             : {
    1352             : template < class GEOMETRY >
    1353           0 : GEOMETRY lcl_convert( const MapMode& _rSource, const MapMode& _rDest, OutputDevice* _pPixelConversion, const GEOMETRY& _rObject )
    1354             : {
    1355           0 :     GEOMETRY aPoint;
    1356           0 :     if ( MAP_PIXEL == _rSource.GetMapUnit() )
    1357             :     {
    1358           0 :         aPoint = _pPixelConversion->PixelToLogic( _rObject, _rDest );
    1359             :     }
    1360             :     else
    1361             :     {
    1362           0 :         aPoint = OutputDevice::LogicToLogic( _rObject, _rSource, _rDest );
    1363             :     }
    1364           0 :     return aPoint;
    1365             : }
    1366             : }
    1367             : 
    1368           0 : void PDFWriterImpl::PDFPage::appendPoint( const Point& rPoint, OStringBuffer& rBuffer, bool bNeg, Point* pOutPoint ) const
    1369             : {
    1370           0 :     if( pOutPoint )
    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           0 :         *pOutPoint = aPoint;
    1377             :     }
    1378             : 
    1379           0 :     Point aPoint( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1380             :                                m_pWriter->m_aMapMode,
    1381             :                                m_pWriter->getReferenceDevice(),
    1382           0 :                                rPoint ) );
    1383             : 
    1384           0 :     sal_Int32 nValue    = aPoint.X();
    1385           0 :     if( bNeg )
    1386           0 :         nValue = -nValue;
    1387             : 
    1388           0 :     appendFixedInt( nValue, rBuffer );
    1389             : 
    1390           0 :     rBuffer.append( ' ' );
    1391             : 
    1392           0 :     nValue      = pointToPixel(getHeight()) - aPoint.Y();
    1393           0 :     if( bNeg )
    1394           0 :         nValue = -nValue;
    1395             : 
    1396           0 :     appendFixedInt( nValue, rBuffer );
    1397           0 : }
    1398             : 
    1399           0 : void PDFWriterImpl::PDFPage::appendPixelPoint( const basegfx::B2DPoint& rPoint, OStringBuffer& rBuffer ) const
    1400             : {
    1401           0 :     double fValue   = pixelToPoint(rPoint.getX());
    1402             : 
    1403           0 :     appendDouble( fValue, rBuffer, nLog10Divisor );
    1404             : 
    1405           0 :     rBuffer.append( ' ' );
    1406             : 
    1407           0 :     fValue      = double(getHeight()) - pixelToPoint(rPoint.getY());
    1408             : 
    1409           0 :     appendDouble( fValue, rBuffer, nLog10Divisor );
    1410           0 : }
    1411             : 
    1412           0 : void PDFWriterImpl::PDFPage::appendRect( const Rectangle& rRect, OStringBuffer& rBuffer ) const
    1413             : {
    1414           0 :     appendPoint( rRect.BottomLeft() + Point( 0, 1 ), rBuffer );
    1415           0 :     rBuffer.append( ' ' );
    1416           0 :     appendMappedLength( (sal_Int32)rRect.GetWidth(), rBuffer, false );
    1417           0 :     rBuffer.append( ' ' );
    1418           0 :     appendMappedLength( (sal_Int32)rRect.GetHeight(), rBuffer, true );
    1419           0 :     rBuffer.append( " re" );
    1420           0 : }
    1421             : 
    1422           0 : void PDFWriterImpl::PDFPage::convertRect( Rectangle& rRect ) const
    1423             : {
    1424           0 :     Point aLL = lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1425             :                              m_pWriter->m_aMapMode,
    1426             :                              m_pWriter->getReferenceDevice(),
    1427           0 :                              rRect.BottomLeft() + Point( 0, 1 )
    1428           0 :                              );
    1429           0 :     Size aSize = lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1430             :                               m_pWriter->m_aMapMode,
    1431             :                               m_pWriter->getReferenceDevice(),
    1432           0 :                               rRect.GetSize() );
    1433           0 :     rRect.Left()    = aLL.X();
    1434           0 :     rRect.Right()   = aLL.X() + aSize.Width();
    1435           0 :     rRect.Top()     = pointToPixel(getHeight()) - aLL.Y();
    1436           0 :     rRect.Bottom()  = rRect.Top() + aSize.Height();
    1437           0 : }
    1438             : 
    1439           0 : void PDFWriterImpl::PDFPage::appendPolygon( const Polygon& rPoly, OStringBuffer& rBuffer, bool bClose ) const
    1440             : {
    1441           0 :     sal_uInt16 nPoints = rPoly.GetSize();
    1442             :     /*
    1443             :      *  #108582# applications do weird things
    1444             :      */
    1445           0 :     sal_uInt32 nBufLen = rBuffer.getLength();
    1446           0 :     if( nPoints > 0 )
    1447             :     {
    1448           0 :         const sal_uInt8* pFlagArray = rPoly.GetConstFlagAry();
    1449           0 :         appendPoint( rPoly[0], rBuffer );
    1450           0 :         rBuffer.append( " m\n" );
    1451           0 :         for( sal_uInt16 i = 1; i < nPoints; i++ )
    1452             :         {
    1453           0 :             if( pFlagArray && pFlagArray[i] == POLY_CONTROL && nPoints-i > 2 )
    1454             :             {
    1455             :                 // bezier
    1456             :                 DBG_ASSERT( pFlagArray[i+1] == POLY_CONTROL && pFlagArray[i+2] != POLY_CONTROL, "unexpected sequence of control points" );
    1457           0 :                 appendPoint( rPoly[i], rBuffer );
    1458           0 :                 rBuffer.append( " " );
    1459           0 :                 appendPoint( rPoly[i+1], rBuffer );
    1460           0 :                 rBuffer.append( " " );
    1461           0 :                 appendPoint( rPoly[i+2], rBuffer );
    1462           0 :                 rBuffer.append( " c" );
    1463           0 :                 i += 2; // add additionally consumed points
    1464             :             }
    1465             :             else
    1466             :             {
    1467             :                 // line
    1468           0 :                 appendPoint( rPoly[i], rBuffer );
    1469           0 :                 rBuffer.append( " l" );
    1470             :             }
    1471           0 :             if( (rBuffer.getLength() - nBufLen) > 65 )
    1472             :             {
    1473           0 :                 rBuffer.append( "\n" );
    1474           0 :                 nBufLen = rBuffer.getLength();
    1475             :             }
    1476             :             else
    1477           0 :                 rBuffer.append( " " );
    1478             :         }
    1479           0 :         if( bClose )
    1480           0 :             rBuffer.append( "h\n" );
    1481             :     }
    1482           0 : }
    1483             : 
    1484           0 : void PDFWriterImpl::PDFPage::appendPolygon( const basegfx::B2DPolygon& rPoly, OStringBuffer& rBuffer, bool bClose ) const
    1485             : {
    1486           0 :     basegfx::B2DPolygon aPoly( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1487             :                                             m_pWriter->m_aMapMode,
    1488             :                                             m_pWriter->getReferenceDevice(),
    1489           0 :                                             rPoly ) );
    1490             : 
    1491           0 :     if( basegfx::tools::isRectangle( aPoly ) )
    1492             :     {
    1493           0 :         basegfx::B2DRange aRange( aPoly.getB2DRange() );
    1494           0 :         basegfx::B2DPoint aBL( aRange.getMinX(), aRange.getMaxY() );
    1495           0 :         appendPixelPoint( aBL, rBuffer );
    1496           0 :         rBuffer.append( ' ' );
    1497           0 :         appendMappedLength( aRange.getWidth(), rBuffer, false, NULL, nLog10Divisor );
    1498           0 :         rBuffer.append( ' ' );
    1499           0 :         appendMappedLength( aRange.getHeight(), rBuffer, true, NULL, nLog10Divisor );
    1500           0 :         rBuffer.append( " re\n" );
    1501           0 :         return;
    1502             :     }
    1503           0 :     sal_uInt32 nPoints = aPoly.count();
    1504           0 :     if( nPoints > 0 )
    1505             :     {
    1506           0 :         sal_uInt32 nBufLen = rBuffer.getLength();
    1507           0 :         basegfx::B2DPoint aLastPoint( aPoly.getB2DPoint( 0 ) );
    1508           0 :         appendPixelPoint( aLastPoint, rBuffer );
    1509           0 :         rBuffer.append( " m\n" );
    1510           0 :         for( sal_uInt32 i = 1; i <= nPoints; i++ )
    1511             :         {
    1512           0 :             if( i != nPoints || aPoly.isClosed() )
    1513             :             {
    1514           0 :                 sal_uInt32 nCurPoint  = i % nPoints;
    1515           0 :                 sal_uInt32 nLastPoint = i-1;
    1516           0 :                 basegfx::B2DPoint aPoint( aPoly.getB2DPoint( nCurPoint ) );
    1517           0 :                 if( aPoly.isNextControlPointUsed( nLastPoint ) &&
    1518           0 :                     aPoly.isPrevControlPointUsed( nCurPoint ) )
    1519             :                 {
    1520           0 :                     appendPixelPoint( aPoly.getNextControlPoint( nLastPoint ), rBuffer );
    1521           0 :                     rBuffer.append( ' ' );
    1522           0 :                     appendPixelPoint( aPoly.getPrevControlPoint( nCurPoint ), rBuffer );
    1523           0 :                     rBuffer.append( ' ' );
    1524           0 :                     appendPixelPoint( aPoint, rBuffer );
    1525           0 :                     rBuffer.append( " c" );
    1526             :                 }
    1527           0 :                 else if( aPoly.isNextControlPointUsed( nLastPoint ) )
    1528             :                 {
    1529           0 :                     appendPixelPoint( aPoly.getNextControlPoint( nLastPoint ), rBuffer );
    1530           0 :                     rBuffer.append( ' ' );
    1531           0 :                     appendPixelPoint( aPoint, rBuffer );
    1532           0 :                     rBuffer.append( " y" );
    1533             :                 }
    1534           0 :                 else if( aPoly.isPrevControlPointUsed( nCurPoint ) )
    1535             :                 {
    1536           0 :                     appendPixelPoint( aPoly.getPrevControlPoint( nCurPoint ), rBuffer );
    1537           0 :                     rBuffer.append( ' ' );
    1538           0 :                     appendPixelPoint( aPoint, rBuffer );
    1539           0 :                     rBuffer.append( " v" );
    1540             :                 }
    1541             :                 else
    1542             :                 {
    1543           0 :                     appendPixelPoint( aPoint, rBuffer );
    1544           0 :                     rBuffer.append( " l" );
    1545             :                 }
    1546           0 :                 if( (rBuffer.getLength() - nBufLen) > 65 )
    1547             :                 {
    1548           0 :                     rBuffer.append( "\n" );
    1549           0 :                     nBufLen = rBuffer.getLength();
    1550             :                 }
    1551             :                 else
    1552           0 :                     rBuffer.append( " " );
    1553             :             }
    1554             :         }
    1555           0 :         if( bClose )
    1556           0 :             rBuffer.append( "h\n" );
    1557           0 :     }
    1558             : }
    1559             : 
    1560           0 : void PDFWriterImpl::PDFPage::appendPolyPolygon( const PolyPolygon& rPolyPoly, OStringBuffer& rBuffer, bool bClose ) const
    1561             : {
    1562           0 :     sal_uInt16 nPolygons = rPolyPoly.Count();
    1563           0 :     for( sal_uInt16 n = 0; n < nPolygons; n++ )
    1564           0 :         appendPolygon( rPolyPoly[n], rBuffer, bClose );
    1565           0 : }
    1566             : 
    1567           0 : void PDFWriterImpl::PDFPage::appendPolyPolygon( const basegfx::B2DPolyPolygon& rPolyPoly, OStringBuffer& rBuffer, bool bClose ) const
    1568             : {
    1569           0 :     sal_uInt32 nPolygons = rPolyPoly.count();
    1570           0 :     for( sal_uInt32 n = 0; n < nPolygons; n++ )
    1571           0 :         appendPolygon( rPolyPoly.getB2DPolygon( n ), rBuffer, bClose );
    1572           0 : }
    1573             : 
    1574           0 : void PDFWriterImpl::PDFPage::appendMappedLength( sal_Int32 nLength, OStringBuffer& rBuffer, bool bVertical, sal_Int32* pOutLength ) const
    1575             : {
    1576           0 :     sal_Int32 nValue = nLength;
    1577           0 :     if ( nLength < 0 )
    1578             :     {
    1579           0 :         rBuffer.append( '-' );
    1580           0 :         nValue = -nLength;
    1581             :     }
    1582           0 :     Size aSize( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1583             :                              m_pWriter->m_aMapMode,
    1584             :                              m_pWriter->getReferenceDevice(),
    1585           0 :                              Size( nValue, nValue ) ) );
    1586           0 :     nValue = bVertical ? aSize.Height() : aSize.Width();
    1587           0 :     if( pOutLength )
    1588           0 :         *pOutLength = ((nLength < 0 ) ? -nValue : nValue);
    1589             : 
    1590           0 :     appendFixedInt( nValue, rBuffer, 1 );
    1591           0 : }
    1592             : 
    1593           0 : void PDFWriterImpl::PDFPage::appendMappedLength( double fLength, OStringBuffer& rBuffer, bool bVertical, sal_Int32* pOutLength, sal_Int32 nPrecision ) const
    1594             : {
    1595           0 :     Size aSize( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1596             :                              m_pWriter->m_aMapMode,
    1597             :                              m_pWriter->getReferenceDevice(),
    1598           0 :                              Size( 1000, 1000 ) ) );
    1599           0 :     if( pOutLength )
    1600           0 :         *pOutLength = (sal_Int32)(fLength*(double)(bVertical ? aSize.Height() : aSize.Width())/1000.0);
    1601           0 :     fLength *= pixelToPoint((double)(bVertical ? aSize.Height() : aSize.Width()) / 1000.0);
    1602           0 :     appendDouble( fLength, rBuffer, nPrecision );
    1603           0 : }
    1604             : 
    1605           0 : bool PDFWriterImpl::PDFPage::appendLineInfo( const LineInfo& rInfo, OStringBuffer& rBuffer ) const
    1606             : {
    1607           0 :     if(LINE_DASH == rInfo.GetStyle() && rInfo.GetDashLen() != rInfo.GetDotLen())
    1608             :     {
    1609             :         // dashed and non-degraded case, check for implementation limits of dash array
    1610             :         // in PDF reader apps (e.g. acroread)
    1611           0 :         if(2 * (rInfo.GetDashCount() + rInfo.GetDotCount()) > 10)
    1612             :         {
    1613           0 :             return false;
    1614             :         }
    1615             :     }
    1616             : 
    1617           0 :     if(basegfx::B2DLINEJOIN_NONE != rInfo.GetLineJoin())
    1618             :     {
    1619             :         // LineJoin used, ExtLineInfo required
    1620           0 :         return false;
    1621             :     }
    1622             : 
    1623           0 :     if(com::sun::star::drawing::LineCap_BUTT != rInfo.GetLineCap())
    1624             :     {
    1625             :         // LineCap used, ExtLineInfo required
    1626           0 :         return false;
    1627             :     }
    1628             : 
    1629           0 :     if( rInfo.GetStyle() == LINE_DASH )
    1630             :     {
    1631           0 :         rBuffer.append( "[ " );
    1632           0 :         if( rInfo.GetDashLen() == rInfo.GetDotLen() ) // degraded case
    1633             :         {
    1634           0 :             appendMappedLength( (sal_Int32)rInfo.GetDashLen(), rBuffer );
    1635           0 :             rBuffer.append( ' ' );
    1636           0 :             appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1637           0 :             rBuffer.append( ' ' );
    1638             :         }
    1639             :         else
    1640             :         {
    1641           0 :             for( int n = 0; n < rInfo.GetDashCount(); n++ )
    1642             :             {
    1643           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDashLen(), rBuffer );
    1644           0 :                 rBuffer.append( ' ' );
    1645           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1646           0 :                 rBuffer.append( ' ' );
    1647             :             }
    1648           0 :             for( int m = 0; m < rInfo.GetDotCount(); m++ )
    1649             :             {
    1650           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDotLen(), rBuffer );
    1651           0 :                 rBuffer.append( ' ' );
    1652           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1653           0 :                 rBuffer.append( ' ' );
    1654             :             }
    1655             :         }
    1656           0 :         rBuffer.append( "] 0 d\n" );
    1657             :     }
    1658             : 
    1659           0 :     if( rInfo.GetWidth() > 1 )
    1660             :     {
    1661           0 :         appendMappedLength( (sal_Int32)rInfo.GetWidth(), rBuffer );
    1662           0 :         rBuffer.append( " w\n" );
    1663             :     }
    1664           0 :     else if( rInfo.GetWidth() == 0 )
    1665             :     {
    1666             :         // "pixel" line
    1667           0 :         appendDouble( 72.0/double(m_pWriter->getReferenceDevice()->ImplGetDPIX()), rBuffer );
    1668           0 :         rBuffer.append( " w\n" );
    1669             :     }
    1670             : 
    1671           0 :     return true;
    1672             : }
    1673             : 
    1674           0 : void PDFWriterImpl::PDFPage::appendWaveLine( sal_Int32 nWidth, sal_Int32 nY, sal_Int32 nDelta, OStringBuffer& rBuffer ) const
    1675             : {
    1676           0 :     if( nWidth <= 0 )
    1677           0 :         return;
    1678           0 :     if( nDelta < 1 )
    1679           0 :         nDelta = 1;
    1680             : 
    1681           0 :     rBuffer.append( "0 " );
    1682           0 :     appendMappedLength( nY, rBuffer, true );
    1683           0 :     rBuffer.append( " m\n" );
    1684           0 :     for( sal_Int32 n = 0; n < nWidth; )
    1685             :     {
    1686           0 :         n += nDelta;
    1687           0 :         appendMappedLength( n, rBuffer, false );
    1688           0 :         rBuffer.append( ' ' );
    1689           0 :         appendMappedLength( nDelta+nY, rBuffer, true );
    1690           0 :         rBuffer.append( ' ' );
    1691           0 :         n += nDelta;
    1692           0 :         appendMappedLength( n, rBuffer, false );
    1693           0 :         rBuffer.append( ' ' );
    1694           0 :         appendMappedLength( nY, rBuffer, true );
    1695           0 :         rBuffer.append( " v " );
    1696           0 :         if( n < nWidth )
    1697             :         {
    1698           0 :             n += nDelta;
    1699           0 :             appendMappedLength( n, rBuffer, false );
    1700           0 :             rBuffer.append( ' ' );
    1701           0 :             appendMappedLength( nY-nDelta, rBuffer, true );
    1702           0 :             rBuffer.append( ' ' );
    1703           0 :             n += nDelta;
    1704           0 :             appendMappedLength( n, rBuffer, false );
    1705           0 :             rBuffer.append( ' ' );
    1706           0 :             appendMappedLength( nY, rBuffer, true );
    1707           0 :             rBuffer.append( " v\n" );
    1708             :         }
    1709             :     }
    1710           0 :     rBuffer.append( "S\n" );
    1711             : }
    1712             : 
    1713             : /*
    1714             :  *  class PDFWriterImpl
    1715             :  */
    1716             : 
    1717           0 :  PDFWriterImpl::PDFWriterImpl( const PDFWriter::PDFWriterContext& rContext,
    1718             :                                const com::sun::star::uno::Reference< com::sun::star::beans::XMaterialHolder >& xEnc,
    1719             :                                PDFWriter& i_rOuterFace)
    1720             :         :
    1721             :         m_pReferenceDevice( NULL ),
    1722             :         m_aMapMode( MAP_POINT, Point(), Fraction( 1L, pointToPixel(1) ), Fraction( 1L, pointToPixel(1) ) ),
    1723             :         m_nCurrentStructElement( 0 ),
    1724             :         m_bEmitStructure( true ),
    1725             :         m_bNewMCID( false ),
    1726             :         m_nNextFID( 1 ),
    1727             :         m_nInheritedPageWidth( 595 ),  // default A4
    1728             :         m_nInheritedPageHeight( 842 ), // default A4
    1729             :         m_eInheritedOrientation( PDFWriter::Portrait ),
    1730             :         m_nCurrentPage( -1 ),
    1731             :         m_nSignatureObject( -1 ),
    1732             :         m_nSignatureContentOffset( 0 ),
    1733             :         m_nSignatureLastByteRangeNoOffset( 0 ),
    1734             :         m_nResourceDict( -1 ),
    1735             :         m_nFontDictObject( -1 ),
    1736             :         m_pCodec( NULL ),
    1737           0 :         m_aDocDigest( rtl_digest_createMD5() ),
    1738             :         m_aCipher( (rtlCipher)NULL ),
    1739             :         m_aDigest( NULL ),
    1740             :         m_bEncryptThisStream( false ),
    1741             :         m_pEncryptionBuffer( NULL ),
    1742             :         m_nEncryptionBufferSize( 0 ),
    1743             :         m_bIsPDF_A1( false ),
    1744           0 :         m_rOuterFace( i_rOuterFace )
    1745             : {
    1746             : #ifdef DO_TEST_PDF
    1747             :     static bool bOnce = true;
    1748             :     if( bOnce )
    1749             :     {
    1750             :         bOnce = false;
    1751             :         doTestCode();
    1752             :     }
    1753             : #endif
    1754           0 :     m_aContext = rContext;
    1755           0 :     m_aStructure.push_back( PDFStructureElement() );
    1756           0 :     m_aStructure[0].m_nOwnElement       = 0;
    1757           0 :     m_aStructure[0].m_nParentElement    = 0;
    1758             : 
    1759           0 :     Font aFont;
    1760           0 :     aFont.SetName( String( "Times" ) );
    1761           0 :     aFont.SetSize( Size( 0, 12 ) );
    1762             : 
    1763           0 :     GraphicsState aState;
    1764           0 :     aState.m_aMapMode       = m_aMapMode;
    1765           0 :     aState.m_aFont          = aFont;
    1766           0 :     m_aGraphicsStack.push_front( aState );
    1767             : 
    1768           0 :     oslFileError  aError = osl_openFile( m_aContext.URL.pData, &m_aFile, osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
    1769           0 :     if( aError != osl_File_E_None )
    1770             :     {
    1771           0 :         if( aError == osl_File_E_EXIST )
    1772             :         {
    1773           0 :             aError = osl_openFile( m_aContext.URL.pData, &m_aFile, osl_File_OpenFlag_Write );
    1774           0 :             if( aError == osl_File_E_None )
    1775           0 :                 aError = osl_setFileSize( m_aFile, 0 );
    1776             :         }
    1777             :     }
    1778           0 :     if( aError != osl_File_E_None )
    1779           0 :         return;
    1780             : 
    1781           0 :     m_bOpen = true;
    1782             : 
    1783             :     // setup DocInfo
    1784           0 :     setupDocInfo();
    1785             : 
    1786             :     /* prepare the cypher engine, can be done in CTOR, free in DTOR */
    1787           0 :     m_aCipher = rtl_cipher_createARCFOUR( rtl_Cipher_ModeStream );
    1788           0 :     m_aDigest = rtl_digest_createMD5();
    1789             : 
    1790             :     /* the size of the Codec default maximum */
    1791           0 :     checkEncryptionBufferSize( 0x4000 );
    1792             : 
    1793           0 :     if( xEnc.is() )
    1794           0 :         prepareEncryption( xEnc );
    1795             : 
    1796           0 :     if( m_aContext.Encryption.Encrypt() )
    1797             :     {
    1798             :         // sanity check
    1799           0 :         if( m_aContext.Encryption.OValue.size() != ENCRYPTED_PWD_SIZE ||
    1800           0 :             m_aContext.Encryption.UValue.size() != ENCRYPTED_PWD_SIZE ||
    1801           0 :             m_aContext.Encryption.EncryptionKey.size() != MAXIMUM_RC4_KEY_LENGTH
    1802             :            )
    1803             :         {
    1804             :             // the field lengths are invalid ? This was not setup by initEncryption.
    1805             :             // do not encrypt after all
    1806           0 :             m_aContext.Encryption.OValue.clear();
    1807           0 :             m_aContext.Encryption.UValue.clear();
    1808             :             OSL_ENSURE( 0, "encryption data failed sanity check, encryption disabled" );
    1809             :         }
    1810             :         else // setup key lengths
    1811           0 :             m_nAccessPermissions = computeAccessPermissions( m_aContext.Encryption, m_nKeyLength, m_nRC4KeyLength );
    1812             :     }
    1813             : 
    1814             :     // write header
    1815           0 :     OStringBuffer aBuffer( 20 );
    1816           0 :     aBuffer.append( "%PDF-" );
    1817           0 :     switch( m_aContext.Version )
    1818             :     {
    1819           0 :         case PDFWriter::PDF_1_2: aBuffer.append( "1.2" );break;
    1820           0 :         case PDFWriter::PDF_1_3: aBuffer.append( "1.3" );break;
    1821             :         case PDFWriter::PDF_A_1:
    1822             :         default:
    1823           0 :         case PDFWriter::PDF_1_4: aBuffer.append( "1.4" );break;
    1824           0 :         case PDFWriter::PDF_1_5: aBuffer.append( "1.5" );break;
    1825             :     }
    1826             :     // append something binary as comment (suggested in PDF Reference)
    1827           0 :     aBuffer.append( "\n%äüöß\n" );
    1828           0 :     if( !writeBuffer( aBuffer.getStr(), aBuffer.getLength() ) )
    1829             :     {
    1830           0 :         osl_closeFile( m_aFile );
    1831           0 :         m_bOpen = false;
    1832           0 :         return;
    1833             :     }
    1834             : 
    1835             :     // insert outline root
    1836           0 :     m_aOutline.push_back( PDFOutlineEntry() );
    1837             : 
    1838           0 :     m_bIsPDF_A1 = (m_aContext.Version == PDFWriter::PDF_A_1);
    1839           0 :     if( m_bIsPDF_A1 )
    1840           0 :         m_aContext.Version = PDFWriter::PDF_1_4; //meaning we need PDF 1.4, PDF/A flavour
    1841             : }
    1842             : 
    1843           0 : PDFWriterImpl::~PDFWriterImpl()
    1844             : {
    1845           0 :     if( m_aDocDigest )
    1846           0 :         rtl_digest_destroyMD5( m_aDocDigest );
    1847           0 :     delete static_cast<VirtualDevice*>(m_pReferenceDevice);
    1848             : 
    1849           0 :     if( m_aCipher )
    1850           0 :         rtl_cipher_destroyARCFOUR( m_aCipher );
    1851           0 :     if( m_aDigest )
    1852           0 :         rtl_digest_destroyMD5( m_aDigest );
    1853             : 
    1854           0 :     rtl_freeMemory( m_pEncryptionBuffer );
    1855           0 : }
    1856             : 
    1857           0 : void PDFWriterImpl::setupDocInfo()
    1858             : {
    1859           0 :     std::vector< sal_uInt8 > aId;
    1860           0 :     computeDocumentIdentifier( aId, m_aContext.DocumentInfo, m_aCreationDateString, m_aCreationMetaDateString );
    1861           0 :     if( m_aContext.Encryption.DocumentIdentifier.empty() )
    1862           0 :         m_aContext.Encryption.DocumentIdentifier = aId;
    1863           0 : }
    1864             : 
    1865           0 : void PDFWriterImpl::computeDocumentIdentifier( std::vector< sal_uInt8 >& o_rIdentifier,
    1866             :                                                const vcl::PDFWriter::PDFDocInfo& i_rDocInfo,
    1867             :                                                OString& o_rCString1,
    1868             :                                                OString& o_rCString2
    1869             :                                                )
    1870             : {
    1871           0 :     o_rIdentifier.clear();
    1872             : 
    1873             :     //build the document id
    1874           0 :     OString aInfoValuesOut;
    1875           0 :     OStringBuffer aID( 1024 );
    1876           0 :     if( i_rDocInfo.Title.Len() )
    1877           0 :         appendUnicodeTextString( i_rDocInfo.Title, aID );
    1878           0 :     if( i_rDocInfo.Author.Len() )
    1879           0 :         appendUnicodeTextString( i_rDocInfo.Author, aID );
    1880           0 :     if( i_rDocInfo.Subject.Len() )
    1881           0 :         appendUnicodeTextString( i_rDocInfo.Subject, aID );
    1882           0 :     if( i_rDocInfo.Keywords.Len() )
    1883           0 :         appendUnicodeTextString( i_rDocInfo.Keywords, aID );
    1884           0 :     if( i_rDocInfo.Creator.Len() )
    1885           0 :         appendUnicodeTextString( i_rDocInfo.Creator, aID );
    1886           0 :     if( i_rDocInfo.Producer.Len() )
    1887           0 :         appendUnicodeTextString( i_rDocInfo.Producer, aID );
    1888             : 
    1889             :     TimeValue aTVal, aGMT;
    1890             :     oslDateTime aDT;
    1891           0 :     osl_getSystemTime( &aGMT );
    1892           0 :     osl_getLocalTimeFromSystemTime( &aGMT, &aTVal );
    1893           0 :     osl_getDateTimeFromTimeValue( &aTVal, &aDT );
    1894           0 :     OStringBuffer aCreationDateString(64), aCreationMetaDateString(64);
    1895           0 :     aCreationDateString.append( "D:" );
    1896           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/1000)%10)) );
    1897           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/100)%10)) );
    1898           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/10)%10)) );
    1899           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year)%10)) );
    1900           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Month/10)%10)) );
    1901           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Month)%10)) );
    1902           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Day/10)%10)) );
    1903           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Day)%10)) );
    1904           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Hours/10)%10)) );
    1905           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Hours)%10)) );
    1906           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Minutes/10)%10)) );
    1907           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Minutes)%10)) );
    1908           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Seconds/10)%10)) );
    1909           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Seconds)%10)) );
    1910             : 
    1911             :     //--> i59651, we fill the Metadata date string as well, if PDF/A is requested
    1912             :     // according to ISO 19005-1:2005 6.7.3 the date is corrected for
    1913             :     // local time zone offset UTC only, whereas Acrobat 8 seems
    1914             :     // to use the localtime notation only
    1915             :     // according to a raccomandation in XMP Specification (Jan 2004, page 75)
    1916             :     // the Acrobat way seems the right approach
    1917           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/1000)%10)) );
    1918           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/100)%10)) );
    1919           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/10)%10)) );
    1920           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year)%10)) );
    1921           0 :     aCreationMetaDateString.append( "-" );
    1922           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Month/10)%10)) );
    1923           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Month)%10)) );
    1924           0 :     aCreationMetaDateString.append( "-" );
    1925           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Day/10)%10)) );
    1926           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Day)%10)) );
    1927           0 :     aCreationMetaDateString.append( "T" );
    1928           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Hours/10)%10)) );
    1929           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Hours)%10)) );
    1930           0 :     aCreationMetaDateString.append( ":" );
    1931           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Minutes/10)%10)) );
    1932           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Minutes)%10)) );
    1933           0 :     aCreationMetaDateString.append( ":" );
    1934           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Seconds/10)%10)) );
    1935           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Seconds)%10)) );
    1936             : 
    1937           0 :     sal_uInt32 nDelta = 0;
    1938           0 :     if( aGMT.Seconds > aTVal.Seconds )
    1939             :     {
    1940           0 :         aCreationDateString.append( "-" );
    1941           0 :         nDelta = aGMT.Seconds-aTVal.Seconds;
    1942           0 :         aCreationMetaDateString.append( "-" );
    1943             :     }
    1944           0 :     else if( aGMT.Seconds < aTVal.Seconds )
    1945             :     {
    1946           0 :         aCreationDateString.append( "+" );
    1947           0 :         nDelta = aTVal.Seconds-aGMT.Seconds;
    1948           0 :         aCreationMetaDateString.append( "+" );
    1949             :     }
    1950             :     else
    1951             :     {
    1952           0 :         aCreationDateString.append( "Z" );
    1953           0 :         aCreationMetaDateString.append( "Z" );
    1954             : 
    1955             :     }
    1956           0 :     if( nDelta )
    1957             :     {
    1958           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/36000)%10)) );
    1959           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/3600)%10)) );
    1960           0 :         aCreationDateString.append( "'" );
    1961           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/600)%6)) );
    1962           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/60)%10)) );
    1963             : 
    1964           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/36000)%10)) );
    1965           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/3600)%10)) );
    1966           0 :         aCreationMetaDateString.append( ":" );
    1967           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/600)%6)) );
    1968           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/60)%10)) );
    1969             :     }
    1970           0 :     aCreationDateString.append( "'" );
    1971           0 :     aID.append( aCreationDateString.getStr(), aCreationDateString.getLength() );
    1972             : 
    1973           0 :     aInfoValuesOut = aID.makeStringAndClear();
    1974           0 :     o_rCString1 = aCreationDateString.makeStringAndClear();
    1975           0 :     o_rCString2 = aCreationMetaDateString.makeStringAndClear();
    1976             : 
    1977           0 :     rtlDigest aDigest = rtl_digest_createMD5();
    1978             :     OSL_ENSURE( aDigest != NULL, "PDFWriterImpl::computeDocumentIdentifier: cannot obtain a digest object !" );
    1979           0 :     if( aDigest )
    1980             :     {
    1981           0 :         rtlDigestError nError = rtl_digest_updateMD5( aDigest, &aGMT, sizeof( aGMT ) );
    1982           0 :         if( nError == rtl_Digest_E_None )
    1983           0 :             nError = rtl_digest_updateMD5( aDigest, aInfoValuesOut.getStr(), aInfoValuesOut.getLength() );
    1984           0 :         if( nError == rtl_Digest_E_None )
    1985             :         {
    1986           0 :             o_rIdentifier = std::vector< sal_uInt8 >( 16, 0 );
    1987             :             //the binary form of the doc id is needed for encryption stuff
    1988           0 :             rtl_digest_getMD5( aDigest, &o_rIdentifier[0], 16 );
    1989             :         }
    1990           0 :         rtl_digest_destroyMD5(aDigest);
    1991           0 :     }
    1992           0 : }
    1993             : 
    1994             : /* i12626 methods */
    1995             : /*
    1996             : check if the Unicode string must be encrypted or not, perform the requested task,
    1997             : append the string as unicode hex, encrypted if needed
    1998             :  */
    1999           0 : inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2000             : {
    2001           0 :     rOutBuffer.append( "<" );
    2002           0 :     if( m_aContext.Encryption.Encrypt() )
    2003             :     {
    2004           0 :         const sal_Unicode* pStr = rInString.getStr();
    2005           0 :         sal_Int32 nLen = rInString.getLength();
    2006             : //prepare a unicode string, encrypt it
    2007           0 :         if( checkEncryptionBufferSize( nLen*2 ) )
    2008             :         {
    2009           0 :             enableStringEncryption( nInObjectNumber );
    2010           0 :             sal_uInt8 *pCopy = m_pEncryptionBuffer;
    2011           0 :             sal_Int32 nChars = 2;
    2012           0 :             *pCopy++ = 0xFE;
    2013           0 :             *pCopy++ = 0xFF;
    2014             : // we need to prepare a byte stream from the unicode string buffer
    2015           0 :             for( int i = 0; i < nLen; i++ )
    2016             :             {
    2017           0 :                 sal_Unicode aUnChar = pStr[i];
    2018           0 :                 *pCopy++ = (sal_uInt8)( aUnChar >> 8 );
    2019           0 :                 *pCopy++ = (sal_uInt8)( aUnChar & 255 );
    2020           0 :                 nChars += 2;
    2021             :             }
    2022             : //encrypt in place
    2023           0 :             rtl_cipher_encodeARCFOUR( m_aCipher, m_pEncryptionBuffer, nChars, m_pEncryptionBuffer, nChars );
    2024             : //now append, hexadecimal (appendHex), the encrypted result
    2025           0 :             for(int i = 0; i < nChars; i++)
    2026           0 :                 appendHex( m_pEncryptionBuffer[i], rOutBuffer );
    2027             :         }
    2028             :     }
    2029             :     else
    2030           0 :         appendUnicodeTextString( rInString, rOutBuffer );
    2031           0 :     rOutBuffer.append( ">" );
    2032           0 : }
    2033             : 
    2034           0 : inline void PDFWriterImpl::appendLiteralStringEncrypt( OStringBuffer& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2035             : {
    2036           0 :     rOutBuffer.append( "(" );
    2037           0 :     sal_Int32 nChars = rInString.getLength();
    2038             : //check for encryption, if ok, encrypt the string, then convert with appndLiteralString
    2039           0 :     if( m_aContext.Encryption.Encrypt() && checkEncryptionBufferSize( nChars ) )
    2040             :     {
    2041             : //encrypt the string in a buffer, then append it
    2042           0 :         enableStringEncryption( nInObjectNumber );
    2043           0 :         rtl_cipher_encodeARCFOUR( m_aCipher, rInString.getStr(), nChars, m_pEncryptionBuffer, nChars );
    2044           0 :         appendLiteralString( (const sal_Char*)m_pEncryptionBuffer, nChars, rOutBuffer );
    2045             :     }
    2046             :     else
    2047           0 :         appendLiteralString( rInString.getStr(), nChars , rOutBuffer );
    2048           0 :     rOutBuffer.append( ")" );
    2049           0 : }
    2050             : 
    2051           0 : inline void PDFWriterImpl::appendLiteralStringEncrypt( const OString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2052             : {
    2053           0 :     OStringBuffer aBufferString( rInString );
    2054           0 :     appendLiteralStringEncrypt( aBufferString, nInObjectNumber, rOutBuffer);
    2055           0 : }
    2056             : 
    2057           0 : void PDFWriterImpl::appendLiteralStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer, rtl_TextEncoding nEnc )
    2058             : {
    2059           0 :     OString aBufferString( OUStringToOString( rInString, nEnc ) );
    2060           0 :     sal_Int32 nLen = aBufferString.getLength();
    2061           0 :     OStringBuffer aBuf( nLen );
    2062           0 :     const sal_Char* pT = aBufferString.getStr();
    2063             : 
    2064           0 :     for( sal_Int32 i = 0; i < nLen; i++, pT++ )
    2065             :     {
    2066           0 :         if( (*pT & 0x80) == 0 )
    2067           0 :             aBuf.append( *pT );
    2068             :         else
    2069             :         {
    2070           0 :             aBuf.append( '<' );
    2071           0 :             appendHex( *pT, aBuf );
    2072           0 :             aBuf.append( '>' );
    2073             :         }
    2074             :     }
    2075           0 :     aBufferString = aBuf.makeStringAndClear();
    2076           0 :     appendLiteralStringEncrypt( aBufferString, nInObjectNumber, rOutBuffer);
    2077           0 : }
    2078             : 
    2079             : /* end i12626 methods */
    2080             : 
    2081           0 : void PDFWriterImpl::emitComment( const char* pComment )
    2082             : {
    2083           0 :     OStringBuffer aLine( 64 );
    2084           0 :     aLine.append( "% " );
    2085           0 :     aLine.append( (const sal_Char*)pComment );
    2086           0 :     aLine.append( "\n" );
    2087           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    2088           0 : }
    2089             : 
    2090           0 : bool PDFWriterImpl::compressStream( SvMemoryStream* pStream )
    2091             : {
    2092             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2093           0 :     pStream->Seek( STREAM_SEEK_TO_END );
    2094           0 :     sal_uLong nEndPos = pStream->Tell();
    2095           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    2096           0 :     ZCodec* pCodec = new ZCodec( 0x4000, 0x4000 );
    2097           0 :     SvMemoryStream aStream;
    2098           0 :     pCodec->BeginCompression();
    2099           0 :     pCodec->Write( aStream, (const sal_uInt8*)pStream->GetData(), nEndPos );
    2100           0 :     pCodec->EndCompression();
    2101           0 :     delete pCodec;
    2102           0 :     nEndPos = aStream.Tell();
    2103           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    2104           0 :     aStream.Seek( STREAM_SEEK_TO_BEGIN );
    2105           0 :     pStream->SetStreamSize( nEndPos );
    2106           0 :     pStream->Write( aStream.GetData(), nEndPos );
    2107           0 :     return true;
    2108             : #else
    2109             :     (void)pStream;
    2110             :     return false;
    2111             : #endif
    2112             : }
    2113             : 
    2114           0 : void PDFWriterImpl::beginCompression()
    2115             : {
    2116             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2117           0 :     m_pCodec = new ZCodec( 0x4000, 0x4000 );
    2118           0 :     m_pMemStream = new SvMemoryStream();
    2119           0 :     m_pCodec->BeginCompression();
    2120             : #endif
    2121           0 : }
    2122             : 
    2123           0 : void PDFWriterImpl::endCompression()
    2124             : {
    2125             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2126           0 :     if( m_pCodec )
    2127             :     {
    2128           0 :         m_pCodec->EndCompression();
    2129           0 :         delete m_pCodec;
    2130           0 :         m_pCodec = NULL;
    2131           0 :         sal_uInt64 nLen = m_pMemStream->Tell();
    2132           0 :         m_pMemStream->Seek( 0 );
    2133           0 :         writeBuffer( m_pMemStream->GetData(), nLen );
    2134           0 :         delete m_pMemStream;
    2135           0 :         m_pMemStream = NULL;
    2136             :     }
    2137             : #endif
    2138           0 : }
    2139             : 
    2140           0 : bool PDFWriterImpl::writeBuffer( const void* pBuffer, sal_uInt64 nBytes )
    2141             : {
    2142           0 :     if( ! m_bOpen ) // we are already down the drain
    2143           0 :         return false;
    2144             : 
    2145           0 :     if( ! nBytes ) // huh ?
    2146           0 :         return true;
    2147             : 
    2148           0 :     if( m_aOutputStreams.begin() != m_aOutputStreams.end() )
    2149             :     {
    2150           0 :         m_aOutputStreams.front().m_pStream->Seek( STREAM_SEEK_TO_END );
    2151           0 :         m_aOutputStreams.front().m_pStream->Write( pBuffer, sal::static_int_cast<sal_Size>(nBytes) );
    2152           0 :         return true;
    2153             :     }
    2154             : 
    2155             :     sal_uInt64 nWritten;
    2156           0 :     if( m_pCodec )
    2157             :     {
    2158           0 :         m_pCodec->Write( *m_pMemStream, static_cast<const sal_uInt8*>(pBuffer), (sal_uLong)nBytes );
    2159           0 :         nWritten = nBytes;
    2160             :     }
    2161             :     else
    2162             :     {
    2163           0 :         bool  buffOK = true;
    2164           0 :         if( m_bEncryptThisStream )
    2165             :         {
    2166             : /* implement the encryption part of the PDF spec encryption algorithm 3.1 */
    2167           0 :             if( ( buffOK = checkEncryptionBufferSize( static_cast<sal_Int32>(nBytes) ) ) )
    2168             :                 rtl_cipher_encodeARCFOUR( m_aCipher,
    2169             :                                           (sal_uInt8*)pBuffer, static_cast<sal_Size>(nBytes),
    2170           0 :                                           m_pEncryptionBuffer, static_cast<sal_Size>(nBytes) );
    2171             :         }
    2172             : 
    2173           0 :         const void* pWriteBuffer = ( m_bEncryptThisStream && buffOK ) ? m_pEncryptionBuffer  : pBuffer;
    2174           0 :         if( m_aDocDigest )
    2175           0 :             rtl_digest_updateMD5( m_aDocDigest, pWriteBuffer, static_cast<sal_uInt32>(nBytes) );
    2176             : 
    2177           0 :         if( osl_writeFile( m_aFile,
    2178             :                            pWriteBuffer,
    2179           0 :                            nBytes, &nWritten ) != osl_File_E_None )
    2180           0 :             nWritten = 0;
    2181             : 
    2182           0 :         if( nWritten != nBytes )
    2183             :         {
    2184           0 :             osl_closeFile( m_aFile );
    2185           0 :             m_bOpen = false;
    2186             :         }
    2187             :     }
    2188             : 
    2189           0 :     return nWritten == nBytes;
    2190             : }
    2191             : 
    2192           0 : OutputDevice* PDFWriterImpl::getReferenceDevice()
    2193             : {
    2194           0 :     if( ! m_pReferenceDevice )
    2195             :     {
    2196           0 :         VirtualDevice*  pVDev = new VirtualDevice( 0 );
    2197             : 
    2198           0 :         m_pReferenceDevice = pVDev;
    2199             : 
    2200           0 :         if( m_aContext.DPIx == 0 || m_aContext.DPIy == 0 )
    2201           0 :             pVDev->SetReferenceDevice( VirtualDevice::REFDEV_MODE_PDF1 );
    2202             :         else
    2203           0 :             pVDev->SetReferenceDevice( m_aContext.DPIx, m_aContext.DPIy );
    2204             : 
    2205           0 :         pVDev->SetOutputSizePixel( Size( 640, 480 ) );
    2206           0 :         pVDev->SetMapMode( MAP_MM );
    2207             : 
    2208           0 :         m_pReferenceDevice->mpPDFWriter = this;
    2209           0 :         m_pReferenceDevice->ImplUpdateFontData( sal_True );
    2210             :     }
    2211           0 :     return m_pReferenceDevice;
    2212             : }
    2213             : 
    2214           0 : class ImplPdfBuiltinFontData : public PhysicalFontFace
    2215             : {
    2216             : private:
    2217             :     const PDFWriterImpl::BuiltinFont& mrBuiltin;
    2218             : 
    2219             : public:
    2220             :     enum {PDF_FONT_MAGIC = 0xBDFF0A1C };
    2221             :                                         ImplPdfBuiltinFontData( const PDFWriterImpl::BuiltinFont& );
    2222           0 :     const PDFWriterImpl::BuiltinFont*   GetBuiltinFont() const  { return &mrBuiltin; }
    2223             : 
    2224           0 :     virtual PhysicalFontFace*           Clone() const { return new ImplPdfBuiltinFontData(*this); }
    2225             :     virtual ImplFontEntry*              CreateFontInstance( FontSelectPattern& ) const;
    2226           0 :     virtual sal_IntPtr                  GetFontId() const { return reinterpret_cast<sal_IntPtr>(&mrBuiltin); }
    2227             : };
    2228             : 
    2229           0 : inline const ImplPdfBuiltinFontData* GetPdfFontData( const PhysicalFontFace* pFontData )
    2230             : {
    2231           0 :     const ImplPdfBuiltinFontData* pFD = NULL;
    2232           0 :     if( pFontData && pFontData->CheckMagic( ImplPdfBuiltinFontData::PDF_FONT_MAGIC ) )
    2233           0 :         pFD = static_cast<const ImplPdfBuiltinFontData*>( pFontData );
    2234           0 :     return pFD;
    2235             : }
    2236             : 
    2237           0 : static ImplDevFontAttributes GetDevFontAttributes( const PDFWriterImpl::BuiltinFont& rBuiltin )
    2238             : {
    2239           0 :     ImplDevFontAttributes aDFA;
    2240           0 :     aDFA.SetFamilyName( OUString::createFromAscii( rBuiltin.m_pName ) );
    2241           0 :     aDFA.SetStyleName( OUString::createFromAscii( rBuiltin.m_pStyleName ) );
    2242           0 :     aDFA.SetFamilyType( rBuiltin.m_eFamily );
    2243           0 :     aDFA.SetSymbolFlag( rBuiltin.m_eCharSet != RTL_TEXTENCODING_MS_1252 );
    2244           0 :     aDFA.SetPitch( rBuiltin.m_ePitch );
    2245           0 :     aDFA.SetWeight( rBuiltin.m_eWeight );
    2246           0 :     aDFA.SetItalic( rBuiltin.m_eItalic );
    2247           0 :     aDFA.SetWidthType( rBuiltin.m_eWidthType );
    2248             : 
    2249           0 :     aDFA.mbOrientation  = true;
    2250           0 :     aDFA.mbDevice       = true;
    2251           0 :     aDFA.mnQuality      = 50000;
    2252           0 :     aDFA.mbSubsettable  = false;
    2253           0 :     aDFA.mbEmbeddable   = false;
    2254           0 :     return aDFA;
    2255             : }
    2256             : 
    2257           0 : ImplPdfBuiltinFontData::ImplPdfBuiltinFontData( const PDFWriterImpl::BuiltinFont& rBuiltin )
    2258             : :   PhysicalFontFace( GetDevFontAttributes(rBuiltin), PDF_FONT_MAGIC ),
    2259           0 :     mrBuiltin( rBuiltin )
    2260           0 : {}
    2261             : 
    2262           0 : ImplFontEntry* ImplPdfBuiltinFontData::CreateFontInstance( FontSelectPattern& rFSD ) const
    2263             : {
    2264           0 :     ImplFontEntry* pEntry = new ImplFontEntry( rFSD );
    2265           0 :     return pEntry;
    2266             : }
    2267             : 
    2268             : // -----------------------------------------------------------------------
    2269             : 
    2270           0 : sal_Int32 PDFWriterImpl::newPage( sal_Int32 nPageWidth, sal_Int32 nPageHeight, PDFWriter::Orientation eOrientation )
    2271             : {
    2272           0 :     endPage();
    2273           0 :     m_nCurrentPage = m_aPages.size();
    2274           0 :     m_aPages.push_back( PDFPage(this, nPageWidth, nPageHeight, eOrientation ) );
    2275           0 :     m_aPages.back().m_nPageIndex = m_nCurrentPage;
    2276           0 :     m_aPages.back().beginStream();
    2277             : 
    2278             :     // setup global graphics state
    2279             :     // linewidth is "1 pixel" by default
    2280           0 :     OStringBuffer aBuf( 16 );
    2281           0 :     appendDouble( 72.0/double(getReferenceDevice()->ImplGetDPIX()), aBuf );
    2282           0 :     aBuf.append( " w\n" );
    2283           0 :     writeBuffer( aBuf.getStr(), aBuf.getLength() );
    2284             : 
    2285           0 :     return m_nCurrentPage;
    2286             : }
    2287             : 
    2288           0 : void PDFWriterImpl::endPage()
    2289             : {
    2290           0 :     if( m_aPages.begin() != m_aPages.end() )
    2291             :     {
    2292             :         // close eventual MC sequence
    2293           0 :         endStructureElementMCSeq();
    2294             : 
    2295             :         // sanity check
    2296           0 :         if( m_aOutputStreams.begin() != m_aOutputStreams.end() )
    2297             :         {
    2298             :             OSL_FAIL( "redirection across pages !!!" );
    2299           0 :             m_aOutputStreams.clear(); // leak !
    2300           0 :             m_aMapMode.SetOrigin( Point() );
    2301             :         }
    2302             : 
    2303           0 :         m_aGraphicsStack.clear();
    2304           0 :         m_aGraphicsStack.push_back( GraphicsState() );
    2305             : 
    2306             :         // this should pop the PDF graphics stack if necessary
    2307           0 :         updateGraphicsState();
    2308             : 
    2309           0 :         m_aPages.back().endStream();
    2310             : 
    2311             :         // reset the default font
    2312           0 :         Font aFont;
    2313           0 :         aFont.SetName( String( "Times" ) );
    2314           0 :         aFont.SetSize( Size( 0, 12 ) );
    2315             : 
    2316           0 :         m_aCurrentPDFState = m_aGraphicsStack.front();
    2317           0 :         m_aGraphicsStack.front().m_aFont =  aFont;
    2318             : 
    2319           0 :         for( std::list<BitmapEmit>::iterator it = m_aBitmaps.begin();
    2320           0 :              it != m_aBitmaps.end(); ++it )
    2321             :         {
    2322           0 :             if( ! it->m_aBitmap.IsEmpty() )
    2323             :             {
    2324           0 :                 writeBitmapObject( *it );
    2325           0 :                 it->m_aBitmap = BitmapEx();
    2326             :             }
    2327             :         }
    2328           0 :         for( std::list<JPGEmit>::iterator jpeg = m_aJPGs.begin(); jpeg != m_aJPGs.end(); ++jpeg )
    2329             :         {
    2330           0 :             if( jpeg->m_pStream )
    2331             :             {
    2332           0 :                 writeJPG( *jpeg );
    2333           0 :                 delete jpeg->m_pStream;
    2334           0 :                 jpeg->m_pStream = NULL;
    2335           0 :                 jpeg->m_aMask = Bitmap();
    2336             :             }
    2337             :         }
    2338           0 :         for( std::list<TransparencyEmit>::iterator t = m_aTransparentObjects.begin();
    2339           0 :              t != m_aTransparentObjects.end(); ++t )
    2340             :         {
    2341           0 :             if( t->m_pContentStream )
    2342             :             {
    2343           0 :                 writeTransparentObject( *t );
    2344           0 :                 delete t->m_pContentStream;
    2345           0 :                 t->m_pContentStream = NULL;
    2346             :             }
    2347           0 :         }
    2348             :     }
    2349           0 : }
    2350             : 
    2351           0 : sal_Int32 PDFWriterImpl::createObject()
    2352             : {
    2353           0 :     m_aObjects.push_back( ~0U );
    2354           0 :     return m_aObjects.size();
    2355             : }
    2356             : 
    2357           0 : bool PDFWriterImpl::updateObject( sal_Int32 n )
    2358             : {
    2359           0 :     if( ! m_bOpen )
    2360           0 :         return false;
    2361             : 
    2362           0 :     sal_uInt64 nOffset = ~0U;
    2363           0 :     oslFileError aError = osl_getFilePos( m_aFile, &nOffset );
    2364             :     DBG_ASSERT( aError == osl_File_E_None, "could not register object" );
    2365           0 :     if( aError != osl_File_E_None )
    2366             :     {
    2367           0 :         osl_closeFile( m_aFile );
    2368           0 :         m_bOpen = false;
    2369             :     }
    2370           0 :     m_aObjects[ n-1 ] = nOffset;
    2371           0 :     return aError == osl_File_E_None;
    2372             : }
    2373             : 
    2374             : #define CHECK_RETURN( x ) if( !(x) ) return 0
    2375             : 
    2376           0 : sal_Int32 PDFWriterImpl::emitStructParentTree( sal_Int32 nObject )
    2377             : {
    2378           0 :     if( nObject > 0 )
    2379             :     {
    2380           0 :         OStringBuffer aLine( 1024 );
    2381             : 
    2382           0 :         aLine.append( nObject );
    2383             :         aLine.append( " 0 obj\n"
    2384           0 :                       "<</Nums[\n" );
    2385           0 :         sal_Int32 nTreeItems = m_aStructParentTree.size();
    2386           0 :         for( sal_Int32 n = 0; n < nTreeItems; n++ )
    2387             :         {
    2388           0 :             aLine.append( n );
    2389           0 :             aLine.append( ' ' );
    2390           0 :             aLine.append( m_aStructParentTree[n] );
    2391           0 :             aLine.append( "\n" );
    2392             :         }
    2393           0 :         aLine.append( "]>>\nendobj\n\n" );
    2394           0 :         CHECK_RETURN( updateObject( nObject ) );
    2395           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2396             :     }
    2397           0 :     return nObject;
    2398             : }
    2399             : 
    2400           0 : const sal_Char* PDFWriterImpl::getAttributeTag( PDFWriter::StructAttribute eAttr )
    2401             : {
    2402           0 :     static std::map< PDFWriter::StructAttribute, const char* > aAttributeStrings;
    2403             :     // fill maps once
    2404           0 :     if( aAttributeStrings.empty() )
    2405             :     {
    2406           0 :         aAttributeStrings[ PDFWriter::Placement ]           = "Placement";
    2407           0 :         aAttributeStrings[ PDFWriter::WritingMode ]         = "WritingMode";
    2408           0 :         aAttributeStrings[ PDFWriter::SpaceBefore ]         = "SpaceBefore";
    2409           0 :         aAttributeStrings[ PDFWriter::SpaceAfter ]          = "SpaceAfter";
    2410           0 :         aAttributeStrings[ PDFWriter::StartIndent ]         = "StartIndent";
    2411           0 :         aAttributeStrings[ PDFWriter::EndIndent ]           = "EndIndent";
    2412           0 :         aAttributeStrings[ PDFWriter::TextIndent ]          = "TextIndent";
    2413           0 :         aAttributeStrings[ PDFWriter::TextAlign ]           = "TextAlign";
    2414           0 :         aAttributeStrings[ PDFWriter::Width ]               = "Width";
    2415           0 :         aAttributeStrings[ PDFWriter::Height ]              = "Height";
    2416           0 :         aAttributeStrings[ PDFWriter::BlockAlign ]          = "BlockAlign";
    2417           0 :         aAttributeStrings[ PDFWriter::InlineAlign ]         = "InlineAlign";
    2418           0 :         aAttributeStrings[ PDFWriter::LineHeight ]          = "LineHeight";
    2419           0 :         aAttributeStrings[ PDFWriter::BaselineShift ]       = "BaselineShift";
    2420           0 :         aAttributeStrings[ PDFWriter::TextDecorationType ]  = "TextDecorationType";
    2421           0 :         aAttributeStrings[ PDFWriter::ListNumbering ]       = "ListNumbering";
    2422           0 :         aAttributeStrings[ PDFWriter::RowSpan ]             = "RowSpan";
    2423           0 :         aAttributeStrings[ PDFWriter::ColSpan ]             = "ColSpan";
    2424           0 :         aAttributeStrings[ PDFWriter::LinkAnnotation ]      = "LinkAnnotation";
    2425             :     }
    2426             : 
    2427             :     std::map< PDFWriter::StructAttribute, const char* >::const_iterator it =
    2428           0 :         aAttributeStrings.find( eAttr );
    2429             : 
    2430             : #if OSL_DEBUG_LEVEL > 1
    2431             :     if( it == aAttributeStrings.end() )
    2432             :         fprintf( stderr, "invalid PDFWriter::StructAttribute %d\n", eAttr );
    2433             : #endif
    2434             : 
    2435           0 :     return it != aAttributeStrings.end() ? it->second : "";
    2436             : }
    2437             : 
    2438           0 : const sal_Char* PDFWriterImpl::getAttributeValueTag( PDFWriter::StructAttributeValue eVal )
    2439             : {
    2440           0 :     static std::map< PDFWriter::StructAttributeValue, const char* > aValueStrings;
    2441             : 
    2442           0 :     if( aValueStrings.empty() )
    2443             :     {
    2444           0 :         aValueStrings[ PDFWriter::NONE ]                    = "None";
    2445           0 :         aValueStrings[ PDFWriter::Block ]                   = "Block";
    2446           0 :         aValueStrings[ PDFWriter::Inline ]                  = "Inline";
    2447           0 :         aValueStrings[ PDFWriter::Before ]                  = "Before";
    2448           0 :         aValueStrings[ PDFWriter::After ]                   = "After";
    2449           0 :         aValueStrings[ PDFWriter::Start ]                   = "Start";
    2450           0 :         aValueStrings[ PDFWriter::End ]                     = "End";
    2451           0 :         aValueStrings[ PDFWriter::LrTb ]                    = "LrTb";
    2452           0 :         aValueStrings[ PDFWriter::RlTb ]                    = "RlTb";
    2453           0 :         aValueStrings[ PDFWriter::TbRl ]                    = "TbRl";
    2454           0 :         aValueStrings[ PDFWriter::Center ]                  = "Center";
    2455           0 :         aValueStrings[ PDFWriter::Justify ]                 = "Justify";
    2456           0 :         aValueStrings[ PDFWriter::Auto ]                    = "Auto";
    2457           0 :         aValueStrings[ PDFWriter::Middle ]                  = "Middle";
    2458           0 :         aValueStrings[ PDFWriter::Normal ]                  = "Normal";
    2459           0 :         aValueStrings[ PDFWriter::Underline ]               = "Underline";
    2460           0 :         aValueStrings[ PDFWriter::Overline ]                = "Overline";
    2461           0 :         aValueStrings[ PDFWriter::LineThrough ]             = "LineThrough";
    2462           0 :         aValueStrings[ PDFWriter::Disc ]                    = "Disc";
    2463           0 :         aValueStrings[ PDFWriter::Circle ]                  = "Circle";
    2464           0 :         aValueStrings[ PDFWriter::Square ]                  = "Square";
    2465           0 :         aValueStrings[ PDFWriter::Decimal ]                 = "Decimal";
    2466           0 :         aValueStrings[ PDFWriter::UpperRoman ]              = "UpperRoman";
    2467           0 :         aValueStrings[ PDFWriter::LowerRoman ]              = "LowerRoman";
    2468           0 :         aValueStrings[ PDFWriter::UpperAlpha ]              = "UpperAlpha";
    2469           0 :         aValueStrings[ PDFWriter::LowerAlpha ]              = "LowerAlpha";
    2470             :     }
    2471             : 
    2472             :     std::map< PDFWriter::StructAttributeValue, const char* >::const_iterator it =
    2473           0 :         aValueStrings.find( eVal );
    2474             : 
    2475             : #if OSL_DEBUG_LEVEL > 1
    2476             :     if( it == aValueStrings.end() )
    2477             :         fprintf( stderr, "invalid PDFWriter::StructAttributeValue %d\n", eVal );
    2478             : #endif
    2479             : 
    2480           0 :     return it != aValueStrings.end() ? it->second : "";
    2481             : }
    2482             : 
    2483           0 : static void appendStructureAttributeLine( PDFWriter::StructAttribute i_eAttr, const PDFWriterImpl::PDFStructureAttribute& i_rVal, OStringBuffer& o_rLine, bool i_bIsFixedInt )
    2484             : {
    2485           0 :     o_rLine.append( "/" );
    2486           0 :     o_rLine.append( PDFWriterImpl::getAttributeTag( i_eAttr ) );
    2487             : 
    2488           0 :     if( i_rVal.eValue != PDFWriter::Invalid )
    2489             :     {
    2490           0 :         o_rLine.append( "/" );
    2491           0 :         o_rLine.append( PDFWriterImpl::getAttributeValueTag( i_rVal.eValue ) );
    2492             :     }
    2493             :     else
    2494             :     {
    2495             :         // numerical value
    2496           0 :         o_rLine.append( " " );
    2497           0 :         if( i_bIsFixedInt )
    2498           0 :             appendFixedInt( i_rVal.nValue, o_rLine );
    2499             :         else
    2500           0 :             o_rLine.append( i_rVal.nValue );
    2501             :     }
    2502           0 :     o_rLine.append( "\n" );
    2503           0 : }
    2504             : 
    2505           0 : OString PDFWriterImpl::emitStructureAttributes( PDFStructureElement& i_rEle )
    2506             : {
    2507             :     // create layout, list and table attribute sets
    2508           0 :     OStringBuffer aLayout(256), aList(64), aTable(64);
    2509           0 :     for( PDFStructAttributes::const_iterator it = i_rEle.m_aAttributes.begin();
    2510           0 :          it != i_rEle.m_aAttributes.end(); ++it )
    2511             :     {
    2512           0 :         if( it->first == PDFWriter::ListNumbering )
    2513           0 :             appendStructureAttributeLine( it->first, it->second, aList, true );
    2514           0 :         else if( it->first == PDFWriter::RowSpan ||
    2515           0 :                  it->first == PDFWriter::ColSpan )
    2516           0 :             appendStructureAttributeLine( it->first, it->second, aTable, false );
    2517           0 :         else if( it->first == PDFWriter::LinkAnnotation )
    2518             :         {
    2519           0 :             sal_Int32 nLink = it->second.nValue;
    2520             :             std::map< sal_Int32, sal_Int32 >::const_iterator link_it =
    2521           0 :                 m_aLinkPropertyMap.find( nLink );
    2522           0 :             if( link_it != m_aLinkPropertyMap.end() )
    2523           0 :                 nLink = link_it->second;
    2524           0 :             if( nLink >= 0 && nLink < (sal_Int32)m_aLinks.size() )
    2525             :             {
    2526             :                 // update struct parent of link
    2527           0 :                 OStringBuffer aStructParentEntry( 32 );
    2528           0 :                 aStructParentEntry.append( i_rEle.m_nObject );
    2529           0 :                 aStructParentEntry.append( " 0 R" );
    2530           0 :                 m_aStructParentTree.push_back( aStructParentEntry.makeStringAndClear() );
    2531           0 :                 m_aLinks[ nLink ].m_nStructParent = m_aStructParentTree.size()-1;
    2532             : 
    2533           0 :                 sal_Int32 nRefObject = createObject();
    2534           0 :                 OStringBuffer aRef( 256 );
    2535           0 :                 aRef.append( nRefObject );
    2536             :                 aRef.append( " 0 obj\n"
    2537           0 :                              "<</Type/OBJR/Obj " );
    2538           0 :                 aRef.append( m_aLinks[ nLink ].m_nObject );
    2539             :                 aRef.append( " 0 R>>\n"
    2540             :                              "endobj\n\n"
    2541           0 :                              );
    2542           0 :                 updateObject( nRefObject );
    2543           0 :                 writeBuffer( aRef.getStr(), aRef.getLength() );
    2544             : 
    2545           0 :                 i_rEle.m_aKids.push_back( PDFStructureElementKid( nRefObject ) );
    2546             :             }
    2547             :             else
    2548             :             {
    2549             :                 OSL_FAIL( "unresolved link id for Link structure" );
    2550             : #if OSL_DEBUG_LEVEL > 1
    2551             :                 fprintf( stderr, "unresolved link id %" SAL_PRIdINT32 " for Link structure\n", nLink );
    2552             :                 {
    2553             :                     OStringBuffer aLine( "unresolved link id " );
    2554             :                     aLine.append( nLink );
    2555             :                     aLine.append( " for Link structure" );
    2556             :                     emitComment( aLine.getStr() );
    2557             :                 }
    2558             : #endif
    2559             :             }
    2560             :         }
    2561             :         else
    2562           0 :             appendStructureAttributeLine( it->first, it->second, aLayout, true );
    2563             :     }
    2564           0 :     if( ! i_rEle.m_aBBox.IsEmpty() )
    2565             :     {
    2566           0 :         aLayout.append( "/BBox[" );
    2567           0 :         appendFixedInt( i_rEle.m_aBBox.Left(), aLayout );
    2568           0 :         aLayout.append( " " );
    2569           0 :         appendFixedInt( i_rEle.m_aBBox.Top(), aLayout );
    2570           0 :         aLayout.append( " " );
    2571           0 :         appendFixedInt( i_rEle.m_aBBox.Right(), aLayout );
    2572           0 :         aLayout.append( " " );
    2573           0 :         appendFixedInt( i_rEle.m_aBBox.Bottom(), aLayout );
    2574           0 :         aLayout.append( "]\n" );
    2575             :     }
    2576             : 
    2577           0 :     std::vector< sal_Int32 > aAttribObjects;
    2578           0 :     if( !aLayout.isEmpty() )
    2579             :     {
    2580           0 :         aAttribObjects.push_back( createObject() );
    2581           0 :         updateObject( aAttribObjects.back() );
    2582           0 :         OStringBuffer aObj( 64 );
    2583           0 :         aObj.append( aAttribObjects.back() );
    2584             :         aObj.append( " 0 obj\n"
    2585           0 :                      "<</O/Layout\n" );
    2586           0 :         aLayout.append( ">>\nendobj\n\n" );
    2587           0 :         writeBuffer( aObj.getStr(), aObj.getLength() );
    2588           0 :         writeBuffer( aLayout.getStr(), aLayout.getLength() );
    2589             :     }
    2590           0 :     if( !aList.isEmpty() )
    2591             :     {
    2592           0 :         aAttribObjects.push_back( createObject() );
    2593           0 :         updateObject( aAttribObjects.back() );
    2594           0 :         OStringBuffer aObj( 64 );
    2595           0 :         aObj.append( aAttribObjects.back() );
    2596             :         aObj.append( " 0 obj\n"
    2597           0 :                      "<</O/List\n" );
    2598           0 :         aList.append( ">>\nendobj\n\n" );
    2599           0 :         writeBuffer( aObj.getStr(), aObj.getLength() );
    2600           0 :         writeBuffer( aList.getStr(), aList.getLength() );
    2601             :     }
    2602           0 :     if( !aTable.isEmpty() )
    2603             :     {
    2604           0 :         aAttribObjects.push_back( createObject() );
    2605           0 :         updateObject( aAttribObjects.back() );
    2606           0 :         OStringBuffer aObj( 64 );
    2607           0 :         aObj.append( aAttribObjects.back() );
    2608             :         aObj.append( " 0 obj\n"
    2609           0 :                      "<</O/Table\n" );
    2610           0 :         aTable.append( ">>\nendobj\n\n" );
    2611           0 :         writeBuffer( aObj.getStr(), aObj.getLength() );
    2612           0 :         writeBuffer( aTable.getStr(), aTable.getLength() );
    2613             :     }
    2614             : 
    2615           0 :     OStringBuffer aRet( 64 );
    2616           0 :     if( aAttribObjects.size() > 1 )
    2617           0 :         aRet.append( " [" );
    2618           0 :     for( std::vector< sal_Int32 >::const_iterator at_it = aAttribObjects.begin();
    2619           0 :          at_it != aAttribObjects.end(); ++at_it )
    2620             :     {
    2621           0 :         aRet.append( " " );
    2622           0 :         aRet.append( *at_it );
    2623           0 :         aRet.append( " 0 R" );
    2624             :     }
    2625           0 :     if( aAttribObjects.size() > 1 )
    2626           0 :         aRet.append( " ]" );
    2627           0 :     return aRet.makeStringAndClear();
    2628             : }
    2629             : 
    2630           0 : sal_Int32 PDFWriterImpl::emitStructure( PDFStructureElement& rEle )
    2631             : {
    2632           0 :     if(
    2633             :        // do not emit NonStruct and its children
    2634           0 :        rEle.m_eType == PDFWriter::NonStructElement &&
    2635           0 :        rEle.m_nOwnElement != rEle.m_nParentElement // but of course emit the struct tree root
    2636             :        )
    2637           0 :         return 0;
    2638             : 
    2639           0 :     for( std::list< sal_Int32 >::const_iterator it = rEle.m_aChildren.begin(); it != rEle.m_aChildren.end(); ++it )
    2640             :     {
    2641           0 :         if( *it > 0 && *it < sal_Int32(m_aStructure.size()) )
    2642             :         {
    2643           0 :             PDFStructureElement& rChild = m_aStructure[ *it ];
    2644           0 :             if( rChild.m_eType != PDFWriter::NonStructElement )
    2645             :             {
    2646           0 :                 if( rChild.m_nParentElement == rEle.m_nOwnElement )
    2647           0 :                     emitStructure( rChild );
    2648             :                 else
    2649             :                 {
    2650             :                     OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure element" );
    2651             : #if OSL_DEBUG_LEVEL > 1
    2652             :                     fprintf( stderr, "PDFWriterImpl::emitStructure: invalid child structure elemnt with id %" SAL_PRIdINT32 "\n", *it );
    2653             : #endif
    2654             :                 }
    2655             :             }
    2656             :         }
    2657             :         else
    2658             :         {
    2659             :             OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure id" );
    2660             : #if OSL_DEBUG_LEVEL > 1
    2661             :             fprintf( stderr, "PDFWriterImpl::emitStructure: invalid child structure id %" SAL_PRIdINT32 "\n", *it );
    2662             : #endif
    2663             :         }
    2664             :     }
    2665             : 
    2666           0 :     OStringBuffer aLine( 512 );
    2667           0 :     aLine.append( rEle.m_nObject );
    2668             :     aLine.append( " 0 obj\n"
    2669           0 :                   "<</Type" );
    2670           0 :     sal_Int32 nParentTree = -1;
    2671           0 :     if( rEle.m_nOwnElement == rEle.m_nParentElement )
    2672             :     {
    2673           0 :         nParentTree = createObject();
    2674           0 :         CHECK_RETURN( nParentTree );
    2675           0 :         aLine.append( "/StructTreeRoot\n" );
    2676           0 :         aLine.append( "/ParentTree " );
    2677           0 :         aLine.append( nParentTree );
    2678           0 :         aLine.append( " 0 R\n" );
    2679           0 :         if( ! m_aRoleMap.empty() )
    2680             :         {
    2681           0 :             aLine.append( "/RoleMap<<" );
    2682           0 :             for( boost::unordered_map<OString,OString,OStringHash>::const_iterator
    2683           0 :                  it = m_aRoleMap.begin(); it != m_aRoleMap.end(); ++it )
    2684             :             {
    2685           0 :                 aLine.append( '/' );
    2686           0 :                 aLine.append(it->first);
    2687           0 :                 aLine.append( '/' );
    2688           0 :                 aLine.append( it->second );
    2689           0 :                 aLine.append( '\n' );
    2690             :             }
    2691           0 :             aLine.append( ">>\n" );
    2692             :         }
    2693             :     }
    2694             :     else
    2695             :     {
    2696             :         aLine.append( "/StructElem\n"
    2697           0 :                       "/S/" );
    2698           0 :         if( !rEle.m_aAlias.isEmpty() )
    2699           0 :             aLine.append( rEle.m_aAlias );
    2700             :         else
    2701           0 :             aLine.append( getStructureTag( rEle.m_eType ) );
    2702             :         aLine.append( "\n"
    2703           0 :                       "/P " );
    2704           0 :         aLine.append( m_aStructure[ rEle.m_nParentElement ].m_nObject );
    2705             :         aLine.append( " 0 R\n"
    2706           0 :                       "/Pg " );
    2707           0 :         aLine.append( rEle.m_nFirstPageObject );
    2708           0 :         aLine.append( " 0 R\n" );
    2709           0 :         if( !rEle.m_aActualText.isEmpty() )
    2710             :         {
    2711           0 :             aLine.append( "/ActualText" );
    2712           0 :             appendUnicodeTextStringEncrypt( rEle.m_aActualText, rEle.m_nObject, aLine );
    2713           0 :             aLine.append( "\n" );
    2714             :         }
    2715           0 :         if( !rEle.m_aAltText.isEmpty() )
    2716             :         {
    2717           0 :             aLine.append( "/Alt" );
    2718           0 :             appendUnicodeTextStringEncrypt( rEle.m_aAltText, rEle.m_nObject, aLine );
    2719           0 :             aLine.append( "\n" );
    2720             :         }
    2721             :     }
    2722           0 :     if( ! rEle.m_aBBox.IsEmpty() || rEle.m_aAttributes.size() )
    2723             :     {
    2724           0 :         OString aAttribs =  emitStructureAttributes( rEle );
    2725           0 :         if( !aAttribs.isEmpty() )
    2726             :         {
    2727           0 :             aLine.append( "/A" );
    2728           0 :             aLine.append( aAttribs );
    2729           0 :             aLine.append( "\n" );
    2730           0 :         }
    2731             :     }
    2732           0 :     if( !rEle.m_aLocale.Language.isEmpty() )
    2733             :     {
    2734           0 :         OUStringBuffer aLocBuf( 16 );
    2735           0 :         aLocBuf.append( rEle.m_aLocale.Language.toAsciiLowerCase() );
    2736           0 :         if( !rEle.m_aLocale.Country.isEmpty() )
    2737             :         {
    2738           0 :             aLocBuf.append( sal_Unicode('-') );
    2739           0 :             aLocBuf.append( rEle.m_aLocale.Country );
    2740             :         }
    2741           0 :         aLine.append( "/Lang" );
    2742           0 :         appendLiteralStringEncrypt( aLocBuf.makeStringAndClear(), rEle.m_nObject, aLine );
    2743           0 :         aLine.append( "\n" );
    2744             :     }
    2745           0 :     if( ! rEle.m_aKids.empty() )
    2746             :     {
    2747           0 :         unsigned int i = 0;
    2748           0 :         aLine.append( "/K[" );
    2749           0 :         for( std::list< PDFStructureElementKid >::const_iterator it =
    2750           0 :                  rEle.m_aKids.begin(); it != rEle.m_aKids.end(); ++it, i++ )
    2751             :         {
    2752           0 :             if( it->nMCID == -1 )
    2753             :             {
    2754           0 :                 aLine.append( it->nObject );
    2755           0 :                 aLine.append( " 0 R" );
    2756           0 :                 aLine.append( ( (i & 15) == 15 ) ? "\n" : " " );
    2757             :             }
    2758             :             else
    2759             :             {
    2760           0 :                 if( it->nObject == rEle.m_nFirstPageObject )
    2761             :                 {
    2762           0 :                     aLine.append( it->nMCID );
    2763           0 :                     aLine.append( " " );
    2764             :                 }
    2765             :                 else
    2766             :                 {
    2767           0 :                     aLine.append( "<</Type/MCR/Pg " );
    2768           0 :                     aLine.append( it->nObject );
    2769           0 :                     aLine.append( " 0 R /MCID " );
    2770           0 :                     aLine.append( it->nMCID );
    2771           0 :                     aLine.append( ">>\n" );
    2772             :                 }
    2773             :             }
    2774             :         }
    2775           0 :         aLine.append( "]\n" );
    2776             :     }
    2777           0 :     aLine.append( ">>\nendobj\n\n" );
    2778             : 
    2779           0 :     CHECK_RETURN( updateObject( rEle.m_nObject ) );
    2780           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2781             : 
    2782           0 :     CHECK_RETURN( emitStructParentTree( nParentTree ) );
    2783             : 
    2784           0 :     return rEle.m_nObject;
    2785             : }
    2786             : 
    2787           0 : bool PDFWriterImpl::emitGradients()
    2788             : {
    2789           0 :     for( std::list<GradientEmit>::iterator it = m_aGradients.begin();
    2790           0 :          it != m_aGradients.end(); ++it )
    2791             :     {
    2792           0 :         CHECK_RETURN( writeGradientFunction( *it ) );
    2793             :     }
    2794           0 :     return true;
    2795             : }
    2796             : 
    2797           0 : bool PDFWriterImpl::emitTilings()
    2798             : {
    2799           0 :     OStringBuffer aTilingObj( 1024 );
    2800             : 
    2801           0 :     for( std::vector<TilingEmit>::iterator it = m_aTilings.begin(); it != m_aTilings.end(); ++it )
    2802             :     {
    2803             :         DBG_ASSERT( it->m_pTilingStream, "tiling without stream" );
    2804           0 :         if( ! it->m_pTilingStream )
    2805           0 :             continue;
    2806             : 
    2807           0 :         aTilingObj.setLength( 0 );
    2808             : 
    2809             :         #if OSL_DEBUG_LEVEL > 1
    2810             :         emitComment( "PDFWriterImpl::emitTilings" );
    2811             :         #endif
    2812             : 
    2813           0 :         sal_Int32 nX = (sal_Int32)it->m_aRectangle.Left();
    2814           0 :         sal_Int32 nY = (sal_Int32)it->m_aRectangle.Top();
    2815           0 :         sal_Int32 nW = (sal_Int32)it->m_aRectangle.GetWidth();
    2816           0 :         sal_Int32 nH = (sal_Int32)it->m_aRectangle.GetHeight();
    2817           0 :         if( it->m_aCellSize.Width() == 0 )
    2818           0 :             it->m_aCellSize.Width() = nW;
    2819           0 :         if( it->m_aCellSize.Height() == 0 )
    2820           0 :             it->m_aCellSize.Height() = nH;
    2821             : 
    2822           0 :         bool bDeflate = compressStream( it->m_pTilingStream );
    2823           0 :         it->m_pTilingStream->Seek( STREAM_SEEK_TO_END );
    2824           0 :         sal_Size nTilingStreamSize = it->m_pTilingStream->Tell();
    2825           0 :         it->m_pTilingStream->Seek( STREAM_SEEK_TO_BEGIN );
    2826             : 
    2827             :         // write pattern object
    2828           0 :         aTilingObj.append( it->m_nObject );
    2829           0 :         aTilingObj.append( " 0 obj\n" );
    2830             :         aTilingObj.append( "<</Type/Pattern/PatternType 1\n"
    2831             :                            "/PaintType 1\n"
    2832             :                            "/TilingType 2\n"
    2833           0 :                            "/BBox[" );
    2834           0 :         appendFixedInt( nX, aTilingObj );
    2835           0 :         aTilingObj.append( ' ' );
    2836           0 :         appendFixedInt( nY, aTilingObj );
    2837           0 :         aTilingObj.append( ' ' );
    2838           0 :         appendFixedInt( nX+nW, aTilingObj );
    2839           0 :         aTilingObj.append( ' ' );
    2840           0 :         appendFixedInt( nY+nH, aTilingObj );
    2841             :         aTilingObj.append( "]\n"
    2842           0 :                            "/XStep " );
    2843           0 :         appendFixedInt( it->m_aCellSize.Width(), aTilingObj );
    2844             :         aTilingObj.append( "\n"
    2845           0 :                            "/YStep " );
    2846           0 :         appendFixedInt( it->m_aCellSize.Height(), aTilingObj );
    2847           0 :         aTilingObj.append( "\n" );
    2848           0 :         if( it->m_aTransform.matrix[0] != 1.0 ||
    2849           0 :             it->m_aTransform.matrix[1] != 0.0 ||
    2850           0 :             it->m_aTransform.matrix[3] != 0.0 ||
    2851           0 :             it->m_aTransform.matrix[4] != 1.0 ||
    2852           0 :             it->m_aTransform.matrix[2] != 0.0 ||
    2853           0 :             it->m_aTransform.matrix[5] != 0.0 )
    2854             :         {
    2855           0 :             aTilingObj.append( "/Matrix [" );
    2856             :             // TODO: scaling, mirroring on y, etc
    2857           0 :             appendDouble( it->m_aTransform.matrix[0], aTilingObj );
    2858           0 :             aTilingObj.append( ' ' );
    2859           0 :             appendDouble( it->m_aTransform.matrix[1], aTilingObj );
    2860           0 :             aTilingObj.append( ' ' );
    2861           0 :             appendDouble( it->m_aTransform.matrix[3], aTilingObj );
    2862           0 :             aTilingObj.append( ' ' );
    2863           0 :             appendDouble( it->m_aTransform.matrix[4], aTilingObj );
    2864           0 :             aTilingObj.append( ' ' );
    2865           0 :             appendDouble( it->m_aTransform.matrix[2], aTilingObj );
    2866           0 :             aTilingObj.append( ' ' );
    2867           0 :             appendDouble( it->m_aTransform.matrix[5], aTilingObj );
    2868           0 :             aTilingObj.append( "]\n" );
    2869             :         }
    2870           0 :         aTilingObj.append( "/Resources" );
    2871           0 :         it->m_aResources.append( aTilingObj, getFontDictObject() );
    2872           0 :         if( bDeflate )
    2873           0 :             aTilingObj.append( "/Filter/FlateDecode" );
    2874           0 :         aTilingObj.append( "/Length " );
    2875           0 :         aTilingObj.append( (sal_Int32)nTilingStreamSize );
    2876           0 :         aTilingObj.append( ">>\nstream\n" );
    2877           0 :         CHECK_RETURN( updateObject( it->m_nObject ) );
    2878           0 :         CHECK_RETURN( writeBuffer( aTilingObj.getStr(), aTilingObj.getLength() ) );
    2879           0 :         checkAndEnableStreamEncryption( it->m_nObject );
    2880           0 :         nTilingStreamSize = writeBuffer( it->m_pTilingStream->GetData(), nTilingStreamSize );
    2881           0 :         delete it->m_pTilingStream;
    2882           0 :         it->m_pTilingStream = NULL;
    2883           0 :         if( nTilingStreamSize == 0 )
    2884           0 :             return false;
    2885           0 :         disableStreamEncryption();
    2886           0 :         aTilingObj.setLength( 0 );
    2887           0 :         aTilingObj.append( "\nendstream\nendobj\n\n" );
    2888           0 :         CHECK_RETURN( writeBuffer( aTilingObj.getStr(), aTilingObj.getLength() ) );
    2889             :     }
    2890           0 :     return true;
    2891             : }
    2892             : 
    2893           0 : sal_Int32 PDFWriterImpl::emitBuiltinFont( const PhysicalFontFace* pFont, sal_Int32 nFontObject )
    2894             : {
    2895           0 :     const ImplPdfBuiltinFontData* pFD = GetPdfFontData( pFont );
    2896           0 :     if( !pFD )
    2897           0 :         return 0;
    2898           0 :     const BuiltinFont* pBuiltinFont = pFD->GetBuiltinFont();
    2899             : 
    2900           0 :     OStringBuffer aLine( 1024 );
    2901             : 
    2902           0 :     if( nFontObject <= 0 )
    2903           0 :         nFontObject = createObject();
    2904           0 :     CHECK_RETURN( updateObject( nFontObject ) );
    2905           0 :     aLine.append( nFontObject );
    2906             :     aLine.append( " 0 obj\n"
    2907           0 :                   "<</Type/Font/Subtype/Type1/BaseFont/" );
    2908           0 :     appendName( pBuiltinFont->m_pPSName, aLine );
    2909           0 :     aLine.append( "\n" );
    2910           0 :     if( pBuiltinFont->m_eCharSet == RTL_TEXTENCODING_MS_1252 )
    2911           0 :          aLine.append( "/Encoding/WinAnsiEncoding\n" );
    2912           0 :     aLine.append( ">>\nendobj\n\n" );
    2913           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2914           0 :     return nFontObject;
    2915             : }
    2916             : 
    2917           0 : std::map< sal_Int32, sal_Int32 > PDFWriterImpl::emitSystemFont( const PhysicalFontFace* pFont, EmbedFont& rEmbed )
    2918             : {
    2919           0 :     std::map< sal_Int32, sal_Int32 > aRet;
    2920             : 
    2921           0 :     sal_Int32 nFontDescriptor = 0;
    2922           0 :     OString aSubType( "/Type1" );
    2923           0 :     FontSubsetInfo aInfo;
    2924             :     // fill in dummy values
    2925           0 :     aInfo.m_nAscent = 1000;
    2926           0 :     aInfo.m_nDescent = 200;
    2927           0 :     aInfo.m_nCapHeight = 1000;
    2928           0 :     aInfo.m_aFontBBox = Rectangle( Point( -200, -200 ), Size( 1700, 1700 ) );
    2929           0 :     aInfo.m_aPSName = pFont->GetFamilyName();
    2930             :     sal_Int32 pWidths[256];
    2931           0 :     memset( pWidths, 0, sizeof(pWidths) );
    2932           0 :     if( pFont->IsEmbeddable() )
    2933             :     {
    2934           0 :         const unsigned char* pFontData = NULL;
    2935           0 :         long nFontLen = 0;
    2936             :         sal_Ucs nEncodedCodes[256];
    2937             :         sal_Int32 pEncWidths[256];
    2938           0 :         if( (pFontData = (const unsigned char*)m_pReferenceDevice->mpGraphics->GetEmbedFontData( pFont, nEncodedCodes, pEncWidths, aInfo, &nFontLen )) != NULL )
    2939             :         {
    2940           0 :             m_pReferenceDevice->mpGraphics->FreeEmbedFontData( pFontData, nFontLen );
    2941           0 :             for( int i = 0; i < 256; i++ )
    2942             :             {
    2943           0 :                 if( nEncodedCodes[i] >= 32 && nEncodedCodes[i] < 256 )
    2944             :                 {
    2945           0 :                     pWidths[i] = pEncWidths[ i ];
    2946             :                 }
    2947             :             }
    2948             :         }
    2949             :     }
    2950           0 :     else if( pFont->mbSubsettable )
    2951             :     {
    2952           0 :         aSubType = OString( "/TrueType" );
    2953           0 :         Int32Vector aGlyphWidths;
    2954           0 :         Ucs2UIntMap aUnicodeMap;
    2955           0 :         m_pReferenceDevice->mpGraphics->GetGlyphWidths( pFont, false, aGlyphWidths, aUnicodeMap );
    2956             : 
    2957           0 :         OUString aTmpName;
    2958           0 :         osl_createTempFile( NULL, NULL, &aTmpName.pData );
    2959             :         sal_Int32 pGlyphIDs[ 256 ];
    2960             :         sal_uInt8 pEncoding[ 256 ];
    2961             :         sal_Int32 pDuWidths[ 256 ];
    2962             : 
    2963           0 :         memset( pGlyphIDs, 0, sizeof( pGlyphIDs ) );
    2964           0 :         memset( pEncoding, 0, sizeof( pEncoding ) );
    2965           0 :         memset( pDuWidths, 0, sizeof( pDuWidths ) );
    2966             : 
    2967           0 :         for( sal_Ucs c = 32; c < 256; c++ )
    2968             :         {
    2969           0 :             pEncoding[c] = c;
    2970           0 :             pGlyphIDs[c] = 0;
    2971           0 :             if( aUnicodeMap.find( c ) != aUnicodeMap.end() )
    2972           0 :                 pWidths[ c ] = aGlyphWidths[ aUnicodeMap[ c ] ];
    2973             :         }
    2974             : 
    2975           0 :         m_pReferenceDevice->mpGraphics->CreateFontSubset( aTmpName, pFont, pGlyphIDs, pEncoding, pDuWidths, 256, aInfo );
    2976           0 :         osl_removeFile( aTmpName.pData );
    2977             :     }
    2978             :     else
    2979             :     {
    2980             :         OSL_FAIL( "system font neither embeddable nor subsettable" );
    2981             :     }
    2982             : 
    2983             :     // write font descriptor
    2984           0 :     nFontDescriptor = emitFontDescriptor( pFont, aInfo, 0, 0 );
    2985           0 :     if( nFontDescriptor )
    2986             :     {
    2987             :         // write font object
    2988           0 :         sal_Int32 nObject = createObject();
    2989           0 :         if( updateObject( nObject ) )
    2990             :         {
    2991           0 :             OStringBuffer aLine( 1024 );
    2992           0 :             aLine.append( nObject );
    2993             :             aLine.append( " 0 obj\n"
    2994           0 :                           "<</Type/Font/Subtype" );
    2995           0 :             aLine.append( aSubType );
    2996           0 :             aLine.append( "/BaseFont/" );
    2997           0 :             appendName( aInfo.m_aPSName, aLine );
    2998           0 :             aLine.append( "\n" );
    2999           0 :             if( !pFont->IsSymbolFont() )
    3000           0 :                 aLine.append( "/Encoding/WinAnsiEncoding\n" );
    3001             :             aLine.append( "/FirstChar 32 /LastChar 255\n"
    3002           0 :                           "/Widths[" );
    3003           0 :             for( int i = 32; i < 256; i++ )
    3004             :             {
    3005           0 :                 aLine.append( pWidths[i] );
    3006           0 :                 aLine.append( ((i&15) == 15) ? "\n" : " " );
    3007             :             }
    3008             :             aLine.append( "]\n"
    3009           0 :                           "/FontDescriptor " );
    3010           0 :             aLine.append( nFontDescriptor );
    3011             :             aLine.append( " 0 R>>\n"
    3012           0 :                           "endobj\n\n" );
    3013           0 :             writeBuffer( aLine.getStr(), aLine.getLength() );
    3014             : 
    3015           0 :             aRet[ rEmbed.m_nNormalFontID ] = nObject;
    3016             :         }
    3017             :     }
    3018             : 
    3019           0 :     return aRet;
    3020             : }
    3021             : 
    3022             : typedef int ThreeInts[3];
    3023           0 : static bool getPfbSegmentLengths( const unsigned char* pFontBytes, int nByteLen,
    3024             :     ThreeInts& rSegmentLengths )
    3025             : {
    3026           0 :     if( !pFontBytes || (nByteLen < 0) )
    3027           0 :         return false;
    3028           0 :     const unsigned char* pPtr = pFontBytes;
    3029           0 :     const unsigned char* pEnd = pFontBytes + nByteLen;
    3030             : 
    3031           0 :     for( int i = 0; i < 3; ++i) {
    3032             :         // read segment1 header
    3033           0 :         if( pPtr+6 >= pEnd )
    3034           0 :             return false;
    3035           0 :         if( (pPtr[0] != 0x80) || (pPtr[1] >= 0x03) )
    3036           0 :             return false;
    3037           0 :         const int nLen = (pPtr[5]<<24) + (pPtr[4]<<16) + (pPtr[3]<<8) + pPtr[2];
    3038           0 :         if( nLen <= 0)
    3039           0 :             return false;
    3040           0 :         rSegmentLengths[i] = nLen;
    3041           0 :         pPtr += nLen + 6;
    3042             :     }
    3043             : 
    3044             :     // read segment-end header
    3045           0 :     if( pPtr+2 >= pEnd )
    3046           0 :         return false;
    3047           0 :     if( (pPtr[0] != 0x80) || (pPtr[1] != 0x03) )
    3048           0 :         return false;
    3049             : 
    3050           0 :     return true;
    3051             : }
    3052             : 
    3053           0 : struct FontException : public std::exception
    3054             : {
    3055             : };
    3056             : 
    3057             : // TODO: always subset instead of embedding the full font => this method becomes obsolete then
    3058           0 : std::map< sal_Int32, sal_Int32 > PDFWriterImpl::emitEmbeddedFont( const PhysicalFontFace* pFont, EmbedFont& rEmbed )
    3059             : {
    3060           0 :     std::map< sal_Int32, sal_Int32 > aRet;
    3061             : 
    3062           0 :     sal_Int32 nStreamObject = 0;
    3063           0 :     sal_Int32 nFontDescriptor = 0;
    3064             : 
    3065             :     // prepare font encoding
    3066           0 :     const Ucs2SIntMap* pEncoding = m_pReferenceDevice->mpGraphics->GetFontEncodingVector( pFont, NULL );
    3067           0 :     sal_Int32 nToUnicodeStream = 0;
    3068             :     sal_uInt8 nEncoding[256];
    3069             :     sal_Ucs nEncodedCodes[256];
    3070           0 :     std::vector<sal_Ucs> aUnicodes;
    3071           0 :     aUnicodes.reserve( 256 );
    3072             :     sal_Int32 pUnicodesPerGlyph[256];
    3073             :     sal_Int32 pEncToUnicodeIndex[256];
    3074           0 :     if( pEncoding )
    3075             :     {
    3076           0 :         memset( nEncoding, 0, sizeof(nEncoding) );
    3077           0 :         memset( nEncodedCodes, 0, sizeof(nEncodedCodes) );
    3078           0 :         memset( pUnicodesPerGlyph, 0, sizeof(pUnicodesPerGlyph) );
    3079           0 :         memset( pEncToUnicodeIndex, 0, sizeof(pEncToUnicodeIndex) );
    3080           0 :         for( Ucs2SIntMap::const_iterator it = pEncoding->begin(); it != pEncoding->end(); ++it )
    3081             :         {
    3082           0 :             if( it->second != -1 )
    3083             :             {
    3084           0 :                 sal_Int32 nCode = (sal_Int32)(it->second & 0x000000ff);
    3085           0 :                 nEncoding[ nCode ] = static_cast<sal_uInt8>( nCode );
    3086           0 :                 nEncodedCodes[ nCode ] = it->first;
    3087           0 :                 pEncToUnicodeIndex[ nCode ] = static_cast<sal_Int32>(aUnicodes.size());
    3088           0 :                 aUnicodes.push_back( it->first );
    3089           0 :                 pUnicodesPerGlyph[ nCode ] = 1;
    3090             :             }
    3091             :         }
    3092             :     }
    3093             : 
    3094           0 :     FontSubsetInfo aInfo;
    3095             :     sal_Int32 pWidths[256];
    3096           0 :     const unsigned char* pFontData = NULL;
    3097           0 :     long nFontLen = 0;
    3098             :     sal_Int32 nLength1, nLength2;
    3099             :     try
    3100             :     {
    3101           0 :         if( (pFontData = (const unsigned char*)m_pReferenceDevice->mpGraphics->GetEmbedFontData( pFont, nEncodedCodes, pWidths, aInfo, &nFontLen )) != NULL )
    3102             :         {
    3103           0 :             if( (aInfo.m_nFontType & FontSubsetInfo::ANY_TYPE1) == 0 )
    3104           0 :                 throw FontException();
    3105             :             // see whether it is pfb or pfa; if it is a pfb, fill ranges
    3106             :             // of 6 bytes that are not part of the font program
    3107           0 :             std::list< int > aSections;
    3108           0 :             std::list< int >::const_iterator it;
    3109           0 :             int nIndex = 0;
    3110           0 :             while( (nIndex < nFontLen-1) && pFontData[nIndex] == 0x80 )
    3111             :             {
    3112           0 :                 aSections.push_back( nIndex );
    3113           0 :                 if( pFontData[nIndex+1] == 0x03 )
    3114           0 :                     break;
    3115             :                 sal_Int32 nBytes =
    3116           0 :                 ((sal_Int32)pFontData[nIndex+2])            |
    3117           0 :                 ((sal_Int32)pFontData[nIndex+3]) << 8       |
    3118           0 :                 ((sal_Int32)pFontData[nIndex+4]) << 16      |
    3119           0 :                 ((sal_Int32)pFontData[nIndex+5]) << 24;
    3120           0 :                 nIndex += nBytes+6;
    3121             :             }
    3122             : 
    3123             :             // search for eexec
    3124             :             // TODO: use getPfbSegmentLengths() if possible to skip the search thingies below
    3125           0 :             nIndex = 0;
    3126             :             int nEndAsciiIndex;
    3127             :             int nBeginBinaryIndex;
    3128             :             int nEndBinaryIndex;
    3129           0 :             do
    3130             :             {
    3131           0 :                 while( nIndex < nFontLen-4 &&
    3132           0 :                     ( pFontData[nIndex] != 'e'  ||
    3133           0 :                         pFontData[nIndex+1] != 'e' ||
    3134           0 :                         pFontData[nIndex+2] != 'x' ||
    3135           0 :                         pFontData[nIndex+3] != 'e' ||
    3136           0 :                         pFontData[nIndex+4] != 'c'
    3137             :                         )
    3138             :                     )
    3139             :                 {
    3140           0 :                     ++nIndex;
    3141             :                 }
    3142             :                 // check whether we are in a excluded section
    3143           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3144             :                     ;
    3145           0 :             } while( it != aSections.end() && nIndex < nFontLen-4 );
    3146             :             // this should end the ascii part
    3147           0 :             if( nIndex > nFontLen-5 )
    3148           0 :                 throw FontException();
    3149             : 
    3150           0 :             nEndAsciiIndex = nIndex+4;
    3151             :             // now count backwards until we can account for 512 '0'
    3152             :             // which is the endmarker of the (hopefully) binary data
    3153             :             // do not count the pfb header sections
    3154           0 :             int nFound = 0;
    3155           0 :             nIndex =  nFontLen-1;
    3156           0 :             while( nIndex > 0 && nFound < 512 )
    3157             :             {
    3158           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3159             :                     ;
    3160           0 :                 if( it == aSections.end() )
    3161             :                 {
    3162             :                     // inside the 512 '0' block there may only be whitespace
    3163             :                     // according to T1 spec; probably it would be to simple
    3164             :                     // if all fonts complied
    3165           0 :                     if( pFontData[nIndex] == '0' )
    3166           0 :                         nFound++;
    3167           0 :                         else if( nFound > 0                 &&
    3168           0 :                             pFontData[nIndex] != '\r'       &&
    3169           0 :                         pFontData[nIndex] != '\t'       &&
    3170           0 :                         pFontData[nIndex] != '\n'       &&
    3171           0 :                         pFontData[nIndex] != ' ' )
    3172           0 :                         break;
    3173             :                 }
    3174           0 :                 nIndex--;
    3175             :             }
    3176             : 
    3177           0 :             if( nIndex < 1 || nIndex <= nEndAsciiIndex )
    3178           0 :                 throw FontException();
    3179             : 
    3180             :             // nLength3 is the rest of the file - excluding any section headers
    3181             :             // nIndex now points before the first of the 512 '0' characters marking the
    3182             :             // fixed content portion
    3183           0 :             sal_Int32 nLength3 = nFontLen - nIndex - 1;
    3184           0 :             for( it = aSections.begin(); it != aSections.end(); ++it )
    3185             :             {
    3186             :                 // special case: nIndex inside a section marker
    3187           0 :                 if( nIndex >= (*it) && (*it)+6 > nIndex )
    3188           0 :                     nLength3 -= (*it)+6 - nIndex;
    3189           0 :                 else if( *it >= nIndex  )
    3190             :                 {
    3191           0 :                     if( *it < nFontLen - 6 )
    3192           0 :                         nLength3 -= 6;
    3193             :                     else // the last section 0x8003 is only 2 bytes after all
    3194           0 :                         nLength3 -= (nFontLen - *it);
    3195             :                 }
    3196             :             }
    3197             : 
    3198             :             // there may be whitespace to ignore before the 512 '0'
    3199           0 :             while( pFontData[nIndex] == '\r' || pFontData[nIndex] == '\n' )
    3200             :             {
    3201           0 :                 nIndex--;
    3202           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3203             :                     ;
    3204           0 :                 if( it != aSections.end() )
    3205             :                 {
    3206           0 :                     nIndex = (*it)-1;
    3207           0 :                     break; // this is surely a binary boundary, in ascii case it wouldn't matter
    3208             :                 }
    3209             :             }
    3210           0 :             nEndBinaryIndex = nIndex;
    3211             : 
    3212             :             // search for beginning of binary section
    3213           0 :             nBeginBinaryIndex = nEndAsciiIndex;
    3214           0 :             do
    3215             :             {
    3216           0 :                 nBeginBinaryIndex++;
    3217           0 :                 for( it = aSections.begin(); it != aSections.end() && (nBeginBinaryIndex < *it || nBeginBinaryIndex > ((*it) + 5) ); ++it )
    3218             :                     ;
    3219           0 :                     } while( nBeginBinaryIndex < nEndBinaryIndex &&
    3220           0 :                         ( pFontData[nBeginBinaryIndex] == '\r'  ||
    3221           0 :                             pFontData[nBeginBinaryIndex] == '\n'    ||
    3222           0 :                             it != aSections.end() ) );
    3223             : 
    3224             :                     // it seems to be vital to copy the exact whitespace between binary data
    3225             :                     // and eexec, else a invalid font results. so make nEndAsciiIndex
    3226             :                     // always immediate in front of nBeginBinaryIndex
    3227           0 :                     nEndAsciiIndex = nBeginBinaryIndex-1;
    3228           0 :                     for( it = aSections.begin(); it != aSections.end() && (nEndAsciiIndex < *it || nEndAsciiIndex > ((*it)+5)); ++it )
    3229             :                         ;
    3230           0 :                     if( it != aSections.end() )
    3231           0 :                         nEndAsciiIndex = (*it)-1;
    3232             : 
    3233           0 :                     nLength1 = nEndAsciiIndex+1; // including the last character
    3234           0 :                     for( it = aSections.begin(); it != aSections.end() && *it < nEndAsciiIndex; ++it )
    3235           0 :                         nLength1 -= 6; // decrease by pfb section size
    3236             : 
    3237             :                     // if the first four bytes are all ascii hex characters, then binary data
    3238             :                     // has to be converted to real binary data
    3239           0 :                     for( nIndex = 0; nIndex < 4 &&
    3240           0 :                         ( ( pFontData[ nBeginBinaryIndex+nIndex ] >= '0' && pFontData[ nBeginBinaryIndex+nIndex ] <= '9' ) ||
    3241           0 :                             ( pFontData[ nBeginBinaryIndex+nIndex ] >= 'a' && pFontData[ nBeginBinaryIndex+nIndex ] <= 'f' ) ||
    3242           0 :                             ( pFontData[ nBeginBinaryIndex+nIndex ] >= 'A' && pFontData[ nBeginBinaryIndex+nIndex ] <= 'F' )
    3243             :                             ); ++nIndex )
    3244             :                         ;
    3245           0 :                     bool bConvertHexData = true;
    3246           0 :                     if( nIndex < 4 )
    3247             :                     {
    3248           0 :                         bConvertHexData = false;
    3249           0 :                         nLength2 = nEndBinaryIndex - nBeginBinaryIndex + 1; // include the last byte
    3250           0 :                         for( it = aSections.begin(); it != aSections.end(); ++it )
    3251           0 :                             if( *it > nBeginBinaryIndex && *it < nEndBinaryIndex )
    3252           0 :                                 nLength2 -= 6;
    3253             :                     }
    3254             :                     else
    3255             :                     {
    3256             :                         // count the hex ascii characters to get nLength2
    3257           0 :                         nLength2 = 0;
    3258           0 :                         int nNextSectionIndex = 0;
    3259           0 :                         for( it = aSections.begin(); it != aSections.end() && *it < nBeginBinaryIndex; ++it )
    3260             :                             ;
    3261           0 :                         if( it != aSections.end() )
    3262           0 :                             nNextSectionIndex = *it;
    3263           0 :                         for( nIndex = nBeginBinaryIndex; nIndex <= nEndBinaryIndex; nIndex++ )
    3264             :                         {
    3265           0 :                             if( nIndex == nNextSectionIndex )
    3266             :                             {
    3267           0 :                                 nIndex += 6;
    3268           0 :                                 ++it;
    3269           0 :                                 nNextSectionIndex = (it == aSections.end() ? 0 : *it );
    3270             :                             }
    3271           0 :                             if( ( pFontData[ nIndex ] >= '0' && pFontData[ nIndex ] <= '9' ) ||
    3272           0 :                                 ( pFontData[ nIndex ] >= 'a' && pFontData[ nIndex ] <= 'f' ) ||
    3273           0 :                             ( pFontData[ nIndex ] >= 'A' && pFontData[ nIndex ] <= 'F' ) )
    3274           0 :                             nLength2++;
    3275             :                         }
    3276             :                         DBG_ASSERT( !(nLength2 & 1), "uneven number of hex chars in binary pfa section" );
    3277           0 :                         nLength2 /= 2;
    3278             :                     }
    3279             : 
    3280             :                     // now we can actually write the font stream !
    3281             :                     #if OSL_DEBUG_LEVEL > 1
    3282             :                     emitComment( " PDFWriterImpl::emitEmbeddedFont" );
    3283             :                     #endif
    3284           0 :                     OStringBuffer aLine( 512 );
    3285           0 :                     nStreamObject = createObject();
    3286           0 :                     if( !updateObject(nStreamObject))
    3287           0 :                         throw FontException();
    3288           0 :                     sal_Int32 nStreamLengthObject = createObject();
    3289           0 :                     aLine.append( nStreamObject );
    3290             :                     aLine.append( " 0 obj\n"
    3291           0 :                         "<</Length " );
    3292           0 :                     aLine.append( nStreamLengthObject );
    3293             :                     aLine.append( " 0 R"
    3294             :                         #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3295             :                         "/Filter/FlateDecode"
    3296             :                         #endif
    3297           0 :                         "/Length1 " );
    3298           0 :                     aLine.append( nLength1 );
    3299           0 :                     aLine.append( " /Length2 " );
    3300           0 :                     aLine.append( nLength2 );
    3301           0 :                     aLine.append( " /Length3 ");
    3302           0 :                     aLine.append( nLength3 );
    3303             :                     aLine.append( ">>\n"
    3304           0 :                         "stream\n" );
    3305           0 :                     if( !writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3306           0 :                         throw FontException();
    3307             : 
    3308           0 :                     sal_uInt64 nBeginStreamPos = 0;
    3309           0 :                     osl_getFilePos( m_aFile, &nBeginStreamPos );
    3310             : 
    3311           0 :                     beginCompression();
    3312           0 :                     checkAndEnableStreamEncryption( nStreamObject );
    3313             : 
    3314             :                     // write ascii section
    3315           0 :                     if( aSections.begin() == aSections.end() )
    3316             :                     {
    3317           0 :                         if( ! writeBuffer( pFontData, nEndAsciiIndex+1 ) )
    3318           0 :                             throw FontException();
    3319             :                     }
    3320             :                     else
    3321             :                     {
    3322             :                         // first section always starts at 0
    3323           0 :                         it = aSections.begin();
    3324           0 :                         nIndex = (*it)+6;
    3325           0 :                         ++it;
    3326           0 :                         while( *it < nEndAsciiIndex )
    3327             :                         {
    3328           0 :                             if( ! writeBuffer( pFontData+nIndex, (*it)-nIndex ) )
    3329           0 :                                 throw FontException();
    3330           0 :                             nIndex = (*it)+6;
    3331           0 :                             ++it;
    3332             :                         }
    3333             :                         // write partial last section
    3334           0 :                         if( ! writeBuffer( pFontData+nIndex, nEndAsciiIndex-nIndex+1 ) )
    3335           0 :                             throw FontException();
    3336             :                     }
    3337             : 
    3338             :                     // write binary section
    3339           0 :                     if( ! bConvertHexData )
    3340             :                     {
    3341           0 :                         if( aSections.begin() == aSections.end() )
    3342             :                         {
    3343           0 :                             if( ! writeBuffer( pFontData+nBeginBinaryIndex, nFontLen-nBeginBinaryIndex ) )
    3344           0 :                                 throw FontException();
    3345             :                         }
    3346             :                         else
    3347             :                         {
    3348           0 :                             for( it = aSections.begin(); *it < nBeginBinaryIndex; ++it )
    3349             :                                 ;
    3350             :                             // write first partial section
    3351           0 :                             if( ! writeBuffer( pFontData+nBeginBinaryIndex, (*it) - nBeginBinaryIndex ) )
    3352           0 :                                 throw FontException();
    3353             :                             // write following sections
    3354           0 :                             while( it != aSections.end() )
    3355             :                             {
    3356           0 :                                 nIndex = (*it)+6;
    3357           0 :                                 ++it;
    3358           0 :                                 if( nIndex < nFontLen ) // last section marker is usually the EOF which has only 2 bytes
    3359             :                                 {
    3360           0 :                                     sal_Int32 nSectionLen = (it == aSections.end()) ? nFontLen - nIndex : (*it) - nIndex;
    3361           0 :                                     if( ! writeBuffer( pFontData+nIndex, nSectionLen ) )
    3362           0 :                                         throw FontException();
    3363             :                                 }
    3364             :                             }
    3365             :                         }
    3366             :                     }
    3367             :                     else
    3368             :                     {
    3369           0 :                         boost::shared_array<unsigned char> pWriteBuffer( new unsigned char[ nLength2 ] );
    3370           0 :                         memset( pWriteBuffer.get(), 0, nLength2 );
    3371           0 :                         int nWriteIndex = 0;
    3372             : 
    3373           0 :                         int nNextSectionIndex = 0;
    3374           0 :                         for( it = aSections.begin(); it != aSections.end() && *it < nBeginBinaryIndex; ++it )
    3375             :                             ;
    3376           0 :                         if( it != aSections.end() )
    3377           0 :                             nNextSectionIndex = *it;
    3378           0 :                         for( nIndex = nBeginBinaryIndex; nIndex <= nEndBinaryIndex; nIndex++ )
    3379             :                         {
    3380           0 :                             if( nIndex == nNextSectionIndex )
    3381             :                             {
    3382           0 :                                 nIndex += 6;
    3383           0 :                                 ++it;
    3384           0 :                                 nNextSectionIndex = (it == aSections.end() ? nFontLen : *it );
    3385             :                             }
    3386           0 :                             unsigned char cNibble = 0x80;
    3387           0 :                             if( pFontData[ nIndex ] >= '0' && pFontData[ nIndex ] <= '9' )
    3388           0 :                                 cNibble = pFontData[nIndex] - '0';
    3389           0 :                             else if( pFontData[ nIndex ] >= 'a' && pFontData[ nIndex ] <= 'f' )
    3390           0 :                                 cNibble = pFontData[nIndex] - 'a' + 10;
    3391           0 :                             else if( pFontData[ nIndex ] >= 'A' && pFontData[ nIndex ] <= 'F' )
    3392           0 :                                 cNibble = pFontData[nIndex] - 'A' + 10;
    3393           0 :                             if( cNibble != 0x80 )
    3394             :                             {
    3395           0 :                                 if( !(nWriteIndex & 1 ) )
    3396           0 :                                     cNibble <<= 4;
    3397           0 :                                 pWriteBuffer.get()[ nWriteIndex/2 ] |= cNibble;
    3398           0 :                                 nWriteIndex++;
    3399             :                             }
    3400             :                         }
    3401           0 :                         if( ! writeBuffer( pWriteBuffer.get(), nLength2 ) )
    3402           0 :                             throw FontException();
    3403           0 :                         if( aSections.empty() )
    3404             :                         {
    3405           0 :                             if( ! writeBuffer( pFontData+nIndex, nFontLen-nIndex ) )
    3406           0 :                                 throw FontException();
    3407             :                         }
    3408             :                         else
    3409             :                         {
    3410             :                             // write rest of this section
    3411           0 :                             if( nIndex < nNextSectionIndex )
    3412             :                             {
    3413           0 :                                 if( ! writeBuffer( pFontData+nIndex, nNextSectionIndex - nIndex ) )
    3414           0 :                                     throw FontException();
    3415             :                             }
    3416             :                             // write following sections
    3417           0 :                             while( it != aSections.end() )
    3418             :                             {
    3419           0 :                                 nIndex = (*it)+6;
    3420           0 :                                 ++it;
    3421           0 :                                 if( nIndex < nFontLen ) // last section marker is usually the EOF which has only 2 bytes
    3422             :                                 {
    3423           0 :                                     sal_Int32 nSectionLen = (it == aSections.end()) ? nFontLen - nIndex : (*it) - nIndex;
    3424           0 :                                     if( ! writeBuffer( pFontData+nIndex, nSectionLen ) )
    3425           0 :                                         throw FontException();
    3426             :                                 }
    3427             :                             }
    3428           0 :                         }
    3429             :                     }
    3430           0 :                     endCompression();
    3431           0 :                     disableStreamEncryption();
    3432             : 
    3433             : 
    3434           0 :                     sal_uInt64 nEndStreamPos = 0;
    3435           0 :                     osl_getFilePos( m_aFile, &nEndStreamPos );
    3436             : 
    3437             :                     // and finally close the stream
    3438           0 :                     aLine.setLength( 0 );
    3439           0 :                     aLine.append( "\nendstream\nendobj\n\n" );
    3440           0 :                     if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3441           0 :                         throw FontException();
    3442             : 
    3443             :                     // write stream length object
    3444           0 :                     aLine.setLength( 0 );
    3445           0 :                     if( ! updateObject( nStreamLengthObject ) )
    3446           0 :                         throw FontException();
    3447           0 :                     aLine.append( nStreamLengthObject );
    3448           0 :                     aLine.append( " 0 obj\n" );
    3449           0 :                     aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos ) );
    3450           0 :                     aLine.append( "\nendobj\n\n" );
    3451           0 :                     if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3452           0 :                         throw FontException();
    3453             :         }
    3454             :         else
    3455             :         {
    3456           0 :             OStringBuffer aErrorComment( 256 );
    3457           0 :             aErrorComment.append( "GetEmbedFontData failed for font \"" );
    3458           0 :             aErrorComment.append( OUStringToOString( pFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ) );
    3459           0 :             aErrorComment.append( '\"' );
    3460           0 :             if( pFont->GetSlant() == ITALIC_NORMAL )
    3461           0 :                 aErrorComment.append( " italic" );
    3462           0 :             else if( pFont->GetSlant() == ITALIC_OBLIQUE )
    3463           0 :                 aErrorComment.append( " oblique" );
    3464           0 :             aErrorComment.append( " weight=" );
    3465           0 :             aErrorComment.append( sal_Int32(pFont->GetWeight()) );
    3466           0 :             emitComment( aErrorComment.getStr() );
    3467             :         }
    3468             : 
    3469           0 :         if( nStreamObject )
    3470             :         {
    3471             :             // write font descriptor
    3472           0 :             nFontDescriptor = emitFontDescriptor( pFont, aInfo, 0, nStreamObject );
    3473             :         }
    3474             : 
    3475           0 :         if( nFontDescriptor )
    3476             :         {
    3477           0 :             if( pEncoding )
    3478           0 :                 nToUnicodeStream = createToUnicodeCMap( nEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, SAL_N_ELEMENTS(nEncoding) );
    3479             : 
    3480             :             // write font object
    3481           0 :             sal_Int32 nObject = createObject();
    3482           0 :             if( ! updateObject( nObject ) )
    3483           0 :                 throw FontException();
    3484             : 
    3485           0 :             OStringBuffer aLine( 1024 );
    3486           0 :             aLine.append( nObject );
    3487             :             aLine.append( " 0 obj\n"
    3488           0 :                 "<</Type/Font/Subtype/Type1/BaseFont/" );
    3489           0 :             appendName( aInfo.m_aPSName, aLine );
    3490           0 :             aLine.append( "\n" );
    3491           0 :             if( !pFont->IsSymbolFont() &&  pEncoding == 0 )
    3492           0 :                 aLine.append( "/Encoding/WinAnsiEncoding\n" );
    3493           0 :             if( nToUnicodeStream )
    3494             :             {
    3495           0 :                 aLine.append( "/ToUnicode " );
    3496           0 :                 aLine.append( nToUnicodeStream );
    3497           0 :                 aLine.append( " 0 R\n" );
    3498             :             }
    3499             :             aLine.append( "/FirstChar 0 /LastChar 255\n"
    3500           0 :                 "/Widths[" );
    3501           0 :             for( int i = 0; i < 256; i++ )
    3502             :             {
    3503           0 :                 aLine.append( pWidths[i] );
    3504           0 :                 aLine.append( ((i&15) == 15) ? "\n" : " " );
    3505             :             }
    3506             :             aLine.append( "]\n"
    3507           0 :                 "/FontDescriptor " );
    3508           0 :             aLine.append( nFontDescriptor );
    3509             :             aLine.append( " 0 R>>\n"
    3510           0 :                 "endobj\n\n" );
    3511           0 :             if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3512           0 :                 throw FontException();
    3513             : 
    3514           0 :             aRet[ rEmbed.m_nNormalFontID ] = nObject;
    3515             : 
    3516             :             // write additional encodings
    3517           0 :             for( std::list< EmbedEncoding >::iterator enc_it = rEmbed.m_aExtendedEncodings.begin(); enc_it != rEmbed.m_aExtendedEncodings.end(); ++enc_it )
    3518             :             {
    3519             :                 sal_Int32 aEncWidths[ 256 ];
    3520             :                 // emit encoding dict
    3521           0 :                 sal_Int32 nEncObject = createObject();
    3522           0 :                 if( ! updateObject( nEncObject ) )
    3523           0 :                     throw FontException();
    3524             : 
    3525           0 :                 OutputDevice* pRef = getReferenceDevice();
    3526           0 :                 pRef->Push( PUSH_FONT | PUSH_MAPMODE );
    3527           0 :                 pRef->SetMapMode( MapMode( MAP_PIXEL ) );
    3528           0 :                 Font aFont( pFont->GetFamilyName(), pFont->GetStyleName(), Size( 0, 1000 ) );
    3529           0 :                 aFont.SetWeight( pFont->GetWeight() );
    3530           0 :                 aFont.SetItalic( pFont->GetSlant() );
    3531           0 :                 aFont.SetPitch( pFont->GetPitch() );
    3532           0 :                 pRef->SetFont( aFont );
    3533           0 :                 pRef->ImplNewFont();
    3534             : 
    3535           0 :                 aLine.setLength( 0 );
    3536           0 :                 aLine.append( nEncObject );
    3537             :                 aLine.append( " 0 obj\n"
    3538           0 :                     "<</Type/Encoding/Differences[ 0\n" );
    3539           0 :                 int nEncoded = 0;
    3540           0 :                 aUnicodes.clear();
    3541           0 :                 for( std::vector< EmbedCode >::iterator str_it = enc_it->m_aEncVector.begin(); str_it != enc_it->m_aEncVector.end(); ++str_it )
    3542             :                 {
    3543           0 :                     OUString aStr( str_it->m_aUnicode );
    3544           0 :                     aEncWidths[nEncoded] = pRef->GetTextWidth( aStr );
    3545           0 :                     nEncodedCodes[nEncoded] = str_it->m_aUnicode;
    3546           0 :                     nEncoding[nEncoded] = sal::static_int_cast<sal_uInt8>(nEncoded);
    3547           0 :                     pEncToUnicodeIndex[nEncoded] = static_cast<sal_Int32>(aUnicodes.size());
    3548           0 :                     aUnicodes.push_back( nEncodedCodes[nEncoded] );
    3549           0 :                     pUnicodesPerGlyph[nEncoded] = 1;
    3550             : 
    3551           0 :                     aLine.append( " /" );
    3552           0 :                     aLine.append( str_it->m_aName );
    3553           0 :                     if( !((++nEncoded) & 15) )
    3554           0 :                         aLine.append( "\n" );
    3555           0 :                 }
    3556             :                 aLine.append( "]>>\n"
    3557           0 :                     "endobj\n\n" );
    3558             : 
    3559           0 :                 pRef->Pop();
    3560             : 
    3561           0 :                 if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3562           0 :                     throw FontException();
    3563             : 
    3564           0 :                 nToUnicodeStream = createToUnicodeCMap( nEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, nEncoded );
    3565             : 
    3566           0 :                 nObject = createObject();
    3567           0 :                 if( ! updateObject( nObject ) )
    3568           0 :                     throw FontException();
    3569             : 
    3570           0 :                 aLine.setLength( 0 );
    3571           0 :                 aLine.append( nObject );
    3572             :                 aLine.append( " 0 obj\n"
    3573           0 :                     "<</Type/Font/Subtype/Type1/BaseFont/" );
    3574           0 :                 appendName( aInfo.m_aPSName, aLine );
    3575           0 :                 aLine.append( "\n" );
    3576           0 :                 aLine.append( "/Encoding " );
    3577           0 :                 aLine.append( nEncObject );
    3578           0 :                 aLine.append( " 0 R\n" );
    3579           0 :                 if( nToUnicodeStream )
    3580             :                 {
    3581           0 :                     aLine.append( "/ToUnicode " );
    3582           0 :                     aLine.append( nToUnicodeStream );
    3583           0 :                     aLine.append( " 0 R\n" );
    3584             :                 }
    3585             :                 aLine.append( "/FirstChar 0\n"
    3586           0 :                     "/LastChar " );
    3587           0 :                 aLine.append( (sal_Int32)(nEncoded-1) );
    3588             :                 aLine.append( "\n"
    3589           0 :                     "/Widths[" );
    3590           0 :                 for( int i = 0; i < nEncoded; i++ )
    3591             :                 {
    3592           0 :                     aLine.append( aEncWidths[i] );
    3593           0 :                     aLine.append( ((i&15) == 15) ? "\n" : " " );
    3594             :                 }
    3595             :                 aLine.append( " ]\n"
    3596           0 :                     "/FontDescriptor " );
    3597           0 :                 aLine.append( nFontDescriptor );
    3598             :                 aLine.append( " 0 R>>\n"
    3599           0 :                     "endobj\n\n" );
    3600           0 :                 if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3601           0 :                     throw FontException();
    3602             : 
    3603           0 :                 aRet[ enc_it->m_nFontID ] = nObject;
    3604           0 :             }
    3605             :         }
    3606             :     }
    3607           0 :     catch( FontException& )
    3608             :     {
    3609             :         // these do nothing in case there was no compression or encryption ongoing
    3610           0 :         endCompression();
    3611           0 :         disableStreamEncryption();
    3612             :     }
    3613             : 
    3614           0 :     if( pFontData )
    3615           0 :         m_pReferenceDevice->mpGraphics->FreeEmbedFontData( pFontData, nFontLen );
    3616             : 
    3617           0 :     return aRet;
    3618             : }
    3619             : 
    3620           0 : static void appendSubsetName( int nSubsetID, const OUString& rPSName, OStringBuffer& rBuffer )
    3621             : {
    3622           0 :     if( nSubsetID )
    3623             :     {
    3624           0 :         for( int i = 0; i < 6; i++ )
    3625             :         {
    3626           0 :             int nOffset = (nSubsetID % 26);
    3627           0 :             nSubsetID /= 26;
    3628           0 :             rBuffer.append( (sal_Char)('A'+nOffset) );
    3629             :         }
    3630           0 :         rBuffer.append( '+' );
    3631             :     }
    3632           0 :     appendName( rPSName, rBuffer );
    3633           0 : }
    3634             : 
    3635           0 : sal_Int32 PDFWriterImpl::createToUnicodeCMap( sal_uInt8* pEncoding,
    3636             :                                               sal_Ucs* pUnicodes,
    3637             :                                               sal_Int32* pUnicodesPerGlyph,
    3638             :                                               sal_Int32* pEncToUnicodeIndex,
    3639             :                                               int nGlyphs )
    3640             : {
    3641           0 :     int nMapped = 0, n = 0;
    3642           0 :     for( n = 0; n < nGlyphs; n++ )
    3643           0 :         if( pUnicodes[pEncToUnicodeIndex[n]] && pUnicodesPerGlyph[n] )
    3644           0 :             nMapped++;
    3645             : 
    3646           0 :     if( nMapped == 0 )
    3647           0 :         return 0;
    3648             : 
    3649           0 :     sal_Int32 nStream = createObject();
    3650           0 :     CHECK_RETURN( updateObject( nStream ) );
    3651             : 
    3652           0 :     OStringBuffer aContents( 1024 );
    3653             :     aContents.append(
    3654             :                      "/CIDInit/ProcSet findresource begin\n"
    3655             :                      "12 dict begin\n"
    3656             :                      "begincmap\n"
    3657             :                      "/CIDSystemInfo<<\n"
    3658             :                      "/Registry (Adobe)\n"
    3659             :                      "/Ordering (UCS)\n"
    3660             :                      "/Supplement 0\n"
    3661             :                      ">> def\n"
    3662             :                      "/CMapName/Adobe-Identity-UCS def\n"
    3663             :                      "/CMapType 2 def\n"
    3664             :                      "1 begincodespacerange\n"
    3665             :                      "<00> <FF>\n"
    3666             :                      "endcodespacerange\n"
    3667           0 :                      );
    3668           0 :     int nCount = 0;
    3669           0 :     for( n = 0; n < nGlyphs; n++ )
    3670             :     {
    3671           0 :         if( pUnicodes[pEncToUnicodeIndex[n]] && pUnicodesPerGlyph[n] )
    3672             :         {
    3673           0 :             if( (nCount % 100) == 0 )
    3674             :             {
    3675           0 :                 if( nCount )
    3676           0 :                     aContents.append( "endbfchar\n" );
    3677           0 :                 aContents.append( (sal_Int32)((nMapped-nCount > 100) ? 100 : nMapped-nCount ) );
    3678           0 :                 aContents.append( " beginbfchar\n" );
    3679             :             }
    3680           0 :             aContents.append( '<' );
    3681           0 :             appendHex( (sal_Int8)pEncoding[n], aContents );
    3682           0 :             aContents.append( "> <" );
    3683             :             // TODO: handle unicodes>U+FFFF
    3684           0 :             sal_Int32 nIndex = pEncToUnicodeIndex[n];
    3685           0 :             for( sal_Int32 j = 0; j < pUnicodesPerGlyph[n]; j++ )
    3686             :             {
    3687           0 :                 appendHex( (sal_Int8)(pUnicodes[nIndex + j] / 256), aContents );
    3688           0 :                 appendHex( (sal_Int8)(pUnicodes[nIndex + j] & 255), aContents );
    3689             :             }
    3690           0 :             aContents.append( ">\n" );
    3691           0 :             nCount++;
    3692             :         }
    3693             :     }
    3694             :     aContents.append( "endbfchar\n"
    3695             :                       "endcmap\n"
    3696             :                       "CMapName currentdict /CMap defineresource pop\n"
    3697             :                       "end\n"
    3698           0 :                       "end\n" );
    3699             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3700           0 :     ZCodec* pCodec = new ZCodec( 0x4000, 0x4000 );
    3701           0 :     SvMemoryStream aStream;
    3702           0 :     pCodec->BeginCompression();
    3703           0 :     pCodec->Write( aStream, (const sal_uInt8*)aContents.getStr(), aContents.getLength() );
    3704           0 :     pCodec->EndCompression();
    3705           0 :     delete pCodec;
    3706             : #endif
    3707             : 
    3708             :     #if OSL_DEBUG_LEVEL > 1
    3709             :     emitComment( "PDFWriterImpl::createToUnicodeCMap" );
    3710             :     #endif
    3711           0 :     OStringBuffer aLine( 40 );
    3712             : 
    3713           0 :     aLine.append( nStream );
    3714           0 :     aLine.append( " 0 obj\n<</Length " );
    3715             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3716           0 :     sal_Int32 nLen = (sal_Int32)aStream.Tell();
    3717           0 :     aStream.Seek( 0 );
    3718           0 :     aLine.append( nLen );
    3719           0 :     aLine.append( "/Filter/FlateDecode" );
    3720             : #else
    3721             :     aLine.append( aContents.getLength() );
    3722             : #endif
    3723           0 :     aLine.append( ">>\nstream\n" );
    3724           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3725           0 :     checkAndEnableStreamEncryption( nStream );
    3726             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3727           0 :     CHECK_RETURN( writeBuffer( aStream.GetData(), nLen ) );
    3728             : #else
    3729             :     CHECK_RETURN( writeBuffer( aContents.getStr(), aContents.getLength() ) );
    3730             : #endif
    3731           0 :     disableStreamEncryption();
    3732           0 :     aLine.setLength( 0 );
    3733             :     aLine.append( "\nendstream\n"
    3734           0 :                   "endobj\n\n" );
    3735           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3736           0 :     return nStream;
    3737             : }
    3738             : 
    3739           0 : sal_Int32 PDFWriterImpl::emitFontDescriptor( const PhysicalFontFace* pFont, FontSubsetInfo& rInfo, sal_Int32 nSubsetID, sal_Int32 nFontStream )
    3740             : {
    3741           0 :     OStringBuffer aLine( 1024 );
    3742             :     // get font flags, see PDF reference 1.4 p. 358
    3743             :     // possibly characters outside Adobe standard encoding
    3744             :     // so set Symbolic flag
    3745           0 :     sal_Int32 nFontFlags = (1<<2);
    3746           0 :     if( pFont->GetSlant() == ITALIC_NORMAL || pFont->GetSlant() == ITALIC_OBLIQUE )
    3747           0 :         nFontFlags |= (1 << 6);
    3748           0 :     if( pFont->GetPitch() == PITCH_FIXED )
    3749           0 :         nFontFlags |= 1;
    3750           0 :     if( pFont->GetFamilyType() == FAMILY_SCRIPT )
    3751           0 :         nFontFlags |= (1 << 3);
    3752           0 :     else if( pFont->GetFamilyType() == FAMILY_ROMAN )
    3753           0 :         nFontFlags |= (1 << 1);
    3754             : 
    3755           0 :     sal_Int32 nFontDescriptor = createObject();
    3756           0 :     CHECK_RETURN( updateObject( nFontDescriptor ) );
    3757           0 :     aLine.setLength( 0 );
    3758           0 :     aLine.append( nFontDescriptor );
    3759             :     aLine.append( " 0 obj\n"
    3760           0 :                   "<</Type/FontDescriptor/FontName/" );
    3761           0 :     appendSubsetName( nSubsetID, rInfo.m_aPSName, aLine );
    3762             :     aLine.append( "\n"
    3763           0 :                   "/Flags " );
    3764           0 :     aLine.append( nFontFlags );
    3765             :     aLine.append( "\n"
    3766           0 :                   "/FontBBox[" );
    3767             :     // note: Top and Bottom are reversed in VCL and PDF rectangles
    3768           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.TopLeft().X() );
    3769           0 :     aLine.append( ' ' );
    3770           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.TopLeft().Y() );
    3771           0 :     aLine.append( ' ' );
    3772           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.BottomRight().X() );
    3773           0 :     aLine.append( ' ' );
    3774           0 :     aLine.append( (sal_Int32)(rInfo.m_aFontBBox.BottomRight().Y()+1) );
    3775           0 :     aLine.append( "]/ItalicAngle " );
    3776           0 :     if( pFont->GetSlant() == ITALIC_OBLIQUE || pFont->GetSlant() == ITALIC_NORMAL )
    3777           0 :         aLine.append( "-30" );
    3778             :     else
    3779           0 :         aLine.append( "0" );
    3780             :     aLine.append( "\n"
    3781           0 :                   "/Ascent " );
    3782           0 :     aLine.append( (sal_Int32)rInfo.m_nAscent );
    3783             :     aLine.append( "\n"
    3784           0 :                   "/Descent " );
    3785           0 :     aLine.append( (sal_Int32)-rInfo.m_nDescent );
    3786             :     aLine.append( "\n"
    3787           0 :                   "/CapHeight " );
    3788           0 :     aLine.append( (sal_Int32)rInfo.m_nCapHeight );
    3789             :     // According to PDF reference 1.4 StemV is required
    3790             :     // seems a tad strange to me, but well ...
    3791             :     aLine.append( "\n"
    3792           0 :                   "/StemV 80\n" );
    3793           0 :     if( nFontStream )
    3794             :     {
    3795           0 :         aLine.append( "/FontFile" );
    3796           0 :         switch( rInfo.m_nFontType )
    3797             :         {
    3798             :             case FontSubsetInfo::SFNT_TTF:
    3799           0 :                 aLine.append( '2' );
    3800           0 :                 break;
    3801             :             case FontSubsetInfo::TYPE1_PFA:
    3802             :             case FontSubsetInfo::TYPE1_PFB:
    3803             :             case FontSubsetInfo::ANY_TYPE1:
    3804           0 :                 break;
    3805             :             default:
    3806             :                 OSL_FAIL( "unknown fonttype in PDF font descriptor" );
    3807           0 :                 return 0;
    3808             :         }
    3809           0 :         aLine.append( ' ' );
    3810           0 :         aLine.append( nFontStream );
    3811           0 :         aLine.append( " 0 R\n" );
    3812             :     }
    3813             :     aLine.append( ">>\n"
    3814           0 :                   "endobj\n\n" );
    3815           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3816             : 
    3817           0 :     return nFontDescriptor;
    3818             : }
    3819             : 
    3820           0 : void PDFWriterImpl::appendBuiltinFontsToDict( OStringBuffer& rDict ) const
    3821             : {
    3822           0 :     for( std::map< sal_Int32, sal_Int32 >::const_iterator it =
    3823           0 :          m_aBuiltinFontToObjectMap.begin(); it != m_aBuiltinFontToObjectMap.end(); ++it )
    3824             :     {
    3825           0 :         rDict.append( m_aBuiltinFonts[it->first].getNameObject() );
    3826           0 :         rDict.append( ' ' );
    3827           0 :         rDict.append( it->second );
    3828           0 :         rDict.append( " 0 R" );
    3829             :     }
    3830           0 : }
    3831             : 
    3832           0 : bool PDFWriterImpl::emitFonts()
    3833             : {
    3834           0 :     if( ! m_pReferenceDevice->ImplGetGraphics() )
    3835           0 :         return false;
    3836             : 
    3837           0 :     OStringBuffer aLine( 1024 );
    3838             : 
    3839           0 :     std::map< sal_Int32, sal_Int32 > aFontIDToObject;
    3840             : 
    3841           0 :     OUString aTmpName;
    3842           0 :     osl_createTempFile( NULL, NULL, &aTmpName.pData );
    3843           0 :     for( FontSubsetData::iterator it = m_aSubsets.begin(); it != m_aSubsets.end(); ++it )
    3844             :     {
    3845           0 :         for( FontEmitList::iterator lit = it->second.m_aSubsets.begin(); lit != it->second.m_aSubsets.end(); ++lit )
    3846             :         {
    3847             :             sal_Int32 pGlyphIDs[ 256 ];
    3848             :             sal_Int32 pWidths[ 256 ];
    3849             :             sal_uInt8 pEncoding[ 256 ];
    3850             :             sal_Int32 pEncToUnicodeIndex[ 256 ];
    3851             :             sal_Int32 pUnicodesPerGlyph[ 256 ];
    3852           0 :             std::vector<sal_Ucs> aUnicodes;
    3853           0 :             aUnicodes.reserve( 256 );
    3854           0 :             int nGlyphs = 1;
    3855             :             // fill arrays and prepare encoding index map
    3856           0 :             sal_Int32 nToUnicodeStream = 0;
    3857             : 
    3858           0 :             memset( pGlyphIDs, 0, sizeof( pGlyphIDs ) );
    3859           0 :             memset( pEncoding, 0, sizeof( pEncoding ) );
    3860           0 :             memset( pUnicodesPerGlyph, 0, sizeof( pUnicodesPerGlyph ) );
    3861           0 :             memset( pEncToUnicodeIndex, 0, sizeof( pEncToUnicodeIndex ) );
    3862           0 :             for( FontEmitMapping::iterator fit = lit->m_aMapping.begin(); fit != lit->m_aMapping.end();++fit )
    3863             :             {
    3864           0 :                 sal_uInt8 nEnc = fit->second.getGlyphId();
    3865             : 
    3866             :                 DBG_ASSERT( pGlyphIDs[nEnc] == 0 && pEncoding[nEnc] == 0, "duplicate glyph" );
    3867             :                 DBG_ASSERT( nEnc <= lit->m_aMapping.size(), "invalid glyph encoding" );
    3868             : 
    3869           0 :                 pGlyphIDs[ nEnc ] = fit->first;
    3870           0 :                 pEncoding[ nEnc ] = nEnc;
    3871           0 :                 pEncToUnicodeIndex[ nEnc ] = static_cast<sal_Int32>(aUnicodes.size());
    3872           0 :                 pUnicodesPerGlyph[ nEnc ] = fit->second.countCodes();
    3873           0 :                 for( sal_Int32 n = 0; n < pUnicodesPerGlyph[ nEnc ]; n++ )
    3874           0 :                     aUnicodes.push_back( fit->second.getCode( n ) );
    3875           0 :                 if( fit->second.getCode(0) )
    3876           0 :                     nToUnicodeStream = 1;
    3877           0 :                 if( nGlyphs < 256 )
    3878           0 :                     nGlyphs++;
    3879             :                 else
    3880             :                 {
    3881             :                     OSL_FAIL( "too many glyphs for subset" );
    3882             :                 }
    3883             :             }
    3884           0 :             FontSubsetInfo aSubsetInfo;
    3885           0 :             if( m_pReferenceDevice->mpGraphics->CreateFontSubset( aTmpName, it->first, pGlyphIDs, pEncoding, pWidths, nGlyphs, aSubsetInfo ) )
    3886             :             {
    3887             :                 // create font stream
    3888             :                 oslFileHandle aFontFile;
    3889           0 :                 CHECK_RETURN( (osl_File_E_None == osl_openFile( aTmpName.pData, &aFontFile, osl_File_OpenFlag_Read ) ) );
    3890             :                 // get file size
    3891             :                 sal_uInt64 nLength1;
    3892           0 :                 CHECK_RETURN( (osl_File_E_None == osl_setFilePos( aFontFile, osl_Pos_End, 0 ) ) );
    3893           0 :                 CHECK_RETURN( (osl_File_E_None == osl_getFilePos( aFontFile, &nLength1 ) ) );
    3894           0 :                 CHECK_RETURN( (osl_File_E_None == osl_setFilePos( aFontFile, osl_Pos_Absolut, 0 ) ) );
    3895             : 
    3896             :                 #if OSL_DEBUG_LEVEL > 1
    3897             :                 emitComment( "PDFWriterImpl::emitFonts" );
    3898             :                 #endif
    3899           0 :                 sal_Int32 nFontStream = createObject();
    3900           0 :                 sal_Int32 nStreamLengthObject = createObject();
    3901           0 :                 CHECK_RETURN( updateObject( nFontStream ) );
    3902           0 :                 aLine.setLength( 0 );
    3903           0 :                 aLine.append( nFontStream );
    3904             :                 aLine.append( " 0 obj\n"
    3905           0 :                              "<</Length " );
    3906           0 :                 aLine.append( (sal_Int32)nStreamLengthObject );
    3907             :                 aLine.append( " 0 R"
    3908             :                              #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3909             :                              "/Filter/FlateDecode"
    3910             :                              #endif
    3911           0 :                              "/Length1 " );
    3912             : 
    3913           0 :                 sal_uInt64 nStartPos = 0;
    3914           0 :                 if( aSubsetInfo.m_nFontType == FontSubsetInfo::SFNT_TTF )
    3915             :                 {
    3916           0 :                     aLine.append( (sal_Int32)nLength1 );
    3917             : 
    3918             :                     aLine.append( ">>\n"
    3919           0 :                                  "stream\n" );
    3920           0 :                     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3921           0 :                     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nStartPos ) ) );
    3922             : 
    3923             :                     // copy font file
    3924           0 :                     beginCompression();
    3925           0 :                     checkAndEnableStreamEncryption( nFontStream );
    3926           0 :                     sal_Bool bEOF = sal_False;
    3927           0 :                     do
    3928             :                     {
    3929             :                         char buf[8192];
    3930             :                         sal_uInt64 nRead;
    3931           0 :                         CHECK_RETURN( (osl_File_E_None == osl_readFile( aFontFile, buf, sizeof( buf ), &nRead ) ) );
    3932           0 :                         CHECK_RETURN( writeBuffer( buf, nRead ) );
    3933           0 :                         CHECK_RETURN( (osl_File_E_None == osl_isEndOfFile( aFontFile, &bEOF ) ) );
    3934           0 :                     } while( ! bEOF );
    3935             :                 }
    3936           0 :                 else if( (aSubsetInfo.m_nFontType & FontSubsetInfo::CFF_FONT) != 0 )
    3937             :                 {
    3938             :                     // TODO: implement
    3939             :                     OSL_FAIL( "PDFWriterImpl does not support CFF-font subsets yet!" );
    3940             :                 }
    3941           0 :                 else if( (aSubsetInfo.m_nFontType & FontSubsetInfo::TYPE1_PFB) != 0 ) // TODO: also support PFA?
    3942             :                 {
    3943           0 :                     boost::shared_array<unsigned char> pBuffer( new unsigned char[ nLength1 ] );
    3944             : 
    3945           0 :                     sal_uInt64 nBytesRead = 0;
    3946           0 :                     CHECK_RETURN( (osl_File_E_None == osl_readFile( aFontFile, pBuffer.get(), nLength1, &nBytesRead ) ) );
    3947             :                     DBG_ASSERT( nBytesRead==nLength1, "PDF-FontSubset read incomplete!" );
    3948           0 :                     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( aFontFile, osl_Pos_Absolut, 0 ) ) );
    3949             :                     // get the PFB-segment lengths
    3950           0 :                     ThreeInts aSegmentLengths = {0,0,0};
    3951           0 :                     getPfbSegmentLengths( pBuffer.get(), (int)nBytesRead, aSegmentLengths );
    3952             :                     // the lengths below are mandatory for PDF-exported Type1 fonts
    3953             :                     // because the PFB segment headers get stripped! WhyOhWhy.
    3954           0 :                     aLine.append( (sal_Int32)aSegmentLengths[0] );
    3955           0 :                     aLine.append( "/Length2 " );
    3956           0 :                     aLine.append( (sal_Int32)aSegmentLengths[1] );
    3957           0 :                     aLine.append( "/Length3 " );
    3958           0 :                     aLine.append( (sal_Int32)aSegmentLengths[2] );
    3959             : 
    3960             :                     aLine.append( ">>\n"
    3961           0 :                                  "stream\n" );
    3962           0 :                     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3963           0 :                     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nStartPos ) ) );
    3964             : 
    3965             :                     // emit PFB-sections without section headers
    3966           0 :                     beginCompression();
    3967           0 :                     checkAndEnableStreamEncryption( nFontStream );
    3968           0 :                     CHECK_RETURN( writeBuffer( &pBuffer[6], aSegmentLengths[0] ) );
    3969           0 :                     CHECK_RETURN( writeBuffer( &pBuffer[12] + aSegmentLengths[0], aSegmentLengths[1] ) );
    3970           0 :                     CHECK_RETURN( writeBuffer( &pBuffer[18] + aSegmentLengths[0] + aSegmentLengths[1], aSegmentLengths[2] ) );
    3971             :                 }
    3972             :                 else
    3973             :                 {
    3974           0 :                     fprintf( stderr, "PDF: CreateFontSubset result in not yet supported format=%d\n",aSubsetInfo.m_nFontType);
    3975           0 :                     aLine.append( "0 >>\nstream\n" );
    3976             :                 }
    3977             : 
    3978           0 :                 endCompression();
    3979           0 :                 disableStreamEncryption();
    3980             :                 // close the file
    3981           0 :                 osl_closeFile( aFontFile );
    3982             : 
    3983           0 :                 sal_uInt64 nEndPos = 0;
    3984           0 :                 CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nEndPos ) ) );
    3985             :                 // end the stream
    3986           0 :                 aLine.setLength( 0 );
    3987           0 :                 aLine.append( "\nendstream\nendobj\n\n" );
    3988           0 :                 CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3989             : 
    3990             :                 // emit stream length object
    3991           0 :                 CHECK_RETURN( updateObject( nStreamLengthObject ) );
    3992           0 :                 aLine.setLength( 0 );
    3993           0 :                 aLine.append( nStreamLengthObject );
    3994           0 :                 aLine.append( " 0 obj\n" );
    3995           0 :                 aLine.append( (sal_Int64)(nEndPos-nStartPos) );
    3996           0 :                 aLine.append( "\nendobj\n\n" );
    3997           0 :                 CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3998             : 
    3999             :                 // write font descriptor
    4000           0 :                 sal_Int32 nFontDescriptor = emitFontDescriptor( it->first, aSubsetInfo, lit->m_nFontID, nFontStream );
    4001             : 
    4002           0 :                 if( nToUnicodeStream )
    4003           0 :                     nToUnicodeStream = createToUnicodeCMap( pEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, nGlyphs );
    4004             : 
    4005           0 :                 sal_Int32 nFontObject = createObject();
    4006           0 :                 CHECK_RETURN( updateObject( nFontObject ) );
    4007           0 :                 aLine.setLength( 0 );
    4008           0 :                 aLine.append( nFontObject );
    4009             : 
    4010           0 :                 aLine.append( " 0 obj\n" );
    4011           0 :                 aLine.append( ((aSubsetInfo.m_nFontType & FontSubsetInfo::ANY_TYPE1) != 0) ?
    4012             :                              "<</Type/Font/Subtype/Type1/BaseFont/" :
    4013           0 :                              "<</Type/Font/Subtype/TrueType/BaseFont/" );
    4014           0 :                 appendSubsetName( lit->m_nFontID, aSubsetInfo.m_aPSName, aLine );
    4015             :                 aLine.append( "\n"
    4016             :                              "/FirstChar 0\n"
    4017           0 :                              "/LastChar " );
    4018           0 :                 aLine.append( (sal_Int32)(nGlyphs-1) );
    4019             :                 aLine.append( "\n"
    4020           0 :                              "/Widths[" );
    4021           0 :                 for( int i = 0; i < nGlyphs; i++ )
    4022             :                 {
    4023           0 :                     aLine.append( pWidths[ i ] );
    4024           0 :                     aLine.append( ((i & 15) == 15) ? "\n" : " " );
    4025             :                 }
    4026             :                 aLine.append( "]\n"
    4027           0 :                              "/FontDescriptor " );
    4028           0 :                 aLine.append( nFontDescriptor );
    4029           0 :                 aLine.append( " 0 R\n" );
    4030           0 :                 if( nToUnicodeStream )
    4031             :                 {
    4032           0 :                     aLine.append( "/ToUnicode " );
    4033           0 :                     aLine.append( nToUnicodeStream );
    4034           0 :                     aLine.append( " 0 R\n" );
    4035             :                 }
    4036             :                 aLine.append( ">>\n"
    4037           0 :                              "endobj\n\n" );
    4038           0 :                 CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4039             : 
    4040           0 :                 aFontIDToObject[ lit->m_nFontID ] = nFontObject;
    4041             :             }
    4042             :             else
    4043             :             {
    4044           0 :                 const PhysicalFontFace* pFont = it->first;
    4045           0 :                 OStringBuffer aErrorComment( 256 );
    4046           0 :                 aErrorComment.append( "CreateFontSubset failed for font \"" );
    4047           0 :                 aErrorComment.append( OUStringToOString( pFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ) );
    4048           0 :                 aErrorComment.append( '\"' );
    4049           0 :                 if( pFont->GetSlant() == ITALIC_NORMAL )
    4050           0 :                     aErrorComment.append( " italic" );
    4051           0 :                 else if( pFont->GetSlant() == ITALIC_OBLIQUE )
    4052           0 :                     aErrorComment.append( " oblique" );
    4053           0 :                 aErrorComment.append( " weight=" );
    4054           0 :                 aErrorComment.append( sal_Int32(pFont->GetWeight()) );
    4055           0 :                 emitComment( aErrorComment.getStr() );
    4056             :             }
    4057           0 :         }
    4058             :     }
    4059           0 :     osl_removeFile( aTmpName.pData );
    4060             : 
    4061             :     // emit embedded fonts
    4062           0 :     for( FontEmbedData::iterator eit = m_aEmbeddedFonts.begin(); eit != m_aEmbeddedFonts.end(); ++eit )
    4063             :     {
    4064           0 :         std::map< sal_Int32, sal_Int32 > aObjects = emitEmbeddedFont( eit->first, eit->second );
    4065           0 :         for( std::map< sal_Int32, sal_Int32 >::iterator fit = aObjects.begin(); fit != aObjects.end(); ++fit )
    4066             :         {
    4067           0 :             CHECK_RETURN( fit->second );
    4068           0 :             aFontIDToObject[ fit->first ] = fit->second;
    4069             :         }
    4070           0 :     }
    4071             : 
    4072             :     // emit system fonts
    4073           0 :     for( FontEmbedData::iterator sit = m_aSystemFonts.begin(); sit != m_aSystemFonts.end(); ++sit )
    4074             :     {
    4075           0 :         std::map< sal_Int32, sal_Int32 > aObjects = emitSystemFont( sit->first, sit->second );
    4076           0 :         for( std::map< sal_Int32, sal_Int32 >::iterator fit = aObjects.begin(); fit != aObjects.end(); ++fit )
    4077             :         {
    4078           0 :             CHECK_RETURN( fit->second );
    4079           0 :             aFontIDToObject[ fit->first ] = fit->second;
    4080             :         }
    4081           0 :     }
    4082             : 
    4083           0 :     OStringBuffer aFontDict( 1024 );
    4084           0 :     aFontDict.append( getFontDictObject() );
    4085             :     aFontDict.append( " 0 obj\n"
    4086           0 :                      "<<" );
    4087           0 :     int ni = 0;
    4088           0 :     for( std::map< sal_Int32, sal_Int32 >::iterator mit = aFontIDToObject.begin(); mit != aFontIDToObject.end(); ++mit )
    4089             :     {
    4090           0 :         aFontDict.append( "/F" );
    4091           0 :         aFontDict.append( mit->first );
    4092           0 :         aFontDict.append( ' ' );
    4093           0 :         aFontDict.append( mit->second );
    4094           0 :         aFontDict.append( " 0 R" );
    4095           0 :         if( ((++ni) & 7) == 0 )
    4096           0 :             aFontDict.append( '\n' );
    4097             :     }
    4098             :     // emit builtin font for widget apperances / variable text
    4099           0 :     for( std::map< sal_Int32, sal_Int32 >::iterator it = m_aBuiltinFontToObjectMap.begin();
    4100           0 :         it != m_aBuiltinFontToObjectMap.end(); ++it )
    4101             :     {
    4102           0 :         ImplPdfBuiltinFontData aData(m_aBuiltinFonts[it->first]);
    4103           0 :         it->second = emitBuiltinFont( &aData, it->second );
    4104           0 :     }
    4105           0 :     appendBuiltinFontsToDict( aFontDict );
    4106           0 :     aFontDict.append( "\n>>\nendobj\n\n" );
    4107             : 
    4108           0 :     CHECK_RETURN( updateObject( getFontDictObject() ) );
    4109           0 :     CHECK_RETURN( writeBuffer( aFontDict.getStr(), aFontDict.getLength() ) );
    4110           0 :     return true;
    4111             : }
    4112             : 
    4113           0 : sal_Int32 PDFWriterImpl::emitResources()
    4114             : {
    4115             :     // emit shadings
    4116           0 :     if( ! m_aGradients.empty() )
    4117           0 :         CHECK_RETURN( emitGradients() );
    4118             :     // emit tilings
    4119           0 :     if( ! m_aTilings.empty() )
    4120           0 :         CHECK_RETURN( emitTilings() );
    4121             : 
    4122             :     // emit font dict
    4123           0 :     CHECK_RETURN( emitFonts() );
    4124             : 
    4125             :     // emit Resource dict
    4126           0 :     OStringBuffer aLine( 512 );
    4127           0 :     sal_Int32 nResourceDict = getResourceDictObj();
    4128           0 :     CHECK_RETURN( updateObject( nResourceDict ) );
    4129           0 :     aLine.setLength( 0 );
    4130           0 :     aLine.append( nResourceDict );
    4131           0 :     aLine.append( " 0 obj\n" );
    4132           0 :     m_aGlobalResourceDict.append( aLine, getFontDictObject() );
    4133           0 :     aLine.append( "endobj\n\n" );
    4134           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4135           0 :     return nResourceDict;
    4136             : }
    4137             : 
    4138           0 : sal_Int32 PDFWriterImpl::updateOutlineItemCount( std::vector< sal_Int32 >& rCounts,
    4139             :                                                  sal_Int32 nItemLevel,
    4140             :                                                  sal_Int32 nCurrentItemId )
    4141             : {
    4142             :     /* The /Count number of an item is
    4143             :        positive: the number of visible subitems
    4144             :        negative: the negative number of subitems that will become visible if
    4145             :                  the item gets opened
    4146             :        see PDF ref 1.4 p 478
    4147             :     */
    4148             : 
    4149           0 :     sal_Int32 nCount = 0;
    4150             : 
    4151           0 :     if( m_aContext.OpenBookmarkLevels < 0           || // all levels arevisible
    4152           0 :         m_aContext.OpenBookmarkLevels >= nItemLevel    // this level is visible
    4153             :       )
    4154             :     {
    4155           0 :         PDFOutlineEntry& rItem = m_aOutline[ nCurrentItemId ];
    4156           0 :         sal_Int32 nChildren = rItem.m_aChildren.size();
    4157           0 :         for( sal_Int32 i = 0; i < nChildren; i++ )
    4158           0 :             nCount += updateOutlineItemCount( rCounts, nItemLevel+1, rItem.m_aChildren[i] );
    4159           0 :         rCounts[nCurrentItemId] = nCount;
    4160             :         // return 1 (this item) + visible sub items
    4161           0 :         if( nCount < 0 )
    4162           0 :             nCount = 0;
    4163           0 :         nCount++;
    4164             :     }
    4165             :     else
    4166             :     {
    4167             :         // this bookmark level is invisible
    4168           0 :         PDFOutlineEntry& rItem = m_aOutline[ nCurrentItemId ];
    4169           0 :         sal_Int32 nChildren = rItem.m_aChildren.size();
    4170           0 :         rCounts[ nCurrentItemId ] = -sal_Int32(rItem.m_aChildren.size());
    4171           0 :         for( sal_Int32 i = 0; i < nChildren; i++ )
    4172           0 :             updateOutlineItemCount( rCounts, nItemLevel+1, rItem.m_aChildren[i] );
    4173           0 :         nCount = -1;
    4174             :     }
    4175             : 
    4176           0 :     return nCount;
    4177             : }
    4178             : 
    4179           0 : sal_Int32 PDFWriterImpl::emitOutline()
    4180             : {
    4181           0 :     int i, nItems = m_aOutline.size();
    4182             : 
    4183             :     // do we have an outline at all ?
    4184           0 :     if( nItems < 2 )
    4185           0 :         return 0;
    4186             : 
    4187             :     // reserve object numbers for all outline items
    4188           0 :     for( i = 0; i < nItems; ++i )
    4189           0 :         m_aOutline[i].m_nObject = createObject();
    4190             : 
    4191             :     // update all parent, next and prev object ids
    4192           0 :     for( i = 0; i < nItems; ++i )
    4193             :     {
    4194           0 :         PDFOutlineEntry& rItem = m_aOutline[i];
    4195           0 :         int nChildren = rItem.m_aChildren.size();
    4196             : 
    4197           0 :         if( nChildren )
    4198             :         {
    4199           0 :             for( int n = 0; n < nChildren; ++n )
    4200             :             {
    4201           0 :                 PDFOutlineEntry& rChild = m_aOutline[ rItem.m_aChildren[n] ];
    4202             : 
    4203           0 :                 rChild.m_nParentObject = rItem.m_nObject;
    4204           0 :                 rChild.m_nPrevObject = (n > 0) ? m_aOutline[ rItem.m_aChildren[n-1] ].m_nObject : 0;
    4205           0 :                 rChild.m_nNextObject = (n < nChildren-1) ? m_aOutline[ rItem.m_aChildren[n+1] ].m_nObject : 0;
    4206             :             }
    4207             : 
    4208             :         }
    4209             :     }
    4210             : 
    4211             :     // calculate Count entries for all items
    4212           0 :     std::vector< sal_Int32 > aCounts( nItems );
    4213           0 :     updateOutlineItemCount( aCounts, 0, 0 );
    4214             : 
    4215             :     // emit hierarchy
    4216           0 :     for( i = 0; i < nItems; ++i )
    4217             :     {
    4218           0 :         PDFOutlineEntry& rItem = m_aOutline[i];
    4219           0 :         OStringBuffer aLine( 1024 );
    4220             : 
    4221           0 :         CHECK_RETURN( updateObject( rItem.m_nObject ) );
    4222           0 :         aLine.append( rItem.m_nObject );
    4223           0 :         aLine.append( " 0 obj\n" );
    4224           0 :         aLine.append( "<<" );
    4225             :         // number of visible children (all levels)
    4226           0 :         if( i > 0 || aCounts[0] > 0 )
    4227             :         {
    4228           0 :             aLine.append( "/Count " );
    4229           0 :             aLine.append( aCounts[i] );
    4230             :         }
    4231           0 :         if( ! rItem.m_aChildren.empty() )
    4232             :         {
    4233             :             // children list: First, Last
    4234           0 :             aLine.append( "/First " );
    4235           0 :             aLine.append( m_aOutline[rItem.m_aChildren.front()].m_nObject );
    4236           0 :             aLine.append( " 0 R/Last " );
    4237           0 :             aLine.append( m_aOutline[rItem.m_aChildren.back()].m_nObject );
    4238           0 :             aLine.append( " 0 R\n" );
    4239             :         }
    4240           0 :         if( i > 0 )
    4241             :         {
    4242             :             // Title, Dest, Parent, Prev, Next
    4243           0 :             aLine.append( "/Title" );
    4244           0 :             appendUnicodeTextStringEncrypt( rItem.m_aTitle, rItem.m_nObject, aLine );
    4245           0 :             aLine.append( "\n" );
    4246             :             // Dest is not required
    4247           0 :             if( rItem.m_nDestID >= 0 && rItem.m_nDestID < (sal_Int32)m_aDests.size() )
    4248             :             {
    4249           0 :                 aLine.append( "/Dest" );
    4250           0 :                 appendDest( rItem.m_nDestID, aLine );
    4251             :             }
    4252           0 :             aLine.append( "/Parent " );
    4253           0 :             aLine.append( rItem.m_nParentObject );
    4254           0 :             aLine.append( " 0 R" );
    4255           0 :             if( rItem.m_nPrevObject )
    4256             :             {
    4257           0 :                 aLine.append( "/Prev " );
    4258           0 :                 aLine.append( rItem.m_nPrevObject );
    4259           0 :                 aLine.append( " 0 R" );
    4260             :             }
    4261           0 :             if( rItem.m_nNextObject )
    4262             :             {
    4263           0 :                 aLine.append( "/Next " );
    4264           0 :                 aLine.append( rItem.m_nNextObject );
    4265           0 :                 aLine.append( " 0 R" );
    4266             :             }
    4267             :         }
    4268           0 :         aLine.append( ">>\nendobj\n\n" );
    4269           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4270           0 :     }
    4271             : 
    4272           0 :     return m_aOutline[0].m_nObject;
    4273             : }
    4274             : 
    4275             : #undef CHECK_RETURN
    4276             : #define CHECK_RETURN( x ) if( !x ) return false
    4277             : 
    4278           0 : bool PDFWriterImpl::appendDest( sal_Int32 nDestID, OStringBuffer& rBuffer )
    4279             : {
    4280           0 :     if( nDestID < 0 || nDestID >= (sal_Int32)m_aDests.size() )
    4281             :     {
    4282             : #if OSL_DEBUG_LEVEL > 1
    4283             :         fprintf( stderr, "ERROR: invalid dest %d requested\n", (int)nDestID );
    4284             : #endif
    4285           0 :         return false;
    4286             :     }
    4287             : 
    4288             : 
    4289           0 :     const PDFDest& rDest        = m_aDests[ nDestID ];
    4290           0 :     const PDFPage& rDestPage    = m_aPages[ rDest.m_nPage ];
    4291             : 
    4292           0 :     rBuffer.append( '[' );
    4293           0 :     rBuffer.append( rDestPage.m_nPageObject );
    4294           0 :     rBuffer.append( " 0 R" );
    4295             : 
    4296           0 :     switch( rDest.m_eType )
    4297             :     {
    4298             :         case PDFWriter::XYZ:
    4299             :         default:
    4300           0 :             rBuffer.append( "/XYZ " );
    4301           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4302           0 :             rBuffer.append( ' ' );
    4303           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4304           0 :             rBuffer.append( " 0" );
    4305           0 :             break;
    4306             :         case PDFWriter::Fit:
    4307           0 :             rBuffer.append( "/Fit" );
    4308           0 :             break;
    4309             :         case PDFWriter::FitRectangle:
    4310           0 :             rBuffer.append( "/FitR " );
    4311           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4312           0 :             rBuffer.append( ' ' );
    4313           0 :             appendFixedInt( rDest.m_aRect.Top(), rBuffer );
    4314           0 :             rBuffer.append( ' ' );
    4315           0 :             appendFixedInt( rDest.m_aRect.Right(), rBuffer );
    4316           0 :             rBuffer.append( ' ' );
    4317           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4318           0 :             break;
    4319             :         case PDFWriter::FitHorizontal:
    4320           0 :             rBuffer.append( "/FitH " );
    4321           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4322           0 :             break;
    4323             :         case PDFWriter::FitVertical:
    4324           0 :             rBuffer.append( "/FitV " );
    4325           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4326           0 :             break;
    4327             :         case PDFWriter::FitPageBoundingBox:
    4328           0 :             rBuffer.append( "/FitB" );
    4329           0 :             break;
    4330             :         case PDFWriter::FitPageBoundingBoxHorizontal:
    4331           0 :             rBuffer.append( "/FitBH " );
    4332           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4333           0 :             break;
    4334             :         case PDFWriter::FitPageBoundingBoxVertical:
    4335           0 :             rBuffer.append( "/FitBV " );
    4336           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4337           0 :             break;
    4338             :     }
    4339           0 :     rBuffer.append( ']' );
    4340             : 
    4341           0 :     return true;
    4342             : }
    4343             : 
    4344           0 : bool PDFWriterImpl::emitLinkAnnotations()
    4345             : {
    4346           0 :     int nAnnots = m_aLinks.size();
    4347           0 :     for( int i = 0; i < nAnnots; i++ )
    4348             :     {
    4349           0 :         const PDFLink& rLink            = m_aLinks[i];
    4350           0 :         if( ! updateObject( rLink.m_nObject ) )
    4351           0 :             continue;
    4352             : 
    4353           0 :         OStringBuffer aLine( 1024 );
    4354           0 :         aLine.append( rLink.m_nObject );
    4355           0 :         aLine.append( " 0 obj\n" );
    4356             : //i59651  key /F set bits Print to 1 rest to 0. We don't set NoZoom NoRotate to 1, since it's a 'should'
    4357             : // see PDF 8.4.2 and ISO 19005-1:2005 6.5.3
    4358           0 :         aLine.append( "<</Type/Annot" );
    4359           0 :         if( m_bIsPDF_A1 )
    4360           0 :             aLine.append( "/F 4" );
    4361           0 :         aLine.append( "/Subtype/Link/Border[0 0 0]/Rect[" );
    4362             : 
    4363           0 :         appendFixedInt( rLink.m_aRect.Left()-7, aLine );//the +7 to have a better shape of the border rectangle
    4364           0 :         aLine.append( ' ' );
    4365           0 :         appendFixedInt( rLink.m_aRect.Top(), aLine );
    4366           0 :         aLine.append( ' ' );
    4367           0 :         appendFixedInt( rLink.m_aRect.Right()+7, aLine );//the +7 to have a better shape of the border rectangle
    4368           0 :         aLine.append( ' ' );
    4369           0 :         appendFixedInt( rLink.m_aRect.Bottom(), aLine );
    4370           0 :         aLine.append( "]" );
    4371           0 :         if( rLink.m_nDest >= 0 )
    4372             :         {
    4373           0 :             aLine.append( "/Dest" );
    4374           0 :             appendDest( rLink.m_nDest, aLine );
    4375             :         }
    4376             :         else
    4377             :         {
    4378             : /*--->i56629
    4379             : destination is external to the document, so
    4380             : we check in the following sequence:
    4381             : 
    4382             :  if target type is neither .pdf, nor .od[tpgs], then
    4383             :           check if relative or absolute and act accordingly (use URI or 'launch application' as requested)
    4384             :                              end processing
    4385             :  else if target is .od[tpgs]: then
    4386             :       if conversion of type from od[tpgs]  to pdf is requested, convert it and this becomes the new target file
    4387             :       processing continue
    4388             : 
    4389             :  if (new)target is .pdf : then
    4390             :      if GotToR is requested, then
    4391             :            convert the target in GoToR where the fragment of the URI is
    4392             :            considered the named destination in the target file, set relative or absolute as requested
    4393             :      else strip the fragment from URL and then set URI or 'launch application' as requested
    4394             : */
    4395             : //
    4396             : // FIXME: check if the decode mechanisms for URL processing throughout this implementation
    4397             : // are the correct one!!
    4398             : //
    4399             : // extract target file type
    4400           0 :             INetURLObject aDocumentURL( m_aContext.BaseURL );
    4401           0 :             INetURLObject aTargetURL( rLink.m_aURL );
    4402           0 :             sal_Int32   nSetGoToRMode = 0;
    4403           0 :             sal_Bool    bTargetHasPDFExtension = sal_False;
    4404           0 :             INetProtocol eTargetProtocol = aTargetURL.GetProtocol();
    4405           0 :             bool    bIsUNCPath = false;
    4406             : // check if the protocol is a known one, or if there is no protocol at all (on target only)
    4407             : // if there is no protocol, make the target relative to the current document directory
    4408             : // getting the needed URL information from the current document path
    4409           0 :             if( eTargetProtocol == INET_PROT_NOT_VALID )
    4410             :             {
    4411           0 :                 if( rLink.m_aURL.getLength() > 4 && rLink.m_aURL.startsWith("\\\\\\\\"))
    4412             :                 {
    4413           0 :                     bIsUNCPath = true;
    4414             :                 }
    4415             :                 else
    4416             :                 {
    4417           0 :                     INetURLObject aNewBase( aDocumentURL );//duplicate document URL
    4418           0 :                     aNewBase.removeSegment(); //remove last segment from it, obtaining the base URL of the
    4419             :                                               //target document
    4420           0 :                     aNewBase.insertName( rLink.m_aURL );
    4421           0 :                     aTargetURL = aNewBase;//reassign the new target URL
    4422             : //recompute the target protocol, with the new URL
    4423             : //normal URL processing resumes
    4424           0 :                     eTargetProtocol = aTargetURL.GetProtocol();
    4425             :                 }
    4426             :             }
    4427             : 
    4428           0 :             OUString aFileExtension = aTargetURL.GetFileExtension();
    4429             : 
    4430             : // Check if the URL ends in '/': if yes it's a directory,
    4431             : // it will be forced to a URI link.
    4432             : // possibly a malformed URI, leave it as it is, force as URI
    4433           0 :             if( aTargetURL.hasFinalSlash() )
    4434           0 :                 m_aContext.DefaultLinkAction = PDFWriter::URIAction;
    4435             : 
    4436           0 :             if( !aFileExtension.isEmpty() )
    4437             :             {
    4438           0 :                 if( m_aContext.ConvertOOoTargetToPDFTarget )
    4439             :                 {
    4440           0 :                     bool bChangeFileExtensionToPDF = false;
    4441             :                     //examine the file type (.odm .odt. .odp, odg, ods)
    4442           0 :                     if( aFileExtension.equalsIgnoreAsciiCase(OUString( "odm"  ) ) )
    4443           0 :                         bChangeFileExtensionToPDF = true;
    4444           0 :                     if( aFileExtension.equalsIgnoreAsciiCase(OUString( "odt"  ) ) )
    4445           0 :                         bChangeFileExtensionToPDF = true;
    4446           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase(OUString( "odp"  ) ) )
    4447           0 :                         bChangeFileExtensionToPDF = true;
    4448           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase(OUString( "odg"  ) ) )
    4449           0 :                         bChangeFileExtensionToPDF = true;
    4450           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase(OUString( "ods"  ) ) )
    4451           0 :                         bChangeFileExtensionToPDF = true;
    4452           0 :                     if( bChangeFileExtensionToPDF )
    4453           0 :                         aTargetURL.setExtension(OUString( "pdf"  ) );
    4454             :                 }
    4455             : //check if extension is pdf, see if GoToR should be forced
    4456           0 :                 bTargetHasPDFExtension = aTargetURL.GetFileExtension().equalsIgnoreAsciiCase(OUString( "pdf"  ) );
    4457           0 :                 if( m_aContext.ForcePDFAction && bTargetHasPDFExtension )
    4458           0 :                     nSetGoToRMode++;
    4459             :             }
    4460             : //prepare the URL, if relative or not
    4461           0 :             INetProtocol eBaseProtocol = aDocumentURL.GetProtocol();
    4462             : //queue the string common to all types of actions
    4463           0 :             aLine.append( "/A<</Type/Action/S");
    4464           0 :             if( bIsUNCPath ) // handle Win UNC paths
    4465             :             {
    4466           0 :                 aLine.append( "/Launch/Win<</F" );
    4467             :                 // INetURLObject is not good with UNC paths, use original path
    4468           0 :                 appendLiteralStringEncrypt(  rLink.m_aURL, rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4469           0 :                 aLine.append( ">>" );
    4470             :             }
    4471             :             else
    4472             :             {
    4473           0 :                 bool bSetRelative = false;
    4474           0 :                 bool bFileSpec = false;
    4475             : //check if relative file link is requested and if the protocol is 'file://'
    4476           0 :                 if( m_aContext.RelFsys && eBaseProtocol == eTargetProtocol && eTargetProtocol == INET_PROT_FILE )
    4477           0 :                     bSetRelative = true;
    4478             : 
    4479           0 :                 OUString aFragment = aTargetURL.GetMark( INetURLObject::NO_DECODE /*DECODE_WITH_CHARSET*/ ); //fragment as is,
    4480           0 :                 if( nSetGoToRMode == 0 )
    4481             :                 {
    4482           0 :                     switch( m_aContext.DefaultLinkAction )
    4483             :                     {
    4484             :                     default:
    4485             :                     case PDFWriter::URIAction :
    4486             :                     case PDFWriter::URIActionDestination :
    4487           0 :                         aLine.append( "/URI/URI" );
    4488           0 :                         break;
    4489             :                     case PDFWriter::LaunchAction:
    4490             : // now:
    4491             : // if a launch action is requested and the hyperlink target has a fragment
    4492             : // and the target file does not have a pdf extension, or it's not a 'file:://' protocol
    4493             : // then force the uri action on it
    4494             : // This code will permit the correct opening of application on web pages, the one that
    4495             : // normally have fragments (but I may be wrong...)
    4496             : // and will force the use of URI when the protocol is not file://
    4497           0 :                         if( (!aFragment.isEmpty() && !bTargetHasPDFExtension) ||
    4498             :                                         eTargetProtocol != INET_PROT_FILE )
    4499             :                         {
    4500           0 :                             aLine.append( "/URI/URI" );
    4501             :                         }
    4502             :                         else
    4503             :                         {
    4504           0 :                             aLine.append( "/Launch/F" );
    4505           0 :                             bFileSpec = true;
    4506             :                         }
    4507           0 :                         break;
    4508             :                     }
    4509             :                 }
    4510             : //fragment are encoded in the same way as in the named destination processing
    4511           0 :                 if( nSetGoToRMode )
    4512             :                 {
    4513             :                     //add the fragment
    4514           0 :                     OUString aURLNoMark = aTargetURL.GetURLNoMark( INetURLObject::DECODE_WITH_CHARSET );
    4515           0 :                     aLine.append("/GoToR");
    4516           0 :                     aLine.append("/F");
    4517           0 :                     bFileSpec = true;
    4518             :                     appendLiteralStringEncrypt( bSetRelative ? INetURLObject::GetRelURL( m_aContext.BaseURL, aURLNoMark,
    4519             :                                                                                          INetURLObject::WAS_ENCODED,
    4520             :                                                                                          INetURLObject::DECODE_WITH_CHARSET ) :
    4521           0 :                                                                    aURLNoMark, rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4522           0 :                     if( !aFragment.isEmpty() )
    4523             :                     {
    4524           0 :                         aLine.append("/D/");
    4525           0 :                         appendDestinationName( aFragment , aLine );
    4526           0 :                     }
    4527             :                 }
    4528             :                 else
    4529             :                 {
    4530             : // change the fragment to accommodate the bookmark (only if the file extension is PDF and
    4531             : // the requested action is of the correct type)
    4532           0 :                     if(m_aContext.DefaultLinkAction == PDFWriter::URIActionDestination &&
    4533           0 :                                bTargetHasPDFExtension && !aFragment.isEmpty() )
    4534             :                     {
    4535           0 :                         OStringBuffer aLineLoc( 1024 );
    4536           0 :                         appendDestinationName( aFragment , aLineLoc );
    4537             : //substitute the fragment
    4538           0 :                         aTargetURL.SetMark( aLineLoc.getStr() );
    4539             :                     }
    4540           0 :                     OUString aURL = aTargetURL.GetMainURL( bFileSpec ? INetURLObject::DECODE_WITH_CHARSET : INetURLObject::NO_DECODE );
    4541             :                     appendLiteralStringEncrypt(bSetRelative ? INetURLObject::GetRelURL( m_aContext.BaseURL, aURL,
    4542             :                                                                                         INetURLObject::WAS_ENCODED,
    4543             :                                                                                             bFileSpec ? INetURLObject::DECODE_WITH_CHARSET : INetURLObject::NO_DECODE
    4544             :                                                                                             ) :
    4545           0 :                                                                                aURL , rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4546           0 :                 }
    4547             : //<--- i56629
    4548             :             }
    4549           0 :             aLine.append( ">>\n" );
    4550             :         }
    4551           0 :         if( rLink.m_nStructParent > 0 )
    4552             :         {
    4553           0 :             aLine.append( "/StructParent " );
    4554           0 :             aLine.append( rLink.m_nStructParent );
    4555             :         }
    4556           0 :         aLine.append( ">>\nendobj\n\n" );
    4557           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4558           0 :     }
    4559             : 
    4560           0 :     return true;
    4561             : }
    4562             : 
    4563           0 : bool PDFWriterImpl::emitNoteAnnotations()
    4564             : {
    4565             :     // emit note annotations
    4566           0 :     int nAnnots = m_aNotes.size();
    4567           0 :     for( int i = 0; i < nAnnots; i++ )
    4568             :     {
    4569           0 :         const PDFNoteEntry& rNote       = m_aNotes[i];
    4570           0 :         if( ! updateObject( rNote.m_nObject ) )
    4571           0 :             return false;
    4572             : 
    4573           0 :         OStringBuffer aLine( 1024 );
    4574           0 :         aLine.append( rNote.m_nObject );
    4575           0 :         aLine.append( " 0 obj\n" );
    4576             : //i59651  key /F set bits Print to 1 rest to 0. We don't set NoZoom NoRotate to 1, since it's a 'should'
    4577             : // see PDF 8.4.2 and ISO 19005-1:2005 6.5.3
    4578           0 :         aLine.append( "<</Type/Annot" );
    4579           0 :         if( m_bIsPDF_A1 )
    4580           0 :             aLine.append( "/F 4" );
    4581           0 :         aLine.append( "/Subtype/Text/Rect[" );
    4582             : 
    4583           0 :         appendFixedInt( rNote.m_aRect.Left(), aLine );
    4584           0 :         aLine.append( ' ' );
    4585           0 :         appendFixedInt( rNote.m_aRect.Top(), aLine );
    4586           0 :         aLine.append( ' ' );
    4587           0 :         appendFixedInt( rNote.m_aRect.Right(), aLine );
    4588           0 :         aLine.append( ' ' );
    4589           0 :         appendFixedInt( rNote.m_aRect.Bottom(), aLine );
    4590           0 :         aLine.append( "]" );
    4591             : 
    4592             :         // contents of the note (type text string)
    4593           0 :         aLine.append( "/Contents\n" );
    4594           0 :         appendUnicodeTextStringEncrypt( rNote.m_aContents.Contents, rNote.m_nObject, aLine );
    4595           0 :         aLine.append( "\n" );
    4596             : 
    4597             :         // optional title
    4598           0 :         if( rNote.m_aContents.Title.Len() )
    4599             :         {
    4600           0 :             aLine.append( "/T" );
    4601           0 :             appendUnicodeTextStringEncrypt( rNote.m_aContents.Title, rNote.m_nObject, aLine );
    4602           0 :             aLine.append( "\n" );
    4603             :         }
    4604             : 
    4605           0 :         aLine.append( ">>\nendobj\n\n" );
    4606           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4607           0 :     }
    4608           0 :     return true;
    4609             : }
    4610             : 
    4611           0 : Font PDFWriterImpl::replaceFont( const Font& rControlFont, const Font&  rAppSetFont )
    4612             : {
    4613           0 :     bool bAdjustSize = false;
    4614             : 
    4615           0 :     Font aFont( rControlFont );
    4616           0 :     if( ! aFont.GetName().Len() )
    4617             :     {
    4618           0 :         aFont = rAppSetFont;
    4619           0 :         if( rControlFont.GetHeight() )
    4620           0 :             aFont.SetSize( Size( 0, rControlFont.GetHeight() ) );
    4621             :         else
    4622           0 :             bAdjustSize = true;
    4623           0 :         if( rControlFont.GetItalic() != ITALIC_DONTKNOW )
    4624           0 :             aFont.SetItalic( rControlFont.GetItalic() );
    4625           0 :         if( rControlFont.GetWeight() != WEIGHT_DONTKNOW )
    4626           0 :             aFont.SetWeight( rControlFont.GetWeight() );
    4627             :     }
    4628           0 :     else if( ! aFont.GetHeight() )
    4629             :     {
    4630           0 :         aFont.SetSize( rAppSetFont.GetSize() );
    4631           0 :         bAdjustSize = true;
    4632             :     }
    4633           0 :     if( bAdjustSize )
    4634             :     {
    4635           0 :         Size aFontSize = aFont.GetSize();
    4636           0 :         OutputDevice* pDefDev = Application::GetDefaultDevice();
    4637           0 :         aFontSize = OutputDevice::LogicToLogic( aFontSize, pDefDev->GetMapMode(), getMapMode() );
    4638           0 :         aFont.SetSize( aFontSize );
    4639             :     }
    4640           0 :     return aFont;
    4641             : }
    4642             : 
    4643           0 : sal_Int32 PDFWriterImpl::getBestBuiltinFont( const Font& rFont )
    4644             : {
    4645           0 :     sal_Int32 nBest = 4; // default to Helvetica
    4646           0 :     OUString aFontName( rFont.GetName() );
    4647           0 :     aFontName = aFontName.toAsciiLowerCase();
    4648             : 
    4649           0 :     if( aFontName.indexOf( "times" ) != -1 )
    4650           0 :         nBest = 8;
    4651           0 :     else if( aFontName.indexOf( "courier" ) != -1 )
    4652           0 :         nBest = 0;
    4653           0 :     else if( aFontName.indexOf( "dingbats" ) != -1 )
    4654           0 :         nBest = 13;
    4655           0 :     else if( aFontName.indexOf( "symbol" ) != -1 )
    4656           0 :         nBest = 12;
    4657           0 :     if( nBest < 12 )
    4658             :     {
    4659           0 :         if( rFont.GetItalic() == ITALIC_OBLIQUE || rFont.GetItalic() == ITALIC_NORMAL )
    4660           0 :             nBest += 1;
    4661           0 :         if( rFont.GetWeight() > WEIGHT_MEDIUM )
    4662           0 :             nBest += 2;
    4663             :     }
    4664             : 
    4665           0 :     if( m_aBuiltinFontToObjectMap.find( nBest ) == m_aBuiltinFontToObjectMap.end() )
    4666           0 :         m_aBuiltinFontToObjectMap[ nBest ] = createObject();
    4667             : 
    4668           0 :     return nBest;
    4669             : }
    4670             : 
    4671           0 : static inline const Color& replaceColor( const Color& rCol1, const Color& rCol2 )
    4672             : {
    4673           0 :     return (rCol1 == Color( COL_TRANSPARENT )) ? rCol2 : rCol1;
    4674             : }
    4675             : 
    4676           0 : void PDFWriterImpl::createDefaultPushButtonAppearance( PDFWidget& rButton, const PDFWriter::PushButtonWidget& rWidget )
    4677             : {
    4678           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4679             : 
    4680             :     // save graphics state
    4681           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4682             : 
    4683             :     // transform relative to control's coordinates since an
    4684             :     // appearance stream is a form XObject
    4685             :     // this relies on the m_aRect member of rButton NOT already being transformed
    4686             :     // to default user space
    4687           0 :     if( rWidget.Background || rWidget.Border )
    4688             :     {
    4689           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetLightColor() ) : Color( COL_TRANSPARENT ) );
    4690           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetDialogColor() ) : Color( COL_TRANSPARENT ) );
    4691           0 :         drawRectangle( rWidget.Location );
    4692             :     }
    4693             :     // prepare font to use
    4694           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetPushButtonFont() );
    4695           0 :     setFont( aFont );
    4696           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetButtonTextColor() ) );
    4697             : 
    4698           0 :     drawText( rButton.m_aRect, rButton.m_aText, rButton.m_nTextStyle );
    4699             : 
    4700             :     // create DA string while local mapmode is still in place
    4701             :     // (that is before endRedirect())
    4702           0 :     OStringBuffer aDA( 256 );
    4703           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetButtonTextColor() ), aDA );
    4704           0 :     Font aDummyFont( String( "Helvetica" ), aFont.GetSize() );
    4705           0 :     sal_Int32 nDummyBuiltin = getBestBuiltinFont( aDummyFont );
    4706           0 :     aDA.append( ' ' );
    4707           0 :     aDA.append( m_aBuiltinFonts[nDummyBuiltin].getNameObject() );
    4708           0 :     aDA.append( ' ' );
    4709           0 :     m_aPages[m_nCurrentPage].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4710           0 :     aDA.append( " Tf" );
    4711           0 :     rButton.m_aDAString = aDA.makeStringAndClear();
    4712             : 
    4713           0 :     pop();
    4714             : 
    4715           0 :     rButton.m_aAppearances[ "N" ][ "Standard" ] = new SvMemoryStream();
    4716             : 
    4717             :     /* seems like a bad hack but at least works in both AR5 and 6:
    4718             :        we draw the button ourselves and tell AR
    4719             :        the button would be totally transparent with no text
    4720             : 
    4721             :        One would expect that simply setting a normal appearance
    4722             :        should suffice, but no, as soon as the user actually presses
    4723             :        the button and an action is tied to it (gasp! a button that
    4724             :        does something) the appearance gets replaced by some crap that AR
    4725             :        creates on the fly even if no DA or MK is given. On AR6 at least
    4726             :        the DA and MK work as expected, but on AR5 this creates a region
    4727             :        filled with the background color but nor text. Urgh.
    4728             :     */
    4729           0 :     rButton.m_aMKDict = "/BC [] /BG [] /CA";
    4730           0 :     rButton.m_aMKDictCAString = "";
    4731           0 : }
    4732             : 
    4733           0 : Font PDFWriterImpl::drawFieldBorder( PDFWidget& rIntern,
    4734             :                                      const PDFWriter::AnyWidget& rWidget,
    4735             :                                      const StyleSettings& rSettings )
    4736             : {
    4737           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetFieldFont() );
    4738             : 
    4739           0 :     if( rWidget.Background || rWidget.Border )
    4740             :     {
    4741           0 :         if( rWidget.Border && rWidget.BorderColor == Color( COL_TRANSPARENT ) )
    4742             :         {
    4743           0 :             sal_Int32 nDelta = getReferenceDevice()->ImplGetDPIX() / 500;
    4744           0 :             if( nDelta < 1 )
    4745           0 :                 nDelta = 1;
    4746           0 :             setLineColor( Color( COL_TRANSPARENT ) );
    4747           0 :             Rectangle aRect = rIntern.m_aRect;
    4748           0 :             setFillColor( rSettings.GetLightBorderColor() );
    4749           0 :             drawRectangle( aRect );
    4750           0 :             aRect.Left()  += nDelta; aRect.Top()     += nDelta;
    4751           0 :             aRect.Right() -= nDelta; aRect.Bottom()  -= nDelta;
    4752           0 :             setFillColor( rSettings.GetFieldColor() );
    4753           0 :             drawRectangle( aRect );
    4754           0 :             setFillColor( rSettings.GetLightColor() );
    4755           0 :             drawRectangle( Rectangle( Point( aRect.Left(), aRect.Bottom()-nDelta ), aRect.BottomRight() ) );
    4756           0 :             drawRectangle( Rectangle( Point( aRect.Right()-nDelta, aRect.Top() ), aRect.BottomRight() ) );
    4757           0 :             setFillColor( rSettings.GetDarkShadowColor() );
    4758           0 :             drawRectangle( Rectangle( aRect.TopLeft(), Point( aRect.Left()+nDelta, aRect.Bottom() ) ) );
    4759           0 :             drawRectangle( Rectangle( aRect.TopLeft(), Point( aRect.Right(), aRect.Top()+nDelta ) ) );
    4760             :         }
    4761             :         else
    4762             :         {
    4763           0 :             setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetShadowColor() ) : Color( COL_TRANSPARENT ) );
    4764           0 :             setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    4765           0 :             drawRectangle( rIntern.m_aRect );
    4766             :         }
    4767             : 
    4768           0 :         if( rWidget.Border )
    4769             :         {
    4770             :             // adjust edit area accounting for border
    4771           0 :             sal_Int32 nDelta = aFont.GetHeight()/4;
    4772           0 :             if( nDelta < 1 )
    4773           0 :                 nDelta = 1;
    4774           0 :             rIntern.m_aRect.Left()  += nDelta;
    4775           0 :             rIntern.m_aRect.Top()   += nDelta;
    4776           0 :             rIntern.m_aRect.Right() -= nDelta;
    4777           0 :             rIntern.m_aRect.Bottom()-= nDelta;
    4778             :         }
    4779             :     }
    4780           0 :     return aFont;
    4781             : }
    4782             : 
    4783           0 : void PDFWriterImpl::createDefaultEditAppearance( PDFWidget& rEdit, const PDFWriter::EditWidget& rWidget )
    4784             : {
    4785           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4786           0 :     SvMemoryStream* pEditStream = new SvMemoryStream( 1024, 1024 );
    4787             : 
    4788           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4789             : 
    4790             :     // prepare font to use, draw field border
    4791           0 :     Font aFont = drawFieldBorder( rEdit, rWidget, rSettings );
    4792           0 :     sal_Int32 nBest = m_aContext.FieldsUseSystemFonts ? getSystemFont( aFont ): getBestBuiltinFont( aFont );
    4793             : 
    4794             :     // prepare DA string
    4795           0 :     OStringBuffer aDA( 32 );
    4796           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetFieldTextColor() ), aDA );
    4797           0 :     aDA.append( ' ' );
    4798           0 :     if( m_aContext.FieldsUseSystemFonts )
    4799             :     {
    4800           0 :         aDA.append( "/F" );
    4801           0 :         aDA.append( nBest );
    4802             : 
    4803           0 :         OStringBuffer aDR( 32 );
    4804           0 :         aDR.append( "/Font " );
    4805           0 :         aDR.append( getFontDictObject() );
    4806           0 :         aDR.append( " 0 R" );
    4807           0 :         rEdit.m_aDRDict = aDR.makeStringAndClear();
    4808             :     }
    4809             :     else
    4810           0 :         aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4811           0 :     aDA.append( ' ' );
    4812           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4813           0 :     aDA.append( " Tf" );
    4814             : 
    4815             :     /*  create an empty appearance stream, let the viewer create
    4816             :         the appearance at runtime. This is because AR5 seems to
    4817             :         paint the widget appearance always, and a dynamically created
    4818             :         appearance on top of it. AR6 is well behaved in that regard, so
    4819             :         that behaviour seems to be a bug. Anyway this empty appearance
    4820             :         relies on /NeedAppearances in the AcroForm dictionary set to "true"
    4821             :      */
    4822           0 :     beginRedirect( pEditStream, rEdit.m_aRect );
    4823           0 :     OStringBuffer aAppearance( 32 );
    4824           0 :     aAppearance.append( "/Tx BMC\nEMC\n" );
    4825           0 :     writeBuffer( aAppearance.getStr(), aAppearance.getLength() );
    4826             : 
    4827           0 :     endRedirect();
    4828           0 :     pop();
    4829             : 
    4830           0 :     rEdit.m_aAppearances[ "N" ][ "Standard" ] = pEditStream;
    4831             : 
    4832           0 :     rEdit.m_aDAString = aDA.makeStringAndClear();
    4833           0 : }
    4834             : 
    4835           0 : void PDFWriterImpl::createDefaultListBoxAppearance( PDFWidget& rBox, const PDFWriter::ListBoxWidget& rWidget )
    4836             : {
    4837           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4838           0 :     SvMemoryStream* pListBoxStream = new SvMemoryStream( 1024, 1024 );
    4839             : 
    4840           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4841             : 
    4842             :     // prepare font to use, draw field border
    4843           0 :     Font aFont = drawFieldBorder( rBox, rWidget, rSettings );
    4844           0 :     sal_Int32 nBest = m_aContext.FieldsUseSystemFonts ? getSystemFont( aFont ): getBestBuiltinFont( aFont );
    4845             : 
    4846           0 :     beginRedirect( pListBoxStream, rBox.m_aRect );
    4847           0 :     OStringBuffer aAppearance( 64 );
    4848             : 
    4849           0 :     setLineColor( Color( COL_TRANSPARENT ) );
    4850           0 :     setFillColor( replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) );
    4851           0 :     drawRectangle( rBox.m_aRect );
    4852             : 
    4853             :     // empty appearance, see createDefaultEditAppearance for reference
    4854           0 :     aAppearance.append( "/Tx BMC\nEMC\n" );
    4855           0 :     writeBuffer( aAppearance.getStr(), aAppearance.getLength() );
    4856             : 
    4857           0 :     endRedirect();
    4858           0 :     pop();
    4859             : 
    4860           0 :     rBox.m_aAppearances[ "N" ][ "Standard" ] = pListBoxStream;
    4861             : 
    4862             :     // prepare DA string
    4863           0 :     OStringBuffer aDA( 256 );
    4864             :     // prepare DA string
    4865           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetFieldTextColor() ), aDA );
    4866           0 :     aDA.append( ' ' );
    4867           0 :     if( m_aContext.FieldsUseSystemFonts )
    4868             :     {
    4869           0 :         aDA.append( "/F" );
    4870           0 :         aDA.append( nBest );
    4871             : 
    4872           0 :         OStringBuffer aDR( 32 );
    4873           0 :         aDR.append( "/Font " );
    4874           0 :         aDR.append( getFontDictObject() );
    4875           0 :         aDR.append( " 0 R" );
    4876           0 :         rBox.m_aDRDict = aDR.makeStringAndClear();
    4877             :     }
    4878             :     else
    4879           0 :         aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4880           0 :     aDA.append( ' ' );
    4881           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4882           0 :     aDA.append( " Tf" );
    4883           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    4884           0 : }
    4885             : 
    4886           0 : void PDFWriterImpl::createDefaultCheckBoxAppearance( PDFWidget& rBox, const PDFWriter::CheckBoxWidget& rWidget )
    4887             : {
    4888           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4889             : 
    4890             :     // save graphics state
    4891           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    4892             : 
    4893           0 :     if( rWidget.Background || rWidget.Border )
    4894             :     {
    4895           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetCheckedColor() ) : Color( COL_TRANSPARENT ) );
    4896           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    4897           0 :         drawRectangle( rBox.m_aRect );
    4898             :     }
    4899             : 
    4900           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetRadioCheckFont() );
    4901           0 :     setFont( aFont );
    4902           0 :     Size aFontSize = aFont.GetSize();
    4903           0 :     if( aFontSize.Height() > rBox.m_aRect.GetHeight() )
    4904           0 :         aFontSize.Height() = rBox.m_aRect.GetHeight();
    4905           0 :     sal_Int32 nDelta = aFontSize.Height()/10;
    4906           0 :     if( nDelta < 1 )
    4907           0 :         nDelta = 1;
    4908             : 
    4909           0 :     Rectangle aCheckRect, aTextRect;
    4910           0 :     if( rWidget.ButtonIsLeft )
    4911             :     {
    4912           0 :         aCheckRect.Left()   = rBox.m_aRect.Left() + nDelta;
    4913           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    4914           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    4915           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    4916             : 
    4917             :         // #i74206# handle small controls without text area
    4918           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    4919             :         {
    4920           0 :             aCheckRect.Right()  -= nDelta;
    4921           0 :             aCheckRect.Top()    += nDelta/2;
    4922           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    4923             :         }
    4924             : 
    4925           0 :         aTextRect.Left()    = rBox.m_aRect.Left() + aCheckRect.GetWidth()+5*nDelta;
    4926           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    4927           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    4928           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    4929             :     }
    4930             :     else
    4931             :     {
    4932           0 :         aCheckRect.Left()   = rBox.m_aRect.Right() - nDelta - aFontSize.Height();
    4933           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    4934           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    4935           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    4936             : 
    4937             :         // #i74206# handle small controls without text area
    4938           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    4939             :         {
    4940           0 :             aCheckRect.Left()   += nDelta;
    4941           0 :             aCheckRect.Top()    += nDelta/2;
    4942           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    4943             :         }
    4944             : 
    4945           0 :         aTextRect.Left()    = rBox.m_aRect.Left();
    4946           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    4947           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    4948           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    4949             :     }
    4950           0 :     setLineColor( Color( COL_BLACK ) );
    4951           0 :     setFillColor( Color( COL_TRANSPARENT ) );
    4952           0 :     OStringBuffer aLW( 32 );
    4953           0 :     aLW.append( "q " );
    4954           0 :     m_aPages[m_nCurrentPage].appendMappedLength( nDelta, aLW );
    4955           0 :     aLW.append( " w " );
    4956           0 :     writeBuffer( aLW.getStr(), aLW.getLength() );
    4957           0 :     drawRectangle( aCheckRect );
    4958           0 :     writeBuffer( " Q\n", 3 );
    4959           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    4960           0 :     drawText( aTextRect, rBox.m_aText, rBox.m_nTextStyle );
    4961             : 
    4962           0 :     pop();
    4963             : 
    4964           0 :     OStringBuffer aDA( 256 );
    4965           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    4966           0 :     sal_Int32 nBest = getBestBuiltinFont( Font( String( "ZapfDingbats" ), aFont.GetSize() ) );
    4967           0 :     aDA.append( ' ' );
    4968           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4969           0 :     aDA.append( " 0 Tf" );
    4970           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    4971           0 :     rBox.m_aMKDict = "/CA";
    4972           0 :     rBox.m_aMKDictCAString = "8";
    4973           0 :     rBox.m_aRect = aCheckRect;
    4974             : 
    4975             :     // create appearance streams
    4976           0 :     sal_Char cMark = '8';
    4977           0 :     sal_Int32 nCharXOffset = 1000-m_aBuiltinFonts[13].m_aWidths[sal_Int32(cMark)];
    4978           0 :     nCharXOffset *= aCheckRect.GetHeight();
    4979           0 :     nCharXOffset /= 2000;
    4980             :     sal_Int32 nCharYOffset = 1000-
    4981           0 :         (m_aBuiltinFonts[13].m_nAscent+m_aBuiltinFonts[13].m_nDescent); // descent is negative
    4982           0 :     nCharYOffset *= aCheckRect.GetHeight();
    4983           0 :     nCharYOffset /= 2000;
    4984             : 
    4985           0 :     SvMemoryStream* pCheckStream = new SvMemoryStream( 256, 256 );
    4986           0 :     beginRedirect( pCheckStream, aCheckRect );
    4987           0 :     aDA.append( "/Tx BMC\nq BT\n" );
    4988           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    4989           0 :     aDA.append( ' ' );
    4990           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4991           0 :     aDA.append( ' ' );
    4992           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aCheckRect.GetHeight() ), aDA );
    4993           0 :     aDA.append( " Tf\n" );
    4994           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nCharXOffset, aDA );
    4995           0 :     aDA.append( " " );
    4996           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nCharYOffset, aDA );
    4997           0 :     aDA.append( " Td (" );
    4998           0 :     aDA.append( cMark );
    4999           0 :     aDA.append( ") Tj\nET\nQ\nEMC\n" );
    5000           0 :     writeBuffer( aDA.getStr(), aDA.getLength() );
    5001           0 :     endRedirect();
    5002           0 :     rBox.m_aAppearances[ "N" ][ "Yes" ] = pCheckStream;
    5003             : 
    5004           0 :     SvMemoryStream* pUncheckStream = new SvMemoryStream( 256, 256 );
    5005           0 :     beginRedirect( pUncheckStream, aCheckRect );
    5006           0 :     writeBuffer( "/Tx BMC\nEMC\n", 12 );
    5007           0 :     endRedirect();
    5008           0 :     rBox.m_aAppearances[ "N" ][ "Off" ] = pUncheckStream;
    5009           0 : }
    5010             : 
    5011           0 : void PDFWriterImpl::createDefaultRadioButtonAppearance( PDFWidget& rBox, const PDFWriter::RadioButtonWidget& rWidget )
    5012             : {
    5013           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    5014             : 
    5015             :     // save graphics state
    5016           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    5017             : 
    5018           0 :     if( rWidget.Background || rWidget.Border )
    5019             :     {
    5020           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetCheckedColor() ) : Color( COL_TRANSPARENT ) );
    5021           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    5022           0 :         drawRectangle( rBox.m_aRect );
    5023             :     }
    5024             : 
    5025           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetRadioCheckFont() );
    5026           0 :     setFont( aFont );
    5027           0 :     Size aFontSize = aFont.GetSize();
    5028           0 :     if( aFontSize.Height() > rBox.m_aRect.GetHeight() )
    5029           0 :         aFontSize.Height() = rBox.m_aRect.GetHeight();
    5030           0 :     sal_Int32 nDelta = aFontSize.Height()/10;
    5031           0 :     if( nDelta < 1 )
    5032           0 :         nDelta = 1;
    5033             : 
    5034           0 :     Rectangle aCheckRect, aTextRect;
    5035           0 :     if( rWidget.ButtonIsLeft )
    5036             :     {
    5037           0 :         aCheckRect.Left()   = rBox.m_aRect.Left() + nDelta;
    5038           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    5039           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    5040           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    5041             : 
    5042             :         // #i74206# handle small controls without text area
    5043           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    5044             :         {
    5045           0 :             aCheckRect.Right()  -= nDelta;
    5046           0 :             aCheckRect.Top()    += nDelta/2;
    5047           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    5048             :         }
    5049             : 
    5050           0 :         aTextRect.Left()    = rBox.m_aRect.Left() + aCheckRect.GetWidth()+5*nDelta;
    5051           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    5052           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    5053           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    5054             :     }
    5055             :     else
    5056             :     {
    5057           0 :         aCheckRect.Left()   = rBox.m_aRect.Right() - nDelta - aFontSize.Height();
    5058           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    5059           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    5060           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    5061             : 
    5062             :         // #i74206# handle small controls without text area
    5063           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    5064             :         {
    5065           0 :             aCheckRect.Left()   += nDelta;
    5066           0 :             aCheckRect.Top()    += nDelta/2;
    5067           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    5068             :         }
    5069             : 
    5070           0 :         aTextRect.Left()    = rBox.m_aRect.Left();
    5071           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    5072           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    5073           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    5074             :     }
    5075           0 :     setLineColor( Color( COL_BLACK ) );
    5076           0 :     setFillColor( Color( COL_TRANSPARENT ) );
    5077           0 :     OStringBuffer aLW( 32 );
    5078           0 :     aLW.append( "q " );
    5079           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nDelta, aLW );
    5080           0 :     aLW.append( " w " );
    5081           0 :     writeBuffer( aLW.getStr(), aLW.getLength() );
    5082           0 :     drawEllipse( aCheckRect );
    5083           0 :     writeBuffer( " Q\n", 3 );
    5084           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    5085           0 :     drawText( aTextRect, rBox.m_aText, rBox.m_nTextStyle );
    5086             : 
    5087           0 :     pop();
    5088             : 
    5089           0 :     OStringBuffer aDA( 256 );
    5090           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5091           0 :     sal_Int32 nBest = getBestBuiltinFont( Font( String( "ZapfDingbats" ), aFont.GetSize() ) );
    5092           0 :     aDA.append( ' ' );
    5093           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5094           0 :     aDA.append( " 0 Tf" );
    5095           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    5096             : //to encrypt this (el)
    5097           0 :     rBox.m_aMKDict = "/CA";
    5098             : //after this assignement, to m_aMKDic cannot be added anything
    5099           0 :     rBox.m_aMKDictCAString = "l";
    5100             : 
    5101           0 :     rBox.m_aRect = aCheckRect;
    5102             : 
    5103             :     // create appearance streams
    5104           0 :     push( sal::static_int_cast<sal_uInt16>(~0U) );
    5105           0 :     SvMemoryStream* pCheckStream = new SvMemoryStream( 256, 256 );
    5106             : 
    5107           0 :     beginRedirect( pCheckStream, aCheckRect );
    5108           0 :     aDA.append( "/Tx BMC\nq BT\n" );
    5109           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5110           0 :     aDA.append( ' ' );
    5111           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5112           0 :     aDA.append( ' ' );
    5113           0 :     m_aPages[m_nCurrentPage].appendMappedLength( sal_Int32( aCheckRect.GetHeight() ), aDA );
    5114           0 :     aDA.append( " Tf\n0 0 Td\nET\nQ\n" );
    5115           0 :     writeBuffer( aDA.getStr(), aDA.getLength() );
    5116           0 :     setFillColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    5117           0 :     setLineColor( Color( COL_TRANSPARENT ) );
    5118           0 :     aCheckRect.Left()   += 3*nDelta;
    5119           0 :     aCheckRect.Top()    += 3*nDelta;
    5120           0 :     aCheckRect.Bottom() -= 3*nDelta;
    5121           0 :     aCheckRect.Right()  -= 3*nDelta;
    5122           0 :     drawEllipse( aCheckRect );
    5123           0 :     writeBuffer( "\nEMC\n", 5 );
    5124           0 :     endRedirect();
    5125             : 
    5126           0 :     pop();
    5127           0 :     rBox.m_aAppearances[ "N" ][ "Yes" ] = pCheckStream;
    5128             : 
    5129           0 :     SvMemoryStream* pUncheckStream = new SvMemoryStream( 256, 256 );
    5130           0 :     beginRedirect( pUncheckStream, aCheckRect );
    5131           0 :     writeBuffer( "/Tx BMC\nEMC\n", 12 );
    5132           0 :     endRedirect();
    5133           0 :     rBox.m_aAppearances[ "N" ][ "Off" ] = pUncheckStream;
    5134           0 : }
    5135             : 
    5136           0 : bool PDFWriterImpl::emitAppearances( PDFWidget& rWidget, OStringBuffer& rAnnotDict )
    5137             : {
    5138             : 
    5139             :     // TODO: check and insert default streams
    5140           0 :     OString aStandardAppearance;
    5141           0 :     switch( rWidget.m_eType )
    5142             :     {
    5143             :         case PDFWriter::CheckBox:
    5144           0 :             aStandardAppearance = OUStringToOString( rWidget.m_aValue, RTL_TEXTENCODING_ASCII_US );
    5145           0 :             break;
    5146             :         default:
    5147           0 :             break;
    5148             :     }
    5149             : 
    5150           0 :     if( rWidget.m_aAppearances.size() )
    5151             :     {
    5152           0 :         rAnnotDict.append( "/AP<<\n" );
    5153           0 :         for( PDFAppearanceMap::iterator dict_it = rWidget.m_aAppearances.begin(); dict_it != rWidget.m_aAppearances.end(); ++dict_it )
    5154             :         {
    5155           0 :             rAnnotDict.append( "/" );
    5156           0 :             rAnnotDict.append( dict_it->first );
    5157           0 :             bool bUseSubDict = (dict_it->second.size() > 1);
    5158           0 :             rAnnotDict.append( bUseSubDict ? "<<" : " " );
    5159             : 
    5160           0 :             for( PDFAppearanceStreams::const_iterator stream_it = dict_it->second.begin();
    5161           0 :                  stream_it != dict_it->second.end(); ++stream_it )
    5162             :             {
    5163           0 :                 SvMemoryStream* pApppearanceStream = stream_it->second;
    5164           0 :                 dict_it->second[ stream_it->first ] = NULL;
    5165             : 
    5166           0 :                 bool bDeflate = compressStream( pApppearanceStream );
    5167             : 
    5168           0 :                 pApppearanceStream->Seek( STREAM_SEEK_TO_END );
    5169           0 :                 sal_Int64 nStreamLen = pApppearanceStream->Tell();
    5170           0 :                 pApppearanceStream->Seek( STREAM_SEEK_TO_BEGIN );
    5171           0 :                 sal_Int32 nObject = createObject();
    5172           0 :                 CHECK_RETURN( updateObject( nObject ) );
    5173             :                 #if OSL_DEBUG_LEVEL > 1
    5174             :                 emitComment( "PDFWriterImpl::emitAppearances" );
    5175             :                 #endif
    5176           0 :                 OStringBuffer aLine;
    5177           0 :                 aLine.append( nObject );
    5178             : 
    5179             :                 aLine.append( " 0 obj\n"
    5180             :                               "<</Type/XObject\n"
    5181             :                               "/Subtype/Form\n"
    5182           0 :                               "/BBox[0 0 " );
    5183           0 :                 appendFixedInt( rWidget.m_aRect.GetWidth()-1, aLine );
    5184           0 :                 aLine.append( " " );
    5185           0 :                 appendFixedInt( rWidget.m_aRect.GetHeight()-1, aLine );
    5186             :                 aLine.append( "]\n"
    5187           0 :                               "/Resources " );
    5188           0 :                 aLine.append( getResourceDictObj() );
    5189             :                 aLine.append( " 0 R\n"
    5190           0 :                               "/Length " );
    5191           0 :                 aLine.append( nStreamLen );
    5192           0 :                 aLine.append( "\n" );
    5193           0 :                 if( bDeflate )
    5194           0 :                     aLine.append( "/Filter/FlateDecode\n" );
    5195           0 :                 aLine.append( ">>\nstream\n" );
    5196           0 :                 CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5197           0 :                 checkAndEnableStreamEncryption( nObject );
    5198           0 :                 CHECK_RETURN( writeBuffer( pApppearanceStream->GetData(), nStreamLen ) );
    5199           0 :                 disableStreamEncryption();
    5200           0 :                 CHECK_RETURN( writeBuffer( "\nendstream\nendobj\n\n", 19 ) );
    5201             : 
    5202           0 :                 if( bUseSubDict )
    5203             :                 {
    5204           0 :                     rAnnotDict.append( " /" );
    5205           0 :                     rAnnotDict.append( stream_it->first );
    5206           0 :                     rAnnotDict.append( " " );
    5207             :                 }
    5208           0 :                 rAnnotDict.append( nObject );
    5209           0 :                 rAnnotDict.append( " 0 R" );
    5210             : 
    5211           0 :                 delete pApppearanceStream;
    5212           0 :             }
    5213             : 
    5214           0 :             rAnnotDict.append( bUseSubDict ? ">>\n" : "\n" );
    5215             :         }
    5216           0 :         rAnnotDict.append( ">>\n" );
    5217           0 :         if( !aStandardAppearance.isEmpty() )
    5218             :         {
    5219           0 :             rAnnotDict.append( "/AS /" );
    5220           0 :             rAnnotDict.append( aStandardAppearance );
    5221           0 :             rAnnotDict.append( "\n" );
    5222             :         }
    5223             :     }
    5224             : 
    5225           0 :     return true;
    5226             : }
    5227             : 
    5228           0 : bool PDFWriterImpl::emitWidgetAnnotations()
    5229             : {
    5230           0 :     ensureUniqueRadioOnValues();
    5231             : 
    5232           0 :     int nAnnots = m_aWidgets.size();
    5233           0 :     for( int a = 0; a < nAnnots; a++ )
    5234             :     {
    5235           0 :         PDFWidget& rWidget = m_aWidgets[a];
    5236             : 
    5237           0 :         OStringBuffer aLine( 1024 );
    5238           0 :         OStringBuffer aValue( 256 );
    5239           0 :         aLine.append( rWidget.m_nObject );
    5240             :         aLine.append( " 0 obj\n"
    5241           0 :                       "<<" );
    5242           0 :         if( rWidget.m_eType != PDFWriter::Hierarchy )
    5243             :         {
    5244             :             // emit widget annotation only for terminal fields
    5245           0 :             if( rWidget.m_aKids.empty() )
    5246             :             {
    5247             :                 int iRectMargin;
    5248             : 
    5249           0 :                 aLine.append( "/Type/Annot/Subtype/Widget/F " );
    5250             : 
    5251           0 :                 if (rWidget.m_eType == PDFWriter::Signature)
    5252             :                 {
    5253           0 :                     aLine.append( "132\n" ); // Print & Locked
    5254           0 :                     iRectMargin = 0;
    5255             :                 }
    5256             :                 else
    5257             :                 {
    5258           0 :                     aLine.append( "4\n" );
    5259           0 :                     iRectMargin = 1;
    5260             :                 }
    5261             : 
    5262           0 :                 aLine.append("/Rect[" );
    5263           0 :                 appendFixedInt( rWidget.m_aRect.Left()-iRectMargin, aLine );
    5264           0 :                 aLine.append( ' ' );
    5265           0 :                 appendFixedInt( rWidget.m_aRect.Top()+iRectMargin, aLine );
    5266           0 :                 aLine.append( ' ' );
    5267           0 :                 appendFixedInt( rWidget.m_aRect.Right()+iRectMargin, aLine );
    5268           0 :                 aLine.append( ' ' );
    5269           0 :                 appendFixedInt( rWidget.m_aRect.Bottom()-iRectMargin, aLine );
    5270           0 :                 aLine.append( "]\n" );
    5271             :             }
    5272           0 :             aLine.append( "/FT/" );
    5273           0 :             switch( rWidget.m_eType )
    5274             :             {
    5275             :                 case PDFWriter::RadioButton:
    5276             :                 case PDFWriter::CheckBox:
    5277             :                     // for radio buttons only the RadioButton field, not the
    5278             :                     // CheckBox children should have a value, else acrobat reader
    5279             :                     // does not always check the right button
    5280             :                     // of course real check boxes (not belonging to a radio group)
    5281             :                     // need their values, too
    5282           0 :                     if( rWidget.m_eType == PDFWriter::RadioButton || rWidget.m_nRadioGroup < 0 )
    5283             :                     {
    5284           0 :                         aValue.append( "/" );
    5285             :                         // check for radio group with all buttons unpressed
    5286           0 :                         if( rWidget.m_aValue.isEmpty() )
    5287           0 :                             aValue.append( "Off" );
    5288             :                         else
    5289           0 :                             appendName( rWidget.m_aValue, aValue );
    5290             :                     }
    5291             :                 case PDFWriter::PushButton:
    5292           0 :                     aLine.append( "Btn" );
    5293           0 :                     break;
    5294             :                 case PDFWriter::ListBox:
    5295           0 :                     if( rWidget.m_nFlags & 0x200000 ) // multiselect
    5296             :                     {
    5297           0 :                         aValue.append( "[" );
    5298           0 :                         for( unsigned int i = 0; i < rWidget.m_aSelectedEntries.size(); i++ )
    5299             :                         {
    5300           0 :                             sal_Int32 nEntry = rWidget.m_aSelectedEntries[i];
    5301           0 :                             if( nEntry >= 0 && nEntry < sal_Int32(rWidget.m_aListEntries.size()) )
    5302           0 :                                 appendUnicodeTextStringEncrypt( rWidget.m_aListEntries[ nEntry ], rWidget.m_nObject, aValue );
    5303             :                         }
    5304           0 :                         aValue.append( "]" );
    5305             :                     }
    5306           0 :                     else if( rWidget.m_aSelectedEntries.size() > 0 &&
    5307           0 :                              rWidget.m_aSelectedEntries[0] >= 0 &&
    5308           0 :                              rWidget.m_aSelectedEntries[0] < sal_Int32(rWidget.m_aListEntries.size()) )
    5309             :                     {
    5310           0 :                         appendUnicodeTextStringEncrypt( rWidget.m_aListEntries[ rWidget.m_aSelectedEntries[0] ], rWidget.m_nObject, aValue );
    5311             :                     }
    5312             :                     else
    5313           0 :                         appendUnicodeTextStringEncrypt( OUString(), rWidget.m_nObject, aValue );
    5314           0 :                     aLine.append( "Ch" );
    5315           0 :                     break;
    5316             :                 case PDFWriter::ComboBox:
    5317           0 :                     appendUnicodeTextStringEncrypt( rWidget.m_aValue, rWidget.m_nObject, aValue );
    5318           0 :                     aLine.append( "Ch" );
    5319           0 :                     break;
    5320             :                 case PDFWriter::Edit:
    5321           0 :                     aLine.append( "Tx" );
    5322           0 :                     appendUnicodeTextStringEncrypt( rWidget.m_aValue, rWidget.m_nObject, aValue );
    5323           0 :                     break;
    5324             :                 case PDFWriter::Signature:
    5325           0 :                     aLine.append( "Sig" );
    5326           0 :                     aValue.append(OUStringToOString(rWidget.m_aValue, RTL_TEXTENCODING_ASCII_US));
    5327           0 :                     break;
    5328             :                 case PDFWriter::Hierarchy: // make the compiler happy
    5329           0 :                     break;
    5330             :             }
    5331           0 :             aLine.append( "\n" );
    5332           0 :             aLine.append( "/P " );
    5333           0 :             aLine.append( m_aPages[ rWidget.m_nPage ].m_nPageObject );
    5334           0 :             aLine.append( " 0 R\n" );
    5335             :         }
    5336           0 :         if( rWidget.m_nParent )
    5337             :         {
    5338           0 :             aLine.append( "/Parent " );
    5339           0 :             aLine.append( rWidget.m_nParent );
    5340           0 :             aLine.append( " 0 R\n" );
    5341             :         }
    5342           0 :         if( rWidget.m_aKids.size() )
    5343             :         {
    5344           0 :             aLine.append( "/Kids[" );
    5345           0 :             for( unsigned int i = 0; i < rWidget.m_aKids.size(); i++ )
    5346             :             {
    5347           0 :                 aLine.append( rWidget.m_aKids[i] );
    5348           0 :                 aLine.append( " 0 R" );
    5349           0 :                 aLine.append( ( (i&15) == 15 ) ? "\n" : " " );
    5350             :             }
    5351           0 :             aLine.append( "]\n" );
    5352             :         }
    5353           0 :         if( !rWidget.m_aName.isEmpty() )
    5354             :         {
    5355           0 :             aLine.append( "/T" );
    5356           0 :             appendLiteralStringEncrypt( rWidget.m_aName, rWidget.m_nObject, aLine );
    5357           0 :             aLine.append( "\n" );
    5358             :         }
    5359           0 :         if( m_aContext.Version > PDFWriter::PDF_1_2 && !rWidget.m_aDescription.isEmpty() )
    5360             :         {
    5361             :             // the alternate field name should be unicode able since it is
    5362             :             // supposed to be used in UI
    5363           0 :             aLine.append( "/TU" );
    5364           0 :             appendUnicodeTextStringEncrypt( rWidget.m_aDescription, rWidget.m_nObject, aLine );
    5365           0 :             aLine.append( "\n" );
    5366             :         }
    5367             : 
    5368           0 :         if( rWidget.m_nFlags )
    5369             :         {
    5370           0 :             aLine.append( "/Ff " );
    5371           0 :             aLine.append( rWidget.m_nFlags );
    5372           0 :             aLine.append( "\n" );
    5373             :         }
    5374           0 :         if( !aValue.isEmpty() )
    5375             :         {
    5376           0 :             OString aVal = aValue.makeStringAndClear();
    5377           0 :             aLine.append( "/V " );
    5378           0 :             aLine.append( aVal );
    5379             :             aLine.append( "\n"
    5380           0 :                           "/DV " );
    5381           0 :             aLine.append( aVal );
    5382           0 :             aLine.append( "\n" );
    5383             :         }
    5384           0 :         if( rWidget.m_eType == PDFWriter::ListBox || rWidget.m_eType == PDFWriter::ComboBox )
    5385             :         {
    5386           0 :             sal_Int32 nTI = -1;
    5387           0 :             aLine.append( "/Opt[\n" );
    5388           0 :             sal_Int32 i = 0;
    5389           0 :             for( std::vector< OUString >::const_iterator it = rWidget.m_aListEntries.begin(); it != rWidget.m_aListEntries.end(); ++it, ++i )
    5390             :             {
    5391           0 :                 appendUnicodeTextStringEncrypt( *it, rWidget.m_nObject, aLine );
    5392           0 :                 aLine.append( "\n" );
    5393           0 :                 if( *it == rWidget.m_aValue )
    5394           0 :                     nTI = i;
    5395             :             }
    5396           0 :             aLine.append( "]\n" );
    5397           0 :             if( nTI > 0 )
    5398             :             {
    5399           0 :                 aLine.append( "/TI " );
    5400           0 :                 aLine.append( nTI );
    5401           0 :                 aLine.append( "\n" );
    5402           0 :                 if( rWidget.m_nFlags & 0x200000 ) // Multiselect
    5403             :                 {
    5404           0 :                     aLine.append( "/I [" );
    5405           0 :                     aLine.append( nTI );
    5406           0 :                     aLine.append( "]\n" );
    5407             :                 }
    5408             :             }
    5409             :         }
    5410           0 :         if( rWidget.m_eType == PDFWriter::Edit && rWidget.m_nMaxLen > 0 )
    5411             :         {
    5412           0 :             aLine.append( "/MaxLen " );
    5413           0 :             aLine.append( rWidget.m_nMaxLen );
    5414           0 :             aLine.append( "\n" );
    5415             :         }
    5416           0 :         if( rWidget.m_eType == PDFWriter::PushButton )
    5417             :         {
    5418           0 :             if(!m_bIsPDF_A1)
    5419             :             {
    5420           0 :                 OStringBuffer aDest;
    5421           0 :                 if( rWidget.m_nDest != -1 && appendDest( m_aDestinationIdTranslation[ rWidget.m_nDest ], aDest ) )
    5422             :                 {
    5423           0 :                     aLine.append( "/AA<</D<</Type/Action/S/GoTo/D " );
    5424           0 :                     aLine.append( aDest.makeStringAndClear() );
    5425           0 :                     aLine.append( ">>>>\n" );
    5426             :                 }
    5427           0 :                 else if( rWidget.m_aListEntries.empty() )
    5428             :                 {
    5429             :                     // create a reset form action
    5430           0 :                     aLine.append( "/AA<</D<</Type/Action/S/ResetForm>>>>\n" );
    5431             :                 }
    5432           0 :                 else if( rWidget.m_bSubmit )
    5433             :                 {
    5434             :                     // create a submit form action
    5435           0 :                     aLine.append( "/AA<</D<</Type/Action/S/SubmitForm/F" );
    5436           0 :                     appendLiteralStringEncrypt( rWidget.m_aListEntries.front(), rWidget.m_nObject, aLine, osl_getThreadTextEncoding() );
    5437           0 :                     aLine.append( "/Flags " );
    5438             : 
    5439           0 :                     sal_Int32 nFlags = 0;
    5440           0 :                     switch( m_aContext.SubmitFormat )
    5441             :                     {
    5442             :                     case PDFWriter::HTML:
    5443           0 :                         nFlags |= 4;
    5444           0 :                         break;
    5445             :                     case PDFWriter::XML:
    5446           0 :                         if( m_aContext.Version > PDFWriter::PDF_1_3 )
    5447           0 :                             nFlags |= 32;
    5448           0 :                         break;
    5449             :                     case PDFWriter::PDF:
    5450           0 :                         if( m_aContext.Version > PDFWriter::PDF_1_3 )
    5451           0 :                             nFlags |= 256;
    5452           0 :                         break;
    5453             :                     case PDFWriter::FDF:
    5454             :                     default:
    5455           0 :                         break;
    5456             :                     }
    5457           0 :                     if( rWidget.m_bSubmitGet )
    5458           0 :                         nFlags |= 8;
    5459           0 :                     aLine.append( nFlags );
    5460           0 :                     aLine.append( ">>>>\n" );
    5461             :                 }
    5462             :                 else
    5463             :                 {
    5464             :                     // create a URI action
    5465           0 :                     aLine.append( "/AA<</D<</Type/Action/S/URI/URI(" );
    5466           0 :                     aLine.append( OUStringToOString( rWidget.m_aListEntries.front(), RTL_TEXTENCODING_ASCII_US ) );
    5467           0 :                     aLine.append( ")>>>>\n" );
    5468           0 :                 }
    5469             :             }
    5470             :             else
    5471           0 :                 m_aErrors.insert( PDFWriter::Warning_FormAction_Omitted_PDFA );
    5472             :         }
    5473           0 :         if( !rWidget.m_aDAString.isEmpty() )
    5474             :         {
    5475           0 :             if( !rWidget.m_aDRDict.isEmpty() )
    5476             :             {
    5477           0 :                 aLine.append( "/DR<<" );
    5478           0 :                 aLine.append( rWidget.m_aDRDict );
    5479           0 :                 aLine.append( ">>\n" );
    5480             :             }
    5481             :             else
    5482             :             {
    5483           0 :                 aLine.append( "/DR<</Font<<" );
    5484           0 :                 appendBuiltinFontsToDict( aLine );
    5485           0 :                 aLine.append( ">>>>\n" );
    5486             :             }
    5487           0 :             aLine.append( "/DA" );
    5488           0 :             appendLiteralStringEncrypt( rWidget.m_aDAString, rWidget.m_nObject, aLine );
    5489           0 :             aLine.append( "\n" );
    5490           0 :             if( rWidget.m_nTextStyle & TEXT_DRAW_CENTER )
    5491           0 :                 aLine.append( "/Q 1\n" );
    5492           0 :             else if( rWidget.m_nTextStyle & TEXT_DRAW_RIGHT )
    5493           0 :                 aLine.append( "/Q 2\n" );
    5494             :         }
    5495             :         // appearance charactristics for terminal fields
    5496             :         // which are supposed to have an appearance constructed
    5497             :         // by the viewer application
    5498           0 :         if( !rWidget.m_aMKDict.isEmpty() )
    5499             :         {
    5500           0 :             aLine.append( "/MK<<" );
    5501           0 :             aLine.append( rWidget.m_aMKDict );
    5502             : //add the CA string, encrypting it
    5503           0 :             appendLiteralStringEncrypt(rWidget.m_aMKDictCAString, rWidget.m_nObject, aLine);
    5504           0 :             aLine.append( ">>\n" );
    5505             :         }
    5506             : 
    5507           0 :         CHECK_RETURN( emitAppearances( rWidget, aLine ) );
    5508             : 
    5509             :         aLine.append( ">>\n"
    5510           0 :                       "endobj\n\n" );
    5511           0 :         CHECK_RETURN( updateObject( rWidget.m_nObject ) );
    5512           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5513           0 :     }
    5514           0 :     return true;
    5515             : }
    5516             : 
    5517           0 : bool PDFWriterImpl::emitAnnotations()
    5518             : {
    5519           0 :     if( m_aPages.size() < 1 )
    5520           0 :         return false;
    5521             : 
    5522           0 :     CHECK_RETURN( emitLinkAnnotations() );
    5523             : 
    5524           0 :     CHECK_RETURN( emitNoteAnnotations() );
    5525             : 
    5526           0 :     CHECK_RETURN( emitWidgetAnnotations() );
    5527             : 
    5528           0 :     return true;
    5529             : }
    5530             : 
    5531             : #undef CHECK_RETURN
    5532             : #define CHECK_RETURN( x ) if( !x ) return false
    5533             : 
    5534           0 : bool PDFWriterImpl::emitCatalog()
    5535             : {
    5536             :     // build page tree
    5537             :     // currently there is only one node that contains all leaves
    5538             : 
    5539             :     // first create a page tree node id
    5540           0 :     sal_Int32 nTreeNode = createObject();
    5541             : 
    5542             :     // emit global resource dictionary (page emit needs it)
    5543           0 :     CHECK_RETURN( emitResources() );
    5544             : 
    5545             :     // emit all pages
    5546           0 :     for( std::vector<PDFPage>::iterator it = m_aPages.begin(); it != m_aPages.end(); ++it )
    5547           0 :         if( ! it->emit( nTreeNode ) )
    5548           0 :             return false;
    5549             : 
    5550           0 :     sal_Int32 nNamedDestinationsDictionary = emitNamedDestinations();
    5551             : 
    5552           0 :     sal_Int32 nOutlineDict = emitOutline();
    5553             : 
    5554             :     //emit Output intent i59651
    5555           0 :     sal_Int32 nOutputIntentObject = emitOutputIntent();
    5556             : 
    5557             :     //emit metadata
    5558           0 :     sal_Int32 nMetadataObject = emitDocumentMetadata();
    5559             : 
    5560           0 :     sal_Int32 nStructureDict = 0;
    5561           0 :     if(m_aStructure.size() > 1)
    5562             :     {
    5563             : ///check if dummy structure containers are needed
    5564           0 :         addInternalStructureContainer(m_aStructure[0]);
    5565           0 :         nStructureDict = m_aStructure[0].m_nObject = createObject();
    5566           0 :         emitStructure( m_aStructure[ 0 ] );
    5567             :     }
    5568             : 
    5569             :     // adjust tree node file offset
    5570           0 :     if( ! updateObject( nTreeNode ) )
    5571           0 :         return false;
    5572             : 
    5573             :     // emit tree node
    5574           0 :     OStringBuffer aLine( 2048 );
    5575           0 :     aLine.append( nTreeNode );
    5576           0 :     aLine.append( " 0 obj\n" );
    5577           0 :     aLine.append( "<</Type/Pages\n" );
    5578           0 :     aLine.append( "/Resources " );
    5579           0 :     aLine.append( getResourceDictObj() );
    5580           0 :     aLine.append( " 0 R\n" );
    5581             : 
    5582           0 :     switch( m_eInheritedOrientation )
    5583             :     {
    5584           0 :         case PDFWriter::Landscape: aLine.append( "/Rotate 90\n" );break;
    5585           0 :         case PDFWriter::Seascape: aLine.append( "/Rotate -90\n" );break;
    5586             : 
    5587             :         case PDFWriter::Inherit: // actually Inherit would be a bug, but insignificant
    5588             :         case PDFWriter::Portrait:
    5589             :         default:
    5590           0 :             break;
    5591             :     }
    5592           0 :     sal_Int32 nMediaBoxWidth = 0;
    5593           0 :     sal_Int32 nMediaBoxHeight = 0;
    5594           0 :     if( m_aPages.empty() ) // sanity check, this should not happen
    5595             :     {
    5596           0 :         nMediaBoxWidth = m_nInheritedPageWidth;
    5597           0 :         nMediaBoxHeight = m_nInheritedPageHeight;
    5598             :     }
    5599             :     else
    5600             :     {
    5601           0 :         for( std::vector<PDFPage>::const_iterator iter = m_aPages.begin(); iter != m_aPages.end(); ++iter )
    5602             :         {
    5603           0 :             if( iter->m_nPageWidth > nMediaBoxWidth )
    5604           0 :                 nMediaBoxWidth = iter->m_nPageWidth;
    5605           0 :             if( iter->m_nPageHeight > nMediaBoxHeight )
    5606           0 :                 nMediaBoxHeight = iter->m_nPageHeight;
    5607             :         }
    5608             :     }
    5609           0 :     aLine.append( "/MediaBox[ 0 0 " );
    5610           0 :     aLine.append( nMediaBoxWidth );
    5611           0 :     aLine.append( ' ' );
    5612           0 :     aLine.append( nMediaBoxHeight );
    5613             :     aLine.append( " ]\n"
    5614           0 :                   "/Kids[ " );
    5615           0 :     unsigned int i = 0;
    5616           0 :     for( std::vector<PDFPage>::const_iterator iter = m_aPages.begin(); iter != m_aPages.end(); ++iter, i++ )
    5617             :     {
    5618           0 :         aLine.append( iter->m_nPageObject );
    5619           0 :         aLine.append( " 0 R" );
    5620           0 :         aLine.append( ( (i&15) == 15 ) ? "\n" : " " );
    5621             :     }
    5622             :     aLine.append( "]\n"
    5623           0 :                   "/Count " );
    5624           0 :     aLine.append( (sal_Int32)m_aPages.size() );
    5625             :     aLine.append( ">>\n"
    5626           0 :                   "endobj\n\n" );
    5627           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5628             : 
    5629             :     // emit annotation objects
    5630           0 :     CHECK_RETURN( emitAnnotations() );
    5631             : 
    5632             :     // emit Catalog
    5633           0 :     m_nCatalogObject = createObject();
    5634           0 :     if( ! updateObject( m_nCatalogObject ) )
    5635           0 :         return false;
    5636           0 :     aLine.setLength( 0 );
    5637           0 :     aLine.append( m_nCatalogObject );
    5638             :     aLine.append( " 0 obj\n"
    5639           0 :                   "<</Type/Catalog/Pages " );
    5640           0 :     aLine.append( nTreeNode );
    5641           0 :     aLine.append( " 0 R\n" );
    5642             : //--->i56629
    5643             : //check if there are named destinations to emit (root must be inside the catalog)
    5644           0 :     if( nNamedDestinationsDictionary )
    5645             :     {
    5646           0 :         aLine.append("/Dests ");
    5647           0 :         aLine.append( nNamedDestinationsDictionary );
    5648           0 :         aLine.append( " 0 R\n" );
    5649             :     }
    5650             : //<----
    5651           0 :     if( m_aContext.PageLayout != PDFWriter::DefaultLayout )
    5652           0 :         switch(  m_aContext.PageLayout )
    5653             :         {
    5654             :         default :
    5655             :         case  PDFWriter::SinglePage :
    5656           0 :             aLine.append( "/PageLayout/SinglePage\n" );
    5657           0 :             break;
    5658             :         case  PDFWriter::Continuous :
    5659           0 :             aLine.append( "/PageLayout/OneColumn\n" );
    5660           0 :             break;
    5661             :         case  PDFWriter::ContinuousFacing :
    5662             : //the flag m_aContext.FirstPageLeft below is used to set the page on the left side
    5663           0 :             aLine.append( "/PageLayout/TwoColumnRight\n" );//odd page on the right side
    5664           0 :             break;
    5665             :         }
    5666           0 :     if( m_aContext.PDFDocumentMode != PDFWriter::ModeDefault && !m_aContext.OpenInFullScreenMode )
    5667           0 :         switch(  m_aContext.PDFDocumentMode )
    5668             :         {
    5669             :         default :
    5670           0 :             aLine.append( "/PageMode/UseNone\n" );
    5671           0 :             break;
    5672             :         case PDFWriter::UseOutlines :
    5673           0 :             aLine.append( "/PageMode/UseOutlines\n" ); //document is opened with outline pane open
    5674           0 :             break;
    5675             :         case PDFWriter::UseThumbs :
    5676           0 :             aLine.append( "/PageMode/UseThumbs\n" ); //document is opened with thumbnails pane open
    5677           0 :             break;
    5678             :         }
    5679           0 :     else if( m_aContext.OpenInFullScreenMode )
    5680           0 :         aLine.append( "/PageMode/FullScreen\n" ); //document is opened full screen
    5681             : 
    5682           0 :     OStringBuffer aInitPageRef;
    5683           0 :     if( m_aContext.InitialPage >= 0 && m_aContext.InitialPage < (sal_Int32)m_aPages.size() )
    5684             :     {
    5685           0 :         aInitPageRef.append( m_aPages[m_aContext.InitialPage].m_nPageObject );
    5686           0 :         aInitPageRef.append( " 0 R" );
    5687             :     }
    5688             :     else
    5689           0 :         aInitPageRef.append( "0" );
    5690             : 
    5691           0 :     switch( m_aContext.PDFDocumentAction )
    5692             :     {
    5693             :     case PDFWriter::ActionDefault :     //do nothing, this is the Acrobat default
    5694             :     default:
    5695           0 :         if( aInitPageRef.getLength() > 1 )
    5696             :         {
    5697           0 :             aLine.append( "/OpenAction[" );
    5698           0 :             aLine.append( aInitPageRef.makeStringAndClear() );
    5699           0 :             aLine.append( " /XYZ null null 0]\n" );
    5700             :         }
    5701           0 :         break;
    5702             :     case PDFWriter::FitInWindow :
    5703           0 :         aLine.append( "/OpenAction[" );
    5704           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5705           0 :         aLine.append( " /Fit]\n" ); //Open fit page
    5706           0 :         break;
    5707             :     case PDFWriter::FitWidth :
    5708           0 :         aLine.append( "/OpenAction[" );
    5709           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5710           0 :         aLine.append( " /FitH " );
    5711           0 :         aLine.append( m_nInheritedPageHeight );//Open fit width
    5712           0 :         aLine.append( "]\n" );
    5713           0 :         break;
    5714             :     case PDFWriter::FitVisible :
    5715           0 :         aLine.append( "/OpenAction[" );
    5716           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5717           0 :         aLine.append( " /FitBH " );
    5718           0 :         aLine.append( m_nInheritedPageHeight );//Open fit visible
    5719           0 :         aLine.append( "]\n" );
    5720           0 :         break;
    5721             :     case PDFWriter::ActionZoom :
    5722           0 :         aLine.append( "/OpenAction[" );
    5723           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5724           0 :         aLine.append( " /XYZ null null " );
    5725           0 :         if( m_aContext.Zoom >= 50 && m_aContext.Zoom <= 1600 )
    5726           0 :             aLine.append( (double)m_aContext.Zoom/100.0 );
    5727             :         else
    5728           0 :             aLine.append( "0" );
    5729           0 :         aLine.append( "]\n" );
    5730           0 :         break;
    5731             :     }
    5732             : // viewer preferences, if we had some, then emit
    5733           0 :     if( m_aContext.HideViewerToolbar ||
    5734           0 :         ( m_aContext.Version > PDFWriter::PDF_1_3 && m_aContext.DocumentInfo.Title.Len() && m_aContext.DisplayPDFDocumentTitle ) ||
    5735           0 :         m_aContext.HideViewerMenubar ||
    5736           0 :         m_aContext.HideViewerWindowControls || m_aContext.FitWindow ||
    5737           0 :         m_aContext.CenterWindow || (m_aContext.FirstPageLeft  &&  m_aContext.PageLayout == PDFWriter::ContinuousFacing ) ||
    5738             :         m_aContext.OpenInFullScreenMode )
    5739             :     {
    5740           0 :         aLine.append( "/ViewerPreferences<<" );
    5741           0 :         if( m_aContext.HideViewerToolbar )
    5742           0 :             aLine.append( "/HideToolbar true\n" );
    5743           0 :         if( m_aContext.HideViewerMenubar )
    5744           0 :             aLine.append( "/HideMenubar true\n" );
    5745           0 :         if( m_aContext.HideViewerWindowControls )
    5746           0 :             aLine.append( "/HideWindowUI true\n" );
    5747           0 :         if( m_aContext.FitWindow )
    5748           0 :             aLine.append( "/FitWindow true\n" );
    5749           0 :         if( m_aContext.CenterWindow )
    5750           0 :             aLine.append( "/CenterWindow true\n" );
    5751           0 :         if( m_aContext.Version > PDFWriter::PDF_1_3 && m_aContext.DocumentInfo.Title.Len() && m_aContext.DisplayPDFDocumentTitle )
    5752           0 :             aLine.append( "/DisplayDocTitle true\n" );
    5753           0 :         if( m_aContext.FirstPageLeft &&  m_aContext.PageLayout == PDFWriter::ContinuousFacing )
    5754           0 :             aLine.append( "/Direction/R2L\n" );
    5755           0 :         if( m_aContext.OpenInFullScreenMode )
    5756           0 :             switch( m_aContext.PDFDocumentMode )
    5757             :             {
    5758             :             default :
    5759             :             case PDFWriter::ModeDefault :
    5760           0 :                 aLine.append( "/NonFullScreenPageMode/UseNone\n" );
    5761           0 :                 break;
    5762             :             case PDFWriter::UseOutlines :
    5763           0 :                 aLine.append( "/NonFullScreenPageMode/UseOutlines\n" );
    5764           0 :                 break;
    5765             :             case PDFWriter::UseThumbs :
    5766           0 :                 aLine.append( "/NonFullScreenPageMode/UseThumbs\n" );
    5767           0 :                 break;
    5768             :             }
    5769           0 :         aLine.append( ">>\n" );
    5770             :     }
    5771             : 
    5772           0 :     if( nOutlineDict )
    5773             :     {
    5774           0 :         aLine.append( "/Outlines " );
    5775           0 :         aLine.append( nOutlineDict );
    5776           0 :         aLine.append( " 0 R\n" );
    5777             :     }
    5778           0 :     if( nStructureDict )
    5779             :     {
    5780           0 :         aLine.append( "/StructTreeRoot " );
    5781           0 :         aLine.append( nStructureDict );
    5782           0 :         aLine.append( " 0 R\n" );
    5783             :     }
    5784           0 :     if( !m_aContext.DocumentLocale.Language.isEmpty() )
    5785             :     {
    5786           0 :         OUStringBuffer aLocBuf( 16 );
    5787           0 :         aLocBuf.append( m_aContext.DocumentLocale.Language.toAsciiLowerCase() );
    5788           0 :         if( !m_aContext.DocumentLocale.Country.isEmpty() )
    5789             :         {
    5790           0 :             aLocBuf.append( sal_Unicode('-') );
    5791           0 :             aLocBuf.append( m_aContext.DocumentLocale.Country );
    5792             :         }
    5793           0 :         aLine.append( "/Lang" );
    5794           0 :         appendLiteralStringEncrypt( aLocBuf.makeStringAndClear(), m_nCatalogObject, aLine );
    5795           0 :         aLine.append( "\n" );
    5796             :     }
    5797           0 :     if( m_aContext.Tagged && m_aContext.Version > PDFWriter::PDF_1_3 )
    5798             :     {
    5799           0 :         aLine.append( "/MarkInfo<</Marked true>>\n" );
    5800             :     }
    5801           0 :     if( m_aWidgets.size() > 0 )
    5802             :     {
    5803           0 :         aLine.append( "/AcroForm<</Fields[\n" );
    5804           0 :         int nWidgets = m_aWidgets.size();
    5805           0 :         int nOut = 0;
    5806           0 :         for( int j = 0; j < nWidgets; j++ )
    5807             :         {
    5808             :             // output only root fields
    5809           0 :             if( m_aWidgets[j].m_nParent < 1 )
    5810             :             {
    5811           0 :                 aLine.append( m_aWidgets[j].m_nObject );
    5812           0 :                 aLine.append( (nOut++ % 5)==4 ? " 0 R\n" : " 0 R " );
    5813             :             }
    5814             :         }
    5815           0 :         aLine.append( "\n]" );
    5816             : 
    5817             : #if !defined(ANDROID) && !defined(IOS)
    5818           0 :         if (m_nSignatureObject != -1)
    5819           0 :             aLine.append( "/SigFlags 3");
    5820             : #endif
    5821             : 
    5822           0 :         aLine.append( "/DR " );
    5823           0 :         aLine.append( getResourceDictObj() );
    5824           0 :         aLine.append( " 0 R" );
    5825             :         // /NeedAppearances must not be used if PDF is signed
    5826           0 :         if( m_bIsPDF_A1
    5827             : #if !defined(ANDROID) && !defined(IOS)
    5828           0 :             || ( m_nSignatureObject != -1 )
    5829             : #endif
    5830             :             )
    5831           0 :             aLine.append( ">>\n" );
    5832             :         else
    5833           0 :             aLine.append( "/NeedAppearances true>>\n" );
    5834             :     }
    5835             : //--->i59651
    5836             : //check if there is a Metadata object
    5837           0 :     if( nOutputIntentObject )
    5838             :     {
    5839           0 :         aLine.append("/OutputIntents[");
    5840           0 :         aLine.append( nOutputIntentObject );
    5841           0 :         aLine.append( " 0 R]" );
    5842             :     }
    5843           0 :     if( nMetadataObject )
    5844             :     {
    5845           0 :         aLine.append("/Metadata ");
    5846           0 :         aLine.append( nMetadataObject );
    5847           0 :         aLine.append( " 0 R" );
    5848             :     }
    5849             : //<----
    5850             :     aLine.append( ">>\n"
    5851           0 :                   "endobj\n\n" );
    5852           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5853             : 
    5854           0 :     return true;
    5855             : }
    5856             : 
    5857             : #if !defined(ANDROID) && !defined(IOS)
    5858             : 
    5859           0 : bool PDFWriterImpl::emitSignature()
    5860             : {
    5861           0 :     if( !updateObject( m_nSignatureObject ) )
    5862           0 :         return false;
    5863             : 
    5864           0 :     OStringBuffer aLine( 0x5000 );
    5865           0 :     aLine.append( m_nSignatureObject );
    5866           0 :     aLine.append( " 0 obj\n" );
    5867           0 :     aLine.append("<</Contents <" );
    5868             : 
    5869           0 :     sal_uInt64 nOffset = ~0U;
    5870           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nOffset ) ) );
    5871             : 
    5872           0 :     m_nSignatureContentOffset = nOffset + aLine.getLength();
    5873             : 
    5874             :     // reserve some space for the PKCS#7 object
    5875           0 :     OStringBuffer aContentFiller( MAX_SIGNATURE_CONTENT_LENGTH );
    5876           0 :     comphelper::string::padToLength(aContentFiller, MAX_SIGNATURE_CONTENT_LENGTH, '0');
    5877           0 :     aLine.append( aContentFiller.makeStringAndClear() );
    5878           0 :     aLine.append( ">\n/Type/Sig/SubFilter/adbe.pkcs7.detached");
    5879             : 
    5880           0 :     if( m_aContext.DocumentInfo.Author.Len() )
    5881             :     {
    5882           0 :         aLine.append( "/Name" );
    5883           0 :         appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Author, m_nSignatureObject, aLine );
    5884             :     }
    5885             : 
    5886           0 :     aLine.append( " /M ");
    5887           0 :     appendLiteralStringEncrypt( m_aCreationDateString, m_nSignatureObject, aLine );
    5888             : 
    5889           0 :     aLine.append( " /ByteRange [ 0 ");
    5890           0 :     aLine.append( m_nSignatureContentOffset - 1, 10 );
    5891           0 :     aLine.append( " " );
    5892           0 :     aLine.append( m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1, 10 );
    5893           0 :     aLine.append( " " );
    5894             : 
    5895           0 :     m_nSignatureLastByteRangeNoOffset = nOffset + aLine.getLength();
    5896             : 
    5897             :     // mark the last ByteRange no and add some space. Now, we don't know
    5898             :     // how many bytes we need for this ByteRange value
    5899             :     // The real value will be overwritten in the finalizeSignature method
    5900           0 :     OStringBuffer aByteRangeFiller( 100  );
    5901           0 :     comphelper::string::padToLength(aByteRangeFiller, 100, ' ');
    5902           0 :     aLine.append( aByteRangeFiller.makeStringAndClear() );
    5903           0 :     aLine.append("  /Filter/Adobe.PPKMS");
    5904             : 
    5905             :     //emit reason, location and contactinfo
    5906           0 :     if ( !m_aContext.SignReason.isEmpty() )
    5907             :     {
    5908           0 :         aLine.append("/Reason");
    5909           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignReason, m_nSignatureObject, aLine );
    5910             :     }
    5911             : 
    5912           0 :     if ( !m_aContext.SignLocation.isEmpty() )
    5913             :     {
    5914           0 :         aLine.append("/Location");
    5915           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignLocation, m_nSignatureObject, aLine );
    5916             :     }
    5917             : 
    5918           0 :     if ( !m_aContext.SignContact.isEmpty() )
    5919             :     {
    5920           0 :         aLine.append("/ContactInfo");
    5921           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignContact, m_nSignatureObject, aLine );
    5922             :     }
    5923             : 
    5924           0 :     aLine.append(" >>\nendobj\n\n" );
    5925             : 
    5926           0 :     if (!writeBuffer( aLine.getStr(), aLine.getLength() ))
    5927           0 :         return false;
    5928             : 
    5929           0 :     return true;
    5930             : }
    5931             : 
    5932           0 : char *PDFSigningPKCS7PasswordCallback(PK11SlotInfo * /*slot*/, PRBool /*retry*/, void *arg)
    5933             : {
    5934           0 :     return (char *)arg;
    5935             : }
    5936             : 
    5937           0 : bool PDFWriterImpl::finalizeSignature()
    5938             : {
    5939             : 
    5940           0 :     if (!m_aContext.SignCertificate.is())
    5941           0 :         return false;
    5942             : 
    5943             :     // 1- calculate last ByteRange value
    5944           0 :     sal_uInt64 nOffset = ~0U;
    5945           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nOffset ) ) );
    5946             : 
    5947           0 :     sal_Int64 nLastByteRangeNo = nOffset - (m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1);
    5948             : 
    5949             :     // 2- overwrite the value to the m_nSignatureLastByteRangeNoOffset position
    5950           0 :     sal_uInt64 nWritten = 0;
    5951           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, m_nSignatureLastByteRangeNoOffset ) ) );
    5952           0 :     OStringBuffer aByteRangeNo( 256 );
    5953           0 :     aByteRangeNo.append( nLastByteRangeNo, 10);
    5954           0 :     aByteRangeNo.append( " ]" );
    5955             : 
    5956           0 :     if( osl_writeFile( m_aFile, aByteRangeNo.getStr(), aByteRangeNo.getLength(), &nWritten ) != osl_File_E_None )
    5957             :     {
    5958           0 :         CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, nOffset ) ) );
    5959           0 :         return false;
    5960             :     }
    5961             : 
    5962             :     // 3- create the PKCS#7 object using NSS
    5963           0 :     com::sun::star::uno::Sequence< sal_Int8 > derEncoded = m_aContext.SignCertificate->getEncoded();
    5964             : 
    5965           0 :     if (!derEncoded.hasElements())
    5966           0 :         return false;
    5967             : 
    5968           0 :     sal_Int8* n_derArray = derEncoded.getArray();
    5969           0 :     sal_Int32 n_derLength = derEncoded.getLength();
    5970             : 
    5971           0 :     NSS_NoDB_Init(".");
    5972             : 
    5973           0 :     CERTCertificate *cert = CERT_DecodeCertFromPackage(reinterpret_cast<char *>(n_derArray), n_derLength);
    5974             : 
    5975           0 :     if (!cert)
    5976             :     {
    5977             :         SAL_WARN("vcl.gdi", "PDF Signing: Error occured, certificate cannot be reconstructed.");
    5978           0 :         return false;
    5979             :     }
    5980             : 
    5981             :     SAL_WARN("vcl.gdi", "PDF Signing: Certificate Subject: " <<  cert->subjectName << "\n\tCertificate Issuer: " << cert->issuerName);
    5982             : 
    5983             :     // Prepare buffer and calculate PDF file digest
    5984           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, 0) ) );
    5985             : 
    5986           0 :     HASHContext *hc = HASH_Create(HASH_AlgSHA1);
    5987             : 
    5988           0 :     if (!hc)
    5989             :     {
    5990             :         SAL_WARN("vcl.gdi", "PDF Signing: SHA1 HASH_Create failed!");
    5991           0 :         return false;
    5992             :     }
    5993             : 
    5994           0 :     HASH_Begin(hc);
    5995             : 
    5996           0 :     char *buffer = new char[m_nSignatureContentOffset + 1];
    5997             :     sal_uInt64 bytesRead;
    5998             : 
    5999             :     //FIXME: Check if SHA1 is calculated from the correct byterange
    6000             : 
    6001           0 :     CHECK_RETURN( (osl_File_E_None == osl_readFile( m_aFile, buffer, m_nSignatureContentOffset - 1 , &bytesRead ) ) );
    6002           0 :     if (bytesRead != (sal_uInt64)m_nSignatureContentOffset - 1)
    6003             :         SAL_WARN("vcl.gdi", "PDF Signing: First buffer read failed!");
    6004             : 
    6005           0 :     HASH_Update(hc, reinterpret_cast<const unsigned char*>(buffer), bytesRead);
    6006           0 :     delete[] buffer;
    6007             : 
    6008           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1) ) );
    6009           0 :     buffer = new char[nLastByteRangeNo + 1];
    6010           0 :     CHECK_RETURN( (osl_File_E_None == osl_readFile( m_aFile, buffer, nLastByteRangeNo, &bytesRead ) ) );
    6011           0 :     if (bytesRead != (sal_uInt64) nLastByteRangeNo)
    6012             :         SAL_WARN("vcl.gdi", "PDF Signing: Second buffer read failed!");
    6013             : 
    6014           0 :     HASH_Update(hc, reinterpret_cast<const unsigned char*>(buffer), bytesRead);
    6015           0 :     delete[] buffer;
    6016             : 
    6017             :     SECItem digest;
    6018             :     unsigned char hash[SHA1_LENGTH];
    6019           0 :     digest.data = hash;
    6020           0 :     HASH_End(hc, digest.data, &digest.len, SHA1_LENGTH);
    6021           0 :     HASH_Destroy(hc);
    6022             : 
    6023           0 :     const char *pass = OUStringToOString( m_aContext.SignPassword, RTL_TEXTENCODING_UTF8 ).getStr();
    6024             : 
    6025           0 :     NSSCMSMessage *cms_msg = NSS_CMSMessage_Create(NULL);
    6026           0 :     if (!cms_msg)
    6027             :     {
    6028             :         SAL_WARN("vcl.gdi", "PDF signing: can't create new CMS message.");
    6029           0 :         return false;
    6030             :     }
    6031             : 
    6032           0 :     NSSCMSSignedData *cms_sd = NSS_CMSSignedData_Create(cms_msg);
    6033           0 :     if (!cms_sd)
    6034             :     {
    6035             :         SAL_WARN("vcl.gdi", "PDF signing: can't create CMS SignedData.");
    6036           0 :         return false;
    6037             :     }
    6038             : 
    6039           0 :     NSSCMSContentInfo *cms_cinfo = NSS_CMSMessage_GetContentInfo(cms_msg);
    6040           0 :     if (NSS_CMSContentInfo_SetContent_SignedData(cms_msg, cms_cinfo, cms_sd) != SECSuccess)
    6041             :     {
    6042             :         SAL_WARN("vcl.gdi", "PDF signing: Can't set CMS content signed data.");
    6043           0 :         return false;
    6044             :     }
    6045             : 
    6046           0 :     cms_cinfo = NSS_CMSSignedData_GetContentInfo(cms_sd);
    6047             :     //attach NULL data as detached data
    6048           0 :     if (NSS_CMSContentInfo_SetContent_Data(cms_msg, cms_cinfo, NULL, PR_TRUE) != SECSuccess)
    6049             :     {
    6050             :         SAL_WARN("vcl.gdi", "PDF signing: Can't set CMS content data.");
    6051           0 :         return false;
    6052             :     }
    6053             : 
    6054           0 :     NSSCMSSignerInfo *cms_signer = NSS_CMSSignerInfo_Create(cms_msg, cert, SEC_OID_SHA1);
    6055           0 :     if (!cms_signer)
    6056             :     {
    6057             :         SAL_WARN("vcl.gdi", "PDF signing: can't create CMS SignerInfo.");
    6058           0 :         return false;
    6059             :     }
    6060             : 
    6061           0 :     if (NSS_CMSSignerInfo_IncludeCerts(cms_signer, NSSCMSCM_CertChain, certUsageEmailSigner) != SECSuccess)
    6062             :     {
    6063             :         SAL_WARN("vcl.gdi", "PDF signing: can't include cert chain.");
    6064           0 :         return false;
    6065             :     }
    6066             : 
    6067           0 :     if (NSS_CMSSignerInfo_AddSigningTime(cms_signer, PR_Now()) != SECSuccess)
    6068             :     {
    6069             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signing time.");
    6070           0 :         return false;
    6071             :     }
    6072             : 
    6073           0 :     if (NSS_CMSSignedData_AddCertificate(cms_sd, cert) != SECSuccess)
    6074             :     {
    6075             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signer certificate.");
    6076           0 :         return false;
    6077             :     }
    6078             : 
    6079           0 :     if (NSS_CMSSignedData_AddSignerInfo(cms_sd, cms_signer) != SECSuccess)
    6080             :     {
    6081             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signer info.");
    6082           0 :         return false;
    6083             :     }
    6084             : 
    6085           0 :     if (NSS_CMSSignedData_SetDigestValue(cms_sd, SEC_OID_SHA1, &digest) != SECSuccess)
    6086             :     {
    6087             :         SAL_WARN("vcl.gdi", "PDF signing: can't set PDF digest value.");
    6088           0 :         return false;
    6089             :     }
    6090             : 
    6091             :     SAL_WARN("vcl.gdi","PKCS7 Object created successfully!");
    6092             : 
    6093             :     SECItem cms_output;
    6094           0 :     cms_output.data = 0;
    6095           0 :     cms_output.len = 0;
    6096           0 :     PLArenaPool *arena = PORT_NewArena(MAX_SIGNATURE_CONTENT_LENGTH);
    6097             :     NSSCMSEncoderContext *cms_ecx;
    6098             : 
    6099             :     //FIXME: Check if password is passed correctly to SEC_PKCS7CreateSignedData function
    6100           0 :     cms_ecx = NSS_CMSEncoder_Start(cms_msg, NULL, NULL, &cms_output, arena, (PK11PasswordFunc)::PDFSigningPKCS7PasswordCallback, (void *)pass, NULL, NULL, NULL, NULL);
    6101             : 
    6102           0 :     if (!cms_ecx)
    6103             :     {
    6104             :         SAL_WARN("vcl.gdi", "PDF Signing: can't start DER encoder.");
    6105           0 :         return false;
    6106             :     }
    6107             :     SAL_WARN("vcl.gdi", "PDF Signing: Started DER encoding.");
    6108             : 
    6109           0 :     if (NSS_CMSEncoder_Finish(cms_ecx) != SECSuccess)
    6110             :     {
    6111             :         SAL_WARN("vcl.gdi", "PDF Signing: can't finish DER encoder.");
    6112           0 :         return false;
    6113             :     }
    6114             :     SAL_WARN("vcl.gdi", "PDF Signing: Finished DER encoding.");
    6115             : 
    6116           0 :     OStringBuffer cms_hexbuffer;
    6117             : 
    6118           0 :     for (unsigned int i = 0; i < cms_output.len ; i++)
    6119           0 :         appendHex(cms_output.data[i], cms_hexbuffer);
    6120             : 
    6121             :     SAL_WARN("vcl.gdi","PKCS7 object encoded successfully!");
    6122             : 
    6123             :     // Set file pointer to the m_nSignatureContentOffset, we're ready to overwrite PKCS7 object
    6124           0 :     nWritten = 0;
    6125           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, m_nSignatureContentOffset) ) );
    6126           0 :     osl_writeFile(m_aFile, cms_hexbuffer.getStr(), cms_hexbuffer.getLength(), &nWritten);
    6127             : 
    6128           0 :     NSS_CMSMessage_Destroy(cms_msg);
    6129             : 
    6130           0 :     CHECK_RETURN( (osl_File_E_None == osl_setFilePos( m_aFile, osl_Pos_Absolut, nOffset ) ) );
    6131           0 :     return true;
    6132             : }
    6133             : 
    6134             : #endif
    6135             : 
    6136           0 : sal_Int32 PDFWriterImpl::emitInfoDict( )
    6137             : {
    6138           0 :     sal_Int32 nObject = createObject();
    6139             : 
    6140           0 :     if( updateObject( nObject ) )
    6141             :     {
    6142           0 :         OStringBuffer aLine( 1024 );
    6143           0 :         aLine.append( nObject );
    6144             :         aLine.append( " 0 obj\n"
    6145           0 :                       "<<" );
    6146           0 :         if( m_aContext.DocumentInfo.Title.Len() )
    6147             :         {
    6148           0 :             aLine.append( "/Title" );
    6149           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Title, nObject, aLine );
    6150           0 :             aLine.append( "\n" );
    6151             :         }
    6152           0 :         if( m_aContext.DocumentInfo.Author.Len() )
    6153             :         {
    6154           0 :             aLine.append( "/Author" );
    6155           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Author, nObject, aLine );
    6156           0 :             aLine.append( "\n" );
    6157             :         }
    6158           0 :         if( m_aContext.DocumentInfo.Subject.Len() )
    6159             :         {
    6160           0 :             aLine.append( "/Subject" );
    6161           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Subject, nObject, aLine );
    6162           0 :             aLine.append( "\n" );
    6163             :         }
    6164           0 :         if( m_aContext.DocumentInfo.Keywords.Len() )
    6165             :         {
    6166           0 :             aLine.append( "/Keywords" );
    6167           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Keywords, nObject, aLine );
    6168           0 :             aLine.append( "\n" );
    6169             :         }
    6170           0 :         if( m_aContext.DocumentInfo.Creator.Len() )
    6171             :         {
    6172           0 :             aLine.append( "/Creator" );
    6173           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Creator, nObject, aLine );
    6174           0 :             aLine.append( "\n" );
    6175             :         }
    6176           0 :         if( m_aContext.DocumentInfo.Producer.Len() )
    6177             :         {
    6178           0 :             aLine.append( "/Producer" );
    6179           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Producer, nObject, aLine );
    6180           0 :             aLine.append( "\n" );
    6181             :         }
    6182             : 
    6183           0 :          aLine.append( "/CreationDate" );
    6184           0 :          appendLiteralStringEncrypt( m_aCreationDateString, nObject, aLine );
    6185           0 :         aLine.append( ">>\nendobj\n\n" );
    6186           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6187           0 :             nObject = 0;
    6188             :     }
    6189             :     else
    6190           0 :         nObject = 0;
    6191             : 
    6192           0 :     return nObject;
    6193             : }
    6194             : 
    6195             : //--->i56629
    6196             : // Part of this function may be shared with method appendDest.
    6197             : //
    6198           0 : sal_Int32 PDFWriterImpl::emitNamedDestinations()
    6199             : {
    6200           0 :     sal_Int32  nCount = m_aNamedDests.size();
    6201           0 :     if( nCount <= 0 )
    6202           0 :         return 0;//define internal error
    6203             : 
    6204             : //get the object number for all the destinations
    6205           0 :     sal_Int32 nObject = createObject();
    6206             : 
    6207           0 :     if( updateObject( nObject ) )
    6208             :     {
    6209             : //emit the dictionary
    6210           0 :         OStringBuffer aLine( 1024 );
    6211           0 :         aLine.append( nObject );
    6212             :         aLine.append( " 0 obj\n"
    6213           0 :                       "<<" );
    6214             : 
    6215             :         sal_Int32  nDestID;
    6216           0 :         for( nDestID = 0; nDestID < nCount; nDestID++ )
    6217             :         {
    6218           0 :             const PDFNamedDest& rDest   = m_aNamedDests[ nDestID ];
    6219             : // In order to correctly function both under an Internet browser and
    6220             : // directly with a reader (provided the reader has the feature) we
    6221             : // need to set the name of the destination the same way it will be encoded
    6222             : // in an Internet link
    6223             :             INetURLObject aLocalURL(
    6224           0 :                 OUString( "http://ahost.ax"  ) ); //dummy location, won't be used
    6225           0 :             aLocalURL.SetMark( rDest.m_aDestName );
    6226             : 
    6227           0 :             const OUString aName   = aLocalURL.GetMark( INetURLObject::NO_DECODE ); //same coding as
    6228             :             // in link creation ( see PDFWriterImpl::emitLinkAnnotations )
    6229           0 :             const PDFPage& rDestPage    = m_aPages[ rDest.m_nPage ];
    6230             : 
    6231           0 :             aLine.append( '/' );
    6232           0 :             appendDestinationName( aName, aLine ); // this conversion must be done when forming the link to target ( see in emitCatalog )
    6233           0 :             aLine.append( '[' ); // the '[' can be emitted immediately, because the appendDestinationName function
    6234             :                                  //maps the preceeding character properly
    6235           0 :             aLine.append( rDestPage.m_nPageObject );
    6236           0 :             aLine.append( " 0 R" );
    6237             : 
    6238           0 :             switch( rDest.m_eType )
    6239             :             {
    6240             :             case PDFWriter::XYZ:
    6241             :             default:
    6242           0 :                 aLine.append( "/XYZ " );
    6243           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6244           0 :                 aLine.append( ' ' );
    6245           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6246           0 :                 aLine.append( " 0" );
    6247           0 :                 break;
    6248             :             case PDFWriter::Fit:
    6249           0 :                 aLine.append( "/Fit" );
    6250           0 :                 break;
    6251             :             case PDFWriter::FitRectangle:
    6252           0 :                 aLine.append( "/FitR " );
    6253           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6254           0 :                 aLine.append( ' ' );
    6255           0 :                 appendFixedInt( rDest.m_aRect.Top(), aLine );
    6256           0 :                 aLine.append( ' ' );
    6257           0 :                 appendFixedInt( rDest.m_aRect.Right(), aLine );
    6258           0 :                 aLine.append( ' ' );
    6259           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6260           0 :                 break;
    6261             :             case PDFWriter::FitHorizontal:
    6262           0 :                 aLine.append( "/FitH " );
    6263           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6264           0 :                 break;
    6265             :             case PDFWriter::FitVertical:
    6266           0 :                 aLine.append( "/FitV " );
    6267           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6268           0 :                 break;
    6269             :             case PDFWriter::FitPageBoundingBox:
    6270           0 :                 aLine.append( "/FitB" );
    6271           0 :                 break;
    6272             :             case PDFWriter::FitPageBoundingBoxHorizontal:
    6273           0 :                 aLine.append( "/FitBH " );
    6274           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6275           0 :                 break;
    6276             :             case PDFWriter::FitPageBoundingBoxVertical:
    6277           0 :                 aLine.append( "/FitBV " );
    6278           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6279           0 :                 break;
    6280             :             }
    6281           0 :             aLine.append( "]\n" );
    6282           0 :         }
    6283             : //close
    6284             : 
    6285           0 :         aLine.append( ">>\nendobj\n\n" );
    6286           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6287           0 :             nObject = 0;
    6288             :     }
    6289             :     else
    6290           0 :         nObject = 0;
    6291             : 
    6292           0 :     return nObject;
    6293             : }
    6294             : //<--- i56629
    6295             : 
    6296             : //--->i59651
    6297             : // emits the output intent dictionary
    6298             : 
    6299           0 : sal_Int32 PDFWriterImpl::emitOutputIntent()
    6300             : {
    6301           0 :     if( !m_bIsPDF_A1 )
    6302           0 :         return 0;
    6303             : 
    6304             : //emit the sRGB standard profile, in ICC format, in a stream, per IEC61966-2.1
    6305             : 
    6306           0 :     OStringBuffer aLine( 1024 );
    6307           0 :     sal_Int32 nICCObject = createObject();
    6308           0 :     sal_Int32 nStreamLengthObject = createObject();
    6309             : 
    6310           0 :     aLine.append( nICCObject );
    6311             : // sRGB has 3 colors, hence /N 3 below (PDF 1.4 table 4.16)
    6312           0 :     aLine.append( " 0 obj\n<</N 3/Length " );
    6313           0 :     aLine.append( nStreamLengthObject );
    6314           0 :     aLine.append( " 0 R" );
    6315             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    6316           0 :     aLine.append( "/Filter/FlateDecode" );
    6317             : #endif
    6318           0 :     aLine.append( ">>\nstream\n" );
    6319           0 :     CHECK_RETURN( updateObject( nICCObject ) );
    6320           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6321             : //get file position
    6322           0 :     sal_uInt64 nBeginStreamPos = 0;
    6323           0 :     osl_getFilePos( m_aFile, &nBeginStreamPos );
    6324           0 :     beginCompression();
    6325           0 :     checkAndEnableStreamEncryption( nICCObject );
    6326           0 :     cmsHPROFILE hProfile = cmsCreate_sRGBProfile();
    6327             : //force ICC profile version 2.1
    6328           0 :     cmsSetProfileVersion(hProfile, 2.1);
    6329           0 :     cmsUInt32Number nBytesNeeded = 0;
    6330           0 :     cmsSaveProfileToMem(hProfile, NULL, &nBytesNeeded);
    6331           0 :     if (!nBytesNeeded)
    6332           0 :       return 0;
    6333           0 :     std::vector<unsigned char> xBuffer(nBytesNeeded);
    6334           0 :     cmsSaveProfileToMem(hProfile, &xBuffer[0], &nBytesNeeded);
    6335           0 :     cmsCloseProfile(hProfile);
    6336           0 :     sal_Int32 nStreamSize = writeBuffer( &xBuffer[0], (sal_Int32) xBuffer.size() );
    6337           0 :     disableStreamEncryption();
    6338           0 :     endCompression();
    6339           0 :     sal_uInt64 nEndStreamPos = 0;
    6340           0 :     osl_getFilePos( m_aFile, &nEndStreamPos );
    6341             : 
    6342           0 :     if( nStreamSize == 0 )
    6343           0 :         return 0;
    6344           0 :     if( ! writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    6345           0 :         return 0 ;
    6346           0 :     aLine.setLength( 0 );
    6347             : 
    6348             : //emit the stream length   object
    6349           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
    6350           0 :     aLine.setLength( 0 );
    6351           0 :     aLine.append( nStreamLengthObject );
    6352           0 :     aLine.append( " 0 obj\n" );
    6353           0 :     aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos) );
    6354           0 :     aLine.append( "\nendobj\n\n" );
    6355           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6356           0 :     aLine.setLength( 0 );
    6357             : 
    6358             : //emit the OutputIntent dictionary
    6359           0 :     sal_Int32 nOIObject = createObject();
    6360           0 :     CHECK_RETURN( updateObject( nOIObject ) );
    6361           0 :     aLine.append( nOIObject );
    6362             :     aLine.append( " 0 obj\n"
    6363           0 :                   "<</Type/OutputIntent/S/GTS_PDFA1/OutputConditionIdentifier");
    6364             : 
    6365           0 :     OUString aComment( "sRGB IEC61966-2.1"  );
    6366           0 :     appendLiteralStringEncrypt( aComment ,nOIObject, aLine );
    6367           0 :     aLine.append("/DestOutputProfile ");
    6368           0 :     aLine.append( nICCObject );
    6369           0 :     aLine.append( " 0 R>>\nendobj\n\n" );;
    6370           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6371             : 
    6372           0 :     return nOIObject;
    6373             : }
    6374             : 
    6375             : // formats the string for the XML stream
    6376           0 : static void escapeStringXML( const OUString& rStr, OUString &rValue)
    6377             : {
    6378           0 :     const sal_Unicode* pUni = rStr.getStr();
    6379           0 :     int nLen = rStr.getLength();
    6380           0 :     for( ; nLen; nLen--, pUni++ )
    6381             :     {
    6382           0 :         switch( *pUni )
    6383             :         {
    6384             :         case sal_Unicode('&'):
    6385           0 :             rValue += OUString( "&amp;"  );
    6386           0 :         break;
    6387             :         case sal_Unicode('<'):
    6388           0 :             rValue += OUString( "&lt;"  );
    6389           0 :         break;
    6390             :         case sal_Unicode('>'):
    6391           0 :             rValue += OUString( "&gt;"  );
    6392           0 :         break;
    6393             :         case sal_Unicode('\''):
    6394           0 :             rValue += OUString( "&apos;"  );
    6395           0 :         break;
    6396             :         case sal_Unicode('"'):
    6397           0 :             rValue += OUString( "&quot;"  );
    6398           0 :         break;
    6399             :         default:
    6400           0 :             rValue += OUString( *pUni );
    6401           0 :             break;
    6402             :         }
    6403             :     }
    6404           0 : }
    6405             : 
    6406             : // emits the document metadata
    6407             : //
    6408           0 : sal_Int32 PDFWriterImpl::emitDocumentMetadata()
    6409             : {
    6410           0 :     if( !m_bIsPDF_A1 )
    6411           0 :         return 0;
    6412             : 
    6413             :     //get the object number for all the destinations
    6414           0 :     sal_Int32 nObject = createObject();
    6415             : 
    6416           0 :     if( updateObject( nObject ) )
    6417             :     {
    6418             : // the following string are written in UTF-8 unicode
    6419           0 :         OStringBuffer aMetadataStream( 8192 );
    6420             : 
    6421           0 :         aMetadataStream.append( "<?xpacket begin=\"" );
    6422             : // this lines writes Unicode “zero width non-breaking space character” (U+FEFF) (aka byte-order mark ) used
    6423             : // as a byte-order marker.
    6424           0 :         aMetadataStream.append( OUStringToOString( OUString( sal_Unicode( 0xFEFF ) ), RTL_TEXTENCODING_UTF8 ) );
    6425           0 :         aMetadataStream.append( "\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" );
    6426           0 :         aMetadataStream.append( "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n" );
    6427           0 :         aMetadataStream.append( " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" );
    6428             : //PDF/A part ( ISO 19005-1:2005 - 6.7.11 )
    6429           0 :         aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6430           0 :         aMetadataStream.append( "      xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n" );
    6431           0 :         aMetadataStream.append( "   <pdfaid:part>1</pdfaid:part>\n" );
    6432           0 :         aMetadataStream.append( "   <pdfaid:conformance>A</pdfaid:conformance>\n" );
    6433           0 :         aMetadataStream.append( "  </rdf:Description>\n" );
    6434             : //... Dublin Core properties go here
    6435           0 :         if( m_aContext.DocumentInfo.Title.Len() ||
    6436           0 :             m_aContext.DocumentInfo.Author.Len() ||
    6437           0 :             m_aContext.DocumentInfo.Subject.Len() )
    6438             :         {
    6439           0 :             aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6440           0 :             aMetadataStream.append( "      xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" );
    6441           0 :             if( m_aContext.DocumentInfo.Title.Len() )
    6442             :             {
    6443             : // this is according to PDF/A-1, technical corrigendum 1 (2007-04-01)
    6444           0 :                 aMetadataStream.append( "   <dc:title>\n" );
    6445           0 :                 aMetadataStream.append( "    <rdf:Alt>\n" );
    6446           0 :                 aMetadataStream.append( "     <rdf:li xml:lang=\"x-default\">" );
    6447           0 :                 OUString aTitle;
    6448           0 :                 escapeStringXML( m_aContext.DocumentInfo.Title, aTitle );
    6449           0 :                 aMetadataStream.append( OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 )  );
    6450           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6451           0 :                 aMetadataStream.append( "    </rdf:Alt>\n" );
    6452           0 :                 aMetadataStream.append( "   </dc:title>\n" );
    6453             :             }
    6454           0 :             if( m_aContext.DocumentInfo.Author.Len() )
    6455             :             {
    6456           0 :                 aMetadataStream.append( "   <dc:creator>\n" );
    6457           0 :                 aMetadataStream.append( "    <rdf:Seq>\n" );
    6458           0 :                 aMetadataStream.append( "     <rdf:li>" );
    6459           0 :                 OUString aAuthor;
    6460           0 :                 escapeStringXML( m_aContext.DocumentInfo.Author, aAuthor );
    6461           0 :                 aMetadataStream.append( OUStringToOString( aAuthor , RTL_TEXTENCODING_UTF8 )  );
    6462           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6463           0 :                 aMetadataStream.append( "    </rdf:Seq>\n" );
    6464           0 :                 aMetadataStream.append( "   </dc:creator>\n" );
    6465             :             }
    6466           0 :             if( m_aContext.DocumentInfo.Subject.Len() )
    6467             :             {
    6468             : // this is according to PDF/A-1, technical corrigendum 1 (2007-04-01)
    6469           0 :                 aMetadataStream.append( "   <dc:description>\n" );
    6470           0 :                 aMetadataStream.append( "    <rdf:Alt>\n" );
    6471           0 :                 aMetadataStream.append( "     <rdf:li xml:lang=\"x-default\">" );
    6472           0 :                 OUString aSubject;
    6473           0 :                 escapeStringXML( m_aContext.DocumentInfo.Subject, aSubject );
    6474           0 :                 aMetadataStream.append( OUStringToOString( aSubject , RTL_TEXTENCODING_UTF8 )  );
    6475           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6476           0 :                 aMetadataStream.append( "    </rdf:Alt>\n" );
    6477           0 :                 aMetadataStream.append( "   </dc:description>\n" );
    6478             :             }
    6479           0 :             aMetadataStream.append( "  </rdf:Description>\n" );
    6480             :         }
    6481             : 
    6482             : //... PDF properties go here
    6483           0 :         if( m_aContext.DocumentInfo.Producer.Len() ||
    6484           0 :             m_aContext.DocumentInfo.Keywords.Len() )
    6485             :         {
    6486           0 :             aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6487           0 :             aMetadataStream.append( "     xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n" );
    6488           0 :             if( m_aContext.DocumentInfo.Producer.Len() )
    6489             :             {
    6490           0 :                 aMetadataStream.append( "   <pdf:Producer>" );
    6491           0 :                 OUString aProducer;
    6492           0 :                 escapeStringXML( m_aContext.DocumentInfo.Producer, aProducer );
    6493           0 :                 aMetadataStream.append( OUStringToOString( aProducer , RTL_TEXTENCODING_UTF8 )  );
    6494           0 :                 aMetadataStream.append( "</pdf:Producer>\n" );
    6495             :             }
    6496           0 :             if( m_aContext.DocumentInfo.Keywords.Len() )
    6497             :             {
    6498           0 :                 aMetadataStream.append( "   <pdf:Keywords>" );
    6499           0 :                 OUString aKeywords;
    6500           0 :                 escapeStringXML( m_aContext.DocumentInfo.Keywords, aKeywords );
    6501           0 :                 aMetadataStream.append( OUStringToOString( aKeywords , RTL_TEXTENCODING_UTF8 )  );
    6502           0 :                 aMetadataStream.append( "</pdf:Keywords>\n" );
    6503             :             }
    6504           0 :             aMetadataStream.append( "  </rdf:Description>\n" );
    6505             :         }
    6506             : 
    6507           0 :         aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6508           0 :         aMetadataStream.append( "    xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\">\n" );
    6509           0 :         if( m_aContext.DocumentInfo.Creator.Len() )
    6510             :         {
    6511           0 :             aMetadataStream.append( "   <xmp:CreatorTool>" );
    6512           0 :             OUString aCreator;
    6513           0 :             escapeStringXML( m_aContext.DocumentInfo.Creator, aCreator );
    6514           0 :             aMetadataStream.append( OUStringToOString( aCreator , RTL_TEXTENCODING_UTF8 )  );
    6515           0 :             aMetadataStream.append( "</xmp:CreatorTool>\n" );
    6516             :         }
    6517             : //creation date
    6518           0 :         aMetadataStream.append( "   <xmp:CreateDate>" );
    6519           0 :         aMetadataStream.append( m_aCreationMetaDateString );
    6520           0 :         aMetadataStream.append( "</xmp:CreateDate>\n" );
    6521             : 
    6522           0 :         aMetadataStream.append( "  </rdf:Description>\n" );
    6523           0 :         aMetadataStream.append( " </rdf:RDF>\n" );
    6524           0 :         aMetadataStream.append( "</x:xmpmeta>\n" );
    6525             : 
    6526             : //add the padding
    6527           0 :         for( sal_Int32 nSpaces = 1; nSpaces <= 2100; nSpaces++ )
    6528             :         {
    6529           0 :             aMetadataStream.append( " " );
    6530           0 :             if( nSpaces % 100 == 0 )
    6531           0 :                 aMetadataStream.append( "\n" );
    6532             :         }
    6533             : 
    6534           0 :         aMetadataStream.append( "<?xpacket end=\"w\"?>\n" );
    6535             : 
    6536           0 :         OStringBuffer aMetadataObj( 1024 );
    6537             : 
    6538           0 :         aMetadataObj.append( nObject );
    6539           0 :         aMetadataObj.append( " 0 obj\n" );
    6540             : 
    6541           0 :         aMetadataObj.append( "<</Type/Metadata/Subtype/XML/Length " );
    6542             : 
    6543           0 :         aMetadataObj.append( (sal_Int32) aMetadataStream.getLength() );
    6544           0 :         aMetadataObj.append( ">>\nstream\n" );
    6545           0 :         CHECK_RETURN( writeBuffer( aMetadataObj.getStr(), aMetadataObj.getLength() ) );
    6546             : //emit the stream
    6547           0 :         CHECK_RETURN( writeBuffer( aMetadataStream.getStr(), aMetadataStream.getLength() ) );
    6548             : 
    6549           0 :         aMetadataObj.setLength( 0 );
    6550           0 :         aMetadataObj.append( "\nendstream\nendobj\n\n" );
    6551           0 :         if( ! writeBuffer( aMetadataObj.getStr(), aMetadataObj.getLength() ) )
    6552           0 :             nObject = 0;
    6553             :     }
    6554             :     else
    6555           0 :         nObject = 0;
    6556             : 
    6557           0 :     return nObject;
    6558             : }
    6559             : //<---i59651
    6560             : 
    6561           0 : bool PDFWriterImpl::emitTrailer()
    6562             : {
    6563             :     // emit doc info
    6564           0 :     sal_Int32 nDocInfoObject = emitInfoDict( );
    6565             : 
    6566           0 :     sal_Int32 nSecObject = 0;
    6567             : 
    6568           0 :     if( m_aContext.Encryption.Encrypt() )
    6569             :     {
    6570             : //emit the security information
    6571             : //must be emitted as indirect dictionary object, since
    6572             : //Acrobat Reader 5 works only with this kind of implementation
    6573           0 :         nSecObject = createObject();
    6574             : 
    6575           0 :         if( updateObject( nSecObject ) )
    6576             :         {
    6577           0 :             OStringBuffer aLineS( 1024 );
    6578           0 :             aLineS.append( nSecObject );
    6579             :             aLineS.append( " 0 obj\n"
    6580           0 :                            "<</Filter/Standard/V " );
    6581             :             // check the version
    6582           0 :             if( m_aContext.Encryption.Security128bit )
    6583           0 :                 aLineS.append( "2/Length 128/R 3" );
    6584             :             else
    6585           0 :                 aLineS.append( "1/R 2" );
    6586             : 
    6587             :             // emit the owner password, must not be encrypted
    6588           0 :             aLineS.append( "/O(" );
    6589           0 :             appendLiteralString( (const sal_Char*)&m_aContext.Encryption.OValue[0], sal_Int32(m_aContext.Encryption.OValue.size()), aLineS );
    6590           0 :             aLineS.append( ")/U(" );
    6591           0 :             appendLiteralString( (const sal_Char*)&m_aContext.Encryption.UValue[0], sal_Int32(m_aContext.Encryption.UValue.size()), aLineS );
    6592           0 :             aLineS.append( ")/P " );// the permission set
    6593           0 :             aLineS.append( m_nAccessPermissions );
    6594           0 :             aLineS.append( ">>\nendobj\n\n" );
    6595           0 :             if( !writeBuffer( aLineS.getStr(), aLineS.getLength() ) )
    6596           0 :                 nSecObject = 0;
    6597             :         }
    6598             :         else
    6599           0 :             nSecObject = 0;
    6600             :     }
    6601             :     // emit xref table
    6602             :     // remember start
    6603           0 :     sal_uInt64 nXRefOffset = 0;
    6604           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nXRefOffset )) );
    6605           0 :     CHECK_RETURN( writeBuffer( "xref\n", 5 ) );
    6606             : 
    6607           0 :     sal_Int32 nObjects = m_aObjects.size();
    6608           0 :     OStringBuffer aLine;
    6609           0 :     aLine.append( "0 " );
    6610           0 :     aLine.append( (sal_Int32)(nObjects+1) );
    6611           0 :     aLine.append( "\n" );
    6612           0 :     aLine.append( "0000000000 65535 f \n" );
    6613           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6614             : 
    6615           0 :     for( sal_Int32 i = 0; i < nObjects; i++ )
    6616             :     {
    6617           0 :         aLine.setLength( 0 );
    6618           0 :         OString aOffset = OString::valueOf( (sal_Int64)m_aObjects[i] );
    6619           0 :         for( sal_Int32 j = 0; j < (10-aOffset.getLength()); j++ )
    6620           0 :             aLine.append( '0' );
    6621           0 :         aLine.append( aOffset );
    6622           0 :         aLine.append( " 00000 n \n" );
    6623             :         DBG_ASSERT( aLine.getLength() == 20, "invalid xref entry" );
    6624           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6625           0 :     }
    6626             : 
    6627             :     // prepare document checksum
    6628           0 :     OStringBuffer aDocChecksum( 2*RTL_DIGEST_LENGTH_MD5+1 );
    6629           0 :     if( m_aDocDigest )
    6630             :     {
    6631             :         sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
    6632           0 :         rtl_digest_getMD5( m_aDocDigest, nMD5Sum, sizeof(nMD5Sum) );
    6633           0 :         for( unsigned int i = 0; i < RTL_DIGEST_LENGTH_MD5; i++ )
    6634           0 :             appendHex( nMD5Sum[i], aDocChecksum );
    6635             :     }
    6636             :     // document id set in setDocInfo method
    6637             :     // emit trailer
    6638           0 :     aLine.setLength( 0 );
    6639             :     aLine.append( "trailer\n"
    6640           0 :                   "<</Size " );
    6641           0 :     aLine.append( (sal_Int32)(nObjects+1) );
    6642           0 :     aLine.append( "/Root " );
    6643           0 :     aLine.append( m_nCatalogObject );
    6644           0 :     aLine.append( " 0 R\n" );
    6645           0 :     if( nSecObject )
    6646             :     {
    6647           0 :         aLine.append( "/Encrypt ");
    6648           0 :         aLine.append( nSecObject );
    6649           0 :         aLine.append( " 0 R\n" );
    6650             :     }
    6651           0 :     if( nDocInfoObject )
    6652             :     {
    6653           0 :         aLine.append( "/Info " );
    6654           0 :         aLine.append( nDocInfoObject );
    6655           0 :         aLine.append( " 0 R\n" );
    6656             :     }
    6657           0 :     if( ! m_aContext.Encryption.DocumentIdentifier.empty() )
    6658             :     {
    6659           0 :         aLine.append( "/ID [ <" );
    6660           0 :         for( std::vector< sal_uInt8 >::const_iterator it = m_aContext.Encryption.DocumentIdentifier.begin();
    6661           0 :              it != m_aContext.Encryption.DocumentIdentifier.end(); ++it )
    6662             :         {
    6663           0 :             appendHex( sal_Int8(*it), aLine );
    6664             :         }
    6665             :         aLine.append( ">\n"
    6666           0 :                       "<" );
    6667           0 :         for( std::vector< sal_uInt8 >::const_iterator it = m_aContext.Encryption.DocumentIdentifier.begin();
    6668           0 :              it != m_aContext.Encryption.DocumentIdentifier.end(); ++it )
    6669             :         {
    6670           0 :             appendHex( sal_Int8(*it), aLine );
    6671             :         }
    6672           0 :         aLine.append( "> ]\n" );
    6673             :     }
    6674           0 :     if( !aDocChecksum.isEmpty() )
    6675             :     {
    6676           0 :         aLine.append( "/DocChecksum /" );
    6677           0 :         aLine.append( aDocChecksum.makeStringAndClear() );
    6678           0 :         aLine.append( "\n" );
    6679             :     }
    6680           0 :     if( m_aAdditionalStreams.size() > 0 )
    6681             :     {
    6682           0 :         aLine.append( "/AdditionalStreams [" );
    6683           0 :         for( unsigned int i = 0; i < m_aAdditionalStreams.size(); i++ )
    6684             :         {
    6685           0 :             aLine.append( "/" );
    6686           0 :             appendName( m_aAdditionalStreams[i].m_aMimeType, aLine );
    6687           0 :             aLine.append( " " );
    6688           0 :             aLine.append( m_aAdditionalStreams[i].m_nStreamObject );
    6689           0 :             aLine.append( " 0 R\n" );
    6690             :         }
    6691           0 :         aLine.append( "]\n" );
    6692             :     }
    6693             :     aLine.append( ">>\n"
    6694           0 :                   "startxref\n" );
    6695           0 :     aLine.append( (sal_Int64)nXRefOffset );
    6696             :     aLine.append( "\n"
    6697           0 :                   "%%EOF\n" );
    6698           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6699             : 
    6700           0 :     return true;
    6701             : }
    6702             : 
    6703             : struct AnnotationSortEntry
    6704             : {
    6705             :     sal_Int32 nTabOrder;
    6706             :     sal_Int32 nObject;
    6707             :     sal_Int32 nWidgetIndex;
    6708             : 
    6709           0 :     AnnotationSortEntry( sal_Int32 nTab, sal_Int32 nObj, sal_Int32 nI ) :
    6710             :         nTabOrder( nTab ),
    6711             :         nObject( nObj ),
    6712           0 :         nWidgetIndex( nI )
    6713           0 :     {}
    6714             : };
    6715             : 
    6716           0 : struct AnnotSortContainer
    6717             : {
    6718             :     std::set< sal_Int32 >               aObjects;
    6719             :     std::vector< AnnotationSortEntry >    aSortedAnnots;
    6720             : };
    6721             : 
    6722             : struct AnnotSorterLess
    6723             : {
    6724             :     std::vector< PDFWriterImpl::PDFWidget >& m_rWidgets;
    6725             : 
    6726           0 :     AnnotSorterLess( std::vector< PDFWriterImpl::PDFWidget >& rWidgets ) : m_rWidgets( rWidgets ) {}
    6727             : 
    6728           0 :     bool operator()( const AnnotationSortEntry& rLeft, const AnnotationSortEntry& rRight )
    6729             :     {
    6730           0 :         if( rLeft.nTabOrder < rRight.nTabOrder )
    6731           0 :             return true;
    6732           0 :         if( rRight.nTabOrder < rLeft.nTabOrder )
    6733           0 :             return false;
    6734           0 :         if( rLeft.nWidgetIndex < 0 && rRight.nWidgetIndex < 0 )
    6735           0 :             return false;
    6736           0 :         if( rRight.nWidgetIndex < 0 )
    6737           0 :             return true;
    6738           0 :         if( rLeft.nWidgetIndex < 0 )
    6739           0 :             return false;
    6740             :         // remember: widget rects are in PDF coordinates, so they are ordered down up
    6741           0 :         if( m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Top() >
    6742           0 :             m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Top() )
    6743           0 :             return true;
    6744           0 :         if( m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Top() >
    6745           0 :             m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Top() )
    6746           0 :             return false;
    6747           0 :         if( m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Left() <
    6748           0 :             m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Left() )
    6749           0 :             return true;
    6750           0 :         return false;
    6751             :     }
    6752             : };
    6753             : 
    6754           0 : void PDFWriterImpl::sortWidgets()
    6755             : {
    6756             :     // sort widget annotations on each page as per their
    6757             :     // TabOrder attribute
    6758           0 :     boost::unordered_map< sal_Int32, AnnotSortContainer > sorted;
    6759           0 :     int nWidgets = m_aWidgets.size();
    6760           0 :     for( int nW = 0; nW < nWidgets; nW++ )
    6761             :     {
    6762           0 :         const PDFWidget& rWidget = m_aWidgets[nW];
    6763           0 :         if( rWidget.m_nPage >= 0 )
    6764             :         {
    6765           0 :             AnnotSortContainer& rCont = sorted[ rWidget.m_nPage ];
    6766             :             // optimize vector allocation
    6767           0 :             if( rCont.aSortedAnnots.empty() )
    6768           0 :                 rCont.aSortedAnnots.reserve( m_aPages[ rWidget.m_nPage ].m_aAnnotations.size() );
    6769             :             // insert widget to tab sorter
    6770             :             // RadioButtons are not page annotations, only their individual check boxes are
    6771           0 :             if( rWidget.m_eType != PDFWriter::RadioButton )
    6772             :             {
    6773           0 :                 rCont.aObjects.insert( rWidget.m_nObject );
    6774           0 :                 rCont.aSortedAnnots.push_back( AnnotationSortEntry( rWidget.m_nTabOrder, rWidget.m_nObject, nW ) );
    6775             :             }
    6776             :         }
    6777             :     }
    6778           0 :     for( boost::unordered_map< sal_Int32, AnnotSortContainer >::iterator it = sorted.begin(); it != sorted.end(); ++it )
    6779             :     {
    6780             :         // append entries for non widget annotations
    6781           0 :         PDFPage& rPage = m_aPages[ it->first ];
    6782           0 :         unsigned int nAnnots = rPage.m_aAnnotations.size();
    6783           0 :         for( unsigned int nA = 0; nA < nAnnots; nA++ )
    6784           0 :             if( it->second.aObjects.find( rPage.m_aAnnotations[nA] ) == it->second.aObjects.end())
    6785           0 :                 it->second.aSortedAnnots.push_back( AnnotationSortEntry( 10000, rPage.m_aAnnotations[nA], -1 ) );
    6786             : 
    6787           0 :         AnnotSorterLess aLess( m_aWidgets );
    6788           0 :         std::stable_sort( it->second.aSortedAnnots.begin(), it->second.aSortedAnnots.end(), aLess );
    6789             :         // sanity check
    6790           0 :         if( it->second.aSortedAnnots.size() == nAnnots)
    6791             :         {
    6792           0 :             for( unsigned int nA = 0; nA < nAnnots; nA++ )
    6793           0 :                 rPage.m_aAnnotations[nA] = it->second.aSortedAnnots[nA].nObject;
    6794             :         }
    6795             :         else
    6796             :         {
    6797             :             DBG_ASSERT( 0, "wrong number of sorted annotations" );
    6798             :             #if OSL_DEBUG_LEVEL > 0
    6799             :             fprintf( stderr, "PDFWriterImpl::sortWidgets(): wrong number of sorted assertions on page nr %ld\n"
    6800             :                      "    %ld sorted and %ld unsorted\n", (long int)it->first, (long int)it->second.aSortedAnnots.size(), (long int)nAnnots );
    6801             :             #endif
    6802             :         }
    6803           0 :     }
    6804             : 
    6805             :     // FIXME: implement tab order in structure tree for PDF 1.5
    6806           0 : }
    6807             : 
    6808             : namespace vcl {
    6809             : class PDFStreamIf :
    6810             :         public cppu::WeakImplHelper1< com::sun::star::io::XOutputStream >
    6811             : {
    6812             :     PDFWriterImpl*  m_pWriter;
    6813             :     bool            m_bWrite;
    6814             :     public:
    6815           0 :     PDFStreamIf( PDFWriterImpl* pWriter ) : m_pWriter( pWriter ), m_bWrite( true ) {}
    6816             :     virtual ~PDFStreamIf();
    6817             : 
    6818             :     virtual void SAL_CALL writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw();
    6819             :     virtual void SAL_CALL flush() throw();
    6820             :     virtual void SAL_CALL closeOutput() throw();
    6821             : };
    6822             : }
    6823             : 
    6824           0 : PDFStreamIf::~PDFStreamIf()
    6825             : {
    6826           0 : }
    6827             : 
    6828           0 : void SAL_CALL  PDFStreamIf::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw()
    6829             : {
    6830           0 :     if( m_bWrite && aData.getLength() )
    6831             :     {
    6832           0 :         sal_Int32 nBytes = aData.getLength();
    6833           0 :         m_pWriter->writeBuffer( aData.getConstArray(), nBytes );
    6834             :     }
    6835           0 : }
    6836             : 
    6837           0 : void SAL_CALL PDFStreamIf::flush() throw()
    6838             : {
    6839           0 : }
    6840             : 
    6841           0 : void SAL_CALL PDFStreamIf::closeOutput() throw()
    6842             : {
    6843           0 :     m_bWrite = false;
    6844           0 : }
    6845             : 
    6846           0 : bool PDFWriterImpl::emitAdditionalStreams()
    6847             : {
    6848           0 :     unsigned int nStreams = m_aAdditionalStreams.size();
    6849           0 :     for( unsigned int i = 0; i < nStreams; i++ )
    6850             :     {
    6851           0 :         PDFAddStream& rStream = m_aAdditionalStreams[i];
    6852           0 :         rStream.m_nStreamObject = createObject();
    6853           0 :         sal_Int32 nSizeObject = createObject();
    6854             : 
    6855           0 :         if( ! updateObject( rStream.m_nStreamObject ) )
    6856           0 :             return false;
    6857             : 
    6858           0 :         OStringBuffer aLine;
    6859           0 :         aLine.append( rStream.m_nStreamObject );
    6860           0 :         aLine.append( " 0 obj\n<</Length " );
    6861           0 :         aLine.append( nSizeObject );
    6862           0 :         aLine.append( " 0 R" );
    6863           0 :         if( rStream.m_bCompress )
    6864           0 :             aLine.append( "/Filter/FlateDecode" );
    6865           0 :         aLine.append( ">>\nstream\n" );
    6866           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6867           0 :             return false;
    6868           0 :         sal_uInt64 nBeginStreamPos = 0, nEndStreamPos = 0;
    6869           0 :         if( osl_File_E_None != osl_getFilePos( m_aFile, &nBeginStreamPos ) )
    6870             :         {
    6871           0 :             osl_closeFile( m_aFile );
    6872           0 :             m_bOpen = false;
    6873             :         }
    6874           0 :         if( rStream.m_bCompress )
    6875           0 :             beginCompression();
    6876             : 
    6877           0 :         checkAndEnableStreamEncryption( rStream.m_nStreamObject );
    6878           0 :         com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > xStream( new PDFStreamIf( this ) );
    6879           0 :         rStream.m_pStream->write( xStream );
    6880           0 :         xStream.clear();
    6881           0 :         delete rStream.m_pStream;
    6882           0 :         rStream.m_pStream = NULL;
    6883           0 :         disableStreamEncryption();
    6884             : 
    6885           0 :         if( rStream.m_bCompress )
    6886           0 :             endCompression();
    6887             : 
    6888           0 :         if( osl_File_E_None != osl_getFilePos( m_aFile, &nEndStreamPos ) )
    6889             :         {
    6890           0 :             osl_closeFile( m_aFile );
    6891           0 :             m_bOpen = false;
    6892           0 :             return false;
    6893             :         }
    6894           0 :         if( ! writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    6895           0 :             return false ;
    6896             :         // emit stream length object
    6897           0 :         if( ! updateObject( nSizeObject ) )
    6898           0 :             return false;
    6899           0 :         aLine.setLength( 0 );
    6900           0 :         aLine.append( nSizeObject );
    6901           0 :         aLine.append( " 0 obj\n" );
    6902           0 :         aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos) );
    6903           0 :         aLine.append( "\nendobj\n\n" );
    6904           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6905           0 :             return false;
    6906           0 :     }
    6907           0 :     return true;
    6908             : }
    6909             : 
    6910           0 : bool PDFWriterImpl::emit()
    6911             : {
    6912           0 :     endPage();
    6913             : 
    6914             :     // resort structure tree and annotations if necessary
    6915             :     // needed for widget tab order
    6916           0 :     sortWidgets();
    6917             : 
    6918             : #if !defined(ANDROID) && !defined(IOS)
    6919           0 :     if( m_aContext.SignPDF )
    6920             :     {
    6921             :         // sign the document
    6922           0 :         PDFWriter::SignatureWidget aSignature;
    6923           0 :         aSignature.Name = OUString("Signature1");
    6924           0 :         createControl( aSignature, 0 );
    6925             :     }
    6926             : #endif
    6927             : 
    6928             :     // emit additional streams
    6929           0 :     CHECK_RETURN( emitAdditionalStreams() );
    6930             : 
    6931             :     // emit catalog
    6932           0 :     CHECK_RETURN( emitCatalog() );
    6933             : 
    6934             : #if !defined(ANDROID) && !defined(IOS)
    6935           0 :     if (m_nSignatureObject != -1) // if document is signed, emit sigdict
    6936           0 :         CHECK_RETURN( emitSignature() );
    6937             : #endif
    6938             : 
    6939             :     // emit trailer
    6940           0 :     CHECK_RETURN( emitTrailer() );
    6941             : 
    6942             : #if !defined(ANDROID) && !defined(IOS)
    6943           0 :     if (m_nSignatureObject != -1) // finalize the signature
    6944           0 :         CHECK_RETURN( finalizeSignature() );
    6945             : #endif
    6946             : 
    6947           0 :     osl_closeFile( m_aFile );
    6948           0 :     m_bOpen = false;
    6949             : 
    6950           0 :     return true;
    6951             : }
    6952             : 
    6953           0 : std::set< PDFWriter::ErrorCode > PDFWriterImpl::getErrors()
    6954             : {
    6955           0 :     return m_aErrors;
    6956             : }
    6957             : 
    6958           0 : sal_Int32 PDFWriterImpl::getSystemFont( const Font& i_rFont )
    6959             : {
    6960           0 :     getReferenceDevice()->Push();
    6961           0 :     getReferenceDevice()->SetFont( i_rFont );
    6962           0 :     getReferenceDevice()->ImplNewFont();
    6963             : 
    6964           0 :     const PhysicalFontFace* pDevFont = m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData;
    6965           0 :     sal_Int32 nFontID = 0;
    6966           0 :     FontEmbedData::iterator it = m_aSystemFonts.find( pDevFont );
    6967           0 :     if( it != m_aSystemFonts.end() )
    6968           0 :         nFontID = it->second.m_nNormalFontID;
    6969             :     else
    6970             :     {
    6971           0 :         nFontID = m_nNextFID++;
    6972           0 :         m_aSystemFonts[ pDevFont ] = EmbedFont();
    6973           0 :         m_aSystemFonts[ pDevFont ].m_nNormalFontID = nFontID;
    6974             :     }
    6975             : 
    6976           0 :     getReferenceDevice()->Pop();
    6977           0 :     getReferenceDevice()->ImplNewFont();
    6978             : 
    6979           0 :     return nFontID;
    6980             : }
    6981             : 
    6982           0 : void PDFWriterImpl::registerGlyphs( int nGlyphs,
    6983             :                                     sal_GlyphId* pGlyphs,
    6984             :                                     sal_Int32* pGlyphWidths,
    6985             :                                     sal_Ucs* pUnicodes,
    6986             :                                     sal_Int32* pUnicodesPerGlyph,
    6987             :                                     sal_uInt8* pMappedGlyphs,
    6988             :                                     sal_Int32* pMappedFontObjects,
    6989             :                                     const PhysicalFontFace* pFallbackFonts[] )
    6990             : {
    6991           0 :     const PhysicalFontFace* pDevFont = m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData;
    6992           0 :     sal_Ucs* pCurUnicode = pUnicodes;
    6993           0 :     for( int i = 0; i < nGlyphs; pCurUnicode += pUnicodesPerGlyph[i] , i++ )
    6994             :     {
    6995           0 :         const int nFontGlyphId = pGlyphs[i] & (GF_IDXMASK | GF_ISCHAR | GF_GSUB);
    6996           0 :         const PhysicalFontFace* pCurrentFont = pFallbackFonts[i] ? pFallbackFonts[i] : pDevFont;
    6997             : 
    6998           0 :         if( pCurrentFont->mbSubsettable )
    6999             :         {
    7000           0 :             FontSubset& rSubset = m_aSubsets[ pCurrentFont ];
    7001             :             // search for font specific glyphID
    7002           0 :             FontMapping::iterator it = rSubset.m_aMapping.find( nFontGlyphId );
    7003           0 :             if( it != rSubset.m_aMapping.end() )
    7004             :             {
    7005           0 :                 pMappedFontObjects[i] = it->second.m_nFontID;
    7006           0 :                 pMappedGlyphs[i] = it->second.m_nSubsetGlyphID;
    7007             :             }
    7008             :             else
    7009             :             {
    7010             :                 // create new subset if necessary
    7011           0 :                 if( rSubset.m_aSubsets.empty()
    7012           0 :                 || (rSubset.m_aSubsets.back().m_aMapping.size() > 254) )
    7013             :                 {
    7014           0 :                     rSubset.m_aSubsets.push_back( FontEmit( m_nNextFID++ ) );
    7015             :                 }
    7016             : 
    7017             :                 // copy font id
    7018           0 :                 pMappedFontObjects[i] = rSubset.m_aSubsets.back().m_nFontID;
    7019             :                 // create new glyph in subset
    7020           0 :                 sal_uInt8 nNewId = sal::static_int_cast<sal_uInt8>(rSubset.m_aSubsets.back().m_aMapping.size()+1);
    7021           0 :                 pMappedGlyphs[i] = nNewId;
    7022             : 
    7023             :                 // add new glyph to emitted font subset
    7024           0 :                 GlyphEmit& rNewGlyphEmit = rSubset.m_aSubsets.back().m_aMapping[ nFontGlyphId ];
    7025           0 :                 rNewGlyphEmit.setGlyphId( nNewId );
    7026           0 :                 for( sal_Int32 n = 0; n < pUnicodesPerGlyph[i]; n++ )
    7027           0 :                     rNewGlyphEmit.addCode( pCurUnicode[n] );
    7028             : 
    7029             :                 // add new glyph to font mapping
    7030           0 :                 Glyph& rNewGlyph = rSubset.m_aMapping[ nFontGlyphId ];
    7031           0 :                 rNewGlyph.m_nFontID = pMappedFontObjects[i];
    7032           0 :                 rNewGlyph.m_nSubsetGlyphID = nNewId;
    7033             :             }
    7034           0 :             getReferenceDevice()->ImplGetGraphics();
    7035           0 :             const bool bVertical = ((pGlyphs[i] & GF_ROTMASK) != 0);
    7036           0 :             pGlyphWidths[i] = m_aFontCache.getGlyphWidth( pCurrentFont,
    7037             :                                                           nFontGlyphId,
    7038             :                                                           bVertical,
    7039           0 :                                                           m_pReferenceDevice->mpGraphics );
    7040             :         }
    7041           0 :         else if( pCurrentFont->IsEmbeddable() )
    7042             :         {
    7043           0 :             sal_Int32 nFontID = 0;
    7044           0 :             FontEmbedData::iterator it = m_aEmbeddedFonts.find( pCurrentFont );
    7045           0 :             if( it != m_aEmbeddedFonts.end() )
    7046           0 :                 nFontID = it->second.m_nNormalFontID;
    7047             :             else
    7048             :             {
    7049           0 :                 nFontID = m_nNextFID++;
    7050           0 :                 m_aEmbeddedFonts[ pCurrentFont ] = EmbedFont();
    7051           0 :                 m_aEmbeddedFonts[ pCurrentFont ].m_nNormalFontID = nFontID;
    7052             :             }
    7053           0 :             EmbedFont& rEmbedFont = m_aEmbeddedFonts[pCurrentFont];
    7054             : 
    7055           0 :             const Ucs2SIntMap* pEncoding = NULL;
    7056           0 :             const Ucs2OStrMap* pNonEncoded = NULL;
    7057           0 :             getReferenceDevice()->ImplGetGraphics();
    7058           0 :             pEncoding = m_pReferenceDevice->mpGraphics->GetFontEncodingVector( pCurrentFont, &pNonEncoded );
    7059             : 
    7060           0 :             Ucs2SIntMap::const_iterator enc_it;
    7061           0 :             Ucs2OStrMap::const_iterator nonenc_it;
    7062             : 
    7063           0 :             sal_Int32 nCurFontID = nFontID;
    7064           0 :             sal_Ucs cChar = *pCurUnicode;
    7065           0 :             if( pEncoding )
    7066             :             {
    7067           0 :                 enc_it = pEncoding->find( cChar );
    7068           0 :                 if( enc_it != pEncoding->end() && enc_it->second > 0 )
    7069             :                 {
    7070             :                     DBG_ASSERT( (enc_it->second & 0xffffff00) == 0, "Invalid character code" );
    7071           0 :                     cChar = (sal_Ucs)enc_it->second;
    7072             :                 }
    7073           0 :                 else if( (enc_it == pEncoding->end() || enc_it->second == -1) &&
    7074           0 :                          pNonEncoded &&
    7075           0 :                          (nonenc_it = pNonEncoded->find( cChar )) != pNonEncoded->end() )
    7076             :                 {
    7077           0 :                     nCurFontID = 0;
    7078             :                     // find non encoded glyph
    7079           0 :                     for( std::list< EmbedEncoding >::iterator nec_it = rEmbedFont.m_aExtendedEncodings.begin(); nec_it != rEmbedFont.m_aExtendedEncodings.end(); ++nec_it )
    7080             :                     {
    7081           0 :                         if( nec_it->m_aCMap.find( cChar ) != nec_it->m_aCMap.end() )
    7082             :                         {
    7083           0 :                             nCurFontID = nec_it->m_nFontID;
    7084           0 :                             cChar = (sal_Ucs)nec_it->m_aCMap[ cChar ];
    7085           0 :                             break;
    7086             :                         }
    7087             :                     }
    7088           0 :                     if( nCurFontID == 0 ) // new nonencoded glyph
    7089             :                     {
    7090           0 :                         if( rEmbedFont.m_aExtendedEncodings.empty() || rEmbedFont.m_aExtendedEncodings.back().m_aEncVector.size() == 255 )
    7091             :                         {
    7092           0 :                             rEmbedFont.m_aExtendedEncodings.push_back( EmbedEncoding() );
    7093           0 :                             rEmbedFont.m_aExtendedEncodings.back().m_nFontID = m_nNextFID++;
    7094             :                         }
    7095           0 :                         EmbedEncoding& rEncoding = rEmbedFont.m_aExtendedEncodings.back();
    7096           0 :                         rEncoding.m_aEncVector.push_back( EmbedCode() );
    7097           0 :                         rEncoding.m_aEncVector.back().m_aUnicode = cChar;
    7098           0 :                         rEncoding.m_aEncVector.back().m_aName = nonenc_it->second;
    7099           0 :                         rEncoding.m_aCMap[ cChar ] = (sal_Int8)(rEncoding.m_aEncVector.size()-1);
    7100           0 :                         nCurFontID = rEncoding.m_nFontID;
    7101           0 :                         cChar = (sal_Ucs)rEncoding.m_aCMap[ cChar ];
    7102             :                     }
    7103             :                 }
    7104             :                 else
    7105           0 :                     pEncoding = NULL;
    7106             :             }
    7107           0 :             if( ! pEncoding )
    7108             :             {
    7109           0 :                 if( cChar & 0xff00 )
    7110             :                 {
    7111             :                     // some characters can be used by conversion
    7112           0 :                     if( cChar >= 0xf000 && cChar <= 0xf0ff ) // symbol encoding in private use area
    7113           0 :                         cChar -= 0xf000;
    7114             :                     else
    7115             :                     {
    7116           0 :                         OString aChar(&cChar, 1, RTL_TEXTENCODING_MS_1252);
    7117           0 :                         cChar = ((sal_Ucs)aChar[0]) & 0x00ff;
    7118             :                     }
    7119             :                 }
    7120             :             }
    7121             : 
    7122           0 :             pMappedGlyphs[ i ] = (sal_Int8)cChar;
    7123           0 :             pMappedFontObjects[ i ] = nCurFontID;
    7124           0 :             pGlyphWidths[ i ] = m_aFontCache.getGlyphWidth( pCurrentFont,
    7125             :                                                             (pEncoding ? *pCurUnicode : cChar) | GF_ISCHAR,
    7126             :                                                             false,
    7127           0 :                                                             m_pReferenceDevice->mpGraphics );
    7128             :         }
    7129             :     }
    7130           0 : }
    7131             : 
    7132           0 : void PDFWriterImpl::drawRelief( SalLayout& rLayout, const String& rText, bool bTextLines )
    7133             : {
    7134           0 :     push( PUSH_ALL );
    7135             : 
    7136           0 :     FontRelief eRelief = m_aCurrentPDFState.m_aFont.GetRelief();
    7137             : 
    7138           0 :     Color aTextColor = m_aCurrentPDFState.m_aFont.GetColor();
    7139           0 :     Color aTextLineColor = m_aCurrentPDFState.m_aTextLineColor;
    7140           0 :     Color aOverlineColor = m_aCurrentPDFState.m_aOverlineColor;
    7141           0 :     Color aReliefColor( COL_LIGHTGRAY );
    7142           0 :     if( aTextColor == COL_BLACK )
    7143           0 :         aTextColor = Color( COL_WHITE );
    7144           0 :     if( aTextLineColor == COL_BLACK )
    7145           0 :         aTextLineColor = Color( COL_WHITE );
    7146           0 :     if( aOverlineColor == COL_BLACK )
    7147           0 :         aOverlineColor = Color( COL_WHITE );
    7148           0 :     if( aTextColor == COL_WHITE )
    7149           0 :         aReliefColor = Color( COL_BLACK );
    7150             : 
    7151           0 :     Font aSetFont = m_aCurrentPDFState.m_aFont;
    7152           0 :     aSetFont.SetRelief( RELIEF_NONE );
    7153           0 :     aSetFont.SetShadow( sal_False );
    7154             : 
    7155           0 :     aSetFont.SetColor( aReliefColor );
    7156           0 :     setTextLineColor( aReliefColor );
    7157           0 :     setOverlineColor( aReliefColor );
    7158           0 :     setFont( aSetFont );
    7159           0 :     long nOff = 1 + getReferenceDevice()->mnDPIX/300;
    7160           0 :     if( eRelief == RELIEF_ENGRAVED )
    7161           0 :         nOff = -nOff;
    7162             : 
    7163           0 :     rLayout.DrawOffset() += Point( nOff, nOff );
    7164           0 :     updateGraphicsState();
    7165           0 :     drawLayout( rLayout, rText, bTextLines );
    7166             : 
    7167           0 :     rLayout.DrawOffset() -= Point( nOff, nOff );
    7168           0 :     setTextLineColor( aTextLineColor );
    7169           0 :     setOverlineColor( aOverlineColor );
    7170           0 :     aSetFont.SetColor( aTextColor );
    7171           0 :     setFont( aSetFont );
    7172           0 :     updateGraphicsState();
    7173           0 :     drawLayout( rLayout, rText, bTextLines );
    7174             : 
    7175             :     // clean up the mess
    7176           0 :     pop();
    7177           0 : }
    7178             : 
    7179           0 : void PDFWriterImpl::drawShadow( SalLayout& rLayout, const String& rText, bool bTextLines )
    7180             : {
    7181           0 :     Font aSaveFont = m_aCurrentPDFState.m_aFont;
    7182           0 :     Color aSaveTextLineColor = m_aCurrentPDFState.m_aTextLineColor;
    7183           0 :     Color aSaveOverlineColor = m_aCurrentPDFState.m_aOverlineColor;
    7184             : 
    7185           0 :     Font& rFont = m_aCurrentPDFState.m_aFont;
    7186           0 :     if( rFont.GetColor() == Color( COL_BLACK ) || rFont.GetColor().GetLuminance() < 8 )
    7187           0 :         rFont.SetColor( Color( COL_LIGHTGRAY ) );
    7188             :     else
    7189           0 :         rFont.SetColor( Color( COL_BLACK ) );
    7190           0 :     rFont.SetShadow( sal_False );
    7191           0 :     rFont.SetOutline( sal_False );
    7192           0 :     setFont( rFont );
    7193           0 :     setTextLineColor( rFont.GetColor() );
    7194           0 :     setOverlineColor( rFont.GetColor() );
    7195           0 :     updateGraphicsState();
    7196             : 
    7197           0 :     long nOff = 1 + ((m_pReferenceDevice->mpFontEntry->mnLineHeight-24)/24);
    7198           0 :     if( rFont.IsOutline() )
    7199           0 :         nOff++;
    7200           0 :     rLayout.DrawBase() += Point( nOff, nOff );
    7201           0 :     drawLayout( rLayout, rText, bTextLines );
    7202           0 :     rLayout.DrawBase() -= Point( nOff, nOff );
    7203             : 
    7204           0 :     setFont( aSaveFont );
    7205           0 :     setTextLineColor( aSaveTextLineColor );
    7206           0 :     setOverlineColor( aSaveOverlineColor );
    7207           0 :     updateGraphicsState();
    7208           0 : }
    7209             : 
    7210           0 : void PDFWriterImpl::drawVerticalGlyphs(
    7211             :         const std::vector<PDFWriterImpl::PDFGlyph>& rGlyphs,
    7212             :         OStringBuffer& rLine,
    7213             :         const Point& rAlignOffset,
    7214             :         const Matrix3& rRotScale,
    7215             :         double fAngle,
    7216             :         double fXScale,
    7217             :         double fSkew,
    7218             :         sal_Int32 nFontHeight )
    7219             : {
    7220           0 :     long nXOffset = 0;
    7221           0 :     Point aCurPos( rGlyphs[0].m_aPos );
    7222           0 :     aCurPos = m_pReferenceDevice->PixelToLogic( aCurPos );
    7223           0 :     aCurPos += rAlignOffset;
    7224           0 :     for( size_t i = 0; i < rGlyphs.size(); i++ )
    7225             :     {
    7226             :         // have to emit each glyph on its own
    7227           0 :         double fDeltaAngle = 0.0;
    7228           0 :         double fYScale = 1.0;
    7229           0 :         double fTempXScale = fXScale;
    7230           0 :         double fSkewB = fSkew;
    7231           0 :         double fSkewA = 0.0;
    7232             : 
    7233           0 :         Point aDeltaPos;
    7234           0 :         if( ( rGlyphs[i].m_nGlyphId & GF_ROTMASK ) == GF_ROTL )
    7235             :         {
    7236           0 :             fDeltaAngle = M_PI/2.0;
    7237           0 :             aDeltaPos.X() = m_pReferenceDevice->GetFontMetric().GetAscent();
    7238           0 :             aDeltaPos.Y() = (int)((double)m_pReferenceDevice->GetFontMetric().GetDescent() * fXScale);
    7239           0 :             fYScale = fXScale;
    7240           0 :             fTempXScale = 1.0;
    7241           0 :             fSkewA = -fSkewB;
    7242           0 :             fSkewB = 0.0;
    7243             :         }
    7244           0 :         else if( ( rGlyphs[i].m_nGlyphId & GF_ROTMASK ) == GF_ROTR )
    7245             :         {
    7246           0 :             fDeltaAngle = -M_PI/2.0;
    7247           0 :             aDeltaPos.X() = (int)((double)m_pReferenceDevice->GetFontMetric().GetDescent()*fXScale);
    7248           0 :             aDeltaPos.Y() = -m_pReferenceDevice->GetFontMetric().GetAscent();
    7249           0 :             fYScale = fXScale;
    7250           0 :             fTempXScale = 1.0;
    7251           0 :             fSkewA = fSkewB;
    7252           0 :             fSkewB = 0.0;
    7253             :         }
    7254           0 :         aDeltaPos += (m_pReferenceDevice->PixelToLogic( Point( (int)((double)nXOffset/fXScale), 0 ) ) - m_pReferenceDevice->PixelToLogic( Point() ) );
    7255           0 :         if( i < rGlyphs.size()-1 )
    7256             :         // #i120627# the text on the Y axis is reversed when export ppt file to PDF format
    7257             :         {
    7258           0 :             long nOffsetX = rGlyphs[i+1].m_aPos.X() - rGlyphs[i].m_aPos.X();
    7259           0 :             long nOffsetY = rGlyphs[i+1].m_aPos.Y() - rGlyphs[i].m_aPos.Y();
    7260           0 :             nXOffset += (int)sqrt(double(nOffsetX*nOffsetX + nOffsetY*nOffsetY));
    7261             :         }
    7262           0 :         if( ! rGlyphs[i].m_nGlyphId )
    7263           0 :             continue;
    7264             : 
    7265           0 :         aDeltaPos = rRotScale.transform( aDeltaPos );
    7266             : 
    7267           0 :         Matrix3 aMat;
    7268           0 :         if( fSkewB != 0.0 || fSkewA != 0.0 )
    7269           0 :             aMat.skew( fSkewA, fSkewB );
    7270           0 :         aMat.scale( fTempXScale, fYScale );
    7271           0 :         aMat.rotate( fAngle+fDeltaAngle );
    7272           0 :         aMat.translate( aCurPos.X()+aDeltaPos.X(), aCurPos.Y()+aDeltaPos.Y() );
    7273           0 :         aMat.append( m_aPages.back(), rLine );
    7274           0 :         rLine.append( " Tm" );
    7275           0 :         if( i == 0 || rGlyphs[i-1].m_nMappedFontId != rGlyphs[i].m_nMappedFontId )
    7276             :         {
    7277           0 :             rLine.append( " /F" );
    7278           0 :             rLine.append( rGlyphs[i].m_nMappedFontId );
    7279           0 :             rLine.append( ' ' );
    7280           0 :             m_aPages.back().appendMappedLength( nFontHeight, rLine, true );
    7281           0 :             rLine.append( " Tf" );
    7282             :         }
    7283           0 :         rLine.append( "<" );
    7284           0 :         appendHex( rGlyphs[i].m_nMappedGlyphId, rLine );
    7285           0 :         rLine.append( ">Tj\n" );
    7286           0 :     }
    7287           0 : }
    7288             : 
    7289           0 : void PDFWriterImpl::drawHorizontalGlyphs(
    7290             :         const std::vector<PDFWriterImpl::PDFGlyph>& rGlyphs,
    7291             :         OStringBuffer& rLine,
    7292             :         const Point& rAlignOffset,
    7293             :         double fAngle,
    7294             :         double fXScale,
    7295             :         double fSkew,
    7296             :         sal_Int32 nFontHeight,
    7297             :         sal_Int32 nPixelFontHeight
    7298             :         )
    7299             : {
    7300             :     // horizontal (= normal) case
    7301             : 
    7302             :     // fill in  run end indices
    7303             :     // end is marked by index of the first glyph of the next run
    7304             :     // a run is marked by same mapped font id and same Y position
    7305           0 :     std::vector< sal_uInt32 > aRunEnds;
    7306           0 :     aRunEnds.reserve( rGlyphs.size() );
    7307           0 :     for( size_t i = 1; i < rGlyphs.size(); i++ )
    7308             :     {
    7309           0 :         if( rGlyphs[i].m_nMappedFontId != rGlyphs[i-1].m_nMappedFontId ||
    7310           0 :             rGlyphs[i].m_aPos.Y() != rGlyphs[i-1].m_aPos.Y() )
    7311             :         {
    7312           0 :             aRunEnds.push_back(i);
    7313             :         }
    7314             :     }
    7315             :     // last run ends at last glyph
    7316           0 :     aRunEnds.push_back( rGlyphs.size() );
    7317             : 
    7318             :     // loop over runs of the same font
    7319           0 :     sal_uInt32 nBeginRun = 0;
    7320           0 :     for( size_t nRun = 0; nRun < aRunEnds.size(); nRun++ )
    7321             :     {
    7322             :         // setup text matrix
    7323           0 :         Point aCurPos = rGlyphs[nBeginRun].m_aPos;
    7324             :         // back transformation to current coordinate system
    7325           0 :         aCurPos = m_pReferenceDevice->PixelToLogic( aCurPos );
    7326           0 :         aCurPos += rAlignOffset;
    7327             :         // the first run can be set with "Td" operator
    7328             :         // subsequent use of that operator would move
    7329             :         // the texline matrix relative to what was set before
    7330             :         // making use of that would drive us into rounding issues
    7331           0 :         Matrix3 aMat;
    7332           0 :         if( nRun == 0 && fAngle == 0.0 && fXScale == 1.0 && fSkew == 0.0 )
    7333             :         {
    7334           0 :             m_aPages.back().appendPoint( aCurPos, rLine, false );
    7335           0 :             rLine.append( " Td " );
    7336             :         }
    7337             :         else
    7338             :         {
    7339           0 :             if( fSkew != 0.0 )
    7340           0 :                 aMat.skew( 0.0, fSkew );
    7341           0 :             aMat.scale( fXScale, 1.0 );
    7342           0 :             aMat.rotate( fAngle );
    7343           0 :             aMat.translate( aCurPos.X(), aCurPos.Y() );
    7344           0 :             aMat.append( m_aPages.back(), rLine );
    7345           0 :             rLine.append( " Tm\n" );
    7346             :         }
    7347             :         // set up correct font
    7348           0 :         rLine.append( "/F" );
    7349           0 :         rLine.append( rGlyphs[nBeginRun].m_nMappedFontId );
    7350           0 :         rLine.append( ' ' );
    7351           0 :         m_aPages.back().appendMappedLength( nFontHeight, rLine, true );
    7352           0 :         rLine.append( " Tf" );
    7353             : 
    7354             :         // output glyphs using Tj or TJ
    7355           0 :         OStringBuffer aKernedLine( 256 ), aUnkernedLine( 256 );
    7356           0 :         aKernedLine.append( "[<" );
    7357           0 :         aUnkernedLine.append( '<' );
    7358           0 :         appendHex( rGlyphs[nBeginRun].m_nMappedGlyphId, aKernedLine );
    7359           0 :         appendHex( rGlyphs[nBeginRun].m_nMappedGlyphId, aUnkernedLine );
    7360             : 
    7361           0 :         aMat.invert();
    7362           0 :         bool bNeedKern = false;
    7363           0 :         for( sal_uInt32 nPos = nBeginRun+1; nPos < aRunEnds[nRun]; nPos++ )
    7364             :         {
    7365           0 :             appendHex( rGlyphs[nPos].m_nMappedGlyphId, aUnkernedLine );
    7366             :             // check if default glyph positioning is sufficient
    7367           0 :             const Point aThisPos = aMat.transform( rGlyphs[nPos].m_aPos );
    7368           0 :             const Point aPrevPos = aMat.transform( rGlyphs[nPos-1].m_aPos );
    7369           0 :             double fAdvance = aThisPos.X() - aPrevPos.X();
    7370           0 :             fAdvance *= 1000.0 / nPixelFontHeight;
    7371           0 :             const sal_Int32 nAdjustment = (sal_Int32)(rGlyphs[nPos-1].m_nNativeWidth - fAdvance + 0.5);
    7372           0 :             if( nAdjustment != 0 )
    7373             :             {
    7374             :                 // apply individual glyph positioning
    7375           0 :                 bNeedKern = true;
    7376           0 :                 aKernedLine.append( ">" );
    7377           0 :                 aKernedLine.append( nAdjustment );
    7378           0 :                 aKernedLine.append( "<" );
    7379             :             }
    7380           0 :             appendHex( rGlyphs[nPos].m_nMappedGlyphId, aKernedLine );
    7381             :         }
    7382           0 :         aKernedLine.append( ">]TJ\n" );
    7383           0 :         aUnkernedLine.append( ">Tj\n" );
    7384             :         rLine.append(
    7385           0 :             (bNeedKern ? aKernedLine : aUnkernedLine).makeStringAndClear() );
    7386             : 
    7387             :         // set beginning of next run
    7388           0 :         nBeginRun = aRunEnds[nRun];
    7389           0 :     }
    7390           0 : }
    7391             : 
    7392           0 : void PDFWriterImpl::drawLayout( SalLayout& rLayout, const String& rText, bool bTextLines )
    7393             : {
    7394             :     // relief takes precedence over shadow (see outdev3.cxx)
    7395           0 :     if(  m_aCurrentPDFState.m_aFont.GetRelief() != RELIEF_NONE )
    7396             :     {
    7397           0 :         drawRelief( rLayout, rText, bTextLines );
    7398           0 :         return;
    7399             :     }
    7400           0 :     else if( m_aCurrentPDFState.m_aFont.IsShadow() )
    7401           0 :         drawShadow( rLayout, rText, bTextLines );
    7402             : 
    7403           0 :     OStringBuffer aLine( 512 );
    7404             : 
    7405           0 :     const int nMaxGlyphs = 256;
    7406             : 
    7407             :     sal_GlyphId pGlyphs[nMaxGlyphs];
    7408             :     sal_Int32 pGlyphWidths[nMaxGlyphs];
    7409             :     sal_uInt8 pMappedGlyphs[nMaxGlyphs];
    7410             :     sal_Int32 pMappedFontObjects[nMaxGlyphs];
    7411           0 :     std::vector<sal_Ucs> aUnicodes;
    7412           0 :     aUnicodes.reserve( nMaxGlyphs );
    7413             :     sal_Int32 pUnicodesPerGlyph[nMaxGlyphs];
    7414             :     int pCharPosAry[nMaxGlyphs];
    7415             :     sal_Int32 nAdvanceWidths[nMaxGlyphs];
    7416           0 :     const PhysicalFontFace* pFallbackFonts[nMaxGlyphs] = { NULL };
    7417           0 :     bool bVertical = m_aCurrentPDFState.m_aFont.IsVertical();
    7418             :     int nGlyphs;
    7419           0 :     int nIndex = 0;
    7420           0 :     int nMinCharPos = 0, nMaxCharPos = rText.Len()-1;
    7421           0 :     double fXScale = 1.0;
    7422           0 :     double fSkew = 0.0;
    7423           0 :     sal_Int32 nPixelFontHeight = m_pReferenceDevice->mpFontEntry->maFontSelData.mnHeight;
    7424           0 :     TextAlign eAlign = m_aCurrentPDFState.m_aFont.GetAlign();
    7425             : 
    7426             :     // transform font height back to current units
    7427             :     // note: the layout calculates in outdevs device pixel !!
    7428           0 :     sal_Int32 nFontHeight = m_pReferenceDevice->ImplDevicePixelToLogicHeight( nPixelFontHeight );
    7429           0 :     if( m_aCurrentPDFState.m_aFont.GetWidth() )
    7430             :     {
    7431           0 :         Font aFont( m_aCurrentPDFState.m_aFont );
    7432           0 :         aFont.SetWidth( 0 );
    7433           0 :         FontMetric aMetric = m_pReferenceDevice->GetFontMetric( aFont );
    7434           0 :         if( aMetric.GetWidth() != m_aCurrentPDFState.m_aFont.GetWidth() )
    7435             :         {
    7436             :             fXScale =
    7437           0 :                 (double)m_aCurrentPDFState.m_aFont.GetWidth() /
    7438           0 :                 (double)aMetric.GetWidth();
    7439             :         }
    7440             :         // force state before GetFontMetric
    7441           0 :         m_pReferenceDevice->ImplNewFont();
    7442             :     }
    7443             : 
    7444             :     // perform artificial italics if necessary
    7445           0 :     if( ( m_aCurrentPDFState.m_aFont.GetItalic() == ITALIC_NORMAL ||
    7446           0 :           m_aCurrentPDFState.m_aFont.GetItalic() == ITALIC_OBLIQUE ) &&
    7447           0 :         !( m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetSlant() == ITALIC_NORMAL ||
    7448           0 :            m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetSlant() == ITALIC_OBLIQUE )
    7449             :         )
    7450             :     {
    7451           0 :         fSkew = M_PI/12.0;
    7452             :     }
    7453             : 
    7454             :     // if the mapmode is distorted we need to adjust for that also
    7455           0 :     if( m_aCurrentPDFState.m_aMapMode.GetScaleX() != m_aCurrentPDFState.m_aMapMode.GetScaleY() )
    7456             :     {
    7457           0 :         fXScale *= double(m_aCurrentPDFState.m_aMapMode.GetScaleX()) / double(m_aCurrentPDFState.m_aMapMode.GetScaleY());
    7458             :     }
    7459             : 
    7460           0 :     int nAngle = m_aCurrentPDFState.m_aFont.GetOrientation();
    7461             :     // normalize angles
    7462           0 :     while( nAngle < 0 )
    7463           0 :         nAngle += 3600;
    7464           0 :     nAngle = nAngle % 3600;
    7465           0 :     double fAngle = (double)nAngle * M_PI / 1800.0;
    7466             : 
    7467           0 :     Matrix3 aRotScale;
    7468           0 :     aRotScale.scale( fXScale, 1.0 );
    7469           0 :     if( fAngle != 0.0 )
    7470           0 :         aRotScale.rotate( -fAngle );
    7471             : 
    7472           0 :     bool bPop = false;
    7473           0 :     bool bABold = false;
    7474             :     // artificial bold necessary ?
    7475           0 :     if( m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetWeight() <= WEIGHT_MEDIUM &&
    7476           0 :         m_pReferenceDevice->mpFontEntry->maFontSelData.GetWeight() > WEIGHT_MEDIUM )
    7477             :     {
    7478           0 :         if( ! bPop )
    7479           0 :             aLine.append( "q " );
    7480           0 :         bPop = true;
    7481           0 :         bABold = true;
    7482             :     }
    7483             :     // setup text colors (if necessary)
    7484           0 :     Color aStrokeColor( COL_TRANSPARENT );
    7485           0 :     Color aNonStrokeColor( COL_TRANSPARENT );
    7486             : 
    7487           0 :     if( m_aCurrentPDFState.m_aFont.IsOutline() )
    7488             :     {
    7489           0 :         aStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7490           0 :         aNonStrokeColor = Color( COL_WHITE );
    7491             :     }
    7492             :     else
    7493           0 :         aNonStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7494           0 :     if( bABold )
    7495           0 :         aStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7496             : 
    7497           0 :     if( aStrokeColor != Color( COL_TRANSPARENT ) && aStrokeColor != m_aCurrentPDFState.m_aLineColor )
    7498             :     {
    7499           0 :         if( ! bPop )
    7500           0 :             aLine.append( "q " );
    7501           0 :         bPop = true;
    7502           0 :         appendStrokingColor( aStrokeColor, aLine );
    7503           0 :         aLine.append( "\n" );
    7504             :     }
    7505           0 :     if( aNonStrokeColor != Color( COL_TRANSPARENT ) && aNonStrokeColor != m_aCurrentPDFState.m_aFillColor )
    7506             :     {
    7507           0 :         if( ! bPop )
    7508           0 :             aLine.append( "q " );
    7509           0 :         bPop = true;
    7510           0 :         appendNonStrokingColor( aNonStrokeColor, aLine );
    7511           0 :         aLine.append( "\n" );
    7512             :     }
    7513             : 
    7514             :     // begin text object
    7515           0 :     aLine.append( "BT\n" );
    7516             :     // outline attribute ?
    7517           0 :     if( m_aCurrentPDFState.m_aFont.IsOutline() || bABold )
    7518             :     {
    7519             :         // set correct text mode, set stroke width
    7520           0 :         aLine.append( "2 Tr " ); // fill, then stroke
    7521             : 
    7522           0 :         if( m_aCurrentPDFState.m_aFont.IsOutline() )
    7523             :         {
    7524             :             // unclear what to do in case of outline and artificial bold
    7525             :             // for the time being outline wins
    7526           0 :             aLine.append( "0.25 w \n" );
    7527             :         }
    7528             :         else
    7529             :         {
    7530           0 :             double fW = (double)m_aCurrentPDFState.m_aFont.GetHeight() / 30.0;
    7531           0 :             m_aPages.back().appendMappedLength( fW, aLine );
    7532           0 :             aLine.append ( " w\n" );
    7533             :         }
    7534             :     }
    7535             : 
    7536           0 :     FontMetric aRefDevFontMetric = m_pReferenceDevice->GetFontMetric();
    7537             : 
    7538             :     // collect the glyphs into a single array
    7539           0 :     const int nTmpMaxGlyphs = rLayout.GetOrientation() ? 1 : nMaxGlyphs; // #i97991# temporary workaround for #i87686#
    7540           0 :     std::vector< PDFGlyph > aGlyphs;
    7541           0 :     aGlyphs.reserve( nTmpMaxGlyphs );
    7542             :     // first get all the glyphs and register them; coordinates still in Pixel
    7543           0 :     Point aGNGlyphPos;
    7544           0 :     while( (nGlyphs = rLayout.GetNextGlyphs( nTmpMaxGlyphs, pGlyphs, aGNGlyphPos, nIndex, nAdvanceWidths, pCharPosAry, pFallbackFonts )) != 0 )
    7545             :     {
    7546           0 :         aUnicodes.clear();
    7547           0 :         for( int i = 0; i < nGlyphs; i++ )
    7548             :         {
    7549             :             // default case: 1 glyph is one unicode
    7550           0 :             pUnicodesPerGlyph[i] = 1;
    7551           0 :             if( (pGlyphs[i] & GF_ISCHAR) )
    7552             :             {
    7553           0 :                 aUnicodes.push_back( static_cast<sal_Ucs>(pGlyphs[i] & GF_IDXMASK) );
    7554             :             }
    7555           0 :             else if( pCharPosAry[i] >= nMinCharPos && pCharPosAry[i] <= nMaxCharPos )
    7556             :             {
    7557           0 :                 int nChars = 1;
    7558           0 :                 aUnicodes.push_back( rText.GetChar( sal::static_int_cast<xub_StrLen>(pCharPosAry[i]) ) );
    7559           0 :                 pUnicodesPerGlyph[i] = 1;
    7560             :                 // try to handle ligatures and such
    7561           0 :                 if( i < nGlyphs-1 )
    7562             :                 {
    7563           0 :                     nChars = pCharPosAry[i+1] - pCharPosAry[i];
    7564             :                     // #i115618# fix for simple RTL+CTL cases
    7565             :                     // TODO: sanitize for RTL ligatures, more complex CTL, etc.
    7566           0 :                     if( nChars < 0 )
    7567           0 :                         nChars = -nChars;
    7568           0 :                     else if( nChars == 0 )
    7569           0 :                         nChars = 1;
    7570           0 :                     pUnicodesPerGlyph[i] = nChars;
    7571           0 :                     for( int n = 1; n < nChars; n++ )
    7572           0 :                         aUnicodes.push_back( rText.GetChar( sal::static_int_cast<xub_StrLen>(pCharPosAry[i]+n) ) );
    7573             :                 }
    7574             :                 // #i36691# hack that is needed because currently the pGlyphs[]
    7575             :                 // argument is ignored for embeddable fonts and so the layout
    7576             :                 // engine's glyph work is ignored (i.e. char mirroring)
    7577             :                 // TODO: a real solution would be to map the layout engine's
    7578             :                 // glyphid (i.e. FreeType's synthetic glyphid for a Type1 font)
    7579             :                 // back to unicode and then to embeddable font's encoding
    7580           0 :                 if( getReferenceDevice()->GetLayoutMode() & TEXT_LAYOUT_BIDI_RTL )
    7581             :                 {
    7582           0 :                     size_t nI = aUnicodes.size()-1;
    7583           0 :                     for( int n = 0; n < nChars; n++, nI-- )
    7584           0 :                         aUnicodes[nI] = static_cast<sal_Ucs>(GetMirroredChar(aUnicodes[nI]));
    7585           0 :                 }
    7586             :             }
    7587             :             else
    7588           0 :                 aUnicodes.push_back( 0 );
    7589             :             // note: in case of ctl one character may result
    7590             :             // in multiple glyphs. The current SalLayout
    7591             :             // implementations set -1 then to indicate that no direct
    7592             :             // mapping is possible
    7593             :         }
    7594             : 
    7595           0 :         registerGlyphs( nGlyphs, pGlyphs, pGlyphWidths, &aUnicodes[0], pUnicodesPerGlyph, pMappedGlyphs, pMappedFontObjects, pFallbackFonts );
    7596             : 
    7597           0 :         for( int i = 0; i < nGlyphs; i++ )
    7598             :         {
    7599             :             aGlyphs.push_back( PDFGlyph( aGNGlyphPos,
    7600             :                                          pGlyphWidths[i],
    7601           0 :                                          pGlyphs[i],
    7602             :                                          pMappedFontObjects[i],
    7603           0 :                                          pMappedGlyphs[i] ) );
    7604           0 :             if( bVertical )
    7605           0 :                 aGNGlyphPos.Y() += nAdvanceWidths[i]/rLayout.GetUnitsPerPixel();
    7606             :             else
    7607           0 :                 aGNGlyphPos.X() += nAdvanceWidths[i]/rLayout.GetUnitsPerPixel();
    7608             :         }
    7609             :     }
    7610             : 
    7611           0 :     Point aAlignOffset;
    7612           0 :     if ( eAlign == ALIGN_BOTTOM )
    7613           0 :         aAlignOffset.Y() -= aRefDevFontMetric.GetDescent();
    7614           0 :     else if ( eAlign == ALIGN_TOP )
    7615           0 :         aAlignOffset.Y() += aRefDevFontMetric.GetAscent();
    7616           0 :     if( aAlignOffset.X() || aAlignOffset.Y() )
    7617           0 :         aAlignOffset = aRotScale.transform( aAlignOffset );
    7618             : 
    7619             :     /* #159153# do not emit an empty glyph vector; this can happen if e.g. the original
    7620             :        string contained only on of the UTF16 BOMs
    7621             :     */
    7622           0 :     if( ! aGlyphs.empty() )
    7623             :     {
    7624           0 :         if( bVertical )
    7625           0 :             drawVerticalGlyphs( aGlyphs, aLine, aAlignOffset, aRotScale, fAngle, fXScale, fSkew, nFontHeight );
    7626             :         else
    7627           0 :             drawHorizontalGlyphs( aGlyphs, aLine, aAlignOffset, fAngle, fXScale, fSkew, nFontHeight, nPixelFontHeight );
    7628             :     }
    7629             : 
    7630             :     // end textobject
    7631           0 :     aLine.append( "ET\n" );
    7632           0 :     if( bPop )
    7633           0 :         aLine.append( "Q\n" );
    7634             : 
    7635           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    7636             : 
    7637             :     // draw eventual textlines
    7638           0 :     FontStrikeout eStrikeout = m_aCurrentPDFState.m_aFont.GetStrikeout();
    7639           0 :     FontUnderline eUnderline = m_aCurrentPDFState.m_aFont.GetUnderline();
    7640           0 :     FontUnderline eOverline  = m_aCurrentPDFState.m_aFont.GetOverline();
    7641           0 :     if( bTextLines &&
    7642             :         (
    7643           0 :          ( eUnderline != UNDERLINE_NONE && eUnderline != UNDERLINE_DONTKNOW ) ||
    7644           0 :          ( eOverline  != UNDERLINE_NONE && eOverline  != UNDERLINE_DONTKNOW ) ||
    7645           0 :          ( eStrikeout != STRIKEOUT_NONE && eStrikeout != STRIKEOUT_DONTKNOW )
    7646             :          )
    7647             :         )
    7648             :     {
    7649           0 :         sal_Bool bUnderlineAbove = OutputDevice::ImplIsUnderlineAbove( m_aCurrentPDFState.m_aFont );
    7650           0 :         if( m_aCurrentPDFState.m_aFont.IsWordLineMode() )
    7651             :         {
    7652           0 :             Point aPos, aStartPt;
    7653           0 :             sal_Int32 nWidth = 0, nAdvance=0;
    7654           0 :             for( int nStart = 0;;)
    7655             :             {
    7656             :                 sal_GlyphId nGlyphIndex;
    7657           0 :                 if( !rLayout.GetNextGlyphs( 1, &nGlyphIndex, aPos, nStart, &nAdvance ) )
    7658           0 :                     break;
    7659             : 
    7660           0 :                 if( !rLayout.IsSpacingGlyph( nGlyphIndex ) )
    7661             :                 {
    7662           0 :                     if( !nWidth )
    7663           0 :                         aStartPt = aPos;
    7664             : 
    7665           0 :                     nWidth += nAdvance;
    7666             :                 }
    7667           0 :                 else if( nWidth > 0 )
    7668             :                 {
    7669           0 :                     drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7670             :                                   m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7671           0 :                                   eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7672           0 :                     nWidth = 0;
    7673             :                 }
    7674           0 :             }
    7675             : 
    7676           0 :             if( nWidth > 0 )
    7677             :             {
    7678           0 :                 drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7679             :                               m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7680           0 :                               eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7681             :             }
    7682             :         }
    7683             :         else
    7684             :         {
    7685           0 :             Point aStartPt = rLayout.GetDrawPosition();
    7686           0 :             int nWidth = rLayout.GetTextWidth() / rLayout.GetUnitsPerPixel();
    7687           0 :             drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7688             :                           m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7689           0 :                           eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7690             :         }
    7691             :     }
    7692             : 
    7693             :     // write eventual emphasis marks
    7694           0 :     if( m_aCurrentPDFState.m_aFont.GetEmphasisMark() & EMPHASISMARK_STYLE )
    7695             :     {
    7696           0 :         PolyPolygon             aEmphPoly;
    7697           0 :         Rectangle               aEmphRect1;
    7698           0 :         Rectangle               aEmphRect2;
    7699             :         long                    nEmphYOff;
    7700             :         long                    nEmphWidth;
    7701             :         long                    nEmphHeight;
    7702             :         sal_Bool                    bEmphPolyLine;
    7703             :         FontEmphasisMark        nEmphMark;
    7704             : 
    7705           0 :         push( PUSH_ALL );
    7706             : 
    7707           0 :         aLine.setLength( 0 );
    7708           0 :         aLine.append( "q\n" );
    7709             : 
    7710           0 :         nEmphMark = m_pReferenceDevice->ImplGetEmphasisMarkStyle( m_aCurrentPDFState.m_aFont );
    7711           0 :         if ( nEmphMark & EMPHASISMARK_POS_BELOW )
    7712           0 :             nEmphHeight = m_pReferenceDevice->mnEmphasisDescent;
    7713             :         else
    7714           0 :             nEmphHeight = m_pReferenceDevice->mnEmphasisAscent;
    7715             :         m_pReferenceDevice->ImplGetEmphasisMark( aEmphPoly,
    7716             :                                                  bEmphPolyLine,
    7717             :                                                  aEmphRect1,
    7718             :                                                  aEmphRect2,
    7719             :                                                  nEmphYOff,
    7720             :                                                  nEmphWidth,
    7721             :                                                  nEmphMark,
    7722             :                                                  m_pReferenceDevice->ImplDevicePixelToLogicWidth(nEmphHeight),
    7723           0 :                                                  m_pReferenceDevice->mpFontEntry->mnOrientation );
    7724           0 :         if ( bEmphPolyLine )
    7725             :         {
    7726           0 :             setLineColor( m_aCurrentPDFState.m_aFont.GetColor() );
    7727           0 :             setFillColor( Color( COL_TRANSPARENT ) );
    7728             :         }
    7729             :         else
    7730             :         {
    7731           0 :             setFillColor( m_aCurrentPDFState.m_aFont.GetColor() );
    7732           0 :             setLineColor( Color( COL_TRANSPARENT ) );
    7733             :         }
    7734           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    7735             : 
    7736           0 :         Point aOffset = Point(0,0);
    7737             : 
    7738           0 :         if ( nEmphMark & EMPHASISMARK_POS_BELOW )
    7739           0 :             aOffset.Y() += m_pReferenceDevice->mpFontEntry->maMetric.mnDescent + nEmphYOff;
    7740             :         else
    7741           0 :             aOffset.Y() -= m_pReferenceDevice->mpFontEntry->maMetric.mnAscent + nEmphYOff;
    7742             : 
    7743           0 :         long nEmphWidth2     = nEmphWidth / 2;
    7744           0 :         long nEmphHeight2    = nEmphHeight / 2;
    7745           0 :         aOffset += Point( nEmphWidth2, nEmphHeight2 );
    7746             : 
    7747           0 :         if ( eAlign == ALIGN_BOTTOM )
    7748           0 :             aOffset.Y() -= m_pReferenceDevice->mpFontEntry->maMetric.mnDescent;
    7749           0 :         else if ( eAlign == ALIGN_TOP )
    7750           0 :             aOffset.Y() += m_pReferenceDevice->mpFontEntry->maMetric.mnAscent;
    7751             : 
    7752           0 :         for( int nStart = 0;;)
    7753             :         {
    7754           0 :             Point aPos;
    7755             :             sal_GlyphId nGlyphIndex;
    7756             :             sal_Int32 nAdvance;
    7757           0 :             if( !rLayout.GetNextGlyphs( 1, &nGlyphIndex, aPos, nStart, &nAdvance ) )
    7758           0 :                 break;
    7759             : 
    7760           0 :             if( !rLayout.IsSpacingGlyph( nGlyphIndex ) )
    7761             :             {
    7762           0 :                 Point aAdjOffset = aOffset;
    7763           0 :                 aAdjOffset.X() += (nAdvance - nEmphWidth) / 2;
    7764           0 :                 aAdjOffset = aRotScale.transform( aAdjOffset );
    7765             : 
    7766           0 :                 aAdjOffset -= Point( nEmphWidth2, nEmphHeight2 );
    7767             : 
    7768           0 :                 aPos += aAdjOffset;
    7769           0 :                 aPos = m_pReferenceDevice->PixelToLogic( aPos );
    7770           0 :                 drawEmphasisMark( aPos.X(), aPos.Y(),
    7771             :                                   aEmphPoly, bEmphPolyLine,
    7772           0 :                                   aEmphRect1, aEmphRect2 );
    7773             :             }
    7774           0 :         }
    7775             : 
    7776           0 :         writeBuffer( "Q\n", 2 );
    7777           0 :         pop();
    7778           0 :     }
    7779             : 
    7780             : }
    7781             : 
    7782           0 : void PDFWriterImpl::drawEmphasisMark( long nX, long nY,
    7783             :                                       const PolyPolygon& rPolyPoly, sal_Bool bPolyLine,
    7784             :                                       const Rectangle& rRect1, const Rectangle& rRect2 )
    7785             : {
    7786             :     // TODO: pass nWidth as width of this mark
    7787             :     // long nWidth = 0;
    7788             : 
    7789           0 :     if ( rPolyPoly.Count() )
    7790             :     {
    7791           0 :         if ( bPolyLine )
    7792             :         {
    7793           0 :             Polygon aPoly = rPolyPoly.GetObject( 0 );
    7794           0 :             aPoly.Move( nX, nY );
    7795           0 :             drawPolyLine( aPoly );
    7796             :         }
    7797             :         else
    7798             :         {
    7799           0 :             PolyPolygon aPolyPoly = rPolyPoly;
    7800           0 :             aPolyPoly.Move( nX, nY );
    7801           0 :             drawPolyPolygon( aPolyPoly );
    7802             :         }
    7803             :     }
    7804             : 
    7805           0 :     if ( !rRect1.IsEmpty() )
    7806             :     {
    7807           0 :         Rectangle aRect( Point( nX+rRect1.Left(),
    7808           0 :                                 nY+rRect1.Top() ), rRect1.GetSize() );
    7809           0 :         drawRectangle( aRect );
    7810             :     }
    7811             : 
    7812           0 :     if ( !rRect2.IsEmpty() )
    7813             :     {
    7814           0 :         Rectangle aRect( Point( nX+rRect2.Left(),
    7815           0 :                                 nY+rRect2.Top() ), rRect2.GetSize() );
    7816             : 
    7817           0 :         drawRectangle( aRect );
    7818             :     }
    7819           0 : }
    7820             : 
    7821           0 : void PDFWriterImpl::drawText( const Point& rPos, const String& rText, xub_StrLen nIndex, xub_StrLen nLen, bool bTextLines )
    7822             : {
    7823           0 :     MARK( "drawText" );
    7824             : 
    7825           0 :     updateGraphicsState();
    7826             : 
    7827             :     // get a layout from the OuputDevice's SalGraphics
    7828             :     // this also enforces font substitution and sets the font on SalGraphics
    7829           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos );
    7830           0 :     if( pLayout )
    7831             :     {
    7832           0 :         drawLayout( *pLayout, rText, bTextLines );
    7833           0 :         pLayout->Release();
    7834             :     }
    7835           0 : }
    7836             : 
    7837           0 : void PDFWriterImpl::drawTextArray( const Point& rPos, const String& rText, const sal_Int32* pDXArray, xub_StrLen nIndex, xub_StrLen nLen, bool bTextLines )
    7838             : {
    7839           0 :     MARK( "drawText with array" );
    7840             : 
    7841           0 :     updateGraphicsState();
    7842             : 
    7843             :     // get a layout from the OuputDevice's SalGraphics
    7844             :     // this also enforces font substitution and sets the font on SalGraphics
    7845           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos, 0, pDXArray );
    7846           0 :     if( pLayout )
    7847             :     {
    7848           0 :         drawLayout( *pLayout, rText, bTextLines );
    7849           0 :         pLayout->Release();
    7850             :     }
    7851           0 : }
    7852             : 
    7853           0 : void PDFWriterImpl::drawStretchText( const Point& rPos, sal_uLong nWidth, const String& rText, xub_StrLen nIndex, xub_StrLen nLen, bool bTextLines )
    7854             : {
    7855           0 :     MARK( "drawStretchText" );
    7856             : 
    7857           0 :     updateGraphicsState();
    7858             : 
    7859             :     // get a layout from the OuputDevice's SalGraphics
    7860             :     // this also enforces font substitution and sets the font on SalGraphics
    7861           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos, nWidth );
    7862           0 :     if( pLayout )
    7863             :     {
    7864           0 :         drawLayout( *pLayout, rText, bTextLines );
    7865           0 :         pLayout->Release();
    7866             :     }
    7867           0 : }
    7868             : 
    7869           0 : void PDFWriterImpl::drawText( const Rectangle& rRect, const String& rOrigStr, sal_uInt16 nStyle, bool bTextLines )
    7870             : {
    7871           0 :     long        nWidth          = rRect.GetWidth();
    7872           0 :     long        nHeight         = rRect.GetHeight();
    7873             : 
    7874           0 :     if ( nWidth <= 0 || nHeight <= 0 )
    7875           0 :         return;
    7876             : 
    7877           0 :     MARK( "drawText with rectangle" );
    7878             : 
    7879           0 :     updateGraphicsState();
    7880             : 
    7881             :     // clip with rectangle
    7882           0 :     OStringBuffer aLine;
    7883           0 :     aLine.append( "q " );
    7884           0 :     m_aPages.back().appendRect( rRect, aLine );
    7885           0 :     aLine.append( " W* n\n" );
    7886           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    7887             : 
    7888             :     // if disabled text is needed, put in here
    7889             : 
    7890           0 :     Point       aPos            = rRect.TopLeft();
    7891             : 
    7892           0 :     long        nTextHeight     = m_pReferenceDevice->GetTextHeight();
    7893           0 :     xub_StrLen  nMnemonicPos    = STRING_NOTFOUND;
    7894             : 
    7895           0 :     String aStr = rOrigStr;
    7896           0 :     if ( nStyle & TEXT_DRAW_MNEMONIC )
    7897           0 :         aStr = m_pReferenceDevice->GetNonMnemonicString( aStr, nMnemonicPos );
    7898             : 
    7899             :     // multiline text
    7900           0 :     if ( nStyle & TEXT_DRAW_MULTILINE )
    7901             :     {
    7902           0 :         OUString           aLastLine;
    7903           0 :         ImplMultiTextLineInfo   aMultiLineInfo;
    7904             :         ImplTextLineInfo*       pLineInfo;
    7905             :         xub_StrLen              i;
    7906             :         xub_StrLen              nLines;
    7907             :         xub_StrLen              nFormatLines;
    7908             : 
    7909           0 :         if ( nTextHeight )
    7910             :         {
    7911           0 :             ::vcl::DefaultTextLayout aLayout( *m_pReferenceDevice );
    7912           0 :             OutputDevice::ImplGetTextLines( aMultiLineInfo, nWidth, aStr, nStyle, aLayout );
    7913           0 :             nLines = (xub_StrLen)(nHeight/nTextHeight);
    7914           0 :             nFormatLines = aMultiLineInfo.Count();
    7915           0 :             if ( !nLines )
    7916           0 :                 nLines = 1;
    7917           0 :             if ( nFormatLines > nLines )
    7918             :             {
    7919           0 :                 if ( nStyle & TEXT_DRAW_ENDELLIPSIS )
    7920             :                 {
    7921             :                     // handle last line
    7922           0 :                     nFormatLines = nLines-1;
    7923             : 
    7924           0 :                     pLineInfo = aMultiLineInfo.GetLine( nFormatLines );
    7925           0 :                     aLastLine = convertLineEnd(aStr.Copy(pLineInfo->GetIndex()), LINEEND_LF);
    7926             :                     // replace line feed by space
    7927           0 :                     aLastLine = aLastLine.replace('\n', ' ');
    7928           0 :                     aLastLine = m_pReferenceDevice->GetEllipsisString( aLastLine, nWidth, nStyle );
    7929           0 :                     nStyle &= ~(TEXT_DRAW_VCENTER | TEXT_DRAW_BOTTOM);
    7930           0 :                     nStyle |= TEXT_DRAW_TOP;
    7931             :                 }
    7932             :             }
    7933             : 
    7934             :             // vertical alignment
    7935           0 :             if ( nStyle & TEXT_DRAW_BOTTOM )
    7936           0 :                 aPos.Y() += nHeight-(nFormatLines*nTextHeight);
    7937           0 :             else if ( nStyle & TEXT_DRAW_VCENTER )
    7938           0 :                 aPos.Y() += (nHeight-(nFormatLines*nTextHeight))/2;
    7939             : 
    7940             :             // draw all lines excluding the last
    7941           0 :             for ( i = 0; i < nFormatLines; i++ )
    7942             :             {
    7943           0 :                 pLineInfo = aMultiLineInfo.GetLine( i );
    7944           0 :                 if ( nStyle & TEXT_DRAW_RIGHT )
    7945           0 :                     aPos.X() += nWidth-pLineInfo->GetWidth();
    7946           0 :                 else if ( nStyle & TEXT_DRAW_CENTER )
    7947           0 :                     aPos.X() += (nWidth-pLineInfo->GetWidth())/2;
    7948           0 :                 xub_StrLen nIndex   = pLineInfo->GetIndex();
    7949           0 :                 xub_StrLen nLineLen = pLineInfo->GetLen();
    7950           0 :                 drawText( aPos, aStr, nIndex, nLineLen, bTextLines );
    7951             :                 // mnemonics should not appear in documents,
    7952             :                 // if the need arises, put them in here
    7953           0 :                 aPos.Y() += nTextHeight;
    7954           0 :                 aPos.X() = rRect.Left();
    7955             :             }
    7956             : 
    7957             : 
    7958             :             // output last line left adjusted since it was shortened
    7959           0 :             if (!aLastLine.isEmpty())
    7960           0 :                 drawText( aPos, aLastLine, 0, STRING_LEN, bTextLines );
    7961           0 :         }
    7962             :     }
    7963             :     else
    7964             :     {
    7965           0 :         long nTextWidth = m_pReferenceDevice->GetTextWidth( aStr );
    7966             : 
    7967             :         // Evt. Text kuerzen
    7968           0 :         if ( nTextWidth > nWidth )
    7969             :         {
    7970           0 :             if ( nStyle & (TEXT_DRAW_ENDELLIPSIS | TEXT_DRAW_PATHELLIPSIS | TEXT_DRAW_NEWSELLIPSIS) )
    7971             :             {
    7972           0 :                 aStr = m_pReferenceDevice->GetEllipsisString( aStr, nWidth, nStyle );
    7973           0 :                 nStyle &= ~(TEXT_DRAW_CENTER | TEXT_DRAW_RIGHT);
    7974           0 :                 nStyle |= TEXT_DRAW_LEFT;
    7975           0 :                 nTextWidth = m_pReferenceDevice->GetTextWidth( aStr );
    7976             :             }
    7977             :         }
    7978             : 
    7979             :         // vertical alignment
    7980           0 :         if ( nStyle & TEXT_DRAW_RIGHT )
    7981           0 :             aPos.X() += nWidth-nTextWidth;
    7982           0 :         else if ( nStyle & TEXT_DRAW_CENTER )
    7983           0 :             aPos.X() += (nWidth-nTextWidth)/2;
    7984             : 
    7985           0 :         if ( nStyle & TEXT_DRAW_BOTTOM )
    7986           0 :             aPos.Y() += nHeight-nTextHeight;
    7987           0 :         else if ( nStyle & TEXT_DRAW_VCENTER )
    7988           0 :             aPos.Y() += (nHeight-nTextHeight)/2;
    7989             : 
    7990             :         // mnemonics should be inserted here if the need arises
    7991             : 
    7992             :         // draw the actual text
    7993           0 :         drawText( aPos, aStr, 0, STRING_LEN, bTextLines );
    7994             :     }
    7995             : 
    7996             :     // reset clip region to original value
    7997           0 :     aLine.setLength( 0 );
    7998           0 :     aLine.append( "Q\n" );
    7999           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8000             : }
    8001             : 
    8002           0 : void PDFWriterImpl::drawLine( const Point& rStart, const Point& rStop )
    8003             : {
    8004           0 :     MARK( "drawLine" );
    8005             : 
    8006           0 :     updateGraphicsState();
    8007             : 
    8008           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    8009           0 :         return;
    8010             : 
    8011           0 :     OStringBuffer aLine;
    8012           0 :     m_aPages.back().appendPoint( rStart, aLine );
    8013           0 :     aLine.append( " m " );
    8014           0 :     m_aPages.back().appendPoint( rStop, aLine );
    8015           0 :     aLine.append( " l S\n" );
    8016             : 
    8017           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8018             : }
    8019             : 
    8020           0 : void PDFWriterImpl::drawLine( const Point& rStart, const Point& rStop, const LineInfo& rInfo )
    8021             : {
    8022           0 :     MARK( "drawLine with LineInfo" );
    8023           0 :     updateGraphicsState();
    8024             : 
    8025           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    8026           0 :         return;
    8027             : 
    8028           0 :     if( rInfo.GetStyle() == LINE_SOLID && rInfo.GetWidth() < 2 )
    8029             :     {
    8030           0 :         drawLine( rStart, rStop );
    8031           0 :         return;
    8032             :     }
    8033             : 
    8034           0 :     OStringBuffer aLine;
    8035             : 
    8036           0 :     aLine.append( "q " );
    8037           0 :     if( m_aPages.back().appendLineInfo( rInfo, aLine ) )
    8038             :     {
    8039           0 :         m_aPages.back().appendPoint( rStart, aLine );
    8040           0 :         aLine.append( " m " );
    8041           0 :         m_aPages.back().appendPoint( rStop, aLine );
    8042           0 :         aLine.append( " l S Q\n" );
    8043             : 
    8044           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    8045             :     }
    8046             :     else
    8047             :     {
    8048           0 :         PDFWriter::ExtLineInfo aInfo;
    8049           0 :         convertLineInfoToExtLineInfo( rInfo, aInfo );
    8050           0 :         Point aPolyPoints[2] = { rStart, rStop };
    8051           0 :         Polygon aPoly( 2, aPolyPoints );
    8052           0 :         drawPolyLine( aPoly, aInfo );
    8053           0 :     }
    8054             : }
    8055             : 
    8056             : #define HCONV( x ) m_pReferenceDevice->ImplDevicePixelToLogicHeight( x )
    8057             : 
    8058           0 : void PDFWriterImpl::drawWaveTextLine( OStringBuffer& aLine, long nWidth, FontUnderline eTextLine, Color aColor, bool bIsAbove )
    8059             : {
    8060             :     // note: units in pFontEntry are ref device pixel
    8061           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8062           0 :     long            nLineHeight = 0;
    8063           0 :     long            nLinePos = 0;
    8064             : 
    8065           0 :     appendStrokingColor( aColor, aLine );
    8066           0 :     aLine.append( "\n" );
    8067             : 
    8068           0 :     if ( bIsAbove )
    8069             :     {
    8070           0 :         if ( !pFontEntry->maMetric.mnAboveWUnderlineSize )
    8071           0 :             m_pReferenceDevice->ImplInitAboveTextLineSize();
    8072           0 :         nLineHeight = HCONV( pFontEntry->maMetric.mnAboveWUnderlineSize );
    8073           0 :         nLinePos = HCONV( pFontEntry->maMetric.mnAboveWUnderlineOffset );
    8074             :     }
    8075             :     else
    8076             :     {
    8077           0 :         if ( !pFontEntry->maMetric.mnWUnderlineSize )
    8078           0 :             m_pReferenceDevice->ImplInitTextLineSize();
    8079           0 :         nLineHeight = HCONV( pFontEntry->maMetric.mnWUnderlineSize );
    8080           0 :         nLinePos = HCONV( pFontEntry->maMetric.mnWUnderlineOffset );
    8081             :     }
    8082           0 :     if ( (eTextLine == UNDERLINE_SMALLWAVE) && (nLineHeight > 3) )
    8083           0 :         nLineHeight = 3;
    8084             : 
    8085           0 :     long nLineWidth = getReferenceDevice()->mnDPIX/450;
    8086           0 :     if ( ! nLineWidth )
    8087           0 :         nLineWidth = 1;
    8088             : 
    8089           0 :     if ( eTextLine == UNDERLINE_BOLDWAVE )
    8090           0 :         nLineWidth = 3*nLineWidth;
    8091             : 
    8092           0 :     m_aPages.back().appendMappedLength( (sal_Int32)nLineWidth, aLine );
    8093           0 :     aLine.append( " w " );
    8094             : 
    8095           0 :     if ( eTextLine == UNDERLINE_DOUBLEWAVE )
    8096             :     {
    8097           0 :         long nOrgLineHeight = nLineHeight;
    8098           0 :         nLineHeight /= 3;
    8099           0 :         if ( nLineHeight < 2 )
    8100             :         {
    8101           0 :             if ( nOrgLineHeight > 1 )
    8102           0 :                 nLineHeight = 2;
    8103             :             else
    8104           0 :                 nLineHeight = 1;
    8105             :         }
    8106           0 :         long nLineDY = nOrgLineHeight-(nLineHeight*2);
    8107           0 :         if ( nLineDY < nLineWidth )
    8108           0 :             nLineDY = nLineWidth;
    8109           0 :         long nLineDY2 = nLineDY/2;
    8110           0 :         if ( !nLineDY2 )
    8111           0 :             nLineDY2 = 1;
    8112             : 
    8113           0 :         nLinePos -= nLineWidth-nLineDY2;
    8114             : 
    8115           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, 2*nLineHeight, aLine );
    8116             : 
    8117           0 :         nLinePos += nLineWidth+nLineDY;
    8118           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, 2*nLineHeight, aLine );
    8119             :     }
    8120             :     else
    8121             :     {
    8122           0 :         if ( eTextLine != UNDERLINE_BOLDWAVE )
    8123           0 :             nLinePos -= nLineWidth/2;
    8124           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, nLineHeight, aLine );
    8125             :     }
    8126           0 : }
    8127             : 
    8128           0 : void PDFWriterImpl::drawStraightTextLine( OStringBuffer& aLine, long nWidth, FontUnderline eTextLine, Color aColor, bool bIsAbove )
    8129             : {
    8130             :     // note: units in pFontEntry are ref device pixel
    8131           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8132           0 :     long            nLineHeight = 0;
    8133           0 :     long            nLinePos  = 0;
    8134           0 :     long            nLinePos2 = 0;
    8135             : 
    8136           0 :     if ( eTextLine > UNDERLINE_BOLDWAVE )
    8137           0 :         eTextLine = UNDERLINE_SINGLE;
    8138             : 
    8139           0 :     switch ( eTextLine )
    8140             :     {
    8141             :         case UNDERLINE_SINGLE:
    8142             :         case UNDERLINE_DOTTED:
    8143             :         case UNDERLINE_DASH:
    8144             :         case UNDERLINE_LONGDASH:
    8145             :         case UNDERLINE_DASHDOT:
    8146             :         case UNDERLINE_DASHDOTDOT:
    8147           0 :             if ( bIsAbove )
    8148             :             {
    8149           0 :                 if ( !pFontEntry->maMetric.mnAboveUnderlineSize )
    8150           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8151           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveUnderlineSize );
    8152           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveUnderlineOffset );
    8153             :             }
    8154             :             else
    8155             :             {
    8156           0 :                 if ( !pFontEntry->maMetric.mnUnderlineSize )
    8157           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8158           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnUnderlineSize );
    8159           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnUnderlineOffset );
    8160             :             }
    8161           0 :             break;
    8162             :         case UNDERLINE_BOLD:
    8163             :         case UNDERLINE_BOLDDOTTED:
    8164             :         case UNDERLINE_BOLDDASH:
    8165             :         case UNDERLINE_BOLDLONGDASH:
    8166             :         case UNDERLINE_BOLDDASHDOT:
    8167             :         case UNDERLINE_BOLDDASHDOTDOT:
    8168           0 :             if ( bIsAbove )
    8169             :             {
    8170           0 :                 if ( !pFontEntry->maMetric.mnAboveBUnderlineSize )
    8171           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8172           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveBUnderlineSize );
    8173           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveBUnderlineOffset );
    8174             :             }
    8175             :             else
    8176             :             {
    8177           0 :                 if ( !pFontEntry->maMetric.mnBUnderlineSize )
    8178           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8179           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnBUnderlineSize );
    8180           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnBUnderlineOffset );
    8181           0 :                 nLinePos += nLineHeight/2;
    8182             :             }
    8183           0 :             break;
    8184             :         case UNDERLINE_DOUBLE:
    8185           0 :             if ( bIsAbove )
    8186             :             {
    8187           0 :                 if ( !pFontEntry->maMetric.mnAboveDUnderlineSize )
    8188           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8189           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveDUnderlineSize );
    8190           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveDUnderlineOffset1 );
    8191           0 :                 nLinePos2   = HCONV( pFontEntry->maMetric.mnAboveDUnderlineOffset2 );
    8192             :             }
    8193             :             else
    8194             :             {
    8195           0 :                 if ( !pFontEntry->maMetric.mnDUnderlineSize )
    8196           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8197           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnDUnderlineSize );
    8198           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnDUnderlineOffset1 );
    8199           0 :                 nLinePos2   = HCONV( pFontEntry->maMetric.mnDUnderlineOffset2 );
    8200             :             }
    8201             :         default:
    8202           0 :             break;
    8203             :     }
    8204             : 
    8205           0 :     if ( nLineHeight )
    8206             :     {
    8207           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, true );
    8208           0 :         aLine.append( " w " );
    8209           0 :         appendStrokingColor( aColor, aLine );
    8210           0 :         aLine.append( "\n" );
    8211             : 
    8212           0 :         switch ( eTextLine )
    8213             :         {
    8214             :             case UNDERLINE_DOTTED:
    8215             :             case UNDERLINE_BOLDDOTTED:
    8216           0 :                 aLine.append( "[ " );
    8217           0 :                 m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8218           0 :                 aLine.append( " ] 0 d\n" );
    8219           0 :                 break;
    8220             :             case UNDERLINE_DASH:
    8221             :             case UNDERLINE_LONGDASH:
    8222             :             case UNDERLINE_BOLDDASH:
    8223             :             case UNDERLINE_BOLDLONGDASH:
    8224             :                 {
    8225           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8226           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8227           0 :                     if ( ( eTextLine == UNDERLINE_LONGDASH ) || ( eTextLine == UNDERLINE_BOLDLONGDASH ) )
    8228           0 :                         nDashLength = 8*nLineHeight;
    8229             : 
    8230           0 :                     aLine.append( "[ " );
    8231           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8232           0 :                     aLine.append( ' ' );
    8233           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8234           0 :                     aLine.append( " ] 0 d\n" );
    8235             :                 }
    8236           0 :                 break;
    8237             :             case UNDERLINE_DASHDOT:
    8238             :             case UNDERLINE_BOLDDASHDOT:
    8239             :                 {
    8240           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8241           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8242           0 :                     aLine.append( "[ " );
    8243           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8244           0 :                     aLine.append( ' ' );
    8245           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8246           0 :                     aLine.append( ' ' );
    8247           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8248           0 :                     aLine.append( ' ' );
    8249           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8250           0 :                     aLine.append( " ] 0 d\n" );
    8251             :                 }
    8252           0 :                 break;
    8253             :             case UNDERLINE_DASHDOTDOT:
    8254             :             case UNDERLINE_BOLDDASHDOTDOT:
    8255             :                 {
    8256           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8257           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8258           0 :                     aLine.append( "[ " );
    8259           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8260           0 :                     aLine.append( ' ' );
    8261           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8262           0 :                     aLine.append( ' ' );
    8263           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8264           0 :                     aLine.append( ' ' );
    8265           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8266           0 :                     aLine.append( ' ' );
    8267           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8268           0 :                     aLine.append( ' ' );
    8269           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8270           0 :                     aLine.append( " ] 0 d\n" );
    8271             :                 }
    8272           0 :                 break;
    8273             :             default:
    8274           0 :                 break;
    8275             :         }
    8276             : 
    8277           0 :         aLine.append( "0 " );
    8278           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8279           0 :         aLine.append( " m " );
    8280           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, false );
    8281           0 :         aLine.append( ' ' );
    8282           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8283           0 :         aLine.append( " l S\n" );
    8284           0 :         if ( eTextLine == UNDERLINE_DOUBLE )
    8285             :         {
    8286           0 :             aLine.append( "0 " );
    8287           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), 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)(-nLinePos2-nLineHeight), aLine, true );
    8292           0 :             aLine.append( " l S\n" );
    8293             :         }
    8294             :     }
    8295           0 : }
    8296             : 
    8297           0 : void PDFWriterImpl::drawStrikeoutLine( OStringBuffer& aLine, long nWidth, FontStrikeout eStrikeout, Color aColor )
    8298             : {
    8299             :     // note: units in pFontEntry are ref device pixel
    8300           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8301           0 :     long            nLineHeight = 0;
    8302           0 :     long            nLinePos  = 0;
    8303           0 :     long            nLinePos2 = 0;
    8304             : 
    8305           0 :     if ( eStrikeout > STRIKEOUT_X )
    8306           0 :         eStrikeout = STRIKEOUT_SINGLE;
    8307             : 
    8308           0 :     switch ( eStrikeout )
    8309             :     {
    8310             :         case STRIKEOUT_SINGLE:
    8311           0 :             if ( !pFontEntry->maMetric.mnStrikeoutSize )
    8312           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8313           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnStrikeoutSize );
    8314           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnStrikeoutOffset );
    8315           0 :             break;
    8316             :         case STRIKEOUT_BOLD:
    8317           0 :             if ( !pFontEntry->maMetric.mnBStrikeoutSize )
    8318           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8319           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnBStrikeoutSize );
    8320           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnBStrikeoutOffset );
    8321           0 :             break;
    8322             :         case STRIKEOUT_DOUBLE:
    8323           0 :             if ( !pFontEntry->maMetric.mnDStrikeoutSize )
    8324           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8325           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnDStrikeoutSize );
    8326           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnDStrikeoutOffset1 );
    8327           0 :             nLinePos2   = HCONV( pFontEntry->maMetric.mnDStrikeoutOffset2 );
    8328           0 :             break;
    8329             :         default:
    8330           0 :             break;
    8331             :     }
    8332             : 
    8333           0 :     if ( nLineHeight )
    8334             :     {
    8335           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, true );
    8336           0 :         aLine.append( " w " );
    8337           0 :         appendStrokingColor( aColor, aLine );
    8338           0 :         aLine.append( "\n" );
    8339             : 
    8340           0 :         aLine.append( "0 " );
    8341           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8342           0 :         aLine.append( " m " );
    8343           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, true );
    8344           0 :         aLine.append( ' ' );
    8345           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8346           0 :         aLine.append( " l S\n" );
    8347             : 
    8348           0 :         if ( eStrikeout == STRIKEOUT_DOUBLE )
    8349             :         {
    8350           0 :             aLine.append( "0 " );
    8351           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8352           0 :             aLine.append( " m " );
    8353           0 :             m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, true );
    8354           0 :             aLine.append( ' ' );
    8355           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8356           0 :             aLine.append( " l S\n" );
    8357             :         }
    8358             :     }
    8359           0 : }
    8360             : 
    8361           0 : void PDFWriterImpl::drawStrikeoutChar( const Point& rPos, long nWidth, FontStrikeout eStrikeout )
    8362             : {
    8363             :     //See qadevOOo/testdocs/StrikeThrough.odt for examples if you need
    8364             :     //to tweak this
    8365             : 
    8366           0 :     OUString aStrikeoutChar = eStrikeout == STRIKEOUT_SLASH ? OUString("/") : OUString("X");
    8367           0 :     String aStrikeout = aStrikeoutChar;
    8368           0 :     while( m_pReferenceDevice->GetTextWidth( aStrikeout ) < nWidth )
    8369           0 :         aStrikeout.Append( aStrikeout );
    8370             : 
    8371             :     // do not get broader than nWidth modulo 1 character
    8372           0 :     while( m_pReferenceDevice->GetTextWidth( aStrikeout ) >= nWidth )
    8373           0 :         aStrikeout.Erase( 0, 1 );
    8374           0 :     aStrikeout.Append( aStrikeoutChar );
    8375           0 :     sal_Bool bShadow = m_aCurrentPDFState.m_aFont.IsShadow();
    8376           0 :     if ( bShadow )
    8377             :     {
    8378           0 :         Font aFont = m_aCurrentPDFState.m_aFont;
    8379           0 :         aFont.SetShadow( sal_False );
    8380           0 :         setFont( aFont );
    8381           0 :         updateGraphicsState();
    8382             :     }
    8383             : 
    8384             :     // strikeout string is left aligned non-CTL text
    8385           0 :     sal_uLong nOrigTLM = m_pReferenceDevice->GetLayoutMode();
    8386           0 :     m_pReferenceDevice->SetLayoutMode( TEXT_LAYOUT_BIDI_STRONG|TEXT_LAYOUT_COMPLEX_DISABLED );
    8387             : 
    8388           0 :     push( PUSH_CLIPREGION );
    8389           0 :     FontMetric aRefDevFontMetric = m_pReferenceDevice->GetFontMetric();
    8390           0 :     Rectangle aRect;
    8391           0 :     aRect.Left() = rPos.X();
    8392           0 :     aRect.Right() = aRect.Left()+nWidth;
    8393           0 :     aRect.Bottom() = rPos.Y()+aRefDevFontMetric.GetDescent();
    8394           0 :     aRect.Top() = rPos.Y()-aRefDevFontMetric.GetAscent();
    8395             : 
    8396           0 :     ImplFontEntry* pFontEntry = m_pReferenceDevice->mpFontEntry;
    8397           0 :     if (pFontEntry->mnOrientation)
    8398             :     {
    8399           0 :         Polygon aPoly( aRect );
    8400           0 :         aPoly.Rotate( rPos, pFontEntry->mnOrientation);
    8401           0 :         aRect = aPoly.GetBoundRect();
    8402             :     }
    8403             : 
    8404           0 :     intersectClipRegion( aRect );
    8405           0 :     drawText( rPos, aStrikeout, 0, aStrikeout.Len(), false );
    8406           0 :     pop();
    8407             : 
    8408           0 :     m_pReferenceDevice->SetLayoutMode( nOrigTLM );
    8409             : 
    8410           0 :     if ( bShadow )
    8411             :     {
    8412           0 :         Font aFont = m_aCurrentPDFState.m_aFont;
    8413           0 :         aFont.SetShadow( sal_True );
    8414           0 :         setFont( aFont );
    8415           0 :         updateGraphicsState();
    8416           0 :     }
    8417           0 : }
    8418             : 
    8419           0 : void PDFWriterImpl::drawTextLine( const Point& rPos, long nWidth, FontStrikeout eStrikeout, FontUnderline eUnderline, FontUnderline eOverline, bool bUnderlineAbove )
    8420             : {
    8421           0 :     if ( !nWidth ||
    8422           0 :          ( ((eStrikeout == STRIKEOUT_NONE)||(eStrikeout == STRIKEOUT_DONTKNOW)) &&
    8423           0 :            ((eUnderline == UNDERLINE_NONE)||(eUnderline == UNDERLINE_DONTKNOW)) &&
    8424           0 :            ((eOverline  == UNDERLINE_NONE)||(eOverline  == UNDERLINE_DONTKNOW)) ) )
    8425           0 :         return;
    8426             : 
    8427           0 :     MARK( "drawTextLine" );
    8428           0 :     updateGraphicsState();
    8429             : 
    8430             :     // note: units in pFontEntry are ref device pixel
    8431           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8432           0 :     Color           aUnderlineColor = m_aCurrentPDFState.m_aTextLineColor;
    8433           0 :     Color           aOverlineColor  = m_aCurrentPDFState.m_aOverlineColor;
    8434           0 :     Color           aStrikeoutColor = m_aCurrentPDFState.m_aFont.GetColor();
    8435           0 :     bool            bStrikeoutDone = false;
    8436           0 :     bool            bUnderlineDone = false;
    8437           0 :     bool            bOverlineDone  = false;
    8438             : 
    8439           0 :     if ( (eStrikeout == STRIKEOUT_SLASH) || (eStrikeout == STRIKEOUT_X) )
    8440             :     {
    8441           0 :         drawStrikeoutChar( rPos, nWidth, eStrikeout );
    8442           0 :         bStrikeoutDone = true;
    8443             :     }
    8444             : 
    8445           0 :     Point aPos( rPos );
    8446           0 :     TextAlign eAlign = m_aCurrentPDFState.m_aFont.GetAlign();
    8447           0 :     if( eAlign == ALIGN_TOP )
    8448           0 :         aPos.Y() += HCONV( pFontEntry->maMetric.mnAscent );
    8449           0 :     else if( eAlign == ALIGN_BOTTOM )
    8450           0 :         aPos.Y() -= HCONV( pFontEntry->maMetric.mnDescent );
    8451             : 
    8452           0 :     OStringBuffer aLine( 512 );
    8453             :     // save GS
    8454           0 :     aLine.append( "q " );
    8455             : 
    8456             :     // rotate and translate matrix
    8457           0 :     double fAngle = (double)m_aCurrentPDFState.m_aFont.GetOrientation() * M_PI / 1800.0;
    8458           0 :     Matrix3 aMat;
    8459           0 :     aMat.rotate( fAngle );
    8460           0 :     aMat.translate( aPos.X(), aPos.Y() );
    8461           0 :     aMat.append( m_aPages.back(), aLine );
    8462           0 :     aLine.append( " cm\n" );
    8463             : 
    8464           0 :     if ( aUnderlineColor.GetTransparency() != 0 )
    8465           0 :         aUnderlineColor = aStrikeoutColor;
    8466             : 
    8467           0 :     if ( (eUnderline == UNDERLINE_SMALLWAVE) ||
    8468           0 :          (eUnderline == UNDERLINE_WAVE) ||
    8469           0 :          (eUnderline == UNDERLINE_DOUBLEWAVE) ||
    8470             :          (eUnderline == UNDERLINE_BOLDWAVE) )
    8471             :     {
    8472           0 :         drawWaveTextLine( aLine, nWidth, eUnderline, aUnderlineColor, bUnderlineAbove );
    8473           0 :         bUnderlineDone = true;
    8474             :     }
    8475             : 
    8476           0 :     if ( (eOverline == UNDERLINE_SMALLWAVE) ||
    8477           0 :          (eOverline == UNDERLINE_WAVE) ||
    8478           0 :          (eOverline == UNDERLINE_DOUBLEWAVE) ||
    8479             :          (eOverline == UNDERLINE_BOLDWAVE) )
    8480             :     {
    8481           0 :         drawWaveTextLine( aLine, nWidth, eOverline, aOverlineColor, true );
    8482           0 :         bOverlineDone = true;
    8483             :     }
    8484             : 
    8485           0 :     if ( !bUnderlineDone )
    8486             :     {
    8487           0 :         drawStraightTextLine( aLine, nWidth, eUnderline, aUnderlineColor, bUnderlineAbove );
    8488             :     }
    8489             : 
    8490           0 :     if ( !bOverlineDone )
    8491             :     {
    8492           0 :         drawStraightTextLine( aLine, nWidth, eOverline, aOverlineColor, true );
    8493             :     }
    8494             : 
    8495           0 :     if ( !bStrikeoutDone )
    8496             :     {
    8497           0 :         drawStrikeoutLine( aLine, nWidth, eStrikeout, aStrikeoutColor );
    8498             :     }
    8499             : 
    8500           0 :     aLine.append( "Q\n" );
    8501           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8502             : }
    8503             : 
    8504           0 : void PDFWriterImpl::drawPolygon( const Polygon& rPoly )
    8505             : {
    8506           0 :     MARK( "drawPolygon" );
    8507             : 
    8508           0 :     updateGraphicsState();
    8509             : 
    8510           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8511           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8512           0 :         return;
    8513             : 
    8514           0 :     int nPoints = rPoly.GetSize();
    8515           0 :     OStringBuffer aLine( 20 * nPoints );
    8516           0 :     m_aPages.back().appendPolygon( rPoly, aLine );
    8517           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8518           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8519           0 :         aLine.append( "B*\n" );
    8520           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8521           0 :         aLine.append( "S\n" );
    8522             :     else
    8523           0 :         aLine.append( "f*\n" );
    8524             : 
    8525           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8526             : }
    8527             : 
    8528           0 : void PDFWriterImpl::drawPolyPolygon( const PolyPolygon& rPolyPoly )
    8529             : {
    8530           0 :     MARK( "drawPolyPolygon" );
    8531             : 
    8532           0 :     updateGraphicsState();
    8533             : 
    8534           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8535           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8536           0 :         return;
    8537             : 
    8538           0 :     int nPolygons = rPolyPoly.Count();
    8539             : 
    8540           0 :     OStringBuffer aLine( 40 * nPolygons );
    8541           0 :     m_aPages.back().appendPolyPolygon( rPolyPoly, aLine );
    8542           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8543           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8544           0 :         aLine.append( "B*\n" );
    8545           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8546           0 :         aLine.append( "S\n" );
    8547             :     else
    8548           0 :         aLine.append( "f*\n" );
    8549             : 
    8550           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8551             : }
    8552             : 
    8553           0 : void PDFWriterImpl::drawTransparent( const PolyPolygon& rPolyPoly, sal_uInt32 nTransparentPercent )
    8554             : {
    8555             :     DBG_ASSERT( nTransparentPercent <= 100, "invalid alpha value" );
    8556           0 :     nTransparentPercent = nTransparentPercent % 100;
    8557             : 
    8558           0 :     MARK( "drawTransparent" );
    8559             : 
    8560           0 :     updateGraphicsState();
    8561             : 
    8562           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8563           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8564           0 :         return;
    8565             : 
    8566           0 :     if( m_bIsPDF_A1 || m_aContext.Version < PDFWriter::PDF_1_4 )
    8567             :     {
    8568             :         m_aErrors.insert( m_bIsPDF_A1 ?
    8569             :                           PDFWriter::Warning_Transparency_Omitted_PDFA :
    8570           0 :                           PDFWriter::Warning_Transparency_Omitted_PDF13 );
    8571             : 
    8572           0 :         drawPolyPolygon( rPolyPoly );
    8573           0 :         return;
    8574             :     }
    8575             : 
    8576             :     // create XObject
    8577           0 :     m_aTransparentObjects.push_back( TransparencyEmit() );
    8578             :     // FIXME: polygons with beziers may yield incorrect bound rect
    8579           0 :     m_aTransparentObjects.back().m_aBoundRect     = rPolyPoly.GetBoundRect();
    8580             :     // convert rectangle to default user space
    8581           0 :     m_aPages.back().convertRect( m_aTransparentObjects.back().m_aBoundRect );
    8582           0 :     m_aTransparentObjects.back().m_nObject          = createObject();
    8583           0 :     m_aTransparentObjects.back().m_nExtGStateObject = createObject();
    8584           0 :     m_aTransparentObjects.back().m_fAlpha           = (double)(100-nTransparentPercent) / 100.0;
    8585           0 :     m_aTransparentObjects.back().m_pContentStream   = new SvMemoryStream( 256, 256 );
    8586             :     // create XObject's content stream
    8587           0 :     OStringBuffer aContent( 256 );
    8588           0 :     m_aPages.back().appendPolyPolygon( rPolyPoly, aContent );
    8589           0 :     if( m_aCurrentPDFState.m_aLineColor != Color( COL_TRANSPARENT ) &&
    8590           0 :         m_aCurrentPDFState.m_aFillColor != Color( COL_TRANSPARENT ) )
    8591           0 :         aContent.append( " B*\n" );
    8592           0 :     else if( m_aCurrentPDFState.m_aLineColor != Color( COL_TRANSPARENT ) )
    8593           0 :         aContent.append( " S\n" );
    8594             :     else
    8595           0 :         aContent.append( " f*\n" );
    8596           0 :     m_aTransparentObjects.back().m_pContentStream->Write( aContent.getStr(), aContent.getLength() );
    8597             : 
    8598           0 :     OStringBuffer aObjName( 16 );
    8599           0 :     aObjName.append( "Tr" );
    8600           0 :     aObjName.append( m_aTransparentObjects.back().m_nObject );
    8601           0 :     OString aTrName( aObjName.makeStringAndClear() );
    8602           0 :     aObjName.append( "EGS" );
    8603           0 :     aObjName.append( m_aTransparentObjects.back().m_nExtGStateObject );
    8604           0 :     OString aExtName( aObjName.makeStringAndClear() );
    8605             : 
    8606           0 :     OStringBuffer aLine( 80 );
    8607             :     // insert XObject
    8608           0 :     aLine.append( "q /" );
    8609           0 :     aLine.append( aExtName );
    8610           0 :     aLine.append( " gs /" );
    8611           0 :     aLine.append( aTrName );
    8612           0 :     aLine.append( " Do Q\n" );
    8613           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8614             : 
    8615           0 :     pushResource( ResXObject, aTrName, m_aTransparentObjects.back().m_nObject );
    8616           0 :     pushResource( ResExtGState, aExtName, m_aTransparentObjects.back().m_nExtGStateObject );
    8617             : }
    8618             : 
    8619           0 : void PDFWriterImpl::pushResource( ResourceKind eKind, const OString& rResource, sal_Int32 nObject )
    8620             : {
    8621           0 :     if( nObject >= 0 )
    8622             :     {
    8623           0 :         switch( eKind )
    8624             :         {
    8625             :             case ResXObject:
    8626           0 :                 m_aGlobalResourceDict.m_aXObjects[ rResource ] = nObject;
    8627           0 :                 if( ! m_aOutputStreams.empty() )
    8628           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aXObjects[ rResource ] = nObject;
    8629           0 :                 break;
    8630             :             case ResExtGState:
    8631           0 :                 m_aGlobalResourceDict.m_aExtGStates[ rResource ] = nObject;
    8632           0 :                 if( ! m_aOutputStreams.empty() )
    8633           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aExtGStates[ rResource ] = nObject;
    8634           0 :                 break;
    8635             :             case ResShading:
    8636           0 :                 m_aGlobalResourceDict.m_aShadings[ rResource ] = nObject;
    8637           0 :                 if( ! m_aOutputStreams.empty() )
    8638           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aShadings[ rResource ] = nObject;
    8639           0 :                 break;
    8640             :             case ResPattern:
    8641           0 :                 m_aGlobalResourceDict.m_aPatterns[ rResource ] = nObject;
    8642           0 :                 if( ! m_aOutputStreams.empty() )
    8643           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aPatterns[ rResource ] = nObject;
    8644           0 :                 break;
    8645             :         }
    8646             :     }
    8647           0 : }
    8648             : 
    8649           0 : void PDFWriterImpl::beginRedirect( SvStream* pStream, const Rectangle& rTargetRect )
    8650             : {
    8651           0 :     push( PUSH_ALL );
    8652             : 
    8653             :     // force reemitting clip region
    8654           0 :     clearClipRegion();
    8655           0 :     updateGraphicsState();
    8656             : 
    8657           0 :     m_aOutputStreams.push_front( StreamRedirect() );
    8658           0 :     m_aOutputStreams.front().m_pStream = pStream;
    8659           0 :     m_aOutputStreams.front().m_aMapMode = m_aMapMode;
    8660             : 
    8661           0 :     if( !rTargetRect.IsEmpty() )
    8662             :     {
    8663           0 :         m_aOutputStreams.front().m_aTargetRect =
    8664           0 :             lcl_convert( m_aGraphicsStack.front().m_aMapMode,
    8665             :                          m_aMapMode,
    8666             :                          getReferenceDevice(),
    8667           0 :                          rTargetRect );
    8668           0 :         Point aDelta = m_aOutputStreams.front().m_aTargetRect.BottomLeft();
    8669           0 :         long nPageHeight = pointToPixel(m_aPages[m_nCurrentPage].getHeight());
    8670           0 :         aDelta.Y() = -(nPageHeight - m_aOutputStreams.front().m_aTargetRect.Bottom());
    8671           0 :         m_aMapMode.SetOrigin( m_aMapMode.GetOrigin() + aDelta );
    8672             :     }
    8673             : 
    8674             :     // setup graphics state for independent object stream
    8675             : 
    8676             :     // force reemitting colors
    8677           0 :     m_aCurrentPDFState.m_aLineColor = Color( COL_TRANSPARENT );
    8678           0 :     m_aCurrentPDFState.m_aFillColor = Color( COL_TRANSPARENT );
    8679           0 : }
    8680             : 
    8681           0 : SvStream* PDFWriterImpl::endRedirect()
    8682             : {
    8683           0 :     SvStream* pStream = NULL;
    8684           0 :     if( ! m_aOutputStreams.empty() )
    8685             :     {
    8686           0 :         pStream     = m_aOutputStreams.front().m_pStream;
    8687           0 :         m_aMapMode  = m_aOutputStreams.front().m_aMapMode;
    8688           0 :         m_aOutputStreams.pop_front();
    8689             :     }
    8690             : 
    8691           0 :     pop();
    8692             :     // force reemitting colors and clip region
    8693           0 :     clearClipRegion();
    8694           0 :     m_aCurrentPDFState.m_bClipRegion = m_aGraphicsStack.front().m_bClipRegion;
    8695           0 :     m_aCurrentPDFState.m_aClipRegion = m_aGraphicsStack.front().m_aClipRegion;
    8696           0 :     m_aCurrentPDFState.m_aLineColor = Color( COL_TRANSPARENT );
    8697           0 :     m_aCurrentPDFState.m_aFillColor = Color( COL_TRANSPARENT );
    8698             : 
    8699           0 :     updateGraphicsState();
    8700             : 
    8701           0 :     return pStream;
    8702             : }
    8703             : 
    8704           0 : void PDFWriterImpl::beginTransparencyGroup()
    8705             : {
    8706           0 :     updateGraphicsState();
    8707           0 :     if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    8708           0 :         beginRedirect( new SvMemoryStream( 1024, 1024 ), Rectangle() );
    8709           0 : }
    8710             : 
    8711           0 : void PDFWriterImpl::endTransparencyGroup( const Rectangle& rBoundingBox, sal_uInt32 nTransparentPercent )
    8712             : {
    8713             :     DBG_ASSERT( nTransparentPercent <= 100, "invalid alpha value" );
    8714           0 :     nTransparentPercent = nTransparentPercent % 100;
    8715             : 
    8716           0 :     if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    8717             :     {
    8718             :         // create XObject
    8719           0 :         m_aTransparentObjects.push_back( TransparencyEmit() );
    8720           0 :         m_aTransparentObjects.back().m_aBoundRect   = rBoundingBox;
    8721             :         // convert rectangle to default user space
    8722           0 :         m_aPages.back().convertRect( m_aTransparentObjects.back().m_aBoundRect );
    8723           0 :         m_aTransparentObjects.back().m_nObject      = createObject();
    8724           0 :         m_aTransparentObjects.back().m_fAlpha       = (double)(100-nTransparentPercent) / 100.0;
    8725             :         // get XObject's content stream
    8726           0 :         m_aTransparentObjects.back().m_pContentStream = static_cast<SvMemoryStream*>(endRedirect());
    8727           0 :         m_aTransparentObjects.back().m_nExtGStateObject = createObject();
    8728             : 
    8729           0 :         OStringBuffer aObjName( 16 );
    8730           0 :         aObjName.append( "Tr" );
    8731           0 :         aObjName.append( m_aTransparentObjects.back().m_nObject );
    8732           0 :         OString aTrName( aObjName.makeStringAndClear() );
    8733           0 :         aObjName.append( "EGS" );
    8734           0 :         aObjName.append( m_aTransparentObjects.back().m_nExtGStateObject );
    8735           0 :         OString aExtName( aObjName.makeStringAndClear() );
    8736             : 
    8737           0 :         OStringBuffer aLine( 80 );
    8738             :         // insert XObject
    8739           0 :         aLine.append( "q /" );
    8740           0 :         aLine.append( aExtName );
    8741           0 :         aLine.append( " gs /" );
    8742           0 :         aLine.append( aTrName );
    8743           0 :         aLine.append( " Do Q\n" );
    8744           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    8745             : 
    8746           0 :         pushResource( ResXObject, aTrName, m_aTransparentObjects.back().m_nObject );
    8747           0 :         pushResource( ResExtGState, aExtName, m_aTransparentObjects.back().m_nExtGStateObject );
    8748             :     }
    8749           0 : }
    8750             : 
    8751           0 : void PDFWriterImpl::drawRectangle( const Rectangle& rRect )
    8752             : {
    8753           0 :     MARK( "drawRectangle" );
    8754             : 
    8755           0 :     updateGraphicsState();
    8756             : 
    8757           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8758           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8759           0 :         return;
    8760             : 
    8761           0 :     OStringBuffer aLine( 40 );
    8762           0 :     m_aPages.back().appendRect( rRect, aLine );
    8763             : 
    8764           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8765           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8766           0 :         aLine.append( " B*\n" );
    8767           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8768           0 :         aLine.append( " S\n" );
    8769             :     else
    8770           0 :         aLine.append( " f*\n" );
    8771             : 
    8772           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8773             : }
    8774             : 
    8775           0 : void PDFWriterImpl::drawRectangle( const Rectangle& rRect, sal_uInt32 nHorzRound, sal_uInt32 nVertRound )
    8776             : {
    8777           0 :     MARK( "drawRectangle with rounded edges" );
    8778             : 
    8779           0 :     if( !nHorzRound && !nVertRound )
    8780           0 :         drawRectangle( rRect );
    8781             : 
    8782           0 :     updateGraphicsState();
    8783             : 
    8784           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8785           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8786           0 :         return;
    8787             : 
    8788           0 :     if( nHorzRound > (sal_uInt32)rRect.GetWidth()/2 )
    8789           0 :         nHorzRound = rRect.GetWidth()/2;
    8790           0 :     if( nVertRound > (sal_uInt32)rRect.GetHeight()/2 )
    8791           0 :         nVertRound = rRect.GetHeight()/2;
    8792             : 
    8793           0 :     Point aPoints[16];
    8794           0 :     const double kappa = 0.5522847498;
    8795           0 :     const sal_uInt32 kx = (sal_uInt32)((kappa*(double)nHorzRound)+0.5);
    8796           0 :     const sal_uInt32 ky = (sal_uInt32)((kappa*(double)nVertRound)+0.5);
    8797             : 
    8798           0 :     aPoints[1]  = Point( rRect.TopLeft().X() + nHorzRound, rRect.TopLeft().Y() );
    8799           0 :     aPoints[0]  = Point( aPoints[1].X() - kx, aPoints[1].Y() );
    8800           0 :     aPoints[2]  = Point( rRect.TopRight().X()+1 - nHorzRound, aPoints[1].Y() );
    8801           0 :     aPoints[3]  = Point( aPoints[2].X()+kx, aPoints[2].Y() );
    8802             : 
    8803           0 :     aPoints[5]  = Point( rRect.TopRight().X()+1, rRect.TopRight().Y()+nVertRound );
    8804           0 :     aPoints[4]  = Point( aPoints[5].X(), aPoints[5].Y()-ky );
    8805           0 :     aPoints[6]  = Point( aPoints[5].X(), rRect.BottomRight().Y()+1 - nVertRound );
    8806           0 :     aPoints[7]  = Point( aPoints[6].X(), aPoints[6].Y()+ky );
    8807             : 
    8808           0 :     aPoints[9]  = Point( rRect.BottomRight().X()+1-nHorzRound, rRect.BottomRight().Y()+1 );
    8809           0 :     aPoints[8]  = Point( aPoints[9].X()+kx, aPoints[9].Y() );
    8810           0 :     aPoints[10] = Point( rRect.BottomLeft().X() + nHorzRound, aPoints[9].Y() );
    8811           0 :     aPoints[11] = Point( aPoints[10].X()-kx, aPoints[10].Y() );
    8812             : 
    8813           0 :     aPoints[13] = Point( rRect.BottomLeft().X(), rRect.BottomLeft().Y()+1-nVertRound );
    8814           0 :     aPoints[12] = Point( aPoints[13].X(), aPoints[13].Y()+ky );
    8815           0 :     aPoints[14] = Point( rRect.TopLeft().X(), rRect.TopLeft().Y()+nVertRound );
    8816           0 :     aPoints[15] = Point( aPoints[14].X(), aPoints[14].Y()-ky );
    8817             : 
    8818             : 
    8819           0 :     OStringBuffer aLine( 80 );
    8820           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8821           0 :     aLine.append( " m " );
    8822           0 :     m_aPages.back().appendPoint( aPoints[2], aLine );
    8823           0 :     aLine.append( " l " );
    8824           0 :     m_aPages.back().appendPoint( aPoints[3], aLine );
    8825           0 :     aLine.append( ' ' );
    8826           0 :     m_aPages.back().appendPoint( aPoints[4], aLine );
    8827           0 :     aLine.append( ' ' );
    8828           0 :     m_aPages.back().appendPoint( aPoints[5], aLine );
    8829           0 :     aLine.append( " c\n" );
    8830           0 :     m_aPages.back().appendPoint( aPoints[6], aLine );
    8831           0 :     aLine.append( " l " );
    8832           0 :     m_aPages.back().appendPoint( aPoints[7], aLine );
    8833           0 :     aLine.append( ' ' );
    8834           0 :     m_aPages.back().appendPoint( aPoints[8], aLine );
    8835           0 :     aLine.append( ' ' );
    8836           0 :     m_aPages.back().appendPoint( aPoints[9], aLine );
    8837           0 :     aLine.append( " c\n" );
    8838           0 :     m_aPages.back().appendPoint( aPoints[10], aLine );
    8839           0 :     aLine.append( " l " );
    8840           0 :     m_aPages.back().appendPoint( aPoints[11], aLine );
    8841           0 :     aLine.append( ' ' );
    8842           0 :     m_aPages.back().appendPoint( aPoints[12], aLine );
    8843           0 :     aLine.append( ' ' );
    8844           0 :     m_aPages.back().appendPoint( aPoints[13], aLine );
    8845           0 :     aLine.append( " c\n" );
    8846           0 :     m_aPages.back().appendPoint( aPoints[14], aLine );
    8847           0 :     aLine.append( " l " );
    8848           0 :     m_aPages.back().appendPoint( aPoints[15], aLine );
    8849           0 :     aLine.append( ' ' );
    8850           0 :     m_aPages.back().appendPoint( aPoints[0], aLine );
    8851           0 :     aLine.append( ' ' );
    8852           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8853           0 :     aLine.append( " c " );
    8854             : 
    8855           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8856           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8857           0 :         aLine.append( "b*\n" );
    8858           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8859           0 :         aLine.append( "s\n" );
    8860             :     else
    8861           0 :         aLine.append( "f*\n" );
    8862             : 
    8863           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8864             : }
    8865             : 
    8866           0 : void PDFWriterImpl::drawEllipse( const Rectangle& rRect )
    8867             : {
    8868           0 :     MARK( "drawEllipse" );
    8869             : 
    8870           0 :     updateGraphicsState();
    8871             : 
    8872           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8873           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8874           0 :         return;
    8875             : 
    8876           0 :     Point aPoints[12];
    8877           0 :     const double kappa = 0.5522847498;
    8878           0 :     const sal_uInt32 kx = (sal_uInt32)((kappa*(double)rRect.GetWidth()/2.0)+0.5);
    8879           0 :     const sal_uInt32 ky = (sal_uInt32)((kappa*(double)rRect.GetHeight()/2.0)+0.5);
    8880             : 
    8881           0 :     aPoints[1]  = Point( rRect.TopLeft().X() + rRect.GetWidth()/2, rRect.TopLeft().Y() );
    8882           0 :     aPoints[0]  = Point( aPoints[1].X() - kx, aPoints[1].Y() );
    8883           0 :     aPoints[2]  = Point( aPoints[1].X() + kx, aPoints[1].Y() );
    8884             : 
    8885           0 :     aPoints[4]  = Point( rRect.TopRight().X()+1, rRect.TopRight().Y() + rRect.GetHeight()/2 );
    8886           0 :     aPoints[3]  = Point( aPoints[4].X(), aPoints[4].Y() - ky );
    8887           0 :     aPoints[5]  = Point( aPoints[4].X(), aPoints[4].Y() + ky );
    8888             : 
    8889           0 :     aPoints[7]  = Point( rRect.BottomLeft().X() + rRect.GetWidth()/2, rRect.BottomLeft().Y()+1 );
    8890           0 :     aPoints[6]  = Point( aPoints[7].X() + kx, aPoints[7].Y() );
    8891           0 :     aPoints[8]  = Point( aPoints[7].X() - kx, aPoints[7].Y() );
    8892             : 
    8893           0 :     aPoints[10] = Point( rRect.TopLeft().X(), rRect.TopLeft().Y() + rRect.GetHeight()/2 );
    8894           0 :     aPoints[9]  = Point( aPoints[10].X(), aPoints[10].Y() + ky );
    8895           0 :     aPoints[11] = Point( aPoints[10].X(), aPoints[10].Y() - ky );
    8896             : 
    8897           0 :     OStringBuffer aLine( 80 );
    8898           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8899           0 :     aLine.append( " m " );
    8900           0 :     m_aPages.back().appendPoint( aPoints[2], aLine );
    8901           0 :     aLine.append( ' ' );
    8902           0 :     m_aPages.back().appendPoint( aPoints[3], aLine );
    8903           0 :     aLine.append( ' ' );
    8904           0 :     m_aPages.back().appendPoint( aPoints[4], aLine );
    8905           0 :     aLine.append( " c\n" );
    8906           0 :     m_aPages.back().appendPoint( aPoints[5], aLine );
    8907           0 :     aLine.append( ' ' );
    8908           0 :     m_aPages.back().appendPoint( aPoints[6], aLine );
    8909           0 :     aLine.append( ' ' );
    8910           0 :     m_aPages.back().appendPoint( aPoints[7], aLine );
    8911           0 :     aLine.append( " c\n" );
    8912           0 :     m_aPages.back().appendPoint( aPoints[8], aLine );
    8913           0 :     aLine.append( ' ' );
    8914           0 :     m_aPages.back().appendPoint( aPoints[9], aLine );
    8915           0 :     aLine.append( ' ' );
    8916           0 :     m_aPages.back().appendPoint( aPoints[10], aLine );
    8917           0 :     aLine.append( " c\n" );
    8918           0 :     m_aPages.back().appendPoint( aPoints[11], aLine );
    8919           0 :     aLine.append( ' ' );
    8920           0 :     m_aPages.back().appendPoint( aPoints[0], aLine );
    8921           0 :     aLine.append( ' ' );
    8922           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8923           0 :     aLine.append( " c " );
    8924             : 
    8925           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8926           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8927           0 :         aLine.append( "b*\n" );
    8928           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8929           0 :         aLine.append( "s\n" );
    8930             :     else
    8931           0 :         aLine.append( "f*\n" );
    8932             : 
    8933           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8934             : }
    8935             : 
    8936           0 : static double calcAngle( const Rectangle& rRect, const Point& rPoint )
    8937             : {
    8938           0 :     Point aOrigin((rRect.Left()+rRect.Right()+1)/2,
    8939           0 :                   (rRect.Top()+rRect.Bottom()+1)/2);
    8940           0 :     Point aPoint = rPoint - aOrigin;
    8941             : 
    8942           0 :     double fX = (double)aPoint.X();
    8943           0 :     double fY = (double)-aPoint.Y();
    8944             : 
    8945           0 :     if( rRect.GetWidth() > rRect.GetHeight() )
    8946           0 :         fY = fY*((double)rRect.GetWidth()/(double)rRect.GetHeight());
    8947           0 :     else if( rRect.GetHeight() > rRect.GetWidth() )
    8948           0 :         fX = fX*((double)rRect.GetHeight()/(double)rRect.GetWidth());
    8949           0 :     return atan2( fY, fX );
    8950             : }
    8951             : 
    8952           0 : void PDFWriterImpl::drawArc( const Rectangle& rRect, const Point& rStart, const Point& rStop, bool bWithPie, bool bWithChord )
    8953             : {
    8954           0 :     MARK( "drawArc" );
    8955             : 
    8956           0 :     updateGraphicsState();
    8957             : 
    8958           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8959           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8960           0 :         return;
    8961             : 
    8962             :     // calculate start and stop angles
    8963           0 :     const double fStartAngle = calcAngle( rRect, rStart );
    8964           0 :     double fStopAngle  = calcAngle( rRect, rStop );
    8965           0 :     while( fStopAngle < fStartAngle )
    8966           0 :         fStopAngle += 2.0*M_PI;
    8967           0 :     const int nFragments = (int)((fStopAngle-fStartAngle)/(M_PI/2.0))+1;
    8968           0 :     const double fFragmentDelta = (fStopAngle-fStartAngle)/(double)nFragments;
    8969           0 :     const double kappa = fabs( 4.0 * (1.0-cos(fFragmentDelta/2.0))/sin(fFragmentDelta/2.0) / 3.0);
    8970           0 :     const double halfWidth = (double)rRect.GetWidth()/2.0;
    8971           0 :     const double halfHeight = (double)rRect.GetHeight()/2.0;
    8972             : 
    8973           0 :     const Point aCenter( (rRect.Left()+rRect.Right()+1)/2,
    8974           0 :                          (rRect.Top()+rRect.Bottom()+1)/2 );
    8975             : 
    8976           0 :     OStringBuffer aLine( 30*nFragments );
    8977           0 :     Point aPoint( (int)(halfWidth * cos(fStartAngle) ),
    8978           0 :                   -(int)(halfHeight * sin(fStartAngle) ) );
    8979           0 :     aPoint += aCenter;
    8980           0 :     m_aPages.back().appendPoint( aPoint, aLine );
    8981           0 :     aLine.append( " m " );
    8982           0 :     if( !basegfx::fTools::equal(fStartAngle, fStopAngle) )
    8983             :     {
    8984           0 :         for( int i = 0; i < nFragments; i++ )
    8985             :         {
    8986           0 :             const double fStartFragment = fStartAngle + (double)i*fFragmentDelta;
    8987           0 :             const double fStopFragment = fStartFragment + fFragmentDelta;
    8988           0 :             aPoint = Point( (int)(halfWidth * (cos(fStartFragment) - kappa*sin(fStartFragment) ) ),
    8989           0 :                             -(int)(halfHeight * (sin(fStartFragment) + kappa*cos(fStartFragment) ) ) );
    8990           0 :             aPoint += aCenter;
    8991           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    8992           0 :             aLine.append( ' ' );
    8993             : 
    8994           0 :             aPoint = Point( (int)(halfWidth * (cos(fStopFragment) + kappa*sin(fStopFragment) ) ),
    8995           0 :                             -(int)(halfHeight * (sin(fStopFragment) - kappa*cos(fStopFragment) ) ) );
    8996           0 :             aPoint += aCenter;
    8997           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    8998           0 :             aLine.append( ' ' );
    8999             : 
    9000           0 :             aPoint = Point( (int)(halfWidth * cos(fStopFragment) ),
    9001           0 :                             -(int)(halfHeight * sin(fStopFragment) ) );
    9002           0 :             aPoint += aCenter;
    9003           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9004           0 :             aLine.append( " c\n" );
    9005             :         }
    9006             :     }
    9007           0 :     if( bWithChord || bWithPie )
    9008             :     {
    9009           0 :         if( bWithPie )
    9010             :         {
    9011           0 :             m_aPages.back().appendPoint( aCenter, aLine );
    9012           0 :             aLine.append( " l " );
    9013             :         }
    9014           0 :         aLine.append( "h " );
    9015             :     }
    9016           0 :     if( ! bWithChord && ! bWithPie )
    9017           0 :         aLine.append( "S\n" );
    9018           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    9019           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    9020           0 :         aLine.append( "B*\n" );
    9021           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    9022           0 :         aLine.append( "S\n" );
    9023             :     else
    9024           0 :         aLine.append( "f*\n" );
    9025             : 
    9026           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9027             : }
    9028             : 
    9029           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly )
    9030             : {
    9031           0 :     MARK( "drawPolyLine" );
    9032             : 
    9033           0 :     sal_uInt16 nPoints = rPoly.GetSize();
    9034           0 :     if( nPoints < 2 )
    9035           0 :         return;
    9036             : 
    9037           0 :     updateGraphicsState();
    9038             : 
    9039           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9040           0 :         return;
    9041             : 
    9042           0 :     OStringBuffer aLine( 20 * nPoints );
    9043           0 :     m_aPages.back().appendPolygon( rPoly, aLine, rPoly[0] == rPoly[nPoints-1] );
    9044           0 :     aLine.append( "S\n" );
    9045             : 
    9046           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9047             : }
    9048             : 
    9049           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly, const LineInfo& rInfo )
    9050             : {
    9051           0 :     MARK( "drawPolyLine with LineInfo" );
    9052             : 
    9053           0 :     updateGraphicsState();
    9054             : 
    9055           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9056           0 :         return;
    9057             : 
    9058           0 :     OStringBuffer aLine;
    9059           0 :     aLine.append( "q " );
    9060           0 :     if( m_aPages.back().appendLineInfo( rInfo, aLine ) )
    9061             :     {
    9062           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9063           0 :         drawPolyLine( rPoly );
    9064           0 :         writeBuffer( "Q\n", 2 );
    9065             :     }
    9066             :     else
    9067             :     {
    9068           0 :         PDFWriter::ExtLineInfo aInfo;
    9069           0 :         convertLineInfoToExtLineInfo( rInfo, aInfo );
    9070           0 :         drawPolyLine( rPoly, aInfo );
    9071           0 :     }
    9072             : }
    9073             : 
    9074           0 : void PDFWriterImpl::convertLineInfoToExtLineInfo( const LineInfo& rIn, PDFWriter::ExtLineInfo& rOut )
    9075             : {
    9076             :     DBG_ASSERT( rIn.GetStyle() == LINE_DASH, "invalid conversion" );
    9077           0 :     rOut.m_fLineWidth           = rIn.GetWidth();
    9078           0 :     rOut.m_fTransparency        = 0.0;
    9079           0 :     rOut.m_eCap                 = PDFWriter::capButt;
    9080           0 :     rOut.m_eJoin                = PDFWriter::joinMiter;
    9081           0 :     rOut.m_fMiterLimit          = 10;
    9082           0 :     rOut.m_aDashArray.clear();
    9083             : 
    9084             :     // add DashDot to DashArray
    9085           0 :     const int nDashes   = rIn.GetDashCount();
    9086           0 :     const int nDashLen  = rIn.GetDashLen();
    9087           0 :     const int nDistance = rIn.GetDistance();
    9088             : 
    9089           0 :     for( int n  = 0; n < nDashes; n++ )
    9090             :     {
    9091           0 :         rOut.m_aDashArray.push_back( nDashLen );
    9092           0 :         rOut.m_aDashArray.push_back( nDistance );
    9093             :     }
    9094           0 :     const int nDots   = rIn.GetDotCount();
    9095           0 :     const int nDotLen = rIn.GetDotLen();
    9096             : 
    9097           0 :     for( int n  = 0; n < nDots; n++ )
    9098             :     {
    9099           0 :         rOut.m_aDashArray.push_back( nDotLen );
    9100           0 :         rOut.m_aDashArray.push_back( nDistance );
    9101             :     }
    9102             : 
    9103             :     // add LineJoin
    9104           0 :     switch(rIn.GetLineJoin())
    9105             :     {
    9106             :         case basegfx::B2DLINEJOIN_BEVEL :
    9107             :         {
    9108           0 :             rOut.m_eJoin = PDFWriter::joinBevel;
    9109           0 :             break;
    9110             :         }
    9111             :         default : // basegfx::B2DLINEJOIN_NONE :
    9112             :         // Pdf has no 'none' lineJoin, default is miter
    9113             :         case basegfx::B2DLINEJOIN_MIDDLE :
    9114             :         case basegfx::B2DLINEJOIN_MITER :
    9115             :         {
    9116           0 :             rOut.m_eJoin = PDFWriter::joinMiter;
    9117           0 :             break;
    9118             :         }
    9119             :         case basegfx::B2DLINEJOIN_ROUND :
    9120             :         {
    9121           0 :             rOut.m_eJoin = PDFWriter::joinRound;
    9122           0 :             break;
    9123             :         }
    9124             :     }
    9125             : 
    9126             :     // add LineCap
    9127           0 :     switch(rIn.GetLineCap())
    9128             :     {
    9129             :         default: /* com::sun::star::drawing::LineCap_BUTT */
    9130             :         {
    9131           0 :             rOut.m_eCap = PDFWriter::capButt;
    9132           0 :             break;
    9133             :         }
    9134             :         case com::sun::star::drawing::LineCap_ROUND:
    9135             :         {
    9136           0 :             rOut.m_eCap = PDFWriter::capRound;
    9137           0 :             break;
    9138             :         }
    9139             :         case com::sun::star::drawing::LineCap_SQUARE:
    9140             :         {
    9141           0 :             rOut.m_eCap = PDFWriter::capSquare;
    9142           0 :             break;
    9143             :         }
    9144             :     }
    9145           0 : }
    9146             : 
    9147           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly, const PDFWriter::ExtLineInfo& rInfo )
    9148             : {
    9149           0 :     MARK( "drawPolyLine with ExtLineInfo" );
    9150             : 
    9151           0 :     updateGraphicsState();
    9152             : 
    9153           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9154           0 :         return;
    9155             : 
    9156           0 :     if( rInfo.m_fTransparency >= 1.0 )
    9157           0 :         return;
    9158             : 
    9159           0 :     if( rInfo.m_fTransparency != 0.0 )
    9160           0 :         beginTransparencyGroup();
    9161             : 
    9162           0 :     OStringBuffer aLine;
    9163           0 :     aLine.append( "q " );
    9164           0 :     m_aPages.back().appendMappedLength( rInfo.m_fLineWidth, aLine );
    9165           0 :     aLine.append( " w" );
    9166           0 :     if( rInfo.m_aDashArray.size() < 10 ) // implmentation limit of acrobat reader
    9167             :     {
    9168           0 :         switch( rInfo.m_eCap )
    9169             :         {
    9170             :             default:
    9171           0 :             case PDFWriter::capButt:   aLine.append( " 0 J" );break;
    9172           0 :             case PDFWriter::capRound:  aLine.append( " 1 J" );break;
    9173           0 :             case PDFWriter::capSquare: aLine.append( " 2 J" );break;
    9174             :         }
    9175           0 :         switch( rInfo.m_eJoin )
    9176             :         {
    9177             :             default:
    9178             :             case PDFWriter::joinMiter:
    9179             :             {
    9180           0 :                 double fLimit = rInfo.m_fMiterLimit;
    9181           0 :                 if( rInfo.m_fLineWidth < rInfo.m_fMiterLimit )
    9182           0 :                     fLimit = fLimit / rInfo.m_fLineWidth;
    9183           0 :                 if( fLimit < 1.0 )
    9184           0 :                     fLimit = 1.0;
    9185           0 :                 aLine.append( " 0 j " );
    9186           0 :                 appendDouble( fLimit, aLine );
    9187           0 :                 aLine.append( " M" );
    9188             :             }
    9189           0 :             break;
    9190           0 :             case PDFWriter::joinRound:  aLine.append( " 1 j" );break;
    9191           0 :             case PDFWriter::joinBevel:  aLine.append( " 2 j" );break;
    9192             :         }
    9193           0 :         if( rInfo.m_aDashArray.size() > 0 )
    9194             :         {
    9195           0 :             aLine.append( " [ " );
    9196           0 :             for( std::vector<double>::const_iterator it = rInfo.m_aDashArray.begin();
    9197           0 :                  it != rInfo.m_aDashArray.end(); ++it )
    9198             :             {
    9199           0 :                 m_aPages.back().appendMappedLength( *it, aLine );
    9200           0 :                 aLine.append( ' ' );
    9201             :             }
    9202           0 :             aLine.append( "] 0 d" );
    9203             :         }
    9204           0 :         aLine.append( "\n" );
    9205           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9206           0 :         drawPolyLine( rPoly );
    9207             :     }
    9208             :     else
    9209             :     {
    9210           0 :         basegfx::B2DPolygon aPoly(rPoly.getB2DPolygon());
    9211           0 :         basegfx::B2DPolyPolygon aPolyPoly;
    9212             : 
    9213           0 :         basegfx::tools::applyLineDashing(aPoly, rInfo.m_aDashArray, &aPolyPoly);
    9214             : 
    9215             :         // Old applyLineDashing subdivided the polygon. New one will create bezier curve segments.
    9216             :         // To mimic old behaviour, apply subdivide here. If beziers shall be written (better quality)
    9217             :         // this line needs to be removed and the loop below adapted accordingly
    9218           0 :         aPolyPoly = basegfx::tools::adaptiveSubdivideByAngle(aPolyPoly);
    9219             : 
    9220           0 :         const sal_uInt32 nPolygonCount(aPolyPoly.count());
    9221             : 
    9222           0 :         for( sal_uInt32 nPoly = 0; nPoly < nPolygonCount; nPoly++ )
    9223             :         {
    9224           0 :             aLine.append( (nPoly != 0 && (nPoly & 7) == 0) ? "\n" : " " );
    9225           0 :             aPoly = aPolyPoly.getB2DPolygon( nPoly );
    9226           0 :             const sal_uInt32 nPointCount(aPoly.count());
    9227             : 
    9228           0 :             if(nPointCount)
    9229             :             {
    9230           0 :                 const sal_uInt32 nEdgeCount(aPoly.isClosed() ? nPointCount : nPointCount - 1);
    9231           0 :                 basegfx::B2DPoint aCurrent(aPoly.getB2DPoint(0));
    9232             : 
    9233           0 :                 for(sal_uInt32 a(0); a < nEdgeCount; a++)
    9234             :                 {
    9235           0 :                     if( a > 0 )
    9236           0 :                         aLine.append( " " );
    9237           0 :                     const sal_uInt32 nNextIndex((a + 1) % nPointCount);
    9238           0 :                     const basegfx::B2DPoint aNext(aPoly.getB2DPoint(nNextIndex));
    9239             : 
    9240           0 :                     m_aPages.back().appendPoint( Point( FRound(aCurrent.getX()),
    9241             :                                                         FRound(aCurrent.getY()) ),
    9242           0 :                                                  aLine );
    9243           0 :                     aLine.append( " m " );
    9244           0 :                     m_aPages.back().appendPoint( Point( FRound(aNext.getX()),
    9245             :                                                         FRound(aNext.getY()) ),
    9246           0 :                                                  aLine );
    9247           0 :                     aLine.append( " l" );
    9248             : 
    9249             :                     // prepare next edge
    9250           0 :                     aCurrent = aNext;
    9251           0 :                 }
    9252             :             }
    9253             :         }
    9254           0 :         aLine.append( " S " );
    9255           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9256             :     }
    9257           0 :     writeBuffer( "Q\n", 2 );
    9258             : 
    9259           0 :     if( rInfo.m_fTransparency != 0.0 )
    9260             :     {
    9261             :         // FIXME: actually this may be incorrect with bezier polygons
    9262           0 :         Rectangle aBoundRect( rPoly.GetBoundRect() );
    9263             :         // avoid clipping with thick lines
    9264           0 :         if( rInfo.m_fLineWidth > 0.0 )
    9265             :         {
    9266           0 :             sal_Int32 nLW = sal_Int32(rInfo.m_fLineWidth);
    9267           0 :             aBoundRect.Top()    -= nLW;
    9268           0 :             aBoundRect.Left()   -= nLW;
    9269           0 :             aBoundRect.Right()  += nLW;
    9270           0 :             aBoundRect.Bottom() += nLW;
    9271             :         }
    9272           0 :         endTransparencyGroup( aBoundRect, (sal_uInt16)(100.0*rInfo.m_fTransparency) );
    9273           0 :     }
    9274             : }
    9275             : 
    9276           0 : void PDFWriterImpl::drawPixel( const Point& rPoint, const Color& rColor )
    9277             : {
    9278           0 :     MARK( "drawPixel" );
    9279             : 
    9280           0 :     Color aColor = ( rColor == Color( COL_TRANSPARENT ) ? m_aGraphicsStack.front().m_aLineColor : rColor );
    9281             : 
    9282           0 :     if( aColor == Color( COL_TRANSPARENT ) )
    9283           0 :         return;
    9284             : 
    9285             :     // pixels are drawn in line color, so have to set
    9286             :     // the nonstroking color to line color
    9287           0 :     Color aOldFillColor = m_aGraphicsStack.front().m_aFillColor;
    9288           0 :     setFillColor( aColor );
    9289             : 
    9290           0 :     updateGraphicsState();
    9291             : 
    9292           0 :     OStringBuffer aLine( 20 );
    9293           0 :     m_aPages.back().appendPoint( rPoint, aLine );
    9294           0 :     aLine.append( ' ' );
    9295           0 :     appendDouble( 1.0/double(getReferenceDevice()->ImplGetDPIX()), aLine );
    9296           0 :     aLine.append( ' ' );
    9297           0 :     appendDouble( 1.0/double(getReferenceDevice()->ImplGetDPIY()), aLine );
    9298           0 :     aLine.append( " re f\n" );
    9299           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9300             : 
    9301           0 :     setFillColor( aOldFillColor );
    9302             : }
    9303             : 
    9304             : class AccessReleaser
    9305             : {
    9306             :     BitmapReadAccess* m_pAccess;
    9307             : public:
    9308           0 :     AccessReleaser( BitmapReadAccess* pAccess ) : m_pAccess( pAccess ){}
    9309           0 :     ~AccessReleaser() { delete m_pAccess; }
    9310             : };
    9311             : 
    9312           0 : bool PDFWriterImpl::writeTransparentObject( TransparencyEmit& rObject )
    9313             : {
    9314           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9315             : 
    9316           0 :     bool bFlateFilter = compressStream( rObject.m_pContentStream );
    9317           0 :     rObject.m_pContentStream->Seek( STREAM_SEEK_TO_END );
    9318           0 :     sal_uLong nSize = rObject.m_pContentStream->Tell();
    9319           0 :     rObject.m_pContentStream->Seek( STREAM_SEEK_TO_BEGIN );
    9320             :     #if OSL_DEBUG_LEVEL > 1
    9321             :     emitComment( "PDFWriterImpl::writeTransparentObject" );
    9322             :     #endif
    9323           0 :     OStringBuffer aLine( 512 );
    9324           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9325           0 :     aLine.append( rObject.m_nObject );
    9326             :     aLine.append( " 0 obj\n"
    9327             :                   "<</Type/XObject\n"
    9328             :                   "/Subtype/Form\n"
    9329           0 :                   "/BBox[ " );
    9330           0 :     appendFixedInt( rObject.m_aBoundRect.Left(), aLine );
    9331           0 :     aLine.append( ' ' );
    9332           0 :     appendFixedInt( rObject.m_aBoundRect.Top(), aLine );
    9333           0 :     aLine.append( ' ' );
    9334           0 :     appendFixedInt( rObject.m_aBoundRect.Right(), aLine );
    9335           0 :     aLine.append( ' ' );
    9336           0 :     appendFixedInt( rObject.m_aBoundRect.Bottom()+1, aLine );
    9337           0 :     aLine.append( " ]\n" );
    9338           0 :     if( ! rObject.m_pSoftMaskStream )
    9339             :     {
    9340           0 :         if( ! m_bIsPDF_A1 )
    9341             :         {
    9342           0 :             aLine.append( "/Group<</S/Transparency/CS/DeviceRGB/K true>>\n" );
    9343             :         }
    9344             :     }
    9345             :     /* #i42884# the PDF reference recommends that each Form XObject
    9346             :     *  should have a resource dict; alas if that is the same object
    9347             :     *  as the one of the page it triggers an endless recursion in
    9348             :     *  acroread 5 (6 and up have that fixed). Since we have only one
    9349             :     *  resource dict anyway, let's use the one from the page by NOT
    9350             :     *  emitting a Resources entry.
    9351             :     */
    9352             : 
    9353           0 :     aLine.append( "/Length " );
    9354           0 :     aLine.append( (sal_Int32)(nSize) );
    9355           0 :     aLine.append( "\n" );
    9356           0 :     if( bFlateFilter )
    9357           0 :         aLine.append( "/Filter/FlateDecode\n" );
    9358             :     aLine.append( ">>\n"
    9359           0 :                   "stream\n" );
    9360           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9361           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9362           0 :     CHECK_RETURN( writeBuffer( rObject.m_pContentStream->GetData(), nSize ) );
    9363           0 :     disableStreamEncryption();
    9364           0 :     aLine.setLength( 0 );
    9365             :     aLine.append( "\n"
    9366             :                   "endstream\n"
    9367           0 :                   "endobj\n\n" );
    9368           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9369             : 
    9370             :     // write ExtGState dict for this XObject
    9371           0 :     aLine.setLength( 0 );
    9372           0 :     aLine.append( rObject.m_nExtGStateObject );
    9373             :     aLine.append( " 0 obj\n"
    9374           0 :                   "<<" );
    9375           0 :     if( ! rObject.m_pSoftMaskStream )
    9376             :     {
    9377             : //i59651
    9378           0 :         if( m_bIsPDF_A1 )
    9379             :         {
    9380           0 :             aLine.append( "/CA 1.0/ca 1.0" );
    9381           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9382             :         }
    9383             :         else
    9384             :         {
    9385           0 :             aLine.append(  "/CA " );
    9386           0 :             appendDouble( rObject.m_fAlpha, aLine );
    9387             :             aLine.append( "\n"
    9388           0 :                           "   /ca " );
    9389           0 :             appendDouble( rObject.m_fAlpha, aLine );
    9390             :         }
    9391           0 :         aLine.append( "\n" );
    9392             :     }
    9393             :     else
    9394             :     {
    9395           0 :         if( m_bIsPDF_A1 )
    9396             :         {
    9397           0 :             aLine.append( "/SMask/None" );
    9398           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9399             :         }
    9400             :         else
    9401             :         {
    9402           0 :             rObject.m_pSoftMaskStream->Seek( STREAM_SEEK_TO_END );
    9403           0 :             sal_Int32 nMaskSize = (sal_Int32)rObject.m_pSoftMaskStream->Tell();
    9404           0 :             rObject.m_pSoftMaskStream->Seek( STREAM_SEEK_TO_BEGIN );
    9405           0 :             sal_Int32 nMaskObject = createObject();
    9406           0 :             aLine.append( "/SMask<</Type/Mask/S/Luminosity/G " );
    9407           0 :             aLine.append( nMaskObject );
    9408           0 :             aLine.append( " 0 R>>\n" );
    9409             : 
    9410           0 :             OStringBuffer aMask;
    9411           0 :             aMask.append( nMaskObject );
    9412             :             aMask.append( " 0 obj\n"
    9413             :                           "<</Type/XObject\n"
    9414             :                           "/Subtype/Form\n"
    9415           0 :                           "/BBox[" );
    9416           0 :             appendFixedInt( rObject.m_aBoundRect.Left(), aMask );
    9417           0 :             aMask.append( ' ' );
    9418           0 :             appendFixedInt( rObject.m_aBoundRect.Top(), aMask );
    9419           0 :             aMask.append( ' ' );
    9420           0 :             appendFixedInt( rObject.m_aBoundRect.Right(), aMask );
    9421           0 :             aMask.append( ' ' );
    9422           0 :             appendFixedInt( rObject.m_aBoundRect.Bottom()+1, aMask );
    9423           0 :             aMask.append( "]\n" );
    9424             : 
    9425             :             /* #i42884# see above */
    9426           0 :             aMask.append( "/Group<</S/Transparency/CS/DeviceRGB>>\n" );
    9427           0 :             aMask.append( "/Length " );
    9428           0 :             aMask.append( nMaskSize );
    9429             :             aMask.append( ">>\n"
    9430           0 :                           "stream\n" );
    9431           0 :             CHECK_RETURN( updateObject( nMaskObject ) );
    9432           0 :             checkAndEnableStreamEncryption(  nMaskObject );
    9433           0 :             CHECK_RETURN( writeBuffer( aMask.getStr(), aMask.getLength() ) );
    9434           0 :             CHECK_RETURN( writeBuffer( rObject.m_pSoftMaskStream->GetData(), nMaskSize ) );
    9435           0 :             disableStreamEncryption();
    9436           0 :             aMask.setLength( 0 );
    9437             :             aMask.append( "\nendstream\n"
    9438           0 :                           "endobj\n\n" );
    9439           0 :             CHECK_RETURN( writeBuffer( aMask.getStr(), aMask.getLength() ) );
    9440             :         }
    9441             :     }
    9442             :     aLine.append( ">>\n"
    9443           0 :                   "endobj\n\n" );
    9444           0 :     CHECK_RETURN( updateObject( rObject.m_nExtGStateObject ) );
    9445           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9446             : 
    9447           0 :     return true;
    9448             : }
    9449             : 
    9450           0 : bool PDFWriterImpl::writeGradientFunction( GradientEmit& rObject )
    9451             : {
    9452           0 :     sal_Int32 nFunctionObject = createObject();
    9453           0 :     CHECK_RETURN( updateObject( nFunctionObject ) );
    9454             : 
    9455           0 :     VirtualDevice aDev;
    9456           0 :     aDev.SetOutputSizePixel( rObject.m_aSize );
    9457           0 :     aDev.SetMapMode( MapMode( MAP_PIXEL ) );
    9458           0 :     if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
    9459           0 :         aDev.SetDrawMode( aDev.GetDrawMode() |
    9460             :                           ( DRAWMODE_GRAYLINE | DRAWMODE_GRAYFILL | DRAWMODE_GRAYTEXT |
    9461           0 :                             DRAWMODE_GRAYBITMAP | DRAWMODE_GRAYGRADIENT ) );
    9462           0 :     aDev.DrawGradient( Rectangle( Point( 0, 0 ), rObject.m_aSize ), rObject.m_aGradient );
    9463             : 
    9464           0 :     Bitmap aSample = aDev.GetBitmap( Point( 0, 0 ), rObject.m_aSize );
    9465           0 :     BitmapReadAccess* pAccess = aSample.AcquireReadAccess();
    9466           0 :     AccessReleaser aReleaser( pAccess );
    9467             : 
    9468           0 :     Size aSize = aSample.GetSizePixel();
    9469             : 
    9470           0 :     sal_Int32 nStreamLengthObject = createObject();
    9471             :     #if OSL_DEBUG_LEVEL > 1
    9472             :     emitComment( "PDFWriterImpl::writeGradientFunction" );
    9473             :     #endif
    9474           0 :     OStringBuffer aLine( 120 );
    9475           0 :     aLine.append( nFunctionObject );
    9476             :     aLine.append( " 0 obj\n"
    9477             :                   "<</FunctionType 0\n"
    9478             :                   "/Domain[ 0 1 0 1 ]\n"
    9479           0 :                   "/Size[ " );
    9480           0 :     aLine.append( (sal_Int32)aSize.Width() );
    9481           0 :     aLine.append( ' ' );
    9482           0 :     aLine.append( (sal_Int32)aSize.Height() );
    9483             :     aLine.append( " ]\n"
    9484             :                   "/BitsPerSample 8\n"
    9485             :                   "/Range[ 0 1 0 1 0 1 ]\n"
    9486             :                   "/Order 3\n"
    9487           0 :                   "/Length " );
    9488           0 :     aLine.append( nStreamLengthObject );
    9489             :     aLine.append( " 0 R\n"
    9490             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9491             :                   "/Filter/FlateDecode"
    9492             : #endif
    9493             :                   ">>\n"
    9494           0 :                   "stream\n" );
    9495           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9496             : 
    9497           0 :     sal_uInt64 nStartStreamPos = 0;
    9498           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nStartStreamPos )) );
    9499             : 
    9500           0 :     checkAndEnableStreamEncryption( nFunctionObject );
    9501           0 :     beginCompression();
    9502           0 :     for( int y = aSize.Height()-1; y >= 0; y-- )
    9503             :     {
    9504           0 :         for( int x = 0; x < aSize.Width(); x++ )
    9505             :         {
    9506             :             sal_uInt8 aCol[3];
    9507           0 :             BitmapColor aColor = pAccess->GetColor( y, x );
    9508           0 :             aCol[0] = aColor.GetRed();
    9509           0 :             aCol[1] = aColor.GetGreen();
    9510           0 :             aCol[2] = aColor.GetBlue();
    9511           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9512           0 :         }
    9513             :     }
    9514           0 :     endCompression();
    9515           0 :     disableStreamEncryption();
    9516             : 
    9517           0 :     sal_uInt64 nEndStreamPos = 0;
    9518           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nEndStreamPos )) );
    9519             : 
    9520           0 :     aLine.setLength( 0 );
    9521           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9522           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9523             : 
    9524             :     // write stream length
    9525           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
    9526           0 :     aLine.setLength( 0 );
    9527           0 :     aLine.append( nStreamLengthObject );
    9528           0 :     aLine.append( " 0 obj\n" );
    9529           0 :     aLine.append( (sal_Int64)(nEndStreamPos-nStartStreamPos) );
    9530           0 :     aLine.append( "\nendobj\n\n" );
    9531           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9532             : 
    9533           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9534           0 :     aLine.setLength( 0 );
    9535           0 :     aLine.append( rObject.m_nObject );
    9536             :     aLine.append( " 0 obj\n"
    9537             :                   "<</ShadingType 1\n"
    9538             :                   "/ColorSpace/DeviceRGB\n"
    9539             :                   "/AntiAlias true\n"
    9540             :                   "/Domain[ 0 1 0 1 ]\n"
    9541           0 :                   "/Matrix[ " );
    9542           0 :     aLine.append( (sal_Int32)aSize.Width() );
    9543           0 :     aLine.append( " 0 0 " );
    9544           0 :     aLine.append( (sal_Int32)aSize.Height() );
    9545             :     aLine.append( " 0 0 ]\n"
    9546           0 :                   "/Function " );
    9547           0 :     aLine.append( nFunctionObject );
    9548             :     aLine.append( " 0 R\n"
    9549             :                   ">>\n"
    9550           0 :                   "endobj\n\n" );
    9551           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9552             : 
    9553           0 :     return true;
    9554             : }
    9555             : 
    9556           0 : bool PDFWriterImpl::writeJPG( JPGEmit& rObject )
    9557             : {
    9558           0 :     CHECK_RETURN( rObject.m_pStream );
    9559           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9560             : 
    9561           0 :     sal_Int32 nLength = 0;
    9562           0 :     rObject.m_pStream->Seek( STREAM_SEEK_TO_END );
    9563           0 :     nLength = rObject.m_pStream->Tell();
    9564           0 :     rObject.m_pStream->Seek( STREAM_SEEK_TO_BEGIN );
    9565             : 
    9566           0 :     sal_Int32 nMaskObject = 0;
    9567           0 :     if( !!rObject.m_aMask )
    9568             :     {
    9569           0 :         if( rObject.m_aMask.GetBitCount() == 1 ||
    9570           0 :             ( rObject.m_aMask.GetBitCount() == 8 && m_aContext.Version >= PDFWriter::PDF_1_4 && !m_bIsPDF_A1 )//i59651
    9571             :             )
    9572             :         {
    9573           0 :             nMaskObject = createObject();
    9574             :         }
    9575           0 :         else if( m_bIsPDF_A1 )
    9576           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9577           0 :         else if( m_aContext.Version < PDFWriter::PDF_1_4 )
    9578           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDF13 );
    9579             : 
    9580             :     }
    9581             :     #if OSL_DEBUG_LEVEL > 1
    9582             :     emitComment( "PDFWriterImpl::writeJPG" );
    9583             :     #endif
    9584             : 
    9585           0 :     OStringBuffer aLine(200);
    9586           0 :     aLine.append( rObject.m_nObject );
    9587             :     aLine.append( " 0 obj\n"
    9588           0 :                   "<</Type/XObject/Subtype/Image/Width " );
    9589           0 :     aLine.append( (sal_Int32)rObject.m_aID.m_aPixelSize.Width() );
    9590           0 :     aLine.append( " /Height " );
    9591           0 :     aLine.append( (sal_Int32)rObject.m_aID.m_aPixelSize.Height() );
    9592           0 :     aLine.append( " /BitsPerComponent 8 " );
    9593           0 :     if( rObject.m_bTrueColor )
    9594           0 :         aLine.append( "/ColorSpace/DeviceRGB" );
    9595             :     else
    9596           0 :         aLine.append( "/ColorSpace/DeviceGray" );
    9597           0 :     aLine.append( "/Filter/DCTDecode/Length " );
    9598           0 :     aLine.append( nLength );
    9599           0 :     if( nMaskObject )
    9600             :     {
    9601           0 :         aLine.append( rObject.m_aMask.GetBitCount() == 1 ? " /Mask " : " /SMask " );
    9602           0 :         aLine.append( nMaskObject );
    9603           0 :         aLine.append( " 0 R " );
    9604             :     }
    9605           0 :     aLine.append( ">>\nstream\n" );
    9606           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9607             : 
    9608           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9609           0 :     CHECK_RETURN( writeBuffer( rObject.m_pStream->GetData(), nLength ) );
    9610           0 :     disableStreamEncryption();
    9611             : 
    9612           0 :     aLine.setLength( 0 );
    9613           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9614           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9615             : 
    9616           0 :     if( nMaskObject )
    9617             :     {
    9618           0 :         BitmapEmit aEmit;
    9619           0 :         aEmit.m_nObject = nMaskObject;
    9620           0 :         if( rObject.m_aMask.GetBitCount() == 1 )
    9621           0 :             aEmit.m_aBitmap = BitmapEx( rObject.m_aMask, rObject.m_aMask );
    9622           0 :         else if( rObject.m_aMask.GetBitCount() == 8 )
    9623           0 :             aEmit.m_aBitmap = BitmapEx( rObject.m_aMask, AlphaMask( rObject.m_aMask ) );
    9624           0 :         writeBitmapObject( aEmit, true );
    9625             :     }
    9626             : 
    9627           0 :     return true;
    9628             : }
    9629             : 
    9630           0 : bool PDFWriterImpl::writeBitmapObject( BitmapEmit& rObject, bool bMask )
    9631             : {
    9632           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9633             : 
    9634           0 :     Bitmap  aBitmap;
    9635           0 :     Color   aTransparentColor( COL_TRANSPARENT );
    9636           0 :     bool    bWriteMask = false;
    9637           0 :     if( ! bMask )
    9638             :     {
    9639           0 :         aBitmap = rObject.m_aBitmap.GetBitmap();
    9640           0 :         if( rObject.m_aBitmap.IsAlpha() )
    9641             :         {
    9642           0 :             if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    9643           0 :                 bWriteMask = true;
    9644             :             // else draw without alpha channel
    9645             :         }
    9646             :         else
    9647             :         {
    9648           0 :             switch( rObject.m_aBitmap.GetTransparentType() )
    9649             :             {
    9650             :                 case TRANSPARENT_NONE:
    9651             :                     // comes from drawMask function
    9652           0 :                     if( aBitmap.GetBitCount() == 1 && rObject.m_bDrawMask )
    9653           0 :                         bMask = true;
    9654           0 :                     break;
    9655             :                 case TRANSPARENT_COLOR:
    9656           0 :                     aTransparentColor = rObject.m_aBitmap.GetTransparentColor();
    9657           0 :                     break;
    9658             :                 case TRANSPARENT_BITMAP:
    9659           0 :                     bWriteMask = true;
    9660           0 :                     break;
    9661             :             }
    9662             :         }
    9663             :     }
    9664             :     else
    9665             :     {
    9666           0 :         if( m_aContext.Version < PDFWriter::PDF_1_4 || ! rObject.m_aBitmap.IsAlpha() )
    9667             :         {
    9668           0 :             aBitmap = rObject.m_aBitmap.GetMask();
    9669           0 :             aBitmap.Convert( BMP_CONVERSION_1BIT_THRESHOLD );
    9670             :             DBG_ASSERT( aBitmap.GetBitCount() == 1, "mask conversion failed" );
    9671             :         }
    9672           0 :         else if( aBitmap.GetBitCount() != 8 )
    9673             :         {
    9674           0 :             aBitmap = rObject.m_aBitmap.GetAlpha().GetBitmap();
    9675           0 :             aBitmap.Convert( BMP_CONVERSION_8BIT_GREYS );
    9676             :             DBG_ASSERT( aBitmap.GetBitCount() == 8, "alpha mask conversion failed" );
    9677             :         }
    9678             :     }
    9679             : 
    9680           0 :     BitmapReadAccess* pAccess = aBitmap.AcquireReadAccess();
    9681           0 :     AccessReleaser aReleaser( pAccess );
    9682             : 
    9683             :     bool bTrueColor;
    9684             :     sal_Int32 nBitsPerComponent;
    9685           0 :     switch( aBitmap.GetBitCount() )
    9686             :     {
    9687             :         case 1:
    9688             :         case 2:
    9689             :         case 4:
    9690             :         case 8:
    9691           0 :             bTrueColor = false;
    9692           0 :             nBitsPerComponent = aBitmap.GetBitCount();
    9693           0 :             break;
    9694             :         default:
    9695           0 :             bTrueColor = true;
    9696           0 :             nBitsPerComponent = 8;
    9697           0 :             break;
    9698             :     }
    9699             : 
    9700           0 :     sal_Int32 nStreamLengthObject   = createObject();
    9701           0 :     sal_Int32 nMaskObject           = 0;
    9702             : 
    9703             :     #if OSL_DEBUG_LEVEL > 1
    9704             :     emitComment( "PDFWriterImpl::writeBitmapObject" );
    9705             :     #endif
    9706           0 :     OStringBuffer aLine(1024);
    9707           0 :     aLine.append( rObject.m_nObject );
    9708             :     aLine.append( " 0 obj\n"
    9709           0 :                   "<</Type/XObject/Subtype/Image/Width " );
    9710           0 :     aLine.append( (sal_Int32)aBitmap.GetSizePixel().Width() );
    9711           0 :     aLine.append( "/Height " );
    9712           0 :     aLine.append( (sal_Int32)aBitmap.GetSizePixel().Height() );
    9713           0 :     aLine.append( "/BitsPerComponent " );
    9714           0 :     aLine.append( nBitsPerComponent );
    9715           0 :     aLine.append( "/Length " );
    9716           0 :     aLine.append( nStreamLengthObject );
    9717           0 :     aLine.append( " 0 R\n" );
    9718             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9719           0 :     if( nBitsPerComponent != 1 )
    9720             :     {
    9721           0 :         aLine.append( "/Filter/FlateDecode" );
    9722             :     }
    9723             :     else
    9724             :     {
    9725           0 :         aLine.append( "/Filter/CCITTFaxDecode/DecodeParms<</K -1/BlackIs1 true/Columns " );
    9726           0 :         aLine.append( (sal_Int32)aBitmap.GetSizePixel().Width() );
    9727           0 :         aLine.append( ">>\n" );
    9728             :     }
    9729             : #endif
    9730           0 :     if( ! bMask )
    9731             :     {
    9732           0 :         aLine.append( "/ColorSpace" );
    9733           0 :         if( bTrueColor )
    9734           0 :             aLine.append( "/DeviceRGB\n" );
    9735           0 :         else if( aBitmap.HasGreyPalette() )
    9736             :         {
    9737           0 :             aLine.append( "/DeviceGray\n" );
    9738           0 :             if( aBitmap.GetBitCount() == 1 )
    9739             :             {
    9740             :                 // #i47395# 1 bit bitmaps occasionally have an inverted grey palette
    9741           0 :                 sal_Int32 nBlackIndex = pAccess->GetBestPaletteIndex( BitmapColor( Color( COL_BLACK ) ) );
    9742             :                 DBG_ASSERT( nBlackIndex == 0 || nBlackIndex == 1, "wrong black index" );
    9743           0 :                 if( nBlackIndex == 1 )
    9744           0 :                     aLine.append( "/Decode[1 0]\n" );
    9745             :             }
    9746             :         }
    9747             :         else
    9748             :         {
    9749           0 :             aLine.append( "[ /Indexed/DeviceRGB " );
    9750           0 :             aLine.append( (sal_Int32)(pAccess->GetPaletteEntryCount()-1) );
    9751           0 :             aLine.append( "\n<" );
    9752           0 :             if( m_aContext.Encryption.Encrypt() )
    9753             :             {
    9754           0 :                 enableStringEncryption( rObject.m_nObject );
    9755             :                 //check encryption buffer size
    9756           0 :                 if( checkEncryptionBufferSize( pAccess->GetPaletteEntryCount()*3 ) )
    9757             :                 {
    9758           0 :                     int nChar = 0;
    9759             :                     //fill the encryption buffer
    9760           0 :                     for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9761             :                     {
    9762           0 :                         const BitmapColor& rColor = pAccess->GetPaletteColor( i );
    9763           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetRed();
    9764           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetGreen();
    9765           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetBlue();
    9766             :                     }
    9767             :                     //encrypt the colorspace lookup table
    9768           0 :                     rtl_cipher_encodeARCFOUR( m_aCipher, m_pEncryptionBuffer, nChar, m_pEncryptionBuffer, nChar );
    9769             :                     //now queue the data for output
    9770           0 :                     nChar = 0;
    9771           0 :                     for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9772             :                     {
    9773           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9774           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9775           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9776             :                     }
    9777             :                 }
    9778             :             }
    9779             :             else //no encryption requested (PDF/A-1a program flow drops here)
    9780             :             {
    9781           0 :                 for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9782             :                 {
    9783           0 :                     const BitmapColor& rColor = pAccess->GetPaletteColor( i );
    9784           0 :                     appendHex( rColor.GetRed(), aLine );
    9785           0 :                     appendHex( rColor.GetGreen(), aLine );
    9786           0 :                     appendHex( rColor.GetBlue(), aLine );
    9787             :                 }
    9788             :             }
    9789           0 :             aLine.append( ">\n]\n" );
    9790             :         }
    9791             :     }
    9792             :     else
    9793             :     {
    9794           0 :         if( aBitmap.GetBitCount() == 1 )
    9795             :         {
    9796           0 :             aLine.append( "/ImageMask true\n" );
    9797           0 :             sal_Int32 nBlackIndex = pAccess->GetBestPaletteIndex( BitmapColor( Color( COL_BLACK ) ) );
    9798             :             DBG_ASSERT( nBlackIndex == 0 || nBlackIndex == 1, "wrong black index" );
    9799           0 :             if( nBlackIndex )
    9800           0 :                 aLine.append( "/Decode[ 1 0 ]\n" );
    9801             :             else
    9802           0 :                 aLine.append( "/Decode[ 0 1 ]\n" );
    9803             :         }
    9804           0 :         else if( aBitmap.GetBitCount() == 8 )
    9805             :         {
    9806             :             aLine.append( "/ColorSpace/DeviceGray\n"
    9807           0 :                           "/Decode [ 1 0 ]\n" );
    9808             :         }
    9809             :     }
    9810             : 
    9811           0 :     if( ! bMask && m_aContext.Version > PDFWriter::PDF_1_2 && !m_bIsPDF_A1 )//i59651
    9812             :     {
    9813           0 :         if( bWriteMask )
    9814             :         {
    9815           0 :             nMaskObject = createObject();
    9816           0 :             if( rObject.m_aBitmap.IsAlpha() && m_aContext.Version > PDFWriter::PDF_1_3 )
    9817           0 :                 aLine.append( "/SMask " );
    9818             :             else
    9819           0 :                 aLine.append( "/Mask " );
    9820           0 :             aLine.append( nMaskObject );
    9821           0 :             aLine.append( " 0 R\n" );
    9822             :         }
    9823           0 :         else if( aTransparentColor != Color( COL_TRANSPARENT ) )
    9824             :         {
    9825           0 :             aLine.append( "/Mask[ " );
    9826           0 :             if( bTrueColor )
    9827             :             {
    9828           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetRed() );
    9829           0 :                 aLine.append( ' ' );
    9830           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetRed() );
    9831           0 :                 aLine.append( ' ' );
    9832           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetGreen() );
    9833           0 :                 aLine.append( ' ' );
    9834           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetGreen() );
    9835           0 :                 aLine.append( ' ' );
    9836           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetBlue() );
    9837           0 :                 aLine.append( ' ' );
    9838           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetBlue() );
    9839             :             }
    9840             :             else
    9841             :             {
    9842           0 :                 sal_Int32 nIndex = pAccess->GetBestPaletteIndex( BitmapColor( aTransparentColor ) );
    9843           0 :                 aLine.append( nIndex );
    9844             :             }
    9845           0 :             aLine.append( " ]\n" );
    9846           0 :         }
    9847             :     }
    9848           0 :     else if( m_bIsPDF_A1 && (bWriteMask || aTransparentColor != Color( COL_TRANSPARENT )) )
    9849           0 :         m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9850             : 
    9851             :     aLine.append( ">>\n"
    9852           0 :                   "stream\n" );
    9853           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9854           0 :     sal_uInt64 nStartPos = 0;
    9855           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nStartPos )) );
    9856             : 
    9857           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9858             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9859           0 :     if( nBitsPerComponent == 1 )
    9860             :     {
    9861           0 :         writeG4Stream( pAccess );
    9862             :     }
    9863             :     else
    9864             : #endif
    9865             :     {
    9866           0 :         beginCompression();
    9867           0 :         if( ! bTrueColor || pAccess->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB )
    9868             :         {
    9869           0 :             const int nScanLineBytes = 1 + ( pAccess->GetBitCount() * ( pAccess->Width() - 1 ) / 8U );
    9870             : 
    9871           0 :             for( int i = 0; i < pAccess->Height(); i++ )
    9872             :             {
    9873           0 :                 CHECK_RETURN( writeBuffer( pAccess->GetScanline( i ), nScanLineBytes ) );
    9874             :             }
    9875             :         }
    9876             :         else
    9877             :         {
    9878           0 :             const int nScanLineBytes = pAccess->Width()*3;
    9879           0 :             boost::shared_array<sal_uInt8> pCol( new sal_uInt8[ nScanLineBytes ] );
    9880           0 :             for( int y = 0; y < pAccess->Height(); y++ )
    9881             :             {
    9882           0 :                 for( int x = 0; x < pAccess->Width(); x++ )
    9883             :                 {
    9884           0 :                     BitmapColor aColor = pAccess->GetColor( y, x );
    9885           0 :                     pCol[3*x+0] = aColor.GetRed();
    9886           0 :                     pCol[3*x+1] = aColor.GetGreen();
    9887           0 :                     pCol[3*x+2] = aColor.GetBlue();
    9888           0 :                 }
    9889           0 :                 CHECK_RETURN( writeBuffer( pCol.get(), nScanLineBytes ) );
    9890           0 :             }
    9891             :         }
    9892           0 :         endCompression();
    9893             :     }
    9894           0 :     disableStreamEncryption();
    9895             : 
    9896           0 :     sal_uInt64 nEndPos = 0;
    9897           0 :     CHECK_RETURN( (osl_File_E_None == osl_getFilePos( m_aFile, &nEndPos )) );
    9898           0 :     aLine.setLength( 0 );
    9899           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9900           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9901           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
    9902           0 :     aLine.setLength( 0 );
    9903           0 :     aLine.append( nStreamLengthObject );
    9904           0 :     aLine.append( " 0 obj\n" );
    9905           0 :     aLine.append( (sal_Int64)(nEndPos-nStartPos) );
    9906           0 :     aLine.append( "\nendobj\n\n" );
    9907           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9908             : 
    9909           0 :     if( nMaskObject )
    9910             :     {
    9911           0 :         BitmapEmit aEmit;
    9912           0 :         aEmit.m_nObject             = nMaskObject;
    9913           0 :         aEmit.m_aBitmap             = rObject.m_aBitmap;
    9914           0 :         return writeBitmapObject( aEmit, true );
    9915             :     }
    9916             : 
    9917           0 :     return true;
    9918             : }
    9919             : 
    9920           0 : void PDFWriterImpl::drawJPGBitmap( SvStream& rDCTData, bool bIsTrueColor, const Size& rSizePixel, const Rectangle& rTargetArea, const Bitmap& rMask )
    9921             : {
    9922           0 :     MARK( "drawJPGBitmap" );
    9923             : 
    9924           0 :     OStringBuffer aLine( 80 );
    9925           0 :     updateGraphicsState();
    9926             : 
    9927             :     // #i40055# sanity check
    9928           0 :     if( ! (rTargetArea.GetWidth() && rTargetArea.GetHeight() ) )
    9929           0 :         return;
    9930           0 :     if( ! (rSizePixel.Width() && rSizePixel.Height()) )
    9931           0 :         return;
    9932             : 
    9933           0 :     rDCTData.Seek( 0 );
    9934           0 :     if( bIsTrueColor && m_aContext.ColorMode == PDFWriter::DrawGreyscale )
    9935             :     {
    9936             :         // need to convert to grayscale;
    9937             :         // load stream to bitmap and draw the bitmap instead
    9938           0 :         Graphic aGraphic;
    9939           0 :         GraphicConverter::Import( rDCTData, aGraphic, CVT_JPG );
    9940           0 :         Bitmap aBmp( aGraphic.GetBitmap() );
    9941           0 :         if( !!rMask && rMask.GetSizePixel() == aBmp.GetSizePixel() )
    9942             :         {
    9943           0 :             BitmapEx aBmpEx( aBmp, rMask );
    9944           0 :             drawBitmap( rTargetArea.TopLeft(), rTargetArea.GetSize(), aBmpEx );
    9945             :         }
    9946             :         else
    9947           0 :             drawBitmap( rTargetArea.TopLeft(), rTargetArea.GetSize(), aBmp );
    9948           0 :         return;
    9949             :     }
    9950             : 
    9951           0 :     SvMemoryStream* pStream = new SvMemoryStream;
    9952           0 :     *pStream << rDCTData;
    9953           0 :     pStream->Seek( STREAM_SEEK_TO_END );
    9954             : 
    9955           0 :     BitmapID aID;
    9956           0 :     aID.m_aPixelSize    = rSizePixel;
    9957           0 :     aID.m_nSize         = pStream->Tell();
    9958           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    9959           0 :     aID.m_nChecksum     = rtl_crc32( 0, pStream->GetData(), aID.m_nSize );
    9960           0 :     if( ! rMask.IsEmpty() )
    9961           0 :         aID.m_nMaskChecksum = rMask.GetChecksum();
    9962             : 
    9963           0 :     std::list< JPGEmit >::const_iterator it;
    9964           0 :     for( it = m_aJPGs.begin(); it != m_aJPGs.end() && ! (aID == it->m_aID); ++it )
    9965             :         ;
    9966           0 :     if( it == m_aJPGs.end() )
    9967             :     {
    9968           0 :         m_aJPGs.push_front( JPGEmit() );
    9969           0 :         JPGEmit& rEmit = m_aJPGs.front();
    9970           0 :         rEmit.m_nObject     = createObject();
    9971           0 :         rEmit.m_aID         = aID;
    9972           0 :         rEmit.m_pStream     = pStream;
    9973           0 :         rEmit.m_bTrueColor  = bIsTrueColor;
    9974           0 :         if( !! rMask && rMask.GetSizePixel() == rSizePixel )
    9975           0 :             rEmit.m_aMask   = rMask;
    9976             : 
    9977           0 :         it = m_aJPGs.begin();
    9978             :     }
    9979             :     else
    9980           0 :         delete pStream;
    9981             : 
    9982           0 :     aLine.append( "q " );
    9983           0 :     sal_Int32 nCheckWidth = 0;
    9984           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rTargetArea.GetWidth(), aLine, false, &nCheckWidth );
    9985           0 :     aLine.append( " 0 0 " );
    9986           0 :     sal_Int32 nCheckHeight = 0;
    9987           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rTargetArea.GetHeight(), aLine, true, &nCheckHeight );
    9988           0 :     aLine.append( ' ' );
    9989           0 :     m_aPages.back().appendPoint( rTargetArea.BottomLeft(), aLine );
    9990           0 :     aLine.append( " cm\n/Im" );
    9991           0 :     aLine.append( it->m_nObject );
    9992           0 :     aLine.append( " Do Q\n" );
    9993           0 :     if( nCheckWidth == 0 || nCheckHeight == 0 )
    9994             :     {
    9995             :         // #i97512# avoid invalid current matrix
    9996           0 :         aLine.setLength( 0 );
    9997           0 :         aLine.append( "\n%jpeg image /Im" );
    9998           0 :         aLine.append( it->m_nObject );
    9999           0 :         aLine.append( " scaled to zero size, omitted\n" );
   10000             :     }
   10001           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10002             : 
   10003           0 :     OStringBuffer aObjName( 16 );
   10004           0 :     aObjName.append( "Im" );
   10005           0 :     aObjName.append( it->m_nObject );
   10006           0 :     pushResource( ResXObject, aObjName.makeStringAndClear(), it->m_nObject );
   10007             : 
   10008             : }
   10009             : 
   10010           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const BitmapEmit& rBitmap, const Color& rFillColor )
   10011             : {
   10012           0 :     OStringBuffer aLine( 80 );
   10013           0 :     updateGraphicsState();
   10014             : 
   10015           0 :     aLine.append( "q " );
   10016           0 :     if( rFillColor != Color( COL_TRANSPARENT ) )
   10017             :     {
   10018           0 :         appendNonStrokingColor( rFillColor, aLine );
   10019           0 :         aLine.append( ' ' );
   10020             :     }
   10021           0 :     sal_Int32 nCheckWidth = 0;
   10022           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rDestSize.Width(), aLine, false, &nCheckWidth );
   10023           0 :     aLine.append( " 0 0 " );
   10024           0 :     sal_Int32 nCheckHeight = 0;
   10025           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rDestSize.Height(), aLine, true, &nCheckHeight );
   10026           0 :     aLine.append( ' ' );
   10027           0 :     m_aPages.back().appendPoint( rDestPoint + Point( 0, rDestSize.Height()-1 ), aLine );
   10028           0 :     aLine.append( " cm\n/Im" );
   10029           0 :     aLine.append( rBitmap.m_nObject );
   10030           0 :     aLine.append( " Do Q\n" );
   10031           0 :     if( nCheckWidth == 0 || nCheckHeight == 0 )
   10032             :     {
   10033             :         // #i97512# avoid invalid current matrix
   10034           0 :         aLine.setLength( 0 );
   10035           0 :         aLine.append( "\n%bitmap image /Im" );
   10036           0 :         aLine.append( rBitmap.m_nObject );
   10037           0 :         aLine.append( " scaled to zero size, omitted\n" );
   10038             :     }
   10039           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10040           0 : }
   10041             : 
   10042           0 : const PDFWriterImpl::BitmapEmit& PDFWriterImpl::createBitmapEmit( const BitmapEx& i_rBitmap, bool bDrawMask )
   10043             : {
   10044           0 :     BitmapEx aBitmap( i_rBitmap );
   10045           0 :     if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
   10046             :     {
   10047           0 :         BmpConversion eConv = BMP_CONVERSION_8BIT_GREYS;
   10048           0 :         int nDepth = aBitmap.GetBitmap().GetBitCount();
   10049           0 :         if( nDepth <= 4 )
   10050           0 :             eConv = BMP_CONVERSION_4BIT_GREYS;
   10051           0 :         if( nDepth > 1 )
   10052           0 :             aBitmap.Convert( eConv );
   10053             :     }
   10054           0 :     BitmapID aID;
   10055           0 :     aID.m_aPixelSize        = aBitmap.GetSizePixel();
   10056           0 :     aID.m_nSize             = aBitmap.GetBitCount();
   10057           0 :     aID.m_nChecksum         = aBitmap.GetBitmap().GetChecksum();
   10058           0 :     aID.m_nMaskChecksum     = 0;
   10059           0 :     if( aBitmap.IsAlpha() )
   10060           0 :         aID.m_nMaskChecksum = aBitmap.GetAlpha().GetChecksum();
   10061             :     else
   10062             :     {
   10063           0 :         Bitmap aMask = aBitmap.GetMask();
   10064           0 :         if( ! aMask.IsEmpty() )
   10065           0 :             aID.m_nMaskChecksum = aMask.GetChecksum();
   10066             :     }
   10067           0 :     std::list< BitmapEmit >::const_iterator it;
   10068           0 :     for( it = m_aBitmaps.begin(); it != m_aBitmaps.end(); ++it )
   10069             :     {
   10070           0 :         if( aID == it->m_aID )
   10071           0 :             break;
   10072             :     }
   10073           0 :     if( it == m_aBitmaps.end() )
   10074             :     {
   10075           0 :         m_aBitmaps.push_front( BitmapEmit() );
   10076           0 :         m_aBitmaps.front().m_aID        = aID;
   10077           0 :         m_aBitmaps.front().m_aBitmap    = aBitmap;
   10078           0 :         m_aBitmaps.front().m_nObject    = createObject();
   10079           0 :         m_aBitmaps.front().m_bDrawMask  = bDrawMask;
   10080           0 :         it = m_aBitmaps.begin();
   10081             :     }
   10082             : 
   10083           0 :     OStringBuffer aObjName( 16 );
   10084           0 :     aObjName.append( "Im" );
   10085           0 :     aObjName.append( it->m_nObject );
   10086           0 :     pushResource( ResXObject, aObjName.makeStringAndClear(), it->m_nObject );
   10087             : 
   10088           0 :     return *it;
   10089             : }
   10090             : 
   10091           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const Bitmap& rBitmap )
   10092             : {
   10093           0 :     MARK( "drawBitmap (Bitmap)" );
   10094             : 
   10095             :     // #i40055# sanity check
   10096           0 :     if( ! (rDestSize.Width() && rDestSize.Height()) )
   10097           0 :         return;
   10098             : 
   10099           0 :     const BitmapEmit& rEmit = createBitmapEmit( BitmapEx( rBitmap ) );
   10100           0 :     drawBitmap( rDestPoint, rDestSize, rEmit, Color( COL_TRANSPARENT ) );
   10101             : }
   10102             : 
   10103           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const BitmapEx& rBitmap )
   10104             : {
   10105           0 :     MARK( "drawBitmap (BitmapEx)" );
   10106             : 
   10107             :     // #i40055# sanity check
   10108           0 :     if( ! (rDestSize.Width() && rDestSize.Height()) )
   10109           0 :         return;
   10110             : 
   10111           0 :     const BitmapEmit& rEmit = createBitmapEmit( rBitmap );
   10112           0 :     drawBitmap( rDestPoint, rDestSize, rEmit, Color( COL_TRANSPARENT ) );
   10113             : }
   10114             : 
   10115           0 : sal_Int32 PDFWriterImpl::createGradient( const Gradient& rGradient, const Size& rSize )
   10116             : {
   10117           0 :     Size aPtSize( lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10118             :                                MapMode( MAP_POINT ),
   10119             :                                getReferenceDevice(),
   10120           0 :                                rSize ) );
   10121             :     // check if we already have this gradient
   10122           0 :     std::list<GradientEmit>::iterator it;
   10123             :     // rounding to point will generally lose some pixels
   10124             :     // round up to point boundary
   10125           0 :     aPtSize.Width()++;
   10126           0 :     aPtSize.Height()++;
   10127           0 :     for( it = m_aGradients.begin(); it != m_aGradients.end(); ++it )
   10128             :     {
   10129           0 :         if( it->m_aGradient == rGradient )
   10130             :         {
   10131           0 :             if( it->m_aSize == aPtSize )
   10132           0 :                 break;
   10133             :         }
   10134             :     }
   10135           0 :     if( it == m_aGradients.end() )
   10136             :     {
   10137           0 :         m_aGradients.push_front( GradientEmit() );
   10138           0 :         m_aGradients.front().m_aGradient    = rGradient;
   10139           0 :         m_aGradients.front().m_nObject      = createObject();
   10140           0 :         m_aGradients.front().m_aSize        = aPtSize;
   10141           0 :         it = m_aGradients.begin();
   10142             :     }
   10143             : 
   10144           0 :     OStringBuffer aObjName( 16 );
   10145           0 :     aObjName.append( 'P' );
   10146           0 :     aObjName.append( it->m_nObject );
   10147           0 :     pushResource( ResShading, aObjName.makeStringAndClear(), it->m_nObject );
   10148             : 
   10149           0 :     return it->m_nObject;
   10150             : }
   10151             : 
   10152           0 : void PDFWriterImpl::drawGradient( const Rectangle& rRect, const Gradient& rGradient )
   10153             : {
   10154           0 :     MARK( "drawGradient (Rectangle)" );
   10155             : 
   10156           0 :     if( m_aContext.Version == PDFWriter::PDF_1_2 )
   10157             :     {
   10158           0 :         drawRectangle( rRect );
   10159           0 :         return;
   10160             :     }
   10161             : 
   10162           0 :     sal_Int32 nGradient = createGradient( rGradient, rRect.GetSize() );
   10163             : 
   10164           0 :     Point aTranslate( rRect.BottomLeft() );
   10165           0 :     aTranslate += Point( 0, 1 );
   10166             : 
   10167           0 :     updateGraphicsState();
   10168             : 
   10169           0 :     OStringBuffer aLine( 80 );
   10170           0 :     aLine.append( "q 1 0 0 1 " );
   10171           0 :     m_aPages.back().appendPoint( aTranslate, aLine );
   10172           0 :     aLine.append( " cm " );
   10173             :     // if a stroke is appended reset the clip region before stroke
   10174           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
   10175           0 :         aLine.append( "q " );
   10176           0 :     aLine.append( "0 0 " );
   10177           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetWidth(), aLine, false );
   10178           0 :     aLine.append( ' ' );
   10179           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetHeight(), aLine, true );
   10180           0 :     aLine.append( " re W n\n" );
   10181             : 
   10182           0 :     aLine.append( "/P" );
   10183           0 :     aLine.append( nGradient );
   10184           0 :     aLine.append( " sh " );
   10185           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
   10186             :     {
   10187           0 :         aLine.append( "Q 0 0 " );
   10188           0 :         m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetWidth(), aLine, false );
   10189           0 :         aLine.append( ' ' );
   10190           0 :         m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetHeight(), aLine, true );
   10191           0 :         aLine.append( " re S " );
   10192             :     }
   10193           0 :     aLine.append( "Q\n" );
   10194           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10195             : }
   10196             : 
   10197           0 : void PDFWriterImpl::drawHatch( const PolyPolygon& rPolyPoly, const Hatch& rHatch )
   10198             : {
   10199           0 :     MARK( "drawHatch" );
   10200             : 
   10201           0 :     updateGraphicsState();
   10202             : 
   10203           0 :     if( rPolyPoly.Count() )
   10204             :     {
   10205           0 :         PolyPolygon     aPolyPoly( rPolyPoly );
   10206             : 
   10207           0 :         aPolyPoly.Optimize( POLY_OPTIMIZE_NO_SAME );
   10208           0 :         push( PUSH_LINECOLOR );
   10209           0 :         setLineColor( rHatch.GetColor() );
   10210           0 :         getReferenceDevice()->ImplDrawHatch( aPolyPoly, rHatch, sal_False );
   10211           0 :         pop();
   10212             :     }
   10213           0 : }
   10214             : 
   10215           0 : void PDFWriterImpl::drawWallpaper( const Rectangle& rRect, const Wallpaper& rWall )
   10216             : {
   10217           0 :     MARK( "drawWallpaper" );
   10218             : 
   10219           0 :     bool bDrawColor         = false;
   10220           0 :     bool bDrawGradient      = false;
   10221           0 :     bool bDrawBitmap        = false;
   10222             : 
   10223           0 :     BitmapEx aBitmap;
   10224           0 :     Point aBmpPos = rRect.TopLeft();
   10225           0 :     Size aBmpSize;
   10226           0 :     if( rWall.IsBitmap() )
   10227             :     {
   10228           0 :         aBitmap = rWall.GetBitmap();
   10229           0 :         aBmpSize = lcl_convert( aBitmap.GetPrefMapMode(),
   10230           0 :                                 getMapMode(),
   10231             :                                 getReferenceDevice(),
   10232           0 :                                 aBitmap.GetPrefSize() );
   10233           0 :         Rectangle aRect( rRect );
   10234           0 :         if( rWall.IsRect() )
   10235             :         {
   10236           0 :             aRect = rWall.GetRect();
   10237           0 :             aBmpPos = aRect.TopLeft();
   10238           0 :             aBmpSize = aRect.GetSize();
   10239             :         }
   10240           0 :         if( rWall.GetStyle() != WALLPAPER_SCALE )
   10241             :         {
   10242           0 :             if( rWall.GetStyle() != WALLPAPER_TILE )
   10243             :             {
   10244           0 :                 bDrawBitmap     = true;
   10245           0 :                 if( rWall.IsGradient() )
   10246           0 :                     bDrawGradient = true;
   10247             :                 else
   10248           0 :                     bDrawColor = true;
   10249           0 :                 switch( rWall.GetStyle() )
   10250             :                 {
   10251             :                     case WALLPAPER_TOPLEFT:
   10252           0 :                         break;
   10253             :                     case WALLPAPER_TOP:
   10254           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10255           0 :                         break;
   10256             :                     case WALLPAPER_LEFT:
   10257           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10258           0 :                         break;
   10259             :                     case WALLPAPER_TOPRIGHT:
   10260           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10261           0 :                         break;
   10262             :                     case WALLPAPER_CENTER:
   10263           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10264           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10265           0 :                         break;
   10266             :                     case WALLPAPER_RIGHT:
   10267           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10268           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10269           0 :                         break;
   10270             :                     case WALLPAPER_BOTTOMLEFT:
   10271           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10272           0 :                         break;
   10273             :                     case WALLPAPER_BOTTOM:
   10274           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10275           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10276           0 :                         break;
   10277             :                     case WALLPAPER_BOTTOMRIGHT:
   10278           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10279           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10280           0 :                         break;
   10281             :                     default: ;
   10282             :                 }
   10283             :             }
   10284             :             else
   10285             :             {
   10286             :                 // push the bitmap
   10287           0 :                 const BitmapEmit& rEmit = createBitmapEmit( BitmapEx( aBitmap ) );
   10288             : 
   10289             :                 // convert to page coordinates; this needs to be done here
   10290             :                 // since the emit does not know the page anymore
   10291           0 :                 Rectangle aConvertRect( aBmpPos, aBmpSize );
   10292           0 :                 m_aPages.back().convertRect( aConvertRect );
   10293             : 
   10294           0 :                 OStringBuffer aNameBuf(16);
   10295           0 :                 aNameBuf.append( "Im" );
   10296           0 :                 aNameBuf.append( rEmit.m_nObject );
   10297           0 :                 OString aImageName( aNameBuf.makeStringAndClear() );
   10298             : 
   10299             :                 // push the pattern
   10300           0 :                 OStringBuffer aTilingStream( 32 );
   10301           0 :                 appendFixedInt( aConvertRect.GetWidth(), aTilingStream );
   10302           0 :                 aTilingStream.append( " 0 0 " );
   10303           0 :                 appendFixedInt( aConvertRect.GetHeight(), aTilingStream );
   10304           0 :                 aTilingStream.append( " 0 0 cm\n/" );
   10305           0 :                 aTilingStream.append( aImageName );
   10306           0 :                 aTilingStream.append( " Do\n" );
   10307             : 
   10308           0 :                 m_aTilings.push_back( TilingEmit() );
   10309           0 :                 m_aTilings.back().m_nObject         = createObject();
   10310           0 :                 m_aTilings.back().m_aRectangle      = Rectangle( Point( 0, 0 ), aConvertRect.GetSize() );
   10311           0 :                 m_aTilings.back().m_pTilingStream   = new SvMemoryStream();
   10312           0 :                 m_aTilings.back().m_pTilingStream->Write( aTilingStream.getStr(), aTilingStream.getLength() );
   10313             :                 // phase the tiling so wallpaper begins on upper left
   10314           0 :                 m_aTilings.back().m_aTransform.matrix[2] = double(aConvertRect.Left() % aConvertRect.GetWidth()) / fDivisor;
   10315           0 :                 m_aTilings.back().m_aTransform.matrix[5] = double(aConvertRect.Top() % aConvertRect.GetHeight()) / fDivisor;
   10316           0 :                 m_aTilings.back().m_aResources.m_aXObjects[aImageName] = rEmit.m_nObject;
   10317             : 
   10318           0 :                 updateGraphicsState();
   10319             : 
   10320           0 :                 OStringBuffer aObjName( 16 );
   10321           0 :                 aObjName.append( 'P' );
   10322           0 :                 aObjName.append( m_aTilings.back().m_nObject );
   10323           0 :                 OString aPatternName( aObjName.makeStringAndClear() );
   10324           0 :                 pushResource( ResPattern, aPatternName, m_aTilings.back().m_nObject );
   10325             : 
   10326             :                 // fill a rRect with the pattern
   10327           0 :                 OStringBuffer aLine( 100 );
   10328           0 :                 aLine.append( "q /Pattern cs /" );
   10329           0 :                 aLine.append( aPatternName );
   10330           0 :                 aLine.append( " scn " );
   10331           0 :                 m_aPages.back().appendRect( rRect, aLine );
   10332           0 :                 aLine.append( " f Q\n" );
   10333           0 :                 writeBuffer( aLine.getStr(), aLine.getLength() );
   10334             :             }
   10335             :         }
   10336             :         else
   10337             :         {
   10338           0 :             aBmpPos     = aRect.TopLeft();
   10339           0 :             aBmpSize    = aRect.GetSize();
   10340           0 :             bDrawBitmap = true;
   10341             :         }
   10342             : 
   10343           0 :         if( aBitmap.IsTransparent() )
   10344             :         {
   10345           0 :             if( rWall.IsGradient() )
   10346           0 :                 bDrawGradient = true;
   10347             :             else
   10348           0 :                 bDrawColor = true;
   10349             :         }
   10350             :     }
   10351           0 :     else if( rWall.IsGradient() )
   10352           0 :         bDrawGradient = true;
   10353             :     else
   10354           0 :         bDrawColor = true;
   10355             : 
   10356           0 :     if( bDrawGradient )
   10357             :     {
   10358           0 :         drawGradient( rRect, rWall.GetGradient() );
   10359             :     }
   10360           0 :     if( bDrawColor )
   10361             :     {
   10362           0 :         Color aOldLineColor = m_aGraphicsStack.front().m_aLineColor;
   10363           0 :         Color aOldFillColor = m_aGraphicsStack.front().m_aFillColor;
   10364           0 :         setLineColor( Color( COL_TRANSPARENT ) );
   10365           0 :         setFillColor( rWall.GetColor() );
   10366           0 :         drawRectangle( rRect );
   10367           0 :         setLineColor( aOldLineColor );
   10368           0 :         setFillColor( aOldFillColor );
   10369             :     }
   10370           0 :     if( bDrawBitmap )
   10371             :     {
   10372             :         // set temporary clip region since aBmpPos and aBmpSize
   10373             :         // may be outside rRect
   10374           0 :         OStringBuffer aLine( 20 );
   10375           0 :         aLine.append( "q " );
   10376           0 :         m_aPages.back().appendRect( rRect, aLine );
   10377           0 :         aLine.append( " W n\n" );
   10378           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10379           0 :         drawBitmap( aBmpPos, aBmpSize, aBitmap );
   10380           0 :         writeBuffer( "Q\n", 2 );
   10381           0 :     }
   10382           0 : }
   10383             : 
   10384           0 : void PDFWriterImpl::updateGraphicsState()
   10385             : {
   10386           0 :     OStringBuffer aLine( 256 );
   10387           0 :     GraphicsState& rNewState = m_aGraphicsStack.front();
   10388             :     // first set clip region since it might invalidate everything else
   10389             : 
   10390           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateClipRegion) )
   10391             :     {
   10392           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateClipRegion;
   10393             : 
   10394           0 :         if( m_aCurrentPDFState.m_bClipRegion != rNewState.m_bClipRegion ||
   10395           0 :             ( rNewState.m_bClipRegion && m_aCurrentPDFState.m_aClipRegion != rNewState.m_aClipRegion ) )
   10396             :         {
   10397           0 :             if( m_aCurrentPDFState.m_bClipRegion && m_aCurrentPDFState.m_aClipRegion.count() )
   10398             :             {
   10399           0 :                 aLine.append( "Q " );
   10400             :                 // invalidate everything but the clip region
   10401           0 :                 m_aCurrentPDFState = GraphicsState();
   10402           0 :                 rNewState.m_nUpdateFlags = sal::static_int_cast<sal_uInt16>(~GraphicsState::updateClipRegion);
   10403             :             }
   10404           0 :             if( rNewState.m_bClipRegion && rNewState.m_aClipRegion.count() )
   10405             :             {
   10406             :                 // clip region is always stored in private PDF mapmode
   10407           0 :                 MapMode aNewMapMode = rNewState.m_aMapMode;
   10408           0 :                 rNewState.m_aMapMode = m_aMapMode;
   10409           0 :                 getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10410           0 :                 m_aCurrentPDFState.m_aMapMode = rNewState.m_aMapMode;
   10411             : 
   10412           0 :                 aLine.append( "q " );
   10413           0 :                 m_aPages.back().appendPolyPolygon( rNewState.m_aClipRegion, aLine );
   10414           0 :                 aLine.append( "W* n\n" );
   10415           0 :                 rNewState.m_aMapMode = aNewMapMode;
   10416           0 :                 getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10417           0 :                 m_aCurrentPDFState.m_aMapMode = rNewState.m_aMapMode;
   10418             :             }
   10419             :         }
   10420             :     }
   10421             : 
   10422           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateMapMode) )
   10423             :     {
   10424           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateMapMode;
   10425           0 :         getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10426             :     }
   10427             : 
   10428           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateFont) )
   10429             :     {
   10430           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateFont;
   10431           0 :         getReferenceDevice()->SetFont( rNewState.m_aFont );
   10432           0 :         getReferenceDevice()->ImplNewFont();
   10433             :     }
   10434             : 
   10435           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateLayoutMode) )
   10436             :     {
   10437           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateLayoutMode;
   10438           0 :         getReferenceDevice()->SetLayoutMode( rNewState.m_nLayoutMode );
   10439             :     }
   10440             : 
   10441           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateDigitLanguage) )
   10442             :     {
   10443           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateDigitLanguage;
   10444           0 :         getReferenceDevice()->SetDigitLanguage( rNewState.m_aDigitLanguage );
   10445             :     }
   10446             : 
   10447           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateLineColor) )
   10448             :     {
   10449           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateLineColor;
   10450           0 :         if( m_aCurrentPDFState.m_aLineColor != rNewState.m_aLineColor &&
   10451           0 :             rNewState.m_aLineColor != Color( COL_TRANSPARENT ) )
   10452             :         {
   10453           0 :             appendStrokingColor( rNewState.m_aLineColor, aLine );
   10454           0 :             aLine.append( "\n" );
   10455             :         }
   10456             :     }
   10457             : 
   10458           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateFillColor) )
   10459             :     {
   10460           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateFillColor;
   10461           0 :         if( m_aCurrentPDFState.m_aFillColor != rNewState.m_aFillColor &&
   10462           0 :             rNewState.m_aFillColor != Color( COL_TRANSPARENT ) )
   10463             :         {
   10464           0 :             appendNonStrokingColor( rNewState.m_aFillColor, aLine );
   10465           0 :             aLine.append( "\n" );
   10466             :         }
   10467             :     }
   10468             : 
   10469           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateTransparentPercent) )
   10470             :     {
   10471           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateTransparentPercent;
   10472           0 :         if( m_aContext.Version >= PDFWriter::PDF_1_4 && m_aCurrentPDFState.m_nTransparentPercent != rNewState.m_nTransparentPercent )
   10473             :         {
   10474             :             // TODO: switch extended graphicsstate
   10475             :         }
   10476             :     }
   10477             : 
   10478             :     // everything is up to date now
   10479           0 :     m_aCurrentPDFState = m_aGraphicsStack.front();
   10480           0 :     if( !aLine.isEmpty() )
   10481           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10482           0 : }
   10483             : 
   10484             : /* #i47544# imitate OutputDevice behaviour:
   10485             : *  if a font with a nontransparent color is set, it overwrites the current
   10486             : *  text color. OTOH setting the text color will overwrite the color of the font.
   10487             : */
   10488           0 : void PDFWriterImpl::setFont( const Font& rFont )
   10489             : {
   10490           0 :     Color aColor = rFont.GetColor();
   10491           0 :     if( aColor == Color( COL_TRANSPARENT ) )
   10492           0 :         aColor = m_aGraphicsStack.front().m_aFont.GetColor();
   10493           0 :     m_aGraphicsStack.front().m_aFont = rFont;
   10494           0 :     m_aGraphicsStack.front().m_aFont.SetColor( aColor );
   10495           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateFont;
   10496           0 : }
   10497             : 
   10498           0 : void PDFWriterImpl::push( sal_uInt16 nFlags )
   10499             : {
   10500             :     OSL_ENSURE( m_aGraphicsStack.size() > 0, "invalid graphics stack" );
   10501           0 :     m_aGraphicsStack.push_front( m_aGraphicsStack.front() );
   10502           0 :     m_aGraphicsStack.front().m_nFlags = nFlags;
   10503           0 : }
   10504             : 
   10505           0 : void PDFWriterImpl::pop()
   10506             : {
   10507             :     OSL_ENSURE( m_aGraphicsStack.size() > 1, "pop without push" );
   10508           0 :     if( m_aGraphicsStack.size() < 2 )
   10509           0 :         return;
   10510             : 
   10511           0 :     GraphicsState aState = m_aGraphicsStack.front();
   10512           0 :     m_aGraphicsStack.pop_front();
   10513           0 :     GraphicsState& rOld = m_aGraphicsStack.front();
   10514             : 
   10515             :     // move those parameters back that were not pushed
   10516             :     // in the first place
   10517           0 :     if( ! (aState.m_nFlags & PUSH_LINECOLOR) )
   10518           0 :         setLineColor( aState.m_aLineColor );
   10519           0 :     if( ! (aState.m_nFlags & PUSH_FILLCOLOR) )
   10520           0 :         setFillColor( aState.m_aFillColor );
   10521           0 :     if( ! (aState.m_nFlags & PUSH_FONT) )
   10522           0 :         setFont( aState.m_aFont );
   10523           0 :     if( ! (aState.m_nFlags & PUSH_TEXTCOLOR) )
   10524           0 :         setTextColor( aState.m_aFont.GetColor() );
   10525           0 :     if( ! (aState.m_nFlags & PUSH_MAPMODE) )
   10526           0 :         setMapMode( aState.m_aMapMode );
   10527           0 :     if( ! (aState.m_nFlags & PUSH_CLIPREGION) )
   10528             :     {
   10529             :         // do not use setClipRegion here
   10530             :         // it would convert again assuming the current mapmode
   10531           0 :         rOld.m_aClipRegion = aState.m_aClipRegion;
   10532           0 :         rOld.m_bClipRegion = aState.m_bClipRegion;
   10533             :     }
   10534           0 :     if( ! (aState.m_nFlags & PUSH_TEXTLINECOLOR ) )
   10535           0 :         setTextLineColor( aState.m_aTextLineColor );
   10536           0 :     if( ! (aState.m_nFlags & PUSH_OVERLINECOLOR ) )
   10537           0 :         setOverlineColor( aState.m_aOverlineColor );
   10538           0 :     if( ! (aState.m_nFlags & PUSH_TEXTALIGN ) )
   10539           0 :         setTextAlign( aState.m_aFont.GetAlign() );
   10540           0 :     if( ! (aState.m_nFlags & PUSH_TEXTFILLCOLOR) )
   10541           0 :         setTextFillColor( aState.m_aFont.GetFillColor() );
   10542           0 :     if( ! (aState.m_nFlags & PUSH_REFPOINT) )
   10543             :     {
   10544             :         // what ?
   10545             :     }
   10546             :     // invalidate graphics state
   10547           0 :     m_aGraphicsStack.front().m_nUpdateFlags = sal::static_int_cast<sal_uInt16>(~0U);
   10548             : }
   10549             : 
   10550           0 : void PDFWriterImpl::setMapMode( const MapMode& rMapMode )
   10551             : {
   10552           0 :     m_aGraphicsStack.front().m_aMapMode = rMapMode;
   10553           0 :     getReferenceDevice()->SetMapMode( rMapMode );
   10554           0 :     m_aCurrentPDFState.m_aMapMode = rMapMode;
   10555           0 : }
   10556             : 
   10557           0 : void PDFWriterImpl::setClipRegion( const basegfx::B2DPolyPolygon& rRegion )
   10558             : {
   10559           0 :     basegfx::B2DPolyPolygon aRegion = getReferenceDevice()->LogicToPixel( rRegion, m_aGraphicsStack.front().m_aMapMode );
   10560           0 :     aRegion = getReferenceDevice()->PixelToLogic( aRegion, m_aMapMode );
   10561           0 :     m_aGraphicsStack.front().m_aClipRegion = aRegion;
   10562           0 :     m_aGraphicsStack.front().m_bClipRegion = true;
   10563           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10564           0 : }
   10565             : 
   10566           0 : void PDFWriterImpl::moveClipRegion( sal_Int32 nX, sal_Int32 nY )
   10567             : {
   10568           0 :     if( m_aGraphicsStack.front().m_bClipRegion && m_aGraphicsStack.front().m_aClipRegion.count() )
   10569             :     {
   10570           0 :         Point aPoint( lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10571             :                                    m_aMapMode,
   10572             :                                    getReferenceDevice(),
   10573           0 :                                    Point( nX, nY ) ) );
   10574           0 :         aPoint -= lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10575             :                                m_aMapMode,
   10576             :                                getReferenceDevice(),
   10577           0 :                                Point() );
   10578           0 :         basegfx::B2DHomMatrix aMat;
   10579           0 :         aMat.translate( aPoint.X(), aPoint.Y() );
   10580           0 :         m_aGraphicsStack.front().m_aClipRegion.transform( aMat );
   10581           0 :         m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10582             :     }
   10583           0 : }
   10584             : 
   10585           0 : bool PDFWriterImpl::intersectClipRegion( const Rectangle& rRect )
   10586             : {
   10587             :     basegfx::B2DPolyPolygon aRect( basegfx::tools::createPolygonFromRect(
   10588           0 :         basegfx::B2DRectangle( rRect.Left(), rRect.Top(), rRect.Right(), rRect.Bottom() ) ) );
   10589           0 :     return intersectClipRegion( aRect );
   10590             : }
   10591             : 
   10592             : 
   10593           0 : bool PDFWriterImpl::intersectClipRegion( const basegfx::B2DPolyPolygon& rRegion )
   10594             : {
   10595           0 :     basegfx::B2DPolyPolygon aRegion( getReferenceDevice()->LogicToPixel( rRegion, m_aGraphicsStack.front().m_aMapMode ) );
   10596           0 :     aRegion = getReferenceDevice()->PixelToLogic( aRegion, m_aMapMode );
   10597           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10598           0 :     if( m_aGraphicsStack.front().m_bClipRegion )
   10599             :     {
   10600           0 :         basegfx::B2DPolyPolygon aOld( basegfx::tools::prepareForPolygonOperation( m_aGraphicsStack.front().m_aClipRegion ) );
   10601           0 :         aRegion = basegfx::tools::prepareForPolygonOperation( aRegion );
   10602           0 :         m_aGraphicsStack.front().m_aClipRegion = basegfx::tools::solvePolygonOperationAnd( aOld, aRegion );
   10603             :     }
   10604             :     else
   10605             :     {
   10606           0 :         m_aGraphicsStack.front().m_aClipRegion = aRegion;
   10607           0 :         m_aGraphicsStack.front().m_bClipRegion = true;
   10608             :     }
   10609           0 :     return true;
   10610             : }
   10611             : 
   10612           0 : void PDFWriterImpl::createNote( const Rectangle& rRect, const PDFNote& rNote, sal_Int32 nPageNr )
   10613             : {
   10614           0 :     if( nPageNr < 0 )
   10615           0 :         nPageNr = m_nCurrentPage;
   10616             : 
   10617           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10618           0 :         return;
   10619             : 
   10620           0 :     m_aNotes.push_back( PDFNoteEntry() );
   10621           0 :     m_aNotes.back().m_nObject       = createObject();
   10622           0 :     m_aNotes.back().m_aContents     = rNote;
   10623           0 :     m_aNotes.back().m_aRect         = rRect;
   10624             :     // convert to default user space now, since the mapmode may change
   10625           0 :     m_aPages[nPageNr].convertRect( m_aNotes.back().m_aRect );
   10626             : 
   10627             :     // insert note to page's annotation list
   10628           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( m_aNotes.back().m_nObject );
   10629             : }
   10630             : 
   10631           0 : sal_Int32 PDFWriterImpl::createLink( const Rectangle& rRect, sal_Int32 nPageNr )
   10632             : {
   10633           0 :     if( nPageNr < 0 )
   10634           0 :         nPageNr = m_nCurrentPage;
   10635             : 
   10636           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10637           0 :         return -1;
   10638             : 
   10639           0 :     sal_Int32 nRet = m_aLinks.size();
   10640             : 
   10641           0 :     m_aLinks.push_back( PDFLink() );
   10642           0 :     m_aLinks.back().m_nObject   = createObject();
   10643           0 :     m_aLinks.back().m_nPage     = nPageNr;
   10644           0 :     m_aLinks.back().m_aRect     = rRect;
   10645             :     // convert to default user space now, since the mapmode may change
   10646           0 :     m_aPages[nPageNr].convertRect( m_aLinks.back().m_aRect );
   10647             : 
   10648             :     // insert link to page's annotation list
   10649           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( m_aLinks.back().m_nObject );
   10650             : 
   10651           0 :     return nRet;
   10652             : }
   10653             : 
   10654             : //--->i56629
   10655           0 : sal_Int32 PDFWriterImpl::createNamedDest( const OUString& sDestName, const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10656             : {
   10657           0 :     if( nPageNr < 0 )
   10658           0 :         nPageNr = m_nCurrentPage;
   10659             : 
   10660           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10661           0 :         return -1;
   10662             : 
   10663           0 :     sal_Int32 nRet = m_aNamedDests.size();
   10664             : 
   10665           0 :     m_aNamedDests.push_back( PDFNamedDest() );
   10666           0 :     m_aNamedDests.back().m_aDestName = sDestName;
   10667           0 :     m_aNamedDests.back().m_nPage = nPageNr;
   10668           0 :     m_aNamedDests.back().m_eType = eType;
   10669           0 :     m_aNamedDests.back().m_aRect = rRect;
   10670             :     // convert to default user space now, since the mapmode may change
   10671           0 :     m_aPages[nPageNr].convertRect( m_aNamedDests.back().m_aRect );
   10672             : 
   10673           0 :     return nRet;
   10674             : }
   10675             : //<---i56629
   10676             : 
   10677           0 : sal_Int32 PDFWriterImpl::createDest( const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10678             : {
   10679           0 :     if( nPageNr < 0 )
   10680           0 :         nPageNr = m_nCurrentPage;
   10681             : 
   10682           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10683           0 :         return -1;
   10684             : 
   10685           0 :     sal_Int32 nRet = m_aDests.size();
   10686             : 
   10687           0 :     m_aDests.push_back( PDFDest() );
   10688           0 :     m_aDests.back().m_nPage = nPageNr;
   10689           0 :     m_aDests.back().m_eType = eType;
   10690           0 :     m_aDests.back().m_aRect = rRect;
   10691             :     // convert to default user space now, since the mapmode may change
   10692           0 :     m_aPages[nPageNr].convertRect( m_aDests.back().m_aRect );
   10693             : 
   10694           0 :     return nRet;
   10695             : }
   10696             : 
   10697           0 : sal_Int32 PDFWriterImpl::registerDestReference( sal_Int32 nDestId, const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10698             : {
   10699           0 :     return m_aDestinationIdTranslation[ nDestId ] = createDest( rRect, nPageNr, eType );
   10700             : }
   10701             : 
   10702           0 : sal_Int32 PDFWriterImpl::setLinkDest( sal_Int32 nLinkId, sal_Int32 nDestId )
   10703             : {
   10704           0 :     if( nLinkId < 0 || nLinkId >= (sal_Int32)m_aLinks.size() )
   10705           0 :         return -1;
   10706           0 :     if( nDestId < 0 || nDestId >= (sal_Int32)m_aDests.size() )
   10707           0 :         return -2;
   10708             : 
   10709           0 :     m_aLinks[ nLinkId ].m_nDest = nDestId;
   10710             : 
   10711           0 :     return 0;
   10712             : }
   10713             : 
   10714           0 : sal_Int32 PDFWriterImpl::setLinkURL( sal_Int32 nLinkId, const OUString& rURL )
   10715             : {
   10716           0 :     if( nLinkId < 0 || nLinkId >= (sal_Int32)m_aLinks.size() )
   10717           0 :         return -1;
   10718             : 
   10719           0 :     m_aLinks[ nLinkId ].m_nDest = -1;
   10720             : 
   10721             :     using namespace ::com::sun::star;
   10722             : 
   10723           0 :     if (!m_xTrans.is())
   10724             :     {
   10725           0 :         uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
   10726           0 :         m_xTrans = util::URLTransformer::create(xContext);;
   10727             :     }
   10728             : 
   10729           0 :     util::URL aURL;
   10730           0 :     aURL.Complete = rURL;
   10731             : 
   10732           0 :     m_xTrans->parseStrict( aURL );
   10733             : 
   10734           0 :     m_aLinks[ nLinkId ].m_aURL  = aURL.Complete;
   10735             : 
   10736           0 :     return 0;
   10737             : }
   10738             : 
   10739           0 : void PDFWriterImpl::setLinkPropertyId( sal_Int32 nLinkId, sal_Int32 nPropertyId )
   10740             : {
   10741           0 :     m_aLinkPropertyMap[ nPropertyId ] = nLinkId;
   10742           0 : }
   10743             : 
   10744           0 : sal_Int32 PDFWriterImpl::createOutlineItem( sal_Int32 nParent, const OUString& rText, sal_Int32 nDestID )
   10745             : {
   10746             :     // create new item
   10747           0 :     sal_Int32 nNewItem = m_aOutline.size();
   10748           0 :     m_aOutline.push_back( PDFOutlineEntry() );
   10749             : 
   10750             :     // set item attributes
   10751           0 :     setOutlineItemParent( nNewItem, nParent );
   10752           0 :     setOutlineItemText( nNewItem, rText );
   10753           0 :     setOutlineItemDest( nNewItem, nDestID );
   10754             : 
   10755           0 :     return nNewItem;
   10756             : }
   10757             : 
   10758           0 : sal_Int32 PDFWriterImpl::setOutlineItemParent( sal_Int32 nItem, sal_Int32 nNewParent )
   10759             : {
   10760           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() )
   10761           0 :         return -1;
   10762             : 
   10763           0 :     int nRet = 0;
   10764             : 
   10765           0 :     if( nNewParent < 0 || nNewParent >= (sal_Int32)m_aOutline.size() || nNewParent == nItem )
   10766             :     {
   10767           0 :         nNewParent = 0;
   10768           0 :         nRet = -2;
   10769             :     }
   10770             :     // remove item from previous parent
   10771           0 :     sal_Int32 nParentID = m_aOutline[ nItem ].m_nParentID;
   10772           0 :     if( nParentID >= 0 && nParentID < (sal_Int32)m_aOutline.size() )
   10773             :     {
   10774           0 :         PDFOutlineEntry& rParent = m_aOutline[ nParentID ];
   10775             : 
   10776           0 :         for( std::vector<sal_Int32>::iterator it = rParent.m_aChildren.begin();
   10777           0 :              it != rParent.m_aChildren.end(); ++it )
   10778             :         {
   10779           0 :             if( *it == nItem )
   10780             :             {
   10781           0 :                 rParent.m_aChildren.erase( it );
   10782           0 :                 break;
   10783             :             }
   10784             :         }
   10785             :     }
   10786             : 
   10787             :     // insert item to new parent's list of children
   10788           0 :     m_aOutline[ nNewParent ].m_aChildren.push_back( nItem );
   10789             : 
   10790           0 :     return nRet;
   10791             : }
   10792             : 
   10793           0 : sal_Int32 PDFWriterImpl::setOutlineItemText( sal_Int32 nItem, const OUString& rText )
   10794             : {
   10795           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() )
   10796           0 :         return -1;
   10797             : 
   10798           0 :     m_aOutline[ nItem ].m_aTitle = psp::WhitespaceToSpace( rText );
   10799           0 :     return 0;
   10800             : }
   10801             : 
   10802           0 : sal_Int32 PDFWriterImpl::setOutlineItemDest( sal_Int32 nItem, sal_Int32 nDestID )
   10803             : {
   10804           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() ) // item does not exist
   10805           0 :         return -1;
   10806           0 :     if( nDestID < 0 || nDestID >= (sal_Int32)m_aDests.size() ) // dest does not exist
   10807           0 :         return -2;
   10808           0 :     m_aOutline[nItem].m_nDestID = nDestID;
   10809           0 :     return 0;
   10810             : }
   10811             : 
   10812           0 : const sal_Char* PDFWriterImpl::getStructureTag( PDFWriter::StructElement eType )
   10813             : {
   10814           0 :     static std::map< PDFWriter::StructElement, const char* > aTagStrings;
   10815           0 :     if( aTagStrings.empty() )
   10816             :     {
   10817           0 :         aTagStrings[ PDFWriter::NonStructElement] = "NonStruct";
   10818           0 :         aTagStrings[ PDFWriter::Document ]      = "Document";
   10819           0 :         aTagStrings[ PDFWriter::Part ]          = "Part";
   10820           0 :         aTagStrings[ PDFWriter::Article ]       = "Art";
   10821           0 :         aTagStrings[ PDFWriter::Section ]       = "Sect";
   10822           0 :         aTagStrings[ PDFWriter::Division ]      = "Div";
   10823           0 :         aTagStrings[ PDFWriter::BlockQuote ]    = "BlockQuote";
   10824           0 :         aTagStrings[ PDFWriter::Caption ]       = "Caption";
   10825           0 :         aTagStrings[ PDFWriter::TOC ]           = "TOC";
   10826           0 :         aTagStrings[ PDFWriter::TOCI ]          = "TOCI";
   10827           0 :         aTagStrings[ PDFWriter::Index ]         = "Index";
   10828           0 :         aTagStrings[ PDFWriter::Paragraph ]     = "P";
   10829           0 :         aTagStrings[ PDFWriter::Heading ]       = "H";
   10830           0 :         aTagStrings[ PDFWriter::H1 ]            = "H1";
   10831           0 :         aTagStrings[ PDFWriter::H2 ]            = "H2";
   10832           0 :         aTagStrings[ PDFWriter::H3 ]            = "H3";
   10833           0 :         aTagStrings[ PDFWriter::H4 ]            = "H4";
   10834           0 :         aTagStrings[ PDFWriter::H5 ]            = "H5";
   10835           0 :         aTagStrings[ PDFWriter::H6 ]            = "H6";
   10836           0 :         aTagStrings[ PDFWriter::List ]          = "L";
   10837           0 :         aTagStrings[ PDFWriter::ListItem ]      = "LI";
   10838           0 :         aTagStrings[ PDFWriter::LILabel ]       = "Lbl";
   10839           0 :         aTagStrings[ PDFWriter::LIBody ]        = "LBody";
   10840           0 :         aTagStrings[ PDFWriter::Table ]         = "Table";
   10841           0 :         aTagStrings[ PDFWriter::TableRow ]      = "TR";
   10842           0 :         aTagStrings[ PDFWriter::TableHeader ]   = "TH";
   10843           0 :         aTagStrings[ PDFWriter::TableData ]     = "TD";
   10844           0 :         aTagStrings[ PDFWriter::Span ]          = "Span";
   10845           0 :         aTagStrings[ PDFWriter::Quote ]         = "Quote";
   10846           0 :         aTagStrings[ PDFWriter::Note ]          = "Note";
   10847           0 :         aTagStrings[ PDFWriter::Reference ]     = "Reference";
   10848           0 :         aTagStrings[ PDFWriter::BibEntry ]      = "BibEntry";
   10849           0 :         aTagStrings[ PDFWriter::Code ]          = "Code";
   10850           0 :         aTagStrings[ PDFWriter::Link ]          = "Link";
   10851           0 :         aTagStrings[ PDFWriter::Figure ]        = "Figure";
   10852           0 :         aTagStrings[ PDFWriter::Formula ]       = "Formula";
   10853           0 :         aTagStrings[ PDFWriter::Form ]          = "Form";
   10854             :     }
   10855             : 
   10856           0 :     std::map< PDFWriter::StructElement, const char* >::const_iterator it = aTagStrings.find( eType );
   10857             : 
   10858           0 :     return it != aTagStrings.end() ? it->second : "Div";
   10859             : }
   10860             : 
   10861           0 : void PDFWriterImpl::beginStructureElementMCSeq()
   10862             : {
   10863           0 :     if( m_bEmitStructure &&
   10864           0 :         m_nCurrentStructElement > 0 && // StructTreeRoot
   10865           0 :         ! m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // already opened sequence
   10866             :         )
   10867             :     {
   10868           0 :         PDFStructureElement& rEle = m_aStructure[ m_nCurrentStructElement ];
   10869           0 :         OStringBuffer aLine( 128 );
   10870           0 :         sal_Int32 nMCID = m_aPages[ m_nCurrentPage ].m_aMCIDParents.size();
   10871           0 :         aLine.append( "/" );
   10872           0 :         if( !rEle.m_aAlias.isEmpty() )
   10873           0 :             aLine.append( rEle.m_aAlias );
   10874             :         else
   10875           0 :             aLine.append( getStructureTag( rEle.m_eType ) );
   10876           0 :         aLine.append( "<</MCID " );
   10877           0 :         aLine.append( nMCID );
   10878           0 :         aLine.append( ">>BDC\n" );
   10879           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10880             : 
   10881             :         // update the element's content list
   10882             : #if OSL_DEBUG_LEVEL > 1
   10883             :         fprintf( stderr, "beginning marked content id %" SAL_PRIdINT32 " on page object %" SAL_PRIdINT32 ", structure first page = %" SAL_PRIdINT32 "\n",
   10884             :                  nMCID,
   10885             :                  m_aPages[ m_nCurrentPage ].m_nPageObject,
   10886             :                  rEle.m_nFirstPageObject );
   10887             : #endif
   10888           0 :         rEle.m_aKids.push_back( PDFStructureElementKid( nMCID, m_aPages[m_nCurrentPage].m_nPageObject ) );
   10889             :         // update the page's mcid parent list
   10890           0 :         m_aPages[ m_nCurrentPage ].m_aMCIDParents.push_back( rEle.m_nObject );
   10891             :         // mark element MC sequence as open
   10892           0 :         rEle.m_bOpenMCSeq = true;
   10893             :     }
   10894             :     // handle artifacts
   10895           0 :     else if( ! m_bEmitStructure && m_aContext.Tagged &&
   10896           0 :                m_nCurrentStructElement > 0 &&
   10897           0 :                m_aStructure[ m_nCurrentStructElement ].m_eType == PDFWriter::NonStructElement &&
   10898           0 :              ! m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // already opened sequence
   10899             :              )
   10900             :     {
   10901           0 :         OStringBuffer aLine( 128 );
   10902           0 :         aLine.append( "/Artifact BMC\n" );
   10903           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10904             :         // mark element MC sequence as open
   10905           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq = true;
   10906             :     }
   10907           0 : }
   10908             : 
   10909           0 : void PDFWriterImpl::endStructureElementMCSeq()
   10910             : {
   10911           0 :     if( m_nCurrentStructElement > 0 && // StructTreeRoot
   10912           0 :         ( m_bEmitStructure || m_aStructure[ m_nCurrentStructElement ].m_eType == PDFWriter::NonStructElement ) &&
   10913           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // must have an opened MC sequence
   10914             :         )
   10915             :     {
   10916           0 :         writeBuffer( "EMC\n", 4 );
   10917           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq = false;
   10918             :     }
   10919           0 : }
   10920             : 
   10921           0 : bool PDFWriterImpl::checkEmitStructure()
   10922             : {
   10923           0 :     bool bEmit = false;
   10924           0 :     if( m_aContext.Tagged )
   10925             :     {
   10926           0 :         bEmit = true;
   10927           0 :         sal_Int32 nEle = m_nCurrentStructElement;
   10928           0 :         while( nEle > 0 && nEle < sal_Int32(m_aStructure.size()) )
   10929             :         {
   10930           0 :             if( m_aStructure[ nEle ].m_eType == PDFWriter::NonStructElement )
   10931             :             {
   10932           0 :                 bEmit = false;
   10933           0 :                 break;
   10934             :             }
   10935           0 :             nEle = m_aStructure[ nEle ].m_nParentElement;
   10936             :         }
   10937             :     }
   10938           0 :     return bEmit;
   10939             : }
   10940             : 
   10941           0 : sal_Int32 PDFWriterImpl::beginStructureElement( PDFWriter::StructElement eType, const OUString& rAlias )
   10942             : {
   10943           0 :     if( m_nCurrentPage < 0 )
   10944           0 :         return -1;
   10945             : 
   10946           0 :     if( ! m_aContext.Tagged )
   10947           0 :         return -1;
   10948             : 
   10949             :     // close eventual current MC sequence
   10950           0 :     endStructureElementMCSeq();
   10951             : 
   10952           0 :     if( m_nCurrentStructElement == 0 &&
   10953           0 :         eType != PDFWriter::Document && eType != PDFWriter::NonStructElement )
   10954             :     {
   10955             :         // struct tree root hit, but not beginning document
   10956             :         // this might happen with setCurrentStructureElement
   10957             :         // silently insert structure into document again if one properly exists
   10958           0 :         if( ! m_aStructure[ 0 ].m_aChildren.empty() )
   10959             :         {
   10960           0 :             PDFWriter::StructElement childType = PDFWriter::NonStructElement;
   10961           0 :             sal_Int32 nNewCurElement = 0;
   10962           0 :             const std::list< sal_Int32 >& rRootChildren = m_aStructure[0].m_aChildren;
   10963           0 :             for( std::list< sal_Int32 >::const_iterator it = rRootChildren.begin();
   10964           0 :                  childType != PDFWriter::Document && it != rRootChildren.end(); ++it )
   10965             :             {
   10966           0 :                 nNewCurElement = *it;
   10967           0 :                 childType = m_aStructure[ nNewCurElement ].m_eType;
   10968             :             }
   10969           0 :             if( childType == PDFWriter::Document )
   10970             :             {
   10971           0 :                 m_nCurrentStructElement = nNewCurElement;
   10972             :                 DBG_ASSERT( 0, "Structure element inserted to StructTreeRoot that is not a document" );
   10973             :             }
   10974             :             else {
   10975             :                 OSL_FAIL( "document structure in disorder !" );
   10976             :             }
   10977             :         }
   10978             :         else {
   10979             :             OSL_FAIL( "PDF document structure MUST be contained in a Document element" );
   10980             :         }
   10981             :     }
   10982             : 
   10983           0 :     sal_Int32 nNewId = sal_Int32(m_aStructure.size());
   10984           0 :     m_aStructure.push_back( PDFStructureElement() );
   10985           0 :     PDFStructureElement& rEle = m_aStructure.back();
   10986           0 :     rEle.m_eType            = eType;
   10987           0 :     rEle.m_nOwnElement      = nNewId;
   10988           0 :     rEle.m_nParentElement   = m_nCurrentStructElement;
   10989           0 :     rEle.m_nFirstPageObject = m_aPages[ m_nCurrentPage ].m_nPageObject;
   10990           0 :     m_aStructure[ m_nCurrentStructElement ].m_aChildren.push_back( nNewId );
   10991           0 :     m_nCurrentStructElement = nNewId;
   10992             : 
   10993             :     // handle alias names
   10994           0 :     if( !rAlias.isEmpty() && eType != PDFWriter::NonStructElement )
   10995             :     {
   10996           0 :         OStringBuffer aNameBuf( rAlias.getLength() );
   10997           0 :         appendName( rAlias, aNameBuf );
   10998           0 :         OString aAliasName( aNameBuf.makeStringAndClear() );
   10999           0 :         rEle.m_aAlias = aAliasName;
   11000           0 :         m_aRoleMap[ aAliasName ] = getStructureTag( eType );
   11001             :     }
   11002             : 
   11003             : #if OSL_DEBUG_LEVEL > 1
   11004             :     OStringBuffer aLine( "beginStructureElement " );
   11005             :     aLine.append( m_nCurrentStructElement );
   11006             :     aLine.append( ": " );
   11007             :     aLine.append( getStructureTag( eType ) );
   11008             :     if( !rEle.m_aAlias.isEmpty() )
   11009             :     {
   11010             :         aLine.append( " aliased as \"" );
   11011             :         aLine.append( rEle.m_aAlias );
   11012             :         aLine.append( '\"' );
   11013             :     }
   11014             :     emitComment( aLine.getStr() );
   11015             : #endif
   11016             : 
   11017             :     // check whether to emit structure henceforth
   11018           0 :     m_bEmitStructure = checkEmitStructure();
   11019             : 
   11020           0 :     if( m_bEmitStructure ) // don't create nonexistant objects
   11021             :     {
   11022           0 :         rEle.m_nObject      = createObject();
   11023             :         // update parent's kids list
   11024           0 :         m_aStructure[ rEle.m_nParentElement ].m_aKids.push_back( rEle.m_nObject );
   11025             :     }
   11026           0 :     return nNewId;
   11027             : }
   11028             : 
   11029           0 : void PDFWriterImpl::endStructureElement()
   11030             : {
   11031           0 :     if( m_nCurrentPage < 0 )
   11032           0 :         return;
   11033             : 
   11034           0 :     if( ! m_aContext.Tagged )
   11035           0 :         return;
   11036             : 
   11037           0 :     if( m_nCurrentStructElement == 0 )
   11038             :     {
   11039             :         // hit the struct tree root, that means there is an endStructureElement
   11040             :         // without corresponding beginStructureElement
   11041           0 :         return;
   11042             :     }
   11043             : 
   11044             :     // end the marked content sequence
   11045           0 :     endStructureElementMCSeq();
   11046             : 
   11047             : #if OSL_DEBUG_LEVEL > 1
   11048             :     OStringBuffer aLine( "endStructureElement " );
   11049             :     aLine.append( m_nCurrentStructElement );
   11050             :     aLine.append( ": " );
   11051             :     aLine.append( getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ) );
   11052             :     if( !m_aStructure[ m_nCurrentStructElement ].m_aAlias.isEmpty() )
   11053             :     {
   11054             :         aLine.append( " aliased as \"" );
   11055             :         aLine.append( m_aStructure[ m_nCurrentStructElement ].m_aAlias );
   11056             :         aLine.append( '\"' );
   11057             :     }
   11058             : #endif
   11059             : 
   11060             :     // "end" the structure element, the parent becomes current element
   11061           0 :     m_nCurrentStructElement = m_aStructure[ m_nCurrentStructElement ].m_nParentElement;
   11062             : 
   11063             :     // check whether to emit structure henceforth
   11064           0 :     m_bEmitStructure = checkEmitStructure();
   11065             : 
   11066             : #if OSL_DEBUG_LEVEL > 1
   11067             :     if( m_bEmitStructure )
   11068             :         emitComment( aLine.getStr() );
   11069             : #endif
   11070             : }
   11071             : 
   11072             : //---> i94258
   11073             : /*
   11074             :  * This function adds an internal structure list container to overcome the 8191 elements array limitation
   11075             :  * in kids element emission.
   11076             :  * Recursive function
   11077             :  *
   11078             :  */
   11079           0 : void PDFWriterImpl::addInternalStructureContainer( PDFStructureElement& rEle )
   11080             : {
   11081           0 :     if( rEle.m_eType == PDFWriter::NonStructElement &&
   11082           0 :         rEle.m_nOwnElement != rEle.m_nParentElement )
   11083           0 :         return;
   11084             : 
   11085           0 :     for( std::list< sal_Int32 >::const_iterator it = rEle.m_aChildren.begin(); it != rEle.m_aChildren.end(); ++it )
   11086             :     {
   11087           0 :         if( *it > 0 && *it < sal_Int32(m_aStructure.size()) )
   11088             :         {
   11089           0 :             PDFStructureElement& rChild = m_aStructure[ *it ];
   11090           0 :             if( rChild.m_eType != PDFWriter::NonStructElement )
   11091             :             {
   11092             :                 //triggered when a child of the rEle element is found
   11093           0 :                 if( rChild.m_nParentElement == rEle.m_nOwnElement )
   11094           0 :                     addInternalStructureContainer( rChild );//examine the child
   11095             :                 else
   11096             :                 {
   11097             :                     OSL_FAIL( "PDFWriterImpl::addInternalStructureContainer: invalid child structure element" );
   11098             : #if OSL_DEBUG_LEVEL > 1
   11099             :                     fprintf( stderr, "PDFWriterImpl::addInternalStructureContainer: invalid child structure elemnt with id %" SAL_PRIdINT32 "\n", *it );
   11100             : #endif
   11101             :                 }
   11102             :             }
   11103             :         }
   11104             :         else
   11105             :         {
   11106             :             OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure id" );
   11107             : #if OSL_DEBUG_LEVEL > 1
   11108             :             fprintf( stderr, "PDFWriterImpl::addInternalStructureContainer: invalid child structure id %" SAL_PRIdINT32 "\n", *it );
   11109             : #endif
   11110             :         }
   11111             :     }
   11112             : 
   11113           0 :     if( rEle.m_nOwnElement != rEle.m_nParentElement )
   11114             :     {
   11115           0 :         if( !rEle.m_aKids.empty() )
   11116             :         {
   11117           0 :             if( rEle.m_aKids.size() > ncMaxPDFArraySize ) {
   11118             :                 //then we need to add the containers for the kids elements
   11119             :                 // a list to be used for the new kid element
   11120           0 :                 std::list< PDFStructureElementKid > aNewKids;
   11121           0 :                 std::list< sal_Int32 > aNewChildren;
   11122             : 
   11123             :                 // add Div in RoleMap, in case no one else did (TODO: is it needed? Is it dangerous?)
   11124           0 :                 OStringBuffer aNameBuf( "Div" );
   11125           0 :                 OString aAliasName( aNameBuf.makeStringAndClear() );
   11126           0 :                 m_aRoleMap[ aAliasName ] = getStructureTag( PDFWriter::Division );
   11127             : 
   11128           0 :                 while( rEle.m_aKids.size() > ncMaxPDFArraySize )
   11129             :                 {
   11130           0 :                     sal_Int32 nCurrentStructElement = rEle.m_nOwnElement;
   11131           0 :                     sal_Int32 nNewId = sal_Int32(m_aStructure.size());
   11132           0 :                     m_aStructure.push_back( PDFStructureElement() );
   11133           0 :                     PDFStructureElement& rEleNew = m_aStructure.back();
   11134           0 :                     rEleNew.m_aAlias            = aAliasName;
   11135           0 :                     rEleNew.m_eType             = PDFWriter::Division; // a new Div type container
   11136           0 :                     rEleNew.m_nOwnElement       = nNewId;
   11137           0 :                     rEleNew.m_nParentElement    = nCurrentStructElement;
   11138             :                     //inherit the same page as the first child to be reparented
   11139           0 :                     rEleNew.m_nFirstPageObject  = m_aStructure[ rEle.m_aChildren.front() ].m_nFirstPageObject;
   11140           0 :                     rEleNew.m_nObject           = createObject();//assign a PDF object number
   11141             :                     //add the object to the kid list of the parent
   11142           0 :                     aNewKids.push_back( PDFStructureElementKid( rEleNew.m_nObject ) );
   11143           0 :                     aNewChildren.push_back( nNewId );
   11144             : 
   11145           0 :                     std::list< sal_Int32 >::iterator aChildEndIt( rEle.m_aChildren.begin() );
   11146           0 :                     std::list< PDFStructureElementKid >::iterator aKidEndIt( rEle.m_aKids.begin() );
   11147           0 :                     advance( aChildEndIt, ncMaxPDFArraySize );
   11148           0 :                     advance( aKidEndIt, ncMaxPDFArraySize );
   11149             : 
   11150             :                     rEleNew.m_aKids.splice( rEleNew.m_aKids.begin(),
   11151             :                                             rEle.m_aKids,
   11152             :                                             rEle.m_aKids.begin(),
   11153           0 :                                             aKidEndIt );
   11154             :                     rEleNew.m_aChildren.splice( rEleNew.m_aChildren.begin(),
   11155             :                                                 rEle.m_aChildren,
   11156             :                                                 rEle.m_aChildren.begin(),
   11157           0 :                                                 aChildEndIt );
   11158             :                     // set the kid's new parent
   11159           0 :                     for( std::list< sal_Int32 >::const_iterator it = rEleNew.m_aChildren.begin();
   11160           0 :                          it != rEleNew.m_aChildren.end(); ++it )
   11161             :                     {
   11162           0 :                         m_aStructure[ *it ].m_nParentElement = nNewId;
   11163             :                     }
   11164             :                 }
   11165             :                 //finally add the new kids resulting from the container added
   11166           0 :                 rEle.m_aKids.insert( rEle.m_aKids.begin(), aNewKids.begin(), aNewKids.end() );
   11167           0 :                 rEle.m_aChildren.insert( rEle.m_aChildren.begin(), aNewChildren.begin(), aNewChildren.end() );
   11168             :             }
   11169             :         }
   11170             :     }
   11171             : }
   11172             : //<--- i94258
   11173             : 
   11174           0 : bool PDFWriterImpl::setCurrentStructureElement( sal_Int32 nEle )
   11175             : {
   11176           0 :     bool bSuccess = false;
   11177             : 
   11178           0 :     if( m_aContext.Tagged && nEle >= 0 && nEle < sal_Int32(m_aStructure.size()) )
   11179             :     {
   11180             :         // end eventual previous marked content sequence
   11181           0 :         endStructureElementMCSeq();
   11182             : 
   11183           0 :         m_nCurrentStructElement = nEle;
   11184           0 :         m_bEmitStructure = checkEmitStructure();
   11185             : #if OSL_DEBUG_LEVEL > 1
   11186             :         OStringBuffer aLine( "setCurrentStructureElement " );
   11187             :         aLine.append( m_nCurrentStructElement );
   11188             :         aLine.append( ": " );
   11189             :         aLine.append( getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ) );
   11190             :         if( !m_aStructure[ m_nCurrentStructElement ].m_aAlias.isEmpty() )
   11191             :         {
   11192             :             aLine.append( " aliased as \"" );
   11193             :             aLine.append( m_aStructure[ m_nCurrentStructElement ].m_aAlias );
   11194             :             aLine.append( '\"' );
   11195             :         }
   11196             :         if( ! m_bEmitStructure )
   11197             :             aLine.append( " (inside NonStruct)" );
   11198             :         emitComment( aLine.getStr() );
   11199             : #endif
   11200           0 :         bSuccess = true;
   11201             :     }
   11202             : 
   11203           0 :     return bSuccess;
   11204             : }
   11205             : 
   11206           0 : bool PDFWriterImpl::setStructureAttribute( enum PDFWriter::StructAttribute eAttr, enum PDFWriter::StructAttributeValue eVal )
   11207             : {
   11208           0 :     if( !m_aContext.Tagged )
   11209           0 :         return false;
   11210             : 
   11211           0 :     bool bInsert = false;
   11212           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11213             :     {
   11214           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11215           0 :         switch( eAttr )
   11216             :         {
   11217             :             case PDFWriter::Placement:
   11218           0 :                 if( eVal == PDFWriter::Block        ||
   11219           0 :                     eVal == PDFWriter::Inline       ||
   11220           0 :                     eVal == PDFWriter::Before       ||
   11221           0 :                     eVal == PDFWriter::Start        ||
   11222             :                     eVal == PDFWriter::End )
   11223           0 :                     bInsert = true;
   11224           0 :                 break;
   11225             :             case PDFWriter::WritingMode:
   11226           0 :                 if( eVal == PDFWriter::LrTb         ||
   11227           0 :                     eVal == PDFWriter::RlTb         ||
   11228             :                     eVal == PDFWriter::TbRl )
   11229             :                 {
   11230           0 :                     bInsert = true;
   11231             :                 }
   11232           0 :                 break;
   11233             :             case PDFWriter::TextAlign:
   11234           0 :                 if( eVal == PDFWriter::Start        ||
   11235           0 :                     eVal == PDFWriter::Center       ||
   11236           0 :                     eVal == PDFWriter::End          ||
   11237             :                     eVal == PDFWriter::Justify )
   11238             :                 {
   11239           0 :                     if( eType == PDFWriter::Paragraph   ||
   11240           0 :                         eType == PDFWriter::Heading     ||
   11241           0 :                         eType == PDFWriter::H1          ||
   11242           0 :                         eType == PDFWriter::H2          ||
   11243           0 :                         eType == PDFWriter::H3          ||
   11244           0 :                         eType == PDFWriter::H4          ||
   11245           0 :                         eType == PDFWriter::H5          ||
   11246           0 :                         eType == PDFWriter::H6          ||
   11247           0 :                         eType == PDFWriter::List        ||
   11248           0 :                         eType == PDFWriter::ListItem    ||
   11249           0 :                         eType == PDFWriter::LILabel     ||
   11250           0 :                         eType == PDFWriter::LIBody      ||
   11251           0 :                         eType == PDFWriter::Table       ||
   11252           0 :                         eType == PDFWriter::TableRow    ||
   11253           0 :                         eType == PDFWriter::TableHeader ||
   11254             :                         eType == PDFWriter::TableData )
   11255             :                     {
   11256           0 :                         bInsert = true;
   11257             :                     }
   11258             :                 }
   11259           0 :                 break;
   11260             :             case PDFWriter::Width:
   11261             :             case PDFWriter::Height:
   11262           0 :                 if( eVal == PDFWriter::Auto )
   11263             :                 {
   11264           0 :                     if( eType == PDFWriter::Figure      ||
   11265           0 :                         eType == PDFWriter::Formula     ||
   11266           0 :                         eType == PDFWriter::Form        ||
   11267           0 :                         eType == PDFWriter::Table       ||
   11268           0 :                         eType == PDFWriter::TableHeader ||
   11269             :                         eType == PDFWriter::TableData )
   11270             :                     {
   11271           0 :                         bInsert = true;
   11272             :                     }
   11273             :                 }
   11274           0 :                 break;
   11275             :             case PDFWriter::BlockAlign:
   11276           0 :                 if( eVal == PDFWriter::Before       ||
   11277           0 :                     eVal == PDFWriter::Middle       ||
   11278           0 :                     eVal == PDFWriter::After        ||
   11279             :                     eVal == PDFWriter::Justify )
   11280             :                 {
   11281           0 :                     if( eType == PDFWriter::TableHeader ||
   11282             :                         eType == PDFWriter::TableData )
   11283             :                     {
   11284           0 :                         bInsert = true;
   11285             :                     }
   11286             :                 }
   11287           0 :                 break;
   11288             :             case PDFWriter::InlineAlign:
   11289           0 :                 if( eVal == PDFWriter::Start        ||
   11290           0 :                     eVal == PDFWriter::Center       ||
   11291             :                     eVal == PDFWriter::End )
   11292             :                 {
   11293           0 :                     if( eType == PDFWriter::TableHeader ||
   11294             :                         eType == PDFWriter::TableData )
   11295             :                     {
   11296           0 :                         bInsert = true;
   11297             :                     }
   11298             :                 }
   11299           0 :                 break;
   11300             :             case PDFWriter::LineHeight:
   11301           0 :                 if( eVal == PDFWriter::Normal       ||
   11302             :                     eVal == PDFWriter::Auto )
   11303             :                 {
   11304             :                     // only for ILSE and BLSE
   11305           0 :                     if( eType == PDFWriter::Paragraph   ||
   11306           0 :                         eType == PDFWriter::Heading     ||
   11307           0 :                         eType == PDFWriter::H1          ||
   11308           0 :                         eType == PDFWriter::H2          ||
   11309           0 :                         eType == PDFWriter::H3          ||
   11310           0 :                         eType == PDFWriter::H4          ||
   11311           0 :                         eType == PDFWriter::H5          ||
   11312           0 :                         eType == PDFWriter::H6          ||
   11313           0 :                         eType == PDFWriter::List        ||
   11314           0 :                         eType == PDFWriter::ListItem    ||
   11315           0 :                         eType == PDFWriter::LILabel     ||
   11316           0 :                         eType == PDFWriter::LIBody      ||
   11317           0 :                         eType == PDFWriter::Table       ||
   11318           0 :                         eType == PDFWriter::TableRow    ||
   11319           0 :                         eType == PDFWriter::TableHeader ||
   11320           0 :                         eType == PDFWriter::TableData   ||
   11321           0 :                         eType == PDFWriter::Span        ||
   11322           0 :                         eType == PDFWriter::Quote       ||
   11323           0 :                         eType == PDFWriter::Note        ||
   11324           0 :                         eType == PDFWriter::Reference   ||
   11325           0 :                         eType == PDFWriter::BibEntry    ||
   11326           0 :                         eType == PDFWriter::Code        ||
   11327             :                         eType == PDFWriter::Link )
   11328             :                     {
   11329           0 :                         bInsert = true;
   11330             :                     }
   11331             :                 }
   11332           0 :                 break;
   11333             :             case PDFWriter::TextDecorationType:
   11334           0 :                 if( eVal == PDFWriter::NONE         ||
   11335           0 :                     eVal == PDFWriter::Underline    ||
   11336           0 :                     eVal == PDFWriter::Overline     ||
   11337             :                     eVal == PDFWriter::LineThrough )
   11338             :                 {
   11339             :                     // only for ILSE and BLSE
   11340           0 :                     if( eType == PDFWriter::Paragraph   ||
   11341           0 :                         eType == PDFWriter::Heading     ||
   11342           0 :                         eType == PDFWriter::H1          ||
   11343           0 :                         eType == PDFWriter::H2          ||
   11344           0 :                         eType == PDFWriter::H3          ||
   11345           0 :                         eType == PDFWriter::H4          ||
   11346           0 :                         eType == PDFWriter::H5          ||
   11347           0 :                         eType == PDFWriter::H6          ||
   11348           0 :                         eType == PDFWriter::List        ||
   11349           0 :                         eType == PDFWriter::ListItem    ||
   11350           0 :                         eType == PDFWriter::LILabel     ||
   11351           0 :                         eType == PDFWriter::LIBody      ||
   11352           0 :                         eType == PDFWriter::Table       ||
   11353           0 :                         eType == PDFWriter::TableRow    ||
   11354           0 :                         eType == PDFWriter::TableHeader ||
   11355           0 :                         eType == PDFWriter::TableData   ||
   11356           0 :                         eType == PDFWriter::Span        ||
   11357           0 :                         eType == PDFWriter::Quote       ||
   11358           0 :                         eType == PDFWriter::Note        ||
   11359           0 :                         eType == PDFWriter::Reference   ||
   11360           0 :                         eType == PDFWriter::BibEntry    ||
   11361           0 :                         eType == PDFWriter::Code        ||
   11362             :                         eType == PDFWriter::Link )
   11363             :                     {
   11364           0 :                         bInsert = true;
   11365             :                     }
   11366             :                 }
   11367           0 :                 break;
   11368             :             case PDFWriter::ListNumbering:
   11369           0 :                 if( eVal == PDFWriter::NONE         ||
   11370           0 :                     eVal == PDFWriter::Disc         ||
   11371           0 :                     eVal == PDFWriter::Circle       ||
   11372           0 :                     eVal == PDFWriter::Square       ||
   11373           0 :                     eVal == PDFWriter::Decimal      ||
   11374           0 :                     eVal == PDFWriter::UpperRoman   ||
   11375           0 :                     eVal == PDFWriter::LowerRoman   ||
   11376           0 :                     eVal == PDFWriter::UpperAlpha   ||
   11377             :                     eVal == PDFWriter::LowerAlpha )
   11378             :                 {
   11379           0 :                     if( eType == PDFWriter::List )
   11380           0 :                         bInsert = true;
   11381             :                 }
   11382           0 :                 break;
   11383           0 :             default: break;
   11384             :         }
   11385             :     }
   11386             : 
   11387           0 :     if( bInsert )
   11388           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAttributes[ eAttr ] = PDFStructureAttribute( eVal );
   11389             : #if OSL_DEBUG_LEVEL > 1
   11390             :     else if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11391             :         fprintf( stderr, "rejecting setStructureAttribute( %s, %s ) on %s (%s) element\n",
   11392             :                  getAttributeTag( eAttr ),
   11393             :                  getAttributeValueTag( eVal ),
   11394             :                  getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ),
   11395             :                  m_aStructure[ m_nCurrentStructElement ].m_aAlias.getStr()
   11396             :                  );
   11397             : #endif
   11398             : 
   11399           0 :     return bInsert;
   11400             : }
   11401             : 
   11402           0 : bool PDFWriterImpl::setStructureAttributeNumerical( enum PDFWriter::StructAttribute eAttr, sal_Int32 nValue )
   11403             : {
   11404           0 :     if( ! m_aContext.Tagged )
   11405           0 :         return false;
   11406             : 
   11407           0 :     bool bInsert = false;
   11408           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11409             :     {
   11410           0 :         if( eAttr == PDFWriter::Language )
   11411             :         {
   11412           0 :             m_aStructure[ m_nCurrentStructElement ].m_aLocale = LanguageTag( (LanguageType)nValue ).getLocale();
   11413           0 :             return true;
   11414             :         }
   11415             : 
   11416           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11417           0 :         switch( eAttr )
   11418             :         {
   11419             :             case PDFWriter::SpaceBefore:
   11420             :             case PDFWriter::SpaceAfter:
   11421             :             case PDFWriter::StartIndent:
   11422             :             case PDFWriter::EndIndent:
   11423             :                 // just for BLSE
   11424           0 :                 if( eType == PDFWriter::Paragraph   ||
   11425           0 :                     eType == PDFWriter::Heading     ||
   11426           0 :                     eType == PDFWriter::H1          ||
   11427           0 :                     eType == PDFWriter::H2          ||
   11428           0 :                     eType == PDFWriter::H3          ||
   11429           0 :                     eType == PDFWriter::H4          ||
   11430           0 :                     eType == PDFWriter::H5          ||
   11431           0 :                     eType == PDFWriter::H6          ||
   11432           0 :                     eType == PDFWriter::List        ||
   11433           0 :                     eType == PDFWriter::ListItem    ||
   11434           0 :                     eType == PDFWriter::LILabel     ||
   11435           0 :                     eType == PDFWriter::LIBody      ||
   11436           0 :                     eType == PDFWriter::Table       ||
   11437           0 :                     eType == PDFWriter::TableRow    ||
   11438           0 :                     eType == PDFWriter::TableHeader ||
   11439             :                     eType == PDFWriter::TableData )
   11440             :                 {
   11441           0 :                     bInsert = true;
   11442             :                 }
   11443           0 :                 break;
   11444             :             case PDFWriter::TextIndent:
   11445             :                 // paragraph like BLSE and additional elements
   11446           0 :                 if( eType == PDFWriter::Paragraph   ||
   11447           0 :                     eType == PDFWriter::Heading     ||
   11448           0 :                     eType == PDFWriter::H1          ||
   11449           0 :                     eType == PDFWriter::H2          ||
   11450           0 :                     eType == PDFWriter::H3          ||
   11451           0 :                     eType == PDFWriter::H4          ||
   11452           0 :                     eType == PDFWriter::H5          ||
   11453           0 :                     eType == PDFWriter::H6          ||
   11454           0 :                     eType == PDFWriter::LILabel     ||
   11455           0 :                     eType == PDFWriter::LIBody      ||
   11456           0 :                     eType == PDFWriter::TableHeader ||
   11457             :                     eType == PDFWriter::TableData )
   11458             :                 {
   11459           0 :                     bInsert = true;
   11460             :                 }
   11461           0 :                 break;
   11462             :             case PDFWriter::Width:
   11463             :             case PDFWriter::Height:
   11464           0 :                 if( eType == PDFWriter::Figure      ||
   11465           0 :                     eType == PDFWriter::Formula     ||
   11466           0 :                     eType == PDFWriter::Form        ||
   11467           0 :                     eType == PDFWriter::Table       ||
   11468           0 :                     eType == PDFWriter::TableHeader ||
   11469             :                     eType == PDFWriter::TableData )
   11470             :                 {
   11471           0 :                     bInsert = true;
   11472             :                 }
   11473           0 :                 break;
   11474             :             case PDFWriter::LineHeight:
   11475             :             case PDFWriter::BaselineShift:
   11476             :                 // only for ILSE and BLSE
   11477           0 :                 if( eType == PDFWriter::Paragraph   ||
   11478           0 :                     eType == PDFWriter::Heading     ||
   11479           0 :                     eType == PDFWriter::H1          ||
   11480           0 :                     eType == PDFWriter::H2          ||
   11481           0 :                     eType == PDFWriter::H3          ||
   11482           0 :                     eType == PDFWriter::H4          ||
   11483           0 :                     eType == PDFWriter::H5          ||
   11484           0 :                     eType == PDFWriter::H6          ||
   11485           0 :                     eType == PDFWriter::List        ||
   11486           0 :                     eType == PDFWriter::ListItem    ||
   11487           0 :                     eType == PDFWriter::LILabel     ||
   11488           0 :                     eType == PDFWriter::LIBody      ||
   11489           0 :                     eType == PDFWriter::Table       ||
   11490           0 :                     eType == PDFWriter::TableRow    ||
   11491           0 :                     eType == PDFWriter::TableHeader ||
   11492           0 :                     eType == PDFWriter::TableData   ||
   11493           0 :                     eType == PDFWriter::Span        ||
   11494           0 :                     eType == PDFWriter::Quote       ||
   11495           0 :                     eType == PDFWriter::Note        ||
   11496           0 :                     eType == PDFWriter::Reference   ||
   11497           0 :                     eType == PDFWriter::BibEntry    ||
   11498           0 :                     eType == PDFWriter::Code        ||
   11499             :                     eType == PDFWriter::Link )
   11500             :                 {
   11501           0 :                         bInsert = true;
   11502             :                 }
   11503           0 :                 break;
   11504             :             case PDFWriter::RowSpan:
   11505             :             case PDFWriter::ColSpan:
   11506             :                 // only for table cells
   11507           0 :                 if( eType == PDFWriter::TableHeader ||
   11508             :                     eType == PDFWriter::TableData )
   11509             :                 {
   11510           0 :                     bInsert = true;
   11511             :                 }
   11512           0 :                 break;
   11513             :             case PDFWriter::LinkAnnotation:
   11514           0 :                 if( eType == PDFWriter::Link )
   11515           0 :                     bInsert = true;
   11516           0 :                 break;
   11517           0 :             default: break;
   11518             :         }
   11519             :     }
   11520             : 
   11521           0 :     if( bInsert )
   11522           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAttributes[ eAttr ] = PDFStructureAttribute( nValue );
   11523             : #if OSL_DEBUG_LEVEL > 1
   11524             :     else if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11525             :         fprintf( stderr, "rejecting setStructureAttributeNumerical( %s, %d ) on %s (%s) element\n",
   11526             :                  getAttributeTag( eAttr ),
   11527             :                  (int)nValue,
   11528             :                  getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ),
   11529             :                  m_aStructure[ m_nCurrentStructElement ].m_aAlias.getStr() );
   11530             : #endif
   11531             : 
   11532           0 :     return bInsert;
   11533             : }
   11534             : 
   11535           0 : void PDFWriterImpl::setStructureBoundingBox( const Rectangle& rRect )
   11536             : {
   11537           0 :     sal_Int32 nPageNr = m_nCurrentPage;
   11538           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() || !m_aContext.Tagged )
   11539           0 :         return;
   11540             : 
   11541             : 
   11542           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11543             :     {
   11544           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11545           0 :         if( eType == PDFWriter::Figure      ||
   11546           0 :             eType == PDFWriter::Formula     ||
   11547           0 :             eType == PDFWriter::Form        ||
   11548             :             eType == PDFWriter::Table )
   11549             :         {
   11550           0 :             m_aStructure[ m_nCurrentStructElement ].m_aBBox = rRect;
   11551             :             // convert to default user space now, since the mapmode may change
   11552           0 :             m_aPages[nPageNr].convertRect( m_aStructure[ m_nCurrentStructElement ].m_aBBox );
   11553             :         }
   11554             :     }
   11555             : }
   11556             : 
   11557           0 : void PDFWriterImpl::setActualText( const String& rText )
   11558             : {
   11559           0 :     if( m_aContext.Tagged && m_nCurrentStructElement > 0 && m_bEmitStructure )
   11560             :     {
   11561           0 :         m_aStructure[ m_nCurrentStructElement ].m_aActualText = rText;
   11562             :     }
   11563           0 : }
   11564             : 
   11565           0 : void PDFWriterImpl::setAlternateText( const String& rText )
   11566             : {
   11567           0 :     if( m_aContext.Tagged && m_nCurrentStructElement > 0 && m_bEmitStructure )
   11568             :     {
   11569           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAltText = rText;
   11570             :     }
   11571           0 : }
   11572             : 
   11573           0 : void PDFWriterImpl::setAutoAdvanceTime( sal_uInt32 nSeconds, sal_Int32 nPageNr )
   11574             : {
   11575           0 :     if( nPageNr < 0 )
   11576           0 :         nPageNr = m_nCurrentPage;
   11577             : 
   11578           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11579           0 :         return;
   11580             : 
   11581           0 :     m_aPages[ nPageNr ].m_nDuration = nSeconds;
   11582             : }
   11583             : 
   11584           0 : void PDFWriterImpl::setPageTransition( PDFWriter::PageTransition eType, sal_uInt32 nMilliSec, sal_Int32 nPageNr )
   11585             : {
   11586           0 :     if( nPageNr < 0 )
   11587           0 :         nPageNr = m_nCurrentPage;
   11588             : 
   11589           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11590           0 :         return;
   11591             : 
   11592           0 :     m_aPages[ nPageNr ].m_eTransition   = eType;
   11593           0 :     m_aPages[ nPageNr ].m_nTransTime    = nMilliSec;
   11594             : }
   11595             : 
   11596           0 : void PDFWriterImpl::ensureUniqueRadioOnValues()
   11597             : {
   11598             :     // loop over radio groups
   11599           0 :     for( std::map<sal_Int32,sal_Int32>::const_iterator group = m_aRadioGroupWidgets.begin();
   11600           0 :          group != m_aRadioGroupWidgets.end(); ++group )
   11601             :     {
   11602           0 :         PDFWidget& rGroupWidget = m_aWidgets[ group->second ];
   11603             :         // check whether all kids have a unique OnValue
   11604           0 :         boost::unordered_map< OUString, sal_Int32, OUStringHash > aOnValues;
   11605           0 :         int nChildren = rGroupWidget.m_aKidsIndex.size();
   11606           0 :         bool bIsUnique = true;
   11607           0 :         for( int nKid = 0; nKid < nChildren && bIsUnique; nKid++ )
   11608             :         {
   11609           0 :             int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11610           0 :             const OUString& rVal = m_aWidgets[nKidIndex].m_aOnValue;
   11611             :             #if OSL_DEBUG_LEVEL > 1
   11612             :             fprintf( stderr, "OnValue: %s\n", OUStringToOString( rVal, RTL_TEXTENCODING_UTF8 ).getStr() );
   11613             :             #endif
   11614           0 :             if( aOnValues.find( rVal ) == aOnValues.end() )
   11615             :             {
   11616           0 :                 aOnValues[ rVal ] = 1;
   11617             :             }
   11618             :             else
   11619             :             {
   11620           0 :                 bIsUnique = false;
   11621             :             }
   11622             :         }
   11623           0 :         if( ! bIsUnique )
   11624             :         {
   11625             :             #if OSL_DEBUG_LEVEL > 1
   11626             :             fprintf( stderr, "enforcing unique OnValues\n" );
   11627             :             #endif
   11628             :             // make unique by using ascending OnValues
   11629           0 :             for( int nKid = 0; nKid < nChildren; nKid++ )
   11630             :             {
   11631           0 :                 int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11632           0 :                 PDFWidget& rKid = m_aWidgets[nKidIndex];
   11633           0 :                 rKid.m_aOnValue = OUString::valueOf( sal_Int32(nKid+1) );
   11634           0 :                 if( rKid.m_aValue != "Off" )
   11635           0 :                     rKid.m_aValue = rKid.m_aOnValue;
   11636             :             }
   11637             :         }
   11638             :         // finally move the "Yes" appearance to the OnValue appearance
   11639           0 :         for( int nKid = 0; nKid < nChildren; nKid++ )
   11640             :         {
   11641           0 :             int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11642           0 :             PDFWidget& rKid = m_aWidgets[nKidIndex];
   11643           0 :             PDFAppearanceMap::iterator app_it = rKid.m_aAppearances.find( "N" );
   11644           0 :             if( app_it != rKid.m_aAppearances.end() )
   11645             :             {
   11646           0 :                 PDFAppearanceStreams::iterator stream_it = app_it->second.find( "Yes" );
   11647           0 :                 if( stream_it != app_it->second.end() )
   11648             :                 {
   11649           0 :                     SvMemoryStream* pStream = stream_it->second;
   11650           0 :                     app_it->second.erase( stream_it );
   11651           0 :                     OStringBuffer aBuf( rKid.m_aOnValue.getLength()*2 );
   11652           0 :                     appendName( rKid.m_aOnValue, aBuf );
   11653           0 :                     (app_it->second)[ aBuf.makeStringAndClear() ] = pStream;
   11654             :                 }
   11655             :                 #if OSL_DEBUG_LEVEL > 1
   11656             :                 else
   11657             :                     fprintf( stderr, "error: RadioButton without \"Yes\" stream\n" );
   11658             :                 #endif
   11659             :             }
   11660             :             // update selected radio button
   11661           0 :             if( rKid.m_aValue != "Off" )
   11662             :             {
   11663           0 :                 rGroupWidget.m_aValue = rKid.m_aValue;
   11664             :             }
   11665             :         }
   11666           0 :     }
   11667           0 : }
   11668             : 
   11669           0 : sal_Int32 PDFWriterImpl::findRadioGroupWidget( const PDFWriter::RadioButtonWidget& rBtn )
   11670             : {
   11671           0 :     sal_Int32 nRadioGroupWidget = -1;
   11672             : 
   11673           0 :     std::map< sal_Int32, sal_Int32 >::const_iterator it = m_aRadioGroupWidgets.find( rBtn.RadioGroup );
   11674             : 
   11675           0 :     if( it == m_aRadioGroupWidgets.end() )
   11676             :     {
   11677           0 :         m_aRadioGroupWidgets[ rBtn.RadioGroup ] = nRadioGroupWidget =
   11678           0 :             sal_Int32(m_aWidgets.size());
   11679             : 
   11680             :         // new group, insert the radiobutton
   11681           0 :         m_aWidgets.push_back( PDFWidget() );
   11682           0 :         m_aWidgets.back().m_nObject     = createObject();
   11683           0 :         m_aWidgets.back().m_nPage       = m_nCurrentPage;
   11684           0 :         m_aWidgets.back().m_eType       = PDFWriter::RadioButton;
   11685           0 :         m_aWidgets.back().m_nRadioGroup = rBtn.RadioGroup;
   11686           0 :         m_aWidgets.back().m_nFlags |= 0x0000C000;   // NoToggleToOff and Radio bits
   11687             : 
   11688           0 :         createWidgetFieldName( sal_Int32(m_aWidgets.size()-1), rBtn );
   11689             :     }
   11690             :     else
   11691           0 :         nRadioGroupWidget = it->second;
   11692             : 
   11693           0 :     return nRadioGroupWidget;
   11694             : }
   11695             : 
   11696           0 : sal_Int32 PDFWriterImpl::createControl( const PDFWriter::AnyWidget& rControl, 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 -1;
   11703             : 
   11704           0 :     bool sigHidden(true);
   11705           0 :     sal_Int32 nNewWidget = m_aWidgets.size();
   11706           0 :     m_aWidgets.push_back( PDFWidget() );
   11707             : 
   11708           0 :     m_aWidgets.back().m_nObject         = createObject();
   11709           0 :     m_aWidgets.back().m_aRect           = rControl.Location;
   11710           0 :     m_aWidgets.back().m_nPage           = nPageNr;
   11711           0 :     m_aWidgets.back().m_eType           = rControl.getType();
   11712             : 
   11713           0 :     sal_Int32 nRadioGroupWidget = -1;
   11714             :     // for unknown reasons the radio buttons of a radio group must not have a
   11715             :     // field name, else the buttons are in fact check boxes -
   11716             :     // that is multiple buttons of the radio group can be selected
   11717           0 :     if( rControl.getType() == PDFWriter::RadioButton )
   11718           0 :         nRadioGroupWidget = findRadioGroupWidget( static_cast<const PDFWriter::RadioButtonWidget&>(rControl) );
   11719             :     else
   11720             :     {
   11721           0 :         createWidgetFieldName( nNewWidget, rControl );
   11722             :     }
   11723             : 
   11724             :     // caution: m_aWidgets must not be changed after here or rNewWidget may be invalid
   11725           0 :     PDFWidget& rNewWidget           = m_aWidgets[nNewWidget];
   11726           0 :     rNewWidget.m_aDescription       = rControl.Description;
   11727           0 :     rNewWidget.m_aText              = rControl.Text;
   11728             :     rNewWidget.m_nTextStyle         = rControl.TextStyle &
   11729             :         (  TEXT_DRAW_LEFT | TEXT_DRAW_CENTER | TEXT_DRAW_RIGHT | TEXT_DRAW_TOP |
   11730             :            TEXT_DRAW_VCENTER | TEXT_DRAW_BOTTOM |
   11731           0 :            TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK  );
   11732           0 :     rNewWidget.m_nTabOrder          = rControl.TabOrder;
   11733             : 
   11734             :     // various properties are set via the flags (/Ff) property of the field dict
   11735           0 :     if( rControl.ReadOnly )
   11736           0 :         rNewWidget.m_nFlags |= 1;
   11737           0 :     if( rControl.getType() == PDFWriter::PushButton )
   11738             :     {
   11739           0 :         const PDFWriter::PushButtonWidget& rBtn = static_cast<const PDFWriter::PushButtonWidget&>(rControl);
   11740           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11741             :             rNewWidget.m_nTextStyle =
   11742             :                 TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER |
   11743           0 :                 TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11744             : 
   11745           0 :         rNewWidget.m_nFlags |= 0x00010000;
   11746           0 :         if( !rBtn.URL.isEmpty() )
   11747           0 :             rNewWidget.m_aListEntries.push_back( rBtn.URL );
   11748           0 :         rNewWidget.m_bSubmit    = rBtn.Submit;
   11749           0 :         rNewWidget.m_bSubmitGet = rBtn.SubmitGet;
   11750           0 :         rNewWidget.m_nDest      = rBtn.Dest;
   11751           0 :         createDefaultPushButtonAppearance( rNewWidget, rBtn );
   11752             :     }
   11753           0 :     else if( rControl.getType() == PDFWriter::RadioButton )
   11754             :     {
   11755           0 :         const PDFWriter::RadioButtonWidget& rBtn = static_cast<const PDFWriter::RadioButtonWidget&>(rControl);
   11756           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11757             :             rNewWidget.m_nTextStyle =
   11758           0 :                 TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11759             :         /*  PDF sees a RadioButton group as one radio button with
   11760             :          *  children which are in turn check boxes
   11761             :          *
   11762             :          *  so we need to create a radio button on demand for a new group
   11763             :          *  and insert a checkbox for each RadioButtonWidget as its child
   11764             :          */
   11765           0 :         rNewWidget.m_eType          = PDFWriter::CheckBox;
   11766           0 :         rNewWidget.m_nRadioGroup    = rBtn.RadioGroup;
   11767             : 
   11768             :         DBG_ASSERT( nRadioGroupWidget >= 0 && nRadioGroupWidget < (sal_Int32)m_aWidgets.size(), "no radio group parent" );
   11769             : 
   11770           0 :         PDFWidget& rRadioButton = m_aWidgets[nRadioGroupWidget];
   11771           0 :         rRadioButton.m_aKids.push_back( rNewWidget.m_nObject );
   11772           0 :         rRadioButton.m_aKidsIndex.push_back( nNewWidget );
   11773           0 :         rNewWidget.m_nParent = rRadioButton.m_nObject;
   11774             : 
   11775           0 :         rNewWidget.m_aValue     = OUString( "Off"  );
   11776           0 :         rNewWidget.m_aOnValue   = rBtn.OnValue;
   11777           0 :         if( rRadioButton.m_aValue.isEmpty() && rBtn.Selected )
   11778             :         {
   11779           0 :             rNewWidget.m_aValue     = rNewWidget.m_aOnValue;
   11780           0 :             rRadioButton.m_aValue   = rNewWidget.m_aOnValue;
   11781             :         }
   11782           0 :         createDefaultRadioButtonAppearance( rNewWidget, rBtn );
   11783             : 
   11784             :         // union rect of radio group
   11785           0 :         Rectangle aRect = rNewWidget.m_aRect;
   11786           0 :         m_aPages[ nPageNr ].convertRect( aRect );
   11787           0 :         rRadioButton.m_aRect.Union( aRect );
   11788             :     }
   11789           0 :     else if( rControl.getType() == PDFWriter::CheckBox )
   11790             :     {
   11791           0 :         const PDFWriter::CheckBoxWidget& rBox = static_cast<const PDFWriter::CheckBoxWidget&>(rControl);
   11792           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11793             :             rNewWidget.m_nTextStyle =
   11794           0 :                 TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11795             : 
   11796           0 :         rNewWidget.m_aValue = rBox.Checked ? OUString("Yes") : OUString("Off" );
   11797             :         // create default appearance before m_aRect gets transformed
   11798           0 :         createDefaultCheckBoxAppearance( rNewWidget, rBox );
   11799             :     }
   11800           0 :     else if( rControl.getType() == PDFWriter::ListBox )
   11801             :     {
   11802           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11803           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_VCENTER;
   11804             : 
   11805           0 :         const PDFWriter::ListBoxWidget& rLstBox = static_cast<const PDFWriter::ListBoxWidget&>(rControl);
   11806           0 :         rNewWidget.m_aListEntries     = rLstBox.Entries;
   11807           0 :         rNewWidget.m_aSelectedEntries = rLstBox.SelectedEntries;
   11808           0 :         rNewWidget.m_aValue           = rLstBox.Text;
   11809           0 :         if( rLstBox.DropDown )
   11810           0 :             rNewWidget.m_nFlags |= 0x00020000;
   11811           0 :         if( rLstBox.Sort )
   11812           0 :             rNewWidget.m_nFlags |= 0x00080000;
   11813           0 :         if( rLstBox.MultiSelect && !rLstBox.DropDown && (int)m_aContext.Version > (int)PDFWriter::PDF_1_3 )
   11814           0 :             rNewWidget.m_nFlags |= 0x00200000;
   11815             : 
   11816           0 :         createDefaultListBoxAppearance( rNewWidget, rLstBox );
   11817             :     }
   11818           0 :     else if( rControl.getType() == PDFWriter::ComboBox )
   11819             :     {
   11820           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11821           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_VCENTER;
   11822             : 
   11823           0 :         const PDFWriter::ComboBoxWidget& rBox = static_cast<const PDFWriter::ComboBoxWidget&>(rControl);
   11824           0 :         rNewWidget.m_aValue         = rBox.Text;
   11825           0 :         rNewWidget.m_aListEntries   = rBox.Entries;
   11826           0 :         rNewWidget.m_nFlags |= 0x00060000; // combo and edit flag
   11827           0 :         if( rBox.Sort )
   11828           0 :             rNewWidget.m_nFlags |= 0x00080000;
   11829             : 
   11830           0 :         PDFWriter::ListBoxWidget aLBox;
   11831           0 :         aLBox.Name              = rBox.Name;
   11832           0 :         aLBox.Description       = rBox.Description;
   11833           0 :         aLBox.Text              = rBox.Text;
   11834           0 :         aLBox.TextStyle         = rBox.TextStyle;
   11835           0 :         aLBox.ReadOnly          = rBox.ReadOnly;
   11836           0 :         aLBox.Border            = rBox.Border;
   11837           0 :         aLBox.BorderColor       = rBox.BorderColor;
   11838           0 :         aLBox.Background        = rBox.Background;
   11839           0 :         aLBox.BackgroundColor   = rBox.BackgroundColor;
   11840           0 :         aLBox.TextFont          = rBox.TextFont;
   11841           0 :         aLBox.TextColor         = rBox.TextColor;
   11842           0 :         aLBox.DropDown          = true;
   11843           0 :         aLBox.Sort              = rBox.Sort;
   11844           0 :         aLBox.MultiSelect       = false;
   11845           0 :         aLBox.Entries           = rBox.Entries;
   11846             : 
   11847           0 :         createDefaultListBoxAppearance( rNewWidget, aLBox );
   11848             :     }
   11849           0 :     else if( rControl.getType() == PDFWriter::Edit )
   11850             :     {
   11851           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11852           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER;
   11853             : 
   11854           0 :         const PDFWriter::EditWidget& rEdit = static_cast<const  PDFWriter::EditWidget&>(rControl);
   11855           0 :         if( rEdit.MultiLine )
   11856             :         {
   11857           0 :             rNewWidget.m_nFlags |= 0x00001000;
   11858           0 :             rNewWidget.m_nTextStyle |= TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11859             :         }
   11860           0 :         if( rEdit.Password )
   11861           0 :             rNewWidget.m_nFlags |= 0x00002000;
   11862           0 :         if( rEdit.FileSelect && m_aContext.Version > PDFWriter::PDF_1_3 )
   11863           0 :             rNewWidget.m_nFlags |= 0x00100000;
   11864           0 :         rNewWidget.m_nMaxLen = rEdit.MaxLen;
   11865           0 :         rNewWidget.m_aValue = rEdit.Text;
   11866             : 
   11867           0 :         createDefaultEditAppearance( rNewWidget, rEdit );
   11868             :     }
   11869             : #if !defined(ANDROID) && !defined(IOS)
   11870           0 :     else if( rControl.getType() == PDFWriter::Signature)
   11871             :     {
   11872           0 :         const PDFWriter::SignatureWidget& rSig = static_cast<const PDFWriter::SignatureWidget&>(rControl);
   11873           0 :         sigHidden = rSig.SigHidden;
   11874             : 
   11875           0 :         if ( sigHidden )
   11876           0 :             rNewWidget.m_aRect = Rectangle(0, 0, 0, 0);
   11877             : 
   11878           0 :         m_nSignatureObject = createObject();
   11879           0 :         rNewWidget.m_aValue = OUString::valueOf( m_nSignatureObject );
   11880           0 :         rNewWidget.m_aValue += " 0 R";
   11881             :         //createDefaultSignatureAppearance( rNewWidget, rSig );
   11882             :         // let's add a fake appearance
   11883           0 :         rNewWidget.m_aAppearances[ "N" ][ "Standard" ] = new SvMemoryStream();
   11884             :     }
   11885             : #endif
   11886             : 
   11887             :     // if control is a hidden signature, do not convert coordinates since we
   11888             :     // need /Rect [ 0 0 0 0 ]
   11889           0 :     if ( ! ( ( rControl.getType() == PDFWriter::Signature ) && ( sigHidden ) ) )
   11890             :     {
   11891             :         // convert to default user space now, since the mapmode may change
   11892             :         // note: create default appearances before m_aRect gets transformed
   11893           0 :         m_aPages[ nPageNr ].convertRect( rNewWidget.m_aRect );
   11894             :     }
   11895             : 
   11896             :     // insert widget to page's annotation list
   11897           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( rNewWidget.m_nObject );
   11898             : 
   11899             :     // mark page as having widgets
   11900           0 :     m_aPages[ nPageNr ].m_bHasWidgets = true;
   11901             : 
   11902           0 :     return nNewWidget;
   11903             : }
   11904             : 
   11905           0 : void PDFWriterImpl::addStream( const String& rMimeType, PDFOutputStream* pStream, bool bCompress )
   11906             : {
   11907           0 :     if( pStream )
   11908             :     {
   11909           0 :         m_aAdditionalStreams.push_back( PDFAddStream() );
   11910           0 :         PDFAddStream& rStream = m_aAdditionalStreams.back();
   11911           0 :         rStream.m_aMimeType = rMimeType.Len()
   11912             :                               ? OUString( rMimeType )
   11913           0 :                               : OUString( "application/octet-stream"  );
   11914           0 :         rStream.m_pStream = pStream;
   11915           0 :         rStream.m_bCompress = bCompress;
   11916             :     }
   11917         465 : }
   11918             : 
   11919             : 
   11920             : 
   11921             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10