summaryrefslogtreecommitdiff
path: root/vcl/unx/source/gdi/salgdi3.cxx
blob: 5b738f2a5de82c5bbb0286d4bc863124cb34fd80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
/*************************************************************************
 *
 *  $RCSfile: salgdi3.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000-09-18 17:05:43 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#define _SV_SALGDI3_CXX

// -=-= #includes =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <alloca.h>

#include <salunx.h>

#ifndef _SV_SALDATA_HXX
#include <saldata.hxx>
#endif
#ifndef _SV_SALDISP_HXX
#include <saldisp.hxx>
#endif
#ifndef _SV_SALGDI_HXX
#include <salgdi.hxx>
#endif
#ifndef _SV_OUTFONT_HXX
#include <outfont.hxx>
#endif
#ifndef _STRING_HXX
#include <tools/string.hxx>
#endif
#ifndef _SV_POLY_HXX
#include <poly.hxx>
#endif
#ifndef _RTL_TENCINFO_H
#include <rtl/tencinfo.h>
#endif

#include <tools/debug.hxx>
#include <tools/stream.hxx>

#ifndef PRINTER_DUMMY
#define Font XLIB_Font
#define Region XLIB_Region
#include <xprinter/xp.h>
#undef Font
#undef Region
#endif

#ifndef ANSI1252_HXX_
#include "ansi1252.hxx"
#endif
#ifndef XLFD_ATTRIBUTE_HXX
#include "xlfd_attr.hxx"
#endif
#ifndef XLFD_SIMPLE_HXX
#include "xlfd_smpl.hxx"
#endif
#ifndef XLFD_EXTENDED_HXX
#include "xlfd_extd.hxx"
#endif
#ifndef SAL_CONVERTER_CACHE_HXX_
#include "salcvt.hxx"
#endif
#include <osl/types.h>

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#ifndef PRINTER_DUMMY
static void
FaxPhoneComment( Display* pDisplay, const sal_Unicode* pStr, USHORT nLen )
{
    #define FAX_PHONE_TOKEN          "@@#"
    #define FAX_PHONE_TOKEN_LENGTH   3
    #define FAX_END_TOKEN            "@@"

    USHORT nPos;
    ByteString aPhone( pStr, nLen, gsl_getSystemTextEncoding() );

    static ByteString aPhoneNumber;
    static BOOL   bIsCollecting = FALSE;

    if( ! bIsCollecting )
    {
        if( ( nPos = aPhone.Search( FAX_PHONE_TOKEN ) ) != STRING_NOTFOUND )
        {
            aPhone.Erase( 0, nPos + FAX_PHONE_TOKEN_LENGTH );
            bIsCollecting = TRUE;
            aPhoneNumber.Erase();
        }
    }
    if( bIsCollecting )
    {
        if( ( nPos = aPhone.Search( FAX_END_TOKEN ) ) != STRING_NOTFOUND )
        {
            aPhone.Erase( nPos );
            bIsCollecting = FALSE;
        }
        aPhoneNumber += aPhone;
        if( ! bIsCollecting )
        {
            aPhone = "PhoneNumber(";
            aPhone += aPhoneNumber;
            aPhone += ")\n";
            XpPSComment( pDisplay, aPhone.GetBuffer() );
            aPhoneNumber = ByteString();
        }
    }
    if( aPhoneNumber.Len() > 1024 )
    {
        bIsCollecting = FALSE;
        aPhoneNumber = ByteString();
    }
}
#endif

// ----------------------------------------------------------------------------
//
// SalDisplay
//
// ----------------------------------------------------------------------------

XlfdStorage*
SalDisplay::GetXlfdList()
{
    if ( mpFontList != NULL )
    {
        return mpFontList;
    }
    else
    {
        // on a display an xlfd of *-0-0-75-75-* means this is a scalable
        // bitmap font, thus it is ugly and thus to avoid. On a printer
        // *-0-0-300-300-* means this is a printer resident font thus nice
        // thus to prefer :-(
        eDeviceT eDevice = this->IsDisplay() ? eDeviceDisplay : eDevicePrinter;

        mpFactory  = new AttributeProvider( eDevice );
        mpFontList = new XlfdStorage();

        int i, nFontCount;
        const int nMaxCount  = 64 * 1024 - 1;
        Display *pDisplay = GetDisplay();
        char **ppFontList = XListFonts(pDisplay, "-*", nMaxCount, &nFontCount);

        //
        // create a list of simple Xlfd font information
        //

        Xlfd  *pXlfdList = (Xlfd*)malloc( nFontCount * sizeof(Xlfd) );
        int    nXlfdCount = 0;

        for ( i = 0; i < nFontCount; i++ )
        {
            if ( pXlfdList[ nXlfdCount ].FromString(ppFontList[i], mpFactory) )
                ++nXlfdCount;
        }

        XFreeFontNames( ppFontList );

        // classification information is needed for sorting, classification
        // of charset (i.e. iso8859-1 <-> ansi-1252) depends on wether the
        // display points to a printer or to a real display. On a printer all
        // iso8859-1 fonts are really capable of ansi-1252
        mpFactory->AddClassification();
        // add some pretty print description
        mpFactory->AddAnnotation();
        // misc feature checking
        mpFactory->TagFeature();

        // sort according to font style
        qsort( pXlfdList, nXlfdCount, sizeof(Xlfd), XlfdCompare );

        //
        // create a font list with merged encoding information
        //

        BitmapXlfdStorage   aBitmapList;
        ScalableXlfd       *pScalableFont = NULL;
        PrinterFontXlfd    *pPrinterFont  = NULL;

        int nFrom = 0;
        for ( i = 0; i < nXlfdCount; i++ )
        {
            // exclude openlook glyph and cursor
            Attribute *pAttr = mpFactory->RetrieveFamily(pXlfdList[i].mnFamily);
            if ( pAttr->HasFeature(   XLFD_FEATURE_OL_GLYPH
                                    | XLFD_FEATURE_OL_CURSOR) )
            {
                continue;
            }
            // exclude fonts with unknown encoding
            if ( pXlfdList[i].GetEncoding() == RTL_TEXTENCODING_DONTKNOW )
            {
                continue;
            }

            Bool bSameOutline = pXlfdList[i].SameFontoutline(pXlfdList + nFrom);
            XlfdFonttype eType = pXlfdList[i].Fonttype();

            // flush the old merged font list if the name doesn't match any more
            if ( !bSameOutline )
            {
                mpFontList->Add( pScalableFont ); pScalableFont = NULL;
                mpFontList->Add( pPrinterFont );  pPrinterFont  = NULL;
                mpFontList->Add( &aBitmapList );  aBitmapList.Reset();
            }

            // merge the font or generate a new one
            switch( eType )
            {
                case eTypeScalable:

                    if ( pScalableFont == NULL )
                        pScalableFont = new ScalableXlfd;
                    pScalableFont->AddEncoding(pXlfdList + i);

                    break;

                case eTypeBitmap:

                    aBitmapList.AddBitmapFont( pXlfdList + i );

                    break;

                case eTypePrinterBuiltIn:
                case eTypePrinterDownload:

                    if ( pPrinterFont == NULL )
                        pPrinterFont = new PrinterFontXlfd;
                    pPrinterFont->AddEncoding( pXlfdList + i );

                    break;

                case eTypeScalableBitmap:
                default:

                    break;
            }

            nFrom = i;
        }

        // flush the merged list into the global list
        mpFontList->Add( pScalableFont );
        mpFontList->Add( pPrinterFont );
        mpFontList->Add( &aBitmapList );

        // cleanup the list of simple font information
        if ( pXlfdList != NULL )
            free( pXlfdList );

        return mpFontList;
    }
}

// ---------------------------------------------------------------------------

ExtendedFontStruct*
SalDisplay::GetFont( ExtendedXlfd *pRequestedFont, int nPixelSize )
{
    if( !pFontCache_ )
    {
        mpCvtCache = new SalConverterCache;
        pFontCache_ = new SalFontCache( 64, 64, 16 ); // ???
    }
    else
    {
        ExtendedFontStruct *pItem;
        for ( pItem  = pFontCache_->First();
              pItem != NULL;
              pItem  = pFontCache_->Next() )
        {
            if ( pItem->Match(pRequestedFont, nPixelSize) )
            {
                if( pFontCache_->GetCurPos() )
                {
                    pFontCache_->Remove( pItem );
                    pFontCache_->Insert( pItem, 0UL );
                }
                return pItem;
            }
        }
    }

    // before we expand the cache, we look for very old and unused items
    if( pFontCache_->Count() >= 64 )
    {
        ExtendedFontStruct *pItem;
        for ( pItem = pFontCache_->Last();
              pItem != NULL;
              pItem = pFontCache_->Prev() )
        {
            if( 1 == pItem->GetRefCount() )
            {
                pFontCache_->Remove( pItem );
                pItem->ReleaseRef();

                if( pFontCache_->Count() < 64 )
                    break;
            }
        }
    }

    ExtendedFontStruct *pItem = new ExtendedFontStruct( GetDisplay(),
                                    nPixelSize, pRequestedFont, mpCvtCache );
    pFontCache_->Insert( pItem, 0UL );
    pItem->AddRef();

    return pItem;
}

// ---------------------------------------------------------------------------

void
SalDisplay::DestroyFontCache()
{
    if( pFontCache_ )
    {
        ExtendedFontStruct *pItem = pFontCache_->First();
        while( pItem )
        {
            delete pItem;
            pItem = pFontCache_->Next();
        }
        delete pFontCache_;
    }
    if( mpFontList )
    {
        mpFontList->Dispose();
        delete mpFontList;
    }
    if ( mpFactory )
    {
        delete mpFactory;
    }
    if ( mpCvtCache )
    {
        delete mpCvtCache;
    }

    pFontCache_ = (SalFontCache*)NULL;
    mpFontList = (XlfdStorage*)NULL;
    mpFactory  = (AttributeProvider*)NULL;
    mpCvtCache = (SalConverterCache*)NULL;
}

// ----------------------------------------------------------------------------
//
// SalGraphicsData
//
// ----------------------------------------------------------------------------

GC
SalGraphicsData::SelectFont()
{
    Display *pDisplay = GetXDisplay();

    if( !pFontGC_ )
    {
        XGCValues values;
        values.subwindow_mode       = IncludeInferiors;
        values.fill_rule            = EvenOddRule;      // Pict import/ Gradient
        values.graphics_exposures   = True;
        values.foreground           = nTextPixel_;

        pFontGC_ = XCreateGC( pDisplay, hDrawable_,
                                GCSubwindowMode | GCFillRule
                              | GCGraphicsExposures | GCForeground,
                              &values );
    }

    if( !bFontGC_ )
    {
        XSetForeground( pDisplay, pFontGC_, nTextPixel_ );
        SetClipRegion( pFontGC_ );
        bFontGC_ = TRUE;
    }

    return pFontGC_;
}

//--------------------------------------------------------------------------

// Select the max size of a font, which is token for real
// This routine is (and should be) called only once, the result should be
// stored in some static variable

static int
GetMaxFontHeight()
{
    #define DEFAULT_MAXFONTHEIGHT 250

    int  nMaxFontHeight = 0;

    char *FontHeight = getenv ("SAL_MAXFONTHEIGHT");
    if (FontHeight)
        nMaxFontHeight = atoi (FontHeight);

    if (nMaxFontHeight <= 0)
        nMaxFontHeight = DEFAULT_MAXFONTHEIGHT;

    return nMaxFontHeight;
}

void
SalGraphicsData::SetFont( const ImplFontSelectData *pEntry )
{
    bFontGC_    = FALSE;
    xFont_      = NULL; // ->ReleaseRef()
    aScale_     = Fraction( 1, 1 );
    nFontOrientation_ = pEntry->mnOrientation;

    if( pEntry->mpFontData && pEntry->mpFontData->mpSysData )
    {
        ExtendedXlfd *pSysFont = (ExtendedXlfd*)pEntry->mpFontData->mpSysData;
        static int nMaxFontHeight = GetMaxFontHeight();

        USHORT         nH, nW;
        if( bWindow_ )
        {
            // see BugId #44528# FontWork (-> #45038#) and as well Bug #47127#
            if( pEntry->mnHeight > nMaxFontHeight )
                nH = nMaxFontHeight;
            else if( pEntry->mnHeight > 2 )
                nH = pEntry->mnHeight;
            else
                nH = 2;
            nW = 0; // pEntry->mnWidth;
        }
        else
        {
            nH = pEntry->mnHeight;
            nW = pEntry->mnWidth;
        }

        xFont_ = GetDisplay()->GetFont( pSysFont, nH );
        if( pEntry->mnHeight > nMaxFontHeight || pEntry->mnHeight < 2 )
            aScale_ = Fraction( pEntry->mnHeight, nH );
    }
    else
    {
        #ifdef DEBUG
        // XXX Fix me: provide a fallback for poor font installations
        // we may be reach this if no font matches the GUI font
        // MS Sans Serif;Geneva;Helv;WarpSans;Dialog;Lucida; ... */
        fprintf( stderr, "SalGraphicsData::SetFont: Invalid Font Selection\n" );
        #endif
    }
}

//--------------------------------------------------------------------------

static sal_Unicode
SwapBytes( const sal_Unicode nIn )
{
    return ((nIn >> 8) & 0x00ff) | ((nIn << 8) & 0xff00);
}


// draw string in a specific multibyte encoding
static void
ConvertTextItem16( XTextItem16* pTextItem,
        SalConverterCache* pCvt, rtl_TextEncoding nEncoding )
{
    if ( pTextItem && pTextItem->nchars > 0 )
    {
        // convert the string into the font encoding
        sal_Size  nSize;
        sal_Size  nBufferSize = pTextItem->nchars * 2;
        sal_Char *pBuffer = (sal_Char*)alloca( nBufferSize );

        nSize = ConvertStringUTF16( (sal_Unicode*)pTextItem->chars, pTextItem->nchars,
                        pBuffer, nBufferSize, pCvt->GetU2TConverter(nEncoding));

        sal_Char *pTextChars = (sal_Char*)pTextItem->chars;
        int n = 0, m = 0;

        if (   nEncoding == RTL_TEXTENCODING_GB_2312
            || nEncoding == RTL_TEXTENCODING_GBT_12345
            || nEncoding == RTL_TEXTENCODING_GBK
            || nEncoding == RTL_TEXTENCODING_BIG5 )
        {
            // GB and Big5 needs special treatment since chars can be single or
            // double byte: encoding is
            // [ 0x00 - 0x7f ] | [ 0x81 - 0xfe ] [ 0x40 - 0x7e 0x80 - 0xfe ]
            while ( n < nSize )
            {
                if ( (unsigned char)pBuffer[ n ] < 0x80 )
                {
                    pTextChars[ m++ ] = 0x0;
                    pTextChars[ m++ ] = pBuffer[ n++ ];
                }
                else
                {
                    pTextChars[ m++ ] = pBuffer[ n++ ];
                    pTextChars[ m++ ] = pBuffer[ n++ ];
                }
            }
            pTextItem->nchars = m / 2;
        }
        else
        if ( pCvt->IsSingleByteEncoding(nEncoding) )
        {
            // Single Byte encoding has to be padded
            while ( n < nSize )
            {
                pTextChars[ m++ ] = 0x0;
                pTextChars[ m++ ] = pBuffer[ n++ ];
            }
            pTextItem->nchars = nSize;
        }
        else
        {
            while ( n < nSize )
            {
                pTextChars[ m++ ] = pBuffer[ n++ ];
            }
            pTextItem->nchars = nSize / 2;
        }
    }
}

// XXX this is a hack since XPrinter is not multibyte capable
// XXX for printing this routine is called for each character
void
XPrinterDrawText16( Display* pDisplay, Drawable nDrawable, GC nGC,
        int nX, int nY, int nAngle, XTextItem16 *pTextItem16, int nItem )
{
    // convert XTextItem16 to XTextItem
    XTextItem *pTextItem = (XTextItem*)alloca( nItem * sizeof(XTextItem) );

    for ( int nCurItem = 0; nCurItem < nItem; nCurItem++ )
    {
        int      nChars      = pTextItem16[ nCurItem ].nchars;
        char*    pDstCharPtr = (char*)alloca( nChars * sizeof(char) );
        XChar2b* pSrcCharPtr = pTextItem16[ nCurItem ].chars;

        pTextItem[ nCurItem ].chars  = pDstCharPtr;
        pTextItem[ nCurItem ].nchars = nChars;
        pTextItem[ nCurItem ].delta  = pTextItem16[ nCurItem ].delta;
        pTextItem[ nCurItem ].font   = pTextItem16[ nCurItem ].font;

        for ( int nCurChar = 0;
              nCurChar < nChars;
              nCurChar++, pDstCharPtr++, pSrcCharPtr++ )
        {
            *pDstCharPtr = (char)pSrcCharPtr->byte2;
        }
    }

    if ( nAngle != 0 )
    {
        for ( int nCurItem = 0; nCurItem < nItem; nCurItem++ )
        {
            // XXX FIXME this is broken, because nX and nY is not sufficiently updated
            XSetFont( pDisplay, nGC, pTextItem[ nItem ].font );
            if ( XSalCanDrawRotString(pDisplay, nGC) )
            {
                XSalDrawRotString( pDisplay, nDrawable, nGC, nX, nY,
                    pTextItem[ nCurItem ].chars, pTextItem[ nCurItem ].nchars, nAngle );
            }
            else
            {
                XDrawString( pDisplay, nDrawable, nGC, nX, nY,
                    pTextItem[ nCurItem ].chars, pTextItem[ nCurItem ].nchars );
            }
        }
    }
    else
    {
        XDrawText( pDisplay, nDrawable, nGC, nX, nY, pTextItem, nItem );
    }
}

// draw string in one of the fonts / encodings that are available in the
// extended font
static void
DrawString( Display *pDisplay, Drawable nDrawable, GC nGC,
        int nX, int nY, const sal_Unicode *pStr, int nLength, int nAngle,
        SalConverterCache *pCvt, ExtendedFontStruct *pFont )
{
    // sanity check
    if ( pFont == NULL || nLength == 0 )
        return;

    rtl_TextEncoding nAsciiEnc = pFont->GetAsciiEncoding();

    if ( nAsciiEnc == RTL_TEXTENCODING_UNICODE )
    {
        // plain Unicode, can handle all chars and can be handled straight forward
        XFontStruct* pFontStruct = pFont->GetFontStruct( nAsciiEnc );

        if ( pFontStruct == NULL )
            return;

        XSetFont( pDisplay, nGC, pFontStruct->fid );

        #ifdef OSL_LITENDIAN
        sal_Unicode *pBuffer = (sal_Unicode*)alloca( nLength * sizeof(sal_Unicode) );
        for ( int i = 0; i < nLength; i++ )
            pBuffer[ i ] = SwapBytes(pStr[ i ]) ;
        #else
        sal_Unicode *pBuffer = const_cast<sal_Unicode*>(pStr);
        #endif

        XDrawString16( pDisplay, nDrawable, nGC, nX, nY, (XChar2b*)pBuffer, nLength );
    }
    else
    {
        // convert the string to a XTextItem16 with each item chars having the
        // encoding matching the font of fontid
        XTextItem16 *pTextItem = (XTextItem16*)alloca( nLength * sizeof(XTextItem16) );
        XChar2b *pMBChar = (XChar2b*)alloca( nLength * sizeof(XChar2b) );
        memcpy( pMBChar, pStr, nLength * sizeof(XChar2b) );

        rtl_TextEncoding nEncoding   = nAsciiEnc;
        XFontStruct*     pFontStruct = pFont->GetFontStruct( nEncoding );

        if ( pFontStruct == NULL )
            return;

        for ( int nChar = 0, nItem = -1; nChar < nLength; nChar++ )
        {
            rtl_TextEncoding nOldEnc = nEncoding;
            pFont->GetFontStruct( pStr[nChar], &nEncoding, &pFontStruct, pCvt );

            if ( (nItem != -1) && (pFontStruct->fid == pTextItem[ nItem ].font) )
            {
                pTextItem[ nItem ].nchars += 1;
            }
            else
            {
                if ( nItem != -1 )
                    ConvertTextItem16( &pTextItem[ nItem ], pCvt, nOldEnc );

                ++nItem;

                pTextItem[ nItem ].chars  = pMBChar + nChar;
                pTextItem[ nItem ].delta  = 0;
                pTextItem[ nItem ].font   = pFontStruct->fid;
                pTextItem[ nItem ].nchars = 1;
            }
        }
        ConvertTextItem16( &pTextItem[ nItem ], pCvt, nEncoding );
        ++nItem;

        if ( XSalIsDisplay( pDisplay ) )
            XDrawText16( pDisplay, nDrawable, nGC, nX, nY, pTextItem, nItem );
        else
            XPrinterDrawText16( pDisplay, nDrawable, nGC, nX, nY, nAngle,
                    pTextItem, nItem );
    }
}

