bump product version to 4.1.6.2
[LibreOffice.git] / filter / source / graphicfilter / idxf / dxfreprd.cxx
blob3ed02322f688123f1feaaa24dba1d85c0f09c736
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <string.h>
22 #include <dxfreprd.hxx>
25 //------------------DXFBoundingBox--------------------------------------------
28 void DXFBoundingBox::Union(const DXFVector & rVector)
30 if (bEmpty==sal_True) {
31 fMinX=rVector.fx;
32 fMinY=rVector.fy;
33 fMinZ=rVector.fz;
34 fMaxX=rVector.fx;
35 fMaxY=rVector.fy;
36 fMaxZ=rVector.fz;
37 bEmpty=sal_False;
39 else {
40 if (fMinX>rVector.fx) fMinX=rVector.fx;
41 if (fMinY>rVector.fy) fMinY=rVector.fy;
42 if (fMinZ>rVector.fz) fMinZ=rVector.fz;
43 if (fMaxX<rVector.fx) fMaxX=rVector.fx;
44 if (fMaxY<rVector.fy) fMaxY=rVector.fy;
45 if (fMaxZ<rVector.fz) fMaxZ=rVector.fz;
50 //------------------DXFPalette------------------------------------------------
53 DXFPalette::DXFPalette()
55 short i,j,nHue,nNSat,nVal,nC[3],nmax,nmed,nmin;
56 sal_uInt8 nV;
58 pRed =new sal_uInt8[256];
59 pGreen=new sal_uInt8[256];
60 pBlue =new sal_uInt8[256];
62 // colors 0 - 9 (normal colors)
63 SetColor(0, 0x00, 0x00, 0x00); // actually never being used
64 SetColor(1, 0xff, 0x00, 0x00);
65 SetColor(2, 0xff, 0xff, 0x00);
66 SetColor(3, 0x00, 0xff, 0x00);
67 SetColor(4, 0x00, 0xff, 0xff);
68 SetColor(5, 0x00, 0x00, 0xff);
69 SetColor(6, 0xff, 0x00, 0xff);
70 SetColor(7, 0x0f, 0x0f, 0x0f); // actually white???
71 SetColor(8, 0x80, 0x80, 0x80);
72 SetColor(9, 0xc0, 0xc0, 0xc0);
74 // colors 10 - 249
75 // (Universal-Palette: 24 hues * 5 lightnesses * 2 saturations )
76 i=10;
77 for (nHue=0; nHue<24; nHue++) {
78 for (nVal=5; nVal>=1; nVal--) {
79 for (nNSat=0; nNSat<2; nNSat++) {
80 nmax=((nHue+3)>>3)%3;
81 j=nHue-(nmax<<3); if (j>4) j=j-24;
82 if (j>=0) {
83 nmed=(nmax+1)%3;
84 nmin=(nmax+2)%3;
86 else {
87 nmed=(nmax+2)%3;
88 nmin=(nmax+1)%3;
89 j=-j;
91 nC[nmin]=0;
92 nC[nmed]=255*j/4;
93 nC[nmax]=255;
94 if (nNSat!=0) {
95 for (j=0; j<3; j++) nC[j]=(nC[j]>>1)+128;
97 for (j=0; j<3; j++) nC[j]=nC[j]*nVal/5;
98 SetColor((sal_uInt8)(i++),(sal_uInt8)nC[0],(sal_uInt8)nC[1],(sal_uInt8)nC[2]);
103 // Farben 250 - 255 (shades of gray)
104 for (i=0; i<6; i++) {
105 nV=(sal_uInt8)(i*38+65);
106 SetColor((sal_uInt8)(250+i),nV,nV,nV);
111 DXFPalette::~DXFPalette()
113 delete[] pBlue;
114 delete[] pGreen;
115 delete[] pRed;
119 void DXFPalette::SetColor(sal_uInt8 nIndex, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
121 pRed[nIndex]=nRed;
122 pGreen[nIndex]=nGreen;
123 pBlue[nIndex]=nBlue;
127 //------------------DXFRepresentation-----------------------------------------
130 DXFRepresentation::DXFRepresentation()
132 setTextEncoding(RTL_TEXTENCODING_IBM_437);
133 setGlobalLineTypeScale(1.0);
137 DXFRepresentation::~DXFRepresentation()
142 sal_Bool DXFRepresentation::Read( SvStream & rIStream, sal_uInt16 nMinPercent, sal_uInt16 nMaxPercent)
144 DXFGroupReader * pDGR;
145 sal_Bool bRes;
147 aTables.Clear();
148 aBlocks.Clear();
149 aEntities.Clear();
151 pDGR = new DXFGroupReader( rIStream, nMinPercent, nMaxPercent );
153 pDGR->Read();
154 while (pDGR->GetG()!=0 || strcmp(pDGR->GetS(),"EOF")!=0) {
155 if (pDGR->GetG()==0 && strcmp(pDGR->GetS(),"SECTION")==0) {
156 if (pDGR->Read()!=2) {
157 pDGR->SetError();
158 break;
160 if (strcmp(pDGR->GetS(),"HEADER" )==0) ReadHeader(*pDGR);
161 else if (strcmp(pDGR->GetS(),"TABLES" )==0) aTables.Read(*pDGR);
162 else if (strcmp(pDGR->GetS(),"BLOCKS" )==0) aBlocks.Read(*pDGR);
163 else if (strcmp(pDGR->GetS(),"ENTITIES")==0) aEntities.Read(*pDGR);
164 else pDGR->Read();
166 else pDGR->Read();
169 bRes=pDGR->GetStatus();
171 delete pDGR;
173 if (bRes==sal_True && aBoundingBox.bEmpty==sal_True)
174 CalcBoundingBox(aEntities,aBoundingBox);
176 return bRes;
180 void DXFRepresentation::ReadHeader(DXFGroupReader & rDGR)
183 while (rDGR.GetG()!=0 || (strcmp(rDGR.GetS(),"EOF")!=0 && strcmp(rDGR.GetS(),"ENDSEC")!=0) )
185 if (rDGR.GetG()==9) {
186 if (strcmp(rDGR.GetS(),"$EXTMIN")==0 ||
187 strcmp(rDGR.GetS(),"$EXTMAX")==0)
189 DXFVector aVector;
190 rDGR.SetF(10,0.0);
191 rDGR.SetF(20,0.0);
192 rDGR.SetF(30,0.0);
193 do {
194 rDGR.Read();
195 } while (rDGR.GetG()!=9 && rDGR.GetG()!=0);
196 aVector.fx=rDGR.GetF(10);
197 aVector.fy=rDGR.GetF(20);
198 aVector.fz=rDGR.GetF(30);
199 aBoundingBox.Union(aVector);
200 } else {
201 if (strcmp(rDGR.GetS(),"$DWGCODEPAGE")==0)
203 rDGR.Read();
205 // FIXME: we really need a whole table of
206 // $DWGCODEPAGE to encodings mappings
207 if ( (strcmp(rDGR.GetS(),"ANSI_932")==0) ||
208 (strcmp(rDGR.GetS(),"ansi_932")==0) ||
209 (strcmp(rDGR.GetS(),"DOS932")==0) ||
210 (strcmp(rDGR.GetS(),"dos932")==0) )
212 setTextEncoding(RTL_TEXTENCODING_MS_932);
215 else if (strcmp(rDGR.GetS(),"$LTSCALE")==0)
217 rDGR.Read();
218 setGlobalLineTypeScale(getGlobalLineTypeScale() * rDGR.GetF());
220 else rDGR.Read();
223 else rDGR.Read();
228 void DXFRepresentation::CalcBoundingBox(const DXFEntities & rEntities,
229 DXFBoundingBox & rBox)
231 DXFBasicEntity * pBE=rEntities.pFirst;
232 while (pBE!=NULL) {
233 switch (pBE->eType) {
234 case DXF_LINE: {
235 const DXFLineEntity * pE = (DXFLineEntity*)pBE;
236 rBox.Union(pE->aP0);
237 rBox.Union(pE->aP1);
238 break;
240 case DXF_POINT: {
241 const DXFPointEntity * pE = (DXFPointEntity*)pBE;
242 rBox.Union(pE->aP0);
243 break;
245 case DXF_CIRCLE: {
246 const DXFCircleEntity * pE = (DXFCircleEntity*)pBE;
247 DXFVector aP;
248 aP=pE->aP0;
249 aP.fx-=pE->fRadius;
250 aP.fy-=pE->fRadius;
251 rBox.Union(aP);
252 aP=pE->aP0;
253 aP.fx+=pE->fRadius;
254 aP.fy+=pE->fRadius;
255 rBox.Union(aP);
256 break;
258 case DXF_ARC: {
259 const DXFArcEntity * pE = (DXFArcEntity*)pBE;
260 DXFVector aP;
261 aP=pE->aP0;
262 aP.fx-=pE->fRadius;
263 aP.fy-=pE->fRadius;
264 rBox.Union(aP);
265 aP=pE->aP0;
266 aP.fx+=pE->fRadius;
267 aP.fy+=pE->fRadius;
268 rBox.Union(aP);
269 break;
271 case DXF_TRACE: {
272 const DXFTraceEntity * pE = (DXFTraceEntity*)pBE;
273 rBox.Union(pE->aP0);
274 rBox.Union(pE->aP1);
275 rBox.Union(pE->aP2);
276 rBox.Union(pE->aP3);
277 break;
279 case DXF_SOLID: {
280 const DXFSolidEntity * pE = (DXFSolidEntity*)pBE;
281 rBox.Union(pE->aP0);
282 rBox.Union(pE->aP1);
283 rBox.Union(pE->aP2);
284 rBox.Union(pE->aP3);
285 break;
287 case DXF_TEXT: {
288 //const DXFTextEntity * pE = (DXFTextEntity*)pBE;
289 //???
290 break;
292 case DXF_SHAPE: {
293 //const DXFShapeEntity * pE = (DXFShapeEntity*)pBE;
294 //???
295 break;
297 case DXF_INSERT: {
298 const DXFInsertEntity * pE = (DXFInsertEntity*)pBE;
299 DXFBlock * pB;
300 DXFBoundingBox aBox;
301 DXFVector aP;
302 pB=aBlocks.Search(pE->m_sName);
303 if (pB==NULL) break;
304 CalcBoundingBox(*pB,aBox);
305 if (aBox.bEmpty==sal_True) break;
306 aP.fx=(aBox.fMinX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
307 aP.fy=(aBox.fMinY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
308 aP.fz=(aBox.fMinZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
309 rBox.Union(aP);
310 aP.fx=(aBox.fMaxX-pB->aBasePoint.fx)*pE->fXScale+pE->aP0.fx;
311 aP.fy=(aBox.fMaxY-pB->aBasePoint.fy)*pE->fYScale+pE->aP0.fy;
312 aP.fz=(aBox.fMaxZ-pB->aBasePoint.fz)*pE->fZScale+pE->aP0.fz;
313 rBox.Union(aP);
314 break;
316 case DXF_ATTDEF: {
317 //const DXFAttDefEntity * pE = (DXFAttDefEntity*)pBE;
318 //???
319 break;
321 case DXF_ATTRIB: {
322 //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
323 //???
324 break;
326 case DXF_VERTEX: {
327 const DXFVertexEntity * pE = (DXFVertexEntity*)pBE;
328 rBox.Union(pE->aP0);
329 break;
331 case DXF_3DFACE: {
332 const DXF3DFaceEntity * pE = (DXF3DFaceEntity*)pBE;
333 rBox.Union(pE->aP0);
334 rBox.Union(pE->aP1);
335 rBox.Union(pE->aP2);
336 rBox.Union(pE->aP3);
337 break;
339 case DXF_DIMENSION: {
340 const DXFDimensionEntity * pE = (DXFDimensionEntity*)pBE;
341 DXFBlock * pB;
342 DXFBoundingBox aBox;
343 DXFVector aP;
344 pB = aBlocks.Search(pE->m_sPseudoBlock);
345 if (pB==NULL) break;
346 CalcBoundingBox(*pB,aBox);
347 if (aBox.bEmpty==sal_True) break;
348 aP.fx=aBox.fMinX-pB->aBasePoint.fx;
349 aP.fy=aBox.fMinY-pB->aBasePoint.fy;
350 aP.fz=aBox.fMinZ-pB->aBasePoint.fz;
351 rBox.Union(aP);
352 aP.fx=aBox.fMaxX-pB->aBasePoint.fx;
353 aP.fy=aBox.fMaxY-pB->aBasePoint.fy;
354 aP.fz=aBox.fMaxZ-pB->aBasePoint.fz;
355 rBox.Union(aP);
356 break;
358 case DXF_POLYLINE: {
359 //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
360 //???
361 break;
363 case DXF_SEQEND: {
364 //const DXFAttribEntity * pE = (DXFAttribEntity*)pBE;
365 //???
366 break;
368 case DXF_HATCH :
369 break;
370 case DXF_LWPOLYLINE :
371 break;
373 pBE=pBE->pSucc;
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */