00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "karbonaiparserbase.h"
00021
00022 #include <core/vcolor.h>
00023 #include <core/vlayer.h>
00024 #include <core/vgroup.h>
00025 #include <core/vclipgroup.h>
00026 #include <core/vvisitor.h>
00027 #include "aicolor.h"
00028 #include <qwmatrix.h>
00029 #include <commands/vtransformcmd.h>
00030
00031 #include <KoPageLayout.h>
00032
00033 const void pottoa (PathOutputType &data);
00034
00035
00036 KarbonAIParserBase::KarbonAIParserBase() : m_pot(POT_Other), m_ptt (PTT_Output), m_bbox(0,0,612,792), m_emptyFill(), m_emptyStroke()
00037 {
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 m_windingOrder = 0;
00051
00052 m_fm = FM_NonZero;
00053
00054 m_curKarbonPath = new VPath( 0L );
00055
00056 m_document = new VDocument();
00057 m_layer = NULL;
00058 m_combination = NULL;
00059
00060 m_emptyFill.setType (VFill::none);
00061 m_emptyStroke.setType (VStroke::none);
00062
00063 setupHandlers();
00064 }
00065
00066
00067 KarbonAIParserBase::~KarbonAIParserBase(){
00068 teardownHandlers();
00069 delete m_curKarbonPath;
00070
00071 delete m_document;
00072 }
00073
00074
00075 void KarbonAIParserBase::parsingStarted(){
00076
00077 }
00078
00079
00080 void KarbonAIParserBase::parsingFinished(){
00081
00082
00083
00084 if (m_document)
00085 {
00086 kdDebug() << "bbox 1 is " << m_bbox << endl;
00087
00088 if (m_bbox.width() > 0. )
00089 m_document->setWidth (m_bbox.width());
00090 if (m_bbox.height() > 0. )
00091 m_document->setHeight (m_bbox.height());
00092
00093
00094
00095
00096
00097
00098 VTranslateCmd cmd (0L, -m_bbox.x(), -m_bbox.y());
00099 m_document->accept (cmd);
00100 }
00101 }
00102
00103
00104 QString KarbonAIParserBase::getParamList(Parameters& params){
00105 QString data("");
00106
00107 Parameter *param;
00108
00109 if (params.count() > 0)
00110 {
00111
00112 for ( param=params.first(); param != 0; param=params.next() ) {
00113 data += " " + param->first + "=\"" + param->second + "\"";
00114 }
00115 }
00116
00117 return data;
00118 }
00119
00120
00121 void KarbonAIParserBase::gotStartTag (const char *tagName, Parameters& params){
00122 qDebug ("<%s%s>", tagName, getParamList (params).latin1() );
00123 }
00124
00125
00126 void KarbonAIParserBase::gotEndTag (const char *tagName){
00127 qDebug ("</%s>", tagName );
00128 }
00129
00130
00131 void KarbonAIParserBase::gotSimpleTag (const char *tagName, Parameters& params){
00132 qDebug ("<%s%s/>", tagName, getParamList (params).latin1() );
00133 }
00134
00135
00136 void KarbonAIParserBase::gotPathElement (PathElement &element){
00137 switch (element.petype)
00138 {
00139 case PET_MoveTo :
00140 m_curKarbonPath->moveTo (KoPoint (element.pevalue.pointdata.x,element.pevalue.pointdata.y));
00141 break;
00142 case PET_LineTo :
00143 m_curKarbonPath->lineTo (KoPoint (element.pevalue.pointdata.x,element.pevalue.pointdata.y));
00144 break;
00145 case PET_CurveTo :
00146 m_curKarbonPath->curveTo (KoPoint (element.pevalue.bezierdata.x1,element.pevalue.bezierdata.y1),
00147 KoPoint (element.pevalue.bezierdata.x2,element.pevalue.bezierdata.y2),
00148 KoPoint (element.pevalue.bezierdata.x3,element.pevalue.bezierdata.y3));
00149 break;
00150 case PET_CurveToOmitC1 :
00151 m_curKarbonPath->curve1To (KoPoint (element.pevalue.bezierdata.x2,element.pevalue.bezierdata.y2),
00152 KoPoint (element.pevalue.bezierdata.x3,element.pevalue.bezierdata.y3));
00153 break;
00154 case PET_CurveToOmitC2 :
00155 m_curKarbonPath->curve2To (KoPoint (element.pevalue.bezierdata.x1,element.pevalue.bezierdata.y1),
00156 KoPoint (element.pevalue.bezierdata.x3,element.pevalue.bezierdata.y3));
00157 break;
00158 }
00159 }
00160
00161
00162 void KarbonAIParserBase::gotFillPath (bool closed, bool reset, FillMode ){
00163
00164
00165
00166 if (closed) m_curKarbonPath->close();
00167
00168 if (!reset)
00169 m_pot = POT_Filled;
00170 else
00171 {
00172 doOutputCurrentPath2 (POT_Filled);
00173 m_pot = POT_Other;
00174 }
00175 }
00176
00177
00178 void KarbonAIParserBase::gotIgnorePath (bool closed, bool reset){
00179
00180
00181 if (closed) m_curKarbonPath->close();
00182
00183 if (! reset)
00184 m_pot = POT_Other;
00185 else
00186 {
00187 doOutputCurrentPath2 (POT_Ignore);
00188 m_pot = POT_Other;
00189 }
00190 }
00191
00192
00193 void KarbonAIParserBase::gotStrokePath (bool closed) {
00194
00195
00196 if (closed) m_curKarbonPath->close();
00197
00198 PathOutputType pot = POT_Stroked;
00199 if (m_pot != POT_Other)
00200 {
00201 pot = POT_FilledStroked;
00202 }
00203
00204 doOutputCurrentPath2 (pot);
00205
00206 m_pot = POT_Other;
00207 }
00208
00209
00210 void KarbonAIParserBase::gotClipPath (bool ){
00211
00212 doOutputCurrentPath2 (POT_Clip);
00213 }
00214
00215
00216 void KarbonAIParserBase::gotFillColor (AIColor &color){
00217
00218
00219
00220
00221
00222 VColor karbonColor = toKarbonColor (color);
00223 m_fill.setColor (karbonColor);
00224 }
00225
00226
00227 void KarbonAIParserBase::gotStrokeColor (AIColor &color){
00228
00229
00230
00231
00232
00233 VColor karbonColor = toKarbonColor (color);
00234 m_stroke.setColor (karbonColor);
00235 }
00236
00237
00238 void KarbonAIParserBase::gotBoundingBox (int llx, int lly, int urx, int ury){
00239
00240
00241
00242
00243 m_bbox.setCoords(llx,lly,urx,ury);
00244 }
00245
00246 void KarbonAIParserBase::gotLineWidth (double val){
00247
00248 m_stroke.setLineWidth (val);
00249 }
00250
00251 void KarbonAIParserBase::gotFlatness (double )
00252 {
00253
00254
00255 }
00256
00257 void KarbonAIParserBase::gotLineCaps (int val)
00258 {
00259
00260 VStroke::VLineCap lineCap = VStroke::capButt;
00261
00262 switch (val)
00263 {
00264 case 0 : lineCap = VStroke::capButt; break;
00265 case 1 : lineCap = VStroke::capRound; break;
00266 case 2 : lineCap = VStroke::capSquare; break;
00267 }
00268
00269 m_stroke.setLineCap (lineCap);
00270 }
00271
00272 void KarbonAIParserBase::gotLineJoin (int val)
00273 {
00274
00275
00276 VStroke::VLineJoin lineJoin = VStroke::joinMiter;
00277
00278 switch (val)
00279 {
00280 case 0 : lineJoin = VStroke::joinMiter; break;
00281 case 1 : lineJoin = VStroke::joinRound; break;
00282 case 2 : lineJoin = VStroke::joinBevel; break;
00283 }
00284
00285 m_stroke.setLineJoin (lineJoin);
00286
00287 }
00288
00289 void KarbonAIParserBase::gotMiterLimit (double val)
00290 {
00291
00292 m_stroke.setMiterLimit (val);
00293 }
00294
00295 void KarbonAIParserBase::gotWindingOrder (int val)
00296 {
00297 m_windingOrder = val;
00298 }
00299
00300 void KarbonAIParserBase::gotBeginGroup (bool clipping)
00301 {
00302
00303 if (clipping)
00304 {
00305 VClipGroup *group = new VClipGroup( 0L );
00306 m_groupStack.push (group);
00307 }
00308 else
00309 {
00310 VGroup *group = new VGroup( 0L );
00311 m_groupStack.push (group);
00312 }
00313
00314
00315
00316 }
00317
00318 void KarbonAIParserBase::gotEndGroup (bool )
00319 {
00320
00321
00322 if (m_debug) qDebug ("got end group");
00323
00324 if (m_groupStack.isEmpty()) return;
00325
00326 if (m_debug) qDebug ("got end group 2");
00327
00328 VGroup *group = m_groupStack.pop();
00329
00330 if (m_debug) qDebug ("got end group 3");
00331
00332 if (m_debug)
00333 {
00334 if (!group) qDebug ("group is NULL");
00335 }
00336
00337 if (m_groupStack.isEmpty())
00338 {
00339 if (m_debug) qDebug ("insert object");
00340 ensureLayer();
00341 m_layer->append (group);
00342 if (m_debug) qDebug ("/insert object");
00343 }
00344 else
00345 {
00346 if (m_debug) qDebug ("insert object to group");
00347
00348 m_groupStack.top()->append (group);
00349 if (m_debug) qDebug ("/insert object to group");
00350 }
00351
00352 if (m_debug) qDebug ("/got end group");
00353
00354
00355 }
00356
00357 void KarbonAIParserBase::gotBeginCombination () {
00358 m_ptt = PTT_Combine;
00359 }
00360
00361 void KarbonAIParserBase::gotEndCombination () {
00362
00363
00364 m_ptt = PTT_Output;
00365
00366 if (m_combination != NULL)
00367 {
00368 m_curKarbonPath = m_combination;
00369 doOutputCurrentPath2 (POT_Leave);
00370 }
00371
00372 m_combination = NULL;
00373 }
00374
00375
00376 const VColor KarbonAIParserBase::toKarbonColor (const AIColor &color)
00377 {
00378 AIColor temp (color);
00379 VColor value;
00380
00381 double v1, v2, v3, v4;
00382 temp.toCMYK (v1, v2, v3, v4);
00383
00384 float cv1 = v1;
00385 float cv2 = v2;
00386 float cv3 = v3;
00387 float cv4 = v4;
00388
00389 value.setColorSpace (VColor::cmyk);
00390 value.set (cv1, cv2, cv3, cv4);
00391
00392 return value;
00393 }
00394
00395 void KarbonAIParserBase::doOutputCurrentPath2(PathOutputType type)
00396 {
00397 ensureLayer();
00398
00399 if (type != POT_Leave)
00400 {
00401
00402
00403 m_curKarbonPath->setStroke(m_emptyStroke);
00404 m_curKarbonPath->setFill(m_emptyFill);
00405
00406 if ((type != POT_Filled) && (type != POT_Stroked) && (type != POT_FilledStroked)) return;
00407 if ((type == POT_Filled) || (type == POT_FilledStroked))
00408 {
00409
00410
00411
00412
00413 m_curKarbonPath->setFill(m_fill);
00414 }
00415
00416 if ((type == POT_Stroked) || (type == POT_FilledStroked))
00417 {
00418
00419
00420
00421
00422 m_curKarbonPath->setStroke (m_stroke);
00423 }
00424 }
00425
00426 if (m_ptt == PTT_Combine)
00427 {
00428
00429 if (m_combination == NULL)
00430 m_combination = m_curKarbonPath;
00431 else
00432 m_combination->combine (*m_curKarbonPath);
00433
00434 m_curKarbonPath = new VPath( 0L );
00435
00436 return;
00437 }
00438
00439 ensureLayer();
00440
00441 if (m_groupStack.isEmpty())
00442 {
00443 m_layer->append( m_curKarbonPath );
00444 }
00445 else
00446 {
00447 m_groupStack.top()->append( m_curKarbonPath );
00448 }
00449
00450 m_curKarbonPath = new VPath( 0L );
00451 }
00452
00453 bool KarbonAIParserBase::parse (QIODevice& fin, QDomDocument &doc)
00454 {
00455
00456 bool res = AIParserBase::parse (fin);
00457
00458
00459 if (res)
00460 {
00461 qDebug ("before save document");
00462 doc = m_document->saveXML();
00463
00464 QDomElement paper = doc.createElement( "PAPER" );
00465 doc.documentElement().appendChild( paper );
00466 paper.setAttribute( "format", PG_CUSTOM );
00467 paper.setAttribute( "width", m_document->width() );
00468 paper.setAttribute( "height", m_document->height() );
00469
00470 qDebug ("after save document");
00471 }
00472 else
00473 {
00474 QDomDocument tempDoc;
00475 doc = tempDoc;
00476 }
00477
00478 return res;
00479 }
00480
00481 void KarbonAIParserBase::ensureLayer ()
00482 {
00483 if (!m_layer)
00484 {
00485 m_layer = new VLayer( 0 );
00486 m_document->insertLayer (m_layer);
00487 }
00488 }
00489
00490
00491 void KarbonAIParserBase::setupHandlers()
00492 {
00493
00494 m_gstateHandler = new KarbonGStateHandler(this);
00495 m_structureHandler = new KarbonStructureHandler(this);
00496 m_pathHandler = new KarbonPathHandler(this);
00497 m_documentHandler = new KarbonDocumentHandler(this);
00498
00499 m_textHandler = new TextHandlerBase();
00500
00501 }
00502
00503 void KarbonAIParserBase::teardownHandlers()
00504 {
00505
00506 delete m_textHandler;
00507
00508 delete m_gstateHandler;
00509 delete m_structureHandler;
00510 delete m_pathHandler;
00511 delete m_documentHandler;
00512 }
00513
00514
00515 void KarbonDocumentHandler::gotBoundingBox (int llx, int lly, int urx, int ury)
00516 {
00517 delegate->gotBoundingBox(llx,lly,urx,ury);
00518 }
00519
00520 void KarbonDocumentHandler::gotCreationDate (const char *,const char *)
00521 {
00522
00523 }
00524
00525 void KarbonDocumentHandler::gotProcessColors (int )
00526 {
00527
00528
00529
00530
00531 }
00532
00533
00534 void KarbonGStateHandler::gotFillColor (AIColor &color)
00535 {
00536 delegate->gotFillColor (color);
00537 }
00538
00539 void KarbonGStateHandler::gotStrokeColor (AIColor &color)
00540 {
00541 delegate->gotStrokeColor(color);
00542 }
00543
00544 void KarbonGStateHandler::gotFlatness (double val)
00545 {
00546 delegate->gotFlatness(val);
00547 }
00548
00549 void KarbonGStateHandler::gotLineWidth (double val)
00550 {
00551 delegate->gotLineWidth(val);
00552 }
00553
00554 void KarbonGStateHandler::gotLineCaps (int val)
00555 {
00556 delegate->gotLineCaps(val);
00557 }
00558
00559 void KarbonGStateHandler::gotLineJoin (int val)
00560 {
00561 delegate->gotLineJoin(val);
00562 }
00563
00564 void KarbonGStateHandler::gotMiterLimit (double val)
00565 {
00566 delegate->gotMiterLimit(val);
00567 }
00568
00569 void KarbonGStateHandler::gotWindingOrder (int val)
00570 {
00571 delegate->gotWindingOrder(val);
00572 }
00573
00574 void KarbonStructureHandler::gotBeginGroup (bool clipping)
00575 {
00576 delegate->gotBeginGroup(clipping);
00577 }
00578
00579 void KarbonStructureHandler::gotEndGroup (bool clipping)
00580 {
00581 delegate->gotEndGroup(clipping);
00582 }
00583
00584 void KarbonStructureHandler::gotBeginCombination ()
00585 {
00586 delegate->gotBeginCombination();
00587 }
00588
00589 void KarbonStructureHandler::gotEndCombination ()
00590 {
00591 delegate->gotEndCombination();
00592 }
00593
00594 void KarbonPathHandler::gotPathElement (PathElement &element)
00595 {
00596 delegate->gotPathElement (element);
00597 }
00598
00599 void KarbonPathHandler::gotFillPath (bool closed, bool reset)
00600 {
00601 delegate->gotFillPath(closed, reset, m_fm);
00602 }
00603
00604 void KarbonPathHandler::gotFillMode (FillMode fm)
00605 {
00606 m_fm = fm;
00607 }
00608
00609 void KarbonPathHandler::gotStrokePath (bool closed)
00610 {
00611 delegate->gotStrokePath(closed);
00612 }
00613
00614 void KarbonPathHandler::gotIgnorePath (bool closed, bool reset)
00615 {
00616 delegate->gotIgnorePath(closed, reset);
00617 }
00618
00619 void KarbonPathHandler::gotClipPath (bool closed)
00620 {
00621 delegate->gotClipPath(closed);
00622 }
00623
00624 const void pottoa (PathOutputType &data)
00625 {
00626 switch (data)
00627 {
00628 case POT_Filled : qDebug ("filled"); break;
00629 case POT_Stroked : qDebug ("stroked"); break;
00630 case POT_FilledStroked : qDebug ("filled/stroked"); break;
00631 case POT_Clip : qDebug ("clip"); break;
00632 case POT_Ignore : qDebug ("ignore"); break;
00633 case POT_Leave : qDebug ("leave"); break;
00634 default : qDebug ("unknown");
00635 }
00636 }
00637