//--------------------------------------------------------------------------

void
SalGraphicsData::DrawText( long nX, long nY,
        const sal_Unicode *pStr, USHORT nLen )
{
#ifndef PRINTER_DUMMY
    if( bPrinter_ )
        FaxPhoneComment( GetXDisplay(), pStr, nLen );
#endif

    #ifdef __notdef__
    // XXX Fix me this part is not unicode / multibyte aware

    // Bug: #45670#
    // some monospace ISO8859-1 fonts have a hole between chars 128 and 159
    // some applications assume these characters have also the default width
    if( ! bPrinter_                                 &&
        PITCH_FIXED == xFont_->GetFont()->mePitch   &&
        nLen > 1 )
    {
        XFontStruct *pXFS   = GetFontInfo();
        long         nWidth = xFont_->GetDim()->GetWidth();

        if( xFont_->GetFixedWidth() != nWidth
            || xFont_->GetDefaultWidth() != nWidth )
        {
            unsigned int min_char   = pXFS->min_char_or_byte2;
            unsigned int max_char   = pXFS->max_char_or_byte2;
            XCharStruct *pXCS       = pXFS->per_char - min_char;

            for( USHORT i = 0; i < nLen-1; i++ )
            {
                unsigned int c = ((unsigned char*)pStr)[i];

                long nW = c < min_char || c > max_char || ! pXFS->per_char
                    ? xFont_->GetDefaultWidth()
                    : pXCS[c].width;

                if( nW != nWidth )
                {
                    long *pDXAry = new long[nLen];

                    for( i = 0; i < nLen; i++ )
                        pDXAry[i] = nWidth * (i+1);

                    DrawText( nX, nY, pStr, nLen, pDXAry );

                    delete pDXAry;

                    return;
                }
            }
        }
    }

    #endif /* __notdef__ */

    Display             *pDisplay = GetXDisplay();
    SalConverterCache   *pCvt     = GetDisplay()->GetConverter();
    GC                  pGC       = SelectFont();

    DrawString( pDisplay, hDrawable_, pGC, nX, nY,
            pStr, nLen, nFontOrientation_ * 64 / 10, pCvt, xFont_ );
}

