LCOV - code coverage report
Current view: top level - vcl/source/gdi - pdfwriter_impl.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 6835 0.1 %
Date: 2014-11-03 Functions: 2 235 0.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <sal/types.h>
      21             : 
      22             : #include <math.h>
      23             : #include <algorithm>
      24             : #include <lcms2.h>
      25             : 
      26             : #include <basegfx/matrix/b2dhommatrix.hxx>
      27             : #include <basegfx/polygon/b2dpolygon.hxx>
      28             : #include <basegfx/polygon/b2dpolygontools.hxx>
      29             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      30             : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
      31             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      32             : #include <boost/scoped_array.hpp>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : #include <com/sun/star/util/URL.hpp>
      35             : #include <com/sun/star/util/URLTransformer.hpp>
      36             : #include <comphelper/processfactory.hxx>
      37             : #include <comphelper/string.hxx>
      38             : #include <cppuhelper/implbase1.hxx>
      39             : #include <i18nlangtag/languagetag.hxx>
      40             : #include <osl/file.hxx>
      41             : #include <osl/thread.h>
      42             : #include <rtl/crc.h>
      43             : #include <rtl/digest.h>
      44             : #include <rtl/ustrbuf.hxx>
      45             : #include <tools/debug.hxx>
      46             : #include <tools/stream.hxx>
      47             : #include <tools/urlobj.hxx>
      48             : #include <tools/zcodec.hxx>
      49             : #include <vcl/bitmapex.hxx>
      50             : #include <vcl/bmpacc.hxx>
      51             : #include <vcl/cvtgrf.hxx>
      52             : #include <vcl/image.hxx>
      53             : #include <vcl/lineinfo.hxx>
      54             : #include <vcl/metric.hxx>
      55             : #include <vcl/settings.hxx>
      56             : #include <vcl/strhelper.hxx>
      57             : #include <vcl/svapp.hxx>
      58             : #include <vcl/virdev.hxx>
      59             : 
      60             : #include "fontsubset.hxx"
      61             : #include "outdev.h"
      62             : #include "PhysicalFontFace.hxx"
      63             : #include "salgdi.hxx"
      64             : #include "sallayout.hxx"
      65             : #include "textlayout.hxx"
      66             : 
      67             : #include "pdfwriter_impl.hxx"
      68             : 
      69             : #if !defined(ANDROID) && !defined(IOS)
      70             : // NSS header files for PDF signing support
      71             : #include "nss.h"
      72             : #include "cert.h"
      73             : #include "hasht.h"
      74             : #include "sechash.h"
      75             : #include "cms.h"
      76             : #include "cmst.h"
      77             : #endif
      78             : 
      79             : #include <config_eot.h>
      80             : 
      81             : #if ENABLE_EOT
      82             : #include <libeot/libeot.h>
      83             : #endif
      84             : 
      85             : using namespace vcl;
      86             : 
      87             : #if (OSL_DEBUG_LEVEL < 3)
      88             : #define COMPRESS_PAGES
      89             : #else
      90             : #define DEBUG_DISABLE_PDFCOMPRESSION // also do not compress streams
      91             : #endif
      92             : 
      93             : #if !defined(ANDROID) && !defined(IOS)
      94             : #define MAX_SIGNATURE_CONTENT_LENGTH 0x4000
      95             : #endif
      96             : 
      97             : #ifdef DO_TEST_PDF
      98             : class PDFTestOutputStream : public PDFOutputStream
      99             : {
     100             :     public:
     101             :     virtual ~PDFTestOutputStream();
     102             :     virtual void write( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xStream );
     103             : };
     104             : 
     105             : PDFTestOutputStream::~PDFTestOutputStream()
     106             : {
     107             : }
     108             : 
     109             : void PDFTestOutputStream::write( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xStream )
     110             : {
     111             :     OString aStr( "lalala\ntest\ntest\ntest" );
     112             :     com::sun::star::uno::Sequence< sal_Int8 > aData( aStr.getLength() );
     113             :     memcpy( aData.getArray(), aStr.getStr(), aStr.getLength() );
     114             :     xStream->writeBytes( aData );
     115             : }
     116             : 
     117             : // this test code cannot be used to test PDF/A-1 because it forces
     118             : // control item (widgets) to bypass the structure controlling
     119             : // the embedding of such elements in actual run
     120             : void doTestCode()
     121             : {
     122             :     static const char* pHome = getenv( "HOME"  );
     123             :     OUString aTestFile( "file://"  );
     124             :     aTestFile += OUString( pHome, strlen( pHome ), RTL_TEXTENCODING_MS_1252 );
     125             :     aTestFile += "/pdf_export_test.pdf";
     126             : 
     127             :     PDFWriter::PDFWriterContext aContext;
     128             :     aContext.URL            = aTestFile;
     129             :     aContext.Version        = PDFWriter::PDF_1_4;
     130             :     aContext.Tagged         = true;
     131             :     aContext.InitialPage    = 2;
     132             :     aContext.DocumentInfo.Title = "PDF export test document";
     133             :     aContext.DocumentInfo.Producer = "VCL";
     134             : 
     135             :     aContext.SignPDF        = true;
     136             :     aContext.SignLocation   = "Burdur";
     137             :     aContext.SignReason     = "Some valid reason to sign";
     138             :     aContext.SignContact    = "signer@example.com";
     139             : 
     140             :     com::sun::star::uno::Reference< com::sun::star::beans::XMaterialHolder > xEnc;
     141             :     PDFWriter aWriter( aContext, xEnc );
     142             :     aWriter.NewPage( 595, 842 );
     143             :     aWriter.BeginStructureElement( PDFWriter::Document );
     144             :     // set duration of 3 sec for first page
     145             :     aWriter.SetAutoAdvanceTime( 3 );
     146             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     147             : 
     148             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     149             :     aWriter.SetLineColor( Color( COL_LIGHTGREEN ) );
     150             :     aWriter.DrawRect( Rectangle( Point( 2000, 200 ), Size( 8000, 3000 ) ), 5000, 2000 );
     151             : 
     152             :     aWriter.SetFont( Font( OUString( "Times" ), Size( 0, 500 ) ) );
     153             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     154             :     aWriter.SetLineColor( Color( COL_BLACK ) );
     155             :     aWriter.SetFillColor( Color( COL_LIGHTBLUE ) );
     156             : 
     157             :     Rectangle aRect( Point( 5000, 5000 ), Size( 6000, 3000 ) );
     158             :     aWriter.DrawRect( aRect );
     159             :     aWriter.DrawText( aRect, OUString( "Link annot 1" ) );
     160             :     sal_Int32 nFirstLink = aWriter.CreateLink( aRect );
     161             :     PDFNote aNote;
     162             :     aNote.Title = "A small test note";
     163             :     aNote.Contents = "There is no business like show business like no business i know. Everything about it is appealing.";
     164             :     aWriter.CreateNote( Rectangle( Point( aRect.Right(), aRect.Top() ), Size( 6000, 3000 ) ), aNote );
     165             : 
     166             :     Rectangle aTargetRect( Point( 3000, 23000 ), Size( 12000, 6000 ) );
     167             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     168             :     aWriter.DrawRect( aTargetRect );
     169             :     aWriter.DrawText( aTargetRect, "Dest second link" );
     170             :     sal_Int32 nSecondDest = aWriter.CreateDest( aTargetRect );
     171             : 
     172             :     aWriter.BeginStructureElement( PDFWriter::Section );
     173             :     aWriter.BeginStructureElement( PDFWriter::Heading );
     174             :     aWriter.DrawText( Point(4500, 9000), "A small structure test" );
     175             :     aWriter.EndStructureElement();
     176             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     177             :     aWriter.SetStructureAttribute( PDFWriter::WritingMode, PDFWriter::LrTb );
     178             :     aWriter.SetStructureAttribute( PDFWriter::TextDecorationType, PDFWriter::Underline );
     179             :     aWriter.DrawText( Rectangle( Point( 4500, 10000 ), Size( 12000, 6000 ) ),
     180             :                      "It was the best of PDF, it was the worst of PDF ... or so. This is a pretty nonsensical text to denote a paragraph. I suggest you stop reading it. Because if you read on you might get bored. So continue on your on risk. Hey, you're still here ? Why do you continue to read this as it is of no use at all ? OK, it's your time, but still... . Woah, i even get bored writing this, so let's end this here and now.",
     181             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     182             :                       );
     183             :     aWriter.SetActualText( "It was the best of PDF, it was the worst of PDF ... or so. This is a pretty nonsensical text to denote a paragraph. I suggest you stop reading it. Because if you read on you might get bored. So continue on your on risk. Hey, you're still here ? Why do you continue to read this as it is of no use at all ? OK, it's your time, but still... . Woah, i even get bored writing this, so let's end this here and now." );
     184             :     aWriter.SetAlternateText( "This paragraph contains some lengthy nonsense to test structural element emission of PDFWriter." );
     185             :     aWriter.EndStructureElement();
     186             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     187             :     aWriter.SetStructureAttribute( PDFWriter::WritingMode, PDFWriter::LrTb );
     188             :     aWriter.DrawText( Rectangle( Point( 4500, 19000 ), Size( 12000, 1000 ) ),
     189             :                       "This paragraph is nothing special either but ends on the next page structurewise",
     190             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     191             :                       );
     192             : 
     193             :     aWriter.NewPage( 595, 842 );
     194             :     // test AddStream interface
     195             :     aWriter.AddStream( "text/plain", new PDFTestOutputStream(), true );
     196             :     // set transitional mode
     197             :     aWriter.SetPageTransition( PDFWriter::WipeRightToLeft, 1500 );
     198             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     199             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     200             :     aWriter.SetFont( Font( OUString( "Times" ), Size( 0, 500 ) ) );
     201             :     aWriter.DrawText( Rectangle( Point( 4500, 1500 ), Size( 12000, 3000 ) ),
     202             :                       "Here's where all things come to an end ... well at least the paragaph from the last page.",
     203             :                       TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK
     204             :                       );
     205             :     aWriter.EndStructureElement();
     206             : 
     207             :     aWriter.SetFillColor( Color( COL_LIGHTBLUE ) );
     208             :     // disable structure
     209             :     aWriter.BeginStructureElement( PDFWriter::NonStructElement );
     210             :     aWriter.DrawRect( aRect );
     211             :     aWriter.BeginStructureElement( PDFWriter::Paragraph );
     212             :     aWriter.DrawText( aRect, "Link annot 2" );
     213             :     sal_Int32 nSecondLink = aWriter.CreateLink( aRect );
     214             : 
     215             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     216             :     aWriter.BeginStructureElement( PDFWriter::ListItem );
     217             :     aWriter.DrawRect( aTargetRect );
     218             :     aWriter.DrawText( aTargetRect, "Dest first link" );
     219             :     sal_Int32 nFirstDest = aWriter.CreateDest( aTargetRect );
     220             :     // enable structure
     221             :     aWriter.EndStructureElement();
     222             : 
     223             :     aWriter.EndStructureElement();
     224             :     aWriter.EndStructureElement();
     225             :     aWriter.BeginStructureElement( PDFWriter::Figure );
     226             :     aWriter.BeginStructureElement( PDFWriter::Caption );
     227             :     aWriter.DrawText( Point( 4500, 9000 ), "Some drawing stuff inside the structure" );
     228             :     aWriter.EndStructureElement();
     229             : 
     230             :     // test clipping
     231             :     basegfx::B2DPolyPolygon aClip;
     232             :     basegfx::B2DPolygon aClipPoly;
     233             :     aClipPoly.append( basegfx::B2DPoint( 8250, 9600 ) );
     234             :     aClipPoly.append( basegfx::B2DPoint( 16500, 11100 ) );
     235             :     aClipPoly.append( basegfx::B2DPoint( 8250, 12600 ) );
     236             :     aClipPoly.append( basegfx::B2DPoint( 4500, 11100 ) );
     237             :     aClipPoly.setClosed( true );
     238             :     aClip.append( aClipPoly );
     239             : 
     240             :     aWriter.Push( PushFlags::CLIPREGION | PushFlags::FILLCOLOR );
     241             :     aWriter.SetClipRegion( aClip );
     242             :     aWriter.DrawEllipse( Rectangle( Point( 4500, 9600 ), Size( 12000, 3000 ) ) );
     243             :     aWriter.MoveClipRegion( 1000, 500 );
     244             :     aWriter.SetFillColor( Color( COL_RED ) );
     245             :     aWriter.DrawEllipse( Rectangle( Point( 4500, 9600 ), Size( 12000, 3000 ) ) );
     246             :     aWriter.Pop();
     247             :     // test transparency
     248             :     // draw background
     249             :     Rectangle aTranspRect( Point( 7500, 13500 ), Size( 9000, 6000 ) );
     250             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     251             :     aWriter.DrawRect( aTranspRect );
     252             :     aWriter.BeginTransparencyGroup();
     253             : 
     254             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     255             :     aWriter.DrawEllipse( aTranspRect );
     256             :     aWriter.SetTextColor( Color( COL_LIGHTBLUE ) );
     257             :     aWriter.DrawText( aTranspRect,
     258             :                       "Some transparent text",
     259             :                       TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
     260             : 
     261             :     aWriter.EndTransparencyGroup( aTranspRect, 50 );
     262             : 
     263             :     // prepare an alpha mask
     264             :     Bitmap aTransMask( Size( 256, 256 ), 8, &Bitmap::GetGreyPalette( 256 ) );
     265             :     BitmapWriteAccess* pAcc = aTransMask.AcquireWriteAccess();
     266             :     for( int nX = 0; nX < 256; nX++ )
     267             :         for( int nY = 0; nY < 256; nY++ )
     268             :             pAcc->SetPixel( nX, nY, BitmapColor( (sal_uInt8)((nX+nY)/2) ) );
     269             :     aTransMask.ReleaseAccess( pAcc );
     270             :     aTransMask.SetPrefMapMode( MAP_MM );
     271             :     aTransMask.SetPrefSize( Size( 10, 10 ) );
     272             : 
     273             :     aWriter.DrawBitmap( Point( 600, 13500 ), Size( 3000, 3000 ), aTransMask );
     274             : 
     275             :     aTranspRect = Rectangle( Point( 4200, 13500 ), Size( 3000, 3000 ) );
     276             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     277             :     aWriter.DrawRect( aTranspRect );
     278             :     aWriter.SetFillColor( Color( COL_LIGHTGREEN ) );
     279             :     aWriter.DrawEllipse( aTranspRect );
     280             :     aWriter.SetTextColor( Color( COL_LIGHTBLUE ) );
     281             :     aWriter.DrawText( aTranspRect,
     282             :                       "Some transparent text",
     283             :                       TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
     284             :     aTranspRect = Rectangle( Point( 1500, 16500 ), Size( 4800, 3000 ) );
     285             :     aWriter.SetFillColor( Color( COL_LIGHTRED ) );
     286             :     aWriter.DrawRect( aTranspRect );
     287             : 
     288             :     Bitmap aImageBmp( Size( 256, 256 ), 24 );
     289             :     pAcc = aImageBmp.AcquireWriteAccess();
     290             :     pAcc->SetFillColor( Color( 0xff, 0, 0xff ) );
     291             :     pAcc->FillRect( Rectangle( Point( 0, 0 ), Size( 256, 256 ) ) );
     292             :     aImageBmp.ReleaseAccess( pAcc );
     293             :     BitmapEx aBmpEx( aImageBmp, AlphaMask( aTransMask ) );
     294             :     aWriter.DrawBitmapEx( Point( 1500, 19500 ), Size( 4800, 3000 ), aBmpEx );
     295             : 
     296             :     aWriter.EndStructureElement();
     297             :     aWriter.EndStructureElement();
     298             : 
     299             :     LineInfo aLI( LINE_DASH, 3 );
     300             :     aLI.SetDashCount( 2 );
     301             :     aLI.SetDashLen( 50 );
     302             :     aLI.SetDotCount( 2 );
     303             :     aLI.SetDotLen( 25 );
     304             :     aLI.SetDistance( 15 );
     305             :     Point aLIPoints[] = { Point( 4000, 10000 ),
     306             :                           Point( 8000, 12000 ),
     307             :                           Point( 3000, 19000 ) };
     308             :     Polygon aLIPoly( 3, aLIPoints );
     309             :     aWriter.SetLineColor( Color( COL_BLUE ) );
     310             :     aWriter.SetFillColor();
     311             :     aWriter.DrawPolyLine( aLIPoly, aLI );
     312             : 
     313             :     aLI.SetDashCount( 4 );
     314             :     aLIPoly.Move( 1000, 1000 );
     315             :     aWriter.DrawPolyLine( aLIPoly, aLI );
     316             : 
     317             :     aWriter.NewPage( 595, 842 );
     318             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     319             :     Wallpaper aWall( aTransMask );
     320             :     aWall.SetStyle( WALLPAPER_TILE );
     321             :     aWriter.DrawWallpaper( Rectangle( Point( 4400, 4200 ), Size( 10200, 6300 ) ), aWall );
     322             : 
     323             :     aWriter.NewPage( 595, 842 );
     324             :     aWriter.SetMapMode( MapMode( MAP_100TH_MM ) );
     325             :     aWriter.SetFont( Font( OUString( "Times" ), Size( 0, 500 ) ) );
     326             :     aWriter.SetTextColor( Color( COL_BLACK ) );
     327             :     aRect = Rectangle( Point( 4500, 6000 ), Size( 6000, 1500 ) );
     328             :     aWriter.DrawRect( aRect );
     329             :     aWriter.DrawText( aRect, "www.heise.de" );
     330             :     sal_Int32 nURILink = aWriter.CreateLink( aRect );
     331             :     aWriter.SetLinkURL( nURILink, OUString( "http://www.heise.de" ) );
     332             : 
     333             :     aWriter.SetLinkDest( nFirstLink, nFirstDest );
     334             :     aWriter.SetLinkDest( nSecondLink, nSecondDest );
     335             : 
     336             :     // include a button
     337             :     PDFWriter::PushButtonWidget aBtn;
     338             :     aBtn.Name = "testButton";
     339             :     aBtn.Description = "A test button";
     340             :     aBtn.Text = "hit me";
     341             :     aBtn.Location = Rectangle( Point( 4500, 9000 ), Size( 4500, 3000 ) );
     342             :     aBtn.Border = aBtn.Background = true;
     343             :     aWriter.CreateControl( aBtn );
     344             : 
     345             :     // include a uri button
     346             :     PDFWriter::PushButtonWidget aUriBtn;
     347             :     aUriBtn.Name = "wwwButton";
     348             :     aUriBtn.Description = "A URI button";
     349             :     aUriBtn.Text = "to www";
     350             :     aUriBtn.Location = Rectangle( Point( 9500, 9000 ), Size( 4500, 3000 ) );
     351             :     aUriBtn.Border = aUriBtn.Background = true;
     352             :     aUriBtn.URL = "http://www.heise.de";
     353             :     aWriter.CreateControl( aUriBtn );
     354             : 
     355             :     // include a dest button
     356             :     PDFWriter::PushButtonWidget aDstBtn;
     357             :     aDstBtn.Name = "destButton";
     358             :     aDstBtn.Description = "A Dest button";
     359             :     aDstBtn.Text = "to paragraph";
     360             :     aDstBtn.Location = Rectangle( Point( 14500, 9000 ), Size( 4500, 3000 ) );
     361             :     aDstBtn.Border = aDstBtn.Background = true;
     362             :     aDstBtn.Dest = nFirstDest;
     363             :     aWriter.CreateControl( aDstBtn );
     364             : 
     365             :     PDFWriter::CheckBoxWidget aCBox;
     366             :     aCBox.Name = "textCheckBox";
     367             :     aCBox.Description = "A test check box";
     368             :     aCBox.Text = "check me";
     369             :     aCBox.Location = Rectangle( Point( 4500, 13500 ), Size( 3000, 750 ) );
     370             :     aCBox.Checked = true;
     371             :     aCBox.Border = aCBox.Background = false;
     372             :     aWriter.CreateControl( aCBox );
     373             : 
     374             :     PDFWriter::CheckBoxWidget aCBox2;
     375             :     aCBox2.Name = "textCheckBox2";
     376             :     aCBox2.Description = "Another test check box";
     377             :     aCBox2.Text = "check me right";
     378             :     aCBox2.Location = Rectangle( Point( 4500, 14250 ), Size( 3000, 750 ) );
     379             :     aCBox2.Checked = true;
     380             :     aCBox2.Border = aCBox2.Background = false;
     381             :     aCBox2.ButtonIsLeft = false;
     382             :     aWriter.CreateControl( aCBox2 );
     383             : 
     384             :     PDFWriter::RadioButtonWidget aRB1;
     385             :     aRB1.Name = "rb1_1";
     386             :     aRB1.Description = "radio 1 button 1";
     387             :     aRB1.Text = "Despair";
     388             :     aRB1.Location = Rectangle( Point( 4500, 15000 ), Size( 6000, 1000 ) );
     389             :     aRB1.Selected = true;
     390             :     aRB1.RadioGroup = 1;
     391             :     aRB1.Border = aRB1.Background = true;
     392             :     aRB1.ButtonIsLeft = false;
     393             :     aRB1.BorderColor = Color( COL_LIGHTGREEN );
     394             :     aRB1.BackgroundColor = Color( COL_LIGHTBLUE );
     395             :     aRB1.TextColor = Color( COL_LIGHTRED );
     396             :     aRB1.TextFont = Font( OUString( "Courier" ), Size( 0, 800 ) );
     397             :     aWriter.CreateControl( aRB1 );
     398             : 
     399             :     PDFWriter::RadioButtonWidget aRB2;
     400             :     aRB2.Name = "rb2_1";
     401             :     aRB2.Description = "radio 2 button 1";
     402             :     aRB2.Text = "Joy";
     403             :     aRB2.Location = Rectangle( Point( 10500, 15000 ), Size( 3000, 1000 ) );
     404             :     aRB2.Selected = true;
     405             :     aRB2.RadioGroup = 2;
     406             :     aWriter.CreateControl( aRB2 );
     407             : 
     408             :     PDFWriter::RadioButtonWidget aRB3;
     409             :     aRB3.Name = "rb1_2";
     410             :     aRB3.Description = "radio 1 button 2";
     411             :     aRB3.Text = "Desperation";
     412             :     aRB3.Location = Rectangle( Point( 4500, 16000 ), Size( 3000, 1000 ) );
     413             :     aRB3.Selected = true;
     414             :     aRB3.RadioGroup = 1;
     415             :     aWriter.CreateControl( aRB3 );
     416             : 
     417             :     PDFWriter::EditWidget aEditBox;
     418             :     aEditBox.Name = "testEdit";
     419             :     aEditBox.Description = "A test edit field";
     420             :     aEditBox.Text = "A little test text";
     421             :     aEditBox.TextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER;
     422             :     aEditBox.Location = Rectangle( Point( 10000, 18000 ), Size( 5000, 1500 ) );
     423             :     aEditBox.MaxLen = 100;
     424             :     aEditBox.Border = aEditBox.Background = true;
     425             :     aEditBox.BorderColor = Color( COL_BLACK );
     426             :     aWriter.CreateControl( aEditBox );
     427             : 
     428             :     // normal list box
     429             :     PDFWriter::ListBoxWidget aLstBox;
     430             :     aLstBox.Name = "testListBox";
     431             :     aLstBox.Text = "One";
     432             :     aLstBox.Description = "select me";
     433             :     aLstBox.Location = Rectangle( Point( 4500, 18000 ), Size( 3000, 1500 ) );
     434             :     aLstBox.Sort = true;
     435             :     aLstBox.MultiSelect = true;
     436             :     aLstBox.Border = aLstBox.Background = true;
     437             :     aLstBox.BorderColor = Color( COL_BLACK );
     438             :     aLstBox.Entries.push_back( OUString( "One"  ) );
     439             :     aLstBox.Entries.push_back( OUString( "Two"  ) );
     440             :     aLstBox.Entries.push_back( OUString( "Three"  ) );
     441             :     aLstBox.Entries.push_back( OUString( "Four"  ) );
     442             :     aLstBox.SelectedEntries.push_back( 1 );
     443             :     aLstBox.SelectedEntries.push_back( 2 );
     444             :     aWriter.CreateControl( aLstBox );
     445             : 
     446             :     // dropdown list box
     447             :     aLstBox.Name = "testDropDownListBox";
     448             :     aLstBox.DropDown = true;
     449             :     aLstBox.Location = Rectangle( Point( 4500, 19500 ), Size( 3000, 500 ) );
     450             :     aWriter.CreateControl( aLstBox );
     451             : 
     452             :     // combo box
     453             :     PDFWriter::ComboBoxWidget aComboBox;
     454             :     aComboBox.Name = "testComboBox";
     455             :     aComboBox.Text = "test a combobox";
     456             :     aComboBox.Entries.push_back( OUString( "Larry"  ) );
     457             :     aComboBox.Entries.push_back( OUString( "Curly"  ) );
     458             :     aComboBox.Entries.push_back( OUString( "Moe"  ) );
     459             :     aComboBox.Location = Rectangle( Point( 4500, 20000 ), Size( 3000, 500 ) );
     460             :     aWriter.CreateControl( aComboBox );
     461             : 
     462             :     // test outlines
     463             :     sal_Int32 nPage1OL = aWriter.CreateOutlineItem();
     464             :     aWriter.SetOutlineItemText( nPage1OL, OUString( "Page 1"  ) );
     465             :     aWriter.SetOutlineItemDest( nPage1OL, nSecondDest );
     466             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2"  ), nSecondDest );
     467             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2 revisited"  ), nSecondDest );
     468             :     aWriter.CreateOutlineItem( nPage1OL, OUString( "Dest 2 again"  ), nSecondDest );
     469             :     sal_Int32 nPage2OL = aWriter.CreateOutlineItem();
     470             :     aWriter.SetOutlineItemText( nPage2OL, OUString( "Page 2"  ) );
     471             :     aWriter.CreateOutlineItem( nPage2OL, OUString( "Dest 1"  ), nFirstDest );
     472             : 
     473             :     aWriter.EndStructureElement(); // close document
     474             : 
     475             :     aWriter.Emit();
     476             : }
     477             : #endif
     478             : 
     479             : static const sal_Int32 nLog10Divisor = 1;
     480             : static const double fDivisor = 10.0;
     481             : 
     482           0 : static inline double pixelToPoint( double px ) { return px/fDivisor; }
     483           0 : static inline sal_Int32 pointToPixel( double pt ) { return sal_Int32(pt*fDivisor); }
     484             : 
     485             : const sal_uInt8 PDFWriterImpl::s_nPadString[32] =
     486             : {
     487             :     0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
     488             :     0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A
     489             : };
     490             : 
     491           0 : static void appendHex( sal_Int8 nInt, OStringBuffer& rBuffer )
     492             : {
     493             :     static const sal_Char pHexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
     494             :                                            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     495           0 :     rBuffer.append( pHexDigits[ (nInt >> 4) & 15 ] );
     496           0 :     rBuffer.append( pHexDigits[ nInt & 15 ] );
     497           0 : }
     498             : 
     499           0 : static void appendName( const OUString& rStr, OStringBuffer& rBuffer )
     500             : {
     501             : // FIXME i59651 add a check for max length of 127 chars? Per PDF spec 1.4, appendix C.1
     502             : // I guess than when reading the #xx sequence it will count for a single character.
     503           0 :     OString aStr( OUStringToOString( rStr, RTL_TEXTENCODING_UTF8 ) );
     504           0 :     const sal_Char* pStr = aStr.getStr();
     505           0 :     int nLen = aStr.getLength();
     506           0 :     for( int i = 0; i < nLen; i++ )
     507             :     {
     508             :         /*  #i16920# PDF recommendation: output UTF8, any byte
     509             :          *  outside the interval [33(=ASCII'!');126(=ASCII'~')]
     510             :          *  should be escaped hexadecimal
     511             :          *  for the sake of ghostscript which also reads PDF
     512             :          *  but has a narrower acceptance rate we only pass
     513             :          *  alphanumerics and '-' literally.
     514             :          */
     515           0 :         if( (pStr[i] >= 'A' && pStr[i] <= 'Z' ) ||
     516           0 :             (pStr[i] >= 'a' && pStr[i] <= 'z' ) ||
     517           0 :             (pStr[i] >= '0' && pStr[i] <= '9' ) ||
     518           0 :             pStr[i] == '-' )
     519             :         {
     520           0 :             rBuffer.append( pStr[i] );
     521             :         }
     522             :         else
     523             :         {
     524           0 :             rBuffer.append( '#' );
     525           0 :             appendHex( (sal_Int8)pStr[i], rBuffer );
     526             :         }
     527           0 :     }
     528           0 : }
     529             : 
     530           0 : static void appendName( const sal_Char* pStr, OStringBuffer& rBuffer )
     531             : {
     532             : //FIXME i59651 see above
     533           0 :     while( pStr && *pStr )
     534             :     {
     535           0 :         if( (*pStr >= 'A' && *pStr <= 'Z' ) ||
     536           0 :             (*pStr >= 'a' && *pStr <= 'z' ) ||
     537           0 :             (*pStr >= '0' && *pStr <= '9' ) ||
     538           0 :             *pStr == '-' )
     539             :         {
     540           0 :             rBuffer.append( *pStr );
     541             :         }
     542             :         else
     543             :         {
     544           0 :             rBuffer.append( '#' );
     545           0 :             appendHex( (sal_Int8)*pStr, rBuffer );
     546             :         }
     547           0 :         pStr++;
     548             :     }
     549           0 : }
     550             : 
     551             : //used only to emit encoded passwords
     552           0 : static void appendLiteralString( const sal_Char* pStr, sal_Int32 nLength, OStringBuffer& rBuffer )
     553             : {
     554           0 :     while( nLength )
     555             :     {
     556           0 :         switch( *pStr )
     557             :         {
     558             :         case '\n' :
     559           0 :             rBuffer.append( "\\n" );
     560           0 :             break;
     561             :         case '\r' :
     562           0 :             rBuffer.append( "\\r" );
     563           0 :             break;
     564             :         case '\t' :
     565           0 :             rBuffer.append( "\\t" );
     566           0 :             break;
     567             :         case '\b' :
     568           0 :             rBuffer.append( "\\b" );
     569           0 :             break;
     570             :         case '\f' :
     571           0 :             rBuffer.append( "\\f" );
     572           0 :             break;
     573             :         case '(' :
     574             :         case ')' :
     575             :         case '\\' :
     576           0 :             rBuffer.append( "\\" );
     577           0 :             rBuffer.append( (sal_Char) *pStr );
     578           0 :             break;
     579             :         default:
     580           0 :             rBuffer.append( (sal_Char) *pStr );
     581           0 :             break;
     582             :         }
     583           0 :         pStr++;
     584           0 :         nLength--;
     585             :     }
     586           0 : }
     587             : 
     588             : /**--->i56629
     589             :  * Convert a string before using it.
     590             :  *
     591             :  * This string conversion function is needed because the destination name
     592             :  * in a PDF file seen through an Internet browser should be
     593             :  * specially crafted, in order to be used directly by the browser.
     594             :  * In this way the fragment part of a hyperlink to a PDF file (e.g. something
     595             :  * as 'test1/test2/a-file.pdf\#thefragment) will be (hopefully) interpreted by the
     596             :  * PDF reader (currently only Adobe Reader plug-in seems to be working that way) called
     597             :  * from inside the Internet browser as: 'open the file test1/test2/a-file.pdf
     598             :  * and go to named destination thefragment using default zoom'.
     599             :  * The conversion is needed because in case of a fragment in the form: Slide%201
     600             :  * (meaning Slide 1) as it is converted obeying the Inet rules, it will become Slide25201
     601             :  * using this conversion, in both the generated named destinations, fragment and GoToR
     602             :  * destination.
     603             :  *
     604             :  * The names for destinations are name objects and so they don't need to be encrypted
     605             :  * even though they expose the content of PDF file (e.g. guessing the PDF content from the
     606             :  * destination name).
     607             :  *
     608             :  * Fhurter limitation: it is advisable to use standard ASCII characters for
     609             :  * OOo bookmarks.
     610             : */
     611           0 : static void appendDestinationName( const OUString& rString, OStringBuffer& rBuffer )
     612             : {
     613           0 :     const sal_Unicode* pStr = rString.getStr();
     614           0 :     sal_Int32 nLen = rString.getLength();
     615           0 :     for( int i = 0; i < nLen; i++ )
     616             :     {
     617           0 :         sal_Unicode aChar = pStr[i];
     618           0 :         if( (aChar >= '0' && aChar <= '9' ) ||
     619           0 :             (aChar >= 'a' && aChar <= 'z' ) ||
     620           0 :             (aChar >= 'A' && aChar <= 'Z' ) ||
     621             :             aChar == '-' )
     622             :         {
     623           0 :             rBuffer.append((sal_Char)aChar);
     624             :         }
     625             :         else
     626             :         {
     627           0 :             sal_Int8 aValueHigh = sal_Int8(aChar >> 8);
     628           0 :             if(aValueHigh > 0)
     629           0 :                 appendHex( aValueHigh, rBuffer );
     630           0 :             appendHex( (sal_Int8)(aChar & 255 ), rBuffer );
     631             :         }
     632             :     }
     633           0 : }
     634             : //<--- i56629
     635             : 
     636           0 : static void appendUnicodeTextString( const OUString& rString, OStringBuffer& rBuffer )
     637             : {
     638           0 :     rBuffer.append( "FEFF" );
     639           0 :     const sal_Unicode* pStr = rString.getStr();
     640           0 :     sal_Int32 nLen = rString.getLength();
     641           0 :     for( int i = 0; i < nLen; i++ )
     642             :     {
     643           0 :         sal_Unicode aChar = pStr[i];
     644           0 :         appendHex( (sal_Int8)(aChar >> 8), rBuffer );
     645           0 :         appendHex( (sal_Int8)(aChar & 255 ), rBuffer );
     646             :     }
     647           0 : }
     648             : 
     649           0 : void PDFWriterImpl::createWidgetFieldName( sal_Int32 i_nWidgetIndex, const PDFWriter::AnyWidget& i_rControl )
     650             : {
     651             :     /* #i80258# previously we use appendName here
     652             :        however we need a slightly different coding scheme than the normal
     653             :        name encoding for field names
     654             :     */
     655           0 :     const OUString& rName = (m_aContext.Version > PDFWriter::PDF_1_2) ? i_rControl.Name : i_rControl.Text;
     656           0 :     OString aStr( OUStringToOString( rName, RTL_TEXTENCODING_UTF8 ) );
     657           0 :     const sal_Char* pStr = aStr.getStr();
     658           0 :     int nLen = aStr.getLength();
     659             : 
     660           0 :     OStringBuffer aBuffer( rName.getLength()+64 );
     661           0 :     for( int i = 0; i < nLen; i++ )
     662             :     {
     663             :         /*  #i16920# PDF recommendation: output UTF8, any byte
     664             :          *  outside the interval [32(=ASCII' ');126(=ASCII'~')]
     665             :          *  should be escaped hexadecimal
     666             :          */
     667           0 :         if( (pStr[i] >= 32 && pStr[i] <= 126 ) )
     668           0 :             aBuffer.append( pStr[i] );
     669             :         else
     670             :         {
     671           0 :             aBuffer.append( '#' );
     672           0 :             appendHex( (sal_Int8)pStr[i], aBuffer );
     673             :         }
     674             :     }
     675             : 
     676           0 :     OString aFullName( aBuffer.makeStringAndClear() );
     677             : 
     678             :     /* #i82785# create hierarchical fields down to the for each dot in i_rName */
     679           0 :     sal_Int32 nTokenIndex = 0, nLastTokenIndex = 0;
     680           0 :     OString aPartialName;
     681           0 :     OString aDomain;
     682           0 :     do
     683             :     {
     684           0 :         nLastTokenIndex = nTokenIndex;
     685           0 :         aPartialName = aFullName.getToken( 0, '.', nTokenIndex );
     686           0 :         if( nTokenIndex != -1 )
     687             :         {
     688             :             // find or create a hierarchical field
     689             :             // first find the fully qualified name up to this field
     690           0 :             aDomain = aFullName.copy( 0, nTokenIndex-1 );
     691           0 :             boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator it = m_aFieldNameMap.find( aDomain );
     692           0 :             if( it == m_aFieldNameMap.end() )
     693             :             {
     694             :                  // create new hierarchy field
     695           0 :                 sal_Int32 nNewWidget = m_aWidgets.size();
     696           0 :                 m_aWidgets.push_back( PDFWidget() );
     697           0 :                 m_aWidgets[nNewWidget].m_nObject = createObject();
     698           0 :                 m_aWidgets[nNewWidget].m_eType = PDFWriter::Hierarchy;
     699           0 :                 m_aWidgets[nNewWidget].m_aName = aPartialName;
     700           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[nNewWidget].m_nObject;
     701           0 :                 m_aFieldNameMap[aDomain] = nNewWidget;
     702           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[nNewWidget].m_nObject;
     703           0 :                 if( nLastTokenIndex > 0 )
     704             :                 {
     705             :                     // this field is not a root field and
     706             :                     // needs to be inserted to its parent
     707           0 :                     OString aParentDomain( aDomain.copy( 0, nLastTokenIndex-1 ) );
     708           0 :                     it = m_aFieldNameMap.find( aParentDomain );
     709             :                     OSL_ENSURE( it != m_aFieldNameMap.end(), "field name not found" );
     710           0 :                     if( it != m_aFieldNameMap.end()  )
     711             :                     {
     712             :                         OSL_ENSURE( it->second < sal_Int32(m_aWidgets.size()), "invalid field number entry" );
     713           0 :                         if( it->second < sal_Int32(m_aWidgets.size()) )
     714             :                         {
     715           0 :                             PDFWidget& rParentField( m_aWidgets[it->second] );
     716           0 :                             rParentField.m_aKids.push_back( m_aWidgets[nNewWidget].m_nObject );
     717           0 :                             rParentField.m_aKidsIndex.push_back( nNewWidget );
     718           0 :                             m_aWidgets[nNewWidget].m_nParent = rParentField.m_nObject;
     719             :                         }
     720           0 :                     }
     721             :                 }
     722             :             }
     723           0 :             else if( m_aWidgets[it->second].m_eType != PDFWriter::Hierarchy )
     724             :             {
     725             :                 // this is invalid, someone tries to have a terminal field as parent
     726             :                 // example: a button with the name foo.bar exists and
     727             :                 // another button is named foo.bar.no
     728             :                 // workaround: put the second terminal field as much up in the hierarchy as
     729             :                 // necessary to have a non-terminal field as parent (or none at all)
     730             :                 // since it->second already is terminal, we just need to use its parent
     731           0 :                 aDomain = OString();
     732           0 :                 aPartialName = aFullName.copy( aFullName.lastIndexOf( '.' )+1 );
     733           0 :                 if( nLastTokenIndex > 0 )
     734             :                 {
     735           0 :                     aDomain = aFullName.copy( 0, nLastTokenIndex-1 );
     736           0 :                     OStringBuffer aBuf( aDomain.getLength() + 1 + aPartialName.getLength() );
     737           0 :                     aBuf.append( aDomain );
     738           0 :                     aBuf.append( '.' );
     739           0 :                     aBuf.append( aPartialName );
     740           0 :                     aFullName = aBuf.makeStringAndClear();
     741             :                 }
     742             :                 else
     743           0 :                     aFullName = aPartialName;
     744           0 :                 break;
     745             :             }
     746             :         }
     747           0 :     } while( nTokenIndex != -1 );
     748             : 
     749             :     // insert widget into its hierarchy field
     750           0 :     if( !aDomain.isEmpty() )
     751             :     {
     752           0 :         boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator it = m_aFieldNameMap.find( aDomain );
     753           0 :         if( it != m_aFieldNameMap.end() )
     754             :         {
     755             :             OSL_ENSURE( it->second >= 0 && it->second < sal_Int32( m_aWidgets.size() ), "invalid field index" );
     756           0 :             if( it->second >= 0 && it->second < sal_Int32(m_aWidgets.size()) )
     757             :             {
     758           0 :                 m_aWidgets[i_nWidgetIndex].m_nParent = m_aWidgets[it->second].m_nObject;
     759           0 :                 m_aWidgets[it->second].m_aKids.push_back( m_aWidgets[i_nWidgetIndex].m_nObject);
     760           0 :                 m_aWidgets[it->second].m_aKidsIndex.push_back( i_nWidgetIndex );
     761             :             }
     762             :         }
     763             :     }
     764             : 
     765           0 :     if( aPartialName.isEmpty() )
     766             :     {
     767             :         // how funny, an empty field name
     768           0 :         if( i_rControl.getType() == PDFWriter::RadioButton )
     769             :         {
     770           0 :             aPartialName  = "RadioGroup";
     771           0 :             aPartialName += OString::number( static_cast<const PDFWriter::RadioButtonWidget&>(i_rControl).RadioGroup );
     772             :         }
     773             :         else
     774           0 :             aPartialName = OString( "Widget" );
     775             :     }
     776             : 
     777           0 :     if( ! m_aContext.AllowDuplicateFieldNames )
     778             :     {
     779           0 :         boost::unordered_map<OString, sal_Int32, OStringHash>::iterator it = m_aFieldNameMap.find( aFullName );
     780             : 
     781           0 :         if( it != m_aFieldNameMap.end() ) // not unique
     782             :         {
     783           0 :             boost::unordered_map< OString, sal_Int32, OStringHash >::const_iterator check_it;
     784           0 :             OString aTry;
     785           0 :             sal_Int32 nTry = 2;
     786           0 :             do
     787             :             {
     788           0 :                 OStringBuffer aUnique( aFullName.getLength() + 16 );
     789           0 :                 aUnique.append( aFullName );
     790           0 :                 aUnique.append( '_' );
     791           0 :                 aUnique.append( nTry++ );
     792           0 :                 aTry = aUnique.makeStringAndClear();
     793           0 :                 check_it = m_aFieldNameMap.find( aTry );
     794           0 :             } while( check_it != m_aFieldNameMap.end() );
     795           0 :             aFullName = aTry;
     796           0 :             m_aFieldNameMap[ aFullName ] = i_nWidgetIndex;
     797           0 :             aPartialName = aFullName.copy( aFullName.lastIndexOf( '.' )+1 );
     798             :         }
     799             :         else
     800           0 :             m_aFieldNameMap[ aFullName ] = i_nWidgetIndex;
     801             :     }
     802             : 
     803             :     // finally
     804           0 :     m_aWidgets[i_nWidgetIndex].m_aName = aPartialName;
     805           0 : }
     806             : 
     807           0 : static void appendFixedInt( sal_Int32 nValue, OStringBuffer& rBuffer, sal_Int32 nPrecision = nLog10Divisor )
     808             : {
     809           0 :     if( nValue < 0 )
     810             :     {
     811           0 :         rBuffer.append( '-' );
     812           0 :         nValue = -nValue;
     813             :     }
     814           0 :     sal_Int32 nFactor = 1, nDiv = nPrecision;
     815           0 :     while( nDiv-- )
     816           0 :         nFactor *= 10;
     817             : 
     818           0 :     sal_Int32 nInt      = nValue / nFactor;
     819           0 :     rBuffer.append( nInt );
     820           0 :     if( nFactor > 1 )
     821             :     {
     822           0 :         sal_Int32 nDecimal  = nValue % nFactor;
     823           0 :         if( nDecimal )
     824             :         {
     825           0 :             rBuffer.append( '.' );
     826             :             // omit trailing zeros
     827           0 :             while( (nDecimal % 10) == 0 )
     828           0 :                 nDecimal /= 10;
     829           0 :             rBuffer.append( nDecimal );
     830             :         }
     831             :     }
     832           0 : }
     833             : 
     834             : // appends a double. PDF does not accept exponential format, only fixed point
     835           0 : static void appendDouble( double fValue, OStringBuffer& rBuffer, sal_Int32 nPrecision = 5 )
     836             : {
     837           0 :     bool bNeg = false;
     838           0 :     if( fValue < 0.0 )
     839             :     {
     840           0 :         bNeg = true;
     841           0 :         fValue=-fValue;
     842             :     }
     843             : 
     844           0 :     sal_Int64 nInt = (sal_Int64)fValue;
     845           0 :     fValue -= (double)nInt;
     846             :     // optimizing hardware may lead to a value of 1.0 after the subtraction
     847           0 :     if( fValue == 1.0 || log10( 1.0-fValue ) <= -nPrecision )
     848             :     {
     849           0 :         nInt++;
     850           0 :         fValue = 0.0;
     851             :     }
     852           0 :     sal_Int64 nFrac = 0;
     853           0 :     if( fValue )
     854             :     {
     855           0 :         fValue *= pow( 10.0, (double)nPrecision );
     856           0 :         nFrac = (sal_Int64)fValue;
     857             :     }
     858           0 :     if( bNeg && ( nInt || nFrac ) )
     859           0 :         rBuffer.append( '-' );
     860           0 :     rBuffer.append( nInt );
     861           0 :     if( nFrac )
     862             :     {
     863             :         int i;
     864           0 :         rBuffer.append( '.' );
     865           0 :         sal_Int64 nBound = (sal_Int64)(pow( 10.0, nPrecision - 1.0 )+0.5);
     866           0 :         for ( i = 0; ( i < nPrecision ) && nFrac; i++ )
     867             :         {
     868           0 :             sal_Int64 nNumb = nFrac / nBound;
     869           0 :             nFrac -= nNumb * nBound;
     870           0 :             rBuffer.append( nNumb );
     871           0 :             nBound /= 10;
     872             :         }
     873             :     }
     874           0 : }
     875             : 
     876           0 : static void appendColor( const Color& rColor, OStringBuffer& rBuffer, bool bConvertToGrey = false )
     877             : {
     878             : 
     879           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     880             :     {
     881           0 :         if( bConvertToGrey )
     882             :         {
     883           0 :             sal_uInt8 cByte = rColor.GetLuminance();
     884           0 :             appendDouble( (double)cByte / 255.0, rBuffer );
     885             :         }
     886             :         else
     887             :         {
     888           0 :             appendDouble( (double)rColor.GetRed() / 255.0, rBuffer );
     889           0 :             rBuffer.append( ' ' );
     890           0 :             appendDouble( (double)rColor.GetGreen() / 255.0, rBuffer );
     891           0 :             rBuffer.append( ' ' );
     892           0 :             appendDouble( (double)rColor.GetBlue() / 255.0, rBuffer );
     893             :         }
     894             :     }
     895           0 : }
     896             : 
     897           0 : void PDFWriterImpl::appendStrokingColor( const Color& rColor, OStringBuffer& rBuffer )
     898             : {
     899           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     900             :     {
     901           0 :         bool bGrey = m_aContext.ColorMode == PDFWriter::DrawGreyscale;
     902           0 :         appendColor( rColor, rBuffer, bGrey );
     903           0 :         rBuffer.append( bGrey ? " G" : " RG" );
     904             :     }
     905           0 : }
     906             : 
     907           0 : void PDFWriterImpl::appendNonStrokingColor( const Color& rColor, OStringBuffer& rBuffer )
     908             : {
     909           0 :     if( rColor != Color( COL_TRANSPARENT ) )
     910             :     {
     911           0 :         bool bGrey = m_aContext.ColorMode == PDFWriter::DrawGreyscale;
     912           0 :         appendColor( rColor, rBuffer, bGrey );
     913           0 :         rBuffer.append( bGrey ? " g" : " rg" );
     914             :     }
     915           0 : }
     916             : 
     917             : // matrix helper class
     918             : // TODO: use basegfx matrix class instead or derive from it
     919             : namespace vcl // TODO: use anonymous namespace to keep this class local
     920             : {
     921             : /*  for sparse matrices of the form (2D linear transformations)
     922             :  *  f[0] f[1] 0
     923             :  *  f[2] f[3] 0
     924             :  *  f[4] f[5] 1
     925             :  */
     926             : class Matrix3
     927             : {
     928             :     double f[6];
     929             : 
     930           0 :     void set( double *pn ) { for( int i = 0 ; i < 6; i++ ) f[i] = pn[i]; }
     931             : public:
     932             :     Matrix3();
     933           0 :     ~Matrix3() {}
     934             : 
     935             :     void skew( double alpha, double beta );
     936             :     void scale( double sx, double sy );
     937             :     void rotate( double angle );
     938             :     void translate( double tx, double ty );
     939             :     bool invert();
     940             : 
     941             :     void append( PDFWriterImpl::PDFPage& rPage, OStringBuffer& rBuffer, Point* pBack = NULL );
     942             : 
     943             :     Point transform( const Point& rPoint ) const;
     944             : };
     945             : }
     946             : 
     947           0 : Matrix3::Matrix3()
     948             : {
     949             :     // initialize to unity
     950           0 :     f[0] = 1.0;
     951           0 :     f[1] = 0.0;
     952           0 :     f[2] = 0.0;
     953           0 :     f[3] = 1.0;
     954           0 :     f[4] = 0.0;
     955           0 :     f[5] = 0.0;
     956           0 : }
     957             : 
     958           0 : Point Matrix3::transform( const Point& rOrig ) const
     959             : {
     960           0 :     double x = (double)rOrig.X(), y = (double)rOrig.Y();
     961           0 :     return Point( (int)(x*f[0] + y*f[2] + f[4]), (int)(x*f[1] + y*f[3] + f[5]) );
     962             : }
     963             : 
     964           0 : void Matrix3::skew( double alpha, double beta )
     965             : {
     966             :     double fn[6];
     967           0 :     double tb = tan( beta );
     968           0 :     fn[0] = f[0] + f[2]*tb;
     969           0 :     fn[1] = f[1];
     970           0 :     fn[2] = f[2] + f[3]*tb;
     971           0 :     fn[3] = f[3];
     972           0 :     fn[4] = f[4] + f[5]*tb;
     973           0 :     fn[5] = f[5];
     974           0 :     if( alpha != 0.0 )
     975             :     {
     976           0 :         double ta = tan( alpha );
     977           0 :         fn[1] += f[0]*ta;
     978           0 :         fn[3] += f[2]*ta;
     979           0 :         fn[5] += f[4]*ta;
     980             :     }
     981           0 :     set( fn );
     982           0 : }
     983             : 
     984           0 : void Matrix3::scale( double sx, double sy )
     985             : {
     986             :     double fn[6];
     987           0 :     fn[0] = sx*f[0];
     988           0 :     fn[1] = sy*f[1];
     989           0 :     fn[2] = sx*f[2];
     990           0 :     fn[3] = sy*f[3];
     991           0 :     fn[4] = sx*f[4];
     992           0 :     fn[5] = sy*f[5];
     993           0 :     set( fn );
     994           0 : }
     995             : 
     996           0 : void Matrix3::rotate( double angle )
     997             : {
     998             :     double fn[6];
     999           0 :     double fSin = sin(angle);
    1000           0 :     double fCos = cos(angle);
    1001           0 :     fn[0] = f[0]*fCos - f[1]*fSin;
    1002           0 :     fn[1] = f[0]*fSin + f[1]*fCos;
    1003           0 :     fn[2] = f[2]*fCos - f[3]*fSin;
    1004           0 :     fn[3] = f[2]*fSin + f[3]*fCos;
    1005           0 :     fn[4] = f[4]*fCos - f[5]*fSin;
    1006           0 :     fn[5] = f[4]*fSin + f[5]*fCos;
    1007           0 :     set( fn );
    1008           0 : }
    1009             : 
    1010           0 : void Matrix3::translate( double tx, double ty )
    1011             : {
    1012           0 :     f[4] += tx;
    1013           0 :     f[5] += ty;
    1014           0 : }
    1015             : 
    1016           0 : bool Matrix3::invert()
    1017             : {
    1018             :     // short circuit trivial cases
    1019           0 :     if( f[1]==f[2] && f[1]==0.0 && f[0]==f[3] && f[0]==1.0 )
    1020             :     {
    1021           0 :         f[4] = -f[4];
    1022           0 :         f[5] = -f[5];
    1023           0 :         return true;
    1024             :     }
    1025             : 
    1026             :     // check determinant
    1027           0 :     const double fDet = f[0]*f[3]-f[1]*f[2];
    1028           0 :     if( fDet == 0.0 )
    1029           0 :         return false;
    1030             : 
    1031             :     // invert the matrix
    1032             :     double fn[6];
    1033           0 :     fn[0] = +f[3] / fDet;
    1034           0 :     fn[1] = -f[1] / fDet;
    1035           0 :     fn[2] = -f[2] / fDet;
    1036           0 :     fn[3] = +f[0] / fDet;
    1037             : 
    1038             :     // apply inversion to translation
    1039           0 :     fn[4] = -(f[4]*fn[0] + f[5]*fn[2]);
    1040           0 :     fn[5] = -(f[4]*fn[1] + f[5]*fn[3]);
    1041             : 
    1042           0 :     set( fn );
    1043           0 :     return true;
    1044             : }
    1045             : 
    1046           0 : void Matrix3::append( PDFWriterImpl::PDFPage& rPage, OStringBuffer& rBuffer, Point* pBack )
    1047             : {
    1048           0 :     appendDouble( f[0], rBuffer );
    1049           0 :     rBuffer.append( ' ' );
    1050           0 :     appendDouble( f[1], rBuffer );
    1051           0 :     rBuffer.append( ' ' );
    1052           0 :     appendDouble( f[2], rBuffer );
    1053           0 :     rBuffer.append( ' ' );
    1054           0 :     appendDouble( f[3], rBuffer );
    1055           0 :     rBuffer.append( ' ' );
    1056           0 :     rPage.appendPoint( Point( (long)f[4], (long)f[5] ), rBuffer, false, pBack );
    1057           0 : }
    1058             : 
    1059           0 : static void appendResourceMap( OStringBuffer& rBuf, const char* pPrefix, const PDFWriterImpl::ResourceMap& rList )
    1060             : {
    1061           0 :     if( rList.empty() )
    1062           0 :         return;
    1063           0 :     rBuf.append( '/' );
    1064           0 :     rBuf.append( pPrefix );
    1065           0 :     rBuf.append( "<<" );
    1066           0 :     int ni = 0;
    1067           0 :     for( PDFWriterImpl::ResourceMap::const_iterator it = rList.begin(); it != rList.end(); ++it )
    1068             :     {
    1069           0 :         if( !it->first.isEmpty() && it->second > 0 )
    1070             :         {
    1071           0 :             rBuf.append( '/' );
    1072           0 :             rBuf.append( it->first );
    1073           0 :             rBuf.append( ' ' );
    1074           0 :             rBuf.append( it->second );
    1075           0 :             rBuf.append( " 0 R" );
    1076           0 :             if( ((++ni) & 7) == 0 )
    1077           0 :                 rBuf.append( '\n' );
    1078             :         }
    1079             :     }
    1080           0 :     rBuf.append( ">>\n" );
    1081             : }
    1082             : 
    1083           0 : void PDFWriterImpl::ResourceDict::append( OStringBuffer& rBuf, sal_Int32 nFontDictObject )
    1084             : {
    1085           0 :     rBuf.append( "<</Font " );
    1086           0 :     rBuf.append( nFontDictObject );
    1087           0 :     rBuf.append( " 0 R\n" );
    1088           0 :     appendResourceMap( rBuf, "XObject", m_aXObjects );
    1089           0 :     appendResourceMap( rBuf, "ExtGState", m_aExtGStates );
    1090           0 :     appendResourceMap( rBuf, "Shading", m_aShadings );
    1091           0 :     appendResourceMap( rBuf, "Pattern", m_aPatterns );
    1092           0 :     rBuf.append( "/ProcSet[/PDF/Text" );
    1093           0 :     if( !m_aXObjects.empty() )
    1094           0 :         rBuf.append( "/ImageC/ImageI/ImageB" );
    1095           0 :     rBuf.append( "]\n>>\n" );
    1096           0 : };
    1097             : 
    1098           0 : PDFWriterImpl::PDFPage::PDFPage( PDFWriterImpl* pWriter, sal_Int32 nPageWidth, sal_Int32 nPageHeight, PDFWriter::Orientation eOrientation )
    1099             :         :
    1100             :         m_pWriter( pWriter ),
    1101             :         m_nPageWidth( nPageWidth ),
    1102             :         m_nPageHeight( nPageHeight ),
    1103             :         m_eOrientation( eOrientation ),
    1104             :         m_nPageObject( 0 ),  // invalid object number
    1105             :         m_nPageIndex( -1 ), // invalid index
    1106             :         m_nStreamLengthObject( 0 ),
    1107             :         m_nBeginStreamPos( 0 ),
    1108             :         m_eTransition( PDFWriter::Regular ),
    1109             :         m_nTransTime( 0 ),
    1110             :         m_nDuration( 0 ),
    1111           0 :         m_bHasWidgets( false )
    1112             : {
    1113             :     // object ref must be only ever updated in emit()
    1114           0 :     m_nPageObject = m_pWriter->createObject();
    1115           0 : }
    1116             : 
    1117           0 : PDFWriterImpl::PDFPage::~PDFPage()
    1118             : {
    1119           0 : }
    1120             : 
    1121           0 : void PDFWriterImpl::PDFPage::beginStream()
    1122             : {
    1123             : #if OSL_DEBUG_LEVEL > 1
    1124             :     {
    1125             :         OStringBuffer aLine( "PDFWriterImpl::PDFPage::beginStream, +" );
    1126             :          m_pWriter->emitComment( aLine.getStr() );
    1127             :     }
    1128             : #endif
    1129           0 :     m_aStreamObjects.push_back(m_pWriter->createObject());
    1130           0 :     if( ! m_pWriter->updateObject( m_aStreamObjects.back() ) )
    1131           0 :         return;
    1132             : 
    1133           0 :     m_nStreamLengthObject = m_pWriter->createObject();
    1134             :     // write content stream header
    1135           0 :     OStringBuffer aLine;
    1136           0 :     aLine.append( m_aStreamObjects.back() );
    1137           0 :     aLine.append( " 0 obj\n<</Length " );
    1138           0 :     aLine.append( m_nStreamLengthObject );
    1139           0 :     aLine.append( " 0 R" );
    1140             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1141           0 :     aLine.append( "/Filter/FlateDecode" );
    1142             : #endif
    1143           0 :     aLine.append( ">>\nstream\n" );
    1144           0 :     if( ! m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() ) )
    1145           0 :         return;
    1146           0 :     if (osl::File::E_None != m_pWriter->m_aFile.getPos(m_nBeginStreamPos))
    1147             :     {
    1148           0 :         m_pWriter->m_aFile.close();
    1149           0 :         m_pWriter->m_bOpen = false;
    1150             :     }
    1151             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1152           0 :     m_pWriter->beginCompression();
    1153             : #endif
    1154           0 :     m_pWriter->checkAndEnableStreamEncryption( m_aStreamObjects.back() );
    1155             : }
    1156             : 
    1157           0 : void PDFWriterImpl::PDFPage::endStream()
    1158             : {
    1159             : #if defined ( COMPRESS_PAGES ) && !defined ( DEBUG_DISABLE_PDFCOMPRESSION )
    1160           0 :     m_pWriter->endCompression();
    1161             : #endif
    1162             :     sal_uInt64 nEndStreamPos;
    1163           0 :     if (osl::File::E_None != m_pWriter->m_aFile.getPos(nEndStreamPos))
    1164             :     {
    1165           0 :         m_pWriter->m_aFile.close();
    1166           0 :         m_pWriter->m_bOpen = false;
    1167           0 :         return;
    1168             :     }
    1169           0 :     m_pWriter->disableStreamEncryption();
    1170           0 :     if( ! m_pWriter->writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    1171           0 :         return;
    1172             :     // emit stream length object
    1173           0 :     if( ! m_pWriter->updateObject( m_nStreamLengthObject ) )
    1174           0 :         return;
    1175           0 :     OStringBuffer aLine;
    1176           0 :     aLine.append( m_nStreamLengthObject );
    1177           0 :     aLine.append( " 0 obj\n" );
    1178           0 :     aLine.append( (sal_Int64)(nEndStreamPos-m_nBeginStreamPos) );
    1179           0 :     aLine.append( "\nendobj\n\n" );
    1180           0 :     m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() );
    1181             : }
    1182             : 
    1183           0 : bool PDFWriterImpl::PDFPage::emit(sal_Int32 nParentObject )
    1184             : {
    1185             :     // emit page object
    1186           0 :     if( ! m_pWriter->updateObject( m_nPageObject ) )
    1187           0 :         return false;
    1188           0 :     OStringBuffer aLine;
    1189             : 
    1190           0 :     aLine.append( m_nPageObject );
    1191             :     aLine.append( " 0 obj\n"
    1192           0 :                   "<</Type/Page/Parent " );
    1193           0 :     aLine.append( nParentObject );
    1194           0 :     aLine.append( " 0 R" );
    1195           0 :     aLine.append( "/Resources " );
    1196           0 :     aLine.append( m_pWriter->getResourceDictObj() );
    1197           0 :     aLine.append( " 0 R" );
    1198           0 :     if( m_nPageWidth && m_nPageHeight )
    1199             :     {
    1200           0 :         aLine.append( "/MediaBox[0 0 " );
    1201           0 :         aLine.append( m_nPageWidth );
    1202           0 :         aLine.append( ' ' );
    1203           0 :         aLine.append( m_nPageHeight );
    1204           0 :         aLine.append( "]" );
    1205             :     }
    1206           0 :     switch( m_eOrientation )
    1207             :     {
    1208           0 :         case PDFWriter::Landscape: aLine.append( "/Rotate 90\n" );break;
    1209           0 :         case PDFWriter::Seascape:  aLine.append( "/Rotate -90\n" );break;
    1210           0 :         case PDFWriter::Portrait:  aLine.append( "/Rotate 0\n" );break;
    1211             : 
    1212             :         case PDFWriter::Inherit:
    1213             :         default:
    1214           0 :             break;
    1215             :     }
    1216           0 :     int nAnnots = m_aAnnotations.size();
    1217           0 :     if( nAnnots > 0 )
    1218             :     {
    1219           0 :         aLine.append( "/Annots[\n" );
    1220           0 :         for( int i = 0; i < nAnnots; i++ )
    1221             :         {
    1222           0 :             aLine.append( m_aAnnotations[i] );
    1223           0 :             aLine.append( " 0 R" );
    1224           0 :             aLine.append( ((i+1)%15) ? " " : "\n" );
    1225             :         }
    1226           0 :         aLine.append( "]\n" );
    1227             :     }
    1228           0 :     if( m_aMCIDParents.size() > 0 )
    1229             :     {
    1230           0 :         OStringBuffer aStructParents( 1024 );
    1231           0 :         aStructParents.append( "[ " );
    1232           0 :         int nParents = m_aMCIDParents.size();
    1233           0 :         for( int i = 0; i < nParents; i++ )
    1234             :         {
    1235           0 :             aStructParents.append( m_aMCIDParents[i] );
    1236           0 :             aStructParents.append( " 0 R" );
    1237           0 :             aStructParents.append( ((i%10) == 9) ? "\n" : " " );
    1238             :         }
    1239           0 :         aStructParents.append( "]" );
    1240           0 :         m_pWriter->m_aStructParentTree.push_back( aStructParents.makeStringAndClear() );
    1241             : 
    1242           0 :         aLine.append( "/StructParents " );
    1243           0 :         aLine.append( sal_Int32(m_pWriter->m_aStructParentTree.size()-1) );
    1244           0 :         aLine.append( "\n" );
    1245             :     }
    1246           0 :     if( m_nDuration > 0 )
    1247             :     {
    1248           0 :         aLine.append( "/Dur " );
    1249           0 :         aLine.append( (sal_Int32)m_nDuration );
    1250           0 :         aLine.append( "\n" );
    1251             :     }
    1252           0 :     if( m_eTransition != PDFWriter::Regular && m_nTransTime > 0 )
    1253             :     {
    1254             :         // transition duration
    1255           0 :         aLine.append( "/Trans<</D " );
    1256           0 :         appendDouble( (double)m_nTransTime/1000.0, aLine, 3 );
    1257           0 :         aLine.append( "\n" );
    1258           0 :         const char *pStyle = NULL, *pDm = NULL, *pM = NULL, *pDi = NULL;
    1259           0 :         switch( m_eTransition )
    1260             :         {
    1261             :             case PDFWriter::SplitHorizontalInward:
    1262           0 :                 pStyle = "Split"; pDm = "H"; pM = "I"; break;
    1263             :             case PDFWriter::SplitHorizontalOutward:
    1264           0 :                 pStyle = "Split"; pDm = "H"; pM = "O"; break;
    1265             :             case PDFWriter::SplitVerticalInward:
    1266           0 :                 pStyle = "Split"; pDm = "V"; pM = "I"; break;
    1267             :             case PDFWriter::SplitVerticalOutward:
    1268           0 :                 pStyle = "Split"; pDm = "V"; pM = "O"; break;
    1269             :             case PDFWriter::BlindsHorizontal:
    1270           0 :                 pStyle = "Blinds"; pDm = "H"; break;
    1271             :             case PDFWriter::BlindsVertical:
    1272           0 :                 pStyle = "Blinds"; pDm = "V"; break;
    1273             :             case PDFWriter::BoxInward:
    1274           0 :                 pStyle = "Box"; pM = "I"; break;
    1275             :             case PDFWriter::BoxOutward:
    1276           0 :                 pStyle = "Box"; pM = "O"; break;
    1277             :             case PDFWriter::WipeLeftToRight:
    1278           0 :                 pStyle = "Wipe"; pDi = "0"; break;
    1279             :             case PDFWriter::WipeBottomToTop:
    1280           0 :                 pStyle = "Wipe"; pDi = "90"; break;
    1281             :             case PDFWriter::WipeRightToLeft:
    1282           0 :                 pStyle = "Wipe"; pDi = "180"; break;
    1283             :             case PDFWriter::WipeTopToBottom:
    1284           0 :                 pStyle = "Wipe"; pDi = "270"; break;
    1285             :             case PDFWriter::Dissolve:
    1286           0 :                 pStyle = "Dissolve"; break;
    1287             :             case PDFWriter::GlitterLeftToRight:
    1288           0 :                 pStyle = "Glitter"; pDi = "0"; break;
    1289             :             case PDFWriter::GlitterTopToBottom:
    1290           0 :                 pStyle = "Glitter"; pDi = "270"; break;
    1291             :             case PDFWriter::GlitterTopLeftToBottomRight:
    1292           0 :                 pStyle = "Glitter"; pDi = "315"; break;
    1293             :             case PDFWriter::Regular:
    1294           0 :                 break;
    1295             :         }
    1296             :         // transition style
    1297           0 :         if( pStyle )
    1298             :         {
    1299           0 :             aLine.append( "/S/" );
    1300           0 :             aLine.append( pStyle );
    1301           0 :             aLine.append( "\n" );
    1302             :         }
    1303           0 :         if( pDm )
    1304             :         {
    1305           0 :             aLine.append( "/Dm/" );
    1306           0 :             aLine.append( pDm );
    1307           0 :             aLine.append( "\n" );
    1308             :         }
    1309           0 :         if( pM )
    1310             :         {
    1311           0 :             aLine.append( "/M/" );
    1312           0 :             aLine.append( pM );
    1313           0 :             aLine.append( "\n" );
    1314             :         }
    1315           0 :         if( pDi  )
    1316             :         {
    1317           0 :             aLine.append( "/Di " );
    1318           0 :             aLine.append( pDi );
    1319           0 :             aLine.append( "\n" );
    1320             :         }
    1321           0 :         aLine.append( ">>\n" );
    1322             :     }
    1323           0 :     if( m_pWriter->getVersion() > PDFWriter::PDF_1_3 && ! m_pWriter->m_bIsPDF_A1 )
    1324             :     {
    1325           0 :         aLine.append( "/Group<</S/Transparency/CS/DeviceRGB/I true>>" );
    1326             :     }
    1327           0 :     aLine.append( "/Contents" );
    1328           0 :     unsigned int nStreamObjects = m_aStreamObjects.size();
    1329           0 :     if( nStreamObjects > 1 )
    1330           0 :         aLine.append( '[' );
    1331           0 :     for( unsigned int i = 0; i < m_aStreamObjects.size(); i++ )
    1332             :     {
    1333           0 :         aLine.append( ' ' );
    1334           0 :         aLine.append( m_aStreamObjects[i] );
    1335           0 :         aLine.append( " 0 R" );
    1336             :     }
    1337           0 :     if( nStreamObjects > 1 )
    1338           0 :         aLine.append( ']' );
    1339           0 :     aLine.append( ">>\nendobj\n\n" );
    1340           0 :     return m_pWriter->writeBuffer( aLine.getStr(), aLine.getLength() );
    1341             : }
    1342             : 
    1343             : namespace vcl
    1344             : {
    1345             : template < class GEOMETRY >
    1346           0 : GEOMETRY lcl_convert( const MapMode& _rSource, const MapMode& _rDest, OutputDevice* _pPixelConversion, const GEOMETRY& _rObject )
    1347             : {
    1348           0 :     GEOMETRY aPoint;
    1349           0 :     if ( MAP_PIXEL == _rSource.GetMapUnit() )
    1350             :     {
    1351           0 :         aPoint = _pPixelConversion->PixelToLogic( _rObject, _rDest );
    1352             :     }
    1353             :     else
    1354             :     {
    1355           0 :         aPoint = OutputDevice::LogicToLogic( _rObject, _rSource, _rDest );
    1356             :     }
    1357           0 :     return aPoint;
    1358             : }
    1359             : }
    1360             : 
    1361           0 : void PDFWriterImpl::PDFPage::appendPoint( const Point& rPoint, OStringBuffer& rBuffer, bool bNeg, Point* pOutPoint ) const
    1362             : {
    1363           0 :     if( pOutPoint )
    1364             :     {
    1365           0 :         Point aPoint( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1366             :                                    m_pWriter->m_aMapMode,
    1367             :                                    m_pWriter->getReferenceDevice(),
    1368           0 :                                    rPoint ) );
    1369           0 :         *pOutPoint = aPoint;
    1370             :     }
    1371             : 
    1372           0 :     Point aPoint( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1373             :                                m_pWriter->m_aMapMode,
    1374             :                                m_pWriter->getReferenceDevice(),
    1375           0 :                                rPoint ) );
    1376             : 
    1377           0 :     sal_Int32 nValue    = aPoint.X();
    1378           0 :     if( bNeg )
    1379           0 :         nValue = -nValue;
    1380             : 
    1381           0 :     appendFixedInt( nValue, rBuffer );
    1382             : 
    1383           0 :     rBuffer.append( ' ' );
    1384             : 
    1385           0 :     nValue      = pointToPixel(getHeight()) - aPoint.Y();
    1386           0 :     if( bNeg )
    1387           0 :         nValue = -nValue;
    1388             : 
    1389           0 :     appendFixedInt( nValue, rBuffer );
    1390           0 : }
    1391             : 
    1392           0 : void PDFWriterImpl::PDFPage::appendPixelPoint( const basegfx::B2DPoint& rPoint, OStringBuffer& rBuffer ) const
    1393             : {
    1394           0 :     double fValue   = pixelToPoint(rPoint.getX());
    1395             : 
    1396           0 :     appendDouble( fValue, rBuffer, nLog10Divisor );
    1397           0 :     rBuffer.append( ' ' );
    1398           0 :     fValue      = double(getHeight()) - pixelToPoint(rPoint.getY());
    1399           0 :     appendDouble( fValue, rBuffer, nLog10Divisor );
    1400           0 : }
    1401             : 
    1402           0 : void PDFWriterImpl::PDFPage::appendRect( const Rectangle& rRect, OStringBuffer& rBuffer ) const
    1403             : {
    1404           0 :     appendPoint( rRect.BottomLeft() + Point( 0, 1 ), rBuffer );
    1405           0 :     rBuffer.append( ' ' );
    1406           0 :     appendMappedLength( (sal_Int32)rRect.GetWidth(), rBuffer, false );
    1407           0 :     rBuffer.append( ' ' );
    1408           0 :     appendMappedLength( (sal_Int32)rRect.GetHeight(), rBuffer, true );
    1409           0 :     rBuffer.append( " re" );
    1410           0 : }
    1411             : 
    1412           0 : void PDFWriterImpl::PDFPage::convertRect( Rectangle& rRect ) const
    1413             : {
    1414           0 :     Point aLL = lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1415             :                              m_pWriter->m_aMapMode,
    1416             :                              m_pWriter->getReferenceDevice(),
    1417           0 :                              rRect.BottomLeft() + Point( 0, 1 )
    1418           0 :                              );
    1419           0 :     Size aSize = lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1420             :                               m_pWriter->m_aMapMode,
    1421             :                               m_pWriter->getReferenceDevice(),
    1422           0 :                               rRect.GetSize() );
    1423           0 :     rRect.Left()    = aLL.X();
    1424           0 :     rRect.Right()   = aLL.X() + aSize.Width();
    1425           0 :     rRect.Top()     = pointToPixel(getHeight()) - aLL.Y();
    1426           0 :     rRect.Bottom()  = rRect.Top() + aSize.Height();
    1427           0 : }
    1428             : 
    1429           0 : void PDFWriterImpl::PDFPage::appendPolygon( const Polygon& rPoly, OStringBuffer& rBuffer, bool bClose ) const
    1430             : {
    1431           0 :     sal_uInt16 nPoints = rPoly.GetSize();
    1432             :     /*
    1433             :      *  #108582# applications do weird things
    1434             :      */
    1435           0 :     sal_uInt32 nBufLen = rBuffer.getLength();
    1436           0 :     if( nPoints > 0 )
    1437             :     {
    1438           0 :         const sal_uInt8* pFlagArray = rPoly.GetConstFlagAry();
    1439           0 :         appendPoint( rPoly[0], rBuffer );
    1440           0 :         rBuffer.append( " m\n" );
    1441           0 :         for( sal_uInt16 i = 1; i < nPoints; i++ )
    1442             :         {
    1443           0 :             if( pFlagArray && pFlagArray[i] == POLY_CONTROL && nPoints-i > 2 )
    1444             :             {
    1445             :                 // bezier
    1446             :                 DBG_ASSERT( pFlagArray[i+1] == POLY_CONTROL && pFlagArray[i+2] != POLY_CONTROL, "unexpected sequence of control points" );
    1447           0 :                 appendPoint( rPoly[i], rBuffer );
    1448           0 :                 rBuffer.append( " " );
    1449           0 :                 appendPoint( rPoly[i+1], rBuffer );
    1450           0 :                 rBuffer.append( " " );
    1451           0 :                 appendPoint( rPoly[i+2], rBuffer );
    1452           0 :                 rBuffer.append( " c" );
    1453           0 :                 i += 2; // add additionally consumed points
    1454             :             }
    1455             :             else
    1456             :             {
    1457             :                 // line
    1458           0 :                 appendPoint( rPoly[i], rBuffer );
    1459           0 :                 rBuffer.append( " l" );
    1460             :             }
    1461           0 :             if( (rBuffer.getLength() - nBufLen) > 65 )
    1462             :             {
    1463           0 :                 rBuffer.append( "\n" );
    1464           0 :                 nBufLen = rBuffer.getLength();
    1465             :             }
    1466             :             else
    1467           0 :                 rBuffer.append( " " );
    1468             :         }
    1469           0 :         if( bClose )
    1470           0 :             rBuffer.append( "h\n" );
    1471             :     }
    1472           0 : }
    1473             : 
    1474           0 : void PDFWriterImpl::PDFPage::appendPolygon( const basegfx::B2DPolygon& rPoly, OStringBuffer& rBuffer, bool bClose ) const
    1475             : {
    1476           0 :     basegfx::B2DPolygon aPoly( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1477             :                                             m_pWriter->m_aMapMode,
    1478             :                                             m_pWriter->getReferenceDevice(),
    1479           0 :                                             rPoly ) );
    1480             : 
    1481           0 :     if( basegfx::tools::isRectangle( aPoly ) )
    1482             :     {
    1483           0 :         basegfx::B2DRange aRange( aPoly.getB2DRange() );
    1484           0 :         basegfx::B2DPoint aBL( aRange.getMinX(), aRange.getMaxY() );
    1485           0 :         appendPixelPoint( aBL, rBuffer );
    1486           0 :         rBuffer.append( ' ' );
    1487           0 :         appendMappedLength( aRange.getWidth(), rBuffer, false, NULL, nLog10Divisor );
    1488           0 :         rBuffer.append( ' ' );
    1489           0 :         appendMappedLength( aRange.getHeight(), rBuffer, true, NULL, nLog10Divisor );
    1490           0 :         rBuffer.append( " re\n" );
    1491           0 :         return;
    1492             :     }
    1493           0 :     sal_uInt32 nPoints = aPoly.count();
    1494           0 :     if( nPoints > 0 )
    1495             :     {
    1496           0 :         sal_uInt32 nBufLen = rBuffer.getLength();
    1497           0 :         basegfx::B2DPoint aLastPoint( aPoly.getB2DPoint( 0 ) );
    1498           0 :         appendPixelPoint( aLastPoint, rBuffer );
    1499           0 :         rBuffer.append( " m\n" );
    1500           0 :         for( sal_uInt32 i = 1; i <= nPoints; i++ )
    1501             :         {
    1502           0 :             if( i != nPoints || aPoly.isClosed() )
    1503             :             {
    1504           0 :                 sal_uInt32 nCurPoint  = i % nPoints;
    1505           0 :                 sal_uInt32 nLastPoint = i-1;
    1506           0 :                 basegfx::B2DPoint aPoint( aPoly.getB2DPoint( nCurPoint ) );
    1507           0 :                 if( aPoly.isNextControlPointUsed( nLastPoint ) &&
    1508           0 :                     aPoly.isPrevControlPointUsed( nCurPoint ) )
    1509             :                 {
    1510           0 :                     appendPixelPoint( aPoly.getNextControlPoint( nLastPoint ), rBuffer );
    1511           0 :                     rBuffer.append( ' ' );
    1512           0 :                     appendPixelPoint( aPoly.getPrevControlPoint( nCurPoint ), rBuffer );
    1513           0 :                     rBuffer.append( ' ' );
    1514           0 :                     appendPixelPoint( aPoint, rBuffer );
    1515           0 :                     rBuffer.append( " c" );
    1516             :                 }
    1517           0 :                 else if( aPoly.isNextControlPointUsed( nLastPoint ) )
    1518             :                 {
    1519           0 :                     appendPixelPoint( aPoly.getNextControlPoint( nLastPoint ), rBuffer );
    1520           0 :                     rBuffer.append( ' ' );
    1521           0 :                     appendPixelPoint( aPoint, rBuffer );
    1522           0 :                     rBuffer.append( " y" );
    1523             :                 }
    1524           0 :                 else if( aPoly.isPrevControlPointUsed( nCurPoint ) )
    1525             :                 {
    1526           0 :                     appendPixelPoint( aPoly.getPrevControlPoint( nCurPoint ), rBuffer );
    1527           0 :                     rBuffer.append( ' ' );
    1528           0 :                     appendPixelPoint( aPoint, rBuffer );
    1529           0 :                     rBuffer.append( " v" );
    1530             :                 }
    1531             :                 else
    1532             :                 {
    1533           0 :                     appendPixelPoint( aPoint, rBuffer );
    1534           0 :                     rBuffer.append( " l" );
    1535             :                 }
    1536           0 :                 if( (rBuffer.getLength() - nBufLen) > 65 )
    1537             :                 {
    1538           0 :                     rBuffer.append( "\n" );
    1539           0 :                     nBufLen = rBuffer.getLength();
    1540             :                 }
    1541             :                 else
    1542           0 :                     rBuffer.append( " " );
    1543             :             }
    1544             :         }
    1545           0 :         if( bClose )
    1546           0 :             rBuffer.append( "h\n" );
    1547           0 :     }
    1548             : }
    1549             : 
    1550           0 : void PDFWriterImpl::PDFPage::appendPolyPolygon( const tools::PolyPolygon& rPolyPoly, OStringBuffer& rBuffer, bool bClose ) const
    1551             : {
    1552           0 :     sal_uInt16 nPolygons = rPolyPoly.Count();
    1553           0 :     for( sal_uInt16 n = 0; n < nPolygons; n++ )
    1554           0 :         appendPolygon( rPolyPoly[n], rBuffer, bClose );
    1555           0 : }
    1556             : 
    1557           0 : void PDFWriterImpl::PDFPage::appendPolyPolygon( const basegfx::B2DPolyPolygon& rPolyPoly, OStringBuffer& rBuffer, bool bClose ) const
    1558             : {
    1559           0 :     sal_uInt32 nPolygons = rPolyPoly.count();
    1560           0 :     for( sal_uInt32 n = 0; n < nPolygons; n++ )
    1561           0 :         appendPolygon( rPolyPoly.getB2DPolygon( n ), rBuffer, bClose );
    1562           0 : }
    1563             : 
    1564           0 : void PDFWriterImpl::PDFPage::appendMappedLength( sal_Int32 nLength, OStringBuffer& rBuffer, bool bVertical, sal_Int32* pOutLength ) const
    1565             : {
    1566           0 :     sal_Int32 nValue = nLength;
    1567           0 :     if ( nLength < 0 )
    1568             :     {
    1569           0 :         rBuffer.append( '-' );
    1570           0 :         nValue = -nLength;
    1571             :     }
    1572           0 :     Size aSize( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1573             :                              m_pWriter->m_aMapMode,
    1574             :                              m_pWriter->getReferenceDevice(),
    1575           0 :                              Size( nValue, nValue ) ) );
    1576           0 :     nValue = bVertical ? aSize.Height() : aSize.Width();
    1577           0 :     if( pOutLength )
    1578           0 :         *pOutLength = ((nLength < 0 ) ? -nValue : nValue);
    1579             : 
    1580           0 :     appendFixedInt( nValue, rBuffer, 1 );
    1581           0 : }
    1582             : 
    1583           0 : void PDFWriterImpl::PDFPage::appendMappedLength( double fLength, OStringBuffer& rBuffer, bool bVertical, sal_Int32* pOutLength, sal_Int32 nPrecision ) const
    1584             : {
    1585           0 :     Size aSize( lcl_convert( m_pWriter->m_aGraphicsStack.front().m_aMapMode,
    1586             :                              m_pWriter->m_aMapMode,
    1587             :                              m_pWriter->getReferenceDevice(),
    1588           0 :                              Size( 1000, 1000 ) ) );
    1589           0 :     if( pOutLength )
    1590           0 :         *pOutLength = (sal_Int32)(fLength*(double)(bVertical ? aSize.Height() : aSize.Width())/1000.0);
    1591           0 :     fLength *= pixelToPoint((double)(bVertical ? aSize.Height() : aSize.Width()) / 1000.0);
    1592           0 :     appendDouble( fLength, rBuffer, nPrecision );
    1593           0 : }
    1594             : 
    1595           0 : bool PDFWriterImpl::PDFPage::appendLineInfo( const LineInfo& rInfo, OStringBuffer& rBuffer ) const
    1596             : {
    1597           0 :     if(LINE_DASH == rInfo.GetStyle() && rInfo.GetDashLen() != rInfo.GetDotLen())
    1598             :     {
    1599             :         // dashed and non-degraded case, check for implementation limits of dash array
    1600             :         // in PDF reader apps (e.g. acroread)
    1601           0 :         if(2 * (rInfo.GetDashCount() + rInfo.GetDotCount()) > 10)
    1602             :         {
    1603           0 :             return false;
    1604             :         }
    1605             :     }
    1606             : 
    1607           0 :     if(basegfx::B2DLINEJOIN_NONE != rInfo.GetLineJoin())
    1608             :     {
    1609             :         // LineJoin used, ExtLineInfo required
    1610           0 :         return false;
    1611             :     }
    1612             : 
    1613           0 :     if(com::sun::star::drawing::LineCap_BUTT != rInfo.GetLineCap())
    1614             :     {
    1615             :         // LineCap used, ExtLineInfo required
    1616           0 :         return false;
    1617             :     }
    1618             : 
    1619           0 :     if( rInfo.GetStyle() == LINE_DASH )
    1620             :     {
    1621           0 :         rBuffer.append( "[ " );
    1622           0 :         if( rInfo.GetDashLen() == rInfo.GetDotLen() ) // degraded case
    1623             :         {
    1624           0 :             appendMappedLength( (sal_Int32)rInfo.GetDashLen(), rBuffer );
    1625           0 :             rBuffer.append( ' ' );
    1626           0 :             appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1627           0 :             rBuffer.append( ' ' );
    1628             :         }
    1629             :         else
    1630             :         {
    1631           0 :             for( int n = 0; n < rInfo.GetDashCount(); n++ )
    1632             :             {
    1633           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDashLen(), rBuffer );
    1634           0 :                 rBuffer.append( ' ' );
    1635           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1636           0 :                 rBuffer.append( ' ' );
    1637             :             }
    1638           0 :             for( int m = 0; m < rInfo.GetDotCount(); m++ )
    1639             :             {
    1640           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDotLen(), rBuffer );
    1641           0 :                 rBuffer.append( ' ' );
    1642           0 :                 appendMappedLength( (sal_Int32)rInfo.GetDistance(), rBuffer );
    1643           0 :                 rBuffer.append( ' ' );
    1644             :             }
    1645             :         }
    1646           0 :         rBuffer.append( "] 0 d\n" );
    1647             :     }
    1648             : 
    1649           0 :     if( rInfo.GetWidth() > 1 )
    1650             :     {
    1651           0 :         appendMappedLength( (sal_Int32)rInfo.GetWidth(), rBuffer );
    1652           0 :         rBuffer.append( " w\n" );
    1653             :     }
    1654           0 :     else if( rInfo.GetWidth() == 0 )
    1655             :     {
    1656             :         // "pixel" line
    1657           0 :         appendDouble( 72.0/double(m_pWriter->getReferenceDevice()->GetDPIX()), rBuffer );
    1658           0 :         rBuffer.append( " w\n" );
    1659             :     }
    1660             : 
    1661           0 :     return true;
    1662             : }
    1663             : 
    1664           0 : void PDFWriterImpl::PDFPage::appendWaveLine( sal_Int32 nWidth, sal_Int32 nY, sal_Int32 nDelta, OStringBuffer& rBuffer ) const
    1665             : {
    1666           0 :     if( nWidth <= 0 )
    1667           0 :         return;
    1668           0 :     if( nDelta < 1 )
    1669           0 :         nDelta = 1;
    1670             : 
    1671           0 :     rBuffer.append( "0 " );
    1672           0 :     appendMappedLength( nY, rBuffer, true );
    1673           0 :     rBuffer.append( " m\n" );
    1674           0 :     for( sal_Int32 n = 0; n < nWidth; )
    1675             :     {
    1676           0 :         n += nDelta;
    1677           0 :         appendMappedLength( n, rBuffer, false );
    1678           0 :         rBuffer.append( ' ' );
    1679           0 :         appendMappedLength( nDelta+nY, rBuffer, true );
    1680           0 :         rBuffer.append( ' ' );
    1681           0 :         n += nDelta;
    1682           0 :         appendMappedLength( n, rBuffer, false );
    1683           0 :         rBuffer.append( ' ' );
    1684           0 :         appendMappedLength( nY, rBuffer, true );
    1685           0 :         rBuffer.append( " v " );
    1686           0 :         if( n < nWidth )
    1687             :         {
    1688           0 :             n += nDelta;
    1689           0 :             appendMappedLength( n, rBuffer, false );
    1690           0 :             rBuffer.append( ' ' );
    1691           0 :             appendMappedLength( nY-nDelta, rBuffer, true );
    1692           0 :             rBuffer.append( ' ' );
    1693           0 :             n += nDelta;
    1694           0 :             appendMappedLength( n, rBuffer, false );
    1695           0 :             rBuffer.append( ' ' );
    1696           0 :             appendMappedLength( nY, rBuffer, true );
    1697           0 :             rBuffer.append( " v\n" );
    1698             :         }
    1699             :     }
    1700           0 :     rBuffer.append( "S\n" );
    1701             : }
    1702             : 
    1703           0 :  PDFWriterImpl::PDFWriterImpl( const PDFWriter::PDFWriterContext& rContext,
    1704             :                                const com::sun::star::uno::Reference< com::sun::star::beans::XMaterialHolder >& xEnc,
    1705             :                                PDFWriter& i_rOuterFace)
    1706             :         :
    1707             :         m_pReferenceDevice( NULL ),
    1708           0 :         m_aMapMode( MAP_POINT, Point(), Fraction( 1L, pointToPixel(1) ), Fraction( 1L, pointToPixel(1) ) ),
    1709             :         m_nCurrentStructElement( 0 ),
    1710             :         m_bEmitStructure( true ),
    1711             :         m_bNewMCID( false ),
    1712             :         m_nNextFID( 1 ),
    1713             :         m_nInheritedPageWidth( 595 ),  // default A4
    1714             :         m_nInheritedPageHeight( 842 ), // default A4
    1715             :         m_eInheritedOrientation( PDFWriter::Portrait ),
    1716             :         m_nCurrentPage( -1 ),
    1717             :         m_nCatalogObject(0),
    1718             :         m_nSignatureObject( -1 ),
    1719             :         m_nSignatureContentOffset( 0 ),
    1720             :         m_nSignatureLastByteRangeNoOffset( 0 ),
    1721             :         m_nResourceDict( -1 ),
    1722             :         m_nFontDictObject( -1 ),
    1723             :         m_aContext(rContext),
    1724             :         m_aFile(m_aContext.URL),
    1725             :         m_bOpen(false),
    1726             :         m_pCodec( NULL ),
    1727             :         m_pMemStream(NULL),
    1728           0 :         m_aDocDigest( rtl_digest_createMD5() ),
    1729             :         m_aCipher( (rtlCipher)NULL ),
    1730             :         m_aDigest( NULL ),
    1731             :         m_nKeyLength(0),
    1732             :         m_nRC4KeyLength(0),
    1733             :         m_bEncryptThisStream( false ),
    1734             :         m_nAccessPermissions(0),
    1735             :         m_pEncryptionBuffer( NULL ),
    1736             :         m_nEncryptionBufferSize( 0 ),
    1737             :         m_bIsPDF_A1( false ),
    1738           0 :         m_rOuterFace( i_rOuterFace )
    1739             : {
    1740             : #ifdef DO_TEST_PDF
    1741             :     static bool bOnce = true;
    1742             :     if( bOnce )
    1743             :     {
    1744             :         bOnce = false;
    1745             :         doTestCode();
    1746             :     }
    1747             : #endif
    1748           0 :     m_aStructure.push_back( PDFStructureElement() );
    1749           0 :     m_aStructure[0].m_nOwnElement       = 0;
    1750           0 :     m_aStructure[0].m_nParentElement    = 0;
    1751             : 
    1752           0 :     Font aFont;
    1753           0 :     aFont.SetName( OUString( "Times" ) );
    1754           0 :     aFont.SetSize( Size( 0, 12 ) );
    1755             : 
    1756           0 :     GraphicsState aState;
    1757           0 :     aState.m_aMapMode       = m_aMapMode;
    1758           0 :     aState.m_aFont          = aFont;
    1759           0 :     m_aGraphicsStack.push_front( aState );
    1760             : 
    1761           0 :     osl::File::RC aError = m_aFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
    1762           0 :     if (aError != osl::File::E_None)
    1763             :     {
    1764           0 :         if (aError == osl::File::E_EXIST)
    1765             :         {
    1766           0 :             aError = m_aFile.open(osl_File_OpenFlag_Write);
    1767           0 :             if (aError == osl::File::E_None)
    1768           0 :                 aError = m_aFile.setSize(0);
    1769             :         }
    1770             :     }
    1771           0 :     if (aError != osl::File::E_None)
    1772           0 :         return;
    1773             : 
    1774           0 :     m_bOpen = true;
    1775             : 
    1776             :     // setup DocInfo
    1777           0 :     setupDocInfo();
    1778             : 
    1779             :     /* prepare the cypher engine, can be done in CTOR, free in DTOR */
    1780           0 :     m_aCipher = rtl_cipher_createARCFOUR( rtl_Cipher_ModeStream );
    1781           0 :     m_aDigest = rtl_digest_createMD5();
    1782             : 
    1783             :     /* the size of the Codec default maximum */
    1784           0 :     if (!checkEncryptionBufferSize(0x4000))
    1785             :     {
    1786           0 :         m_aFile.close();
    1787           0 :         m_bOpen = false;
    1788           0 :         return;
    1789             :     }
    1790             : 
    1791           0 :     if( xEnc.is() )
    1792           0 :         prepareEncryption( xEnc );
    1793             : 
    1794           0 :     if( m_aContext.Encryption.Encrypt() )
    1795             :     {
    1796             :         // sanity check
    1797           0 :         if( m_aContext.Encryption.OValue.size() != ENCRYPTED_PWD_SIZE ||
    1798           0 :             m_aContext.Encryption.UValue.size() != ENCRYPTED_PWD_SIZE ||
    1799           0 :             m_aContext.Encryption.EncryptionKey.size() != MAXIMUM_RC4_KEY_LENGTH
    1800             :            )
    1801             :         {
    1802             :             // the field lengths are invalid ? This was not setup by initEncryption.
    1803             :             // do not encrypt after all
    1804           0 :             m_aContext.Encryption.OValue.clear();
    1805           0 :             m_aContext.Encryption.UValue.clear();
    1806             :             OSL_ENSURE( false, "encryption data failed sanity check, encryption disabled" );
    1807             :         }
    1808             :         else // setup key lengths
    1809           0 :             m_nAccessPermissions = computeAccessPermissions( m_aContext.Encryption, m_nKeyLength, m_nRC4KeyLength );
    1810             :     }
    1811             : 
    1812             :     // write header
    1813           0 :     OStringBuffer aBuffer( 20 );
    1814           0 :     aBuffer.append( "%PDF-" );
    1815           0 :     switch( m_aContext.Version )
    1816             :     {
    1817           0 :         case PDFWriter::PDF_1_2: aBuffer.append( "1.2" );break;
    1818           0 :         case PDFWriter::PDF_1_3: aBuffer.append( "1.3" );break;
    1819             :         case PDFWriter::PDF_A_1:
    1820             :         default:
    1821           0 :         case PDFWriter::PDF_1_4: aBuffer.append( "1.4" );break;
    1822           0 :         case PDFWriter::PDF_1_5: aBuffer.append( "1.5" );break;
    1823             :     }
    1824             :     // append something binary as comment (suggested in PDF Reference)
    1825           0 :     aBuffer.append( "\n%\303\244\303\274\303\266\303\237\n" );
    1826           0 :     if( !writeBuffer( aBuffer.getStr(), aBuffer.getLength() ) )
    1827             :     {
    1828           0 :         m_aFile.close();
    1829           0 :         m_bOpen = false;
    1830           0 :         return;
    1831             :     }
    1832             : 
    1833             :     // insert outline root
    1834           0 :     m_aOutline.push_back( PDFOutlineEntry() );
    1835             : 
    1836           0 :     m_bIsPDF_A1 = (m_aContext.Version == PDFWriter::PDF_A_1);
    1837           0 :     if( m_bIsPDF_A1 )
    1838           0 :         m_aContext.Version = PDFWriter::PDF_1_4; //meaning we need PDF 1.4, PDF/A flavour
    1839             : }
    1840             : 
    1841           0 : PDFWriterImpl::~PDFWriterImpl()
    1842             : {
    1843           0 :     if( m_aDocDigest )
    1844           0 :         rtl_digest_destroyMD5( m_aDocDigest );
    1845           0 :     delete static_cast<VirtualDevice*>(m_pReferenceDevice);
    1846             : 
    1847           0 :     if( m_aCipher )
    1848           0 :         rtl_cipher_destroyARCFOUR( m_aCipher );
    1849           0 :     if( m_aDigest )
    1850           0 :         rtl_digest_destroyMD5( m_aDigest );
    1851             : 
    1852           0 :     rtl_freeMemory( m_pEncryptionBuffer );
    1853           0 : }
    1854             : 
    1855           0 : void PDFWriterImpl::setupDocInfo()
    1856             : {
    1857           0 :     std::vector< sal_uInt8 > aId;
    1858           0 :     computeDocumentIdentifier( aId, m_aContext.DocumentInfo, m_aCreationDateString, m_aCreationMetaDateString );
    1859           0 :     if( m_aContext.Encryption.DocumentIdentifier.empty() )
    1860           0 :         m_aContext.Encryption.DocumentIdentifier = aId;
    1861           0 : }
    1862             : 
    1863           0 : void PDFWriterImpl::computeDocumentIdentifier( std::vector< sal_uInt8 >& o_rIdentifier,
    1864             :                                                const vcl::PDFWriter::PDFDocInfo& i_rDocInfo,
    1865             :                                                OString& o_rCString1,
    1866             :                                                OString& o_rCString2
    1867             :                                                )
    1868             : {
    1869           0 :     o_rIdentifier.clear();
    1870             : 
    1871             :     //build the document id
    1872           0 :     OString aInfoValuesOut;
    1873           0 :     OStringBuffer aID( 1024 );
    1874           0 :     if( !i_rDocInfo.Title.isEmpty() )
    1875           0 :         appendUnicodeTextString( i_rDocInfo.Title, aID );
    1876           0 :     if( !i_rDocInfo.Author.isEmpty() )
    1877           0 :         appendUnicodeTextString( i_rDocInfo.Author, aID );
    1878           0 :     if( !i_rDocInfo.Subject.isEmpty() )
    1879           0 :         appendUnicodeTextString( i_rDocInfo.Subject, aID );
    1880           0 :     if( !i_rDocInfo.Keywords.isEmpty() )
    1881           0 :         appendUnicodeTextString( i_rDocInfo.Keywords, aID );
    1882           0 :     if( !i_rDocInfo.Creator.isEmpty() )
    1883           0 :         appendUnicodeTextString( i_rDocInfo.Creator, aID );
    1884           0 :     if( !i_rDocInfo.Producer.isEmpty() )
    1885           0 :         appendUnicodeTextString( i_rDocInfo.Producer, aID );
    1886             : 
    1887             :     TimeValue aTVal, aGMT;
    1888             :     oslDateTime aDT;
    1889           0 :     osl_getSystemTime( &aGMT );
    1890           0 :     osl_getLocalTimeFromSystemTime( &aGMT, &aTVal );
    1891           0 :     osl_getDateTimeFromTimeValue( &aTVal, &aDT );
    1892           0 :     OStringBuffer aCreationDateString(64), aCreationMetaDateString(64);
    1893           0 :     aCreationDateString.append( "D:" );
    1894           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/1000)%10)) );
    1895           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/100)%10)) );
    1896           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year/10)%10)) );
    1897           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Year)%10)) );
    1898           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Month/10)%10)) );
    1899           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Month)%10)) );
    1900           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Day/10)%10)) );
    1901           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Day)%10)) );
    1902           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Hours/10)%10)) );
    1903           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Hours)%10)) );
    1904           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Minutes/10)%10)) );
    1905           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Minutes)%10)) );
    1906           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Seconds/10)%10)) );
    1907           0 :     aCreationDateString.append( (sal_Char)('0' + ((aDT.Seconds)%10)) );
    1908             : 
    1909             :     //--> i59651, we fill the Metadata date string as well, if PDF/A is requested
    1910             :     // according to ISO 19005-1:2005 6.7.3 the date is corrected for
    1911             :     // local time zone offset UTC only, whereas Acrobat 8 seems
    1912             :     // to use the localtime notation only
    1913             :     // according to a recommendation in XMP Specification (Jan 2004, page 75)
    1914             :     // the Acrobat way seems the right approach
    1915           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/1000)%10)) );
    1916           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/100)%10)) );
    1917           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year/10)%10)) );
    1918           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Year)%10)) );
    1919           0 :     aCreationMetaDateString.append( "-" );
    1920           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Month/10)%10)) );
    1921           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Month)%10)) );
    1922           0 :     aCreationMetaDateString.append( "-" );
    1923           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Day/10)%10)) );
    1924           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Day)%10)) );
    1925           0 :     aCreationMetaDateString.append( "T" );
    1926           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Hours/10)%10)) );
    1927           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Hours)%10)) );
    1928           0 :     aCreationMetaDateString.append( ":" );
    1929           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Minutes/10)%10)) );
    1930           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Minutes)%10)) );
    1931           0 :     aCreationMetaDateString.append( ":" );
    1932           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Seconds/10)%10)) );
    1933           0 :     aCreationMetaDateString.append( (sal_Char)('0' + ((aDT.Seconds)%10)) );
    1934             : 
    1935           0 :     sal_uInt32 nDelta = 0;
    1936           0 :     if( aGMT.Seconds > aTVal.Seconds )
    1937             :     {
    1938           0 :         aCreationDateString.append( "-" );
    1939           0 :         nDelta = aGMT.Seconds-aTVal.Seconds;
    1940           0 :         aCreationMetaDateString.append( "-" );
    1941             :     }
    1942           0 :     else if( aGMT.Seconds < aTVal.Seconds )
    1943             :     {
    1944           0 :         aCreationDateString.append( "+" );
    1945           0 :         nDelta = aTVal.Seconds-aGMT.Seconds;
    1946           0 :         aCreationMetaDateString.append( "+" );
    1947             :     }
    1948             :     else
    1949             :     {
    1950           0 :         aCreationDateString.append( "Z" );
    1951           0 :         aCreationMetaDateString.append( "Z" );
    1952             : 
    1953             :     }
    1954           0 :     if( nDelta )
    1955             :     {
    1956           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/36000)%10)) );
    1957           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/3600)%10)) );
    1958           0 :         aCreationDateString.append( "'" );
    1959           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/600)%6)) );
    1960           0 :         aCreationDateString.append( (sal_Char)('0' + ((nDelta/60)%10)) );
    1961             : 
    1962           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/36000)%10)) );
    1963           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/3600)%10)) );
    1964           0 :         aCreationMetaDateString.append( ":" );
    1965           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/600)%6)) );
    1966           0 :         aCreationMetaDateString.append( (sal_Char)('0' + ((nDelta/60)%10)) );
    1967             :     }
    1968           0 :     aCreationDateString.append( "'" );
    1969           0 :     aID.append( aCreationDateString.getStr(), aCreationDateString.getLength() );
    1970             : 
    1971           0 :     aInfoValuesOut = aID.makeStringAndClear();
    1972           0 :     o_rCString1 = aCreationDateString.makeStringAndClear();
    1973           0 :     o_rCString2 = aCreationMetaDateString.makeStringAndClear();
    1974             : 
    1975           0 :     rtlDigest aDigest = rtl_digest_createMD5();
    1976             :     OSL_ENSURE( aDigest != NULL, "PDFWriterImpl::computeDocumentIdentifier: cannot obtain a digest object !" );
    1977           0 :     if( aDigest )
    1978             :     {
    1979           0 :         rtlDigestError nError = rtl_digest_updateMD5( aDigest, &aGMT, sizeof( aGMT ) );
    1980           0 :         if( nError == rtl_Digest_E_None )
    1981           0 :             nError = rtl_digest_updateMD5( aDigest, aInfoValuesOut.getStr(), aInfoValuesOut.getLength() );
    1982           0 :         if( nError == rtl_Digest_E_None )
    1983             :         {
    1984           0 :             o_rIdentifier = std::vector< sal_uInt8 >( 16, 0 );
    1985             :             //the binary form of the doc id is needed for encryption stuff
    1986           0 :             rtl_digest_getMD5( aDigest, &o_rIdentifier[0], 16 );
    1987             :         }
    1988           0 :         rtl_digest_destroyMD5(aDigest);
    1989           0 :     }
    1990           0 : }
    1991             : 
    1992             : /* i12626 methods */
    1993             : /*
    1994             : check if the Unicode string must be encrypted or not, perform the requested task,
    1995             : append the string as unicode hex, encrypted if needed
    1996             :  */
    1997           0 : inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    1998             : {
    1999           0 :     rOutBuffer.append( "<" );
    2000           0 :     if( m_aContext.Encryption.Encrypt() )
    2001             :     {
    2002           0 :         const sal_Unicode* pStr = rInString.getStr();
    2003           0 :         sal_Int32 nLen = rInString.getLength();
    2004             :         //prepare a unicode string, encrypt it
    2005           0 :         if( checkEncryptionBufferSize( nLen*2 ) )
    2006             :         {
    2007           0 :             enableStringEncryption( nInObjectNumber );
    2008           0 :             sal_uInt8 *pCopy = m_pEncryptionBuffer;
    2009           0 :             sal_Int32 nChars = 2;
    2010           0 :             *pCopy++ = 0xFE;
    2011           0 :             *pCopy++ = 0xFF;
    2012             :             // we need to prepare a byte stream from the unicode string buffer
    2013           0 :             for( int i = 0; i < nLen; i++ )
    2014             :             {
    2015           0 :                 sal_Unicode aUnChar = pStr[i];
    2016           0 :                 *pCopy++ = (sal_uInt8)( aUnChar >> 8 );
    2017           0 :                 *pCopy++ = (sal_uInt8)( aUnChar & 255 );
    2018           0 :                 nChars += 2;
    2019             :             }
    2020             :             //encrypt in place
    2021           0 :             rtl_cipher_encodeARCFOUR( m_aCipher, m_pEncryptionBuffer, nChars, m_pEncryptionBuffer, nChars );
    2022             :             //now append, hexadecimal (appendHex), the encrypted result
    2023           0 :             for(int i = 0; i < nChars; i++)
    2024           0 :                 appendHex( m_pEncryptionBuffer[i], rOutBuffer );
    2025             :         }
    2026             :     }
    2027             :     else
    2028           0 :         appendUnicodeTextString( rInString, rOutBuffer );
    2029           0 :     rOutBuffer.append( ">" );
    2030           0 : }
    2031             : 
    2032           0 : inline void PDFWriterImpl::appendLiteralStringEncrypt( OStringBuffer& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2033             : {
    2034           0 :     rOutBuffer.append( "(" );
    2035           0 :     sal_Int32 nChars = rInString.getLength();
    2036             :     //check for encryption, if ok, encrypt the string, then convert with appndLiteralString
    2037           0 :     if( m_aContext.Encryption.Encrypt() && checkEncryptionBufferSize( nChars ) )
    2038             :     {
    2039             :         //encrypt the string in a buffer, then append it
    2040           0 :         enableStringEncryption( nInObjectNumber );
    2041           0 :         rtl_cipher_encodeARCFOUR( m_aCipher, rInString.getStr(), nChars, m_pEncryptionBuffer, nChars );
    2042           0 :         appendLiteralString( (const sal_Char*)m_pEncryptionBuffer, nChars, rOutBuffer );
    2043             :     }
    2044             :     else
    2045           0 :         appendLiteralString( rInString.getStr(), nChars , rOutBuffer );
    2046           0 :     rOutBuffer.append( ")" );
    2047           0 : }
    2048             : 
    2049           0 : inline void PDFWriterImpl::appendLiteralStringEncrypt( const OString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer )
    2050             : {
    2051           0 :     OStringBuffer aBufferString( rInString );
    2052           0 :     appendLiteralStringEncrypt( aBufferString, nInObjectNumber, rOutBuffer);
    2053           0 : }
    2054             : 
    2055           0 : void PDFWriterImpl::appendLiteralStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer, rtl_TextEncoding nEnc )
    2056             : {
    2057           0 :     OString aBufferString( OUStringToOString( rInString, nEnc ) );
    2058           0 :     sal_Int32 nLen = aBufferString.getLength();
    2059           0 :     OStringBuffer aBuf( nLen );
    2060           0 :     const sal_Char* pT = aBufferString.getStr();
    2061             : 
    2062           0 :     for( sal_Int32 i = 0; i < nLen; i++, pT++ )
    2063             :     {
    2064           0 :         if( (*pT & 0x80) == 0 )
    2065           0 :             aBuf.append( *pT );
    2066             :         else
    2067             :         {
    2068           0 :             aBuf.append( '<' );
    2069           0 :             appendHex( *pT, aBuf );
    2070           0 :             aBuf.append( '>' );
    2071             :         }
    2072             :     }
    2073           0 :     aBufferString = aBuf.makeStringAndClear();
    2074           0 :     appendLiteralStringEncrypt( aBufferString, nInObjectNumber, rOutBuffer);
    2075           0 : }
    2076             : 
    2077             : /* end i12626 methods */
    2078             : 
    2079           0 : void PDFWriterImpl::emitComment( const char* pComment )
    2080             : {
    2081           0 :     OStringBuffer aLine( 64 );
    2082           0 :     aLine.append( "% " );
    2083           0 :     aLine.append( (const sal_Char*)pComment );
    2084           0 :     aLine.append( "\n" );
    2085           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    2086           0 : }
    2087             : 
    2088           0 : bool PDFWriterImpl::compressStream( SvMemoryStream* pStream )
    2089             : {
    2090             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2091           0 :     pStream->Seek( STREAM_SEEK_TO_END );
    2092           0 :     sal_uLong nEndPos = pStream->Tell();
    2093           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    2094           0 :     ZCodec pCodec( 0x4000, 0x4000 );
    2095           0 :     SvMemoryStream aStream;
    2096           0 :     pCodec.BeginCompression();
    2097           0 :     pCodec.Write( aStream, (const sal_uInt8*)pStream->GetData(), nEndPos );
    2098           0 :     pCodec.EndCompression();
    2099           0 :     nEndPos = aStream.Tell();
    2100           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
    2101           0 :     aStream.Seek( STREAM_SEEK_TO_BEGIN );
    2102           0 :     pStream->SetStreamSize( nEndPos );
    2103           0 :     pStream->Write( aStream.GetData(), nEndPos );
    2104           0 :     return true;
    2105             : #else
    2106             :     (void)pStream;
    2107             :     return false;
    2108             : #endif
    2109             : }
    2110             : 
    2111           0 : void PDFWriterImpl::beginCompression()
    2112             : {
    2113             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2114           0 :     m_pCodec = new ZCodec( 0x4000, 0x4000 );
    2115           0 :     m_pMemStream = new SvMemoryStream();
    2116           0 :     m_pCodec->BeginCompression();
    2117             : #endif
    2118           0 : }
    2119             : 
    2120           0 : void PDFWriterImpl::endCompression()
    2121             : {
    2122             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    2123           0 :     if( m_pCodec )
    2124             :     {
    2125           0 :         m_pCodec->EndCompression();
    2126           0 :         delete m_pCodec;
    2127           0 :         m_pCodec = NULL;
    2128           0 :         sal_uInt64 nLen = m_pMemStream->Tell();
    2129           0 :         m_pMemStream->Seek( 0 );
    2130           0 :         writeBuffer( m_pMemStream->GetData(), nLen );
    2131           0 :         delete m_pMemStream;
    2132           0 :         m_pMemStream = NULL;
    2133             :     }
    2134             : #endif
    2135           0 : }
    2136             : 
    2137           0 : bool PDFWriterImpl::writeBuffer( const void* pBuffer, sal_uInt64 nBytes )
    2138             : {
    2139           0 :     if( ! m_bOpen ) // we are already down the drain
    2140           0 :         return false;
    2141             : 
    2142           0 :     if( ! nBytes ) // huh ?
    2143           0 :         return true;
    2144             : 
    2145           0 :     if( m_aOutputStreams.begin() != m_aOutputStreams.end() )
    2146             :     {
    2147           0 :         m_aOutputStreams.front().m_pStream->Seek( STREAM_SEEK_TO_END );
    2148           0 :         m_aOutputStreams.front().m_pStream->Write( pBuffer, sal::static_int_cast<sal_Size>(nBytes) );
    2149           0 :         return true;
    2150             :     }
    2151             : 
    2152             :     sal_uInt64 nWritten;
    2153           0 :     if( m_pCodec )
    2154             :     {
    2155           0 :         m_pCodec->Write( *m_pMemStream, static_cast<const sal_uInt8*>(pBuffer), (sal_uLong)nBytes );
    2156           0 :         nWritten = nBytes;
    2157             :     }
    2158             :     else
    2159             :     {
    2160           0 :         bool  buffOK = true;
    2161           0 :         if( m_bEncryptThisStream )
    2162             :         {
    2163             :             /* implement the encryption part of the PDF spec encryption algorithm 3.1 */
    2164           0 :             if( ( buffOK = checkEncryptionBufferSize( static_cast<sal_Int32>(nBytes) ) ) )
    2165             :                 rtl_cipher_encodeARCFOUR( m_aCipher,
    2166             :                                           (sal_uInt8*)pBuffer, static_cast<sal_Size>(nBytes),
    2167           0 :                                           m_pEncryptionBuffer, static_cast<sal_Size>(nBytes) );
    2168             :         }
    2169             : 
    2170           0 :         const void* pWriteBuffer = ( m_bEncryptThisStream && buffOK ) ? m_pEncryptionBuffer  : pBuffer;
    2171           0 :         if( m_aDocDigest )
    2172           0 :             rtl_digest_updateMD5( m_aDocDigest, pWriteBuffer, static_cast<sal_uInt32>(nBytes) );
    2173             : 
    2174           0 :         if (m_aFile.write(pWriteBuffer, nBytes, nWritten) != osl::File::E_None)
    2175           0 :             nWritten = 0;
    2176             : 
    2177           0 :         if( nWritten != nBytes )
    2178             :         {
    2179           0 :             m_aFile.close();
    2180           0 :             m_bOpen = false;
    2181             :         }
    2182             :     }
    2183             : 
    2184           0 :     return nWritten == nBytes;
    2185             : }
    2186             : 
    2187           0 : OutputDevice* PDFWriterImpl::getReferenceDevice()
    2188             : {
    2189           0 :     if( ! m_pReferenceDevice )
    2190             :     {
    2191           0 :         VirtualDevice*  pVDev = new VirtualDevice( 0 );
    2192             : 
    2193           0 :         m_pReferenceDevice = pVDev;
    2194             : 
    2195           0 :         if( m_aContext.DPIx == 0 || m_aContext.DPIy == 0 )
    2196           0 :             pVDev->SetReferenceDevice( VirtualDevice::REFDEV_MODE_PDF1 );
    2197             :         else
    2198           0 :             pVDev->SetReferenceDevice( m_aContext.DPIx, m_aContext.DPIy );
    2199             : 
    2200           0 :         pVDev->SetOutputSizePixel( Size( 640, 480 ) );
    2201           0 :         pVDev->SetMapMode( MAP_MM );
    2202             : 
    2203           0 :         m_pReferenceDevice->mpPDFWriter = this;
    2204           0 :         m_pReferenceDevice->ImplUpdateFontData( true );
    2205             :     }
    2206           0 :     return m_pReferenceDevice;
    2207             : }
    2208             : 
    2209           0 : class ImplPdfBuiltinFontData : public PhysicalFontFace
    2210             : {
    2211             : private:
    2212             :     const PDFWriterImpl::BuiltinFont& mrBuiltin;
    2213             : 
    2214             : public:
    2215             :     enum {PDF_FONT_MAGIC = 0xBDFF0A1C };
    2216             :                                         ImplPdfBuiltinFontData( const PDFWriterImpl::BuiltinFont& );
    2217           0 :     const PDFWriterImpl::BuiltinFont&   GetBuiltinFont() const  { return mrBuiltin; }
    2218             : 
    2219           0 :     virtual PhysicalFontFace*           Clone() const SAL_OVERRIDE { return new ImplPdfBuiltinFontData(*this); }
    2220             :     virtual ImplFontEntry*              CreateFontInstance( FontSelectPattern& ) const SAL_OVERRIDE;
    2221           0 :     virtual sal_IntPtr                  GetFontId() const SAL_OVERRIDE { return reinterpret_cast<sal_IntPtr>(&mrBuiltin); }
    2222             : };
    2223             : 
    2224           0 : inline const ImplPdfBuiltinFontData* GetPdfFontData( const PhysicalFontFace* pFontData )
    2225             : {
    2226           0 :     const ImplPdfBuiltinFontData* pFD = NULL;
    2227           0 :     if( pFontData && pFontData->CheckMagic( ImplPdfBuiltinFontData::PDF_FONT_MAGIC ) )
    2228           0 :         pFD = static_cast<const ImplPdfBuiltinFontData*>( pFontData );
    2229           0 :     return pFD;
    2230             : }
    2231             : 
    2232           0 : static ImplDevFontAttributes GetDevFontAttributes( const PDFWriterImpl::BuiltinFont& rBuiltin )
    2233             : {
    2234           0 :     ImplDevFontAttributes aDFA;
    2235           0 :     aDFA.SetFamilyName( OUString::createFromAscii( rBuiltin.m_pName ) );
    2236           0 :     aDFA.SetStyleName( OUString::createFromAscii( rBuiltin.m_pStyleName ) );
    2237           0 :     aDFA.SetFamilyType( rBuiltin.m_eFamily );
    2238           0 :     aDFA.SetSymbolFlag( rBuiltin.m_eCharSet != RTL_TEXTENCODING_MS_1252 );
    2239           0 :     aDFA.SetPitch( rBuiltin.m_ePitch );
    2240           0 :     aDFA.SetWeight( rBuiltin.m_eWeight );
    2241           0 :     aDFA.SetItalic( rBuiltin.m_eItalic );
    2242           0 :     aDFA.SetWidthType( rBuiltin.m_eWidthType );
    2243             : 
    2244           0 :     aDFA.mbOrientation  = true;
    2245           0 :     aDFA.mbDevice       = true;
    2246           0 :     aDFA.mnQuality      = 50000;
    2247           0 :     aDFA.mbSubsettable  = false;
    2248           0 :     aDFA.mbEmbeddable   = false;
    2249           0 :     return aDFA;
    2250             : }
    2251             : 
    2252           0 : ImplPdfBuiltinFontData::ImplPdfBuiltinFontData( const PDFWriterImpl::BuiltinFont& rBuiltin )
    2253             : :   PhysicalFontFace( GetDevFontAttributes(rBuiltin), PDF_FONT_MAGIC ),
    2254           0 :     mrBuiltin( rBuiltin )
    2255           0 : {}
    2256             : 
    2257           0 : ImplFontEntry* ImplPdfBuiltinFontData::CreateFontInstance( FontSelectPattern& rFSD ) const
    2258             : {
    2259           0 :     ImplFontEntry* pEntry = new ImplFontEntry( rFSD );
    2260           0 :     return pEntry;
    2261             : }
    2262             : 
    2263             : // - PDFWriterImpl -
    2264             : 
    2265           0 : sal_Int32 PDFWriterImpl::newPage( sal_Int32 nPageWidth, sal_Int32 nPageHeight, PDFWriter::Orientation eOrientation )
    2266             : {
    2267           0 :     endPage();
    2268           0 :     m_nCurrentPage = m_aPages.size();
    2269           0 :     m_aPages.push_back( PDFPage(this, nPageWidth, nPageHeight, eOrientation ) );
    2270           0 :     m_aPages.back().m_nPageIndex = m_nCurrentPage;
    2271           0 :     m_aPages.back().beginStream();
    2272             : 
    2273             :     // setup global graphics state
    2274             :     // linewidth is "1 pixel" by default
    2275           0 :     OStringBuffer aBuf( 16 );
    2276           0 :     appendDouble( 72.0/double(getReferenceDevice()->GetDPIX()), aBuf );
    2277           0 :     aBuf.append( " w\n" );
    2278           0 :     writeBuffer( aBuf.getStr(), aBuf.getLength() );
    2279             : 
    2280           0 :     return m_nCurrentPage;
    2281             : }
    2282             : 
    2283           0 : void PDFWriterImpl::endPage()
    2284             : {
    2285           0 :     if( m_aPages.begin() != m_aPages.end() )
    2286             :     {
    2287             :         // close eventual MC sequence
    2288           0 :         endStructureElementMCSeq();
    2289             : 
    2290             :         // sanity check
    2291           0 :         if( m_aOutputStreams.begin() != m_aOutputStreams.end() )
    2292             :         {
    2293             :             OSL_FAIL( "redirection across pages !!!" );
    2294           0 :             m_aOutputStreams.clear(); // leak !
    2295           0 :             m_aMapMode.SetOrigin( Point() );
    2296             :         }
    2297             : 
    2298           0 :         m_aGraphicsStack.clear();
    2299           0 :         m_aGraphicsStack.push_back( GraphicsState() );
    2300             : 
    2301             :         // this should pop the PDF graphics stack if necessary
    2302           0 :         updateGraphicsState();
    2303             : 
    2304           0 :         m_aPages.back().endStream();
    2305             : 
    2306             :         // reset the default font
    2307           0 :         Font aFont;
    2308           0 :         aFont.SetName( OUString( "Times" ) );
    2309           0 :         aFont.SetSize( Size( 0, 12 ) );
    2310             : 
    2311           0 :         m_aCurrentPDFState = m_aGraphicsStack.front();
    2312           0 :         m_aGraphicsStack.front().m_aFont =  aFont;
    2313             : 
    2314           0 :         for( std::list<BitmapEmit>::iterator it = m_aBitmaps.begin();
    2315           0 :              it != m_aBitmaps.end(); ++it )
    2316             :         {
    2317           0 :             if( ! it->m_aBitmap.IsEmpty() )
    2318             :             {
    2319           0 :                 writeBitmapObject( *it );
    2320           0 :                 it->m_aBitmap = BitmapEx();
    2321             :             }
    2322             :         }
    2323           0 :         for( std::list<JPGEmit>::iterator jpeg = m_aJPGs.begin(); jpeg != m_aJPGs.end(); ++jpeg )
    2324             :         {
    2325           0 :             if( jpeg->m_pStream )
    2326             :             {
    2327           0 :                 writeJPG( *jpeg );
    2328           0 :                 delete jpeg->m_pStream;
    2329           0 :                 jpeg->m_pStream = NULL;
    2330           0 :                 jpeg->m_aMask = Bitmap();
    2331             :             }
    2332             :         }
    2333           0 :         for( std::list<TransparencyEmit>::iterator t = m_aTransparentObjects.begin();
    2334           0 :              t != m_aTransparentObjects.end(); ++t )
    2335             :         {
    2336           0 :             if( t->m_pContentStream )
    2337             :             {
    2338           0 :                 writeTransparentObject( *t );
    2339           0 :                 delete t->m_pContentStream;
    2340           0 :                 t->m_pContentStream = NULL;
    2341             :             }
    2342           0 :         }
    2343             :     }
    2344           0 : }
    2345             : 
    2346           0 : sal_Int32 PDFWriterImpl::createObject()
    2347             : {
    2348           0 :     m_aObjects.push_back( ~0U );
    2349           0 :     return m_aObjects.size();
    2350             : }
    2351             : 
    2352           0 : bool PDFWriterImpl::updateObject( sal_Int32 n )
    2353             : {
    2354           0 :     if( ! m_bOpen )
    2355           0 :         return false;
    2356             : 
    2357           0 :     sal_uInt64 nOffset = ~0U;
    2358           0 :     osl::File::RC aError = m_aFile.getPos(nOffset);
    2359             :     DBG_ASSERT( aError == osl::File::E_None, "could not register object" );
    2360           0 :     if (aError != osl::File::E_None)
    2361             :     {
    2362           0 :         m_aFile.close();
    2363           0 :         m_bOpen = false;
    2364             :     }
    2365           0 :     m_aObjects[ n-1 ] = nOffset;
    2366           0 :     return aError == osl::File::E_None;
    2367             : }
    2368             : 
    2369             : #define CHECK_RETURN( x ) if( !(x) ) return 0
    2370             : 
    2371           0 : sal_Int32 PDFWriterImpl::emitStructParentTree( sal_Int32 nObject )
    2372             : {
    2373           0 :     if( nObject > 0 )
    2374             :     {
    2375           0 :         OStringBuffer aLine( 1024 );
    2376             : 
    2377           0 :         aLine.append( nObject );
    2378             :         aLine.append( " 0 obj\n"
    2379           0 :                       "<</Nums[\n" );
    2380           0 :         sal_Int32 nTreeItems = m_aStructParentTree.size();
    2381           0 :         for( sal_Int32 n = 0; n < nTreeItems; n++ )
    2382             :         {
    2383           0 :             aLine.append( n );
    2384           0 :             aLine.append( ' ' );
    2385           0 :             aLine.append( m_aStructParentTree[n] );
    2386           0 :             aLine.append( "\n" );
    2387             :         }
    2388           0 :         aLine.append( "]>>\nendobj\n\n" );
    2389           0 :         CHECK_RETURN( updateObject( nObject ) );
    2390           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2391             :     }
    2392           0 :     return nObject;
    2393             : }
    2394             : 
    2395           0 : const sal_Char* PDFWriterImpl::getAttributeTag( PDFWriter::StructAttribute eAttr )
    2396             : {
    2397           0 :     static std::map< PDFWriter::StructAttribute, const char* > aAttributeStrings;
    2398             :     // fill maps once
    2399           0 :     if( aAttributeStrings.empty() )
    2400             :     {
    2401           0 :         aAttributeStrings[ PDFWriter::Placement ]           = "Placement";
    2402           0 :         aAttributeStrings[ PDFWriter::WritingMode ]         = "WritingMode";
    2403           0 :         aAttributeStrings[ PDFWriter::SpaceBefore ]         = "SpaceBefore";
    2404           0 :         aAttributeStrings[ PDFWriter::SpaceAfter ]          = "SpaceAfter";
    2405           0 :         aAttributeStrings[ PDFWriter::StartIndent ]         = "StartIndent";
    2406           0 :         aAttributeStrings[ PDFWriter::EndIndent ]           = "EndIndent";
    2407           0 :         aAttributeStrings[ PDFWriter::TextIndent ]          = "TextIndent";
    2408           0 :         aAttributeStrings[ PDFWriter::TextAlign ]           = "TextAlign";
    2409           0 :         aAttributeStrings[ PDFWriter::Width ]               = "Width";
    2410           0 :         aAttributeStrings[ PDFWriter::Height ]              = "Height";
    2411           0 :         aAttributeStrings[ PDFWriter::BlockAlign ]          = "BlockAlign";
    2412           0 :         aAttributeStrings[ PDFWriter::InlineAlign ]         = "InlineAlign";
    2413           0 :         aAttributeStrings[ PDFWriter::LineHeight ]          = "LineHeight";
    2414           0 :         aAttributeStrings[ PDFWriter::BaselineShift ]       = "BaselineShift";
    2415           0 :         aAttributeStrings[ PDFWriter::TextDecorationType ]  = "TextDecorationType";
    2416           0 :         aAttributeStrings[ PDFWriter::ListNumbering ]       = "ListNumbering";
    2417           0 :         aAttributeStrings[ PDFWriter::RowSpan ]             = "RowSpan";
    2418           0 :         aAttributeStrings[ PDFWriter::ColSpan ]             = "ColSpan";
    2419           0 :         aAttributeStrings[ PDFWriter::LinkAnnotation ]      = "LinkAnnotation";
    2420             :     }
    2421             : 
    2422             :     std::map< PDFWriter::StructAttribute, const char* >::const_iterator it =
    2423           0 :         aAttributeStrings.find( eAttr );
    2424             : 
    2425             : #if OSL_DEBUG_LEVEL > 1
    2426             :     if( it == aAttributeStrings.end() )
    2427             :         fprintf( stderr, "invalid PDFWriter::StructAttribute %d\n", eAttr );
    2428             : #endif
    2429             : 
    2430           0 :     return it != aAttributeStrings.end() ? it->second : "";
    2431             : }
    2432             : 
    2433           0 : const sal_Char* PDFWriterImpl::getAttributeValueTag( PDFWriter::StructAttributeValue eVal )
    2434             : {
    2435           0 :     static std::map< PDFWriter::StructAttributeValue, const char* > aValueStrings;
    2436             : 
    2437           0 :     if( aValueStrings.empty() )
    2438             :     {
    2439           0 :         aValueStrings[ PDFWriter::NONE ]                    = "None";
    2440           0 :         aValueStrings[ PDFWriter::Block ]                   = "Block";
    2441           0 :         aValueStrings[ PDFWriter::Inline ]                  = "Inline";
    2442           0 :         aValueStrings[ PDFWriter::Before ]                  = "Before";
    2443           0 :         aValueStrings[ PDFWriter::After ]                   = "After";
    2444           0 :         aValueStrings[ PDFWriter::Start ]                   = "Start";
    2445           0 :         aValueStrings[ PDFWriter::End ]                     = "End";
    2446           0 :         aValueStrings[ PDFWriter::LrTb ]                    = "LrTb";
    2447           0 :         aValueStrings[ PDFWriter::RlTb ]                    = "RlTb";
    2448           0 :         aValueStrings[ PDFWriter::TbRl ]                    = "TbRl";
    2449           0 :         aValueStrings[ PDFWriter::Center ]                  = "Center";
    2450           0 :         aValueStrings[ PDFWriter::Justify ]                 = "Justify";
    2451           0 :         aValueStrings[ PDFWriter::Auto ]                    = "Auto";
    2452           0 :         aValueStrings[ PDFWriter::Middle ]                  = "Middle";
    2453           0 :         aValueStrings[ PDFWriter::Normal ]                  = "Normal";
    2454           0 :         aValueStrings[ PDFWriter::Underline ]               = "Underline";
    2455           0 :         aValueStrings[ PDFWriter::Overline ]                = "Overline";
    2456           0 :         aValueStrings[ PDFWriter::LineThrough ]             = "LineThrough";
    2457           0 :         aValueStrings[ PDFWriter::Disc ]                    = "Disc";
    2458           0 :         aValueStrings[ PDFWriter::Circle ]                  = "Circle";
    2459           0 :         aValueStrings[ PDFWriter::Square ]                  = "Square";
    2460           0 :         aValueStrings[ PDFWriter::Decimal ]                 = "Decimal";
    2461           0 :         aValueStrings[ PDFWriter::UpperRoman ]              = "UpperRoman";
    2462           0 :         aValueStrings[ PDFWriter::LowerRoman ]              = "LowerRoman";
    2463           0 :         aValueStrings[ PDFWriter::UpperAlpha ]              = "UpperAlpha";
    2464           0 :         aValueStrings[ PDFWriter::LowerAlpha ]              = "LowerAlpha";
    2465             :     }
    2466             : 
    2467             :     std::map< PDFWriter::StructAttributeValue, const char* >::const_iterator it =
    2468           0 :         aValueStrings.find( eVal );
    2469             : 
    2470             : #if OSL_DEBUG_LEVEL > 1
    2471             :     if( it == aValueStrings.end() )
    2472             :         fprintf( stderr, "invalid PDFWriter::StructAttributeValue %d\n", eVal );
    2473             : #endif
    2474             : 
    2475           0 :     return it != aValueStrings.end() ? it->second : "";
    2476             : }
    2477             : 
    2478           0 : static void appendStructureAttributeLine( PDFWriter::StructAttribute i_eAttr, const PDFWriterImpl::PDFStructureAttribute& i_rVal, OStringBuffer& o_rLine, bool i_bIsFixedInt )
    2479             : {
    2480           0 :     o_rLine.append( "/" );
    2481           0 :     o_rLine.append( PDFWriterImpl::getAttributeTag( i_eAttr ) );
    2482             : 
    2483           0 :     if( i_rVal.eValue != PDFWriter::Invalid )
    2484             :     {
    2485           0 :         o_rLine.append( "/" );
    2486           0 :         o_rLine.append( PDFWriterImpl::getAttributeValueTag( i_rVal.eValue ) );
    2487             :     }
    2488             :     else
    2489             :     {
    2490             :         // numerical value
    2491           0 :         o_rLine.append( " " );
    2492           0 :         if( i_bIsFixedInt )
    2493           0 :             appendFixedInt( i_rVal.nValue, o_rLine );
    2494             :         else
    2495           0 :             o_rLine.append( i_rVal.nValue );
    2496             :     }
    2497           0 :     o_rLine.append( "\n" );
    2498           0 : }
    2499             : 
    2500           0 : OString PDFWriterImpl::emitStructureAttributes( PDFStructureElement& i_rEle )
    2501             : {
    2502             :     // create layout, list and table attribute sets
    2503           0 :     OStringBuffer aLayout(256), aList(64), aTable(64);
    2504           0 :     for( PDFStructAttributes::const_iterator it = i_rEle.m_aAttributes.begin();
    2505           0 :          it != i_rEle.m_aAttributes.end(); ++it )
    2506             :     {
    2507           0 :         if( it->first == PDFWriter::ListNumbering )
    2508           0 :             appendStructureAttributeLine( it->first, it->second, aList, true );
    2509           0 :         else if( it->first == PDFWriter::RowSpan ||
    2510           0 :                  it->first == PDFWriter::ColSpan )
    2511           0 :             appendStructureAttributeLine( it->first, it->second, aTable, false );
    2512           0 :         else if( it->first == PDFWriter::LinkAnnotation )
    2513             :         {
    2514           0 :             sal_Int32 nLink = it->second.nValue;
    2515             :             std::map< sal_Int32, sal_Int32 >::const_iterator link_it =
    2516           0 :                 m_aLinkPropertyMap.find( nLink );
    2517           0 :             if( link_it != m_aLinkPropertyMap.end() )
    2518           0 :                 nLink = link_it->second;
    2519           0 :             if( nLink >= 0 && nLink < (sal_Int32)m_aLinks.size() )
    2520             :             {
    2521             :                 // update struct parent of link
    2522           0 :                 OStringBuffer aStructParentEntry( 32 );
    2523           0 :                 aStructParentEntry.append( i_rEle.m_nObject );
    2524           0 :                 aStructParentEntry.append( " 0 R" );
    2525           0 :                 m_aStructParentTree.push_back( aStructParentEntry.makeStringAndClear() );
    2526           0 :                 m_aLinks[ nLink ].m_nStructParent = m_aStructParentTree.size()-1;
    2527             : 
    2528           0 :                 sal_Int32 nRefObject = createObject();
    2529           0 :                 OStringBuffer aRef( 256 );
    2530           0 :                 aRef.append( nRefObject );
    2531             :                 aRef.append( " 0 obj\n"
    2532           0 :                              "<</Type/OBJR/Obj " );
    2533           0 :                 aRef.append( m_aLinks[ nLink ].m_nObject );
    2534             :                 aRef.append( " 0 R>>\n"
    2535             :                              "endobj\n\n"
    2536           0 :                              );
    2537           0 :                 if (updateObject(nRefObject))
    2538             :                 {
    2539           0 :                     writeBuffer( aRef.getStr(), aRef.getLength() );
    2540             :                 }
    2541             : 
    2542           0 :                 i_rEle.m_aKids.push_back( PDFStructureElementKid( nRefObject ) );
    2543             :             }
    2544             :             else
    2545             :             {
    2546             :                 OSL_FAIL( "unresolved link id for Link structure" );
    2547             : #if OSL_DEBUG_LEVEL > 1
    2548             :                 fprintf( stderr, "unresolved link id %" SAL_PRIdINT32 " for Link structure\n", nLink );
    2549             :                 {
    2550             :                     OStringBuffer aLine( "unresolved link id " );
    2551             :                     aLine.append( nLink );
    2552             :                     aLine.append( " for Link structure" );
    2553             :                     emitComment( aLine.getStr() );
    2554             :                 }
    2555             : #endif
    2556             :             }
    2557             :         }
    2558             :         else
    2559           0 :             appendStructureAttributeLine( it->first, it->second, aLayout, true );
    2560             :     }
    2561           0 :     if( ! i_rEle.m_aBBox.IsEmpty() )
    2562             :     {
    2563           0 :         aLayout.append( "/BBox[" );
    2564           0 :         appendFixedInt( i_rEle.m_aBBox.Left(), aLayout );
    2565           0 :         aLayout.append( " " );
    2566           0 :         appendFixedInt( i_rEle.m_aBBox.Top(), aLayout );
    2567           0 :         aLayout.append( " " );
    2568           0 :         appendFixedInt( i_rEle.m_aBBox.Right(), aLayout );
    2569           0 :         aLayout.append( " " );
    2570           0 :         appendFixedInt( i_rEle.m_aBBox.Bottom(), aLayout );
    2571           0 :         aLayout.append( "]\n" );
    2572             :     }
    2573             : 
    2574           0 :     std::vector< sal_Int32 > aAttribObjects;
    2575           0 :     if( !aLayout.isEmpty() )
    2576             :     {
    2577           0 :         aAttribObjects.push_back( createObject() );
    2578           0 :         if (updateObject( aAttribObjects.back() ))
    2579             :         {
    2580           0 :             OStringBuffer aObj( 64 );
    2581           0 :             aObj.append( aAttribObjects.back() );
    2582             :             aObj.append( " 0 obj\n"
    2583           0 :                          "<</O/Layout\n" );
    2584           0 :             aLayout.append( ">>\nendobj\n\n" );
    2585           0 :             writeBuffer( aObj.getStr(), aObj.getLength() );
    2586           0 :             writeBuffer( aLayout.getStr(), aLayout.getLength() );
    2587             :         }
    2588             :     }
    2589           0 :     if( !aList.isEmpty() )
    2590             :     {
    2591           0 :         aAttribObjects.push_back( createObject() );
    2592           0 :         if (updateObject( aAttribObjects.back() ))
    2593             :         {
    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             :     }
    2603           0 :     if( !aTable.isEmpty() )
    2604             :     {
    2605           0 :         aAttribObjects.push_back( createObject() );
    2606           0 :         if (updateObject( aAttribObjects.back() ))
    2607             :         {
    2608           0 :             OStringBuffer aObj( 64 );
    2609           0 :             aObj.append( aAttribObjects.back() );
    2610             :             aObj.append( " 0 obj\n"
    2611           0 :                          "<</O/Table\n" );
    2612           0 :             aTable.append( ">>\nendobj\n\n" );
    2613           0 :             writeBuffer( aObj.getStr(), aObj.getLength() );
    2614           0 :             writeBuffer( aTable.getStr(), aTable.getLength() );
    2615             :         }
    2616             :     }
    2617             : 
    2618           0 :     OStringBuffer aRet( 64 );
    2619           0 :     if( aAttribObjects.size() > 1 )
    2620           0 :         aRet.append( " [" );
    2621           0 :     for( std::vector< sal_Int32 >::const_iterator at_it = aAttribObjects.begin();
    2622           0 :          at_it != aAttribObjects.end(); ++at_it )
    2623             :     {
    2624           0 :         aRet.append( " " );
    2625           0 :         aRet.append( *at_it );
    2626           0 :         aRet.append( " 0 R" );
    2627             :     }
    2628           0 :     if( aAttribObjects.size() > 1 )
    2629           0 :         aRet.append( " ]" );
    2630           0 :     return aRet.makeStringAndClear();
    2631             : }
    2632             : 
    2633           0 : sal_Int32 PDFWriterImpl::emitStructure( PDFStructureElement& rEle )
    2634             : {
    2635           0 :     if(
    2636             :        // do not emit NonStruct and its children
    2637           0 :        rEle.m_eType == PDFWriter::NonStructElement &&
    2638           0 :        rEle.m_nOwnElement != rEle.m_nParentElement // but of course emit the struct tree root
    2639             :        )
    2640           0 :         return 0;
    2641             : 
    2642           0 :     for( std::list< sal_Int32 >::const_iterator it = rEle.m_aChildren.begin(); it != rEle.m_aChildren.end(); ++it )
    2643             :     {
    2644           0 :         if( *it > 0 && *it < sal_Int32(m_aStructure.size()) )
    2645             :         {
    2646           0 :             PDFStructureElement& rChild = m_aStructure[ *it ];
    2647           0 :             if( rChild.m_eType != PDFWriter::NonStructElement )
    2648             :             {
    2649           0 :                 if( rChild.m_nParentElement == rEle.m_nOwnElement )
    2650           0 :                     emitStructure( rChild );
    2651             :                 else
    2652             :                 {
    2653             :                     OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure element" );
    2654             : #if OSL_DEBUG_LEVEL > 1
    2655             :                     fprintf( stderr, "PDFWriterImpl::emitStructure: invalid child structure elemnt with id %" SAL_PRIdINT32 "\n", *it );
    2656             : #endif
    2657             :                 }
    2658             :             }
    2659             :         }
    2660             :         else
    2661             :         {
    2662             :             OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure id" );
    2663             : #if OSL_DEBUG_LEVEL > 1
    2664             :             fprintf( stderr, "PDFWriterImpl::emitStructure: invalid child structure id %" SAL_PRIdINT32 "\n", *it );
    2665             : #endif
    2666             :         }
    2667             :     }
    2668             : 
    2669           0 :     OStringBuffer aLine( 512 );
    2670           0 :     aLine.append( rEle.m_nObject );
    2671             :     aLine.append( " 0 obj\n"
    2672           0 :                   "<</Type" );
    2673           0 :     sal_Int32 nParentTree = -1;
    2674           0 :     if( rEle.m_nOwnElement == rEle.m_nParentElement )
    2675             :     {
    2676           0 :         nParentTree = createObject();
    2677           0 :         CHECK_RETURN( nParentTree );
    2678           0 :         aLine.append( "/StructTreeRoot\n" );
    2679           0 :         aLine.append( "/ParentTree " );
    2680           0 :         aLine.append( nParentTree );
    2681           0 :         aLine.append( " 0 R\n" );
    2682           0 :         if( ! m_aRoleMap.empty() )
    2683             :         {
    2684           0 :             aLine.append( "/RoleMap<<" );
    2685           0 :             for( boost::unordered_map<OString,OString,OStringHash>::const_iterator
    2686           0 :                  it = m_aRoleMap.begin(); it != m_aRoleMap.end(); ++it )
    2687             :             {
    2688           0 :                 aLine.append( '/' );
    2689           0 :                 aLine.append(it->first);
    2690           0 :                 aLine.append( '/' );
    2691           0 :                 aLine.append( it->second );
    2692           0 :                 aLine.append( '\n' );
    2693             :             }
    2694           0 :             aLine.append( ">>\n" );
    2695             :         }
    2696             :     }
    2697             :     else
    2698             :     {
    2699             :         aLine.append( "/StructElem\n"
    2700           0 :                       "/S/" );
    2701           0 :         if( !rEle.m_aAlias.isEmpty() )
    2702           0 :             aLine.append( rEle.m_aAlias );
    2703             :         else
    2704           0 :             aLine.append( getStructureTag( rEle.m_eType ) );
    2705             :         aLine.append( "\n"
    2706           0 :                       "/P " );
    2707           0 :         aLine.append( m_aStructure[ rEle.m_nParentElement ].m_nObject );
    2708             :         aLine.append( " 0 R\n"
    2709           0 :                       "/Pg " );
    2710           0 :         aLine.append( rEle.m_nFirstPageObject );
    2711           0 :         aLine.append( " 0 R\n" );
    2712           0 :         if( !rEle.m_aActualText.isEmpty() )
    2713             :         {
    2714           0 :             aLine.append( "/ActualText" );
    2715           0 :             appendUnicodeTextStringEncrypt( rEle.m_aActualText, rEle.m_nObject, aLine );
    2716           0 :             aLine.append( "\n" );
    2717             :         }
    2718           0 :         if( !rEle.m_aAltText.isEmpty() )
    2719             :         {
    2720           0 :             aLine.append( "/Alt" );
    2721           0 :             appendUnicodeTextStringEncrypt( rEle.m_aAltText, rEle.m_nObject, aLine );
    2722           0 :             aLine.append( "\n" );
    2723             :         }
    2724             :     }
    2725           0 :     if( (! rEle.m_aBBox.IsEmpty()) || (! rEle.m_aAttributes.empty()) )
    2726             :     {
    2727           0 :         OString aAttribs =  emitStructureAttributes( rEle );
    2728           0 :         if( !aAttribs.isEmpty() )
    2729             :         {
    2730           0 :             aLine.append( "/A" );
    2731           0 :             aLine.append( aAttribs );
    2732           0 :             aLine.append( "\n" );
    2733           0 :         }
    2734             :     }
    2735           0 :     if( !rEle.m_aLocale.Language.isEmpty() )
    2736             :     {
    2737             :         /* PDF allows only RFC 3066, which is only partly BCP 47 and does not
    2738             :          * include script tags and others.
    2739             :          * http://pdf.editme.com/pdfua-naturalLanguageSpecification
    2740             :          * http://partners.adobe.com/public/developer/en/pdf/PDFReference16.pdf#page=886
    2741             :          * https://www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf#M13.9.19332.1Heading.97.Natural.Language.Specification
    2742             :          * */
    2743           0 :         LanguageTag aLanguageTag( rEle.m_aLocale);
    2744           0 :         OUString aLanguage, aScript, aCountry;
    2745           0 :         aLanguageTag.getIsoLanguageScriptCountry( aLanguage, aScript, aCountry);
    2746           0 :         if (!aLanguage.isEmpty())
    2747             :         {
    2748           0 :             OUStringBuffer aLocBuf( 16 );
    2749           0 :             aLocBuf.append( aLanguage );
    2750           0 :             if( !aCountry.isEmpty() )
    2751             :             {
    2752           0 :                 aLocBuf.append( '-' );
    2753           0 :                 aLocBuf.append( aCountry );
    2754             :             }
    2755           0 :             aLine.append( "/Lang" );
    2756           0 :             appendLiteralStringEncrypt( aLocBuf.makeStringAndClear(), rEle.m_nObject, aLine );
    2757           0 :             aLine.append( "\n" );
    2758           0 :         }
    2759             :     }
    2760           0 :     if( ! rEle.m_aKids.empty() )
    2761             :     {
    2762           0 :         unsigned int i = 0;
    2763           0 :         aLine.append( "/K[" );
    2764           0 :         for( std::list< PDFStructureElementKid >::const_iterator it =
    2765           0 :                  rEle.m_aKids.begin(); it != rEle.m_aKids.end(); ++it, i++ )
    2766             :         {
    2767           0 :             if( it->nMCID == -1 )
    2768             :             {
    2769           0 :                 aLine.append( it->nObject );
    2770           0 :                 aLine.append( " 0 R" );
    2771           0 :                 aLine.append( ( (i & 15) == 15 ) ? "\n" : " " );
    2772             :             }
    2773             :             else
    2774             :             {
    2775           0 :                 if( it->nObject == rEle.m_nFirstPageObject )
    2776             :                 {
    2777           0 :                     aLine.append( it->nMCID );
    2778           0 :                     aLine.append( " " );
    2779             :                 }
    2780             :                 else
    2781             :                 {
    2782           0 :                     aLine.append( "<</Type/MCR/Pg " );
    2783           0 :                     aLine.append( it->nObject );
    2784           0 :                     aLine.append( " 0 R /MCID " );
    2785           0 :                     aLine.append( it->nMCID );
    2786           0 :                     aLine.append( ">>\n" );
    2787             :                 }
    2788             :             }
    2789             :         }
    2790           0 :         aLine.append( "]\n" );
    2791             :     }
    2792           0 :     aLine.append( ">>\nendobj\n\n" );
    2793             : 
    2794           0 :     CHECK_RETURN( updateObject( rEle.m_nObject ) );
    2795           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2796             : 
    2797           0 :     CHECK_RETURN( emitStructParentTree( nParentTree ) );
    2798             : 
    2799           0 :     return rEle.m_nObject;
    2800             : }
    2801             : 
    2802           0 : bool PDFWriterImpl::emitGradients()
    2803             : {
    2804           0 :     for( std::list<GradientEmit>::iterator it = m_aGradients.begin();
    2805           0 :          it != m_aGradients.end(); ++it )
    2806             :     {
    2807           0 :         if ( !writeGradientFunction( *it ) ) return false;
    2808             :     }
    2809           0 :     return true;
    2810             : }
    2811             : 
    2812           0 : bool PDFWriterImpl::emitTilings()
    2813             : {
    2814           0 :     OStringBuffer aTilingObj( 1024 );
    2815             : 
    2816           0 :     for( std::vector<TilingEmit>::iterator it = m_aTilings.begin(); it != m_aTilings.end(); ++it )
    2817             :     {
    2818             :         DBG_ASSERT( it->m_pTilingStream, "tiling without stream" );
    2819           0 :         if( ! it->m_pTilingStream )
    2820           0 :             continue;
    2821             : 
    2822           0 :         aTilingObj.setLength( 0 );
    2823             : 
    2824             :         #if OSL_DEBUG_LEVEL > 1
    2825             :         emitComment( "PDFWriterImpl::emitTilings" );
    2826             :         #endif
    2827             : 
    2828           0 :         sal_Int32 nX = (sal_Int32)it->m_aRectangle.Left();
    2829           0 :         sal_Int32 nY = (sal_Int32)it->m_aRectangle.Top();
    2830           0 :         sal_Int32 nW = (sal_Int32)it->m_aRectangle.GetWidth();
    2831           0 :         sal_Int32 nH = (sal_Int32)it->m_aRectangle.GetHeight();
    2832           0 :         if( it->m_aCellSize.Width() == 0 )
    2833           0 :             it->m_aCellSize.Width() = nW;
    2834           0 :         if( it->m_aCellSize.Height() == 0 )
    2835           0 :             it->m_aCellSize.Height() = nH;
    2836             : 
    2837           0 :         bool bDeflate = compressStream( it->m_pTilingStream );
    2838           0 :         it->m_pTilingStream->Seek( STREAM_SEEK_TO_END );
    2839           0 :         sal_Size nTilingStreamSize = it->m_pTilingStream->Tell();
    2840           0 :         it->m_pTilingStream->Seek( STREAM_SEEK_TO_BEGIN );
    2841             : 
    2842             :         // write pattern object
    2843           0 :         aTilingObj.append( it->m_nObject );
    2844           0 :         aTilingObj.append( " 0 obj\n" );
    2845             :         aTilingObj.append( "<</Type/Pattern/PatternType 1\n"
    2846             :                            "/PaintType 1\n"
    2847             :                            "/TilingType 2\n"
    2848           0 :                            "/BBox[" );
    2849           0 :         appendFixedInt( nX, aTilingObj );
    2850           0 :         aTilingObj.append( ' ' );
    2851           0 :         appendFixedInt( nY, aTilingObj );
    2852           0 :         aTilingObj.append( ' ' );
    2853           0 :         appendFixedInt( nX+nW, aTilingObj );
    2854           0 :         aTilingObj.append( ' ' );
    2855           0 :         appendFixedInt( nY+nH, aTilingObj );
    2856             :         aTilingObj.append( "]\n"
    2857           0 :                            "/XStep " );
    2858           0 :         appendFixedInt( it->m_aCellSize.Width(), aTilingObj );
    2859             :         aTilingObj.append( "\n"
    2860           0 :                            "/YStep " );
    2861           0 :         appendFixedInt( it->m_aCellSize.Height(), aTilingObj );
    2862           0 :         aTilingObj.append( "\n" );
    2863           0 :         if( it->m_aTransform.matrix[0] != 1.0 ||
    2864           0 :             it->m_aTransform.matrix[1] != 0.0 ||
    2865           0 :             it->m_aTransform.matrix[3] != 0.0 ||
    2866           0 :             it->m_aTransform.matrix[4] != 1.0 ||
    2867           0 :             it->m_aTransform.matrix[2] != 0.0 ||
    2868           0 :             it->m_aTransform.matrix[5] != 0.0 )
    2869             :         {
    2870           0 :             aTilingObj.append( "/Matrix [" );
    2871             :             // TODO: scaling, mirroring on y, etc
    2872           0 :             appendDouble( it->m_aTransform.matrix[0], aTilingObj );
    2873           0 :             aTilingObj.append( ' ' );
    2874           0 :             appendDouble( it->m_aTransform.matrix[1], aTilingObj );
    2875           0 :             aTilingObj.append( ' ' );
    2876           0 :             appendDouble( it->m_aTransform.matrix[3], aTilingObj );
    2877           0 :             aTilingObj.append( ' ' );
    2878           0 :             appendDouble( it->m_aTransform.matrix[4], aTilingObj );
    2879           0 :             aTilingObj.append( ' ' );
    2880           0 :             appendDouble( it->m_aTransform.matrix[2], aTilingObj );
    2881           0 :             aTilingObj.append( ' ' );
    2882           0 :             appendDouble( it->m_aTransform.matrix[5], aTilingObj );
    2883           0 :             aTilingObj.append( "]\n" );
    2884             :         }
    2885           0 :         aTilingObj.append( "/Resources" );
    2886           0 :         it->m_aResources.append( aTilingObj, getFontDictObject() );
    2887           0 :         if( bDeflate )
    2888           0 :             aTilingObj.append( "/Filter/FlateDecode" );
    2889           0 :         aTilingObj.append( "/Length " );
    2890           0 :         aTilingObj.append( (sal_Int32)nTilingStreamSize );
    2891           0 :         aTilingObj.append( ">>\nstream\n" );
    2892           0 :         if ( !updateObject( it->m_nObject ) ) return false;
    2893           0 :         if ( !writeBuffer( aTilingObj.getStr(), aTilingObj.getLength() ) ) return false;
    2894           0 :         checkAndEnableStreamEncryption( it->m_nObject );
    2895           0 :         bool written = writeBuffer( it->m_pTilingStream->GetData(), nTilingStreamSize );
    2896           0 :         delete it->m_pTilingStream;
    2897           0 :         it->m_pTilingStream = NULL;
    2898           0 :         if( !written )
    2899           0 :             return false;
    2900           0 :         disableStreamEncryption();
    2901           0 :         aTilingObj.setLength( 0 );
    2902           0 :         aTilingObj.append( "\nendstream\nendobj\n\n" );
    2903           0 :         if ( !writeBuffer( aTilingObj.getStr(), aTilingObj.getLength() ) ) return false;
    2904             :     }
    2905           0 :     return true;
    2906             : }
    2907             : 
    2908           0 : sal_Int32 PDFWriterImpl::emitBuiltinFont( const PhysicalFontFace* pFont, sal_Int32 nFontObject )
    2909             : {
    2910           0 :     const ImplPdfBuiltinFontData* pFD = GetPdfFontData( pFont );
    2911           0 :     if( !pFD )
    2912           0 :         return 0;
    2913           0 :     const BuiltinFont& rBuiltinFont = pFD->GetBuiltinFont();
    2914             : 
    2915           0 :     OStringBuffer aLine( 1024 );
    2916             : 
    2917           0 :     if( nFontObject <= 0 )
    2918           0 :         nFontObject = createObject();
    2919           0 :     CHECK_RETURN( updateObject( nFontObject ) );
    2920           0 :     aLine.append( nFontObject );
    2921             :     aLine.append( " 0 obj\n"
    2922           0 :                   "<</Type/Font/Subtype/Type1/BaseFont/" );
    2923           0 :     appendName( rBuiltinFont.m_pPSName, aLine );
    2924           0 :     aLine.append( "\n" );
    2925           0 :     if( rBuiltinFont.m_eCharSet == RTL_TEXTENCODING_MS_1252 )
    2926           0 :          aLine.append( "/Encoding/WinAnsiEncoding\n" );
    2927           0 :     aLine.append( ">>\nendobj\n\n" );
    2928           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    2929           0 :     return nFontObject;
    2930             : }
    2931             : 
    2932           0 : std::map< sal_Int32, sal_Int32 > PDFWriterImpl::emitSystemFont( const PhysicalFontFace* pFont, EmbedFont& rEmbed )
    2933             : {
    2934           0 :     std::map< sal_Int32, sal_Int32 > aRet;
    2935             : 
    2936           0 :     sal_Int32 nFontDescriptor = 0;
    2937           0 :     OString aSubType( "/Type1" );
    2938           0 :     FontSubsetInfo aInfo;
    2939             :     // fill in dummy values
    2940           0 :     aInfo.m_nAscent = 1000;
    2941           0 :     aInfo.m_nDescent = 200;
    2942           0 :     aInfo.m_nCapHeight = 1000;
    2943           0 :     aInfo.m_aFontBBox = Rectangle( Point( -200, -200 ), Size( 1700, 1700 ) );
    2944           0 :     aInfo.m_aPSName = pFont->GetFamilyName();
    2945             :     sal_Int32 pWidths[256];
    2946           0 :     memset( pWidths, 0, sizeof(pWidths) );
    2947           0 :     if( pFont->IsEmbeddable() )
    2948             :     {
    2949           0 :         const unsigned char* pFontData = NULL;
    2950           0 :         long nFontLen = 0;
    2951             :         sal_Ucs nEncodedCodes[256];
    2952             :         sal_Int32 pEncWidths[256];
    2953           0 :         if( (pFontData = (const unsigned char*)m_pReferenceDevice->mpGraphics->GetEmbedFontData( pFont, nEncodedCodes, pEncWidths, aInfo, &nFontLen )) != NULL )
    2954             :         {
    2955           0 :             m_pReferenceDevice->mpGraphics->FreeEmbedFontData( pFontData, nFontLen );
    2956           0 :             for( int i = 0; i < 256; i++ )
    2957             :             {
    2958           0 :                 if( nEncodedCodes[i] >= 32 && nEncodedCodes[i] < 256 )
    2959             :                 {
    2960           0 :                     pWidths[i] = pEncWidths[ i ];
    2961             :                 }
    2962             :             }
    2963             :         }
    2964             :     }
    2965           0 :     else if( pFont->mbSubsettable )
    2966             :     {
    2967           0 :         aSubType = OString( "/TrueType" );
    2968           0 :         Int32Vector aGlyphWidths;
    2969           0 :         Ucs2UIntMap aUnicodeMap;
    2970           0 :         m_pReferenceDevice->mpGraphics->GetGlyphWidths( pFont, false, aGlyphWidths, aUnicodeMap );
    2971             : 
    2972           0 :         OUString aTmpName;
    2973           0 :         osl_createTempFile( NULL, NULL, &aTmpName.pData );
    2974             :         sal_GlyphId aGlyphIds[ 256 ];
    2975             :         sal_uInt8 pEncoding[ 256 ];
    2976             :         sal_Int32 pDuWidths[ 256 ];
    2977             : 
    2978           0 :         memset( aGlyphIds, 0, sizeof( aGlyphIds ) );
    2979           0 :         memset( pEncoding, 0, sizeof( pEncoding ) );
    2980           0 :         memset( pDuWidths, 0, sizeof( pDuWidths ) );
    2981             : 
    2982           0 :         for( sal_Ucs c = 32; c < 256; c++ )
    2983             :         {
    2984           0 :             pEncoding[c] = c;
    2985           0 :             aGlyphIds[c] = 0;
    2986           0 :             if( aUnicodeMap.find( c ) != aUnicodeMap.end() )
    2987           0 :                 pWidths[ c ] = aGlyphWidths[ aUnicodeMap[ c ] ];
    2988             :         }
    2989             : 
    2990           0 :         m_pReferenceDevice->mpGraphics->CreateFontSubset( aTmpName, pFont, aGlyphIds, pEncoding, pDuWidths, 256, aInfo );
    2991           0 :         osl_removeFile( aTmpName.pData );
    2992             :     }
    2993             :     else
    2994             :     {
    2995             :         OSL_FAIL( "system font neither embeddable nor subsettable" );
    2996             :     }
    2997             : 
    2998             :     // write font descriptor
    2999           0 :     nFontDescriptor = emitFontDescriptor( pFont, aInfo, 0, 0 );
    3000           0 :     if( nFontDescriptor )
    3001             :     {
    3002             :         // write font object
    3003           0 :         sal_Int32 nObject = createObject();
    3004           0 :         if( updateObject( nObject ) )
    3005             :         {
    3006           0 :             OStringBuffer aLine( 1024 );
    3007           0 :             aLine.append( nObject );
    3008             :             aLine.append( " 0 obj\n"
    3009           0 :                           "<</Type/Font/Subtype" );
    3010           0 :             aLine.append( aSubType );
    3011           0 :             aLine.append( "/BaseFont/" );
    3012           0 :             appendName( aInfo.m_aPSName, aLine );
    3013           0 :             aLine.append( "\n" );
    3014           0 :             if( !pFont->IsSymbolFont() )
    3015           0 :                 aLine.append( "/Encoding/WinAnsiEncoding\n" );
    3016             :             aLine.append( "/FirstChar 32 /LastChar 255\n"
    3017           0 :                           "/Widths[" );
    3018           0 :             for( int i = 32; i < 256; i++ )
    3019             :             {
    3020           0 :                 aLine.append( pWidths[i] );
    3021           0 :                 aLine.append( ((i&15) == 15) ? "\n" : " " );
    3022             :             }
    3023             :             aLine.append( "]\n"
    3024           0 :                           "/FontDescriptor " );
    3025           0 :             aLine.append( nFontDescriptor );
    3026             :             aLine.append( " 0 R>>\n"
    3027           0 :                           "endobj\n\n" );
    3028           0 :             writeBuffer( aLine.getStr(), aLine.getLength() );
    3029             : 
    3030           0 :             aRet[ rEmbed.m_nNormalFontID ] = nObject;
    3031             :         }
    3032             :     }
    3033             : 
    3034           0 :     return aRet;
    3035             : }
    3036             : 
    3037             : typedef int ThreeInts[3];
    3038           0 : static bool getPfbSegmentLengths( const unsigned char* pFontBytes, int nByteLen,
    3039             :     ThreeInts& rSegmentLengths )
    3040             : {
    3041           0 :     if( !pFontBytes || (nByteLen < 0) )
    3042           0 :         return false;
    3043           0 :     const unsigned char* pPtr = pFontBytes;
    3044           0 :     const unsigned char* pEnd = pFontBytes + nByteLen;
    3045             : 
    3046           0 :     for( int i = 0; i < 3; ++i) {
    3047             :         // read segment1 header
    3048           0 :         if( pPtr+6 >= pEnd )
    3049           0 :             return false;
    3050           0 :         if( (pPtr[0] != 0x80) || (pPtr[1] >= 0x03) )
    3051           0 :             return false;
    3052           0 :         const int nLen = (pPtr[5]<<24) + (pPtr[4]<<16) + (pPtr[3]<<8) + pPtr[2];
    3053           0 :         if( nLen <= 0)
    3054           0 :             return false;
    3055           0 :         rSegmentLengths[i] = nLen;
    3056           0 :         pPtr += nLen + 6;
    3057             :     }
    3058             : 
    3059             :     // read segment-end header
    3060           0 :     if( pPtr+2 >= pEnd )
    3061           0 :         return false;
    3062           0 :     if( (pPtr[0] != 0x80) || (pPtr[1] != 0x03) )
    3063           0 :         return false;
    3064             : 
    3065           0 :     return true;
    3066             : }
    3067             : 
    3068           0 : struct FontException : public std::exception
    3069             : {
    3070             : };
    3071             : 
    3072             : // TODO: always subset instead of embedding the full font => this method becomes obsolete then
    3073           0 : std::map< sal_Int32, sal_Int32 > PDFWriterImpl::emitEmbeddedFont( const PhysicalFontFace* pFont, EmbedFont& rEmbed )
    3074             : {
    3075           0 :     std::map< sal_Int32, sal_Int32 > aRet;
    3076             : 
    3077           0 :     sal_Int32 nStreamObject = 0;
    3078           0 :     sal_Int32 nFontDescriptor = 0;
    3079             : 
    3080             :     // prepare font encoding
    3081           0 :     const Ucs2SIntMap* pEncoding = m_pReferenceDevice->mpGraphics->GetFontEncodingVector( pFont, NULL );
    3082           0 :     sal_Int32 nToUnicodeStream = 0;
    3083             :     sal_uInt8 nEncoding[256];
    3084             :     sal_Ucs nEncodedCodes[256];
    3085           0 :     std::vector<sal_Ucs> aUnicodes;
    3086           0 :     aUnicodes.reserve( 256 );
    3087             :     sal_Int32 pUnicodesPerGlyph[256];
    3088             :     sal_Int32 pEncToUnicodeIndex[256];
    3089           0 :     if( pEncoding )
    3090             :     {
    3091           0 :         memset( nEncoding, 0, sizeof(nEncoding) );
    3092           0 :         memset( nEncodedCodes, 0, sizeof(nEncodedCodes) );
    3093           0 :         memset( pUnicodesPerGlyph, 0, sizeof(pUnicodesPerGlyph) );
    3094           0 :         memset( pEncToUnicodeIndex, 0, sizeof(pEncToUnicodeIndex) );
    3095           0 :         for( Ucs2SIntMap::const_iterator it = pEncoding->begin(); it != pEncoding->end(); ++it )
    3096             :         {
    3097           0 :             if( it->second != -1 )
    3098             :             {
    3099           0 :                 sal_Int32 nCode = (sal_Int32)(it->second & 0x000000ff);
    3100           0 :                 nEncoding[ nCode ] = static_cast<sal_uInt8>( nCode );
    3101           0 :                 nEncodedCodes[ nCode ] = it->first;
    3102           0 :                 pEncToUnicodeIndex[ nCode ] = static_cast<sal_Int32>(aUnicodes.size());
    3103           0 :                 aUnicodes.push_back( it->first );
    3104           0 :                 pUnicodesPerGlyph[ nCode ] = 1;
    3105             :             }
    3106             :         }
    3107             :     }
    3108             : 
    3109           0 :     FontSubsetInfo aInfo;
    3110             :     sal_Int32 pWidths[256];
    3111           0 :     const unsigned char* pFontData = NULL;
    3112           0 :     long nFontLen = 0;
    3113             :     sal_Int32 nLength1, nLength2;
    3114             :     try
    3115             :     {
    3116           0 :         if( (pFontData = (const unsigned char*)m_pReferenceDevice->mpGraphics->GetEmbedFontData( pFont, nEncodedCodes, pWidths, aInfo, &nFontLen )) != NULL )
    3117             :         {
    3118           0 :             if( (aInfo.m_nFontType & FontSubsetInfo::ANY_TYPE1) == 0 )
    3119           0 :                 throw FontException();
    3120             :             // see whether it is pfb or pfa; if it is a pfb, fill ranges
    3121             :             // of 6 bytes that are not part of the font program
    3122           0 :             std::list< int > aSections;
    3123           0 :             std::list< int >::const_iterator it;
    3124           0 :             int nIndex = 0;
    3125           0 :             while( (nIndex < nFontLen-1) && pFontData[nIndex] == 0x80 )
    3126             :             {
    3127           0 :                 aSections.push_back( nIndex );
    3128           0 :                 if( pFontData[nIndex+1] == 0x03 )
    3129           0 :                     break;
    3130             :                 sal_Int32 nBytes =
    3131           0 :                 ((sal_Int32)pFontData[nIndex+2])            |
    3132           0 :                 ((sal_Int32)pFontData[nIndex+3]) << 8       |
    3133           0 :                 ((sal_Int32)pFontData[nIndex+4]) << 16      |
    3134           0 :                 ((sal_Int32)pFontData[nIndex+5]) << 24;
    3135           0 :                 nIndex += nBytes+6;
    3136             :             }
    3137             : 
    3138             :             // search for eexec
    3139             :             // TODO: use getPfbSegmentLengths() if possible to skip the search thingies below
    3140           0 :             nIndex = 0;
    3141             :             int nEndAsciiIndex;
    3142             :             int nBeginBinaryIndex;
    3143             :             int nEndBinaryIndex;
    3144           0 :             do
    3145             :             {
    3146           0 :                 while( nIndex < nFontLen-4 &&
    3147           0 :                     ( pFontData[nIndex] != 'e'  ||
    3148           0 :                         pFontData[nIndex+1] != 'e' ||
    3149           0 :                         pFontData[nIndex+2] != 'x' ||
    3150           0 :                         pFontData[nIndex+3] != 'e' ||
    3151           0 :                         pFontData[nIndex+4] != 'c'
    3152             :                         )
    3153             :                     )
    3154             :                 {
    3155           0 :                     ++nIndex;
    3156             :                 }
    3157             :                 // check whether we are in a excluded section
    3158           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3159             :                     ;
    3160           0 :             } while( it != aSections.end() && nIndex < nFontLen-4 );
    3161             :             // this should end the ascii part
    3162           0 :             if( nIndex > nFontLen-5 )
    3163           0 :                 throw FontException();
    3164             : 
    3165           0 :             nEndAsciiIndex = nIndex+4;
    3166             :             // now count backwards until we can account for 512 '0'
    3167             :             // which is the endmarker of the (hopefully) binary data
    3168             :             // do not count the pfb header sections
    3169           0 :             int nFound = 0;
    3170           0 :             nIndex =  nFontLen-1;
    3171           0 :             while( nIndex > 0 && nFound < 512 )
    3172             :             {
    3173           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3174             :                     ;
    3175           0 :                 if( it == aSections.end() )
    3176             :                 {
    3177             :                     // inside the 512 '0' block there may only be whitespace
    3178             :                     // according to T1 spec; probably it would be to simple
    3179             :                     // if all fonts complied
    3180           0 :                     if( pFontData[nIndex] == '0' )
    3181           0 :                         nFound++;
    3182           0 :                         else if( nFound > 0                 &&
    3183           0 :                             pFontData[nIndex] != '\r'       &&
    3184           0 :                         pFontData[nIndex] != '\t'       &&
    3185           0 :                         pFontData[nIndex] != '\n'       &&
    3186           0 :                         pFontData[nIndex] != ' ' )
    3187           0 :                         break;
    3188             :                 }
    3189           0 :                 nIndex--;
    3190             :             }
    3191             : 
    3192           0 :             if( nIndex < 1 || nIndex <= nEndAsciiIndex )
    3193           0 :                 throw FontException();
    3194             : 
    3195             :             // nLength3 is the rest of the file - excluding any section headers
    3196             :             // nIndex now points before the first of the 512 '0' characters marking the
    3197             :             // fixed content portion
    3198           0 :             sal_Int32 nLength3 = nFontLen - nIndex - 1;
    3199           0 :             for( it = aSections.begin(); it != aSections.end(); ++it )
    3200             :             {
    3201             :                 // special case: nIndex inside a section marker
    3202           0 :                 if( nIndex >= (*it) && (*it)+6 > nIndex )
    3203           0 :                     nLength3 -= (*it)+6 - nIndex;
    3204           0 :                 else if( *it >= nIndex  )
    3205             :                 {
    3206           0 :                     if( *it < nFontLen - 6 )
    3207           0 :                         nLength3 -= 6;
    3208             :                     else // the last section 0x8003 is only 2 bytes after all
    3209           0 :                         nLength3 -= (nFontLen - *it);
    3210             :                 }
    3211             :             }
    3212             : 
    3213             :             // there may be whitespace to ignore before the 512 '0'
    3214           0 :             while( pFontData[nIndex] == '\r' || pFontData[nIndex] == '\n' )
    3215             :             {
    3216           0 :                 nIndex--;
    3217           0 :                 for( it = aSections.begin(); it != aSections.end() && (nIndex < *it || nIndex > ((*it) + 5) ); ++it )
    3218             :                     ;
    3219           0 :                 if( it != aSections.end() )
    3220             :                 {
    3221           0 :                     nIndex = (*it)-1;
    3222           0 :                     break; // this is surely a binary boundary, in ascii case it wouldn't matter
    3223             :                 }
    3224             :             }
    3225           0 :             nEndBinaryIndex = nIndex;
    3226             : 
    3227             :             // search for beginning of binary section
    3228           0 :             nBeginBinaryIndex = nEndAsciiIndex;
    3229           0 :             do
    3230             :             {
    3231           0 :                 nBeginBinaryIndex++;
    3232           0 :                 for( it = aSections.begin(); it != aSections.end() && (nBeginBinaryIndex < *it || nBeginBinaryIndex > ((*it) + 5) ); ++it )
    3233             :                     ;
    3234           0 :                     } while( nBeginBinaryIndex < nEndBinaryIndex &&
    3235           0 :                         ( pFontData[nBeginBinaryIndex] == '\r'  ||
    3236           0 :                             pFontData[nBeginBinaryIndex] == '\n'    ||
    3237           0 :                             it != aSections.end() ) );
    3238             : 
    3239             :                     // it seems to be vital to copy the exact whitespace between binary data
    3240             :                     // and eexec, else a invalid font results. so make nEndAsciiIndex
    3241             :                     // always immediate in front of nBeginBinaryIndex
    3242           0 :                     nEndAsciiIndex = nBeginBinaryIndex-1;
    3243           0 :                     for( it = aSections.begin(); it != aSections.end() && (nEndAsciiIndex < *it || nEndAsciiIndex > ((*it)+5)); ++it )
    3244             :                         ;
    3245           0 :                     if( it != aSections.end() )
    3246           0 :                         nEndAsciiIndex = (*it)-1;
    3247             : 
    3248           0 :                     nLength1 = nEndAsciiIndex+1; // including the last character
    3249           0 :                     for( it = aSections.begin(); it != aSections.end() && *it < nEndAsciiIndex; ++it )
    3250           0 :                         nLength1 -= 6; // decrease by pfb section size
    3251             : 
    3252             :                     // if the first four bytes are all ascii hex characters, then binary data
    3253             :                     // has to be converted to real binary data
    3254           0 :                     for( nIndex = 0; nIndex < 4 &&
    3255           0 :                         ( ( pFontData[ nBeginBinaryIndex+nIndex ] >= '0' && pFontData[ nBeginBinaryIndex+nIndex ] <= '9' ) ||
    3256           0 :                             ( pFontData[ nBeginBinaryIndex+nIndex ] >= 'a' && pFontData[ nBeginBinaryIndex+nIndex ] <= 'f' ) ||
    3257           0 :                             ( pFontData[ nBeginBinaryIndex+nIndex ] >= 'A' && pFontData[ nBeginBinaryIndex+nIndex ] <= 'F' )
    3258             :                             ); ++nIndex )
    3259             :                         ;
    3260           0 :                     bool bConvertHexData = true;
    3261           0 :                     if( nIndex < 4 )
    3262             :                     {
    3263           0 :                         bConvertHexData = false;
    3264           0 :                         nLength2 = nEndBinaryIndex - nBeginBinaryIndex + 1; // include the last byte
    3265           0 :                         for( it = aSections.begin(); it != aSections.end(); ++it )
    3266           0 :                             if( *it > nBeginBinaryIndex && *it < nEndBinaryIndex )
    3267           0 :                                 nLength2 -= 6;
    3268             :                     }
    3269             :                     else
    3270             :                     {
    3271             :                         // count the hex ascii characters to get nLength2
    3272           0 :                         nLength2 = 0;
    3273           0 :                         int nNextSectionIndex = 0;
    3274           0 :                         for( it = aSections.begin(); it != aSections.end() && *it < nBeginBinaryIndex; ++it )
    3275             :                             ;
    3276           0 :                         if( it != aSections.end() )
    3277           0 :                             nNextSectionIndex = *it;
    3278           0 :                         for( nIndex = nBeginBinaryIndex; nIndex <= nEndBinaryIndex; nIndex++ )
    3279             :                         {
    3280           0 :                             if( nIndex == nNextSectionIndex )
    3281             :                             {
    3282           0 :                                 nIndex += 6;
    3283           0 :                                 ++it;
    3284           0 :                                 nNextSectionIndex = (it == aSections.end() ? 0 : *it );
    3285             :                             }
    3286           0 :                             if( ( pFontData[ nIndex ] >= '0' && pFontData[ nIndex ] <= '9' ) ||
    3287           0 :                                 ( pFontData[ nIndex ] >= 'a' && pFontData[ nIndex ] <= 'f' ) ||
    3288           0 :                             ( pFontData[ nIndex ] >= 'A' && pFontData[ nIndex ] <= 'F' ) )
    3289           0 :                             nLength2++;
    3290             :                         }
    3291             :                         DBG_ASSERT( !(nLength2 & 1), "uneven number of hex chars in binary pfa section" );
    3292           0 :                         nLength2 /= 2;
    3293             :                     }
    3294             : 
    3295             :                     // now we can actually write the font stream !
    3296             :                     #if OSL_DEBUG_LEVEL > 1
    3297             :                     emitComment( " PDFWriterImpl::emitEmbeddedFont" );
    3298             :                     #endif
    3299           0 :                     OStringBuffer aLine( 512 );
    3300           0 :                     nStreamObject = createObject();
    3301           0 :                     if( !updateObject(nStreamObject))
    3302           0 :                         throw FontException();
    3303           0 :                     sal_Int32 nStreamLengthObject = createObject();
    3304           0 :                     aLine.append( nStreamObject );
    3305             :                     aLine.append( " 0 obj\n"
    3306           0 :                         "<</Length " );
    3307           0 :                     aLine.append( nStreamLengthObject );
    3308             :                     aLine.append( " 0 R"
    3309             :                         #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3310             :                         "/Filter/FlateDecode"
    3311             :                         #endif
    3312           0 :                         "/Length1 " );
    3313           0 :                     aLine.append( nLength1 );
    3314           0 :                     aLine.append( " /Length2 " );
    3315           0 :                     aLine.append( nLength2 );
    3316           0 :                     aLine.append( " /Length3 ");
    3317           0 :                     aLine.append( nLength3 );
    3318             :                     aLine.append( ">>\n"
    3319           0 :                         "stream\n" );
    3320           0 :                     if( !writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3321           0 :                         throw FontException();
    3322             : 
    3323           0 :                     sal_uInt64 nBeginStreamPos = 0;
    3324           0 :                     m_aFile.getPos(nBeginStreamPos);
    3325             : 
    3326           0 :                     beginCompression();
    3327           0 :                     checkAndEnableStreamEncryption( nStreamObject );
    3328             : 
    3329             :                     // write ascii section
    3330           0 :                     if( aSections.begin() == aSections.end() )
    3331             :                     {
    3332           0 :                         if( ! writeBuffer( pFontData, nEndAsciiIndex+1 ) )
    3333           0 :                             throw FontException();
    3334             :                     }
    3335             :                     else
    3336             :                     {
    3337             :                         // first section always starts at 0
    3338           0 :                         it = aSections.begin();
    3339           0 :                         nIndex = (*it)+6;
    3340           0 :                         ++it;
    3341           0 :                         while( *it < nEndAsciiIndex )
    3342             :                         {
    3343           0 :                             if( ! writeBuffer( pFontData+nIndex, (*it)-nIndex ) )
    3344           0 :                                 throw FontException();
    3345           0 :                             nIndex = (*it)+6;
    3346           0 :                             ++it;
    3347             :                         }
    3348             :                         // write partial last section
    3349           0 :                         if( ! writeBuffer( pFontData+nIndex, nEndAsciiIndex-nIndex+1 ) )
    3350           0 :                             throw FontException();
    3351             :                     }
    3352             : 
    3353             :                     // write binary section
    3354           0 :                     if( ! bConvertHexData )
    3355             :                     {
    3356           0 :                         if( aSections.begin() == aSections.end() )
    3357             :                         {
    3358           0 :                             if( ! writeBuffer( pFontData+nBeginBinaryIndex, nFontLen-nBeginBinaryIndex ) )
    3359           0 :                                 throw FontException();
    3360             :                         }
    3361             :                         else
    3362             :                         {
    3363           0 :                             for( it = aSections.begin(); *it < nBeginBinaryIndex; ++it )
    3364             :                                 ;
    3365             :                             // write first partial section
    3366           0 :                             if( ! writeBuffer( pFontData+nBeginBinaryIndex, (*it) - nBeginBinaryIndex ) )
    3367           0 :                                 throw FontException();
    3368             :                             // write following sections
    3369           0 :                             while( it != aSections.end() )
    3370             :                             {
    3371           0 :                                 nIndex = (*it)+6;
    3372           0 :                                 ++it;
    3373           0 :                                 if( nIndex < nFontLen ) // last section marker is usually the EOF which has only 2 bytes
    3374             :                                 {
    3375           0 :                                     sal_Int32 nSectionLen = (it == aSections.end()) ? nFontLen - nIndex : (*it) - nIndex;
    3376           0 :                                     if( ! writeBuffer( pFontData+nIndex, nSectionLen ) )
    3377           0 :                                         throw FontException();
    3378             :                                 }
    3379             :                             }
    3380             :                         }
    3381             :                     }
    3382             :                     else
    3383             :                     {
    3384           0 :                         boost::shared_array<unsigned char> pWriteBuffer( new unsigned char[ nLength2 ] );
    3385           0 :                         memset( pWriteBuffer.get(), 0, nLength2 );
    3386           0 :                         int nWriteIndex = 0;
    3387             : 
    3388           0 :                         int nNextSectionIndex = 0;
    3389           0 :                         for( it = aSections.begin(); it != aSections.end() && *it < nBeginBinaryIndex; ++it )
    3390             :                             ;
    3391           0 :                         if( it != aSections.end() )
    3392           0 :                             nNextSectionIndex = *it;
    3393           0 :                         for( nIndex = nBeginBinaryIndex; nIndex <= nEndBinaryIndex; nIndex++ )
    3394             :                         {
    3395           0 :                             if( nIndex == nNextSectionIndex )
    3396             :                             {
    3397           0 :                                 nIndex += 6;
    3398           0 :                                 ++it;
    3399           0 :                                 nNextSectionIndex = (it == aSections.end() ? nFontLen : *it );
    3400             :                             }
    3401           0 :                             unsigned char cNibble = 0x80;
    3402           0 :                             if( pFontData[ nIndex ] >= '0' && pFontData[ nIndex ] <= '9' )
    3403           0 :                                 cNibble = pFontData[nIndex] - '0';
    3404           0 :                             else if( pFontData[ nIndex ] >= 'a' && pFontData[ nIndex ] <= 'f' )
    3405           0 :                                 cNibble = pFontData[nIndex] - 'a' + 10;
    3406           0 :                             else if( pFontData[ nIndex ] >= 'A' && pFontData[ nIndex ] <= 'F' )
    3407           0 :                                 cNibble = pFontData[nIndex] - 'A' + 10;
    3408           0 :                             if( cNibble != 0x80 )
    3409             :                             {
    3410           0 :                                 if( !(nWriteIndex & 1 ) )
    3411           0 :                                     cNibble <<= 4;
    3412           0 :                                 pWriteBuffer.get()[ nWriteIndex/2 ] |= cNibble;
    3413           0 :                                 nWriteIndex++;
    3414             :                             }
    3415             :                         }
    3416           0 :                         if( ! writeBuffer( pWriteBuffer.get(), nLength2 ) )
    3417           0 :                             throw FontException();
    3418           0 :                         if( aSections.empty() )
    3419             :                         {
    3420           0 :                             if( ! writeBuffer( pFontData+nIndex, nFontLen-nIndex ) )
    3421           0 :                                 throw FontException();
    3422             :                         }
    3423             :                         else
    3424             :                         {
    3425             :                             // write rest of this section
    3426           0 :                             if( nIndex < nNextSectionIndex )
    3427             :                             {
    3428           0 :                                 if( ! writeBuffer( pFontData+nIndex, nNextSectionIndex - nIndex ) )
    3429           0 :                                     throw FontException();
    3430             :                             }
    3431             :                             // write following sections
    3432           0 :                             while( it != aSections.end() )
    3433             :                             {
    3434           0 :                                 nIndex = (*it)+6;
    3435           0 :                                 ++it;
    3436           0 :                                 if( nIndex < nFontLen ) // last section marker is usually the EOF which has only 2 bytes
    3437             :                                 {
    3438           0 :                                     sal_Int32 nSectionLen = (it == aSections.end()) ? nFontLen - nIndex : (*it) - nIndex;
    3439           0 :                                     if( ! writeBuffer( pFontData+nIndex, nSectionLen ) )
    3440           0 :                                         throw FontException();
    3441             :                                 }
    3442             :                             }
    3443           0 :                         }
    3444             :                     }
    3445           0 :                     endCompression();
    3446           0 :                     disableStreamEncryption();
    3447             : 
    3448           0 :                     sal_uInt64 nEndStreamPos = 0;
    3449           0 :                     m_aFile.getPos(nEndStreamPos);
    3450             : 
    3451             :                     // and finally close the stream
    3452           0 :                     aLine.setLength( 0 );
    3453           0 :                     aLine.append( "\nendstream\nendobj\n\n" );
    3454           0 :                     if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3455           0 :                         throw FontException();
    3456             : 
    3457             :                     // write stream length object
    3458           0 :                     aLine.setLength( 0 );
    3459           0 :                     if( ! updateObject( nStreamLengthObject ) )
    3460           0 :                         throw FontException();
    3461           0 :                     aLine.append( nStreamLengthObject );
    3462           0 :                     aLine.append( " 0 obj\n" );
    3463           0 :                     aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos ) );
    3464           0 :                     aLine.append( "\nendobj\n\n" );
    3465           0 :                     if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3466           0 :                         throw FontException();
    3467             :         }
    3468             :         else
    3469             :         {
    3470           0 :             OStringBuffer aErrorComment( 256 );
    3471           0 :             aErrorComment.append( "GetEmbedFontData failed for font \"" );
    3472           0 :             aErrorComment.append( OUStringToOString( pFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ) );
    3473           0 :             aErrorComment.append( '\"' );
    3474           0 :             if( pFont->GetSlant() == ITALIC_NORMAL )
    3475           0 :                 aErrorComment.append( " italic" );
    3476           0 :             else if( pFont->GetSlant() == ITALIC_OBLIQUE )
    3477           0 :                 aErrorComment.append( " oblique" );
    3478           0 :             aErrorComment.append( " weight=" );
    3479           0 :             aErrorComment.append( sal_Int32(pFont->GetWeight()) );
    3480           0 :             emitComment( aErrorComment.getStr() );
    3481             :         }
    3482             : 
    3483           0 :         if( nStreamObject )
    3484             :         {
    3485             :             // write font descriptor
    3486           0 :             nFontDescriptor = emitFontDescriptor( pFont, aInfo, 0, nStreamObject );
    3487             :         }
    3488             : 
    3489           0 :         if( nFontDescriptor )
    3490             :         {
    3491           0 :             if( pEncoding )
    3492           0 :                 nToUnicodeStream = createToUnicodeCMap( nEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, SAL_N_ELEMENTS(nEncoding) );
    3493             : 
    3494             :             // write font object
    3495           0 :             sal_Int32 nObject = createObject();
    3496           0 :             if( ! updateObject( nObject ) )
    3497           0 :                 throw FontException();
    3498             : 
    3499           0 :             OStringBuffer aLine( 1024 );
    3500           0 :             aLine.append( nObject );
    3501             :             aLine.append( " 0 obj\n"
    3502           0 :                 "<</Type/Font/Subtype/Type1/BaseFont/" );
    3503           0 :             appendName( aInfo.m_aPSName, aLine );
    3504           0 :             aLine.append( "\n" );
    3505           0 :             if( !pFont->IsSymbolFont() )
    3506           0 :                 aLine.append( "/Encoding/WinAnsiEncoding\n" );
    3507           0 :             if( nToUnicodeStream )
    3508             :             {
    3509           0 :                 aLine.append( "/ToUnicode " );
    3510           0 :                 aLine.append( nToUnicodeStream );
    3511           0 :                 aLine.append( " 0 R\n" );
    3512             :             }
    3513             :             aLine.append( "/FirstChar 0 /LastChar 255\n"
    3514           0 :                 "/Widths[" );
    3515           0 :             for( int i = 0; i < 256; i++ )
    3516             :             {
    3517           0 :                 aLine.append( pWidths[i] );
    3518           0 :                 aLine.append( ((i&15) == 15) ? "\n" : " " );
    3519             :             }
    3520             :             aLine.append( "]\n"
    3521           0 :                 "/FontDescriptor " );
    3522           0 :             aLine.append( nFontDescriptor );
    3523             :             aLine.append( " 0 R>>\n"
    3524           0 :                 "endobj\n\n" );
    3525           0 :             if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3526           0 :                 throw FontException();
    3527             : 
    3528           0 :             aRet[ rEmbed.m_nNormalFontID ] = nObject;
    3529             : 
    3530             :             // write additional encodings
    3531           0 :             for( std::list< EmbedEncoding >::iterator enc_it = rEmbed.m_aExtendedEncodings.begin(); enc_it != rEmbed.m_aExtendedEncodings.end(); ++enc_it )
    3532             :             {
    3533             :                 sal_Int32 aEncWidths[ 256 ];
    3534             :                 // emit encoding dict
    3535           0 :                 sal_Int32 nEncObject = createObject();
    3536           0 :                 if( ! updateObject( nEncObject ) )
    3537           0 :                     throw FontException();
    3538             : 
    3539           0 :                 OutputDevice* pRef = getReferenceDevice();
    3540           0 :                 pRef->Push( PushFlags::FONT | PushFlags::MAPMODE );
    3541           0 :                 pRef->SetMapMode( MapMode( MAP_PIXEL ) );
    3542           0 :                 Font aFont( pFont->GetFamilyName(), pFont->GetStyleName(), Size( 0, 1000 ) );
    3543           0 :                 aFont.SetWeight( pFont->GetWeight() );
    3544           0 :                 aFont.SetItalic( pFont->GetSlant() );
    3545           0 :                 aFont.SetPitch( pFont->GetPitch() );
    3546           0 :                 pRef->SetFont( aFont );
    3547           0 :                 pRef->ImplNewFont();
    3548             : 
    3549           0 :                 aLine.setLength( 0 );
    3550           0 :                 aLine.append( nEncObject );
    3551             :                 aLine.append( " 0 obj\n"
    3552           0 :                     "<</Type/Encoding/Differences[ 0\n" );
    3553           0 :                 int nEncoded = 0;
    3554           0 :                 aUnicodes.clear();
    3555           0 :                 for( std::vector< EmbedCode >::iterator str_it = enc_it->m_aEncVector.begin(); str_it != enc_it->m_aEncVector.end(); ++str_it )
    3556             :                 {
    3557           0 :                     OUString aStr( str_it->m_aUnicode );
    3558           0 :                     aEncWidths[nEncoded] = pRef->GetTextWidth( aStr );
    3559           0 :                     nEncodedCodes[nEncoded] = str_it->m_aUnicode;
    3560           0 :                     nEncoding[nEncoded] = sal::static_int_cast<sal_uInt8>(nEncoded);
    3561           0 :                     pEncToUnicodeIndex[nEncoded] = static_cast<sal_Int32>(aUnicodes.size());
    3562           0 :                     aUnicodes.push_back( nEncodedCodes[nEncoded] );
    3563           0 :                     pUnicodesPerGlyph[nEncoded] = 1;
    3564             : 
    3565           0 :                     aLine.append( " /" );
    3566           0 :                     aLine.append( str_it->m_aName );
    3567           0 :                     if( !((++nEncoded) & 15) )
    3568           0 :                         aLine.append( "\n" );
    3569           0 :                 }
    3570             :                 aLine.append( "]>>\n"
    3571           0 :                     "endobj\n\n" );
    3572             : 
    3573           0 :                 pRef->Pop();
    3574             : 
    3575           0 :                 if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3576           0 :                     throw FontException();
    3577             : 
    3578           0 :                 nToUnicodeStream = createToUnicodeCMap( nEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, nEncoded );
    3579             : 
    3580           0 :                 nObject = createObject();
    3581           0 :                 if( ! updateObject( nObject ) )
    3582           0 :                     throw FontException();
    3583             : 
    3584           0 :                 aLine.setLength( 0 );
    3585           0 :                 aLine.append( nObject );
    3586             :                 aLine.append( " 0 obj\n"
    3587           0 :                     "<</Type/Font/Subtype/Type1/BaseFont/" );
    3588           0 :                 appendName( aInfo.m_aPSName, aLine );
    3589           0 :                 aLine.append( "\n" );
    3590           0 :                 aLine.append( "/Encoding " );
    3591           0 :                 aLine.append( nEncObject );
    3592           0 :                 aLine.append( " 0 R\n" );
    3593           0 :                 if( nToUnicodeStream )
    3594             :                 {
    3595           0 :                     aLine.append( "/ToUnicode " );
    3596           0 :                     aLine.append( nToUnicodeStream );
    3597           0 :                     aLine.append( " 0 R\n" );
    3598             :                 }
    3599             :                 aLine.append( "/FirstChar 0\n"
    3600           0 :                     "/LastChar " );
    3601           0 :                 aLine.append( (sal_Int32)(nEncoded-1) );
    3602             :                 aLine.append( "\n"
    3603           0 :                     "/Widths[" );
    3604           0 :                 for( int i = 0; i < nEncoded; i++ )
    3605             :                 {
    3606           0 :                     aLine.append( aEncWidths[i] );
    3607           0 :                     aLine.append( ((i&15) == 15) ? "\n" : " " );
    3608             :                 }
    3609             :                 aLine.append( " ]\n"
    3610           0 :                     "/FontDescriptor " );
    3611           0 :                 aLine.append( nFontDescriptor );
    3612             :                 aLine.append( " 0 R>>\n"
    3613           0 :                     "endobj\n\n" );
    3614           0 :                 if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    3615           0 :                     throw FontException();
    3616             : 
    3617           0 :                 aRet[ enc_it->m_nFontID ] = nObject;
    3618           0 :             }
    3619             :         }
    3620             :     }
    3621           0 :     catch( FontException& )
    3622             :     {
    3623             :         // these do nothing in case there was no compression or encryption ongoing
    3624           0 :         endCompression();
    3625           0 :         disableStreamEncryption();
    3626             :     }
    3627             : 
    3628           0 :     if( pFontData )
    3629           0 :         m_pReferenceDevice->mpGraphics->FreeEmbedFontData( pFontData, nFontLen );
    3630             : 
    3631           0 :     return aRet;
    3632             : }
    3633             : 
    3634           0 : static void appendSubsetName( int nSubsetID, const OUString& rPSName, OStringBuffer& rBuffer )
    3635             : {
    3636           0 :     if( nSubsetID )
    3637             :     {
    3638           0 :         for( int i = 0; i < 6; i++ )
    3639             :         {
    3640           0 :             int nOffset = (nSubsetID % 26);
    3641           0 :             nSubsetID /= 26;
    3642           0 :             rBuffer.append( (sal_Char)('A'+nOffset) );
    3643             :         }
    3644           0 :         rBuffer.append( '+' );
    3645             :     }
    3646           0 :     appendName( rPSName, rBuffer );
    3647           0 : }
    3648             : 
    3649           0 : sal_Int32 PDFWriterImpl::createToUnicodeCMap( sal_uInt8* pEncoding,
    3650             :                                               sal_Ucs* pUnicodes,
    3651             :                                               sal_Int32* pUnicodesPerGlyph,
    3652             :                                               sal_Int32* pEncToUnicodeIndex,
    3653             :                                               int nGlyphs )
    3654             : {
    3655           0 :     int nMapped = 0, n = 0;
    3656           0 :     for( n = 0; n < nGlyphs; n++ )
    3657           0 :         if( pUnicodes[pEncToUnicodeIndex[n]] && pUnicodesPerGlyph[n] )
    3658           0 :             nMapped++;
    3659             : 
    3660           0 :     if( nMapped == 0 )
    3661           0 :         return 0;
    3662             : 
    3663           0 :     sal_Int32 nStream = createObject();
    3664           0 :     CHECK_RETURN( updateObject( nStream ) );
    3665             : 
    3666           0 :     OStringBuffer aContents( 1024 );
    3667             :     aContents.append(
    3668             :                      "/CIDInit/ProcSet findresource begin\n"
    3669             :                      "12 dict begin\n"
    3670             :                      "begincmap\n"
    3671             :                      "/CIDSystemInfo<<\n"
    3672             :                      "/Registry (Adobe)\n"
    3673             :                      "/Ordering (UCS)\n"
    3674             :                      "/Supplement 0\n"
    3675             :                      ">> def\n"
    3676             :                      "/CMapName/Adobe-Identity-UCS def\n"
    3677             :                      "/CMapType 2 def\n"
    3678             :                      "1 begincodespacerange\n"
    3679             :                      "<00> <FF>\n"
    3680             :                      "endcodespacerange\n"
    3681           0 :                      );
    3682           0 :     int nCount = 0;
    3683           0 :     for( n = 0; n < nGlyphs; n++ )
    3684             :     {
    3685           0 :         if( pUnicodes[pEncToUnicodeIndex[n]] && pUnicodesPerGlyph[n] )
    3686             :         {
    3687           0 :             if( (nCount % 100) == 0 )
    3688             :             {
    3689           0 :                 if( nCount )
    3690           0 :                     aContents.append( "endbfchar\n" );
    3691           0 :                 aContents.append( (sal_Int32)((nMapped-nCount > 100) ? 100 : nMapped-nCount ) );
    3692           0 :                 aContents.append( " beginbfchar\n" );
    3693             :             }
    3694           0 :             aContents.append( '<' );
    3695           0 :             appendHex( (sal_Int8)pEncoding[n], aContents );
    3696           0 :             aContents.append( "> <" );
    3697             :             // TODO: handle unicodes>U+FFFF
    3698           0 :             sal_Int32 nIndex = pEncToUnicodeIndex[n];
    3699           0 :             for( sal_Int32 j = 0; j < pUnicodesPerGlyph[n]; j++ )
    3700             :             {
    3701           0 :                 appendHex( (sal_Int8)(pUnicodes[nIndex + j] / 256), aContents );
    3702           0 :                 appendHex( (sal_Int8)(pUnicodes[nIndex + j] & 255), aContents );
    3703             :             }
    3704           0 :             aContents.append( ">\n" );
    3705           0 :             nCount++;
    3706             :         }
    3707             :     }
    3708             :     aContents.append( "endbfchar\n"
    3709             :                       "endcmap\n"
    3710             :                       "CMapName currentdict /CMap defineresource pop\n"
    3711             :                       "end\n"
    3712           0 :                       "end\n" );
    3713             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3714           0 :     ZCodec pCodec( 0x4000, 0x4000 );
    3715           0 :     SvMemoryStream aStream;
    3716           0 :     pCodec.BeginCompression();
    3717           0 :     pCodec.Write( aStream, (const sal_uInt8*)aContents.getStr(), aContents.getLength() );
    3718           0 :     pCodec.EndCompression();
    3719             : #endif
    3720             : 
    3721             :     #if OSL_DEBUG_LEVEL > 1
    3722             :     emitComment( "PDFWriterImpl::createToUnicodeCMap" );
    3723             :     #endif
    3724           0 :     OStringBuffer aLine( 40 );
    3725             : 
    3726           0 :     aLine.append( nStream );
    3727           0 :     aLine.append( " 0 obj\n<</Length " );
    3728             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3729           0 :     sal_Int32 nLen = (sal_Int32)aStream.Tell();
    3730           0 :     aStream.Seek( 0 );
    3731           0 :     aLine.append( nLen );
    3732           0 :     aLine.append( "/Filter/FlateDecode" );
    3733             : #else
    3734             :     aLine.append( aContents.getLength() );
    3735             : #endif
    3736           0 :     aLine.append( ">>\nstream\n" );
    3737           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3738           0 :     checkAndEnableStreamEncryption( nStream );
    3739             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3740           0 :     CHECK_RETURN( writeBuffer( aStream.GetData(), nLen ) );
    3741             : #else
    3742             :     CHECK_RETURN( writeBuffer( aContents.getStr(), aContents.getLength() ) );
    3743             : #endif
    3744           0 :     disableStreamEncryption();
    3745           0 :     aLine.setLength( 0 );
    3746             :     aLine.append( "\nendstream\n"
    3747           0 :                   "endobj\n\n" );
    3748           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3749           0 :     return nStream;
    3750             : }
    3751             : 
    3752           0 : sal_Int32 PDFWriterImpl::emitFontDescriptor( const PhysicalFontFace* pFont, FontSubsetInfo& rInfo, sal_Int32 nSubsetID, sal_Int32 nFontStream )
    3753             : {
    3754           0 :     OStringBuffer aLine( 1024 );
    3755             :     // get font flags, see PDF reference 1.4 p. 358
    3756             :     // possibly characters outside Adobe standard encoding
    3757             :     // so set Symbolic flag
    3758           0 :     sal_Int32 nFontFlags = (1<<2);
    3759           0 :     if( pFont->GetSlant() == ITALIC_NORMAL || pFont->GetSlant() == ITALIC_OBLIQUE )
    3760           0 :         nFontFlags |= (1 << 6);
    3761           0 :     if( pFont->GetPitch() == PITCH_FIXED )
    3762           0 :         nFontFlags |= 1;
    3763           0 :     if( pFont->GetFamilyType() == FAMILY_SCRIPT )
    3764           0 :         nFontFlags |= (1 << 3);
    3765           0 :     else if( pFont->GetFamilyType() == FAMILY_ROMAN )
    3766           0 :         nFontFlags |= (1 << 1);
    3767             : 
    3768           0 :     sal_Int32 nFontDescriptor = createObject();
    3769           0 :     CHECK_RETURN( updateObject( nFontDescriptor ) );
    3770           0 :     aLine.setLength( 0 );
    3771           0 :     aLine.append( nFontDescriptor );
    3772             :     aLine.append( " 0 obj\n"
    3773           0 :                   "<</Type/FontDescriptor/FontName/" );
    3774           0 :     appendSubsetName( nSubsetID, rInfo.m_aPSName, aLine );
    3775             :     aLine.append( "\n"
    3776           0 :                   "/Flags " );
    3777           0 :     aLine.append( nFontFlags );
    3778             :     aLine.append( "\n"
    3779           0 :                   "/FontBBox[" );
    3780             :     // note: Top and Bottom are reversed in VCL and PDF rectangles
    3781           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.TopLeft().X() );
    3782           0 :     aLine.append( ' ' );
    3783           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.TopLeft().Y() );
    3784           0 :     aLine.append( ' ' );
    3785           0 :     aLine.append( (sal_Int32)rInfo.m_aFontBBox.BottomRight().X() );
    3786           0 :     aLine.append( ' ' );
    3787           0 :     aLine.append( (sal_Int32)(rInfo.m_aFontBBox.BottomRight().Y()+1) );
    3788           0 :     aLine.append( "]/ItalicAngle " );
    3789           0 :     if( pFont->GetSlant() == ITALIC_OBLIQUE || pFont->GetSlant() == ITALIC_NORMAL )
    3790           0 :         aLine.append( "-30" );
    3791             :     else
    3792           0 :         aLine.append( "0" );
    3793             :     aLine.append( "\n"
    3794           0 :                   "/Ascent " );
    3795           0 :     aLine.append( (sal_Int32)rInfo.m_nAscent );
    3796             :     aLine.append( "\n"
    3797           0 :                   "/Descent " );
    3798           0 :     aLine.append( (sal_Int32)-rInfo.m_nDescent );
    3799             :     aLine.append( "\n"
    3800           0 :                   "/CapHeight " );
    3801           0 :     aLine.append( (sal_Int32)rInfo.m_nCapHeight );
    3802             :     // According to PDF reference 1.4 StemV is required
    3803             :     // seems a tad strange to me, but well ...
    3804             :     aLine.append( "\n"
    3805           0 :                   "/StemV 80\n" );
    3806           0 :     if( nFontStream )
    3807             :     {
    3808           0 :         aLine.append( "/FontFile" );
    3809           0 :         switch( rInfo.m_nFontType )
    3810             :         {
    3811             :             case FontSubsetInfo::SFNT_TTF:
    3812           0 :                 aLine.append( '2' );
    3813           0 :                 break;
    3814             :             case FontSubsetInfo::TYPE1_PFA:
    3815             :             case FontSubsetInfo::TYPE1_PFB:
    3816             :             case FontSubsetInfo::ANY_TYPE1:
    3817           0 :                 break;
    3818             :             default:
    3819             :                 OSL_FAIL( "unknown fonttype in PDF font descriptor" );
    3820           0 :                 return 0;
    3821             :         }
    3822           0 :         aLine.append( ' ' );
    3823           0 :         aLine.append( nFontStream );
    3824           0 :         aLine.append( " 0 R\n" );
    3825             :     }
    3826             :     aLine.append( ">>\n"
    3827           0 :                   "endobj\n\n" );
    3828           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    3829             : 
    3830           0 :     return nFontDescriptor;
    3831             : }
    3832             : 
    3833           0 : void PDFWriterImpl::appendBuiltinFontsToDict( OStringBuffer& rDict ) const
    3834             : {
    3835           0 :     for( std::map< sal_Int32, sal_Int32 >::const_iterator it =
    3836           0 :          m_aBuiltinFontToObjectMap.begin(); it != m_aBuiltinFontToObjectMap.end(); ++it )
    3837             :     {
    3838           0 :         rDict.append( m_aBuiltinFonts[it->first].getNameObject() );
    3839           0 :         rDict.append( ' ' );
    3840           0 :         rDict.append( it->second );
    3841           0 :         rDict.append( " 0 R" );
    3842             :     }
    3843           0 : }
    3844             : 
    3845           0 : bool PDFWriterImpl::emitFonts()
    3846             : {
    3847           0 :     if( ! m_pReferenceDevice->AcquireGraphics() )
    3848           0 :         return false;
    3849             : 
    3850           0 :     OStringBuffer aLine( 1024 );
    3851             : 
    3852           0 :     std::map< sal_Int32, sal_Int32 > aFontIDToObject;
    3853             : 
    3854           0 :     OUString aTmpName;
    3855           0 :     osl_createTempFile( NULL, NULL, &aTmpName.pData );
    3856           0 :     for( FontSubsetData::iterator it = m_aSubsets.begin(); it != m_aSubsets.end(); ++it )
    3857             :     {
    3858           0 :         for( FontEmitList::iterator lit = it->second.m_aSubsets.begin(); lit != it->second.m_aSubsets.end(); ++lit )
    3859             :         {
    3860             :             sal_GlyphId aGlyphIds[ 256 ];
    3861             :             sal_Int32 pWidths[ 256 ];
    3862             :             sal_uInt8 pEncoding[ 256 ];
    3863             :             sal_Int32 pEncToUnicodeIndex[ 256 ];
    3864             :             sal_Int32 pUnicodesPerGlyph[ 256 ];
    3865           0 :             std::vector<sal_Ucs> aUnicodes;
    3866           0 :             aUnicodes.reserve( 256 );
    3867           0 :             int nGlyphs = 1;
    3868             :             // fill arrays and prepare encoding index map
    3869           0 :             sal_Int32 nToUnicodeStream = 0;
    3870             : 
    3871           0 :             memset( aGlyphIds, 0, sizeof( aGlyphIds ) );
    3872           0 :             memset( pEncoding, 0, sizeof( pEncoding ) );
    3873           0 :             memset( pUnicodesPerGlyph, 0, sizeof( pUnicodesPerGlyph ) );
    3874           0 :             memset( pEncToUnicodeIndex, 0, sizeof( pEncToUnicodeIndex ) );
    3875           0 :             for( FontEmitMapping::iterator fit = lit->m_aMapping.begin(); fit != lit->m_aMapping.end();++fit )
    3876             :             {
    3877           0 :                 sal_uInt8 nEnc = fit->second.getGlyphId();
    3878             : 
    3879             :                 DBG_ASSERT( aGlyphIds[nEnc] == 0 && pEncoding[nEnc] == 0, "duplicate glyph" );
    3880             :                 DBG_ASSERT( nEnc <= lit->m_aMapping.size(), "invalid glyph encoding" );
    3881             : 
    3882           0 :                 aGlyphIds[ nEnc ] = fit->first;
    3883           0 :                 pEncoding[ nEnc ] = nEnc;
    3884           0 :                 pEncToUnicodeIndex[ nEnc ] = static_cast<sal_Int32>(aUnicodes.size());
    3885           0 :                 pUnicodesPerGlyph[ nEnc ] = fit->second.countCodes();
    3886           0 :                 for( sal_Int32 n = 0; n < pUnicodesPerGlyph[ nEnc ]; n++ )
    3887           0 :                     aUnicodes.push_back( fit->second.getCode( n ) );
    3888           0 :                 if( fit->second.getCode(0) )
    3889           0 :                     nToUnicodeStream = 1;
    3890           0 :                 if( nGlyphs < 256 )
    3891           0 :                     nGlyphs++;
    3892             :                 else
    3893             :                 {
    3894             :                     OSL_FAIL( "too many glyphs for subset" );
    3895             :                 }
    3896             :             }
    3897           0 :             FontSubsetInfo aSubsetInfo;
    3898           0 :             if( m_pReferenceDevice->mpGraphics->CreateFontSubset( aTmpName, it->first, aGlyphIds, pEncoding, pWidths, nGlyphs, aSubsetInfo ) )
    3899             :             {
    3900             :                 // create font stream
    3901           0 :                 osl::File aFontFile(aTmpName);
    3902           0 :                 if (osl::File::E_None != aFontFile.open(osl_File_OpenFlag_Read)) return false;
    3903             :                 // get file size
    3904             :                 sal_uInt64 nLength1;
    3905           0 :                 if ( osl::File::E_None != aFontFile.setPos(osl_Pos_End, 0) ) return false;
    3906           0 :                 if ( osl::File::E_None != aFontFile.getPos(nLength1) ) return false;
    3907           0 :                 if ( osl::File::E_None != aFontFile.setPos(osl_Pos_Absolut, 0) ) return false;
    3908             : 
    3909             :                 #if OSL_DEBUG_LEVEL > 1
    3910             :                 emitComment( "PDFWriterImpl::emitFonts" );
    3911             :                 #endif
    3912           0 :                 sal_Int32 nFontStream = createObject();
    3913           0 :                 sal_Int32 nStreamLengthObject = createObject();
    3914           0 :                 if ( !updateObject( nFontStream ) ) return false;
    3915           0 :                 aLine.setLength( 0 );
    3916           0 :                 aLine.append( nFontStream );
    3917             :                 aLine.append( " 0 obj\n"
    3918           0 :                              "<</Length " );
    3919           0 :                 aLine.append( (sal_Int32)nStreamLengthObject );
    3920             :                 aLine.append( " 0 R"
    3921             :                              #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    3922             :                              "/Filter/FlateDecode"
    3923             :                              #endif
    3924           0 :                              "/Length1 " );
    3925             : 
    3926           0 :                 sal_uInt64 nStartPos = 0;
    3927           0 :                 if( aSubsetInfo.m_nFontType == FontSubsetInfo::SFNT_TTF )
    3928             :                 {
    3929           0 :                     aLine.append( (sal_Int32)nLength1 );
    3930             : 
    3931             :                     aLine.append( ">>\n"
    3932           0 :                                  "stream\n" );
    3933           0 :                     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    3934           0 :                     if ( osl::File::E_None != m_aFile.getPos(nStartPos) ) return false;
    3935             : 
    3936             :                     // copy font file
    3937           0 :                     beginCompression();
    3938           0 :                     checkAndEnableStreamEncryption( nFontStream );
    3939           0 :                     sal_Bool bEOF = sal_False;
    3940           0 :                     do
    3941             :                     {
    3942             :                         char buf[8192];
    3943             :                         sal_uInt64 nRead;
    3944           0 :                         if ( osl::File::E_None != aFontFile.read(buf, sizeof(buf), nRead) ) return false;
    3945           0 :                         if ( !writeBuffer( buf, nRead ) ) return false;
    3946           0 :                         if ( osl::File::E_None != aFontFile.isEndOfFile(&bEOF) ) return false;
    3947           0 :                     } while( ! bEOF );
    3948             :                 }
    3949           0 :                 else if( (aSubsetInfo.m_nFontType & FontSubsetInfo::CFF_FONT) != 0 )
    3950             :                 {
    3951             :                     // TODO: implement
    3952             :                     OSL_FAIL( "PDFWriterImpl does not support CFF-font subsets yet!" );
    3953             :                 }
    3954           0 :                 else if( (aSubsetInfo.m_nFontType & FontSubsetInfo::TYPE1_PFB) != 0 ) // TODO: also support PFA?
    3955             :                 {
    3956           0 :                     boost::shared_array<unsigned char> pBuffer( new unsigned char[ nLength1 ] );
    3957             : 
    3958           0 :                     sal_uInt64 nBytesRead = 0;
    3959           0 :                     if ( osl::File::E_None != aFontFile.read(pBuffer.get(), nLength1, nBytesRead) ) return false;
    3960             :                     DBG_ASSERT( nBytesRead==nLength1, "PDF-FontSubset read incomplete!" );
    3961           0 :                     if ( osl::File::E_None != aFontFile.setPos(osl_Pos_Absolut, 0) ) return false;
    3962             :                     // get the PFB-segment lengths
    3963           0 :                     ThreeInts aSegmentLengths = {0,0,0};
    3964           0 :                     getPfbSegmentLengths( pBuffer.get(), (int)nBytesRead, aSegmentLengths );
    3965             :                     // the lengths below are mandatory for PDF-exported Type1 fonts
    3966             :                     // because the PFB segment headers get stripped! WhyOhWhy.
    3967           0 :                     aLine.append( (sal_Int32)aSegmentLengths[0] );
    3968           0 :                     aLine.append( "/Length2 " );
    3969           0 :                     aLine.append( (sal_Int32)aSegmentLengths[1] );
    3970           0 :                     aLine.append( "/Length3 " );
    3971           0 :                     aLine.append( (sal_Int32)aSegmentLengths[2] );
    3972             : 
    3973             :                     aLine.append( ">>\n"
    3974           0 :                                  "stream\n" );
    3975           0 :                     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    3976           0 :                     if ( osl::File::E_None != m_aFile.getPos(nStartPos) ) return false;
    3977             : 
    3978             :                     // emit PFB-sections without section headers
    3979           0 :                     beginCompression();
    3980           0 :                     checkAndEnableStreamEncryption( nFontStream );
    3981           0 :                     if ( !writeBuffer( &pBuffer[6], aSegmentLengths[0] ) ) return false;
    3982           0 :                     if ( !writeBuffer( &pBuffer[12] + aSegmentLengths[0], aSegmentLengths[1] ) ) return false;
    3983           0 :                     if ( !writeBuffer( &pBuffer[18] + aSegmentLengths[0] + aSegmentLengths[1], aSegmentLengths[2] ) ) return false;
    3984             :                 }
    3985             :                 else
    3986             :                 {
    3987           0 :                     fprintf( stderr, "PDF: CreateFontSubset result in not yet supported format=%d\n",aSubsetInfo.m_nFontType);
    3988           0 :                     aLine.append( "0 >>\nstream\n" );
    3989             :                 }
    3990             : 
    3991           0 :                 endCompression();
    3992           0 :                 disableStreamEncryption();
    3993             :                 // close the file
    3994           0 :                 aFontFile.close();
    3995             : 
    3996           0 :                 sal_uInt64 nEndPos = 0;
    3997           0 :                 if ( osl::File::E_None != m_aFile.getPos(nEndPos) ) return false;
    3998             :                 // end the stream
    3999           0 :                 aLine.setLength( 0 );
    4000           0 :                 aLine.append( "\nendstream\nendobj\n\n" );
    4001           0 :                 if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    4002             : 
    4003             :                 // emit stream length object
    4004           0 :                 if ( !updateObject( nStreamLengthObject ) ) return false;
    4005           0 :                 aLine.setLength( 0 );
    4006           0 :                 aLine.append( nStreamLengthObject );
    4007           0 :                 aLine.append( " 0 obj\n" );
    4008           0 :                 aLine.append( (sal_Int64)(nEndPos-nStartPos) );
    4009           0 :                 aLine.append( "\nendobj\n\n" );
    4010           0 :                 if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    4011             : 
    4012             :                 // write font descriptor
    4013           0 :                 sal_Int32 nFontDescriptor = emitFontDescriptor( it->first, aSubsetInfo, lit->m_nFontID, nFontStream );
    4014             : 
    4015           0 :                 if( nToUnicodeStream )
    4016           0 :                     nToUnicodeStream = createToUnicodeCMap( pEncoding, &aUnicodes[0], pUnicodesPerGlyph, pEncToUnicodeIndex, nGlyphs );
    4017             : 
    4018           0 :                 sal_Int32 nFontObject = createObject();
    4019           0 :                 if ( !updateObject( nFontObject ) ) return false;
    4020           0 :                 aLine.setLength( 0 );
    4021           0 :                 aLine.append( nFontObject );
    4022             : 
    4023           0 :                 aLine.append( " 0 obj\n" );
    4024           0 :                 aLine.append( ((aSubsetInfo.m_nFontType & FontSubsetInfo::ANY_TYPE1) != 0) ?
    4025             :                              "<</Type/Font/Subtype/Type1/BaseFont/" :
    4026           0 :                              "<</Type/Font/Subtype/TrueType/BaseFont/" );
    4027           0 :                 appendSubsetName( lit->m_nFontID, aSubsetInfo.m_aPSName, aLine );
    4028             :                 aLine.append( "\n"
    4029             :                              "/FirstChar 0\n"
    4030           0 :                              "/LastChar " );
    4031           0 :                 aLine.append( (sal_Int32)(nGlyphs-1) );
    4032             :                 aLine.append( "\n"
    4033           0 :                              "/Widths[" );
    4034           0 :                 for( int i = 0; i < nGlyphs; i++ )
    4035             :                 {
    4036           0 :                     aLine.append( pWidths[ i ] );
    4037           0 :                     aLine.append( ((i & 15) == 15) ? "\n" : " " );
    4038             :                 }
    4039             :                 aLine.append( "]\n"
    4040           0 :                              "/FontDescriptor " );
    4041           0 :                 aLine.append( nFontDescriptor );
    4042           0 :                 aLine.append( " 0 R\n" );
    4043           0 :                 if( nToUnicodeStream )
    4044             :                 {
    4045           0 :                     aLine.append( "/ToUnicode " );
    4046           0 :                     aLine.append( nToUnicodeStream );
    4047           0 :                     aLine.append( " 0 R\n" );
    4048             :                 }
    4049             :                 aLine.append( ">>\n"
    4050           0 :                              "endobj\n\n" );
    4051           0 :                 if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return false;
    4052             : 
    4053           0 :                 aFontIDToObject[ lit->m_nFontID ] = nFontObject;
    4054             :             }
    4055             :             else
    4056             :             {
    4057           0 :                 const PhysicalFontFace* pFont = it->first;
    4058           0 :                 OStringBuffer aErrorComment( 256 );
    4059           0 :                 aErrorComment.append( "CreateFontSubset failed for font \"" );
    4060           0 :                 aErrorComment.append( OUStringToOString( pFont->GetFamilyName(), RTL_TEXTENCODING_UTF8 ) );
    4061           0 :                 aErrorComment.append( '\"' );
    4062           0 :                 if( pFont->GetSlant() == ITALIC_NORMAL )
    4063           0 :                     aErrorComment.append( " italic" );
    4064           0 :                 else if( pFont->GetSlant() == ITALIC_OBLIQUE )
    4065           0 :                     aErrorComment.append( " oblique" );
    4066           0 :                 aErrorComment.append( " weight=" );
    4067           0 :                 aErrorComment.append( sal_Int32(pFont->GetWeight()) );
    4068           0 :                 emitComment( aErrorComment.getStr() );
    4069             :             }
    4070           0 :         }
    4071             :     }
    4072           0 :     osl_removeFile( aTmpName.pData );
    4073             : 
    4074             :     // emit embedded fonts
    4075           0 :     for( FontEmbedData::iterator eit = m_aEmbeddedFonts.begin(); eit != m_aEmbeddedFonts.end(); ++eit )
    4076             :     {
    4077           0 :         std::map< sal_Int32, sal_Int32 > aObjects = emitEmbeddedFont( eit->first, eit->second );
    4078           0 :         for( std::map< sal_Int32, sal_Int32 >::iterator fit = aObjects.begin(); fit != aObjects.end(); ++fit )
    4079             :         {
    4080           0 :             if ( !fit->second ) return false;
    4081           0 :             aFontIDToObject[ fit->first ] = fit->second;
    4082             :         }
    4083           0 :     }
    4084             : 
    4085             :     // emit system fonts
    4086           0 :     for( FontEmbedData::iterator sit = m_aSystemFonts.begin(); sit != m_aSystemFonts.end(); ++sit )
    4087             :     {
    4088           0 :         std::map< sal_Int32, sal_Int32 > aObjects = emitSystemFont( sit->first, sit->second );
    4089           0 :         for( std::map< sal_Int32, sal_Int32 >::iterator fit = aObjects.begin(); fit != aObjects.end(); ++fit )
    4090             :         {
    4091           0 :             if ( !fit->second ) return false;
    4092           0 :             aFontIDToObject[ fit->first ] = fit->second;
    4093             :         }
    4094           0 :     }
    4095             : 
    4096           0 :     OStringBuffer aFontDict( 1024 );
    4097           0 :     aFontDict.append( getFontDictObject() );
    4098             :     aFontDict.append( " 0 obj\n"
    4099           0 :                      "<<" );
    4100           0 :     int ni = 0;
    4101           0 :     for( std::map< sal_Int32, sal_Int32 >::iterator mit = aFontIDToObject.begin(); mit != aFontIDToObject.end(); ++mit )
    4102             :     {
    4103           0 :         aFontDict.append( "/F" );
    4104           0 :         aFontDict.append( mit->first );
    4105           0 :         aFontDict.append( ' ' );
    4106           0 :         aFontDict.append( mit->second );
    4107           0 :         aFontDict.append( " 0 R" );
    4108           0 :         if( ((++ni) & 7) == 0 )
    4109           0 :             aFontDict.append( '\n' );
    4110             :     }
    4111             :     // emit builtin font for widget appearances / variable text
    4112           0 :     for( std::map< sal_Int32, sal_Int32 >::iterator it = m_aBuiltinFontToObjectMap.begin();
    4113           0 :         it != m_aBuiltinFontToObjectMap.end(); ++it )
    4114             :     {
    4115           0 :         ImplPdfBuiltinFontData aData(m_aBuiltinFonts[it->first]);
    4116           0 :         it->second = emitBuiltinFont( &aData, it->second );
    4117           0 :     }
    4118           0 :     appendBuiltinFontsToDict( aFontDict );
    4119           0 :     aFontDict.append( "\n>>\nendobj\n\n" );
    4120             : 
    4121           0 :     if ( !updateObject( getFontDictObject() ) ) return false;
    4122           0 :     if ( !writeBuffer( aFontDict.getStr(), aFontDict.getLength() ) ) return false;
    4123           0 :     return true;
    4124             : }
    4125             : 
    4126           0 : sal_Int32 PDFWriterImpl::emitResources()
    4127             : {
    4128             :     // emit shadings
    4129           0 :     if( ! m_aGradients.empty() )
    4130           0 :         CHECK_RETURN( emitGradients() );
    4131             :     // emit tilings
    4132           0 :     if( ! m_aTilings.empty() )
    4133           0 :         CHECK_RETURN( emitTilings() );
    4134             : 
    4135             :     // emit font dict
    4136           0 :     CHECK_RETURN( emitFonts() );
    4137             : 
    4138             :     // emit Resource dict
    4139           0 :     OStringBuffer aLine( 512 );
    4140           0 :     sal_Int32 nResourceDict = getResourceDictObj();
    4141           0 :     CHECK_RETURN( updateObject( nResourceDict ) );
    4142           0 :     aLine.setLength( 0 );
    4143           0 :     aLine.append( nResourceDict );
    4144           0 :     aLine.append( " 0 obj\n" );
    4145           0 :     m_aGlobalResourceDict.append( aLine, getFontDictObject() );
    4146           0 :     aLine.append( "endobj\n\n" );
    4147           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4148           0 :     return nResourceDict;
    4149             : }
    4150             : 
    4151           0 : sal_Int32 PDFWriterImpl::updateOutlineItemCount( std::vector< sal_Int32 >& rCounts,
    4152             :                                                  sal_Int32 nItemLevel,
    4153             :                                                  sal_Int32 nCurrentItemId )
    4154             : {
    4155             :     /* The /Count number of an item is
    4156             :        positive: the number of visible subitems
    4157             :        negative: the negative number of subitems that will become visible if
    4158             :                  the item gets opened
    4159             :        see PDF ref 1.4 p 478
    4160             :     */
    4161             : 
    4162           0 :     sal_Int32 nCount = 0;
    4163             : 
    4164           0 :     if( m_aContext.OpenBookmarkLevels < 0           || // all levels arevisible
    4165           0 :         m_aContext.OpenBookmarkLevels >= nItemLevel    // this level is visible
    4166             :       )
    4167             :     {
    4168           0 :         PDFOutlineEntry& rItem = m_aOutline[ nCurrentItemId ];
    4169           0 :         sal_Int32 nChildren = rItem.m_aChildren.size();
    4170           0 :         for( sal_Int32 i = 0; i < nChildren; i++ )
    4171           0 :             nCount += updateOutlineItemCount( rCounts, nItemLevel+1, rItem.m_aChildren[i] );
    4172           0 :         rCounts[nCurrentItemId] = nCount;
    4173             :         // return 1 (this item) + visible sub items
    4174           0 :         if( nCount < 0 )
    4175           0 :             nCount = 0;
    4176           0 :         nCount++;
    4177             :     }
    4178             :     else
    4179             :     {
    4180             :         // this bookmark level is invisible
    4181           0 :         PDFOutlineEntry& rItem = m_aOutline[ nCurrentItemId ];
    4182           0 :         sal_Int32 nChildren = rItem.m_aChildren.size();
    4183           0 :         rCounts[ nCurrentItemId ] = -sal_Int32(rItem.m_aChildren.size());
    4184           0 :         for( sal_Int32 i = 0; i < nChildren; i++ )
    4185           0 :             updateOutlineItemCount( rCounts, nItemLevel+1, rItem.m_aChildren[i] );
    4186           0 :         nCount = -1;
    4187             :     }
    4188             : 
    4189           0 :     return nCount;
    4190             : }
    4191             : 
    4192           0 : sal_Int32 PDFWriterImpl::emitOutline()
    4193             : {
    4194           0 :     int i, nItems = m_aOutline.size();
    4195             : 
    4196             :     // do we have an outline at all ?
    4197           0 :     if( nItems < 2 )
    4198           0 :         return 0;
    4199             : 
    4200             :     // reserve object numbers for all outline items
    4201           0 :     for( i = 0; i < nItems; ++i )
    4202           0 :         m_aOutline[i].m_nObject = createObject();
    4203             : 
    4204             :     // update all parent, next and prev object ids
    4205           0 :     for( i = 0; i < nItems; ++i )
    4206             :     {
    4207           0 :         PDFOutlineEntry& rItem = m_aOutline[i];
    4208           0 :         int nChildren = rItem.m_aChildren.size();
    4209             : 
    4210           0 :         if( nChildren )
    4211             :         {
    4212           0 :             for( int n = 0; n < nChildren; ++n )
    4213             :             {
    4214           0 :                 PDFOutlineEntry& rChild = m_aOutline[ rItem.m_aChildren[n] ];
    4215             : 
    4216           0 :                 rChild.m_nParentObject = rItem.m_nObject;
    4217           0 :                 rChild.m_nPrevObject = (n > 0) ? m_aOutline[ rItem.m_aChildren[n-1] ].m_nObject : 0;
    4218           0 :                 rChild.m_nNextObject = (n < nChildren-1) ? m_aOutline[ rItem.m_aChildren[n+1] ].m_nObject : 0;
    4219             :             }
    4220             : 
    4221             :         }
    4222             :     }
    4223             : 
    4224             :     // calculate Count entries for all items
    4225           0 :     std::vector< sal_Int32 > aCounts( nItems );
    4226           0 :     updateOutlineItemCount( aCounts, 0, 0 );
    4227             : 
    4228             :     // emit hierarchy
    4229           0 :     for( i = 0; i < nItems; ++i )
    4230             :     {
    4231           0 :         PDFOutlineEntry& rItem = m_aOutline[i];
    4232           0 :         OStringBuffer aLine( 1024 );
    4233             : 
    4234           0 :         CHECK_RETURN( updateObject( rItem.m_nObject ) );
    4235           0 :         aLine.append( rItem.m_nObject );
    4236           0 :         aLine.append( " 0 obj\n" );
    4237           0 :         aLine.append( "<<" );
    4238             :         // number of visible children (all levels)
    4239           0 :         if( i > 0 || aCounts[0] > 0 )
    4240             :         {
    4241           0 :             aLine.append( "/Count " );
    4242           0 :             aLine.append( aCounts[i] );
    4243             :         }
    4244           0 :         if( ! rItem.m_aChildren.empty() )
    4245             :         {
    4246             :             // children list: First, Last
    4247           0 :             aLine.append( "/First " );
    4248           0 :             aLine.append( m_aOutline[rItem.m_aChildren.front()].m_nObject );
    4249           0 :             aLine.append( " 0 R/Last " );
    4250           0 :             aLine.append( m_aOutline[rItem.m_aChildren.back()].m_nObject );
    4251           0 :             aLine.append( " 0 R\n" );
    4252             :         }
    4253           0 :         if( i > 0 )
    4254             :         {
    4255             :             // Title, Dest, Parent, Prev, Next
    4256           0 :             aLine.append( "/Title" );
    4257           0 :             appendUnicodeTextStringEncrypt( rItem.m_aTitle, rItem.m_nObject, aLine );
    4258           0 :             aLine.append( "\n" );
    4259             :             // Dest is not required
    4260           0 :             if( rItem.m_nDestID >= 0 && rItem.m_nDestID < (sal_Int32)m_aDests.size() )
    4261             :             {
    4262           0 :                 aLine.append( "/Dest" );
    4263           0 :                 appendDest( rItem.m_nDestID, aLine );
    4264             :             }
    4265           0 :             aLine.append( "/Parent " );
    4266           0 :             aLine.append( rItem.m_nParentObject );
    4267           0 :             aLine.append( " 0 R" );
    4268           0 :             if( rItem.m_nPrevObject )
    4269             :             {
    4270           0 :                 aLine.append( "/Prev " );
    4271           0 :                 aLine.append( rItem.m_nPrevObject );
    4272           0 :                 aLine.append( " 0 R" );
    4273             :             }
    4274           0 :             if( rItem.m_nNextObject )
    4275             :             {
    4276           0 :                 aLine.append( "/Next " );
    4277           0 :                 aLine.append( rItem.m_nNextObject );
    4278           0 :                 aLine.append( " 0 R" );
    4279             :             }
    4280             :         }
    4281           0 :         aLine.append( ">>\nendobj\n\n" );
    4282           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4283           0 :     }
    4284             : 
    4285           0 :     return m_aOutline[0].m_nObject;
    4286             : }
    4287             : 
    4288             : #undef CHECK_RETURN
    4289             : #define CHECK_RETURN( x ) if( !x ) return false
    4290             : 
    4291           0 : bool PDFWriterImpl::appendDest( sal_Int32 nDestID, OStringBuffer& rBuffer )
    4292             : {
    4293           0 :     if( nDestID < 0 || nDestID >= (sal_Int32)m_aDests.size() )
    4294             :     {
    4295             : #if OSL_DEBUG_LEVEL > 1
    4296             :         fprintf( stderr, "ERROR: invalid dest %d requested\n", (int)nDestID );
    4297             : #endif
    4298           0 :         return false;
    4299             :     }
    4300             : 
    4301           0 :     const PDFDest& rDest        = m_aDests[ nDestID ];
    4302           0 :     const PDFPage& rDestPage    = m_aPages[ rDest.m_nPage ];
    4303             : 
    4304           0 :     rBuffer.append( '[' );
    4305           0 :     rBuffer.append( rDestPage.m_nPageObject );
    4306           0 :     rBuffer.append( " 0 R" );
    4307             : 
    4308           0 :     switch( rDest.m_eType )
    4309             :     {
    4310             :         case PDFWriter::XYZ:
    4311             :         default:
    4312           0 :             rBuffer.append( "/XYZ " );
    4313           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4314           0 :             rBuffer.append( ' ' );
    4315           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4316           0 :             rBuffer.append( " 0" );
    4317           0 :             break;
    4318             :         case PDFWriter::Fit:
    4319           0 :             rBuffer.append( "/Fit" );
    4320           0 :             break;
    4321             :         case PDFWriter::FitRectangle:
    4322           0 :             rBuffer.append( "/FitR " );
    4323           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4324           0 :             rBuffer.append( ' ' );
    4325           0 :             appendFixedInt( rDest.m_aRect.Top(), rBuffer );
    4326           0 :             rBuffer.append( ' ' );
    4327           0 :             appendFixedInt( rDest.m_aRect.Right(), rBuffer );
    4328           0 :             rBuffer.append( ' ' );
    4329           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4330           0 :             break;
    4331             :         case PDFWriter::FitHorizontal:
    4332           0 :             rBuffer.append( "/FitH " );
    4333           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4334           0 :             break;
    4335             :         case PDFWriter::FitVertical:
    4336           0 :             rBuffer.append( "/FitV " );
    4337           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4338           0 :             break;
    4339             :         case PDFWriter::FitPageBoundingBox:
    4340           0 :             rBuffer.append( "/FitB" );
    4341           0 :             break;
    4342             :         case PDFWriter::FitPageBoundingBoxHorizontal:
    4343           0 :             rBuffer.append( "/FitBH " );
    4344           0 :             appendFixedInt( rDest.m_aRect.Bottom(), rBuffer );
    4345           0 :             break;
    4346             :         case PDFWriter::FitPageBoundingBoxVertical:
    4347           0 :             rBuffer.append( "/FitBV " );
    4348           0 :             appendFixedInt( rDest.m_aRect.Left(), rBuffer );
    4349           0 :             break;
    4350             :     }
    4351           0 :     rBuffer.append( ']' );
    4352             : 
    4353           0 :     return true;
    4354             : }
    4355             : 
    4356           0 : bool PDFWriterImpl::emitLinkAnnotations()
    4357             : {
    4358           0 :     int nAnnots = m_aLinks.size();
    4359           0 :     for( int i = 0; i < nAnnots; i++ )
    4360             :     {
    4361           0 :         const PDFLink& rLink            = m_aLinks[i];
    4362           0 :         if( ! updateObject( rLink.m_nObject ) )
    4363           0 :             continue;
    4364             : 
    4365           0 :         OStringBuffer aLine( 1024 );
    4366           0 :         aLine.append( rLink.m_nObject );
    4367           0 :         aLine.append( " 0 obj\n" );
    4368             : //i59651  key /F set bits Print to 1 rest to 0. We don't set NoZoom NoRotate to 1, since it's a 'should'
    4369             : // see PDF 8.4.2 and ISO 19005-1:2005 6.5.3
    4370           0 :         aLine.append( "<</Type/Annot" );
    4371           0 :         if( m_bIsPDF_A1 )
    4372           0 :             aLine.append( "/F 4" );
    4373           0 :         aLine.append( "/Subtype/Link/Border[0 0 0]/Rect[" );
    4374             : 
    4375           0 :         appendFixedInt( rLink.m_aRect.Left()-7, aLine );//the +7 to have a better shape of the border rectangle
    4376           0 :         aLine.append( ' ' );
    4377           0 :         appendFixedInt( rLink.m_aRect.Top(), aLine );
    4378           0 :         aLine.append( ' ' );
    4379           0 :         appendFixedInt( rLink.m_aRect.Right()+7, aLine );//the +7 to have a better shape of the border rectangle
    4380           0 :         aLine.append( ' ' );
    4381           0 :         appendFixedInt( rLink.m_aRect.Bottom(), aLine );
    4382           0 :         aLine.append( "]" );
    4383           0 :         if( rLink.m_nDest >= 0 )
    4384             :         {
    4385           0 :             aLine.append( "/Dest" );
    4386           0 :             appendDest( rLink.m_nDest, aLine );
    4387             :         }
    4388             :         else
    4389             :         {
    4390             : /*--->i56629
    4391             : destination is external to the document, so
    4392             : we check in the following sequence:
    4393             : 
    4394             :  if target type is neither .pdf, nor .od[tpgs], then
    4395             :           check if relative or absolute and act accordingly (use URI or 'launch application' as requested)
    4396             :                              end processing
    4397             :  else if target is .od[tpgs]: then
    4398             :       if conversion of type from od[tpgs]  to pdf is requested, convert it and this becomes the new target file
    4399             :       processing continue
    4400             : 
    4401             :  if (new)target is .pdf : then
    4402             :      if GotToR is requested, then
    4403             :            convert the target in GoToR where the fragment of the URI is
    4404             :            considered the named destination in the target file, set relative or absolute as requested
    4405             :      else strip the fragment from URL and then set URI or 'launch application' as requested
    4406             : */
    4407             : 
    4408             : // FIXME: check if the decode mechanisms for URL processing throughout this implementation
    4409             : // are the correct one!!
    4410             : 
    4411             : // extract target file type
    4412           0 :             INetURLObject aDocumentURL( m_aContext.BaseURL );
    4413           0 :             INetURLObject aTargetURL( rLink.m_aURL );
    4414           0 :             sal_Int32   nSetGoToRMode = 0;
    4415           0 :             bool    bTargetHasPDFExtension = false;
    4416           0 :             INetProtocol eTargetProtocol = aTargetURL.GetProtocol();
    4417           0 :             bool    bIsUNCPath = false;
    4418             : 
    4419             :             // check if the protocol is a known one, or if there is no protocol at all (on target only)
    4420             :             // if there is no protocol, make the target relative to the current document directory
    4421             :             // getting the needed URL information from the current document path
    4422           0 :             if( eTargetProtocol == INET_PROT_NOT_VALID )
    4423             :             {
    4424           0 :                 if( rLink.m_aURL.getLength() > 4 && rLink.m_aURL.startsWith("\\\\\\\\"))
    4425             :                 {
    4426           0 :                     bIsUNCPath = true;
    4427             :                 }
    4428             :                 else
    4429             :                 {
    4430           0 :                     INetURLObject aNewBase( aDocumentURL );//duplicate document URL
    4431           0 :                     aNewBase.removeSegment(); //remove last segment from it, obtaining the base URL of the
    4432             :                                               //target document
    4433           0 :                     aNewBase.insertName( rLink.m_aURL );
    4434           0 :                     aTargetURL = aNewBase;//reassign the new target URL
    4435             :                     //recompute the target protocol, with the new URL
    4436             :                     //normal URL processing resumes
    4437           0 :                     eTargetProtocol = aTargetURL.GetProtocol();
    4438             :                 }
    4439             :             }
    4440             : 
    4441           0 :             OUString aFileExtension = aTargetURL.GetFileExtension();
    4442             : 
    4443             :             // Check if the URL ends in '/': if yes it's a directory,
    4444             :             // it will be forced to a URI link.
    4445             :             // possibly a malformed URI, leave it as it is, force as URI
    4446           0 :             if( aTargetURL.hasFinalSlash() )
    4447           0 :                 m_aContext.DefaultLinkAction = PDFWriter::URIAction;
    4448             : 
    4449           0 :             if( !aFileExtension.isEmpty() )
    4450             :             {
    4451           0 :                 if( m_aContext.ConvertOOoTargetToPDFTarget )
    4452             :                 {
    4453           0 :                     bool bChangeFileExtensionToPDF = false;
    4454             :                     //examine the file type (.odm .odt. .odp, odg, ods)
    4455           0 :                     if( aFileExtension.equalsIgnoreAsciiCase( "odm" ) )
    4456           0 :                         bChangeFileExtensionToPDF = true;
    4457           0 :                     if( aFileExtension.equalsIgnoreAsciiCase( "odt" ) )
    4458           0 :                         bChangeFileExtensionToPDF = true;
    4459           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase( "odp" ) )
    4460           0 :                         bChangeFileExtensionToPDF = true;
    4461           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase( "odg" ) )
    4462           0 :                         bChangeFileExtensionToPDF = true;
    4463           0 :                     else if( aFileExtension.equalsIgnoreAsciiCase( "ods" ) )
    4464           0 :                         bChangeFileExtensionToPDF = true;
    4465           0 :                     if( bChangeFileExtensionToPDF )
    4466           0 :                         aTargetURL.setExtension(OUString( "pdf"  ) );
    4467             :                 }
    4468             :                 //check if extension is pdf, see if GoToR should be forced
    4469           0 :                 bTargetHasPDFExtension = aTargetURL.GetFileExtension().equalsIgnoreAsciiCase( "pdf" );
    4470           0 :                 if( m_aContext.ForcePDFAction && bTargetHasPDFExtension )
    4471           0 :                     nSetGoToRMode++;
    4472             :             }
    4473             :             //prepare the URL, if relative or not
    4474           0 :             INetProtocol eBaseProtocol = aDocumentURL.GetProtocol();
    4475             :             //queue the string common to all types of actions
    4476           0 :             aLine.append( "/A<</Type/Action/S");
    4477           0 :             if( bIsUNCPath ) // handle Win UNC paths
    4478             :             {
    4479           0 :                 aLine.append( "/Launch/Win<</F" );
    4480             :                 // INetURLObject is not good with UNC paths, use original path
    4481           0 :                 appendLiteralStringEncrypt(  rLink.m_aURL, rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4482           0 :                 aLine.append( ">>" );
    4483             :             }
    4484             :             else
    4485             :             {
    4486           0 :                 bool bSetRelative = false;
    4487           0 :                 bool bFileSpec = false;
    4488             :                 //check if relative file link is requested and if the protocol is 'file://'
    4489           0 :                 if( m_aContext.RelFsys && eBaseProtocol == eTargetProtocol && eTargetProtocol == INET_PROT_FILE )
    4490           0 :                     bSetRelative = true;
    4491             : 
    4492           0 :                 OUString aFragment = aTargetURL.GetMark( INetURLObject::NO_DECODE /*DECODE_WITH_CHARSET*/ ); //fragment as is,
    4493           0 :                 if( nSetGoToRMode == 0 )
    4494             :                 {
    4495           0 :                     switch( m_aContext.DefaultLinkAction )
    4496             :                     {
    4497             :                     default:
    4498             :                     case PDFWriter::URIAction :
    4499             :                     case PDFWriter::URIActionDestination :
    4500           0 :                         aLine.append( "/URI/URI" );
    4501           0 :                         break;
    4502             :                     case PDFWriter::LaunchAction:
    4503             :                         // now:
    4504             :                         // if a launch action is requested and the hyperlink target has a fragment
    4505             :                         // and the target file does not have a pdf extension, or it's not a 'file:://'
    4506             :                         // protocol then force the uri action on it
    4507             :                         // This code will permit the correct opening of application on web pages,
    4508             :                         // the one that normally have fragments (but I may be wrong...)
    4509             :                         // and will force the use of URI when the protocol is not file:
    4510           0 :                         if( (!aFragment.isEmpty() && !bTargetHasPDFExtension) ||
    4511             :                                         eTargetProtocol != INET_PROT_FILE )
    4512             :                         {
    4513           0 :                             aLine.append( "/URI/URI" );
    4514             :                         }
    4515             :                         else
    4516             :                         {
    4517           0 :                             aLine.append( "/Launch/F" );
    4518           0 :                             bFileSpec = true;
    4519             :                         }
    4520           0 :                         break;
    4521             :                     }
    4522             :                 }
    4523             : 
    4524             :                 //fragment are encoded in the same way as in the named destination processing
    4525           0 :                 if( nSetGoToRMode )
    4526             :                 {
    4527             :                     //add the fragment
    4528           0 :                     OUString aURLNoMark = aTargetURL.GetURLNoMark( INetURLObject::DECODE_WITH_CHARSET );
    4529           0 :                     aLine.append("/GoToR");
    4530           0 :                     aLine.append("/F");
    4531           0 :                     bFileSpec = true;
    4532             :                     appendLiteralStringEncrypt( bSetRelative ? INetURLObject::GetRelURL( m_aContext.BaseURL, aURLNoMark,
    4533             :                                                                                          INetURLObject::WAS_ENCODED,
    4534             :                                                                                          INetURLObject::DECODE_WITH_CHARSET ) :
    4535           0 :                                                                    aURLNoMark, rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4536           0 :                     if( !aFragment.isEmpty() )
    4537             :                     {
    4538           0 :                         aLine.append("/D/");
    4539           0 :                         appendDestinationName( aFragment , aLine );
    4540           0 :                     }
    4541             :                 }
    4542             :                 else
    4543             :                 {
    4544             :                     // change the fragment to accommodate the bookmark (only if the file extension
    4545             :                     // is PDF and the requested action is of the correct type)
    4546           0 :                     if(m_aContext.DefaultLinkAction == PDFWriter::URIActionDestination &&
    4547           0 :                                bTargetHasPDFExtension && !aFragment.isEmpty() )
    4548             :                     {
    4549           0 :                         OStringBuffer aLineLoc( 1024 );
    4550           0 :                         appendDestinationName( aFragment , aLineLoc );
    4551             :                         //substitute the fragment
    4552           0 :                         aTargetURL.SetMark( OStringToOUString(aLineLoc.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US) );
    4553             :                     }
    4554           0 :                     OUString aURL = aTargetURL.GetMainURL( bFileSpec ? INetURLObject::DECODE_WITH_CHARSET : INetURLObject::NO_DECODE );
    4555             :                     appendLiteralStringEncrypt(bSetRelative ? INetURLObject::GetRelURL( m_aContext.BaseURL, aURL,
    4556             :                                                                                         INetURLObject::WAS_ENCODED,
    4557             :                                                                                             bFileSpec ? INetURLObject::DECODE_WITH_CHARSET : INetURLObject::NO_DECODE
    4558             :                                                                                             ) :
    4559           0 :                                                                                aURL , rLink.m_nObject, aLine, osl_getThreadTextEncoding() );
    4560           0 :                 }
    4561             : //<--- i56629
    4562             :             }
    4563           0 :             aLine.append( ">>\n" );
    4564             :         }
    4565           0 :         if( rLink.m_nStructParent > 0 )
    4566             :         {
    4567           0 :             aLine.append( "/StructParent " );
    4568           0 :             aLine.append( rLink.m_nStructParent );
    4569             :         }
    4570           0 :         aLine.append( ">>\nendobj\n\n" );
    4571           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4572           0 :     }
    4573             : 
    4574           0 :     return true;
    4575             : }
    4576             : 
    4577           0 : bool PDFWriterImpl::emitNoteAnnotations()
    4578             : {
    4579             :     // emit note annotations
    4580           0 :     int nAnnots = m_aNotes.size();
    4581           0 :     for( int i = 0; i < nAnnots; i++ )
    4582             :     {
    4583           0 :         const PDFNoteEntry& rNote       = m_aNotes[i];
    4584           0 :         if( ! updateObject( rNote.m_nObject ) )
    4585           0 :             return false;
    4586             : 
    4587           0 :         OStringBuffer aLine( 1024 );
    4588           0 :         aLine.append( rNote.m_nObject );
    4589           0 :         aLine.append( " 0 obj\n" );
    4590             : //i59651  key /F set bits Print to 1 rest to 0. We don't set NoZoom NoRotate to 1, since it's a 'should'
    4591             : // see PDF 8.4.2 and ISO 19005-1:2005 6.5.3
    4592           0 :         aLine.append( "<</Type/Annot" );
    4593           0 :         if( m_bIsPDF_A1 )
    4594           0 :             aLine.append( "/F 4" );
    4595           0 :         aLine.append( "/Subtype/Text/Rect[" );
    4596             : 
    4597           0 :         appendFixedInt( rNote.m_aRect.Left(), aLine );
    4598           0 :         aLine.append( ' ' );
    4599           0 :         appendFixedInt( rNote.m_aRect.Top(), aLine );
    4600           0 :         aLine.append( ' ' );
    4601           0 :         appendFixedInt( rNote.m_aRect.Right(), aLine );
    4602           0 :         aLine.append( ' ' );
    4603           0 :         appendFixedInt( rNote.m_aRect.Bottom(), aLine );
    4604           0 :         aLine.append( "]" );
    4605             : 
    4606             :         // contents of the note (type text string)
    4607           0 :         aLine.append( "/Contents\n" );
    4608           0 :         appendUnicodeTextStringEncrypt( rNote.m_aContents.Contents, rNote.m_nObject, aLine );
    4609           0 :         aLine.append( "\n" );
    4610             : 
    4611             :         // optional title
    4612           0 :         if( !rNote.m_aContents.Title.isEmpty() )
    4613             :         {
    4614           0 :             aLine.append( "/T" );
    4615           0 :             appendUnicodeTextStringEncrypt( rNote.m_aContents.Title, rNote.m_nObject, aLine );
    4616           0 :             aLine.append( "\n" );
    4617             :         }
    4618             : 
    4619           0 :         aLine.append( ">>\nendobj\n\n" );
    4620           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    4621           0 :     }
    4622           0 :     return true;
    4623             : }
    4624             : 
    4625           0 : Font PDFWriterImpl::replaceFont( const vcl::Font& rControlFont, const vcl::Font&  rAppSetFont )
    4626             : {
    4627           0 :     bool bAdjustSize = false;
    4628             : 
    4629           0 :     Font aFont( rControlFont );
    4630           0 :     if( aFont.GetName().isEmpty() )
    4631             :     {
    4632           0 :         aFont = rAppSetFont;
    4633           0 :         if( rControlFont.GetHeight() )
    4634           0 :             aFont.SetSize( Size( 0, rControlFont.GetHeight() ) );
    4635             :         else
    4636           0 :             bAdjustSize = true;
    4637           0 :         if( rControlFont.GetItalic() != ITALIC_DONTKNOW )
    4638           0 :             aFont.SetItalic( rControlFont.GetItalic() );
    4639           0 :         if( rControlFont.GetWeight() != WEIGHT_DONTKNOW )
    4640           0 :             aFont.SetWeight( rControlFont.GetWeight() );
    4641             :     }
    4642           0 :     else if( ! aFont.GetHeight() )
    4643             :     {
    4644           0 :         aFont.SetSize( rAppSetFont.GetSize() );
    4645           0 :         bAdjustSize = true;
    4646             :     }
    4647           0 :     if( bAdjustSize )
    4648             :     {
    4649           0 :         Size aFontSize = aFont.GetSize();
    4650           0 :         OutputDevice* pDefDev = Application::GetDefaultDevice();
    4651           0 :         aFontSize = OutputDevice::LogicToLogic( aFontSize, pDefDev->GetMapMode(), getMapMode() );
    4652           0 :         aFont.SetSize( aFontSize );
    4653             :     }
    4654           0 :     return aFont;
    4655             : }
    4656             : 
    4657           0 : sal_Int32 PDFWriterImpl::getBestBuiltinFont( const vcl::Font& rFont )
    4658             : {
    4659           0 :     sal_Int32 nBest = 4; // default to Helvetica
    4660           0 :     OUString aFontName( rFont.GetName() );
    4661           0 :     aFontName = aFontName.toAsciiLowerCase();
    4662             : 
    4663           0 :     if( aFontName.indexOf( "times" ) != -1 )
    4664           0 :         nBest = 8;
    4665           0 :     else if( aFontName.indexOf( "courier" ) != -1 )
    4666           0 :         nBest = 0;
    4667           0 :     else if( aFontName.indexOf( "dingbats" ) != -1 )
    4668           0 :         nBest = 13;
    4669           0 :     else if( aFontName.indexOf( "symbol" ) != -1 )
    4670           0 :         nBest = 12;
    4671           0 :     if( nBest < 12 )
    4672             :     {
    4673           0 :         if( rFont.GetItalic() == ITALIC_OBLIQUE || rFont.GetItalic() == ITALIC_NORMAL )
    4674           0 :             nBest += 1;
    4675           0 :         if( rFont.GetWeight() > WEIGHT_MEDIUM )
    4676           0 :             nBest += 2;
    4677             :     }
    4678             : 
    4679           0 :     if( m_aBuiltinFontToObjectMap.find( nBest ) == m_aBuiltinFontToObjectMap.end() )
    4680           0 :         m_aBuiltinFontToObjectMap[ nBest ] = createObject();
    4681             : 
    4682           0 :     return nBest;
    4683             : }
    4684             : 
    4685           0 : static inline const Color& replaceColor( const Color& rCol1, const Color& rCol2 )
    4686             : {
    4687           0 :     return (rCol1 == Color( COL_TRANSPARENT )) ? rCol2 : rCol1;
    4688             : }
    4689             : 
    4690           0 : void PDFWriterImpl::createDefaultPushButtonAppearance( PDFWidget& rButton, const PDFWriter::PushButtonWidget& rWidget )
    4691             : {
    4692           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4693             : 
    4694             :     // save graphics state
    4695           0 :     push( PushFlags::ALL );
    4696             : 
    4697             :     // transform relative to control's coordinates since an
    4698             :     // appearance stream is a form XObject
    4699             :     // this relies on the m_aRect member of rButton NOT already being transformed
    4700             :     // to default user space
    4701           0 :     if( rWidget.Background || rWidget.Border )
    4702             :     {
    4703           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetLightColor() ) : Color( COL_TRANSPARENT ) );
    4704           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetDialogColor() ) : Color( COL_TRANSPARENT ) );
    4705           0 :         drawRectangle( rWidget.Location );
    4706             :     }
    4707             :     // prepare font to use
    4708           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetPushButtonFont() );
    4709           0 :     setFont( aFont );
    4710           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetButtonTextColor() ) );
    4711             : 
    4712           0 :     drawText( rButton.m_aRect, rButton.m_aText, rButton.m_nTextStyle );
    4713             : 
    4714             :     // create DA string while local mapmode is still in place
    4715             :     // (that is before endRedirect())
    4716           0 :     OStringBuffer aDA( 256 );
    4717           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetButtonTextColor() ), aDA );
    4718           0 :     Font aDummyFont( OUString( "Helvetica" ), aFont.GetSize() );
    4719           0 :     sal_Int32 nDummyBuiltin = getBestBuiltinFont( aDummyFont );
    4720           0 :     aDA.append( ' ' );
    4721           0 :     aDA.append( m_aBuiltinFonts[nDummyBuiltin].getNameObject() );
    4722           0 :     aDA.append( ' ' );
    4723           0 :     m_aPages[m_nCurrentPage].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4724           0 :     aDA.append( " Tf" );
    4725           0 :     rButton.m_aDAString = aDA.makeStringAndClear();
    4726             : 
    4727           0 :     pop();
    4728             : 
    4729           0 :     rButton.m_aAppearances[ "N" ][ "Standard" ] = new SvMemoryStream();
    4730             : 
    4731             :     /* seems like a bad hack but at least works in both AR5 and 6:
    4732             :        we draw the button ourselves and tell AR
    4733             :        the button would be totally transparent with no text
    4734             : 
    4735             :        One would expect that simply setting a normal appearance
    4736             :        should suffice, but no, as soon as the user actually presses
    4737             :        the button and an action is tied to it (gasp! a button that
    4738             :        does something) the appearance gets replaced by some crap that AR
    4739             :        creates on the fly even if no DA or MK is given. On AR6 at least
    4740             :        the DA and MK work as expected, but on AR5 this creates a region
    4741             :        filled with the background color but nor text. Urgh.
    4742             :     */
    4743           0 :     rButton.m_aMKDict = "/BC [] /BG [] /CA";
    4744           0 :     rButton.m_aMKDictCAString = "";
    4745           0 : }
    4746             : 
    4747           0 : Font PDFWriterImpl::drawFieldBorder( PDFWidget& rIntern,
    4748             :                                      const PDFWriter::AnyWidget& rWidget,
    4749             :                                      const StyleSettings& rSettings )
    4750             : {
    4751           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetFieldFont() );
    4752             : 
    4753           0 :     if( rWidget.Background || rWidget.Border )
    4754             :     {
    4755           0 :         if( rWidget.Border && rWidget.BorderColor == Color( COL_TRANSPARENT ) )
    4756             :         {
    4757           0 :             sal_Int32 nDelta = getReferenceDevice()->GetDPIX() / 500;
    4758           0 :             if( nDelta < 1 )
    4759           0 :                 nDelta = 1;
    4760           0 :             setLineColor( Color( COL_TRANSPARENT ) );
    4761           0 :             Rectangle aRect = rIntern.m_aRect;
    4762           0 :             setFillColor( rSettings.GetLightBorderColor() );
    4763           0 :             drawRectangle( aRect );
    4764           0 :             aRect.Left()  += nDelta; aRect.Top()     += nDelta;
    4765           0 :             aRect.Right() -= nDelta; aRect.Bottom()  -= nDelta;
    4766           0 :             setFillColor( rSettings.GetFieldColor() );
    4767           0 :             drawRectangle( aRect );
    4768           0 :             setFillColor( rSettings.GetLightColor() );
    4769           0 :             drawRectangle( Rectangle( Point( aRect.Left(), aRect.Bottom()-nDelta ), aRect.BottomRight() ) );
    4770           0 :             drawRectangle( Rectangle( Point( aRect.Right()-nDelta, aRect.Top() ), aRect.BottomRight() ) );
    4771           0 :             setFillColor( rSettings.GetDarkShadowColor() );
    4772           0 :             drawRectangle( Rectangle( aRect.TopLeft(), Point( aRect.Left()+nDelta, aRect.Bottom() ) ) );
    4773           0 :             drawRectangle( Rectangle( aRect.TopLeft(), Point( aRect.Right(), aRect.Top()+nDelta ) ) );
    4774             :         }
    4775             :         else
    4776             :         {
    4777           0 :             setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetShadowColor() ) : Color( COL_TRANSPARENT ) );
    4778           0 :             setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    4779           0 :             drawRectangle( rIntern.m_aRect );
    4780             :         }
    4781             : 
    4782           0 :         if( rWidget.Border )
    4783             :         {
    4784             :             // adjust edit area accounting for border
    4785           0 :             sal_Int32 nDelta = aFont.GetHeight()/4;
    4786           0 :             if( nDelta < 1 )
    4787           0 :                 nDelta = 1;
    4788           0 :             rIntern.m_aRect.Left()  += nDelta;
    4789           0 :             rIntern.m_aRect.Top()   += nDelta;
    4790           0 :             rIntern.m_aRect.Right() -= nDelta;
    4791           0 :             rIntern.m_aRect.Bottom()-= nDelta;
    4792             :         }
    4793             :     }
    4794           0 :     return aFont;
    4795             : }
    4796             : 
    4797           0 : void PDFWriterImpl::createDefaultEditAppearance( PDFWidget& rEdit, const PDFWriter::EditWidget& rWidget )
    4798             : {
    4799           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4800           0 :     SvMemoryStream* pEditStream = new SvMemoryStream( 1024, 1024 );
    4801             : 
    4802           0 :     push( PushFlags::ALL );
    4803             : 
    4804             :     // prepare font to use, draw field border
    4805           0 :     Font aFont = drawFieldBorder( rEdit, rWidget, rSettings );
    4806           0 :     sal_Int32 nBest = m_aContext.FieldsUseSystemFonts ? getSystemFont( aFont ): getBestBuiltinFont( aFont );
    4807             : 
    4808             :     // prepare DA string
    4809           0 :     OStringBuffer aDA( 32 );
    4810           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetFieldTextColor() ), aDA );
    4811           0 :     aDA.append( ' ' );
    4812           0 :     if( m_aContext.FieldsUseSystemFonts )
    4813             :     {
    4814           0 :         aDA.append( "/F" );
    4815           0 :         aDA.append( nBest );
    4816             : 
    4817           0 :         OStringBuffer aDR( 32 );
    4818           0 :         aDR.append( "/Font " );
    4819           0 :         aDR.append( getFontDictObject() );
    4820           0 :         aDR.append( " 0 R" );
    4821           0 :         rEdit.m_aDRDict = aDR.makeStringAndClear();
    4822             :     }
    4823             :     else
    4824           0 :         aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4825           0 :     aDA.append( ' ' );
    4826           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4827           0 :     aDA.append( " Tf" );
    4828             : 
    4829             :     /*  create an empty appearance stream, let the viewer create
    4830             :         the appearance at runtime. This is because AR5 seems to
    4831             :         paint the widget appearance always, and a dynamically created
    4832             :         appearance on top of it. AR6 is well behaved in that regard, so
    4833             :         that behaviour seems to be a bug. Anyway this empty appearance
    4834             :         relies on /NeedAppearances in the AcroForm dictionary set to "true"
    4835             :      */
    4836           0 :     beginRedirect( pEditStream, rEdit.m_aRect );
    4837           0 :     OStringBuffer aAppearance( 32 );
    4838           0 :     aAppearance.append( "/Tx BMC\nEMC\n" );
    4839           0 :     writeBuffer( aAppearance.getStr(), aAppearance.getLength() );
    4840             : 
    4841           0 :     endRedirect();
    4842           0 :     pop();
    4843             : 
    4844           0 :     rEdit.m_aAppearances[ "N" ][ "Standard" ] = pEditStream;
    4845             : 
    4846           0 :     rEdit.m_aDAString = aDA.makeStringAndClear();
    4847           0 : }
    4848             : 
    4849           0 : void PDFWriterImpl::createDefaultListBoxAppearance( PDFWidget& rBox, const PDFWriter::ListBoxWidget& rWidget )
    4850             : {
    4851           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4852           0 :     SvMemoryStream* pListBoxStream = new SvMemoryStream( 1024, 1024 );
    4853             : 
    4854           0 :     push( PushFlags::ALL );
    4855             : 
    4856             :     // prepare font to use, draw field border
    4857           0 :     Font aFont = drawFieldBorder( rBox, rWidget, rSettings );
    4858           0 :     sal_Int32 nBest = m_aContext.FieldsUseSystemFonts ? getSystemFont( aFont ): getBestBuiltinFont( aFont );
    4859             : 
    4860           0 :     beginRedirect( pListBoxStream, rBox.m_aRect );
    4861           0 :     OStringBuffer aAppearance( 64 );
    4862             : 
    4863           0 :     setLineColor( Color( COL_TRANSPARENT ) );
    4864           0 :     setFillColor( replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) );
    4865           0 :     drawRectangle( rBox.m_aRect );
    4866             : 
    4867             :     // empty appearance, see createDefaultEditAppearance for reference
    4868           0 :     aAppearance.append( "/Tx BMC\nEMC\n" );
    4869           0 :     writeBuffer( aAppearance.getStr(), aAppearance.getLength() );
    4870             : 
    4871           0 :     endRedirect();
    4872           0 :     pop();
    4873             : 
    4874           0 :     rBox.m_aAppearances[ "N" ][ "Standard" ] = pListBoxStream;
    4875             : 
    4876             :     // prepare DA string
    4877           0 :     OStringBuffer aDA( 256 );
    4878             :     // prepare DA string
    4879           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetFieldTextColor() ), aDA );
    4880           0 :     aDA.append( ' ' );
    4881           0 :     if( m_aContext.FieldsUseSystemFonts )
    4882             :     {
    4883           0 :         aDA.append( "/F" );
    4884           0 :         aDA.append( nBest );
    4885             : 
    4886           0 :         OStringBuffer aDR( 32 );
    4887           0 :         aDR.append( "/Font " );
    4888           0 :         aDR.append( getFontDictObject() );
    4889           0 :         aDR.append( " 0 R" );
    4890           0 :         rBox.m_aDRDict = aDR.makeStringAndClear();
    4891             :     }
    4892             :     else
    4893           0 :         aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4894           0 :     aDA.append( ' ' );
    4895           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aFont.GetHeight() ), aDA );
    4896           0 :     aDA.append( " Tf" );
    4897           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    4898           0 : }
    4899             : 
    4900           0 : void PDFWriterImpl::createDefaultCheckBoxAppearance( PDFWidget& rBox, const PDFWriter::CheckBoxWidget& rWidget )
    4901             : {
    4902           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    4903             : 
    4904             :     // save graphics state
    4905           0 :     push( PushFlags::ALL );
    4906             : 
    4907           0 :     if( rWidget.Background || rWidget.Border )
    4908             :     {
    4909           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetCheckedColor() ) : Color( COL_TRANSPARENT ) );
    4910           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    4911           0 :         drawRectangle( rBox.m_aRect );
    4912             :     }
    4913             : 
    4914           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetRadioCheckFont() );
    4915           0 :     setFont( aFont );
    4916           0 :     Size aFontSize = aFont.GetSize();
    4917           0 :     if( aFontSize.Height() > rBox.m_aRect.GetHeight() )
    4918           0 :         aFontSize.Height() = rBox.m_aRect.GetHeight();
    4919           0 :     sal_Int32 nDelta = aFontSize.Height()/10;
    4920           0 :     if( nDelta < 1 )
    4921           0 :         nDelta = 1;
    4922             : 
    4923           0 :     Rectangle aCheckRect, aTextRect;
    4924           0 :     if( rWidget.ButtonIsLeft )
    4925             :     {
    4926           0 :         aCheckRect.Left()   = rBox.m_aRect.Left() + nDelta;
    4927           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    4928           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    4929           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    4930             : 
    4931             :         // #i74206# handle small controls without text area
    4932           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    4933             :         {
    4934           0 :             aCheckRect.Right()  -= nDelta;
    4935           0 :             aCheckRect.Top()    += nDelta/2;
    4936           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    4937             :         }
    4938             : 
    4939           0 :         aTextRect.Left()    = rBox.m_aRect.Left() + aCheckRect.GetWidth()+5*nDelta;
    4940           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    4941           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    4942           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    4943             :     }
    4944             :     else
    4945             :     {
    4946           0 :         aCheckRect.Left()   = rBox.m_aRect.Right() - nDelta - aFontSize.Height();
    4947           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    4948           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    4949           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    4950             : 
    4951             :         // #i74206# handle small controls without text area
    4952           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    4953             :         {
    4954           0 :             aCheckRect.Left()   += nDelta;
    4955           0 :             aCheckRect.Top()    += nDelta/2;
    4956           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    4957             :         }
    4958             : 
    4959           0 :         aTextRect.Left()    = rBox.m_aRect.Left();
    4960           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    4961           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    4962           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    4963             :     }
    4964           0 :     setLineColor( Color( COL_BLACK ) );
    4965           0 :     setFillColor( Color( COL_TRANSPARENT ) );
    4966           0 :     OStringBuffer aLW( 32 );
    4967           0 :     aLW.append( "q " );
    4968           0 :     m_aPages[m_nCurrentPage].appendMappedLength( nDelta, aLW );
    4969           0 :     aLW.append( " w " );
    4970           0 :     writeBuffer( aLW.getStr(), aLW.getLength() );
    4971           0 :     drawRectangle( aCheckRect );
    4972           0 :     writeBuffer( " Q\n", 3 );
    4973           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    4974           0 :     drawText( aTextRect, rBox.m_aText, rBox.m_nTextStyle );
    4975             : 
    4976           0 :     pop();
    4977             : 
    4978           0 :     OStringBuffer aDA( 256 );
    4979           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    4980           0 :     sal_Int32 nBest = getBestBuiltinFont( Font( OUString( "ZapfDingbats" ), aFont.GetSize() ) );
    4981           0 :     aDA.append( ' ' );
    4982           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    4983           0 :     aDA.append( " 0 Tf" );
    4984           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    4985           0 :     rBox.m_aMKDict = "/CA";
    4986           0 :     rBox.m_aMKDictCAString = "8";
    4987           0 :     rBox.m_aRect = aCheckRect;
    4988             : 
    4989             :     // create appearance streams
    4990           0 :     sal_Char cMark = '8';
    4991           0 :     sal_Int32 nCharXOffset = 1000-m_aBuiltinFonts[13].m_aWidths[sal_Int32(cMark)];
    4992           0 :     nCharXOffset *= aCheckRect.GetHeight();
    4993           0 :     nCharXOffset /= 2000;
    4994             :     sal_Int32 nCharYOffset = 1000-
    4995           0 :         (m_aBuiltinFonts[13].m_nAscent+m_aBuiltinFonts[13].m_nDescent); // descent is negative
    4996           0 :     nCharYOffset *= aCheckRect.GetHeight();
    4997           0 :     nCharYOffset /= 2000;
    4998             : 
    4999           0 :     SvMemoryStream* pCheckStream = new SvMemoryStream( 256, 256 );
    5000           0 :     beginRedirect( pCheckStream, aCheckRect );
    5001           0 :     aDA.append( "/Tx BMC\nq BT\n" );
    5002           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5003           0 :     aDA.append( ' ' );
    5004           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5005           0 :     aDA.append( ' ' );
    5006           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( sal_Int32( aCheckRect.GetHeight() ), aDA );
    5007           0 :     aDA.append( " Tf\n" );
    5008           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nCharXOffset, aDA );
    5009           0 :     aDA.append( " " );
    5010           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nCharYOffset, aDA );
    5011           0 :     aDA.append( " Td (" );
    5012           0 :     aDA.append( cMark );
    5013           0 :     aDA.append( ") Tj\nET\nQ\nEMC\n" );
    5014           0 :     writeBuffer( aDA.getStr(), aDA.getLength() );
    5015           0 :     endRedirect();
    5016           0 :     rBox.m_aAppearances[ "N" ][ "Yes" ] = pCheckStream;
    5017             : 
    5018           0 :     SvMemoryStream* pUncheckStream = new SvMemoryStream( 256, 256 );
    5019           0 :     beginRedirect( pUncheckStream, aCheckRect );
    5020           0 :     writeBuffer( "/Tx BMC\nEMC\n", 12 );
    5021           0 :     endRedirect();
    5022           0 :     rBox.m_aAppearances[ "N" ][ "Off" ] = pUncheckStream;
    5023           0 : }
    5024             : 
    5025           0 : void PDFWriterImpl::createDefaultRadioButtonAppearance( PDFWidget& rBox, const PDFWriter::RadioButtonWidget& rWidget )
    5026             : {
    5027           0 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
    5028             : 
    5029             :     // save graphics state
    5030           0 :     push( PushFlags::ALL );
    5031             : 
    5032           0 :     if( rWidget.Background || rWidget.Border )
    5033             :     {
    5034           0 :         setLineColor( rWidget.Border ? replaceColor( rWidget.BorderColor, rSettings.GetCheckedColor() ) : Color( COL_TRANSPARENT ) );
    5035           0 :         setFillColor( rWidget.Background ? replaceColor( rWidget.BackgroundColor, rSettings.GetFieldColor() ) : Color( COL_TRANSPARENT ) );
    5036           0 :         drawRectangle( rBox.m_aRect );
    5037             :     }
    5038             : 
    5039           0 :     Font aFont = replaceFont( rWidget.TextFont, rSettings.GetRadioCheckFont() );
    5040           0 :     setFont( aFont );
    5041           0 :     Size aFontSize = aFont.GetSize();
    5042           0 :     if( aFontSize.Height() > rBox.m_aRect.GetHeight() )
    5043           0 :         aFontSize.Height() = rBox.m_aRect.GetHeight();
    5044           0 :     sal_Int32 nDelta = aFontSize.Height()/10;
    5045           0 :     if( nDelta < 1 )
    5046           0 :         nDelta = 1;
    5047             : 
    5048           0 :     Rectangle aCheckRect, aTextRect;
    5049           0 :     if( rWidget.ButtonIsLeft )
    5050             :     {
    5051           0 :         aCheckRect.Left()   = rBox.m_aRect.Left() + nDelta;
    5052           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    5053           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    5054           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    5055             : 
    5056             :         // #i74206# handle small controls without text area
    5057           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    5058             :         {
    5059           0 :             aCheckRect.Right()  -= nDelta;
    5060           0 :             aCheckRect.Top()    += nDelta/2;
    5061           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    5062             :         }
    5063             : 
    5064           0 :         aTextRect.Left()    = rBox.m_aRect.Left() + aCheckRect.GetWidth()+5*nDelta;
    5065           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    5066           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    5067           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    5068             :     }
    5069             :     else
    5070             :     {
    5071           0 :         aCheckRect.Left()   = rBox.m_aRect.Right() - nDelta - aFontSize.Height();
    5072           0 :         aCheckRect.Top()    = rBox.m_aRect.Top() + (rBox.m_aRect.GetHeight()-aFontSize.Height())/2;
    5073           0 :         aCheckRect.Right()  = aCheckRect.Left() + aFontSize.Height();
    5074           0 :         aCheckRect.Bottom() = aCheckRect.Top() + aFontSize.Height();
    5075             : 
    5076             :         // #i74206# handle small controls without text area
    5077           0 :         while( aCheckRect.GetWidth() > rBox.m_aRect.GetWidth() && aCheckRect.GetWidth() > nDelta )
    5078             :         {
    5079           0 :             aCheckRect.Left()   += nDelta;
    5080           0 :             aCheckRect.Top()    += nDelta/2;
    5081           0 :             aCheckRect.Bottom() -= nDelta - (nDelta/2);
    5082             :         }
    5083             : 
    5084           0 :         aTextRect.Left()    = rBox.m_aRect.Left();
    5085           0 :         aTextRect.Top()     = rBox.m_aRect.Top();
    5086           0 :         aTextRect.Right()   = aTextRect.Left() + rBox.m_aRect.GetWidth() - aCheckRect.GetWidth()-6*nDelta;
    5087           0 :         aTextRect.Bottom()  = rBox.m_aRect.Bottom();
    5088             :     }
    5089           0 :     setLineColor( Color( COL_BLACK ) );
    5090           0 :     setFillColor( Color( COL_TRANSPARENT ) );
    5091           0 :     OStringBuffer aLW( 32 );
    5092           0 :     aLW.append( "q " );
    5093           0 :     m_aPages[ m_nCurrentPage ].appendMappedLength( nDelta, aLW );
    5094           0 :     aLW.append( " w " );
    5095           0 :     writeBuffer( aLW.getStr(), aLW.getLength() );
    5096           0 :     drawEllipse( aCheckRect );
    5097           0 :     writeBuffer( " Q\n", 3 );
    5098           0 :     setTextColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    5099           0 :     drawText( aTextRect, rBox.m_aText, rBox.m_nTextStyle );
    5100             : 
    5101           0 :     pop();
    5102             : 
    5103           0 :     OStringBuffer aDA( 256 );
    5104           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5105           0 :     sal_Int32 nBest = getBestBuiltinFont( Font( OUString( "ZapfDingbats" ), aFont.GetSize() ) );
    5106           0 :     aDA.append( ' ' );
    5107           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5108           0 :     aDA.append( " 0 Tf" );
    5109           0 :     rBox.m_aDAString = aDA.makeStringAndClear();
    5110             :     //to encrypt this (el)
    5111           0 :     rBox.m_aMKDict = "/CA";
    5112             :     //after this assignement, to m_aMKDic cannot be added anything
    5113           0 :     rBox.m_aMKDictCAString = "l";
    5114             : 
    5115           0 :     rBox.m_aRect = aCheckRect;
    5116             : 
    5117             :     // create appearance streams
    5118           0 :     push( PushFlags::ALL);
    5119           0 :     SvMemoryStream* pCheckStream = new SvMemoryStream( 256, 256 );
    5120             : 
    5121           0 :     beginRedirect( pCheckStream, aCheckRect );
    5122           0 :     aDA.append( "/Tx BMC\nq BT\n" );
    5123           0 :     appendNonStrokingColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ), aDA );
    5124           0 :     aDA.append( ' ' );
    5125           0 :     aDA.append( m_aBuiltinFonts[nBest].getNameObject() );
    5126           0 :     aDA.append( ' ' );
    5127           0 :     m_aPages[m_nCurrentPage].appendMappedLength( sal_Int32( aCheckRect.GetHeight() ), aDA );
    5128           0 :     aDA.append( " Tf\n0 0 Td\nET\nQ\n" );
    5129           0 :     writeBuffer( aDA.getStr(), aDA.getLength() );
    5130           0 :     setFillColor( replaceColor( rWidget.TextColor, rSettings.GetRadioCheckTextColor() ) );
    5131           0 :     setLineColor( Color( COL_TRANSPARENT ) );
    5132           0 :     aCheckRect.Left()   += 3*nDelta;
    5133           0 :     aCheckRect.Top()    += 3*nDelta;
    5134           0 :     aCheckRect.Bottom() -= 3*nDelta;
    5135           0 :     aCheckRect.Right()  -= 3*nDelta;
    5136           0 :     drawEllipse( aCheckRect );
    5137           0 :     writeBuffer( "\nEMC\n", 5 );
    5138           0 :     endRedirect();
    5139             : 
    5140           0 :     pop();
    5141           0 :     rBox.m_aAppearances[ "N" ][ "Yes" ] = pCheckStream;
    5142             : 
    5143           0 :     SvMemoryStream* pUncheckStream = new SvMemoryStream( 256, 256 );
    5144           0 :     beginRedirect( pUncheckStream, aCheckRect );
    5145           0 :     writeBuffer( "/Tx BMC\nEMC\n", 12 );
    5146           0 :     endRedirect();
    5147           0 :     rBox.m_aAppearances[ "N" ][ "Off" ] = pUncheckStream;
    5148           0 : }
    5149             : 
    5150           0 : bool PDFWriterImpl::emitAppearances( PDFWidget& rWidget, OStringBuffer& rAnnotDict )
    5151             : {
    5152             :     // TODO: check and insert default streams
    5153           0 :     OString aStandardAppearance;
    5154           0 :     switch( rWidget.m_eType )
    5155             :     {
    5156             :         case PDFWriter::CheckBox:
    5157           0 :             aStandardAppearance = OUStringToOString( rWidget.m_aValue, RTL_TEXTENCODING_ASCII_US );
    5158           0 :             break;
    5159             :         default:
    5160           0 :             break;
    5161             :     }
    5162             : 
    5163           0 :     if( rWidget.m_aAppearances.size() )
    5164             :     {
    5165           0 :         rAnnotDict.append( "/AP<<\n" );
    5166           0 :         for( PDFAppearanceMap::iterator dict_it = rWidget.m_aAppearances.begin(); dict_it != rWidget.m_aAppearances.end(); ++dict_it )
    5167             :         {
    5168           0 :             rAnnotDict.append( "/" );
    5169           0 :             rAnnotDict.append( dict_it->first );
    5170           0 :             bool bUseSubDict = (dict_it->second.size() > 1);
    5171           0 :             rAnnotDict.append( bUseSubDict ? "<<" : " " );
    5172             : 
    5173           0 :             for( PDFAppearanceStreams::const_iterator stream_it = dict_it->second.begin();
    5174           0 :                  stream_it != dict_it->second.end(); ++stream_it )
    5175             :             {
    5176           0 :                 SvMemoryStream* pApppearanceStream = stream_it->second;
    5177           0 :                 dict_it->second[ stream_it->first ] = NULL;
    5178             : 
    5179           0 :                 bool bDeflate = compressStream( pApppearanceStream );
    5180             : 
    5181           0 :                 pApppearanceStream->Seek( STREAM_SEEK_TO_END );
    5182           0 :                 sal_Int64 nStreamLen = pApppearanceStream->Tell();
    5183           0 :                 pApppearanceStream->Seek( STREAM_SEEK_TO_BEGIN );
    5184           0 :                 sal_Int32 nObject = createObject();
    5185           0 :                 CHECK_RETURN( updateObject( nObject ) );
    5186             :                 #if OSL_DEBUG_LEVEL > 1
    5187             :                 emitComment( "PDFWriterImpl::emitAppearances" );
    5188             :                 #endif
    5189           0 :                 OStringBuffer aLine;
    5190           0 :                 aLine.append( nObject );
    5191             : 
    5192             :                 aLine.append( " 0 obj\n"
    5193             :                               "<</Type/XObject\n"
    5194             :                               "/Subtype/Form\n"
    5195           0 :                               "/BBox[0 0 " );
    5196           0 :                 appendFixedInt( rWidget.m_aRect.GetWidth()-1, aLine );
    5197           0 :                 aLine.append( " " );
    5198           0 :                 appendFixedInt( rWidget.m_aRect.GetHeight()-1, aLine );
    5199             :                 aLine.append( "]\n"
    5200           0 :                               "/Resources " );
    5201           0 :                 aLine.append( getResourceDictObj() );
    5202             :                 aLine.append( " 0 R\n"
    5203           0 :                               "/Length " );
    5204           0 :                 aLine.append( nStreamLen );
    5205           0 :                 aLine.append( "\n" );
    5206           0 :                 if( bDeflate )
    5207           0 :                     aLine.append( "/Filter/FlateDecode\n" );
    5208           0 :                 aLine.append( ">>\nstream\n" );
    5209           0 :                 CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5210           0 :                 checkAndEnableStreamEncryption( nObject );
    5211           0 :                 CHECK_RETURN( writeBuffer( pApppearanceStream->GetData(), nStreamLen ) );
    5212           0 :                 disableStreamEncryption();
    5213           0 :                 CHECK_RETURN( writeBuffer( "\nendstream\nendobj\n\n", 19 ) );
    5214             : 
    5215           0 :                 if( bUseSubDict )
    5216             :                 {
    5217           0 :                     rAnnotDict.append( " /" );
    5218           0 :                     rAnnotDict.append( stream_it->first );
    5219           0 :                     rAnnotDict.append( " " );
    5220             :                 }
    5221           0 :                 rAnnotDict.append( nObject );
    5222           0 :                 rAnnotDict.append( " 0 R" );
    5223             : 
    5224           0 :                 delete pApppearanceStream;
    5225           0 :             }
    5226             : 
    5227           0 :             rAnnotDict.append( bUseSubDict ? ">>\n" : "\n" );
    5228             :         }
    5229           0 :         rAnnotDict.append( ">>\n" );
    5230           0 :         if( !aStandardAppearance.isEmpty() )
    5231             :         {
    5232           0 :             rAnnotDict.append( "/AS /" );
    5233           0 :             rAnnotDict.append( aStandardAppearance );
    5234           0 :             rAnnotDict.append( "\n" );
    5235             :         }
    5236             :     }
    5237             : 
    5238           0 :     return true;
    5239             : }
    5240             : 
    5241           0 : bool PDFWriterImpl::emitWidgetAnnotations()
    5242             : {
    5243           0 :     ensureUniqueRadioOnValues();
    5244             : 
    5245           0 :     int nAnnots = m_aWidgets.size();
    5246           0 :     for( int a = 0; a < nAnnots; a++ )
    5247             :     {
    5248           0 :         PDFWidget& rWidget = m_aWidgets[a];
    5249             : 
    5250           0 :         OStringBuffer aLine( 1024 );
    5251           0 :         OStringBuffer aValue( 256 );
    5252           0 :         aLine.append( rWidget.m_nObject );
    5253             :         aLine.append( " 0 obj\n"
    5254           0 :                       "<<" );
    5255           0 :         if( rWidget.m_eType != PDFWriter::Hierarchy )
    5256             :         {
    5257             :             // emit widget annotation only for terminal fields
    5258           0 :             if( rWidget.m_aKids.empty() )
    5259             :             {
    5260             :                 int iRectMargin;
    5261             : 
    5262           0 :                 aLine.append( "/Type/Annot/Subtype/Widget/F " );
    5263             : 
    5264           0 :                 if (rWidget.m_eType == PDFWriter::Signature)
    5265             :                 {
    5266           0 :                     aLine.append( "132\n" ); // Print & Locked
    5267           0 :                     iRectMargin = 0;
    5268             :                 }
    5269             :                 else
    5270             :                 {
    5271           0 :                     aLine.append( "4\n" );
    5272           0 :                     iRectMargin = 1;
    5273             :                 }
    5274             : 
    5275           0 :                 aLine.append("/Rect[" );
    5276           0 :                 appendFixedInt( rWidget.m_aRect.Left()-iRectMargin, aLine );
    5277           0 :                 aLine.append( ' ' );
    5278           0 :                 appendFixedInt( rWidget.m_aRect.Top()+iRectMargin, aLine );
    5279           0 :                 aLine.append( ' ' );
    5280           0 :                 appendFixedInt( rWidget.m_aRect.Right()+iRectMargin, aLine );
    5281           0 :                 aLine.append( ' ' );
    5282           0 :                 appendFixedInt( rWidget.m_aRect.Bottom()-iRectMargin, aLine );
    5283           0 :                 aLine.append( "]\n" );
    5284             :             }
    5285           0 :             aLine.append( "/FT/" );
    5286           0 :             switch( rWidget.m_eType )
    5287             :             {
    5288             :                 case PDFWriter::RadioButton:
    5289             :                 case PDFWriter::CheckBox:
    5290             :                     // for radio buttons only the RadioButton field, not the
    5291             :                     // CheckBox children should have a value, else acrobat reader
    5292             :                     // does not always check the right button
    5293             :                     // of course real check boxes (not belonging to a radio group)
    5294             :                     // need their values, too
    5295           0 :                     if( rWidget.m_eType == PDFWriter::RadioButton || rWidget.m_nRadioGroup < 0 )
    5296             :                     {
    5297           0 :                         aValue.append( "/" );
    5298             :                         // check for radio group with all buttons unpressed
    5299           0 :                         if( rWidget.m_aValue.isEmpty() )
    5300           0 :                             aValue.append( "Off" );
    5301             :                         else
    5302           0 :                             appendName( rWidget.m_aValue, aValue );
    5303             :                     }
    5304             :                     // fall-through
    5305             :                 case PDFWriter::PushButton:
    5306           0 :                     aLine.append( "Btn" );
    5307           0 :                     break;
    5308             :                 case PDFWriter::ListBox:
    5309           0 :                     if( rWidget.m_nFlags & 0x200000 ) // multiselect
    5310             :                     {
    5311           0 :                         aValue.append( "[" );
    5312           0 :                         for( unsigned int i = 0; i < rWidget.m_aSelectedEntries.size(); i++ )
    5313             :                         {
    5314           0 :                             sal_Int32 nEntry = rWidget.m_aSelectedEntries[i];
    5315           0 :                             if( nEntry >= 0 && nEntry < sal_Int32(rWidget.m_aListEntries.size()) )
    5316           0 :                                 appendUnicodeTextStringEncrypt( rWidget.m_aListEntries[ nEntry ], rWidget.m_nObject, aValue );
    5317             :                         }
    5318           0 :                         aValue.append( "]" );
    5319             :                     }
    5320           0 :                     else if( rWidget.m_aSelectedEntries.size() > 0 &&
    5321           0 :                              rWidget.m_aSelectedEntries[0] >= 0 &&
    5322           0 :                              rWidget.m_aSelectedEntries[0] < sal_Int32(rWidget.m_aListEntries.size()) )
    5323             :                     {
    5324           0 :                         appendUnicodeTextStringEncrypt( rWidget.m_aListEntries[ rWidget.m_aSelectedEntries[0] ], rWidget.m_nObject, aValue );
    5325             :                     }
    5326             :                     else
    5327           0 :                         appendUnicodeTextStringEncrypt( OUString(), rWidget.m_nObject, aValue );
    5328           0 :                     aLine.append( "Ch" );
    5329           0 :                     break;
    5330             :                 case PDFWriter::ComboBox:
    5331           0 :                     appendUnicodeTextStringEncrypt( rWidget.m_aValue, rWidget.m_nObject, aValue );
    5332           0 :                     aLine.append( "Ch" );
    5333           0 :                     break;
    5334             :                 case PDFWriter::Edit:
    5335           0 :                     aLine.append( "Tx" );
    5336           0 :                     appendUnicodeTextStringEncrypt( rWidget.m_aValue, rWidget.m_nObject, aValue );
    5337           0 :                     break;
    5338             :                 case PDFWriter::Signature:
    5339           0 :                     aLine.append( "Sig" );
    5340           0 :                     aValue.append(OUStringToOString(rWidget.m_aValue, RTL_TEXTENCODING_ASCII_US));
    5341           0 :                     break;
    5342             :                 case PDFWriter::Hierarchy: // make the compiler happy
    5343           0 :                     break;
    5344             :             }
    5345           0 :             aLine.append( "\n" );
    5346           0 :             aLine.append( "/P " );
    5347           0 :             aLine.append( m_aPages[ rWidget.m_nPage ].m_nPageObject );
    5348           0 :             aLine.append( " 0 R\n" );
    5349             :         }
    5350           0 :         if( rWidget.m_nParent )
    5351             :         {
    5352           0 :             aLine.append( "/Parent " );
    5353           0 :             aLine.append( rWidget.m_nParent );
    5354           0 :             aLine.append( " 0 R\n" );
    5355             :         }
    5356           0 :         if( rWidget.m_aKids.size() )
    5357             :         {
    5358           0 :             aLine.append( "/Kids[" );
    5359           0 :             for( unsigned int i = 0; i < rWidget.m_aKids.size(); i++ )
    5360             :             {
    5361           0 :                 aLine.append( rWidget.m_aKids[i] );
    5362           0 :                 aLine.append( " 0 R" );
    5363           0 :                 aLine.append( ( (i&15) == 15 ) ? "\n" : " " );
    5364             :             }
    5365           0 :             aLine.append( "]\n" );
    5366             :         }
    5367           0 :         if( !rWidget.m_aName.isEmpty() )
    5368             :         {
    5369           0 :             aLine.append( "/T" );
    5370           0 :             appendLiteralStringEncrypt( rWidget.m_aName, rWidget.m_nObject, aLine );
    5371           0 :             aLine.append( "\n" );
    5372             :         }
    5373           0 :         if( m_aContext.Version > PDFWriter::PDF_1_2 && !rWidget.m_aDescription.isEmpty() )
    5374             :         {
    5375             :             // the alternate field name should be unicode able since it is
    5376             :             // supposed to be used in UI
    5377           0 :             aLine.append( "/TU" );
    5378           0 :             appendUnicodeTextStringEncrypt( rWidget.m_aDescription, rWidget.m_nObject, aLine );
    5379           0 :             aLine.append( "\n" );
    5380             :         }
    5381             : 
    5382           0 :         if( rWidget.m_nFlags )
    5383             :         {
    5384           0 :             aLine.append( "/Ff " );
    5385           0 :             aLine.append( rWidget.m_nFlags );
    5386           0 :             aLine.append( "\n" );
    5387             :         }
    5388           0 :         if( !aValue.isEmpty() )
    5389             :         {
    5390           0 :             OString aVal = aValue.makeStringAndClear();
    5391           0 :             aLine.append( "/V " );
    5392           0 :             aLine.append( aVal );
    5393             :             aLine.append( "\n"
    5394           0 :                           "/DV " );
    5395           0 :             aLine.append( aVal );
    5396           0 :             aLine.append( "\n" );
    5397             :         }
    5398           0 :         if( rWidget.m_eType == PDFWriter::ListBox || rWidget.m_eType == PDFWriter::ComboBox )
    5399             :         {
    5400           0 :             sal_Int32 nTI = -1;
    5401           0 :             aLine.append( "/Opt[\n" );
    5402           0 :             sal_Int32 i = 0;
    5403           0 :             for( std::vector< OUString >::const_iterator it = rWidget.m_aListEntries.begin(); it != rWidget.m_aListEntries.end(); ++it, ++i )
    5404             :             {
    5405           0 :                 appendUnicodeTextStringEncrypt( *it, rWidget.m_nObject, aLine );
    5406           0 :                 aLine.append( "\n" );
    5407           0 :                 if( *it == rWidget.m_aValue )
    5408           0 :                     nTI = i;
    5409             :             }
    5410           0 :             aLine.append( "]\n" );
    5411           0 :             if( nTI > 0 )
    5412             :             {
    5413           0 :                 aLine.append( "/TI " );
    5414           0 :                 aLine.append( nTI );
    5415           0 :                 aLine.append( "\n" );
    5416           0 :                 if( rWidget.m_nFlags & 0x200000 ) // Multiselect
    5417             :                 {
    5418           0 :                     aLine.append( "/I [" );
    5419           0 :                     aLine.append( nTI );
    5420           0 :                     aLine.append( "]\n" );
    5421             :                 }
    5422             :             }
    5423             :         }
    5424           0 :         if( rWidget.m_eType == PDFWriter::Edit && rWidget.m_nMaxLen > 0 )
    5425             :         {
    5426           0 :             aLine.append( "/MaxLen " );
    5427           0 :             aLine.append( rWidget.m_nMaxLen );
    5428           0 :             aLine.append( "\n" );
    5429             :         }
    5430           0 :         if( rWidget.m_eType == PDFWriter::PushButton )
    5431             :         {
    5432           0 :             if(!m_bIsPDF_A1)
    5433             :             {
    5434           0 :                 OStringBuffer aDest;
    5435           0 :                 if( rWidget.m_nDest != -1 && appendDest( m_aDestinationIdTranslation[ rWidget.m_nDest ], aDest ) )
    5436             :                 {
    5437           0 :                     aLine.append( "/AA<</D<</Type/Action/S/GoTo/D " );
    5438           0 :                     aLine.append( aDest.makeStringAndClear() );
    5439           0 :                     aLine.append( ">>>>\n" );
    5440             :                 }
    5441           0 :                 else if( rWidget.m_aListEntries.empty() )
    5442             :                 {
    5443             :                     // create a reset form action
    5444           0 :                     aLine.append( "/AA<</D<</Type/Action/S/ResetForm>>>>\n" );
    5445             :                 }
    5446           0 :                 else if( rWidget.m_bSubmit )
    5447             :                 {
    5448             :                     // create a submit form action
    5449           0 :                     aLine.append( "/AA<</D<</Type/Action/S/SubmitForm/F" );
    5450           0 :                     appendLiteralStringEncrypt( rWidget.m_aListEntries.front(), rWidget.m_nObject, aLine, osl_getThreadTextEncoding() );
    5451           0 :                     aLine.append( "/Flags " );
    5452             : 
    5453           0 :                     sal_Int32 nFlags = 0;
    5454           0 :                     switch( m_aContext.SubmitFormat )
    5455             :                     {
    5456             :                     case PDFWriter::HTML:
    5457           0 :                         nFlags |= 4;
    5458           0 :                         break;
    5459             :                     case PDFWriter::XML:
    5460           0 :                         if( m_aContext.Version > PDFWriter::PDF_1_3 )
    5461           0 :                             nFlags |= 32;
    5462           0 :                         break;
    5463             :                     case PDFWriter::PDF:
    5464           0 :                         if( m_aContext.Version > PDFWriter::PDF_1_3 )
    5465           0 :                             nFlags |= 256;
    5466           0 :                         break;
    5467             :                     case PDFWriter::FDF:
    5468             :                     default:
    5469           0 :                         break;
    5470             :                     }
    5471           0 :                     if( rWidget.m_bSubmitGet )
    5472           0 :                         nFlags |= 8;
    5473           0 :                     aLine.append( nFlags );
    5474           0 :                     aLine.append( ">>>>\n" );
    5475             :                 }
    5476             :                 else
    5477             :                 {
    5478             :                     // create a URI action
    5479           0 :                     aLine.append( "/AA<</D<</Type/Action/S/URI/URI(" );
    5480           0 :                     aLine.append( OUStringToOString( rWidget.m_aListEntries.front(), RTL_TEXTENCODING_ASCII_US ) );
    5481           0 :                     aLine.append( ")>>>>\n" );
    5482           0 :                 }
    5483             :             }
    5484             :             else
    5485           0 :                 m_aErrors.insert( PDFWriter::Warning_FormAction_Omitted_PDFA );
    5486             :         }
    5487           0 :         if( !rWidget.m_aDAString.isEmpty() )
    5488             :         {
    5489           0 :             if( !rWidget.m_aDRDict.isEmpty() )
    5490             :             {
    5491           0 :                 aLine.append( "/DR<<" );
    5492           0 :                 aLine.append( rWidget.m_aDRDict );
    5493           0 :                 aLine.append( ">>\n" );
    5494             :             }
    5495             :             else
    5496             :             {
    5497           0 :                 aLine.append( "/DR<</Font<<" );
    5498           0 :                 appendBuiltinFontsToDict( aLine );
    5499           0 :                 aLine.append( ">>>>\n" );
    5500             :             }
    5501           0 :             aLine.append( "/DA" );
    5502           0 :             appendLiteralStringEncrypt( rWidget.m_aDAString, rWidget.m_nObject, aLine );
    5503           0 :             aLine.append( "\n" );
    5504           0 :             if( rWidget.m_nTextStyle & TEXT_DRAW_CENTER )
    5505           0 :                 aLine.append( "/Q 1\n" );
    5506           0 :             else if( rWidget.m_nTextStyle & TEXT_DRAW_RIGHT )
    5507           0 :                 aLine.append( "/Q 2\n" );
    5508             :         }
    5509             :         // appearance charactristics for terminal fields
    5510             :         // which are supposed to have an appearance constructed
    5511             :         // by the viewer application
    5512           0 :         if( !rWidget.m_aMKDict.isEmpty() )
    5513             :         {
    5514           0 :             aLine.append( "/MK<<" );
    5515           0 :             aLine.append( rWidget.m_aMKDict );
    5516             :             //add the CA string, encrypting it
    5517           0 :             appendLiteralStringEncrypt(rWidget.m_aMKDictCAString, rWidget.m_nObject, aLine);
    5518           0 :             aLine.append( ">>\n" );
    5519             :         }
    5520             : 
    5521           0 :         CHECK_RETURN( emitAppearances( rWidget, aLine ) );
    5522             : 
    5523             :         aLine.append( ">>\n"
    5524           0 :                       "endobj\n\n" );
    5525           0 :         CHECK_RETURN( updateObject( rWidget.m_nObject ) );
    5526           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5527           0 :     }
    5528           0 :     return true;
    5529             : }
    5530             : 
    5531           0 : bool PDFWriterImpl::emitAnnotations()
    5532             : {
    5533           0 :     if( m_aPages.size() < 1 )
    5534           0 :         return false;
    5535             : 
    5536           0 :     CHECK_RETURN( emitLinkAnnotations() );
    5537           0 :     CHECK_RETURN( emitNoteAnnotations() );
    5538           0 :     CHECK_RETURN( emitWidgetAnnotations() );
    5539             : 
    5540           0 :     return true;
    5541             : }
    5542             : 
    5543             : #undef CHECK_RETURN
    5544             : #define CHECK_RETURN( x ) if( !x ) return false
    5545             : 
    5546           0 : bool PDFWriterImpl::emitCatalog()
    5547             : {
    5548             :     // build page tree
    5549             :     // currently there is only one node that contains all leaves
    5550             : 
    5551             :     // first create a page tree node id
    5552           0 :     sal_Int32 nTreeNode = createObject();
    5553             : 
    5554             :     // emit global resource dictionary (page emit needs it)
    5555           0 :     CHECK_RETURN( emitResources() );
    5556             : 
    5557             :     // emit all pages
    5558           0 :     for( std::vector<PDFPage>::iterator it = m_aPages.begin(); it != m_aPages.end(); ++it )
    5559           0 :         if( ! it->emit( nTreeNode ) )
    5560           0 :             return false;
    5561             : 
    5562           0 :     sal_Int32 nNamedDestinationsDictionary = emitNamedDestinations();
    5563             : 
    5564           0 :     sal_Int32 nOutlineDict = emitOutline();
    5565             : 
    5566             :     // emit Output intent i59651
    5567           0 :     sal_Int32 nOutputIntentObject = emitOutputIntent();
    5568             : 
    5569             :     // emit metadata
    5570           0 :     sal_Int32 nMetadataObject = emitDocumentMetadata();
    5571             : 
    5572           0 :     sal_Int32 nStructureDict = 0;
    5573           0 :     if(m_aStructure.size() > 1)
    5574             :     {
    5575             :         // check if dummy structure containers are needed
    5576           0 :         addInternalStructureContainer(m_aStructure[0]);
    5577           0 :         nStructureDict = m_aStructure[0].m_nObject = createObject();
    5578           0 :         emitStructure( m_aStructure[ 0 ] );
    5579             :     }
    5580             : 
    5581             :     // adjust tree node file offset
    5582           0 :     if( ! updateObject( nTreeNode ) )
    5583           0 :         return false;
    5584             : 
    5585             :     // emit tree node
    5586           0 :     OStringBuffer aLine( 2048 );
    5587           0 :     aLine.append( nTreeNode );
    5588           0 :     aLine.append( " 0 obj\n" );
    5589           0 :     aLine.append( "<</Type/Pages\n" );
    5590           0 :     aLine.append( "/Resources " );
    5591           0 :     aLine.append( getResourceDictObj() );
    5592           0 :     aLine.append( " 0 R\n" );
    5593             : 
    5594           0 :     switch( m_eInheritedOrientation )
    5595             :     {
    5596           0 :         case PDFWriter::Landscape: aLine.append( "/Rotate 90\n" );break;
    5597           0 :         case PDFWriter::Seascape: aLine.append( "/Rotate -90\n" );break;
    5598             : 
    5599             :         case PDFWriter::Inherit: // actually Inherit would be a bug, but insignificant
    5600             :         case PDFWriter::Portrait:
    5601             :         default:
    5602           0 :             break;
    5603             :     }
    5604           0 :     sal_Int32 nMediaBoxWidth = 0;
    5605           0 :     sal_Int32 nMediaBoxHeight = 0;
    5606           0 :     if( m_aPages.empty() ) // sanity check, this should not happen
    5607             :     {
    5608           0 :         nMediaBoxWidth = m_nInheritedPageWidth;
    5609           0 :         nMediaBoxHeight = m_nInheritedPageHeight;
    5610             :     }
    5611             :     else
    5612             :     {
    5613           0 :         for( std::vector<PDFPage>::const_iterator iter = m_aPages.begin(); iter != m_aPages.end(); ++iter )
    5614             :         {
    5615           0 :             if( iter->m_nPageWidth > nMediaBoxWidth )
    5616           0 :                 nMediaBoxWidth = iter->m_nPageWidth;
    5617           0 :             if( iter->m_nPageHeight > nMediaBoxHeight )
    5618           0 :                 nMediaBoxHeight = iter->m_nPageHeight;
    5619             :         }
    5620             :     }
    5621           0 :     aLine.append( "/MediaBox[ 0 0 " );
    5622           0 :     aLine.append( nMediaBoxWidth );
    5623           0 :     aLine.append( ' ' );
    5624           0 :     aLine.append( nMediaBoxHeight );
    5625             :     aLine.append( " ]\n"
    5626           0 :                   "/Kids[ " );
    5627           0 :     unsigned int i = 0;
    5628           0 :     for( std::vector<PDFPage>::const_iterator iter = m_aPages.begin(); iter != m_aPages.end(); ++iter, i++ )
    5629             :     {
    5630           0 :         aLine.append( iter->m_nPageObject );
    5631           0 :         aLine.append( " 0 R" );
    5632           0 :         aLine.append( ( (i&15) == 15 ) ? "\n" : " " );
    5633             :     }
    5634             :     aLine.append( "]\n"
    5635           0 :                   "/Count " );
    5636           0 :     aLine.append( (sal_Int32)m_aPages.size() );
    5637             :     aLine.append( ">>\n"
    5638           0 :                   "endobj\n\n" );
    5639           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5640             : 
    5641             :     // emit annotation objects
    5642           0 :     CHECK_RETURN( emitAnnotations() );
    5643             : 
    5644             :     // emit Catalog
    5645           0 :     m_nCatalogObject = createObject();
    5646           0 :     if( ! updateObject( m_nCatalogObject ) )
    5647           0 :         return false;
    5648           0 :     aLine.setLength( 0 );
    5649           0 :     aLine.append( m_nCatalogObject );
    5650             :     aLine.append( " 0 obj\n"
    5651           0 :                   "<</Type/Catalog/Pages " );
    5652           0 :     aLine.append( nTreeNode );
    5653           0 :     aLine.append( " 0 R\n" );
    5654             : //--->i56629
    5655             :     // check if there are named destinations to emit (root must be inside the catalog)
    5656           0 :     if( nNamedDestinationsDictionary )
    5657             :     {
    5658           0 :         aLine.append("/Dests ");
    5659           0 :         aLine.append( nNamedDestinationsDictionary );
    5660           0 :         aLine.append( " 0 R\n" );
    5661             :     }
    5662             : //<----
    5663           0 :     if( m_aContext.PageLayout != PDFWriter::DefaultLayout )
    5664           0 :         switch(  m_aContext.PageLayout )
    5665             :         {
    5666             :         default :
    5667             :         case  PDFWriter::SinglePage :
    5668           0 :             aLine.append( "/PageLayout/SinglePage\n" );
    5669           0 :             break;
    5670             :         case  PDFWriter::Continuous :
    5671           0 :             aLine.append( "/PageLayout/OneColumn\n" );
    5672           0 :             break;
    5673             :         case  PDFWriter::ContinuousFacing :
    5674             :             // the flag m_aContext.FirstPageLeft below is used to set the page on the left side
    5675           0 :             aLine.append( "/PageLayout/TwoColumnRight\n" );//odd page on the right side
    5676           0 :             break;
    5677             :         }
    5678           0 :     if( m_aContext.PDFDocumentMode != PDFWriter::ModeDefault && !m_aContext.OpenInFullScreenMode )
    5679           0 :         switch(  m_aContext.PDFDocumentMode )
    5680             :         {
    5681             :         default :
    5682           0 :             aLine.append( "/PageMode/UseNone\n" );
    5683           0 :             break;
    5684             :         case PDFWriter::UseOutlines :
    5685           0 :             aLine.append( "/PageMode/UseOutlines\n" ); //document is opened with outline pane open
    5686           0 :             break;
    5687             :         case PDFWriter::UseThumbs :
    5688           0 :             aLine.append( "/PageMode/UseThumbs\n" ); //document is opened with thumbnails pane open
    5689           0 :             break;
    5690             :         }
    5691           0 :     else if( m_aContext.OpenInFullScreenMode )
    5692           0 :         aLine.append( "/PageMode/FullScreen\n" ); //document is opened full screen
    5693             : 
    5694           0 :     OStringBuffer aInitPageRef;
    5695           0 :     if( m_aContext.InitialPage >= 0 && m_aContext.InitialPage < (sal_Int32)m_aPages.size() )
    5696             :     {
    5697           0 :         aInitPageRef.append( m_aPages[m_aContext.InitialPage].m_nPageObject );
    5698           0 :         aInitPageRef.append( " 0 R" );
    5699             :     }
    5700             :     else
    5701           0 :         aInitPageRef.append( "0" );
    5702             : 
    5703           0 :     switch( m_aContext.PDFDocumentAction )
    5704             :     {
    5705             :     case PDFWriter::ActionDefault :     //do nothing, this is the Acrobat default
    5706             :     default:
    5707           0 :         if( aInitPageRef.getLength() > 1 )
    5708             :         {
    5709           0 :             aLine.append( "/OpenAction[" );
    5710           0 :             aLine.append( aInitPageRef.makeStringAndClear() );
    5711           0 :             aLine.append( " /XYZ null null 0]\n" );
    5712             :         }
    5713           0 :         break;
    5714             :     case PDFWriter::FitInWindow :
    5715           0 :         aLine.append( "/OpenAction[" );
    5716           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5717           0 :         aLine.append( " /Fit]\n" ); //Open fit page
    5718           0 :         break;
    5719             :     case PDFWriter::FitWidth :
    5720           0 :         aLine.append( "/OpenAction[" );
    5721           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5722           0 :         aLine.append( " /FitH " );
    5723           0 :         aLine.append( m_nInheritedPageHeight );//Open fit width
    5724           0 :         aLine.append( "]\n" );
    5725           0 :         break;
    5726             :     case PDFWriter::FitVisible :
    5727           0 :         aLine.append( "/OpenAction[" );
    5728           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5729           0 :         aLine.append( " /FitBH " );
    5730           0 :         aLine.append( m_nInheritedPageHeight );//Open fit visible
    5731           0 :         aLine.append( "]\n" );
    5732           0 :         break;
    5733             :     case PDFWriter::ActionZoom :
    5734           0 :         aLine.append( "/OpenAction[" );
    5735           0 :         aLine.append( aInitPageRef.makeStringAndClear() );
    5736           0 :         aLine.append( " /XYZ null null " );
    5737           0 :         if( m_aContext.Zoom >= 50 && m_aContext.Zoom <= 1600 )
    5738           0 :             aLine.append( (double)m_aContext.Zoom/100.0 );
    5739             :         else
    5740           0 :             aLine.append( "0" );
    5741           0 :         aLine.append( "]\n" );
    5742           0 :         break;
    5743             :     }
    5744             : 
    5745             :     // viewer preferences, if we had some, then emit
    5746           0 :     if( m_aContext.HideViewerToolbar ||
    5747           0 :         ( m_aContext.Version > PDFWriter::PDF_1_3 && !m_aContext.DocumentInfo.Title.isEmpty() && m_aContext.DisplayPDFDocumentTitle ) ||
    5748           0 :         m_aContext.HideViewerMenubar ||
    5749           0 :         m_aContext.HideViewerWindowControls || m_aContext.FitWindow ||
    5750           0 :         m_aContext.CenterWindow || (m_aContext.FirstPageLeft  &&  m_aContext.PageLayout == PDFWriter::ContinuousFacing ) ||
    5751             :         m_aContext.OpenInFullScreenMode )
    5752             :     {
    5753           0 :         aLine.append( "/ViewerPreferences<<" );
    5754           0 :         if( m_aContext.HideViewerToolbar )
    5755           0 :             aLine.append( "/HideToolbar true\n" );
    5756           0 :         if( m_aContext.HideViewerMenubar )
    5757           0 :             aLine.append( "/HideMenubar true\n" );
    5758           0 :         if( m_aContext.HideViewerWindowControls )
    5759           0 :             aLine.append( "/HideWindowUI true\n" );
    5760           0 :         if( m_aContext.FitWindow )
    5761           0 :             aLine.append( "/FitWindow true\n" );
    5762           0 :         if( m_aContext.CenterWindow )
    5763           0 :             aLine.append( "/CenterWindow true\n" );
    5764           0 :         if( m_aContext.Version > PDFWriter::PDF_1_3 && !m_aContext.DocumentInfo.Title.isEmpty() && m_aContext.DisplayPDFDocumentTitle )
    5765           0 :             aLine.append( "/DisplayDocTitle true\n" );
    5766           0 :         if( m_aContext.FirstPageLeft &&  m_aContext.PageLayout == PDFWriter::ContinuousFacing )
    5767           0 :             aLine.append( "/Direction/R2L\n" );
    5768           0 :         if( m_aContext.OpenInFullScreenMode )
    5769           0 :             switch( m_aContext.PDFDocumentMode )
    5770             :             {
    5771             :             default :
    5772             :             case PDFWriter::ModeDefault :
    5773           0 :                 aLine.append( "/NonFullScreenPageMode/UseNone\n" );
    5774           0 :                 break;
    5775             :             case PDFWriter::UseOutlines :
    5776           0 :                 aLine.append( "/NonFullScreenPageMode/UseOutlines\n" );
    5777           0 :                 break;
    5778             :             case PDFWriter::UseThumbs :
    5779           0 :                 aLine.append( "/NonFullScreenPageMode/UseThumbs\n" );
    5780           0 :                 break;
    5781             :             }
    5782           0 :         aLine.append( ">>\n" );
    5783             :     }
    5784             : 
    5785           0 :     if( nOutlineDict )
    5786             :     {
    5787           0 :         aLine.append( "/Outlines " );
    5788           0 :         aLine.append( nOutlineDict );
    5789           0 :         aLine.append( " 0 R\n" );
    5790             :     }
    5791           0 :     if( nStructureDict )
    5792             :     {
    5793           0 :         aLine.append( "/StructTreeRoot " );
    5794           0 :         aLine.append( nStructureDict );
    5795           0 :         aLine.append( " 0 R\n" );
    5796             :     }
    5797           0 :     if( !m_aContext.DocumentLocale.Language.isEmpty() )
    5798             :     {
    5799             :         /* PDF allows only RFC 3066, see above in emitStructure(). */
    5800           0 :         LanguageTag aLanguageTag( m_aContext.DocumentLocale);
    5801           0 :         OUString aLanguage, aScript, aCountry;
    5802           0 :         aLanguageTag.getIsoLanguageScriptCountry( aLanguage, aScript, aCountry);
    5803           0 :         if (!aLanguage.isEmpty())
    5804             :         {
    5805           0 :             OUStringBuffer aLocBuf( 16 );
    5806           0 :             aLocBuf.append( aLanguage );
    5807           0 :             if( !aCountry.isEmpty() )
    5808             :             {
    5809           0 :                 aLocBuf.append( '-' );
    5810           0 :                 aLocBuf.append( aCountry );
    5811             :             }
    5812           0 :             aLine.append( "/Lang" );
    5813           0 :             appendLiteralStringEncrypt( aLocBuf.makeStringAndClear(), m_nCatalogObject, aLine );
    5814           0 :             aLine.append( "\n" );
    5815           0 :         }
    5816             :     }
    5817           0 :     if( m_aContext.Tagged && m_aContext.Version > PDFWriter::PDF_1_3 )
    5818             :     {
    5819           0 :         aLine.append( "/MarkInfo<</Marked true>>\n" );
    5820             :     }
    5821           0 :     if( m_aWidgets.size() > 0 )
    5822             :     {
    5823           0 :         aLine.append( "/AcroForm<</Fields[\n" );
    5824           0 :         int nWidgets = m_aWidgets.size();
    5825           0 :         int nOut = 0;
    5826           0 :         for( int j = 0; j < nWidgets; j++ )
    5827             :         {
    5828             :             // output only root fields
    5829           0 :             if( m_aWidgets[j].m_nParent < 1 )
    5830             :             {
    5831           0 :                 aLine.append( m_aWidgets[j].m_nObject );
    5832           0 :                 aLine.append( (nOut++ % 5)==4 ? " 0 R\n" : " 0 R " );
    5833             :             }
    5834             :         }
    5835           0 :         aLine.append( "\n]" );
    5836             : 
    5837             : #if !defined(ANDROID) && !defined(IOS)
    5838           0 :         if (m_nSignatureObject != -1)
    5839           0 :             aLine.append( "/SigFlags 3");
    5840             : #endif
    5841             : 
    5842           0 :         aLine.append( "/DR " );
    5843           0 :         aLine.append( getResourceDictObj() );
    5844           0 :         aLine.append( " 0 R" );
    5845             :         // /NeedAppearances must not be used if PDF is signed
    5846           0 :         if( m_bIsPDF_A1
    5847             : #if !defined(ANDROID) && !defined(IOS)
    5848           0 :             || ( m_nSignatureObject != -1 )
    5849             : #endif
    5850             :             )
    5851           0 :             aLine.append( ">>\n" );
    5852             :         else
    5853           0 :             aLine.append( "/NeedAppearances true>>\n" );
    5854             :     }
    5855             : 
    5856             : //--->i59651
    5857             :     //check if there is a Metadata object
    5858           0 :     if( nOutputIntentObject )
    5859             :     {
    5860           0 :         aLine.append("/OutputIntents[");
    5861           0 :         aLine.append( nOutputIntentObject );
    5862           0 :         aLine.append( " 0 R]" );
    5863             :     }
    5864             : 
    5865           0 :     if( nMetadataObject )
    5866             :     {
    5867           0 :         aLine.append("/Metadata ");
    5868           0 :         aLine.append( nMetadataObject );
    5869           0 :         aLine.append( " 0 R" );
    5870             :     }
    5871             : //<----
    5872             :     aLine.append( ">>\n"
    5873           0 :                   "endobj\n\n" );
    5874           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    5875             : 
    5876           0 :     return true;
    5877             : }
    5878             : 
    5879             : #if !defined(ANDROID) && !defined(IOS)
    5880             : 
    5881           0 : bool PDFWriterImpl::emitSignature()
    5882             : {
    5883           0 :     if( !updateObject( m_nSignatureObject ) )
    5884           0 :         return false;
    5885             : 
    5886           0 :     OStringBuffer aLine( 0x5000 );
    5887           0 :     aLine.append( m_nSignatureObject );
    5888           0 :     aLine.append( " 0 obj\n" );
    5889           0 :     aLine.append("<</Contents <" );
    5890             : 
    5891           0 :     sal_uInt64 nOffset = ~0U;
    5892           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nOffset) ) );
    5893             : 
    5894           0 :     m_nSignatureContentOffset = nOffset + aLine.getLength();
    5895             : 
    5896             :     // reserve some space for the PKCS#7 object
    5897           0 :     OStringBuffer aContentFiller( MAX_SIGNATURE_CONTENT_LENGTH );
    5898           0 :     comphelper::string::padToLength(aContentFiller, MAX_SIGNATURE_CONTENT_LENGTH, '0');
    5899           0 :     aLine.append( aContentFiller.makeStringAndClear() );
    5900           0 :     aLine.append( ">\n/Type/Sig/SubFilter/adbe.pkcs7.detached");
    5901             : 
    5902           0 :     if( !m_aContext.DocumentInfo.Author.isEmpty() )
    5903             :     {
    5904           0 :         aLine.append( "/Name" );
    5905           0 :         appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Author, m_nSignatureObject, aLine );
    5906             :     }
    5907             : 
    5908           0 :     aLine.append( " /M ");
    5909           0 :     appendLiteralStringEncrypt( m_aCreationDateString, m_nSignatureObject, aLine );
    5910             : 
    5911           0 :     aLine.append( " /ByteRange [ 0 ");
    5912           0 :     aLine.append( m_nSignatureContentOffset - 1, 10 );
    5913           0 :     aLine.append( " " );
    5914           0 :     aLine.append( m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1, 10 );
    5915           0 :     aLine.append( " " );
    5916             : 
    5917           0 :     m_nSignatureLastByteRangeNoOffset = nOffset + aLine.getLength();
    5918             : 
    5919             :     // mark the last ByteRange no and add some space. Now, we don't know
    5920             :     // how many bytes we need for this ByteRange value
    5921             :     // The real value will be overwritten in the finalizeSignature method
    5922           0 :     OStringBuffer aByteRangeFiller( 100  );
    5923           0 :     comphelper::string::padToLength(aByteRangeFiller, 100, ' ');
    5924           0 :     aLine.append( aByteRangeFiller.makeStringAndClear() );
    5925           0 :     aLine.append("  /Filter/Adobe.PPKMS");
    5926             : 
    5927             :     //emit reason, location and contactinfo
    5928           0 :     if ( !m_aContext.SignReason.isEmpty() )
    5929             :     {
    5930           0 :         aLine.append("/Reason");
    5931           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignReason, m_nSignatureObject, aLine );
    5932             :     }
    5933             : 
    5934           0 :     if ( !m_aContext.SignLocation.isEmpty() )
    5935             :     {
    5936           0 :         aLine.append("/Location");
    5937           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignLocation, m_nSignatureObject, aLine );
    5938             :     }
    5939             : 
    5940           0 :     if ( !m_aContext.SignContact.isEmpty() )
    5941             :     {
    5942           0 :         aLine.append("/ContactInfo");
    5943           0 :         appendUnicodeTextStringEncrypt( m_aContext.SignContact, m_nSignatureObject, aLine );
    5944             :     }
    5945             : 
    5946           0 :     aLine.append(" >>\nendobj\n\n" );
    5947             : 
    5948           0 :     if (!writeBuffer( aLine.getStr(), aLine.getLength() ))
    5949           0 :         return false;
    5950             : 
    5951           0 :     return true;
    5952             : }
    5953             : 
    5954           0 : char *PDFSigningPKCS7PasswordCallback(PK11SlotInfo * /*slot*/, PRBool /*retry*/, void *arg)
    5955             : {
    5956           0 :     return (char *)arg;
    5957             : }
    5958             : 
    5959             : namespace {
    5960             :     class HashContextScope {
    5961             :         HASHContext *mpPtr;
    5962             :     public:
    5963           0 :         HashContextScope(HASHContext *pPtr) : mpPtr(pPtr) {}
    5964           0 :         ~HashContextScope() { clear(); }
    5965           0 :         void clear() { if (mpPtr) { HASH_Destroy(mpPtr); } mpPtr = NULL; }
    5966           0 :         HASHContext *get() { return mpPtr; }
    5967             :     };
    5968             : }
    5969             : 
    5970           0 : bool PDFWriterImpl::finalizeSignature()
    5971             : {
    5972             : 
    5973           0 :     if (!m_aContext.SignCertificate.is())
    5974           0 :         return false;
    5975             : 
    5976             :     // 1- calculate last ByteRange value
    5977           0 :     sal_uInt64 nOffset = ~0U;
    5978           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nOffset) ) );
    5979             : 
    5980           0 :     sal_Int64 nLastByteRangeNo = nOffset - (m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1);
    5981             : 
    5982             :     // 2- overwrite the value to the m_nSignatureLastByteRangeNoOffset position
    5983           0 :     sal_uInt64 nWritten = 0;
    5984           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.setPos(osl_Pos_Absolut, m_nSignatureLastByteRangeNoOffset) ) );
    5985           0 :     OStringBuffer aByteRangeNo( 256 );
    5986           0 :     aByteRangeNo.append( nLastByteRangeNo, 10);
    5987           0 :     aByteRangeNo.append( " ]" );
    5988             : 
    5989           0 :     if (m_aFile.write(aByteRangeNo.getStr(), aByteRangeNo.getLength(), nWritten) != osl::File::E_None)
    5990             :     {
    5991           0 :         CHECK_RETURN( (osl::File::E_None == m_aFile.setPos(osl_Pos_Absolut, nOffset)) );
    5992           0 :         return false;
    5993             :     }
    5994             : 
    5995             :     // 3- create the PKCS#7 object using NSS
    5996           0 :     com::sun::star::uno::Sequence< sal_Int8 > derEncoded = m_aContext.SignCertificate->getEncoded();
    5997             : 
    5998           0 :     if (!derEncoded.hasElements())
    5999           0 :         return false;
    6000             : 
    6001           0 :     sal_Int8* n_derArray = derEncoded.getArray();
    6002           0 :     sal_Int32 n_derLength = derEncoded.getLength();
    6003             : 
    6004           0 :     NSS_NoDB_Init(".");
    6005             : 
    6006           0 :     CERTCertificate *cert = CERT_DecodeCertFromPackage(reinterpret_cast<char *>(n_derArray), n_derLength);
    6007             : 
    6008           0 :     if (!cert)
    6009             :     {
    6010             :         SAL_WARN("vcl.gdi", "PDF Signing: Error occurred, certificate cannot be reconstructed.");
    6011           0 :         return false;
    6012             :     }
    6013             : 
    6014             :     SAL_WARN("vcl.gdi", "PDF Signing: Certificate Subject: " <<  cert->subjectName << "\n\tCertificate Issuer: " << cert->issuerName);
    6015             : 
    6016             :     // Prepare buffer and calculate PDF file digest
    6017           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.setPos(osl_Pos_Absolut, 0)) );
    6018             : 
    6019           0 :     HashContextScope hc(HASH_Create(HASH_AlgSHA1));
    6020           0 :     if (!hc.get())
    6021             :     {
    6022             :         SAL_WARN("vcl.gdi", "PDF Signing: SHA1 HASH_Create failed!");
    6023           0 :         return false;
    6024             :     }
    6025             : 
    6026           0 :     HASH_Begin(hc.get());
    6027             : 
    6028           0 :     boost::scoped_array<char> buffer(new char[m_nSignatureContentOffset + 1]);
    6029             :     sal_uInt64 bytesRead;
    6030             : 
    6031             :     //FIXME: Check if SHA1 is calculated from the correct byterange
    6032           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.read(buffer.get(), m_nSignatureContentOffset - 1 , bytesRead)) );
    6033           0 :     if (bytesRead != (sal_uInt64)m_nSignatureContentOffset - 1)
    6034             :         SAL_WARN("vcl.gdi", "PDF Signing: First buffer read failed!");
    6035             : 
    6036           0 :     HASH_Update(hc.get(), reinterpret_cast<const unsigned char*>(buffer.get()), bytesRead);
    6037             : 
    6038           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.setPos(osl_Pos_Absolut, m_nSignatureContentOffset + MAX_SIGNATURE_CONTENT_LENGTH + 1)) );
    6039           0 :     buffer.reset(new char[nLastByteRangeNo + 1]);
    6040           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.read(buffer.get(), nLastByteRangeNo, bytesRead)) );
    6041           0 :     if (bytesRead != (sal_uInt64) nLastByteRangeNo)
    6042             :         SAL_WARN("vcl.gdi", "PDF Signing: Second buffer read failed!");
    6043             : 
    6044           0 :     HASH_Update(hc.get(), reinterpret_cast<const unsigned char*>(buffer.get()), bytesRead);
    6045             : 
    6046             :     SECItem digest;
    6047             :     unsigned char hash[SHA1_LENGTH];
    6048           0 :     digest.data = hash;
    6049           0 :     HASH_End(hc.get(), digest.data, &digest.len, SHA1_LENGTH);
    6050           0 :     hc.clear();
    6051             : 
    6052           0 :     OString pass = OUStringToOString( m_aContext.SignPassword, RTL_TEXTENCODING_UTF8 );
    6053             : 
    6054           0 :     NSSCMSMessage *cms_msg = NSS_CMSMessage_Create(NULL);
    6055           0 :     if (!cms_msg)
    6056             :     {
    6057             :         SAL_WARN("vcl.gdi", "PDF signing: can't create new CMS message.");
    6058           0 :         return false;
    6059             :     }
    6060             : 
    6061           0 :     NSSCMSSignedData *cms_sd = NSS_CMSSignedData_Create(cms_msg);
    6062           0 :     if (!cms_sd)
    6063             :     {
    6064             :         SAL_WARN("vcl.gdi", "PDF signing: can't create CMS SignedData.");
    6065           0 :         return false;
    6066             :     }
    6067             : 
    6068           0 :     NSSCMSContentInfo *cms_cinfo = NSS_CMSMessage_GetContentInfo(cms_msg);
    6069           0 :     if (NSS_CMSContentInfo_SetContent_SignedData(cms_msg, cms_cinfo, cms_sd) != SECSuccess)
    6070             :     {
    6071             :         SAL_WARN("vcl.gdi", "PDF signing: Can't set CMS content signed data.");
    6072           0 :         return false;
    6073             :     }
    6074             : 
    6075           0 :     cms_cinfo = NSS_CMSSignedData_GetContentInfo(cms_sd);
    6076             :     //attach NULL data as detached data
    6077           0 :     if (NSS_CMSContentInfo_SetContent_Data(cms_msg, cms_cinfo, NULL, PR_TRUE) != SECSuccess)
    6078             :     {
    6079             :         SAL_WARN("vcl.gdi", "PDF signing: Can't set CMS content data.");
    6080           0 :         return false;
    6081             :     }
    6082             : 
    6083           0 :     NSSCMSSignerInfo *cms_signer = NSS_CMSSignerInfo_Create(cms_msg, cert, SEC_OID_SHA1);
    6084           0 :     if (!cms_signer)
    6085             :     {
    6086             :         SAL_WARN("vcl.gdi", "PDF signing: can't create CMS SignerInfo.");
    6087           0 :         return false;
    6088             :     }
    6089             : 
    6090           0 :     if (NSS_CMSSignerInfo_IncludeCerts(cms_signer, NSSCMSCM_CertChain, certUsageEmailSigner) != SECSuccess)
    6091             :     {
    6092             :         SAL_WARN("vcl.gdi", "PDF signing: can't include cert chain.");
    6093           0 :         return false;
    6094             :     }
    6095             : 
    6096           0 :     if (NSS_CMSSignerInfo_AddSigningTime(cms_signer, PR_Now()) != SECSuccess)
    6097             :     {
    6098             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signing time.");
    6099           0 :         return false;
    6100             :     }
    6101             : 
    6102           0 :     if (NSS_CMSSignedData_AddCertificate(cms_sd, cert) != SECSuccess)
    6103             :     {
    6104             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signer certificate.");
    6105           0 :         return false;
    6106             :     }
    6107             : 
    6108           0 :     if (NSS_CMSSignedData_AddSignerInfo(cms_sd, cms_signer) != SECSuccess)
    6109             :     {
    6110             :         SAL_WARN("vcl.gdi", "PDF signing: can't add signer info.");
    6111           0 :         return false;
    6112             :     }
    6113             : 
    6114           0 :     if (NSS_CMSSignedData_SetDigestValue(cms_sd, SEC_OID_SHA1, &digest) != SECSuccess)
    6115             :     {
    6116             :         SAL_WARN("vcl.gdi", "PDF signing: can't set PDF digest value.");
    6117           0 :         return false;
    6118             :     }
    6119             : 
    6120             :     SAL_WARN("vcl.gdi","PKCS7 Object created successfully!");
    6121             : 
    6122             :     SECItem cms_output;
    6123           0 :     cms_output.data = 0;
    6124           0 :     cms_output.len = 0;
    6125           0 :     PLArenaPool *arena = PORT_NewArena(MAX_SIGNATURE_CONTENT_LENGTH);
    6126             :     NSSCMSEncoderContext *cms_ecx;
    6127             : 
    6128             :     //FIXME: Check if password is passed correctly to SEC_PKCS7CreateSignedData function
    6129           0 :     cms_ecx = NSS_CMSEncoder_Start(cms_msg, NULL, NULL, &cms_output, arena, (PK11PasswordFunc)::PDFSigningPKCS7PasswordCallback, (void *)pass.getStr(), NULL, NULL, NULL, NULL);
    6130             : 
    6131           0 :     if (!cms_ecx)
    6132             :     {
    6133             :         SAL_WARN("vcl.gdi", "PDF Signing: can't start DER encoder.");
    6134           0 :         return false;
    6135             :     }
    6136             :     SAL_WARN("vcl.gdi", "PDF Signing: Started DER encoding.");
    6137             : 
    6138           0 :     if (NSS_CMSEncoder_Finish(cms_ecx) != SECSuccess)
    6139             :     {
    6140             :         SAL_WARN("vcl.gdi", "PDF Signing: can't finish DER encoder.");
    6141           0 :         return false;
    6142             :     }
    6143             :     SAL_WARN("vcl.gdi", "PDF Signing: Finished DER encoding.");
    6144             : 
    6145           0 :     OStringBuffer cms_hexbuffer;
    6146             : 
    6147           0 :     for (unsigned int i = 0; i < cms_output.len ; i++)
    6148           0 :         appendHex(cms_output.data[i], cms_hexbuffer);
    6149             : 
    6150             :     SAL_WARN("vcl.gdi","PKCS7 object encoded successfully!");
    6151             : 
    6152             :     // Set file pointer to the m_nSignatureContentOffset, we're ready to overwrite PKCS7 object
    6153           0 :     nWritten = 0;
    6154           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.setPos(osl_Pos_Absolut, m_nSignatureContentOffset)) );
    6155           0 :     m_aFile.write(cms_hexbuffer.getStr(), cms_hexbuffer.getLength(), nWritten);
    6156             : 
    6157           0 :     NSS_CMSMessage_Destroy(cms_msg);
    6158             : 
    6159           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.setPos(osl_Pos_Absolut, nOffset)) );
    6160           0 :     return true;
    6161             : }
    6162             : 
    6163             : #endif
    6164             : 
    6165           0 : sal_Int32 PDFWriterImpl::emitInfoDict( )
    6166             : {
    6167           0 :     sal_Int32 nObject = createObject();
    6168             : 
    6169           0 :     if( updateObject( nObject ) )
    6170             :     {
    6171           0 :         OStringBuffer aLine( 1024 );
    6172           0 :         aLine.append( nObject );
    6173             :         aLine.append( " 0 obj\n"
    6174           0 :                       "<<" );
    6175           0 :         if( !m_aContext.DocumentInfo.Title.isEmpty() )
    6176             :         {
    6177           0 :             aLine.append( "/Title" );
    6178           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Title, nObject, aLine );
    6179           0 :             aLine.append( "\n" );
    6180             :         }
    6181           0 :         if( !m_aContext.DocumentInfo.Author.isEmpty() )
    6182             :         {
    6183           0 :             aLine.append( "/Author" );
    6184           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Author, nObject, aLine );
    6185           0 :             aLine.append( "\n" );
    6186             :         }
    6187           0 :         if( !m_aContext.DocumentInfo.Subject.isEmpty() )
    6188             :         {
    6189           0 :             aLine.append( "/Subject" );
    6190           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Subject, nObject, aLine );
    6191           0 :             aLine.append( "\n" );
    6192             :         }
    6193           0 :         if( !m_aContext.DocumentInfo.Keywords.isEmpty() )
    6194             :         {
    6195           0 :             aLine.append( "/Keywords" );
    6196           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Keywords, nObject, aLine );
    6197           0 :             aLine.append( "\n" );
    6198             :         }
    6199           0 :         if( !m_aContext.DocumentInfo.Creator.isEmpty() )
    6200             :         {
    6201           0 :             aLine.append( "/Creator" );
    6202           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Creator, nObject, aLine );
    6203           0 :             aLine.append( "\n" );
    6204             :         }
    6205           0 :         if( !m_aContext.DocumentInfo.Producer.isEmpty() )
    6206             :         {
    6207           0 :             aLine.append( "/Producer" );
    6208           0 :             appendUnicodeTextStringEncrypt( m_aContext.DocumentInfo.Producer, nObject, aLine );
    6209           0 :             aLine.append( "\n" );
    6210             :         }
    6211             : 
    6212           0 :          aLine.append( "/CreationDate" );
    6213           0 :          appendLiteralStringEncrypt( m_aCreationDateString, nObject, aLine );
    6214           0 :         aLine.append( ">>\nendobj\n\n" );
    6215           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6216           0 :             nObject = 0;
    6217             :     }
    6218             :     else
    6219           0 :         nObject = 0;
    6220             : 
    6221           0 :     return nObject;
    6222             : }
    6223             : 
    6224             : //--->i56629
    6225             : // Part of this function may be shared with method appendDest.
    6226           0 : sal_Int32 PDFWriterImpl::emitNamedDestinations()
    6227             : {
    6228           0 :     sal_Int32  nCount = m_aNamedDests.size();
    6229           0 :     if( nCount <= 0 )
    6230           0 :         return 0;//define internal error
    6231             : 
    6232             :     //get the object number for all the destinations
    6233           0 :     sal_Int32 nObject = createObject();
    6234             : 
    6235           0 :     if( updateObject( nObject ) )
    6236             :     {
    6237             :         //emit the dictionary
    6238           0 :         OStringBuffer aLine( 1024 );
    6239           0 :         aLine.append( nObject );
    6240             :         aLine.append( " 0 obj\n"
    6241           0 :                       "<<" );
    6242             : 
    6243             :         sal_Int32  nDestID;
    6244           0 :         for( nDestID = 0; nDestID < nCount; nDestID++ )
    6245             :         {
    6246           0 :             const PDFNamedDest& rDest   = m_aNamedDests[ nDestID ];
    6247             :             // In order to correctly function both under an Internet browser and
    6248             :             // directly with a reader (provided the reader has the feature) we
    6249             :             // need to set the name of the destination the same way it will be encoded
    6250             :             // in an Internet link
    6251             :             INetURLObject aLocalURL(
    6252           0 :                 OUString( "http://ahost.ax"  ) ); //dummy location, won't be used
    6253           0 :             aLocalURL.SetMark( rDest.m_aDestName );
    6254             : 
    6255           0 :             const OUString aName   = aLocalURL.GetMark( INetURLObject::NO_DECODE ); //same coding as
    6256             :             // in link creation ( see PDFWriterImpl::emitLinkAnnotations )
    6257           0 :             const PDFPage& rDestPage    = m_aPages[ rDest.m_nPage ];
    6258             : 
    6259           0 :             aLine.append( '/' );
    6260           0 :             appendDestinationName( aName, aLine ); // this conversion must be done when forming the link to target ( see in emitCatalog )
    6261           0 :             aLine.append( '[' ); // the '[' can be emitted immediately, because the appendDestinationName function
    6262             :                                  //maps the preceding character properly
    6263           0 :             aLine.append( rDestPage.m_nPageObject );
    6264           0 :             aLine.append( " 0 R" );
    6265             : 
    6266           0 :             switch( rDest.m_eType )
    6267             :             {
    6268             :             case PDFWriter::XYZ:
    6269             :             default:
    6270           0 :                 aLine.append( "/XYZ " );
    6271           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6272           0 :                 aLine.append( ' ' );
    6273           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6274           0 :                 aLine.append( " 0" );
    6275           0 :                 break;
    6276             :             case PDFWriter::Fit:
    6277           0 :                 aLine.append( "/Fit" );
    6278           0 :                 break;
    6279             :             case PDFWriter::FitRectangle:
    6280           0 :                 aLine.append( "/FitR " );
    6281           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6282           0 :                 aLine.append( ' ' );
    6283           0 :                 appendFixedInt( rDest.m_aRect.Top(), aLine );
    6284           0 :                 aLine.append( ' ' );
    6285           0 :                 appendFixedInt( rDest.m_aRect.Right(), aLine );
    6286           0 :                 aLine.append( ' ' );
    6287           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6288           0 :                 break;
    6289             :             case PDFWriter::FitHorizontal:
    6290           0 :                 aLine.append( "/FitH " );
    6291           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6292           0 :                 break;
    6293             :             case PDFWriter::FitVertical:
    6294           0 :                 aLine.append( "/FitV " );
    6295           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6296           0 :                 break;
    6297             :             case PDFWriter::FitPageBoundingBox:
    6298           0 :                 aLine.append( "/FitB" );
    6299           0 :                 break;
    6300             :             case PDFWriter::FitPageBoundingBoxHorizontal:
    6301           0 :                 aLine.append( "/FitBH " );
    6302           0 :                 appendFixedInt( rDest.m_aRect.Bottom(), aLine );
    6303           0 :                 break;
    6304             :             case PDFWriter::FitPageBoundingBoxVertical:
    6305           0 :                 aLine.append( "/FitBV " );
    6306           0 :                 appendFixedInt( rDest.m_aRect.Left(), aLine );
    6307           0 :                 break;
    6308             :             }
    6309           0 :             aLine.append( "]\n" );
    6310           0 :         }
    6311             : 
    6312             :         //close
    6313           0 :         aLine.append( ">>\nendobj\n\n" );
    6314           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6315           0 :             nObject = 0;
    6316             :     }
    6317             :     else
    6318           0 :         nObject = 0;
    6319             : 
    6320           0 :     return nObject;
    6321             : }
    6322             : //<--- i56629
    6323             : 
    6324             : //--->i59651
    6325             : // emits the output intent dictionary
    6326           0 : sal_Int32 PDFWriterImpl::emitOutputIntent()
    6327             : {
    6328           0 :     if( !m_bIsPDF_A1 )
    6329           0 :         return 0;
    6330             : 
    6331             :     //emit the sRGB standard profile, in ICC format, in a stream, per IEC61966-2.1
    6332             : 
    6333           0 :     OStringBuffer aLine( 1024 );
    6334           0 :     sal_Int32 nICCObject = createObject();
    6335           0 :     sal_Int32 nStreamLengthObject = createObject();
    6336             : 
    6337           0 :     aLine.append( nICCObject );
    6338             : // sRGB has 3 colors, hence /N 3 below (PDF 1.4 table 4.16)
    6339           0 :     aLine.append( " 0 obj\n<</N 3/Length " );
    6340           0 :     aLine.append( nStreamLengthObject );
    6341           0 :     aLine.append( " 0 R" );
    6342             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    6343           0 :     aLine.append( "/Filter/FlateDecode" );
    6344             : #endif
    6345           0 :     aLine.append( ">>\nstream\n" );
    6346           0 :     if ( !updateObject( nICCObject ) ) return 0;
    6347           0 :     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return 0;
    6348             :     //get file position
    6349           0 :     sal_uInt64 nBeginStreamPos = 0;
    6350           0 :     m_aFile.getPos(nBeginStreamPos);
    6351           0 :     beginCompression();
    6352           0 :     checkAndEnableStreamEncryption( nICCObject );
    6353           0 :     cmsHPROFILE hProfile = cmsCreate_sRGBProfile();
    6354             :     //force ICC profile version 2.1
    6355           0 :     cmsSetProfileVersion(hProfile, 2.1);
    6356           0 :     cmsUInt32Number nBytesNeeded = 0;
    6357           0 :     cmsSaveProfileToMem(hProfile, NULL, &nBytesNeeded);
    6358           0 :     if (!nBytesNeeded)
    6359           0 :       return 0;
    6360           0 :     std::vector<unsigned char> xBuffer(nBytesNeeded);
    6361           0 :     cmsSaveProfileToMem(hProfile, &xBuffer[0], &nBytesNeeded);
    6362           0 :     cmsCloseProfile(hProfile);
    6363           0 :     bool written = writeBuffer( &xBuffer[0], (sal_Int32) xBuffer.size() );
    6364           0 :     disableStreamEncryption();
    6365           0 :     endCompression();
    6366           0 :     sal_uInt64 nEndStreamPos = 0;
    6367           0 :     m_aFile.getPos(nEndStreamPos);
    6368             : 
    6369           0 :     if( !written )
    6370           0 :         return 0;
    6371           0 :     if( ! writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    6372           0 :         return 0 ;
    6373           0 :     aLine.setLength( 0 );
    6374             : 
    6375             :     //emit the stream length   object
    6376           0 :     if ( !updateObject( nStreamLengthObject ) ) return 0;
    6377           0 :     aLine.setLength( 0 );
    6378           0 :     aLine.append( nStreamLengthObject );
    6379           0 :     aLine.append( " 0 obj\n" );
    6380           0 :     aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos) );
    6381           0 :     aLine.append( "\nendobj\n\n" );
    6382           0 :     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return 0;
    6383           0 :     aLine.setLength( 0 );
    6384             : 
    6385             :     //emit the OutputIntent dictionary
    6386           0 :     sal_Int32 nOIObject = createObject();
    6387           0 :     if ( !updateObject( nOIObject ) ) return 0;
    6388           0 :     aLine.append( nOIObject );
    6389             :     aLine.append( " 0 obj\n"
    6390           0 :                   "<</Type/OutputIntent/S/GTS_PDFA1/OutputConditionIdentifier");
    6391             : 
    6392           0 :     OUString aComment( "sRGB IEC61966-2.1"  );
    6393           0 :     appendLiteralStringEncrypt( aComment ,nOIObject, aLine );
    6394           0 :     aLine.append("/DestOutputProfile ");
    6395           0 :     aLine.append( nICCObject );
    6396           0 :     aLine.append( " 0 R>>\nendobj\n\n" );;
    6397           0 :     if ( !writeBuffer( aLine.getStr(), aLine.getLength() ) ) return 0;
    6398             : 
    6399           0 :     return nOIObject;
    6400             : }
    6401             : 
    6402             : // formats the string for the XML stream
    6403           0 : static void escapeStringXML( const OUString& rStr, OUString &rValue)
    6404             : {
    6405           0 :     const sal_Unicode* pUni = rStr.getStr();
    6406           0 :     int nLen = rStr.getLength();
    6407           0 :     for( ; nLen; nLen--, pUni++ )
    6408             :     {
    6409           0 :         switch( *pUni )
    6410             :         {
    6411             :         case sal_Unicode('&'):
    6412           0 :             rValue += "&amp;";
    6413           0 :         break;
    6414             :         case sal_Unicode('<'):
    6415           0 :             rValue += "&lt;";
    6416           0 :         break;
    6417             :         case sal_Unicode('>'):
    6418           0 :             rValue += "&gt;";
    6419           0 :         break;
    6420             :         case sal_Unicode('\''):
    6421           0 :             rValue += "&apos;";
    6422           0 :         break;
    6423             :         case sal_Unicode('"'):
    6424           0 :             rValue += "&quot;";
    6425           0 :         break;
    6426             :         default:
    6427           0 :             rValue += OUString( *pUni );
    6428           0 :             break;
    6429             :         }
    6430             :     }
    6431           0 : }
    6432             : 
    6433             : // emits the document metadata
    6434           0 : sal_Int32 PDFWriterImpl::emitDocumentMetadata()
    6435             : {
    6436           0 :     if( !m_bIsPDF_A1 )
    6437           0 :         return 0;
    6438             : 
    6439             :     //get the object number for all the destinations
    6440           0 :     sal_Int32 nObject = createObject();
    6441             : 
    6442           0 :     if( updateObject( nObject ) )
    6443             :     {
    6444             :         // the following string are written in UTF-8 unicode
    6445           0 :         OStringBuffer aMetadataStream( 8192 );
    6446             : 
    6447           0 :         aMetadataStream.append( "<?xpacket begin=\"" );
    6448             :         // these lines write Unicode "zero width non-breaking space character" (U+FEFF)
    6449             :         // (aka byte-order mark ) used as a byte-order marker.
    6450           0 :         aMetadataStream.append( OUStringToOString( OUString( sal_Unicode( 0xFEFF ) ), RTL_TEXTENCODING_UTF8 ) );
    6451           0 :         aMetadataStream.append( "\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" );
    6452           0 :         aMetadataStream.append( "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n" );
    6453           0 :         aMetadataStream.append( " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" );
    6454             :         //PDF/A part ( ISO 19005-1:2005 - 6.7.11 )
    6455           0 :         aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6456           0 :         aMetadataStream.append( "      xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n" );
    6457           0 :         aMetadataStream.append( "   <pdfaid:part>1</pdfaid:part>\n" );
    6458           0 :         aMetadataStream.append( "   <pdfaid:conformance>A</pdfaid:conformance>\n" );
    6459           0 :         aMetadataStream.append( "  </rdf:Description>\n" );
    6460             :         //... Dublin Core properties go here
    6461           0 :         if( !m_aContext.DocumentInfo.Title.isEmpty() ||
    6462           0 :             !m_aContext.DocumentInfo.Author.isEmpty() ||
    6463           0 :             !m_aContext.DocumentInfo.Subject.isEmpty() )
    6464             :         {
    6465           0 :             aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6466           0 :             aMetadataStream.append( "      xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" );
    6467           0 :             if( !m_aContext.DocumentInfo.Title.isEmpty() )
    6468             :             {
    6469             :                 // this is according to PDF/A-1, technical corrigendum 1 (2007-04-01)
    6470           0 :                 aMetadataStream.append( "   <dc:title>\n" );
    6471           0 :                 aMetadataStream.append( "    <rdf:Alt>\n" );
    6472           0 :                 aMetadataStream.append( "     <rdf:li xml:lang=\"x-default\">" );
    6473           0 :                 OUString aTitle;
    6474           0 :                 escapeStringXML( m_aContext.DocumentInfo.Title, aTitle );
    6475           0 :                 aMetadataStream.append( OUStringToOString( aTitle, RTL_TEXTENCODING_UTF8 )  );
    6476           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6477           0 :                 aMetadataStream.append( "    </rdf:Alt>\n" );
    6478           0 :                 aMetadataStream.append( "   </dc:title>\n" );
    6479             :             }
    6480           0 :             if( !m_aContext.DocumentInfo.Author.isEmpty() )
    6481             :             {
    6482           0 :                 aMetadataStream.append( "   <dc:creator>\n" );
    6483           0 :                 aMetadataStream.append( "    <rdf:Seq>\n" );
    6484           0 :                 aMetadataStream.append( "     <rdf:li>" );
    6485           0 :                 OUString aAuthor;
    6486           0 :                 escapeStringXML( m_aContext.DocumentInfo.Author, aAuthor );
    6487           0 :                 aMetadataStream.append( OUStringToOString( aAuthor , RTL_TEXTENCODING_UTF8 )  );
    6488           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6489           0 :                 aMetadataStream.append( "    </rdf:Seq>\n" );
    6490           0 :                 aMetadataStream.append( "   </dc:creator>\n" );
    6491             :             }
    6492           0 :             if( !m_aContext.DocumentInfo.Subject.isEmpty() )
    6493             :             {
    6494             :                 // this is according to PDF/A-1, technical corrigendum 1 (2007-04-01)
    6495           0 :                 aMetadataStream.append( "   <dc:description>\n" );
    6496           0 :                 aMetadataStream.append( "    <rdf:Alt>\n" );
    6497           0 :                 aMetadataStream.append( "     <rdf:li xml:lang=\"x-default\">" );
    6498           0 :                 OUString aSubject;
    6499           0 :                 escapeStringXML( m_aContext.DocumentInfo.Subject, aSubject );
    6500           0 :                 aMetadataStream.append( OUStringToOString( aSubject , RTL_TEXTENCODING_UTF8 )  );
    6501           0 :                 aMetadataStream.append( "</rdf:li>\n" );
    6502           0 :                 aMetadataStream.append( "    </rdf:Alt>\n" );
    6503           0 :                 aMetadataStream.append( "   </dc:description>\n" );
    6504             :             }
    6505           0 :             aMetadataStream.append( "  </rdf:Description>\n" );
    6506             :         }
    6507             : 
    6508             :         //... PDF properties go here
    6509           0 :         if( !m_aContext.DocumentInfo.Producer.isEmpty() ||
    6510           0 :             !m_aContext.DocumentInfo.Keywords.isEmpty() )
    6511             :         {
    6512           0 :             aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6513           0 :             aMetadataStream.append( "     xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\">\n" );
    6514           0 :             if( !m_aContext.DocumentInfo.Producer.isEmpty() )
    6515             :             {
    6516           0 :                 aMetadataStream.append( "   <pdf:Producer>" );
    6517           0 :                 OUString aProducer;
    6518           0 :                 escapeStringXML( m_aContext.DocumentInfo.Producer, aProducer );
    6519           0 :                 aMetadataStream.append( OUStringToOString( aProducer , RTL_TEXTENCODING_UTF8 )  );
    6520           0 :                 aMetadataStream.append( "</pdf:Producer>\n" );
    6521             :             }
    6522           0 :             if( !m_aContext.DocumentInfo.Keywords.isEmpty() )
    6523             :             {
    6524           0 :                 aMetadataStream.append( "   <pdf:Keywords>" );
    6525           0 :                 OUString aKeywords;
    6526           0 :                 escapeStringXML( m_aContext.DocumentInfo.Keywords, aKeywords );
    6527           0 :                 aMetadataStream.append( OUStringToOString( aKeywords , RTL_TEXTENCODING_UTF8 )  );
    6528           0 :                 aMetadataStream.append( "</pdf:Keywords>\n" );
    6529             :             }
    6530           0 :             aMetadataStream.append( "  </rdf:Description>\n" );
    6531             :         }
    6532             : 
    6533           0 :         aMetadataStream.append( "  <rdf:Description rdf:about=\"\"\n" );
    6534           0 :         aMetadataStream.append( "    xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\">\n" );
    6535           0 :         if( !m_aContext.DocumentInfo.Creator.isEmpty() )
    6536             :         {
    6537           0 :             aMetadataStream.append( "   <xmp:CreatorTool>" );
    6538           0 :             OUString aCreator;
    6539           0 :             escapeStringXML( m_aContext.DocumentInfo.Creator, aCreator );
    6540           0 :             aMetadataStream.append( OUStringToOString( aCreator , RTL_TEXTENCODING_UTF8 )  );
    6541           0 :             aMetadataStream.append( "</xmp:CreatorTool>\n" );
    6542             :         }
    6543             :         //creation date
    6544           0 :         aMetadataStream.append( "   <xmp:CreateDate>" );
    6545           0 :         aMetadataStream.append( m_aCreationMetaDateString );
    6546           0 :         aMetadataStream.append( "</xmp:CreateDate>\n" );
    6547             : 
    6548           0 :         aMetadataStream.append( "  </rdf:Description>\n" );
    6549           0 :         aMetadataStream.append( " </rdf:RDF>\n" );
    6550           0 :         aMetadataStream.append( "</x:xmpmeta>\n" );
    6551             : 
    6552             :         //add the padding
    6553           0 :         for( sal_Int32 nSpaces = 1; nSpaces <= 2100; nSpaces++ )
    6554             :         {
    6555           0 :             aMetadataStream.append( " " );
    6556           0 :             if( nSpaces % 100 == 0 )
    6557           0 :                 aMetadataStream.append( "\n" );
    6558             :         }
    6559             : 
    6560           0 :         aMetadataStream.append( "<?xpacket end=\"w\"?>\n" );
    6561             : 
    6562           0 :         OStringBuffer aMetadataObj( 1024 );
    6563             : 
    6564           0 :         aMetadataObj.append( nObject );
    6565           0 :         aMetadataObj.append( " 0 obj\n" );
    6566             : 
    6567           0 :         aMetadataObj.append( "<</Type/Metadata/Subtype/XML/Length " );
    6568             : 
    6569           0 :         aMetadataObj.append( (sal_Int32) aMetadataStream.getLength() );
    6570           0 :         aMetadataObj.append( ">>\nstream\n" );
    6571           0 :         if ( !writeBuffer( aMetadataObj.getStr(), aMetadataObj.getLength() ) )
    6572           0 :             return 0;
    6573             :         //emit the stream
    6574           0 :         if ( !writeBuffer( aMetadataStream.getStr(), aMetadataStream.getLength() ) )
    6575           0 :             return 0;
    6576             : 
    6577           0 :         aMetadataObj.setLength( 0 );
    6578           0 :         aMetadataObj.append( "\nendstream\nendobj\n\n" );
    6579           0 :         if( ! writeBuffer( aMetadataObj.getStr(), aMetadataObj.getLength() ) )
    6580           0 :             nObject = 0;
    6581             :     }
    6582             :     else
    6583           0 :         nObject = 0;
    6584             : 
    6585           0 :     return nObject;
    6586             : }
    6587             : //<---i59651
    6588             : 
    6589           0 : bool PDFWriterImpl::emitTrailer()
    6590             : {
    6591             :     // emit doc info
    6592           0 :     sal_Int32 nDocInfoObject = emitInfoDict( );
    6593             : 
    6594           0 :     sal_Int32 nSecObject = 0;
    6595             : 
    6596           0 :     if( m_aContext.Encryption.Encrypt() )
    6597             :     {
    6598             :         //emit the security information
    6599             :         //must be emitted as indirect dictionary object, since
    6600             :         //Acrobat Reader 5 works only with this kind of implementation
    6601           0 :         nSecObject = createObject();
    6602             : 
    6603           0 :         if( updateObject( nSecObject ) )
    6604             :         {
    6605           0 :             OStringBuffer aLineS( 1024 );
    6606           0 :             aLineS.append( nSecObject );
    6607             :             aLineS.append( " 0 obj\n"
    6608           0 :                            "<</Filter/Standard/V " );
    6609             :             // check the version
    6610           0 :             if( m_aContext.Encryption.Security128bit )
    6611           0 :                 aLineS.append( "2/Length 128/R 3" );
    6612             :             else
    6613           0 :                 aLineS.append( "1/R 2" );
    6614             : 
    6615             :             // emit the owner password, must not be encrypted
    6616           0 :             aLineS.append( "/O(" );
    6617           0 :             appendLiteralString( (const sal_Char*)&m_aContext.Encryption.OValue[0], sal_Int32(m_aContext.Encryption.OValue.size()), aLineS );
    6618           0 :             aLineS.append( ")/U(" );
    6619           0 :             appendLiteralString( (const sal_Char*)&m_aContext.Encryption.UValue[0], sal_Int32(m_aContext.Encryption.UValue.size()), aLineS );
    6620           0 :             aLineS.append( ")/P " );// the permission set
    6621           0 :             aLineS.append( m_nAccessPermissions );
    6622           0 :             aLineS.append( ">>\nendobj\n\n" );
    6623           0 :             if( !writeBuffer( aLineS.getStr(), aLineS.getLength() ) )
    6624           0 :                 nSecObject = 0;
    6625             :         }
    6626             :         else
    6627           0 :             nSecObject = 0;
    6628             :     }
    6629             :     // emit xref table
    6630             :     // remember start
    6631           0 :     sal_uInt64 nXRefOffset = 0;
    6632           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nXRefOffset )) );
    6633           0 :     CHECK_RETURN( writeBuffer( "xref\n", 5 ) );
    6634             : 
    6635           0 :     sal_Int32 nObjects = m_aObjects.size();
    6636           0 :     OStringBuffer aLine;
    6637           0 :     aLine.append( "0 " );
    6638           0 :     aLine.append( (sal_Int32)(nObjects+1) );
    6639           0 :     aLine.append( "\n" );
    6640           0 :     aLine.append( "0000000000 65535 f \n" );
    6641           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6642             : 
    6643           0 :     for( sal_Int32 i = 0; i < nObjects; i++ )
    6644             :     {
    6645           0 :         aLine.setLength( 0 );
    6646           0 :         OString aOffset = OString::number( m_aObjects[i] );
    6647           0 :         for( sal_Int32 j = 0; j < (10-aOffset.getLength()); j++ )
    6648           0 :             aLine.append( '0' );
    6649           0 :         aLine.append( aOffset );
    6650           0 :         aLine.append( " 00000 n \n" );
    6651             :         DBG_ASSERT( aLine.getLength() == 20, "invalid xref entry" );
    6652           0 :         CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6653           0 :     }
    6654             : 
    6655             :     // prepare document checksum
    6656           0 :     OStringBuffer aDocChecksum( 2*RTL_DIGEST_LENGTH_MD5+1 );
    6657           0 :     if( m_aDocDigest )
    6658             :     {
    6659             :         sal_uInt8 nMD5Sum[ RTL_DIGEST_LENGTH_MD5 ];
    6660           0 :         rtl_digest_getMD5( m_aDocDigest, nMD5Sum, sizeof(nMD5Sum) );
    6661           0 :         for( unsigned int i = 0; i < RTL_DIGEST_LENGTH_MD5; i++ )
    6662           0 :             appendHex( nMD5Sum[i], aDocChecksum );
    6663             :     }
    6664             :     // document id set in setDocInfo method
    6665             :     // emit trailer
    6666           0 :     aLine.setLength( 0 );
    6667             :     aLine.append( "trailer\n"
    6668           0 :                   "<</Size " );
    6669           0 :     aLine.append( (sal_Int32)(nObjects+1) );
    6670           0 :     aLine.append( "/Root " );
    6671           0 :     aLine.append( m_nCatalogObject );
    6672           0 :     aLine.append( " 0 R\n" );
    6673           0 :     if( nSecObject )
    6674             :     {
    6675           0 :         aLine.append( "/Encrypt ");
    6676           0 :         aLine.append( nSecObject );
    6677           0 :         aLine.append( " 0 R\n" );
    6678             :     }
    6679           0 :     if( nDocInfoObject )
    6680             :     {
    6681           0 :         aLine.append( "/Info " );
    6682           0 :         aLine.append( nDocInfoObject );
    6683           0 :         aLine.append( " 0 R\n" );
    6684             :     }
    6685           0 :     if( ! m_aContext.Encryption.DocumentIdentifier.empty() )
    6686             :     {
    6687           0 :         aLine.append( "/ID [ <" );
    6688           0 :         for( std::vector< sal_uInt8 >::const_iterator it = m_aContext.Encryption.DocumentIdentifier.begin();
    6689           0 :              it != m_aContext.Encryption.DocumentIdentifier.end(); ++it )
    6690             :         {
    6691           0 :             appendHex( sal_Int8(*it), aLine );
    6692             :         }
    6693             :         aLine.append( ">\n"
    6694           0 :                       "<" );
    6695           0 :         for( std::vector< sal_uInt8 >::const_iterator it = m_aContext.Encryption.DocumentIdentifier.begin();
    6696           0 :              it != m_aContext.Encryption.DocumentIdentifier.end(); ++it )
    6697             :         {
    6698           0 :             appendHex( sal_Int8(*it), aLine );
    6699             :         }
    6700           0 :         aLine.append( "> ]\n" );
    6701             :     }
    6702           0 :     if( !aDocChecksum.isEmpty() )
    6703             :     {
    6704           0 :         aLine.append( "/DocChecksum /" );
    6705           0 :         aLine.append( aDocChecksum.makeStringAndClear() );
    6706           0 :         aLine.append( "\n" );
    6707             :     }
    6708           0 :     if( m_aAdditionalStreams.size() > 0 )
    6709             :     {
    6710           0 :         aLine.append( "/AdditionalStreams [" );
    6711           0 :         for( unsigned int i = 0; i < m_aAdditionalStreams.size(); i++ )
    6712             :         {
    6713           0 :             aLine.append( "/" );
    6714           0 :             appendName( m_aAdditionalStreams[i].m_aMimeType, aLine );
    6715           0 :             aLine.append( " " );
    6716           0 :             aLine.append( m_aAdditionalStreams[i].m_nStreamObject );
    6717           0 :             aLine.append( " 0 R\n" );
    6718             :         }
    6719           0 :         aLine.append( "]\n" );
    6720             :     }
    6721             :     aLine.append( ">>\n"
    6722           0 :                   "startxref\n" );
    6723           0 :     aLine.append( (sal_Int64)nXRefOffset );
    6724             :     aLine.append( "\n"
    6725           0 :                   "%%EOF\n" );
    6726           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    6727             : 
    6728           0 :     return true;
    6729             : }
    6730             : 
    6731             : struct AnnotationSortEntry
    6732             : {
    6733             :     sal_Int32 nTabOrder;
    6734             :     sal_Int32 nObject;
    6735             :     sal_Int32 nWidgetIndex;
    6736             : 
    6737           0 :     AnnotationSortEntry( sal_Int32 nTab, sal_Int32 nObj, sal_Int32 nI ) :
    6738             :         nTabOrder( nTab ),
    6739             :         nObject( nObj ),
    6740           0 :         nWidgetIndex( nI )
    6741           0 :     {}
    6742             : };
    6743             : 
    6744           0 : struct AnnotSortContainer
    6745             : {
    6746             :     std::set< sal_Int32 >               aObjects;
    6747             :     std::vector< AnnotationSortEntry >    aSortedAnnots;
    6748             : };
    6749             : 
    6750             : struct AnnotSorterLess
    6751             : {
    6752             :     std::vector< PDFWriterImpl::PDFWidget >& m_rWidgets;
    6753             : 
    6754           0 :     AnnotSorterLess( std::vector< PDFWriterImpl::PDFWidget >& rWidgets ) : m_rWidgets( rWidgets ) {}
    6755             : 
    6756           0 :     bool operator()( const AnnotationSortEntry& rLeft, const AnnotationSortEntry& rRight )
    6757             :     {
    6758           0 :         if( rLeft.nTabOrder < rRight.nTabOrder )
    6759           0 :             return true;
    6760           0 :         if( rRight.nTabOrder < rLeft.nTabOrder )
    6761           0 :             return false;
    6762           0 :         if( rLeft.nWidgetIndex < 0 && rRight.nWidgetIndex < 0 )
    6763           0 :             return false;
    6764           0 :         if( rRight.nWidgetIndex < 0 )
    6765           0 :             return true;
    6766           0 :         if( rLeft.nWidgetIndex < 0 )
    6767           0 :             return false;
    6768             :         // remember: widget rects are in PDF coordinates, so they are ordered down up
    6769           0 :         if( m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Top() >
    6770           0 :             m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Top() )
    6771           0 :             return true;
    6772           0 :         if( m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Top() >
    6773           0 :             m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Top() )
    6774           0 :             return false;
    6775           0 :         if( m_rWidgets[ rLeft.nWidgetIndex ].m_aRect.Left() <
    6776           0 :             m_rWidgets[ rRight.nWidgetIndex ].m_aRect.Left() )
    6777           0 :             return true;
    6778           0 :         return false;
    6779             :     }
    6780             : };
    6781             : 
    6782           0 : void PDFWriterImpl::sortWidgets()
    6783             : {
    6784             :     // sort widget annotations on each page as per their
    6785             :     // TabOrder attribute
    6786           0 :     boost::unordered_map< sal_Int32, AnnotSortContainer > sorted;
    6787           0 :     int nWidgets = m_aWidgets.size();
    6788           0 :     for( int nW = 0; nW < nWidgets; nW++ )
    6789             :     {
    6790           0 :         const PDFWidget& rWidget = m_aWidgets[nW];
    6791           0 :         if( rWidget.m_nPage >= 0 )
    6792             :         {
    6793           0 :             AnnotSortContainer& rCont = sorted[ rWidget.m_nPage ];
    6794             :             // optimize vector allocation
    6795           0 :             if( rCont.aSortedAnnots.empty() )
    6796           0 :                 rCont.aSortedAnnots.reserve( m_aPages[ rWidget.m_nPage ].m_aAnnotations.size() );
    6797             :             // insert widget to tab sorter
    6798             :             // RadioButtons are not page annotations, only their individual check boxes are
    6799           0 :             if( rWidget.m_eType != PDFWriter::RadioButton )
    6800             :             {
    6801           0 :                 rCont.aObjects.insert( rWidget.m_nObject );
    6802           0 :                 rCont.aSortedAnnots.push_back( AnnotationSortEntry( rWidget.m_nTabOrder, rWidget.m_nObject, nW ) );
    6803             :             }
    6804             :         }
    6805             :     }
    6806           0 :     for( boost::unordered_map< sal_Int32, AnnotSortContainer >::iterator it = sorted.begin(); it != sorted.end(); ++it )
    6807             :     {
    6808             :         // append entries for non widget annotations
    6809           0 :         PDFPage& rPage = m_aPages[ it->first ];
    6810           0 :         unsigned int nAnnots = rPage.m_aAnnotations.size();
    6811           0 :         for( unsigned int nA = 0; nA < nAnnots; nA++ )
    6812           0 :             if( it->second.aObjects.find( rPage.m_aAnnotations[nA] ) == it->second.aObjects.end())
    6813           0 :                 it->second.aSortedAnnots.push_back( AnnotationSortEntry( 10000, rPage.m_aAnnotations[nA], -1 ) );
    6814             : 
    6815           0 :         AnnotSorterLess aLess( m_aWidgets );
    6816           0 :         std::stable_sort( it->second.aSortedAnnots.begin(), it->second.aSortedAnnots.end(), aLess );
    6817             :         // sanity check
    6818           0 :         if( it->second.aSortedAnnots.size() == nAnnots)
    6819             :         {
    6820           0 :             for( unsigned int nA = 0; nA < nAnnots; nA++ )
    6821           0 :                 rPage.m_aAnnotations[nA] = it->second.aSortedAnnots[nA].nObject;
    6822             :         }
    6823             :         else
    6824             :         {
    6825             :             DBG_ASSERT( false, "wrong number of sorted annotations" );
    6826             :             #if OSL_DEBUG_LEVEL > 0
    6827             :             fprintf( stderr, "PDFWriterImpl::sortWidgets(): wrong number of sorted assertions on page nr %ld\n"
    6828             :                      "    %ld sorted and %ld unsorted\n", (long int)it->first, (long int)it->second.aSortedAnnots.size(), (long int)nAnnots );
    6829             :             #endif
    6830             :         }
    6831           0 :     }
    6832             : 
    6833             :     // FIXME: implement tab order in structure tree for PDF 1.5
    6834           0 : }
    6835             : 
    6836             : namespace vcl {
    6837             : class PDFStreamIf :
    6838             :         public cppu::WeakImplHelper1< com::sun::star::io::XOutputStream >
    6839             : {
    6840             :     PDFWriterImpl*  m_pWriter;
    6841             :     bool            m_bWrite;
    6842             :     public:
    6843           0 :     PDFStreamIf( PDFWriterImpl* pWriter ) : m_pWriter( pWriter ), m_bWrite( true ) {}
    6844             :     virtual ~PDFStreamIf();
    6845             : 
    6846             :     virtual void SAL_CALL writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw(std::exception) SAL_OVERRIDE;
    6847             :     virtual void SAL_CALL flush() throw(std::exception) SAL_OVERRIDE;
    6848             :     virtual void SAL_CALL closeOutput() throw(std::exception) SAL_OVERRIDE;
    6849             : };
    6850             : }
    6851             : 
    6852           0 : PDFStreamIf::~PDFStreamIf()
    6853             : {
    6854           0 : }
    6855             : 
    6856           0 : void SAL_CALL  PDFStreamIf::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw(std::exception)
    6857             : {
    6858           0 :     if( m_bWrite && aData.getLength() )
    6859             :     {
    6860           0 :         sal_Int32 nBytes = aData.getLength();
    6861           0 :         m_pWriter->writeBuffer( aData.getConstArray(), nBytes );
    6862             :     }
    6863           0 : }
    6864             : 
    6865           0 : void SAL_CALL PDFStreamIf::flush() throw(std::exception)
    6866             : {
    6867           0 : }
    6868             : 
    6869           0 : void SAL_CALL PDFStreamIf::closeOutput() throw(std::exception)
    6870             : {
    6871           0 :     m_bWrite = false;
    6872           0 : }
    6873             : 
    6874           0 : bool PDFWriterImpl::emitAdditionalStreams()
    6875             : {
    6876           0 :     unsigned int nStreams = m_aAdditionalStreams.size();
    6877           0 :     for( unsigned int i = 0; i < nStreams; i++ )
    6878             :     {
    6879           0 :         PDFAddStream& rStream = m_aAdditionalStreams[i];
    6880           0 :         rStream.m_nStreamObject = createObject();
    6881           0 :         sal_Int32 nSizeObject = createObject();
    6882             : 
    6883           0 :         if( ! updateObject( rStream.m_nStreamObject ) )
    6884           0 :             return false;
    6885             : 
    6886           0 :         OStringBuffer aLine;
    6887           0 :         aLine.append( rStream.m_nStreamObject );
    6888           0 :         aLine.append( " 0 obj\n<</Length " );
    6889           0 :         aLine.append( nSizeObject );
    6890           0 :         aLine.append( " 0 R" );
    6891           0 :         if( rStream.m_bCompress )
    6892           0 :             aLine.append( "/Filter/FlateDecode" );
    6893           0 :         aLine.append( ">>\nstream\n" );
    6894           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6895           0 :             return false;
    6896           0 :         sal_uInt64 nBeginStreamPos = 0, nEndStreamPos = 0;
    6897           0 :         if( osl::File::E_None != m_aFile.getPos(nBeginStreamPos) )
    6898             :         {
    6899           0 :             m_aFile.close();
    6900           0 :             m_bOpen = false;
    6901             :         }
    6902           0 :         if( rStream.m_bCompress )
    6903           0 :             beginCompression();
    6904             : 
    6905           0 :         checkAndEnableStreamEncryption( rStream.m_nStreamObject );
    6906           0 :         com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > xStream( new PDFStreamIf( this ) );
    6907             :         assert(rStream.m_pStream);
    6908           0 :         if (!rStream.m_pStream)
    6909           0 :             return false;
    6910           0 :         rStream.m_pStream->write( xStream );
    6911           0 :         xStream.clear();
    6912           0 :         delete rStream.m_pStream;
    6913           0 :         rStream.m_pStream = NULL;
    6914           0 :         disableStreamEncryption();
    6915             : 
    6916           0 :         if( rStream.m_bCompress )
    6917           0 :             endCompression();
    6918             : 
    6919           0 :         if (osl::File::E_None != m_aFile.getPos(nEndStreamPos))
    6920             :         {
    6921           0 :             m_aFile.close();
    6922           0 :             m_bOpen = false;
    6923           0 :             return false;
    6924             :         }
    6925           0 :         if( ! writeBuffer( "\nendstream\nendobj\n\n", 19 ) )
    6926           0 :             return false ;
    6927             :         // emit stream length object
    6928           0 :         if( ! updateObject( nSizeObject ) )
    6929           0 :             return false;
    6930           0 :         aLine.setLength( 0 );
    6931           0 :         aLine.append( nSizeObject );
    6932           0 :         aLine.append( " 0 obj\n" );
    6933           0 :         aLine.append( (sal_Int64)(nEndStreamPos-nBeginStreamPos) );
    6934           0 :         aLine.append( "\nendobj\n\n" );
    6935           0 :         if( ! writeBuffer( aLine.getStr(), aLine.getLength() ) )
    6936           0 :             return false;
    6937           0 :     }
    6938           0 :     return true;
    6939             : }
    6940             : 
    6941           0 : bool PDFWriterImpl::emit()
    6942             : {
    6943           0 :     endPage();
    6944             : 
    6945             :     // resort structure tree and annotations if necessary
    6946             :     // needed for widget tab order
    6947           0 :     sortWidgets();
    6948             : 
    6949             : #if !defined(ANDROID) && !defined(IOS)
    6950           0 :     if( m_aContext.SignPDF )
    6951             :     {
    6952             :         // sign the document
    6953           0 :         PDFWriter::SignatureWidget aSignature;
    6954           0 :         aSignature.Name = "Signature1";
    6955           0 :         createControl( aSignature, 0 );
    6956             :     }
    6957             : #endif
    6958             : 
    6959             :     // emit additional streams
    6960           0 :     CHECK_RETURN( emitAdditionalStreams() );
    6961             : 
    6962             :     // emit catalog
    6963           0 :     CHECK_RETURN( emitCatalog() );
    6964             : 
    6965             : #if !defined(ANDROID) && !defined(IOS)
    6966           0 :     if (m_nSignatureObject != -1) // if document is signed, emit sigdict
    6967           0 :         CHECK_RETURN( emitSignature() );
    6968             : #endif
    6969             : 
    6970             :     // emit trailer
    6971           0 :     CHECK_RETURN( emitTrailer() );
    6972             : 
    6973             : #if !defined(ANDROID) && !defined(IOS)
    6974           0 :     if (m_nSignatureObject != -1) // finalize the signature
    6975           0 :         CHECK_RETURN( finalizeSignature() );
    6976             : #endif
    6977             : 
    6978           0 :     m_aFile.close();
    6979           0 :     m_bOpen = false;
    6980             : 
    6981           0 :     return true;
    6982             : }
    6983             : 
    6984             : 
    6985           0 : sal_Int32 PDFWriterImpl::getSystemFont( const vcl::Font& i_rFont )
    6986             : {
    6987           0 :     getReferenceDevice()->Push();
    6988           0 :     getReferenceDevice()->SetFont( i_rFont );
    6989           0 :     getReferenceDevice()->ImplNewFont();
    6990             : 
    6991           0 :     const PhysicalFontFace* pDevFont = m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData;
    6992           0 :     sal_Int32 nFontID = 0;
    6993           0 :     FontEmbedData::iterator it = m_aSystemFonts.find( pDevFont );
    6994           0 :     if( it != m_aSystemFonts.end() )
    6995           0 :         nFontID = it->second.m_nNormalFontID;
    6996             :     else
    6997             :     {
    6998           0 :         nFontID = m_nNextFID++;
    6999           0 :         m_aSystemFonts[ pDevFont ] = EmbedFont();
    7000           0 :         m_aSystemFonts[ pDevFont ].m_nNormalFontID = nFontID;
    7001             :     }
    7002             : 
    7003           0 :     getReferenceDevice()->Pop();
    7004           0 :     getReferenceDevice()->ImplNewFont();
    7005             : 
    7006           0 :     return nFontID;
    7007             : }
    7008             : 
    7009           0 : void PDFWriterImpl::registerGlyphs( int nGlyphs,
    7010             :                                     sal_GlyphId* pGlyphs,
    7011             :                                     sal_Int32* pGlyphWidths,
    7012             :                                     sal_Ucs* pUnicodes,
    7013             :                                     sal_Int32* pUnicodesPerGlyph,
    7014             :                                     sal_uInt8* pMappedGlyphs,
    7015             :                                     sal_Int32* pMappedFontObjects,
    7016             :                                     const PhysicalFontFace* pFallbackFonts[] )
    7017             : {
    7018           0 :     const PhysicalFontFace* pDevFont = m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData;
    7019           0 :     sal_Ucs* pCurUnicode = pUnicodes;
    7020           0 :     for( int i = 0; i < nGlyphs; pCurUnicode += pUnicodesPerGlyph[i] , i++ )
    7021             :     {
    7022           0 :         const int nFontGlyphId = pGlyphs[i] & (GF_IDXMASK | GF_ISCHAR | GF_GSUB);
    7023           0 :         const PhysicalFontFace* pCurrentFont = pFallbackFonts[i] ? pFallbackFonts[i] : pDevFont;
    7024             : 
    7025           0 :         if( pCurrentFont->mbSubsettable )
    7026             :         {
    7027           0 :             FontSubset& rSubset = m_aSubsets[ pCurrentFont ];
    7028             :             // search for font specific glyphID
    7029           0 :             FontMapping::iterator it = rSubset.m_aMapping.find( nFontGlyphId );
    7030           0 :             if( it != rSubset.m_aMapping.end() )
    7031             :             {
    7032           0 :                 pMappedFontObjects[i] = it->second.m_nFontID;
    7033           0 :                 pMappedGlyphs[i] = it->second.m_nSubsetGlyphID;
    7034             :             }
    7035             :             else
    7036             :             {
    7037             :                 // create new subset if necessary
    7038           0 :                 if( rSubset.m_aSubsets.empty()
    7039           0 :                 || (rSubset.m_aSubsets.back().m_aMapping.size() > 254) )
    7040             :                 {
    7041           0 :                     rSubset.m_aSubsets.push_back( FontEmit( m_nNextFID++ ) );
    7042             :                 }
    7043             : 
    7044             :                 // copy font id
    7045           0 :                 pMappedFontObjects[i] = rSubset.m_aSubsets.back().m_nFontID;
    7046             :                 // create new glyph in subset
    7047           0 :                 sal_uInt8 nNewId = sal::static_int_cast<sal_uInt8>(rSubset.m_aSubsets.back().m_aMapping.size()+1);
    7048           0 :                 pMappedGlyphs[i] = nNewId;
    7049             : 
    7050             :                 // add new glyph to emitted font subset
    7051           0 :                 GlyphEmit& rNewGlyphEmit = rSubset.m_aSubsets.back().m_aMapping[ nFontGlyphId ];
    7052           0 :                 rNewGlyphEmit.setGlyphId( nNewId );
    7053           0 :                 for( sal_Int32 n = 0; n < pUnicodesPerGlyph[i]; n++ )
    7054           0 :                     rNewGlyphEmit.addCode( pCurUnicode[n] );
    7055             : 
    7056             :                 // add new glyph to font mapping
    7057           0 :                 Glyph& rNewGlyph = rSubset.m_aMapping[ nFontGlyphId ];
    7058           0 :                 rNewGlyph.m_nFontID = pMappedFontObjects[i];
    7059           0 :                 rNewGlyph.m_nSubsetGlyphID = nNewId;
    7060             :             }
    7061           0 :             getReferenceDevice()->AcquireGraphics();
    7062           0 :             const bool bVertical = ((pGlyphs[i] & GF_ROTMASK) != 0);
    7063           0 :             pGlyphWidths[i] = m_aFontCache.getGlyphWidth( pCurrentFont,
    7064             :                                                           nFontGlyphId,
    7065             :                                                           bVertical,
    7066           0 :                                                           m_pReferenceDevice->mpGraphics );
    7067             :         }
    7068           0 :         else if( pCurrentFont->IsEmbeddable() )
    7069             :         {
    7070           0 :             sal_Int32 nFontID = 0;
    7071           0 :             FontEmbedData::iterator it = m_aEmbeddedFonts.find( pCurrentFont );
    7072           0 :             if( it != m_aEmbeddedFonts.end() )
    7073           0 :                 nFontID = it->second.m_nNormalFontID;
    7074             :             else
    7075             :             {
    7076           0 :                 nFontID = m_nNextFID++;
    7077           0 :                 m_aEmbeddedFonts[ pCurrentFont ] = EmbedFont();
    7078           0 :                 m_aEmbeddedFonts[ pCurrentFont ].m_nNormalFontID = nFontID;
    7079             :             }
    7080           0 :             EmbedFont& rEmbedFont = m_aEmbeddedFonts[pCurrentFont];
    7081             : 
    7082           0 :             const Ucs2SIntMap* pEncoding = NULL;
    7083           0 :             const Ucs2OStrMap* pNonEncoded = NULL;
    7084           0 :             getReferenceDevice()->AcquireGraphics();
    7085           0 :             pEncoding = m_pReferenceDevice->mpGraphics->GetFontEncodingVector( pCurrentFont, &pNonEncoded );
    7086             : 
    7087           0 :             Ucs2SIntMap::const_iterator enc_it;
    7088           0 :             Ucs2OStrMap::const_iterator nonenc_it;
    7089             : 
    7090           0 :             sal_Int32 nCurFontID = nFontID;
    7091           0 :             sal_Ucs cChar = *pCurUnicode;
    7092           0 :             if( pEncoding )
    7093             :             {
    7094           0 :                 enc_it = pEncoding->find( cChar );
    7095           0 :                 if( enc_it != pEncoding->end() && enc_it->second > 0 )
    7096             :                 {
    7097             :                     DBG_ASSERT( (enc_it->second & 0xffffff00) == 0, "Invalid character code" );
    7098           0 :                     cChar = (sal_Ucs)enc_it->second;
    7099             :                 }
    7100           0 :                 else if( (enc_it == pEncoding->end() || enc_it->second == -1) &&
    7101           0 :                          pNonEncoded &&
    7102           0 :                          (nonenc_it = pNonEncoded->find( cChar )) != pNonEncoded->end() )
    7103             :                 {
    7104           0 :                     nCurFontID = 0;
    7105             :                     // find non encoded glyph
    7106           0 :                     for( std::list< EmbedEncoding >::iterator nec_it = rEmbedFont.m_aExtendedEncodings.begin(); nec_it != rEmbedFont.m_aExtendedEncodings.end(); ++nec_it )
    7107             :                     {
    7108           0 :                         if( nec_it->m_aCMap.find( cChar ) != nec_it->m_aCMap.end() )
    7109             :                         {
    7110           0 :                             nCurFontID = nec_it->m_nFontID;
    7111           0 :                             cChar = (sal_Ucs)nec_it->m_aCMap[ cChar ];
    7112           0 :                             break;
    7113             :                         }
    7114             :                     }
    7115           0 :                     if( nCurFontID == 0 ) // new nonencoded glyph
    7116             :                     {
    7117           0 :                         if( rEmbedFont.m_aExtendedEncodings.empty() || rEmbedFont.m_aExtendedEncodings.back().m_aEncVector.size() == 255 )
    7118             :                         {
    7119           0 :                             rEmbedFont.m_aExtendedEncodings.push_back( EmbedEncoding() );
    7120           0 :                             rEmbedFont.m_aExtendedEncodings.back().m_nFontID = m_nNextFID++;
    7121             :                         }
    7122           0 :                         EmbedEncoding& rEncoding = rEmbedFont.m_aExtendedEncodings.back();
    7123           0 :                         rEncoding.m_aEncVector.push_back( EmbedCode() );
    7124           0 :                         rEncoding.m_aEncVector.back().m_aUnicode = cChar;
    7125           0 :                         rEncoding.m_aEncVector.back().m_aName = nonenc_it->second;
    7126           0 :                         rEncoding.m_aCMap[ cChar ] = (sal_Int8)(rEncoding.m_aEncVector.size()-1);
    7127           0 :                         nCurFontID = rEncoding.m_nFontID;
    7128           0 :                         cChar = (sal_Ucs)rEncoding.m_aCMap[ cChar ];
    7129             :                     }
    7130             :                 }
    7131             :                 else
    7132           0 :                     pEncoding = NULL;
    7133             :             }
    7134           0 :             if( ! pEncoding )
    7135             :             {
    7136           0 :                 if( cChar & 0xff00 )
    7137             :                 {
    7138             :                     // some characters can be used by conversion
    7139           0 :                     if( cChar >= 0xf000 && cChar <= 0xf0ff ) // symbol encoding in private use area
    7140           0 :                         cChar -= 0xf000;
    7141             :                     else
    7142             :                     {
    7143           0 :                         OString aChar(&cChar, 1, RTL_TEXTENCODING_MS_1252);
    7144           0 :                         cChar = ((sal_Ucs)aChar[0]) & 0x00ff;
    7145             :                     }
    7146             :                 }
    7147             :             }
    7148             : 
    7149           0 :             pMappedGlyphs[ i ] = (sal_Int8)cChar;
    7150           0 :             pMappedFontObjects[ i ] = nCurFontID;
    7151           0 :             pGlyphWidths[ i ] = m_aFontCache.getGlyphWidth( pCurrentFont,
    7152             :                                                             (pEncoding ? *pCurUnicode : cChar) | GF_ISCHAR,
    7153             :                                                             false,
    7154           0 :                                                             m_pReferenceDevice->mpGraphics );
    7155             :         }
    7156             :     }
    7157           0 : }
    7158             : 
    7159           0 : void PDFWriterImpl::drawRelief( SalLayout& rLayout, const OUString& rText, bool bTextLines )
    7160             : {
    7161           0 :     push( PushFlags::ALL );
    7162             : 
    7163           0 :     FontRelief eRelief = m_aCurrentPDFState.m_aFont.GetRelief();
    7164             : 
    7165           0 :     Color aTextColor = m_aCurrentPDFState.m_aFont.GetColor();
    7166           0 :     Color aTextLineColor = m_aCurrentPDFState.m_aTextLineColor;
    7167           0 :     Color aOverlineColor = m_aCurrentPDFState.m_aOverlineColor;
    7168           0 :     Color aReliefColor( COL_LIGHTGRAY );
    7169           0 :     if( aTextColor == COL_BLACK )
    7170           0 :         aTextColor = Color( COL_WHITE );
    7171           0 :     if( aTextLineColor == COL_BLACK )
    7172           0 :         aTextLineColor = Color( COL_WHITE );
    7173           0 :     if( aOverlineColor == COL_BLACK )
    7174           0 :         aOverlineColor = Color( COL_WHITE );
    7175           0 :     if( aTextColor == COL_WHITE )
    7176           0 :         aReliefColor = Color( COL_BLACK );
    7177             : 
    7178           0 :     Font aSetFont = m_aCurrentPDFState.m_aFont;
    7179           0 :     aSetFont.SetRelief( RELIEF_NONE );
    7180           0 :     aSetFont.SetShadow( false );
    7181             : 
    7182           0 :     aSetFont.SetColor( aReliefColor );
    7183           0 :     setTextLineColor( aReliefColor );
    7184           0 :     setOverlineColor( aReliefColor );
    7185           0 :     setFont( aSetFont );
    7186           0 :     long nOff = 1 + getReferenceDevice()->mnDPIX/300;
    7187           0 :     if( eRelief == RELIEF_ENGRAVED )
    7188           0 :         nOff = -nOff;
    7189             : 
    7190           0 :     rLayout.DrawOffset() += Point( nOff, nOff );
    7191           0 :     updateGraphicsState();
    7192           0 :     drawLayout( rLayout, rText, bTextLines );
    7193             : 
    7194           0 :     rLayout.DrawOffset() -= Point( nOff, nOff );
    7195           0 :     setTextLineColor( aTextLineColor );
    7196           0 :     setOverlineColor( aOverlineColor );
    7197           0 :     aSetFont.SetColor( aTextColor );
    7198           0 :     setFont( aSetFont );
    7199           0 :     updateGraphicsState();
    7200           0 :     drawLayout( rLayout, rText, bTextLines );
    7201             : 
    7202             :     // clean up the mess
    7203           0 :     pop();
    7204           0 : }
    7205             : 
    7206           0 : void PDFWriterImpl::drawShadow( SalLayout& rLayout, const OUString& rText, bool bTextLines )
    7207             : {
    7208           0 :     Font aSaveFont = m_aCurrentPDFState.m_aFont;
    7209           0 :     Color aSaveTextLineColor = m_aCurrentPDFState.m_aTextLineColor;
    7210           0 :     Color aSaveOverlineColor = m_aCurrentPDFState.m_aOverlineColor;
    7211             : 
    7212           0 :     Font& rFont = m_aCurrentPDFState.m_aFont;
    7213           0 :     if( rFont.GetColor() == Color( COL_BLACK ) || rFont.GetColor().GetLuminance() < 8 )
    7214           0 :         rFont.SetColor( Color( COL_LIGHTGRAY ) );
    7215             :     else
    7216           0 :         rFont.SetColor( Color( COL_BLACK ) );
    7217           0 :     rFont.SetShadow( false );
    7218           0 :     rFont.SetOutline( false );
    7219           0 :     setFont( rFont );
    7220           0 :     setTextLineColor( rFont.GetColor() );
    7221           0 :     setOverlineColor( rFont.GetColor() );
    7222           0 :     updateGraphicsState();
    7223             : 
    7224           0 :     long nOff = 1 + ((m_pReferenceDevice->mpFontEntry->mnLineHeight-24)/24);
    7225           0 :     if( rFont.IsOutline() )
    7226           0 :         nOff++;
    7227           0 :     rLayout.DrawBase() += Point( nOff, nOff );
    7228           0 :     drawLayout( rLayout, rText, bTextLines );
    7229           0 :     rLayout.DrawBase() -= Point( nOff, nOff );
    7230             : 
    7231           0 :     setFont( aSaveFont );
    7232           0 :     setTextLineColor( aSaveTextLineColor );
    7233           0 :     setOverlineColor( aSaveOverlineColor );
    7234           0 :     updateGraphicsState();
    7235           0 : }
    7236             : 
    7237           0 : void PDFWriterImpl::drawVerticalGlyphs(
    7238             :         const std::vector<PDFWriterImpl::PDFGlyph>& rGlyphs,
    7239             :         OStringBuffer& rLine,
    7240             :         const Point& rAlignOffset,
    7241             :         const Matrix3& rRotScale,
    7242             :         double fAngle,
    7243             :         double fXScale,
    7244             :         double fSkew,
    7245             :         sal_Int32 nFontHeight )
    7246             : {
    7247           0 :     long nXOffset = 0;
    7248           0 :     Point aCurPos( rGlyphs[0].m_aPos );
    7249           0 :     aCurPos = m_pReferenceDevice->PixelToLogic( aCurPos );
    7250           0 :     aCurPos += rAlignOffset;
    7251           0 :     for( size_t i = 0; i < rGlyphs.size(); i++ )
    7252             :     {
    7253             :         // have to emit each glyph on its own
    7254           0 :         double fDeltaAngle = 0.0;
    7255           0 :         double fYScale = 1.0;
    7256           0 :         double fTempXScale = fXScale;
    7257           0 :         double fSkewB = fSkew;
    7258           0 :         double fSkewA = 0.0;
    7259             : 
    7260           0 :         Point aDeltaPos;
    7261           0 :         if( ( rGlyphs[i].m_nGlyphId & GF_ROTMASK ) == GF_ROTL )
    7262             :         {
    7263           0 :             fDeltaAngle = M_PI/2.0;
    7264           0 :             aDeltaPos.X() = m_pReferenceDevice->GetFontMetric().GetAscent();
    7265           0 :             aDeltaPos.Y() = (int)((double)m_pReferenceDevice->GetFontMetric().GetDescent() * fXScale);
    7266           0 :             fYScale = fXScale;
    7267           0 :             fTempXScale = 1.0;
    7268           0 :             fSkewA = -fSkewB;
    7269           0 :             fSkewB = 0.0;
    7270             :         }
    7271           0 :         else if( ( rGlyphs[i].m_nGlyphId & GF_ROTMASK ) == GF_ROTR )
    7272             :         {
    7273           0 :             fDeltaAngle = -M_PI/2.0;
    7274           0 :             aDeltaPos.X() = (int)((double)m_pReferenceDevice->GetFontMetric().GetDescent()*fXScale);
    7275           0 :             aDeltaPos.Y() = -m_pReferenceDevice->GetFontMetric().GetAscent();
    7276           0 :             fYScale = fXScale;
    7277           0 :             fTempXScale = 1.0;
    7278           0 :             fSkewA = fSkewB;
    7279           0 :             fSkewB = 0.0;
    7280             :         }
    7281           0 :         aDeltaPos += (m_pReferenceDevice->PixelToLogic( Point( (int)((double)nXOffset/fXScale), 0 ) ) - m_pReferenceDevice->PixelToLogic( Point() ) );
    7282           0 :         if( i < rGlyphs.size()-1 )
    7283             :         // #i120627# the text on the Y axis is reversed when export ppt file to PDF format
    7284             :         {
    7285           0 :             long nOffsetX = rGlyphs[i+1].m_aPos.X() - rGlyphs[i].m_aPos.X();
    7286           0 :             long nOffsetY = rGlyphs[i+1].m_aPos.Y() - rGlyphs[i].m_aPos.Y();
    7287           0 :             nXOffset += (int)sqrt(double(nOffsetX*nOffsetX + nOffsetY*nOffsetY));
    7288             :         }
    7289           0 :         if( ! rGlyphs[i].m_nGlyphId )
    7290           0 :             continue;
    7291             : 
    7292           0 :         aDeltaPos = rRotScale.transform( aDeltaPos );
    7293             : 
    7294           0 :         Matrix3 aMat;
    7295           0 :         if( fSkewB != 0.0 || fSkewA != 0.0 )
    7296           0 :             aMat.skew( fSkewA, fSkewB );
    7297           0 :         aMat.scale( fTempXScale, fYScale );
    7298           0 :         aMat.rotate( fAngle+fDeltaAngle );
    7299           0 :         aMat.translate( aCurPos.X()+aDeltaPos.X(), aCurPos.Y()+aDeltaPos.Y() );
    7300           0 :         aMat.append( m_aPages.back(), rLine );
    7301           0 :         rLine.append( " Tm" );
    7302           0 :         if( i == 0 || rGlyphs[i-1].m_nMappedFontId != rGlyphs[i].m_nMappedFontId )
    7303             :         {
    7304           0 :             rLine.append( " /F" );
    7305           0 :             rLine.append( rGlyphs[i].m_nMappedFontId );
    7306           0 :             rLine.append( ' ' );
    7307           0 :             m_aPages.back().appendMappedLength( nFontHeight, rLine, true );
    7308           0 :             rLine.append( " Tf" );
    7309             :         }
    7310           0 :         rLine.append( "<" );
    7311           0 :         appendHex( rGlyphs[i].m_nMappedGlyphId, rLine );
    7312           0 :         rLine.append( ">Tj\n" );
    7313           0 :     }
    7314           0 : }
    7315             : 
    7316           0 : void PDFWriterImpl::drawHorizontalGlyphs(
    7317             :         const std::vector<PDFWriterImpl::PDFGlyph>& rGlyphs,
    7318             :         OStringBuffer& rLine,
    7319             :         const Point& rAlignOffset,
    7320             :         double fAngle,
    7321             :         double fXScale,
    7322             :         double fSkew,
    7323             :         sal_Int32 nFontHeight,
    7324             :         sal_Int32 nPixelFontHeight
    7325             :         )
    7326             : {
    7327             :     // horizontal (= normal) case
    7328             : 
    7329             :     // fill in  run end indices
    7330             :     // end is marked by index of the first glyph of the next run
    7331             :     // a run is marked by same mapped font id and same Y position
    7332           0 :     std::vector< sal_uInt32 > aRunEnds;
    7333           0 :     aRunEnds.reserve( rGlyphs.size() );
    7334           0 :     for( size_t i = 1; i < rGlyphs.size(); i++ )
    7335             :     {
    7336           0 :         if( rGlyphs[i].m_nMappedFontId != rGlyphs[i-1].m_nMappedFontId ||
    7337           0 :             rGlyphs[i].m_aPos.Y() != rGlyphs[i-1].m_aPos.Y() )
    7338             :         {
    7339           0 :             aRunEnds.push_back(i);
    7340             :         }
    7341             :     }
    7342             :     // last run ends at last glyph
    7343           0 :     aRunEnds.push_back( rGlyphs.size() );
    7344             : 
    7345             :     // loop over runs of the same font
    7346           0 :     sal_uInt32 nBeginRun = 0;
    7347           0 :     for( size_t nRun = 0; nRun < aRunEnds.size(); nRun++ )
    7348             :     {
    7349             :         // setup text matrix
    7350           0 :         Point aCurPos = rGlyphs[nBeginRun].m_aPos;
    7351             :         // back transformation to current coordinate system
    7352           0 :         aCurPos = m_pReferenceDevice->PixelToLogic( aCurPos );
    7353           0 :         aCurPos += rAlignOffset;
    7354             :         // the first run can be set with "Td" operator
    7355             :         // subsequent use of that operator would move
    7356             :         // the texline matrix relative to what was set before
    7357             :         // making use of that would drive us into rounding issues
    7358           0 :         Matrix3 aMat;
    7359           0 :         if( nRun == 0 && fAngle == 0.0 && fXScale == 1.0 && fSkew == 0.0 )
    7360             :         {
    7361           0 :             m_aPages.back().appendPoint( aCurPos, rLine, false );
    7362           0 :             rLine.append( " Td " );
    7363             :         }
    7364             :         else
    7365             :         {
    7366           0 :             if( fSkew != 0.0 )
    7367           0 :                 aMat.skew( 0.0, fSkew );
    7368           0 :             aMat.scale( fXScale, 1.0 );
    7369           0 :             aMat.rotate( fAngle );
    7370           0 :             aMat.translate( aCurPos.X(), aCurPos.Y() );
    7371           0 :             aMat.append( m_aPages.back(), rLine );
    7372           0 :             rLine.append( " Tm\n" );
    7373             :         }
    7374             :         // set up correct font
    7375           0 :         rLine.append( "/F" );
    7376           0 :         rLine.append( rGlyphs[nBeginRun].m_nMappedFontId );
    7377           0 :         rLine.append( ' ' );
    7378           0 :         m_aPages.back().appendMappedLength( nFontHeight, rLine, true );
    7379           0 :         rLine.append( " Tf" );
    7380             : 
    7381             :         // output glyphs using Tj or TJ
    7382           0 :         OStringBuffer aKernedLine( 256 ), aUnkernedLine( 256 );
    7383           0 :         aKernedLine.append( "[<" );
    7384           0 :         aUnkernedLine.append( '<' );
    7385           0 :         appendHex( rGlyphs[nBeginRun].m_nMappedGlyphId, aKernedLine );
    7386           0 :         appendHex( rGlyphs[nBeginRun].m_nMappedGlyphId, aUnkernedLine );
    7387             : 
    7388           0 :         aMat.invert();
    7389           0 :         bool bNeedKern = false;
    7390           0 :         for( sal_uInt32 nPos = nBeginRun+1; nPos < aRunEnds[nRun]; nPos++ )
    7391             :         {
    7392           0 :             appendHex( rGlyphs[nPos].m_nMappedGlyphId, aUnkernedLine );
    7393             :             // check if default glyph positioning is sufficient
    7394           0 :             const Point aThisPos = aMat.transform( rGlyphs[nPos].m_aPos );
    7395           0 :             const Point aPrevPos = aMat.transform( rGlyphs[nPos-1].m_aPos );
    7396           0 :             double fAdvance = aThisPos.X() - aPrevPos.X();
    7397           0 :             fAdvance *= 1000.0 / nPixelFontHeight;
    7398           0 :             const sal_Int32 nAdjustment = (sal_Int32)(rGlyphs[nPos-1].m_nNativeWidth - fAdvance + 0.5);
    7399           0 :             if( nAdjustment != 0 )
    7400             :             {
    7401             :                 // apply individual glyph positioning
    7402           0 :                 bNeedKern = true;
    7403           0 :                 aKernedLine.append( ">" );
    7404           0 :                 aKernedLine.append( nAdjustment );
    7405           0 :                 aKernedLine.append( "<" );
    7406             :             }
    7407           0 :             appendHex( rGlyphs[nPos].m_nMappedGlyphId, aKernedLine );
    7408             :         }
    7409           0 :         aKernedLine.append( ">]TJ\n" );
    7410           0 :         aUnkernedLine.append( ">Tj\n" );
    7411             :         rLine.append(
    7412           0 :             (bNeedKern ? aKernedLine : aUnkernedLine).makeStringAndClear() );
    7413             : 
    7414             :         // set beginning of next run
    7415           0 :         nBeginRun = aRunEnds[nRun];
    7416           0 :     }
    7417           0 : }
    7418             : 
    7419           0 : void PDFWriterImpl::drawLayout( SalLayout& rLayout, const OUString& rText, bool bTextLines )
    7420             : {
    7421             :     // relief takes precedence over shadow (see outdev3.cxx)
    7422           0 :     if(  m_aCurrentPDFState.m_aFont.GetRelief() != RELIEF_NONE )
    7423             :     {
    7424           0 :         drawRelief( rLayout, rText, bTextLines );
    7425           0 :         return;
    7426             :     }
    7427           0 :     else if( m_aCurrentPDFState.m_aFont.IsShadow() )
    7428           0 :         drawShadow( rLayout, rText, bTextLines );
    7429             : 
    7430           0 :     OStringBuffer aLine( 512 );
    7431             : 
    7432           0 :     const int nMaxGlyphs = 256;
    7433             : 
    7434             :     sal_GlyphId pGlyphs[nMaxGlyphs];
    7435             :     sal_Int32 pGlyphWidths[nMaxGlyphs];
    7436             :     sal_uInt8 pMappedGlyphs[nMaxGlyphs];
    7437             :     sal_Int32 pMappedFontObjects[nMaxGlyphs];
    7438           0 :     std::vector<sal_Ucs> aUnicodes;
    7439           0 :     aUnicodes.reserve( nMaxGlyphs );
    7440             :     sal_Int32 pUnicodesPerGlyph[nMaxGlyphs];
    7441             :     int pCharPosAry[nMaxGlyphs];
    7442             :     DeviceCoordinate nAdvanceWidths[nMaxGlyphs];
    7443           0 :     const PhysicalFontFace* pFallbackFonts[nMaxGlyphs] = { NULL };
    7444           0 :     bool bVertical = m_aCurrentPDFState.m_aFont.IsVertical();
    7445             :     int nGlyphs;
    7446           0 :     int nIndex = 0;
    7447           0 :     int nMinCharPos = 0, nMaxCharPos = rText.getLength()-1;
    7448           0 :     double fXScale = 1.0;
    7449           0 :     double fSkew = 0.0;
    7450           0 :     sal_Int32 nPixelFontHeight = m_pReferenceDevice->mpFontEntry->maFontSelData.mnHeight;
    7451           0 :     TextAlign eAlign = m_aCurrentPDFState.m_aFont.GetAlign();
    7452             : 
    7453             :     // transform font height back to current units
    7454             :     // note: the layout calculates in outdevs device pixel !!
    7455           0 :     sal_Int32 nFontHeight = m_pReferenceDevice->ImplDevicePixelToLogicHeight( nPixelFontHeight );
    7456           0 :     if( m_aCurrentPDFState.m_aFont.GetWidth() )
    7457             :     {
    7458           0 :         Font aFont( m_aCurrentPDFState.m_aFont );
    7459           0 :         aFont.SetWidth( 0 );
    7460           0 :         FontMetric aMetric = m_pReferenceDevice->GetFontMetric( aFont );
    7461           0 :         if( aMetric.GetWidth() != m_aCurrentPDFState.m_aFont.GetWidth() )
    7462             :         {
    7463             :             fXScale =
    7464           0 :                 (double)m_aCurrentPDFState.m_aFont.GetWidth() /
    7465           0 :                 (double)aMetric.GetWidth();
    7466             :         }
    7467             :         // force state before GetFontMetric
    7468           0 :         m_pReferenceDevice->ImplNewFont();
    7469             :     }
    7470             : 
    7471             :     // perform artificial italics if necessary
    7472           0 :     if( ( m_aCurrentPDFState.m_aFont.GetItalic() == ITALIC_NORMAL ||
    7473           0 :           m_aCurrentPDFState.m_aFont.GetItalic() == ITALIC_OBLIQUE ) &&
    7474           0 :         !( m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetSlant() == ITALIC_NORMAL ||
    7475           0 :            m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetSlant() == ITALIC_OBLIQUE )
    7476             :         )
    7477             :     {
    7478           0 :         fSkew = M_PI/12.0;
    7479             :     }
    7480             : 
    7481             :     // if the mapmode is distorted we need to adjust for that also
    7482           0 :     if( m_aCurrentPDFState.m_aMapMode.GetScaleX() != m_aCurrentPDFState.m_aMapMode.GetScaleY() )
    7483             :     {
    7484           0 :         fXScale *= double(m_aCurrentPDFState.m_aMapMode.GetScaleX()) / double(m_aCurrentPDFState.m_aMapMode.GetScaleY());
    7485             :     }
    7486             : 
    7487           0 :     int nAngle = m_aCurrentPDFState.m_aFont.GetOrientation();
    7488             :     // normalize angles
    7489           0 :     while( nAngle < 0 )
    7490           0 :         nAngle += 3600;
    7491           0 :     nAngle = nAngle % 3600;
    7492           0 :     double fAngle = (double)nAngle * M_PI / 1800.0;
    7493             : 
    7494           0 :     Matrix3 aRotScale;
    7495           0 :     aRotScale.scale( fXScale, 1.0 );
    7496           0 :     if( fAngle != 0.0 )
    7497           0 :         aRotScale.rotate( -fAngle );
    7498             : 
    7499           0 :     bool bPop = false;
    7500           0 :     bool bABold = false;
    7501             :     // artificial bold necessary ?
    7502           0 :     if( m_pReferenceDevice->mpFontEntry->maFontSelData.mpFontData->GetWeight() <= WEIGHT_MEDIUM &&
    7503           0 :         m_pReferenceDevice->mpFontEntry->maFontSelData.GetWeight() > WEIGHT_MEDIUM )
    7504             :     {
    7505           0 :         if( ! bPop )
    7506           0 :             aLine.append( "q " );
    7507           0 :         bPop = true;
    7508           0 :         bABold = true;
    7509             :     }
    7510             :     // setup text colors (if necessary)
    7511           0 :     Color aStrokeColor( COL_TRANSPARENT );
    7512           0 :     Color aNonStrokeColor( COL_TRANSPARENT );
    7513             : 
    7514           0 :     if( m_aCurrentPDFState.m_aFont.IsOutline() )
    7515             :     {
    7516           0 :         aStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7517           0 :         aNonStrokeColor = Color( COL_WHITE );
    7518             :     }
    7519             :     else
    7520           0 :         aNonStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7521           0 :     if( bABold )
    7522           0 :         aStrokeColor = m_aCurrentPDFState.m_aFont.GetColor();
    7523             : 
    7524           0 :     if( aStrokeColor != Color( COL_TRANSPARENT ) && aStrokeColor != m_aCurrentPDFState.m_aLineColor )
    7525             :     {
    7526           0 :         if( ! bPop )
    7527           0 :             aLine.append( "q " );
    7528           0 :         bPop = true;
    7529           0 :         appendStrokingColor( aStrokeColor, aLine );
    7530           0 :         aLine.append( "\n" );
    7531             :     }
    7532           0 :     if( aNonStrokeColor != Color( COL_TRANSPARENT ) && aNonStrokeColor != m_aCurrentPDFState.m_aFillColor )
    7533             :     {
    7534           0 :         if( ! bPop )
    7535           0 :             aLine.append( "q " );
    7536           0 :         bPop = true;
    7537           0 :         appendNonStrokingColor( aNonStrokeColor, aLine );
    7538           0 :         aLine.append( "\n" );
    7539             :     }
    7540             : 
    7541             :     // begin text object
    7542           0 :     aLine.append( "BT\n" );
    7543             :     // outline attribute ?
    7544           0 :     if( m_aCurrentPDFState.m_aFont.IsOutline() || bABold )
    7545             :     {
    7546             :         // set correct text mode, set stroke width
    7547           0 :         aLine.append( "2 Tr " ); // fill, then stroke
    7548             : 
    7549           0 :         if( m_aCurrentPDFState.m_aFont.IsOutline() )
    7550             :         {
    7551             :             // unclear what to do in case of outline and artificial bold
    7552             :             // for the time being outline wins
    7553           0 :             aLine.append( "0.25 w \n" );
    7554             :         }
    7555             :         else
    7556             :         {
    7557           0 :             double fW = (double)m_aCurrentPDFState.m_aFont.GetHeight() / 30.0;
    7558           0 :             m_aPages.back().appendMappedLength( fW, aLine );
    7559           0 :             aLine.append ( " w\n" );
    7560             :         }
    7561             :     }
    7562             : 
    7563           0 :     FontMetric aRefDevFontMetric = m_pReferenceDevice->GetFontMetric();
    7564             : 
    7565             :     // collect the glyphs into a single array
    7566           0 :     const int nTmpMaxGlyphs = rLayout.GetOrientation() ? 1 : nMaxGlyphs; // #i97991# temporary workaround for #i87686#
    7567           0 :     std::vector< PDFGlyph > aGlyphs;
    7568           0 :     aGlyphs.reserve( nTmpMaxGlyphs );
    7569             :     // first get all the glyphs and register them; coordinates still in Pixel
    7570           0 :     Point aGNGlyphPos;
    7571           0 :     while( (nGlyphs = rLayout.GetNextGlyphs( nTmpMaxGlyphs, pGlyphs, aGNGlyphPos, nIndex, nAdvanceWidths, pCharPosAry, pFallbackFonts )) != 0 )
    7572             :     {
    7573           0 :         aUnicodes.clear();
    7574           0 :         for( int i = 0; i < nGlyphs; i++ )
    7575             :         {
    7576             :             // default case: 1 glyph is one unicode
    7577           0 :             pUnicodesPerGlyph[i] = 1;
    7578           0 :             if( (pGlyphs[i] & GF_ISCHAR) )
    7579             :             {
    7580           0 :                 aUnicodes.push_back( static_cast<sal_Ucs>(pGlyphs[i] & GF_IDXMASK) );
    7581             :             }
    7582           0 :             else if( pCharPosAry[i] >= nMinCharPos && pCharPosAry[i] <= nMaxCharPos )
    7583             :             {
    7584           0 :                 int nChars = 1;
    7585           0 :                 aUnicodes.push_back( rText[ pCharPosAry[i] ] );
    7586           0 :                 pUnicodesPerGlyph[i] = 1;
    7587             :                 // try to handle ligatures and such
    7588           0 :                 if( i < nGlyphs-1 )
    7589             :                 {
    7590           0 :                     nChars = pCharPosAry[i+1] - pCharPosAry[i];
    7591             :                     // #i115618# fix for simple RTL+CTL cases
    7592             :                     // TODO: sanitize for RTL ligatures, more complex CTL, etc.
    7593           0 :                     if( nChars < 0 )
    7594           0 :                         nChars = -nChars;
    7595           0 :                     else if( nChars == 0 )
    7596           0 :                         nChars = 1;
    7597           0 :                     pUnicodesPerGlyph[i] = nChars;
    7598           0 :                     for( int n = 1; n < nChars; n++ )
    7599           0 :                         aUnicodes.push_back( rText[ pCharPosAry[i] + n ] );
    7600             :                 }
    7601             :                 // #i36691# hack that is needed because currently the pGlyphs[]
    7602             :                 // argument is ignored for embeddable fonts and so the layout
    7603             :                 // engine's glyph work is ignored (i.e. char mirroring)
    7604             :                 // TODO: a real solution would be to map the layout engine's
    7605             :                 // glyphid (i.e. FreeType's synthetic glyphid for a Type1 font)
    7606             :                 // back to unicode and then to embeddable font's encoding
    7607           0 :                 if( (getReferenceDevice()->GetLayoutMode() & TEXT_LAYOUT_BIDI_RTL) != TEXT_LAYOUT_DEFAULT )
    7608             :                 {
    7609           0 :                     size_t nI = aUnicodes.size()-1;
    7610           0 :                     for( int n = 0; n < nChars; n++, nI-- )
    7611           0 :                         aUnicodes[nI] = static_cast<sal_Ucs>(GetMirroredChar(aUnicodes[nI]));
    7612           0 :                 }
    7613             :             }
    7614             :             else
    7615           0 :                 aUnicodes.push_back( 0 );
    7616             :             // note: in case of ctl one character may result
    7617             :             // in multiple glyphs. The current SalLayout
    7618             :             // implementations set -1 then to indicate that no direct
    7619             :             // mapping is possible
    7620             :         }
    7621             : 
    7622           0 :         registerGlyphs( nGlyphs, pGlyphs, pGlyphWidths, &aUnicodes[0], pUnicodesPerGlyph, pMappedGlyphs, pMappedFontObjects, pFallbackFonts );
    7623             : 
    7624           0 :         for( int i = 0; i < nGlyphs; i++ )
    7625             :         {
    7626             :             aGlyphs.push_back( PDFGlyph( aGNGlyphPos,
    7627             :                                          pGlyphWidths[i],
    7628           0 :                                          pGlyphs[i],
    7629             :                                          pMappedFontObjects[i],
    7630           0 :                                          pMappedGlyphs[i] ) );
    7631           0 :             if( bVertical )
    7632           0 :                 aGNGlyphPos.Y() += nAdvanceWidths[i]/rLayout.GetUnitsPerPixel();
    7633             :             else
    7634           0 :                 aGNGlyphPos.X() += nAdvanceWidths[i]/rLayout.GetUnitsPerPixel();
    7635             :         }
    7636             :     }
    7637             : 
    7638           0 :     Point aAlignOffset;
    7639           0 :     if ( eAlign == ALIGN_BOTTOM )
    7640           0 :         aAlignOffset.Y() -= aRefDevFontMetric.GetDescent();
    7641           0 :     else if ( eAlign == ALIGN_TOP )
    7642           0 :         aAlignOffset.Y() += aRefDevFontMetric.GetAscent();
    7643           0 :     if( aAlignOffset.X() || aAlignOffset.Y() )
    7644           0 :         aAlignOffset = aRotScale.transform( aAlignOffset );
    7645             : 
    7646             :     /* #159153# do not emit an empty glyph vector; this can happen if e.g. the original
    7647             :        string contained only on of the UTF16 BOMs
    7648             :     */
    7649           0 :     if( ! aGlyphs.empty() )
    7650             :     {
    7651           0 :         if( bVertical )
    7652           0 :             drawVerticalGlyphs( aGlyphs, aLine, aAlignOffset, aRotScale, fAngle, fXScale, fSkew, nFontHeight );
    7653             :         else
    7654           0 :             drawHorizontalGlyphs( aGlyphs, aLine, aAlignOffset, fAngle, fXScale, fSkew, nFontHeight, nPixelFontHeight );
    7655             :     }
    7656             : 
    7657             :     // end textobject
    7658           0 :     aLine.append( "ET\n" );
    7659           0 :     if( bPop )
    7660           0 :         aLine.append( "Q\n" );
    7661             : 
    7662           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    7663             : 
    7664             :     // draw eventual textlines
    7665           0 :     FontStrikeout eStrikeout = m_aCurrentPDFState.m_aFont.GetStrikeout();
    7666           0 :     FontUnderline eUnderline = m_aCurrentPDFState.m_aFont.GetUnderline();
    7667           0 :     FontUnderline eOverline  = m_aCurrentPDFState.m_aFont.GetOverline();
    7668           0 :     if( bTextLines &&
    7669             :         (
    7670           0 :          ( eUnderline != UNDERLINE_NONE && eUnderline != UNDERLINE_DONTKNOW ) ||
    7671           0 :          ( eOverline  != UNDERLINE_NONE && eOverline  != UNDERLINE_DONTKNOW ) ||
    7672           0 :          ( eStrikeout != STRIKEOUT_NONE && eStrikeout != STRIKEOUT_DONTKNOW )
    7673             :          )
    7674             :         )
    7675             :     {
    7676           0 :         bool bUnderlineAbove = OutputDevice::ImplIsUnderlineAbove( m_aCurrentPDFState.m_aFont );
    7677           0 :         if( m_aCurrentPDFState.m_aFont.IsWordLineMode() )
    7678             :         {
    7679           0 :             Point aPos, aStartPt;
    7680           0 :             sal_Int32 nWidth = 0;
    7681           0 :             DeviceCoordinate nAdvance = 0;
    7682           0 :             for( int nStart = 0;;)
    7683             :             {
    7684             :                 sal_GlyphId aGlyphId;
    7685           0 :                 if( !rLayout.GetNextGlyphs( 1, &aGlyphId, aPos, nStart, &nAdvance ) )
    7686           0 :                     break;
    7687             : 
    7688           0 :                 if( !rLayout.IsSpacingGlyph( aGlyphId ) )
    7689             :                 {
    7690           0 :                     if( !nWidth )
    7691           0 :                         aStartPt = aPos;
    7692             : 
    7693           0 :                     nWidth += nAdvance;
    7694             :                 }
    7695           0 :                 else if( nWidth > 0 )
    7696             :                 {
    7697           0 :                     drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7698             :                                   m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7699           0 :                                   eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7700           0 :                     nWidth = 0;
    7701             :                 }
    7702           0 :             }
    7703             : 
    7704           0 :             if( nWidth > 0 )
    7705             :             {
    7706           0 :                 drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7707             :                               m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7708           0 :                               eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7709             :             }
    7710             :         }
    7711             :         else
    7712             :         {
    7713           0 :             Point aStartPt = rLayout.GetDrawPosition();
    7714           0 :             int nWidth = rLayout.GetTextWidth() / rLayout.GetUnitsPerPixel();
    7715           0 :             drawTextLine( m_pReferenceDevice->PixelToLogic( aStartPt ),
    7716             :                           m_pReferenceDevice->ImplDevicePixelToLogicWidth( nWidth ),
    7717           0 :                           eStrikeout, eUnderline, eOverline, bUnderlineAbove );
    7718             :         }
    7719             :     }
    7720             : 
    7721             :     // write eventual emphasis marks
    7722           0 :     if( m_aCurrentPDFState.m_aFont.GetEmphasisMark() & EMPHASISMARK_STYLE )
    7723             :     {
    7724           0 :         tools::PolyPolygon             aEmphPoly;
    7725           0 :         Rectangle               aEmphRect1;
    7726           0 :         Rectangle               aEmphRect2;
    7727             :         long                    nEmphYOff;
    7728             :         long                    nEmphWidth;
    7729             :         long                    nEmphHeight;
    7730             :         bool                    bEmphPolyLine;
    7731             :         FontEmphasisMark        nEmphMark;
    7732             : 
    7733           0 :         push( PushFlags::ALL );
    7734             : 
    7735           0 :         aLine.setLength( 0 );
    7736           0 :         aLine.append( "q\n" );
    7737             : 
    7738           0 :         nEmphMark = OutputDevice::ImplGetEmphasisMarkStyle( m_aCurrentPDFState.m_aFont );
    7739           0 :         if ( nEmphMark & EMPHASISMARK_POS_BELOW )
    7740           0 :             nEmphHeight = m_pReferenceDevice->mnEmphasisDescent;
    7741             :         else
    7742           0 :             nEmphHeight = m_pReferenceDevice->mnEmphasisAscent;
    7743             :         m_pReferenceDevice->ImplGetEmphasisMark( aEmphPoly,
    7744             :                                                  bEmphPolyLine,
    7745             :                                                  aEmphRect1,
    7746             :                                                  aEmphRect2,
    7747             :                                                  nEmphYOff,
    7748             :                                                  nEmphWidth,
    7749             :                                                  nEmphMark,
    7750             :                                                  m_pReferenceDevice->ImplDevicePixelToLogicWidth(nEmphHeight),
    7751           0 :                                                  m_pReferenceDevice->mpFontEntry->mnOrientation );
    7752           0 :         if ( bEmphPolyLine )
    7753             :         {
    7754           0 :             setLineColor( m_aCurrentPDFState.m_aFont.GetColor() );
    7755           0 :             setFillColor( Color( COL_TRANSPARENT ) );
    7756             :         }
    7757             :         else
    7758             :         {
    7759           0 :             setFillColor( m_aCurrentPDFState.m_aFont.GetColor() );
    7760           0 :             setLineColor( Color( COL_TRANSPARENT ) );
    7761             :         }
    7762           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    7763             : 
    7764           0 :         Point aOffset = Point(0,0);
    7765             : 
    7766           0 :         if ( nEmphMark & EMPHASISMARK_POS_BELOW )
    7767           0 :             aOffset.Y() += m_pReferenceDevice->mpFontEntry->maMetric.mnDescent + nEmphYOff;
    7768             :         else
    7769           0 :             aOffset.Y() -= m_pReferenceDevice->mpFontEntry->maMetric.mnAscent + nEmphYOff;
    7770             : 
    7771           0 :         long nEmphWidth2     = nEmphWidth / 2;
    7772           0 :         long nEmphHeight2    = nEmphHeight / 2;
    7773           0 :         aOffset += Point( nEmphWidth2, nEmphHeight2 );
    7774             : 
    7775           0 :         if ( eAlign == ALIGN_BOTTOM )
    7776           0 :             aOffset.Y() -= m_pReferenceDevice->mpFontEntry->maMetric.mnDescent;
    7777           0 :         else if ( eAlign == ALIGN_TOP )
    7778           0 :             aOffset.Y() += m_pReferenceDevice->mpFontEntry->maMetric.mnAscent;
    7779             : 
    7780           0 :         for( int nStart = 0;;)
    7781             :         {
    7782           0 :             Point aPos;
    7783             :             sal_GlyphId aGlyphId;
    7784             :             DeviceCoordinate nAdvance;
    7785           0 :             if( !rLayout.GetNextGlyphs( 1, &aGlyphId, aPos, nStart, &nAdvance ) )
    7786           0 :                 break;
    7787             : 
    7788           0 :             if( !rLayout.IsSpacingGlyph( aGlyphId ) )
    7789             :             {
    7790           0 :                 Point aAdjOffset = aOffset;
    7791           0 :                 aAdjOffset.X() += (nAdvance - nEmphWidth) / 2;
    7792           0 :                 aAdjOffset = aRotScale.transform( aAdjOffset );
    7793             : 
    7794           0 :                 aAdjOffset -= Point( nEmphWidth2, nEmphHeight2 );
    7795             : 
    7796           0 :                 aPos += aAdjOffset;
    7797           0 :                 aPos = m_pReferenceDevice->PixelToLogic( aPos );
    7798           0 :                 drawEmphasisMark( aPos.X(), aPos.Y(),
    7799             :                                   aEmphPoly, bEmphPolyLine,
    7800           0 :                                   aEmphRect1, aEmphRect2 );
    7801             :             }
    7802           0 :         }
    7803             : 
    7804           0 :         writeBuffer( "Q\n", 2 );
    7805           0 :         pop();
    7806           0 :     }
    7807             : 
    7808             : }
    7809             : 
    7810           0 : void PDFWriterImpl::drawEmphasisMark( long nX, long nY,
    7811             :                                       const tools::PolyPolygon& rPolyPoly, bool bPolyLine,
    7812             :                                       const Rectangle& rRect1, const Rectangle& rRect2 )
    7813             : {
    7814             :     // TODO: pass nWidth as width of this mark
    7815             :     // long nWidth = 0;
    7816             : 
    7817           0 :     if ( rPolyPoly.Count() )
    7818             :     {
    7819           0 :         if ( bPolyLine )
    7820             :         {
    7821           0 :             Polygon aPoly = rPolyPoly.GetObject( 0 );
    7822           0 :             aPoly.Move( nX, nY );
    7823           0 :             drawPolyLine( aPoly );
    7824             :         }
    7825             :         else
    7826             :         {
    7827           0 :             tools::PolyPolygon aPolyPoly = rPolyPoly;
    7828           0 :             aPolyPoly.Move( nX, nY );
    7829           0 :             drawPolyPolygon( aPolyPoly );
    7830             :         }
    7831             :     }
    7832             : 
    7833           0 :     if ( !rRect1.IsEmpty() )
    7834             :     {
    7835           0 :         Rectangle aRect( Point( nX+rRect1.Left(),
    7836           0 :                                 nY+rRect1.Top() ), rRect1.GetSize() );
    7837           0 :         drawRectangle( aRect );
    7838             :     }
    7839             : 
    7840           0 :     if ( !rRect2.IsEmpty() )
    7841             :     {
    7842           0 :         Rectangle aRect( Point( nX+rRect2.Left(),
    7843           0 :                                 nY+rRect2.Top() ), rRect2.GetSize() );
    7844             : 
    7845           0 :         drawRectangle( aRect );
    7846             :     }
    7847           0 : }
    7848             : 
    7849           0 : void PDFWriterImpl::drawText( const Point& rPos, const OUString& rText, sal_Int32 nIndex, sal_Int32 nLen, bool bTextLines )
    7850             : {
    7851           0 :     MARK( "drawText" );
    7852             : 
    7853           0 :     updateGraphicsState();
    7854             : 
    7855             :     // get a layout from the OuputDevice's SalGraphics
    7856             :     // this also enforces font substitution and sets the font on SalGraphics
    7857           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos );
    7858           0 :     if( pLayout )
    7859             :     {
    7860           0 :         drawLayout( *pLayout, rText, bTextLines );
    7861           0 :         pLayout->Release();
    7862             :     }
    7863           0 : }
    7864             : 
    7865           0 : void PDFWriterImpl::drawTextArray( const Point& rPos, const OUString& rText, const long* pDXArray, sal_Int32 nIndex, sal_Int32 nLen, bool bTextLines )
    7866             : {
    7867           0 :     MARK( "drawText with array" );
    7868             : 
    7869           0 :     updateGraphicsState();
    7870             : 
    7871             :     // get a layout from the OuputDevice's SalGraphics
    7872             :     // this also enforces font substitution and sets the font on SalGraphics
    7873           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos, 0, pDXArray );
    7874           0 :     if( pLayout )
    7875             :     {
    7876           0 :         drawLayout( *pLayout, rText, bTextLines );
    7877           0 :         pLayout->Release();
    7878             :     }
    7879           0 : }
    7880             : 
    7881           0 : void PDFWriterImpl::drawStretchText( const Point& rPos, sal_uLong nWidth, const OUString& rText, sal_Int32 nIndex, sal_Int32 nLen, bool bTextLines )
    7882             : {
    7883           0 :     MARK( "drawStretchText" );
    7884             : 
    7885           0 :     updateGraphicsState();
    7886             : 
    7887             :     // get a layout from the OuputDevice's SalGraphics
    7888             :     // this also enforces font substitution and sets the font on SalGraphics
    7889           0 :     SalLayout* pLayout = m_pReferenceDevice->ImplLayout( rText, nIndex, nLen, rPos, nWidth );
    7890           0 :     if( pLayout )
    7891             :     {
    7892           0 :         drawLayout( *pLayout, rText, bTextLines );
    7893           0 :         pLayout->Release();
    7894             :     }
    7895           0 : }
    7896             : 
    7897           0 : void PDFWriterImpl::drawText( const Rectangle& rRect, const OUString& rOrigStr, sal_uInt16 nStyle, bool bTextLines )
    7898             : {
    7899           0 :     long        nWidth          = rRect.GetWidth();
    7900           0 :     long        nHeight         = rRect.GetHeight();
    7901             : 
    7902           0 :     if ( nWidth <= 0 || nHeight <= 0 )
    7903           0 :         return;
    7904             : 
    7905           0 :     MARK( "drawText with rectangle" );
    7906             : 
    7907           0 :     updateGraphicsState();
    7908             : 
    7909             :     // clip with rectangle
    7910           0 :     OStringBuffer aLine;
    7911           0 :     aLine.append( "q " );
    7912           0 :     m_aPages.back().appendRect( rRect, aLine );
    7913           0 :     aLine.append( " W* n\n" );
    7914           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    7915             : 
    7916             :     // if disabled text is needed, put in here
    7917             : 
    7918           0 :     Point       aPos            = rRect.TopLeft();
    7919             : 
    7920           0 :     long        nTextHeight     = m_pReferenceDevice->GetTextHeight();
    7921           0 :     sal_Int32   nMnemonicPos    = -1;
    7922             : 
    7923           0 :     OUString aStr = rOrigStr;
    7924           0 :     if ( nStyle & TEXT_DRAW_MNEMONIC )
    7925           0 :         aStr = OutputDevice::GetNonMnemonicString( aStr, nMnemonicPos );
    7926             : 
    7927             :     // multiline text
    7928           0 :     if ( nStyle & TEXT_DRAW_MULTILINE )
    7929             :     {
    7930           0 :         OUString           aLastLine;
    7931           0 :         ImplMultiTextLineInfo   aMultiLineInfo;
    7932             :         ImplTextLineInfo*       pLineInfo;
    7933             :         sal_Int32               i;
    7934             :         sal_Int32               nLines;
    7935             :         sal_Int32               nFormatLines;
    7936             : 
    7937           0 :         if ( nTextHeight )
    7938             :         {
    7939           0 :             ::vcl::DefaultTextLayout aLayout( *m_pReferenceDevice );
    7940           0 :             OutputDevice::ImplGetTextLines( aMultiLineInfo, nWidth, aStr, nStyle, aLayout );
    7941           0 :             nLines = nHeight/nTextHeight;
    7942           0 :             nFormatLines = aMultiLineInfo.Count();
    7943           0 :             if ( !nLines )
    7944           0 :                 nLines = 1;
    7945           0 :             if ( nFormatLines > nLines )
    7946             :             {
    7947           0 :                 if ( nStyle & TEXT_DRAW_ENDELLIPSIS )
    7948             :                 {
    7949             :                     // handle last line
    7950           0 :                     nFormatLines = nLines-1;
    7951             : 
    7952           0 :                     pLineInfo = aMultiLineInfo.GetLine( nFormatLines );
    7953           0 :                     aLastLine = convertLineEnd(aStr.copy(pLineInfo->GetIndex()), LINEEND_LF);
    7954             :                     // replace line feed by space
    7955           0 :                     aLastLine = aLastLine.replace('\n', ' ');
    7956           0 :                     aLastLine = m_pReferenceDevice->GetEllipsisString( aLastLine, nWidth, nStyle );
    7957           0 :                     nStyle &= ~(TEXT_DRAW_VCENTER | TEXT_DRAW_BOTTOM);
    7958           0 :                     nStyle |= TEXT_DRAW_TOP;
    7959             :                 }
    7960             :             }
    7961             : 
    7962             :             // vertical alignment
    7963           0 :             if ( nStyle & TEXT_DRAW_BOTTOM )
    7964           0 :                 aPos.Y() += nHeight-(nFormatLines*nTextHeight);
    7965           0 :             else if ( nStyle & TEXT_DRAW_VCENTER )
    7966           0 :                 aPos.Y() += (nHeight-(nFormatLines*nTextHeight))/2;
    7967             : 
    7968             :             // draw all lines excluding the last
    7969           0 :             for ( i = 0; i < nFormatLines; i++ )
    7970             :             {
    7971           0 :                 pLineInfo = aMultiLineInfo.GetLine( i );
    7972           0 :                 if ( nStyle & TEXT_DRAW_RIGHT )
    7973           0 :                     aPos.X() += nWidth-pLineInfo->GetWidth();
    7974           0 :                 else if ( nStyle & TEXT_DRAW_CENTER )
    7975           0 :                     aPos.X() += (nWidth-pLineInfo->GetWidth())/2;
    7976           0 :                 sal_Int32 nIndex = pLineInfo->GetIndex();
    7977           0 :                 sal_Int32 nLineLen = pLineInfo->GetLen();
    7978           0 :                 drawText( aPos, aStr, nIndex, nLineLen, bTextLines );
    7979             :                 // mnemonics should not appear in documents,
    7980             :                 // if the need arises, put them in here
    7981           0 :                 aPos.Y() += nTextHeight;
    7982           0 :                 aPos.X() = rRect.Left();
    7983             :             }
    7984             : 
    7985             :             // output last line left adjusted since it was shortened
    7986           0 :             if (!aLastLine.isEmpty())
    7987           0 :                 drawText( aPos, aLastLine, 0, aLastLine.getLength(), bTextLines );
    7988           0 :         }
    7989             :     }
    7990             :     else
    7991             :     {
    7992           0 :         long nTextWidth = m_pReferenceDevice->GetTextWidth( aStr );
    7993             : 
    7994             :         // Evt. Text kuerzen
    7995           0 :         if ( nTextWidth > nWidth )
    7996             :         {
    7997           0 :             if ( nStyle & (TEXT_DRAW_ENDELLIPSIS | TEXT_DRAW_PATHELLIPSIS | TEXT_DRAW_NEWSELLIPSIS) )
    7998             :             {
    7999           0 :                 aStr = m_pReferenceDevice->GetEllipsisString( aStr, nWidth, nStyle );
    8000           0 :                 nStyle &= ~(TEXT_DRAW_CENTER | TEXT_DRAW_RIGHT);
    8001           0 :                 nStyle |= TEXT_DRAW_LEFT;
    8002           0 :                 nTextWidth = m_pReferenceDevice->GetTextWidth( aStr );
    8003             :             }
    8004             :         }
    8005             : 
    8006             :         // vertical alignment
    8007           0 :         if ( nStyle & TEXT_DRAW_RIGHT )
    8008           0 :             aPos.X() += nWidth-nTextWidth;
    8009           0 :         else if ( nStyle & TEXT_DRAW_CENTER )
    8010           0 :             aPos.X() += (nWidth-nTextWidth)/2;
    8011             : 
    8012           0 :         if ( nStyle & TEXT_DRAW_BOTTOM )
    8013           0 :             aPos.Y() += nHeight-nTextHeight;
    8014           0 :         else if ( nStyle & TEXT_DRAW_VCENTER )
    8015           0 :             aPos.Y() += (nHeight-nTextHeight)/2;
    8016             : 
    8017             :         // mnemonics should be inserted here if the need arises
    8018             : 
    8019             :         // draw the actual text
    8020           0 :         drawText( aPos, aStr, 0, aStr.getLength(), bTextLines );
    8021             :     }
    8022             : 
    8023             :     // reset clip region to original value
    8024           0 :     aLine.setLength( 0 );
    8025           0 :     aLine.append( "Q\n" );
    8026           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8027             : }
    8028             : 
    8029           0 : void PDFWriterImpl::drawLine( const Point& rStart, const Point& rStop )
    8030             : {
    8031           0 :     MARK( "drawLine" );
    8032             : 
    8033           0 :     updateGraphicsState();
    8034             : 
    8035           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    8036           0 :         return;
    8037             : 
    8038           0 :     OStringBuffer aLine;
    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\n" );
    8043             : 
    8044           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8045             : }
    8046             : 
    8047           0 : void PDFWriterImpl::drawLine( const Point& rStart, const Point& rStop, const LineInfo& rInfo )
    8048             : {
    8049           0 :     MARK( "drawLine with LineInfo" );
    8050           0 :     updateGraphicsState();
    8051             : 
    8052           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    8053           0 :         return;
    8054             : 
    8055           0 :     if( rInfo.GetStyle() == LINE_SOLID && rInfo.GetWidth() < 2 )
    8056             :     {
    8057           0 :         drawLine( rStart, rStop );
    8058           0 :         return;
    8059             :     }
    8060             : 
    8061           0 :     OStringBuffer aLine;
    8062             : 
    8063           0 :     aLine.append( "q " );
    8064           0 :     if( m_aPages.back().appendLineInfo( rInfo, aLine ) )
    8065             :     {
    8066           0 :         m_aPages.back().appendPoint( rStart, aLine );
    8067           0 :         aLine.append( " m " );
    8068           0 :         m_aPages.back().appendPoint( rStop, aLine );
    8069           0 :         aLine.append( " l S Q\n" );
    8070             : 
    8071           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    8072             :     }
    8073             :     else
    8074             :     {
    8075           0 :         PDFWriter::ExtLineInfo aInfo;
    8076           0 :         convertLineInfoToExtLineInfo( rInfo, aInfo );
    8077           0 :         Point aPolyPoints[2] = { rStart, rStop };
    8078           0 :         Polygon aPoly( 2, aPolyPoints );
    8079           0 :         drawPolyLine( aPoly, aInfo );
    8080           0 :     }
    8081             : }
    8082             : 
    8083             : #define HCONV( x ) m_pReferenceDevice->ImplDevicePixelToLogicHeight( x )
    8084             : 
    8085           0 : void PDFWriterImpl::drawWaveTextLine( OStringBuffer& aLine, long nWidth, FontUnderline eTextLine, Color aColor, bool bIsAbove )
    8086             : {
    8087             :     // note: units in pFontEntry are ref device pixel
    8088           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8089           0 :     long            nLineHeight = 0;
    8090           0 :     long            nLinePos = 0;
    8091             : 
    8092           0 :     appendStrokingColor( aColor, aLine );
    8093           0 :     aLine.append( "\n" );
    8094             : 
    8095           0 :     if ( bIsAbove )
    8096             :     {
    8097           0 :         if ( !pFontEntry->maMetric.mnAboveWUnderlineSize )
    8098           0 :             m_pReferenceDevice->ImplInitAboveTextLineSize();
    8099           0 :         nLineHeight = HCONV( pFontEntry->maMetric.mnAboveWUnderlineSize );
    8100           0 :         nLinePos = HCONV( pFontEntry->maMetric.mnAboveWUnderlineOffset );
    8101             :     }
    8102             :     else
    8103             :     {
    8104           0 :         if ( !pFontEntry->maMetric.mnWUnderlineSize )
    8105           0 :             m_pReferenceDevice->ImplInitTextLineSize();
    8106           0 :         nLineHeight = HCONV( pFontEntry->maMetric.mnWUnderlineSize );
    8107           0 :         nLinePos = HCONV( pFontEntry->maMetric.mnWUnderlineOffset );
    8108             :     }
    8109           0 :     if ( (eTextLine == UNDERLINE_SMALLWAVE) && (nLineHeight > 3) )
    8110           0 :         nLineHeight = 3;
    8111             : 
    8112           0 :     long nLineWidth = getReferenceDevice()->mnDPIX/450;
    8113           0 :     if ( ! nLineWidth )
    8114           0 :         nLineWidth = 1;
    8115             : 
    8116           0 :     if ( eTextLine == UNDERLINE_BOLDWAVE )
    8117           0 :         nLineWidth = 3*nLineWidth;
    8118             : 
    8119           0 :     m_aPages.back().appendMappedLength( (sal_Int32)nLineWidth, aLine );
    8120           0 :     aLine.append( " w " );
    8121             : 
    8122           0 :     if ( eTextLine == UNDERLINE_DOUBLEWAVE )
    8123             :     {
    8124           0 :         long nOrgLineHeight = nLineHeight;
    8125           0 :         nLineHeight /= 3;
    8126           0 :         if ( nLineHeight < 2 )
    8127             :         {
    8128           0 :             if ( nOrgLineHeight > 1 )
    8129           0 :                 nLineHeight = 2;
    8130             :             else
    8131           0 :                 nLineHeight = 1;
    8132             :         }
    8133           0 :         long nLineDY = nOrgLineHeight-(nLineHeight*2);
    8134           0 :         if ( nLineDY < nLineWidth )
    8135           0 :             nLineDY = nLineWidth;
    8136           0 :         long nLineDY2 = nLineDY/2;
    8137           0 :         if ( !nLineDY2 )
    8138           0 :             nLineDY2 = 1;
    8139             : 
    8140           0 :         nLinePos -= nLineWidth-nLineDY2;
    8141             : 
    8142           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, 2*nLineHeight, aLine );
    8143             : 
    8144           0 :         nLinePos += nLineWidth+nLineDY;
    8145           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, 2*nLineHeight, aLine );
    8146             :     }
    8147             :     else
    8148             :     {
    8149           0 :         if ( eTextLine != UNDERLINE_BOLDWAVE )
    8150           0 :             nLinePos -= nLineWidth/2;
    8151           0 :         m_aPages.back().appendWaveLine( nWidth, -nLinePos, nLineHeight, aLine );
    8152             :     }
    8153           0 : }
    8154             : 
    8155           0 : void PDFWriterImpl::drawStraightTextLine( OStringBuffer& aLine, long nWidth, FontUnderline eTextLine, Color aColor, bool bIsAbove )
    8156             : {
    8157             :     // note: units in pFontEntry are ref device pixel
    8158           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8159           0 :     long            nLineHeight = 0;
    8160           0 :     long            nLinePos  = 0;
    8161           0 :     long            nLinePos2 = 0;
    8162             : 
    8163           0 :     if ( eTextLine > UNDERLINE_BOLDWAVE )
    8164           0 :         eTextLine = UNDERLINE_SINGLE;
    8165             : 
    8166           0 :     switch ( eTextLine )
    8167             :     {
    8168             :         case UNDERLINE_SINGLE:
    8169             :         case UNDERLINE_DOTTED:
    8170             :         case UNDERLINE_DASH:
    8171             :         case UNDERLINE_LONGDASH:
    8172             :         case UNDERLINE_DASHDOT:
    8173             :         case UNDERLINE_DASHDOTDOT:
    8174           0 :             if ( bIsAbove )
    8175             :             {
    8176           0 :                 if ( !pFontEntry->maMetric.mnAboveUnderlineSize )
    8177           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8178           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveUnderlineSize );
    8179           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveUnderlineOffset );
    8180             :             }
    8181             :             else
    8182             :             {
    8183           0 :                 if ( !pFontEntry->maMetric.mnUnderlineSize )
    8184           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8185           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnUnderlineSize );
    8186           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnUnderlineOffset );
    8187             :             }
    8188           0 :             break;
    8189             :         case UNDERLINE_BOLD:
    8190             :         case UNDERLINE_BOLDDOTTED:
    8191             :         case UNDERLINE_BOLDDASH:
    8192             :         case UNDERLINE_BOLDLONGDASH:
    8193             :         case UNDERLINE_BOLDDASHDOT:
    8194             :         case UNDERLINE_BOLDDASHDOTDOT:
    8195           0 :             if ( bIsAbove )
    8196             :             {
    8197           0 :                 if ( !pFontEntry->maMetric.mnAboveBUnderlineSize )
    8198           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8199           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveBUnderlineSize );
    8200           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveBUnderlineOffset );
    8201             :             }
    8202             :             else
    8203             :             {
    8204           0 :                 if ( !pFontEntry->maMetric.mnBUnderlineSize )
    8205           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8206           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnBUnderlineSize );
    8207           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnBUnderlineOffset );
    8208           0 :                 nLinePos += nLineHeight/2;
    8209             :             }
    8210           0 :             break;
    8211             :         case UNDERLINE_DOUBLE:
    8212           0 :             if ( bIsAbove )
    8213             :             {
    8214           0 :                 if ( !pFontEntry->maMetric.mnAboveDUnderlineSize )
    8215           0 :                     m_pReferenceDevice->ImplInitAboveTextLineSize();
    8216           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnAboveDUnderlineSize );
    8217           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnAboveDUnderlineOffset1 );
    8218           0 :                 nLinePos2   = HCONV( pFontEntry->maMetric.mnAboveDUnderlineOffset2 );
    8219             :             }
    8220             :             else
    8221             :             {
    8222           0 :                 if ( !pFontEntry->maMetric.mnDUnderlineSize )
    8223           0 :                     m_pReferenceDevice->ImplInitTextLineSize();
    8224           0 :                 nLineHeight = HCONV( pFontEntry->maMetric.mnDUnderlineSize );
    8225           0 :                 nLinePos    = HCONV( pFontEntry->maMetric.mnDUnderlineOffset1 );
    8226           0 :                 nLinePos2   = HCONV( pFontEntry->maMetric.mnDUnderlineOffset2 );
    8227             :             }
    8228             :         default:
    8229           0 :             break;
    8230             :     }
    8231             : 
    8232           0 :     if ( nLineHeight )
    8233             :     {
    8234           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, true );
    8235           0 :         aLine.append( " w " );
    8236           0 :         appendStrokingColor( aColor, aLine );
    8237           0 :         aLine.append( "\n" );
    8238             : 
    8239           0 :         switch ( eTextLine )
    8240             :         {
    8241             :             case UNDERLINE_DOTTED:
    8242             :             case UNDERLINE_BOLDDOTTED:
    8243           0 :                 aLine.append( "[ " );
    8244           0 :                 m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8245           0 :                 aLine.append( " ] 0 d\n" );
    8246           0 :                 break;
    8247             :             case UNDERLINE_DASH:
    8248             :             case UNDERLINE_LONGDASH:
    8249             :             case UNDERLINE_BOLDDASH:
    8250             :             case UNDERLINE_BOLDLONGDASH:
    8251             :                 {
    8252           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8253           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8254           0 :                     if ( ( eTextLine == UNDERLINE_LONGDASH ) || ( eTextLine == UNDERLINE_BOLDLONGDASH ) )
    8255           0 :                         nDashLength = 8*nLineHeight;
    8256             : 
    8257           0 :                     aLine.append( "[ " );
    8258           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8259           0 :                     aLine.append( ' ' );
    8260           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8261           0 :                     aLine.append( " ] 0 d\n" );
    8262             :                 }
    8263           0 :                 break;
    8264             :             case UNDERLINE_DASHDOT:
    8265             :             case UNDERLINE_BOLDDASHDOT:
    8266             :                 {
    8267           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8268           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8269           0 :                     aLine.append( "[ " );
    8270           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8271           0 :                     aLine.append( ' ' );
    8272           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8273           0 :                     aLine.append( ' ' );
    8274           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8275           0 :                     aLine.append( ' ' );
    8276           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8277           0 :                     aLine.append( " ] 0 d\n" );
    8278             :                 }
    8279           0 :                 break;
    8280             :             case UNDERLINE_DASHDOTDOT:
    8281             :             case UNDERLINE_BOLDDASHDOTDOT:
    8282             :                 {
    8283           0 :                     sal_Int32 nDashLength = 4*nLineHeight;
    8284           0 :                     sal_Int32 nVoidLength = 2*nLineHeight;
    8285           0 :                     aLine.append( "[ " );
    8286           0 :                     m_aPages.back().appendMappedLength( nDashLength, aLine, false );
    8287           0 :                     aLine.append( ' ' );
    8288           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8289           0 :                     aLine.append( ' ' );
    8290           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8291           0 :                     aLine.append( ' ' );
    8292           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8293           0 :                     aLine.append( ' ' );
    8294           0 :                     m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, false );
    8295           0 :                     aLine.append( ' ' );
    8296           0 :                     m_aPages.back().appendMappedLength( nVoidLength, aLine, false );
    8297           0 :                     aLine.append( " ] 0 d\n" );
    8298             :                 }
    8299           0 :                 break;
    8300             :             default:
    8301           0 :                 break;
    8302             :         }
    8303             : 
    8304           0 :         aLine.append( "0 " );
    8305           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8306           0 :         aLine.append( " m " );
    8307           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, false );
    8308           0 :         aLine.append( ' ' );
    8309           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8310           0 :         aLine.append( " l S\n" );
    8311           0 :         if ( eTextLine == UNDERLINE_DOUBLE )
    8312             :         {
    8313           0 :             aLine.append( "0 " );
    8314           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8315           0 :             aLine.append( " m " );
    8316           0 :             m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, false );
    8317           0 :             aLine.append( ' ' );
    8318           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8319           0 :             aLine.append( " l S\n" );
    8320             :         }
    8321             :     }
    8322           0 : }
    8323             : 
    8324           0 : void PDFWriterImpl::drawStrikeoutLine( OStringBuffer& aLine, long nWidth, FontStrikeout eStrikeout, Color aColor )
    8325             : {
    8326             :     // note: units in pFontEntry are ref device pixel
    8327           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8328           0 :     long            nLineHeight = 0;
    8329           0 :     long            nLinePos  = 0;
    8330           0 :     long            nLinePos2 = 0;
    8331             : 
    8332           0 :     if ( eStrikeout > STRIKEOUT_X )
    8333           0 :         eStrikeout = STRIKEOUT_SINGLE;
    8334             : 
    8335           0 :     switch ( eStrikeout )
    8336             :     {
    8337             :         case STRIKEOUT_SINGLE:
    8338           0 :             if ( !pFontEntry->maMetric.mnStrikeoutSize )
    8339           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8340           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnStrikeoutSize );
    8341           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnStrikeoutOffset );
    8342           0 :             break;
    8343             :         case STRIKEOUT_BOLD:
    8344           0 :             if ( !pFontEntry->maMetric.mnBStrikeoutSize )
    8345           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8346           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnBStrikeoutSize );
    8347           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnBStrikeoutOffset );
    8348           0 :             break;
    8349             :         case STRIKEOUT_DOUBLE:
    8350           0 :             if ( !pFontEntry->maMetric.mnDStrikeoutSize )
    8351           0 :                 m_pReferenceDevice->ImplInitTextLineSize();
    8352           0 :             nLineHeight = HCONV( pFontEntry->maMetric.mnDStrikeoutSize );
    8353           0 :             nLinePos    = HCONV( pFontEntry->maMetric.mnDStrikeoutOffset1 );
    8354           0 :             nLinePos2   = HCONV( pFontEntry->maMetric.mnDStrikeoutOffset2 );
    8355           0 :             break;
    8356             :         default:
    8357           0 :             break;
    8358             :     }
    8359             : 
    8360           0 :     if ( nLineHeight )
    8361             :     {
    8362           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nLineHeight, aLine, true );
    8363           0 :         aLine.append( " w " );
    8364           0 :         appendStrokingColor( aColor, aLine );
    8365           0 :         aLine.append( "\n" );
    8366             : 
    8367           0 :         aLine.append( "0 " );
    8368           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8369           0 :         aLine.append( " m " );
    8370           0 :         m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, true );
    8371           0 :         aLine.append( ' ' );
    8372           0 :         m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos), aLine, true );
    8373           0 :         aLine.append( " l S\n" );
    8374             : 
    8375           0 :         if ( eStrikeout == STRIKEOUT_DOUBLE )
    8376             :         {
    8377           0 :             aLine.append( "0 " );
    8378           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8379           0 :             aLine.append( " m " );
    8380           0 :             m_aPages.back().appendMappedLength( (sal_Int32)nWidth, aLine, true );
    8381           0 :             aLine.append( ' ' );
    8382           0 :             m_aPages.back().appendMappedLength( (sal_Int32)(-nLinePos2-nLineHeight), aLine, true );
    8383           0 :             aLine.append( " l S\n" );
    8384             :         }
    8385             :     }
    8386           0 : }
    8387             : 
    8388           0 : void PDFWriterImpl::drawStrikeoutChar( const Point& rPos, long nWidth, FontStrikeout eStrikeout )
    8389             : {
    8390             :     //See qadevOOo/testdocs/StrikeThrough.odt for examples if you need
    8391             :     //to tweak this
    8392             : 
    8393           0 :     OUString aStrikeoutChar = eStrikeout == STRIKEOUT_SLASH ? OUString( "/" ) : OUString( "X" );
    8394           0 :     OUString aStrikeout = aStrikeoutChar;
    8395           0 :     while( m_pReferenceDevice->GetTextWidth( aStrikeout ) < nWidth )
    8396           0 :         aStrikeout += aStrikeout;
    8397             : 
    8398             :     // do not get broader than nWidth modulo 1 character
    8399           0 :     while( m_pReferenceDevice->GetTextWidth( aStrikeout ) >= nWidth )
    8400           0 :         aStrikeout = aStrikeout.replaceAt( 0, 1, "" );
    8401           0 :     aStrikeout += aStrikeoutChar;
    8402           0 :     bool bShadow = m_aCurrentPDFState.m_aFont.IsShadow();
    8403           0 :     if ( bShadow )
    8404             :     {
    8405           0 :         Font aFont = m_aCurrentPDFState.m_aFont;
    8406           0 :         aFont.SetShadow( false );
    8407           0 :         setFont( aFont );
    8408           0 :         updateGraphicsState();
    8409             :     }
    8410             : 
    8411             :     // strikeout string is left aligned non-CTL text
    8412           0 :     ComplexTextLayoutMode nOrigTLM = m_pReferenceDevice->GetLayoutMode();
    8413           0 :     m_pReferenceDevice->SetLayoutMode( TEXT_LAYOUT_BIDI_STRONG|TEXT_LAYOUT_COMPLEX_DISABLED );
    8414             : 
    8415           0 :     push( PushFlags::CLIPREGION );
    8416           0 :     FontMetric aRefDevFontMetric = m_pReferenceDevice->GetFontMetric();
    8417           0 :     Rectangle aRect;
    8418           0 :     aRect.Left() = rPos.X();
    8419           0 :     aRect.Right() = aRect.Left()+nWidth;
    8420           0 :     aRect.Bottom() = rPos.Y()+aRefDevFontMetric.GetDescent();
    8421           0 :     aRect.Top() = rPos.Y()-aRefDevFontMetric.GetAscent();
    8422             : 
    8423           0 :     ImplFontEntry* pFontEntry = m_pReferenceDevice->mpFontEntry;
    8424           0 :     if (pFontEntry->mnOrientation)
    8425             :     {
    8426           0 :         Polygon aPoly( aRect );
    8427           0 :         aPoly.Rotate( rPos, pFontEntry->mnOrientation);
    8428           0 :         aRect = aPoly.GetBoundRect();
    8429             :     }
    8430             : 
    8431           0 :     intersectClipRegion( aRect );
    8432           0 :     drawText( rPos, aStrikeout, 0, aStrikeout.getLength(), false );
    8433           0 :     pop();
    8434             : 
    8435           0 :     m_pReferenceDevice->SetLayoutMode( nOrigTLM );
    8436             : 
    8437           0 :     if ( bShadow )
    8438             :     {
    8439           0 :         Font aFont = m_aCurrentPDFState.m_aFont;
    8440           0 :         aFont.SetShadow( true );
    8441           0 :         setFont( aFont );
    8442           0 :         updateGraphicsState();
    8443           0 :     }
    8444           0 : }
    8445             : 
    8446           0 : void PDFWriterImpl::drawTextLine( const Point& rPos, long nWidth, FontStrikeout eStrikeout, FontUnderline eUnderline, FontUnderline eOverline, bool bUnderlineAbove )
    8447             : {
    8448           0 :     if ( !nWidth ||
    8449           0 :          ( ((eStrikeout == STRIKEOUT_NONE)||(eStrikeout == STRIKEOUT_DONTKNOW)) &&
    8450           0 :            ((eUnderline == UNDERLINE_NONE)||(eUnderline == UNDERLINE_DONTKNOW)) &&
    8451           0 :            ((eOverline  == UNDERLINE_NONE)||(eOverline  == UNDERLINE_DONTKNOW)) ) )
    8452           0 :         return;
    8453             : 
    8454           0 :     MARK( "drawTextLine" );
    8455           0 :     updateGraphicsState();
    8456             : 
    8457             :     // note: units in pFontEntry are ref device pixel
    8458           0 :     ImplFontEntry*  pFontEntry = m_pReferenceDevice->mpFontEntry;
    8459           0 :     Color           aUnderlineColor = m_aCurrentPDFState.m_aTextLineColor;
    8460           0 :     Color           aOverlineColor  = m_aCurrentPDFState.m_aOverlineColor;
    8461           0 :     Color           aStrikeoutColor = m_aCurrentPDFState.m_aFont.GetColor();
    8462           0 :     bool            bStrikeoutDone = false;
    8463           0 :     bool            bUnderlineDone = false;
    8464           0 :     bool            bOverlineDone  = false;
    8465             : 
    8466           0 :     if ( (eStrikeout == STRIKEOUT_SLASH) || (eStrikeout == STRIKEOUT_X) )
    8467             :     {
    8468           0 :         drawStrikeoutChar( rPos, nWidth, eStrikeout );
    8469           0 :         bStrikeoutDone = true;
    8470             :     }
    8471             : 
    8472           0 :     Point aPos( rPos );
    8473           0 :     TextAlign eAlign = m_aCurrentPDFState.m_aFont.GetAlign();
    8474           0 :     if( eAlign == ALIGN_TOP )
    8475           0 :         aPos.Y() += HCONV( pFontEntry->maMetric.mnAscent );
    8476           0 :     else if( eAlign == ALIGN_BOTTOM )
    8477           0 :         aPos.Y() -= HCONV( pFontEntry->maMetric.mnDescent );
    8478             : 
    8479           0 :     OStringBuffer aLine( 512 );
    8480             :     // save GS
    8481           0 :     aLine.append( "q " );
    8482             : 
    8483             :     // rotate and translate matrix
    8484           0 :     double fAngle = (double)m_aCurrentPDFState.m_aFont.GetOrientation() * M_PI / 1800.0;
    8485           0 :     Matrix3 aMat;
    8486           0 :     aMat.rotate( fAngle );
    8487           0 :     aMat.translate( aPos.X(), aPos.Y() );
    8488           0 :     aMat.append( m_aPages.back(), aLine );
    8489           0 :     aLine.append( " cm\n" );
    8490             : 
    8491           0 :     if ( aUnderlineColor.GetTransparency() != 0 )
    8492           0 :         aUnderlineColor = aStrikeoutColor;
    8493             : 
    8494           0 :     if ( (eUnderline == UNDERLINE_SMALLWAVE) ||
    8495           0 :          (eUnderline == UNDERLINE_WAVE) ||
    8496           0 :          (eUnderline == UNDERLINE_DOUBLEWAVE) ||
    8497             :          (eUnderline == UNDERLINE_BOLDWAVE) )
    8498             :     {
    8499           0 :         drawWaveTextLine( aLine, nWidth, eUnderline, aUnderlineColor, bUnderlineAbove );
    8500           0 :         bUnderlineDone = true;
    8501             :     }
    8502             : 
    8503           0 :     if ( (eOverline == UNDERLINE_SMALLWAVE) ||
    8504           0 :          (eOverline == UNDERLINE_WAVE) ||
    8505           0 :          (eOverline == UNDERLINE_DOUBLEWAVE) ||
    8506             :          (eOverline == UNDERLINE_BOLDWAVE) )
    8507             :     {
    8508           0 :         drawWaveTextLine( aLine, nWidth, eOverline, aOverlineColor, true );
    8509           0 :         bOverlineDone = true;
    8510             :     }
    8511             : 
    8512           0 :     if ( !bUnderlineDone )
    8513             :     {
    8514           0 :         drawStraightTextLine( aLine, nWidth, eUnderline, aUnderlineColor, bUnderlineAbove );
    8515             :     }
    8516             : 
    8517           0 :     if ( !bOverlineDone )
    8518             :     {
    8519           0 :         drawStraightTextLine( aLine, nWidth, eOverline, aOverlineColor, true );
    8520             :     }
    8521             : 
    8522           0 :     if ( !bStrikeoutDone )
    8523             :     {
    8524           0 :         drawStrikeoutLine( aLine, nWidth, eStrikeout, aStrikeoutColor );
    8525             :     }
    8526             : 
    8527           0 :     aLine.append( "Q\n" );
    8528           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8529             : }
    8530             : 
    8531           0 : void PDFWriterImpl::drawPolygon( const Polygon& rPoly )
    8532             : {
    8533           0 :     MARK( "drawPolygon" );
    8534             : 
    8535           0 :     updateGraphicsState();
    8536             : 
    8537           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8538           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8539           0 :         return;
    8540             : 
    8541           0 :     int nPoints = rPoly.GetSize();
    8542           0 :     OStringBuffer aLine( 20 * nPoints );
    8543           0 :     m_aPages.back().appendPolygon( rPoly, aLine );
    8544           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8545           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8546           0 :         aLine.append( "B*\n" );
    8547           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8548           0 :         aLine.append( "S\n" );
    8549             :     else
    8550           0 :         aLine.append( "f*\n" );
    8551             : 
    8552           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8553             : }
    8554             : 
    8555           0 : void PDFWriterImpl::drawPolyPolygon( const tools::PolyPolygon& rPolyPoly )
    8556             : {
    8557           0 :     MARK( "drawPolyPolygon" );
    8558             : 
    8559           0 :     updateGraphicsState();
    8560             : 
    8561           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8562           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8563           0 :         return;
    8564             : 
    8565           0 :     int nPolygons = rPolyPoly.Count();
    8566             : 
    8567           0 :     OStringBuffer aLine( 40 * nPolygons );
    8568           0 :     m_aPages.back().appendPolyPolygon( rPolyPoly, aLine );
    8569           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8570           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8571           0 :         aLine.append( "B*\n" );
    8572           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8573           0 :         aLine.append( "S\n" );
    8574             :     else
    8575           0 :         aLine.append( "f*\n" );
    8576             : 
    8577           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8578             : }
    8579             : 
    8580           0 : void PDFWriterImpl::drawTransparent( const tools::PolyPolygon& rPolyPoly, sal_uInt32 nTransparentPercent )
    8581             : {
    8582             :     DBG_ASSERT( nTransparentPercent <= 100, "invalid alpha value" );
    8583           0 :     nTransparentPercent = nTransparentPercent % 100;
    8584             : 
    8585           0 :     MARK( "drawTransparent" );
    8586             : 
    8587           0 :     updateGraphicsState();
    8588             : 
    8589           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8590           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8591           0 :         return;
    8592             : 
    8593           0 :     if( m_bIsPDF_A1 || m_aContext.Version < PDFWriter::PDF_1_4 )
    8594             :     {
    8595             :         m_aErrors.insert( m_bIsPDF_A1 ?
    8596             :                           PDFWriter::Warning_Transparency_Omitted_PDFA :
    8597           0 :                           PDFWriter::Warning_Transparency_Omitted_PDF13 );
    8598             : 
    8599           0 :         drawPolyPolygon( rPolyPoly );
    8600           0 :         return;
    8601             :     }
    8602             : 
    8603             :     // create XObject
    8604           0 :     m_aTransparentObjects.push_back( TransparencyEmit() );
    8605             :     // FIXME: polygons with beziers may yield incorrect bound rect
    8606           0 :     m_aTransparentObjects.back().m_aBoundRect     = rPolyPoly.GetBoundRect();
    8607             :     // convert rectangle to default user space
    8608           0 :     m_aPages.back().convertRect( m_aTransparentObjects.back().m_aBoundRect );
    8609           0 :     m_aTransparentObjects.back().m_nObject          = createObject();
    8610           0 :     m_aTransparentObjects.back().m_nExtGStateObject = createObject();
    8611           0 :     m_aTransparentObjects.back().m_fAlpha           = (double)(100-nTransparentPercent) / 100.0;
    8612           0 :     m_aTransparentObjects.back().m_pContentStream   = new SvMemoryStream( 256, 256 );
    8613             :     // create XObject's content stream
    8614           0 :     OStringBuffer aContent( 256 );
    8615           0 :     m_aPages.back().appendPolyPolygon( rPolyPoly, aContent );
    8616           0 :     if( m_aCurrentPDFState.m_aLineColor != Color( COL_TRANSPARENT ) &&
    8617           0 :         m_aCurrentPDFState.m_aFillColor != Color( COL_TRANSPARENT ) )
    8618           0 :         aContent.append( " B*\n" );
    8619           0 :     else if( m_aCurrentPDFState.m_aLineColor != Color( COL_TRANSPARENT ) )
    8620           0 :         aContent.append( " S\n" );
    8621             :     else
    8622           0 :         aContent.append( " f*\n" );
    8623           0 :     m_aTransparentObjects.back().m_pContentStream->Write( aContent.getStr(), aContent.getLength() );
    8624             : 
    8625           0 :     OStringBuffer aObjName( 16 );
    8626           0 :     aObjName.append( "Tr" );
    8627           0 :     aObjName.append( m_aTransparentObjects.back().m_nObject );
    8628           0 :     OString aTrName( aObjName.makeStringAndClear() );
    8629           0 :     aObjName.append( "EGS" );
    8630           0 :     aObjName.append( m_aTransparentObjects.back().m_nExtGStateObject );
    8631           0 :     OString aExtName( aObjName.makeStringAndClear() );
    8632             : 
    8633           0 :     OStringBuffer aLine( 80 );
    8634             :     // insert XObject
    8635           0 :     aLine.append( "q /" );
    8636           0 :     aLine.append( aExtName );
    8637           0 :     aLine.append( " gs /" );
    8638           0 :     aLine.append( aTrName );
    8639           0 :     aLine.append( " Do Q\n" );
    8640           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8641             : 
    8642           0 :     pushResource( ResXObject, aTrName, m_aTransparentObjects.back().m_nObject );
    8643           0 :     pushResource( ResExtGState, aExtName, m_aTransparentObjects.back().m_nExtGStateObject );
    8644             : }
    8645             : 
    8646           0 : void PDFWriterImpl::pushResource( ResourceKind eKind, const OString& rResource, sal_Int32 nObject )
    8647             : {
    8648           0 :     if( nObject >= 0 )
    8649             :     {
    8650           0 :         switch( eKind )
    8651             :         {
    8652             :             case ResXObject:
    8653           0 :                 m_aGlobalResourceDict.m_aXObjects[ rResource ] = nObject;
    8654           0 :                 if( ! m_aOutputStreams.empty() )
    8655           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aXObjects[ rResource ] = nObject;
    8656           0 :                 break;
    8657             :             case ResExtGState:
    8658           0 :                 m_aGlobalResourceDict.m_aExtGStates[ rResource ] = nObject;
    8659           0 :                 if( ! m_aOutputStreams.empty() )
    8660           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aExtGStates[ rResource ] = nObject;
    8661           0 :                 break;
    8662             :             case ResShading:
    8663           0 :                 m_aGlobalResourceDict.m_aShadings[ rResource ] = nObject;
    8664           0 :                 if( ! m_aOutputStreams.empty() )
    8665           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aShadings[ rResource ] = nObject;
    8666           0 :                 break;
    8667             :             case ResPattern:
    8668           0 :                 m_aGlobalResourceDict.m_aPatterns[ rResource ] = nObject;
    8669           0 :                 if( ! m_aOutputStreams.empty() )
    8670           0 :                     m_aOutputStreams.front().m_aResourceDict.m_aPatterns[ rResource ] = nObject;
    8671           0 :                 break;
    8672             :         }
    8673             :     }
    8674           0 : }
    8675             : 
    8676           0 : void PDFWriterImpl::beginRedirect( SvStream* pStream, const Rectangle& rTargetRect )
    8677             : {
    8678           0 :     push( PushFlags::ALL );
    8679             : 
    8680             :     // force reemitting clip region inside the new stream, and
    8681             :     // prevent emitting an unbalanced "Q" at the start
    8682           0 :     clearClipRegion();
    8683             :     // this is needed to point m_aCurrentPDFState at the pushed state
    8684             :     // ... but it's pointless to actually write into the "outer" stream here!
    8685           0 :     updateGraphicsState(NOWRITE);
    8686             : 
    8687           0 :     m_aOutputStreams.push_front( StreamRedirect() );
    8688           0 :     m_aOutputStreams.front().m_pStream = pStream;
    8689           0 :     m_aOutputStreams.front().m_aMapMode = m_aMapMode;
    8690             : 
    8691           0 :     if( !rTargetRect.IsEmpty() )
    8692             :     {
    8693           0 :         m_aOutputStreams.front().m_aTargetRect =
    8694           0 :             lcl_convert( m_aGraphicsStack.front().m_aMapMode,
    8695             :                          m_aMapMode,
    8696             :                          getReferenceDevice(),
    8697           0 :                          rTargetRect );
    8698           0 :         Point aDelta = m_aOutputStreams.front().m_aTargetRect.BottomLeft();
    8699           0 :         long nPageHeight = pointToPixel(m_aPages[m_nCurrentPage].getHeight());
    8700           0 :         aDelta.Y() = -(nPageHeight - m_aOutputStreams.front().m_aTargetRect.Bottom());
    8701           0 :         m_aMapMode.SetOrigin( m_aMapMode.GetOrigin() + aDelta );
    8702             :     }
    8703             : 
    8704             :     // setup graphics state for independent object stream
    8705             : 
    8706             :     // force reemitting colors
    8707           0 :     m_aCurrentPDFState.m_aLineColor = Color( COL_TRANSPARENT );
    8708           0 :     m_aCurrentPDFState.m_aFillColor = Color( COL_TRANSPARENT );
    8709           0 : }
    8710             : 
    8711           0 : SvStream* PDFWriterImpl::endRedirect()
    8712             : {
    8713           0 :     SvStream* pStream = NULL;
    8714           0 :     if( ! m_aOutputStreams.empty() )
    8715             :     {
    8716           0 :         pStream     = m_aOutputStreams.front().m_pStream;
    8717           0 :         m_aMapMode  = m_aOutputStreams.front().m_aMapMode;
    8718           0 :         m_aOutputStreams.pop_front();
    8719             :     }
    8720             : 
    8721           0 :     pop();
    8722             : 
    8723           0 :     m_aCurrentPDFState.m_aLineColor = Color( COL_TRANSPARENT );
    8724           0 :     m_aCurrentPDFState.m_aFillColor = Color( COL_TRANSPARENT );
    8725             : 
    8726             :     // needed after pop() to set m_aCurrentPDFState
    8727           0 :     updateGraphicsState(NOWRITE);
    8728             : 
    8729           0 :     return pStream;
    8730             : }
    8731             : 
    8732           0 : void PDFWriterImpl::beginTransparencyGroup()
    8733             : {
    8734           0 :     updateGraphicsState();
    8735           0 :     if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    8736           0 :         beginRedirect( new SvMemoryStream( 1024, 1024 ), Rectangle() );
    8737           0 : }
    8738             : 
    8739           0 : void PDFWriterImpl::endTransparencyGroup( const Rectangle& rBoundingBox, sal_uInt32 nTransparentPercent )
    8740             : {
    8741             :     DBG_ASSERT( nTransparentPercent <= 100, "invalid alpha value" );
    8742           0 :     nTransparentPercent = nTransparentPercent % 100;
    8743             : 
    8744           0 :     if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    8745             :     {
    8746             :         // create XObject
    8747           0 :         m_aTransparentObjects.push_back( TransparencyEmit() );
    8748           0 :         m_aTransparentObjects.back().m_aBoundRect   = rBoundingBox;
    8749             :         // convert rectangle to default user space
    8750           0 :         m_aPages.back().convertRect( m_aTransparentObjects.back().m_aBoundRect );
    8751           0 :         m_aTransparentObjects.back().m_nObject      = createObject();
    8752           0 :         m_aTransparentObjects.back().m_fAlpha       = (double)(100-nTransparentPercent) / 100.0;
    8753             :         // get XObject's content stream
    8754           0 :         m_aTransparentObjects.back().m_pContentStream = static_cast<SvMemoryStream*>(endRedirect());
    8755           0 :         m_aTransparentObjects.back().m_nExtGStateObject = createObject();
    8756             : 
    8757           0 :         OStringBuffer aObjName( 16 );
    8758           0 :         aObjName.append( "Tr" );
    8759           0 :         aObjName.append( m_aTransparentObjects.back().m_nObject );
    8760           0 :         OString aTrName( aObjName.makeStringAndClear() );
    8761           0 :         aObjName.append( "EGS" );
    8762           0 :         aObjName.append( m_aTransparentObjects.back().m_nExtGStateObject );
    8763           0 :         OString aExtName( aObjName.makeStringAndClear() );
    8764             : 
    8765           0 :         OStringBuffer aLine( 80 );
    8766             :         // insert XObject
    8767           0 :         aLine.append( "q /" );
    8768           0 :         aLine.append( aExtName );
    8769           0 :         aLine.append( " gs /" );
    8770           0 :         aLine.append( aTrName );
    8771           0 :         aLine.append( " Do Q\n" );
    8772           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    8773             : 
    8774           0 :         pushResource( ResXObject, aTrName, m_aTransparentObjects.back().m_nObject );
    8775           0 :         pushResource( ResExtGState, aExtName, m_aTransparentObjects.back().m_nExtGStateObject );
    8776             :     }
    8777           0 : }
    8778             : 
    8779           0 : void PDFWriterImpl::drawRectangle( const Rectangle& rRect )
    8780             : {
    8781           0 :     MARK( "drawRectangle" );
    8782             : 
    8783           0 :     updateGraphicsState();
    8784             : 
    8785           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8786           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8787           0 :         return;
    8788             : 
    8789           0 :     OStringBuffer aLine( 40 );
    8790           0 :     m_aPages.back().appendRect( rRect, aLine );
    8791             : 
    8792           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8793           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8794           0 :         aLine.append( " B*\n" );
    8795           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8796           0 :         aLine.append( " S\n" );
    8797             :     else
    8798           0 :         aLine.append( " f*\n" );
    8799             : 
    8800           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8801             : }
    8802             : 
    8803           0 : void PDFWriterImpl::drawRectangle( const Rectangle& rRect, sal_uInt32 nHorzRound, sal_uInt32 nVertRound )
    8804             : {
    8805           0 :     MARK( "drawRectangle with rounded edges" );
    8806             : 
    8807           0 :     if( !nHorzRound && !nVertRound )
    8808           0 :         drawRectangle( rRect );
    8809             : 
    8810           0 :     updateGraphicsState();
    8811             : 
    8812           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8813           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8814           0 :         return;
    8815             : 
    8816           0 :     if( nHorzRound > (sal_uInt32)rRect.GetWidth()/2 )
    8817           0 :         nHorzRound = rRect.GetWidth()/2;
    8818           0 :     if( nVertRound > (sal_uInt32)rRect.GetHeight()/2 )
    8819           0 :         nVertRound = rRect.GetHeight()/2;
    8820             : 
    8821           0 :     Point aPoints[16];
    8822           0 :     const double kappa = 0.5522847498;
    8823           0 :     const sal_uInt32 kx = (sal_uInt32)((kappa*(double)nHorzRound)+0.5);
    8824           0 :     const sal_uInt32 ky = (sal_uInt32)((kappa*(double)nVertRound)+0.5);
    8825             : 
    8826           0 :     aPoints[1]  = Point( rRect.TopLeft().X() + nHorzRound, rRect.TopLeft().Y() );
    8827           0 :     aPoints[0]  = Point( aPoints[1].X() - kx, aPoints[1].Y() );
    8828           0 :     aPoints[2]  = Point( rRect.TopRight().X()+1 - nHorzRound, aPoints[1].Y() );
    8829           0 :     aPoints[3]  = Point( aPoints[2].X()+kx, aPoints[2].Y() );
    8830             : 
    8831           0 :     aPoints[5]  = Point( rRect.TopRight().X()+1, rRect.TopRight().Y()+nVertRound );
    8832           0 :     aPoints[4]  = Point( aPoints[5].X(), aPoints[5].Y()-ky );
    8833           0 :     aPoints[6]  = Point( aPoints[5].X(), rRect.BottomRight().Y()+1 - nVertRound );
    8834           0 :     aPoints[7]  = Point( aPoints[6].X(), aPoints[6].Y()+ky );
    8835             : 
    8836           0 :     aPoints[9]  = Point( rRect.BottomRight().X()+1-nHorzRound, rRect.BottomRight().Y()+1 );
    8837           0 :     aPoints[8]  = Point( aPoints[9].X()+kx, aPoints[9].Y() );
    8838           0 :     aPoints[10] = Point( rRect.BottomLeft().X() + nHorzRound, aPoints[9].Y() );
    8839           0 :     aPoints[11] = Point( aPoints[10].X()-kx, aPoints[10].Y() );
    8840             : 
    8841           0 :     aPoints[13] = Point( rRect.BottomLeft().X(), rRect.BottomLeft().Y()+1-nVertRound );
    8842           0 :     aPoints[12] = Point( aPoints[13].X(), aPoints[13].Y()+ky );
    8843           0 :     aPoints[14] = Point( rRect.TopLeft().X(), rRect.TopLeft().Y()+nVertRound );
    8844           0 :     aPoints[15] = Point( aPoints[14].X(), aPoints[14].Y()-ky );
    8845             : 
    8846           0 :     OStringBuffer aLine( 80 );
    8847           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8848           0 :     aLine.append( " m " );
    8849           0 :     m_aPages.back().appendPoint( aPoints[2], aLine );
    8850           0 :     aLine.append( " l " );
    8851           0 :     m_aPages.back().appendPoint( aPoints[3], aLine );
    8852           0 :     aLine.append( ' ' );
    8853           0 :     m_aPages.back().appendPoint( aPoints[4], aLine );
    8854           0 :     aLine.append( ' ' );
    8855           0 :     m_aPages.back().appendPoint( aPoints[5], aLine );
    8856           0 :     aLine.append( " c\n" );
    8857           0 :     m_aPages.back().appendPoint( aPoints[6], aLine );
    8858           0 :     aLine.append( " l " );
    8859           0 :     m_aPages.back().appendPoint( aPoints[7], aLine );
    8860           0 :     aLine.append( ' ' );
    8861           0 :     m_aPages.back().appendPoint( aPoints[8], aLine );
    8862           0 :     aLine.append( ' ' );
    8863           0 :     m_aPages.back().appendPoint( aPoints[9], aLine );
    8864           0 :     aLine.append( " c\n" );
    8865           0 :     m_aPages.back().appendPoint( aPoints[10], aLine );
    8866           0 :     aLine.append( " l " );
    8867           0 :     m_aPages.back().appendPoint( aPoints[11], aLine );
    8868           0 :     aLine.append( ' ' );
    8869           0 :     m_aPages.back().appendPoint( aPoints[12], aLine );
    8870           0 :     aLine.append( ' ' );
    8871           0 :     m_aPages.back().appendPoint( aPoints[13], aLine );
    8872           0 :     aLine.append( " c\n" );
    8873           0 :     m_aPages.back().appendPoint( aPoints[14], aLine );
    8874           0 :     aLine.append( " l " );
    8875           0 :     m_aPages.back().appendPoint( aPoints[15], aLine );
    8876           0 :     aLine.append( ' ' );
    8877           0 :     m_aPages.back().appendPoint( aPoints[0], aLine );
    8878           0 :     aLine.append( ' ' );
    8879           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8880           0 :     aLine.append( " c " );
    8881             : 
    8882           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8883           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8884           0 :         aLine.append( "b*\n" );
    8885           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8886           0 :         aLine.append( "s\n" );
    8887             :     else
    8888           0 :         aLine.append( "f*\n" );
    8889             : 
    8890           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8891             : }
    8892             : 
    8893           0 : void PDFWriterImpl::drawEllipse( const Rectangle& rRect )
    8894             : {
    8895           0 :     MARK( "drawEllipse" );
    8896             : 
    8897           0 :     updateGraphicsState();
    8898             : 
    8899           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8900           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8901           0 :         return;
    8902             : 
    8903           0 :     Point aPoints[12];
    8904           0 :     const double kappa = 0.5522847498;
    8905           0 :     const sal_uInt32 kx = (sal_uInt32)((kappa*(double)rRect.GetWidth()/2.0)+0.5);
    8906           0 :     const sal_uInt32 ky = (sal_uInt32)((kappa*(double)rRect.GetHeight()/2.0)+0.5);
    8907             : 
    8908           0 :     aPoints[1]  = Point( rRect.TopLeft().X() + rRect.GetWidth()/2, rRect.TopLeft().Y() );
    8909           0 :     aPoints[0]  = Point( aPoints[1].X() - kx, aPoints[1].Y() );
    8910           0 :     aPoints[2]  = Point( aPoints[1].X() + kx, aPoints[1].Y() );
    8911             : 
    8912           0 :     aPoints[4]  = Point( rRect.TopRight().X()+1, rRect.TopRight().Y() + rRect.GetHeight()/2 );
    8913           0 :     aPoints[3]  = Point( aPoints[4].X(), aPoints[4].Y() - ky );
    8914           0 :     aPoints[5]  = Point( aPoints[4].X(), aPoints[4].Y() + ky );
    8915             : 
    8916           0 :     aPoints[7]  = Point( rRect.BottomLeft().X() + rRect.GetWidth()/2, rRect.BottomLeft().Y()+1 );
    8917           0 :     aPoints[6]  = Point( aPoints[7].X() + kx, aPoints[7].Y() );
    8918           0 :     aPoints[8]  = Point( aPoints[7].X() - kx, aPoints[7].Y() );
    8919             : 
    8920           0 :     aPoints[10] = Point( rRect.TopLeft().X(), rRect.TopLeft().Y() + rRect.GetHeight()/2 );
    8921           0 :     aPoints[9]  = Point( aPoints[10].X(), aPoints[10].Y() + ky );
    8922           0 :     aPoints[11] = Point( aPoints[10].X(), aPoints[10].Y() - ky );
    8923             : 
    8924           0 :     OStringBuffer aLine( 80 );
    8925           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8926           0 :     aLine.append( " m " );
    8927           0 :     m_aPages.back().appendPoint( aPoints[2], aLine );
    8928           0 :     aLine.append( ' ' );
    8929           0 :     m_aPages.back().appendPoint( aPoints[3], aLine );
    8930           0 :     aLine.append( ' ' );
    8931           0 :     m_aPages.back().appendPoint( aPoints[4], aLine );
    8932           0 :     aLine.append( " c\n" );
    8933           0 :     m_aPages.back().appendPoint( aPoints[5], aLine );
    8934           0 :     aLine.append( ' ' );
    8935           0 :     m_aPages.back().appendPoint( aPoints[6], aLine );
    8936           0 :     aLine.append( ' ' );
    8937           0 :     m_aPages.back().appendPoint( aPoints[7], aLine );
    8938           0 :     aLine.append( " c\n" );
    8939           0 :     m_aPages.back().appendPoint( aPoints[8], aLine );
    8940           0 :     aLine.append( ' ' );
    8941           0 :     m_aPages.back().appendPoint( aPoints[9], aLine );
    8942           0 :     aLine.append( ' ' );
    8943           0 :     m_aPages.back().appendPoint( aPoints[10], aLine );
    8944           0 :     aLine.append( " c\n" );
    8945           0 :     m_aPages.back().appendPoint( aPoints[11], aLine );
    8946           0 :     aLine.append( ' ' );
    8947           0 :     m_aPages.back().appendPoint( aPoints[0], aLine );
    8948           0 :     aLine.append( ' ' );
    8949           0 :     m_aPages.back().appendPoint( aPoints[1], aLine );
    8950           0 :     aLine.append( " c " );
    8951             : 
    8952           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    8953           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    8954           0 :         aLine.append( "b*\n" );
    8955           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    8956           0 :         aLine.append( "s\n" );
    8957             :     else
    8958           0 :         aLine.append( "f*\n" );
    8959             : 
    8960           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    8961             : }
    8962             : 
    8963           0 : static double calcAngle( const Rectangle& rRect, const Point& rPoint )
    8964             : {
    8965           0 :     Point aOrigin((rRect.Left()+rRect.Right()+1)/2,
    8966           0 :                   (rRect.Top()+rRect.Bottom()+1)/2);
    8967           0 :     Point aPoint = rPoint - aOrigin;
    8968             : 
    8969           0 :     double fX = (double)aPoint.X();
    8970           0 :     double fY = (double)-aPoint.Y();
    8971             : 
    8972           0 :     if( rRect.GetWidth() > rRect.GetHeight() )
    8973           0 :         fY = fY*((double)rRect.GetWidth()/(double)rRect.GetHeight());
    8974           0 :     else if( rRect.GetHeight() > rRect.GetWidth() )
    8975           0 :         fX = fX*((double)rRect.GetHeight()/(double)rRect.GetWidth());
    8976           0 :     return atan2( fY, fX );
    8977             : }
    8978             : 
    8979           0 : void PDFWriterImpl::drawArc( const Rectangle& rRect, const Point& rStart, const Point& rStop, bool bWithPie, bool bWithChord )
    8980             : {
    8981           0 :     MARK( "drawArc" );
    8982             : 
    8983           0 :     updateGraphicsState();
    8984             : 
    8985           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) &&
    8986           0 :         m_aGraphicsStack.front().m_aFillColor == Color( COL_TRANSPARENT ) )
    8987           0 :         return;
    8988             : 
    8989             :     // calculate start and stop angles
    8990           0 :     const double fStartAngle = calcAngle( rRect, rStart );
    8991           0 :     double fStopAngle  = calcAngle( rRect, rStop );
    8992           0 :     while( fStopAngle < fStartAngle )
    8993           0 :         fStopAngle += 2.0*M_PI;
    8994           0 :     const int nFragments = (int)((fStopAngle-fStartAngle)/(M_PI/2.0))+1;
    8995           0 :     const double fFragmentDelta = (fStopAngle-fStartAngle)/(double)nFragments;
    8996           0 :     const double kappa = fabs( 4.0 * (1.0-cos(fFragmentDelta/2.0))/sin(fFragmentDelta/2.0) / 3.0);
    8997           0 :     const double halfWidth = (double)rRect.GetWidth()/2.0;
    8998           0 :     const double halfHeight = (double)rRect.GetHeight()/2.0;
    8999             : 
    9000           0 :     const Point aCenter( (rRect.Left()+rRect.Right()+1)/2,
    9001           0 :                          (rRect.Top()+rRect.Bottom()+1)/2 );
    9002             : 
    9003           0 :     OStringBuffer aLine( 30*nFragments );
    9004           0 :     Point aPoint( (int)(halfWidth * cos(fStartAngle) ),
    9005           0 :                   -(int)(halfHeight * sin(fStartAngle) ) );
    9006           0 :     aPoint += aCenter;
    9007           0 :     m_aPages.back().appendPoint( aPoint, aLine );
    9008           0 :     aLine.append( " m " );
    9009           0 :     if( !basegfx::fTools::equal(fStartAngle, fStopAngle) )
    9010             :     {
    9011           0 :         for( int i = 0; i < nFragments; i++ )
    9012             :         {
    9013           0 :             const double fStartFragment = fStartAngle + (double)i*fFragmentDelta;
    9014           0 :             const double fStopFragment = fStartFragment + fFragmentDelta;
    9015           0 :             aPoint = Point( (int)(halfWidth * (cos(fStartFragment) - kappa*sin(fStartFragment) ) ),
    9016           0 :                             -(int)(halfHeight * (sin(fStartFragment) + kappa*cos(fStartFragment) ) ) );
    9017           0 :             aPoint += aCenter;
    9018           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9019           0 :             aLine.append( ' ' );
    9020             : 
    9021           0 :             aPoint = Point( (int)(halfWidth * (cos(fStopFragment) + kappa*sin(fStopFragment) ) ),
    9022           0 :                             -(int)(halfHeight * (sin(fStopFragment) - kappa*cos(fStopFragment) ) ) );
    9023           0 :             aPoint += aCenter;
    9024           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9025           0 :             aLine.append( ' ' );
    9026             : 
    9027           0 :             aPoint = Point( (int)(halfWidth * cos(fStopFragment) ),
    9028           0 :                             -(int)(halfHeight * sin(fStopFragment) ) );
    9029           0 :             aPoint += aCenter;
    9030           0 :             m_aPages.back().appendPoint( aPoint, aLine );
    9031           0 :             aLine.append( " c\n" );
    9032             :         }
    9033             :     }
    9034           0 :     if( bWithChord || bWithPie )
    9035             :     {
    9036           0 :         if( bWithPie )
    9037             :         {
    9038           0 :             m_aPages.back().appendPoint( aCenter, aLine );
    9039           0 :             aLine.append( " l " );
    9040             :         }
    9041           0 :         aLine.append( "h " );
    9042             :     }
    9043           0 :     if( ! bWithChord && ! bWithPie )
    9044           0 :         aLine.append( "S\n" );
    9045           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) &&
    9046           0 :         m_aGraphicsStack.front().m_aFillColor != Color( COL_TRANSPARENT ) )
    9047           0 :         aLine.append( "B*\n" );
    9048           0 :     else if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
    9049           0 :         aLine.append( "S\n" );
    9050             :     else
    9051           0 :         aLine.append( "f*\n" );
    9052             : 
    9053           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9054             : }
    9055             : 
    9056           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly )
    9057             : {
    9058           0 :     MARK( "drawPolyLine" );
    9059             : 
    9060           0 :     sal_uInt16 nPoints = rPoly.GetSize();
    9061           0 :     if( nPoints < 2 )
    9062           0 :         return;
    9063             : 
    9064           0 :     updateGraphicsState();
    9065             : 
    9066           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9067           0 :         return;
    9068             : 
    9069           0 :     OStringBuffer aLine( 20 * nPoints );
    9070           0 :     m_aPages.back().appendPolygon( rPoly, aLine, rPoly[0] == rPoly[nPoints-1] );
    9071           0 :     aLine.append( "S\n" );
    9072             : 
    9073           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9074             : }
    9075             : 
    9076           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly, const LineInfo& rInfo )
    9077             : {
    9078           0 :     MARK( "drawPolyLine with LineInfo" );
    9079             : 
    9080           0 :     updateGraphicsState();
    9081             : 
    9082           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9083           0 :         return;
    9084             : 
    9085           0 :     OStringBuffer aLine;
    9086           0 :     aLine.append( "q " );
    9087           0 :     if( m_aPages.back().appendLineInfo( rInfo, aLine ) )
    9088             :     {
    9089           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9090           0 :         drawPolyLine( rPoly );
    9091           0 :         writeBuffer( "Q\n", 2 );
    9092             :     }
    9093             :     else
    9094             :     {
    9095           0 :         PDFWriter::ExtLineInfo aInfo;
    9096           0 :         convertLineInfoToExtLineInfo( rInfo, aInfo );
    9097           0 :         drawPolyLine( rPoly, aInfo );
    9098           0 :     }
    9099             : }
    9100             : 
    9101           0 : void PDFWriterImpl::convertLineInfoToExtLineInfo( const LineInfo& rIn, PDFWriter::ExtLineInfo& rOut )
    9102             : {
    9103             :     DBG_ASSERT( rIn.GetStyle() == LINE_DASH, "invalid conversion" );
    9104           0 :     rOut.m_fLineWidth           = rIn.GetWidth();
    9105           0 :     rOut.m_fTransparency        = 0.0;
    9106           0 :     rOut.m_eCap                 = PDFWriter::capButt;
    9107           0 :     rOut.m_eJoin                = PDFWriter::joinMiter;
    9108           0 :     rOut.m_fMiterLimit          = 10;
    9109           0 :     rOut.m_aDashArray.clear();
    9110             : 
    9111             :     // add DashDot to DashArray
    9112           0 :     const int nDashes   = rIn.GetDashCount();
    9113           0 :     const int nDashLen  = rIn.GetDashLen();
    9114           0 :     const int nDistance = rIn.GetDistance();
    9115             : 
    9116           0 :     for( int n  = 0; n < nDashes; n++ )
    9117             :     {
    9118           0 :         rOut.m_aDashArray.push_back( nDashLen );
    9119           0 :         rOut.m_aDashArray.push_back( nDistance );
    9120             :     }
    9121           0 :     const int nDots   = rIn.GetDotCount();
    9122           0 :     const int nDotLen = rIn.GetDotLen();
    9123             : 
    9124           0 :     for( int n  = 0; n < nDots; n++ )
    9125             :     {
    9126           0 :         rOut.m_aDashArray.push_back( nDotLen );
    9127           0 :         rOut.m_aDashArray.push_back( nDistance );
    9128             :     }
    9129             : 
    9130             :     // add LineJoin
    9131           0 :     switch(rIn.GetLineJoin())
    9132             :     {
    9133             :         case basegfx::B2DLINEJOIN_BEVEL :
    9134             :         {
    9135           0 :             rOut.m_eJoin = PDFWriter::joinBevel;
    9136           0 :             break;
    9137             :         }
    9138             :         default : // basegfx::B2DLINEJOIN_NONE :
    9139             :         // Pdf has no 'none' lineJoin, default is miter
    9140             :         case basegfx::B2DLINEJOIN_MIDDLE :
    9141             :         case basegfx::B2DLINEJOIN_MITER :
    9142             :         {
    9143           0 :             rOut.m_eJoin = PDFWriter::joinMiter;
    9144           0 :             break;
    9145             :         }
    9146             :         case basegfx::B2DLINEJOIN_ROUND :
    9147             :         {
    9148           0 :             rOut.m_eJoin = PDFWriter::joinRound;
    9149           0 :             break;
    9150             :         }
    9151             :     }
    9152             : 
    9153             :     // add LineCap
    9154           0 :     switch(rIn.GetLineCap())
    9155             :     {
    9156             :         default: /* com::sun::star::drawing::LineCap_BUTT */
    9157             :         {
    9158           0 :             rOut.m_eCap = PDFWriter::capButt;
    9159           0 :             break;
    9160             :         }
    9161             :         case com::sun::star::drawing::LineCap_ROUND:
    9162             :         {
    9163           0 :             rOut.m_eCap = PDFWriter::capRound;
    9164           0 :             break;
    9165             :         }
    9166             :         case com::sun::star::drawing::LineCap_SQUARE:
    9167             :         {
    9168           0 :             rOut.m_eCap = PDFWriter::capSquare;
    9169           0 :             break;
    9170             :         }
    9171             :     }
    9172           0 : }
    9173             : 
    9174           0 : void PDFWriterImpl::drawPolyLine( const Polygon& rPoly, const PDFWriter::ExtLineInfo& rInfo )
    9175             : {
    9176           0 :     MARK( "drawPolyLine with ExtLineInfo" );
    9177             : 
    9178           0 :     updateGraphicsState();
    9179             : 
    9180           0 :     if( m_aGraphicsStack.front().m_aLineColor == Color( COL_TRANSPARENT ) )
    9181           0 :         return;
    9182             : 
    9183           0 :     if( rInfo.m_fTransparency >= 1.0 )
    9184           0 :         return;
    9185             : 
    9186           0 :     if( rInfo.m_fTransparency != 0.0 )
    9187           0 :         beginTransparencyGroup();
    9188             : 
    9189           0 :     OStringBuffer aLine;
    9190           0 :     aLine.append( "q " );
    9191           0 :     m_aPages.back().appendMappedLength( rInfo.m_fLineWidth, aLine );
    9192           0 :     aLine.append( " w" );
    9193           0 :     if( rInfo.m_aDashArray.size() < 10 ) // implmentation limit of acrobat reader
    9194             :     {
    9195           0 :         switch( rInfo.m_eCap )
    9196             :         {
    9197             :             default:
    9198           0 :             case PDFWriter::capButt:   aLine.append( " 0 J" );break;
    9199           0 :             case PDFWriter::capRound:  aLine.append( " 1 J" );break;
    9200           0 :             case PDFWriter::capSquare: aLine.append( " 2 J" );break;
    9201             :         }
    9202           0 :         switch( rInfo.m_eJoin )
    9203             :         {
    9204             :             default:
    9205             :             case PDFWriter::joinMiter:
    9206             :             {
    9207           0 :                 double fLimit = rInfo.m_fMiterLimit;
    9208           0 :                 if( rInfo.m_fLineWidth < rInfo.m_fMiterLimit )
    9209           0 :                     fLimit = fLimit / rInfo.m_fLineWidth;
    9210           0 :                 if( fLimit < 1.0 )
    9211           0 :                     fLimit = 1.0;
    9212           0 :                 aLine.append( " 0 j " );
    9213           0 :                 appendDouble( fLimit, aLine );
    9214           0 :                 aLine.append( " M" );
    9215             :             }
    9216           0 :             break;
    9217           0 :             case PDFWriter::joinRound:  aLine.append( " 1 j" );break;
    9218           0 :             case PDFWriter::joinBevel:  aLine.append( " 2 j" );break;
    9219             :         }
    9220           0 :         if( rInfo.m_aDashArray.size() > 0 )
    9221             :         {
    9222           0 :             aLine.append( " [ " );
    9223           0 :             for( std::vector<double>::const_iterator it = rInfo.m_aDashArray.begin();
    9224           0 :                  it != rInfo.m_aDashArray.end(); ++it )
    9225             :             {
    9226           0 :                 m_aPages.back().appendMappedLength( *it, aLine );
    9227           0 :                 aLine.append( ' ' );
    9228             :             }
    9229           0 :             aLine.append( "] 0 d" );
    9230             :         }
    9231           0 :         aLine.append( "\n" );
    9232           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9233           0 :         drawPolyLine( rPoly );
    9234             :     }
    9235             :     else
    9236             :     {
    9237           0 :         basegfx::B2DPolygon aPoly(rPoly.getB2DPolygon());
    9238           0 :         basegfx::B2DPolyPolygon aPolyPoly;
    9239             : 
    9240           0 :         basegfx::tools::applyLineDashing(aPoly, rInfo.m_aDashArray, &aPolyPoly);
    9241             : 
    9242             :         // Old applyLineDashing subdivided the polygon. New one will create bezier curve segments.
    9243             :         // To mimic old behaviour, apply subdivide here. If beziers shall be written (better quality)
    9244             :         // this line needs to be removed and the loop below adapted accordingly
    9245           0 :         aPolyPoly = basegfx::tools::adaptiveSubdivideByAngle(aPolyPoly);
    9246             : 
    9247           0 :         const sal_uInt32 nPolygonCount(aPolyPoly.count());
    9248             : 
    9249           0 :         for( sal_uInt32 nPoly = 0; nPoly < nPolygonCount; nPoly++ )
    9250             :         {
    9251           0 :             aLine.append( (nPoly != 0 && (nPoly & 7) == 0) ? "\n" : " " );
    9252           0 :             aPoly = aPolyPoly.getB2DPolygon( nPoly );
    9253           0 :             const sal_uInt32 nPointCount(aPoly.count());
    9254             : 
    9255           0 :             if(nPointCount)
    9256             :             {
    9257           0 :                 const sal_uInt32 nEdgeCount(aPoly.isClosed() ? nPointCount : nPointCount - 1);
    9258           0 :                 basegfx::B2DPoint aCurrent(aPoly.getB2DPoint(0));
    9259             : 
    9260           0 :                 for(sal_uInt32 a(0); a < nEdgeCount; a++)
    9261             :                 {
    9262           0 :                     if( a > 0 )
    9263           0 :                         aLine.append( " " );
    9264           0 :                     const sal_uInt32 nNextIndex((a + 1) % nPointCount);
    9265           0 :                     const basegfx::B2DPoint aNext(aPoly.getB2DPoint(nNextIndex));
    9266             : 
    9267           0 :                     m_aPages.back().appendPoint( Point( FRound(aCurrent.getX()),
    9268             :                                                         FRound(aCurrent.getY()) ),
    9269           0 :                                                  aLine );
    9270           0 :                     aLine.append( " m " );
    9271           0 :                     m_aPages.back().appendPoint( Point( FRound(aNext.getX()),
    9272             :                                                         FRound(aNext.getY()) ),
    9273           0 :                                                  aLine );
    9274           0 :                     aLine.append( " l" );
    9275             : 
    9276             :                     // prepare next edge
    9277           0 :                     aCurrent = aNext;
    9278           0 :                 }
    9279             :             }
    9280             :         }
    9281           0 :         aLine.append( " S " );
    9282           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
    9283             :     }
    9284           0 :     writeBuffer( "Q\n", 2 );
    9285             : 
    9286           0 :     if( rInfo.m_fTransparency != 0.0 )
    9287             :     {
    9288             :         // FIXME: actually this may be incorrect with bezier polygons
    9289           0 :         Rectangle aBoundRect( rPoly.GetBoundRect() );
    9290             :         // avoid clipping with thick lines
    9291           0 :         if( rInfo.m_fLineWidth > 0.0 )
    9292             :         {
    9293           0 :             sal_Int32 nLW = sal_Int32(rInfo.m_fLineWidth);
    9294           0 :             aBoundRect.Top()    -= nLW;
    9295           0 :             aBoundRect.Left()   -= nLW;
    9296           0 :             aBoundRect.Right()  += nLW;
    9297           0 :             aBoundRect.Bottom() += nLW;
    9298             :         }
    9299           0 :         endTransparencyGroup( aBoundRect, (sal_uInt16)(100.0*rInfo.m_fTransparency) );
    9300           0 :     }
    9301             : }
    9302             : 
    9303           0 : void PDFWriterImpl::drawPixel( const Point& rPoint, const Color& rColor )
    9304             : {
    9305           0 :     MARK( "drawPixel" );
    9306             : 
    9307           0 :     Color aColor = ( rColor == Color( COL_TRANSPARENT ) ? m_aGraphicsStack.front().m_aLineColor : rColor );
    9308             : 
    9309           0 :     if( aColor == Color( COL_TRANSPARENT ) )
    9310           0 :         return;
    9311             : 
    9312             :     // pixels are drawn in line color, so have to set
    9313             :     // the nonstroking color to line color
    9314           0 :     Color aOldFillColor = m_aGraphicsStack.front().m_aFillColor;
    9315           0 :     setFillColor( aColor );
    9316             : 
    9317           0 :     updateGraphicsState();
    9318             : 
    9319           0 :     OStringBuffer aLine( 20 );
    9320           0 :     m_aPages.back().appendPoint( rPoint, aLine );
    9321           0 :     aLine.append( ' ' );
    9322           0 :     appendDouble( 1.0/double(getReferenceDevice()->GetDPIX()), aLine );
    9323           0 :     aLine.append( ' ' );
    9324           0 :     appendDouble( 1.0/double(getReferenceDevice()->GetDPIY()), aLine );
    9325           0 :     aLine.append( " re f\n" );
    9326           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
    9327             : 
    9328           0 :     setFillColor( aOldFillColor );
    9329             : }
    9330             : 
    9331             : class AccessReleaser
    9332             : {
    9333             :     BitmapReadAccess* m_pAccess;
    9334             : public:
    9335           0 :     AccessReleaser( BitmapReadAccess* pAccess ) : m_pAccess( pAccess ){}
    9336           0 :     ~AccessReleaser() { delete m_pAccess; }
    9337             : };
    9338             : 
    9339           0 : bool PDFWriterImpl::writeTransparentObject( TransparencyEmit& rObject )
    9340             : {
    9341           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9342             : 
    9343           0 :     bool bFlateFilter = compressStream( rObject.m_pContentStream );
    9344           0 :     rObject.m_pContentStream->Seek( STREAM_SEEK_TO_END );
    9345           0 :     sal_uLong nSize = rObject.m_pContentStream->Tell();
    9346           0 :     rObject.m_pContentStream->Seek( STREAM_SEEK_TO_BEGIN );
    9347             :     #if OSL_DEBUG_LEVEL > 1
    9348             :     emitComment( "PDFWriterImpl::writeTransparentObject" );
    9349             :     #endif
    9350           0 :     OStringBuffer aLine( 512 );
    9351           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9352           0 :     aLine.append( rObject.m_nObject );
    9353             :     aLine.append( " 0 obj\n"
    9354             :                   "<</Type/XObject\n"
    9355             :                   "/Subtype/Form\n"
    9356           0 :                   "/BBox[ " );
    9357           0 :     appendFixedInt( rObject.m_aBoundRect.Left(), aLine );
    9358           0 :     aLine.append( ' ' );
    9359           0 :     appendFixedInt( rObject.m_aBoundRect.Top(), aLine );
    9360           0 :     aLine.append( ' ' );
    9361           0 :     appendFixedInt( rObject.m_aBoundRect.Right(), aLine );
    9362           0 :     aLine.append( ' ' );
    9363           0 :     appendFixedInt( rObject.m_aBoundRect.Bottom()+1, aLine );
    9364           0 :     aLine.append( " ]\n" );
    9365           0 :     if( ! rObject.m_pSoftMaskStream )
    9366             :     {
    9367           0 :         if( ! m_bIsPDF_A1 )
    9368             :         {
    9369           0 :             aLine.append( "/Group<</S/Transparency/CS/DeviceRGB/K true>>\n" );
    9370             :         }
    9371             :     }
    9372             :     /* #i42884# the PDF reference recommends that each Form XObject
    9373             :     *  should have a resource dict; alas if that is the same object
    9374             :     *  as the one of the page it triggers an endless recursion in
    9375             :     *  acroread 5 (6 and up have that fixed). Since we have only one
    9376             :     *  resource dict anyway, let's use the one from the page by NOT
    9377             :     *  emitting a Resources entry.
    9378             :     */
    9379             : 
    9380           0 :     aLine.append( "/Length " );
    9381           0 :     aLine.append( (sal_Int32)(nSize) );
    9382           0 :     aLine.append( "\n" );
    9383           0 :     if( bFlateFilter )
    9384           0 :         aLine.append( "/Filter/FlateDecode\n" );
    9385             :     aLine.append( ">>\n"
    9386           0 :                   "stream\n" );
    9387           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9388           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9389           0 :     CHECK_RETURN( writeBuffer( rObject.m_pContentStream->GetData(), nSize ) );
    9390           0 :     disableStreamEncryption();
    9391           0 :     aLine.setLength( 0 );
    9392             :     aLine.append( "\n"
    9393             :                   "endstream\n"
    9394           0 :                   "endobj\n\n" );
    9395           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9396             : 
    9397             :     // write ExtGState dict for this XObject
    9398           0 :     aLine.setLength( 0 );
    9399           0 :     aLine.append( rObject.m_nExtGStateObject );
    9400             :     aLine.append( " 0 obj\n"
    9401           0 :                   "<<" );
    9402           0 :     if( ! rObject.m_pSoftMaskStream )
    9403             :     {
    9404             : //i59651
    9405           0 :         if( m_bIsPDF_A1 )
    9406             :         {
    9407           0 :             aLine.append( "/CA 1.0/ca 1.0" );
    9408           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9409             :         }
    9410             :         else
    9411             :         {
    9412           0 :             aLine.append(  "/CA " );
    9413           0 :             appendDouble( rObject.m_fAlpha, aLine );
    9414             :             aLine.append( "\n"
    9415           0 :                           "   /ca " );
    9416           0 :             appendDouble( rObject.m_fAlpha, aLine );
    9417             :         }
    9418           0 :         aLine.append( "\n" );
    9419             :     }
    9420             :     else
    9421             :     {
    9422           0 :         if( m_bIsPDF_A1 )
    9423             :         {
    9424           0 :             aLine.append( "/SMask/None" );
    9425           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9426             :         }
    9427             :         else
    9428             :         {
    9429           0 :             rObject.m_pSoftMaskStream->Seek( STREAM_SEEK_TO_END );
    9430           0 :             sal_Int32 nMaskSize = (sal_Int32)rObject.m_pSoftMaskStream->Tell();
    9431           0 :             rObject.m_pSoftMaskStream->Seek( STREAM_SEEK_TO_BEGIN );
    9432           0 :             sal_Int32 nMaskObject = createObject();
    9433           0 :             aLine.append( "/SMask<</Type/Mask/S/Luminosity/G " );
    9434           0 :             aLine.append( nMaskObject );
    9435           0 :             aLine.append( " 0 R>>\n" );
    9436             : 
    9437           0 :             OStringBuffer aMask;
    9438           0 :             aMask.append( nMaskObject );
    9439             :             aMask.append( " 0 obj\n"
    9440             :                           "<</Type/XObject\n"
    9441             :                           "/Subtype/Form\n"
    9442           0 :                           "/BBox[" );
    9443           0 :             appendFixedInt( rObject.m_aBoundRect.Left(), aMask );
    9444           0 :             aMask.append( ' ' );
    9445           0 :             appendFixedInt( rObject.m_aBoundRect.Top(), aMask );
    9446           0 :             aMask.append( ' ' );
    9447           0 :             appendFixedInt( rObject.m_aBoundRect.Right(), aMask );
    9448           0 :             aMask.append( ' ' );
    9449           0 :             appendFixedInt( rObject.m_aBoundRect.Bottom()+1, aMask );
    9450           0 :             aMask.append( "]\n" );
    9451             : 
    9452             :             /* #i42884# see above */
    9453           0 :             aMask.append( "/Group<</S/Transparency/CS/DeviceRGB>>\n" );
    9454           0 :             aMask.append( "/Length " );
    9455           0 :             aMask.append( nMaskSize );
    9456             :             aMask.append( ">>\n"
    9457           0 :                           "stream\n" );
    9458           0 :             CHECK_RETURN( updateObject( nMaskObject ) );
    9459           0 :             checkAndEnableStreamEncryption(  nMaskObject );
    9460           0 :             CHECK_RETURN( writeBuffer( aMask.getStr(), aMask.getLength() ) );
    9461           0 :             CHECK_RETURN( writeBuffer( rObject.m_pSoftMaskStream->GetData(), nMaskSize ) );
    9462           0 :             disableStreamEncryption();
    9463           0 :             aMask.setLength( 0 );
    9464             :             aMask.append( "\nendstream\n"
    9465           0 :                           "endobj\n\n" );
    9466           0 :             CHECK_RETURN( writeBuffer( aMask.getStr(), aMask.getLength() ) );
    9467             :         }
    9468             :     }
    9469             :     aLine.append( ">>\n"
    9470           0 :                   "endobj\n\n" );
    9471           0 :     CHECK_RETURN( updateObject( rObject.m_nExtGStateObject ) );
    9472           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9473             : 
    9474           0 :     return true;
    9475             : }
    9476             : 
    9477           0 : bool PDFWriterImpl::writeGradientFunction( GradientEmit& rObject )
    9478             : {
    9479             :     // LO internal gradient -> PDF shading type:
    9480             :     //  * GradientStyle_LINEAR: axial shading, using sampled-function with 2 samples
    9481             :     //                          [t=0:colorStart, t=1:colorEnd]
    9482             :     //  * GradientStyle_AXIAL: axial shading, using sampled-function with 3 samples
    9483             :     //                          [t=0:colorEnd, t=0.5:colorStart, t=1:colorEnd]
    9484             :     //  * other styles: function shading with aSize.Width() * aSize.Height() samples
    9485           0 :     sal_Int32 nFunctionObject = createObject();
    9486           0 :     CHECK_RETURN( updateObject( nFunctionObject ) );
    9487             : 
    9488           0 :     VirtualDevice aDev;
    9489           0 :     aDev.SetOutputSizePixel( rObject.m_aSize );
    9490           0 :     aDev.SetMapMode( MapMode( MAP_PIXEL ) );
    9491           0 :     if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
    9492           0 :         aDev.SetDrawMode( aDev.GetDrawMode() |
    9493             :                           ( DRAWMODE_GRAYLINE | DRAWMODE_GRAYFILL | DRAWMODE_GRAYTEXT |
    9494           0 :                             DRAWMODE_GRAYBITMAP | DRAWMODE_GRAYGRADIENT ) );
    9495           0 :     aDev.DrawGradient( Rectangle( Point( 0, 0 ), rObject.m_aSize ), rObject.m_aGradient );
    9496             : 
    9497           0 :     Bitmap aSample = aDev.GetBitmap( Point( 0, 0 ), rObject.m_aSize );
    9498           0 :     BitmapReadAccess* pAccess = aSample.AcquireReadAccess();
    9499           0 :     AccessReleaser aReleaser( pAccess );
    9500             : 
    9501           0 :     Size aSize = aSample.GetSizePixel();
    9502             : 
    9503           0 :     sal_Int32 nStreamLengthObject = createObject();
    9504             :     #if OSL_DEBUG_LEVEL > 1
    9505             :     emitComment( "PDFWriterImpl::writeGradientFunction" );
    9506             :     #endif
    9507           0 :     OStringBuffer aLine( 120 );
    9508           0 :     aLine.append( nFunctionObject );
    9509             :     aLine.append( " 0 obj\n"
    9510           0 :                   "<</FunctionType 0\n");
    9511           0 :     switch (rObject.m_aGradient.GetStyle())
    9512             :     {
    9513             :         case GradientStyle_LINEAR:
    9514             :         case GradientStyle_AXIAL:
    9515           0 :             aLine.append("/Domain[ 0 1]\n");
    9516           0 :             break;
    9517             :         default:
    9518           0 :             aLine.append("/Domain[ 0 1 0 1]\n");
    9519             :     }
    9520           0 :     aLine.append("/Size[ " );
    9521           0 :     switch (rObject.m_aGradient.GetStyle())
    9522             :     {
    9523             :         case GradientStyle_LINEAR:
    9524           0 :             aLine.append('2');
    9525           0 :             break;
    9526             :         case GradientStyle_AXIAL:
    9527           0 :             aLine.append('3');
    9528           0 :             break;
    9529             :         default:
    9530           0 :             aLine.append( (sal_Int32)aSize.Width() );
    9531           0 :             aLine.append( ' ' );
    9532           0 :             aLine.append( (sal_Int32)aSize.Height() );
    9533             :     }
    9534             :     aLine.append( " ]\n"
    9535             :                   "/BitsPerSample 8\n"
    9536             :                   "/Range[ 0 1 0 1 0 1 ]\n"
    9537             :                   "/Order 3\n"
    9538           0 :                   "/Length " );
    9539           0 :     aLine.append( nStreamLengthObject );
    9540             :     aLine.append( " 0 R\n"
    9541             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9542             :                   "/Filter/FlateDecode"
    9543             : #endif
    9544             :                   ">>\n"
    9545           0 :                   "stream\n" );
    9546           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9547             : 
    9548           0 :     sal_uInt64 nStartStreamPos = 0;
    9549           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nStartStreamPos)) );
    9550             : 
    9551           0 :     checkAndEnableStreamEncryption( nFunctionObject );
    9552           0 :     beginCompression();
    9553             :     sal_uInt8 aCol[3];
    9554           0 :     switch (rObject.m_aGradient.GetStyle())
    9555             :     {
    9556             :         case GradientStyle_AXIAL:
    9557           0 :             aCol[0] = rObject.m_aGradient.GetEndColor().GetRed();
    9558           0 :             aCol[1] = rObject.m_aGradient.GetEndColor().GetGreen();
    9559           0 :             aCol[2] = rObject.m_aGradient.GetEndColor().GetBlue();
    9560           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9561             :         case GradientStyle_LINEAR:
    9562             :         {
    9563           0 :             aCol[0] = rObject.m_aGradient.GetStartColor().GetRed();
    9564           0 :             aCol[1] = rObject.m_aGradient.GetStartColor().GetGreen();
    9565           0 :             aCol[2] = rObject.m_aGradient.GetStartColor().GetBlue();
    9566           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9567             : 
    9568           0 :             aCol[0] = rObject.m_aGradient.GetEndColor().GetRed();
    9569           0 :             aCol[1] = rObject.m_aGradient.GetEndColor().GetGreen();
    9570           0 :             aCol[2] = rObject.m_aGradient.GetEndColor().GetBlue();
    9571           0 :             CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9572           0 :             break;
    9573             :         }
    9574             :         default:
    9575           0 :             for( int y = aSize.Height()-1; y >= 0; y-- )
    9576             :             {
    9577           0 :                 for( int x = 0; x < aSize.Width(); x++ )
    9578             :                 {
    9579           0 :                     BitmapColor aColor = pAccess->GetColor( y, x );
    9580           0 :                     aCol[0] = aColor.GetRed();
    9581           0 :                     aCol[1] = aColor.GetGreen();
    9582           0 :                     aCol[2] = aColor.GetBlue();
    9583           0 :                     CHECK_RETURN( writeBuffer( aCol, 3 ) );
    9584           0 :                 }
    9585             :             }
    9586             :     }
    9587           0 :     endCompression();
    9588           0 :     disableStreamEncryption();
    9589             : 
    9590           0 :     sal_uInt64 nEndStreamPos = 0;
    9591           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nEndStreamPos)) );
    9592             : 
    9593           0 :     aLine.setLength( 0 );
    9594           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9595           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9596             : 
    9597             :     // write stream length
    9598           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
    9599           0 :     aLine.setLength( 0 );
    9600           0 :     aLine.append( nStreamLengthObject );
    9601           0 :     aLine.append( " 0 obj\n" );
    9602           0 :     aLine.append( (sal_Int64)(nEndStreamPos-nStartStreamPos) );
    9603           0 :     aLine.append( "\nendobj\n\n" );
    9604           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9605             : 
    9606           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9607           0 :     aLine.setLength( 0 );
    9608           0 :     aLine.append( rObject.m_nObject );
    9609           0 :     aLine.append( " 0 obj\n");
    9610           0 :     switch (rObject.m_aGradient.GetStyle())
    9611             :     {
    9612             :         case GradientStyle_LINEAR:
    9613             :         case GradientStyle_AXIAL:
    9614           0 :             aLine.append("<</ShadingType 2\n");
    9615           0 :             break;
    9616             :         default:
    9617           0 :             aLine.append("<</ShadingType 1\n");
    9618             :     }
    9619             :     aLine.append("/ColorSpace/DeviceRGB\n"
    9620           0 :                   "/AntiAlias true\n");
    9621             : 
    9622             :     // Determination of shading axis
    9623             :     // See: OutputDevice::ImplDrawLinearGradient for reference
    9624           0 :     Rectangle aRect;
    9625           0 :     aRect.Left() = aRect.Top() = 0;
    9626           0 :     aRect.Right() = aSize.Width();
    9627           0 :     aRect.Bottom() = aSize.Height();
    9628             : 
    9629           0 :     Rectangle aBoundRect;
    9630           0 :     Point     aCenter;
    9631           0 :     sal_uInt16    nAngle = rObject.m_aGradient.GetAngle() % 3600;
    9632           0 :     rObject.m_aGradient.GetBoundRect( aRect, aBoundRect, aCenter );
    9633             : 
    9634           0 :     const bool bLinear = (rObject.m_aGradient.GetStyle() == GradientStyle_LINEAR);
    9635           0 :     double fBorder = aBoundRect.GetHeight() * rObject.m_aGradient.GetBorder() / 100.0;
    9636           0 :     if ( !bLinear )
    9637             :     {
    9638           0 :         fBorder /= 2.0;
    9639             :     }
    9640             : 
    9641           0 :     aBoundRect.Bottom() -= fBorder;
    9642           0 :     if (!bLinear)
    9643             :     {
    9644           0 :         aBoundRect.Top() += fBorder;
    9645             :     }
    9646             : 
    9647           0 :     switch (rObject.m_aGradient.GetStyle())
    9648             :     {
    9649             :         case GradientStyle_LINEAR:
    9650             :         case GradientStyle_AXIAL:
    9651             :         {
    9652             :             aLine.append("/Domain[ 0 1 ]\n"
    9653           0 :                     "/Coords[ " );
    9654           0 :             Polygon     aPoly( 2 );
    9655           0 :             aPoly[0] = aBoundRect.BottomCenter();
    9656           0 :             aPoly[1] = aBoundRect.TopCenter();
    9657           0 :             aPoly.Rotate( aCenter, 3600 - nAngle );
    9658             : 
    9659           0 :             aLine.append( (sal_Int32) aPoly[0].X() );
    9660           0 :             aLine.append( " " );
    9661           0 :             aLine.append( (sal_Int32) aPoly[0].Y() );
    9662           0 :             aLine.append( " " );
    9663           0 :             aLine.append( (sal_Int32) aPoly[1].X());
    9664           0 :             aLine.append( " ");
    9665           0 :             aLine.append( (sal_Int32) aPoly[1].Y());
    9666           0 :             aLine.append( " ]\n");
    9667           0 :             aLine.append("/Extend [true true]\n");
    9668           0 :             break;
    9669             :         }
    9670             :         default:
    9671             :             aLine.append("/Domain[ 0 1 0 1 ]\n"
    9672           0 :                     "/Matrix[ " );
    9673           0 :             aLine.append( (sal_Int32)aSize.Width() );
    9674           0 :             aLine.append( " 0 0 " );
    9675           0 :             aLine.append( (sal_Int32)aSize.Height() );
    9676           0 :             aLine.append( " 0 0 ]\n");
    9677             :     }
    9678           0 :     aLine.append("/Function " );
    9679           0 :     aLine.append( nFunctionObject );
    9680             :     aLine.append( " 0 R\n"
    9681             :                   ">>\n"
    9682           0 :                   "endobj\n\n" );
    9683           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9684             : 
    9685           0 :     return true;
    9686             : }
    9687             : 
    9688           0 : bool PDFWriterImpl::writeJPG( JPGEmit& rObject )
    9689             : {
    9690           0 :     CHECK_RETURN( rObject.m_pStream );
    9691           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9692             : 
    9693           0 :     sal_Int32 nLength = 0;
    9694           0 :     rObject.m_pStream->Seek( STREAM_SEEK_TO_END );
    9695           0 :     nLength = rObject.m_pStream->Tell();
    9696           0 :     rObject.m_pStream->Seek( STREAM_SEEK_TO_BEGIN );
    9697             : 
    9698           0 :     sal_Int32 nMaskObject = 0;
    9699           0 :     if( !!rObject.m_aMask )
    9700             :     {
    9701           0 :         if( rObject.m_aMask.GetBitCount() == 1 ||
    9702           0 :             ( rObject.m_aMask.GetBitCount() == 8 && m_aContext.Version >= PDFWriter::PDF_1_4 && !m_bIsPDF_A1 )//i59651
    9703             :             )
    9704             :         {
    9705           0 :             nMaskObject = createObject();
    9706             :         }
    9707           0 :         else if( m_bIsPDF_A1 )
    9708           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9709           0 :         else if( m_aContext.Version < PDFWriter::PDF_1_4 )
    9710           0 :             m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDF13 );
    9711             : 
    9712             :     }
    9713             :     #if OSL_DEBUG_LEVEL > 1
    9714             :     emitComment( "PDFWriterImpl::writeJPG" );
    9715             :     #endif
    9716             : 
    9717           0 :     OStringBuffer aLine(200);
    9718           0 :     aLine.append( rObject.m_nObject );
    9719             :     aLine.append( " 0 obj\n"
    9720           0 :                   "<</Type/XObject/Subtype/Image/Width " );
    9721           0 :     aLine.append( (sal_Int32)rObject.m_aID.m_aPixelSize.Width() );
    9722           0 :     aLine.append( " /Height " );
    9723           0 :     aLine.append( (sal_Int32)rObject.m_aID.m_aPixelSize.Height() );
    9724           0 :     aLine.append( " /BitsPerComponent 8 " );
    9725           0 :     if( rObject.m_bTrueColor )
    9726           0 :         aLine.append( "/ColorSpace/DeviceRGB" );
    9727             :     else
    9728           0 :         aLine.append( "/ColorSpace/DeviceGray" );
    9729           0 :     aLine.append( "/Filter/DCTDecode/Length " );
    9730           0 :     aLine.append( nLength );
    9731           0 :     if( nMaskObject )
    9732             :     {
    9733           0 :         aLine.append( rObject.m_aMask.GetBitCount() == 1 ? " /Mask " : " /SMask " );
    9734           0 :         aLine.append( nMaskObject );
    9735           0 :         aLine.append( " 0 R " );
    9736             :     }
    9737           0 :     aLine.append( ">>\nstream\n" );
    9738           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9739             : 
    9740           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9741           0 :     CHECK_RETURN( writeBuffer( rObject.m_pStream->GetData(), nLength ) );
    9742           0 :     disableStreamEncryption();
    9743             : 
    9744           0 :     aLine.setLength( 0 );
    9745           0 :     aLine.append( "\nendstream\nendobj\n\n" );
    9746           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9747             : 
    9748           0 :     if( nMaskObject )
    9749             :     {
    9750           0 :         BitmapEmit aEmit;
    9751           0 :         aEmit.m_nObject = nMaskObject;
    9752           0 :         if( rObject.m_aMask.GetBitCount() == 1 )
    9753           0 :             aEmit.m_aBitmap = BitmapEx( rObject.m_aMask, rObject.m_aMask );
    9754           0 :         else if( rObject.m_aMask.GetBitCount() == 8 )
    9755           0 :             aEmit.m_aBitmap = BitmapEx( rObject.m_aMask, AlphaMask( rObject.m_aMask ) );
    9756           0 :         writeBitmapObject( aEmit, true );
    9757             :     }
    9758             : 
    9759           0 :     return true;
    9760             : }
    9761             : 
    9762           0 : bool PDFWriterImpl::writeBitmapObject( BitmapEmit& rObject, bool bMask )
    9763             : {
    9764           0 :     CHECK_RETURN( updateObject( rObject.m_nObject ) );
    9765             : 
    9766           0 :     Bitmap  aBitmap;
    9767           0 :     Color   aTransparentColor( COL_TRANSPARENT );
    9768           0 :     bool    bWriteMask = false;
    9769           0 :     if( ! bMask )
    9770             :     {
    9771           0 :         aBitmap = rObject.m_aBitmap.GetBitmap();
    9772           0 :         if( rObject.m_aBitmap.IsAlpha() )
    9773             :         {
    9774           0 :             if( m_aContext.Version >= PDFWriter::PDF_1_4 )
    9775           0 :                 bWriteMask = true;
    9776             :             // else draw without alpha channel
    9777             :         }
    9778             :         else
    9779             :         {
    9780           0 :             switch( rObject.m_aBitmap.GetTransparentType() )
    9781             :             {
    9782             :                 case TRANSPARENT_NONE:
    9783             :                     // comes from drawMask function
    9784           0 :                     if( aBitmap.GetBitCount() == 1 && rObject.m_bDrawMask )
    9785           0 :                         bMask = true;
    9786           0 :                     break;
    9787             :                 case TRANSPARENT_COLOR:
    9788           0 :                     aTransparentColor = rObject.m_aBitmap.GetTransparentColor();
    9789           0 :                     break;
    9790             :                 case TRANSPARENT_BITMAP:
    9791           0 :                     bWriteMask = true;
    9792           0 :                     break;
    9793             :             }
    9794             :         }
    9795             :     }
    9796             :     else
    9797             :     {
    9798           0 :         if( m_aContext.Version < PDFWriter::PDF_1_4 || ! rObject.m_aBitmap.IsAlpha() )
    9799             :         {
    9800           0 :             aBitmap = rObject.m_aBitmap.GetMask();
    9801           0 :             aBitmap.Convert( BMP_CONVERSION_1BIT_THRESHOLD );
    9802             :             DBG_ASSERT( aBitmap.GetBitCount() == 1, "mask conversion failed" );
    9803             :         }
    9804           0 :         else if( aBitmap.GetBitCount() != 8 )
    9805             :         {
    9806           0 :             aBitmap = rObject.m_aBitmap.GetAlpha().GetBitmap();
    9807           0 :             aBitmap.Convert( BMP_CONVERSION_8BIT_GREYS );
    9808             :             DBG_ASSERT( aBitmap.GetBitCount() == 8, "alpha mask conversion failed" );
    9809             :         }
    9810             :     }
    9811             : 
    9812           0 :     BitmapReadAccess* pAccess = aBitmap.AcquireReadAccess();
    9813           0 :     AccessReleaser aReleaser( pAccess );
    9814             : 
    9815             :     bool bTrueColor;
    9816             :     sal_Int32 nBitsPerComponent;
    9817           0 :     switch( aBitmap.GetBitCount() )
    9818             :     {
    9819             :         case 1:
    9820             :         case 2:
    9821             :         case 4:
    9822             :         case 8:
    9823           0 :             bTrueColor = false;
    9824           0 :             nBitsPerComponent = aBitmap.GetBitCount();
    9825           0 :             break;
    9826             :         default:
    9827           0 :             bTrueColor = true;
    9828           0 :             nBitsPerComponent = 8;
    9829           0 :             break;
    9830             :     }
    9831             : 
    9832           0 :     sal_Int32 nStreamLengthObject   = createObject();
    9833           0 :     sal_Int32 nMaskObject           = 0;
    9834             : 
    9835             :     #if OSL_DEBUG_LEVEL > 1
    9836             :     emitComment( "PDFWriterImpl::writeBitmapObject" );
    9837             :     #endif
    9838           0 :     OStringBuffer aLine(1024);
    9839           0 :     aLine.append( rObject.m_nObject );
    9840             :     aLine.append( " 0 obj\n"
    9841           0 :                   "<</Type/XObject/Subtype/Image/Width " );
    9842           0 :     aLine.append( (sal_Int32)aBitmap.GetSizePixel().Width() );
    9843           0 :     aLine.append( "/Height " );
    9844           0 :     aLine.append( (sal_Int32)aBitmap.GetSizePixel().Height() );
    9845           0 :     aLine.append( "/BitsPerComponent " );
    9846           0 :     aLine.append( nBitsPerComponent );
    9847           0 :     aLine.append( "/Length " );
    9848           0 :     aLine.append( nStreamLengthObject );
    9849           0 :     aLine.append( " 0 R\n" );
    9850             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9851           0 :     if( nBitsPerComponent != 1 )
    9852             :     {
    9853           0 :         aLine.append( "/Filter/FlateDecode" );
    9854             :     }
    9855             :     else
    9856             :     {
    9857           0 :         aLine.append( "/Filter/CCITTFaxDecode/DecodeParms<</K -1/BlackIs1 true/Columns " );
    9858           0 :         aLine.append( (sal_Int32)aBitmap.GetSizePixel().Width() );
    9859           0 :         aLine.append( ">>\n" );
    9860             :     }
    9861             : #endif
    9862           0 :     if( ! bMask )
    9863             :     {
    9864           0 :         aLine.append( "/ColorSpace" );
    9865           0 :         if( bTrueColor )
    9866           0 :             aLine.append( "/DeviceRGB\n" );
    9867           0 :         else if( aBitmap.HasGreyPalette() )
    9868             :         {
    9869           0 :             aLine.append( "/DeviceGray\n" );
    9870           0 :             if( aBitmap.GetBitCount() == 1 )
    9871             :             {
    9872             :                 // #i47395# 1 bit bitmaps occasionally have an inverted grey palette
    9873           0 :                 sal_Int32 nBlackIndex = pAccess->GetBestPaletteIndex( BitmapColor( Color( COL_BLACK ) ) );
    9874             :                 DBG_ASSERT( nBlackIndex == 0 || nBlackIndex == 1, "wrong black index" );
    9875           0 :                 if( nBlackIndex == 1 )
    9876           0 :                     aLine.append( "/Decode[1 0]\n" );
    9877             :             }
    9878             :         }
    9879             :         else
    9880             :         {
    9881           0 :             aLine.append( "[ /Indexed/DeviceRGB " );
    9882           0 :             aLine.append( (sal_Int32)(pAccess->GetPaletteEntryCount()-1) );
    9883           0 :             aLine.append( "\n<" );
    9884           0 :             if( m_aContext.Encryption.Encrypt() )
    9885             :             {
    9886           0 :                 enableStringEncryption( rObject.m_nObject );
    9887             :                 //check encryption buffer size
    9888           0 :                 if( checkEncryptionBufferSize( pAccess->GetPaletteEntryCount()*3 ) )
    9889             :                 {
    9890           0 :                     int nChar = 0;
    9891             :                     //fill the encryption buffer
    9892           0 :                     for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9893             :                     {
    9894           0 :                         const BitmapColor& rColor = pAccess->GetPaletteColor( i );
    9895           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetRed();
    9896           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetGreen();
    9897           0 :                         m_pEncryptionBuffer[nChar++] = rColor.GetBlue();
    9898             :                     }
    9899             :                     //encrypt the colorspace lookup table
    9900           0 :                     rtl_cipher_encodeARCFOUR( m_aCipher, m_pEncryptionBuffer, nChar, m_pEncryptionBuffer, nChar );
    9901             :                     //now queue the data for output
    9902           0 :                     nChar = 0;
    9903           0 :                     for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9904             :                     {
    9905           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9906           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9907           0 :                         appendHex(m_pEncryptionBuffer[nChar++], aLine );
    9908             :                     }
    9909             :                 }
    9910             :             }
    9911             :             else //no encryption requested (PDF/A-1a program flow drops here)
    9912             :             {
    9913           0 :                 for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); i++ )
    9914             :                 {
    9915           0 :                     const BitmapColor& rColor = pAccess->GetPaletteColor( i );
    9916           0 :                     appendHex( rColor.GetRed(), aLine );
    9917           0 :                     appendHex( rColor.GetGreen(), aLine );
    9918           0 :                     appendHex( rColor.GetBlue(), aLine );
    9919             :                 }
    9920             :             }
    9921           0 :             aLine.append( ">\n]\n" );
    9922             :         }
    9923             :     }
    9924             :     else
    9925             :     {
    9926           0 :         if( aBitmap.GetBitCount() == 1 )
    9927             :         {
    9928           0 :             aLine.append( "/ImageMask true\n" );
    9929           0 :             sal_Int32 nBlackIndex = pAccess->GetBestPaletteIndex( BitmapColor( Color( COL_BLACK ) ) );
    9930             :             DBG_ASSERT( nBlackIndex == 0 || nBlackIndex == 1, "wrong black index" );
    9931           0 :             if( nBlackIndex )
    9932           0 :                 aLine.append( "/Decode[ 1 0 ]\n" );
    9933             :             else
    9934           0 :                 aLine.append( "/Decode[ 0 1 ]\n" );
    9935             :         }
    9936           0 :         else if( aBitmap.GetBitCount() == 8 )
    9937             :         {
    9938             :             aLine.append( "/ColorSpace/DeviceGray\n"
    9939           0 :                           "/Decode [ 1 0 ]\n" );
    9940             :         }
    9941             :     }
    9942             : 
    9943           0 :     if( ! bMask && m_aContext.Version > PDFWriter::PDF_1_2 && !m_bIsPDF_A1 )//i59651
    9944             :     {
    9945           0 :         if( bWriteMask )
    9946             :         {
    9947           0 :             nMaskObject = createObject();
    9948           0 :             if( rObject.m_aBitmap.IsAlpha() && m_aContext.Version > PDFWriter::PDF_1_3 )
    9949           0 :                 aLine.append( "/SMask " );
    9950             :             else
    9951           0 :                 aLine.append( "/Mask " );
    9952           0 :             aLine.append( nMaskObject );
    9953           0 :             aLine.append( " 0 R\n" );
    9954             :         }
    9955           0 :         else if( aTransparentColor != Color( COL_TRANSPARENT ) )
    9956             :         {
    9957           0 :             aLine.append( "/Mask[ " );
    9958           0 :             if( bTrueColor )
    9959             :             {
    9960           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetRed() );
    9961           0 :                 aLine.append( ' ' );
    9962           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetRed() );
    9963           0 :                 aLine.append( ' ' );
    9964           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetGreen() );
    9965           0 :                 aLine.append( ' ' );
    9966           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetGreen() );
    9967           0 :                 aLine.append( ' ' );
    9968           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetBlue() );
    9969           0 :                 aLine.append( ' ' );
    9970           0 :                 aLine.append( (sal_Int32)aTransparentColor.GetBlue() );
    9971             :             }
    9972             :             else
    9973             :             {
    9974           0 :                 sal_Int32 nIndex = pAccess->GetBestPaletteIndex( BitmapColor( aTransparentColor ) );
    9975           0 :                 aLine.append( nIndex );
    9976             :             }
    9977           0 :             aLine.append( " ]\n" );
    9978           0 :         }
    9979             :     }
    9980           0 :     else if( m_bIsPDF_A1 && (bWriteMask || aTransparentColor != Color( COL_TRANSPARENT )) )
    9981           0 :         m_aErrors.insert( PDFWriter::Warning_Transparency_Omitted_PDFA );
    9982             : 
    9983             :     aLine.append( ">>\n"
    9984           0 :                   "stream\n" );
    9985           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
    9986           0 :     sal_uInt64 nStartPos = 0;
    9987           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nStartPos)) );
    9988             : 
    9989           0 :     checkAndEnableStreamEncryption( rObject.m_nObject );
    9990             : #ifndef DEBUG_DISABLE_PDFCOMPRESSION
    9991           0 :     if( nBitsPerComponent == 1 )
    9992             :     {
    9993           0 :         writeG4Stream( pAccess );
    9994             :     }
    9995             :     else
    9996             : #endif
    9997             :     {
    9998           0 :         beginCompression();
    9999           0 :         if( ! bTrueColor || pAccess->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB )
   10000             :         {
   10001           0 :             const int nScanLineBytes = 1 + ( pAccess->GetBitCount() * ( pAccess->Width() - 1 ) / 8U );
   10002             : 
   10003           0 :             for( int i = 0; i < pAccess->Height(); i++ )
   10004             :             {
   10005           0 :                 CHECK_RETURN( writeBuffer( pAccess->GetScanline( i ), nScanLineBytes ) );
   10006             :             }
   10007             :         }
   10008             :         else
   10009             :         {
   10010           0 :             const int nScanLineBytes = pAccess->Width()*3;
   10011           0 :             boost::shared_array<sal_uInt8> pCol( new sal_uInt8[ nScanLineBytes ] );
   10012           0 :             for( int y = 0; y < pAccess->Height(); y++ )
   10013             :             {
   10014           0 :                 for( int x = 0; x < pAccess->Width(); x++ )
   10015             :                 {
   10016           0 :                     BitmapColor aColor = pAccess->GetColor( y, x );
   10017           0 :                     pCol[3*x+0] = aColor.GetRed();
   10018           0 :                     pCol[3*x+1] = aColor.GetGreen();
   10019           0 :                     pCol[3*x+2] = aColor.GetBlue();
   10020           0 :                 }
   10021           0 :                 CHECK_RETURN( writeBuffer( pCol.get(), nScanLineBytes ) );
   10022           0 :             }
   10023             :         }
   10024           0 :         endCompression();
   10025             :     }
   10026           0 :     disableStreamEncryption();
   10027             : 
   10028           0 :     sal_uInt64 nEndPos = 0;
   10029           0 :     CHECK_RETURN( (osl::File::E_None == m_aFile.getPos(nEndPos)) );
   10030           0 :     aLine.setLength( 0 );
   10031           0 :     aLine.append( "\nendstream\nendobj\n\n" );
   10032           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
   10033           0 :     CHECK_RETURN( updateObject( nStreamLengthObject ) );
   10034           0 :     aLine.setLength( 0 );
   10035           0 :     aLine.append( nStreamLengthObject );
   10036           0 :     aLine.append( " 0 obj\n" );
   10037           0 :     aLine.append( (sal_Int64)(nEndPos-nStartPos) );
   10038           0 :     aLine.append( "\nendobj\n\n" );
   10039           0 :     CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) );
   10040             : 
   10041           0 :     if( nMaskObject )
   10042             :     {
   10043           0 :         BitmapEmit aEmit;
   10044           0 :         aEmit.m_nObject             = nMaskObject;
   10045           0 :         aEmit.m_aBitmap             = rObject.m_aBitmap;
   10046           0 :         return writeBitmapObject( aEmit, true );
   10047             :     }
   10048             : 
   10049           0 :     return true;
   10050             : }
   10051             : 
   10052           0 : void PDFWriterImpl::drawJPGBitmap( SvStream& rDCTData, bool bIsTrueColor, const Size& rSizePixel, const Rectangle& rTargetArea, const Bitmap& rMask )
   10053             : {
   10054           0 :     MARK( "drawJPGBitmap" );
   10055             : 
   10056           0 :     OStringBuffer aLine( 80 );
   10057           0 :     updateGraphicsState();
   10058             : 
   10059             :     // #i40055# sanity check
   10060           0 :     if( ! (rTargetArea.GetWidth() && rTargetArea.GetHeight() ) )
   10061           0 :         return;
   10062           0 :     if( ! (rSizePixel.Width() && rSizePixel.Height()) )
   10063           0 :         return;
   10064             : 
   10065           0 :     rDCTData.Seek( 0 );
   10066           0 :     if( bIsTrueColor && m_aContext.ColorMode == PDFWriter::DrawGreyscale )
   10067             :     {
   10068             :         // need to convert to grayscale;
   10069             :         // load stream to bitmap and draw the bitmap instead
   10070           0 :         Graphic aGraphic;
   10071           0 :         GraphicConverter::Import( rDCTData, aGraphic, CVT_JPG );
   10072           0 :         Bitmap aBmp( aGraphic.GetBitmap() );
   10073           0 :         if( !!rMask && rMask.GetSizePixel() == aBmp.GetSizePixel() )
   10074             :         {
   10075           0 :             BitmapEx aBmpEx( aBmp, rMask );
   10076           0 :             drawBitmap( rTargetArea.TopLeft(), rTargetArea.GetSize(), aBmpEx );
   10077             :         }
   10078             :         else
   10079           0 :             drawBitmap( rTargetArea.TopLeft(), rTargetArea.GetSize(), aBmp );
   10080           0 :         return;
   10081             :     }
   10082             : 
   10083           0 :     SvMemoryStream* pStream = new SvMemoryStream;
   10084           0 :     pStream->WriteStream( rDCTData );
   10085           0 :     pStream->Seek( STREAM_SEEK_TO_END );
   10086             : 
   10087           0 :     BitmapID aID;
   10088           0 :     aID.m_aPixelSize    = rSizePixel;
   10089           0 :     aID.m_nSize         = pStream->Tell();
   10090           0 :     pStream->Seek( STREAM_SEEK_TO_BEGIN );
   10091           0 :     aID.m_nChecksum     = rtl_crc32( 0, pStream->GetData(), aID.m_nSize );
   10092           0 :     if( ! rMask.IsEmpty() )
   10093           0 :         aID.m_nMaskChecksum = rMask.GetChecksum();
   10094             : 
   10095           0 :     std::list< JPGEmit >::const_iterator it;
   10096           0 :     for( it = m_aJPGs.begin(); it != m_aJPGs.end() && ! (aID == it->m_aID); ++it )
   10097             :         ;
   10098           0 :     if( it == m_aJPGs.end() )
   10099             :     {
   10100           0 :         m_aJPGs.push_front( JPGEmit() );
   10101           0 :         JPGEmit& rEmit = m_aJPGs.front();
   10102           0 :         rEmit.m_nObject     = createObject();
   10103           0 :         rEmit.m_aID         = aID;
   10104           0 :         rEmit.m_pStream     = pStream;
   10105           0 :         rEmit.m_bTrueColor  = bIsTrueColor;
   10106           0 :         if( !! rMask && rMask.GetSizePixel() == rSizePixel )
   10107           0 :             rEmit.m_aMask   = rMask;
   10108             : 
   10109           0 :         it = m_aJPGs.begin();
   10110             :     }
   10111             :     else
   10112           0 :         delete pStream;
   10113             : 
   10114           0 :     aLine.append( "q " );
   10115           0 :     sal_Int32 nCheckWidth = 0;
   10116           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rTargetArea.GetWidth(), aLine, false, &nCheckWidth );
   10117           0 :     aLine.append( " 0 0 " );
   10118           0 :     sal_Int32 nCheckHeight = 0;
   10119           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rTargetArea.GetHeight(), aLine, true, &nCheckHeight );
   10120           0 :     aLine.append( ' ' );
   10121           0 :     m_aPages.back().appendPoint( rTargetArea.BottomLeft(), aLine );
   10122           0 :     aLine.append( " cm\n/Im" );
   10123           0 :     aLine.append( it->m_nObject );
   10124           0 :     aLine.append( " Do Q\n" );
   10125           0 :     if( nCheckWidth == 0 || nCheckHeight == 0 )
   10126             :     {
   10127             :         // #i97512# avoid invalid current matrix
   10128           0 :         aLine.setLength( 0 );
   10129           0 :         aLine.append( "\n%jpeg image /Im" );
   10130           0 :         aLine.append( it->m_nObject );
   10131           0 :         aLine.append( " scaled to zero size, omitted\n" );
   10132             :     }
   10133           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10134             : 
   10135           0 :     OStringBuffer aObjName( 16 );
   10136           0 :     aObjName.append( "Im" );
   10137           0 :     aObjName.append( it->m_nObject );
   10138           0 :     pushResource( ResXObject, aObjName.makeStringAndClear(), it->m_nObject );
   10139             : 
   10140             : }
   10141             : 
   10142           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const BitmapEmit& rBitmap, const Color& rFillColor )
   10143             : {
   10144           0 :     OStringBuffer aLine( 80 );
   10145           0 :     updateGraphicsState();
   10146             : 
   10147           0 :     aLine.append( "q " );
   10148           0 :     if( rFillColor != Color( COL_TRANSPARENT ) )
   10149             :     {
   10150           0 :         appendNonStrokingColor( rFillColor, aLine );
   10151           0 :         aLine.append( ' ' );
   10152             :     }
   10153           0 :     sal_Int32 nCheckWidth = 0;
   10154           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rDestSize.Width(), aLine, false, &nCheckWidth );
   10155           0 :     aLine.append( " 0 0 " );
   10156           0 :     sal_Int32 nCheckHeight = 0;
   10157           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rDestSize.Height(), aLine, true, &nCheckHeight );
   10158           0 :     aLine.append( ' ' );
   10159           0 :     m_aPages.back().appendPoint( rDestPoint + Point( 0, rDestSize.Height()-1 ), aLine );
   10160           0 :     aLine.append( " cm\n/Im" );
   10161           0 :     aLine.append( rBitmap.m_nObject );
   10162           0 :     aLine.append( " Do Q\n" );
   10163           0 :     if( nCheckWidth == 0 || nCheckHeight == 0 )
   10164             :     {
   10165             :         // #i97512# avoid invalid current matrix
   10166           0 :         aLine.setLength( 0 );
   10167           0 :         aLine.append( "\n%bitmap image /Im" );
   10168           0 :         aLine.append( rBitmap.m_nObject );
   10169           0 :         aLine.append( " scaled to zero size, omitted\n" );
   10170             :     }
   10171           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10172           0 : }
   10173             : 
   10174           0 : const PDFWriterImpl::BitmapEmit& PDFWriterImpl::createBitmapEmit( const BitmapEx& i_rBitmap, bool bDrawMask )
   10175             : {
   10176           0 :     BitmapEx aBitmap( i_rBitmap );
   10177           0 :     if( m_aContext.ColorMode == PDFWriter::DrawGreyscale )
   10178             :     {
   10179           0 :         BmpConversion eConv = BMP_CONVERSION_8BIT_GREYS;
   10180           0 :         int nDepth = aBitmap.GetBitmap().GetBitCount();
   10181           0 :         if( nDepth <= 4 )
   10182           0 :             eConv = BMP_CONVERSION_4BIT_GREYS;
   10183           0 :         if( nDepth > 1 )
   10184           0 :             aBitmap.Convert( eConv );
   10185             :     }
   10186           0 :     BitmapID aID;
   10187           0 :     aID.m_aPixelSize        = aBitmap.GetSizePixel();
   10188           0 :     aID.m_nSize             = aBitmap.GetBitCount();
   10189           0 :     aID.m_nChecksum         = aBitmap.GetBitmap().GetChecksum();
   10190           0 :     aID.m_nMaskChecksum     = 0;
   10191           0 :     if( aBitmap.IsAlpha() )
   10192           0 :         aID.m_nMaskChecksum = aBitmap.GetAlpha().GetChecksum();
   10193             :     else
   10194             :     {
   10195           0 :         Bitmap aMask = aBitmap.GetMask();
   10196           0 :         if( ! aMask.IsEmpty() )
   10197           0 :             aID.m_nMaskChecksum = aMask.GetChecksum();
   10198             :     }
   10199           0 :     std::list< BitmapEmit >::const_iterator it;
   10200           0 :     for( it = m_aBitmaps.begin(); it != m_aBitmaps.end(); ++it )
   10201             :     {
   10202           0 :         if( aID == it->m_aID )
   10203           0 :             break;
   10204             :     }
   10205           0 :     if( it == m_aBitmaps.end() )
   10206             :     {
   10207           0 :         m_aBitmaps.push_front( BitmapEmit() );
   10208           0 :         m_aBitmaps.front().m_aID        = aID;
   10209           0 :         m_aBitmaps.front().m_aBitmap    = aBitmap;
   10210           0 :         m_aBitmaps.front().m_nObject    = createObject();
   10211           0 :         m_aBitmaps.front().m_bDrawMask  = bDrawMask;
   10212           0 :         it = m_aBitmaps.begin();
   10213             :     }
   10214             : 
   10215           0 :     OStringBuffer aObjName( 16 );
   10216           0 :     aObjName.append( "Im" );
   10217           0 :     aObjName.append( it->m_nObject );
   10218           0 :     pushResource( ResXObject, aObjName.makeStringAndClear(), it->m_nObject );
   10219             : 
   10220           0 :     return *it;
   10221             : }
   10222             : 
   10223           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const Bitmap& rBitmap )
   10224             : {
   10225           0 :     MARK( "drawBitmap (Bitmap)" );
   10226             : 
   10227             :     // #i40055# sanity check
   10228           0 :     if( ! (rDestSize.Width() && rDestSize.Height()) )
   10229           0 :         return;
   10230             : 
   10231           0 :     const BitmapEmit& rEmit = createBitmapEmit( BitmapEx( rBitmap ) );
   10232           0 :     drawBitmap( rDestPoint, rDestSize, rEmit, Color( COL_TRANSPARENT ) );
   10233             : }
   10234             : 
   10235           0 : void PDFWriterImpl::drawBitmap( const Point& rDestPoint, const Size& rDestSize, const BitmapEx& rBitmap )
   10236             : {
   10237           0 :     MARK( "drawBitmap (BitmapEx)" );
   10238             : 
   10239             :     // #i40055# sanity check
   10240           0 :     if( ! (rDestSize.Width() && rDestSize.Height()) )
   10241           0 :         return;
   10242             : 
   10243           0 :     const BitmapEmit& rEmit = createBitmapEmit( rBitmap );
   10244           0 :     drawBitmap( rDestPoint, rDestSize, rEmit, Color( COL_TRANSPARENT ) );
   10245             : }
   10246             : 
   10247           0 : sal_Int32 PDFWriterImpl::createGradient( const Gradient& rGradient, const Size& rSize )
   10248             : {
   10249           0 :     Size aPtSize( lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10250             :                                MapMode( MAP_POINT ),
   10251             :                                getReferenceDevice(),
   10252           0 :                                rSize ) );
   10253             :     // check if we already have this gradient
   10254           0 :     std::list<GradientEmit>::iterator it;
   10255             :     // rounding to point will generally lose some pixels
   10256             :     // round up to point boundary
   10257           0 :     aPtSize.Width()++;
   10258           0 :     aPtSize.Height()++;
   10259           0 :     for( it = m_aGradients.begin(); it != m_aGradients.end(); ++it )
   10260             :     {
   10261           0 :         if( it->m_aGradient == rGradient )
   10262             :         {
   10263           0 :             if( it->m_aSize == aPtSize )
   10264           0 :                 break;
   10265             :         }
   10266             :     }
   10267           0 :     if( it == m_aGradients.end() )
   10268             :     {
   10269           0 :         m_aGradients.push_front( GradientEmit() );
   10270           0 :         m_aGradients.front().m_aGradient    = rGradient;
   10271           0 :         m_aGradients.front().m_nObject      = createObject();
   10272           0 :         m_aGradients.front().m_aSize        = aPtSize;
   10273           0 :         it = m_aGradients.begin();
   10274             :     }
   10275             : 
   10276           0 :     OStringBuffer aObjName( 16 );
   10277           0 :     aObjName.append( 'P' );
   10278           0 :     aObjName.append( it->m_nObject );
   10279           0 :     pushResource( ResShading, aObjName.makeStringAndClear(), it->m_nObject );
   10280             : 
   10281           0 :     return it->m_nObject;
   10282             : }
   10283             : 
   10284           0 : void PDFWriterImpl::drawGradient( const Rectangle& rRect, const Gradient& rGradient )
   10285             : {
   10286           0 :     MARK( "drawGradient (Rectangle)" );
   10287             : 
   10288           0 :     if( m_aContext.Version == PDFWriter::PDF_1_2 )
   10289             :     {
   10290           0 :         drawRectangle( rRect );
   10291           0 :         return;
   10292             :     }
   10293             : 
   10294           0 :     sal_Int32 nGradient = createGradient( rGradient, rRect.GetSize() );
   10295             : 
   10296           0 :     Point aTranslate( rRect.BottomLeft() );
   10297           0 :     aTranslate += Point( 0, 1 );
   10298             : 
   10299           0 :     updateGraphicsState();
   10300             : 
   10301           0 :     OStringBuffer aLine( 80 );
   10302           0 :     aLine.append( "q 1 0 0 1 " );
   10303           0 :     m_aPages.back().appendPoint( aTranslate, aLine );
   10304           0 :     aLine.append( " cm " );
   10305             :     // if a stroke is appended reset the clip region before stroke
   10306           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
   10307           0 :         aLine.append( "q " );
   10308           0 :     aLine.append( "0 0 " );
   10309           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetWidth(), aLine, false );
   10310           0 :     aLine.append( ' ' );
   10311           0 :     m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetHeight(), aLine, true );
   10312           0 :     aLine.append( " re W n\n" );
   10313             : 
   10314           0 :     aLine.append( "/P" );
   10315           0 :     aLine.append( nGradient );
   10316           0 :     aLine.append( " sh " );
   10317           0 :     if( m_aGraphicsStack.front().m_aLineColor != Color( COL_TRANSPARENT ) )
   10318             :     {
   10319           0 :         aLine.append( "Q 0 0 " );
   10320           0 :         m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetWidth(), aLine, false );
   10321           0 :         aLine.append( ' ' );
   10322           0 :         m_aPages.back().appendMappedLength( (sal_Int32)rRect.GetHeight(), aLine, true );
   10323           0 :         aLine.append( " re S " );
   10324             :     }
   10325           0 :     aLine.append( "Q\n" );
   10326           0 :     writeBuffer( aLine.getStr(), aLine.getLength() );
   10327             : }
   10328             : 
   10329           0 : void PDFWriterImpl::drawHatch( const tools::PolyPolygon& rPolyPoly, const Hatch& rHatch )
   10330             : {
   10331           0 :     MARK( "drawHatch" );
   10332             : 
   10333           0 :     updateGraphicsState();
   10334             : 
   10335           0 :     if( rPolyPoly.Count() )
   10336             :     {
   10337           0 :         tools::PolyPolygon     aPolyPoly( rPolyPoly );
   10338             : 
   10339           0 :         aPolyPoly.Optimize( POLY_OPTIMIZE_NO_SAME );
   10340           0 :         push( PushFlags::LINECOLOR );
   10341           0 :         setLineColor( rHatch.GetColor() );
   10342           0 :         getReferenceDevice()->DrawHatch( aPolyPoly, rHatch, false );
   10343           0 :         pop();
   10344             :     }
   10345           0 : }
   10346             : 
   10347           0 : void PDFWriterImpl::drawWallpaper( const Rectangle& rRect, const Wallpaper& rWall )
   10348             : {
   10349           0 :     MARK( "drawWallpaper" );
   10350             : 
   10351           0 :     bool bDrawColor         = false;
   10352           0 :     bool bDrawGradient      = false;
   10353           0 :     bool bDrawBitmap        = false;
   10354             : 
   10355           0 :     BitmapEx aBitmap;
   10356           0 :     Point aBmpPos = rRect.TopLeft();
   10357           0 :     Size aBmpSize;
   10358           0 :     if( rWall.IsBitmap() )
   10359             :     {
   10360           0 :         aBitmap = rWall.GetBitmap();
   10361           0 :         aBmpSize = lcl_convert( aBitmap.GetPrefMapMode(),
   10362           0 :                                 getMapMode(),
   10363             :                                 getReferenceDevice(),
   10364           0 :                                 aBitmap.GetPrefSize() );
   10365           0 :         Rectangle aRect( rRect );
   10366           0 :         if( rWall.IsRect() )
   10367             :         {
   10368           0 :             aRect = rWall.GetRect();
   10369           0 :             aBmpPos = aRect.TopLeft();
   10370           0 :             aBmpSize = aRect.GetSize();
   10371             :         }
   10372           0 :         if( rWall.GetStyle() != WALLPAPER_SCALE )
   10373             :         {
   10374           0 :             if( rWall.GetStyle() != WALLPAPER_TILE )
   10375             :             {
   10376           0 :                 bDrawBitmap     = true;
   10377           0 :                 if( rWall.IsGradient() )
   10378           0 :                     bDrawGradient = true;
   10379             :                 else
   10380           0 :                     bDrawColor = true;
   10381           0 :                 switch( rWall.GetStyle() )
   10382             :                 {
   10383             :                     case WALLPAPER_TOPLEFT:
   10384           0 :                         break;
   10385             :                     case WALLPAPER_TOP:
   10386           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10387           0 :                         break;
   10388             :                     case WALLPAPER_LEFT:
   10389           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10390           0 :                         break;
   10391             :                     case WALLPAPER_TOPRIGHT:
   10392           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10393           0 :                         break;
   10394             :                     case WALLPAPER_CENTER:
   10395           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10396           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10397           0 :                         break;
   10398             :                     case WALLPAPER_RIGHT:
   10399           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10400           0 :                         aBmpPos.Y() += (aRect.GetHeight()-aBmpSize.Height())/2;
   10401           0 :                         break;
   10402             :                     case WALLPAPER_BOTTOMLEFT:
   10403           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10404           0 :                         break;
   10405             :                     case WALLPAPER_BOTTOM:
   10406           0 :                         aBmpPos.X() += (aRect.GetWidth()-aBmpSize.Width())/2;
   10407           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10408           0 :                         break;
   10409             :                     case WALLPAPER_BOTTOMRIGHT:
   10410           0 :                         aBmpPos.X() += aRect.GetWidth()-aBmpSize.Width();
   10411           0 :                         aBmpPos.Y() += aRect.GetHeight()-aBmpSize.Height();
   10412           0 :                         break;
   10413             :                     default: ;
   10414             :                 }
   10415             :             }
   10416             :             else
   10417             :             {
   10418             :                 // push the bitmap
   10419           0 :                 const BitmapEmit& rEmit = createBitmapEmit( BitmapEx( aBitmap ) );
   10420             : 
   10421             :                 // convert to page coordinates; this needs to be done here
   10422             :                 // since the emit does not know the page anymore
   10423           0 :                 Rectangle aConvertRect( aBmpPos, aBmpSize );
   10424           0 :                 m_aPages.back().convertRect( aConvertRect );
   10425             : 
   10426           0 :                 OStringBuffer aNameBuf(16);
   10427           0 :                 aNameBuf.append( "Im" );
   10428           0 :                 aNameBuf.append( rEmit.m_nObject );
   10429           0 :                 OString aImageName( aNameBuf.makeStringAndClear() );
   10430             : 
   10431             :                 // push the pattern
   10432           0 :                 OStringBuffer aTilingStream( 32 );
   10433           0 :                 appendFixedInt( aConvertRect.GetWidth(), aTilingStream );
   10434           0 :                 aTilingStream.append( " 0 0 " );
   10435           0 :                 appendFixedInt( aConvertRect.GetHeight(), aTilingStream );
   10436           0 :                 aTilingStream.append( " 0 0 cm\n/" );
   10437           0 :                 aTilingStream.append( aImageName );
   10438           0 :                 aTilingStream.append( " Do\n" );
   10439             : 
   10440           0 :                 m_aTilings.push_back( TilingEmit() );
   10441           0 :                 m_aTilings.back().m_nObject         = createObject();
   10442           0 :                 m_aTilings.back().m_aRectangle      = Rectangle( Point( 0, 0 ), aConvertRect.GetSize() );
   10443           0 :                 m_aTilings.back().m_pTilingStream   = new SvMemoryStream();
   10444           0 :                 m_aTilings.back().m_pTilingStream->Write( aTilingStream.getStr(), aTilingStream.getLength() );
   10445             :                 // phase the tiling so wallpaper begins on upper left
   10446           0 :                 m_aTilings.back().m_aTransform.matrix[2] = double(aConvertRect.Left() % aConvertRect.GetWidth()) / fDivisor;
   10447           0 :                 m_aTilings.back().m_aTransform.matrix[5] = double(aConvertRect.Top() % aConvertRect.GetHeight()) / fDivisor;
   10448           0 :                 m_aTilings.back().m_aResources.m_aXObjects[aImageName] = rEmit.m_nObject;
   10449             : 
   10450           0 :                 updateGraphicsState();
   10451             : 
   10452           0 :                 OStringBuffer aObjName( 16 );
   10453           0 :                 aObjName.append( 'P' );
   10454           0 :                 aObjName.append( m_aTilings.back().m_nObject );
   10455           0 :                 OString aPatternName( aObjName.makeStringAndClear() );
   10456           0 :                 pushResource( ResPattern, aPatternName, m_aTilings.back().m_nObject );
   10457             : 
   10458             :                 // fill a rRect with the pattern
   10459           0 :                 OStringBuffer aLine( 100 );
   10460           0 :                 aLine.append( "q /Pattern cs /" );
   10461           0 :                 aLine.append( aPatternName );
   10462           0 :                 aLine.append( " scn " );
   10463           0 :                 m_aPages.back().appendRect( rRect, aLine );
   10464           0 :                 aLine.append( " f Q\n" );
   10465           0 :                 writeBuffer( aLine.getStr(), aLine.getLength() );
   10466             :             }
   10467             :         }
   10468             :         else
   10469             :         {
   10470           0 :             aBmpPos     = aRect.TopLeft();
   10471           0 :             aBmpSize    = aRect.GetSize();
   10472           0 :             bDrawBitmap = true;
   10473             :         }
   10474             : 
   10475           0 :         if( aBitmap.IsTransparent() )
   10476             :         {
   10477           0 :             if( rWall.IsGradient() )
   10478           0 :                 bDrawGradient = true;
   10479             :             else
   10480           0 :                 bDrawColor = true;
   10481             :         }
   10482             :     }
   10483           0 :     else if( rWall.IsGradient() )
   10484           0 :         bDrawGradient = true;
   10485             :     else
   10486           0 :         bDrawColor = true;
   10487             : 
   10488           0 :     if( bDrawGradient )
   10489             :     {
   10490           0 :         drawGradient( rRect, rWall.GetGradient() );
   10491             :     }
   10492           0 :     if( bDrawColor )
   10493             :     {
   10494           0 :         Color aOldLineColor = m_aGraphicsStack.front().m_aLineColor;
   10495           0 :         Color aOldFillColor = m_aGraphicsStack.front().m_aFillColor;
   10496           0 :         setLineColor( Color( COL_TRANSPARENT ) );
   10497           0 :         setFillColor( rWall.GetColor() );
   10498           0 :         drawRectangle( rRect );
   10499           0 :         setLineColor( aOldLineColor );
   10500           0 :         setFillColor( aOldFillColor );
   10501             :     }
   10502           0 :     if( bDrawBitmap )
   10503             :     {
   10504             :         // set temporary clip region since aBmpPos and aBmpSize
   10505             :         // may be outside rRect
   10506           0 :         OStringBuffer aLine( 20 );
   10507           0 :         aLine.append( "q " );
   10508           0 :         m_aPages.back().appendRect( rRect, aLine );
   10509           0 :         aLine.append( " W n\n" );
   10510           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10511           0 :         drawBitmap( aBmpPos, aBmpSize, aBitmap );
   10512           0 :         writeBuffer( "Q\n", 2 );
   10513           0 :     }
   10514           0 : }
   10515             : 
   10516           0 : void PDFWriterImpl::updateGraphicsState(Mode const mode)
   10517             : {
   10518           0 :     OStringBuffer aLine( 256 );
   10519           0 :     GraphicsState& rNewState = m_aGraphicsStack.front();
   10520             :     // first set clip region since it might invalidate everything else
   10521             : 
   10522           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateClipRegion) )
   10523             :     {
   10524           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateClipRegion;
   10525             : 
   10526           0 :         if( m_aCurrentPDFState.m_bClipRegion != rNewState.m_bClipRegion ||
   10527           0 :             ( rNewState.m_bClipRegion && m_aCurrentPDFState.m_aClipRegion != rNewState.m_aClipRegion ) )
   10528             :         {
   10529           0 :             if( m_aCurrentPDFState.m_bClipRegion && m_aCurrentPDFState.m_aClipRegion.count() )
   10530             :             {
   10531           0 :                 aLine.append( "Q " );
   10532             :                 // invalidate everything but the clip region
   10533           0 :                 m_aCurrentPDFState = GraphicsState();
   10534           0 :                 rNewState.m_nUpdateFlags = sal::static_int_cast<sal_uInt16>(~GraphicsState::updateClipRegion);
   10535             :             }
   10536           0 :             if( rNewState.m_bClipRegion && rNewState.m_aClipRegion.count() )
   10537             :             {
   10538             :                 // clip region is always stored in private PDF mapmode
   10539           0 :                 MapMode aNewMapMode = rNewState.m_aMapMode;
   10540           0 :                 rNewState.m_aMapMode = m_aMapMode;
   10541           0 :                 getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10542           0 :                 m_aCurrentPDFState.m_aMapMode = rNewState.m_aMapMode;
   10543             : 
   10544           0 :                 aLine.append( "q " );
   10545           0 :                 m_aPages.back().appendPolyPolygon( rNewState.m_aClipRegion, aLine );
   10546           0 :                 aLine.append( "W* n\n" );
   10547           0 :                 rNewState.m_aMapMode = aNewMapMode;
   10548           0 :                 getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10549           0 :                 m_aCurrentPDFState.m_aMapMode = rNewState.m_aMapMode;
   10550             :             }
   10551             :         }
   10552             :     }
   10553             : 
   10554           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateMapMode) )
   10555             :     {
   10556           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateMapMode;
   10557           0 :         getReferenceDevice()->SetMapMode( rNewState.m_aMapMode );
   10558             :     }
   10559             : 
   10560           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateFont) )
   10561             :     {
   10562           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateFont;
   10563           0 :         getReferenceDevice()->SetFont( rNewState.m_aFont );
   10564           0 :         getReferenceDevice()->ImplNewFont();
   10565             :     }
   10566             : 
   10567           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateLayoutMode) )
   10568             :     {
   10569           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateLayoutMode;
   10570           0 :         getReferenceDevice()->SetLayoutMode( rNewState.m_nLayoutMode );
   10571             :     }
   10572             : 
   10573           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateDigitLanguage) )
   10574             :     {
   10575           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateDigitLanguage;
   10576           0 :         getReferenceDevice()->SetDigitLanguage( rNewState.m_aDigitLanguage );
   10577             :     }
   10578             : 
   10579           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateLineColor) )
   10580             :     {
   10581           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateLineColor;
   10582           0 :         if( m_aCurrentPDFState.m_aLineColor != rNewState.m_aLineColor &&
   10583           0 :             rNewState.m_aLineColor != Color( COL_TRANSPARENT ) )
   10584             :         {
   10585           0 :             appendStrokingColor( rNewState.m_aLineColor, aLine );
   10586           0 :             aLine.append( "\n" );
   10587             :         }
   10588             :     }
   10589             : 
   10590           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateFillColor) )
   10591             :     {
   10592           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateFillColor;
   10593           0 :         if( m_aCurrentPDFState.m_aFillColor != rNewState.m_aFillColor &&
   10594           0 :             rNewState.m_aFillColor != Color( COL_TRANSPARENT ) )
   10595             :         {
   10596           0 :             appendNonStrokingColor( rNewState.m_aFillColor, aLine );
   10597           0 :             aLine.append( "\n" );
   10598             :         }
   10599             :     }
   10600             : 
   10601           0 :     if( (rNewState.m_nUpdateFlags & GraphicsState::updateTransparentPercent) )
   10602             :     {
   10603           0 :         rNewState.m_nUpdateFlags &= ~GraphicsState::updateTransparentPercent;
   10604           0 :         if( m_aContext.Version >= PDFWriter::PDF_1_4 && m_aCurrentPDFState.m_nTransparentPercent != rNewState.m_nTransparentPercent )
   10605             :         {
   10606             :             // TODO: switch extended graphicsstate
   10607             :         }
   10608             :     }
   10609             : 
   10610             :     // everything is up to date now
   10611           0 :     m_aCurrentPDFState = m_aGraphicsStack.front();
   10612           0 :     if ((mode != NOWRITE) &&  !aLine.isEmpty())
   10613           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   10614           0 : }
   10615             : 
   10616             : /* #i47544# imitate OutputDevice behaviour:
   10617             : *  if a font with a nontransparent color is set, it overwrites the current
   10618             : *  text color. OTOH setting the text color will overwrite the color of the font.
   10619             : */
   10620           0 : void PDFWriterImpl::setFont( const vcl::Font& rFont )
   10621             : {
   10622           0 :     Color aColor = rFont.GetColor();
   10623           0 :     if( aColor == Color( COL_TRANSPARENT ) )
   10624           0 :         aColor = m_aGraphicsStack.front().m_aFont.GetColor();
   10625           0 :     m_aGraphicsStack.front().m_aFont = rFont;
   10626           0 :     m_aGraphicsStack.front().m_aFont.SetColor( aColor );
   10627           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateFont;
   10628           0 : }
   10629             : 
   10630           0 : void PDFWriterImpl::push( PushFlags nFlags )
   10631             : {
   10632             :     OSL_ENSURE( !m_aGraphicsStack.empty(), "invalid graphics stack" );
   10633           0 :     m_aGraphicsStack.push_front( m_aGraphicsStack.front() );
   10634           0 :     m_aGraphicsStack.front().m_nFlags = nFlags;
   10635           0 : }
   10636             : 
   10637           0 : void PDFWriterImpl::pop()
   10638             : {
   10639             :     OSL_ENSURE( m_aGraphicsStack.size() > 1, "pop without push" );
   10640           0 :     if( m_aGraphicsStack.size() < 2 )
   10641           0 :         return;
   10642             : 
   10643           0 :     GraphicsState aState = m_aGraphicsStack.front();
   10644           0 :     m_aGraphicsStack.pop_front();
   10645           0 :     GraphicsState& rOld = m_aGraphicsStack.front();
   10646             : 
   10647             :     // move those parameters back that were not pushed
   10648             :     // in the first place
   10649           0 :     if( ! (aState.m_nFlags & PushFlags::LINECOLOR) )
   10650           0 :         setLineColor( aState.m_aLineColor );
   10651           0 :     if( ! (aState.m_nFlags & PushFlags::FILLCOLOR) )
   10652           0 :         setFillColor( aState.m_aFillColor );
   10653           0 :     if( ! (aState.m_nFlags & PushFlags::FONT) )
   10654           0 :         setFont( aState.m_aFont );
   10655           0 :     if( ! (aState.m_nFlags & PushFlags::TEXTCOLOR) )
   10656           0 :         setTextColor( aState.m_aFont.GetColor() );
   10657           0 :     if( ! (aState.m_nFlags & PushFlags::MAPMODE) )
   10658           0 :         setMapMode( aState.m_aMapMode );
   10659           0 :     if( ! (aState.m_nFlags & PushFlags::CLIPREGION) )
   10660             :     {
   10661             :         // do not use setClipRegion here
   10662             :         // it would convert again assuming the current mapmode
   10663           0 :         rOld.m_aClipRegion = aState.m_aClipRegion;
   10664           0 :         rOld.m_bClipRegion = aState.m_bClipRegion;
   10665             :     }
   10666           0 :     if( ! (aState.m_nFlags & PushFlags::TEXTLINECOLOR ) )
   10667           0 :         setTextLineColor( aState.m_aTextLineColor );
   10668           0 :     if( ! (aState.m_nFlags & PushFlags::OVERLINECOLOR ) )
   10669           0 :         setOverlineColor( aState.m_aOverlineColor );
   10670           0 :     if( ! (aState.m_nFlags & PushFlags::TEXTALIGN ) )
   10671           0 :         setTextAlign( aState.m_aFont.GetAlign() );
   10672           0 :     if( ! (aState.m_nFlags & PushFlags::TEXTFILLCOLOR) )
   10673           0 :         setTextFillColor( aState.m_aFont.GetFillColor() );
   10674           0 :     if( ! (aState.m_nFlags & PushFlags::REFPOINT) )
   10675             :     {
   10676             :         // what ?
   10677             :     }
   10678             :     // invalidate graphics state
   10679           0 :     m_aGraphicsStack.front().m_nUpdateFlags = sal::static_int_cast<sal_uInt16>(~0U);
   10680             : }
   10681             : 
   10682           0 : void PDFWriterImpl::setMapMode( const MapMode& rMapMode )
   10683             : {
   10684           0 :     m_aGraphicsStack.front().m_aMapMode = rMapMode;
   10685           0 :     getReferenceDevice()->SetMapMode( rMapMode );
   10686           0 :     m_aCurrentPDFState.m_aMapMode = rMapMode;
   10687           0 : }
   10688             : 
   10689           0 : void PDFWriterImpl::setClipRegion( const basegfx::B2DPolyPolygon& rRegion )
   10690             : {
   10691           0 :     basegfx::B2DPolyPolygon aRegion = getReferenceDevice()->LogicToPixel( rRegion, m_aGraphicsStack.front().m_aMapMode );
   10692           0 :     aRegion = getReferenceDevice()->PixelToLogic( aRegion, m_aMapMode );
   10693           0 :     m_aGraphicsStack.front().m_aClipRegion = aRegion;
   10694           0 :     m_aGraphicsStack.front().m_bClipRegion = true;
   10695           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10696           0 : }
   10697             : 
   10698           0 : void PDFWriterImpl::moveClipRegion( sal_Int32 nX, sal_Int32 nY )
   10699             : {
   10700           0 :     if( m_aGraphicsStack.front().m_bClipRegion && m_aGraphicsStack.front().m_aClipRegion.count() )
   10701             :     {
   10702           0 :         Point aPoint( lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10703             :                                    m_aMapMode,
   10704             :                                    getReferenceDevice(),
   10705           0 :                                    Point( nX, nY ) ) );
   10706           0 :         aPoint -= lcl_convert( m_aGraphicsStack.front().m_aMapMode,
   10707             :                                m_aMapMode,
   10708             :                                getReferenceDevice(),
   10709           0 :                                Point() );
   10710           0 :         basegfx::B2DHomMatrix aMat;
   10711           0 :         aMat.translate( aPoint.X(), aPoint.Y() );
   10712           0 :         m_aGraphicsStack.front().m_aClipRegion.transform( aMat );
   10713           0 :         m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10714             :     }
   10715           0 : }
   10716             : 
   10717           0 : bool PDFWriterImpl::intersectClipRegion( const Rectangle& rRect )
   10718             : {
   10719             :     basegfx::B2DPolyPolygon aRect( basegfx::tools::createPolygonFromRect(
   10720           0 :         basegfx::B2DRectangle( rRect.Left(), rRect.Top(), rRect.Right(), rRect.Bottom() ) ) );
   10721           0 :     return intersectClipRegion( aRect );
   10722             : }
   10723             : 
   10724           0 : bool PDFWriterImpl::intersectClipRegion( const basegfx::B2DPolyPolygon& rRegion )
   10725             : {
   10726           0 :     basegfx::B2DPolyPolygon aRegion( getReferenceDevice()->LogicToPixel( rRegion, m_aGraphicsStack.front().m_aMapMode ) );
   10727           0 :     aRegion = getReferenceDevice()->PixelToLogic( aRegion, m_aMapMode );
   10728           0 :     m_aGraphicsStack.front().m_nUpdateFlags |= GraphicsState::updateClipRegion;
   10729           0 :     if( m_aGraphicsStack.front().m_bClipRegion )
   10730             :     {
   10731           0 :         basegfx::B2DPolyPolygon aOld( basegfx::tools::prepareForPolygonOperation( m_aGraphicsStack.front().m_aClipRegion ) );
   10732           0 :         aRegion = basegfx::tools::prepareForPolygonOperation( aRegion );
   10733           0 :         m_aGraphicsStack.front().m_aClipRegion = basegfx::tools::solvePolygonOperationAnd( aOld, aRegion );
   10734             :     }
   10735             :     else
   10736             :     {
   10737           0 :         m_aGraphicsStack.front().m_aClipRegion = aRegion;
   10738           0 :         m_aGraphicsStack.front().m_bClipRegion = true;
   10739             :     }
   10740           0 :     return true;
   10741             : }
   10742             : 
   10743           0 : void PDFWriterImpl::createNote( const Rectangle& rRect, const PDFNote& rNote, sal_Int32 nPageNr )
   10744             : {
   10745           0 :     if( nPageNr < 0 )
   10746           0 :         nPageNr = m_nCurrentPage;
   10747             : 
   10748           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10749           0 :         return;
   10750             : 
   10751           0 :     m_aNotes.push_back( PDFNoteEntry() );
   10752           0 :     m_aNotes.back().m_nObject       = createObject();
   10753           0 :     m_aNotes.back().m_aContents     = rNote;
   10754           0 :     m_aNotes.back().m_aRect         = rRect;
   10755             :     // convert to default user space now, since the mapmode may change
   10756           0 :     m_aPages[nPageNr].convertRect( m_aNotes.back().m_aRect );
   10757             : 
   10758             :     // insert note to page's annotation list
   10759           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( m_aNotes.back().m_nObject );
   10760             : }
   10761             : 
   10762           0 : sal_Int32 PDFWriterImpl::createLink( const Rectangle& rRect, sal_Int32 nPageNr )
   10763             : {
   10764           0 :     if( nPageNr < 0 )
   10765           0 :         nPageNr = m_nCurrentPage;
   10766             : 
   10767           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10768           0 :         return -1;
   10769             : 
   10770           0 :     sal_Int32 nRet = m_aLinks.size();
   10771             : 
   10772           0 :     m_aLinks.push_back( PDFLink() );
   10773           0 :     m_aLinks.back().m_nObject   = createObject();
   10774           0 :     m_aLinks.back().m_nPage     = nPageNr;
   10775           0 :     m_aLinks.back().m_aRect     = rRect;
   10776             :     // convert to default user space now, since the mapmode may change
   10777           0 :     m_aPages[nPageNr].convertRect( m_aLinks.back().m_aRect );
   10778             : 
   10779             :     // insert link to page's annotation list
   10780           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( m_aLinks.back().m_nObject );
   10781             : 
   10782           0 :     return nRet;
   10783             : }
   10784             : 
   10785             : //--->i56629
   10786           0 : sal_Int32 PDFWriterImpl::createNamedDest( const OUString& sDestName, const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10787             : {
   10788           0 :     if( nPageNr < 0 )
   10789           0 :         nPageNr = m_nCurrentPage;
   10790             : 
   10791           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10792           0 :         return -1;
   10793             : 
   10794           0 :     sal_Int32 nRet = m_aNamedDests.size();
   10795             : 
   10796           0 :     m_aNamedDests.push_back( PDFNamedDest() );
   10797           0 :     m_aNamedDests.back().m_aDestName = sDestName;
   10798           0 :     m_aNamedDests.back().m_nPage = nPageNr;
   10799           0 :     m_aNamedDests.back().m_eType = eType;
   10800           0 :     m_aNamedDests.back().m_aRect = rRect;
   10801             :     // convert to default user space now, since the mapmode may change
   10802           0 :     m_aPages[nPageNr].convertRect( m_aNamedDests.back().m_aRect );
   10803             : 
   10804           0 :     return nRet;
   10805             : }
   10806             : //<---i56629
   10807             : 
   10808           0 : sal_Int32 PDFWriterImpl::createDest( const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10809             : {
   10810           0 :     if( nPageNr < 0 )
   10811           0 :         nPageNr = m_nCurrentPage;
   10812             : 
   10813           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   10814           0 :         return -1;
   10815             : 
   10816           0 :     sal_Int32 nRet = m_aDests.size();
   10817             : 
   10818           0 :     m_aDests.push_back( PDFDest() );
   10819           0 :     m_aDests.back().m_nPage = nPageNr;
   10820           0 :     m_aDests.back().m_eType = eType;
   10821           0 :     m_aDests.back().m_aRect = rRect;
   10822             :     // convert to default user space now, since the mapmode may change
   10823           0 :     m_aPages[nPageNr].convertRect( m_aDests.back().m_aRect );
   10824             : 
   10825           0 :     return nRet;
   10826             : }
   10827             : 
   10828           0 : sal_Int32 PDFWriterImpl::registerDestReference( sal_Int32 nDestId, const Rectangle& rRect, sal_Int32 nPageNr, PDFWriter::DestAreaType eType )
   10829             : {
   10830           0 :     return m_aDestinationIdTranslation[ nDestId ] = createDest( rRect, nPageNr, eType );
   10831             : }
   10832             : 
   10833           0 : sal_Int32 PDFWriterImpl::setLinkDest( sal_Int32 nLinkId, sal_Int32 nDestId )
   10834             : {
   10835           0 :     if( nLinkId < 0 || nLinkId >= (sal_Int32)m_aLinks.size() )
   10836           0 :         return -1;
   10837           0 :     if( nDestId < 0 || nDestId >= (sal_Int32)m_aDests.size() )
   10838           0 :         return -2;
   10839             : 
   10840           0 :     m_aLinks[ nLinkId ].m_nDest = nDestId;
   10841             : 
   10842           0 :     return 0;
   10843             : }
   10844             : 
   10845           0 : sal_Int32 PDFWriterImpl::setLinkURL( sal_Int32 nLinkId, const OUString& rURL )
   10846             : {
   10847           0 :     if( nLinkId < 0 || nLinkId >= (sal_Int32)m_aLinks.size() )
   10848           0 :         return -1;
   10849             : 
   10850           0 :     m_aLinks[ nLinkId ].m_nDest = -1;
   10851             : 
   10852             :     using namespace ::com::sun::star;
   10853             : 
   10854           0 :     if (!m_xTrans.is())
   10855             :     {
   10856           0 :         uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() );
   10857           0 :         m_xTrans = util::URLTransformer::create(xContext);;
   10858             :     }
   10859             : 
   10860           0 :     util::URL aURL;
   10861           0 :     aURL.Complete = rURL;
   10862             : 
   10863           0 :     m_xTrans->parseStrict( aURL );
   10864             : 
   10865           0 :     m_aLinks[ nLinkId ].m_aURL  = aURL.Complete;
   10866             : 
   10867           0 :     return 0;
   10868             : }
   10869             : 
   10870           0 : void PDFWriterImpl::setLinkPropertyId( sal_Int32 nLinkId, sal_Int32 nPropertyId )
   10871             : {
   10872           0 :     m_aLinkPropertyMap[ nPropertyId ] = nLinkId;
   10873           0 : }
   10874             : 
   10875           0 : sal_Int32 PDFWriterImpl::createOutlineItem( sal_Int32 nParent, const OUString& rText, sal_Int32 nDestID )
   10876             : {
   10877             :     // create new item
   10878           0 :     sal_Int32 nNewItem = m_aOutline.size();
   10879           0 :     m_aOutline.push_back( PDFOutlineEntry() );
   10880             : 
   10881             :     // set item attributes
   10882           0 :     setOutlineItemParent( nNewItem, nParent );
   10883           0 :     setOutlineItemText( nNewItem, rText );
   10884           0 :     setOutlineItemDest( nNewItem, nDestID );
   10885             : 
   10886           0 :     return nNewItem;
   10887             : }
   10888             : 
   10889           0 : sal_Int32 PDFWriterImpl::setOutlineItemParent( sal_Int32 nItem, sal_Int32 nNewParent )
   10890             : {
   10891           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() )
   10892           0 :         return -1;
   10893             : 
   10894           0 :     int nRet = 0;
   10895             : 
   10896           0 :     if( nNewParent < 0 || nNewParent >= (sal_Int32)m_aOutline.size() || nNewParent == nItem )
   10897             :     {
   10898           0 :         nNewParent = 0;
   10899           0 :         nRet = -2;
   10900             :     }
   10901             :     // remove item from previous parent
   10902           0 :     sal_Int32 nParentID = m_aOutline[ nItem ].m_nParentID;
   10903           0 :     if( nParentID >= 0 && nParentID < (sal_Int32)m_aOutline.size() )
   10904             :     {
   10905           0 :         PDFOutlineEntry& rParent = m_aOutline[ nParentID ];
   10906             : 
   10907           0 :         for( std::vector<sal_Int32>::iterator it = rParent.m_aChildren.begin();
   10908           0 :              it != rParent.m_aChildren.end(); ++it )
   10909             :         {
   10910           0 :             if( *it == nItem )
   10911             :             {
   10912           0 :                 rParent.m_aChildren.erase( it );
   10913           0 :                 break;
   10914             :             }
   10915             :         }
   10916             :     }
   10917             : 
   10918             :     // insert item to new parent's list of children
   10919           0 :     m_aOutline[ nNewParent ].m_aChildren.push_back( nItem );
   10920             : 
   10921           0 :     return nRet;
   10922             : }
   10923             : 
   10924           0 : sal_Int32 PDFWriterImpl::setOutlineItemText( sal_Int32 nItem, const OUString& rText )
   10925             : {
   10926           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() )
   10927           0 :         return -1;
   10928             : 
   10929           0 :     m_aOutline[ nItem ].m_aTitle = psp::WhitespaceToSpace( rText );
   10930           0 :     return 0;
   10931             : }
   10932             : 
   10933           0 : sal_Int32 PDFWriterImpl::setOutlineItemDest( sal_Int32 nItem, sal_Int32 nDestID )
   10934             : {
   10935           0 :     if( nItem < 1 || nItem >= (sal_Int32)m_aOutline.size() ) // item does not exist
   10936           0 :         return -1;
   10937           0 :     if( nDestID < 0 || nDestID >= (sal_Int32)m_aDests.size() ) // dest does not exist
   10938           0 :         return -2;
   10939           0 :     m_aOutline[nItem].m_nDestID = nDestID;
   10940           0 :     return 0;
   10941             : }
   10942             : 
   10943           0 : const sal_Char* PDFWriterImpl::getStructureTag( PDFWriter::StructElement eType )
   10944             : {
   10945           0 :     static std::map< PDFWriter::StructElement, const char* > aTagStrings;
   10946           0 :     if( aTagStrings.empty() )
   10947             :     {
   10948           0 :         aTagStrings[ PDFWriter::NonStructElement] = "NonStruct";
   10949           0 :         aTagStrings[ PDFWriter::Document ]      = "Document";
   10950           0 :         aTagStrings[ PDFWriter::Part ]          = "Part";
   10951           0 :         aTagStrings[ PDFWriter::Article ]       = "Art";
   10952           0 :         aTagStrings[ PDFWriter::Section ]       = "Sect";
   10953           0 :         aTagStrings[ PDFWriter::Division ]      = "Div";
   10954           0 :         aTagStrings[ PDFWriter::BlockQuote ]    = "BlockQuote";
   10955           0 :         aTagStrings[ PDFWriter::Caption ]       = "Caption";
   10956           0 :         aTagStrings[ PDFWriter::TOC ]           = "TOC";
   10957           0 :         aTagStrings[ PDFWriter::TOCI ]          = "TOCI";
   10958           0 :         aTagStrings[ PDFWriter::Index ]         = "Index";
   10959           0 :         aTagStrings[ PDFWriter::Paragraph ]     = "P";
   10960           0 :         aTagStrings[ PDFWriter::Heading ]       = "H";
   10961           0 :         aTagStrings[ PDFWriter::H1 ]            = "H1";
   10962           0 :         aTagStrings[ PDFWriter::H2 ]            = "H2";
   10963           0 :         aTagStrings[ PDFWriter::H3 ]            = "H3";
   10964           0 :         aTagStrings[ PDFWriter::H4 ]            = "H4";
   10965           0 :         aTagStrings[ PDFWriter::H5 ]            = "H5";
   10966           0 :         aTagStrings[ PDFWriter::H6 ]            = "H6";
   10967           0 :         aTagStrings[ PDFWriter::List ]          = "L";
   10968           0 :         aTagStrings[ PDFWriter::ListItem ]      = "LI";
   10969           0 :         aTagStrings[ PDFWriter::LILabel ]       = "Lbl";
   10970           0 :         aTagStrings[ PDFWriter::LIBody ]        = "LBody";
   10971           0 :         aTagStrings[ PDFWriter::Table ]         = "Table";
   10972           0 :         aTagStrings[ PDFWriter::TableRow ]      = "TR";
   10973           0 :         aTagStrings[ PDFWriter::TableHeader ]   = "TH";
   10974           0 :         aTagStrings[ PDFWriter::TableData ]     = "TD";
   10975           0 :         aTagStrings[ PDFWriter::Span ]          = "Span";
   10976           0 :         aTagStrings[ PDFWriter::Quote ]         = "Quote";
   10977           0 :         aTagStrings[ PDFWriter::Note ]          = "Note";
   10978           0 :         aTagStrings[ PDFWriter::Reference ]     = "Reference";
   10979           0 :         aTagStrings[ PDFWriter::BibEntry ]      = "BibEntry";
   10980           0 :         aTagStrings[ PDFWriter::Code ]          = "Code";
   10981           0 :         aTagStrings[ PDFWriter::Link ]          = "Link";
   10982           0 :         aTagStrings[ PDFWriter::Figure ]        = "Figure";
   10983           0 :         aTagStrings[ PDFWriter::Formula ]       = "Formula";
   10984           0 :         aTagStrings[ PDFWriter::Form ]          = "Form";
   10985             :     }
   10986             : 
   10987           0 :     std::map< PDFWriter::StructElement, const char* >::const_iterator it = aTagStrings.find( eType );
   10988             : 
   10989           0 :     return it != aTagStrings.end() ? it->second : "Div";
   10990             : }
   10991             : 
   10992           0 : void PDFWriterImpl::beginStructureElementMCSeq()
   10993             : {
   10994           0 :     if( m_bEmitStructure &&
   10995           0 :         m_nCurrentStructElement > 0 && // StructTreeRoot
   10996           0 :         ! m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // already opened sequence
   10997             :         )
   10998             :     {
   10999           0 :         PDFStructureElement& rEle = m_aStructure[ m_nCurrentStructElement ];
   11000           0 :         OStringBuffer aLine( 128 );
   11001           0 :         sal_Int32 nMCID = m_aPages[ m_nCurrentPage ].m_aMCIDParents.size();
   11002           0 :         aLine.append( "/" );
   11003           0 :         if( !rEle.m_aAlias.isEmpty() )
   11004           0 :             aLine.append( rEle.m_aAlias );
   11005             :         else
   11006           0 :             aLine.append( getStructureTag( rEle.m_eType ) );
   11007           0 :         aLine.append( "<</MCID " );
   11008           0 :         aLine.append( nMCID );
   11009           0 :         aLine.append( ">>BDC\n" );
   11010           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   11011             : 
   11012             :         // update the element's content list
   11013             : #if OSL_DEBUG_LEVEL > 1
   11014             :         fprintf( stderr, "beginning marked content id %" SAL_PRIdINT32 " on page object %" SAL_PRIdINT32 ", structure first page = %" SAL_PRIdINT32 "\n",
   11015             :                  nMCID,
   11016             :                  m_aPages[ m_nCurrentPage ].m_nPageObject,
   11017             :                  rEle.m_nFirstPageObject );
   11018             : #endif
   11019           0 :         rEle.m_aKids.push_back( PDFStructureElementKid( nMCID, m_aPages[m_nCurrentPage].m_nPageObject ) );
   11020             :         // update the page's mcid parent list
   11021           0 :         m_aPages[ m_nCurrentPage ].m_aMCIDParents.push_back( rEle.m_nObject );
   11022             :         // mark element MC sequence as open
   11023           0 :         rEle.m_bOpenMCSeq = true;
   11024             :     }
   11025             :     // handle artifacts
   11026           0 :     else if( ! m_bEmitStructure && m_aContext.Tagged &&
   11027           0 :                m_nCurrentStructElement > 0 &&
   11028           0 :                m_aStructure[ m_nCurrentStructElement ].m_eType == PDFWriter::NonStructElement &&
   11029           0 :              ! m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // already opened sequence
   11030             :              )
   11031             :     {
   11032           0 :         OStringBuffer aLine( 128 );
   11033           0 :         aLine.append( "/Artifact BMC\n" );
   11034           0 :         writeBuffer( aLine.getStr(), aLine.getLength() );
   11035             :         // mark element MC sequence as open
   11036           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq = true;
   11037             :     }
   11038           0 : }
   11039             : 
   11040           0 : void PDFWriterImpl::endStructureElementMCSeq()
   11041             : {
   11042           0 :     if( m_nCurrentStructElement > 0 && // StructTreeRoot
   11043           0 :         ( m_bEmitStructure || m_aStructure[ m_nCurrentStructElement ].m_eType == PDFWriter::NonStructElement ) &&
   11044           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq // must have an opened MC sequence
   11045             :         )
   11046             :     {
   11047           0 :         writeBuffer( "EMC\n", 4 );
   11048           0 :         m_aStructure[ m_nCurrentStructElement ].m_bOpenMCSeq = false;
   11049             :     }
   11050           0 : }
   11051             : 
   11052           0 : bool PDFWriterImpl::checkEmitStructure()
   11053             : {
   11054           0 :     bool bEmit = false;
   11055           0 :     if( m_aContext.Tagged )
   11056             :     {
   11057           0 :         bEmit = true;
   11058           0 :         sal_Int32 nEle = m_nCurrentStructElement;
   11059           0 :         while( nEle > 0 && nEle < sal_Int32(m_aStructure.size()) )
   11060             :         {
   11061           0 :             if( m_aStructure[ nEle ].m_eType == PDFWriter::NonStructElement )
   11062             :             {
   11063           0 :                 bEmit = false;
   11064           0 :                 break;
   11065             :             }
   11066           0 :             nEle = m_aStructure[ nEle ].m_nParentElement;
   11067             :         }
   11068             :     }
   11069           0 :     return bEmit;
   11070             : }
   11071             : 
   11072           0 : sal_Int32 PDFWriterImpl::beginStructureElement( PDFWriter::StructElement eType, const OUString& rAlias )
   11073             : {
   11074           0 :     if( m_nCurrentPage < 0 )
   11075           0 :         return -1;
   11076             : 
   11077           0 :     if( ! m_aContext.Tagged )
   11078           0 :         return -1;
   11079             : 
   11080             :     // close eventual current MC sequence
   11081           0 :     endStructureElementMCSeq();
   11082             : 
   11083           0 :     if( m_nCurrentStructElement == 0 &&
   11084           0 :         eType != PDFWriter::Document && eType != PDFWriter::NonStructElement )
   11085             :     {
   11086             :         // struct tree root hit, but not beginning document
   11087             :         // this might happen with setCurrentStructureElement
   11088             :         // silently insert structure into document again if one properly exists
   11089           0 :         if( ! m_aStructure[ 0 ].m_aChildren.empty() )
   11090             :         {
   11091           0 :             PDFWriter::StructElement childType = PDFWriter::NonStructElement;
   11092           0 :             sal_Int32 nNewCurElement = 0;
   11093           0 :             const std::list< sal_Int32 >& rRootChildren = m_aStructure[0].m_aChildren;
   11094           0 :             for( std::list< sal_Int32 >::const_iterator it = rRootChildren.begin();
   11095           0 :                  childType != PDFWriter::Document && it != rRootChildren.end(); ++it )
   11096             :             {
   11097           0 :                 nNewCurElement = *it;
   11098           0 :                 childType = m_aStructure[ nNewCurElement ].m_eType;
   11099             :             }
   11100           0 :             if( childType == PDFWriter::Document )
   11101             :             {
   11102           0 :                 m_nCurrentStructElement = nNewCurElement;
   11103             :                 DBG_ASSERT( false, "Structure element inserted to StructTreeRoot that is not a document" );
   11104             :             }
   11105             :             else {
   11106             :                 OSL_FAIL( "document structure in disorder !" );
   11107             :             }
   11108             :         }
   11109             :         else {
   11110             :             OSL_FAIL( "PDF document structure MUST be contained in a Document element" );
   11111             :         }
   11112             :     }
   11113             : 
   11114           0 :     sal_Int32 nNewId = sal_Int32(m_aStructure.size());
   11115           0 :     m_aStructure.push_back( PDFStructureElement() );
   11116           0 :     PDFStructureElement& rEle = m_aStructure.back();
   11117           0 :     rEle.m_eType            = eType;
   11118           0 :     rEle.m_nOwnElement      = nNewId;
   11119           0 :     rEle.m_nParentElement   = m_nCurrentStructElement;
   11120           0 :     rEle.m_nFirstPageObject = m_aPages[ m_nCurrentPage ].m_nPageObject;
   11121           0 :     m_aStructure[ m_nCurrentStructElement ].m_aChildren.push_back( nNewId );
   11122           0 :     m_nCurrentStructElement = nNewId;
   11123             : 
   11124             :     // handle alias names
   11125           0 :     if( !rAlias.isEmpty() && eType != PDFWriter::NonStructElement )
   11126             :     {
   11127           0 :         OStringBuffer aNameBuf( rAlias.getLength() );
   11128           0 :         appendName( rAlias, aNameBuf );
   11129           0 :         OString aAliasName( aNameBuf.makeStringAndClear() );
   11130           0 :         rEle.m_aAlias = aAliasName;
   11131           0 :         m_aRoleMap[ aAliasName ] = getStructureTag( eType );
   11132             :     }
   11133             : 
   11134             : #if OSL_DEBUG_LEVEL > 1
   11135             :     OStringBuffer aLine( "beginStructureElement " );
   11136             :     aLine.append( m_nCurrentStructElement );
   11137             :     aLine.append( ": " );
   11138             :     aLine.append( getStructureTag( eType ) );
   11139             :     if( !rEle.m_aAlias.isEmpty() )
   11140             :     {
   11141             :         aLine.append( " aliased as \"" );
   11142             :         aLine.append( rEle.m_aAlias );
   11143             :         aLine.append( '\"' );
   11144             :     }
   11145             :     emitComment( aLine.getStr() );
   11146             : #endif
   11147             : 
   11148             :     // check whether to emit structure henceforth
   11149           0 :     m_bEmitStructure = checkEmitStructure();
   11150             : 
   11151           0 :     if( m_bEmitStructure ) // don't create nonexistant objects
   11152             :     {
   11153           0 :         rEle.m_nObject      = createObject();
   11154             :         // update parent's kids list
   11155           0 :         m_aStructure[ rEle.m_nParentElement ].m_aKids.push_back( rEle.m_nObject );
   11156             :     }
   11157           0 :     return nNewId;
   11158             : }
   11159             : 
   11160           0 : void PDFWriterImpl::endStructureElement()
   11161             : {
   11162           0 :     if( m_nCurrentPage < 0 )
   11163           0 :         return;
   11164             : 
   11165           0 :     if( ! m_aContext.Tagged )
   11166           0 :         return;
   11167             : 
   11168           0 :     if( m_nCurrentStructElement == 0 )
   11169             :     {
   11170             :         // hit the struct tree root, that means there is an endStructureElement
   11171             :         // without corresponding beginStructureElement
   11172           0 :         return;
   11173             :     }
   11174             : 
   11175             :     // end the marked content sequence
   11176           0 :     endStructureElementMCSeq();
   11177             : 
   11178             : #if OSL_DEBUG_LEVEL > 1
   11179             :     OStringBuffer aLine( "endStructureElement " );
   11180             :     aLine.append( m_nCurrentStructElement );
   11181             :     aLine.append( ": " );
   11182             :     aLine.append( getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ) );
   11183             :     if( !m_aStructure[ m_nCurrentStructElement ].m_aAlias.isEmpty() )
   11184             :     {
   11185             :         aLine.append( " aliased as \"" );
   11186             :         aLine.append( m_aStructure[ m_nCurrentStructElement ].m_aAlias );
   11187             :         aLine.append( '\"' );
   11188             :     }
   11189             : #endif
   11190             : 
   11191             :     // "end" the structure element, the parent becomes current element
   11192           0 :     m_nCurrentStructElement = m_aStructure[ m_nCurrentStructElement ].m_nParentElement;
   11193             : 
   11194             :     // check whether to emit structure henceforth
   11195           0 :     m_bEmitStructure = checkEmitStructure();
   11196             : 
   11197             : #if OSL_DEBUG_LEVEL > 1
   11198             :     if( m_bEmitStructure )
   11199             :         emitComment( aLine.getStr() );
   11200             : #endif
   11201             : }
   11202             : 
   11203             : //---> i94258
   11204             : /*
   11205             :  * This function adds an internal structure list container to overcome the 8191 elements array limitation
   11206             :  * in kids element emission.
   11207             :  * Recursive function
   11208             :  *
   11209             :  */
   11210           0 : void PDFWriterImpl::addInternalStructureContainer( PDFStructureElement& rEle )
   11211             : {
   11212           0 :     if( rEle.m_eType == PDFWriter::NonStructElement &&
   11213           0 :         rEle.m_nOwnElement != rEle.m_nParentElement )
   11214           0 :         return;
   11215             : 
   11216           0 :     for( std::list< sal_Int32 >::const_iterator it = rEle.m_aChildren.begin(); it != rEle.m_aChildren.end(); ++it )
   11217             :     {
   11218           0 :         if( *it > 0 && *it < sal_Int32(m_aStructure.size()) )
   11219             :         {
   11220           0 :             PDFStructureElement& rChild = m_aStructure[ *it ];
   11221           0 :             if( rChild.m_eType != PDFWriter::NonStructElement )
   11222             :             {
   11223             :                 //triggered when a child of the rEle element is found
   11224           0 :                 if( rChild.m_nParentElement == rEle.m_nOwnElement )
   11225           0 :                     addInternalStructureContainer( rChild );//examine the child
   11226             :                 else
   11227             :                 {
   11228             :                     OSL_FAIL( "PDFWriterImpl::addInternalStructureContainer: invalid child structure element" );
   11229             : #if OSL_DEBUG_LEVEL > 1
   11230             :                     fprintf( stderr, "PDFWriterImpl::addInternalStructureContainer: invalid child structure elemnt with id %" SAL_PRIdINT32 "\n", *it );
   11231             : #endif
   11232             :                 }
   11233             :             }
   11234             :         }
   11235             :         else
   11236             :         {
   11237             :             OSL_FAIL( "PDFWriterImpl::emitStructure: invalid child structure id" );
   11238             : #if OSL_DEBUG_LEVEL > 1
   11239             :             fprintf( stderr, "PDFWriterImpl::addInternalStructureContainer: invalid child structure id %" SAL_PRIdINT32 "\n", *it );
   11240             : #endif
   11241             :         }
   11242             :     }
   11243             : 
   11244           0 :     if( rEle.m_nOwnElement != rEle.m_nParentElement )
   11245             :     {
   11246           0 :         if( !rEle.m_aKids.empty() )
   11247             :         {
   11248           0 :             if( rEle.m_aKids.size() > ncMaxPDFArraySize ) {
   11249             :                 //then we need to add the containers for the kids elements
   11250             :                 // a list to be used for the new kid element
   11251           0 :                 std::list< PDFStructureElementKid > aNewKids;
   11252           0 :                 std::list< sal_Int32 > aNewChildren;
   11253             : 
   11254             :                 // add Div in RoleMap, in case no one else did (TODO: is it needed? Is it dangerous?)
   11255           0 :                 OStringBuffer aNameBuf( "Div" );
   11256           0 :                 OString aAliasName( aNameBuf.makeStringAndClear() );
   11257           0 :                 m_aRoleMap[ aAliasName ] = getStructureTag( PDFWriter::Division );
   11258             : 
   11259           0 :                 while( rEle.m_aKids.size() > ncMaxPDFArraySize )
   11260             :                 {
   11261           0 :                     sal_Int32 nCurrentStructElement = rEle.m_nOwnElement;
   11262           0 :                     sal_Int32 nNewId = sal_Int32(m_aStructure.size());
   11263           0 :                     m_aStructure.push_back( PDFStructureElement() );
   11264           0 :                     PDFStructureElement& rEleNew = m_aStructure.back();
   11265           0 :                     rEleNew.m_aAlias            = aAliasName;
   11266           0 :                     rEleNew.m_eType             = PDFWriter::Division; // a new Div type container
   11267           0 :                     rEleNew.m_nOwnElement       = nNewId;
   11268           0 :                     rEleNew.m_nParentElement    = nCurrentStructElement;
   11269             :                     //inherit the same page as the first child to be reparented
   11270           0 :                     rEleNew.m_nFirstPageObject  = m_aStructure[ rEle.m_aChildren.front() ].m_nFirstPageObject;
   11271           0 :                     rEleNew.m_nObject           = createObject();//assign a PDF object number
   11272             :                     //add the object to the kid list of the parent
   11273           0 :                     aNewKids.push_back( PDFStructureElementKid( rEleNew.m_nObject ) );
   11274           0 :                     aNewChildren.push_back( nNewId );
   11275             : 
   11276           0 :                     std::list< sal_Int32 >::iterator aChildEndIt( rEle.m_aChildren.begin() );
   11277           0 :                     std::list< PDFStructureElementKid >::iterator aKidEndIt( rEle.m_aKids.begin() );
   11278           0 :                     advance( aChildEndIt, ncMaxPDFArraySize );
   11279           0 :                     advance( aKidEndIt, ncMaxPDFArraySize );
   11280             : 
   11281           0 :                     rEleNew.m_aKids.splice( rEleNew.m_aKids.begin(),
   11282             :                                             rEle.m_aKids,
   11283           0 :                                             rEle.m_aKids.begin(),
   11284           0 :                                             aKidEndIt );
   11285           0 :                     rEleNew.m_aChildren.splice( rEleNew.m_aChildren.begin(),
   11286             :                                                 rEle.m_aChildren,
   11287           0 :                                                 rEle.m_aChildren.begin(),
   11288           0 :                                                 aChildEndIt );
   11289             :                     // set the kid's new parent
   11290           0 :                     for( std::list< sal_Int32 >::const_iterator it = rEleNew.m_aChildren.begin();
   11291           0 :                          it != rEleNew.m_aChildren.end(); ++it )
   11292             :                     {
   11293           0 :                         m_aStructure[ *it ].m_nParentElement = nNewId;
   11294             :                     }
   11295             :                 }
   11296             :                 //finally add the new kids resulting from the container added
   11297           0 :                 rEle.m_aKids.insert( rEle.m_aKids.begin(), aNewKids.begin(), aNewKids.end() );
   11298           0 :                 rEle.m_aChildren.insert( rEle.m_aChildren.begin(), aNewChildren.begin(), aNewChildren.end() );
   11299             :             }
   11300             :         }
   11301             :     }
   11302             : }
   11303             : //<--- i94258
   11304             : 
   11305           0 : bool PDFWriterImpl::setCurrentStructureElement( sal_Int32 nEle )
   11306             : {
   11307           0 :     bool bSuccess = false;
   11308             : 
   11309           0 :     if( m_aContext.Tagged && nEle >= 0 && nEle < sal_Int32(m_aStructure.size()) )
   11310             :     {
   11311             :         // end eventual previous marked content sequence
   11312           0 :         endStructureElementMCSeq();
   11313             : 
   11314           0 :         m_nCurrentStructElement = nEle;
   11315           0 :         m_bEmitStructure = checkEmitStructure();
   11316             : #if OSL_DEBUG_LEVEL > 1
   11317             :         OStringBuffer aLine( "setCurrentStructureElement " );
   11318             :         aLine.append( m_nCurrentStructElement );
   11319             :         aLine.append( ": " );
   11320             :         aLine.append( getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ) );
   11321             :         if( !m_aStructure[ m_nCurrentStructElement ].m_aAlias.isEmpty() )
   11322             :         {
   11323             :             aLine.append( " aliased as \"" );
   11324             :             aLine.append( m_aStructure[ m_nCurrentStructElement ].m_aAlias );
   11325             :             aLine.append( '\"' );
   11326             :         }
   11327             :         if( ! m_bEmitStructure )
   11328             :             aLine.append( " (inside NonStruct)" );
   11329             :         emitComment( aLine.getStr() );
   11330             : #endif
   11331           0 :         bSuccess = true;
   11332             :     }
   11333             : 
   11334           0 :     return bSuccess;
   11335             : }
   11336             : 
   11337           0 : bool PDFWriterImpl::setStructureAttribute( enum PDFWriter::StructAttribute eAttr, enum PDFWriter::StructAttributeValue eVal )
   11338             : {
   11339           0 :     if( !m_aContext.Tagged )
   11340           0 :         return false;
   11341             : 
   11342           0 :     bool bInsert = false;
   11343           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11344             :     {
   11345           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11346           0 :         switch( eAttr )
   11347             :         {
   11348             :             case PDFWriter::Placement:
   11349           0 :                 if( eVal == PDFWriter::Block        ||
   11350           0 :                     eVal == PDFWriter::Inline       ||
   11351           0 :                     eVal == PDFWriter::Before       ||
   11352           0 :                     eVal == PDFWriter::Start        ||
   11353             :                     eVal == PDFWriter::End )
   11354           0 :                     bInsert = true;
   11355           0 :                 break;
   11356             :             case PDFWriter::WritingMode:
   11357           0 :                 if( eVal == PDFWriter::LrTb         ||
   11358           0 :                     eVal == PDFWriter::RlTb         ||
   11359             :                     eVal == PDFWriter::TbRl )
   11360             :                 {
   11361           0 :                     bInsert = true;
   11362             :                 }
   11363           0 :                 break;
   11364             :             case PDFWriter::TextAlign:
   11365           0 :                 if( eVal == PDFWriter::Start        ||
   11366           0 :                     eVal == PDFWriter::Center       ||
   11367           0 :                     eVal == PDFWriter::End          ||
   11368             :                     eVal == PDFWriter::Justify )
   11369             :                 {
   11370           0 :                     if( eType == PDFWriter::Paragraph   ||
   11371           0 :                         eType == PDFWriter::Heading     ||
   11372           0 :                         eType == PDFWriter::H1          ||
   11373           0 :                         eType == PDFWriter::H2          ||
   11374           0 :                         eType == PDFWriter::H3          ||
   11375           0 :                         eType == PDFWriter::H4          ||
   11376           0 :                         eType == PDFWriter::H5          ||
   11377           0 :                         eType == PDFWriter::H6          ||
   11378           0 :                         eType == PDFWriter::List        ||
   11379           0 :                         eType == PDFWriter::ListItem    ||
   11380           0 :                         eType == PDFWriter::LILabel     ||
   11381           0 :                         eType == PDFWriter::LIBody      ||
   11382           0 :                         eType == PDFWriter::Table       ||
   11383           0 :                         eType == PDFWriter::TableRow    ||
   11384           0 :                         eType == PDFWriter::TableHeader ||
   11385             :                         eType == PDFWriter::TableData )
   11386             :                     {
   11387           0 :                         bInsert = true;
   11388             :                     }
   11389             :                 }
   11390           0 :                 break;
   11391             :             case PDFWriter::Width:
   11392             :             case PDFWriter::Height:
   11393           0 :                 if( eVal == PDFWriter::Auto )
   11394             :                 {
   11395           0 :                     if( eType == PDFWriter::Figure      ||
   11396           0 :                         eType == PDFWriter::Formula     ||
   11397           0 :                         eType == PDFWriter::Form        ||
   11398           0 :                         eType == PDFWriter::Table       ||
   11399           0 :                         eType == PDFWriter::TableHeader ||
   11400             :                         eType == PDFWriter::TableData )
   11401             :                     {
   11402           0 :                         bInsert = true;
   11403             :                     }
   11404             :                 }
   11405           0 :                 break;
   11406             :             case PDFWriter::BlockAlign:
   11407           0 :                 if( eVal == PDFWriter::Before       ||
   11408           0 :                     eVal == PDFWriter::Middle       ||
   11409           0 :                     eVal == PDFWriter::After        ||
   11410             :                     eVal == PDFWriter::Justify )
   11411             :                 {
   11412           0 :                     if( eType == PDFWriter::TableHeader ||
   11413             :                         eType == PDFWriter::TableData )
   11414             :                     {
   11415           0 :                         bInsert = true;
   11416             :                     }
   11417             :                 }
   11418           0 :                 break;
   11419             :             case PDFWriter::InlineAlign:
   11420           0 :                 if( eVal == PDFWriter::Start        ||
   11421           0 :                     eVal == PDFWriter::Center       ||
   11422             :                     eVal == PDFWriter::End )
   11423             :                 {
   11424           0 :                     if( eType == PDFWriter::TableHeader ||
   11425             :                         eType == PDFWriter::TableData )
   11426             :                     {
   11427           0 :                         bInsert = true;
   11428             :                     }
   11429             :                 }
   11430           0 :                 break;
   11431             :             case PDFWriter::LineHeight:
   11432           0 :                 if( eVal == PDFWriter::Normal       ||
   11433             :                     eVal == PDFWriter::Auto )
   11434             :                 {
   11435             :                     // only for ILSE and BLSE
   11436           0 :                     if( eType == PDFWriter::Paragraph   ||
   11437           0 :                         eType == PDFWriter::Heading     ||
   11438           0 :                         eType == PDFWriter::H1          ||
   11439           0 :                         eType == PDFWriter::H2          ||
   11440           0 :                         eType == PDFWriter::H3          ||
   11441           0 :                         eType == PDFWriter::H4          ||
   11442           0 :                         eType == PDFWriter::H5          ||
   11443           0 :                         eType == PDFWriter::H6          ||
   11444           0 :                         eType == PDFWriter::List        ||
   11445           0 :                         eType == PDFWriter::ListItem    ||
   11446           0 :                         eType == PDFWriter::LILabel     ||
   11447           0 :                         eType == PDFWriter::LIBody      ||
   11448           0 :                         eType == PDFWriter::Table       ||
   11449           0 :                         eType == PDFWriter::TableRow    ||
   11450           0 :                         eType == PDFWriter::TableHeader ||
   11451           0 :                         eType == PDFWriter::TableData   ||
   11452           0 :                         eType == PDFWriter::Span        ||
   11453           0 :                         eType == PDFWriter::Quote       ||
   11454           0 :                         eType == PDFWriter::Note        ||
   11455           0 :                         eType == PDFWriter::Reference   ||
   11456           0 :                         eType == PDFWriter::BibEntry    ||
   11457           0 :                         eType == PDFWriter::Code        ||
   11458             :                         eType == PDFWriter::Link )
   11459             :                     {
   11460           0 :                         bInsert = true;
   11461             :                     }
   11462             :                 }
   11463           0 :                 break;
   11464             :             case PDFWriter::TextDecorationType:
   11465           0 :                 if( eVal == PDFWriter::NONE         ||
   11466           0 :                     eVal == PDFWriter::Underline    ||
   11467           0 :                     eVal == PDFWriter::Overline     ||
   11468             :                     eVal == PDFWriter::LineThrough )
   11469             :                 {
   11470             :                     // only for ILSE and BLSE
   11471           0 :                     if( eType == PDFWriter::Paragraph   ||
   11472           0 :                         eType == PDFWriter::Heading     ||
   11473           0 :                         eType == PDFWriter::H1          ||
   11474           0 :                         eType == PDFWriter::H2          ||
   11475           0 :                         eType == PDFWriter::H3          ||
   11476           0 :                         eType == PDFWriter::H4          ||
   11477           0 :                         eType == PDFWriter::H5          ||
   11478           0 :                         eType == PDFWriter::H6          ||
   11479           0 :                         eType == PDFWriter::List        ||
   11480           0 :                         eType == PDFWriter::ListItem    ||
   11481           0 :                         eType == PDFWriter::LILabel     ||
   11482           0 :                         eType == PDFWriter::LIBody      ||
   11483           0 :                         eType == PDFWriter::Table       ||
   11484           0 :                         eType == PDFWriter::TableRow    ||
   11485           0 :                         eType == PDFWriter::TableHeader ||
   11486           0 :                         eType == PDFWriter::TableData   ||
   11487           0 :                         eType == PDFWriter::Span        ||
   11488           0 :                         eType == PDFWriter::Quote       ||
   11489           0 :                         eType == PDFWriter::Note        ||
   11490           0 :                         eType == PDFWriter::Reference   ||
   11491           0 :                         eType == PDFWriter::BibEntry    ||
   11492           0 :                         eType == PDFWriter::Code        ||
   11493             :                         eType == PDFWriter::Link )
   11494             :                     {
   11495           0 :                         bInsert = true;
   11496             :                     }
   11497             :                 }
   11498           0 :                 break;
   11499             :             case PDFWriter::ListNumbering:
   11500           0 :                 if( eVal == PDFWriter::NONE         ||
   11501           0 :                     eVal == PDFWriter::Disc         ||
   11502           0 :                     eVal == PDFWriter::Circle       ||
   11503           0 :                     eVal == PDFWriter::Square       ||
   11504           0 :                     eVal == PDFWriter::Decimal      ||
   11505           0 :                     eVal == PDFWriter::UpperRoman   ||
   11506           0 :                     eVal == PDFWriter::LowerRoman   ||
   11507           0 :                     eVal == PDFWriter::UpperAlpha   ||
   11508             :                     eVal == PDFWriter::LowerAlpha )
   11509             :                 {
   11510           0 :                     if( eType == PDFWriter::List )
   11511           0 :                         bInsert = true;
   11512             :                 }
   11513           0 :                 break;
   11514           0 :             default: break;
   11515             :         }
   11516             :     }
   11517             : 
   11518           0 :     if( bInsert )
   11519           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAttributes[ eAttr ] = PDFStructureAttribute( eVal );
   11520             : #if OSL_DEBUG_LEVEL > 1
   11521             :     else if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11522             :         fprintf( stderr, "rejecting setStructureAttribute( %s, %s ) on %s (%s) element\n",
   11523             :                  getAttributeTag( eAttr ),
   11524             :                  getAttributeValueTag( eVal ),
   11525             :                  getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ),
   11526             :                  m_aStructure[ m_nCurrentStructElement ].m_aAlias.getStr()
   11527             :                  );
   11528             : #endif
   11529             : 
   11530           0 :     return bInsert;
   11531             : }
   11532             : 
   11533           0 : bool PDFWriterImpl::setStructureAttributeNumerical( enum PDFWriter::StructAttribute eAttr, sal_Int32 nValue )
   11534             : {
   11535           0 :     if( ! m_aContext.Tagged )
   11536           0 :         return false;
   11537             : 
   11538           0 :     bool bInsert = false;
   11539           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11540             :     {
   11541           0 :         if( eAttr == PDFWriter::Language )
   11542             :         {
   11543           0 :             m_aStructure[ m_nCurrentStructElement ].m_aLocale = LanguageTag( (LanguageType)nValue ).getLocale();
   11544           0 :             return true;
   11545             :         }
   11546             : 
   11547           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11548           0 :         switch( eAttr )
   11549             :         {
   11550             :             case PDFWriter::SpaceBefore:
   11551             :             case PDFWriter::SpaceAfter:
   11552             :             case PDFWriter::StartIndent:
   11553             :             case PDFWriter::EndIndent:
   11554             :                 // just for BLSE
   11555           0 :                 if( eType == PDFWriter::Paragraph   ||
   11556           0 :                     eType == PDFWriter::Heading     ||
   11557           0 :                     eType == PDFWriter::H1          ||
   11558           0 :                     eType == PDFWriter::H2          ||
   11559           0 :                     eType == PDFWriter::H3          ||
   11560           0 :                     eType == PDFWriter::H4          ||
   11561           0 :                     eType == PDFWriter::H5          ||
   11562           0 :                     eType == PDFWriter::H6          ||
   11563           0 :                     eType == PDFWriter::List        ||
   11564           0 :                     eType == PDFWriter::ListItem    ||
   11565           0 :                     eType == PDFWriter::LILabel     ||
   11566           0 :                     eType == PDFWriter::LIBody      ||
   11567           0 :                     eType == PDFWriter::Table       ||
   11568           0 :                     eType == PDFWriter::TableRow    ||
   11569           0 :                     eType == PDFWriter::TableHeader ||
   11570             :                     eType == PDFWriter::TableData )
   11571             :                 {
   11572           0 :                     bInsert = true;
   11573             :                 }
   11574           0 :                 break;
   11575             :             case PDFWriter::TextIndent:
   11576             :                 // paragraph like BLSE and additional elements
   11577           0 :                 if( eType == PDFWriter::Paragraph   ||
   11578           0 :                     eType == PDFWriter::Heading     ||
   11579           0 :                     eType == PDFWriter::H1          ||
   11580           0 :                     eType == PDFWriter::H2          ||
   11581           0 :                     eType == PDFWriter::H3          ||
   11582           0 :                     eType == PDFWriter::H4          ||
   11583           0 :                     eType == PDFWriter::H5          ||
   11584           0 :                     eType == PDFWriter::H6          ||
   11585           0 :                     eType == PDFWriter::LILabel     ||
   11586           0 :                     eType == PDFWriter::LIBody      ||
   11587           0 :                     eType == PDFWriter::TableHeader ||
   11588             :                     eType == PDFWriter::TableData )
   11589             :                 {
   11590           0 :                     bInsert = true;
   11591             :                 }
   11592           0 :                 break;
   11593             :             case PDFWriter::Width:
   11594             :             case PDFWriter::Height:
   11595           0 :                 if( eType == PDFWriter::Figure      ||
   11596           0 :                     eType == PDFWriter::Formula     ||
   11597           0 :                     eType == PDFWriter::Form        ||
   11598           0 :                     eType == PDFWriter::Table       ||
   11599           0 :                     eType == PDFWriter::TableHeader ||
   11600             :                     eType == PDFWriter::TableData )
   11601             :                 {
   11602           0 :                     bInsert = true;
   11603             :                 }
   11604           0 :                 break;
   11605             :             case PDFWriter::LineHeight:
   11606             :             case PDFWriter::BaselineShift:
   11607             :                 // only for ILSE and BLSE
   11608           0 :                 if( eType == PDFWriter::Paragraph   ||
   11609           0 :                     eType == PDFWriter::Heading     ||
   11610           0 :                     eType == PDFWriter::H1          ||
   11611           0 :                     eType == PDFWriter::H2          ||
   11612           0 :                     eType == PDFWriter::H3          ||
   11613           0 :                     eType == PDFWriter::H4          ||
   11614           0 :                     eType == PDFWriter::H5          ||
   11615           0 :                     eType == PDFWriter::H6          ||
   11616           0 :                     eType == PDFWriter::List        ||
   11617           0 :                     eType == PDFWriter::ListItem    ||
   11618           0 :                     eType == PDFWriter::LILabel     ||
   11619           0 :                     eType == PDFWriter::LIBody      ||
   11620           0 :                     eType == PDFWriter::Table       ||
   11621           0 :                     eType == PDFWriter::TableRow    ||
   11622           0 :                     eType == PDFWriter::TableHeader ||
   11623           0 :                     eType == PDFWriter::TableData   ||
   11624           0 :                     eType == PDFWriter::Span        ||
   11625           0 :                     eType == PDFWriter::Quote       ||
   11626           0 :                     eType == PDFWriter::Note        ||
   11627           0 :                     eType == PDFWriter::Reference   ||
   11628           0 :                     eType == PDFWriter::BibEntry    ||
   11629           0 :                     eType == PDFWriter::Code        ||
   11630             :                     eType == PDFWriter::Link )
   11631             :                 {
   11632           0 :                         bInsert = true;
   11633             :                 }
   11634           0 :                 break;
   11635             :             case PDFWriter::RowSpan:
   11636             :             case PDFWriter::ColSpan:
   11637             :                 // only for table cells
   11638           0 :                 if( eType == PDFWriter::TableHeader ||
   11639             :                     eType == PDFWriter::TableData )
   11640             :                 {
   11641           0 :                     bInsert = true;
   11642             :                 }
   11643           0 :                 break;
   11644             :             case PDFWriter::LinkAnnotation:
   11645           0 :                 if( eType == PDFWriter::Link )
   11646           0 :                     bInsert = true;
   11647           0 :                 break;
   11648           0 :             default: break;
   11649             :         }
   11650             :     }
   11651             : 
   11652           0 :     if( bInsert )
   11653           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAttributes[ eAttr ] = PDFStructureAttribute( nValue );
   11654             : #if OSL_DEBUG_LEVEL > 1
   11655             :     else if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11656             :         fprintf( stderr, "rejecting setStructureAttributeNumerical( %s, %d ) on %s (%s) element\n",
   11657             :                  getAttributeTag( eAttr ),
   11658             :                  (int)nValue,
   11659             :                  getStructureTag( m_aStructure[ m_nCurrentStructElement ].m_eType ),
   11660             :                  m_aStructure[ m_nCurrentStructElement ].m_aAlias.getStr() );
   11661             : #endif
   11662             : 
   11663           0 :     return bInsert;
   11664             : }
   11665             : 
   11666           0 : void PDFWriterImpl::setStructureBoundingBox( const Rectangle& rRect )
   11667             : {
   11668           0 :     sal_Int32 nPageNr = m_nCurrentPage;
   11669           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() || !m_aContext.Tagged )
   11670           0 :         return;
   11671             : 
   11672           0 :     if( m_nCurrentStructElement > 0 && m_bEmitStructure )
   11673             :     {
   11674           0 :         PDFWriter::StructElement eType = m_aStructure[ m_nCurrentStructElement ].m_eType;
   11675           0 :         if( eType == PDFWriter::Figure      ||
   11676           0 :             eType == PDFWriter::Formula     ||
   11677           0 :             eType == PDFWriter::Form        ||
   11678             :             eType == PDFWriter::Table )
   11679             :         {
   11680           0 :             m_aStructure[ m_nCurrentStructElement ].m_aBBox = rRect;
   11681             :             // convert to default user space now, since the mapmode may change
   11682           0 :             m_aPages[nPageNr].convertRect( m_aStructure[ m_nCurrentStructElement ].m_aBBox );
   11683             :         }
   11684             :     }
   11685             : }
   11686             : 
   11687           0 : void PDFWriterImpl::setActualText( const OUString& rText )
   11688             : {
   11689           0 :     if( m_aContext.Tagged && m_nCurrentStructElement > 0 && m_bEmitStructure )
   11690             :     {
   11691           0 :         m_aStructure[ m_nCurrentStructElement ].m_aActualText = rText;
   11692             :     }
   11693           0 : }
   11694             : 
   11695           0 : void PDFWriterImpl::setAlternateText( const OUString& rText )
   11696             : {
   11697           0 :     if( m_aContext.Tagged && m_nCurrentStructElement > 0 && m_bEmitStructure )
   11698             :     {
   11699           0 :         m_aStructure[ m_nCurrentStructElement ].m_aAltText = rText;
   11700             :     }
   11701           0 : }
   11702             : 
   11703           0 : void PDFWriterImpl::setAutoAdvanceTime( sal_uInt32 nSeconds, sal_Int32 nPageNr )
   11704             : {
   11705           0 :     if( nPageNr < 0 )
   11706           0 :         nPageNr = m_nCurrentPage;
   11707             : 
   11708           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11709           0 :         return;
   11710             : 
   11711           0 :     m_aPages[ nPageNr ].m_nDuration = nSeconds;
   11712             : }
   11713             : 
   11714           0 : void PDFWriterImpl::setPageTransition( PDFWriter::PageTransition eType, sal_uInt32 nMilliSec, sal_Int32 nPageNr )
   11715             : {
   11716           0 :     if( nPageNr < 0 )
   11717           0 :         nPageNr = m_nCurrentPage;
   11718             : 
   11719           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11720           0 :         return;
   11721             : 
   11722           0 :     m_aPages[ nPageNr ].m_eTransition   = eType;
   11723           0 :     m_aPages[ nPageNr ].m_nTransTime    = nMilliSec;
   11724             : }
   11725             : 
   11726           0 : void PDFWriterImpl::ensureUniqueRadioOnValues()
   11727             : {
   11728             :     // loop over radio groups
   11729           0 :     for( std::map<sal_Int32,sal_Int32>::const_iterator group = m_aRadioGroupWidgets.begin();
   11730           0 :          group != m_aRadioGroupWidgets.end(); ++group )
   11731             :     {
   11732           0 :         PDFWidget& rGroupWidget = m_aWidgets[ group->second ];
   11733             :         // check whether all kids have a unique OnValue
   11734           0 :         boost::unordered_map< OUString, sal_Int32, OUStringHash > aOnValues;
   11735           0 :         int nChildren = rGroupWidget.m_aKidsIndex.size();
   11736           0 :         bool bIsUnique = true;
   11737           0 :         for( int nKid = 0; nKid < nChildren && bIsUnique; nKid++ )
   11738             :         {
   11739           0 :             int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11740           0 :             const OUString& rVal = m_aWidgets[nKidIndex].m_aOnValue;
   11741             :             #if OSL_DEBUG_LEVEL > 1
   11742             :             fprintf( stderr, "OnValue: %s\n", OUStringToOString( rVal, RTL_TEXTENCODING_UTF8 ).getStr() );
   11743             :             #endif
   11744           0 :             if( aOnValues.find( rVal ) == aOnValues.end() )
   11745             :             {
   11746           0 :                 aOnValues[ rVal ] = 1;
   11747             :             }
   11748             :             else
   11749             :             {
   11750           0 :                 bIsUnique = false;
   11751             :             }
   11752             :         }
   11753           0 :         if( ! bIsUnique )
   11754             :         {
   11755             :             #if OSL_DEBUG_LEVEL > 1
   11756             :             fprintf( stderr, "enforcing unique OnValues\n" );
   11757             :             #endif
   11758             :             // make unique by using ascending OnValues
   11759           0 :             for( int nKid = 0; nKid < nChildren; nKid++ )
   11760             :             {
   11761           0 :                 int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11762           0 :                 PDFWidget& rKid = m_aWidgets[nKidIndex];
   11763           0 :                 rKid.m_aOnValue = OUString::number( nKid+1 );
   11764           0 :                 if( rKid.m_aValue != "Off" )
   11765           0 :                     rKid.m_aValue = rKid.m_aOnValue;
   11766             :             }
   11767             :         }
   11768             :         // finally move the "Yes" appearance to the OnValue appearance
   11769           0 :         for( int nKid = 0; nKid < nChildren; nKid++ )
   11770             :         {
   11771           0 :             int nKidIndex = rGroupWidget.m_aKidsIndex[nKid];
   11772           0 :             PDFWidget& rKid = m_aWidgets[nKidIndex];
   11773           0 :             PDFAppearanceMap::iterator app_it = rKid.m_aAppearances.find( "N" );
   11774           0 :             if( app_it != rKid.m_aAppearances.end() )
   11775             :             {
   11776           0 :                 PDFAppearanceStreams::iterator stream_it = app_it->second.find( "Yes" );
   11777           0 :                 if( stream_it != app_it->second.end() )
   11778             :                 {
   11779           0 :                     SvMemoryStream* pStream = stream_it->second;
   11780           0 :                     app_it->second.erase( stream_it );
   11781           0 :                     OStringBuffer aBuf( rKid.m_aOnValue.getLength()*2 );
   11782           0 :                     appendName( rKid.m_aOnValue, aBuf );
   11783           0 :                     (app_it->second)[ aBuf.makeStringAndClear() ] = pStream;
   11784             :                 }
   11785             :                 #if OSL_DEBUG_LEVEL > 1
   11786             :                 else
   11787             :                     fprintf( stderr, "error: RadioButton without \"Yes\" stream\n" );
   11788             :                 #endif
   11789             :             }
   11790             :             // update selected radio button
   11791           0 :             if( rKid.m_aValue != "Off" )
   11792             :             {
   11793           0 :                 rGroupWidget.m_aValue = rKid.m_aValue;
   11794             :             }
   11795             :         }
   11796           0 :     }
   11797           0 : }
   11798             : 
   11799           0 : sal_Int32 PDFWriterImpl::findRadioGroupWidget( const PDFWriter::RadioButtonWidget& rBtn )
   11800             : {
   11801           0 :     sal_Int32 nRadioGroupWidget = -1;
   11802             : 
   11803           0 :     std::map< sal_Int32, sal_Int32 >::const_iterator it = m_aRadioGroupWidgets.find( rBtn.RadioGroup );
   11804             : 
   11805           0 :     if( it == m_aRadioGroupWidgets.end() )
   11806             :     {
   11807           0 :         m_aRadioGroupWidgets[ rBtn.RadioGroup ] = nRadioGroupWidget =
   11808           0 :             sal_Int32(m_aWidgets.size());
   11809             : 
   11810             :         // new group, insert the radiobutton
   11811           0 :         m_aWidgets.push_back( PDFWidget() );
   11812           0 :         m_aWidgets.back().m_nObject     = createObject();
   11813           0 :         m_aWidgets.back().m_nPage       = m_nCurrentPage;
   11814           0 :         m_aWidgets.back().m_eType       = PDFWriter::RadioButton;
   11815           0 :         m_aWidgets.back().m_nRadioGroup = rBtn.RadioGroup;
   11816           0 :         m_aWidgets.back().m_nFlags |= 0x0000C000;   // NoToggleToOff and Radio bits
   11817             : 
   11818           0 :         createWidgetFieldName( sal_Int32(m_aWidgets.size()-1), rBtn );
   11819             :     }
   11820             :     else
   11821           0 :         nRadioGroupWidget = it->second;
   11822             : 
   11823           0 :     return nRadioGroupWidget;
   11824             : }
   11825             : 
   11826           0 : sal_Int32 PDFWriterImpl::createControl( const PDFWriter::AnyWidget& rControl, sal_Int32 nPageNr )
   11827             : {
   11828           0 :     if( nPageNr < 0 )
   11829           0 :         nPageNr = m_nCurrentPage;
   11830             : 
   11831           0 :     if( nPageNr < 0 || nPageNr >= (sal_Int32)m_aPages.size() )
   11832           0 :         return -1;
   11833             : 
   11834           0 :     bool sigHidden(true);
   11835           0 :     sal_Int32 nNewWidget = m_aWidgets.size();
   11836           0 :     m_aWidgets.push_back( PDFWidget() );
   11837             : 
   11838           0 :     m_aWidgets.back().m_nObject         = createObject();
   11839           0 :     m_aWidgets.back().m_aRect           = rControl.Location;
   11840           0 :     m_aWidgets.back().m_nPage           = nPageNr;
   11841           0 :     m_aWidgets.back().m_eType           = rControl.getType();
   11842             : 
   11843           0 :     sal_Int32 nRadioGroupWidget = -1;
   11844             :     // for unknown reasons the radio buttons of a radio group must not have a
   11845             :     // field name, else the buttons are in fact check boxes -
   11846             :     // that is multiple buttons of the radio group can be selected
   11847           0 :     if( rControl.getType() == PDFWriter::RadioButton )
   11848           0 :         nRadioGroupWidget = findRadioGroupWidget( static_cast<const PDFWriter::RadioButtonWidget&>(rControl) );
   11849             :     else
   11850             :     {
   11851           0 :         createWidgetFieldName( nNewWidget, rControl );
   11852             :     }
   11853             : 
   11854             :     // caution: m_aWidgets must not be changed after here or rNewWidget may be invalid
   11855           0 :     PDFWidget& rNewWidget           = m_aWidgets[nNewWidget];
   11856           0 :     rNewWidget.m_aDescription       = rControl.Description;
   11857           0 :     rNewWidget.m_aText              = rControl.Text;
   11858             :     rNewWidget.m_nTextStyle         = rControl.TextStyle &
   11859             :         (  TEXT_DRAW_LEFT | TEXT_DRAW_CENTER | TEXT_DRAW_RIGHT | TEXT_DRAW_TOP |
   11860             :            TEXT_DRAW_VCENTER | TEXT_DRAW_BOTTOM |
   11861           0 :            TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK  );
   11862           0 :     rNewWidget.m_nTabOrder          = rControl.TabOrder;
   11863             : 
   11864             :     // various properties are set via the flags (/Ff) property of the field dict
   11865           0 :     if( rControl.ReadOnly )
   11866           0 :         rNewWidget.m_nFlags |= 1;
   11867           0 :     if( rControl.getType() == PDFWriter::PushButton )
   11868             :     {
   11869           0 :         const PDFWriter::PushButtonWidget& rBtn = static_cast<const PDFWriter::PushButtonWidget&>(rControl);
   11870           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11871             :             rNewWidget.m_nTextStyle =
   11872             :                 TEXT_DRAW_CENTER | TEXT_DRAW_VCENTER |
   11873           0 :                 TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11874             : 
   11875           0 :         rNewWidget.m_nFlags |= 0x00010000;
   11876           0 :         if( !rBtn.URL.isEmpty() )
   11877           0 :             rNewWidget.m_aListEntries.push_back( rBtn.URL );
   11878           0 :         rNewWidget.m_bSubmit    = rBtn.Submit;
   11879           0 :         rNewWidget.m_bSubmitGet = rBtn.SubmitGet;
   11880           0 :         rNewWidget.m_nDest      = rBtn.Dest;
   11881           0 :         createDefaultPushButtonAppearance( rNewWidget, rBtn );
   11882             :     }
   11883           0 :     else if( rControl.getType() == PDFWriter::RadioButton )
   11884             :     {
   11885           0 :         const PDFWriter::RadioButtonWidget& rBtn = static_cast<const PDFWriter::RadioButtonWidget&>(rControl);
   11886           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11887             :             rNewWidget.m_nTextStyle =
   11888           0 :                 TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11889             :         /*  PDF sees a RadioButton group as one radio button with
   11890             :          *  children which are in turn check boxes
   11891             :          *
   11892             :          *  so we need to create a radio button on demand for a new group
   11893             :          *  and insert a checkbox for each RadioButtonWidget as its child
   11894             :          */
   11895           0 :         rNewWidget.m_eType          = PDFWriter::CheckBox;
   11896           0 :         rNewWidget.m_nRadioGroup    = rBtn.RadioGroup;
   11897             : 
   11898             :         DBG_ASSERT( nRadioGroupWidget >= 0 && nRadioGroupWidget < (sal_Int32)m_aWidgets.size(), "no radio group parent" );
   11899             : 
   11900           0 :         PDFWidget& rRadioButton = m_aWidgets[nRadioGroupWidget];
   11901           0 :         rRadioButton.m_aKids.push_back( rNewWidget.m_nObject );
   11902           0 :         rRadioButton.m_aKidsIndex.push_back( nNewWidget );
   11903           0 :         rNewWidget.m_nParent = rRadioButton.m_nObject;
   11904             : 
   11905           0 :         rNewWidget.m_aValue     = "Off";
   11906           0 :         rNewWidget.m_aOnValue   = rBtn.OnValue;
   11907           0 :         if( rRadioButton.m_aValue.isEmpty() && rBtn.Selected )
   11908             :         {
   11909           0 :             rNewWidget.m_aValue     = rNewWidget.m_aOnValue;
   11910           0 :             rRadioButton.m_aValue   = rNewWidget.m_aOnValue;
   11911             :         }
   11912           0 :         createDefaultRadioButtonAppearance( rNewWidget, rBtn );
   11913             : 
   11914             :         // union rect of radio group
   11915           0 :         Rectangle aRect = rNewWidget.m_aRect;
   11916           0 :         m_aPages[ nPageNr ].convertRect( aRect );
   11917           0 :         rRadioButton.m_aRect.Union( aRect );
   11918             :     }
   11919           0 :     else if( rControl.getType() == PDFWriter::CheckBox )
   11920             :     {
   11921           0 :         const PDFWriter::CheckBoxWidget& rBox = static_cast<const PDFWriter::CheckBoxWidget&>(rControl);
   11922           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11923             :             rNewWidget.m_nTextStyle =
   11924           0 :                 TEXT_DRAW_VCENTER | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11925             : 
   11926           0 :         rNewWidget.m_aValue = rBox.Checked ? OUString("Yes") : OUString("Off" );
   11927             :         // create default appearance before m_aRect gets transformed
   11928           0 :         createDefaultCheckBoxAppearance( rNewWidget, rBox );
   11929             :     }
   11930           0 :     else if( rControl.getType() == PDFWriter::ListBox )
   11931             :     {
   11932           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11933           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_VCENTER;
   11934             : 
   11935           0 :         const PDFWriter::ListBoxWidget& rLstBox = static_cast<const PDFWriter::ListBoxWidget&>(rControl);
   11936           0 :         rNewWidget.m_aListEntries     = rLstBox.Entries;
   11937           0 :         rNewWidget.m_aSelectedEntries = rLstBox.SelectedEntries;
   11938           0 :         rNewWidget.m_aValue           = rLstBox.Text;
   11939           0 :         if( rLstBox.DropDown )
   11940           0 :             rNewWidget.m_nFlags |= 0x00020000;
   11941           0 :         if( rLstBox.Sort )
   11942           0 :             rNewWidget.m_nFlags |= 0x00080000;
   11943           0 :         if( rLstBox.MultiSelect && !rLstBox.DropDown && (int)m_aContext.Version > (int)PDFWriter::PDF_1_3 )
   11944           0 :             rNewWidget.m_nFlags |= 0x00200000;
   11945             : 
   11946           0 :         createDefaultListBoxAppearance( rNewWidget, rLstBox );
   11947             :     }
   11948           0 :     else if( rControl.getType() == PDFWriter::ComboBox )
   11949             :     {
   11950           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11951           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_VCENTER;
   11952             : 
   11953           0 :         const PDFWriter::ComboBoxWidget& rBox = static_cast<const PDFWriter::ComboBoxWidget&>(rControl);
   11954           0 :         rNewWidget.m_aValue         = rBox.Text;
   11955           0 :         rNewWidget.m_aListEntries   = rBox.Entries;
   11956           0 :         rNewWidget.m_nFlags |= 0x00060000; // combo and edit flag
   11957           0 :         if( rBox.Sort )
   11958           0 :             rNewWidget.m_nFlags |= 0x00080000;
   11959             : 
   11960           0 :         PDFWriter::ListBoxWidget aLBox;
   11961           0 :         aLBox.Name              = rBox.Name;
   11962           0 :         aLBox.Description       = rBox.Description;
   11963           0 :         aLBox.Text              = rBox.Text;
   11964           0 :         aLBox.TextStyle         = rBox.TextStyle;
   11965           0 :         aLBox.ReadOnly          = rBox.ReadOnly;
   11966           0 :         aLBox.Border            = rBox.Border;
   11967           0 :         aLBox.BorderColor       = rBox.BorderColor;
   11968           0 :         aLBox.Background        = rBox.Background;
   11969           0 :         aLBox.BackgroundColor   = rBox.BackgroundColor;
   11970           0 :         aLBox.TextFont          = rBox.TextFont;
   11971           0 :         aLBox.TextColor         = rBox.TextColor;
   11972           0 :         aLBox.DropDown          = true;
   11973           0 :         aLBox.Sort              = rBox.Sort;
   11974           0 :         aLBox.MultiSelect       = false;
   11975           0 :         aLBox.Entries           = rBox.Entries;
   11976             : 
   11977           0 :         createDefaultListBoxAppearance( rNewWidget, aLBox );
   11978             :     }
   11979           0 :     else if( rControl.getType() == PDFWriter::Edit )
   11980             :     {
   11981           0 :         if( rNewWidget.m_nTextStyle == 0 )
   11982           0 :             rNewWidget.m_nTextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER;
   11983             : 
   11984           0 :         const PDFWriter::EditWidget& rEdit = static_cast<const  PDFWriter::EditWidget&>(rControl);
   11985           0 :         if( rEdit.MultiLine )
   11986             :         {
   11987           0 :             rNewWidget.m_nFlags |= 0x00001000;
   11988           0 :             rNewWidget.m_nTextStyle |= TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK;
   11989             :         }
   11990           0 :         if( rEdit.Password )
   11991           0 :             rNewWidget.m_nFlags |= 0x00002000;
   11992           0 :         if( rEdit.FileSelect && m_aContext.Version > PDFWriter::PDF_1_3 )
   11993           0 :             rNewWidget.m_nFlags |= 0x00100000;
   11994           0 :         rNewWidget.m_nMaxLen = rEdit.MaxLen;
   11995           0 :         rNewWidget.m_aValue = rEdit.Text;
   11996             : 
   11997           0 :         createDefaultEditAppearance( rNewWidget, rEdit );
   11998             :     }
   11999             : #if !defined(ANDROID) && !defined(IOS)
   12000           0 :     else if( rControl.getType() == PDFWriter::Signature)
   12001             :     {
   12002           0 :         const PDFWriter::SignatureWidget& rSig = static_cast<const PDFWriter::SignatureWidget&>(rControl);
   12003           0 :         sigHidden = rSig.SigHidden;
   12004             : 
   12005           0 :         if ( sigHidden )
   12006           0 :             rNewWidget.m_aRect = Rectangle(0, 0, 0, 0);
   12007             : 
   12008           0 :         m_nSignatureObject = createObject();
   12009           0 :         rNewWidget.m_aValue = OUString::number( m_nSignatureObject );
   12010           0 :         rNewWidget.m_aValue += " 0 R";
   12011             :         // let's add a fake appearance
   12012           0 :         rNewWidget.m_aAppearances[ "N" ][ "Standard" ] = new SvMemoryStream();
   12013             :     }
   12014             : #endif
   12015             : 
   12016             :     // if control is a hidden signature, do not convert coordinates since we
   12017             :     // need /Rect [ 0 0 0 0 ]
   12018           0 :     if ( ! ( ( rControl.getType() == PDFWriter::Signature ) && ( sigHidden ) ) )
   12019             :     {
   12020             :         // convert to default user space now, since the mapmode may change
   12021             :         // note: create default appearances before m_aRect gets transformed
   12022           0 :         m_aPages[ nPageNr ].convertRect( rNewWidget.m_aRect );
   12023             :     }
   12024             : 
   12025             :     // insert widget to page's annotation list
   12026           0 :     m_aPages[ nPageNr ].m_aAnnotations.push_back( rNewWidget.m_nObject );
   12027             : 
   12028             :     // mark page as having widgets
   12029           0 :     m_aPages[ nPageNr ].m_bHasWidgets = true;
   12030             : 
   12031           0 :     return nNewWidget;
   12032             : }
   12033             : 
   12034           0 : void PDFWriterImpl::addStream( const OUString& rMimeType, PDFOutputStream* pStream, bool bCompress )
   12035             : {
   12036           0 :     if( pStream )
   12037             :     {
   12038           0 :         m_aAdditionalStreams.push_back( PDFAddStream() );
   12039           0 :         PDFAddStream& rStream = m_aAdditionalStreams.back();
   12040           0 :         rStream.m_aMimeType = !rMimeType.isEmpty()
   12041             :                               ? OUString( rMimeType )
   12042           0 :                               : OUString( "application/octet-stream"  );
   12043           0 :         rStream.m_pStream = pStream;
   12044           0 :         rStream.m_bCompress = bCompress;
   12045             :     }
   12046        1233 : }
   12047             : 
   12048             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10