summaryrefslogtreecommitdiff
path: root/lotuswordpro/source/filter/lwpgrfobj.cxx
blob: 83a3a1f3b9cf385ddcfd83751daa59f82b60cdff (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 *  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: IBM Corporation
 *
 *  Copyright: 2008 by IBM Corporation
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
/*****************************************************************************
* Change History
* 2005/2  draft code for chart
****************************************************************************/

/**
 * @file
 *  For LWP filter architecture prototype
*/
#include <stdio.h>
#include "lwp9reader.hxx"
#include "lwpgrfobj.hxx"
#include "lwpsdwfileloader.hxx"
#include "bento.hxx"

#include "lwpglobalmgr.hxx"
#include "xfilter/xfframe.hxx"
#include "xfilter/xfimage.hxx"
#include "xfilter/xfimagestyle.hxx"
#include "xfilter/xfstylemanager.hxx"
#include "xfilter/xfparagraph.hxx"
#include "xfilter/xfannotation.hxx"

//For chart
#include "string.h"

#ifndef _OSL_THREAD_H_
#include <osl/thread.h>
#endif

#define EF_NONE 0x0000
#define EF_FTP 0x0001
#define EF_ODMA 0x0002
#define EF_NOS 0x0003

LwpGraphicObject::LwpGraphicObject(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
    : LwpGraphicOleObject(objHdr, pStrm)
    , m_nCachedBaseLine(0)
    , m_bIsLinked(0)
    , m_bCompressed(0)
{
    memset(m_sDataFormat, 0, sizeof(m_sDataFormat));
    memset(m_sServerContextFormat, 0, sizeof(m_sServerContextFormat));
}
LwpGraphicObject::~LwpGraphicObject()
{
    m_vXFDrawObjects.clear();
}
void LwpGraphicObject::Read()
{
    LwpGraphicOleObject::Read();
    m_pObjStrm->QuickReaduInt16(); //disksize
    sal_uInt16 strsize = m_pObjStrm->QuickReaduInt16();
    if (strsize<AFID_MAX_FILE_FORMAT_SIZE)
    {
        m_pObjStrm->QuickRead(m_sDataFormat,strsize);
        m_sDataFormat[strsize] = '\0';
    }
/*
    if (m_sDataFormat!=(unsigned char*)(".lch"))
    {
        return;
    }
*/
    sal_uInt32 nServerContextSize = m_pObjStrm->QuickReaduInt32();
    unsigned char *pServerContext = NULL;
    if (nServerContextSize > 0)
    {
        pServerContext = new unsigned char[nServerContextSize];
        m_pObjStrm->QuickRead(pServerContext, static_cast<sal_uInt16>(nServerContextSize));
        // add by , 04/05/2005
        if (nServerContextSize > 44)
        {
            m_aIPData.nBrightness = pServerContext[14];
            m_aIPData.nContrast = pServerContext[19];
            m_aIPData.nEdgeEnchancement = pServerContext[24];
            m_aIPData.nSmoothing = pServerContext[29];
            m_aIPData.bInvertImage = (sal_Bool)(pServerContext[34] == 0x01);
            m_aIPData.bAutoContrast = (sal_Bool)(pServerContext[44] == 0x00);
        }
        // end add
    }
    m_pObjStrm->QuickReaduInt16(); //disksize
    strsize = m_pObjStrm->QuickReaduInt16();
    if (strsize<AFID_MAX_FILE_FORMAT_SIZE)
    {
        m_pObjStrm->QuickRead(m_sServerContextFormat,strsize);
        m_sServerContextFormat[strsize] = '\0';
    }
    if (nServerContextSize == 0)
    {
        if (strcmp((char *)m_sServerContextFormat, ".cht") == 0 &&
            strcmp((char *)m_sDataFormat, ".sdw") == 0)
        {
            strcpy((char *)m_sServerContextFormat, ".lch");
            strcpy((char *)m_sDataFormat, ".lch");
        }
    }
    m_nCachedBaseLine = m_pObjStrm->QuickReadInt32();
    m_bIsLinked = m_pObjStrm->QuickReadInt16();
    //OUString pLinkedFilePath;
    unsigned char * pFilterContext = NULL;
    sal_uInt32 nFilterContextSize = 0;

    if (m_bIsLinked)
    {
        m_LinkedFilePath = m_pObjStrm->QuickReadStringPtr();

        nFilterContextSize = m_pObjStrm->QuickReaduInt32();
        if (nFilterContextSize > 0)
        {
            pFilterContext = new unsigned char[nFilterContextSize];
            m_pObjStrm->QuickRead(pFilterContext, static_cast<sal_uInt16>(nFilterContextSize));
        }
        if (LwpFileHeader::m_nFileRevision >= 0x000b)
        {

            // read external file object stuff
            sal_uInt16 type = m_pObjStrm->QuickReaduInt16();
//          if (EF_ODMA == type)
//              ;
            if ((EF_ODMA != type) && (EF_NONE != type)) // don't know about this
            {
                sal_uInt32 size = m_pObjStrm->QuickReaduInt32();
                m_pObjStrm->SeekRel(static_cast<sal_uInt16>(size));
            }
            // else no external file object
        }
    }

    if (LwpFileHeader::m_nFileRevision >= 0x000b)
    {
        m_bCompressed = m_pObjStrm->QuickReadInt16();
        m_Cache.LinkedFileSize = m_pObjStrm->QuickReaduInt32();
        m_Cache.LinkedFileTime = m_pObjStrm->QuickReaduInt32();
        m_Cache.Width = m_pObjStrm->QuickReadInt32();
        m_Cache.Height = m_pObjStrm->QuickReadInt32();
    }

    if(LwpFileHeader::m_nFileRevision >= 0x000c)
    {
        m_WatermarkName = m_pObjStrm->QuickReadStringPtr();
    }

    if (pServerContext != NULL)
        delete[] pServerContext;

    if (pFilterContext != NULL)
        delete[] pFilterContext;

}

void LwpGraphicObject::XFConvert (XFContentContainer* pCont)
{
    if ((m_sServerContextFormat[1]=='s'&&m_sServerContextFormat[2]=='d'&&m_sServerContextFormat[3]=='w'))
    {
        //XFParagraph* pPara = new XFParagraph();
        std::vector <XFFrame*>::iterator iter;
        for (iter = m_vXFDrawObjects.begin(); iter != m_vXFDrawObjects.end(); ++iter)
        {
            //pPara->Add(*iter);
            pCont->Add(*iter);
        }

        //pCont->Add(pPara);

    }
    else if (this->IsGrafFormatValid())
    {
        XFImage* pImage = static_cast<XFImage*>(m_vXFDrawObjects.front());

        if (m_bIsLinked)
        {
            OUString fileURL = LwpTools::convertToFileUrl(OUStringToOString(m_LinkedFilePath, osl_getThreadTextEncoding()));
            pImage->SetFileURL(fileURL);
        }
        else
        {
            sal_uInt8* pGrafData = NULL;
            sal_uInt32 nDataLen = this->GetRawGrafData(pGrafData);

            if (pGrafData)
            {
                pImage->SetImageData(pGrafData, nDataLen);

                // delete used image data
                delete [] pGrafData;
                pGrafData = NULL;
            }
        }

        pCont->Add(pImage);
    }
    else if((m_sServerContextFormat[1]=='t'&&m_sServerContextFormat[2]=='e'&&m_sServerContextFormat[3]=='x'))
    {
        XFConvertEquation(pCont);
    }
    if (m_sServerContextFormat[1]=='l'&&m_sServerContextFormat[2]=='c'&&m_sServerContextFormat[3]=='h')
    {
        //LwpSvStream* pDocStream = m_pStrm;
        //LwpChartStreamTools::ParseChart(pDocStream, GetObjectID(),
        //                              GetRectIn100thMM(), GetRectInCM(), pOutputStream);
        //LwpChartStreamTools::ParseChart(pDocStream, GetObjectID(),
        //                              GetRectIn100thMM(), GetRectInCM(), pCont, m_strStyleName);
    }
}

/**
* @short   Get the rectangle of a chart in 100thMM
* @descr
* @return the rectangle of the chart
*/
Rectangle LwpGraphicObject::GetRectIn100thMM()
{
#define To100thMM(num) (long)(2540* (double(num)/(72 * 65536L)))
    sal_Int32 nLeft,nTop,nRight,nBottom;
    GetRect(nLeft,nTop,nRight,nBottom);
    return Rectangle( To100thMM(nLeft),To100thMM(nTop),To100thMM(nRight),To100thMM(nBottom) );
}

/**
* @short   Get the rectangle of a chart in CM
* @descr
* @return   The rectangle of the chart
*/
XFRect LwpGraphicObject::GetRectInCM()
{
#define ToCM(num) (2.54*(double(num)/(72 * 65536L)))
    sal_Int32 nLeft,nTop,nRight,nBottom;
    GetRect(nLeft,nTop,nRight,nBottom);
    return XFRect( ToCM(nLeft),ToCM(nTop),ToCM(nRight-nLeft),ToCM(nBottom-nTop) );
}

/**
* @short   Get the rectangle of a chart
* @descr
* @param   nLeft
* @param   nTop
* @param   nRight
* @param   nBottom
*/
#include "lwpframelayout.hxx"
void LwpGraphicObject::GetRect(sal_Int32& nLeft, sal_Int32& nTop, sal_Int32& nRight, sal_Int32& nBottom)
{
    nLeft = nTop = nRight = nBottom = 0;

    LwpObjectID* pObjID = GetLayoutsWithMe()->GetOnlyLayout();
    if (pObjID)
    {
        LwpFrameLayout* pLayout = (LwpFrameLayout*)pObjID->obj();
        if (pLayout)
        {
            LwpLayoutGeometry* pGeometry =(LwpLayoutGeometry*) pLayout->GetGeometry();

            if (pGeometry)
            {
                double fWidth =0;
                double fHeight = 0;
                GetGrafScaledSize(fWidth, fHeight);

                sal_Int32 nWidth = fWidth * UNITS_PER_INCH /CM_PER_INCH;
                sal_Int32 nHeight = fHeight * UNITS_PER_INCH /CM_PER_INCH;

                nLeft = pLayout->GetMarginsValue(MARGIN_LEFT) * UNITS_PER_INCH /CM_PER_INCH;
                nTop = pLayout->GetMarginsValue(MARGIN_TOP)* UNITS_PER_INCH /CM_PER_INCH;
                nRight = nLeft+nWidth;
                nBottom = nTop+nHeight;
            }
        }
    }
}

/**
 * @descr   judge if the graphic format is what we can support: bmp, jpg, wmf, gif, tgf(tif). other format will be filtered to
 *  these formats by Word Pro.
 * @return  sal_True if yes sal_False if not.
 */
sal_Bool LwpGraphicObject::IsGrafFormatValid()
{
    if ((m_sServerContextFormat[1]=='b'&& m_sServerContextFormat[2]=='m' && m_sServerContextFormat[3]=='p')
    || (m_sServerContextFormat[1]=='j' && m_sServerContextFormat[2]=='p' && m_sServerContextFormat[3]=='g')
    || (m_sServerContextFormat[1]=='w' && m_sServerContextFormat[2]=='m' && m_sServerContextFormat[3]=='f')
    || (m_sServerContextFormat[1]=='g' && m_sServerContextFormat[2]=='i' && m_sServerContextFormat[3]=='f')
    || (m_sServerContextFormat[1]=='t' && m_sServerContextFormat[2]=='g' && m_sServerContextFormat[3]=='f')
    || (m_sServerContextFormat[1]=='p' && m_sServerContextFormat[2]=='n' && m_sServerContextFormat[3]=='g')
    || (m_sServerContextFormat[1]=='e' && m_sServerContextFormat[2]=='p' && m_sServerContextFormat[3]=='s'))
    {
        return sal_True;
    }
    else
    {
        return sal_False;
    }
}

/**
 * @descr   create drawing object and image object.
 */
void LwpGraphicObject::RegisterStyle()
{
    if (m_sServerContextFormat[1]=='s'&&m_sServerContextFormat[2]=='d'&&m_sServerContextFormat[3]=='w')
    {
        this->CreateDrawObjects();
    }
    // test codes for importing pictures
    else if(this->IsGrafFormatValid())
    {
        this->CreateGrafObject();
    }

    if (m_sServerContextFormat[1]=='l'&&m_sServerContextFormat[2]=='c'&&m_sServerContextFormat[3]=='h')
    {
        LwpVirtualLayout* pMyLayout = GetLayout(NULL);
        if(pMyLayout->IsFrame())
        {
            XFFrameStyle* pXFFrameStyle = new XFFrameStyle();
            pXFFrameStyle->SetXPosType(enumXFFrameXPosFromLeft, enumXFFrameXRelFrame);
            pXFFrameStyle->SetYPosType(enumXFFrameYPosFromTop, enumXFFrameYRelPara);
            XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
            m_strStyleName = pXFStyleManager->AddStyle(pXFFrameStyle)->GetStyleName();
        }
    }

}

// add by , 03/25/2005
/**
 * @descr   create drawing object.
 */
void LwpGraphicObject::CreateDrawObjects()
{
    // if small file, use the compressed stream for BENTO
    LwpSvStream* pStream = m_pStrm->GetCompressedStream() ?  m_pStrm->GetCompressedStream(): m_pStrm;

    OpenStormBento::LtcBenContainer* pBentoContainer;
    sal_uLong ulRet = OpenStormBento::BenOpenContainer(pStream, &pBentoContainer);
    if (ulRet != OpenStormBento::BenErr_OK)
        return;

    SvStream* pDrawObjStream = NULL;

    // get graphic object's bento objet name
    LwpObjectID* pMyID = this->GetObjectID();
    std::string aGrfObjName;
    this->GetBentoNamebyID(pMyID,  aGrfObjName);

    // get bento stream by the name
    pBentoContainer->CreateGraphicStream(pDrawObjStream, aGrfObjName.c_str());
    if (pDrawObjStream)
    {
        LwpSdwFileLoader fileLoader(pDrawObjStream, this);
        fileLoader.CreateDrawObjects(&m_vXFDrawObjects);

        delete pDrawObjStream;
        pDrawObjStream = NULL;
    }
}

/**
 * @descr   create drawing object.
 */
void LwpGraphicObject::GetBentoNamebyID(LwpObjectID* pMyID, std::string& rName)
{
    sal_uInt16 nHigh = pMyID->GetHigh();
    sal_uInt16 nLow = pMyID->GetLow();
    char pTempStr[32];
    rName = std::string("Gr");
    sprintf(pTempStr, "%X,%X", nHigh, nLow);
    rName.append(pTempStr);
}

/**
 * @descr   get the image data read from bento stream according to the VO_GRAPHIC ID.
 * @param   pGrafData   the array to store the image data. the pointer need to be deleted outside.
 * @return  the length of the image data.
 */
sal_uInt32 LwpGraphicObject::GetRawGrafData(sal_uInt8*& pGrafData)
{
    // create graphic object
    // if small file, use the compressed stream for BENTO
    LwpSvStream* pStream = m_pStrm->GetCompressedStream() ?  m_pStrm->GetCompressedStream(): m_pStrm;

    OpenStormBento::LtcBenContainer* pBentoContainer;
    sal_uLong ulRet = OpenStormBento::BenOpenContainer(pStream, &pBentoContainer);
    if (ulRet != OpenStormBento::BenErr_OK)
        return 0;

    SvStream* pGrafStream = NULL;

    // get graphic object's bento objet name
    LwpObjectID* pMyID = this->GetObjectID();
    std::string aGrfObjName;
    this->GetBentoNamebyID(pMyID,  aGrfObjName);

    // get bento stream by the name
    pBentoContainer->CreateGraphicStream(pGrafStream, aGrfObjName.c_str());
    SvMemoryStream* pMemGrafStream = static_cast<SvMemoryStream*>(pGrafStream);

    if (pMemGrafStream)
    {
        // read image data
        sal_uInt32 nDataLen = pMemGrafStream->GetEndOfData();
        pGrafData = new sal_uInt8 [nDataLen];
        pMemGrafStream->Read(pGrafData, nDataLen);

        delete pMemGrafStream;
        pMemGrafStream = NULL;

        return nDataLen;
    }

    return 0;
}

/**
 * @descr   get the image data (only -D dara) read from bento stream according to the VO_GRAPHIC ID.
 * @param   pGrafData   the array to store the image data. the pointer need to be deleted outside.
 * @return  the length of the image data.
 */
sal_uInt32 LwpGraphicObject::GetGrafData(sal_uInt8*& pGrafData)
{
    // create graphic object
    // if small file, use the compressed stream for BENTO
    LwpSvStream* pStream = m_pStrm->GetCompressedStream() ?  m_pStrm->GetCompressedStream(): m_pStrm;

    OpenStormBento::LtcBenContainer* pBentoContainer;
    sal_uLong ulRet = OpenStormBento::BenOpenContainer(pStream, &pBentoContainer);
    if (ulRet != OpenStormBento::BenErr_OK)
        return 0;

    SvStream* pGrafStream = NULL;

    // get graphic object's bento objet name
    LwpObjectID* pMyID = this->GetObjectID();
    std::string aGrfObjName;
    this->GetBentoNamebyID(pMyID,  aGrfObjName);

    char sDName[64]="";
    sprintf(sDName, "%s-D", aGrfObjName.c_str());

    // get bento stream by the name
    pGrafStream = pBentoContainer->FindValueStreamWithPropertyName(sDName);

    SvMemoryStream* pMemGrafStream = static_cast<SvMemoryStream*>(pGrafStream);

    if (pMemGrafStream)
    {
        // read image data
        sal_uInt32 nPos = pGrafStream->Tell();
        pGrafStream->Seek(STREAM_SEEK_TO_END);
        sal_uInt32 nDataLen = pGrafStream->Tell();
        pGrafStream->Seek(nPos);

        pGrafData = new sal_uInt8 [nDataLen];
        pMemGrafStream->Read(pGrafData, nDataLen);

        delete pMemGrafStream;
        pMemGrafStream = NULL;

        return nDataLen;
    }

    return 0;
}


/**
 * @descr   create xf-image object and save it in the container: m_vXFDrawObjects.
 */
void LwpGraphicObject::CreateGrafObject()
{

    XFImage* pImage = new XFImage();

    // set image processing styles
    XFImageStyle* pImageStyle = new XFImageStyle();
    if (m_sServerContextFormat[1]!='w' || m_sServerContextFormat[2]!='m' || m_sServerContextFormat[3]!='f')
    {
        if (m_aIPData.nBrightness != 50)
        {
            sal_Int32 nSODCBrightness = (sal_Int32)m_aIPData.nBrightness*2 - 100;
            pImageStyle->SetBrightness(nSODCBrightness);
        }
        if (m_aIPData.nContrast != 50)
        {
            //sal_Int32 nSODCContrast = (sal_Int32)m_aIPData.nContrast*2 - 100;
            //lwp [0, 100] map to sodc [80, -80]
            sal_Int32 nSODCContrast = (sal_Int32)(80 - (double)m_aIPData.nContrast*1.6);
            pImageStyle->SetContrast(nSODCContrast);
        }
    }

    // set scale and crop styles
    LwpAssociatedLayouts* pLayoutWithMe = GetLayoutsWithMe();
    LwpFrameLayout* pMyFrameLayout =
        static_cast<LwpFrameLayout*>(pLayoutWithMe->GetOnlyLayout()->obj(VO_FRAMELAYOUT));
    if (pMyFrameLayout)
    {
        LwpLayoutScale* pMyScale = pMyFrameLayout->GetLayoutScale();
        LwpLayoutGeometry* pFrameGeo = pMyFrameLayout->GetGeometry();

        // original image size
        double fOrgGrafWidth = (double)m_Cache.Width/TWIPS_PER_CM;
        double fOrgGrafHeight = (double)m_Cache.Height/TWIPS_PER_CM;

        // get margin values
        double fLeftMargin = pMyFrameLayout->GetMarginsValue(MARGIN_LEFT);
        double fRightMargin = pMyFrameLayout->GetMarginsValue(MARGIN_RIGHT);
        double fTopMargin = pMyFrameLayout->GetMarginsValue(MARGIN_TOP);
        double fBottomMargin = pMyFrameLayout->GetMarginsValue(MARGIN_BOTTOM);

        if (pMyScale && pFrameGeo)
        {
            // frame size
            double fFrameWidth = LwpTools::ConvertFromUnitsToMetric(pFrameGeo->GetWidth());
            double fFrameHeight = LwpTools::ConvertFromUnitsToMetric(pFrameGeo->GetHeight());

            // calculate the displayed size of the frame
            double fDisFrameWidth = fFrameWidth - (fLeftMargin+fRightMargin);
            double fDisFrameHeight = fFrameHeight - (fTopMargin+fBottomMargin);

            // scaled image size
            double fSclGrafWidth = fOrgGrafWidth;//LwpTools::ConvertFromUnitsToMetric(pMyScale->GetScaleWidth());
            double fSclGrafHeight = fOrgGrafHeight;//LwpTools::ConvertFromUnitsToMetric(pMyScale->GetScaleHeight());

            // get scale mode
            sal_uInt16 nScalemode = pMyScale->GetScaleMode();
            if (nScalemode & LwpLayoutScale::CUSTOM)
            {
                fSclGrafWidth = LwpTools::ConvertFromUnitsToMetric(pMyScale->GetScaleWidth());
                fSclGrafHeight = LwpTools::ConvertFromUnitsToMetric(pMyScale->GetScaleHeight());
            }
            else if (nScalemode & LwpLayoutScale::PERCENTAGE)
            {
                double fScalePercentage = (double)pMyScale->GetScalePercentage() / 1000;
                fSclGrafWidth = fScalePercentage * fOrgGrafWidth;
                fSclGrafHeight = fScalePercentage * fOrgGrafHeight;
            }
            else if (nScalemode & LwpLayoutScale::FIT_IN_FRAME)
            {
                if (pMyFrameLayout->IsFitGraphic())
                {
                    fSclGrafWidth = fOrgGrafWidth;
                    fSclGrafHeight = fOrgGrafHeight;
                }
                else if (nScalemode & LwpLayoutScale::MAINTAIN_ASPECT_RATIO)
                {
                    if (fOrgGrafWidth/fOrgGrafHeight >= fDisFrameWidth/fDisFrameHeight)
                    {
                        fSclGrafWidth = fDisFrameWidth;
                        fSclGrafHeight = (fDisFrameWidth/fOrgGrafWidth) * fOrgGrafHeight;
                    }
                    else
                    {
                        fSclGrafHeight = fDisFrameHeight;
                        fSclGrafWidth = (fDisFrameHeight/fOrgGrafHeight) * fOrgGrafWidth;
                    }
                }
                else
                {
                    fSclGrafWidth = fDisFrameWidth;
                    fSclGrafHeight = fDisFrameHeight;
                }
            }

            // scaled ratio
            double fXRatio = fSclGrafWidth / fOrgGrafWidth;
            double fYRatio = fSclGrafHeight / fOrgGrafHeight;

            // set image to scaled size.
            pImage->SetWidth(fSclGrafWidth);
            pImage->SetHeight(fSclGrafHeight);

            // placement:centered or tiled. tiled style is not supported so it's processed together with centered.
            if (pMyFrameLayout->GetScaleCenter() || pMyFrameLayout->GetScaleTile())
            {
                // set center alignment
                pImageStyle->SetXPosType(enumXFFrameXPosCenter, enumXFFrameXRelFrame);
                pImageStyle->SetYPosType(enumXFFrameYPosMiddle, enumXFFrameYRelFrame);

                // need horizontal crop?
                double fClipWidth = 0;
                double fClipHeight = 0;
                sal_Bool sal_bCropped = sal_False;
                if (fSclGrafWidth > fDisFrameWidth)
                {
                    fClipWidth = (fSclGrafWidth-fDisFrameWidth ) / 2 / fXRatio;
                    sal_bCropped = sal_True;
                }

                // need vertical crop?
                if (fSclGrafHeight > fDisFrameHeight)
                {
                    fClipHeight = (fSclGrafHeight-fDisFrameHeight ) / 2 / fYRatio;
                    sal_bCropped = sal_True;
                }

                if (sal_bCropped)
                {
                    pImageStyle->SetClip(fClipWidth, fClipWidth, fClipHeight, fClipHeight);
                    pImage->SetWidth(fDisFrameWidth);
                    pImage->SetHeight(fDisFrameHeight);
                }
            }
            // placement:automatic
            else
            {
                // set left-top alignment
//                  pImageStyle->SetXPosType(enumXFFrameXPosLeft, enumXFFrameXRelFrame);
//                  pImageStyle->SetYPosType(enumXFFrameYPosTop, enumXFFrameYRelFrame);
                pImageStyle->SetYPosType(enumXFFrameYPosFromTop, enumXFFrameYRelFrame);
                pImageStyle->SetXPosType(enumXFFrameXPosFromLeft, enumXFFrameXRelFrame);

                // get image position offset
                LwpPoint* pOffset = pMyScale->GetOffset();
                double fOffsetX = LwpTools::ConvertFromUnitsToMetric(pOffset->GetX());
                double fOffsetY = LwpTools::ConvertFromUnitsToMetric(pOffset->GetY());

                struct LwpRect
                {
                    double fLeft;
                    double fRight;
                    double fTop;
                    double fBottom;

                    LwpRect()
                    {
                        fLeft = 0.00;
                        fRight = 0.00;
                        fTop = 0.00;
                        fBottom = 0.00;
                    }
                    LwpRect(double fL, double fR, double fT, double fB)
                    {
                        fLeft = fL;
                        fRight = fR;
                        fTop = fT;
                        fBottom = fB;
                    }
                };
                LwpRect aFrameRect(-fOffsetX, (fDisFrameWidth-fOffsetX), (-fOffsetY), ((fDisFrameHeight-fOffsetY)));
                LwpRect aImageRect(0, fSclGrafWidth, 0, fSclGrafHeight);
                LwpRect aCropRect;

                if (aFrameRect.fRight <= aImageRect.fLeft || aFrameRect.fLeft >= aImageRect.fRight
                    ||aFrameRect.fBottom <= aImageRect.fTop|| aFrameRect.fTop >= aImageRect.fBottom)
                {
                    // display blank
                }
                else// need cropped
                {
                    // horizontal crop
                    if (aFrameRect.fLeft > aImageRect.fLeft)
                    {
                        aCropRect.fLeft = (aFrameRect.fLeft - aImageRect.fLeft) / fXRatio;
                    }

                    if (aFrameRect.fRight < aImageRect.fRight)
                    {
                        aCropRect.fRight = (aImageRect.fRight - aFrameRect.fRight) / fXRatio;
                    }

                    // vertical crop
                    if (aFrameRect.fTop > aImageRect.fTop)
                    {
                        aCropRect.fTop = (aFrameRect.fTop - aImageRect.fTop) / fYRatio;
                    }
                    if (aFrameRect.fBottom < aImageRect.fBottom)
                    {
                        aCropRect.fBottom = (aImageRect.fBottom - aFrameRect.fBottom) / fYRatio;
                    }

                    pImageStyle->SetClip(aCropRect.fLeft, aCropRect.fRight, aCropRect.fTop, aCropRect.fBottom);
                    double fPicWidth = fSclGrafWidth - (aCropRect.fLeft+aCropRect.fRight)*fXRatio;
                    double fPicHeight = fSclGrafHeight- (aCropRect.fTop+aCropRect.fBottom)*fYRatio;
                    double fX = fOffsetX > 0 ? fOffsetX : 0.00;
                    double fY = fOffsetY > 0 ? fOffsetY : 0.00;
                    pImage->SetPosition((fX+fLeftMargin), (fY+fTopMargin), fPicWidth, fPicHeight);
                }
            }
        }
    }

    // set style for the image
    XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
    pImage->SetStyleName(pXFStyleManager->AddStyle(pImageStyle)->GetStyleName());

    // set archor to frame
    pImage->SetAnchorType(enumXFAnchorFrame);
//      pImage->SetAnchorType(enumXFAnchorPara);//enumXFAnchorFrame);

    // set object name
    LwpAtomHolder* pHolder = this->GetName();
    if ( pHolder && pHolder->str().getLength() )
    {
        pImage->SetName(pHolder->str());
    }

    // insert image object into array
    m_vXFDrawObjects.push_back(pImage);

}
// end add

/**
 * @descr   Reserve the equation text in a note in the context.
 */
void LwpGraphicObject::XFConvertEquation(XFContentContainer * pCont)
{
    sal_uInt8* pGrafData = NULL;
    sal_uInt32 nDataLen = this->GetGrafData(pGrafData);
    if(pGrafData)
    {
        //convert equation
        XFParagraph* pXFPara = new XFParagraph;
        pXFPara->Add(A2OUSTR("Formula:"));
        //add notes
        XFAnnotation* pXFNote = new XFAnnotation;
        //add equation to comment notes
        XFParagraph* pXFNotePara = new XFParagraph;
        //equation header text: Times New Roman,
        //                                18,12,0,0,0,0,0.
        //                                 .TCIformat{2}
        //total head length = 45
        sal_uInt32 nBegin = 45;
        sal_uInt32 nEnd = nDataLen -1;
        sal_uInt32 nIndex = 0;

        if(pGrafData[nEnd] == '$' && pGrafData[nEnd-1]!= '\\')
        {
            //equation body is contained by '$';
            nBegin++;
            nEnd--;
        }

        if(nEnd >= nBegin)
        {
            sal_uInt8* pEquData = new sal_uInt8[nEnd - nBegin + 1];
            for(nIndex = 0; nIndex < nEnd - nBegin +1 ; nIndex++)
            {
                pEquData[nIndex] = pGrafData[nBegin + nIndex];
            }
            pXFNotePara->Add(rtl::OUString((sal_Char*)pEquData, (nEnd - nBegin + 1), gsl_getSystemTextEncoding()));
            delete [] pEquData;
        }
        pXFNote->Add(pXFNotePara);

        pXFPara->Add(pXFNote);
        pCont->Add(pXFPara);

        delete [] pGrafData;
        pGrafData = NULL;
    }

}

void LwpGraphicObject::GetGrafOrgSize(double & rWidth, double & rHeight)
{
    // original image size
    rWidth = (double)m_Cache.Width/TWIPS_PER_CM;
    rHeight = (double)m_Cache.Height/TWIPS_PER_CM;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */