summaryrefslogtreecommitdiffstats
path: root/filter/source/graphicfilter/idxf/dxfreprd.cxx
blob: 93847e53acc4e74e5b6404bc6f618e02d58cc4e9 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */


#include <string.h>
#include "dxfreprd.hxx"
#include "osl/nlsupport.h"
#include "officecfg/Setup.hxx"
#include "officecfg/Office/Linguistic.hxx"
#include "unotools/wincodepage.hxx"

//------------------DXFBoundingBox--------------------------------------------


void DXFBoundingBox::Union(const DXFVector & rVector)
{
    if (bEmpty) {
        fMinX=rVector.fx;
        fMinY=rVector.fy;
        fMinZ=rVector.fz;
        fMaxX=rVector.fx;
        fMaxY=rVector.fy;
        fMaxZ=rVector.fz;
        bEmpty=false;
    }
    else {
        if (fMinX>rVector.fx) fMinX=rVector.fx;
        if (fMinY>rVector.fy) fMinY=rVector.fy;
        if (fMinZ>rVector.fz) fMinZ=rVector.fz;
        if (fMaxX<rVector.fx) fMaxX=rVector.fx;
        if (fMaxY<rVector.fy) fMaxY=rVector.fy;
        if (fMaxZ<rVector.fz) fMaxZ=rVector.fz;
    }
}


//------------------DXFPalette------------------------------------------------


DXFPalette::DXFPalette()
{
    short i,j,nHue,nNSat,nVal,nC[3],nmax,nmed,nmin;
    sal_uInt8 nV;

    pRed  =new sal_uInt8[256];
    pGreen=new sal_uInt8[256];
    pBlue =new sal_uInt8[256];

    // colors 0 - 9 (normal colors)
    SetColor(0, 0x00, 0x00, 0x00); // actually never being used
    SetColor(1, 0xff, 0x00, 0x00);
    SetColor(2, 0xff, 0xff, 0x00);
    SetColor(3, 0x00, 0xff, 0x00);
    SetColor(4, 0x00, 0xff, 0xff);
    SetColor(5, 0x00, 0x00, 0xff);
    SetColor(6, 0xff, 0x00, 0xff);
    SetColor(7, 0x0f, 0x0f, 0x0f); // actually white???
    SetColor(8, 0x80, 0x80, 0x80);
    SetColor(9, 0xc0, 0xc0, 0xc0);

    // colors 10 - 249
    // (Universal-Palette: 24 hues * 5 lightnesses * 2 saturations )
    i=10;
    for (nHue=0; nHue<24; nHue++) {
        for (nVal=5; nVal>=1; nVal--) {
            for (nNSat=0; nNSat<2; nNSat++) {
                nmax=((nHue+3)>>3)%3;
                j=nHue-(nmax<<3); if (j>4) j=j-24;
                if (j>=0) {
                    nmed=(nmax+1)%3;
                    nmin=(nmax+2)%3;
                }
                else {
                    nmed=(nmax+2)%3;
                    nmin=(nmax+1)%3;
                    j=-j;
                }
                nC[nmin]=0;
                nC[nmed]=255*j/4;
                nC[nmax]=255;
                if (nNSat!=0) {
                    for (j=0; j<3; j++) nC[j]=(nC[j]>>1)+128;
                }
                for (j=0; j<3; j++) nC[j]=nC[j]*nVal/5;
                SetColor((sal_uInt8)(i++),(sal_uInt8)nC[0],(sal_uInt8)nC[1],(sal_uInt8)nC[2]);
            }
        }
    }

    // Farben 250 - 255 (shades of gray)
    for (i=0; i<6; i++) {
        nV=(sal_uInt8)(i*38+65);
        SetColor((sal_uInt8)(250+i),nV,nV,nV);
    }
}


DXFPalette::~DXFPalette()
{
    delete[] pBlue;
    delete[] pGreen;
    delete[] pRed;
}


void DXFPalette::SetColor(sal_uInt8 nIndex, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
{
    pRed[nIndex]=nRed;
    pGreen[nIndex]=nGreen;
    pBlue[nIndex]=nBlue;
}


//------------------DXFRepresentation-----------------------------------------


DXFRepresentation::DXFRepresentation()
    : mEnc(RTL_TEXTENCODING_DONTKNOW)
    , mbInCalc(false)
{
    setGlobalLineTypeScale(1.0);
}

DXFRepresentation::~DXFRepresentation()
{
}

namespace {

OUString getLODefaultLanguage()
{
    OUString result(officecfg::Office::Linguistic::General::DefaultLocale::get());
    if (result.isEmpty())
        result = officecfg::Setup::L10N::ooSetupSystemLocale::get();
    return result;
}

}

rtl_TextEncoding DXFRepresentation::getTextEncoding() const
{
    return (isTextEncodingSet()) ?
        mEnc :
        osl_getTextEncodingFromLocale(nullptr); // Use default encoding if none specified
}

bool DXFRepresentation::Read( SvStream & rIStream, sal_uInt16 /*nMinPercent*/, sal_uInt16 /*nMaxPercent*/)
{
    bool bRes;

    aTables.Clear();
    aBlocks.Clear();
    aEntities.Clear();

    DXFGroupReader DGR( rIStream );

    DGR.Read();
    while (DGR.GetG()!=0 || (DGR.GetS() != "EOF")) {
        if (DGR.GetG()==0 && DGR.GetS() == "SECTION") {
            if (DGR.Read()!=2) {
                DGR.SetError();
                break;
            }
            if      (DGR.GetS() == "HEADER")   ReadHeader(DGR);
            else if (DGR.GetS() == "TABLES")   aTables.Read(DGR);
            else if (DGR.GetS() == "BLOCKS")   aBlocks.Read(DGR);
            else if (DGR.GetS() == "ENTITIES") aEntities.Read(DGR);
            else DGR.Read();
        }
        else DGR.Read();
    }

    bRes=DGR.GetStatus();

    if (bRes && aBoundingBox.bEmpty)
        CalcBoundingBox(aEntities,aBoundingBox);

    return bRes;
}

void DXFRepresentation::ReadHeader(DXFGroupReader & rDGR)
{
    while (rDGR.GetG()!=0 || (rDGR.GetS() != "EOF" && rDGR.GetS() != "ENDSEC") )
    {
        if (rDGR.GetG()==9) {
            if (rDGR.GetS() == "$EXTMIN" ||
                rDGR.GetS() == "$EXTMAX")
            {
                DXFVector aVector;
                while (rDGR.Read()!=9 && rDGR.GetG()!=0) {
                    switch (rDGR.GetG()) {
                    case 10: aVector.fx = rDGR.GetF(); break;
                    case 20: aVector.fy = rDGR.GetF(); break;
                    case 30: aVector.fz = rDGR.GetF(); break;
                    }
                }
                aBoundingBox.Union(aVector);
            }
            else if (rDGR.GetS() == "$ACADVER")
            {
                if (!rDGR.Read(1))
                    continue;
                // Versions of AutoCAD up to Release 12 (inclusive, AC1009)
                // were DOS software and used OEM encoding for storing strings.
                // Release 13 (AC1012) had both DOS and Windows variants.
                // Its Windows variant, and later releases used ANSI encodings for
                // strings (up to version 2006, which was the last one to do so).
                // Later versions (2007+, AC1021+) use UTF-8 for that.
                // Other (non-Autodesk) implementations may have used different
                // encodings for storing to corresponding formats, but there's
                // no way to know that.
                // See http://autodesk.blogs.com/between_the_lines/autocad-release-history.html
                if ((rDGR.GetS() <= "AC1009") || (rDGR.GetS() == "AC2.22") || (rDGR.GetS() == "AC2.21") || (rDGR.GetS() == "AC2.10") ||
                    (rDGR.GetS() == "AC1.50") || (rDGR.GetS() == "AC1.40") || (rDGR.GetS() == "AC1.2")  || (rDGR.GetS() == "MC0.0"))
                {
                    // Set OEM encoding for old DOS formats
                    // only if the encoding is not set yet
                    // e.g. by previous $DWGCODEPAGE
                    if (!isTextEncodingSet())
                        setTextEncoding(utl_getWinTextEncodingFromLangStr(getLODefaultLanguage().toUtf8().getStr(), true));
                }
                else if (rDGR.GetS() >= "AC1021")
                    setTextEncoding(RTL_TEXTENCODING_UTF8);
                else
                {
                    // Set ANSI encoding for old Windows formats
                    // only if the encoding is not set yet
                    // e.g. by previous $DWGCODEPAGE
                    if (!isTextEncodingSet())
                        setTextEncoding(utl_getWinTextEncodingFromLangStr(getLODefaultLanguage().toUtf8().getStr()));
                }
            }
            else if (rDGR.GetS() == "$DWGCODEPAGE")
            {
                if (!rDGR.Read(3))
                    continue;

                // If we already use UTF8, then don't update encoding anymore
                if (mEnc == RTL_TEXTENCODING_UTF8)
                    continue;
                // FIXME: we really need a whole table of
                // $DWGCODEPAGE to encodings mappings
                else if ( (rDGR.GetS().equalsIgnoreAsciiCase("ANSI_932")) ||
                          (rDGR.GetS().equalsIgnoreAsciiCase("DOS932")) )
                {
                    setTextEncoding(RTL_TEXTENCODING_MS_932);
                }
                else if (rDGR.GetS().equalsIgnoreAsciiCase("ANSI_936"))
                {
                    setTextEncoding(RTL_TEXTENCODING_MS_936);
                }
                else if (rDGR.GetS().equalsIgnoreAsciiCase("ANSI_949"))
                {
                    setTextEncoding(RTL_TEXTENCODING_MS_949);
                }
                else if (rDGR.GetS().equalsIgnoreAsciiCase("ANSI_950"))
                {
                    setTextEncoding(RTL_TEXTENCODING_MS_950);
                }
                else if (rDGR.GetS().equalsIgnoreAsciiCase("ANSI_1251"))
                {
                    setTextEncoding(RTL_TEXTENCODING_MS_1251);
                }
            }
            else if (rDGR.GetS() == "$LTSCALE")
            {
                if (!rDGR.Read(40))
                    continue;
                setGlobalLineTypeScale(getGlobalLineTypeScale() * rDGR.GetF());
            }
            else rDGR.Read();
        }
        else rDGR.Read();
    }
}

void DXFRepresentation::CalcBoundingBox(const DXFEntities & rEntities,
                                        DXFBoundingBox & rBox)
{
    if (mbInCalc)
        return;
    mbInCalc = true;

    DXFBasicEntity * pBE=rEntities.pFirst;
    while (pBE!=nullptr) {
        switch (pBE->eType) {
            case DXF_LINE: {
                const DXFLineEntity * pE = static_cast<const DXFLineEntity*>(pBE);
                rBox.Union(pE->aP0);
                rBox.Union(pE->aP1);
                break;
            }
            case DXF_POINT: {
                const DXFPointEntity * pE = static_cast<const DXFPointEntity*>(pBE);
                rBox.Union(pE->aP0);
                break;
            }
            case DXF_CIRCLE: {
                const DXFCircleEntity * pE = static_cast<const DXFCircleEntity*>(pBE);
                DXFVector aP;
                aP=pE->aP0;
                aP.fx-=pE->fRadius;
                aP.fy-=pE->fRadius;
                rBox.Union(aP);
                aP=pE->aP0;
                aP.fx+=pE->fRadius;
                aP.fy+=pE->fRadius;
                rBox.Union(aP);
                break;
            }
            case DXF_ARC: {
                const DXFArcEntity * pE = static_cast<const DXFArcEntity*>(pBE);
                DXFVector aP;
                aP=pE->aP0;
                aP.fx-=pE->fRadius;
                aP.fy-=pE->fRadius;
                rBox.Union(aP);
                aP=pE->aP0;
                aP.fx+=pE->fRadius;
                aP.fy+=pE->fRadius;
                rBox.Union(aP);
                break;
            }
            case DXF_TRACE: {
                const DXFTraceEntity * pE = static_cast<const DXFTraceEntity*>(pBE);
                rBox.Union(pE->aP0);
                rBox.Union(pE->aP1);
                rBox.Union(pE->aP2);
                rBox.Union(pE->aP3);
                break;
            }
            case DXF_SOLID: {
                const DXFSolidEntity * pE = static_cast<const DXFSolidEntity*>(pBE);
                rBox.Union(pE->aP0);
                rBox.Union(pE->aP1);
                rBox.Union(pE->aP2);
                rBox.Union(pE->aP3);
                break;
            }
            case DXF_TEXT: {
                //const DXFTextEntity * pE = (DXFTextEntity*)pBE;
                //???
                break;
            }
            case DXF_SHAPE: {
                //const DXFShapeEntity * pE = (DXFShapeEntity*)pBE;
                //???
                break;
            }
            case DXF_INSERT: {
                const DXFInsertEntity * pE = static_cast<const DXFInsertEntity*>(pBE);
                DXFBlock * pB;
                DXFBoundingBox aBox;
                DXFVector aP;
                pB=aBlocks.Search(pE->m_sName);
                if (pB==nullptr) break;
                CalcBoundingBox(*pB,aBox);
                if (aBox.bEmpty) break;
                aP.fx=(aBox.fMinX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
                aP.fy=(aBox.fMinY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
                aP.fz=(aBox.fMinZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
                rBox.Union(aP);
                aP.fx=(aBox.fMaxX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
                aP.fy=(aBox.fMaxY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
                aP.fz=(aBox.fMaxZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
                rBox.Union(aP);
                break;
            }
            case DXF_ATTDEF: {
                //const DXFAttDefEntity * pE = (DXFAttDefEntity*)pBE;
                //???
                break;
            }
            case DXF_ATTRIB: {
                //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
                //???
                break;
            }
            case DXF_VERTEX: {
                const DXFVertexEntity * pE = static_cast<const DXFVertexEntity*>(pBE);
                rBox.Union(pE->aP0);
                break;
            }
            case DXF_3DFACE: {
                const DXF3DFaceEntity * pE = static_cast<const DXF3DFaceEntity*>(pBE);
                rBox.Union(pE->aP0);
                rBox.Union(pE->aP1);
                rBox.Union(pE->aP2);
                rBox.Union(pE->aP3);
                break;
            }
            case DXF_DIMENSION: {
                const DXFDimensionEntity * pE = static_cast<const DXFDimensionEntity*>(pBE);
                DXFBlock * pB;
                DXFBoundingBox aBox;
                DXFVector aP;
                pB = aBlocks.Search(pE->m_sPseudoBlock);
                if (pB==nullptr) break;
                CalcBoundingBox(*pB,aBox);
                if (aBox.bEmpty) break;
                aP.fx=aBox.fMinX-pB->aBasePoint.fx;
                aP.fy=aBox.fMinY-pB->aBasePoint.fy;
                aP.fz=aBox.fMinZ-pB->aBasePoint.fz;
                rBox.Union(aP);
                aP.fx=aBox.fMaxX-pB->aBasePoint.fx;
                aP.fy=aBox.fMaxY-pB->aBasePoint.fy;
                aP.fz=aBox.fMaxZ-pB->aBasePoint.fz;
                rBox.Union(aP);
                break;
            }
            case DXF_POLYLINE: {
                //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
                //???
                break;
            }
            case DXF_SEQEND: {
                //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
                //???
                break;
            }
            case DXF_HATCH :
                break;
            case DXF_LWPOLYLINE :
                break;
        }
        pBE=pBE->pSucc;
    }
    mbInCalc = false;
}

namespace {
    inline bool lcl_isDec(sal_Unicode ch)
    {
        return ch >= L'0' && ch <= L'9';
    }
    inline bool lcl_isHex(sal_Unicode ch)
    {
        return lcl_isDec(ch) || (ch >= L'A' && ch <= L'F') || (ch >= L'a' && ch <= L'f');
    }
}

OUString DXFRepresentation::ToOUString(const OString& s) const
{
    OUString result = OStringToOUString(s, getTextEncoding());
    result = result.replaceAll("%%o", "")                     // Overscore - simply remove
                   .replaceAll("%%u", "")                     // Underscore - simply remove
                   .replaceAll("%%d", OUStringLiteral1(0x00B0)) // Degrees symbol (°)
                   .replaceAll("%%p", OUStringLiteral1(0x00B1)) // Tolerance symbol (±)
                   .replaceAll("%%c", OUStringLiteral1(0x2205)) // Diameter symbol
                   .replaceAll("%%%", "%");                   // Percent symbol

    sal_Int32 pos = result.indexOf("%%"); // %%nnn, where nnn - 3-digit decimal ASCII code
    while (pos != -1 && pos <= result.getLength() - 5) {
        OUString asciiNum = result.copy(pos + 2, 3);
        if (lcl_isDec(asciiNum[0]) &&
            lcl_isDec(asciiNum[1]) &&
            lcl_isDec(asciiNum[2]))
        {
            char ch = static_cast<char>(asciiNum.toUInt32());
            OUString codePt(&ch, 1, mEnc);
            result = result.replaceAll(result.copy(pos, 5), codePt, pos);
        }
        pos = result.indexOf("%%", pos + 1);
    }

    pos = result.indexOf("\\U+"); // \U+XXXX, where XXXX - 4-digit hex unicode
    while (pos != -1 && pos <= result.getLength() - 7) {
        OUString codePtNum = result.copy(pos + 3, 4);
        if (lcl_isHex(codePtNum[0]) &&
            lcl_isHex(codePtNum[1]) &&
            lcl_isHex(codePtNum[2]) &&
            lcl_isHex(codePtNum[3]))
        {
            OUString codePt(static_cast<sal_Unicode>(codePtNum.toUInt32(16)));
            result = result.replaceAll(result.copy(pos, 7), codePt, pos);
        }
        pos = result.indexOf("\\U+", pos + 1);
    }
    return result;
}

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