void
SalGraphics::DrawText( long nX, long nY, const xub_Unicode* pStr, USHORT nLen )
{
    maGraphicsData.DrawText( nX, nY, pStr, nLen );
}

//--------------------------------------------------------------------------

static BOOL
CheckNoNegativeCoordinateWorkaround()
{
    /* Motivation: one of our clients uses a Solaris 2.4 X86 system with an
       XServer for the Matrox Mystique graphics card. This board/server
       sometimes does not draw Text with negative x-coordinates into a
       virtual device (for unknown reasons). A stock X-server just clips the
       part in the negative area. */
    static int nCheck = -2;
    if( nCheck == -2 )
    {
        char* pCmp = getenv( "SAL_NO_NEGATIVE_TEXT_OFFSET" );
        if( pCmp && ! strncasecmp( pCmp, "true", 4 ) )
            nCheck = 1;
        else
            nCheck = 0;
    }
    return nCheck ? TRUE : FALSE;
}

void
SalGraphicsData::DrawText(
        long nX, long nY,
        const sal_Unicode* pStr, USHORT nLen, const long* pDXAry )
{
    #ifndef PRINTER_DUMMY
    if( bPrinter_ )
        FaxPhoneComment( GetXDisplay(), pStr, nLen );
    #endif
    GC pGC = SelectFont();

    // workaround for problems with negative coordinates
    long* pTmpAry = NULL;
    if( nX < 0 && CheckNoNegativeCoordinateWorkaround() )
    {
        long nOldX = nX;
        while( nX < 0 )
        {
            nX = nOldX + *pDXAry;
            pStr++, pDXAry++, nLen--;
            if( nLen < 1 )
                return;
        }
        pTmpAry = new long[ nLen ];
        for( int q = 0; q < nLen-1; q++ )
            pTmpAry[q] = pDXAry[q] - ( nX - nOldX );
        pDXAry = pTmpAry;
    }

    // draw every single character
    SalConverterCache *pCvt = GetDisplay()->GetConverter();
    int angle = nFontOrientation_ * 64 / 10;
    Polygon aPolygon(1);
    Point   aOrigin( nX, nY );
    Point   aCharPos;

    DrawString( GetXDisplay(), hDrawable_, pGC,
            aOrigin.X(), aOrigin.Y(), pStr, 1, angle, pCvt, xFont_ );

    for( int i = 1; i < nLen ; i++ )
    {
        aCharPos = Point( aOrigin.X() + pDXAry[ i - 1 ], aOrigin.Y() );
        aPolygon.SetPoint( aCharPos, 0 );
        aPolygon.Rotate( aOrigin, nFontOrientation_ );
        aCharPos = aPolygon.GetPoint( 0 );

        DrawString( GetXDisplay(), hDrawable_, pGC,
                aCharPos.X(), aCharPos.Y(), pStr + i, 1, angle, pCvt, xFont_ );
    }

    if( pTmpAry )
        delete pTmpAry;
}

// ----------------------------------------------------------------------------
//
// SalGraphics
//
// ----------------------------------------------------------------------------

USHORT
SalGraphics::SetFont( ImplFontSelectData *pEntry )
{
    maGraphicsData.SetFont( pEntry );
    return _IsPrinter() ? SAL_SETFONT_USEDRAWTEXTARRAY : 0;
}

// ----------------------------------------------------------------------------

void
SalGraphics::DrawTextArray( long nX, long nY,
        const xub_Unicode *pStr, USHORT nLen, const long *pDXAry )
{
    maGraphicsData.DrawText( nX, nY, pStr, nLen, pDXAry );
}

// ----------------------------------------------------------------------------

void
SalGraphics::SetTextColor( SalColor nSalColor )
{
    if( _GetTextColor() != nSalColor )
    {
        _GetTextColor()     = nSalColor;
        _GetTextPixel()     = _GetPixel( nSalColor );
        _IsFontGC()         = FALSE;
    }
}

// ----------------------------------------------------------------------------

void
SalGraphics::GetDevFontList( ImplDevFontList *pList )
{
    XlfdStorage* pFonts = _GetDisplay()->GetXlfdList();

    for ( int nIdx = 0; nIdx < pFonts->GetCount(); nIdx++ )
    {
        ImplFontData *pFontData = new ImplFontData;
        pFonts->Get(nIdx)->ToImplFontData( pFontData );
        pList->Add( pFontData );
    }
}

// ----------------------------------------------------------------------------

inline long
sal_DivideNeg( long n1, long n2 )
{
    return ( n1 < 0 ) ? (n1 - n2 / 2) / n2 : (n1 + n2 / 2) / n2;
}

void
SalGraphics::GetFontMetric( ImplFontMetricData *pMetric )
{
    ExtendedFontStruct* pFont = maGraphicsData.xFont_;

    if ( pFont != NULL )
    {
        pFont->ToImplFontMetricData( pMetric );

        if( XSalCanDrawRotString( maGraphicsData.GetXDisplay(), None ) )
            pMetric->mnOrientation = maGraphicsData.nFontOrientation_;

        long n;

        n = maGraphicsData.aScale_.GetNumerator();
        if( n != 1 )
        {
            pMetric->mnWidth    *= n;
            pMetric->mnAscent   *= n;
            pMetric->mnDescent  *= n;
            pMetric->mnLeading  *= n;
            pMetric->mnSlant    *= n;
        }

        n = maGraphicsData.aScale_.GetDenominator();
        if( n != 1 )
        {
            pMetric->mnWidth    = Divide( pMetric->mnWidth, n );
            pMetric->mnAscent   = sal_DivideNeg( pMetric->mnAscent,  n );
            pMetric->mnDescent  = sal_DivideNeg( pMetric->mnDescent, n );
            pMetric->mnLeading  = sal_DivideNeg( pMetric->mnLeading, n );
            pMetric->mnSlant    = sal_DivideNeg( pMetric->mnSlant,   n );
        }
    }
}

// ---------------------------------------------------------------------------

static long
InitializeWidthArray( long *pWidthArray, sal_Size nItems, int nValue = 0  )
{
    const long nPrecision = 1;

    for ( int i = 0; i < nItems; i++, pWidthArray++ )
        *pWidthArray = nValue;

    return nPrecision;
}

long
SalGraphics::GetCharWidth( USHORT nChar1, USHORT nChar2, long  *pWidthAry )
{
    // return the precision of the calculated charwidth, e.g. 1000 = 3 digits
    // defaultet to 1 for now
    const long nPrecision = 1;
    int nRequestedWidth = nChar2 - nChar1 + 1;
    int nCharWidth;

    // XXX sanity check, this may happen if no font at all is installed
    // or no system font matches the requirements for the user interface
    if ( maGraphicsData.xFont_ == NULL )
        return InitializeWidthArray( pWidthAry, nRequestedWidth, 12 );

    // the font should know it's metrics best
    SalDisplay *pSalDisplay = maGraphicsData.GetDisplay();

    nCharWidth = maGraphicsData.xFont_->GetCharWidth(
            pSalDisplay->GetConverter(), nChar1, nChar2, pWidthAry );

    // XXX sanity check, this may happen if the font cannot be loaded/queried
    // either because of a garbled fontpath or because of invalid fontfile
    if ( nCharWidth != nRequestedWidth )
        InitializeWidthArray( pWidthAry + nCharWidth,
                nRequestedWidth - nCharWidth );

    // handle internal scaling
    const long nNumerator   = maGraphicsData.aScale_.GetNumerator();
    const long nDenominator = maGraphicsData.aScale_.GetDenominator();
    long *pPtr;
    sal_Unicode nChar;

    if ( nNumerator != 1 )
        for( pPtr = pWidthAry, nChar = nChar1; nChar <= nChar2; nChar++, pPtr++)
            *pPtr *= nNumerator;
    if ( nDenominator != 1 )
        for( pPtr = pWidthAry, nChar = nChar1; nChar <= nChar2; nChar++, pPtr++)
            *pPtr = Divide( *pPtr, nDenominator );

    // return
    return nPrecision;
}

// ---------------------------------------------------------------------------

extern unsigned char TranslateCharName( char* );

ULONG
SalGraphics::GetKernPairs( ULONG nPairs, ImplKernPairData *pKernPairs )
{
    if( ! _IsPrinter() )
        return 0;

    // get pair kerning table ( internal data from xprinter )
    int i, nCurPair=0;

    // XXX Fix me, improve this to be multi encoding aware: merge kern
    // pair list for all encodings available in the xfont
    rtl_TextEncoding nEncoding = maGraphicsData.xFont_->GetAsciiEncoding();
    XFontStruct *pXFS = maGraphicsData.xFont_->GetFontStruct( nEncoding );
    XExtData    *pXES = pXFS->ext_data;

    for( i = 0; pXES && i < 2; i++, pXES = pXES->next );
    if( i < 2 )
        return 0;
    XpPairKernData* pXpPKD = (XpPairKernData*)(pXES->private_data);
    PairKernData*   pPKD   = pXpPKD->pkd;

    for( i = 0, nCurPair=0; i < pXpPKD->numOfPairs; i++ )
    {
        unsigned char c1 = TranslateCharName( pPKD[i].name1 );
        unsigned char c2 = TranslateCharName( pPKD[i].name2 );
        if( c1 && c2 )
        {
            if( pKernPairs && nCurPair < nPairs )
            {
                pKernPairs[ nCurPair ].mnChar1 = c1;
                pKernPairs[ nCurPair ].mnChar2 = c2;
                pKernPairs[ nCurPair ].mnKern =
                        (long)(pPKD[i].xamt * pXpPKD->pointsize / 1000 );
            }
            nCurPair++;
        }
    }

    return nCurPair;
}

// ---------------------------------------------------------------------------

BOOL
SalGraphics::GetGlyphBoundRect( xub_Unicode c,
        long *pX, long *pY, long *pDX, long *pDY )
{
    return FALSE;
}

// ---------------------------------------------------------------------------

ULONG
SalGraphics::GetGlyphOutline( xub_Unicode c,
        USHORT **ppPolySizes, SalPoint **ppPoints, BYTE **ppFlags )
{
    return 0;
}